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

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

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

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

Python

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

Q&A

解決済

1回答

1988閲覧

ValueErrorが出てしまいます

syosinnsya23

総合スコア9

Keras

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

Python

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

0グッド

0クリップ

投稿2018/09/02 03:14

初めて質問させていただきます
今回kerasで画像分類をしようと思ったのですが

エラーコード ValueError: Error when checking input: expected dense_1_input to have shape (1875,) but got array with shape (1,)

このようなエラーが出てきて困っています。

プログラムは
https://qiita.com/hiroeorz@github/items/ecb39ed4042ebdc0a957
このサイトからコピペして

from keras.models import Sequential from keras.layers import Activation, Dense, Dropout from keras.utils.np_utils import to_categorical from keras.optimizers import Adagrad from keras.optimizers import Adam import numpy as np from PIL import Image import os # 学習用のデータを作る. image_list = [] label_list = [] # ./data/train 以下のorange,appleディレクトリ以下の画像を読み込む。 for dir in os.listdir("data/train"): if dir == ".DS_Store": continue dir1 = "data/train/" + dir label = 0 if dir == "apple": # appleはラベル0 label = 0 elif dir == "orange": # orangeはラベル1 label = 1 for file in os.listdir(dir1): if file != ".DS_Store": # 配列label_listに正解ラベルを追加(りんご:0 オレンジ:1) label_list.append(label) filepath = dir1 + "/" + file # 画像を25x25pixelに変換し、1要素が[R,G,B]3要素を含む配列の25x25の2次元配列として読み込む。 # [R,G,B]はそれぞれが0-255の配列。 image = np.array(Image.open(filepath).resize((25, 25))) print(filepath) # 配列を変換し、[[Redの配列],[Greenの配列],[Blueの配列]] のような形にする。 image = image.transpose(2, 0, 1) # さらにフラットな1次元配列に変換。最初の1/3はRed、次がGreenの、最後がBlueの要素がフラットに並ぶ。 image = image.reshape(1, image.shape[0] * image.shape[1] * image.shape[2]).astype("float32")[0] # 出来上がった配列をimage_listに追加。 image_list.append(image / 255.) # kerasに渡すためにnumpy配列に変換。 image_list = np.array(image_list) # ラベルの配列を1と0からなるラベル配列に変更 # 0 -> [1,0], 1 -> [0,1] という感じ。 Y = to_categorical(label_list) # モデルを生成してニューラルネットを構築 model = Sequential() model.add(Dense(200, input_dim=1875)) model.add(Activation("relu")) model.add(Dropout(0.2)) model.add(Dense(200)) model.add(Activation("relu")) model.add(Dropout(0.2)) model.add(Dense(2)) model.add(Activation("softmax")) # オプティマイザにAdamを使用 opt = Adam(lr=0.001) # モデルをコンパイル model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # 学習を実行。10%はテストに使用。 model.fit(image_list, Y, nb_epoch=1500, batch_size=100, validation_split=0.1) # テスト用ディレクトリ(./data/train/)の画像でチェック。正解率を表示する。 total = 0. ok_count = 0. for dir in os.listdir("data/train"): if dir == ".DS_Store": continue dir1 = "data/test/" + dir label = 0 if dir == "apple": label = 0 elif dir == "orange": label = 1 for file in os.listdir(dir1): if file != ".DS_Store": label_list.append(label) filepath = dir1 + "/" + file image = np.array(Image.open(filepath).resize((25, 25))) print(filepath) image = image.transpose(2, 0, 1) image = image.reshape(1, image.shape[0] * image.shape[1] * image.shape[2]).astype("float32")[0] result = model.predict_classes(np.array([image / 255.])) print("label:", label, "result:", result[0]) total += 1. if label == result[0]: ok_count += 1. print("seikai: ", ok_count / total * 100, "%")

このような感じです。

変更したのは画像を読み込む

for dir in os.listdir("data/train"): if dir == ".DS_Store": continue dir1 = "data/train/" + dir label = 0 if dir == "apple": # appleはラベル0 label = 0 elif dir == "orange": # orangeはラベル1 label = 1

この部分を自分のファイル名に合うようにして、ピクセル数を64*64にしました

何故このようなエラーが出てしまったのでしょうか。

回答よろしくお願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

入力こ次元がおかしいと言われているので、
print(image_list.shape)
を確かめてみてください。

エラーは省略しないことをお勧めします。
エラーが出うるところが2ヶ所あって、fitとpredict_classesです。
省略するとどっちでエラーが出ているのかわからなくなります。

投稿2018/09/02 09:21

mkgrei

総合スコア8560

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

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

syosinnsya23

2018/09/03 15:27

言われた通り確かめてみたら、次元がおかしくなっていたので直したら動きました。 アドバイスありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問