質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1089閲覧

ValueError: Fetch argument <tensorflow.python.framework.ops.Operation object at 0x0000020C052A2A20>

Kawasaki02021

総合スコア1

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/10/20 01:35

編集2021/10/20 05:04

前提・実現したいこと

https://github.com/totti0223/deepstomata
はckptファイルを読み込むように書かれていますが、h5ファイルを読み込むように変えたいので、
https://github.com/totti0223/deepstomata/blob/master/deepstomata/utils.py
の「def stomata_stat_batch_classify(...」(466行目〜)を質問に記載のコードの内容に変更して実行したら、以下のエラーが出ました。学習モデル(.h5)はkerasを使って学習させたので、復元したモデルがtensorflow形式に合っていないことから起こっていると考えています。該当のコードはエラーの原因と思われるコードの一部を抜粋して下記に置きます。元のコードが長いので以下のリンクに元コードを貼っておきます。
該当コード
[追記]
元々のプログラム(https://github.com/totti0223/deepstomata)ではckptファイル単体で実行するのですが、私の場合3つのファイル(.meta, .index, data-000)が保存され一つのckptファイルとして保存できません。これが本来の仕様であることは理解していますが、何か方法などがあればご教授いただければ幸いです。
よろしくお願いします。

発生している問題・エラーメッセージ

>>> deepstomata("C:/Users/Rx-80/OneDrive/デスクトップ/研究室/stomata/201216/WT mock") Reading config file using user defined config.ini from: C:\Users\Rx-80\AppData\Local\Programs\Python\Python35\lib\site-packages\deepstomata/config.ini config file path: C:\Users\Rx-80\AppData\Local\Programs\Python\Python35\lib\site-packages\deepstomata/config.ini Reading config file. Validating detector path Detector located Validating classifier path Classifier located config file imported properly listing image files in: C:/Users/Rx-80/OneDrive/デスクトップ/研究室/stomata/201216/WT mock directory detected. reading files inside directory. Will be analyzing the listed files... ['seq22124_result.jpg', 'seq22125_result.jpg', 'seq22126_result.jpg', 'seq22128_result.jpg', 'seq22129_result.jpg', 'seq22130_result.jpg', 'seq22131_result.jpg', 'seq22132_result.jpg', 'seq22133_result.jpg', 'seq22134_result.jpg', 'seq22135_result.jpg', 'seq22136_result.jpg', 'seq22137_result.jpg', 'seq22138_result.jpg', 'seq22139_result.jpg', 'seq22140_result.jpg', 'seq22141_result.jpg', 'seq22142_result.jpg', 'seq22143_result.jpg', 'seq22144_result.jpg', 'seq22145_result.jpg', 'seq22146_result.jpg', 'seq22147_result.jpg', 'seq22148_result.jpg', 'seq22149_result.jpg', 'seq22150_result.jpg', 'seq22151_result.jpg', 'seq22152_result.jpg', 'seq22153_result.jpg', 'seq22154_result.jpg', 'seq22155_result.jpg', 'seq22156_result.jpg', 'seq22157_result.jpg', 'seq22158_result.jpg', 'seq22159_result.jpg', 'seq22160_result.jpg', 'seq22161_result.jpg', 'seq22162_result.jpg', 'seq22163_result.jpg'] analysis start seq22124_result.jpg 2 stomata detected Exception ignored in: <bound method BaseSession.__del__ of <tensorflow.python.client.session.Session object at 0x0000020C04EBCC50>> Traceback (most recent call last): File "C:\Users\Rx-80\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 576, in __del__ if self._session is not None: AttributeError: 'Session' object has no attribute '_session' Traceback (most recent call last): File "C:\Users\Rx-80\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 267, in __init__ fetch, allow_tensor=True, allow_operation=True)) File "C:\Users\Rx-80\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2318, in as_graph_element return self._as_graph_element_locked(obj, allow_tensor, allow_operation) File "C:\Users\Rx-80\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2402, in _as_graph_element_locked raise ValueError("Operation %s is not an element of this graph." % obj) ValueError: Operation name: "init" op: "NoOp" input: "^conv1/weights/Assign" input: "^conv1/biases/Assign" input: "^conv2/weights/Assign" input: "^conv2/biases/Assign" input: "^conv3/weights/Assign" input: "^conv3/biases/Assign" input: "^conv4/weights/Assign" input: "^conv4/biases/Assign" input: "^fc5/weights/Assign" input: "^fc5/biases/Assign" input: "^fc6/weights/Assign" input: "^fc6/biases/Assign" input: "^fc7/weights/Assign" input: "^fc7/biases/Assign" is not an element of this graph. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\Rx-80\AppData\Local\Programs\Python\Python35\lib\site-packages\deepstomata\__init__.py", line 39, in deepstomata utils.analyze(item) # core module File "C:\Users\Rx-80\AppData\Local\Programs\Python\Python35\lib\site-packages\deepstomata\utils.py", line 569, in analyze results = stomata_stat_batch_classify(stomata_all, len(original_dets), ini['path']['classifier_path']) File "C:\Users\Rx-80\AppData\Local\Programs\Python\Python35\lib\site-packages\deepstomata\utils.py", line 495, in stomata_stat_batch_classify with tf.Session(graph=K.get_session()) as sess: File "C:\Users\Rx-80\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\backend\tensorflow_backend.py", line 125, in get_session _initialize_variables() File "C:\Users\Rx-80\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\backend\tensorflow_backend.py", line 282, in _initialize_variables sess.run(tf.variables_initializer(uninitialized_variables)) File "C:\Users\Rx-80\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 766, in run run_metadata_ptr) File "C:\Users\Rx-80\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 951, in _run fetch_handler = _FetchHandler(self._graph, fetches, feed_dict_string) File "C:\Users\Rx-80\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 407, in __init__ self._fetch_mapper = _FetchMapper.for_fetch(fetches) File "C:\Users\Rx-80\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 238, in for_fetch return _ElementFetchMapper(fetches, contraction_fn) File "C:\Users\Rx-80\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 274, in __init__ 'Tensor. (%s)' % (fetch, str(e))) ValueError: Fetch argument <tensorflow.python.framework.ops.Operation object at 0x0000020C052A2A20> cannot be interpreted as a Tensor. (Operation name: "init" op: "NoOp" input: "^conv1/weights/Assign" input: "^conv1/biases/Assign" input: "^conv2/weights/Assign" input: "^conv2/biases/Assign" input: "^conv3/weights/Assign" input: "^conv3/biases/Assign" input: "^conv4/weights/Assign" input: "^conv4/biases/Assign" input: "^fc5/weights/Assign" input: "^fc5/biases/Assign" input: "^fc6/weights/Assign" input: "^fc6/biases/Assign" input: "^fc7/weights/Assign" input: "^fc7/biases/Assign" is not an element of this graph.)

