简体   繁体   English

CNN 的准确率非常低

[英]Very low accuracy on CNN

I am learning about neural networks and trying to build my own CNN model from scratch.我正在学习神经网络并尝试从头开始构建我自己的 CNN model。 Currently I'm working on the Stanford Dog Dataset with 20000 pictures.目前我正在研究包含 20000 张图片的斯坦福狗数据集。 I already built a model and getting already low cost.我已经构建了 model 并且成本已经很低。 But the accuracy is very low, I can't understand the reason though.但是准确率很低,虽然我不明白原因。

Here I do data augmentation:在这里我做数据增强:

import os
import time
import zipfile
import pandas as pd
import tensorflow as tf
from tensorflow import keras # Tensorflow high-level api
from tensorflow.keras import layers

from keras import optimizers
from keras.models import Model, Sequential
from keras.layers import Dense,Flatten, GlobalAveragePooling2D, BatchNormalization, Activation, Dropout, Conv2D,MaxPooling2D
from keras.callbacks import ModelCheckpoint, EarlyStopping, TensorBoard, CSVLogger, ReduceLROnPlateau
#from keras.layers import Activation, Dropout, Flatten, Dense
from keras.applications.xception import Xception
from keras.preprocessing.image import ImageDataGenerator

!pip install kaggle
from google.colab import files
files.upload()

#before importing the dataset we want to use this code
# The Kaggle API client expects this file to be in ~/.kaggle,
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/

# This permissions change avoids a warning on Kaggle tool startup.
!chmod 600 ~/.kaggle/kaggle.json

!kaggle datasets download -d jessicali9530/stanford-dogs-dataset

local_zip = '/content/stanford-dogs-dataset.zip'
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall('/content/stanford-dogs')
zip_ref.close()


train_data_dir = os.path.join("/content", "stanford-dogs", "images", "Images")
img_width, img_height = 128, 128
batch_size = 32

train_datagen = ImageDataGenerator(
    rescale=1./255,             
    vertical_flip = True,
    horizontal_flip = True,
    rotation_range=20,
    shear_range=0.05,           
    zoom_range=0.2,   
    width_shift_range=0.1,
    height_shift_range=0.1,
    validation_split=0.15
    channel_shift_range=0.1
 )

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_height, img_width),
    batch_size=batch_size,
    class_mode='categorical',    # 2D one-hot encoded labels (batch_size x 101)
    subset='training')
    
validation_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_height, img_width),
    batch_size=batch_size,
    class_mode='categorical',    # 2D one-hot encoded labels (batch_size x 101)
    subset='validation')
 model.add(Conv2D(kernel_size=(3,3),filters=32,input_shape = (img_width, img_height, 3),activation="relu",padding="valid"))

model.add(Conv2D(kernel_size=(3,3),filters=32,activation="relu",padding="same"))
model.add(Dropout(0.15))

model.add(Conv2D(kernel_size=(3,3),filters=24))
model.add(Conv2D(kernel_size=(3,3),filters=64,activation="relu",padding="same"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(kernel_size=(3,3),filters=24))
model.add(Dropout(0.25))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(kernel_size=(5,5),filters=32,activation="relu",padding="same"))

model.add(MaxPooling2D(pool_size=(3,3)))


model.add(Flatten())
model.add(Dense(100,activation="relu",kernel_regularizer=keras.regularizers.l2(0.01)))
model.add(Dropout(0.4))
model.add(Dense(120,activation="softmax"))


model.summary()

model.compile(loss=keras.losses.binary_crossentropy,
           optimizer=keras.optimizers.Adadelta(lr=0.01),
           metrics=['accuracy'])

history = model.fit_generator(train_generator,
       steps_per_epoch = train_generator.n // train_generator.batch_size,
       validation_data = validation_generator,
       validation_steps = validation_generator.n // validation_generator.batch_size,
       epochs = 10,
       shuffle= True,         
       verbose = 1)

The cost is on an expected level, it starts with 1.9 and goes down like I want it to.成本处于预期水平,从 1.9 开始,然后按我的意愿下降。 But I am not sure what to do about the accuracy.但我不确定如何处理准确性。

Edit: I edited the code, I'm running it currently on Google Colab.编辑:我编辑了代码,我目前在 Google Colab 上运行它。

There are multiple inconsistencies in your model.您的 model 中有多个不一致之处。

  1. model.add(Dense(120,activation="softmax")) - this line sugggests you've 120 classes, 120 classes is a lot, the expected randomized accuracy for 120 classes = 0.83 % You need good amount of samples per class. model.add(Dense(120,activation="softmax")) - 这行表明你有 120 个类,120 个类很多,120 个类的预期随机准确度 = 0.83 %你需要每个 ZA2F2ED4F8EBC2CBB4C21A29DC40AB16 的大量样本

You most probably need a better model.您很可能需要更好的 model。 Also, you need to show the loss and metrics per epoch to get a better idea.此外,您需要显示每个时期的损失和指标以获得更好的想法。

  1. If we use softmax for multi-class classification, the preferred loss function is categorical_crossentropy如果我们使用softmax进行多类分类,首选损失function是categorical_crossentropy

Your code is not using the correct loss function because the final classification is multiclass (there's more than two types of dog in the dataset's annotations).您的代码没有使用正确的损失 function,因为最终分类是多类的(数据集的注释中有两种以上的狗)。 The keras docs state the following about BinaryCrossentropy : keras 文档state 以下关于BinaryCrossentropy

Use this cross-entropy loss when there are only two label classes (assumed to be 0 and 1).当只有两个 label 类(假设为 0 和 1)时使用此交叉熵损失。 For each example, there should be a single floating-point value per prediction.对于每个示例,每个预测都应该有一个浮点值。

You need to use CategoricalCrossentropy .您需要使用CategoricalCrossentropy From the docs :文档

Use this crossentropy loss function when there are two or more label classes.当有两个或更多 label 类时,使用此交叉熵损失 function。 We expect labels to be provided in a one_hot representation.我们希望以 one_hot 表示形式提供标签。 If you want to provide labels as integers, please use SparseCategoricalCrossentropy loss.如果您想以整数形式提供标签,请使用 SparseCategoricalCrossentropy 损失。 There should be # classes floating point values per feature.每个特征应该有 # 个类浮点值。

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

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