{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","scrolled":true,"execution":{"iopub.status.busy":"2021-06-17T09:57:40.110491Z","iopub.execute_input":"2021-06-17T09:57:40.110826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pwd\n%ls","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"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 as cv\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom PIL import Image\nimport torchvision.datasets as datasets\nfrom torchvision.utils import make_grid\nimport matplotlib.pyplot as plt\nimport numpy as np","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2021-07-08T15:15:31.201276Z","iopub.execute_input":"2021-07-08T15:15:31.201597Z","iopub.status.idle":"2021-07-08T15:15:33.693335Z","shell.execute_reply.started":"2021-07-08T15:15:31.201567Z","shell.execute_reply":"2021-07-08T15:15:33.692432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/input/acc-german-traffic-sign-classification/GTSRB_Challenge/train","metadata":{"execution":{"iopub.status.busy":"2021-07-08T15:15:33.698533Z","iopub.execute_input":"2021-07-08T15:15:33.700652Z","iopub.status.idle":"2021-07-08T15:15:34.512611Z","shell.execute_reply.started":"2021-07-08T15:15:33.70061Z","shell.execute_reply":"2021-07-08T15:15:34.511544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load data file names\ndata = []\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":{"execution":{"iopub.status.busy":"2021-07-08T15:16:06.834957Z","iopub.execute_input":"2021-07-08T15:16:06.835328Z","iopub.status.idle":"2021-07-08T15:16:08.164704Z","shell.execute_reply.started":"2021-07-08T15:16:06.835285Z","shell.execute_reply":"2021-07-08T15:16:08.163903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data[0])","metadata":{"execution":{"iopub.status.busy":"2021-07-08T15:16:12.406579Z","iopub.execute_input":"2021-07-08T15:16:12.406909Z","iopub.status.idle":"2021-07-08T15:16:12.411911Z","shell.execute_reply.started":"2021-07-08T15:16:12.406878Z","shell.execute_reply":"2021-07-08T15:16:12.410774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split dataset\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-07-08T15:16:12.829592Z","iopub.execute_input":"2021-07-08T15:16:12.829887Z","iopub.status.idle":"2021-07-08T15:16:12.85492Z","shell.execute_reply.started":"2021-07-08T15:16:12.829858Z","shell.execute_reply":"2021-07-08T15:16:12.853487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_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-07-08T15:16:14.066614Z","iopub.execute_input":"2021-07-08T15:16:14.066946Z","iopub.status.idle":"2021-07-08T15:16:14.084597Z","shell.execute_reply.started":"2021-07-08T15:16:14.066916Z","shell.execute_reply":"2021-07-08T15:16:14.083823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-07-08T15:16:15.020319Z","iopub.execute_input":"2021-07-08T15:16:15.020669Z","iopub.status.idle":"2021-07-08T15:16:15.031435Z","shell.execute_reply.started":"2021-07-08T15:16:15.020638Z","shell.execute_reply":"2021-07-08T15:16:15.030452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_map = {\n    0: '20_speed',\n    1: '30_speed',\n    2: '50_speed',\n    3: '60_speed',\n    4: '70_speed',\n    5: '80_speed',\n    6: '80_lifted',\n    7: '100_speed',\n    8: '120_speed',\n    9: 'no_overtaking_general',\n    10: 'no_overtaking_trucks',\n    11: 'right_of_way_crossing',\n    12: 'right_of_way_general',\n    13: 'give_way',\n    14: 'stop',\n    15: 'no_way_general',\n    16: 'no_way_trucks',\n    17: 'no_way_one_way',\n    18: 'attention_general',\n    19: 'attention_left_turn',\n    20: 'attention_right_turn',\n    21: 'attention_curvy',\n    22: 'attention_bumpers',\n    23: 'attention_slippery',\n    24: 'attention_bottleneck',\n    25: 'attention_construction',\n    26: 'attention_traffic_light',\n    27: 'attention_pedestrian',\n    28: 'attention_children',\n    29: 'attention_bikes',\n    30: 'attention_snowflake',\n    31: 'attention_deer',\n    32: 'lifted_general',\n    33: 'turn_right',\n    34: 'turn_left',\n    35: 'turn_straight',\n    36: 'turn_straight_right',\n    37: 'turn_straight_left',\n    38: 'turn_right_down',\n    39: 'turn_left_down',\n    40: 'turn_circle',\n    41: 'lifted_no_overtaking_general',\n    42: 'lifted_no_overtaking_trucks'\n}","metadata":{"execution":{"iopub.status.busy":"2021-07-08T15:16:15.54063Z","iopub.execute_input":"2021-07-08T15:16:15.540964Z","iopub.status.idle":"2021-07-08T15:16:15.551175Z","shell.execute_reply.started":"2021-07-08T15:16:15.540908Z","shell.execute_reply":"2021-07-08T15:16:15.550086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainset = Dataset(train_x, train_y)\nimg, y = trainset[0]","metadata":{"execution":{"iopub.status.busy":"2021-07-08T15:16:16.880917Z","iopub.execute_input":"2021-07-08T15:16:16.881231Z","iopub.status.idle":"2021-07-08T15:16:16.908593Z","shell.execute_reply.started":"2021-07-08T15:16:16.881199Z","shell.execute_reply":"2021-07-08T15:16:16.907862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(label_map[y])\nimg","metadata":{"execution":{"iopub.status.busy":"2021-07-08T15:16:17.444839Z","iopub.execute_input":"2021-07-08T15:16:17.445134Z","iopub.status.idle":"2021-07-08T15:16:17.455826Z","shell.execute_reply.started":"2021-07-08T15:16:17.445106Z","shell.execute_reply":"2021-07-08T15:16:17.45472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# TODO: Visualize the first 5 images in the trainset\n# Use https://pytorch.org/docs/stable/torchvision/utils.html\n# Print a list containing the classes of the 5 images in orde\ntrainset[5][0]","metadata":{"execution":{"iopub.status.busy":"2021-07-08T15:16:18.000468Z","iopub.execute_input":"2021-07-08T15:16:18.000757Z","iopub.status.idle":"2021-07-08T15:16:18.021598Z","shell.execute_reply.started":"2021-07-08T15:16:18.000728Z","shell.execute_reply":"2021-07-08T15:16:18.020929Z"},"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.5, 0.5, 0.5), (0.5, 0.5, 0.5))])\n\ntrainset = Dataset(train_x, train_y, transform=transform)\nvalset = Dataset(val_x, val_y, transform=transform)","metadata":{"execution":{"iopub.status.busy":"2021-07-08T15:16:18.752509Z","iopub.execute_input":"2021-07-08T15:16:18.752813Z","iopub.status.idle":"2021-07-08T15:16:18.758984Z","shell.execute_reply.started":"2021-07-08T15:16:18.752784Z","shell.execute_reply":"2021-07-08T15:16:18.757751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = torch.utils.data.DataLoader(trainset, batch_size=4,\n                                          shuffle=True, num_workers=2)\nval_loader = torch.utils.data.DataLoader(valset, batch_size=4,\n                                          shuffle=False, num_workers=2)\n\ndata_loaders = {'train':train_loader, 'val':val_loader}","metadata":{"execution":{"iopub.status.busy":"2021-07-08T15:16:19.625216Z","iopub.execute_input":"2021-07-08T15:16:19.625569Z","iopub.status.idle":"2021-07-08T15:16:19.63097Z","shell.execute_reply.started":"2021-07-08T15:16:19.625538Z","shell.execute_reply":"2021-07-08T15:16:19.62989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport torch\nfrom torchvision import datasets, transforms\nfrom torch.nn import Flatten, Linear, ReLU, Conv2d,MaxPool2d","metadata":{"execution":{"iopub.status.busy":"2021-07-08T15:16:20.486583Z","iopub.execute_input":"2021-07-08T15:16:20.486908Z","iopub.status.idle":"2021-07-08T15:16:20.493124Z","shell.execute_reply.started":"2021-07-08T15:16:20.486876Z","shell.execute_reply":"2021-07-08T15:16:20.49195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = torch.nn.modules.Sequential(Conv2d(in_channels=3, out_channels=10, kernel_size=3, padding=1, stride=1),\n                                    ReLU(),\n                                    MaxPool2d(2, 2),   # Filter size 2, stride 2\n                                    Conv2d(in_channels=10, out_channels=10, kernel_size=3, padding=1, stride=1),\n                                    ReLU(),\n                                    Flatten(),\n                                    Linear(10*16*16, 43)\n                                    )\nmodel = model.cuda()\noptimizer = torch.optim.Adam(model.parameters(), lr=1e-3)\n\nprint_every = 100\nnum_epochs = 2\n\nfor epoch in range(num_epochs):\n    print('Epoch number:', epoch)\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']:\n        print(phase)\n\n        if phase == 'train':\n            model.train()\n        else:\n            model.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 = model(X)\n\n            # Backward\n            loss = torch.nn.functional.cross_entropy(scores, y)\n            \n            # Update the network only on training data\n            if phase == 'train':\n                loss.backward()\n                # Update\n                with torch.no_grad():\n                    optimizer.step()\n                    optimizer.zero_grad()\n                    \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)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-07-08T15:16:21.236778Z","iopub.execute_input":"2021-07-08T15:16:21.237088Z","iopub.status.idle":"2021-07-08T15:18:59.496649Z","shell.execute_reply.started":"2021-07-08T15:16:21.237059Z","shell.execute_reply":"2021-07-08T15:18:59.495744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# TODO: Visualize the first 5 images in the trainset\n# Use plt.subplot\n# For each image, put its predicted class from the trained model as its title (use label_map)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-08T15:18:59.500368Z","iopub.execute_input":"2021-07-08T15:18:59.50066Z","iopub.status.idle":"2021-07-08T15:18:59.505545Z","shell.execute_reply.started":"2021-07-08T15:18:59.500632Z","shell.execute_reply":"2021-07-08T15:18:59.504297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# TODO: Write a loop to compute the test predictions and submit it on Kaggle\n# Load data file names\nimages = []\nfor img in glob.glob('/kaggle/input/acc-german-traffic-sign-classification/GTSRB_Challenge/test/*'):\n    images.append(img)\nimages","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-07-08T15:18:59.506846Z","iopub.execute_input":"2021-07-08T15:18:59.507212Z","iopub.status.idle":"2021-07-08T15:18:59.58008Z","shell.execute_reply.started":"2021-07-08T15:18:59.507176Z","shell.execute_reply":"2021-07-08T15:18:59.57931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loc = [x for x in images]\nids = [x.split('/')[-1] for x in images]\ntest_dataset = Dataset(test_loc, ids, transform=transform)\ntest_loader = torch.utils.data.DataLoader(test_dataset, batch_size=4,\n                                          shuffle=False, num_workers=2)","metadata":{"execution":{"iopub.status.busy":"2021-07-08T15:18:59.581197Z","iopub.execute_input":"2021-07-08T15:18:59.581664Z","iopub.status.idle":"2021-07-08T15:18:59.59466Z","shell.execute_reply.started":"2021-07-08T15:18:59.581622Z","shell.execute_reply":"2021-07-08T15:18:59.593829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# submission","metadata":{}},{"cell_type":"code","source":"!rm /kaggle/working/answer.csv","metadata":{"execution":{"iopub.status.busy":"2021-07-08T15:15:40.306202Z","iopub.status.idle":"2021-07-08T15:15:40.30692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Measure test accuracy\ncorrect = 0\noutput = open('answer.csv', 'w+')\noutput.write('Filename,ClassID\\n')\nwith 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\n        for idd, cla in zip(y, preds):\n            output.write(f'{idd},{cla}\\n')\noutput.close()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-07-08T15:20:30.010443Z","iopub.execute_input":"2021-07-08T15:20:30.010803Z","iopub.status.idle":"2021-07-08T15:20:34.882959Z","shell.execute_reply.started":"2021-07-08T15:20:30.010763Z","shell.execute_reply":"2021-07-08T15:20:34.880126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Measure test accuracy\ncorrect = 0\noutput = open('answer.csv', 'w+')\noutput.write('Filename,ClassID\\n')\nwith 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\n        for idd, cla in zip(y, preds):\n            output.write(f'{idd},{cla}\\n')\noutput.close()","metadata":{"execution":{"iopub.status.busy":"2021-06-24T15:26:48.203584Z","iopub.execute_input":"2021-06-24T15:26:48.203925Z","iopub.status.idle":"2021-06-24T15:26:57.954062Z","shell.execute_reply.started":"2021-06-24T15:26:48.203887Z","shell.execute_reply":"2021-06-24T15:26:57.953135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nans = pd.read_csv('./answer.csv')\nans.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-08T15:20:41.606429Z","iopub.execute_input":"2021-07-08T15:20:41.606774Z","iopub.status.idle":"2021-07-08T15:20:41.628042Z","shell.execute_reply.started":"2021-07-08T15:20:41.606745Z","shell.execute_reply":"2021-07-08T15:20:41.626983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = '/kaggle/input/acc-german-traffic-sign-classification/GTSRB_Challenge/test/'\nplt.imshow(plt.imread(test_path + ans['Filename'][0]))\nprint(label_map[ans['ClassID'][0]])","metadata":{"execution":{"iopub.status.busy":"2021-07-08T15:20:43.759755Z","iopub.execute_input":"2021-07-08T15:20:43.760097Z","iopub.status.idle":"2021-07-08T15:20:43.927022Z","shell.execute_reply.started":"2021-07-08T15:20:43.760064Z","shell.execute_reply":"2021-07-08T15:20:43.92611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = 3\nplt.figure()\n\n#subplot(r,c) provide the no. of rows and columns\nf, axarr = plt.subplots(1,N) \n\n# use the created array to output your multiple images. In this case I have stacked 4 images vertically\nfor i, j in zip(range(10, 10+N), range(N)):\n    axarr[j].imshow( plt.imread( test_path + ans['Filename'][i] ) )\n    axarr[j].set_title( label_map[ans['ClassID'][i]] )\n ","metadata":{"execution":{"iopub.status.busy":"2021-07-08T15:20:48.551501Z","iopub.execute_input":"2021-07-08T15:20:48.551826Z","iopub.status.idle":"2021-07-08T15:20:48.916543Z","shell.execute_reply.started":"2021-07-08T15:20:48.551797Z","shell.execute_reply":"2021-07-08T15:20:48.915571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}