Exercise Learn Training By Example: python, Jupyter Notebook, TensorFlow
...with Keras using TesnorBoard and deploying to Android for runtime
PART 1 Learn Training By Example INDIVIDUAL work
-
Pre-requistes: experience you gained in Lab (Classification) and go to the OLDER example ONLY to which will show you where the Cat/Dog data I wish you to use is located.
-
Task:
-
Create your OWN COLAB (hosted in your Google Drive) so that it users the Cat/Dog training data from the OLDER example. Youo must download this code and then upload it to your Google Drive. You also must learn how to mount your Google Drive (serach online for this info) in the Colab and use it to create datasets for training/validation and testing.
from google.colab import drive drive.mount('/content/gdrive/', force_remount=True)
-
Add code to SAVE checkpoints to your Google Drive in some directory as you train. Read this information to learn how through a callback function used in training.
# see https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/ModelCheckpointcheckpoint_dir = "content/drive/MyDrive/ObjectDetection/SmartFashion/Regression/TrainingRun/Run8"checkpoint_dir = checkpoint_dir + "/checkpoint-{epoch:02d}.tf"
#callback for ModelCheckpoint saving
modelcheckpoint_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_dir, save_best_only=True, verbose=1, save_freq='epoch')
#callback for tensorboardtensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
NOW your model.fit will become something like
epochs = 10history = model.fit(train_ds,validation_data=val_ds,epochs=epochs,callbacks=[modelchecpoint_callback, tensorboard_callback]) -
ALL generated log files and the model itself need to be saved on your Google Drive. Make sure when you specify this in your colab as paths.
-
Make sure you have run evaluation on your testing dataset and have run Tensorboard to visualize the results of training.
-
PARAMETER TURNING. NOW rerun training by changing the following hyper-parameters (epochs, batch, learning rate& Optimizer) one at a time. For each change show the resulting TensorBoard result. Discuss why you made the change in each parameter (up or down) what is going on with your results when you changed these parameters.
-
Add code in a cell to reload the model from a checkpoint that was saved (see step 2). This is particularly useful when your training stops and you wish to continue.
AFTER you have done some training and saved some checkpoints. You can then Load the checkpoint weights. Step 1 not shown is to define the model (this code is in the colab alread). Step 2 shown below is to use this model and load the weights.
NEXT the model is ready to use for predictions or to continue training with
#specify the path of model checkpoint you want to load....here is an examplecheckpoint_path = "/content/drive/MyDrive/ObjectDetection/SmartFashion/Regression/TrainingRun/Run2/checkpoint-436-0.72.tf"
# Loads the weightsmodel.load_weights(checkpoint_path) -
Add code to save your model. Here is some example code (search online for latest)
saved_model_path = "/content/drive/MyDrive/ObjectDetection/SmartFashion/Regression/saved_model"print(saved_model_path)!ls $saved_model_path#tf.saved_model.save(model, saved_model_path)model.save(saved_model_path, save_format='tf') # NOTE-there is a new saved format .keras --otherwise you must specify tf to create SAVEDMODEL format
-
Add code to convert your model to a TFlite model. Here is some sample code --but, search online for latest YOU MUST NOW LOOK AT THE LATEST FOR DISCOVERING THE CORRECT CODE
# Convert the model.converter = tf.lite.TFLiteConverter.from_keras_model(model)tflite_model = converter.convert()
# Save the model.tflite_model_location = "drive/MyDrive/iLab/Orientation/ML_MultiInput_Regression/tfLiteModel/Run3/model-608.tflite"
#create directory if does not existfrom pathlib import Pathfile = Path(os.path.dirname(tflite_model_location))print("creating " + str(file))file.parent.mkdir(parents=True, exist_ok=True)file.mkdir(parents=True, exist_ok=True)
#savewith open(tflite_model_location, 'wb') as f:f.write(tflite_model)
-
-
Deliverables: turn in the following screenshots+Descriptions in a single PDF document.
- Screenshot 1: After first trainging - Tensorboard accuracy/loss curves + Discuss what you think is happening in the curves. Do you think you need to train further? Is there any overtraining?
- Screenshot 2 + Discussion: After alter epochs - Tensorboard accuracy/loss curves
- Screenshot 3 + Discussion: After alter batchsize - Tensorboard accuracy/loss curves
- Screenshot 4 + Discussion: After learning rate change - Tensorboard accuracy/loss curves
- Screenshot 5 + Discussion: After Optimizer change - Tensorboard accuracy/loss curves
-
Resources + Special Notes:
-
See our outline page that discusses how to setup a COLAB or search the web/Goolge for information on how to do it: Training on Colab and Instructions for Colab setup and Google Provided Colab Basics.
CAUTION: APIs change all of the time. For example, in the older example: After tensorflow 2.2+ tf.keras.model.fit will work and tf.keras.model.fit_generator will be deprecated.
Part 2: Android Deployment GROUP work
-
Pre-requistes: Read on Android Website how to create a Tensorflow Lite Applicaiton for Mobile Devlopment-- use the information for this linked off our Android section of our website outline page. You can also look (but, dont use) the following OLDER example that is purely Java based (base Android App w/ML ). Also, read latest from Android on how to covert a trained model to an android / mobile ready model
-
Task:
-
Make sure you save your Model in your Google drive
-
Make sure you display the recognition results = Dog X% , Cat Y%
-
Convert your Model you created in Part 1 to a TFlite model for mobile deployment.
-
Create a Kotlin/Java or Kotlin or Java Android Application to deploy your ML model to classify Dog/Cats. NOTE there may be some changes from sample code you have from Google AI Edge to support the new LiteRT(previously called TFlite)
-
Test your application and see if it works.
-
Deliverables
-
FOR EACH PART 1 & 2 -
-
part 1 = INDIVIDUAL work Post to Canvas-Assignments-Ex:Learn Training PART 1 a single PDF document containing the following screenshots+descriptions. This is INDIVIDUAL work.
- Screenshot 1: After first trainging - Tensorboard accuracy/loss curves + Discuss what you think is happening in the curves. Do you think you need to train further? Is there any overtraining?
- Screenshot 2 + Discussion: After alter epochs - Tensorboard accuracy/loss curves
- Screenshot 3 + Discussion: After alter batchsize - Tensorboard accuracy/loss curves
- Screenshot 4 + Discussion: After learning rate change - Tensorboard accuracy/loss curves
- Screenshot 5 + Discussion: After Optimizer change - Tensorboard accuracy/loss curves
-
part 2 = GROUP work (turn in one per group) Convert model to TFLite and get it to work in your Android app you had working from previous exercises. You will need to have the model and the corresponding label file as part of the assets in your Android Application. Make a Youtube video showing the application working and discuss any problems you had in creating the application and how you resolved them. Post to Canvas-Assignments-Ex:Learn Training PART 2
-