我使用EMNIST生成了一个数据集,每个图像包含一个字符或两个字符。图像尺寸为28×56(高x宽)。
我主要是想预测给定图像中的一个或两个字符。我不确定应该采用哪种架构来实现这一点。共有62个字符类别。
对于单个字符,y = [23]
对于两个字符,y = [35,11]
我尝试了以下方法。
- 我尝试通过CTC来实现,但遇到了无法解决的无限损失问题。
- 我用62填充单个字符的真实标签以表示空白字符,并训练了一个具有以下层的CNN。
print()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)y_train = sequence.pad_sequences(y_train, padding='post', value = 62)y_test = sequence.pad_sequences(y_test,padding='post', value = 62)X_train = X_train/255.0X_test = X_test/255.0input_shape = (28, 56, 1)model = Sequential()model.add(Conv2D(filters=72, kernel_size=(11,11), padding = 'same', activation='relu',input_shape=input_shape))model.add(MaxPooling2D(pool_size=(2,2),strides=2))model.add(Conv2D(filters=144, kernel_size=(7,7) , padding = 'same', activation='relu'))model.add(Conv2D(filters=144, kernel_size=(3,3) , padding = 'same', activation='relu'))model.add(MaxPooling2D(pool_size=(2,2)))model.add(Flatten())model.add(Dense(units=1024, activation='relu'))model.add(Dropout(.5))model.add(Dense(512, activation='relu'))model.add(Dense(units=2, activation='relu'))model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])model.summary()batch_size = 128steps = math.ceil(X_train.shape[0]/batch_size)datagen = ImageDataGenerator( featurewise_center=False, # set input mean to 0 over the dataset samplewise_center=False, # set each sample mean to 0 featurewise_std_normalization=False, # divide inputs by std of the dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180) zoom_range = 0.2, # Randomly zoom image width_shift_range=0.2, # randomly shift images horizontally (fraction of total width) height_shift_range=0.1, # randomly shift images vertically (fraction of total height) horizontal_flip=False, # randomly flip images vertical_flip=False)history = model.fit_generator(datagen.flow(X_train,y_train, batch_size=batch_size), epochs = 6, validation_data = (X_test, y_test), verbose = 1,steps_per_epoch=steps)
我能够在验证集上达到大约90%的准确率。然而,当我输入生成的图像来查看其预测结果时,预测结果与正确分类相差几个字符。我在创建模型或预处理数据的方式上是否有问题?
回答:
我已经认识到我的错误。我试图使用回归方法来解决这个问题,而实际上这是一个分类问题。