我正在努力处理我的第一个tensorflow代码。我正在尝试定义哪张照片是苹果或梨。但是,即使图片显然是梨,它也会返回相同的苹果值。该代码可以正常工作。我该如何修改?
x= np.array(x)
y = np.array(y)
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(2))
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])