{"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 provides the possibility to sample and view batches of images by re-running the same cell multiple times","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport pathlib\nfrom PIL import Image\nimport IPython.display as display\nimport tensorflow as tf\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-04T15:41:40.384581Z","iopub.execute_input":"2022-04-04T15:41:40.384977Z","iopub.status.idle":"2022-04-04T15:41:40.390612Z","shell.execute_reply.started":"2022-04-04T15:41:40.384932Z","shell.execute_reply":"2022-04-04T15:41:40.389173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train_dir = pathlib.Path('../input/landmark-retrieval-2020/train')\ntrain_df = pd.read_csv('../input/landmark-retrieval-2020/train.csv')\n\n# Add extra column with respective path (upto the train data folder)\ntrain_df['id_path']=train_df['id'].map(lambda x: '/'.join(list(x[:3])) + f'/{x}.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-04-04T15:41:40.396089Z","iopub.execute_input":"2022-04-04T15:41:40.396443Z","iopub.status.idle":"2022-04-04T15:41:43.824503Z","shell.execute_reply.started":"2022-04-04T15:41:40.396408Z","shell.execute_reply":"2022-04-04T15:41:43.823245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show the slice of lables\ntrain_df['landmark_id'].unique()[2060:2070]","metadata":{"execution":{"iopub.status.busy":"2022-04-04T15:41:43.826857Z","iopub.execute_input":"2022-04-04T15:41:43.827277Z","iopub.status.idle":"2022-04-04T15:41:43.854726Z","shell.execute_reply.started":"2022-04-04T15:41:43.827242Z","shell.execute_reply":"2022-04-04T15:41:43.853647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Select images with the label 5139 (landmark_id)\nimg_group = train_df[train_df['landmark_id'] == 5139]['id'].values\nimg_group[:5]","metadata":{"execution":{"iopub.status.busy":"2022-04-04T15:41:43.856322Z","iopub.execute_input":"2022-04-04T15:41:43.856643Z","iopub.status.idle":"2022-04-04T15:41:44.030148Z","shell.execute_reply.started":"2022-04-04T15:41:43.856609Z","shell.execute_reply":"2022-04-04T15:41:44.029029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def img_path(img_id, data_dir):\n    \"\"\"Returns the path for a img_id.\"\"\"\n    return data_dir/pathlib.Path('/'.join(list(img_id[:3])) + f'/{img_id}.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-04-04T15:41:44.031501Z","iopub.execute_input":"2022-04-04T15:41:44.031815Z","iopub.status.idle":"2022-04-04T15:41:44.038200Z","shell.execute_reply.started":"2022-04-04T15:41:44.031783Z","shell.execute_reply":"2022-04-04T15:41:44.036816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Given the path, display up to 5 images (or change the slicing for more) in original size\nfor image_path in [img_path(i, data_train_dir) for i in img_group][:5]:\n    display.display(Image.open(str(image_path)))","metadata":{"execution":{"iopub.status.busy":"2022-04-04T15:41:44.041878Z","iopub.execute_input":"2022-04-04T15:41:44.042517Z","iopub.status.idle":"2022-04-04T15:41:45.049238Z","shell.execute_reply.started":"2022-04-04T15:41:44.042398Z","shell.execute_reply":"2022-04-04T15:41:45.048438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# top 10 landmark_id\ntrain_df[['landmark_id', 'id']].groupby('landmark_id').count().sort_values(by='id', ascending=False).head(10)","metadata":{"execution":{"iopub.status.busy":"2022-04-04T15:41:45.050515Z","iopub.execute_input":"2022-04-04T15:41:45.050976Z","iopub.status.idle":"2022-04-04T15:41:45.381977Z","shell.execute_reply.started":"2022-04-04T15:41:45.050926Z","shell.execute_reply":"2022-04-04T15:41:45.380820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Resizing/rescaling images\nimage_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2022-04-04T15:41:45.383386Z","iopub.execute_input":"2022-04-04T15:41:45.383823Z","iopub.status.idle":"2022-04-04T15:41:45.388816Z","shell.execute_reply.started":"2022-04-04T15:41:45.383786Z","shell.execute_reply":"2022-04-04T15:41:45.387908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 25\nIMG_HEIGHT = 224\nIMG_WIDTH = 224","metadata":{"execution":{"iopub.status.busy":"2022-04-04T15:41:45.390011Z","iopub.execute_input":"2022-04-04T15:41:45.390457Z","iopub.status.idle":"2022-04-04T15:41:45.399715Z","shell.execute_reply.started":"2022-04-04T15:41:45.390406Z","shell.execute_reply":"2022-04-04T15:41:45.398913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sample n pictures\nn = 1000\n\ntrain_data_gen = image_generator.flow_from_dataframe(\n    directory=data_train_dir,\n    dataframe=train_df.sample(n=n),\n    class_mode='raw',\n    x_col='id_path', y_col='landmark_id',\n    batch_size=BATCH_SIZE,\n    shuffle=True,\n    target_size=(IMG_HEIGHT, IMG_WIDTH),\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-04T15:41:45.401219Z","iopub.execute_input":"2022-04-04T15:41:45.401688Z","iopub.status.idle":"2022-04-04T15:41:48.760029Z","shell.execute_reply.started":"2022-04-04T15:41:45.401643Z","shell.execute_reply":"2022-04-04T15:41:48.758153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_batch(image_batch, label_batch):\n  plt.figure(figsize=(20,20))\n  for n in range(len(image_batch)):\n      ax = plt.subplot(5,5,n+1)\n      plt.imshow(image_batch[n])\n      plt.title(label_batch[n])\n      plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-04-04T15:41:48.761310Z","iopub.execute_input":"2022-04-04T15:41:48.761608Z","iopub.status.idle":"2022-04-04T15:41:48.769320Z","shell.execute_reply.started":"2022-04-04T15:41:48.761564Z","shell.execute_reply":"2022-04-04T15:41:48.767633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Now you can re-run this cell multiple times to get next batch of images\n# Since we have n=1000 and BATCH_SIZE = 25, you can run the cell 40 times\n# After that, it will start over again.\n\nimage_batch, label_batch = next(train_data_gen)\nshow_batch(image_batch, label_batch)","metadata":{"execution":{"iopub.status.busy":"2022-04-04T15:41:48.771197Z","iopub.execute_input":"2022-04-04T15:41:48.771936Z","iopub.status.idle":"2022-04-04T15:41:51.252051Z","shell.execute_reply.started":"2022-04-04T15:41:48.771859Z","shell.execute_reply":"2022-04-04T15:41:51.250204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sample pictures with given landmark_id\nlandmark_id = 4239\n\ntrain_data_gen_label = image_generator.flow_from_dataframe(\n    directory=data_train_dir,\n    dataframe=train_df[train_df['landmark_id'] == landmark_id],\n    class_mode='raw',\n    x_col='id_path', y_col='landmark_id',\n    batch_size=BATCH_SIZE,\n    shuffle=True,\n    target_size=(IMG_HEIGHT, IMG_WIDTH),\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-04T15:41:51.255116Z","iopub.execute_input":"2022-04-04T15:41:51.255542Z","iopub.status.idle":"2022-04-04T15:41:51.492114Z","shell.execute_reply.started":"2022-04-04T15:41:51.255472Z","shell.execute_reply":"2022-04-04T15:41:51.491035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# You can re-run this cell multiple times to get next batch of images\nimage_batch, label_batch = next(train_data_gen_label)\nshow_batch(image_batch, label_batch)","metadata":{"execution":{"iopub.status.busy":"2022-04-04T15:41:51.493824Z","iopub.execute_input":"2022-04-04T15:41:51.494366Z","iopub.status.idle":"2022-04-04T15:41:53.928189Z","shell.execute_reply.started":"2022-04-04T15:41:51.494316Z","shell.execute_reply":"2022-04-04T15:41:53.927327Z"},"trusted":true},"execution_count":null,"outputs":[]}]}