Closed talentlei closed 7 years ago
Hi, I recently had the same experience of training a CNN while my validation accuracy doesn't change. I tried different setups from LR, optimizer, number of filters and even playing with the model size. But later I discovered it was an issue with my preprocessing of data. Basically, I was doing some preprocessing to my data before training which ends up squeezing the pixel intensity to near zero (in short all images were just black images). I discovered it after debugging my preprocessing step in which I tried to write some of the images in a disk.
To be honest, I was suspecting it was a bug from Keras but boom! it was not. I tried to share my experience in case anyone else is facing the same issue.
Were you dividing your images by 255? I am facing the same issue and am starting to suspect this is the problem. I divide my pixels by 255 (as is customary) but can still see what the image looks like when plotting it.
I faced the same issue. It got resolved by changing the optimizer from 'rmsprop' to 'adam'.
I tried changing optimizers, learning rates, momentum, network depth, and all other parameters. Turns out, I just needed to let it train for a long time before it started to find where the loss was decreasing. The AUC was stagnant for 35 epochs then it started increasing. Can't think of why, but it eventually started to learn.
@sayedathar11 When I use model.add(Dense(1,activation='sigmoid')), am getting the following error. ValueError: Error when checking target: expected dense_4 to have shape (1,) but got array with shape (2,)
Here is my code:
batch_size = 32 nb_classes = 2 data_augmentation = True
img_rows, img_cols = 224,224 img_channels = 3
Creating array of training samples
train_path = "D:/data/train." training_data=[] for file in glob.glob(train_path): print(file) train_array= cv2.imread(file) train_array=cv2.resize(train_array,(img_rows,img_cols),3) training_data.append(train_array)
x_train=np.array(training_data)
Creating array of validation samples
valid_path = "D:/data/valid." valid_data=[] for file in glob.glob(valid_path): print(file) valid_array= cv2.imread(file) valid_array=cv2.resize(valid_array,(img_rows,img_cols),3) valid_data.append(train_array)
x_valid=np.array(valid_data)
x_train = np.array(x_train, dtype="float")/255.0 x_valid = np.array(x_valid, dtype="float")/255.0
Creating array for Labels
y_train=np.ones((num_trainsamples,),dtype = int) y_train[0:224]=0 #Class1=0 y_train[225:363]=1 #Class2=1 print(y_train)
y_valid=np.ones((num_validsamples,),dtype = int) y_valid[0:101]=0 y_valid[102:155]=1 print(y_valid)
y_train = np_utils.to_categorical(y_train,nb_classes,dtype='int32') y_valid = np_utils.to_categorical(y_valid,nb_classes,dtype='int32')
base_model=ResNet50(weights='imagenet',include_top=False)
x = base_model.output x = GlobalMaxPooling2D()(x) x=Dense(1024,activation='relu')(x) x=Dense(1024,activation='relu')(x) x=Dense(512,activation='relu')(x) x=Dense(1, activation= 'sigmoid')(x) model = Model(inputs = base_model.input, outputs = x)
for i,layer in enumerate(model.layers): print(i,layer.name)
for layer in model.layers[:75]: layer.trainable=False for layer in model.layers[75:]: layer.trainable=True
adam = Adam(lr=0.0001) model.compile(optimizer= adam, loss='binary_crossentropy', metrics=['accuracy'])
train_datagen = ImageDataGenerator( brightness_range=(0.2,2.5), rotation_range=180, zoom_range=0.5, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True, vertical_flip=True)
train_datagen.fit(x_train)
history= model.fit_generator(train_datagen.flow(x_train, y_train, batch_size = 10,shuffle=True),steps_per_epoch=len(x_train),epochs = 500,shuffle=True, validation_data=(x_valid,y_valid),validation_steps=num_validsamples // batch_size,callbacks=[tensorboard])
eval = model.evaluate(x_valid, y_valid) print ("Loss = " + str(eval[0])) print ("Test Accuracy = " + str(eval[1]))
predictions= model.predict(x_valid) print(predictions)
What is the variable num_trainsamples
?
Go with the suggestion given by @kodon0 . It works !
For Those who still have this problem and wondering why this occurs. The reason is pretty straightforward in your final Dense layers where you are specifying the output basically the softmax layer , here number of cells should be equal to number of classes. If you are solving Binary Classification all you need to do this use 1 cell with sigmoid activation.
for Binary
model.add(Dense(1,activation='sigmoid'))
for n_class
model.add(Dense(n_class,activation='softmax')) #where n_class is number of classes Thanks to :https://stackoverflow.com/questions/51581521/accuracy-stuck-at-50-keras
This worked for me!
For Those who still have this problem and wondering why this occurs. The reason is pretty straightforward in your final Dense layers where you are specifying the output basically the softmax layer , here number of cells should be equal to number of classes. If you are solving Binary Classification all you need to do this use 1 cell with sigmoid activation.
for Binary
model.add(Dense(1,activation='sigmoid'))
for n_class
model.add(Dense(n_class,activation='softmax')) #where n_class is number of classes Thanks to :https://stackoverflow.com/questions/51581521/accuracy-stuck-at-50-keras
This solution work like a charm! thx
I use LSTM to do a sequence labeling task, but I got the same acc and cal_acc for each epoch. here is my code:
def moduleRnn(self): model = Sequential() model.add(LSTM(output_dim=64,input_length=self.seq_len,batch_input_shape=(16,1,200),input_dim=self.embed_length,return_sequences=True,stateful=False ))
model.add(LSTM(output_dim=16,return_sequences=True,stateful=False ))
Anyone meets the same problem? please help me