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

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

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

1900閲覧

Tensorflowで画像の正解のラベルを返したい

TyoNgc

総合スコア14

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2017/12/12 12:10

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ファイルにしてコマンドラインから実行しました。

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

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

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

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

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

mkgrei

2017/12/12 13:33

InvalidArgumentError (see above for traceback)なので、それ以前のエラーメッセージがあるとデバッグが捗ります。
guest

回答1

0

ベストアンサー

エラーを再現できません。
"./model.ckpt"を作った際のモデルと構造が変わっていたりしませんか。

python

1import sys 2import numpy as np 3import tensorflow as tf 4import cv2 5 6NUM_CLASSES = 3 7IMAGE_SIZE = 28 8IMAGE_PIXELS = IMAGE_SIZE*IMAGE_SIZE*3 9 10def inference(images_placeholder, keep_prob): 11 def weight_variable(shape): 12 initial = tf.truncated_normal(shape, stddev=0.1) 13 return tf.Variable(initial) 14 15 def bias_variable(shape): 16 initial = tf.constant(0.1, shape=shape) 17 return tf.Variable(initial) 18 19 def conv2d(x, W): 20 return tf.nn.conv2d(x, 21 W, 22 strides=[1, 1, 1, 1], 23 padding='SAME') 24 25 def max_pool_2x2(x): 26 return tf.nn.max_pool(x, 27 ksize=[1, 2, 2, 1], 28 strides=[1, 2, 2, 1], 29 padding='SAME') 30 31 x_image = tf.reshape(images_placeholder, [-1, 28, 28, 3]) 32 33 with tf.name_scope('conv1') as scope: 34 W_conv1 = weight_variable([5, 5, 3, 32]) 35 b_conv1 = bias_variable([32]) 36 h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) 37 h_pool1 = max_pool_2x2(h_conv1) 38 39 with tf.name_scope('conv2') as scope: 40 W_conv2 = weight_variable([5, 5, 32, 64]) 41 b_conv2 = bias_variable([64]) 42 h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) 43 44 with tf.name_scope('pool2') as scope: 45 h_pool2 = max_pool_2x2(h_conv2) 46 47 with tf.name_scope('fc1') as scope: 48 W_fc1 = weight_variable([7*7*64, 1024]) 49 b_fc1 = bias_variable([1024]) 50 h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) 51 h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 52 h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 53 54 with tf.name_scope('fc2') as scope: 55 W_fc2 = weight_variable([1024, NUM_CLASSES]) 56 b_fc2 = bias_variable([NUM_CLASSES]) 57 58 with tf.name_scope('softmax') as scope: 59 y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 60 61 return y_conv 62 63if __name__ == '__main__': 64 test_image = np.random.random((5, 28*28*3)) 65 66 images_placeholder = tf.placeholder("float", shape=(None, IMAGE_PIXELS)) 67 labels_placeholder = tf.placeholder("float", shape=(None, NUM_CLASSES)) 68 keep_prob = tf.placeholder("float") 69 70 logits = inference(images_placeholder, keep_prob) 71 sess = tf.InteractiveSession() 72 saver = tf.train.Saver() 73 sess.run(tf.global_variables_initializer()) 74 #saver.restore(sess, "./model.ckpt") 75 76 for i in range(len(test_image)): 77 accr = logits.eval(feed_dict={images_placeholder: [test_image[i]], 78 keep_prob: 1.0 })[0] 79 pred = np.argmax(logits.eval(feed_dict={images_placeholder: [test_image[i]], 80 keep_prob: 1.0 })[0]) 81 print(pred, accr)

投稿2017/12/12 15:18

mkgrei

総合スコア8560

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

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

TyoNgc

2017/12/13 04:33

無事に解決しました。同じディレクトリに他の実行した他のPythonファイルを入れていたことが原因でした。ご回答ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問