<jupyter_start><jupyter_text><jupyter_code>import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
from tensorflow.keras.utils import to_categorical<jupyter_output><empty_output><jupyter_text>Data<jupyter_code>_URL = 'https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip'
path_to_zip = tf.keras.utils.get_file('cats_and_dogs.zip', origin=_URL, extract=True)
PATH = os.path.join(os.path.dirname(path_to_zip), 'cats_and_dogs_filtered')

train_dir = os.path.join(PATH, 'train')
validation_dir = os.path.join(PATH, 'validation')

BATCH_SIZE = 32
EPOCHS = 5
IMG_SIZE = (160, 160)
IMG_SHAPE = IMG_SIZE + (3,)<jupyter_output>Downloading data from https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip
68608000/68606236 [==============================] - 1s 0us/step
68616192/68606236 [==============================] - 1s 0us/step<jupyter_text>MobileNetV2<jupyter_code>base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                               include_top=False,
                                               weights='imagenet')
base_model.summary()<jupyter_output>Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/mobilenet_v2/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_160_no_top.h5
9412608/9406464 [==============================] - 0s 0us/step
9420800/9406464 [==============================] - 0s 0us/step
Model: "mobilenetv2_1.00_160"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, 160, 160, 3) 0                                            
__________________________________________________________________________________________________
Conv1 (Conv2D)                  (None, 80, 80, 32)   864         input_1[0][0]                    
________________________________________________________________________________________[...]<jupyter_text>Transfer Learning Model<jupyter_code>for layer in base_model.layers:
    layer.trainable = False
    
x = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)
x = tf.keras.layers.Dropout(0.2)(x)
outputs = tf.keras.layers.Dense(units=2, activation="softmax")(x)

model = tf.keras.Model(inputs=base_model.input, outputs=outputs)
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Data Generators<jupyter_code>train_datagen = ImageDataGenerator(
    rotation_range=15,
    rescale=1./255,
    shear_range=0.1,
    zoom_range=0.2,
    horizontal_flip=True,
    width_shift_range=0.1,
    height_shift_range=0.1
)
train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size=IMG_SIZE,
    class_mode='categorical',
    batch_size=32
)

validation_datagen = ImageDataGenerator(
    rescale=1./255,
)
validation_generator = validation_datagen.flow_from_directory(
    validation_dir, 
    target_size=IMG_SIZE,
    class_mode='categorical',
    batch_size=32
)<jupyter_output>Found 2000 images belonging to 2 classes.
Found 1000 images belonging to 2 classes.<jupyter_text>Training<jupyter_code>history = model.fit(train_generator,
                    epochs=EPOCHS,
                    validation_data=validation_generator)<jupyter_output>Epoch 1/5
63/63 [==============================] - 55s 375ms/step - loss: 0.2757 - accuracy: 0.8755 - val_loss: 0.1068 - val_accuracy: 0.9620
Epoch 2/5
63/63 [==============================] - 22s 348ms/step - loss: 0.1195 - accuracy: 0.9540 - val_loss: 0.1038 - val_accuracy: 0.9560
Epoch 3/5
63/63 [==============================] - 22s 355ms/step - loss: 0.1023 - accuracy: 0.9590 - val_loss: 0.0871 - val_accuracy: 0.9710
Epoch 4/5
63/63 [==============================] - 23s 361ms/step - loss: 0.1002 - accuracy: 0.9585 - val_loss: 0.1064 - val_accuracy: 0.9540
Epoch 5/5
63/63 [==============================] - 23s 363ms/step - loss: 0.0836 - accuracy: 0.9680 - val_loss: 0.0868 - val_accuracy: 0.9700<jupyter_text>Export to TFjs<jupyter_code>!pip install tensorflowjs
import tensorflowjs as tfjs
tfjs.converters.save_keras_model(model, "./export_model")
import shutil
shutil.make_archive("export_model", 'zip', "export_model/")

from google.colab import files
files.download('export_model.zip')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1746641074856046, 'fraction_numerical': 0.06589891234804862, 'mean_word_length': 4.403225806451613, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '243666', 'license': 'Apache-2.0', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1470, 'n_words': 243, 'path': 'lesson04/Transfer_Learning_DogsAndCats.ipynb', 'repo_name': 'simecek/dspracticum2021', 'size': '4689'}