Question: I need improvements on Python code for CNN ( convolutional neural network ) model that can follow the steps: 1 . Recognize the crop; 2
I need improvements on Python code for CNN convolutional neural networkmodel that can follow the steps:
Recognize the crop;
Predict healthyunhealthy crop;
Detect the disease;
If the crop is not recognized, try to predict healthyunhealthy and then detect the disease.
Data from kaggle.com: kinds of crop, images for training and images for testing.
Print an accuracy plot diagram for each model. Also, logic needs to be corrected and final result printed. Please, leave comments for all steps.
import tensorflow as tf from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.models import Sequential from tensorflow.keras.layers import ConvD MaxPoolingD Flatten, Dense, Dropout from tensorflow.keras.callbacks import EarlyStopping from tensorflow.keras.preprocessing import image import matplotlib.pyplot as plt import numpy as np import os traindir train testdir test IMGHEIGHT IMGWIDTH BATCHSIZE EPOCHS traindatagen ImageDataGenerator rescale rotationrange widthshiftrange heightshiftrange shearrange zoomrange horizontalflip True, fillmode 'nearest' testdatagen ImageDataGeneratorrescale traindata traindatagen.flowfromdirectory traindir, targetsize IMGHEIGHT, IMGWIDTH batchsize BATCHSIZE, classmode 'categorical' testdata testdatagen.flowfromdirectory testdir, targetsize IMGHEIGHT, IMGWIDTH batchsize BATCHSIZE, classmode 'categorical' def createmodeloutputclasses: model Sequential ConvD activation 'relu', inputshape IMGHEIGHT, IMGWIDTH, MaxPoolingD ConvD activation 'relu' MaxPoolingD ConvD activation 'relu' MaxPoolingD ConvD activation 'relu' MaxPoolingD Flatten Dense activation 'relu' Dropout Denseoutputclasses, activation 'softmax' return model cropmodel createmodeloutputclasses cropmodel.compileoptimizer 'adam', loss 'categoricalcrossentropy', metrics accuracy crop cropmodel.fittestdata, validationdata traindata, epochs EPOCHS pltplotcrophistoryaccuracy label 'Train Accuracy' pltplotcrophistoryvalaccuracy' label 'Validation Accuracy' pltxlabelEpochs pltylabelAccuracy pltlegend pltshow testloss, testaccuracy cropmodel.evaluatetestdata printfTest Accuracy: testaccuracy :f healthmodel createmodeloutputclasses #HealthyUnhealthy healthmodel.compileoptimizer 'adam', loss 'categoricalcrossentropy', metrics accuracy health healthmodel.fittestdata, validationdata traindata, epochs EPOCHS pltplothealthhistoryaccuracy label 'Train Accuracy' pltplothealthhistoryvalaccuracy' label 'Validation Accuracy' pltxlabelEpochs pltylabelAccuracy pltlegend pltshow diseasemodel createmodeloutputclasses # diseases diseasemodel.compileoptimizer 'adam', loss 'categoricalcrossentropy', metrics accuracy disease diseasemodel.fittestdata, validationdata traindata, epochs EPOCHS pltplotdiseasehistoryaccuracy label 'Train Accuracy' pltplotdiseasehistoryvalaccuracy' label 'Validation Accuracy' pltxlabelEpochs pltylabelAccuracy pltlegend pltshow def classifyimageimagepath: imagepath img image.loadimgimagepath, targetsizeIMGHEIGHT, IMGWIDTH imgarray image.imgtoarrayimg imgarray npexpanddimsimgarray, axis cropprediction cropmodel.predictimgarray croplabel npargmaxcropprediction if croplabel : healthprediction healthmodel.predictimgarray healthlabel npargmaxhealthprediction if healthlabel : diseaseprediction diseasemodel.predictimgarray diseaselabel npargmaxdiseaseprediction return fUnknown crop, Unhealthy, Disease detected: diseaselabel else: return "Unknown crop, Healthy" else: healthprediction healthmodel.predictimgarray healthlabel npargmaxhealthprediction if healthlabel : diseaseprediction diseasemodel.predictimgarray diseaselabel npargmaxdiseaseprediction return fCrop: croplabel Unhealthy, Disease detected: diseaselabel else: return fCrop: croplabel Healthy" result classifyimage printresult cropmodel.savecroprecognitionmodel.h healthmodel.savehealthrecognitionmodel.h diseasemodel.savediseaserecognitionmodel.h
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
