|
| 1 | +# THIS CODE IS A MODULARIZED AND UPDATED VERSION OF CODE FROM THE "DEEPLEARNING.AI TENSORFLOW DEVELOPER" COURSE |
| 2 | +# SOURCE: |
| 3 | +# https://github.com/lmoroney/dlaicourse/blob/master/Course%201%20-%20Part%204%20-%20Lesson%202%20-%20Notebook.ipynb |
| 4 | + |
| 5 | +import matplotlib.pyplot as plt |
| 6 | +import numpy as np |
| 7 | +import tensorflow as tf |
| 8 | +from tensorflow import keras |
| 9 | +from tensorflow.keras import layers |
| 10 | + |
| 11 | +import ptmlib.model_tools as modt |
| 12 | + |
| 13 | + |
| 14 | +class MyCallback(keras.callbacks.Callback): |
| 15 | + |
| 16 | + def __init__(self, target): |
| 17 | + super().__init__() |
| 18 | + self.target = target |
| 19 | + |
| 20 | + def on_epoch_end(self, _, logs=None): |
| 21 | + |
| 22 | + if logs is None: |
| 23 | + logs = {} |
| 24 | + if logs.get("accuracy") > self.target: |
| 25 | + print(f"\nReached {self.target * 100}% accuracy so cancelling training!") |
| 26 | + self.model.stop_training = True |
| 27 | + |
| 28 | + |
| 29 | +def print_diagnostics() -> None: |
| 30 | + print('TF VERSION:', tf.__version__) |
| 31 | + print('KERAS VERSION:', keras.__version__) |
| 32 | + |
| 33 | + |
| 34 | +def get_data(): |
| 35 | + mnist = keras.datasets.fashion_mnist |
| 36 | + (training_images, training_labels), (test_images, test_labels) = mnist.load_data() |
| 37 | + |
| 38 | + np.set_printoptions(linewidth=200) |
| 39 | + plt.imshow(training_images[0]) |
| 40 | + plt.show() |
| 41 | + |
| 42 | + print(training_labels[0]) |
| 43 | + |
| 44 | + # normalize image data to values between 0 and 1 |
| 45 | + training_images = training_images / 255.0 |
| 46 | + test_images = test_images / 255.0 |
| 47 | + |
| 48 | + return (training_images, training_labels), (test_images, test_labels) |
| 49 | + |
| 50 | + |
| 51 | +def get_model() -> keras.models.Sequential: |
| 52 | + model = keras.models.Sequential([ |
| 53 | + layers.Flatten(input_shape=(28, 28)), |
| 54 | + layers.Dropout(0.2), |
| 55 | + layers.Dense(512, activation=tf.nn.relu), |
| 56 | + layers.Dense(10, activation=tf.nn.softmax) |
| 57 | + ]) |
| 58 | + |
| 59 | + model.summary() |
| 60 | + |
| 61 | + model.compile( |
| 62 | + optimizer=tf.optimizers.Adam(), |
| 63 | + loss="sparse_categorical_crossentropy", |
| 64 | + metrics=["accuracy"] |
| 65 | + ) |
| 66 | + |
| 67 | + return model |
| 68 | + |
| 69 | + |
| 70 | +def main(): |
| 71 | + |
| 72 | + # HYPER PARAMS, CONSTANTS, ETC |
| 73 | + hp_epochs = 50 |
| 74 | + hp_target = 0.91 |
| 75 | + hp_validation_split = 0.2 |
| 76 | + model_file_name = "computer_vision_1" |
| 77 | + |
| 78 | + print_diagnostics() |
| 79 | + |
| 80 | + (training_images, training_labels), (test_images, test_labels) = get_data() |
| 81 | + |
| 82 | + model = get_model() |
| 83 | + |
| 84 | + early_callback = MyCallback(target=hp_target) |
| 85 | + |
| 86 | + fit_model_function_with_callback = lambda my_model, x, y, validation_data, epochs: my_model.fit( |
| 87 | + x, y, validation_data, epochs=epochs, callbacks=[early_callback], validation_split=hp_validation_split) |
| 88 | + |
| 89 | + model, history = modt.load_or_fit_model(model, model_file_name, x=training_images, y=training_labels, |
| 90 | + epochs=hp_epochs, fit_model_function=fit_model_function_with_callback, |
| 91 | + metrics=["accuracy"]) |
| 92 | + |
| 93 | + model.evaluate(test_images, test_labels) |
| 94 | + |
| 95 | + classifications = model.predict(test_images) |
| 96 | + print(classifications[0]) |
| 97 | + print(test_labels[0]) |
| 98 | + print(max(classifications[0])) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + main() |
0 commit comments