The code below is for my CNN model and I want to plot the accuracy and loss for it, any help would be much appreciated. I want the output to be plotted using matplotlib so need any advice as Im not sure how to approach this. Two plots with training and validation accuracy and another plot with training and validation loss.
JavaScript
x
86
86
1
bin_labels = {1:'EOSINOPHIL',2:'LYMPHOCYTE',3:'MONOCYTE',4:'NEUTROPHIL'}
2
3
def CNN(imgs,img_labels,test_imgs,test_labels,stride):
4
5
#Number of classes (2)
6
num_classes = len(img_labels[0])
7
8
#Size of image
9
img_rows,img_cols=imgs.shape[1],imgs.shape[2]
10
input_shape = (img_rows, img_cols, 3)
11
12
#Creating the model
13
model = Sequential()
14
15
#First convolution layer
16
model.add(Conv2D(32, kernel_size=(3, 3),
17
activation='relu',
18
input_shape=input_shape,
19
strides=stride))
20
21
#First maxpooling layer
22
model.add(MaxPooling2D(pool_size=(2, 2)))
23
24
#Second convolution layer
25
model.add(Conv2D(64, (3, 3), activation='relu'))
26
27
#Second maxpooling layer
28
model.add(MaxPooling2D(pool_size=(2, 2)))
29
30
#Third convolution layer
31
model.add(Conv2D(128, (3, 3), activation='relu'))
32
33
#Third maxpooling layer
34
model.add(MaxPooling2D(pool_size=(2, 2)))
35
36
#Convert the matrix to a fully connected layer
37
model.add(Flatten())
38
39
#Dense function to convert FCL to 128 values
40
model.add(Dense(128, activation='relu'))
41
42
#Final dense layer on which softmax function is performed
43
model.add(Dense(num_classes, activation='softmax'))
44
45
#Model parameters
46
model.compile(loss='categorical_crossentropy',
47
optimizer='adam',
48
metrics=['accuracy'])
49
50
#Evaluate the model on the test data before training your model
51
score = model.evaluate(test_imgs,test_labels, verbose=1)
52
53
print('nKeras CNN binary accuracy:', score[1],'n')
54
55
#The model details
56
history = model.fit(imgs,img_labels,
57
shuffle = True,
58
epochs=3,
59
validation_data = (test_imgs, test_labels))
60
61
#Evaluate the model on the test data after training your model
62
score = model.evaluate(test_imgs,test_labels, verbose=1)
63
print('nKeras CNN binary accuracy:', score[1],'n')
64
65
#Predict the labels from test data
66
y_pred = model.predict(test_imgs)
67
Y_pred_classes = np.argmax(y_pred,axis=1)
68
Y_true = np.argmax(test_labels,axis=1)
69
70
#Correct labels
71
for i in range(len(Y_true)):
72
if(Y_pred_classes[i] == Y_true[i]):
73
print("The predicted class is : " , Y_pred_classes[i])
74
print("The real class is : " , Y_true[i])
75
break
76
77
#The confusion matrix made from the real Y values and the predicted Y values
78
confusion_mtx = [Y_true, Y_pred_classes]
79
80
#Summary of the model
81
model.summary()
82
83
return model,confusion_mtx
84
85
model,conf_mat = CNN(X_train,y_trainHot,X_test,y_testHot,1);
86
Advertisement
Answer
this worked for me when worked on CNN model:
JavaScript
1
19
19
1
import matplotlib.pyplot as plt
2
3
# summarize history for accuracy
4
plt.plot(history.history['accuracy'])
5
plt.plot(history.history['val_accuracy'])
6
plt.title('model accuracy')
7
plt.ylabel('accuracy')
8
plt.xlabel('epoch')
9
plt.legend(['Train', 'Validation'], loc='upper left')
10
plt.show()
11
# summarize history for loss
12
plt.plot(history.history['loss'])
13
plt.plot(history.history['val_loss'])
14
plt.title('model loss')
15
plt.ylabel('loss')
16
plt.xlabel('epoch')
17
plt.legend(['Train', 'Validation'], loc='upper left')
18
plt.show()
19
you can see the image of plot