{"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":"# This notebook is for beginners like me, any suggestions are welcome. This notebook will be updated as and when I learn new things","metadata":{"execution":{"iopub.status.busy":"2022-07-22T12:25:45.139791Z","iopub.execute_input":"2022-07-22T12:25:45.140216Z","iopub.status.idle":"2022-07-22T12:25:45.145191Z","shell.execute_reply.started":"2022-07-22T12:25:45.140184Z","shell.execute_reply":"2022-07-22T12:25:45.143870Z"}}},{"cell_type":"markdown","source":"# Image embedding:\n* \tImage embedding is a process of transforming image into a vector. This vector will have all major information required to classify the image.\n* \tThis vector can be used to retrieve similar images in the dataset.\n* \tGenerally this process involves training large CNN architecture to classify images in the dataset. The penultimate layer of the trained architecture represents the input image and its output vector is called embedding of the image.\n","metadata":{}},{"cell_type":"markdown","source":"# Libraries","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport pandas as pd\nimport numpy as np\nimport os\nimport matplotlib.pyplot as plt\nimport matplotlib.pylab as pylab\nfrom tensorflow.data import AUTOTUNE\nfrom sklearn.neighbors import NearestNeighbors\nimport random","metadata":{"execution":{"iopub.status.busy":"2022-07-22T11:59:51.014751Z","iopub.execute_input":"2022-07-22T11:59:51.015261Z","iopub.status.idle":"2022-07-22T11:59:57.427228Z","shell.execute_reply.started":"2022-07-22T11:59:51.015223Z","shell.execute_reply":"2022-07-22T11:59:57.425719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"params={'legend.fontsize':'x-large',\n       'figure.figsize':(16,8),\n       'axes.labelsize':'x-large',\n       'axes.titlesize':16}\n\npylab.rcParams.update(params)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T11:59:57.429318Z","iopub.execute_input":"2022-07-22T11:59:57.430583Z","iopub.status.idle":"2022-07-22T11:59:57.436062Z","shell.execute_reply.started":"2022-07-22T11:59:57.430544Z","shell.execute_reply":"2022-07-22T11:59:57.434907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Getting filenames","metadata":{}},{"cell_type":"code","source":"path=[]\nfolder ='../input/caltech256'\nfor root,dirs,files in os.walk(folder):\n    for file in files:\n        if file.endswith('.jpg'):\n            path.append(os.path.join(root,file))\nfilenames=path[0:30000] #dataset has two times 256 categories","metadata":{"execution":{"iopub.status.busy":"2022-07-22T11:59:57.437414Z","iopub.execute_input":"2022-07-22T11:59:57.437889Z","iopub.status.idle":"2022-07-22T12:00:22.815542Z","shell.execute_reply.started":"2022-07-22T11:59:57.437854Z","shell.execute_reply":"2022-07-22T12:00:22.814398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Required functions","metadata":{}},{"cell_type":"code","source":"def loadimage(filename):\n    img=tf.io.read_file(filename)\n    img=tf.image.decode_png(img,channels=3)\n    img = tf.image.resize(img, (216, 216)) / 255.0\n    label=tf.strings.split(filename, sep='/')[4]\n    #label=tf.strings.split(label, sep='.')[1]\n    #label=tf.strings.split(label[2],sep='.').numpy()[1]\n    return([img,label])\ndef plotting_img(img,label):\n    for i in range(10):\n        plt.subplot(2,5,i+1)\n        #plt.figure(figsize=(10,10))\n        plt.imshow(img[i])\n        plt.axis('off')\n        plt.title(label[i].numpy().decode('utf-8'))\n    \ndef data_prep(filenames,plotting=False):\n    dataset=tf.data.Dataset.from_tensor_slices(filenames)\n    if plotting:\n        dataset=(dataset\n                 .shuffle(1032)\n                 .map(loadimage, num_parallel_calls=AUTOTUNE)\n                 .batch(32)\n                 .prefetch(AUTOTUNE))\n    else:\n        dataset=(dataset\n            .map(loadimage, num_parallel_calls=AUTOTUNE)\n            .batch(8)\n            .prefetch(AUTOTUNE))\n    return dataset      \n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-22T12:00:22.817766Z","iopub.execute_input":"2022-07-22T12:00:22.818490Z","iopub.status.idle":"2022-07-22T12:00:22.831550Z","shell.execute_reply.started":"2022-07-22T12:00:22.818453Z","shell.execute_reply":"2022-07-22T12:00:22.830701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualization","metadata":{}},{"cell_type":"code","source":"dataset=data_prep(filenames,plotting=True)\nfor img, label in dataset:\n    plotting_img(img,label)\n    break\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-22T12:00:22.832699Z","iopub.execute_input":"2022-07-22T12:00:22.833471Z","iopub.status.idle":"2022-07-22T12:00:24.460644Z","shell.execute_reply.started":"2022-07-22T12:00:22.833437Z","shell.execute_reply":"2022-07-22T12:00:24.459732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modelling","metadata":{}},{"cell_type":"markdown","source":"**I have used mobilenetv2 architecture as base model to extract image embeddings, you are free to use any architecture**","metadata":{}},{"cell_type":"code","source":"\nIMG_SHAPE = (216, 216,3)\nbase_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,\n                                               include_top=False,\n                                               weights='imagenet')\nglobal_average_layer = tf.keras.layers.GlobalAveragePooling2D()\nmodel = tf.keras.Sequential([\n  base_model,\n  global_average_layer,\n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T12:00:24.461870Z","iopub.execute_input":"2022-07-22T12:00:24.462393Z","iopub.status.idle":"2022-07-22T12:00:27.608432Z","shell.execute_reply.started":"2022-07-22T12:00:24.462362Z","shell.execute_reply":"2022-07-22T12:00:27.607012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# embedding","metadata":{}},{"cell_type":"code","source":"embed=[]\ny=[]\n#filenames=path[0:30592]\ndataset=data_prep(filenames)\nfor img, label in dataset:\n    embed.append(model.predict(img))\n    y.append(label)\n#result=model.predict(dataset, verbose=1)\nembed=np.vstack(embed)\ny=np.hstack(y)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T12:00:27.620809Z","iopub.execute_input":"2022-07-22T12:00:27.621767Z","iopub.status.idle":"2022-07-22T12:10:00.255072Z","shell.execute_reply.started":"2022-07-22T12:00:27.621718Z","shell.execute_reply":"2022-07-22T12:10:00.254091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# KNN","metadata":{}},{"cell_type":"markdown","source":"Nearest neighbors is a unsupervised algorithm to cluster near by vectors.","metadata":{}},{"cell_type":"code","source":"\nKNN = 10\nknn_model = NearestNeighbors(n_neighbors=KNN)\nknn_model.fit(embed)\ndistances, indices = knn_model.kneighbors(embed)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T12:10:00.408305Z","iopub.execute_input":"2022-07-22T12:10:00.409366Z","iopub.status.idle":"2022-07-22T12:11:03.446815Z","shell.execute_reply.started":"2022-07-22T12:10:00.409318Z","shell.execute_reply":"2022-07-22T12:11:03.445155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluation","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"idx=random.randint(0,len(filenames)) \ntemp=np.array(path)[indices[idx]]\ndata=tf.data.Dataset.from_tensor_slices(temp)\ndata=(data\n         .map(loadimage, num_parallel_calls=AUTOTUNE)\n         .batch(10)\n         .prefetch(AUTOTUNE))\nfor img, label in data:\n    plotting_img(img,label)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T12:32:14.533807Z","iopub.execute_input":"2022-07-22T12:32:14.534252Z","iopub.status.idle":"2022-07-22T12:32:15.907649Z","shell.execute_reply.started":"2022-07-22T12:32:14.534217Z","shell.execute_reply":"2022-07-22T12:32:15.906216Z"},"trusted":true},"execution_count":null,"outputs":[]}]}