該当のソースコード

python

1def stomata_stat_batch_classify(image, region_number, ckpt_path): 2 3 ''' 4 input 5 image : image read by scipy. imread if by opencv, bgr to rgb must be performed 6 ckpt_path : checkpoint absolute path 7 output 8 most likely stat of stomata, confidential level of most likely stat of stomata 9 ''' 10 DST_INPUT_SIZE = 56 11 NUM_CLASS = 4 12 tf.reset_default_graph() 13 14 image = tf.reshape(image, [-1, DST_INPUT_SIZE, DST_INPUT_SIZE, 3]) 15 logits = stomata_model.tf_inference(image, region_number, DST_INPUT_SIZE, NUM_CLASS) 16 17 with tf.Graph().as_default(): 18 with tf.session(graph=K.get_session()) as sess: 19 import keras.backend.tensorflow_backend as KTF 20 sess.run(tf.global_variables_initializer()) 21 model = load_model() 22 K.set_image_dim_ordering('tf') 23 if ckpt_path: 24 model 25 K.set_image_dim_ordering('tf') 26 softmax = tf.nn.softmax(logits).eval() 27 results = [[None for _ in range(2)] for _ in range(region_number)] 28 q = 0 29 for logit in softmax: 30 logit = [round(n * 100, 1) for n in logit] 31 logit = np.asarray(logit) 32 result = [["open", logit[0]], ["closed", logit[1]], ["partially_open", logit[2]], ["false_positive", logit[3]]] 33 result = sorted(result, key =lambda x: int(x[1]), reverse = True) 34 results[q][0] = result[0][0] 35 results[q][1] = result[0][1] 36 q += 1 37 #print ("\t",results) 38 return results

