前提・実現したいこと
tensorflowで自作のdatasetを用いて文字認識を行いたいと思っています。
そこで、kerasのMNISTサンプルプログラムを改良してプログラムの作成を行いました。
具体的には、MNISTのdataset読み込みの部分を改良し、
画像データを置いているディレクトリ,ラベルをまとめたcsvファイルを作成、
tensorflowに読み込ませるようにしました。
発生している問題・エラーメッセージ
しかし、次のようなエラーが出て学習させることができませんでした。
どのように修正すれば良いのでしょうか?
bash
1/home/background/anaconda3/envs/tensorflow/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`. 2 from ._conv import register_converters as _register_converters 3Using TensorFlow backend. 42018-10-09 09:59:02.531361: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX 52018-10-09 09:59:02.544577: I tensorflow/core/common_runtime/process_util.cc:69] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance. 6Traceback (most recent call last): 7 File "ml_modi.py", line 55, in <module> 8 x_train = x_train.reshape(1000, 4096) 9ValueError: cannot reshape array of size 12288 into shape (1000,4096)
該当のソースコード
python
1from __future__ import print_function 2 3import tensorflow as tf 4 5import keras 6from keras.datasets import mnist 7from keras.models import Sequential 8from keras.layers import Dense, Dropout 9from keras.optimizers import RMSprop 10from keras.preprocessing.image import load_img, img_to_array 11 12from sklearn import svm 13from sklearn.model_selection import train_test_split 14from sklearn.metrics import accuracy_score 15 16import numpy as np 17 18batch_size = 128 19num_classes = 10 20epochs = 20 21 22# setup queue for importing images 23 24filename_queue = tf.train.string_input_producer(["./dataset.csv"]) 25reader = tf.TextLineReader() 26key, value = reader.read(filename_queue) 27filename, labels = tf.decode_csv(value, [["aa"], [1]]) 28 29img_r = tf.read_file(filename) 30img = tf.image.decode_png(img_r, channels=3) 31 32with tf.Session() as sess: 33 sess.run(tf.global_variables_initializer()) 34 35 try: 36 coord = tf.train.Coordinator() 37 threads = tf.train.start_queue_runners(coord=coord) 38 39 for i in range(1000): 40 image, label = sess.run([img, labels]) 41 42 finally: 43 coord.request_stop() 44 coord.join(threads) 45 46x_train = np.float32(image) 47x_test = np.float32(image) 48y_train = np.float32(label) 49y_test = np.float32(label) 50 51x_train = x_train.reshape(1000, 4096) 52x_test = x_test.reshape(1000, 4096) 53#x_train = x_train.astype('float32') 54#x_test = x_test.astype('float32') 55x_train /= 255 56x_test /= 255 57print(x_train.shape[0], 'train sample') 58print(x_test.shape[0], 'test sample') 59 60# convert class vectors to binary class matrices 61y_train = keras.utils.to_categorical(y_train, num_classes) 62y_test = keras.utils.to_categorical(y_test, num_classes) 63 64model = Sequential() 65model.add(Dense(512, activation='relu', input_shape=(784,))) 66model.add(Dropout(0.2)) 67model.add(Dense(512, activation='relu')) 68model.add(Dropout(0.2)) 69model.add(Dense(num_classes, activation='softmax')) 70 71model.summary() 72 73model.compile(loss='categorical_crossentropy', 74 optimizer=RMSprop(), 75 metrics=['accuracy']) 76 77history = model.fit(x_train, y_train, 78 batch_size=batch_size, 79 epochs=epochs, 80 verbose=1) 81#score = model.evaluate(x_test, y_test, verbose=0) 82#print('Test loss:', score[0]) 83#print('Test accuracy:', score[1])
補足情報(FW/ツールのバージョンなど)
- プログラムは、最低限tensorflowを動かせる状態にしたいので、評価はせずに学習だけするようにしています。
- datasetの画像データは一つの数値に100枚の画像を用意しています。サイズは64×64です。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/09 04:51
2018/10/09 05:10
2018/10/09 07:56