Short Introduction
Here, Python programming language, Keras library is used to manipulate raw input image. To train on CNN architecture and creating a machine learning model that can predict the type of diseases, image data is collected from ourselves and the authenticated online source. As the result, few diseases that usually occurs in tomato plants such as Late blight (training 100, test 21), Gray spot (training 95, test 18) and bacterial canker (training 90, test 21) are detected. So, basically this project is a way to digitize the information in an image for the purpose of convenient retrieval and efficient processing of data. Dataset Preparation, Image processing of a certain level and a Convolutional Neural Network as a classifier are the three main areas, in which this project is completely relying on. The prime focus of this project is firstly to design a convolutional neural network with suitable parameters and train it with a dataset of our own. Finally, predicting the classes in an input image by processing the image into a desired format and then feeding it into the trained neural network.
Dataset For the Tomato Deases Prediction:
https://drive.google.com/open?id=1e43UolvmfnPvbTXKcbexDEgdqQT8y7dQ
Code for this project:
import numpy as np import keras from keras import backend as K from keras.models import Sequential from keras.layers import Activation from keras.layers.core import Dense,Flatten from keras.optimizers import Adam from keras.metrics import categorical_crossentropy from keras.preprocessing.image import ImageDataGenerator from keras.layers.normalization import BatchNormalization from keras.layers.convolutional import * from matplotlib import pyplot as plt from sklearn.metrics import confusion_matrix import itertools import matplotlib.pyplot as plt %matplotlib inline from keras.applications import VGG16#Load the VGG modelvgg_conv = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
# Freeze the layers except the last 6 layers for layer in vgg_conv.layers[:-7]: layer.trainable = False # Check the trainable status of the individual layers for layer in vgg_conv.layers: print(layer, layer.trainable)
vgg_conv.summary() from keras import models from keras import layers from keras import optimizers from keras.regularizers import l2 # Create the model model = models.Sequential() # Add the vgg convolutional base model model.add(vgg_conv) # Add new layers model.add(layers.Dropout(0.25)) model.add(layers.Flatten()) model.add(layers.Dropout(0.4)) model.add(layers.Dense(512,kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01),activation='relu')) model.add(layers.Dense(8,kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01),activation='softmax')) # "Show a summary of the model. Check the number of trainable parameters" model.summary() import tensorflow as tf tf.test.gpu_device_name() # this code to is to connect the google drive with google #colab from google.colab import drive drive.mount('/content/drive') import tarfile #importing tarfile to extract the tarfile where dataset is located train_path='proj/train' test_path='proj/test' valid_path='proj/valid' train_generator=ImageDataGenerator().flow_from_directory(train_path,target_size=(224,224),classes=['anthracnose','calciumdefficiency','healthy','lateblight','bacterialSpot','tomatomosaic','yellowcurved','septorailleafspot'],batch_size=10) validation_generator=ImageDataGenerator().flow_from_directory(valid_path,target_size=(224,224),classes=['anthracnose','calciumdefficiency','healthy','lateblight','bacterialSpot','tomatomosaic','yellowcurved','septorailleafspot'],batch_size=10) test_batches=ImageDataGenerator().flow_from_directory(test_path,target_size=(224,224),classes=['anthracnose','calciumdefficiency','healthy','lateblight','bacterialSpot','tomatomosaic','yellowcurved','septorailleafspot'],batch_size=10) from PIL import ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True train_datagen = ImageDataGenerator( rescale=1./255, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True, fill_mode='nearest') validation_datagen = ImageDataGenerator(rescale=1./255) # Change the batchsize according to your system RAM train_batchsize = 62 val_batchsize = 10 # Compile the model model.compile(loss='categorical_crossentropy', optimizer=optimizers.RMSprop(lr=1e-5), metrics=['acc']) # Train the model history = model.fit_generator( train_generator, steps_per_epoch=train_generator.samples/train_generator.batch_size , epochs=25, validation_data=validation_generator, validation_steps=validation_generator.samples/validation_generator.batch_size, verbose=1) # Save the model model.save('trainedmodel_10.h5') test_eval = model.evaluate(test_batches, verbose=0) print("Test Loss:",test_eval[0]) print("Test Accuracy:",test_eval[1]) print("Test Loss:",test_eval[0]) print("Test Accuracy:",test_eval[1])
Leave a Reply