試したこと

インターネットで検索したところ、"image_dim_ordering": "tf"をkerasの.jsonファイルに入れると解決するらしいのですが、該当のファイルがpython内にあるkerasフォルダ内にないのですが、この場合の解決策などもあれば教えて頂きたいです。
参考サイト
https://stackoverflow.com/questions/46497869/valueerror-fetch-argument-tf-operation-init-8-type-noop-cannot-be-interpret

補足情報(FW/ツールのバージョンなど)

現在の環境
python 3.5.6
tensorflow 0.12.0rc0
keras 1.2.2
matplotlib 2.0.0
pandas 0.25.3
numpy 1.15.2

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jbpb0

2021/10/20 02:26 編集

質問のエラーの先頭の >>> deepstomata("C:/Users/Rx-80/OneDrive/デスクトップ/研究室/stomata/201216/WT mock") が、質問のコードにも、「元コード」 https://github.com/Kawasaki02021/deepstomata_convert_h5/blob/main/utils_test.py にも見当たらないのですが 【追記】 https://github.com/totti0223/deepstomata/blob/master/deepstomata/utils.py を、「*.h5」を読み込むように書き換えたのが https://github.com/Kawasaki02021/deepstomata_convert_h5/blob/main/utils_test.py で、それに差し替えて https://github.com/totti0223/deepstomata を動かそうとしたらエラーが出る、ということでしょうか?
Kawasaki02021

2021/10/20 02:35

返信ありがとうございます。 すみません、jbpb0さんが記載していただいた通りhttps://github.com/totti0223/deepstomata/blob/master/deepstomata/utils.py内のコードを.h5に差し替えて実行しようとしたらエラーがでたということです。 >>> deepstomata("C:/Users/Rx-80/OneDrive/デスクトップ/研究室/stomata/201216/WT mock") の部分は実際にdeepstomataを実行した際に出てきたエラーです。 元々は.ckptファイルで実行するのですが、単一のckptファイルにまとめられないので、.h5で実行しようとしています。 teratailにアップしたコードの部分をオリジナルから変更しました。 https://github.com/totti0223/deepstomata/blob/master/deepstomata/utils.py の482行目から486行目の部分を変更しました。 説明不足ですみません。
jbpb0

2021/10/20 03:18 編集

質問を編集して、下記を追記してください そうしないと、質問を読んだ人には状況が伝わりません https://github.com/totti0223/deepstomata はckptファイルを読み込むように書かれているが、h5ファイルを読み込むように変えたいので、 https://github.com/totti0223/deepstomata/blob/master/deepstomata/utils.py の「def stomata_stat_batch_classify(...」(466行目〜)を質問に記載のコードの内容に変更して実行したら、質問のエラーが出た
jbpb0

2021/10/20 03:23

質問に記載のコードでは、「ckpt_path」が使われてないのですが、 model = load_model(ckpt_path) とかするのでは??
Kawasaki02021

2021/10/20 05:41

ご指摘ありがとうございます。  質問の方は修正しました。  h5からckptに変換する件なのですが、deepstomataのcofigファイルで分類器を指定します。configファイルに元々指定してあるファイルがmodels/model.ckpt-5000ckptとなっており、https://github.com/totti0223/deepstomata/tree/master/deepstomata/modelsで確認できるかと思います。先ほど載せて頂いたリンクを参考にh5からckpt保存すると3つのファイル(.meta, .index, data-000)が生成されますが、これらを統合する方法が分かりません。単体で保存するにはtensorflow 0.10.0 で学習する必要があるのですが、python 3.5.6ではサポートされていません。どうやって一つのファイルとして認識させるかが分かりません。  2つ目にご指摘いただいた通りにmodel = load_model(ckpt_path)と実行するとTypeError: graph must be a tf.Graph, but got <class 'tensorflow.python.client.session.Session'>と出ます。モデルのグラフをtf.Graphに対応させる方法が分かりません。教えて頂けると幸いです。
jbpb0

