简体   繁体   English

VGG16 对痤疮分级的准确度极低

[英]VGG16 extremely low accuracy on acne grading

I am trying to build an ance grading classifier with 3 classes (Level 0, Level 1, Level 2).我正在尝试构建一个具有 3 个类别(0 级、1 级、2 级)的 ance 分级分类器。 I do not have a lot of images in the dataset (around 500 in each class).我在数据集中没有很多图像(每个类大约 500 个)。 Therefore I used the VGG16 pre-trained model.因此我使用了 VGG16 预训练的 model。 However, the accuracy is really low (~0.33) and barely increased along with the training.然而,准确率非常低(~0.33),并且随着训练几乎没有增加。

# Load VGG16 model
vgg_model = VGG16(weights="imagenet", 
                  include_top=False, 
                  input_tensor=Input(shape=(224,224,3)))
vgg_model.summary()

#make the model layers untrainable
for layer in vgg_model.layers:
    layer.trainable = False

# Add output layer
output_model = vgg_model.output
output_model = layers.Dropout(0.25)(output_model)
output_model = layers.Flatten()(output_model)
output_model = layers.Dense(128,activation="relu")(output_model)
output_model = layers.Dropout(0.5)(output_model)
output_model = layers.Dense(3,activation="softmax")(output_model)

vggmodel = models.Model(inputs=vgg_model.input, outputs=output_model)

vggmodel.summary()


# Image augmentation on training set
train_datagen = ImageDataGenerator(
    rotation_range = 40,
    width_shift_range = 0.2,
    height_shift_range = 0.2,
    shear_range = 0.15,
    rescale = 1./255,
)

# only rescale on validation set
validate_datagen = ImageDataGenerator(
    rescale = 1./255
)

#set size of batches of data to 64
batch_size = 64
#resizing to 224x224
target_size = (224, 224)

# initialize the training data augmentation object
train_generator = train_datagen.flow_from_directory(directory=training_path, class_mode="categorical",
                                                  batch_size=batch_size, target_size=target_size, color_mode='rgb', shuffle= True)
validation_generator = validate_datagen.flow_from_directory(directory=validation_path, class_mode="categorical",
                                                  batch_size=batch_size, target_size=target_size, color_mode='rgb', shuffle= True)

vggmodel.compile(loss="categorical_crossentropy", optimizer=SGD(0.01),metrics=["accuracy"])

earlystopping = keras.callbacks.EarlyStopping(monitor ="val_loss",  
                                        mode ="min", patience = 2,  
                                        restore_best_weights = True) 

vggmodel.fit(train_generator, steps_per_epoch=int(1166/batch_size), epochs= 100,
                        validation_data=validation_generator, validation_steps=5, callbacks=[earlystopping])

I have also tried building my own model but the performance is similar.我也尝试过构建自己的 model 但性能相似。

input_shape = (224,224,3)
cnn_model = models.Sequential()
cnn_model.add(layers.Conv2D(64, (3, 3), activation='relu',
                               input_shape=input_shape))
cnn_model.add(layers.Conv2D(64, (3, 3), activation='relu',
                                input_shape=input_shape))
cnn_model.add(layers.MaxPool2D((2, 2)))
cnn_model.add(layers.Conv2D(64, (3, 3), activation='relu',
                                input_shape=input_shape))
cnn_model.add(layers.MaxPool2D((2, 2)))
cnn_model.add(layers.Dropout(0.25))
cnn_model.add(layers.Flatten())
cnn_model.add(layers.Dense(128, activation='relu'))
cnn_model.add(layers.Dropout(0.5))
cnn_model.add(layers.Dense(3, activation='softmax'))
cnn_model.summary()

cnn_model.compile(loss="categorical_crossentropy", optimizer=SGD(0.01),metrics=["accuracy"])


earlystopping = keras.callbacks.EarlyStopping(monitor ="val_loss",  
                                        mode ="min", patience = 2,  
                                        restore_best_weights = True) 

cnn_model.fit(train_generator, steps_per_epoch=int(1248/batch_size), epochs= 15,
                        validation_data=validation_generator, validation_steps=2)

What did I do wrong?我做错了什么? Sorry, I am a beginner:/对不起,我是初学者:/

  1. Use the max-pooling layer, instead of a flatten layer.使用最大池化层,而不是展平层。
  2. This user had to reduce learning rate to 0.001, to get acceptable results. 用户必须将学习率降低到 0.001,才能获得可接受的结果。
  3. Use smaller dropout rate.使用较小的辍学率。 .2, .3 for example. .2, .3 例如。
  4. 33% is just random guessing. 33% 只是随机猜测。 Print some images and see that their labels are correct.打印一些图像并查看它们的标签是否正确。
  5. Check the output of your model on some sample image.在一些示例图像上检查 model 的 output。 Is it too close to 1 or 0 in some columns?在某些列中是否太接近 1 或 0? Does it seem just random?它似乎只是随机的吗?

Do these and you might catch the bug.做这些,你可能会发现这个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM