<jupyter_start><jupyter_text><jupyter_code>import tensorflow as tf
from tensorflow.keras.layers import Input, Lambda, Dense, Flatten
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 import callbacks,optimizers
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
from tensorflow.keras.models import Sequential
import numpy as np
from glob import glob
IMAGE_SIZE = [224,224]
train_path = '/content/drive/MyDrive/train'
test_path = '/content/drive/MyDrive/test'
inception = InceptionV3(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False )
 for layer in inception.layers:
   layer.trainable = False
folders = glob ('/content/drive/MyDrive/train/*')
folders
x = Flatten()(inception.output)
prediction = Dense(len(folders), activation='softmax')(x)

model = Model(inputs = inception.input, outputs=prediction)
model.summary()
model.compile(
    loss = 'categorical_crossentropy',
    optimizer='adam',
    metrics = ['accuracy']
)
elst = callbacks.EarlyStopping(monitor='val_loss', patience=5, mode='min')
save_ck = callbacks.ModelCheckpoint('/content/drive/MyDrive/apple.h5', save_best_only=True, monitor='val_loss', mode='min')
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_set = train_datagen.flow_from_directory('/content/drive/MyDrive/train',
                                                 target_size = (224,224),
                                                 batch_size = 32,
                                                 class_mode = 'categorical')

test_set = test_datagen.flow_from_directory('/content/drive/MyDrive/test',
                                            target_size = (224,224),
                                            batch_size = 32,
                                            class_mode = 'categorical')
r = model.fit(
    training_set,
    validation_data = test_set,
    #callbacks = [elst, save_ck],
    epochs = 50,
    steps_per_epoch = len(training_set),
    validation_steps=len(test_set) 
)
import matplotlib.pyplot as plt
# plot the loss
plt.plot(r.history['loss'], label='train loss')
plt.plot(r.history['val_loss'], label='val loss')
plt.legend()
plt.title("Epochs vs Loss")
plt.show()
plt.savefig('LossVal_loss')

# plot the accuracy
plt.plot(r.history['accuracy'], label='train acc')
plt.plot(r.history['val_accuracy'], label='val acc')
plt.legend()
plt.title("Epochs vs Accuracy")
plt.show()
plt.savefig('AccVal_acc')
y_pred = model.predict(test_set)
y_pred
import numpy as np
y_pred = np.argmax(y_pred, axis=1)
y_pred
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.inception_v3 import preprocess_input
import numpy as np
model.save('/content/drive/MyDrive/Apple/Apple2.h5')

model = load_model('/content/drive/MyDrive/Apple/Apple2.h5')
img = image.load_img('/content/drive/MyDrive/train/Healthy/Apple_Healthy (1062).JPG', target_size = (224,224))
arr_img = image.img_to_array(img)
arr_img
arr_img = arr_img/255
arr_img = np.expand_dims(arr_img, axis = 0)
img_data = preprocess_input(arr_img)
img_data.shape
prediction = model.predict(img_data)

prediction
class_img = np.argmax(prediction, axis = 1)

class_img<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11004784688995216, 'fraction_numerical': 0.018075491759702286, 'mean_word_length': 3.517406962785114, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '106333', 'license': 'Unlicense', 'n_tokens_mistral': 1237, 'n_tokens_neox': 1166, 'n_words': 230, 'path': 'Apple_disease.ipynb', 'repo_name': 'DrsimpleMugo/Apple-Leaf-Disease-Classification', 'size': '3762'}