text_plain_output_1.png
from tensorflow.keras import models,layers

import tensorflow as tf

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)

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)

scores = model.evaluate(test_ds)