{"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"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":17018,"databundleVersionId":799209,"sourceType":"competition"}],"dockerImageVersionId":30554,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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.v2 import Compose, Normalize, RandomResizedCrop, ColorJitter, ToTensor\nfrom torchvision.transforms.v2 import Compose, ColorJitter, ToTensor\nfrom torchvision.transforms.v2 import (\n    CenterCrop,\n    Compose,\n    Normalize,\n    RandomHorizontalFlip,\n    RandomResizedCrop,\n    RandomPerspective,\n    RandomRotation,\n    Resize,\n    RandAugment,\n    ToImageTensor, \n    ConvertImageDtype,\n    ToTensor,\n)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-25T18:48:42.579123Z","iopub.execute_input":"2024-08-25T18:48:42.57959Z","iopub.status.idle":"2024-08-25T18:48:48.713532Z","shell.execute_reply.started":"2024-08-25T18:48:42.579563Z","shell.execute_reply":"2024-08-25T18:48:48.712418Z"},"trusted":true},"execution_count":1,"outputs":[{"name":"stderr","text":"/opt/conda/lib/python3.10/site-packages/torchvision/datapoints/__init__.py:12: UserWarning: The torchvision.datapoints and torchvision.transforms.v2 namespaces are still Beta. While we do not expect major breaking changes, some APIs may still change according to user feedback. Please submit any feedback you may have in this issue: https://github.com/pytorch/vision/issues/6753, and you can also check out https://github.com/pytorch/vision/issues/7319 to learn more about the APIs that we suspect might involve future changes. You can silence this warning by calling torchvision.disable_beta_transforms_warning().\n  warnings.warn(_BETA_TRANSFORMS_WARNING)\n/opt/conda/lib/python3.10/site-packages/torchvision/transforms/v2/__init__.py:54: UserWarning: The torchvision.datapoints and torchvision.transforms.v2 namespaces are still Beta. While we do not expect major breaking changes, some APIs may still change according to user feedback. Please submit any feedback you may have in this issue: https://github.com/pytorch/vision/issues/6753, and you can also check out https://github.com/pytorch/vision/issues/7319 to learn more about the APIs that we suspect might involve future changes. You can silence this warning by calling torchvision.disable_beta_transforms_warning().\n  warnings.warn(_BETA_TRANSFORMS_WARNING)\n","output_type":"stream"}]},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available else 'cpu'\ndevice","metadata":{"execution":{"iopub.status.busy":"2024-08-25T18:48:56.422968Z","iopub.execute_input":"2024-08-25T18:48:56.423783Z","iopub.status.idle":"2024-08-25T18:48:56.430767Z","shell.execute_reply.started":"2024-08-25T18:48:56.42375Z","shell.execute_reply":"2024-08-25T18:48:56.429838Z"},"trusted":true},"execution_count":2,"outputs":[{"execution_count":2,"output_type":"execute_result","data":{"text/plain":"'cuda'"},"metadata":{}}]},{"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":"2024-08-25T18:49:00.339793Z","iopub.execute_input":"2024-08-25T18:49:00.340565Z","iopub.status.idle":"2024-08-25T18:49:10.316387Z","shell.execute_reply.started":"2024-08-25T18:49:00.340535Z","shell.execute_reply":"2024-08-25T18:49:10.315365Z"},"trusted":true},"execution_count":3,"outputs":[{"name":"stderr","text":"/opt/conda/lib/python3.10/site-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.16.5 and <1.23.0 is required for this version of SciPy (detected version 1.23.5\n  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n","output_type":"stream"},{"output_type":"display_data","data":{"text/plain":"Downloading (…)rocessor_config.json:   0%|          | 0.00/266 [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"e148f0982f714cb6a14da826da58fa9d"}},"metadata":{}},{"name":"stderr","text":"Could not find image processor class in the image processor config or the model config. Loading based on pattern matching with the model's feature extractor configuration.\n","output_type":"stream"},{"output_type":"display_data","data":{"text/plain":"Downloading config.json:   0%|          | 0.00/69.6k [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"0d757e056c4349948576766a90283786"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"Downloading model.safetensors:   0%|          | 0.00/102M [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"e071f6a1112d4a1a9a00894beccc4800"}},"metadata":{}}]},{"cell_type":"code","source":"dataset = load_dataset(\"imagefolder\", data_dir=\"/kaggle/input/vehicle/train/train\")","metadata":{"execution":{"iopub.status.busy":"2024-08-25T18:49:17.382264Z","iopub.execute_input":"2024-08-25T18:49:17.383112Z","iopub.status.idle":"2024-08-25T18:51:50.505153Z","shell.execute_reply.started":"2024-08-25T18:49:17.38308Z","shell.execute_reply":"2024-08-25T18:51:50.504028Z"},"trusted":true},"execution_count":4,"outputs":[{"output_type":"display_data","data":{"text/plain":"Resolving data files:   0%|          | 0/28045 [00:00<?, ?it/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"84fc371ff64e46ecbc4c8cc464363c8e"}},"metadata":{}},{"name":"stdout","text":"Downloading and preparing dataset image_folder/default to /root/.cache/huggingface/datasets/image_folder/default-d14055fe71600295/0.0.0/ee92df8e96c6907f3c851a987be3fd03d4b93b247e727b69a8e23ac94392a091...\n      ","output_type":"stream"},{"output_type":"display_data","data":{"text/plain":"Downloading data files #0:   0%|          | 0/1753 [00:00<?, ?obj/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"5488c1f176e542199beb277a25371a0b"}},"metadata":{}},{"name":"stdout","text":"   ","output_type":"stream"},{"output_type":"display_data","data":{"text/plain":"Downloading data files #3:   0%|          | 0/1753 [00:00<?, ?obj/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"6b6ffa4f3ead4ad4a03275a00472526d"}},"metadata":{}},{"name":"stdout","text":"  ","output_type":"stream"},{"output_type":"display_data","data":{"text/plain":"Downloading data files #2:   0%|          | 0/1753 [00:00<?, ?obj/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"6188a74710844af291ac0592a7ba06f9"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"Downloading data files #1:   0%|          | 0/1753 [00:00<?, ?obj/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"2c09a07d60634e3891323059f6dcef1a"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"Downloading data files #4:   0%|          | 0/1753 [00:00<?, ?obj/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"1e218b6085fa4b6f9e9689b6ac8d78a6"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"Downloading data files #5:   0%|          | 0/1753 [00:00<?, ?obj/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"62e8a0c877594298baeb1694f58e00ab"}},"metadata":{}},{"name":"stdout","text":"  ","output_type":"stream"},{"output_type":"display_data","data":{"text/plain":"Downloading data files #6:   0%|          | 0/1753 [00:00<?, ?obj/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"72baa161defe48899141c3193c5afc34"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"Downloading data files #7:   0%|          | 0/1753 [00:00<?, ?obj/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"6548beb4640240c5837a7ff1b6b96332"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"Downloading data files #9:   0%|          | 0/1753 [00:00<?, ?obj/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"8fc91ef9cc9c41a690b74fedc0b6e061"}},"metadata":{}},{"name":"stdout","text":" ","output_type":"stream"},{"output_type":"display_data","data":{"text/plain":"Downloading data files #8:   0%|          | 0/1753 [00:00<?, ?obj/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"b6d8bea95fd043dfaa159033008faf30"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"Downloading data files #10:   0%|          | 0/1753 [00:00<?, ?obj/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"56e1382161744b4cb8d94f4020ebab3b"}},"metadata":{}},{"name":"stdout","text":"  ","output_type":"stream"},{"output_type":"display_data","data":{"text/plain":"Downloading data files #13:   0%|          | 0/1752 [00:00<?, ?obj/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"70edb1155f6f497e9bc383193d1c05a7"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"Downloading data files #12:   0%|          | 0/1753 [00:00<?, ?obj/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"811c0215b2dd48c0a001eef080816489"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"Downloading data files #11:   0%|          | 0/1753 [00:00<?, ?obj/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"28bafd81d9cd4f4eb6e00af74f39c5dd"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"Downloading data files #14:   0%|          | 0/1752 [00:00<?, ?obj/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"274382d385d340e4b23caef036b4c514"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"Downloading data files #15:   0%|          | 0/1752 [00:00<?, ?obj/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"4636a1b740f3442884cca01b802cd8a5"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"Downloading data files: 0it [00:00, ?it/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"af390eac53e548608ccb74960b3673f9"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"Extracting data files: 0it [00:00, ?it/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"7ed07ad136c14996aeb31296abbfef34"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"Generating train split: 0 examples [00:00, ? examples/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":""}},"metadata":{}},{"name":"stdout","text":"Dataset image_folder downloaded and prepared to /root/.cache/huggingface/datasets/image_folder/default-d14055fe71600295/0.0.0/ee92df8e96c6907f3c851a987be3fd03d4b93b247e727b69a8e23ac94392a091. Subsequent calls will reuse this data.\n","output_type":"stream"},{"output_type":"display_data","data":{"text/plain":"  0%|          | 0/1 [00:00<?, ?it/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"2a502c142dae4c3d9e0688cd1cb80f50"}},"metadata":{}}]},{"cell_type":"code","source":"%%timeit -n10 -r10\n(Compose(\n        [\n            RandomResizedCrop(processor.size['shortest_edge']),\n            RandAugment(),\n        ]\n    )(dataset['train'][0]['image']))","metadata":{"execution":{"iopub.status.busy":"2024-08-25T18:53:39.940764Z","iopub.execute_input":"2024-08-25T18:53:39.941474Z","iopub.status.idle":"2024-08-25T18:53:40.905336Z","shell.execute_reply.started":"2024-08-25T18:53:39.94144Z","shell.execute_reply":"2024-08-25T18:53:40.904339Z"},"trusted":true},"execution_count":17,"outputs":[{"name":"stdout","text":"9.57 ms ± 531 µs per loop (mean ± std. dev. of 10 runs, 10 loops each)\n","output_type":"stream"}]},{"cell_type":"code","source":"#%%timeit -n10 -r10\n(nn.Sequential(\n    *[\n        \n        RandomResizedCrop(processor.size['shortest_edge']),\n        RandAugment(),\n        ConvertImageDtype(torch.float)\n    ]\n)(ToImageTensor()(dataset['train'][0]['image']).to('cuda')))","metadata":{"execution":{"iopub.status.busy":"2024-08-25T18:53:44.020826Z","iopub.execute_input":"2024-08-25T18:53:44.021864Z","iopub.status.idle":"2024-08-25T18:53:44.058199Z","shell.execute_reply.started":"2024-08-25T18:53:44.021815Z","shell.execute_reply":"2024-08-25T18:53:44.056754Z"},"trusted":true},"execution_count":18,"outputs":[{"execution_count":18,"output_type":"execute_result","data":{"text/plain":"Image([[[0.1725, 0.1725, 0.1765,  ..., 0.1020, 0.1176, 0.1412],\n        [0.1843, 0.1843, 0.1843,  ..., 0.0863, 0.0902, 0.1333],\n        [0.1804, 0.1765, 0.1804,  ..., 0.3137, 0.2980, 0.3216],\n        ...,\n        [0.5765, 0.5725, 0.5686,  ..., 0.0000, 0.0000, 0.0000],\n        [0.5765, 0.5765, 0.5725,  ..., 0.0000, 0.0000, 0.0000],\n        [0.5804, 0.5765, 0.5725,  ..., 0.0000, 0.0000, 0.0000]],\n\n       [[0.1059, 0.1059, 0.1098,  ..., 0.1020, 0.0980, 0.1020],\n        [0.1176, 0.1176, 0.1176,  ..., 0.0627, 0.0588, 0.0863],\n        [0.1137, 0.1137, 0.1137,  ..., 0.2706, 0.2549, 0.2706],\n        ...,\n        [0.5804, 0.5725, 0.5686,  ..., 0.0000, 0.0000, 0.0000],\n        [0.5804, 0.5765, 0.5725,  ..., 0.0000, 0.0000, 0.0000],\n        [0.5804, 0.5765, 0.5725,  ..., 0.0000, 0.0000, 0.0000]],\n\n       [[0.0667, 0.0667, 0.0667,  ..., 0.0863, 0.1020, 0.1176],\n        [0.0784, 0.0784, 0.0784,  ..., 0.0588, 0.0510, 0.0824],\n        [0.0824, 0.0824, 0.0824,  ..., 0.2824, 0.2314, 0.2510],\n        ...,\n        [0.5569, 0.5647, 0.5608,  ..., 0.0000, 0.0000, 0.0000],\n        [0.5608, 0.5686, 0.5647,  ..., 0.0000, 0.0000, 0.0000],\n        [0.5686, 0.5686, 0.5647,  ..., 0.0000, 0.0000, 0.0000]]],\n      device='cuda:0', )"},"metadata":{}}]},{"cell_type":"code","source":"dataset = dataset['train'].train_test_split(test_size=0.15)","metadata":{"execution":{"iopub.status.busy":"2024-08-25T18:53:57.76164Z","iopub.execute_input":"2024-08-25T18:53:57.762274Z","iopub.status.idle":"2024-08-25T18:53:58.603696Z","shell.execute_reply.started":"2024-08-25T18:53:57.762239Z","shell.execute_reply":"2024-08-25T18:53:58.60267Z"},"trusted":true},"execution_count":21,"outputs":[]},{"cell_type":"code","source":"def transforms(examples):\n    compose=Compose(\n        [\n            ToImageTensor(),\n            RandomResizedCrop(processor.size['shortest_edge']),\n            RandAugment(),\n            ConvertImageDtype()\n        ]\n    )\n    #precompose=\n    return {\n        'pixel_values':processor([compose(image) for image in examples[\"image\"]],return_tensors='pt')['pixel_values'],\n        'labels':examples['label']\n    }","metadata":{"execution":{"iopub.status.busy":"2024-08-25T18:54:05.233807Z","iopub.execute_input":"2024-08-25T18:54:05.234735Z","iopub.status.idle":"2024-08-25T18:54:05.24058Z","shell.execute_reply.started":"2024-08-25T18:54:05.2347Z","shell.execute_reply":"2024-08-25T18:54:05.239557Z"},"trusted":true},"execution_count":24,"outputs":[]},{"cell_type":"code","source":"#transforms(dataset['train'][:2])","metadata":{"execution":{"iopub.status.busy":"2023-09-07T07:08:14.361258Z","iopub.execute_input":"2023-09-07T07:08:14.361839Z","iopub.status.idle":"2023-09-07T07:08:14.36909Z","shell.execute_reply.started":"2023-09-07T07:08:14.361789Z","shell.execute_reply":"2023-09-07T07:08:14.36805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['train'].set_transform(lambda x:x)\ndataset['test'].set_transform(lambda x:x)\n\ndataset['train'].set_transform(transforms)\ndataset['test'].set_transform(transforms)","metadata":{"execution":{"iopub.status.busy":"2024-08-25T18:54:10.283603Z","iopub.execute_input":"2024-08-25T18:54:10.28397Z","iopub.status.idle":"2024-08-25T18:54:10.292855Z","shell.execute_reply.started":"2024-08-25T18:54:10.28394Z","shell.execute_reply":"2024-08-25T18:54:10.291862Z"},"trusted":true},"execution_count":25,"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":"2024-08-25T18:54:16.869496Z","iopub.execute_input":"2024-08-25T18:54:16.86988Z","iopub.status.idle":"2024-08-25T18:54:16.876031Z","shell.execute_reply.started":"2024-08-25T18:54:16.869848Z","shell.execute_reply":"2024-08-25T18:54:16.874934Z"},"trusted":true},"execution_count":27,"outputs":[]},{"cell_type":"code","source":"a.keys()","metadata":{"execution":{"iopub.status.busy":"2024-08-25T18:54:21.1058Z","iopub.execute_input":"2024-08-25T18:54:21.106578Z","iopub.status.idle":"2024-08-25T18:54:22.065447Z","shell.execute_reply.started":"2024-08-25T18:54:21.106548Z","shell.execute_reply":"2024-08-25T18:54:22.064107Z"},"trusted":true},"execution_count":28,"outputs":[{"traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)","Cell \u001b[0;32mIn[28], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43ma\u001b[49m\u001b[38;5;241m.\u001b[39mkeys()\n","\u001b[0;31mNameError\u001b[0m: name 'a' is not defined"],"ename":"NameError","evalue":"name 'a' is not defined","output_type":"error"}]},{"cell_type":"code","source":"model.eval()\nvdl=tqdm(DataLoader(dataset['test'],batch_size=64,num_workers=2,shuffle=True))\ntotal_loss=0\nn=0\nwith torch.no_grad():\n    for a in vdl:\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        total_loss+=nn.CrossEntropyLoss()(labels,preds.logits.softmax(-1)).detach()\n        n+=1\n        vdl.set_description(f'average validation loss = {total_loss.item()/n}')","metadata":{"execution":{"iopub.status.busy":"2024-08-25T18:54:32.338052Z","iopub.execute_input":"2024-08-25T18:54:32.338397Z","iopub.status.idle":"2024-08-25T18:55:40.928652Z","shell.execute_reply.started":"2024-08-25T18:54:32.338371Z","shell.execute_reply":"2024-08-25T18:55:40.927507Z"},"trusted":true},"execution_count":29,"outputs":[{"name":"stderr","text":"average validation loss = 2.8704065595354353: 100%|██████████| 56/56 [01:08<00:00,  1.22s/it]\n","output_type":"stream"}]},{"cell_type":"code","source":"learnables=[]\nfor n,p in model.named_parameters():\n    p.requires_grad = True\n    if 'classifier' in n:\n        learnables.append({'params':p,'lr':0.01})\n    elif any(i in n for i in '345'):\n        learnables.append({'params':p,'lr':0.001})\n    else:\n        p.requires_grad = False","metadata":{"execution":{"iopub.status.busy":"2024-08-25T18:55:58.248662Z","iopub.execute_input":"2024-08-25T18:55:58.249429Z","iopub.status.idle":"2024-08-25T18:55:58.256861Z","shell.execute_reply.started":"2024-08-25T18:55:58.249397Z","shell.execute_reply":"2024-08-25T18:55:58.255977Z"},"trusted":true},"execution_count":32,"outputs":[]},{"cell_type":"code","source":"optimizer=torch.optim.AdamW(learnables)\nEPOCHS=4\nloss_fn=nn.CrossEntropyLoss()\nfor epoch in range(EPOCHS):\n    tdl=tqdm(DataLoader(dataset['train'],batch_size=64,num_workers=2,shuffle=True))\n    model.train()\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'train loss = {loss.item()}')\n    model.eval()\n    with torch.no_grad():\n        total_loss=0\n        n=0\n        vdl=tqdm(DataLoader(dataset['test'],batch_size=64,num_workers=2,shuffle=True))\n        for a in vdl:\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            total_loss+=loss_fn(labels,preds.logits.softmax(-1)).detach()\n            n+=1\n            vdl.set_description(f'average validation loss = {total_loss.item()/n}')\n        \n        ","metadata":{"execution":{"iopub.status.busy":"2024-08-25T18:56:01.821268Z","iopub.execute_input":"2024-08-25T18:56:01.821622Z"},"trusted":true},"execution_count":null,"outputs":[{"name":"stderr","text":"train loss = 2.5873677730560303: 100%|██████████| 317/317 [06:30<00:00,  1.23s/it]\naverage validation loss = 2.46666990007673: 100%|██████████| 56/56 [01:06<00:00,  1.18s/it]  \ntrain loss = 2.5570883750915527:  92%|█████████▏| 292/317 [06:10<00:24,  1.02it/s]","output_type":"stream"}]},{"cell_type":"code","source":"_a=a['labels']\n_b=preds.logits.softmax(-1).argmax(-1)","metadata":{"execution":{"iopub.status.busy":"2023-09-07T04:48:59.993962Z","iopub.execute_input":"2023-09-07T04:48:59.99493Z","iopub.status.idle":"2023-09-07T04:49:00.000855Z","shell.execute_reply.started":"2023-09-07T04:48:59.994894Z","shell.execute_reply":"2023-09-07T04:48:59.999866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\nprint(classification_report(_a.detach().cpu().numpy(),_b.detach().cpu().numpy()))","metadata":{"execution":{"iopub.status.busy":"2023-09-07T04:53:11.321082Z","iopub.execute_input":"2023-09-07T04:53:11.322252Z","iopub.status.idle":"2023-09-07T04:53:11.338291Z","shell.execute_reply.started":"2023-09-07T04:53:11.322209Z","shell.execute_reply":"2023-09-07T04:53:11.337312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds=[]#TODO\nvdl=tqdm(DataLoader(dataset['test'],batch_size=64,num_workers=2,shuffle=True))\nfor a in vdl:\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    total_loss+=loss_fn(labels,preds.logits.softmax(-1)).detach()\n    n+=1\n    vdl.set_description(f'average validation loss = {total_loss.item()/n}')\n\n","metadata":{},"execution_count":null,"outputs":[]}]}