我正在学习神经网络,并试图从头开始构建自己的CNN模型。目前,我正在研究带有20000张图片的斯坦福狗数据集。我已经建立了一个模型,成本已经很低。但是准确性很低,虽然我不明白原因。
我在这里进行数据扩充:
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)
成本在预期的水平上,从1.9开始,并按照我的意愿下降。但是我不确定该怎么做。
编辑:我已经编辑了代码,目前正在Google Colab上运行它。
答案 0 :(得分:0)
您的模型中存在多个不一致之处。
model.add(Dense(120,activation="softmax"))
-这行建议您有120个类别,其中120个类别很多,预期的120个类别的随机准确度= 0.83 %
每个类别需要大量的样本。您很可能需要更好的模型。另外,您需要显示每个时期的损耗和度量标准,以获得更好的主意。
softmax
进行多类别分类,则首选损失函数为categorical_crossentropy
答案 1 :(得分:0)
您的代码未使用正确的损失函数,因为最终的分类是多类的(数据集中的注释中有两种以上的狗)。 keras docs对BinaryCrossentropy
进行以下说明:
只有两个标签类别(假定为0和1)时,请使用此交叉熵损失。对于每个示例,每个预测应该有一个浮点值。
您需要使用CategoricalCrossentropy
。来自the docs:
当存在两个或多个标签类别时,请使用此交叉熵损失函数。我们希望标签以one_hot表示形式提供。如果要以整数形式提供标签,请使用SparseCategoricalCrossentropy损失。每个要素应有#个类浮点值。