model_vgg16 = VGG16(include_top=False, weights='imagenet', input_shape=(48, 48, 3))
for layer in model_vgg16.layers:
layer.train = False
model = Flatten(name="faltten")(model_vgg16.output)
model = Dense(4096, activation="relu")(model)
model = Dense(4096, activation="relu")(model)
model = Dropout(0.5)(model)
model = Dense(10, activation="softmax")(model)
vgg16_Model = Model(inputs=model_vgg16.input, outputs=model)
sgd = SGD(learning_rate=0.001)
vgg16_Model = vgg16_Model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
# 导入数据
(x_train, y_train), (x_test, y_test) = mnist.load_data("../data")
print(x_test)
x_train = [cv2.cvtColor(cv2.resize(i, (48, 48)), cv2.COLOR_GRAY2RGB) for i in x_train]
x_train = np.concatenate([arr[np.newaxis] for arr in x_train])
print(x_train.shape)
x_test = [cv2.cvtColor(cv2.resize(i, (48, 48)), cv2.COLOR_GRAY2RGB) for i in x_test]
x_test = np.concatenate([arr[np.newaxis] for arr in x_test])
# one-hot of y
def yonehot(y):
y_one = np.zeros((10))
y_one[y] = 1
return y_one
y_test = np.array(yonehot(y_test[i] for i in range(len(y_test))))
y_train = np.array(yonehot(y_train[i] for i in range(len(y_train))))
vgg16_Model.fit(x_train, y_train)
model = Dense(4096, activation="relu")(model) 4096是规定死的吗?
vgg16_Model = vgg16_Model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
loss是怎么规定的?