Keras二进制分类精度停留在50%

时间:2019-11-03 23:51:56

标签: r machine-learning keras classification conv-neural-network

我在R中使用keras来构建猫/狗二进制分类器,并使用3000个样本的数据集(1500只猫,1500条狗,9:1训练/测试比例)。在每个时期,我的分类精度不会超过50%。

将感谢您对可能出现问题的任何帮助。我对测试和培训目录进行了三重检查,split/testing/Cat/包含150张猫图像,split/testing/Dog/包含150张狗图像,split/training/Cat/包含1350张猫图像,split/training/Dog/包含1350张狗图像。这是我第一次使用keras并运行CNN图像分类器,因此我将不胜感激。

model <- keras_model_sequential() %>%
  layer_conv_2d(filters = 32, 
     kernel_size = c(3,3), 
     activation = "relu", 
     input_shape = c(256, 256, 3)) %>%
  layer_max_pooling_2d(pool_size = c(2,2)) %>%
  layer_conv_2d(filters = 32, 
     kernel_size = c(3,3), 
     activation = "relu", 
     input_shape = c(256, 256, 3)) %>%
  layer_max_pooling_2d(pool_size = c(2,2)) %>%
  layer_dropout(rate = 0.25) %>%
  layer_flatten() %>%
  layer_dense(units = 256, activation = "relu") %>%
  layer_dense(units = 1, activation = "sigmoid") %>%
  compile(optimizer = 'adam', 
          loss = 'binary_crossentropy', 
          metrics = c("accuracy"))

datagen <- image_data_generator(rescale = 1/255)

train_generator <- flow_images_from_directory(directory = "split/training/",
   generator = datagen, 
   target_size = c(256, 256), 
   classes = c("Cat","Dog"), 
   class_mode = "binary", 
   batch_size = 32)
test_generator <- flow_images_from_directory(directory = "split/testing/",
   generator = datagen, 
   target_size = c(256, 256), 
   classes = c("Cat","Dog"), 
   class_mode = "binary", 
   batch_size = 32)

model %>% fit_generator(generator = train_generator, 
   steps_per_epoch = as.integer(train_generator$n / 32), 
   epochs = 5, 
   verbose = 1, 
   validation_data = test_generator)

我的模型如下:

> summary(model)
Model: "sequential_11"
______________________________________________________________________________
Layer (type)                       Output Shape                   Param #     
==============================================================================
conv2d_10 (Conv2D)                 (None, 254, 254, 32)           896         
______________________________________________________________________________
max_pooling2d_10 (MaxPooling2D)    (None, 127, 127, 32)           0           
______________________________________________________________________________
conv2d_11 (Conv2D)                 (None, 125, 125, 32)           9248        
______________________________________________________________________________
max_pooling2d_11 (MaxPooling2D)    (None, 62, 62, 32)             0           
______________________________________________________________________________
dropout_7 (Dropout)                (None, 62, 62, 32)             0           
______________________________________________________________________________
flatten_5 (Flatten)                (None, 123008)                 0           
______________________________________________________________________________
dense_13 (Dense)                   (None, 256)                    31490304    
______________________________________________________________________________
dense_14 (Dense)                   (None, 1)                      257         
==============================================================================
Total params: 31,500,705
Trainable params: 31,500,705
Non-trainable params: 0
______________________________________________________________________________

我的纪元摘要如下:

> model %>% fit_generator(generator = train_generator, steps_per_epoch = as.integer(train_generator$n / 32), epochs = 5, verbose = 1, validation_data = test_generator)
Found 2700 images belonging to 2 classes.
Found 300 images belonging to 2 classes.
Epoch 1/5
84/84 [==============================] - 83s 984ms/step - loss: 7.5916 - accuracy: 0.4974 - val_loss: 7.5069 - val_accuracy: 0.5000
Epoch 2/5
84/84 [==============================] - 82s 977ms/step - loss: 7.6703 - accuracy: 0.5004 - val_loss: 7.8263 - val_accuracy: 0.5000
Epoch 3/5
84/84 [==============================] - 83s 984ms/step - loss: 7.7310 - accuracy: 0.4970 - val_loss: 7.8263 - val_accuracy: 0.5000

0 个答案:

没有答案