{"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":"code","source":"import os\nimport random\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:28:44.511528Z","iopub.execute_input":"2021-09-03T05:28:44.511848Z","iopub.status.idle":"2021-09-03T05:28:44.661511Z","shell.execute_reply.started":"2021-09-03T05:28:44.511821Z","shell.execute_reply":"2021-09-03T05:28:44.660675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('../input/landmark-retrieval-2021/')","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:29:01.393944Z","iopub.execute_input":"2021-09-03T05:29:01.394269Z","iopub.status.idle":"2021-09-03T05:29:01.404779Z","shell.execute_reply.started":"2021-09-03T05:29:01.394239Z","shell.execute_reply":"2021-09-03T05:29:01.403473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/landmark-retrieval-2021/train.csv', index_col=0)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:29:32.879219Z","iopub.execute_input":"2021-09-03T05:29:32.879598Z","iopub.status.idle":"2021-09-03T05:29:34.762303Z","shell.execute_reply.started":"2021-09-03T05:29:32.879568Z","shell.execute_reply":"2021-09-03T05:29:34.761555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_full_path(name):\n    return os.path.join(\n        '../input/landmark-retrieval-2021/train/',\n        name[0],\n        name[1],\n        name[2],\n        f'{name}.jpg'\n    )","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:29:55.994310Z","iopub.execute_input":"2021-09-03T05:29:55.994680Z","iopub.status.idle":"2021-09-03T05:29:55.999484Z","shell.execute_reply.started":"2021-09-03T05:29:55.994650Z","shell.execute_reply":"2021-09-03T05:29:55.998265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef vis(_id):\n    arr = df[df['landmark_id'] == _id].index.tolist()\n\n    plt.figure(figsize=(16, 16))\n    for i, name in enumerate(arr):\n        img = cv2.imread(create_full_path(name))\n        plt.subplot(4, 3, i + 1)\n        plt.imshow(img)\n        plt.xticks([])\n        plt.yticks([])\n        if i >= 11:\n            break\n    plt.suptitle(_id)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:30:32.359839Z","iopub.execute_input":"2021-09-03T05:30:32.360153Z","iopub.status.idle":"2021-09-03T05:30:32.367536Z","shell.execute_reply.started":"2021-09-03T05:30:32.360124Z","shell.execute_reply":"2021-09-03T05:30:32.366549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vis(random.choice(df['landmark_id'].unique()))","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:30:46.889069Z","iopub.execute_input":"2021-09-03T05:30:46.889422Z","iopub.status.idle":"2021-09-03T05:30:48.312641Z","shell.execute_reply.started":"2021-09-03T05:30:46.889381Z","shell.execute_reply":"2021-09-03T05:30:48.311621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport fastai\nfrom fastai.tabular.all import *\nfrom fastai.text.all import *\nfrom fastai.vision.all import *\nfrom fastai.medical.imaging import *\nfrom fastai import *\n\n%reload_ext autoreload\n%autoreload 2\n%matplotlib inline\nimport warnings\nwarnings.simplefilter(action='ignore', category=Warning)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:31:11.139255Z","iopub.execute_input":"2021-09-03T05:31:11.139625Z","iopub.status.idle":"2021-09-03T05:31:16.082213Z","shell.execute_reply.started":"2021-09-03T05:31:11.139595Z","shell.execute_reply":"2021-09-03T05:31:16.081427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\nimg = Image.open(\"../input/landmark-retrieval-2021/train/0/0/0/00001b2ba2c69ac5.jpg\")\nimg","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:31:51.905310Z","iopub.execute_input":"2021-09-03T05:31:51.905739Z","iopub.status.idle":"2021-09-03T05:31:52.144532Z","shell.execute_reply.started":"2021-09-03T05:31:51.905697Z","shell.execute_reply":"2021-09-03T05:31:52.143389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_images(path, n_images, is_random=True, figsize=(16, 16)):\n    plt.figure(figsize=figsize)\n    w = int(n_images ** .5)\n    h = math.ceil(n_images / w)\n    \n    all_names = os.listdir(path)\n    image_names = all_names[:n_images]   \n    if is_random:\n        image_names = random.sample(all_names, n_images)\n            \n    for ind, image_name in enumerate(image_names):\n        img = cv2.imread(os.path.join(path, image_name))\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \n        plt.subplot(h, w, ind + 1)\n        plt.imshow(img)\n        plt.xticks([])\n        plt.yticks([])\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:32:21.109134Z","iopub.execute_input":"2021-09-03T05:32:21.109492Z","iopub.status.idle":"2021-09-03T05:32:21.184257Z","shell.execute_reply.started":"2021-09-03T05:32:21.109461Z","shell.execute_reply":"2021-09-03T05:32:21.183173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"landmarks_JPG_PATH = '../input/landmark-retrieval-2021/train/0/0/0'","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:32:41.108992Z","iopub.execute_input":"2021-09-03T05:32:41.109343Z","iopub.status.idle":"2021-09-03T05:32:41.180064Z","shell.execute_reply.started":"2021-09-03T05:32:41.109312Z","shell.execute_reply":"2021-09-03T05:32:41.179148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_images(landmarks_JPG_PATH, 9)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:32:52.913889Z","iopub.execute_input":"2021-09-03T05:32:52.914203Z","iopub.status.idle":"2021-09-03T05:32:54.114654Z","shell.execute_reply.started":"2021-09-03T05:32:52.914176Z","shell.execute_reply":"2021-09-03T05:32:54.113635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_color_histogram(path):\n    image_names = os.listdir(path)\n    image_name = random.choice(image_names)\n    img = cv2.imread(os.path.join(path, image_name))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \n    fig = make_subplots(1, 2)\n\n    fig.add_trace(go.Image(z=img), 1, 1)\n    for channel, color in enumerate(['red', 'green', 'blue']):\n        fig.add_trace(\n            go.Histogram(\n                x=img[..., channel].ravel(), \n                opacity=0.5,\n                marker_color=color, \n                name='%s channel' %color\n            ), 1, 2)\n    fig.update_layout(height=400)\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:33:14.106184Z","iopub.execute_input":"2021-09-03T05:33:14.106600Z","iopub.status.idle":"2021-09-03T05:33:14.181995Z","shell.execute_reply.started":"2021-09-03T05:33:14.106567Z","shell.execute_reply":"2021-09-03T05:33:14.180926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef color_hist_visualization(image_path, figsize=(16, 4)):\n    plt.figure(figsize=figsize)\n    \n    img = cv2.imread(image_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \n    plt.subplot(1, 4, 1)\n    plt.imshow(img)\n    plt.axis(\"off\")\n    \n    colors = [\"red\", \"green\", \"blue\"]\n    for i in range(len(colors)):\n        plt.subplot(1, 4, i + 2)\n        plt.hist(\n            img[:, :, i].reshape(-1),\n            bins=25,\n            alpha=0.5,\n            color=colors[i],\n            density=True\n        )\n        plt.xlim(0, 255)\n        plt.xticks([])\n        plt.yticks([])\n","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:33:33.218875Z","iopub.execute_input":"2021-09-03T05:33:33.219190Z","iopub.status.idle":"2021-09-03T05:33:33.293407Z","shell.execute_reply.started":"2021-09-03T05:33:33.219161Z","shell.execute_reply":"2021-09-03T05:33:33.292471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimg_path = '../input/landmark-retrieval-2021/train/0/0/0/0000fb2148d6c0e4.jpg'\ncolor_hist_visualization(img_path)\n\nimg_path = '../input/landmark-retrieval-2021/train/2/0/2/20200a7671167ac4.jpg'\ncolor_hist_visualization(img_path)\n\nimg_path = '../input/landmark-retrieval-2021/train/6/0/0/6000a31590ca910f.jpg'\ncolor_hist_visualization(img_path)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:35:22.218996Z","iopub.execute_input":"2021-09-03T05:35:22.219332Z","iopub.status.idle":"2021-09-03T05:35:23.574223Z","shell.execute_reply.started":"2021-09-03T05:35:22.219303Z","shell.execute_reply":"2021-09-03T05:35:23.573096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport plotly.graph_objs as go\nfrom plotly.subplots import make_subplots\n\nshow_color_histogram(landmarks_JPG_PATH)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:36:53.289068Z","iopub.execute_input":"2021-09-03T05:36:53.289542Z","iopub.status.idle":"2021-09-03T05:36:54.666389Z","shell.execute_reply.started":"2021-09-03T05:36:53.289503Z","shell.execute_reply":"2021-09-03T05:36:54.658115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef batch_visualization(path, n_images, is_random=True, figsize=(16, 16)):\n    plt.figure(figsize=figsize)\n    \n    w = int(n_images ** .5)\n    h = math.ceil(n_images / w)\n    \n    all_names = os.listdir(path)\n    \n    image_names = all_names[:n_images]\n    if is_random:\n        image_names = random.sample(all_names, n_images)\n    \n    for ind, image_name in enumerate(image_names):\n        img = cv2.imread(os.path.join(path, image_name))\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) \n        plt.subplot(h, w, ind + 1)\n        plt.imshow(img)\n        plt.axis(\"off\")\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:37:39.759296Z","iopub.execute_input":"2021-09-03T05:37:39.759659Z","iopub.status.idle":"2021-09-03T05:37:39.837091Z","shell.execute_reply.started":"2021-09-03T05:37:39.759630Z","shell.execute_reply":"2021-09-03T05:37:39.836220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_visualization(landmarks_JPG_PATH, 1, is_random=True, figsize=(5, 5))","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:38:03.429499Z","iopub.execute_input":"2021-09-03T05:38:03.429821Z","iopub.status.idle":"2021-09-03T05:38:03.658104Z","shell.execute_reply.started":"2021-09-03T05:38:03.429792Z","shell.execute_reply":"2021-09-03T05:38:03.657329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_visualization(landmarks_JPG_PATH, 4, is_random=True, figsize=(10, 10))","metadata":{"execution":{"iopub.status.busy":"2021-09-03T05:38:56.338923Z","iopub.execute_input":"2021-09-03T05:38:56.339243Z","iopub.status.idle":"2021-09-03T05:38:56.909258Z","shell.execute_reply.started":"2021-09-03T05:38:56.339213Z","shell.execute_reply":"2021-09-03T05:38:56.908275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}