###前提・実現したいこと
CNNで画像分類を行っていました.
CNN,NNでの分類は行えたのですが,K近傍法やランダムフォレストなどで実装してみたいと考えています.(CNNなどのディープラーニングがどれほど画像認識に優れているかを確認したいので)
MNISTを用いたサンプルコードは見かけるのですが,MNISTでなくこちらで用意した画像で分類したいのですが,PythonやTensorFlowに慣れておらず,また市販の本も読んでみたのですがどれもMNISTを題材に挙げているためうまく実装できていない現状です.
解決策,ご意見よろしくお願いします.
###該当のソースコード
Python
1import random 2import sys 3import cv2 4import numpy as np 5import tensorflow as tf 6import tensorflow.python.platform 7 8NUM_CLASSES = 2 9IMAGE_SIZE_x = 48 #横のリサイズ 10IMAGE_SIZE_y = 36 #縦のリサイズ 11IMAGE_CHANNELS = 3 #RGB 12IMAGE_PIXELS = IMAGE_SIZE_x*IMAGE_SIZE_y*IMAGE_CHANNELS 13 14flags = tf.app.flags 15FLAGS = flags.FLAGS 16flags.DEFINE_string('save_model', 'models/model.ckpt', 'File name of model data') 17flags.DEFINE_string('train', 'train.txt', 'File name of train data') 18flags.DEFINE_string('test', 'test.txt', 'File name of test data') 19flags.DEFINE_string('image_dir', 'data', 'Directory of images') 20flags.DEFINE_string('train_dir', '/tmp/data', 'Directory to put the training data.') 21flags.DEFINE_integer('max_steps', 1000, 'Number of steps to run trainer.') 22flags.DEFINE_integer('batch_size', 10, 'Batch size' 23 'Must divide evenly into the dataset sizes.') 24 25 26 27#訓練テストの入力 MNISTではそれぞれサンプルから10000,200のバッチを用いている 28X_traning,Y_traning = ?? 29X_test,Y_test = ?? 30 31 # モデルの入出力の設定 32x_train = tf.placeholder(tf.float32, [None,48*36]) #training input 入力画像は48*36画素 33y_train = tf.placeholder(tf.float32, [None,2]) #traning label 2クラスに分類予定 34x_test = tf.placeholder(tf.float32, [48*36]) #testing input 35 36# 比較する近傍ベクトルの数を設定 37K = 3 38nearest_neighbors = tf.Variable(tf.zeros([K])) 39 40# 入力ベクトルと近傍ベクトルの距離を計算 41distance = tf.negative(tf.reduce_sum(tf.abs(tf.add(x_train, tf.negative(x_test))),axis=1)) #L1 42# 上でマイナスを付けたので、大きい順に取り出せばいい 43values, indices = tf.nn.top_k(distance, k=K, sorted=False) 44 45# 近傍値を保存するリスト 46nn_vector = [] 47for i in range(K): 48 nn_vector.append(tf.argmax(y_train[indices[i]], 0)) 49 50# 近傍ベクトルのリストをtensorflowの変数へ変換する 51nearest_neighbors = nn_vector 52 53# クラスラベル、そのインデックス、個数を計算 54y, idx, count = tf.unique_with_counts(nearest_neighbors) 55""" 56例: 57x = [1, 1, 2, 4, 4, 4, 7, 8, 8] 58y, idx, count = unique_with_counts(x) 59y ==> [1, 2, 4, 7, 8] 60idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4] 61count ==> [2, 1, 3, 1, 2] 62""" 63 64# 最も個数の多いクラスを取り出す 65pred = tf.slice(y, begin=[tf.argmax(count, 0)], size=tf.constant([1], dtype=tf.int64))[0] 66# y = [ 1, 2, 3, 4, 11] count = [3, 2, 2, 7, 1] の時、 1を返す 67 68#訓練の実行 69accuracy = 0 70init = tf.global_variables_initializer() 71 72with tf.Session() as sess: 73 74 for i in range(X_test.shape[0]): 75 predicted_value = sess.run(pred,feed_dict={x_train:X_traning, y_train:Y_traning, x_test:X_test[i,:]}) 76 77 print("Test", i, "Prediction", predicted_value, "True Class:", np.argmax(Y_test[i])) 78 79 if predicted_value == np.argmax(Y_test[i]): 80 accuracy += 1.0 81 82 accuracy /= len(Y_test) 83 print("Accuracy:", accuracy) 84 85if __name__ == '__main__': 86 # ファイルを開く 87 with open(FLAGS.train, 'r') as f: # train.txt 88 train_image = [] 89 train_label = [] 90 for line in f: 91 # 改行を除いてスペース区切りにする 92 line = line.rstrip() 93 l = line.split() 94 # データを読み込んで縮小 95 img = cv2.imread(FLAGS.image_dir + '/' +l[0]) 96 img = cv2.resize(img, (IMAGE_SIZE_x, IMAGE_SIZE_y)) 97 # 一列にした後、0-1のfloat値にする 98 train_image.append(img.flatten().astype(np.float32)/255.0) 99 # ラベルを1-of-k方式で用意する 100 tmp = np.zeros(NUM_CLASSES) 101 tmp[int(l[1])] = 1 102 train_label.append(tmp) 103 # numpy形式に変換 104 train_image = np.asarray(train_image) 105 train_label = np.asarray(train_label) 106 train_len = len(train_image) 107 108 with open(FLAGS.test, 'r') as f: # test.txt 109 test_image = [] 110 test_label = [] 111 for line in f: 112 line = line.rstrip() 113 l = line.split() 114 img = cv2.imread(FLAGS.image_dir + '/' +l[0]) 115 img = cv2.resize(img, (IMAGE_SIZE_x, IMAGE_SIZE_y)) 116 test_image.append(img.flatten().astype(np.float32)/255.0) 117 tmp = np.zeros(NUM_CLASSES) 118 tmp[int(l[1])] = 1 119 test_label.append(tmp) 120 test_image = np.asarray(test_image) 121 test_label = np.asarray(test_label) 122 test_len = len(test_image) 123 124 with tf.Graph().as_default(): 125 # 画像を入れる仮のTensor 126 images_placeholder = tf.placeholder("float", shape=(None, IMAGE_PIXELS)) 127 # ラベルを入れる仮のTensor 128 labels_placeholder = tf.placeholder("float", shape=(None, NUM_CLASSES)) 129
###試したこと
CNNでの実装,K最近傍法のMNISTでの実装は確認できています.
flags.DEFINE_string('train', 'train.txt', 'File name of train data') flags.DEFINE_string('test', 'test.txt', 'File name of test data') flags.DEFINE_string('image_dir', 'data', 'Directory of images')
のdataには画像ファイルが入っており,train.txt,test.txtには画像のファイル名と正解ラベルを記入しております.
CNNの分類とK最近傍法の実装は以下のサイトを参照しました.
TensorFlowでアニメゆるゆりの制作会社を識別する
TensorFlowでK最近傍法を実装
###補足情報(言語/FW/ツール等のバージョンなど)
Windows10 (64bit)
Python 3.5.4(仮想環境)(Anaconda4.4.0(64bit))
TensorFlow 1.4.0
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/10 17:23
2018/01/10 17:41
2018/01/11 14:00