# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="WbdSBVjN_LS1"
# # Save and Store Features
# In this notebook we will compute all prediction and store the relative features in drive using the model computed in the notebook "ResNet50".
#
# *Note*: the features related to the simple feature extraction model are already computed in the notebook "ResNet50", thus they won't again be computed here.

# + id="fOEEsshaASKY" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643386358392, "user_tz": -60, "elapsed": 24963, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="e1269398-eaa9-4a53-be82-0dad30b6c8a4"
from google.colab import drive
drive.mount('/content/drive')

# + id="768ZK3lEBFKZ" executionInfo={"status": "ok", "timestamp": 1643386370184, "user_tz": -60, "elapsed": 2936, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
import tensorflow as tf
from tensorflow import keras as ks
from tensorflow.keras import layers
from tensorflow.keras.applications import ResNet50V2
from tensorflow.keras import regularizers
import pathlib
import matplotlib.pyplot as plt
import numpy as np

# + colab={"base_uri": "https://localhost:8080/", "height": 73, "resources": {"http://localhost:8080/nbextensions/google.colab/files.js": {"data": "<KEY>", "headers": [["content-type", "application/javascript"]], "ok": true, "status": 200, "status_text": ""}}} executionInfo={"elapsed": 8404, "status": "ok", "timestamp": 1643386378584, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}, "user_tz": -60} id="_B3mi1RqblLY" outputId="9f68bf0f-1e0b-4fed-a08e-5b3017293a46"
# ! pip install -q kaggle

from google.colab import files
_ = files.upload()

# ! mkdir -p ~/.kaggle
# ! cp kaggle.json ~/.kaggle/
# ! chmod 600 ~/.kaggle/kaggle.json

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"elapsed": 12553, "status": "ok", "timestamp": 1643386395251, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}, "user_tz": -60} id="VoWnE3oCb1eG" outputId="a1e20e34-667a-439a-c2b5-9255b3c60d7c"
# ! kaggle datasets download -d gpiosenka/100-bird-species

# + colab={"base_uri": "https://localhost:8080/"} id="-tdH48JqcIDE" outputId="666cae5c-ed4d-4e8b-8bc3-2f57d4d3cc4b" executionInfo={"status": "ok", "timestamp": 1643386419497, "user_tz": -60, "elapsed": 21194, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
# !unzip 100-bird-species.zip

# + [markdown] id="SMGEEWo0_2pg"
# ## Create the different sets
# In this section the training set, the test set and the discrimator sets are computed in order to extract the features from them

# + id="_jrjDHV9-xFI" executionInfo={"status": "ok", "timestamp": 1643386484042, "user_tz": -60, "elapsed": 235, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
TRAIN_DIR = 'train/'
VALID_DIR = 'valid/'
TEST_DIR = 'test/'
DISTRACTOR_DIR = 'mirflickr'
BATCH_SIZE = 128
IMAGE_HEIGHT = 224
IMAGE_WIDTH = 224
RANDOM_SEED = 42

# + [markdown] id="Kns-kYL_dslj"
# Distractor path:

# + id="G1X7pqSOduoD" executionInfo={"status": "ok", "timestamp": 1643386596818, "user_tz": -60, "elapsed": 111439, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
# !unzip -q '/content/drive/My Drive/CV_Birds/mirflickr.zip' -d '/content'

# + [markdown] id="sSl3bW9BdvII"
# Create sets:

# + id="zSs64XNtAAaX" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643368549784, "user_tz": -60, "elapsed": 5004, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="3a785598-2e21-499c-c1bf-30e1b447a081"
training_images = tf.keras.preprocessing.image_dataset_from_directory(
    TRAIN_DIR, labels='inferred', label_mode='categorical',
    class_names=None, color_mode='rgb', batch_size=BATCH_SIZE, image_size=(IMAGE_HEIGHT,
    IMAGE_WIDTH), shuffle=False, seed=RANDOM_SEED, interpolation='bilinear')

test_images = tf.keras.preprocessing.image_dataset_from_directory(
    TEST_DIR, labels='inferred', label_mode='categorical',
    class_names=None, color_mode='rgb', batch_size=BATCH_SIZE, image_size=(IMAGE_HEIGHT,
    IMAGE_WIDTH), shuffle=False, seed=RANDOM_SEED, interpolation='bilinear')

# + colab={"base_uri": "https://localhost:8080/"} id="-Ib-8nmbcsfZ" executionInfo={"status": "ok", "timestamp": 1643386604875, "user_tz": -60, "elapsed": 6336, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="b53c1bc5-e9e6-4fb5-d4ac-c545227776f2"
distractor_images = tf.keras.preprocessing.image_dataset_from_directory(
    DISTRACTOR_DIR,
    image_size = (IMAGE_HEIGHT, IMAGE_WIDTH),
    batch_size = BATCH_SIZE,
    seed=RANDOM_SEED,
    labels=None,
    label_mode=None)

# + [markdown] id="tiaw9QlvF0Di"
# ## Model 1
# Load the model from drive:

# + id="KIH-VSPCF0Dj" executionInfo={"status": "ok", "timestamp": 1643387103416, "user_tz": -60, "elapsed": 3755, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
MODEL_PATH = '/content/drive/MyDrive/CV_Birds/models/ResNet50v2/model1.keras'
model = ks.models.load_model(MODEL_PATH)

# + colab={"base_uri": "https://localhost:8080/"} id="S_i7Di5-bJ_D" executionInfo={"status": "ok", "timestamp": 1643303702087, "user_tz": -60, "elapsed": 7, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="e8934e4d-245a-4a0d-8a32-baf664a5b2ed"
model.summary()

# + id="buMzVaHqbcDi" executionInfo={"status": "ok", "timestamp": 1643387103761, "user_tz": -60, "elapsed": 348, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
a = ks.models.Sequential(model.layers[:2])

# + colab={"base_uri": "https://localhost:8080/"} id="XgU19qq0fao1" executionInfo={"status": "ok", "timestamp": 1643303702510, "user_tz": -60, "elapsed": 7, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="e2ebd367-9ae7-492b-8851-a1686c1469d2"
a.summary()

# + [markdown] id="obhgZHk5F0Dj"
# Predict features for training set and save them:

# + id="599cWVnhF0Dk" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643303965780, "user_tz": -60, "elapsed": 263275, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="b0fc9bd5-7a87-4a31-8551-fc6b5c03c057"
features_model = a.predict(training_images, batch_size=BATCH_SIZE, verbose=True)

# + id="8wC29uGmF0Dk"
np.save('/content/drive/MyDrive/CV_Birds/features/training/ResNet50v2/model1_train_features.npy', features_model)

# + [markdown] id="Eyz9_Ww5F0Dk"
# Predict features for test set and save them:

# + id="2vOD2g7KF0Dk" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643304080016, "user_tz": -60, "elapsed": 11865, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="4a05baba-c393-454b-8b8d-260800e6e10b"
features_model = a.predict(test_images, batch_size=BATCH_SIZE, verbose=True)

# + id="_GkPaYYJF0Dk"
np.save('/content/drive/MyDrive/CV_Birds/features/test/ResNet50v2/model1_test_features.npy', features_model)

# + [markdown] id="Xnwv2XZrF0Dl"
# Predict features for the distractor and save them

# + id="Sroc6txMF0Dl" colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643387235092, "user_tz": -60, "elapsed": 128023, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="a1bc1d3d-7f8c-4e62-ac4c-0e3770bf8e05"
features_model = a.predict(distractor_images, batch_size=BATCH_SIZE, verbose=True)

# + id="3rAelSeRF0Dl" executionInfo={"status": "ok", "timestamp": 1643387235800, "user_tz": -60, "elapsed": 712, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
np.save('/content/drive/MyDrive/CV_Birds/features/distractor/ResNet50v2/model1_distractor_features.npy', features_model)

# + [markdown] id="YblEBZqNjqfy"
# ## Model 2
# Load the model from drive:

# + id="72Reuutkjqf1" executionInfo={"status": "ok", "timestamp": 1643387239299, "user_tz": -60, "elapsed": 3500, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
MODEL_PATH = '/content/drive/MyDrive/CV_Birds/models/ResNet50v2/model2.keras'
model = ks.models.load_model(MODEL_PATH)

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643386856251, "user_tz": -60, "elapsed": 9, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="c6376608-8c74-4361-f1c3-0914ae87ffa4" id="lLlo70Ovjqf2"
model.summary()

# + id="8cIySEGpjqf3" executionInfo={"status": "ok", "timestamp": 1643387239300, "user_tz": -60, "elapsed": 5, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
a = ks.models.Sequential(model.layers[:2])

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643386856627, "user_tz": -60, "elapsed": 5, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="f2f8c271-8679-46f5-f9a8-96507ad61a1f" id="7viI_8MSjqf3"
a.summary()

# + [markdown] id="Ww7dtoXojqf3"
# Predict features for training set and save them:

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643304432097, "user_tz": -60, "elapsed": 247401, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="14aa659b-0661-4a43-b180-21acc1b0e053" id="f0wqJFjsjqf4"
features_model = a.predict(training_images, batch_size=BATCH_SIZE, verbose=True)

# + id="z6151Ut0jqf4"
np.save('/content/drive/MyDrive/CV_Birds/features/training/ResNet50v2/model2_train_features.npy', features_model)

# + [markdown] id="5FtDBTpYjqf4"
# Predict features for test set and save them:

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643304444264, "user_tz": -60, "elapsed": 10304, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="a9543955-3f44-4b16-be1c-b481a8b21c2c" id="KknjPS9ljqf4"
features_model = a.predict(test_images, batch_size=BATCH_SIZE, verbose=True)

# + id="HcNAHf6wjqf5"
np.save('/content/drive/MyDrive/CV_Birds/features/test/ResNet50v2/model2_test_features.npy', features_model)

# + [markdown] id="SHR1JRd4jqf5"
# Predict features for the distractor and save them

# + colab={"base_uri": "https://localhost:8080/"} outputId="49bdc896-e736-42f2-9168-d941ff3d1ae4" id="gQNwThAMe6_r" executionInfo={"status": "ok", "timestamp": 1643387368216, "user_tz": -60, "elapsed": 127620, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
features_model = a.predict(distractor_images, batch_size=BATCH_SIZE, verbose=True)

# + id="rmCe7_V4e6_s" executionInfo={"status": "ok", "timestamp": 1643387369211, "user_tz": -60, "elapsed": 999, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
np.save('/content/drive/MyDrive/CV_Birds/features/distractor/ResNet50v2/model2_distractor_features.npy', features_model)

# + [markdown] id="PGIKNMo5kAeE"
# ## Model 3
# Load the model from drive:

# + id="ZyJxuQj2kAeE" executionInfo={"status": "ok", "timestamp": 1643387376298, "user_tz": -60, "elapsed": 7089, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
MODEL_PATH = '/content/drive/MyDrive/CV_Birds/models/ResNet50v2/model3.keras'
model = ks.models.load_model(MODEL_PATH)

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643387376298, "user_tz": -60, "elapsed": 5, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="e7f64b5e-f06f-4e8a-a008-2e7b8cb8fe31" id="mhTa5XOqkAeF"
model.summary()

# + id="rBiUUqtMkAeF" executionInfo={"status": "ok", "timestamp": 1643387376899, "user_tz": -60, "elapsed": 604, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
a = ks.models.Sequential(model.layers[:2])

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643387376900, "user_tz": -60, "elapsed": 6, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="942f936f-7255-4333-ea7e-e21cacf0721d" id="j2bp2dX8kAeG"
a.summary()

# + [markdown] id="68ARb_HekAeG"
# Predict features for training set and save them:

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643304766086, "user_tz": -60, "elapsed": 246879, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="41b257df-6e2b-46ee-bead-935126aad3eb" id="sb9l53_JkAeG"
features_model = a.predict(training_images, batch_size=BATCH_SIZE, verbose=True)

# + id="92eo3ExZkAeG"
np.save('/content/drive/MyDrive/CV_Birds/features/training/ResNet50v2/model3_train_features.npy', features_model)

# + [markdown] id="dxG9sRgikAeH"
# Predict features for test set and save them:

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643304776783, "user_tz": -60, "elapsed": 9096, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="b2ecbfb5-7af7-4608-e217-5af77891516c" id="zO9PlwFJkAeH"
features_model = a.predict(test_images, batch_size=BATCH_SIZE, verbose=True)

# + id="6IT0_mDckAeH"
np.save('/content/drive/MyDrive/CV_Birds/features/test/ResNet50v2/model3_test_features.npy', features_model)

# + [markdown] id="N0eNEEm_kAeH"
# Predict features for the distractor and save them

# + colab={"base_uri": "https://localhost:8080/"} outputId="cc54eca4-6a21-4311-8440-4a2c2e9c05cd" id="LduuGJh7fBw1" executionInfo={"status": "ok", "timestamp": 1643387506032, "user_tz": -60, "elapsed": 129136, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
features_model = a.predict(distractor_images, batch_size=BATCH_SIZE, verbose=True)

