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 / importing_librairies.py
Last active July 4, 2019 18:53
Importing librairies
# Install nightly package for some functionalities that aren't in alpha
!pip install tensorflow-gpu==2.0.0-beta1
# Install TF Hub for TF2
!pip install 'tensorflow-hub == 0.5'
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras.layers import Dense, Flatten, Conv2D
@obeshor
obeshor / validation_training_data.py
Created June 25, 2019 22:31
Load PlantVillage Dataset
#Load data
zip_file=tf.keras.utils.get_file(origin='https://storage.googleapis.com/plantdata/PlantVillage.zip',
fname='PlantVillage.zip', extract=True)
#Create the training and validation directories
data_dir = os.path.join(os.path.dirname(zip_file), 'PlantVillage')
train_dir = os.path.join(data_dir, 'train')
validation_dir = os.path.join(data_dir, 'validation')
@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')