{"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":"# Understand the competition domains and ImageNet-21k label\n\n\nI'm guessing that the proportion of images in the competition's target domain comes from statistics from google image search data. At the same time, through the public kernel, it is found that the imagenet 21k pre-training model is far better than the imagenet 1k model. Let's analyze the relationship between competition goals and imagenet 21k.\n\n\n## where to find different pretrained weight?\n\ntimm maybe the best choice,  you can goto timm github website,  check the model config py file,  there will list all pretrained details. [Here is an example of ConvNext](https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/convnext.py),  the tiny/small/base/large represente the size of model,  in22ft1k means pretrain on imagetnet 22k and fintune in 1K,  in22k means only train on imagenet 22k:\n\n```python\n\nconvnext_tiny_in22ft1k=_cfg(\n        url='https://dl.fbaipublicfiles.com/convnext/convnext_tiny_22k_1k_224.pth'),\n    convnext_small_in22ft1k=_cfg(\n        url='https://dl.fbaipublicfiles.com/convnext/convnext_small_22k_1k_224.pth'),\n\n    convnext_tiny_384_in22ft1k=_cfg(\n        url='https://dl.fbaipublicfiles.com/convnext/convnext_tiny_22k_1k_384.pth',\n        input_size=(3, 384, 384), pool_size=(12, 12), crop_pct=1.0),\n    convnext_small_384_in22ft1k=_cfg(\n        url='https://dl.fbaipublicfiles.com/convnext/convnext_small_22k_1k_384.pth',\n        input_size=(3, 384, 384), pool_size=(12, 12), crop_pct=1.0),\n    convnext_base_384_in22ft1k=_cfg(\n        url='https://dl.fbaipublicfiles.com/convnext/convnext_base_22k_1k_384.pth',\n        input_size=(3, 384, 384), pool_size=(12, 12), crop_pct=1.0),\n\n    convnext_tiny_in22k=_cfg(\n        url=\"https://dl.fbaipublicfiles.com/convnext/convnext_tiny_22k_224.pth\", num_classes=21841),\n    convnext_small_in22k=_cfg(\n        url=\"https://dl.fbaipublicfiles.com/convnext/convnext_small_22k_224.pth\", num_classes=21841),\n    convnext_base_in22k=_cfg(\n        url=\"https://dl.fbaipublicfiles.com/convnext/convnext_base_22k_224.pth\", num_classes=21841),\n    convnext_large_in22k=_cfg(\n        url=\"https://dl.fbaipublicfiles.com/convnext/convnext_large_22k_224.pth\", num_classes=21841),\n    convnext_xlarge_in22k=_cfg(\n        url=\"https://dl.fbaipublicfiles.com/convnext/convnext_xlarge_22k_224.pth\", num_classes=21841),\n```","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-23T14:07:18.207726Z","iopub.execute_input":"2022-07-23T14:07:18.208659Z","iopub.status.idle":"2022-07-23T14:07:34.030034Z","shell.execute_reply.started":"2022-07-23T14:07:18.208528Z","shell.execute_reply":"2022-07-23T14:07:34.028868Z"}}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:30:23.374674Z","iopub.execute_input":"2022-07-23T14:30:23.375074Z","iopub.status.idle":"2022-07-23T14:30:23.382095Z","shell.execute_reply.started":"2022-07-23T14:30:23.375040Z","shell.execute_reply":"2022-07-23T14:30:23.380549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -U sentence-transformers","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sentence_transformers import SentenceTransformer\nmodel = SentenceTransformer('all-MiniLM-L6-v2')","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:15:42.451219Z","iopub.execute_input":"2022-07-23T14:15:42.451655Z","iopub.status.idle":"2022-07-23T14:15:42.759534Z","shell.execute_reply.started":"2022-07-23T14:15:42.451620Z","shell.execute_reply":"2022-07-23T14:15:42.758537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_domin = ['apparel & accessories', 'packaged goods', 'furniture & home decor',\n               'toys', 'landmarks', 'storefronts', 'dishes', 'artwork', 'memes, illustrations, cars']\n\ntarget_emb = model.encode(target_domin)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:15:43.825569Z","iopub.execute_input":"2022-07-23T14:15:43.826353Z","iopub.status.idle":"2022-07-23T14:15:43.905625Z","shell.execute_reply.started":"2022-07-23T14:15:43.826308Z","shell.execute_reply":"2022-07-23T14:15:43.904532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(\"../input/imagenet-21k-label/imagenet21k_wordnet_lemmas.txt\") as f:\n    imagenet21k_labels = [i.strip(\"\\n\") for i in f.readlines()]","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:17:10.840385Z","iopub.execute_input":"2022-07-23T14:17:10.840827Z","iopub.status.idle":"2022-07-23T14:17:10.855837Z","shell.execute_reply.started":"2022-07-23T14:17:10.840793Z","shell.execute_reply":"2022-07-23T14:17:10.854805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(imagenet21k_labels)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:17:13.217560Z","iopub.execute_input":"2022-07-23T14:17:13.218627Z","iopub.status.idle":"2022-07-23T14:17:13.224375Z","shell.execute_reply.started":"2022-07-23T14:17:13.218583Z","shell.execute_reply":"2022-07-23T14:17:13.223586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imagenet21k_emb = model.encode(imagenet21k_labels)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:17:26.400421Z","iopub.execute_input":"2022-07-23T14:17:26.401619Z","iopub.status.idle":"2022-07-23T14:18:39.508550Z","shell.execute_reply.started":"2022-07-23T14:17:26.401578Z","shell.execute_reply":"2022-07-23T14:18:39.507353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn.functional as F\n\ntarget_emb = F.normalize(torch.tensor(target_emb).float(), 1)\nimagenet21k_emb = F.normalize(torch.tensor(imagenet21k_emb).float(), 1)\n\nsim_mat = target_emb @ imagenet21k_emb.T","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:18:46.889756Z","iopub.execute_input":"2022-07-23T14:18:46.890875Z","iopub.status.idle":"2022-07-23T14:18:46.941673Z","shell.execute_reply.started":"2022-07-23T14:18:46.890831Z","shell.execute_reply":"2022-07-23T14:18:46.940813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sim_mat.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:18:51.613841Z","iopub.execute_input":"2022-07-23T14:18:51.614234Z","iopub.status.idle":"2022-07-23T14:18:51.622693Z","shell.execute_reply.started":"2022-07-23T14:18:51.614200Z","shell.execute_reply":"2022-07-23T14:18:51.621873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Over All","metadata":{}},{"cell_type":"code","source":"for j in range(len(target_domin)):\n\n    print(\"=\" * 20)\n    print(f\"For {target_domin[j]} domin\")\n    print(\"=\" * 20)\n    sim, top10_sim_21k_labels = torch.topk(sim_mat[j], 10)\n    for i, idx in enumerate(top10_sim_21k_labels):\n        print(f\"top {i} sim {sim[i]:.4f},  {imagenet21k_labels[idx]}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:24:53.950904Z","iopub.execute_input":"2022-07-23T14:24:53.951987Z","iopub.status.idle":"2022-07-23T14:24:53.961354Z","shell.execute_reply.started":"2022-07-23T14:24:53.951934Z","shell.execute_reply":"2022-07-23T14:24:53.960528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Try to calculate the domain proportion of imagenet-21k\n\nset threshold with 0.001 for beat the domain.","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:21:35.561858Z","iopub.execute_input":"2022-07-23T14:21:35.562240Z","iopub.status.idle":"2022-07-23T14:21:35.569469Z","shell.execute_reply.started":"2022-07-23T14:21:35.562201Z","shell.execute_reply":"2022-07-23T14:21:35.568424Z"}}},{"cell_type":"code","source":"pp = []\n\nfor j in range(len(target_domin)):\n    proportion = (torch.sum(sim_mat[j] > 0.001) / sim_mat.shape[1]).item()\n    print(f\"{target_domin[j]} domin : {proportion*100:.1f}%\")\n    pp.append(proportion)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:29:42.808236Z","iopub.execute_input":"2022-07-23T14:29:42.808674Z","iopub.status.idle":"2022-07-23T14:29:42.817665Z","shell.execute_reply.started":"2022-07-23T14:29:42.808637Z","shell.execute_reply":"2022-07-23T14:29:42.816519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 5))\npd.Series(pp, index=target_domin).sort_values(ascending=False).plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:31:09.269591Z","iopub.execute_input":"2022-07-23T14:31:09.269953Z","iopub.status.idle":"2022-07-23T14:31:09.491280Z","shell.execute_reply.started":"2022-07-23T14:31:09.269925Z","shell.execute_reply":"2022-07-23T14:31:09.489868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Emmm, the proportion is not match competition setting, so maybe can try something on it.","metadata":{}}]}