Keras用于多位识别

时间:2017-02-07 04:58:17

标签: python keras conv-neural-network mnist

我试图通过Keras(theano后端)的一些练习来了解CNN。我无法使用下面的模型(错误:AttributeError:' Convolution2D'对象没有属性' get_shape')。该数据集是连接在一起的MNIST数据的图像(28 * 28),最多五个图像。因此输入形状应为1,28,140(灰度= 1,高度= 28,宽度= 28 * 5)

目标是预测数字的顺序。谢谢!!

batch_size = 128
nb_classes = 10
nb_epoch = 2

img_rows =28
img_cols=140
img_channels = 1

model_input=(img_channels, img_rows, img_cols)

x = Convolution2D(32, 3, 3, border_mode='same')(model_input)
x = Activation('relu')(x)
x = Convolution2D(32, 3, 3)(x)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.25)(x)
conv_out = Flatten()(x)

x1 = Dense(nb_classes, activation='softmax')(conv_out)
x2 = Dense(nb_classes, activation='softmax')(conv_out)
x3 = Dense(nb_classes, activation='softmax')(conv_out)
x4 = Dense(nb_classes, activation='softmax')(conv_out)
x5 = Dense(nb_classes, activation='softmax')(conv_out)

lst = [x1, x2, x3, x4, x5]

model = Sequential(input=model_input, output=lst)

model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])

model.fit(dataset, data_labels, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1)

1 个答案:

答案 0 :(得分:4)

问题出在输入层。进行以下更改:

model_input=Input(shape=(img_channels, img_rows, img_cols))

如果您的image_dim_orderingth。从Input导入keras.layers图层。

我还注意到有多个输出。所以你需要使用Function模型而不是Sequential。只需将其更改为:

model = Model(input=model_input, output=lst)

Model导入keras.models