Skip to content

Instantly share code, notes, and snippets.

View obeshor's full-sized avatar
😊
enthusiastic

Yannick Serge Obam obeshor

😊
enthusiastic
View GitHub Profile
@obeshor
obeshor / import.ipynb
Last active June 25, 2019 22:18
Importing the Librairies
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@obeshor
obeshor / label_mapping.py
Created June 25, 2019 22:32
Label mapping
!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)
@obeshor
obeshor / transfer_learning.py
Created June 25, 2019 22:34
Tranfer Learning with TFHub
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"}
@obeshor
obeshor / data_processing.py
Created June 25, 2019 22:36
Data processing with image augmentation
# 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)
@obeshor
obeshor / buildilg_model.py
Created June 25, 2019 22:37
Building model
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
@obeshor
obeshor / optimizer.py
Last active June 25, 2019 22:40
Adam Optimizer
|#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'])
@obeshor
obeshor / training_model.py
Created June 25, 2019 22:41
Training Model
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)
@obeshor
obeshor / plot.py
Created June 25, 2019 22:42
Check performance
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')
# 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]) )
@obeshor
obeshor / convert.py
Created July 4, 2019 18:51
Convert the model to TFLite
# 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(