Q&A
識別(りんごとオレンジ)https://qiita.com/hiroeorz@github/items/ecb39ed4042ebdc0a957
上記のプログラム参考にしてRGBではなくグレースケールに変換して読み込もうとしました。
しかし、
alueError: Error when checking input: expected dense_1_input to have 2 dimensios, but got array with shape (612, 1, 360000)
というエラーが出ました。
612は訓練データの数・1は1次元配列?・360000は一枚の画像の総ピクセル数だと思われます。
python
1image =np.reshape(image, (1,360000))
これを変換してからエラーが出たのこれが原因だと思います。。。
ちなみに、グレースケールの画像の読み込みは出来ていました。
以下にソースを載せているのでおかしい所を教えてください。
宜しくお願い致します。
python
1 2from keras.models import Sequential 3from keras.layers import Activation, Dense, Dropout 4from keras.utils.np_utils import to_categorical 5from keras.optimizers import Adagrad 6from keras.optimizers import Adam 7import numpy as np 8from PIL import Image 9import os 10 11# 学習用のデータを作る. 12image_list = [] 13label_list = [] 14 15# ./data/train 以下のorange,appleディレクトリ以下の画像を読み込む。 16for dir in os.listdir("data/train"): 17 if dir == ".DS_Store": 18 continue 19 20 dir1 = "data/train/" + dir 21 label = 0 22 23 if dir == "a": # appleはラベル0 24 label = 0 25 elif dir == "b": # orangeはラベル1 26 label = 1 27 28 for file in os.listdir(dir1): 29 if file != ".DS_Store": 30 # 配列label_listに正解ラベルを追加(りんご:0 オレンジ:1) 31 label_list.append(label) 32 filepath = dir1 + "/" + file 33 # 画像を25x25pixelに変換し、1要素が[R,G,B]3要素を含む配列の25x25の2次元配列として読み込む。 34 # [R,G,B]はそれぞれが0-255の配列。 35 image = np.array(Image.open(filepath).resize((600, 600))) 36 print(image.shape) 37 print(filepath) 38 image =np.reshape(image, (1,360000)) 39 print(image.shape) 40 print('\n') 41 # 出来上がった配列をimage_listに追加。 42 image_list.append(image / 255.) 43 44# kerasに渡すためにnumpy配列に変換。 45image_list = np.array(image_list) 46 47# ラベルの配列を1と0からなるラベル配列に変更 48# 0 -> [1,0], 1 -> [0,1] という感じ。 49Y = to_categorical(label_list) 50 51# モデルを生成してニューラルネットを構築 52model = Sequential() 53 54model.add(Dense(32, input_dim=360000)) 55model.add(Activation("relu")) 56model.add(Dropout(0.8)) 57 58model.add(Dense(1500)) 59model.add(Activation("relu")) 60model.add(Dropout(0.8)) 61 62 63model.add(Dense(1000)) 64model.add(Activation("relu")) 65model.add(Dropout(0.8)) 66 67model.add(Dense(500)) 68model.add(Activation("relu")) 69model.add(Dropout(0.8)) 70 71 72 73model.add(Dense(2)) 74model.add(Activation("softmax")) 75 76# オプティマイザにAdamを使用 77opt = Adam(lr=0.001) 78# モデルをコンパイル 79model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) 80# 学習を実行。10%はテストに使用。 81model.fit(image_list, Y, nb_epoch=100, batch_size=100, validation_split=0.1) 82 83# テスト用ディレクトリ(./data/train/)の画像でチェック。正解率を表示する。 84total = 0. 85ok_count = 0. 86 87for dir in os.listdir("data/train"): 88 if dir == ".DS_Store": 89 continue 90 91 dir1 = "data/test/" + dir 92 label = 0 93 94 if dir == "a": 95 label = 0 96 elif dir == "b": 97 label = 1 98 99 for file in os.listdir(dir1): 100 if file != ".DS_Store": 101 label_list.append(label) 102 filepath = dir1 + "/" + file 103 image = np.array(Image.open(filepath).resize((600, 600))) 104 print(filepath) 105 #image = image.transpose(2, 0, 1) 106 #image = image.reshape(1, image.shape[0] * image.shape[1] * image.shape[2]).astype("float32")[0] 107 #iamge = image.reshape(1,360000).astype("float32")[0] 108 image =np.reshape(image, (1,360000)) 109 110 result = model.predict_classes(np.array([image / 255.])) 111 print("label:", label, "result:", result[0]) 112 113 total += 1. 114 115 if label == result[0]: 116 ok_count += 1. 117 118print("seikai: ", ok_count / total * 100, "%") 119
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2018/06/28 01:58