Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!wget https://github.com/obeshor/Plant-Diseases-Detector/archive/master.zip | |
!unzip master.zip; | |
import json | |
with open('Plant-Diseases-Detector-master/categories.json', 'r') as f: | |
cat_to_name = json.load(f) | |
classes = list(cat_to_name.values()) | |
print (classes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module_selection = ("inception_v3", 299, 2048) #@param ["(\"mobilenet_v2\", 224, 1280)", "(\"inception_v3\", 299, 2048)"] {type:"raw", allow-input: true} | |
handle_base, pixels, FV_SIZE = module_selection | |
MODULE_HANDLE ="https://tfhub.dev/google/tf2- preview/{}/feature_vector/2".format(handle_base) | |
IMAGE_SIZE = (pixels, pixels) | |
BATCH_SIZE = 64 #@param {type:"integer"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Inputs are suitably resized for the selected module. | |
validation_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255) | |
validation_generator = validation_datagen.flow_from_directory( | |
validation_dir, | |
shuffle=False, | |
seed=42, | |
color_mode="rgb", | |
class_mode="categorical", | |
target_size=IMAGE_SIZE, | |
batch_size=BATCH_SIZE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
feature_extractor = hub.KerasLayer(MODULE_HANDLE, | |
input_shape=IMAGE_SIZE+(3,), | |
output_shape=[FV_SIZE]) | |
do_fine_tuning = False #@param {type:"boolean"} | |
if do_fine_tuning: | |
feature_extractor.trainable = True | |
# unfreeze some layers of base network for fine-tuning | |
for layer in feature_extractor.layers[-30:]: | |
layer.trainable =True | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|#Compile model specifying the optimizer learning rate | |
LEARNING_RATE = 0.001 #@param {type:"number"} | |
model.compile( | |
optimizer=tf.keras.optimizers.Adam(lr=LEARNING_RATE), | |
loss='categorical_crossentropy', | |
metrics=['accuracy']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EPOCHS=10 #@param {type:"integer"} | |
STEPS_EPOCHS = train_generator.samples//train_generator.batch_size | |
VALID_STEPS=validation_generator.samples//validation_generator.batch_size | |
history = model.fit_generator( | |
train_generator, | |
steps_per_epoch=STEPS_EPOCHS, | |
epochs=EPOCHS, | |
validation_data=validation_generator, | |
validation_steps=VALID_STEPS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pylab as plt | |
import numpy as np | |
acc = history.history['accuracy'] | |
val_acc = history.history['val_accuracy'] | |
loss = history.history['loss'] | |
val_loss = history.history['val_loss'] | |
epochs_range = range(EPOCHS) | |
plt.figure(figsize=(8, 8)) | |
plt.subplot(1, 2, 1) | |
plt.plot(epochs_range, acc, label='Training Accuracy') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import OpenCV | |
import cv2 | |
# Utility | |
import itertools | |
import random | |
from collections import Counter | |
from glob import iglob | |
def load_image(filename): | |
img = cv2.imread(os.path.join(data_dir, validation_dir, filename)) | |
img = cv2.resize(img, (IMAGE_SIZE[0], IMAGE_SIZE[1]) ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# convert the model to TFLite | |
!mkdir "tflite_models" | |
TFLITE_MODEL = "tflite_models/plant_disease_model.tflite" | |
# Get the concrete function from the Keras model. | |
run_model = tf.function(lambda x : reloaded(x)) | |
# Save the concrete function. | |
concrete_func = run_model.get_concrete_function( |
OlderNewer