{"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":29705,"databundleVersionId":2662435,"sourceType":"competition"}],"dockerImageVersionId":30123,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install rapidfuzz -qq","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-10-09T13:31:38.036564Z","iopub.execute_input":"2021-10-09T13:31:38.037227Z","iopub.status.idle":"2021-10-09T13:31:48.900412Z","shell.execute_reply.started":"2021-10-09T13:31:38.037136Z","shell.execute_reply":"2021-10-09T13:31:48.899404Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nsub = pd.read_csv('../input/wikipedia-image-caption/sample_submission.csv')\nsub.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-10-09T13:31:51.127174Z","iopub.execute_input":"2021-10-09T13:31:51.127468Z","iopub.status.idle":"2021-10-09T13:31:51.622134Z","shell.execute_reply.started":"2021-10-09T13:31:51.127437Z","shell.execute_reply":"2021-10-09T13:31:51.621385Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"captions = pd.read_csv('../input/wikipedia-image-caption/test_caption_list.csv')\nprint(len(captions))\ncaptions.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-09T13:32:15.098577Z","iopub.execute_input":"2021-10-09T13:32:15.098844Z","iopub.status.idle":"2021-10-09T13:32:15.463098Z","shell.execute_reply.started":"2021-10-09T13:32:15.098816Z","shell.execute_reply":"2021-10-09T13:32:15.46232Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = pd.read_csv('../input/wikipedia-image-caption/test.tsv', sep='\\t')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-09T13:32:25.379097Z","iopub.execute_input":"2021-10-09T13:32:25.379417Z","iopub.status.idle":"2021-10-09T13:32:25.59473Z","shell.execute_reply.started":"2021-10-09T13:32:25.379386Z","shell.execute_reply":"2021-10-09T13:32:25.593965Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(5):\n    print(test.image_url.loc[i])","metadata":{"execution":{"iopub.status.busy":"2021-10-09T13:32:36.237272Z","iopub.execute_input":"2021-10-09T13:32:36.237885Z","iopub.status.idle":"2021-10-09T13:32:36.245805Z","shell.execute_reply.started":"2021-10-09T13:32:36.237844Z","shell.execute_reply":"2021-10-09T13:32:36.245207Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls ../input/wikipedia-image-caption/image_data_test/image_pixels","metadata":{"execution":{"iopub.status.busy":"2021-10-09T13:36:56.324078Z","iopub.execute_input":"2021-10-09T13:36:56.3249Z","iopub.status.idle":"2021-10-09T13:36:57.097677Z","shell.execute_reply.started":"2021-10-09T13:36:56.324853Z","shell.execute_reply":"2021-10-09T13:36:57.096849Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tst0 = pd.read_csv('../input/wikipedia-image-caption/image_data_test/image_pixels/test_image_pixels_part-00000.csv', sep='\\t', header=None)\ntst0.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-09T13:37:10.585022Z","iopub.execute_input":"2021-10-09T13:37:10.585285Z","iopub.status.idle":"2021-10-09T13:37:18.007679Z","shell.execute_reply.started":"2021-10-09T13:37:10.585256Z","shell.execute_reply":"2021-10-09T13:37:18.006896Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import base64 \nfrom PIL import Image\nimport io\n\nimage_64_decode = base64.b64decode(tst0[1].loc[0])\nimg = Image.open(io.BytesIO(image_64_decode))\nimg","metadata":{"execution":{"iopub.status.busy":"2021-10-09T13:43:21.922833Z","iopub.execute_input":"2021-10-09T13:43:21.923772Z","iopub.status.idle":"2021-10-09T13:43:21.992052Z","shell.execute_reply.started":"2021-10-09T13:43:21.92372Z","shell.execute_reply":"2021-10-09T13:43:21.991243Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import datatable as dt\ntrain0 = dt.fread('../input/wikipedia-image-caption/train-00000-of-00005.tsv')\ntrain0.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-09T13:43:49.227402Z","iopub.execute_input":"2021-10-09T13:43:49.228308Z","iopub.status.idle":"2021-10-09T13:44:52.387242Z","shell.execute_reply.started":"2021-10-09T13:43:49.228259Z","shell.execute_reply":"2021-10-09T13:44:52.38658Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from urllib.parse import unquote\n\nt = test.image_url.loc[2]\n\ndef convert(t):\n    t = t.rsplit('/',1)[1]\n    t = unquote(t)\n    t = t.replace('_', ' ')\n    t = t + ' [SEP]'\n    return(t)","metadata":{"execution":{"iopub.status.busy":"2021-10-09T13:53:35.464257Z","iopub.execute_input":"2021-10-09T13:53:35.464771Z","iopub.status.idle":"2021-10-09T13:53:35.472243Z","shell.execute_reply.started":"2021-10-09T13:53:35.46473Z","shell.execute_reply":"2021-10-09T13:53:35.47135Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(5):\n    print(f'target: {train0[i,-1]}')\n    print(f'prediction: {convert(train0[i,1])}')\n    print()","metadata":{"execution":{"iopub.status.busy":"2021-10-09T13:54:42.199024Z","iopub.execute_input":"2021-10-09T13:54:42.199486Z","iopub.status.idle":"2021-10-09T13:54:42.207854Z","shell.execute_reply.started":"2021-10-09T13:54:42.199454Z","shell.execute_reply":"2021-10-09T13:54:42.206827Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nimport tarfile\nimport urllib.request as urllib2\n\nrt = urllib2.urlopen(\"https://storage.cloud.google.com/wikimedia-image-caption-public/image_data_train.tar\")\ninputs = [tarfile.open(fileobj=rt, mode='r|')]\nfor tarfileobj in inputs:        \n    tarfileobj.extractall(path='/kaggle/working', members=None)\n","metadata":{"execution":{"iopub.status.busy":"2021-10-09T14:50:42.799449Z","iopub.execute_input":"2021-10-09T14:50:42.799727Z","iopub.status.idle":"2021-10-09T14:50:43.102739Z","shell.execute_reply.started":"2021-10-09T14:50:42.7997Z","shell.execute_reply":"2021-10-09T14:50:43.101369Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tarfile       \nthetarfile = tarfile.open(target_path)\nthetarfile.extract(\"/kaggle/working\")\nthetarfile.close()","metadata":{"execution":{"iopub.status.busy":"2021-10-09T14:36:23.417065Z","iopub.execute_input":"2021-10-09T14:36:23.417748Z","iopub.status.idle":"2021-10-09T14:36:23.434519Z","shell.execute_reply.started":"2021-10-09T14:36:23.417708Z","shell.execute_reply":"2021-10-09T14:36:23.433483Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"CAPTIONS = captions.caption_title_and_reference_description.values.tolist()\nlen(CAPTIONS)","metadata":{"execution":{"iopub.status.busy":"2021-10-09T13:55:07.941794Z","iopub.execute_input":"2021-10-09T13:55:07.942099Z","iopub.status.idle":"2021-10-09T13:55:07.95396Z","shell.execute_reply.started":"2021-10-09T13:55:07.942067Z","shell.execute_reply":"2021-10-09T13:55:07.952956Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from rapidfuzz import process, fuzz","metadata":{"execution":{"iopub.status.busy":"2021-10-09T13:55:10.948216Z","iopub.execute_input":"2021-10-09T13:55:10.948505Z","iopub.status.idle":"2021-10-09T13:55:11.009606Z","shell.execute_reply.started":"2021-10-09T13:55:10.948478Z","shell.execute_reply":"2021-10-09T13:55:11.008929Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\n\nfor i in range(5):\n    s = test.prediction.loc[i]\n    print(f'image_url: {s}')\n    res = process.extract(s, CAPTIONS, scorer=fuzz.ratio, processor=None, limit=5)\n    print(f'closest captions:')\n    for c in res:\n        print(c[0])\n    print('*'*60)\n    print()  ","metadata":{"execution":{"iopub.status.busy":"2021-10-09T13:55:12.987946Z","iopub.execute_input":"2021-10-09T13:55:12.988747Z","iopub.status.idle":"2021-10-09T13:55:13.099957Z","shell.execute_reply.started":"2021-10-09T13:55:12.988701Z","shell.execute_reply":"2021-10-09T13:55:13.099197Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def find_closest_match(s):\n    res = process.extract(s, CAPTIONS, scorer=fuzz.ratio, processor=None, limit=5)\n    res = [x[0] for x in res]\n    return res","metadata":{"execution":{"iopub.status.busy":"2021-10-09T13:55:18.558136Z","iopub.execute_input":"2021-10-09T13:55:18.558628Z","iopub.status.idle":"2021-10-09T13:55:18.563741Z","shell.execute_reply.started":"2021-10-09T13:55:18.558595Z","shell.execute_reply":"2021-10-09T13:55:18.562823Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tqdm.auto import tqdm\ntqdm.pandas()","metadata":{"execution":{"iopub.status.busy":"2021-10-09T13:55:20.577979Z","iopub.execute_input":"2021-10-09T13:55:20.578262Z","iopub.status.idle":"2021-10-09T13:55:20.668146Z","shell.execute_reply.started":"2021-10-09T13:55:20.578234Z","shell.execute_reply":"2021-10-09T13:55:20.667533Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['caption_title_and_reference_description'] = test['prediction'].progress_apply(find_closest_match)","metadata":{"execution":{"iopub.status.busy":"2021-10-09T13:55:22.180728Z","iopub.execute_input":"2021-10-09T13:55:22.181172Z","iopub.status.idle":"2021-10-09T14:12:31.510741Z","shell.execute_reply.started":"2021-10-09T13:55:22.181143Z","shell.execute_reply":"2021-10-09T14:12:31.508812Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub = test[['id', 'caption_title_and_reference_description']]\nsub = sub.explode('caption_title_and_reference_description')\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-09T14:12:31.512975Z","iopub.status.idle":"2021-10-09T14:12:31.513243Z","shell.execute_reply.started":"2021-10-09T14:12:31.513098Z","shell.execute_reply":"2021-10-09T14:12:31.513112Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-09T14:12:31.514256Z","iopub.status.idle":"2021-10-09T14:12:31.514602Z","shell.execute_reply.started":"2021-10-09T14:12:31.514393Z","shell.execute_reply":"2021-10-09T14:12:31.514407Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## to be continued ...","metadata":{}},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null}]}