0

私は自分のデータセットでalexnet_v2を訓練しましたが、別のアプリケーションで使用したいと考えています。これは非常にシンプルでなければならず、私はいくつかの方法で実装しようとしましたが、回避できないエラーや、(以下のコードの場合は)無期限にハングするエラーが発生します。事前トレーニング(Tensorflow)を使用して画像を分類するCNN

理想的には、私はC++で好きです(ただし、C++ APIは信頼できないようですが、少なくとも多くの場所で文書化されているので、Pythonも受け入れられます)、画像の大きなグループ例えば、動物の80枚の画像を提供し、いずれかが猫を示すかどうかを返す)。

私はこのコードを以下のコードで正しく実行していますか?もしそうなら、私はそれをどのように修正することができます。

もしそうでない場合は、良い方法の実例がありますか?

多くのありがとうございます。

import tensorflow as tf 

#Using preprocessing and alexnet_v2 net from the slim examples 

from nets import nets_factory 
from preprocessing import preprocessing_factory 

#Checkpoint file from training on binary dataset 

checkpoint_path = '/home/ubuntu/tensorflow/models/slim/data/checkpoint.ckpt' 

slim = tf.contrib.slim 

number_of_classes = 2 


image_filename = '/home/ubuntu/tensorflow/models/slim/data/images/neg_sample_123459.jpg' 

image_filename_placeholder = tf.placeholder(tf.string) 

image_tensor = tf.read_file(image_filename_placeholder) 

image_tensor = tf.image.decode_jpeg(image_tensor, channels=3) 

image_batch_tensor = tf.expand_dims(image_tensor, axis=0) 

#Use slim's alexnet_v2 implementation 

network_fn = nets_factory.get_network_fn('alexnet_v2',num_classes=2,is_training=False) 

#Use inception preprocessing 

preprocessing_name = 'inception' 
image_preprocessing_fn= preprocessing_factory.get_preprocessing(preprocessing_name,is_training=False) 

image_tensor=image_preprocessing_fn(image_tensor,network_fn.default_image_size,network_fn.default_image_size) 

label=3 
images,labels=tf.train.batch(
    [image_tensor,label], 
    batch_size=2, 
    num_threads=1, 
    capacity=10) 

pred,_=network_fn(images) 

initializer = tf.local_variables_initializer() 

init_fn=slim.assign_from_checkpoint_fn(
    checkpoint_path, 
    slim.get_model_variables('alexnet_v2')) 

with tf.Session() as sess: 

    sess.run(initializer) 
    init_fn(sess) 
    tf.train.start_queue_runners(sess) 
    image_np, pred_np = sess.run([image_tensor, pred], feed_dict={image_filename_placeholder: image_filename}) 

編集:太字で行を追加した後、プログラムはもはやハングしません。しかし、私はプレースホルダエラーを取得しています:

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype string [[Node: Placeholder = Placeholderdtype=DT_STRING, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]]

私は二重のスペルをチェックしましたし、私の知る限り、私はそれを正しく供給しています。どうしましたか?

答えて

0

tf.train.batch()関数はバックグラウンドスレッドを使用して例をプリフェッチしますが、これらのスレッドを開始するには明示的なコマンド(tf.train.start_queue_runners(sess))を追加する必要があります。コードの最後の部分を次のように書き直してください。

with tf.Session() as sess: 
    sess.run(initializer) 
    init_fn(sess) 

    # Starts background threads for input preprocessing. 
    tf.train.start_queue_runners(sess) 

    image_np, pred_np = sess.run(
     [image_tensor, pred], 
     feed_dict={image_filename_placeholder: image_filename}) 
+0

これは懸案事項を解決しました。ありがとうございます!しかし、今ではプレースホルダエラーが発生しました... – Loz

+0

問題は、キューランナー( 'sess.run()'を内部的に呼び出すことによってイメージをバッチするために使用されるキューを埋める) 'image_filename_placeholder'ですが、キューランナーは、どのファイル名を送るべきか分からない。あなたのプログラムをもっと詳しく見てみると、一度に1つの画像を分類するなら、 'tf.train.batch()'はまったく必要ありません。単に 'tf.decode_jpeg()'の結果を渡すだけです。 (おそらくそれを再形成した後に) 'network_fn()'に書き換えます。 – mrry

+0

最終的なプログラムでは、1回の呼び出しで約80枚の画像をネットワークに分類しようとしています。そのため、バッチを渡す必要があるのはなぜですか(元の質問に対して少し単純化しました)。 – Loz

関連する問題