简体   繁体   中英

CNN - validation loss is very high - solve overfitting model?

I'm creating a CNN to determine age based on a dataset. My training loss is about 10 after 50 epoch, my validation loss is however around 100.

Not sure why is it so high, I tried basically everything. I tried changing dropout rates, changing the number of convolution layers, changing learning rates..

My dataset is alligned, cropped and has about 10 000 images.

What should I do?

path = ...
pixels = [] 
age = []
for img in os.listdir(path):
  ages = int( img.split("_")[0])
  img = cv2.imread(str(path)+"/"+str(img)) 
  img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 
  pixels.append(img) 
  age.append(ages) 
age = np.array(age) 
pixels = np.array(pixels)

x_train,x_test,y_train,y_test = train_test_split(pixels,age,train_size=.7, shuffle=True,random_state=100) 

input = Input(shape=(200,200,3)) 
conv1 = Conv2D(filters=64,kernel_size=(3,3),activation="relu")(input)
pool1 = MaxPool2D((2,2))(conv1)
conv2 = Conv2D(filters=128,kernel_size=(3,3),activation="relu")(pool1)
pool2 = MaxPool2D((2,2))(conv2)
conv3 = Conv2D(filters=256,kernel_size=(3,3),activation="relu")(pool2)
pool3 = MaxPool2D((2,2))(conv3)
conv4 = Conv2D(filters=512,kernel_size=(3,3),activation="relu")(pool3)
pool4 = MaxPool2D((2,2))(conv4)
conv5 = Conv2D(filters=1024,kernel_size=(3,3),activation="relu")(pool4)
flt = tf.keras.layers.GlobalAveragePooling2D()(conv5)

age_l = Dense(128,activation="relu")(flt)
age_l = Dense(64,activation="relu")(age_l)
drop=Dropout(rate=.4, seed=123)(age_l)
age_l = Dense(32,activation="linear")(drop)
age_l = Dense(10,activation="relu")(drop)

model = Model(inputs=input,outputs=age_l)


model.compile(optimizer="adam",loss="mse",metrics=['mae','accuracy'])
rlronp=tf.keras.callbacks.ReduceLROnPlateau(monitor="val_loss",factor=0.5, 
                                            patience=2, verbose=1)
estop=tf.keras.callbacks.EarlyStopping(monitor="val_loss", patience=4,
                                       verbose=1, restore_best_weights=True)
save = model.fit(x_train,y_train,validation_data=(x_test,y_test),epochs=50,
                  callbacks=[rlronp,estop])

change

age_l = Dense(32,activation="linear")(drop)
age_l = Dense(10,activation="relu")(drop)
model.compile(optimizer="adam",loss="mse",metrics=['mae','accuracy'])

to

age_l = Dense(32,activation="relu")(drop)
age_l = Dense(1,activation="linear")(age_1)
model.compile(optimizer="adam",loss="mse",metrics=['mae'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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