# + id="NbLkfXRTfBw2" executionInfo={"status": "ok", "timestamp": 1643387506873, "user_tz": -60, "elapsed": 845, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
np.save('/content/drive/MyDrive/CV_Birds/features/distractor/ResNet50v2/model3_distractor_features.npy', features_model)

# + [markdown] id="Nv4nZ-GAkHsF"
# ## Model 4
# Load the model from drive:

# + id="sxqkBLIBkHsG" executionInfo={"status": "ok", "timestamp": 1643387515198, "user_tz": -60, "elapsed": 8327, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
MODEL_PATH = '/content/drive/MyDrive/CV_Birds/models/ResNet50v2/model4.keras'
model = ks.models.load_model(MODEL_PATH)

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643387515199, "user_tz": -60, "elapsed": 6, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="2551d798-1416-4424-a86d-292398e56b5e" id="M4bwdhj6kHsG"
model.summary()

# + id="R-v6GF9rkHsH" executionInfo={"status": "ok", "timestamp": 1643387515850, "user_tz": -60, "elapsed": 655, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
a = ks.models.Sequential(model.layers[:2])

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643387515850, "user_tz": -60, "elapsed": 4, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="28525c75-6484-4d11-e470-02186e3b4d25" id="SUc2EWfkkHsH"
a.summary()

# + [markdown] id="cUOeiZXLkHsH"
# Predict features for training set and save them:

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643305029605, "user_tz": -60, "elapsed": 247637, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="67b0646e-d44b-4936-c3d9-ad2a27be35b5" id="d5Gxk0sDkHsH"
features_model = a.predict(training_images, batch_size=BATCH_SIZE, verbose=True)

# + id="Y0c_OxAkkHsI"
np.save('/content/drive/MyDrive/CV_Birds/features/training/ResNet50v2/model4_train_features.npy', features_model)

# + [markdown] id="H3x_VD3CkHsI"
# Predict features for test set and save them:

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643305039742, "user_tz": -60, "elapsed": 9109, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="e0db5384-f501-49f3-ca33-b507ff079a2c" id="jit4jPvbkHsI"
features_model = a.predict(test_images, batch_size=BATCH_SIZE, verbose=True)

# + id="DzEw4QD2kHsI"
np.save('/content/drive/MyDrive/CV_Birds/features/test/ResNet50v2/model4_test_features.npy', features_model)

# + [markdown] id="XN_H94yjkHsJ"
# Predict features for the distractor and save them

# + colab={"base_uri": "https://localhost:8080/"} outputId="e34a088d-a6a3-4c35-c092-38135032b73b" id="bguKj8sefL0S" executionInfo={"status": "ok", "timestamp": 1643387643524, "user_tz": -60, "elapsed": 127677, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
features_model = a.predict(distractor_images, batch_size=BATCH_SIZE, verbose=True)

# + id="Uim09Sc1fL0U" executionInfo={"status": "ok", "timestamp": 1643387643894, "user_tz": -60, "elapsed": 373, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
np.save('/content/drive/MyDrive/CV_Birds/features/distractor/ResNet50v2/model4_distractor_features.npy', features_model)

# + [markdown] id="IZD534QIkTyh"
# ## Model 9
# Load the model from drive:

# + id="b9P9F03AkTyi" executionInfo={"status": "ok", "timestamp": 1643387651699, "user_tz": -60, "elapsed": 7806, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
MODEL_PATH = '/content/drive/MyDrive/CV_Birds/models/ResNet50v2/model9.keras'
model = ks.models.load_model(MODEL_PATH)

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643387651700, "user_tz": -60, "elapsed": 4, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="2f4f62c4-d9b4-40cc-f94d-b6c2267d1b35" id="vXoi2kGLkTyi"
model.summary()

# + id="aya5YHQbkTyj" executionInfo={"status": "ok", "timestamp": 1643387652553, "user_tz": -60, "elapsed": 856, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
a = ks.models.Sequential(model.layers[:2])

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643387652553, "user_tz": -60, "elapsed": 6, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="3339899a-5930-412f-8002-1381fd5c58a9" id="Uizj-43BkTyj"
a.summary()

# + [markdown] id="kFxGYYuekTyj"
# Predict features for training set and save them:

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643305295818, "user_tz": -60, "elapsed": 248162, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="6786cfb5-9ee8-4e5c-8d02-35702d07aa46" id="MebbgEGokTyj"
features_model = a.predict(training_images, batch_size=BATCH_SIZE, verbose=True)

