训练模型字符识别的准确性并未提高

时间:2020-05-23 06:36:30

标签: python tensorflow machine-learning keras neural-network

我正在为我的字符识别系统构建训练模型。在每个时期,我都获得相同的准确性,但并没有提高。我目前有4000张训练图像和77张验证图像。 我的模型如下:

inputs = Input(shape=(32,32,3))
x = Conv2D(filters = 64, kernel_size = 5, activation = 'relu')(inputs)
x = MaxPooling2D()(x)
x = Conv2D(filters = 32,        
kernel_size = 3,            
activation = 'relu')(x)  
x = MaxPooling2D()(x) 
x = Flatten()(x) 
x=Dense(256,
activation='relu')(x)
outputs = Dense(1, activation = 'softmax')(x) 
model = Model(inputs = inputs, outputs = outputs) 
model.compile(
optimizer='adam', 
loss='categorical_crossentropy', 
metrics=['accuracy']) 
data_gen_train = ImageDataGenerator(rescale=1/255)

data_gen_test=ImageDataGenerator(rescale=1/255)

data_gen_valid = ImageDataGenerator(rescale=1/255)

train_generator = data_gen_train.flow_from_directory(directory=r"./drive/My Drive/train_dataset", 
target_size=(32,32), batch_size=10, class_mode="binary")

valid_generator = data_gen_valid.flow_from_directory(directory=r"./drive/My 
                  Drive/validation_dataset", target_size=(32,32), batch_size=2, class_mode="binary")

test_generator = data_gen_test.flow_from_directory(
                 directory=r"./drive/My Drive/test_dataset",
                 target_size=(32, 32),

                 batch_size=6,
                 class_mode="binary"
)
model.fit(
train_generator,
epochs =10, 
steps_per_epoch=400,
validation_steps=37,
validation_data=valid_generator) 

结果如下:

Found 4000 images belonging to 2 classes.
Found 77 images belonging to 2 classes.
Found 6 images belonging to 2 classes.
Epoch 1/10
400/400 [==============================] - 14s 35ms/step - loss: 0.0000e+00 - accuracy: 0.5000 - val_loss: 0.0000e+00 - val_accuracy: 0.5811
Epoch 2/10
400/400 [==============================] - 13s 33ms/step - loss: 0.0000e+00 - accuracy: 0.5000 - val_loss: 0.0000e+00 - val_accuracy: 0.5811
Epoch 3/10
400/400 [==============================] - 13s 34ms/step - loss: 0.0000e+00 - accuracy: 0.5000 - val_loss: 0.0000e+00 - val_accuracy: 0.5676
Epoch 4/10
400/400 [==============================] - 13s 33ms/step - loss: 0.0000e+00 - accuracy: 0.5000 - val_loss: 0.0000e+00 - val_accuracy: 0.5676
Epoch 5/10
400/400 [==============================] - 18s 46ms/step - loss: 0.0000e+00 - accuracy: 0.5000 - val_loss: 0.0000e+00 - val_accuracy: 0.5541
Epoch 6/10
400/400 [==============================] - 13s 34ms/step - loss: 0.0000e+00 - accuracy: 0.5000 - val_loss: 0.0000e+00 - val_accuracy: 0.5676
Epoch 7/10
400/400 [==============================] - 13s 33ms/step - loss: 0.0000e+00 - accuracy: 0.5000 - val_loss: 0.0000e+00 - val_accuracy: 0.5676
Epoch 8/10
400/400 [==============================] - 13s 33ms/step - loss: 0.0000e+00 - accuracy: 0.5000 - val_loss: 0.0000e+00 - val_accuracy: 0.5946
Epoch 9/10
400/400 [==============================] - 13s 33ms/step - loss: 0.0000e+00 - accuracy: 0.5000 - val_loss: 0.0000e+00 - val_accuracy: 0.5811
Epoch 10/10
400/400 [==============================] - 13s 33ms/step - loss: 0.0000e+00 - accuracy: 0.5000 - val_loss: 0.0000e+00 - val_accuracy: 0.5811
<tensorflow.python.keras.callbacks.History at 0x7fa3a5f4a8d0>

1 个答案:

答案 0 :(得分:2)

如果您试图识别2个类别的字符,则应该:

  • class_mode="binary"函数中使用flow_from_directory
  • binary_crossentropy用作损失
  • 您的最后一层必须具有1个具有sigmoid激活功能的神经元

如果有两个以上的班级:

  • 请勿在{{1​​}}函数中使用class_mode="binary"
  • flow_from_directory用作损失
  • 您的最后一层必须具有激活categorical_crossentropy n 个神经元,其中 n 代表类数