{"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":"### Add your notebook output and change model path to check before submit\n#### All credits to [Pytorch - sample gpu submittion](https://www.kaggle.com/code/massy103/pytorch-sample-gpu-submittion) by [massy](https://www.kaggle.com/massy103)","metadata":{}},{"cell_type":"code","source":"model_path = '../input/pytorchuecinceptionv3baseline/saved_model.pt'","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:52:34.402348Z","iopub.execute_input":"2022-08-07T15:52:34.402742Z","iopub.status.idle":"2022-08-07T15:52:34.407855Z","shell.execute_reply.started":"2022-08-07T15:52:34.402706Z","shell.execute_reply":"2022-08-07T15:52:34.406788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip3 install faiss-gpu\n! pip3 install nvidia-ml-py3 \nfrom IPython.core.display import display, HTML\nimport nvidia_smi\n\ndef time_format(sec):\n    sec = sec % (24 * 3600)\n    hour = sec // 3600\n    sec %= 3600\n    min = sec // 60\n    sec %= 60\n    time_str = \"%02d:%02d:%02d\" % (hour, min, sec)\n    return hour, min, sec, time_str\n\ndef print_estimate(sec):\n    total_secs = sec\n    hour, min, sec, time_str = time_format(sec)\n    if total_secs > (8*3600+45*60):\n        display(HTML(f\"<h3 style='color:red'>Approx Timeout - {time_str}</h3>\"))\n    else:\n        display(HTML(f\"<h3 style='color:green'>Safe to Submit - {time_str}</h3>\"))\nnvidia_smi.nvmlInit()","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-08-07T15:53:04.953298Z","iopub.execute_input":"2022-08-07T15:53:04.953793Z","iopub.status.idle":"2022-08-07T15:53:23.700682Z","shell.execute_reply.started":"2022-08-07T15:53:04.953744Z","shell.execute_reply":"2022-08-07T15:53:23.699362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nquery_size = 5_000\nindex_size = 200_000\ninference_size = index_size + query_size\ndim = 64\ntopk = 5\ntrack = []","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:53:23.703567Z","iopub.execute_input":"2022-08-07T15:53:23.703978Z","iopub.status.idle":"2022-08-07T15:53:23.710947Z","shell.execute_reply.started":"2022-08-07T15:53:23.703933Z","shell.execute_reply":"2022-08-07T15:53:23.709127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.auto import tqdm\nimport numpy as np\nstart = time.time()\nimport faiss\nqindex = faiss.IndexFlatL2(dim)\nfor _ in tqdm(range(0,index_size), total=index_size):\n    dummy = np.random.random((1, dim)).astype('float32')\n    qindex.add(dummy)\nend = time.time()\ntrack.append({\"stage\":\"Build Index\", \"time\":(end-start)})\ntrack","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-08-07T15:53:23.712945Z","iopub.execute_input":"2022-08-07T15:53:23.714772Z","iopub.status.idle":"2022-08-07T15:53:24.791756Z","shell.execute_reply.started":"2022-08-07T15:53:23.714734Z","shell.execute_reply":"2022-08-07T15:53:24.790630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"elapsed_time_list = []\nfor _ in tqdm(range(0,10), total=10):\n    start_time = time.time()\n    query_dummy = np.random.random((1, dim)).astype('float32')\n    _, _ = qindex.search(query_dummy, topk)\n    end_time = time.time()\n    elapsed_time_list.append(end_time - start_time)\ntrack.append({\"stage\":\"Search Index\", \"time\":np.mean(elapsed_time_list)*query_size})","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-08-07T15:53:24.794764Z","iopub.execute_input":"2022-08-07T15:53:24.795425Z","iopub.status.idle":"2022-08-07T15:53:24.995595Z","shell.execute_reply.started":"2022-08-07T15:53:24.795387Z","shell.execute_reply":"2022-08-07T15:53:24.994355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchvision\nimport numpy as np\nimport time\n\nimport warnings\nwarnings.filterwarnings(\"ignore\", category=DeprecationWarning) \n\n# Model loading.\nmodel = torch.jit.load(model_path)\nmodel.eval()\nembedding_fn = model\nend_time = time.time()\ntrack.append({\"stage\":\"Loading Model\", \"time\":end_time - start_time})\n# Load image and extract its embedding.\ninput_tensor = torch.randn((3, 512, 512))\ninput_batch = input_tensor.unsqueeze(0)\n\nelapsed_time_list = []\nall_embeddings = []\nfor i in range(100):\n    start_time = time.time()\n    with torch.no_grad():\n        embedding = torch.flatten(embedding_fn(input_batch)[0]).cpu().data.numpy()\n    end_time = time.time()\n    elapsed_time_list.append(end_time - start_time)\ntrack.append({\"stage\":\"Creating Vectors\", \"time\":np.mean(elapsed_time_list)*inference_size})","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-08-07T15:53:24.997658Z","iopub.execute_input":"2022-08-07T15:53:24.998102Z","iopub.status.idle":"2022-08-07T15:53:36.552170Z","shell.execute_reply.started":"2022-08-07T15:53:24.998002Z","shell.execute_reply":"2022-08-07T15:53:36.551099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"deviceCount = nvidia_smi.nvmlDeviceGetCount()\nfor i in range(deviceCount):\n    handle = nvidia_smi.nvmlDeviceGetHandleByIndex(i)\n    info = nvidia_smi.nvmlDeviceGetMemoryInfo(handle)\n    print(\"Device {}: {}, Memory : ({:.2f}% free): {}(total), {} (free), {} (used)\".format(i, nvidia_smi.nvmlDeviceGetName(handle), 100*info.free/info.total, info.total, info.free, info.used))\nnvidia_smi.nvmlShutdown()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:53:36.553818Z","iopub.execute_input":"2022-08-07T15:53:36.554198Z","iopub.status.idle":"2022-08-07T15:53:36.566865Z","shell.execute_reply.started":"2022-08-07T15:53:36.554157Z","shell.execute_reply":"2022-08-07T15:53:36.565783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for d in track:\n    print(d['stage'], time_format(d['time'])[3])","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:53:36.568783Z","iopub.execute_input":"2022-08-07T15:53:36.569183Z","iopub.status.idle":"2022-08-07T15:53:36.574680Z","shell.execute_reply.started":"2022-08-07T15:53:36.569147Z","shell.execute_reply":"2022-08-07T15:53:36.573578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total = sum([d['time'] for d in track])\nprint_estimate(total)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:53:36.576427Z","iopub.execute_input":"2022-08-07T15:53:36.577486Z","iopub.status.idle":"2022-08-07T15:53:36.587583Z","shell.execute_reply.started":"2022-08-07T15:53:36.577451Z","shell.execute_reply":"2022-08-07T15:53:36.586604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Please upvote if you find it helpful! :D","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}