{"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":"Efficentnet repo : https://github.com/lukemelas/EfficientNet-PyTorch/blob/master/examples/simple/example.ipynb","metadata":{}},{"cell_type":"markdown","source":"# GETTING EFFICIENTNET","metadata":{}},{"cell_type":"code","source":"import json\nfrom PIL import Image\nimport torch\nfrom torchvision import transforms","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-13T09:35:37.674453Z","iopub.execute_input":"2021-10-13T09:35:37.675638Z","iopub.status.idle":"2021-10-13T09:35:37.680514Z","shell.execute_reply.started":"2021-10-13T09:35:37.675556Z","shell.execute_reply":"2021-10-13T09:35:37.679467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install efficientnet_pytorch","metadata":{"execution":{"iopub.status.busy":"2021-10-13T09:36:00.807336Z","iopub.execute_input":"2021-10-13T09:36:00.807696Z","iopub.status.idle":"2021-10-13T09:36:12.845171Z","shell.execute_reply.started":"2021-10-13T09:36:00.807658Z","shell.execute_reply":"2021-10-13T09:36:12.844178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from efficientnet_pytorch import EfficientNet","metadata":{"execution":{"iopub.status.busy":"2021-10-13T09:36:52.962776Z","iopub.execute_input":"2021-10-13T09:36:52.963206Z","iopub.status.idle":"2021-10-13T09:36:52.97528Z","shell.execute_reply.started":"2021-10-13T09:36:52.963163Z","shell.execute_reply":"2021-10-13T09:36:52.974235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel_name = 'efficientnet-b0'\nimage_size = EfficientNet.get_image_size(model_name)","metadata":{"execution":{"iopub.status.busy":"2021-10-13T09:37:09.067022Z","iopub.execute_input":"2021-10-13T09:37:09.067415Z","iopub.status.idle":"2021-10-13T09:37:09.072539Z","shell.execute_reply.started":"2021-10-13T09:37:09.06738Z","shell.execute_reply":"2021-10-13T09:37:09.071606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget \"https://github.com/lukemelas/EfficientNet-PyTorch/raw/master/examples/simple/img2.jpg\"","metadata":{"execution":{"iopub.status.busy":"2021-10-13T09:37:52.255729Z","iopub.execute_input":"2021-10-13T09:37:52.256772Z","iopub.status.idle":"2021-10-13T09:37:53.757006Z","shell.execute_reply.started":"2021-10-13T09:37:52.256722Z","shell.execute_reply":"2021-10-13T09:37:53.755804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget \"https://raw.githubusercontent.com/lukemelas/EfficientNet-PyTorch/master/examples/simple/labels_map.txt\"","metadata":{"execution":{"iopub.status.busy":"2021-10-13T09:41:44.450543Z","iopub.execute_input":"2021-10-13T09:41:44.450867Z","iopub.status.idle":"2021-10-13T09:41:45.500424Z","shell.execute_reply.started":"2021-10-13T09:41:44.450832Z","shell.execute_reply":"2021-10-13T09:41:45.499676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open('img2.jpg')\nimg","metadata":{"execution":{"iopub.status.busy":"2021-10-13T09:38:29.081317Z","iopub.execute_input":"2021-10-13T09:38:29.081694Z","iopub.status.idle":"2021-10-13T09:38:29.193511Z","shell.execute_reply.started":"2021-10-13T09:38:29.081656Z","shell.execute_reply":"2021-10-13T09:38:29.192297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size","metadata":{"execution":{"iopub.status.busy":"2021-10-13T09:40:51.347102Z","iopub.execute_input":"2021-10-13T09:40:51.348074Z","iopub.status.idle":"2021-10-13T09:40:51.357383Z","shell.execute_reply.started":"2021-10-13T09:40:51.348023Z","shell.execute_reply":"2021-10-13T09:40:51.356456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## INFERENCE","metadata":{}},{"cell_type":"code","source":"tfms  =  transforms.Compose([transforms.Resize(image_size), transforms.CenterCrop(image_size), transforms.ToTensor(), transforms.Normalize([0.485, 0.456,0.406], [0.229,0.224,0.225]),])\nimg = tfms(img).unsqueeze(0)","metadata":{"execution":{"iopub.status.busy":"2021-10-13T09:40:20.486255Z","iopub.execute_input":"2021-10-13T09:40:20.48655Z","iopub.status.idle":"2021-10-13T09:40:20.557042Z","shell.execute_reply.started":"2021-10-13T09:40:20.48652Z","shell.execute_reply":"2021-10-13T09:40:20.556061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_map = json.load(open('labels_map.txt'))\nlabels_map = [labels_map[str(i)] for i in range(1000)]","metadata":{"execution":{"iopub.status.busy":"2021-10-13T09:43:20.357423Z","iopub.execute_input":"2021-10-13T09:43:20.357846Z","iopub.status.idle":"2021-10-13T09:43:20.367959Z","shell.execute_reply.started":"2021-10-13T09:43:20.357804Z","shell.execute_reply":"2021-10-13T09:43:20.366604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = EfficientNet.from_pretrained(model_name)\nmodel.eval()\n\nwith torch.no_grad():\n    logits = model(img)\n\npreds = torch.topk(logits, k=3).indices.squeeze(0).tolist()\n\nprint('-----PREDICTION-----')\nfor idx in preds:\n    label = labels_map[idx]\n    prob = torch.softmax(logits, dim=1)[0, idx].item()\n    print('{:<75}({:.2f}%)'.format(label, prob * 100))","metadata":{"execution":{"iopub.status.busy":"2021-10-13T09:46:08.371422Z","iopub.execute_input":"2021-10-13T09:46:08.371772Z","iopub.status.idle":"2021-10-13T09:46:08.602816Z","shell.execute_reply.started":"2021-10-13T09:46:08.37174Z","shell.execute_reply":"2021-10-13T09:46:08.601938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!tar -xvzf \"../input/imagenet-object-localization-challenge/imagenet_object_localization_patched2019.tar.gz\" ","metadata":{"execution":{"iopub.status.busy":"2021-10-13T09:48:29.498499Z","iopub.execute_input":"2021-10-13T09:48:29.500954Z","iopub.status.idle":"2021-10-13T09:48:30.281688Z","shell.execute_reply.started":"2021-10-13T09:48:29.500885Z","shell.execute_reply":"2021-10-13T09:48:30.280761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"only xml files here","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}}]}