# + id="U_WmzfeikTyj"
np.save('/content/drive/MyDrive/CV_Birds/features/training/ResNet50v2/model9_train_features.npy', features_model)

# + [markdown] id="6M4QOSJekTyk"
# Predict features for test set and save them:

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643305305919, "user_tz": -60, "elapsed": 8291, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="ea0e9139-677a-4087-db7f-516d0e19be72" id="MchSSaTgkTyk"
features_model = a.predict(test_images, batch_size=BATCH_SIZE, verbose=True)

# + id="oqEVWtVJkTyk"
np.save('/content/drive/MyDrive/CV_Birds/features/test/ResNet50v2/model9_test_features.npy', features_model)

# + [markdown] id="W31nIewtkTyk"
# Predict features for the distractor and save them

# + colab={"base_uri": "https://localhost:8080/"} outputId="be222e9a-89b4-48d1-f54c-4314ef563a92" id="woED7AU6foOh" executionInfo={"status": "ok", "timestamp": 1643387780506, "user_tz": -60, "elapsed": 127957, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
features_model = a.predict(distractor_images, batch_size=BATCH_SIZE, verbose=True)

# + id="Dbp5n2v1foOi" executionInfo={"status": "ok", "timestamp": 1643387781517, "user_tz": -60, "elapsed": 1014, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
np.save('/content/drive/MyDrive/CV_Birds/features/distractor/ResNet50v2/model9_distractor_features.npy', features_model)

# + [markdown] id="-R6qG88skgis"
# ## Model 10
# Load the model from drive:

# + id="tw7coul0kgit" executionInfo={"status": "ok", "timestamp": 1643387788869, "user_tz": -60, "elapsed": 7355, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
MODEL_PATH = '/content/drive/MyDrive/CV_Birds/models/ResNet50v2/model10.keras'
model = ks.models.load_model(MODEL_PATH)

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643305314021, "user_tz": -60, "elapsed": 21, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="63ce1a59-975c-414e-9cdc-3c98698e8b39" id="egA0Y0hqkgit"
model.summary()

# + id="VomIU6H0kgiu" executionInfo={"status": "ok", "timestamp": 1643387788870, "user_tz": -60, "elapsed": 5, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
a = ks.models.Sequential(model.layers[:2])

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643305314723, "user_tz": -60, "elapsed": 8, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="990cc266-aff4-42ed-e08e-40df7da8b0f8" id="LVkZluvIkgiu"
a.summary()

# + [markdown] id="1RXsNMEZkgiu"
# Predict features for training set and save them:

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643305560425, "user_tz": -60, "elapsed": 245707, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="606ac191-0eda-4d5a-d0d5-fc57b85a8255" id="bm3USwJQkgiu"
features_model = a.predict(training_images, batch_size=BATCH_SIZE, verbose=True)

# + id="xhv_UXkQkgiv"
np.save('/content/drive/MyDrive/CV_Birds/features/training/ResNet50v2/model10_train_features.npy', features_model)

# + [markdown] id="Orvcp0dPkgiv"
# Predict features for test set and save them:

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643305570565, "user_tz": -60, "elapsed": 8588, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="bf0a534b-04ba-41a9-9853-50f216fb1d58" id="-pWLyDwukgiv"
features_model = a.predict(test_images, batch_size=BATCH_SIZE, verbose=True)

# + id="JTJriH8kkgiv"
np.save('/content/drive/MyDrive/CV_Birds/features/test/ResNet50v2/model10_test_features.npy', features_model)

# + [markdown] id="dJnE5Z0ekgiw"
# Predict features for the distractor and save them

# + colab={"base_uri": "https://localhost:8080/"} outputId="f72f0904-4b3a-49e1-8eef-7e065155e7be" id="yfGEBdtXf4Wq" executionInfo={"status": "ok", "timestamp": 1643387918136, "user_tz": -60, "elapsed": 129270, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
features_model = a.predict(distractor_images, batch_size=BATCH_SIZE, verbose=True)

# + id="Bd5hR8UZf4Wr" executionInfo={"status": "ok", "timestamp": 1643387918868, "user_tz": -60, "elapsed": 736, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
np.save('/content/drive/MyDrive/CV_Birds/features/distractor/ResNet50v2/model10_distractor_features.npy', features_model)

