我是 Python CNN 模型的初学者,我已经建立了一个模型,可以根据此链接 https://www.kaggle.com/grassknoted/asl-alphabet 中的数据集对 ASL 字母进行分类,准确率达到 95%。但是,我从这个链接 https://www.kaggle.com/danrasband/asl-alphabet-test 获得了另一个数据集来评估模型,并且准确度大幅下降,我从内置评估函数中获得了 45% 的准确度。那么,这是过拟合问题还是代码有问题。
from keras.models import Sequential, load_model
from keras.layers import Convolution2D, MaxPooling2D, Flatten, Dense, Dropout
from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
#from keras import utils
#from sklearn.model_selection import train_test_split
from math import ceil
import numpy as np
import os
#import cv2
import matplotlib.pyplot as plt
% matplotlib inline
class Model:
classifier = None
def __init__(self, Type):
self.classifier = Type
def build_model(classifier, choose_model):
if choose_model == 1:
classifier.add(Convolution2D(128, (3, 3), input_shape=(64, 64, 1), activation='relu'))
#classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Convolution2D(256, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Dropout(0.25))
classifier.add(Convolution2D(256, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Dropout(0.25))
classifier.add(Convolution2D(512, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Dropout(0.5))
classifier.add(Convolution2D(512, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Dropout(0.5))
elif choose_model == 2:
classifier.add(Convolution2D(64, (3, 3), input_shape=(64, 64, 1), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Convolution2D(128, (3, 3), activation='relu'))
#classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Dropout(0.5))
classifier.add(Convolution2D(128, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Dropout(0.5))
classifier.add(Convolution2D(256, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Dropout(0.5))
classifier.add(Convolution2D(512, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Dropout(0.5))
elif choose_model == 3:
classifier.add(Convolution2D(64, (3, 3), padding='same', input_shape=(64, 64, 1), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Convolution2D(128, (3, 3), padding='same', activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Dropout(0.5))
classifier.add(Convolution2D(256, (3, 3), padding='same', activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Flatten())
#classifier.add(Dense(512, activation='relu'))
classifier.add(Dropout(0.5))
classifier.add(Dense(1024, activation='relu'))
classifier.add(Dense(15, activation='softmax'))
return classifier
def save_classifier(path, classifier):
classifier.save(path)
def load_classifier(path):
classifier = load_model(path)
return classifier
def predict(classess, classifier, img):
pred = classifier.predict(img)
print(classes[np.argmax(pred)])
class DataGatherer:
def __init__(self, dir):
self.dir = dir
#the below 3 variabels are for data(image) augmentation
data_gen = None
train_data_gen = None
test_data_gen = None
#this function does not augment the images
def load_images(self):
images = []
labels = []
index = -1
for folder in os.listdir(self.dir):
index += 1
counter = 0
print("Loading images from folder ", folder ," has started.")
for image in os.listdir(self.dir + '/' + folder):
if (counter % 500) == 0:
print(counter, " images has been loaded from folder ", folder)
#img = cv2.imread(self.dir + '/' + folder + '/' + image, 0)
#img = cv2.resize(img, (64, 64))
img = load_img(self.dir + '/' + folder + '/' + image, target_size=(64, 64), color_mode='grayscale')
img = img_to_array(img)
img = np.expand_dims(img, axis=0)
images.append(img)
labels.append(index)
counter += 1
print("Loading images from folder ", folder ," has finished.")
#cv2.destroyAllWindows()
images = np.array(images)
labels = utils.to_categorical(labels)
print("Converting numerical labels to categorical has finished")
#x_train, x_test, y_train, y_test = train_test_split(images, labels, test_size=0.1)
#print("Spliting data has finished")
return images, labels
training_dir = '/content/drive/MyDrive/Colab Notebooks/asl alphapet/asl_alphapet_train/half_data'
validate_dir = '/content/drive/MyDrive/Colab Notebooks/asl alphapet/asl_alphabet_validate'
batch_size = 64
training_size = 40500
test_size = 4500
compute_steps_per_epoch = lambda x: int(ceil(1. * x/batch_size))
steps_per_epoch = compute_steps_per_epoch(training_size)
val_steps = compute_steps_per_epoch(test_size)
data_gatherer = DataGatherer(training_dir)
data_gatherer.data_gen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
#rotation_range=20,
validation_split=0.1
)
data_gatherer.train_data_gen = train_datagen.flow_from_directory(
training_dir,
target_size=(64, 64),
batch_size=batch_size,
class_mode='categorical',
shuffle=True, seed=42,
subset='training',
color_mode='grayscale')
data_gatherer.test_data_gen = train_datagen.flow_from_directory(
training_dir,
target_size=(64, 64),
batch_size=batch_size,
class_mode='categorical',
shuffle=True, seed=42,
subset='validation',
color_mode='grayscale')
classifier = Model.load_classifier("/content/drive/MyDrive/Colab Notebooks/alphapet_classifiers/15_litters_classifier.h5")
classifier.evaluate(data_gatherer.test_data_gen)
data_gen = ImageDataGenerator(rescale=1./255)
validate_data = data_gen.flow_from_directory(validate_dir, color_mode='grayscale', batch_size=batch_size, target_size=(64, 64))
classifier.evaluate(validate_data)
答案 0 :(得分:0)
看起来原始数据集是一个黑白数据集,这是您的模型的构建目的,但第二个数据集是 RGB 数据集,而且 RGB 图像上有很多噪声,就像第二个数据集,因此这也可能会影响您的最终验证准确性。