2021/10/20 06:04

https://github.com/totti0223/deepstomata/tree/master/deepstomata/models でも、「model.ckpt-5000.meta」は別にあります > h5からckpt保存すると3つのファイル(.meta, .index, data-000)が生成 の「data-000」を「model.ckpt-5000ckpt」の代わりに指定するのでは、読み込めないのですか? > 単体で保存するにはtensorflow 0.10.0 で学習する必要がある ckptは複数のファイルがあるものだと思ってましたけど、昔はそうではなかったのですね 知らなかった
jbpb0

2021/10/20 06:16

> 「data-000」を「model.ckpt-5000ckpt」の代わりに指定 がダメなら、ファイル名の「data-000」より前の部分(たぶん「*.ckpt」)のみを指定してみてください
Kawasaki02021

2021/10/20 06:20

返信ありがとうございます。 >https://github.com/totti0223/deepstomata/tree/master/deepstomata/models でも、「model.ckpt-5000.meta」は別にあります 確かにそうですけど、実際に使用するファイルはmodel.ckpt-5000の方です。.metaの方はなぜあるのかよく分からないです。 > の「data-000」を「model.ckpt-5000ckpt」の代わりに指定するのでは、読み込めないのですか? その通りです。改変する前のコードで実行した際に読み取れないとエラーが出ました。 エラーの内容はhttps://github.com/Kawasaki02021/deepstomata_convert_h5/blob/main/deepstomata%20error%20codeにある通りです。これは.h5で実行していますが、エラー内容はこれと同じものが出てきました。復元過程が統合されたckptファイル用になっていることが原因だと考えています。私が改変した箇所をdata-000用に書き換えればいいのでしょか? 分からないことばかりですみません。
jbpb0

2021/10/20 06:34 編集

> 単体で保存するにはtensorflow 0.10.0 で学習する必要がある 下記に、単一のファイルで保存する方法が書かれてました https://ja.stackoverflow.com/questions/34844/tensor-flow%E3%81%AE%E3%83%90%E3%83%BC%E3%82%B8%E3%83%A7%E3%83%B3%E3%82%A2%E3%83%83%E3%83%97%E3%82%92%E8%A1%8C%E3%81%A3%E3%81%9F%E7%B5%90%E6%9E%9C-checkpoint%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%AE%E3%83%95%E3%82%A9%E3%83%BC%E3%83%9E%E3%83%83%E3%83%88%E3%81%8C%E5%A4%89%E6%9B%B4%E3%81%95%E3%82%8C%E3%81%A6%E3%81%84%E3%82%8B https://stackoverflow.com/questions/52650842/how-to-convert-hdf5-to-tensorflow-checkpoint の方法と合わせ技で適当なh5ファイルを変換したら、 https://github.com/totti0223/deepstomata/tree/master/deepstomata/models と同じ、ファイル名の最後に「.meta」が付いてるファイルと付いてないファイルができました (「*.index」と「*.data-*」はできない)
Kawasaki02021

2021/10/20 06:40 編集

ありがとうございます。一つのファイルに保存できました。 Tensor name "conv2/biases" not found in checkpoint filesというエラーが出ましたが、おそらく学習モデル(VGG16)を作る際に重みの更新を14層まで更新しないように設定したのが原因だと思うので、学習モデルを変更してまた試してみます。いろいろアドバイスをいただきありがとうございました。
guest

回答1

0

ベストアンサー

学習モデル(.h5)はkerasを使って学習させた

How to convert hdf5 to tensorflow checkpoint
に、h5ファイルからckptファイルを作成する方法が書かれてます

元々のプログラムではckptファイル単体で実行するのですが、私の場合3つのファイル(.meta, .index, data-000)が保存され一つのckptファイルとして保存できません。

tensor flowのバージョンアップを行った結果、checkpointファイルのフォーマットが変更されている
を見てください

投稿2021/10/20 10:10

jbpb0

総合スコア7653

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Kawasaki02021

2021/10/22 06:34

お答えいただきありがとうございます。発生したエラーもこのリンクにある内容で解決できました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問