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
# 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 |
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
#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') |
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') |
OlderNewer