{"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":"import numpy as np\nimport torch\nimport torchvision\nimport torchvision.transforms as transforms\nimport glob\nfrom matplotlib import pyplot as plt\nimport cv2\nimport pandas as pd\nfrom csv import DictReader\nfrom sklearn.model_selection import train_test_split\nfrom PIL import Image\nfrom torch.nn import Flatten, Linear, ReLU, Conv2d,MaxPool2d, AdaptiveAvgPool2d\nfrom tqdm.notebook import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-07T22:18:24.906384Z","iopub.execute_input":"2021-08-07T22:18:24.906682Z","iopub.status.idle":"2021-08-07T22:18:27.104784Z","shell.execute_reply.started":"2021-08-07T22:18:24.906655Z","shell.execute_reply":"2021-08-07T22:18:27.103883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/input/acc-german-traffic-sign-classification/GTSRB_Challenge","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:18:27.106472Z","iopub.execute_input":"2021-08-07T22:18:27.106832Z","iopub.status.idle":"2021-08-07T22:18:27.755411Z","shell.execute_reply.started":"2021-08-07T22:18:27.106785Z","shell.execute_reply":"2021-08-07T22:18:27.754553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_path = '../input/acc-german-traffic-sign-classification/GTSRB_Challenge/sample_submission.csv'\nsample_submission = pd.read_csv(sample_submission_path)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:18:27.756976Z","iopub.execute_input":"2021-08-07T22:18:27.757351Z","iopub.status.idle":"2021-08-07T22:18:27.782442Z","shell.execute_reply.started":"2021-08-07T22:18:27.757310Z","shell.execute_reply":"2021-08-07T22:18:27.781747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.shape\nsample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:18:35.571522Z","iopub.execute_input":"2021-08-07T22:18:35.571867Z","iopub.status.idle":"2021-08-07T22:18:35.589156Z","shell.execute_reply.started":"2021-08-07T22:18:35.571836Z","shell.execute_reply":"2021-08-07T22:18:35.588093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# open file in read mode\nwith open(sample_submission_path, 'r') as read_obj:\n    # pass the file object to DictReader() to get the DictReader object\n    csv_dict_reader = DictReader(read_obj)\n    # iterate over each line as a ordered dictionary\n    print_every = 1000\n    cnt = 1\n    for row in csv_dict_reader:\n        # row variable is a dictionary that represents a row in csv\n        if cnt % print_every == 0:\n            print(row)\n        cnt += 1","metadata":{"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2021-08-07T22:18:39.475607Z","iopub.execute_input":"2021-08-07T22:18:39.475939Z","iopub.status.idle":"2021-08-07T22:18:39.537858Z","shell.execute_reply.started":"2021-08-07T22:18:39.475908Z","shell.execute_reply":"2021-08-07T22:18:39.536932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/input/acc-german-traffic-sign-classification/GTSRB_Challenge/t","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:18:42.179550Z","iopub.execute_input":"2021-08-07T22:18:42.179906Z","iopub.status.idle":"2021-08-07T22:18:42.984207Z","shell.execute_reply.started":"2021-08-07T22:18:42.179873Z","shell.execute_reply":"2021-08-07T22:18:42.983001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# # Data set has 43 classes","metadata":{}},{"cell_type":"code","source":"!ls /kaggle/input/acc-german-traffic-sign-classification/GTSRB_Challenge/train","metadata":{"execution":{"iopub.status.busy":"2021-07-13T01:25:40.321268Z","iopub.execute_input":"2021-07-13T01:25:40.321672Z","iopub.status.idle":"2021-07-13T01:25:40.961947Z","shell.execute_reply.started":"2021-07-13T01:25:40.321635Z","shell.execute_reply":"2021-07-13T01:25:40.96103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/input/acc-german-traffic-sign-classification/GTSRB_Challenge/train/00040","metadata":{"_kg_hide-output":true,"_kg_hide-input":false,"execution":{"iopub.status.busy":"2021-07-13T01:25:40.96558Z","iopub.execute_input":"2021-07-13T01:25:40.965845Z","iopub.status.idle":"2021-07-13T01:25:41.705786Z","shell.execute_reply.started":"2021-07-13T01:25:40.965816Z","shell.execute_reply":"2021-07-13T01:25:41.704906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n#device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n#device","metadata":{"execution":{"iopub.status.busy":"2021-07-13T01:25:41.707811Z","iopub.execute_input":"2021-07-13T01:25:41.708169Z","iopub.status.idle":"2021-07-13T01:25:41.713647Z","shell.execute_reply.started":"2021-07-13T01:25:41.708129Z","shell.execute_reply":"2021-07-13T01:25:41.712178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# data <class id, file_name>\n# Load data file names\ndata = []\ncnt = 0\nfor file_name in glob.glob('/kaggle/input/acc-german-traffic-sign-classification/GTSRB_Challenge/train/*/*.ppm'):\n    im_class = int(file_name.split('/')[-2])\n    data.append((im_class, file_name)) ","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2021-08-07T22:18:45.379285Z","iopub.execute_input":"2021-08-07T22:18:45.379724Z","iopub.status.idle":"2021-08-07T22:18:55.959126Z","shell.execute_reply.started":"2021-08-07T22:18:45.379690Z","shell.execute_reply":"2021-08-07T22:18:55.958259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data[0]","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:18:55.961106Z","iopub.execute_input":"2021-08-07T22:18:55.961492Z","iopub.status.idle":"2021-08-07T22:18:55.969867Z","shell.execute_reply.started":"2021-08-07T22:18:55.961456Z","shell.execute_reply":"2021-08-07T22:18:55.969007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data[0][0]) # -> class number\nprint(data[0][1]) # -> image_path","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:18:55.971725Z","iopub.execute_input":"2021-08-07T22:18:55.972248Z","iopub.status.idle":"2021-08-07T22:18:55.978283Z","shell.execute_reply.started":"2021-08-07T22:18:55.972211Z","shell.execute_reply":"2021-08-07T22:18:55.977377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split dataset to train_set and val_set","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_data, val_data = train_test_split(data, test_size = 0.2)\nprint(f'train size is: {len(train_data)}')\nprint(f'val size is: {len(val_data)}') ","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:18:55.979855Z","iopub.execute_input":"2021-08-07T22:18:55.980602Z","iopub.status.idle":"2021-08-07T22:18:56.005171Z","shell.execute_reply.started":"2021-08-07T22:18:55.980561Z","shell.execute_reply":"2021-08-07T22:18:56.004095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# split data to <class_label, Path> ","metadata":{}},{"cell_type":"code","source":"# x -> path \n# y -> class_label\ntrain_y    = [data[0] for data in train_data]\ntrain_x    = [data[1] for data in train_data]\nval_y     = [data[0] for data in val_data]\nval_x     = [data[1] for data in val_data]","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:18:56.007891Z","iopub.execute_input":"2021-08-07T22:18:56.008318Z","iopub.status.idle":"2021-08-07T22:18:56.025230Z","shell.execute_reply.started":"2021-08-07T22:18:56.008267Z","shell.execute_reply":"2021-08-07T22:18:56.024022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('image_Path is',train_x[0])\nprint('class_label is',train_y[0])","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:18:56.026952Z","iopub.execute_input":"2021-08-07T22:18:56.027539Z","iopub.status.idle":"2021-08-07T22:18:56.035903Z","shell.execute_reply.started":"2021-08-07T22:18:56.027496Z","shell.execute_reply":"2021-08-07T22:18:56.034943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating Dataset","metadata":{}},{"cell_type":"code","source":"class Dataset(torch.utils.data.Dataset):\n    def __init__(self, images, labels, transform = None):\n        self.labels   = labels\n        self.images = images\n        self.transform = transform\n    def __len__(self):\n        return len(self.images)\n    \n    def __getitem__(self, index):\n        i = self.images[index]\n        x = Image.open(i)\n        y = self.labels[index]\n        \n        if self.transform != None:\n            x = self.transform(x)\n    \n        return x,y","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:18:56.037420Z","iopub.execute_input":"2021-08-07T22:18:56.038084Z","iopub.status.idle":"2021-08-07T22:18:56.050860Z","shell.execute_reply.started":"2021-08-07T22:18:56.038025Z","shell.execute_reply":"2021-08-07T22:18:56.049775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_map = {\n    0: '20_speed',1: '30_speed',2: '50_speed',3: '60_speed',\n    4: '70_speed',5: '80_speed',6: '80_lifted',\n    7: '100_speed',8: '120_speed',9: 'no_overtaking_general',\n    10: 'no_overtaking_trucks',11: 'right_of_way_crossing',\n    12: 'right_of_way_general', 13: 'give_way',14: 'stop',15: 'no_way_general',\n    16: 'no_way_trucks',17: 'no_way_one_way',18: 'attention_general',\n    19: 'attention_left_turn',20: 'attention_right_turn',21: 'attention_curvy',\n    22: 'attention_bumpers',23: 'attention_slippery', 24: 'attention_bottleneck',\n    25: 'attention_construction',26: 'attention_traffic_light',\n    27: 'attention_pedestrian', 28: 'attention_children',29: 'attention_bikes',\n    30: 'attention_snowflake',31: 'attention_deer',32: 'lifted_general',\n    33: 'turn_right', 34: 'turn_left',35: 'turn_straight',\n    36: 'turn_straight_right', 37: 'turn_straight_left', 38: 'turn_right_down',\n    39: 'turn_left_down',40: 'turn_circle',41: 'lifted_no_overtaking_general',\n    42: 'lifted_no_overtaking_trucks'\n}","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:18:56.052170Z","iopub.execute_input":"2021-08-07T22:18:56.052701Z","iopub.status.idle":"2021-08-07T22:18:56.063809Z","shell.execute_reply.started":"2021-08-07T22:18:56.052660Z","shell.execute_reply":"2021-08-07T22:18:56.062956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creating object from Dataset class\ntrainset   = Dataset(train_x, train_y)\nfig, axis = plt.subplots(4,4, figsize=(10,10))\nk = 1\nfor i in range(1, 5):\n    for j in range(1, 5):\n        img, label = trainset[j*k]\n        axis[i - 1][j - 1].imshow(img)\n        axis[i - 1][j - 1].set_title(label_map[label])\n        print(\"\")\n        k += 1\nfor ax in axis.flat:\n    ax.label_outer()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:18:58.970272Z","iopub.execute_input":"2021-08-07T22:18:58.970684Z","iopub.status.idle":"2021-08-07T22:19:01.702755Z","shell.execute_reply.started":"2021-08-07T22:18:58.970651Z","shell.execute_reply":"2021-08-07T22:19:01.701868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose(\n    [transforms.Resize((32,32)),\n     transforms.ToTensor(),\n     transforms.Normalize((0.485, 0.456, 0.406),(0.229, 0.224, 0.225))]\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:19:05.236794Z","iopub.execute_input":"2021-08-07T22:19:05.237159Z","iopub.status.idle":"2021-08-07T22:19:05.242305Z","shell.execute_reply.started":"2021-08-07T22:19:05.237126Z","shell.execute_reply":"2021-08-07T22:19:05.241297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainset = Dataset(train_x, train_y, transform=transform)\nvalset   = Dataset(val_x, val_y, transform=transform)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:19:07.155977Z","iopub.execute_input":"2021-08-07T22:19:07.156331Z","iopub.status.idle":"2021-08-07T22:19:07.162115Z","shell.execute_reply.started":"2021-08-07T22:19:07.156299Z","shell.execute_reply":"2021-08-07T22:19:07.160882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x, y = trainset[0]\nprint(type(x))","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:19:09.177929Z","iopub.execute_input":"2021-08-07T22:19:09.178312Z","iopub.status.idle":"2021-08-07T22:19:09.226608Z","shell.execute_reply.started":"2021-08-07T22:19:09.178273Z","shell.execute_reply":"2021-08-07T22:19:09.225636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = torch.utils.data.DataLoader(trainset, batch_size=16,\n                                          shuffle=True, num_workers=2)\nval_loader = torch.utils.data.DataLoader(valset, batch_size=16,\n                                          shuffle=False, num_workers=2)\n\ndata_loaders = {'train':train_loader, 'val':val_loader} \nprint(len(train_loader))\nprint(len(val_loader))","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:19:11.108998Z","iopub.execute_input":"2021-08-07T22:19:11.109367Z","iopub.status.idle":"2021-08-07T22:19:11.116407Z","shell.execute_reply.started":"2021-08-07T22:19:11.109336Z","shell.execute_reply":"2021-08-07T22:19:11.115543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def imshow(img):\n    # Unnormalize from (-1 : 1) to (0 : 1)\n    img = img / 2 + 0.5\n    # converting from tensor to numpy\n    npimg = img.numpy()\n    plt.imshow(np.transpose(npimg,(1, 2, 0)))\n    plt.show()\ndataiter = iter(train_loader)\na = dataiter.next()\nimshow(torchvision.utils.make_grid(a[0]))","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:19:18.670373Z","iopub.execute_input":"2021-08-07T22:19:18.670702Z","iopub.status.idle":"2021-08-07T22:19:18.975133Z","shell.execute_reply.started":"2021-08-07T22:19:18.670672Z","shell.execute_reply":"2021-08-07T22:19:18.974309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = torchvision.models.vgg16(pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:26:55.313528Z","iopub.execute_input":"2021-08-07T22:26:55.313902Z","iopub.status.idle":"2021-08-07T22:26:56.786075Z","shell.execute_reply.started":"2021-08-07T22:26:55.313868Z","shell.execute_reply":"2021-08-07T22:26:56.785286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:26:59.267387Z","iopub.execute_input":"2021-08-07T22:26:59.267749Z","iopub.status.idle":"2021-08-07T22:26:59.279998Z","shell.execute_reply.started":"2021-08-07T22:26:59.267717Z","shell.execute_reply":"2021-08-07T22:26:59.278988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\n# The new Added convolution layer\nAdded_Layers = torch.nn.modules.Sequential(Conv2d(in_channels=512, out_channels=512,\n                                            kernel_size=3, padding=1, stride=1),\n                                    ReLU(),\n                                    AdaptiveAvgPool2d(output_size=(7, 7))\n                                    )\n# Freeze all layers\nfor param in model.parameters():\n    param.requires_grad = False\n\n\n# Parameters of newly constructed modules have requires_grad=True by default\n# Replace the last layers\nmodel.avgpool =Added_Layers\nmodel.classifier = nn.Linear(25088, 43)  \n# Optimize only the parameters of ''classifier', and 'avgpool'\noptimizer = torch.optim.Adam(model.classifier.parameters(), lr=1e-3)\n\nmodel.train()\nmodel = model.cuda()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:27:03.171868Z","iopub.execute_input":"2021-08-07T22:27:03.172225Z","iopub.status.idle":"2021-08-07T22:27:03.246480Z","shell.execute_reply.started":"2021-08-07T22:27:03.172194Z","shell.execute_reply":"2021-08-07T22:27:03.245402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:27:05.468161Z","iopub.execute_input":"2021-08-07T22:27:05.468492Z","iopub.status.idle":"2021-08-07T22:27:05.474173Z","shell.execute_reply.started":"2021-08-07T22:27:05.468462Z","shell.execute_reply":"2021-08-07T22:27:05.473160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2 = torch.nn.modules.Sequential(Conv2d(in_channels=3, out_channels=10, \n                                    kernel_size=3, padding=1, stride=1),\n                                    ReLU(),\n                                    MaxPool2d(2, 2),   \n                                    Conv2d(in_channels=10, out_channels=20,\n                                    kernel_size=3, padding=1, stride=1),\n                                    ReLU(),\n                                    MaxPool2d(2, 2),\n                                    Conv2d(in_channels=20, out_channels=15, \n                                    kernel_size=3, padding=1, stride=1),\n                                    ReLU(),\n                                    Flatten(),\n                                    Linear(15*8*8, 43)\n                                    )\nmodel2 = model2.cuda()\noptimizer = torch.optim.Adam(model2.parameters(), lr=1e-3)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:27:20.258585Z","iopub.execute_input":"2021-08-07T22:27:20.258902Z","iopub.status.idle":"2021-08-07T22:27:20.270432Z","shell.execute_reply.started":"2021-08-07T22:27:20.258874Z","shell.execute_reply":"2021-08-07T22:27:20.269572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"step_losses = []\nepoch_losses = []","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:27:23.728461Z","iopub.execute_input":"2021-08-07T22:27:23.728782Z","iopub.status.idle":"2021-08-07T22:27:23.734478Z","shell.execute_reply.started":"2021-08-07T22:27:23.728754Z","shell.execute_reply":"2021-08-07T22:27:23.733565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print_every = 200\nnum_epochs = 15\n\nfor epoch in range(num_epochs):\n    print('Epoch number:', epoch)\n    \n    epoch_loss = 0\n\n    # For each epoch, do training on the trainset\n    # Then evaluate the accuracy on the validation set\n    for phase in ['train', 'val']:# iterate on train then validate\n        print(phase)\n\n        if phase == 'train':\n            model2.train()\n        else:\n            model2.eval()\n\n        total_loss = 0\n        total_examples = 0\n        total_correct = 0\n\n        # Loop over training / validation data\n        for iteration, (X, y) in enumerate(data_loaders[phase]):\n            # Forward\n            X = X.cuda()\n            y = y.cuda()\n            scores = model2(X)\n\n            # Backward\n            loss = torch.nn.functional.cross_entropy(scores, y)\n            epoch_loss += loss.item()\n            step_losses.append(loss.item())\n            # Update the network only on training data\n            if phase == 'train':\n\n                # Backpropagate the loss\n                loss.backward()\n                # Then update the network parameters\n                with torch.no_grad():\n                    optimizer.step()\n                    optimizer.zero_grad()\n            # Accuracy\n            preds = torch.argmax(scores, dim=1)\n            num_correct = torch.sum(preds == y)        \n\n            # Print\n            total_loss += loss.item()\n            total_correct += num_correct.item()\n            total_examples += len(X)\n            if iteration % print_every == 0:\n                print(total_loss / total_examples, total_correct / total_examples)\n        epoch_losses.append(epoch_loss/len(data_loaders))","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:27:25.928723Z","iopub.execute_input":"2021-08-07T22:27:25.929071Z","iopub.status.idle":"2021-08-07T22:33:47.026329Z","shell.execute_reply.started":"2021-08-07T22:27:25.929024Z","shell.execute_reply":"2021-08-07T22:33:47.025348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize=(10, 5))\naxes[0].plot(step_losses)\naxes[1].plot(epoch_losses)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:41:29.236606Z","iopub.execute_input":"2021-08-07T22:41:29.236945Z","iopub.status.idle":"2021-08-07T22:41:29.775613Z","shell.execute_reply.started":"2021-08-07T22:41:29.236911Z","shell.execute_reply":"2021-08-07T22:41:29.774779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(total_correct / total_examples)\nprint(total_loss / total_examples)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:43:54.002209Z","iopub.execute_input":"2021-08-07T22:43:54.002587Z","iopub.status.idle":"2021-08-07T22:43:54.007801Z","shell.execute_reply.started":"2021-08-07T22:43:54.002557Z","shell.execute_reply":"2021-08-07T22:43:54.006785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images= []\nfor img in glob.glob('/kaggle/input/acc-german-traffic-sign-classification/GTSRB_Challenge/test/*'):\n    images.append(img)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:44:19.383522Z","iopub.execute_input":"2021-08-07T22:44:19.383885Z","iopub.status.idle":"2021-08-07T22:44:19.438617Z","shell.execute_reply.started":"2021-08-07T22:44:19.383851Z","shell.execute_reply":"2021-08-07T22:44:19.437881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images[0]","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:44:28.168955Z","iopub.execute_input":"2021-08-07T22:44:28.169408Z","iopub.status.idle":"2021-08-07T22:44:28.175127Z","shell.execute_reply.started":"2021-08-07T22:44:28.169355Z","shell.execute_reply":"2021-08-07T22:44:28.174138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_loc -> PATH\n# test_id  -> image_name ... (1558973073.8946502.ppm)\ntest_loc = [x for x in images]\ntest_id  = [x.split('/')[-1] for x in images]\ntest_data_set = Dataset(test_loc, test_id , transform = transform)\ntest_loader   = torch.utils.data.DataLoader(test_data_set, batch_size=4,\n                                          shuffle=True, num_workers=2)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:44:37.871172Z","iopub.execute_input":"2021-08-07T22:44:37.871512Z","iopub.status.idle":"2021-08-07T22:44:37.884563Z","shell.execute_reply.started":"2021-08-07T22:44:37.871483Z","shell.execute_reply":"2021-08-07T22:44:37.883384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    output = open('answer3.csv', 'w+')\n    output.write('Filename,ClassID\\n')\n    \n    with torch.no_grad():\n        for iteration, (X, y) in enumerate(test_loader): \n            X = X.cuda()\n            scores = model(X)\n            preds = torch.argmax(scores, dim=1)  \n            for idd,cla in zip(y,preds):\n                output.write(f'{idd},{cla.item()}\\n') \n    output.close()\n    \n          ","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:44:41.201063Z","iopub.execute_input":"2021-08-07T22:44:41.201421Z","iopub.status.idle":"2021-08-07T22:45:10.340303Z","shell.execute_reply.started":"2021-08-07T22:44:41.201389Z","shell.execute_reply":"2021-08-07T22:45:10.339394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}