Created
May 30, 2020 16:05
-
-
Save sgodfrey66/60eef1e881cd144e2d1ac765fe54bfbe to your computer and use it in GitHub Desktop.
A TensorFlow Modeling Pipeline using TensorFlow Datasets and TensorBoard
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
# Run through the pipeline for each test defined above in tests | |
# If you don't want to wait for processing to complete, you can shorten the | |
# the test details list | |
for test in tests: | |
# 1. Read the inputs from the test details dictionary | |
dataset_name=test['dataset_name'] | |
shuffle_size=test['shuffle_size'] | |
batch_size=test['batch_size'] | |
target_height=test['target_height'] | |
target_width=test['target_width'] | |
target_channels=test['target_channels'] | |
model_name=test['model_name'] | |
epochs=test['epochs'] | |
# 2. Read the data by intantiating a class which | |
# does the data loading & processing | |
ds=ImageLoaderTFDS(dataset_name=dataset_name, | |
shuffle_size=shuffle_size, | |
batch_size=batch_size, | |
target_height=target_height, | |
target_width=target_width) | |
# 3. Set up some parameters for TensorBoard | |
# Record the time to use for labeling TensorBoard contents | |
init_time=dt.datetime.now(tz.timezone('US/Pacific')).strftime("%b%d_%H:%M") | |
# Create a TensorBoard log directory for input images | |
images_logs='input_images' | |
image_log='{}'.format(os.path.join(logs_dir,images_logs)) | |
# Set the number of random images to show; perfect squares work best | |
no_log_images=16 | |
log_images_split='train' | |
# Create a TensorBoard log directory for training and evaluation data | |
model_log='{}_{}'.format(os.path.join(logs_dir,model_name),init_time) | |
# Create a TensorBoard log directory for the confusion matrix | |
cm_log='{}_{}/conf_matrix'.format(os.path.join(logs_dir,model_name),init_time) | |
# 4. Set up some parameters for modeling | |
# Define the input shape | |
input_shape=(target_height,target_width,target_channels) | |
# Get the number of classes from the dataset | |
num_classes=ds.num_classes | |
# 5. Save some example images to the TensorBoard logs | |
# Create a file_writer for the graph | |
image_writer=tf.summary.create_file_writer(logdir=image_log) | |
rnd_imgs=create_TB_image_grid(no_log_images=no_log_images, | |
log_images_split=log_images_split) | |
# Add it to the TensorBoard logs | |
with image_writer.as_default(): | |
tf.summary.image(name=rnd_imgs[0], data=rnd_imgs[2], | |
max_outputs=no_log_images, step=0, | |
description=rnd_imgs[1]) | |
# 6. Build the model by instantiating the model class of interest | |
if model_name=='TBConvTest': | |
# Instantiate the TBConvTest | |
model=TBConvTest(model_name=model_name, | |
input_shape=input_shape, | |
num_classes=num_classes) | |
# Set the loss function | |
loss_object = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) | |
# Choose the optimizer | |
optimizer = tf.keras.optimizers.Adam() | |
elif model_name=='VGG16Test': | |
# Instantiate the VGG16Test | |
model=VGG16Test(model_name=model_name, | |
input_shape=input_shape, | |
num_classes=num_classes) | |
# Set the loss function | |
loss_object = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) | |
# Choose the optimizer | |
optimizer = tf.keras.optimizers.SGD(lr=0.00001, decay=1e-6, momentum=0.9, nesterov=True) | |
# 7. Create callbacks for TensorBoard | |
# Create a callback for TensorBoard | |
tb_callback=tf.keras.callbacks.TensorBoard(log_dir=model_log, | |
histogram_freq=1, | |
write_graph=True, | |
write_images=False, | |
update_freq='epoch') | |
# Create a list of callbacks and add the TensorBoard callback | |
callbacks = [tb_callback] | |
# Create the TensorBoard writer for the confusion matrix writer | |
cm_file_writer=tf.summary.create_file_writer(logdir=cm_log) | |
# Create the per-epoch callback for the confusion matrix | |
cm_callback=keras.callbacks.LambdaCallback(on_epoch_end=log_confusion_matrix) | |
# Add it to the callbacks list | |
callbacks.append(cm_callback) | |
# 8. Compile the model | |
model.compile(optimizer=optimizer, | |
loss=loss_object, | |
metrics=['accuracy']) | |
# 9. Fit the model | |
history=model.fit(x=ds.train_ds_modeling, | |
y=None, | |
batch_size=None, | |
verbose=1, | |
validation_split=0.0, | |
epochs=epochs, | |
validation_data=ds.test_ds_modeling, | |
callbacks=callbacks) | |
# 10. Evaluate the model | |
result=model.evaluate(x=ds.test_ds_modeling, | |
verbose=True, | |
callbacks=callbacks) | |
# 11. Free some memory resources | |
# https://www.tensorflow.org/api_docs/python/tf/keras/backend/clear_session | |
tf.keras.backend.clear_session() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment