!wget https://video.udacity-data.com/topher/2019/September/5d8e8cb3_tflite-apps/tflite-apps.zip !unzip tflite-apps.zip import tensorflow as tf # store data for x and y x = [-1,0,1,2,3,4] y = [-3,-1,1,3,5,7] # create a simple keras model model = tf.keras.models.Sequential( tf.keras.layers.Dense(units=1, input_shape=[1])) model.compile(optimizer='sgd', loss='mean_squared_error') model.fit(x, y, epochs=500) import pathlib # export the savedmodel export_dir = '/content/saved_model' tf.saved_model.save(model, export_dir) # convert the model converter = tf.lite.TFLiteConverter.from_saved_model(export_dir) tflite_model = converter.convert() # save the model tflite_model_file = pathlib.Path('/content/foo.tflite') tflite_model_file.write_bytes(tflite_model) ### Alternative - Directly convert keras model into tflite model ### import tensorflow as tf import pathlib # load the MobileNet tf.keras.model model = tf.keras.applications.MobileNetV2(weights='imagenet', input_shape=(224,224,3)) # convert the model converter = tf.lite.TFLiteConverter.from_keras_model(model) tflite_model = converter.convert() # save the model tflite_model_file = pathlib.Path('/content/foo2.tflite') tflite_model_file.write_bytes(tflite_model) # quantize only the weights converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE] tflite_quant_model = converter.convert() import tensorflow as tf import pathlib import numpy as np import matplotlib.pyplot as plt from tensorflow.keras.models import Model from tensorflow.keras.layers import Input # Create a simple Keras model. x = [-1, 0, 1, 2, 3, 4] y = [-3, -1, 1, 3, 5, 7] model = tf.keras.models.Sequential([ tf.keras.layers.Dense(units=1, input_shape=[1]) ]) model.compile(optimizer='sgd', loss='mean_squared_error') model.fit(x, y, epochs=200, verbose=1) # Generate a SavedModel export_dir = 'saved_model/1' tf.saved_model.save(model, export_dir) converter = tf.lite.TFLiteConverter.from_saved_model(export_dir) tflite_model = converter.convert() tflite_model_file = pathlib.Path('model.tflite') tflite_model_file.write_bytes(tflite_model) # Load TFLite model and allocate tensors. interpreter = tf.lite.Interpreter(model_content=tflite_model) interpreter.allocate_tensors() # Get input and output tensors. input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() # Test the TensorFlow Lite model on random input data. input_shape = input_details[0]['shape'] inputs, outputs = [], [] for _ in range(100): input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32) interpreter.set_tensor(input_details[0]['index'], input_data) interpreter.invoke() tflite_results = interpreter.get_tensor(output_details[0]['index']) # Test the TensorFlow model on random input data. tf_results = model(tf.constant(input_data)) output_data = np.array(tf_results) inputs.append(input_data[0][0]) outputs.append(output_data[0][0]) plt.plot(inputs, outputs, 'r') plt.show() try: from google.colab import files files.download(tflite_model_file) except: pass import os import matplotlib.pylab as plt import numpy as np import tensorflow as tf import tensorflow_hub as hub print("Version: ", tf.__version__) print("Eager mode: ", tf.executing_eagerly()) print("Hub version: ", hub.__version__) print("GPU is", "available" if tf.test.is_gpu_available() else "NOT AVAILABLE") module_selection = ("mobilenet_v2", 224, 1280) #@param ["(\"mobilenet_v2\", 224, 1280)", "(\"inception_v3\", 299, 2048)"] {type:"raw", allow-input: true} handle_base, pixels, FV_SIZE = module_selection MODULE_HANDLE ="https://tfhub.dev/google/tf2-preview/{}/feature_vector/4".format(handle_base) IMAGE_SIZE = (pixels, pixels) print("Using {} with input size {} and output dimension {}".format( MODULE_HANDLE, IMAGE_SIZE, FV_SIZE)) import tensorflow_datasets as tfds tfds.disable_progress_bar() (train_examples, validation_examples, test_examples), info = tfds.load( 'cats_vs_dogs', split=['train[80%:]', 'train[80%:90%]', 'train[90%:]'], with_info=True, as_supervised=True, ) num_examples = info.splits['train'].num_examples num_classes = info.features['label'].num_classes def format_image(image, label): image = tf.image.resize(image, IMAGE_SIZE) / 255.0 return image, label BATCH_SIZE = 32 #@param {type:"integer"} train_batches = train_examples.shuffle(num_examples // 4).map(format_image).batch(BATCH_SIZE).prefetch(1) validation_batches = validation_examples.map(format_image).batch(BATCH_SIZE).prefetch(1) test_batches = test_examples.map(format_image).batch(1) for image_batch, label_batch in train_batches.take(1): pass image_batch.shape do_fine_tuning = True #@param {type:"boolean"} feature_extractor = hub.KerasLayer(MODULE_HANDLE, input_shape=IMAGE_SIZE + (3,), output_shape=[FV_SIZE], trainable=do_fine_tuning) print("Building model with", MODULE_HANDLE) model = tf.keras.Sequential([ feature_extractor, tf.keras.layers.Dense(num_classes) ]) model.summary() #@title (Optional) Unfreeze some layers NUM_LAYERS = 7 #@param {type:"slider", min:1, max:50, step:1} if do_fine_tuning: feature_extractor.trainable = True for layer in model.layers[-NUM_LAYERS:]: layer.trainable = True else: feature_extractor.trainable = False if do_fine_tuning: model.compile( optimizer=tf.keras.optimizers.SGD(lr=0.002, momentum=0.9), loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) else: model.compile( optimizer='adam', loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) EPOCHS = 5 hist = model.fit(train_batches, epochs=EPOCHS, validation_data=validation_batches) CATS_VS_DOGS_SAVED_MODEL = "exp_saved_model" tf.saved_model.save(model, CATS_VS_DOGS_SAVED_MODEL) %%bash -s $CATS_VS_DOGS_SAVED_MODEL saved_model_cli show --dir $1 --tag_set serve --signature_def serving_default loaded = tf.saved_model.load(CATS_VS_DOGS_SAVED_MODEL) print(list(loaded.signatures.keys())) infer = loaded.signatures["serving_default"] print(infer.structured_input_signature) print(infer.structured_outputs) converter = tf.lite.TFLiteConverter.from_saved_model(CATS_VS_DOGS_SAVED_MODEL) converter.optimizations = [tf.lite.Optimize.DEFAULT] def representative_data_gen(): for input_value, _ in test_batches.take(100): yield [input_value] converter.representative_dataset = representative_data_gen converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8] tflite_model = converter.convert() tflite_model_file = 'converted_model.tflite' with open(tflite_model_file, "wb") as f: f.write(tflite_model) # Load TFLite model and allocate tensors. interpreter = tf.lite.Interpreter(model_path=tflite_model_file) interpreter.allocate_tensors() input_index = interpreter.get_input_details()[0]["index"] output_index = interpreter.get_output_details()[0]["index"] from tqdm import tqdm # Gather results for the randomly sampled test images predictions = [] test_labels, test_imgs = [], [] for img, label in tqdm(test_batches.take(10)): interpreter.set_tensor(input_index, img) interpreter.invoke() predictions.append(interpreter.get_tensor(output_index)) test_labels.append(label.numpy()[0]) test_imgs.append(img) #@title Utility functions for plotting # Utilities for plotting class_names = ['cat', 'dog'] def plot_image(i, predictions_array, true_label, img): predictions_array, true_label, img = predictions_array[i], true_label[i], img[i] plt.grid(False) plt.xticks([]) plt.yticks([]) img = np.squeeze(img) plt.imshow(img, cmap=plt.cm.binary) predicted_label = np.argmax(predictions_array) if predicted_label == true_label: color = 'green' else: color = 'red' plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label], 100*np.max(predictions_array), class_names[true_label]), color=color) #@title Visualize the outputs { run: "auto" } index = 0 #@param {type:"slider", min:0, max:9, step:1} plt.figure(figsize=(6,3)) plt.subplot(1,2,1) plot_image(index, predictions, test_labels, test_imgs) plt.show() labels = ['cat', 'dog'] with open('labels.txt', 'w') as f: f.write('\n'.join(labels)) try: from google.colab import files files.download('converted_model.tflite') files.download('labels.txt') except: pass !mkdir -p test_images from PIL import Image for index, (image, label) in enumerate(test_batches.take(50)): image = tf.cast(image * 255.0, tf.uint8) image = tf.squeeze(image).numpy() pil_image = Image.fromarray(image) pil_image.save('test_images/{}_{}.jpg'.format(class_names[label[0]], index)) !ls test_images !zip -qq cats_vs_dogs_test_images.zip -r test_images/ try: files.download('cats_vs_dogs_test_images.zip') except: pass # TensorFlow and tf.keras import tensorflow as tf from tensorflow import keras import tensorflow_datasets as tfds tfds.disable_progress_bar() # Helper libraries import numpy as np import matplotlib.pyplot as plt import pathlib print(tf.__version__) splits = tfds.Split.ALL.subsplit(weighted=(80, 10, 10)) splits, info = tfds.load('fashion_mnist', with_info=True, as_supervised=True, split=splits) (train_examples, validation_examples, test_examples) = splits num_examples = info.splits['train'].num_examples num_classes = info.features['label'].num_classes class_names = ['T-shirt_top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] with open('labels.txt', 'w') as f: f.write('\n'.join(class_names)) IMG_SIZE = 28 # Write a function to normalize and resize the images def format_example(image, label): # Cast image to float32 image = # YOUR CODE HERE # Resize the image if necessary image = # YOUR CODE HERE # Normalize the image in the range [0, 1] image = # YOUR CODE HERE return image, label # Set the batch size to 32 BATCH_SIZE = 32 # Prepare the examples by preprocessing the them and then batching them (and optionally prefetching them) # If you wish you can shuffle train set here train_batches = # YOUR CODE HERE validation_batches = # YOUR CODE HERE test_batches = # YOUR CODE HERE """ Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 26, 26, 16) 160 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 13, 13, 16) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 11, 11, 32) 4640 _________________________________________________________________ flatten (Flatten) (None, 3872) 0 _________________________________________________________________ dense (Dense) (None, 64) 247872 _________________________________________________________________ dense_1 (Dense) (None, 10) 650 ================================================================= Total params: 253,322 Trainable params: 253,322 Non-trainable params: 0 """ # Build the model shown in the previous cell model = tf.keras.Sequential([ # Set the input shape to (28, 28, 1), kernel size=3, filters=16 and use ReLU activation, tf.keras.layers.Conv2D(# YOUR CODE HERE), tf.keras.layers.MaxPooling2D(), # Set the number of filters to 32, kernel size to 3 and use ReLU activation tf.keras.layers.Conv2D(# YOUR CODE HERE), # Flatten the output layer to 1 dimension tf.keras.layers.Flatten(), # Add a fully connected layer with 64 hidden units and ReLU activation tf.keras.layers.Dense(# YOUR CODE HERE), # Attach a final softmax classification head tf.keras.layers.Dense(# YOUR CODE HERE)]) # Set the loss and accuracy metrics model.compile( optimizer='adam', loss=# YOUR CODE HERE, metrics=# YOUR CODE HERE) model.fit(train_batches, epochs=10, validation_data=validation_batches) export_dir = 'saved_model/1' # Use the tf.saved_model API to export the SavedModel # Your Code Here #@title Select mode of optimization mode = "Speed" #@param ["Default", "Storage", "Speed"] if mode == 'Storage': optimization = tf.lite.Optimize.OPTIMIZE_FOR_SIZE elif mode == 'Speed': optimization = tf.lite.Optimize.OPTIMIZE_FOR_LATENCY else: optimization = tf.lite.Optimize.DEFAULT optimization # Use the TFLiteConverter SavedModel API to initialize the converter converter = # YOUR CODE HERE # Set the optimzations converter.optimizations = # YOUR CODE HERE # Invoke the converter to finally generate the TFLite model tflite_model = # YOUR CODE HERE tflite_model_file = 'model.tflite' with open(tflite_model_file, "wb") as f: f.write(tflite_model) # Load TFLite model and allocate tensors. interpreter = tf.lite.Interpreter(model_content=tflite_model) interpreter.allocate_tensors() input_index = interpreter.get_input_details()[0]["index"] output_index = interpreter.get_output_details()[0]["index"] # Gather results for the randomly sampled test images predictions = [] test_labels = [] test_images = [] for img, label in test_batches.take(50): interpreter.set_tensor(input_index, img) interpreter.invoke() predictions.append(interpreter.get_tensor(output_index)) test_labels.append(label[0]) test_images.append(np.array(img)) #@title Utility functions for plotting # Utilities for plotting def plot_image(i, predictions_array, true_label, img): predictions_array, true_label, img = predictions_array[i], true_label[i], img[i] plt.grid(False) plt.xticks([]) plt.yticks([]) img = np.squeeze(img) plt.imshow(img, cmap=plt.cm.binary) predicted_label = np.argmax(predictions_array) if predicted_label == true_label.numpy(): color = 'green' else: color = 'red' plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label], 100*np.max(predictions_array), class_names[true_label]), color=color) def plot_value_array(i, predictions_array, true_label): predictions_array, true_label = predictions_array[i], true_label[i] plt.grid(False) plt.xticks(list(range(10)), class_names, rotation='vertical') plt.yticks([]) thisplot = plt.bar(range(10), predictions_array[0], color="#777777") plt.ylim([0, 1]) predicted_label = np.argmax(predictions_array[0]) thisplot[predicted_label].set_color('red') thisplot[true_label].set_color('green') #@title Visualize the outputs { run: "auto" } index = 49 #@param {type:"slider", min:1, max:50, step:1} plt.figure(figsize=(6,3)) plt.subplot(1,2,1) plot_image(index, predictions, test_labels, test_images) plt.show() plot_value_array(index, predictions, test_labels) plt.show() try: from google.colab import files files.download(tflite_model_file) files.download('labels.txt') except: pass !mkdir -p test_images from PIL import Image for index, (image, label) in enumerate(test_batches.take(50)): image = tf.cast(image * 255.0, tf.uint8) image = tf.squeeze(image).numpy() pil_image = Image.fromarray(image) pil_image.save('test_images/{}_{}.jpg'.format(class_names[label[0]].lower(), index)) !ls test_images !zip -qq fmnist_test_images.zip -r test_images/ try: files.download('fmnist_test_images.zip') except: pass !wget https://video.udacity-data.com/topher/2019/September/5d8e8ca8_lesson-3-android-apps/lesson-3-android-apps.zip !mv '/content/android-apps.zip' '/content/drive/My Drive/TFLite' !mv '/content/tflite-apps.zip' '/content/drive/My Drive/TFLite'