ValueError: Input 0 of layer conv2d_7 is incompatible with the layer: : expected min_ndim=4, found ndim=1. Full shape received: [None]
というerrorがでましたが、なにが原因でerrorが出ているのかこの文章から理解できません。このerrorは、mainという関数を呼び出した時に出てきました。Errorの原因と、その直し方を教えてください。
(環境:google Colaboratory)
from PIL import Image
import os, glob
import numpy as np
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
classes = ["Jomon", "Yayoi"]
num_classes = len(classes)
image_size = 64
num_testdata =25
x_train =[]
x_test = []
y_train = []
y_test = []
for index, classlabel in enumerate(classes):
photos_dir = "./"+ classlabel
files = glob.glob(photos_dir + "/*.jpg")
for i, file in enumerate(files):
image = Image.open(file)
image = image.convert("RGB")
image = image.resize((image_size, image_size))
data = np.asarray(image)
if i < num_testdata:
x_test.append(data)
y_test.append(index)
for angle in range(-20, 20, 5):
img_r = image.rotate(angle)
data = np.asarray(img_r)
x_train.append(data)
y_train.append(index)
img_trains = img_r.transpose(Image.FLIP_LEFT_RIGHT) data = np.asarray(img_trains) x_train.append(data) y_train.append(index)
x_train = np.array(x_train)
x_test = np.array(x_test)
y_train = np.array(y_train)
y_test = np.array(y_test)
xy = (x_train, x_test, y_train, y_test)
np.save("./alien_exam.npy", xy)
from keras.models import Sequential#a plain stack of layers where each layer has exactly one input tensor and one output tensor.
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.optimizers import RMSprop
from keras.utils import np_utils
import keras
import numpy as np
classes = ["Jomon", "Yayoi"]
num_classes = len(classes)
image_size = 64
"""
データを読み込み
"""
def load_data():
x_train, x_test, y_train, y_test = np.load("./alien_exam.npy", allow_pickle=True)
x_train = x_train.astype("float") /255
x_test = x_test.astype("float") /255
y_train = np_utils.to_categorical(y_train, num_classes)
y_tets = np_utils.to_categorical(y_test, num_classes)
return x_train, y_train, x_test, y_test
def train(x, y, x_test, y_test):
model = Sequential()
model.add(Conv2D(32,(3,3), padding="same",input_shape=x.shape[1:]))
model.add(Acticaiton("relu"))
model.add(Conv2D(32,(3,3)))
model.add(Acticaiton("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation("relu"))
model.add(Dropout(0.45))
model.add(Dense(2))
model.add(Activation("softmax"))
opt = RMSprop(lr=0.00005, decay=le-6)
model.compile(lostt="categorical_cossentropy", optimizer=opt, metrics=["accuracy"])
model.fit(x, y, batch_size=28, epochs=40)
model.save("./cnn.h1")
return model
def main():
x_train, y_train, x_test, y_test = load_data()
model = train(x_train, y_train, x_test, y_test)
main()
#ここでErrorがでました。
回答1件
あなたの回答
tips
プレビュー