Ruchi GajjarNagendra GajarProf. Manish PatelPARIN SHAHGayatri Rathod
Published

Agriculture crop disease detection using ML & Kria KV260

To combat loss due to disease in crops, a machine learning based system with edge AI which detects disease can aid farmers for higher yield.

AdvancedOver 8 days1,234
Agriculture crop disease detection using ML & Kria KV260

Things used in this project

Story

Read more

Schematics

Test Images

Insert them where the test samples go

Code

'model.py' for project Agriculture crop disease detection system using machine learning & Edge AI

Python
Run the code.
The project requires weight files to run. Pl click the link below to access the weight files.

https://drive.google.com/drive/folders/1tdZC5k9NyPRZNGSfX0pYTn1ZnVMNYrct
from google.colab import drive
drive.mount('/content/gdrive')

# -*- coding: utf-8 -*-
"""


# -*- coding: utf-8 -*-
"""

import tensorflow as tf
from tensorflow import keras

def create_model(input_shape,activation_fn,Batch_norm,kernel_init,dropout_rate,target_class):
    input_layer=keras.layers.Input(shape=input_shape)
    layer_1=keras.layers.Conv2D(filters=32,kernel_size=3,strides=1,padding='same',
                                kernel_initializer=kernel_init,activation=activation_fn)(input_layer)
    layer_2=keras.layers.Conv2D(filters=32,kernel_size=3,strides=2,padding='same',
                                kernel_initializer=kernel_init,activation=activation_fn)(layer_1)
    pool_1=keras.layers.MaxPool2D(pool_size=2)(layer_2)
    layer_3=keras.layers.Conv2D(filters=64,kernel_size=5,strides=2,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(pool_1)
    inception_1=keras.layers.Conv2D(filters=32,kernel_size=1,strides=1,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(layer_3)
    inception_1a=keras.layers.Conv2D(filters=32,kernel_size=3,strides=1,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(inception_1)
    inception_1b=keras.layers.Conv2D(filters=32,kernel_size=3,strides=2,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(inception_1a)
    inception_2=keras.layers.Conv2D(filters=32,kernel_size=1,strides=1,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(layer_3)
    inception_2a=keras.layers.Conv2D(filters=32,kernel_size=3,strides=2,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(inception_2)
    inception_3=keras.layers.MaxPool2D(pool_size=2)(layer_3)
    inception_3a=keras.layers.Conv2D(filters=32,kernel_size=1,strides=1,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(inception_3)
    inception_4=keras.layers.Conv2D(filters=32,kernel_size=1,strides=2,padding='same'
                                ,kernel_initializer=kernel_init)(layer_3)
    concat=keras.layers.Concatenate()([inception_1b,inception_2a,inception_3a,inception_4])
    layer_4=keras.layers.Conv2D(filters=128,kernel_size=5,strides=1,padding='same',
                                kernel_initializer=kernel_init,activation=activation_fn)(concat)
    
    _inception_1=keras.layers.Conv2D(filters=32,kernel_size=1,strides=1,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(layer_4)
    _inception_1a=keras.layers.Conv2D(filters=32,kernel_size=3,strides=1,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(_inception_1)
    _inception_1b=keras.layers.Conv2D(filters=32,kernel_size=3,strides=2,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(_inception_1a)
    _inception_2=keras.layers.Conv2D(filters=32,kernel_size=1,strides=1,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(layer_4)
    _inception_2a=keras.layers.Conv2D(filters=32,kernel_size=3,strides=2,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(_inception_2)
    _inception_3=keras.layers.MaxPool2D(pool_size=2)(layer_4)
    _inception_3a=keras.layers.Conv2D(filters=32,kernel_size=1,strides=1,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(_inception_3)
    _inception_4=keras.layers.Conv2D(filters=32,kernel_size=1,strides=2,padding='same'
                                ,kernel_initializer=kernel_init)(layer_4)
    concat=keras.layers.Concatenate()([_inception_1b,_inception_2a,_inception_3a,_inception_4])
    
    
    layer_5=keras.layers.Conv2D(filters=128,kernel_size=5,strides=2,padding='same',
                                kernel_initializer=kernel_init,activation=activation_fn)(concat)
    layer_6=keras.layers.Conv2D(filters=256,kernel_size=7,strides=2,padding='same',
                                kernel_initializer=kernel_init,activation=activation_fn)(layer_5)
    layer_7=keras.layers.Conv2D(filters=256,kernel_size=7,strides=2,padding='same',
                                kernel_initializer=kernel_init,activation=activation_fn)(layer_6)
    flat=keras.layers.Flatten()(layer_7)
    dense1=keras.layers.Dense(512,activation='relu',kernel_initializer=kernel_init)(flat)
    drop_1=keras.layers.Dropout(rate=0.25)(dense1)
    dense2=keras.layers.Dense(256,activation='relu',kernel_initializer=kernel_init)(drop_1)
    drop_2=keras.layers.Dropout(rate=0.3)(dense2)
    dense3=keras.layers.Dense(128,activation='relu',kernel_initializer=kernel_init)(drop_2)
    drop_3=keras.layers.Dropout(rate=0.5)(dense3)
    output=keras.layers.Dense(target_class,activation='softmax')(drop_3)
    
    model=keras.Model(inputs=[input_layer],outputs=[output])
    return model
    
    # -*- coding: utf-8 -*-


import tensorflow as tf
from tensorflow import keras

def create_model(input_shape,activation_fn,Batch_norm,kernel_init,dropout_rate,target_class):
    input_layer=keras.layers.Input(shape=input_shape)
    layer_1=keras.layers.Conv2D(filters=32,kernel_size=3,strides=1,padding='same',
                                kernel_initializer=kernel_init,activation=activation_fn)(input_layer)
    layer_2=keras.layers.Conv2D(filters=32,kernel_size=3,strides=2,padding='same',
                                kernel_initializer=kernel_init,activation=activation_fn)(layer_1)
    pool_1=keras.layers.MaxPool2D(pool_size=2)(layer_2)
    layer_3=keras.layers.Conv2D(filters=64,kernel_size=5,strides=2,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(pool_1)
    inception_1=keras.layers.Conv2D(filters=32,kernel_size=1,strides=1,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(layer_3)
    inception_1a=keras.layers.Conv2D(filters=32,kernel_size=3,strides=1,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(inception_1)
    inception_1b=keras.layers.Conv2D(filters=32,kernel_size=3,strides=2,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(inception_1a)
    inception_2=keras.layers.Conv2D(filters=32,kernel_size=1,strides=1,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(layer_3)
    inception_2a=keras.layers.Conv2D(filters=32,kernel_size=3,strides=2,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(inception_2)
    inception_3=keras.layers.MaxPool2D(pool_size=2)(layer_3)
    inception_3a=keras.layers.Conv2D(filters=32,kernel_size=1,strides=1,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(inception_3)
    inception_4=keras.layers.Conv2D(filters=32,kernel_size=1,strides=2,padding='same'
                                ,kernel_initializer=kernel_init)(layer_3)
    concat=keras.layers.Concatenate()([inception_1b,inception_2a,inception_3a,inception_4])
    layer_4=keras.layers.Conv2D(filters=128,kernel_size=5,strides=1,padding='same',
                                kernel_initializer=kernel_init,activation=activation_fn)(concat)
    
    _inception_1=keras.layers.Conv2D(filters=32,kernel_size=1,strides=1,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(layer_4)
    _inception_1a=keras.layers.Conv2D(filters=32,kernel_size=3,strides=1,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(_inception_1)
    _inception_1b=keras.layers.Conv2D(filters=32,kernel_size=3,strides=2,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(_inception_1a)
    _inception_2=keras.layers.Conv2D(filters=32,kernel_size=1,strides=1,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(layer_4)
    _inception_2a=keras.layers.Conv2D(filters=32,kernel_size=3,strides=2,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(_inception_2)
    _inception_3=keras.layers.MaxPool2D(pool_size=2)(layer_4)
    _inception_3a=keras.layers.Conv2D(filters=32,kernel_size=1,strides=1,padding='same'
                                ,kernel_initializer=kernel_init,activation=activation_fn)(_inception_3)
    _inception_4=keras.layers.Conv2D(filters=32,kernel_size=1,strides=2,padding='same'
                                ,kernel_initializer=kernel_init)(layer_4)
    concat=keras.layers.Concatenate()([_inception_1b,_inception_2a,_inception_3a,_inception_4])
    
    
    layer_5=keras.layers.Conv2D(filters=128,kernel_size=5,strides=2,padding='same',
                                kernel_initializer=kernel_init,activation=activation_fn)(concat)
    layer_6=keras.layers.Conv2D(filters=256,kernel_size=7,strides=2,padding='same',
                                kernel_initializer=kernel_init,activation=activation_fn)(layer_5)
    layer_7=keras.layers.Conv2D(filters=256,kernel_size=7,strides=2,padding='same',
                                kernel_initializer=kernel_init,activation=activation_fn)(layer_6)
    flat=keras.layers.Flatten()(layer_7)
    dense1=keras.layers.Dense(512,activation='relu',kernel_initializer=kernel_init)(flat)
    drop_1=keras.layers.Dropout(rate=0.25)(dense1)
    dense2=keras.layers.Dense(256,activation='relu',kernel_initializer=kernel_init)(drop_1)
    drop_2=keras.layers.Dropout(rate=0.3)(dense2)
    dense3=keras.layers.Dense(128,activation='relu',kernel_initializer=kernel_init)(drop_2)
    drop_3=keras.layers.Dropout(rate=0.5)(dense3)
    output=keras.layers.Dense(target_class,activation='softmax')(drop_3)
    
    model=keras.Model(inputs=[input_layer],outputs=[output])
    return model
    
    #!/usr/bin/env python3
# -*- coding: utf-8 -*-


from tensorflow import keras
import tensorflow as tf
import numpy as np
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg



#from dataset_tf import dataset

#from clr_callback import CyclicLR
classes=np.array(os.listdir("/content/gdrive/MyDrive/KRIA_PROJECT_2022/project_94%/dataset_final/main_dataset_20/Test"))
learning_rate=1e-3
batch_size=1
momentum=0.9
nesterov=True
metrics=[keras.metrics.categorical_accuracy]
epochs=30
valid_step=137
train_step=1200
epoch_step=1    #no of epoch after which lr decays
  #factor by which lr decays
activation_fn="elu"
Batch_norm=False
kernel_init="he_normal"
dropout_rate=0.5
target_class=20
steps=123
image_="/content/gdrive/MyDrive/KRIA_PROJECT_2022/Tomato/A.JPG"
image_1="/content/gdrive/MyDrive/KRIA_PROJECT_2022/Tomato/E.jpg"
image_2="/content/gdrive/MyDrive/KRIA_PROJECT_2022/Tomato/2.jpg"
image_3="/content/gdrive/MyDrive/KRIA_PROJECT_2022/Tomato/5.jpg"
#image_="/media/NIK/project_94%/dataset_final/main_dataset_20/Test/Apple___Black_rot/0bb8fb61-d561-43fd-89a3-d24d253adef9___JR_FrgE.S 2849.JPG"
#image_1="/home/Documents/project-118,68/dataset_final/main_dataset_20/Test/Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot/2aca721c-1600-4f8c-8e79-df33973c671c___RS_GLSp 9348.JPG"
#image_2="/home/Documents/project-118,68/dataset_final/main_dataset_20/Test/Potato___Late_blight/127521ec-3cc5-4f98-ac73-ad561ec29e73___RS_LB 3219.JPG"
#image_3="/home/Documents/project-118,68/dataset_final/main_dataset_20/Test/Tomato___Leaf_Mold/1c03ffcb-9476-453c-96be-5bd1f629b2d1___Crnl_L.Mold 8853.JPG"
img=[image_,image_1,image_2,image_3]
model=create_model(input_shape=[256,256,3],activation_fn='relu',Batch_norm=False,
                   kernel_init='he_normal',dropout_rate=dropout_rate,target_class=target_class)
optimizer=keras.optimizers.Nadam(learning_rate=learning_rate,beta_1=0.9,beta_2=0.999,epsilon=1e-07)
model.compile(loss="categorical_crossentropy",optimizer=optimizer,metrics=metrics)
latest=tf.train.latest_checkpoint("/content/gdrive/MyDrive/KRIA_PROJECT_2022/project_94%/weights")
model.load_weights(latest)

#obj=dataset("test",batch_size=batch_size)
#test_ds=obj.test_dataset_ready()
for i in range(len(img)):
    image=tf.io.read_file(img[i])
    image=tf.image.decode_jpeg(image,channels=3)
    # print(image.shape)
    image/=255
    image=tf.reshape(image,[1,256,256,3])
    conf=model.predict(image,steps=1)
    # print(conf)
    index=np.argmax(conf.reshape(20))
    # print(index)
    print(classes[index])

fig=plt.figure(figsize=(8,8))
col=2
rows=2
for i in range(1,col*rows+1):
    img_=mpimg.imread(img[i-1])
    fig.add_subplot(col,rows,i)
    plt.imshow(img_)
plt.show()

Credits

Ruchi Gajjar

Ruchi Gajjar

1 project • 6 followers
Nagendra Gajar

Nagendra Gajar

1 project • 2 followers
Prof. Manish Patel

Prof. Manish Patel

1 project • 2 followers
PARIN SHAH

PARIN SHAH

1 project • 3 followers
Gayatri Rathod

Gayatri Rathod

1 project • 3 followers

Comments