我是深度学习的新手,我有3个班级来分类,当我训练模型时,我观察到我的“ val_loss> val_accuracy” 表示我的模型过拟合该如何解决?这样我也会得到“ val_accuracy:0.0000e + 00” 。最初,我一直保持低调。我缺少训练模型的数据。
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dropout
from keras.layers import Dense
from keras.preprocessing.image import ImageDataGenerator, image
import numpy as np
import os
from pathlib import Path
country = "armenia"
cwd = os.getcwd()
print("cwd",cwd)
save_path = r'E://paymentz//'+country+'/'
abc = os.listdir(r'E:/paymentz/'+country+'/training')
print("list of subfolders in directory:",abc)
model = Sequential()
model.add(Convolution2D(16, 2, 2, input_shape = ( 64, 64, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Dropout(0.5))
model.add(Convolution2D(32, 3, 3, activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(output_dim= 64, activation='relu' ))
output_dim = os.listdir(r'E:/paymentz/'+country+'/training')
print(len(output_dim))
output_dim = len(output_dim)
model.add(Dense(output_dim , activation = 'softmax'))
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics =['accuracy'])
batch_size = 16
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
rotation_range = 360)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = test_datagen.flow_from_directory(r'E:/paymentz/'+country+'/training',
target_size = (64, 64),
batch_size= batch_size,
class_mode='categorical')
test_set = test_datagen.flow_from_directory(r'E:/paymentz/'+country+'/testing',
target_size= (64, 64),
batch_size= batch_size,
class_mode='categorical')
training_path = Path(fr'E://paymentz//{country}//training')
training_png_count = len(list(training_path.rglob('*.png')))
training_jpg_count = len(list(training_path.rglob('*.jpg')))
training_jpeg_count = len(list(training_path.rglob('*.jpeg')))
training_count = training_png_count + training_jpg_count + training_jpeg_count
print("training_count ", training_count)
testing_path = Path(fr'E://paymentz//{country}//testing')
testing_png_count = len(list(testing_path.rglob('*.png')))
testing_jpg_count = len(list(testing_path.rglob('*.jpg')))
testing_jpeg_count = len(list(testing_path.rglob('*.jpeg')))
testing_count = testing_png_count + testing_jpg_count + testing_jpeg_count
print("testing_count ", testing_count)
steps_per_epoch = (training_count// batch_size )
print("steps_per_epoch", steps_per_epoch)
validation_steps = ( testing_count // batch_size )
print("validation_steps", validation_steps)
model.fit_generator(
training_set,
validation_data = test_set,
samples_per_epoch = training_count,
epochs = 15,
validation_steps = validation_steps,
steps_per_epoch = steps_per_epoch)
print("training done.")
score = model.fit(test_set)
score= model.evaluate_generator(test_set)
print("test_set ",score)
#score= model.evaluate_generator(training_set)
#print("training_set ", score)
#if score[0] < 0.05 and score [1] < .85:
save_path = r'E:/paymentz/'+country+'/'
model.save(save_path+country+'.model')
model.save(save_path+country+'.model.h5')
#model.save("StatewiseDLmodel.model.h5")
print("model saved")
abc = os.listdir(r'E:/paymentz/'+country+'/training')
model_path = r''+country+'model.h5'
#model = tf.keras.models.load_model(country+'.model.h5')
print("model trained to:",score)```
答案 0 :(得分:0)
为什么要在测试集中再次拟合模型?
print("training done.")
score = model.fit(test_set) # this should be predict.
score= model.evaluate_generator(test_set)
print("test_set ",score)
删除以下行,然后重试。
score = model.fit(test_set)