# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import numpy as np
import tensorflow as tf

from matplotlib import pyplot as plt
from tensorflow.keras.models import load_model
from tensorflow.keras.utils import load_img, img_to_array

# + pycharm={"name": "#%%\n"}
model = load_model('../models/best models/plant_or_not_vgg_transfer.hdf5')

# + pycharm={"name": "#%%\n"}
model.layers

# + pycharm={"name": "#%%\n"}
model.summary()

# + pycharm={"name": "#%%\n"}
model.get_weights()

# + pycharm={"name": "#%%\n"}
tf.saved_model.save(model, '../models/best models/Plant_or_not_Trans')

# + pycharm={"name": "#%%\n"}
# convert tensorflow model into tensorflow lite

converter = tf.lite.TFLiteConverter.from_saved_model('../models/best models/VGGNet_BE_WA')
tflite_model = converter.convert()
tflite_model_path = '../models/TFLite model/VGGNet_BE_WA.tflite'

with open(tflite_model_path, 'wb') as file:
    file.write(tflite_model)

# + pycharm={"name": "#%%\n"}
converter = tf.lite.TFLiteConverter.from_saved_model('../models/best models/VGGNetInception_BE_WOAD')
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()
tflite_model_path = '../models/TFLite model/VGGNetInception_BE_WOAD.tflite'

with open(tflite_model_path, 'wb') as file:
    file.write(tflite_model)

# + pycharm={"name": "#%%\n"}
converter = tf.lite.TFLiteConverter.from_saved_model('../models/best models/DeepWideNet_BE_WND')
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()
tflite_model_path = '../models/TFLite model/DeepWideNet_BE_WND.tflite'

with open(tflite_model_path, 'wb') as file:
    file.write(tflite_model)

# + pycharm={"name": "#%%\n"}
converter = tf.lite.TFLiteConverter.from_saved_model('../models/best models/Plant_or_not_Trans')
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()
tflite_model_path = '../models/TFLite model/Plant_or_not.tflite'

with open(tflite_model_path, 'wb') as file:
    file.write(tflite_model)

# + pycharm={"name": "#%%\n"}
interpreter = tf.lite.Interpreter(model_path=tflite_model_path)
interpreter.allocate_tensors()

# + pycharm={"name": "#%%\n"}
interpreter.get_input_details()

# + pycharm={"name": "#%%\n"}
interpreter.get_output_details()

# + pycharm={"name": "#%%\n"}
test_image = load_img(
    path='../data/image data/test/Tomato/Tomato mosaic virus/b8a9c9dd-fbba-41e2-b75d-7fd7f990cda0___PSU_CG 2185.JPG',
    target_size=(72, 72))
plt.imshow(test_image)

# + pycharm={"name": "#%%\n"}
test_image = img_to_array(test_image) / 255.0
test_image = test_image.reshape((1, 72, 72, 3))
test_image

# + pycharm={"name": "#%%\n"}
# make predictions
input_dtl = interpreter.get_input_details()
output_dtl = interpreter.get_output_details()

interpreter.set_tensor(input_dtl[0]['index'], test_image)
interpreter.invoke()
pred = interpreter.get_tensor(output_dtl[0]['index'])

# + pycharm={"name": "#%%\n"}
pred

# + pycharm={"name": "#%%\n"}
classes_ = ['Apple', 'Apple scab', 'Bacterial Blight (CBB)', 'Bacterial spot',
            'Black rot', 'Brown Streak Disease (CBSD)', 'BrownSpot', 'Cassava',
            'Cedar apple rust', 'Cercospora leaf spot Gray leaf spot',
            'Cherry (including sour)', 'Common rust', 'Corn (maize)',
            'Early blight', 'Esca (Black Measles)', 'Grape',
            'Green Mottle (CGM)', 'Haunglongbing (Citrus greening)', 'Healthy',
            'Hispa', 'Late blight', 'Leaf Mold',
            'Leaf blight (Isariopsis Leaf Spot)', 'Leaf scorch', 'LeafBlast',
            'Mosaic Disease (CMD)', 'Northern Leaf Blight', 'Orange', 'Peach',
            'Pepper, bell', 'Potato', 'Powdery mildew', 'Rice',
            'Septoria leaf spot', 'Spider mites Two-spotted spider mite',
            'Squash', 'Strawberry', 'Target Spot', 'Tomato',
            'Tomato Yellow Leaf Curl Virus', 'Tomato mosaic virus', 'healthy']
p = zip(list(classes_), list(pred[0]))
p = sorted(list(p), key=lambda z: z[1], reverse=True)
p

StarcoderdataJupyterScriptsDedupFiltered