This file contains hidden or 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
def train_model(model): | |
model.compile(optimizer='adam', | |
loss='sparse_categorical_crossentropy', | |
metrics=['accuracy']) | |
# Creating Keras callbacks | |
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1) | |
model_checkpoint_callback = keras.callbacks.ModelCheckpoint( | |
'training_checkpoints/weights.{epoch:02d}-{val_loss:.2f}.hdf5', period=5) | |
os.makedirs('training_checkpoints/', exist_ok=True) |
This file contains hidden or 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
from tensorflow.keras.preprocessing.image import ImageDataGenerator | |
datagen = ImageDataGenerator(rotation_range=20, | |
width_shift_range=0.2, | |
height_shift_range=0.2, | |
horizontal_flip=True) | |
# Performing model training for the given number of epochs | |
for e in range(epochs): | |
print('Epoch', e) |
This file contains hidden or 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
from tensorflow import keras | |
# Create the base model from the pre-trained model MobileNet V2 | |
base_model = keras.applications.InceptionV3(input_shape=IMG_SHAPE, | |
# We cannot use the top classification layer of the pre-trained model as it contains 1000 classes. | |
# It also restricts our input dimensions to that which this model is trained on (default: 299x299) | |
include_top=False, | |
weights='imagenet') |
This file contains hidden or 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
def build_model(): | |
# Using Sequential API to stack up the layers | |
model = keras.Sequential([ | |
base_model, | |
keras.layers.GlobalAveragePooling2D(), | |
keras.layers.Dense(metadata.features['label'].num_classes, | |
activation='softmax') | |
]) | |
This file contains hidden or 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
# Un-freeze the top layers of the model | |
base_model.trainable = True | |
# Let's take a look to see how many layers are in the base model | |
print("Number of layers in the base model: ", len(base_model.layers)) | |
# Fine tune from this layer onwards | |
fine_tune_at = 249 | |
# Freeze all the layers before the `fine_tune_at` layer | |
for layer in base_model.layers[:fine_tune_at]: |
This file contains hidden or 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
from tensorflow import keras | |
# '/1' specifies the version of a model, or "servable" we want to export | |
path_to_saved_model = 'SavedModel/inceptionv3_128_tf_flowers/1' | |
# Saving the keras model in SavedModel format | |
keras.experimental.export_saved_model(inception_model, path_to_saved_model) | |
# Load the saved keras model back | |
restored_saved_model = keras.experimental.load_from_saved_model(path_to_saved_model) |
This file contains hidden or 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 json, requests | |
from tensorflow.keras.preprocessing.image import img_to_array, load_img | |
import numpy as np | |
image_path = 'sunflower.jpg' | |
# Loading and pre-processing our input image | |
img = image.img_to_array(image.load_img(image_path, target_size=(128, 128))) / 255. | |
img = np.expand_dims(img, axis=0) | |
payload = {"instances": img.tolist()} |
This file contains hidden or 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
def augment_data(image, label): | |
print("Augment data called!") | |
image = tf.image.random_flip_left_right(image) | |
image = tf.image.random_contrast(image, lower=0.0, upper=1.0) | |
# Add more augmentation of your choice | |
return image, label | |
train = train.map(augment_data) |
This file contains hidden or 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 tensorflow as tf | |
def FCN_model(len_classes=5, dropout_rate=0.2): | |
# Input layer | |
input = tf.keras.layers.Input(shape=(None, None, 3)) | |
# A convolution block | |
x = tf.keras.layers.Conv2D(filters=32, kernel_size=3, strides=1)(input) | |
x = tf.keras.layers.Dropout(dropout_rate)(x) |
This file contains hidden or 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
# Uncomment the below line if you're using dense layers | |
# x = tf.keras.layers.GlobalMaxPooling2D()(x) | |
# Fully connected layer 1 | |
# x = tf.keras.layers.Dropout(dropout_rate)(x) | |
# x = tf.keras.layers.BatchNormalization()(x) | |
# x = tf.keras.layers.Dense(units=64)(x) | |
# x = tf.keras.layers.Activation('relu')(x) | |
# Fully connected layer 1 |