# + [markdown] id="BHlL9DVvkpZ4"
# ## Model 11
# Load the model from drive:

# + id="3Ss1tqS4kpZ5" executionInfo={"status": "ok", "timestamp": 1643387926154, "user_tz": -60, "elapsed": 7289, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
MODEL_PATH = '/content/drive/MyDrive/CV_Birds/models/ResNet50v2/model11.keras'
model = ks.models.load_model(MODEL_PATH)

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643305579100, "user_tz": -60, "elapsed": 6, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="e1bd284b-1c61-432d-be73-e2b56568ba06" id="4TmUZgQYkpZ6"
model.summary()

# + id="ALlqnWZJkpZ6" executionInfo={"status": "ok", "timestamp": 1643387926521, "user_tz": -60, "elapsed": 370, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
a = ks.models.Sequential(model.layers[:2])

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643305579491, "user_tz": -60, "elapsed": 7, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="9f6fa1fd-50f7-48e7-f3b3-f5e2521502de" id="1HRnuXV0kpZ6"
a.summary()

# + [markdown] id="sHpQhN9dkpZ7"
# Predict features for training set and save them:

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643305825515, "user_tz": -60, "elapsed": 246028, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="13f8b5f5-6260-4a4c-eaee-5e9497a302c2" id="IPvZ5ciCkpZ7"
features_model = a.predict(training_images, batch_size=BATCH_SIZE, verbose=True)

# + id="f_xI_BWykpZ7"
np.save('/content/drive/MyDrive/CV_Birds/features/training/ResNet50v2/model11_train_features.npy', features_model)

# + [markdown] id="16f6JEpqkpZ7"
# Predict features for test set and save them:

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643305835501, "user_tz": -60, "elapsed": 8392, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="020b629d-81f9-4c43-d5ea-bd20fb68936a" id="tRaPaq1ikpZ8"
features_model = a.predict(test_images, batch_size=BATCH_SIZE, verbose=True)

# + id="tougrDHKkpZ8"
np.save('/content/drive/MyDrive/CV_Birds/features/test/ResNet50v2/model11_test_features.npy', features_model)

# + [markdown] id="7FKZdx1FkpZ8"
# Predict features for the distractor and save them

# + colab={"base_uri": "https://localhost:8080/"} outputId="96e35884-4614-4f46-a91d-8f78e009a18c" id="YB70rEmGf8cC" executionInfo={"status": "ok", "timestamp": 1643388055185, "user_tz": -60, "elapsed": 128667, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
features_model = a.predict(distractor_images, batch_size=BATCH_SIZE, verbose=True)

# + id="fwhBTLT2f8cD" executionInfo={"status": "ok", "timestamp": 1643388055961, "user_tz": -60, "elapsed": 783, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
np.save('/content/drive/MyDrive/CV_Birds/features/distractor/ResNet50v2/model11_distractor_features.npy', features_model)

# + [markdown] id="GLDCdFZ5UCJQ"
# # Resnet 101

# + [markdown] id="7e5tCU-qUXEc"
# ## Model 1
# Load the model from drive:

# + id="JsrnC978UXEd" executionInfo={"status": "ok", "timestamp": 1643388066903, "user_tz": -60, "elapsed": 10945, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
MODEL_PATH = '/content/drive/MyDrive/CV_Birds/models/ResNet101v2/resNet101_model1.keras'
model = ks.models.load_model(MODEL_PATH)

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643368559828, "user_tz": -60, "elapsed": 7, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="939dd36f-bee0-4c5c-b906-8999a39aa066" id="L87SVO8bUXEd"
model.summary()

# + id="6X34v9pPUXEe" executionInfo={"status": "ok", "timestamp": 1643388067649, "user_tz": -60, "elapsed": 750, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
a = ks.models.Sequential(model.layers[:2])

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643368561303, "user_tz": -60, "elapsed": 5, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="4df3f7de-50b0-4342-e3e3-64c2ef9f35e4" id="B14UolWnUXEe"
a.summary()

# + [markdown] id="fZTXTVFuUXEe"
# Predict features for training set and save them:

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643369041231, "user_tz": -60, "elapsed": 458343, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="64a51aa4-7459-454a-8816-da9edba64655" id="TjQMJf5jUXEf"
features_model = a.predict(training_images, batch_size=BATCH_SIZE, verbose=True)

# + id="UKxAt2ZEUXEf"
np.save('/content/drive/MyDrive/CV_Birds/features/training/ResNet101v2/model1_train_features.npy', features_model)

