{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import transformers\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport datasets\nfrom tqdm import tqdm\nimport torch\nfrom torch import nn\nfrom torch.utils.data import DataLoader\nfrom datasets import load_dataset\nfrom torchvision.transforms import Compose, Normalize, RandomResizedCrop, ColorJitter, ToTensor","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-05T08:04:33.056081Z","iopub.execute_input":"2023-09-05T08:04:33.056534Z","iopub.status.idle":"2023-09-05T08:04:33.068469Z","shell.execute_reply.started":"2023-09-05T08:04:33.056495Z","shell.execute_reply":"2023-09-05T08:04:33.067600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available else 'cpu'\ndevice","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:34:33.493971Z","iopub.execute_input":"2023-09-05T07:34:33.494607Z","iopub.status.idle":"2023-09-05T07:34:33.502445Z","shell.execute_reply.started":"2023-09-05T07:34:33.494572Z","shell.execute_reply":"2023-09-05T07:34:33.501397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import AutoImageProcessor, ResNetForImageClassification\nprocessor = AutoImageProcessor.from_pretrained(\"microsoft/resnet-50\")\nmodel = ResNetForImageClassification.from_pretrained(\"microsoft/resnet-50\").to(device)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:34:33.503881Z","iopub.execute_input":"2023-09-05T07:34:33.504360Z","iopub.status.idle":"2023-09-05T07:34:49.527576Z","shell.execute_reply.started":"2023-09-05T07:34:33.504328Z","shell.execute_reply":"2023-09-05T07:34:49.526555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = load_dataset(\"imagefolder\", data_dir=\"/kaggle/input/vehicle/train/train\")","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:39:32.366874Z","iopub.execute_input":"2023-09-05T07:39:32.367328Z","iopub.status.idle":"2023-09-05T07:42:14.525479Z","shell.execute_reply.started":"2023-09-05T07:39:32.367287Z","shell.execute_reply":"2023-09-05T07:42:14.523788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def transforms(examples):\n    return {\n        'pixel_values':processor(examples[\"image\"],return_tensors='pt')['pixel_values'],\n        'labels':examples['label']\n    }","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:58:11.888419Z","iopub.execute_input":"2023-09-05T07:58:11.888995Z","iopub.status.idle":"2023-09-05T07:58:11.894943Z","shell.execute_reply.started":"2023-09-05T07:58:11.888954Z","shell.execute_reply":"2023-09-05T07:58:11.893961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.classifier[1]=nn.Linear(2048,17).to(device)\nmodel.config.num_labels=17\nmodel.num_labels=17","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:59:57.907330Z","iopub.execute_input":"2023-09-05T07:59:57.907716Z","iopub.status.idle":"2023-09-05T07:59:57.914816Z","shell.execute_reply.started":"2023-09-05T07:59:57.907686Z","shell.execute_reply":"2023-09-05T07:59:57.913584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset=dataset['train']","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:43:25.996697Z","iopub.execute_input":"2023-09-05T07:43:25.997057Z","iopub.status.idle":"2023-09-05T07:43:26.001325Z","shell.execute_reply.started":"2023-09-05T07:43:25.997028Z","shell.execute_reply":"2023-09-05T07:43:26.000423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.set_transform(transforms)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:58:14.595674Z","iopub.execute_input":"2023-09-05T07:58:14.596046Z","iopub.status.idle":"2023-09-05T07:58:14.601660Z","shell.execute_reply.started":"2023-09-05T07:58:14.596018Z","shell.execute_reply":"2023-09-05T07:58:14.600528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer=torch.optim.AdamW(model.parameters(),lr=0.0001)\nEPOCHS=3\nloss_fn=nn.CrossEntropyLoss()\nfor epoch in range(EPOCHS):\n    tdl=tqdm(DataLoader(dataset,batch_size=64,num_workers=2,shuffle=True))\n    for a in tdl:\n        labels=nn.functional.one_hot(a['labels'].long(),17).float().to(device)\n        preds=model(**{k:a[k].to(device) for k in ('pixel_values','labels')})\n        loss=loss_fn(labels,preds.logits.softmax(-1))\n        model.zero_grad(set_to_none=True)\n        loss.backward()\n        optimizer.step()\n        tdl.set_description(f'loss = {loss.item()}')\n        ","metadata":{"execution":{"iopub.status.busy":"2023-09-05T08:11:37.260669Z","iopub.execute_input":"2023-09-05T08:11:37.261043Z","iopub.status.idle":"2023-09-05T09:04:27.716008Z","shell.execute_reply.started":"2023-09-05T08:11:37.261014Z","shell.execute_reply":"2023-09-05T09:04:27.714702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a","metadata":{"execution":{"iopub.status.busy":"2023-09-05T08:11:15.387495Z","iopub.execute_input":"2023-09-05T08:11:15.388166Z","iopub.status.idle":"2023-09-05T08:11:15.409919Z","shell.execute_reply.started":"2023-09-05T08:11:15.388131Z","shell.execute_reply":"2023-09-05T08:11:15.409080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    outputs = model(**inputs.to(device))\n    logits = outputs.logits\n\n# model predicts one of the 1000 ImageNet classes\npredicted_label = logits.argmax(-1).detach()\nprint([model.config.id2label[i.item()] for i in predicted_label])\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-05T08:10:47.787199Z","iopub.execute_input":"2023-09-05T08:10:47.787577Z","iopub.status.idle":"2023-09-05T08:10:47.812647Z","shell.execute_reply.started":"2023-09-05T08:10:47.787546Z","shell.execute_reply":"2023-09-05T08:10:47.811740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logits","metadata":{"execution":{"iopub.status.busy":"2023-09-05T08:10:54.774555Z","iopub.execute_input":"2023-09-05T08:10:54.774947Z","iopub.status.idle":"2023-09-05T08:10:54.789772Z","shell.execute_reply.started":"2023-09-05T08:10:54.774918Z","shell.execute_reply":"2023-09-05T08:10:54.788685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"outputs","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:44:51.066187Z","iopub.execute_input":"2023-09-05T07:44:51.066545Z","iopub.status.idle":"2023-09-05T07:44:51.102621Z","shell.execute_reply.started":"2023-09-05T07:44:51.066515Z","shell.execute_reply":"2023-09-05T07:44:51.101730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['train'][0]['image']","metadata":{"execution":{"iopub.status.busy":"2023-09-05T07:20:03.494472Z","iopub.execute_input":"2023-09-05T07:20:03.494831Z","iopub.status.idle":"2023-09-05T07:20:03.868373Z","shell.execute_reply.started":"2023-09-05T07:20:03.494802Z","shell.execute_reply":"2023-09-05T07:20:03.867546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}