{"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 pandas as pd\nimport torch","metadata":{"execution":{"iopub.status.busy":"2022-02-03T14:48:53.181862Z","iopub.execute_input":"2022-02-03T14:48:53.182200Z","iopub.status.idle":"2022-02-03T14:48:53.187376Z","shell.execute_reply.started":"2022-02-03T14:48:53.182159Z","shell.execute_reply":"2022-02-03T14:48:53.186383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fixing the seed for reproducibility\n\nnp.random.seed(42)\ntorch.manual_seed(42)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T14:48:53.189434Z","iopub.execute_input":"2022-02-03T14:48:53.190172Z","iopub.status.idle":"2022-02-03T14:48:53.200452Z","shell.execute_reply.started":"2022-02-03T14:48:53.190093Z","shell.execute_reply":"2022-02-03T14:48:53.199743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creating dataset\n\nfrom torch.utils.data import Dataset, DataLoader\n\nclass MnistDataset(Dataset):\n    def __init__(self, df, transforms, train=True):\n        self.train = train\n        self.transforms = transforms\n        \n        if train:\n            self.labels = df.iloc[:, 0]\n            self.images = df.iloc[:, 1:]\n        else:\n            self.labels = None\n            self.images = df\n        \n    def __len__(self):\n        return len(self.images)\n    \n    def __getitem__(self, idx):\n        data = self.images.iloc[idx, :]\n        data = torch.Tensor(np.array(data)).view(-1, 28, 28)\n        \n        if self.transforms:\n            data = self.transforms(data)\n        \n        if self.train:\n            return data, self.labels[idx]\n        return data","metadata":{"execution":{"iopub.status.busy":"2022-02-03T14:48:53.201585Z","iopub.execute_input":"2022-02-03T14:48:53.201907Z","iopub.status.idle":"2022-02-03T14:48:53.211177Z","shell.execute_reply.started":"2022-02-03T14:48:53.201871Z","shell.execute_reply":"2022-02-03T14:48:53.210282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read data\n\ndf_train = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ndf_test = pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-02-03T14:48:53.213525Z","iopub.execute_input":"2022-02-03T14:48:53.213962Z","iopub.status.idle":"2022-02-03T14:48:56.954322Z","shell.execute_reply.started":"2022-02-03T14:48:53.213917Z","shell.execute_reply":"2022-02-03T14:48:56.953539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.transforms import transforms\n\n# transform = transforms.Normalize((0.5, ), (0.5, ))\n\ntransform = transforms.Compose([\n    transforms.Resize(256),\n    transforms.Normalize((0.5, ), (0.5, ))\n])","metadata":{"execution":{"iopub.status.busy":"2022-02-03T14:48:56.955734Z","iopub.execute_input":"2022-02-03T14:48:56.955976Z","iopub.status.idle":"2022-02-03T14:48:56.961783Z","shell.execute_reply.started":"2022-02-03T14:48:56.955943Z","shell.execute_reply":"2022-02-03T14:48:56.961011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creating dataloaders\n\ntrain_data = MnistDataset(df_train, transform, train=True)\ntest_data = MnistDataset(df_test, transform, train=False)\n\ntrainloader = DataLoader(train_data, batch_size=64, shuffle=True)\ntestloader = DataLoader(test_data, batch_size=64, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T14:48:56.962848Z","iopub.execute_input":"2022-02-03T14:48:56.963505Z","iopub.status.idle":"2022-02-03T14:48:56.972997Z","shell.execute_reply.started":"2022-02-03T14:48:56.963463Z","shell.execute_reply":"2022-02-03T14:48:56.972229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test MnistDataset\n\n%matplotlib inline\n\nimport matplotlib.pyplot as plt\n\nbreakloop_number = 16\nfig = plt.figure(figsize=(8, 8))\n\nfor i, (x, y) in enumerate(train_data):\n    if i == breakloop_number:\n        break\n\n    fig.add_subplot(4, 4, i+1)\n    plt.imshow(x.permute(1, 2, 0))\n    plt.axis(\"off\")\n    plt.title(str(y))\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-03T14:48:56.974232Z","iopub.execute_input":"2022-02-03T14:48:56.974495Z","iopub.status.idle":"2022-02-03T14:48:57.925160Z","shell.execute_reply.started":"2022-02-03T14:48:56.974457Z","shell.execute_reply":"2022-02-03T14:48:57.924420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check number of batches in trainloader\n\nnum_of_batches = 0\nfor x, y in trainloader:\n    num_of_batches += 1\nprint(f\"number of batches = {num_of_batches}\")","metadata":{"execution":{"iopub.status.busy":"2022-02-03T14:48:57.926483Z","iopub.execute_input":"2022-02-03T14:48:57.926861Z","iopub.status.idle":"2022-02-03T14:50:13.444173Z","shell.execute_reply.started":"2022-02-03T14:48:57.926823Z","shell.execute_reply":"2022-02-03T14:50:13.443408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transfer to gpu if it's available\n\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T14:50:13.445552Z","iopub.execute_input":"2022-02-03T14:50:13.445957Z","iopub.status.idle":"2022-02-03T14:50:13.452219Z","shell.execute_reply.started":"2022-02-03T14:50:13.445919Z","shell.execute_reply":"2022-02-03T14:50:13.451513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creating model\n! pip install timm\n\nimport torchvision.models as models\nimport timm\n\nmodel = timm.create_model(\"resnet34\", num_classes=10, in_chans=1, pretrained=True)\n\nmodel.to(device)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T14:50:13.454972Z","iopub.execute_input":"2022-02-03T14:50:13.455262Z","iopub.status.idle":"2022-02-03T14:50:21.938302Z","shell.execute_reply.started":"2022-02-03T14:50:13.455223Z","shell.execute_reply":"2022-02-03T14:50:21.937490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create optimizer and loss function\n\noptimizer = torch.optim.Adam(model.parameters(), lr=0.001)\n\ncriterion = torch.nn.CrossEntropyLoss()","metadata":{"execution":{"iopub.status.busy":"2022-02-03T14:50:21.939669Z","iopub.execute_input":"2022-02-03T14:50:21.939987Z","iopub.status.idle":"2022-02-03T14:50:21.946651Z","shell.execute_reply.started":"2022-02-03T14:50:21.939945Z","shell.execute_reply":"2022-02-03T14:50:21.945637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_correct_predictions(logits, labels):\n    _, predicted = torch.max(logits.data, 1)\n    correct = (predicted == labels).sum().item()\n    \n    return correct","metadata":{"execution":{"iopub.status.busy":"2022-02-03T14:50:21.947640Z","iopub.execute_input":"2022-02-03T14:50:21.948270Z","iopub.status.idle":"2022-02-03T14:50:21.957745Z","shell.execute_reply.started":"2022-02-03T14:50:21.948233Z","shell.execute_reply":"2022-02-03T14:50:21.956911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training loop\n\nfrom tqdm import tqdm\n\nmodel.train()\nepochs = 5\nfor epoch in range(1, epochs + 1):\n    correct_predictions = 0\n    for x, y in tqdm(trainloader):\n        x = x.to(device)\n        y = y.to(device)\n        \n        optimizer.zero_grad()\n                \n        y_predict = model(x)\n                \n        loss = criterion(y_predict, y)\n        loss.backward()\n        \n        optimizer.step()\n        \n        correct_predictions += get_correct_predictions(y_predict, y)\n\n    print(f\"epoch {epoch}: accuracy = {correct_predictions / (len(trainloader) * 64)}\")","metadata":{"execution":{"iopub.status.busy":"2022-02-03T14:50:21.959448Z","iopub.execute_input":"2022-02-03T14:50:21.960114Z","iopub.status.idle":"2022-02-03T15:11:20.248533Z","shell.execute_reply.started":"2022-02-03T14:50:21.960066Z","shell.execute_reply":"2022-02-03T15:11:20.247832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test loop\n\npredictions = []\n\nmodel.eval()\nwith torch.no_grad():\n    correct_predictions = 0\n    for x in tqdm(testloader):\n        x = x.to(device)\n        y = y.to(device)\n\n        y_predict = model(x)\n        _, predicted = torch.max(y_predict.data, 1)\n        \n        predictions.extend(predicted.cpu().numpy())\n        \nsubmission_df = pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")\nsubmission_df['Label'] = predictions\nprint(submission_df.head())\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T15:11:20.249736Z","iopub.execute_input":"2022-02-03T15:11:20.250247Z","iopub.status.idle":"2022-02-03T15:12:39.247126Z","shell.execute_reply.started":"2022-02-03T15:11:20.250208Z","shell.execute_reply":"2022-02-03T15:12:39.246413Z"},"trusted":true},"execution_count":null,"outputs":[]}]}