keep-loving-pythonのブログ

Pythonを愛し続けたいです(Pythonが流行っている限りですが。。。)

解決策。NotImplementedError: Cannot convert a symbolic tf.Tensor (ArgMax:0) to a numpy array.

解決策。NotImplementedError: Cannot convert a symbolic tf.Tensor (ArgMax:0) to a numpy array.

エラーの内容

NotImplementedError: Cannot convert a symbolic tf.Tensor (ArgMax:0) to a numpy array.

エラー詳細。


Traceback (most recent call last):
  File "tf_trans.py", line 54, in <module>
    predicted_class_name = imagenet_labels[predicted_class]
  File "C:\Users\XYZZ0\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\ops.py", line 927, in __array__
    f"Cannot convert a symbolic tf.Tensor ({self.name}) to a numpy array."
NotImplementedError: Cannot convert a symbolic tf.Tensor (ArgMax:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported.


解決策

原因は、誤って、以下の一行が入っていたため。

tf.compat.v1.disable_eager_execution() 

申し遅れましたが。。。。環境としては、

tensorflow                        2.9.1

tensorflow-hub                    0.12.0

です。

コメント

コメントなどあれば、お願いします。

解決策。TypeError: Variable is unhashable. Instead, use variable.ref() as the key.

解決策。TypeError: Variable is unhashable. Instead, use variable.ref() as the key.

エラーの内容

TypeError: Variable is unhashable. Instead, use variable.ref() as the key.

エラー詳細

Traceback (most recent call last):
  File "tf_trans.py", line 28, in <module>
    hub.KerasLayer(classifier_model, input_shape=IMAGE_SHAPE+(3,))
  File "C:\Users\XYZZ0\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_hub\keras_layer.py", line 119, in __init__
    trainable_variables = set(self._func.trainable_variables)
  File "C:\Users\XYZZ0\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\ops\variables.py", line 1082, in __hash__
    "Variable is unhashable. "
TypeError: Variable is unhashable. Instead, use variable.ref() as the key. (Variable: <tf.Variable 'MobilenetV2/expanded_conv_14/depthwise/depthwise_weights:0' shape=(3, 3, 960, 1) dtype=float32, numpy=

解決策

tensorflow-hubが古いのが原因でした。 修正前。

tensorflow                        2.9.1

tensorflow-hub                    0.5.0   <---えらく古い!!!

解決したバージョン

tensorflow                        2.9.1

tensorflow-hub                    0.12.0  <---2022/08/20 Windows10にて。

コメント

もしも、この情報が、役立ったら、、、、コメントとかいいねとか宜しくーーー。 シンプルに検索にかかるようにしておきたい、、、この記事は。

解決策。tensorflow.lite.python.convert_phase.ConverterError: bad allocation

解決策。tensorflow.lite.python.convert_phase.ConverterError: bad allocation

エラー

tensorflow.lite.python.convert_phase.ConverterError: bad allocation

エラーの全体

Traceback (most recent call last):
  File "tf00.py", line 81, in <module>
    tflite_quant_model = converter.convert()
  File "C:\Users\XYZZ0\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\lite\python\lite.py", line 929, in wrapper
    return self._convert_and_export_metrics(convert_func, *args, **kwargs)
  File "C:\Users\XYZZ0\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\lite\python\lite.py", line 908, in _convert_and_export_metrics
    result = convert_func(self, *args, **kwargs)
  File "C:\Users\XYZZ0\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\lite\python\lite.py", line 1212, in convert
    return self._convert_from_saved_model(graph_def)
  File "C:\Users\XYZZ0\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\lite\python\lite.py", line 1095, in _convert_from_saved_model
    result = _convert_saved_model(**converter_kwargs)
  File "C:\Users\XYZZ0\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\lite\python\convert_phase.py", line 212, in wrapper
    raise converter_error from None  # Re-throws the exception.
  File "C:\Users\XYZZ0\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\lite\python\convert_phase.py", line 205, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\XYZZ0\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\lite\python\convert.py", line 814, in convert_saved_model
    enable_mlir_converter=True)
  File "C:\Users\XYZZ0\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\lite\python\convert.py", line 311, in convert
    raise converter_error
tensorflow.lite.python.convert_phase.ConverterError: bad allocation

解決策

いま、作業してるんです。。。 デバッグの生中継中。。。 。。。。。
・・・
・・・

解決しました!!!!! (やはり単にPCのメモリ不足でした。) 不要ファイルを消して1GBほど空きをつくったら解決!!

解決策。FileNotFoundError: [Errno 2] No such file or directory: './models/output.tflite'

解決策。FileNotFoundError: [Errno 2] No such file or directory: './models/output.tflite'

エラーの内容

FileNotFoundError: [Errno 2] No such file or directory: './models/output.tflite'

メッセージ全体

Traceback (most recent call last):
  File "tf00.py", line 25, in <module>
    with open(output_model, 'wb') as o_:
FileNotFoundError: [Errno 2] No such file or directory: './models/output.tflite'

解決策

単に、modelsというディレクトリを作っておくだけ!!

コメント

こういうの、どうしておけば、ディレクトリがなくても勝手にディレクトリを作ってくれるのだろう。。。(Windows)。 コメント等で教えて頂けると幸甚。

Pythonのimportの意味は難しいね。誰か、C/C++とかのincludeとかの比較とかで説明してくれないかな。。。【考えながら、のんびり書く記事】

Pythonのimportの意味は難しいね。誰か、C/C++とかのincludeとかの比較とかで説明してくれないかな。。。

  • import文難しい。
  • from xxも難しい。
  • 関係ないかもしれないが、init.pyも意味がよくわからない。

まず、知りたいのが、importしたときに、どういう「作用」があるか。

コメント

まだ、書き始めです。

Pythonのことを調べていてよくみかける「note.nkmk.me」って。

Pythonのことを調べていてよくみかける「note.nkmk.me」って。

ワタシは、全く、関係者じゃないですーーー。

わかったこと

note.nkmk.me

上記のページを引用すると。。。。 ↓

追記(2024/01/27)

そろそろ、このレベルの別の人が現れるべき。他力。

コメント

この方のサイトの良し悪しは、不明です。何か、詳しそう。。。という印象。
適宜、以下などでも引用してみたいと思います。

keep-loving-python.hatenablog.com

Pythonの基本的な型の階層構造、この図、素晴らしすぎーー!!(Sequenceだと、自動的に、Iterableだとか)

Pythonの基本的な型の階層構造、この図、素晴らしすぎーー!!

以下のサイトの図、素晴らしすぎーーー。

qiita.com

なくなると嫌なので。。。

この図で何がわかるか

例えば、

  • Sequenceだと、自動的に、Iterableだとか
  • OederedDictは、Sequenceだとか

すばらしい。。。。コードもあるけど動かせなかった。Python2の時代か?(2018年)

別の領域も、もう少し、大きく

コメント

どなたか、このコードを動かして、動かし方、コメントで教えて欲しいです。 筆者、おんみずからでも。。。 ワタシも、別途、挑戦します(ちょっとトライしたけど、エラーが取れなかった!!!)