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
# 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) |
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
# 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) |
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
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) |
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
# 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], |
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
# 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) |
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
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 |
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 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() |
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'] |
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 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() |
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
# 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 |
NewerOlder