深層学習で2つの画像を見分けるというものを実装しているのですが
import imageio import numpy as np from PIL import Image X = [] y = [] for i in range(1, 61): filepath = f"/Users/K/Downloads/hifu_photos/normal/normal-{i:03d}.png" img = Image.open(filepath) img = img.convert("RGBA") img.save(f"/Users/K/Downloads/hifu_photos/normal/normal-{i:03d}.png") filepath = f"/Users/K/Downloads/hifu_photos/melano/melano-{i:03d}.png" img = Image.open(filepath) img = img.convert("RGBA") ## RGBAカラーに変換 img.save(f"/Users/K/Downloads/hifu_photos/melano/melano-{i:03d}.png") p1 = imageio.imread(f"/Users/K/Downloads/hifu_photos/melano/melano-{i:03d}.png").reshape(150,150,4) X = np.append(X, p1) y = np.append(y, np.array([0], dtype = np.uint8)).reshape(-1,1) p2 =imageio.imread(f"/Users/K/Downloads/hifu_photos/normal/normal-{i:03d}.png").reshape(150,150,4) X = np.append(X, p2) y = np.append(y, np.array([1], dtype = np.uint8)).reshape(-1,1) X_train = X[:48] y_train = y[:48] X_test = X[48:] y_test = y[48:] print(X_train.shape) rom keras.models import Sequential from keras.layers import Dense, Dropout, Flatten from keras.layers import Conv2D, MaxPooling2D from keras.optimizers import Adam import time model = Sequential() model.add(Conv2D(16, (3, 3), input_shape=(150, 150, 4), activation='relu')) model.add(Conv2D(32, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) # (A) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) # (B) model.add(Dropout(0.25)) # (C) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(0.25)) # (D) model.add(Dense(2, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer=Adam(), metrics=['accuracy']) startTime = time.time() history = model.fit(X_train, y_train, batch_size=1000, epochs=20,verbose=1, validation_data=(X_test, y_test)) loss, accuracy = model.evaluate(X_test, y_test) print("loss:{} accuracy:{}".format(loss, accuracy)) print("Computation time:{0:.3f} sec".format(time.time() - startTime))
のようにコードを書くと
---> 26 history = model.fit(X_train, y_train, batch_size=1000, epochs=20,verbose=1, validation_data=(X_test, y_test)) ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 1]
のようにエラーが出てしまいます。どこかおかしい場所はありますでしょうか。。。
回答1件
あなたの回答
tips
プレビュー