Algorithm
Algorithm
BACHELOR OF
SUBMITTED BY:
AJITH S
DARSHAN L
SAKSHI R
SATHWIK C S
September 2023
IMPORTANT DIAGRAMS
def convert_image_to_array(image_dir):
try:
image = cv2.imread(image_dir)
if image is not None :
image = cv2.resize(image, (256, 256))
return img_to_array(image)
else :
return np.array([])
except Exception as e:
print (f"Error: {e}")
return None
dataset_path = "C:\Plant-Disease-Detection\Dataset"
labels = os.listdir(dataset_path)
print(labels)
dataset_path = "C:\Plant-Disease-Detection\Dataset"
root_dir = listdir(dataset_path)
image_list, label_list = [], []
all_labels = ['Corn-Common_rust', 'Potato-Early_blight', 'Tomato-Bacterial_spot']
binary_labels = [0, 1, 2]
temp = -1
### Visualize the class count and Check for class imbalance
label_counts = pd. DataFrame(label_list).value_counts ()
label_counts.head()
image_list [0]. Shape
label_list = np.array(label_list)
label_list. Shape
### Splitting the dataset into train, validate and test sets
x_train, x_test, y_train, y_test = train_test_split (image_list, label_list, test_size=0.2, random_state =
10)
# Now we will normalize the dataset of our images. As pixel values ranges from 0 to 255 so we will
divide each image pixel with 255 to normalize the dataset.
### Creating the model architecture, compile the model and then fit it using the
training data
model = Sequential()
model.add (Conv2D (32, (3, 3), padding = "same”, input_shape = (256, 256, 3), activation = "relu"))
model.add(MaxPooling2D (pool_size = (3, 3)))
model.add(Conv2D(16, (3, 3), padding = "same", activation = "relu"))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Flatten())
model.add(Dense(8, activation = "relu"))
model.add(Dense(3, activation = "softmax"))
model. summary ()
model. compile (loss = 'categorical_crossentropy', optimizer = Adam (0.0001), metrics = ['accuracy'])
# Splitting the training data set into training and validation data sets
x_train, x_val, y_train, y_val = train_test_split (x_train, y_train, test_size = 0.2, random_state = 10)
### Visualizing the original and predicted labels for the test images
# Plotting image to compare
img = array_to_img (x_test [11])
img
# Finding max value from perdition list and comparing original value vs predicted
print ("Originally: ", all_labels [np. argmax (y_test [11])])
print ("Predicted : ", all_labels[np.argmax(y_pred[4])])
print (y_pred [2])