我想从以下层开始训练深度网络:
def cnn_model():
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',
input_shape=(3,48, 48),
activation='relu'))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3, 3), padding='same',
activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(128, (3, 3), padding='same',
activation='relu'))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(NUM_CLASSES, activation='softmax'))
return model
model = cnn_model()
# let's train the model using SGD + momentum (how original).
lr = 0.01
sgd = SGD(lr=lr, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
def lr_schedule(epoch):
return lr*(0.1**int(epoch/10))
使用以下生成器:
model = cnn_model()
# let's train the model using SGD + momentum
lr = 0.01
sgd = SGD(lr=lr, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
def lr_schedule(epoch):
return lr * (0.1 ** int(epoch / 10))
batch_size = 32
epochs = 30
model.fit(X, Y,
batch_size=batch_size,
epochs=epochs,
validation_split=0.2,
callbacks=[LearningRateScheduler(lr_schedule),
ModelCheckpoint('model.h5', save_best_only=True)]
)
(验证生成器看起来很相似)。
在训练期间,我收到错误:
ValueError: Error when checking input: expected conv2d_175_input to have 4 dimensions, but got array with shape (0, 1)
输入print(model.summary())
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_175 (Conv2D) (None, 32, 48, 48) 896
_________________________________________________________________
conv2d_176 (Conv2D) (None, 32, 46, 46) 9248
_________________________________________________________________
max_pooling2d_85 (MaxPooling (None, 32, 23, 23) 0
_________________________________________________________________
dropout_103 (Dropout) (None, 32, 23, 23) 0
_________________________________________________________________
conv2d_177 (Conv2D) (None, 64, 23, 23) 18496
_________________________________________________________________
conv2d_178 (Conv2D) (None, 64, 21, 21) 36928
_________________________________________________________________
max_pooling2d_86 (MaxPooling (None, 64, 10, 10) 0
_________________________________________________________________
dropout_104 (Dropout) (None, 64, 10, 10) 0
_________________________________________________________________
conv2d_179 (Conv2D) (None, 128, 10, 10) 73856
_________________________________________________________________
conv2d_180 (Conv2D) (None, 128, 8, 8) 147584
_________________________________________________________________
max_pooling2d_87 (MaxPooling (None, 128, 4, 4) 0
_________________________________________________________________
dropout_105 (Dropout) (None, 128, 4, 4) 0
_________________________________________________________________
flatten_28 (Flatten) (None, 2048) 0
_________________________________________________________________
dense_43 (Dense) (None, 512) 1049088
_________________________________________________________________
dropout_106 (Dropout) (None, 512) 0
_________________________________________________________________
dense_44 (Dense) (None, 43) 22059
=================================================================
Total params: 1,358,155
Trainable params: 1,358,155
Non-trainable params: 0
我试过了什么? 我正在阅读关于“Convolution2D”/“输入形状”https://keras.io/layers/convolutional/的文档,所以我试图更改input_shape,我知道我的输出应该是4D但我不知道该怎么做。 如果你能提供帮助,请分享。
谢谢
答案 0 :(得分:0)
正如@nuric所提到的,问题在于输入图像尺寸。
如果输入图像的高度为48,宽度为48,输入通道为3,结果为(3,48,48),那么您对模型的输入为(批量大小,3,48,48)。
这里批量大小只是给予训练模型的单批训练图像数量
伪代码
training_list = []
for i in range(0, batch_size):
image = read_image(training_set[i])
training_list.append(image)
training_set = numpy.array(training_list)
所以现在训练集的维度是(批量大小,3,48,48)