###前提・実現したいこと
Tensorflowを使ってPythonで画像分類のプログラムを実行しようとしています。openCVを実行するところで画像がうまくインポートできていないのかエラーメッセージが出てしまいます。トレーニングデータをTrain.txtテストデータをtest.txtとして画像のファイルと同じディレクトリに配置しています。
###発生している問題・エラーメッセージ
```Traceback (most recent call last) <ipython-input-8-ac855ac373a9> in <module>() 22 # データを読み込んで28x28に縮小 23 img = cv2.imread(FLAGS.image_dir + '/' + l[0]) ---> 24 img = cv2.resize(img, (28, 28)) 25 # 一列にした後、0-1のfloat値にする 26 train_image.append(img.flatten().astype(np.float32)/255.0) error: ......\modules\imgproc\src\imgwarp.cpp:3229: error: (-215) ssize.area() > 0 in function cv::resize ###該当のソースコード ```Python def accuracy(logits, labels): """ 正解率(accuracy)を計算する関数 引数: logits: inference()の結果 labels: ラベルのtensor, int32 - [batch_size, NUM_CLASSES] 返り値: accuracy: 正解率(float) """ correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) tf.summary.scalar("accuracy", accuracy) return accuracy if __name__ == '__main__': f = open(FLAGS.train, 'r') # データを入れる配列 train_image = [] train_label = [] for line in f: # 改行を除いてスペース区切りにする line = line.rstrip() l = line.split() # データを読み込んで28x28に縮小 img = cv2.imread(FLAGS.image_dir + '/' + l[0]) img = cv2.resize(img, (28, 28)) # 一列にした後、0-1のfloat値にする train_image.append(img.flatten().astype(np.float32)/255.0) # ラベルを1-of-k方式で用意する tmp = np.zeros(NUM_CLASSES) tmp[int(l[1])] = 1 train_label.append(tmp) # numpy形式に変換 train_image = np.asarray(train_image) train_label = np.asarray(train_label) f.close() f = open(FLAGS.test, 'r') test_image = [] test_label = [] for line in f: line = line.rstrip() l = line.split() img = cv2.imread(FLAGS.image_dir + '/' + l[0]) img = cv2.resize(img, (28, 28)) test_image.append(img.flatten().astype(np.float32)/255.0) tmp = np.zeros(NUM_CLASSES) tmp[int(l[1])] = 1 test_label.append(tmp) test_image = np.asarray(test_image) test_label = np.asarray(test_label) f.close() with tf.Graph().as_default(): # 画像を入れる仮のTensor images_placeholder = tf.placeholder("float", shape=(None, IMAGE_PIXELS)) # ラベルを入れる仮のTensor labels_placeholder = tf.placeholder("float", shape=(None, NUM_CLASSES)) # dropout率を入れる仮のTensor keep_prob = tf.placeholder("float") # inference()を呼び出してモデルを作る logits = inference(images_placeholder, keep_prob) # loss()を呼び出して損失を計算 loss_value = loss(logits, labels_placeholder) # training()を呼び出して訓練 train_op = training(loss_value, FLAGS.learning_rate) # 精度の計算 acc = accuracy(logits, labels_placeholder) # 保存の準備 saver = tf.train.Saver() # Sessionの作成 sess = tf.Session() # 変数の初期化 sess.run(tf.global_variables_initializer()) # TensorBoardで表示する値の設定 summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.train_dir, sess.graph) # 訓練の実行 for step in range(FLAGS.max_steps): for i in range(int(len(train_image)/FLAGS.batch_size)): # batch_size分の画像に対して訓練の実行 batch = FLAGS.batch_size*i # feed_dictでplaceholderに入れるデータを指定する sess.run(train_op, feed_dict={ images_placeholder: train_image[batch:batch+FLAGS.batch_size], labels_placeholder: train_label[batch:batch+FLAGS.batch_size], keep_prob: 0.5}) # 1 step終わるたびに精度を計算する train_accuracy = sess.run(acc, feed_dict={ images_placeholder: train_image, labels_placeholder: train_label, keep_prob: 1.0}) print("step %d, training accuracy %g"%(step, train_accuracy)) # 1 step終わるたびにTensorBoardに表示する値を追加する summary_str = sess.run(summary_op, feed_dict={ images_placeholder: train_image, labels_placeholder: train_label, keep_prob: 1.0}) summary_writer.add_summary(summary_str, step) # 訓練が終了したらテストデータに対する精度を表示 print("test accuracy %g"%sess.run(acc, feed_dict={ images_placeholder: test_image, labels_placeholder: test_label, keep_prob: 1.0})) # 最終的なモデルを保存 save_path = saver.save(sess, os.getcwd() + "\model.ckpt")
エラー発生する直前行に「print(FLAGS.image_dir + '/' + l[0])」入れて出力結果を追記ください(パスが正しいか?)。
コメントありがとうございます。実行したところ、自分が予期していないフォルダのjpgファイルにパスが通っていました。数百の画像データを別のファルダに格納しているのですが、そこにパスを通すためにはどのようにすればよいでしょうか(無知ですみません...)
コード詳細確認できていませんが「FLAGS.image_dir」変数に正しいフォルダパスをセットすればよいと思います。
おそらく正しいパスはセットできたと思いますが、その次の行のflattenの所でエラーが出ました。('NoneType' object has no attribute 'flatten')これは何が原因だと考えられますか。
コードが確認しずらいのでコード部分を「コード表示」になるように修正ください。多分、コード前後の区切り「```」がうまく入っていないようです。
回答1件
あなたの回答
tips
プレビュー