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 tensorflow as tf | |
# A simple convolutional neural network model | |
model = tf.keras.Sequential() | |
# A convolutional layer | |
model.add(tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3))) | |
# A pooling layer | |
model.add(tf.keras.layers.MaxPooling2D((2, 2))) | |
# Flattening the above result | |
model.add(tf.keras.layers.Flatten()) |
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 tensorflow as tf | |
from tensorflow.keras import layers, utils, models | |
# Input layer | |
read = layers.Input(shape=(64,64,1)) | |
# First feature extractor | |
conv1 = layers.Conv2D(16, kernel_size=3, activation='relu')(read) | |
pool1 = layers.MaxPooling2D(pool_size=(2, 2))(conv1) | |
flat1 = layers.Flatten()(pool1) | |
# Second feature extractor |
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 tensorflow as tf | |
class MyModel(tf.keras.Model): | |
def __init__(self): | |
super(MyModel, self).__init__(name='my_classifier') | |
# The layers are defined here | |
self.dense_1 = tf.keras.layers.Dense(16) | |
self.dense_2 = tf.keras.layers.Dense(1, activation='sigmoid') | |
def call(self, inputs): | |
# The forward pass is defined here |
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
model.compile( | |
# Mean Squared Error is used as loss function | |
loss = tf.keras.losses.MeanSquaredError(), | |
# If you do not pass parameters the default ones are used for Adam() | |
optimizer = tf.keras.optimizers.Adam(), | |
# Metrics are passed as a list seperated by commas. | |
metrics = ['accuracy'] | |
) | |
model.fit( |
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
callbacks = [ | |
tf.keras.callbacks.EarlyStopping( | |
# Monitoring the accuracy metric | |
monitor='accuracy', | |
# Specifies that the accuracy should improve by 1e-5. Which means that the absolute differnce | |
# between the current and the last value of accuracy should surpass min_delta | |
min_delta=1e-5, | |
# Training is stopped if accuracy does not imaprove for 5 epochs | |
patience=5 | |
) |
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
# Directory storing the images placed into folder such that the folder name corresponds | |
# to the class name inside of the folder | |
train_dir = '/tmp/data/' | |
# ImageDataGenerator can be used to augment images by rotation_range, zoom_range. | |
#The line below will scale pixel values between [0,1] | |
ImageDataGenerator(rescale = 1./255) | |
# Flow training images in batches of 20 | |
train_generator = datagen.flow_from_directory( |
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 tensorflow as tf | |
# Spread training across multiple GPUs | |
strategy = tf.distribute.MirroredStrategy() | |
with strategy.scope(): | |
model = tf.keras.Sequential([ | |
# Define model here | |
]) | |
# Compile model |
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 tensorflow as tf | |
# Create and train your model | |
model.fit() | |
# Save model using savedModel. It saves the complete model | |
tf.saved_model.save(model, "directory/to/model_dir") |
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 tensorflow as tf | |
# Load model | |
model = tf.saved_model.load("directory/to/model_dir") |
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 numpy as np | |
np.random.seed(0) | |
import tensorflow as tf | |
try: | |
tf.get_logger().setLevel('INFO') | |
except Exception as exc: | |
print(exc) | |
import warnings | |
warnings.simplefilter("ignore") |