{"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":"#just from original \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-14T06:59:10.353928Z","iopub.execute_input":"2022-07-14T06:59:10.355098Z","iopub.status.idle":"2022-07-14T06:59:10.393782Z","shell.execute_reply.started":"2022-07-14T06:59:10.354967Z","shell.execute_reply":"2022-07-14T06:59:10.392302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#install timm\n!pip install timm","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:59:10.396596Z","iopub.execute_input":"2022-07-14T06:59:10.397566Z","iopub.status.idle":"2022-07-14T06:59:28.288825Z","shell.execute_reply.started":"2022-07-14T06:59:10.397518Z","shell.execute_reply":"2022-07-14T06:59:28.287168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import all we need\nimport torch\nimport torch.nn as nn\nfrom torchvision import transforms\nfrom PIL import Image\nimport timm\n","metadata":{"execution":{"iopub.status.busy":"2022-07-14T06:59:28.291644Z","iopub.execute_input":"2022-07-14T06:59:28.292112Z","iopub.status.idle":"2022-07-14T06:59:38.275812Z","shell.execute_reply.started":"2022-07-14T06:59:28.292063Z","shell.execute_reply":"2022-07-14T06:59:38.274315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#just a little modifying\nclass MyModel(nn.Module):\n  def __init__(self):\n    super().__init__()\n    self.feature_extractor = base_model = timm.create_model('vit_base_patch16_224', pretrained=True, num_classes=64)\n    \n  def forward(self, x):\n    x = transforms.functional.resize(x,size=[224, 224])\n    x = x/255.0\n    x = transforms.functional.normalize(x, \n                                            mean=[0.485, 0.456, 0.406], \n                                            std=[0.229, 0.224, 0.225])\n\n    return self.feature_extractor(x)#.logits\n\nmodel = MyModel()\nmodel.eval()\nprint('success')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:00:02.899969Z","iopub.execute_input":"2022-07-14T07:00:02.901233Z","iopub.status.idle":"2022-07-14T07:00:08.603848Z","shell.execute_reply.started":"2022-07-14T07:00:02.901196Z","shell.execute_reply":"2022-07-14T07:00:08.602438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"saved_model = torch.jit.script(model)\nsaved_model.save('saved_model.pt')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:00:12.915578Z","iopub.execute_input":"2022-07-14T07:00:12.916188Z","iopub.status.idle":"2022-07-14T07:00:15.318785Z","shell.execute_reply.started":"2022-07-14T07:00:12.916138Z","shell.execute_reply":"2022-07-14T07:00:15.317317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from zipfile import ZipFile\n\nwith ZipFile('submission.zip','w') as zip:           \n  zip.write('saved_model.pt', arcname='saved_model.pt') ","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:00:18.806893Z","iopub.execute_input":"2022-07-14T07:00:18.807355Z","iopub.status.idle":"2022-07-14T07:00:20.242813Z","shell.execute_reply.started":"2022-07-14T07:00:18.807322Z","shell.execute_reply":"2022-07-14T07:00:20.241285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#check the embedding\nimage_path = '../input/666forme/Acne Awards_What Is Acne__Promo.jpg' #your file\ninput_image = Image.open(image_path).convert(\"RGB\")\ndevice = torch.device('cpu')\nconvert_to_tensor = transforms.Compose([transforms.PILToTensor()])\ninput_tensor = convert_to_tensor(input_image)\ninput_batch = input_tensor.unsqueeze(0)\nmodel = model.to(device)\nwith torch.no_grad():\n  embedding = torch.flatten(model(input_batch)[0].to(device)).cpu().data.numpy()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:01:11.310799Z","iopub.execute_input":"2022-07-14T07:01:11.311232Z","iopub.status.idle":"2022-07-14T07:01:12.162653Z","shell.execute_reply.started":"2022-07-14T07:01:11.311197Z","shell.execute_reply":"2022-07-14T07:01:12.161289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#check the embedding shape\nembedding.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:01:19.386178Z","iopub.execute_input":"2022-07-14T07:01:19.386626Z","iopub.status.idle":"2022-07-14T07:01:19.395835Z","shell.execute_reply.started":"2022-07-14T07:01:19.386593Z","shell.execute_reply":"2022-07-14T07:01:19.394243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#watch inside embedding\nembedding","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:01:13.400446Z","iopub.execute_input":"2022-07-14T07:01:13.401518Z","iopub.status.idle":"2022-07-14T07:01:13.415082Z","shell.execute_reply.started":"2022-07-14T07:01:13.401468Z","shell.execute_reply":"2022-07-14T07:01:13.413299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}