如何训练模型以识别一张图片中的五个数字。
以下是代码:
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dropout, Dense, Input
from keras.models import Model, Sequential
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 140, 1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dropout(0.5))
这里应该有一个循环来识别图片中的每个数字,但我不知道如何实现。
model.add(Dense(11, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.fit(X_train, y_train,
batch_size=1000,
epochs=8,
verbose=1,
validation_data=(X_valid, y_valid))
合并的MNIST数字图片如下所示: