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

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

新規登録して質問してみよう
ただいま回答率
85.48%
OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

936閲覧

CNNにてaccuracyの値が変化しない。

INASUKE

総合スコア12

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/07/21 12:52

前提・実現したいこと

現在、CNNとカスケードを活用して自分の顔のみ認識し、その顔に対してカスケードを使い矩形を描画するプログラムを作成しています。そこで、「自分の顔」というものを学習させるプログラムを以下の通り作成したのですが、 training accuracyの値が一切変化しません。
そこで、どのような改善をすれば学習が開始されるのかアドバイスをお願いいたします。
また過不足な情報があればお申し付けください。

発生している問題・エラーメッセージ

![イメージ説明](49c0662e790182f8fed721754593e1df.png)

該当のソースコード

python

1import os 2import random 3import cv2 4import numpy as np 5import tensorflow as tf 6 7import matplotlib.pyplot as plt 8 9path = "./faces/train" 10dirs = os.listdir(path) 11dirs = [f for f in dirs if os.path.isdir(os.path.join(path, f))] 12 13label_dict = {} 14i = 0 15 16for dirname in dirs: 17 label_dict[dirname] = i 18 i += 1 19 20def load_data(data_type): 21 22 filenames, images, labels = [], [], [] 23 24 walk = filter(lambda _: not len(_[1]) and data_type in _[0], os.walk('faces')) 25 26 for root, dirs, files in walk: 27 filenames += ['{}/{}'.format(root, _) for _ in files if not _.startswith('.')] 28 29 # Shuffle files 30 random.shuffle(filenames) 31 32 # Read, resize, and reshape images 33 images = [] 34 for file in filenames: 35 img = cv2.imread(file) 36 img = cv2.resize(img, (32,32)) 37 images.append(img.flatten().astype(np.float32) / 255.0) 38 images = np.asarray(images) 39 40 for filename in filenames: 41 label = np.zeros(len(label_dict)) 42 for k, v in label_dict.items(): 43 if k in filename: 44 label[v] = 1. 45 labels.append(label) 46 47 return images, labels 48 49def get_batch_list(l, batch_size): 50 # [1, 2, 3, 4, 5,...] -> [[1, 2, 3], [4, 5,..]] 51 return [np.asarray(l[_:_+batch_size]) for _ in range(0, len(l), batch_size)] 52 53def weight_variable(shape): 54 initial = tf.truncated_normal(shape, stddev=0.1) 55 return tf.Variable(initial) 56 57def bias_variable(shape): 58 initial = tf.constant(0.1, shape=shape) 59 return tf.Variable(initial) 60 61def conv2d(x, W): 62 return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 63 64def max_pool_2x2(x): 65 return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 66 67def inference(images_placeholder, keep_prob): 68 69 x_image = tf.reshape(images_placeholder, [-1, 32, 32, 3]) 70 71 # Convolution layer 72 W_conv1 = weight_variable([5, 5, 3, 32]) 73 b_conv1 = bias_variable([32]) 74 h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) 75 76 # Pooling layer 77 h_pool1 = max_pool_2x2(h_conv1) 78 79 # Convolution layer 80 W_conv2 = weight_variable([5, 5, 32, 64]) 81 b_conv2 = bias_variable([64]) 82 h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) 83 84 # Pooling layer 85 h_pool2 = max_pool_2x2(h_conv2) 86 87 # Full connected layer 88 W_fc1 = weight_variable([8 * 8 * 64, 1024]) 89 b_fc1 = bias_variable([1024]) 90 h_pool2_flat = tf.reshape(h_pool2, [-1, 8 * 8 * 64]) 91 h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 92 93 # Dropout 94 h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 95 96 # Full connected layer 97 W_fc2 = weight_variable([1024, len(label_dict)]) 98 b_fc2 = bias_variable([len(label_dict)]) 99 100 return tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 101 102# データ読込 103train_images, train_labels = load_data('train') 104test_images, test_labels = load_data('test') 105 106print("train_images", len(train_images)) 107print("test_images", len(test_images)) 108 109# 110x = tf.placeholder('float', shape=[None, 32 * 32 * 3]) # 32 * 32, 3 channels 111y_ = tf.placeholder('float', shape=[None, len(label_dict)]) # label_dict size 112keep_prob = tf.placeholder('float') 113y_conv = inference(x, keep_prob) 114 115# Loss function 116cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv)) 117tf.summary.scalar('cross_entropy', cross_entropy) 118 119# Minimize cross entropy by using SGD 120train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) 121 122# Accuracy 123correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) 124accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) 125 126saver = tf.train.Saver() 127sess = tf.InteractiveSession() 128sess.run(tf.global_variables_initializer()) 129 130# Batch 131batch_size = 20 132batched_train_images = get_batch_list(train_images, batch_size) 133batched_train_labels = get_batch_list(train_labels, batch_size) 134print(len(batched_train_labels)) 135 136train_labels, test_labels = np.asarray(train_labels), np.asarray(test_labels) 137 138cnt = 0 139 140accuracys = [] 141 142# Train 143for i in range(15): 144 145 for step, (images, labels) in enumerate(zip(batched_train_images, batched_train_labels)): 146 sess.run(train_step, feed_dict={ x: images, y_: labels, keep_prob: 0.5 }) 147 train_accuracy = accuracy.eval(feed_dict = { 148 x: train_images, y_: train_labels, keep_prob: 1.0 }) 149 accuracys.append(train_accuracy) 150 cnt += 1 151 152 print('step {}, training accuracy {}'.format(cnt, train_accuracy)) 153 154# Test trained model 155test_accuracy = accuracy.eval(feed_dict = { 156 x: test_images, y_: test_labels, keep_prob: 1.0 }) 157print('test accuracy {}'.format(test_accuracy)) 158 159# Save model 160save_path = saver.save(sess, "model_face/model.ckpt") 161 162sess.close() 163 164plt.plot(accuracys) 165plt.show()

試したこと

何通りかtrain_stepの値を変えて実行してみましたが、変化はなかったです。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問