{"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\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torchvision import datasets, transforms, models\nfrom torch.utils.data import DataLoader, Dataset\nimport time\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import tqdm\n\n# You can write up to 20GB 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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2022-01-08T07:49:40.350658Z","iopub.execute_input":"2022-01-08T07:49:40.350927Z","iopub.status.idle":"2022-01-08T07:49:40.357469Z","shell.execute_reply.started":"2022-01-08T07:49:40.350897Z","shell.execute_reply":"2022-01-08T07:49:40.356566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cpu'\nif torch.cuda.is_available(): # 若想使用 cuda 且可以使用 cuda\n    device = 'cuda'\nprint(f'Using {device} for training.')","metadata":{"execution":{"iopub.status.busy":"2022-01-08T07:49:40.359260Z","iopub.execute_input":"2022-01-08T07:49:40.359648Z","iopub.status.idle":"2022-01-08T07:49:40.369103Z","shell.execute_reply.started":"2022-01-08T07:49:40.359587Z","shell.execute_reply":"2022-01-08T07:49:40.368120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EPOCH=30\nLR=0.01\nBATCH=64\nSTEP=5","metadata":{"execution":{"iopub.status.busy":"2022-01-08T07:49:40.370492Z","iopub.execute_input":"2022-01-08T07:49:40.370841Z","iopub.status.idle":"2022-01-08T07:49:40.379745Z","shell.execute_reply.started":"2022-01-08T07:49:40.370806Z","shell.execute_reply":"2022-01-08T07:49:40.379058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nsubmit_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\nlabel=train_data['label']\nimg=train_data.iloc[:,1:]\nimg=img.values.reshape(-1,28,28,1).astype('uint8')\n\nMEAN=0.5\nSTD=0.5\n\n# print(MEAN,STD)\n\ndata=[]\n\nfor i in range(len(img)):\n    data.append((img[i], label[i]))\n\n# bigger = img>255 \n# img[img[bigger]]=255\n\n# plt.imshow(img[0])\n# print(img[0].shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-08T07:49:40.382041Z","iopub.execute_input":"2022-01-08T07:49:40.382627Z","iopub.status.idle":"2022-01-08T07:49:44.043023Z","shell.execute_reply.started":"2022-01-08T07:49:40.382591Z","shell.execute_reply":"2022-01-08T07:49:44.042285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MNISTDataset(Dataset):\n    def __init__(self, data, transform=None, train=True):\n        self.trans=transform\n        self.img=[x[0] for x in data]\n        self.train=train\n        if train == True:\n            self.label=[x[1] for x in data]\n\n    def __len__(self):\n        return len(self.img)\n\n    def __getitem__(self, index):\n        img=self.trans(self.img[index])\n        if self.train == True:\n            return img, self.label[index]\n        return img, 0\n    ","metadata":{"execution":{"iopub.status.busy":"2022-01-08T07:49:44.045395Z","iopub.execute_input":"2022-01-08T07:49:44.045645Z","iopub.status.idle":"2022-01-08T07:49:44.052571Z","shell.execute_reply.started":"2022-01-08T07:49:44.045609Z","shell.execute_reply":"2022-01-08T07:49:44.051893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transform = transforms.Compose([\n    transforms.ToPILImage(),\n    transforms.RandomAffine(degrees=(-45,45), translate=(0.1, 0.1)),\n    transforms.ColorJitter(brightness=0.5,contrast=0.5),\n    transforms.ToTensor(),\n    transforms.Normalize(MEAN,STD)\n])\ntest_transform = transforms.Compose([\n    transforms.ToPILImage(),\n    transforms.ToTensor(),\n    transforms.Normalize(MEAN,STD)\n])","metadata":{"execution":{"iopub.status.busy":"2022-01-08T07:49:44.054044Z","iopub.execute_input":"2022-01-08T07:49:44.054449Z","iopub.status.idle":"2022-01-08T07:49:44.064465Z","shell.execute_reply.started":"2022-01-08T07:49:44.054415Z","shell.execute_reply":"2022-01-08T07:49:44.063784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dataset, test_dataset=train_test_split(data, train_size=0.9,random_state=int(time.time()%100))\n# # print(len(train_dataset),len(test_dataset))\n\n# train_set=MNISTDataset(train_dataset,transform=train_transform, train=True)\n# test_set=MNISTDataset(test_dataset,transform=test_transform, train=True)\n\n# train_loader=DataLoader(train_set,batch_size=BATCH,shuffle=True)\n# test_loader=DataLoader(test_set, batch_size=BATCH*2)\n\n# print(train_set[0][1])\n# plt.imshow(train_set[0][0].reshape(28,28,1),cmap='Greys')\n","metadata":{"execution":{"iopub.status.busy":"2022-01-08T07:49:44.065716Z","iopub.execute_input":"2022-01-08T07:49:44.066043Z","iopub.status.idle":"2022-01-08T07:49:44.072762Z","shell.execute_reply.started":"2022-01-08T07:49:44.065987Z","shell.execute_reply":"2022-01-08T07:49:44.071991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train(model, epoch, input_data, optimizer, criterion,lr_scheduler):\n    train_acc, train_loss=[],[]\n    test_acc, test_loss=[],[]\n    best_loss=1.0\n    best_acc=0\n\n    for i in range(epoch):\n        print('----------Epoch:{}----------'.format(i+1))\n        \n        train_dataset, test_dataset=train_test_split(input_data, train_size=0.9,random_state=i)\n\n        train_set=MNISTDataset(train_dataset,transform=train_transform)\n        test_set=MNISTDataset(test_dataset,transform=test_transform)\n\n        train_loader=DataLoader(train_set,batch_size=BATCH,shuffle=True,num_workers=2)\n        test_loader=DataLoader(test_set, batch_size=BATCH*2,num_workers=2)\n        \n        local_acc, local_loss = 0,0\n        total_num=0\n        model.train()\n\n        for data, label in tqdm(train_loader):\n            data, label = data.to(device), label.to(device)\n#             print(data.shape)\n\n            output=model(data)\n\n            loss=criterion(output, label)\n            local_loss+=loss.item()\n\n            optimizer.zero_grad()\n            loss.backward()\n            optimizer.step()\n            \n            pred=output.argmax(1)\n            acc=pred.eq(label).float().sum(0) / label.size(0)\n            local_acc+=acc\n\n        train_acc.append(local_acc/len(train_loader))\n        train_loss.append(local_loss/len(train_loader))\n        \n\n        model.eval()\n        local_acc, local_loss = 0,0\n        with torch.no_grad():\n            for data, label in tqdm(test_loader):\n                data, label = data.to(device), label.to(device)\n\n                output=model(data)\n\n                loss=criterion(output, label)\n                local_loss+=loss.item()\n\n                pred=output.argmax(1)\n                acc=pred.eq(label).float().sum(0) / label.size(0)\n                local_acc+=acc\n        \n        test_acc.append(local_acc/len(test_loader))\n        test_loss.append(local_loss/len(test_loader))\n        \n        print('Learning rate',optimizer.param_groups[0]['lr'])\n        lr_scheduler.step()\n        \n        # Save the best model\n        if test_loss[i]<best_loss:\n            best_loss=test_loss[i]\n            torch.save(model,'best_model.pt')\n        if test_acc[i]>best_acc:\n            best_acc=test_acc[i]\n        \n        print('Train Loss {} Accuracy {}'.format(train_loss[i],train_acc[i]))\n        print('Test Loss {} Accuracy {}'.format(test_loss[i],test_acc[i]))\n        \n    print('Best accuracy',best_acc.item())\n        \n    return train_acc, train_loss, test_acc, test_loss","metadata":{"execution":{"iopub.status.busy":"2022-01-08T07:49:44.075087Z","iopub.execute_input":"2022-01-08T07:49:44.075282Z","iopub.status.idle":"2022-01-08T07:49:44.092168Z","shell.execute_reply.started":"2022-01-08T07:49:44.075251Z","shell.execute_reply":"2022-01-08T07:49:44.091042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=models.resnet18(pretrained=False)\nmodel.conv1=nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\nmodel.fc=nn.Linear(512,10)\nmodel.to(device)\n\n# print(model)","metadata":{"execution":{"iopub.status.busy":"2022-01-08T07:49:44.094300Z","iopub.execute_input":"2022-01-08T07:49:44.094735Z","iopub.status.idle":"2022-01-08T07:49:44.289032Z","shell.execute_reply.started":"2022-01-08T07:49:44.094697Z","shell.execute_reply":"2022-01-08T07:49:44.288310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion=nn.CrossEntropyLoss()\ncriterion.to(device)\noptimizer=torch.optim.Adam(model.parameters(),lr=LR)\nlr_scheduler=torch.optim.lr_scheduler.StepLR(optimizer, STEP, gamma=0.1)\n# lr_scheduler =torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience = 15, verbose = True, factor=0.5)","metadata":{"execution":{"iopub.status.busy":"2022-01-08T07:49:44.290628Z","iopub.execute_input":"2022-01-08T07:49:44.290885Z","iopub.status.idle":"2022-01-08T07:49:44.296338Z","shell.execute_reply.started":"2022-01-08T07:49:44.290849Z","shell.execute_reply":"2022-01-08T07:49:44.295679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_acc, train_loss, test_acc, test_loss = train(model, EPOCH, data, optimizer, criterion, lr_scheduler)\n\nplt.figure(figsize=(10, 7))\nplt.plot(train_acc, color='green', label='Train Accuracy')\nplt.plot(train_loss, color='red', label='Train Loss')\nplt.plot(test_acc, color='black', label='Test Accuracy')\nplt.plot(test_loss, color='grey', label='Test Loss')\nplt.legend()\nplt.title('Accuracy and Loss')\nplt.ylabel('Epoch')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-08T07:49:44.297949Z","iopub.execute_input":"2022-01-08T07:49:44.298442Z","iopub.status.idle":"2022-01-08T08:02:50.822560Z","shell.execute_reply.started":"2022-01-08T07:49:44.298404Z","shell.execute_reply":"2022-01-08T08:02:50.821127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_model=torch.load('./best_model.pt')\ntest_img=submit_data.values.reshape(-1,28,28,1).astype('uint8')\n\nsubmit_dataset=[]\nfor i in range(len(test_img)):\n    submit_dataset.append((test_img[i], 0))\n\ntestset = MNISTDataset(submit_dataset,transform=test_transform,train=False)\n# plt.imshow(testset[0][0].reshape(28,28,1),cmap='Greys')\n\nsubmit_loader=DataLoader(testset, batch_size=BATCH*2)\n\nmodel.eval()\nresult=[]\nwith torch.no_grad():\n    for data, label in tqdm(submit_loader):\n        data = data.to(device)\n        \n        output=model(data)\n\n        pred=output.argmax(1)\n        result.extend(pred.tolist())\n# print(result[:5])\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-08T08:02:50.824216Z","iopub.execute_input":"2022-01-08T08:02:50.824464Z","iopub.status.idle":"2022-01-08T08:02:57.335046Z","shell.execute_reply.started":"2022-01-08T08:02:50.824428Z","shell.execute_reply":"2022-01-08T08:02:57.334229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsubmit_df['Label']=result\nsubmit_df.to_csv('submission.csv',index=False)\n\nsubmit_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-08T08:02:57.336355Z","iopub.execute_input":"2022-01-08T08:02:57.336679Z","iopub.status.idle":"2022-01-08T08:02:57.466114Z","shell.execute_reply.started":"2022-01-08T08:02:57.336641Z","shell.execute_reply":"2022-01-08T08:02:57.464677Z"},"trusted":true},"execution_count":null,"outputs":[]}]}