i want to calculate precision and recall for my report
this is my project, how should i procced
here i used cnn for this project
for this projected i imorted tensor flow and sklearn
and i am done training with the model
the model is predicting well
train_datagen = ImageDataGenerator(rescale = 1.0/255, shear_range = 0.2, zoom_range = 0.2,
horizontal_flip = True,vertical_flip = True,
rotation_range=20,width_shift_range=0.2,
height_shift_range=0.2)
train_df = train_datagen.flow_from_directory('Cotton Disease/train',
target_size = (128,128), batch_size = 32, class_mode= 'categorical',
seed=42,shuffle=True)
valid_datagen = ImageDataGenerator(rescale = 1.0/255)
valid_df = valid_datagen.flow_from_directory('Cotton Disease/val',
target_size = (128, 128), batch_size = 32,
class_mode = 'categorical',seed=42,shuffle=True)
test_datagen = ImageDataGenerator(rescale = 1.0/255)
test_df = test_datagen.flow_from_directory('Cotton Disease/test',
target_size = (128,128), batch_size = 32,
class_mode = 'categorical',seed=42,shuffle=False)
cnn = Sequential()
cnn.add(Conv2D(filters = 32, padding = 'same', kernel_size=3, activation='relu',
input_shape=[128, 128, 3]))
cnn.add(MaxPool2D(pool_size=2, strides=2))
cnn.add(Dropout(rate=0.25))
cnn.add(Conv2D(filters = 32, padding='same', kernel_size=3, activation='relu'))
cnn.add(Conv2D(filters = 64, padding='same', kernel_size=3, activation='relu'))
cnn.add(MaxPool2D(pool_size=2, strides=2))
cnn.add(Dropout(rate=0.25))
cnn.add(Flatten())
cnn.add(Dense(units=128, activation='relu'))
cnn.add(Dense(units=128, activation='relu'))
cnn.add(Dropout(rate=0.25))
cnn.add(Dense(units=4, kernel_regularizer=tf.keras.regularizers.l2(0.01), activation='softmax'))
cnn.summary()
# Compiling the CNN
cnn.compile(optimizer = 'adam',loss = 'squared_hinge', metrics = ['accuracy'])
history = cnn.fit(train_df, validation_data = valid_df, epochs = 20)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
Please do some changes in model.compile
to add recall
and precision
metrics to the model and train the model again after changes. You can use below code:
#import the metrics to use recall and precision in your model
from tensorflow.keras import metrics
#at model compile
cnn.compile(optimizer = 'adam',loss = 'squared_hinge', metrics = ['accuracy',tf.keras.metrics.Recall(),tf.keras.metrics.Precision()])
To plot the recall and precison of the model:
# summarize history for recall
plt.plot(history.history['recall'])
plt.plot(history.history['val_recall'])
plt.title('model Recall')
plt.ylabel('Recall')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
# summarize history for precision
plt.plot(history.history['precision'])
plt.plot(history.history['val_precision'])
plt.title('model precision')
plt.ylabel('precison')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
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.