{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import libs","metadata":{}},{"cell_type":"code","source":"!pip install -U image_embeddings","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-13T07:18:10.163422Z","iopub.execute_input":"2022-07-13T07:18:10.164571Z","iopub.status.idle":"2022-07-13T07:18:57.023214Z","shell.execute_reply.started":"2022-07-13T07:18:10.164464Z","shell.execute_reply":"2022-07-13T07:18:57.022162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nimport os\nimport time\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import activations, backend as K\n\nfrom keras.preprocessing.sequence import pad_sequences\nfrom keras.applications.resnet import *\nfrom keras.applications.efficientnet import *\nfrom keras.applications.mobilenet import *\nfrom keras.applications.mobilenet_v2 import *\nfrom keras.applications.vgg16 import *\nfrom keras.applications.vgg19 import *\nfrom keras.applications.densenet import *\nfrom keras.models import load_model\nfrom keras.metrics import Precision, Recall\n\nfrom sklearn.metrics import classification_report, confusion_matrix\nfrom sklearn.preprocessing import OrdinalEncoder\nimport pandas as pd \nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.dates as mdates\nimport seaborn as sns\n\nfrom IPython import display\nfrom PIL import Image\nimport random\nimport json\nimport datetime","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-13T07:18:57.025644Z","iopub.execute_input":"2022-07-13T07:18:57.026015Z","iopub.status.idle":"2022-07-13T07:19:07.507324Z","shell.execute_reply.started":"2022-07-13T07:18:57.025983Z","shell.execute_reply":"2022-07-13T07:19:07.506271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create model","metadata":{}},{"cell_type":"code","source":"# Create a VGG model\nbase_model = VGG19(\n    weights='imagenet',\n    include_top=False, \n    classes = 64\n)\n\nmodel = keras.Sequential()\n\n# Input\nmodel.add(layers.Input(shape=(None, None, 3), dtype=tf.uint8))\n# VGG\nmodel.add(base_model)\n# Output\nmodel.add(layers.Embedding(10000, 64, name=\"embedding_norm\")) # add an Embedding layer\n\nmodel.compile()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T07:28:59.237268Z","iopub.execute_input":"2022-07-13T07:28:59.237696Z","iopub.status.idle":"2022-07-13T07:28:59.789943Z","shell.execute_reply.started":"2022-07-13T07:28:59.237664Z","shell.execute_reply":"2022-07-13T07:28:59.788692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save and display the model\nmodel.save('')\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T07:29:00.326007Z","iopub.execute_input":"2022-07-13T07:29:00.326414Z","iopub.status.idle":"2022-07-13T07:29:04.024899Z","shell.execute_reply.started":"2022-07-13T07:29:00.326381Z","shell.execute_reply":"2022-07-13T07:29:04.023593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save model in zip for submission","metadata":{}},{"cell_type":"code","source":"from zipfile import ZipFile\n\n# Create submit zip file\nwith ZipFile('submission.zip','w') as zip:           \n    zip.write('saved_model.pb', arcname='saved_model.pb') \n    zip.write('variables/variables.data-00000-of-00001', arcname='variables/variables.data-00000-of-00001') \n    zip.write('variables/variables.index', arcname='variables/variables.index') ","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:26:15.624414Z","iopub.execute_input":"2022-07-12T13:26:15.624853Z","iopub.status.idle":"2022-07-12T13:26:15.914989Z","shell.execute_reply.started":"2022-07-12T13:26:15.624803Z","shell.execute_reply":"2022-07-12T13:26:15.914049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls -al","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:28:24.325115Z","iopub.execute_input":"2022-07-12T13:28:24.325555Z","iopub.status.idle":"2022-07-12T13:28:25.152335Z","shell.execute_reply.started":"2022-07-12T13:28:24.325522Z","shell.execute_reply":"2022-07-12T13:28:25.151079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check if the model for submission","metadata":{}},{"cell_type":"markdown","source":"## Download Image Embedding dataset","metadata":{}},{"cell_type":"code","source":"import image_embeddings\nfrom pathlib import Path\n\n# Make path to download the dataset\n\nhome = '/root/'\nos.makedirs(home, exist_ok=True)\n\ndataset = \"tf_flowers\"\nos.makedirs(os.path.join(home, dataset), exist_ok=True)\n\npath_images = f\"{home}/{dataset}/images\"\nos.makedirs(path_images, exist_ok=True)\n\npath_tfrecords = f\"{home}/{dataset}/tfrecords\"\nos.makedirs(path_tfrecords, exist_ok=True)\n\npath_embeddings = f\"{home}/{dataset}/embeddings\"\nos.makedirs(path_embeddings, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:26:39.862468Z","iopub.execute_input":"2022-07-12T13:26:39.862936Z","iopub.status.idle":"2022-07-12T13:26:41.326804Z","shell.execute_reply.started":"2022-07-12T13:26:39.862899Z","shell.execute_reply":"2022-07-12T13:26:41.325514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Download and compute inference on everything\nimage_embeddings.downloader.save_examples_to_folder(output_folder=path_images, images_count=1000, dataset=dataset)\nimage_embeddings.inference.write_tfrecord(image_folder=path_images, output_folder=path_tfrecords, num_shards=10)\nimage_embeddings.inference.run_inference(tfrecords_folder=path_tfrecords, output_folder=path_embeddings, batch_size=1000)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-12T13:26:53.925595Z","iopub.execute_input":"2022-07-12T13:26:53.926094Z","iopub.status.idle":"2022-07-12T13:28:08.932059Z","shell.execute_reply.started":"2022-07-12T13:26:53.926057Z","shell.execute_reply":"2022-07-12T13:28:08.929957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"[id_to_name, name_to_id, embeddings] = image_embeddings.knn.read_embeddings(path_embeddings)\nindex = image_embeddings.knn.build_index(embeddings)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:28:08.934874Z","iopub.execute_input":"2022-07-12T13:28:08.935302Z","iopub.status.idle":"2022-07-12T13:28:09.113778Z","shell.execute_reply.started":"2022-07-12T13:28:08.935267Z","shell.execute_reply":"2022-07-12T13:28:09.112664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Try to open flower picture\nimagePath = f\"{path_images}/image_roses_122.jpeg\"\nImage.open(imagePath)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:18:01.958561Z","iopub.execute_input":"2022-07-12T13:18:01.959077Z","iopub.status.idle":"2022-07-12T13:18:01.992176Z","shell.execute_reply.started":"2022-07-12T13:18:01.959038Z","shell.execute_reply":"2022-07-12T13:18:01.990732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model testing","metadata":{}},{"cell_type":"code","source":"# Model loading\nmodel_test = tf.saved_model.load('')\nembedding_fn = model_test.signatures[\"serving_default\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:16:20.499504Z","iopub.execute_input":"2022-07-12T13:16:20.500098Z","iopub.status.idle":"2022-07-12T13:16:21.857468Z","shell.execute_reply.started":"2022-07-12T13:16:20.500046Z","shell.execute_reply":"2022-07-12T13:16:21.856277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load image and extract its embedding\nimage_tensor = tf.convert_to_tensor(\n    np.array(Image.open(imagePath).convert(\"RGB\"))\n)\n\nexpanded_tensor = tf.expand_dims(image_tensor, axis=0)\nembedding = embedding_fn(expanded_tensor)[\"embedding_norm\"]\nprint(f'Embedding shape : {embedding.shape}')","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:18:09.115225Z","iopub.execute_input":"2022-07-12T13:18:09.115718Z","iopub.status.idle":"2022-07-12T13:18:09.522396Z","shell.execute_reply.started":"2022-07-12T13:18:09.115679Z","shell.execute_reply":"2022-07-12T13:18:09.521387Z"},"trusted":true},"execution_count":null,"outputs":[]}]}