Python keras不兼容的数据形状

Python keras不兼容的数据形状,python,tensorflow,keras,Python,Tensorflow,Keras,我正在努力使用我的第一个tensorflow代码。我试图定义哪幅图片是苹果还是梨,但输出给了我错误。请帮帮我! 每次运行代码都会得到相同的结果有什么问题 x_train, x_test, y_train, y_test = \ train_test_split(x, y) xy = (x_train, x_test, y_train, y_test) np.save('C:train.npy',xy) x_train, x_test, y_train, y_test = np.loa

我正在努力使用我的第一个tensorflow代码。我试图定义哪幅图片是苹果还是梨,但输出给了我错误。请帮帮我! 每次运行代码都会得到相同的结果有什么问题

x_train, x_test, y_train, y_test = \
    train_test_split(x, y)
xy = (x_train, x_test, y_train, y_test)

np.save('C:train.npy',xy)

x_train, x_test, y_train, y_test = np.load('train.npy',allow_pickle=True)

x_train = x_train.astype('float') / 256
x_test = x_test.astype('float') /256


model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=x_train.shape[1:], padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))

model.add(Conv2D(64, (3, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

# 전결합층
model.add(Flatten())    # 벡터형태로 reshape
model.add(Dense(512))   # 출력
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(1))
model.add(Activation('sigmoid'))
# 모델 구축하기
model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

hdf5_file = "7obj-model.hdf5"
if os.path.exists(hdf5_file):
    model.load_weights(hdf5_file)
else:
    model.fit(x_train, y_train, batch_size=32, nb_epoch=10)
    model.save_weights(hdf5_file)

score = model.evaluate(x_test, y_test)
print('loss = ', score[0])
print('accuracy = ', score[1])

img_list = ['apple.jpg', 'apple.jpg', 'pear.jpg', 'pear1.jpg', 'pear2.jpg', 'pear3.jpg', 'pear4.jpg'] # test pictures
for test_image in img_list:
    img = Image.open(test_image)
    img = img.convert("RGB")
    img = img.resize((64,64))
    data = np.asarray(img)
    X = np.array(data)
    X = X.astype("float") / 256
    X = X.reshape(-1, 64, 64,3)
    # 예측
    pred = model.predict(X)
    result = [np.argmax(value) for value in pred]
    print('New data category : ',categories[result[0]]) # return prediction
输出,我发布了它,可能还有错误行85:

      File "C:/Users/심현규/PycharmProjects/yoyeo2012/main.py", line 85, in <module>
        model.load_weights(hdf5_file
        raise ValueError("Shapes %s and %s are incompatible" % (self, other))
    ValueError: Shapes (512, 1) and (512, 2) are incompatible
文件“C:/Users/심현규/PycharmProjects/yoyeo2012/main.py”,第85行,in
模型。加载权重(hdf5\u文件
raise VALUERROR(“形状%s和%s不兼容”%(自身、其他))
ValueError:形状(512,1)和(512,2)不兼容

显示生成错误的行。可能是model.fit(…)。模型输出的最后一个尺寸是1。检查y_列的形状。我想它的最后一个尺寸是2。我编辑了错误。你能检查我如何更改代码吗?文件“7obj model.hdf5”与您的模型不关联。它可能是从以前的版本保存的。请重命名它,然后重新运行