I am trying to classify the severity of COVID XRay using 426 256x256 xray images and 4 classes present. However the validation accuracy doesnt improve at all. The validation loss also barely decreases from the start
This is the model I am using
from keras.models import Sequential
from keras.layers import Dense,Conv2D,MaxPooling2D,Dropout,Flatten
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras import regularizers
model=Sequential()
model.add(Conv2D(filters=64,kernel_size=(4,4),input_shape=image_shape,activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))
model.add(Conv2D(filters=128,kernel_size=(6,6),input_shape=image_shape,activation="relu"))
model.add(MaxPooling2D(pool_size=(3,3)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(64,activation="relu"))
model.add(Dense(16,activation="relu"))
model.add(Dense(4,activation="softmax"))
model.compile(loss="categorical_crossentropy",optimizer="adam",metrics=["accuracy"])
These are the outputs I get
epochs = 20
batch_size = 8
model.fit(X_train, y_train, validation_data=(X_test, y_test),
epochs=epochs,
batch_size=batch_size
)
Epoch 1/20
27/27 [==============================] - 4s 143ms/step - loss: 0.1776 - accuracy: 0.9528 - val_loss: 3.7355 - val_accuracy: 0.2717
Epoch 2/20
27/27 [==============================] - 4s 142ms/step - loss: 0.1152 - accuracy: 0.9481 - val_loss: 4.0038 - val_accuracy: 0.2283
Epoch 3/20
27/27 [==============================] - 4s 142ms/step - loss: 0.0875 - accuracy: 0.9858 - val_loss: 4.1756 - val_accuracy: 0.2391
Epoch 4/20
27/27 [==============================] - 4s 142ms/step - loss: 0.0521 - accuracy: 0.9906 - val_loss: 4.1034 - val_accuracy: 0.2717
Epoch 5/20
27/27 [==============================] - 4s 142ms/step - loss: 0.0496 - accuracy: 0.9858 - val_loss: 4.8433 - val_accuracy: 0.3152
Epoch 6/20
27/27 [==============================] - 4s 142ms/step - loss: 0.0170 - accuracy: 0.9953 - val_loss: 5.6027 - val_accuracy: 0.3043
Epoch 7/20
27/27 [==============================] - 4s 142ms/step - loss: 0.2307 - accuracy: 0.9245 - val_loss: 4.2759 - val_accuracy: 0.3152
Epoch 8/20
27/27 [==============================] - 4s 142ms/step - loss: 0.6493 - accuracy: 0.7830 - val_loss: 3.8390 - val_accuracy: 0.3478
Epoch 9/20
27/27 [==============================] - 4s 142ms/step - loss: 0.2563 - accuracy: 0.9009 - val_loss: 5.0250 - val_accuracy: 0.2500
Epoch 10/20
27/27 [==============================] - 4s 142ms/step - loss: 0.0286 - accuracy: 1.0000 - val_loss: 4.6475 - val_accuracy: 0.2391
Epoch 11/20
27/27 [==============================] - 4s 142ms/step - loss: 0.0097 - accuracy: 1.0000 - val_loss: 5.2198 - val_accuracy: 0.2391
Epoch 12/20
27/27 [==============================] - 4s 142ms/step - loss: 0.0037 - accuracy: 1.0000 - val_loss: 5.7914 - val_accuracy: 0.2500
Epoch 13/20
27/27 [==============================] - 4s 142ms/step - loss: 0.0048 - accuracy: 1.0000 - val_loss: 5.4341 - val_accuracy: 0.2391
Epoch 14/20
27/27 [==============================] - 4s 142ms/step - loss: 0.0044 - accuracy: 1.0000 - val_loss: 5.6364 - val_accuracy: 0.2391
Epoch 15/20
27/27 [==============================] - 4s 143ms/step - loss: 0.0019 - accuracy: 1.0000 - val_loss: 5.8504 - val_accuracy: 0.2391
Epoch 16/20
27/27 [==============================] - 4s 143ms/step - loss: 0.0013 - accuracy: 1.0000 - val_loss: 5.9604 - val_accuracy: 0.2500
Epoch 17/20
27/27 [==============================] - 4s 149ms/step - loss: 0.0023 - accuracy: 1.0000 - val_loss: 6.0851 - val_accuracy: 0.2717
Epoch 18/20
27/27 [==============================] - 4s 142ms/step - loss: 0.0134 - accuracy: 0.9953 - val_loss: 4.9783 - val_accuracy: 0.2717
Epoch 19/20
27/27 [==============================] - 4s 141ms/step - loss: 0.0068 - accuracy: 1.0000 - val_loss: 5.7421 - val_accuracy: 0.2500
Epoch 20/20
27/27 [==============================] - 4s 142ms/step - loss: 0.0024 - accuracy: 1.0000 - val_loss: 5.8480 - val_accuracy: 0.2283
Any tips on how i can solve this or If i am doing something wrong?
I am trying to train a convolutional network but the loss does change no matter what i do. I want to know where i am going wrong and also would appreciate any friendly advises as this is my first time i am dealing with such large data.
I have tried many combinations of optimizers(adam,SGD,adamdelta...),loss functions(sqauare mean error,binary cross entropy....) and activation(Relu,elu,selu....) but the problem still persists.
Nature of my project: this is my attempt at training a simple self driving car in simulation.
Training data: the training data is split in around ~4000 .h5 files. Each file has exactly 200 images with respective data for each image like speed,acceleration etc.
Due to the nature of the data I decided to train in mini batches of 200 and cycle through all the files.
# model (I am a beginner so forgive my sloppy code)
rgb_in = Input(batch_shape=(200, 88, 200, 3), name='rgb_in')
conv_1 = Conv2D(filters=10,kernel_size=5,activation="elu",data_format="channels_last",init = "he_normal")(rgb_in)
conv_2 = Conv2D(filters=16,kernel_size=5,activation="elu",data_format="channels_last",init = "he_normal")(conv_1)
conv_3 = Conv2D(filters=24,kernel_size=5,activation="elu",data_format="channels_last",init = "he_normal")(conv_2)
conv_4 = Conv2D(filters=32,kernel_size=3,activation="elu",data_format="channels_last",init = "he_normal")(conv_3)
conv_5 = Conv2D(filters=32,kernel_size=3,activation="elu",data_format="channels_last",init = "he_normal")(conv_4)
flat = Flatten(data_format="channels_last")(conv_5)
t_in = Input(batch_shape=(200,14), name='t_in')
x = concatenate([flat, t_in])
dense_1 = Dense(100,activation="elu",init = "he_normal")(x)
dense_2 = Dense(50,activation="elu",init = "he_normal")(dense_1)
dense_3 = Dense(25,activation="elu",init = "he_normal")(dense_2)
out = Dense(5,activation="elu",init = "he_normal")(dense_3)
model = Model(inputs=[rgb_in, t_in], outputs=[out])
model.compile(optimizer='Adadelta', loss='binary_crossentropy')
for i in range(3663,6951):
filename = 'data_0'+str(i)+'.h5'
f = h5py.File(filename, 'r')
rgb = f["rgb"][:,:,:,:]
targets = f["targets"][:,:]
rgb = (rgb - rgb.mean())/rgb.std()
input_target[:,0] = targets[:,10]
input_target[:,1] = targets[:,11]
input_target[:,2] = targets[:,12]
input_target[:,3] = targets[:,13]
input_target[:,4] = targets[:,16]
input_target[:,5] = targets[:,17]
input_target[:,6] = targets[:,18]
input_target[:,7] = targets[:,21]
input_target[:,8] = targets[:,22]
input_target[:,9] = targets[:,23]
a = one_hot(targets[:,24].astype(int),6)
input_target[:,10] = a[:,2]
input_target[:,11] = a[:,3]
input_target[:,12] = a[:,4]
input_target[:,13] = a[:,5]
output[:,0] = targets[:,0]
output[:,1] = targets[:,1]
output[:,2] = targets[:,2]
output[:,3] = targets[:,4]
output[:,4] = targets[:,5]
model.fit([rgb,input_target], output,epochs=10,batch_size=200)
The result:
Epoch 1/10
200/200 [==============================] - 7s 35ms/step - loss: 6.1657
Epoch 2/10
200/200 [==============================] - 0s 2ms/step - loss: 2.3812
Epoch 3/10
200/200 [==============================] - 0s 2ms/step - loss: 2.2955
Epoch 4/10
200/200 [==============================] - 0s 2ms/step - loss: 2.2778
Epoch 5/10
200/200 [==============================] - 0s 2ms/step - loss: 2.2778
Epoch 6/10
200/200 [==============================] - 0s 2ms/step - loss: 2.2778
Epoch 7/10
200/200 [==============================] - 0s 2ms/step - loss: 2.2778
Epoch 8/10
200/200 [==============================] - 0s 2ms/step - loss: 2.2778
Epoch 9/10
200/200 [==============================] - 0s 2ms/step - loss: 2.2778
Epoch 10/10
200/200 [==============================] - 0s 2ms/step - loss: 2.2778
Epoch 1/10
200/200 [==============================] - 0s 2ms/step - loss: 1.9241
Epoch 2/10
200/200 [==============================] - 0s 2ms/step - loss: 1.9241
Epoch 3/10
200/200 [==============================] - 0s 2ms/step - loss: 1.9241
Epoch 4/10
200/200 [==============================] - 0s 2ms/step - loss: 1.9241
Epoch 5/10
200/200 [==============================] - 0s 2ms/step - loss: 1.9241
Epoch 6/10
200/200 [==============================] - 0s 2ms/step - loss: 1.9241
Epoch 7/10
200/200 [==============================] - 0s 2ms/step - loss: 1.9241
Epoch 8/10
200/200 [==============================] - 0s 2ms/step - loss: 1.9241
Epoch 9/10
200/200 [==============================] - 0s 2ms/step - loss: 1.9241
Epoch 10/10
200/200 [==============================] - 0s 2ms/step - loss: 1.9241
And lastly I will appretiate if you any advice for me regarding the project。
How about using a ReduceLROnPlateau callback?
from keras.callbacks import ReduceLROnPlateau
reduce_lr = ReduceLROnPlateau(monitor='loss', patience=6)
model.fit(X,y,num_epochs=666,callbacks=[reduce_lr])
I have used cyclic learning rate and it has fixed the problem.
for who ever has suffered a similar issue, here is a link to it
https://github.com/bckenstler/CLR
I want to build a model using Keras to predict the price of the natural gas.
The dataset contains the price for the gas daily and monthly since 1997 and it is available Here.
The following graph shows the prices during a sequence of days. X is days and Y is the price.
I have tried LSTM with 4,50,100 cell in hidden layer but the accuracy still not was bad and the model failed to predict future price.
I have added another two hidden layers (full connected) with 100 and 128 cell but it did not work too.
This is the model and the result form training process:
num_units = 100
activation_function = 'sigmoid'
optimizer = 'adam'
loss_function = 'mean_squared_error'
batch_size = 5
num_epochs = 10
log_file_name = f"{SEQ_LEN}-SEQ-{1}-PRED-{int(time.time())}"
# Initialize the model (of a Sequential type)
model = Sequential()
# Adding the input layer and the LSTM layer
model.add(LSTM(units = num_units, activation = activation_function,input_shape=(None, 1)))
# Adding the output layer
model.add(Dense(units = 1))
# Compiling the RNN
model.compile(optimizer = optimizer, loss = loss_function, metrics=['accuracy'])
# Using the training set to train the model
history = model.fit(train_x, train_y, batch_size = batch_size, epochs =num_epochs,validation_data=(test_x, test_y))
and the output is :
Train on 4362 samples, validate on 1082 samples
Epoch 1/10
4362/4362 [==============================] - 11s 3ms/step - loss: 0.0057 - acc: 2.2925e-04 - val_loss: 0.0016 - val_acc: 0.0018
Epoch 2/10
4362/4362 [==============================] - 9s 2ms/step - loss: 6.2463e-04 - acc: 4.5851e-04 - val_loss: 0.0013 - val_acc: 0.0018
Epoch 3/10
4362/4362 [==============================] - 9s 2ms/step - loss: 6.1073e-04 - acc: 2.2925e-04 - val_loss: 0.0014 - val_acc: 0.0018
Epoch 4/10
4362/4362 [==============================] - 8s 2ms/step - loss: 5.4403e-04 - acc: 4.5851e-04 - val_loss: 0.0014 - val_acc: 0.0018
Epoch 5/10
4362/4362 [==============================] - 7s 2ms/step - loss: 5.4765e-04 - acc: 4.5851e-04 - val_loss: 0.0012 - val_acc: 0.0018
Epoch 6/10
4362/4362 [==============================] - 8s 2ms/step - loss: 5.1991e-04 - acc: 4.5851e-04 - val_loss: 0.0013 - val_acc: 0.0018
Epoch 7/10
4362/4362 [==============================] - 7s 2ms/step - loss: 5.7324e-04 - acc: 2.2925e-04 - val_loss: 0.0011 - val_acc: 0.0018
Epoch 8/10
4362/4362 [==============================] - 7s 2ms/step - loss: 4.4248e-04 - acc: 4.5851e-04 - val_loss: 0.0011 - val_acc: 0.0018
Epoch 9/10
4362/4362 [==============================] - 7s 2ms/step - loss: 4.3868e-04 - acc: 4.5851e-04 - val_loss: 0.0011 - val_acc: 0.0018
Epoch 10/10
4362/4362 [==============================] - 7s 2ms/step - loss: 4.6654e-04 - acc: 4.5851e-04 - val_loss: 0.0011 - val_acc: 0.0018
How to know the number of layers and cells for problem like this? Anyone can suggest a netwrok structure that can solve this problem?
hth do I increase the speed of this? I mean the loss is moving down by hairs. HAIRS.
Epoch 1/30
4998/4998 [==============================] - 307s 62ms/step - loss: 0.6861 - acc: 0.6347
Epoch 2/30
4998/4998 [==============================] - 316s 63ms/step - loss: 0.6751 - acc: 0.6387
Epoch 3/30
4998/4998 [==============================] - 357s 71ms/step - loss: 0.6676 - acc: 0.6387
Epoch 4/30
4998/4998 [==============================] - 376s 75ms/step - loss: 0.6625 - acc: 0.6387
Epoch 5/30
4998/4998 [==============================] - 354s 71ms/step - loss: 0.6592 - acc: 0.6387
Epoch 6/30
4998/4998 [==============================] - 345s 69ms/step - loss: 0.6571 - acc: 0.6387
Epoch 7/30
4998/4998 [==============================] - 349s 70ms/step - loss: 0.6559 - acc: 0.6387
Model Architecture:
resnet50 (CNN with skip connections)
Except instead of 1 FC I have two. And I changed the softmax output to sigmoid for binary classification.
num positive training data: 1806
num neg training data: 3192
My output is represented by a 1 or 0 for each example ( [0, 0, 1, 1, ...])
batches = 40, num epochs =30, but that doesn't matter because the loss stopped
I am training a simple model in keras for label classification task with following code.
This dataset has 5 classes so final layer of the network has 5 outputs.
Labels are also one-hot encoded. Here are my results:
32/4000 [..............................] - ETA: 0s - loss: 0.2264 - acc:
0.8750
2176/4000 [===============>..............] - ETA: 0s - loss: 0.3092 - acc:
0.8755
4000/4000 [==============================] - 0s 26us/step - loss: 0.2870 -
acc: 0.8805 - val_loss: 15.9636 - val_acc: 0.0070
Epoch 99/100
32/4000 [..............................] - ETA: 0s - loss: 0.1408 - acc:
0.9688
2176/4000 [===============>..............] - ETA: 0s - loss: 0.2696 - acc:
0.8824
4000/4000 [==============================] - 0s 25us/step - loss: 0.2729 -
acc: 0.8868 - val_loss: 15.9731 - val_acc: 0.0070
Epoch 100/100
32/4000 [..............................] - ETA: 0s - loss: 0.2299 - acc:
0.9375
2176/4000 [===============>..............] - ETA: 0s - loss: 0.2861 - acc:
0.8787
4000/4000 [==============================] - 0s 25us/step - loss: 0.2763 -
acc: 0.8865 - val_loss: 15.9791 - val_acc: 0.0070
10/1000 [..............................] - ETA: 0s
1000/1000 [==============================] - 0s 26us/step
32/5000 [..............................] - ETA: 0s
5000/5000 [==============================] - 0s 9us/step
When do tests at the end of training I get almost 100% error on the test data
I have looked at many related posts, but could not figure out what is wrong, but no luck.
Any advice ?