text_plain_output_1.png
from tensorflow.keras import models,layers

import glob as gb

import matplotlib.pyplot as plt

import os

import os

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import tensorflow as tf

import numpy as np
import pandas as pd
import os

IMAGE_SIZE = (256, 256)
BATCH_SIZE = 32
CHANNELS = 3
EPOCHES = 100

dataset = tf.keras.preprocessing.image_dataset_from_directory('/kaggle/input/corn-or-maize-leaf-disease-dataset/data', shuffle=True, image_size=IMAGE_SIZE, batch_size=BATCH_SIZE)

import os
import glob as gb
path = '//kaggle//input//corn-or-maize-leaf-disease-dataset//data'
size = []
for folder in os.listdir(path):
    files = gb.glob(pathname=str(path + '//' + folder + '/*.jpg'))
    for file in files:
        image = plt.imread(file)
        size.append(image.shape)
pd.Series(size).value_counts()

class_names = dataset.class_names
class_names

plt.figure(figsize=(10,10))
for image_batch , label_batch in dataset.take(1):
    for i in range(12):
        ax = plt.subplot(3,4,i+1)
        plt.imshow(image_batch[i].numpy().astype('uint8'))
        plt.title(class_names[label_batch[i]])
        plt.axis('off')

def get_dataset(ds, train_split=0.8, val_split=0.1, test_split=0.1, shuffle=True, shuffle_size=10000):
    ds_size = len(ds)
    if shuffle:
        ds = ds.shuffle(shuffle_size, seed=8)
    train_size = int(train_split * ds_size)
    val_size = int(val_split * ds_size)
    train_ds = ds.take(train_size)
    val_ds = ds.skip(train_size).take(val_size)
    test_ds = ds.skip(train_size).skip(val_size)
    return (train_ds, val_ds, test_ds)

train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=tf.data.AUTOTUNE)
val_ds = val_ds.cache().shuffle(1000).prefetch(buffer_size=tf.data.AUTOTUNE)
test_ds = test_ds.cache().shuffle(1000).prefetch(buffer_size=tf.data.AUTOTUNE)

resize_and_rescale = tf.keras.Sequential([layers.experimental.preprocessing.Resizing(256, 256), layers.experimental.preprocessing.Rescaling(1.0 / 255)])

data_augmentation = tf.keras.Sequential([layers.experimental.preprocessing.RandomFlip('horizontal_and_vertical'), layers.experimental.preprocessing.RandomRotation(0.3)])

n_classes = 4
input_shape = (BATCH_SIZE, 256, 256, 3)
model = models.Sequential([resize_and_rescale, data_augmentation, layers.Conv2D(32, (3, 3), activation='relu', input_shape=input_shape), layers.MaxPooling2D((2, 2)), layers.Conv2D(64, kernel_size=(3, 3), activation='relu'), layers.MaxPooling2D((2, 2)), layers.Conv2D(128, kernel_size=(3, 3), activation='relu'), layers.MaxPooling2D((2, 2)), layers.Flatten(), layers.Dense(256, activation='relu'), layers.Dense(128, activation='relu'), layers.Dense(64, activation='relu'), layers.Dense(n_classes, activation='softmax')])
model.build(input_shape=input_shape)

model.summary()

model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), metrics=['accuracy'])

history = model.fit(train_ds, epochs=60, batch_size=BATCH_SIZE, verbose=1, validation_data=val_ds)

history.params

history.history.keys()

his_data = pd.DataFrame(history.history)
plt.figure(figsize=(20, 5))
plt.subplot(1, 2, 1)
plt.plot(his_data.loss, label='Training loss')
plt.plot(his_data.val_loss, label='Validation loss')
plt.xlabel('Epochs')
plt.ylabel('loss')
plt.title('Losses')
plt.grid()
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(his_data.accuracy, label='Training accuracy')
plt.plot(his_data.val_accuracy, label='Validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.title('Accuracy')
plt.grid()
plt.legend()