Skip to content

Instantly share code, notes, and snippets.

View sgodfrey66's full-sized avatar

Stephen Godfrey sgodfrey66

View GitHub Profile
# Define the confusion matrix logging function
def cm_logger(epoch, logs):
# Create a name for this image
i_name='Confusion Matrix: {}; {}'.format(model_name, ds.dataset_name)
# Create the per-epoch callback for the confusion matrix
cm_image=image_funcs.plot_to_image(metrics_funcs.generate_heatmap(
dataset=ds,
model=model).figure)
# Create a callback for TensorBoard and put it in a list
callbacks=[tf.keras.callbacks.TensorBoard(log_dir=model_log,
histogram_freq=1,
write_graph=True,
write_images=False,
update_freq='epoch')]
# Create the TensorBoard writer for the confusion matrix writer
cm_file_writer=tf.summary.create_file_writer(logdir=cm_log)
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)
# Create a TensorBoard writer for sample images
image_writer=tf.summary.create_file_writer(logdir=image_log)
# Generate an image grid in a format that can be used by TensorBoard
rnd_imgs=image_funcs.create_TB_image_grid(dataset=ds,
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],
# Create a TensorBoard log directory for input images
init_time=dt.datetime.now(tz.timezone('US/Pacific')).strftime("%b%d_%H:%M")
images_logs='input_images'
image_log='{}'.format(os.path.join(logs_dir,images_logs))
# 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)
class VGG16Test(tf.keras.models.Sequential):
"""Construct a Sequential model using transfer learning with
VGG16 as the base.
This class uses some transfer learning and follows the work of Dr. Sivarama Krishnan Rajaraman, et al in
using 'pre-trained convolutional neural networks' to detect malaria infections in thin blood smear samples;
specifically, the pretrained VGG16 model.
As is the case with SequentialTest, we're building a Sequential model in which the first layer is a part of
the VGG16 model. Therefore, we can follow the same approach in which this class inherits from the
# Load a dataset
tensorflow_datasets.load
# Iterate through a dataset
tensorflow.data.Dataset.__iter__()
# Shuffle a dataset
tensorflow.data.Dataset.shuffle()
# Batch a dataset
tensorflow.data.Dataset.batch()
# Map a function to Dataset elements
tensorflow.data.Dataset.map()
@sgodfrey66
sgodfrey66 / modeling_pipeline.py
Created May 30, 2020 16:05
A TensorFlow Modeling Pipeline using TensorFlow Datasets and TensorBoard
# 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']
# Run the model evaluator function to find the best models
# with between 1 and 13 features selecting from the features list
model_df = model_evaluator(X, y, features, 1, len(features))
# Rearrange columns in the data frame
model_df = model_df[['features', 'model', 'no_features', 'intercept', 'coeff', 'R2', 'CV_R2']]
# Sort the resulting data frame to see the best feature sets
model_df.sort_values(by = 'CV_R2', ascending = False).head()
# Imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score