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

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

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

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python

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

Q&A

解決済

1回答

1837閲覧

tensorflowで自作のdatasetを用いて学習させたい

background

総合スコア12

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python

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

0グッド

0クリップ

投稿2018/10/09 01:20

前提・実現したいこと

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です。

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

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

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

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

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

guest

回答1

0

ベストアンサー

reshape のエラー

x_train = x_train.reshape(1000, 4096) x_test = x_test.reshape(1000, 4096)

reshape 前の x_train、y_train の形状が (1000, 64, 64) であることを確認してください。

ネットワークの入力

model.add(Dense(512, activation='relu', input_shape=(784,)))

MNIST は 28x28 なので 784 でしたが、今回は 64x64 なので、ここも 4096 に変えてあげないといけません。

model.add(Dense(512, activation='relu', input_shape=(4096,)))

投稿2018/10/09 03:00

tiitoi

総合スコア21956

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

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

background

2018/10/09 04:51

## reshape のエラー ありがとうございます。 x_trainに代入する変数はimageなので、imageの形状を調べてみたところ次のようになっていました。 '''python >>> print(image.shape) (64, 64, 3) ''' これは読み取り方が良くなかったということでしょうか? ## ネットワークの入力 コピペしたものをそのまま使用していたため、修正を忘れていました。 ありがとうございました!
tiitoi

2018/10/09 05:10

画像はグレースケールではなく、カラー画像でしょうか? だとしたら、4096 ではなく、64x64x3=12288 になると思います。
background

2018/10/09 07:56

画像の読み取りで12288と指定したらうまく動きました。 回答ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問