0% found this document useful (0 votes)
9 views

Train

This document defines a convolutional neural network model using transfer learning with InceptionV3 to classify images of plant diseases. It loads image data, trains the model for 10 epochs, plots the loss and accuracy curves, and saves the trained model. The model takes preprocessed images as input and outputs a prediction with softmax activations across the number of disease classes.

Uploaded by

Phenias Manyasha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Train

This document defines a convolutional neural network model using transfer learning with InceptionV3 to classify images of plant diseases. It loads image data, trains the model for 10 epochs, plots the loss and accuracy curves, and saves the trained model. The model takes preprocessed images as input and outputs a prediction with softmax activations across the number of disease classes.

Uploaded by

Phenias Manyasha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import tensorflow as tf

import pandas as pd
from keras.utils import to_categorical
import random
import numpy as np
import os
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split


from tensorflow.keras.layers import Input, Lambda, Dense, Flatten, Conv2D,
MaxPooling2D, Dropout, Activation, BatchNormalization
from tensorflow.keras.models import Model
from tensorflow.keras.applications.inception_v3 import InceptionV3
from keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.inception_v3 import preprocess_input
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img,
array_to_img, img_to_array
from tensorflow.keras.models import Sequential
from glob import glob

# Define Constants by re-sizing all the images


IMAGE_SIZE = [224, 224]

train_path = '../input/tomato/New Plant Diseases Dataset(Augmented)/train'


valid_path = '../input/tomato/New Plant Diseases Dataset(Augmented)/valid'

# Import the InceptionV3 model and here we will be using imagenet weights

inception = InceptionV3(input_shape=IMAGE_SIZE + [3], weights='imagenet',


include_top=False)

# We don't need to train existing weights


for layer in inception.layers:
layer.trainable = False

# Folders in the Training Set


folders = glob('../input/tomato/New Plant Diseases Dataset(Augmented)/train/*')
folders

# Model layers -> can add more if required


x = Flatten()(inception.output)
prediction = Dense(len(folders), activation='softmax')(x)

# Create a model object


model = Model(inputs=inception.input, outputs=prediction)

# View the structure of the model


model.summary()

# Defining the cost and model optimization method to use


model.compile(
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)

# Using the Image Data Generator to import the images from the dataset
from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,


shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

# Training Generator
training_set = train_datagen.flow_from_directory('../input/tomato/New Plant
Diseases Dataset(Augmented)/train',
target_size = (224, 224),
batch_size = 32,
class_mode = 'categorical')

# Testing Generator
test_set = test_datagen.flow_from_directory('../input/tomato/New Plant Diseases
Dataset(Augmented)/train',
target_size = (224, 224),
batch_size = 32,
class_mode = 'categorical')

# fit the model, it will take some time to execute


r = model.fit_generator(
training_set,
validation_data=test_set,
epochs=10,
steps_per_epoch=len(training_set),
validation_steps=len(test_set)
)

# Plot the Loss and Accuracy


# Loss
plt.plot(r.history['loss'], label='train loss')
plt.plot(r.history['val_loss'], label='val loss')
plt.legend()
plt.show()
plt.savefig('LossVal_loss')

# Accuracy
plt.plot(r.history['accuracy'], label='train acc')
plt.plot(r.history['val_accuracy'], label='val acc')
plt.legend()
plt.show()
plt.savefig('AccVal_acc')

# Saving the model as a h5 file

from tensorflow.keras.models import load_model


model.save('model_inception.h5')

y_pred = model.predict(test_set)
y_pred
import numpy as np
y_pred = np.argmax(y_pred, axis=1)
y_pred

You might also like