# + [markdown] id="IuGW33ICUXEf"
# Predict features for test set and save them:

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643369061694, "user_tz": -60, "elapsed": 18021, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="0bd41b24-a19a-4df8-9a5d-2cca601c4a84" id="DXaDXIXhUXEf"
features_model = a.predict(test_images, batch_size=BATCH_SIZE, verbose=True)

# + id="maNeOVd8UXEf"
np.save('/content/drive/MyDrive/CV_Birds/features/test/ResNet101v2/model1_test_features.npy', features_model)

# + [markdown] id="nweMTfZWUXEg"
# Predict features for the distractor and save them

# + colab={"base_uri": "https://localhost:8080/"} outputId="173939c3-9812-424b-abe0-7462aa4ab84f" id="HJAUbCA0gCN3" executionInfo={"status": "ok", "timestamp": 1643388331941, "user_tz": -60, "elapsed": 264296, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
features_model = a.predict(distractor_images, batch_size=BATCH_SIZE, verbose=True)

# + id="yI0G0aP0gCN4" executionInfo={"status": "ok", "timestamp": 1643388332925, "user_tz": -60, "elapsed": 986, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
np.save('/content/drive/MyDrive/CV_Birds/features/distractor/ResNet101v2/model1_distractor_features.npy', features_model)

# + [markdown] id="JT3Q7bRgUXEg"
# ## Model 2
# Load the model from drive:

# + id="kJFdOm02UXEg" executionInfo={"status": "ok", "timestamp": 1643388344892, "user_tz": -60, "elapsed": 11969, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
MODEL_PATH = '/content/drive/MyDrive/CV_Birds/models/ResNet101v2/resNet101_model2.keras'
model = ks.models.load_model(MODEL_PATH)

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643369073876, "user_tz": -60, "elapsed": 6, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="2ce6dd36-347c-4a0a-9e20-38d66875d559" id="hqEX_jkcUXEg"
model.summary()

# + id="Iym7XZLMUXEh" executionInfo={"status": "ok", "timestamp": 1643388346398, "user_tz": -60, "elapsed": 1520, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
a = ks.models.Sequential(model.layers[:2])

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643369075625, "user_tz": -60, "elapsed": 5, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="1dffad9c-f3a3-45b3-de7c-5b28f64d3ec3" id="1hscnCvrUXEh"
a.summary()

# + [markdown] id="HogIFJyyUXEh"
# Predict features for training set and save them:

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643369520245, "user_tz": -60, "elapsed": 444623, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="f97ce364-4056-4c04-fad8-fc90bf520b42" id="vunLXc4RUXEh"
features_model = a.predict(training_images, batch_size=BATCH_SIZE, verbose=True)

# + id="0_wnr4ilUXEi"
np.save('/content/drive/MyDrive/CV_Birds/features/training/ResNet101v2/model2_train_features.npy', features_model)

# + [markdown] id="EW37sxsnUXEi"
# Predict features for test set and save them:

# + colab={"base_uri": "https://localhost:8080/"} executionInfo={"status": "ok", "timestamp": 1643369537349, "user_tz": -60, "elapsed": 16055, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}} outputId="fae35931-8ba0-4f2f-b308-d2212eec12a9" id="90BOzh-3UXEi"
features_model = a.predict(test_images, batch_size=BATCH_SIZE, verbose=True)

# + id="kk8OR04qUXEi"
np.save('/content/drive/MyDrive/CV_Birds/features/test/ResNet101v2/model2_test_features.npy', features_model)

# + [markdown] id="rIe-HDhgUXEi"
# Predict features for the distractor and save them

# + colab={"base_uri": "https://localhost:8080/"} outputId="f2712260-edae-4125-fd94-438d2b776546" id="lVBVTD6_gHUz" executionInfo={"status": "ok", "timestamp": 1643388570004, "user_tz": -60, "elapsed": 223612, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
features_model = a.predict(distractor_images, batch_size=BATCH_SIZE, verbose=True)

# + id="tqJjYg-ugHU0" executionInfo={"status": "ok", "timestamp": 1643388570742, "user_tz": -60, "elapsed": 743, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "11023693490829624613"}}
np.save('/content/drive/MyDrive/CV_Birds/features/distractor/ResNet101v2/model2_distractor_features.npy', features_model)

StarcoderdataJupyterScriptsDedupFiltered