Tensorflowでオリジナルの画像で画像認識をしています。そのプログラムに対する正解のラベルを返したいのですが、エラーが出てしまいます。何が原因でしょうか。参考にしているサイトはこちらです。TensorFlowでアニメゆるゆりの制作会社を識別する
test.py
python
1#!/usr/bin/env python 2import sys 3import numpy as np 4import tensorflow as tf 5import cv2 6 7NUM_CLASSES = 3 8IMAGE_SIZE = 28 9IMAGE_PIXELS = IMAGE_SIZE*IMAGE_SIZE*3 10 11def inference(images_placeholder, keep_prob): 12 # 重みを標準偏差0.1の正規分布で初期化 13 def weight_variable(shape): 14 initial = tf.truncated_normal(shape, stddev=0.1) 15 return tf.Variable(initial) 16 # バイアスを標準偏差0.1の正規分布で初期化 17 def bias_variable(shape): 18 initial = tf.constant(0.1, shape=shape) 19 return tf.Variable(initial) 20 # 畳み込み層の作成 21 def conv2d(x, W): 22 return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 23 # プーリング層の作成 24 def max_pool_2x2(x): 25 return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], 26 strides=[1, 2, 2, 1], padding='SAME') 27 x_image = tf.reshape(images_placeholder, [-1, 28, 28, 3]) 28 # 畳み込み層1の作成 29 with tf.name_scope('conv1') as scope: 30 # 引数は[width, height, input, filters] 31 # 32個の特徴を検出する 32 W_conv1 = weight_variable([5, 5, 3, 32]) 33 b_conv1 = bias_variable([32]) 34 h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) 35 # プーリング層1の作成 36 h_pool1 = max_pool_2x2(h_conv1) 37 38 # 畳み込み層2の作成 39 with tf.name_scope('conv2') as scope: 40 W_conv2 = weight_variable([5, 5, 32, 64]) 41 b_conv2 = bias_variable([64]) 42 # 検出した特徴の整理(第一レイヤーと同じ) 43 h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) 44 # プーリング層2の作成 45 with tf.name_scope('pool2') as scope: 46 h_pool2 = max_pool_2x2(h_conv2) 47 # 全結合層1の作成 48 with tf.name_scope('fc1') as scope: 49 W_fc1 = weight_variable([7*7*64, 1024]) 50 b_fc1 = bias_variable([1024]) 51 #画像の解析結果をベクトルへ変換 52 h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) 53 # 第一、第二と同じく、検出した特徴を活性化させている 54 h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 55 # dropoutの設定 56 h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 57 # 全結合層2の作成 58 with tf.name_scope('fc2') as scope: 59 W_fc2 = weight_variable([1024, NUM_CLASSES]) 60 b_fc2 = bias_variable([NUM_CLASSES]) 61 # ソフトマックス関数による正規化 62 with tf.name_scope('softmax') as scope: 63 y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 64 # 各ラベルの確率のようなものを返す 65 return y_conv 66 67if __name__ == '__main__': 68 test_image = [] 69 for i in range(1, len(sys.argv)): 70 img = cv2.imread(sys.argv[i]) 71 img = cv2.resize(img, (28, 28)) 72 test_image.append(img.flatten().astype(np.float32)/255.0) 73 test_image = np.asarray(test_image) 74 75 images_placeholder = tf.placeholder("float", shape=(None, IMAGE_PIXELS)) 76 labels_placeholder = tf.placeholder("float", shape=(None, NUM_CLASSES)) 77 keep_prob = tf.placeholder("float") 78 79 logits = inference(images_placeholder, keep_prob) 80 sess = tf.InteractiveSession() 81 saver = tf.train.Saver() 82 sess.run(tf.global_variables_initializer()) 83 saver.restore(sess, "./model.ckpt") 84 85 for i in range(len(test_image)): 86 accr = logits.eval(feed_dict={ 87 images_placeholder: [test_image[i]], 88 keep_prob: 1.0 })[0] 89 pred = np.argmax(logits.eval(feed_dict={ 90 images_placeholder: [test_image[i]], 91 keep_prob: 1.0 })[0]) 92 print (pred,accr)
$python test.py image.jpg
InvalidArgumentError (see above for traceback): Assign requires shapes of both tensors to match. lhs shape= [3] rhs shape= [1024] [[Node: save/Assign_7 = Assign[T=DT_FLOAT, _class=["loc:@fc2/Variable_1"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/gpu:0"](fc2/Variable_1, save/RestoreV2_7/_1)]]
ディレクトリ構成
cnn
---data(画像が入ったディレクトリ)
---train.ipynb
---test.py
---train.txt(画像の名前とラベル)
---test.txt
---logs
---image.jpg
Jupiter notebookでテストファイルを作っていたのですが、OpenCVのエラーが出たためpythonファイルにしてコマンドラインから実行しました。
InvalidArgumentError (see above for traceback)なので、それ以前のエラーメッセージがあるとデバッグが捗ります。
回答1件
あなたの回答
tips
プレビュー