{"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 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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-26T12:45:57.480563Z","iopub.execute_input":"2022-09-26T12:45:57.480932Z","iopub.status.idle":"2022-09-26T12:45:57.489095Z","shell.execute_reply.started":"2022-09-26T12:45:57.480902Z","shell.execute_reply":"2022-09-26T12:45:57.487967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-26T12:45:57.498400Z","iopub.execute_input":"2022-09-26T12:45:57.499222Z","iopub.status.idle":"2022-09-26T12:45:59.092203Z","shell.execute_reply.started":"2022-09-26T12:45:57.499187Z","shell.execute_reply":"2022-09-26T12:45:59.091202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-26T12:45:59.094133Z","iopub.execute_input":"2022-09-26T12:45:59.095154Z","iopub.status.idle":"2022-09-26T12:46:00.291612Z","shell.execute_reply.started":"2022-09-26T12:45:59.095116Z","shell.execute_reply":"2022-09-26T12:46:00.290446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# conver to 28*28*1 matrix\ndata = train_data.to_numpy()\nnp.random.shuffle(data)\n#data.shape\nlabel = []\nimage = []\nfor i in data:\n    label.append(np.array(i[0]))\n    image.append(np.array(i[1:]).reshape((28,28)))\nlabels=np.array(label).reshape((len(label),1))\nimages=np.expand_dims(np.array(image), axis=-1).astype(np.float32)\ntrain_images = images[:int(42000*0.8)]\ntrain_labels = labels[:int(42000*0.8)]\ndev_images = images[int(42000*0.8):]\ndev_labels = labels[int(42000*0.8):]\nprint(train_labels.shape)\nprint(train_images.shape)\nprint(dev_labels.shape)\nprint(dev_images.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T12:46:00.293366Z","iopub.execute_input":"2022-09-26T12:46:00.293795Z","iopub.status.idle":"2022-09-26T12:46:01.316810Z","shell.execute_reply.started":"2022-09-26T12:46:00.293753Z","shell.execute_reply":"2022-09-26T12:46:01.315895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = test_data.to_numpy()\n#data.shape\nimage = []\nfor i in data:\n    image.append(np.array(i).reshape((28,28)))\ntest_images=np.expand_dims(np.array(image), axis=-1).astype(np.float32)\ntest_labels = labels[:len(test_images)]\nprint(test_images.shape) \nprint(test_labels.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T13:08:16.564080Z","iopub.execute_input":"2022-09-26T13:08:16.564506Z","iopub.status.idle":"2022-09-26T13:08:16.780720Z","shell.execute_reply.started":"2022-09-26T13:08:16.564471Z","shell.execute_reply":"2022-09-26T13:08:16.779577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# vgg model \nimport torch\nimport torch.nn as nn\n\n\nclass VGG(nn.Module):\n    def __init__(self, features, num_classes=10):\n        super(VGG, self).__init__()\n        self.features = features\n        self.avgpool = nn.AdaptiveAvgPool2d((7, 7))\n        self.classifier = nn.Sequential(\n            nn.Linear(128 * 7 * 7, 4096),   # 512*7*7 --> 4096\n            nn.ReLU(True),\n            nn.Dropout(),\n            nn.Linear(4096, 4096),          # 4096 --> 4096\n            nn.ReLU(True),\n            nn.Dropout(),\n            nn.Linear(4096, num_classes),   # 4096 --> 1000\n        )\n        \n    def forward(self, x):\n        x = self.features(x)\n        x = self.avgpool(x)\n        x = torch.flatten(x, 1)\n        x = self.classifier(x)\n        return x\n        pass \n\n    pass\n\n\ndef make_layers(cfg, batch_norm: bool = False) -> nn.Sequential:\n    layers: List[nn.Module] = []\n    in_channels = 1\n    for v in cfg:\n        if v == \"M\":\n            layers += [nn.MaxPool2d(kernel_size=2, stride=2)]\n        else:\n            \n            conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)\n            if batch_norm:\n                layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]\n            else:\n                layers += [conv2d, nn.ReLU(inplace=True)]\n            in_channels = v\n    return nn.Sequential(*layers)\n\n\n\n\ndef _vgg(cfg, batch_norm):\n    '''\n    通用网络构造器，主要实现网络模型生成，以及预训练权重的导入\n    '''\n    #if pretrained:\n     #   kwargs['init_weights'] = False\n    model = VGG(make_layers(cfgs[cfg], batch_norm=batch_norm))\n\n    #if pretrained:\n     #   state_dict = load_state_dict_from_url(model_urls[arch],\n     #                                         progress=progress)\n      #  model.load_state_dict(state_dict)\n    return model\n\ncfgs= {\n    \"A\": [64, \"M\", 128, \"M\"],\n#     \"A\": [64, \"M\", 128, \"M\", 256, 256, \"M\", 512, 512, \"M\", 512, 512, \"M\"],\n    \"B\": [64, 64, \"M\", 128, 128, \"M\", 256, 256, \"M\", 512, 512, \"M\", 512, 512, \"M\"],\n    \"D\": [64, 64, \"M\", 128, 128, \"M\", 256, 256, 256, \"M\", 512, 512, 512, \"M\", 512, 512, 512, \"M\"],\n    \"E\": [64, 64, \"M\", 128, 128, \"M\", 256, 256, 256, 256, \"M\", 512, 512, 512, 512, \"M\", 512, 512, 512, 512, \"M\"],\n}\n    \n","metadata":{"execution":{"iopub.status.busy":"2022-09-26T12:46:01.613390Z","iopub.execute_input":"2022-09-26T12:46:01.614070Z","iopub.status.idle":"2022-09-26T12:46:01.629893Z","shell.execute_reply.started":"2022-09-26T12:46:01.614031Z","shell.execute_reply":"2022-09-26T12:46:01.628906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# resnet model","metadata":{"execution":{"iopub.status.busy":"2022-09-26T12:46:01.631400Z","iopub.execute_input":"2022-09-26T12:46:01.631840Z","iopub.status.idle":"2022-09-26T12:46:01.644771Z","shell.execute_reply.started":"2022-09-26T12:46:01.631803Z","shell.execute_reply":"2022-09-26T12:46:01.643935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data loader\nfrom torchvision import transforms, datasets, utils\ndata_transform = {\n    'train': transforms.Compose([#transforms.RandomHorizontalFlip(0.5),\n                                 transforms.ToTensor(),\n                                 transforms.Normalize((0.5),(0.5))]),\n    \n    'test': transforms.Compose([ transforms.ToTensor(),\n                                 transforms.Normalize((0.5),(0.5))])\n}\n\nfrom torch.utils.data import Dataset, DataLoader\n\nclass MyDataset(Dataset):\n    def __init__(self, images, labels, transform=None):\n        self.x_data = images\n        self.y_data = labels\n        self.transform = transform\n        self.length = len(self.y_data)\n        pass \n    def __getitem__(self, index):\n        if self.transform is not None:\n            \n            return self.transform(self.x_data[index]), self.y_data[index]\n        else:\n            return self.x_data[index], self.y_data[index]\n            \n    \n    def __len__(self):\n        return self.length\n    \n    pass\n\ntrain_dataset = MyDataset(train_images, train_labels,transform=data_transform['train'])\ntrain_loader = DataLoader(dataset=train_dataset,\n                         batch_size=2048,\n                         shuffle=True,\n                         num_workers=0)\ndev_dataset = MyDataset(dev_images, dev_labels, transform=data_transform['test'])\ndev_loader = DataLoader(dataset=dev_dataset,\n                       batch_size=2048,\n                       shuffle=False,\n                       num_workers=0)\nval_num = len(dev_dataset)\nprint(len(train_dataset))\nprint(len(dev_dataset))\n","metadata":{"execution":{"iopub.status.busy":"2022-09-26T12:46:01.646422Z","iopub.execute_input":"2022-09-26T12:46:01.646887Z","iopub.status.idle":"2022-09-26T12:46:01.660071Z","shell.execute_reply.started":"2022-09-26T12:46:01.646847Z","shell.execute_reply":"2022-09-26T12:46:01.659060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train\nimport torch.optim as optim\nfrom tqdm import tqdm\nDEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nbest_model = None\n\ndef train(net, batch_size=64, epoches=40, LR = 0.001):\n    net.to(DEVICE)\n    lost_function = nn.CrossEntropyLoss()\n    optimizer = optim.Adam(net.parameters(), lr=LR)\n    best_acc=0.0\n    for epoch in range(epoches):\n        net.train()\n        running_loss = 0.0\n        \n        for step, data in tqdm(enumerate(train_loader)):\n            \n            # 迭代训练\n            images, labels = data\n            optimizer.zero_grad()\n            outputs = net(images.to(DEVICE))\n            loss = lost_function(outputs, labels.squeeze(dim=1).to(DEVICE))\n            loss.backward()\n            optimizer.step()\n            running_loss += loss.item()\n            \n        # 验证\n        net.eval()\n        acc = 0.0\n        with torch.no_grad():\n\n            for val_data in dev_loader:\n                val_image, val_label = val_data\n                outputs = net(val_image.to(DEVICE))\n                predict_y = torch.max(outputs, dim=1)[1]\n                acc += (predict_y==val_label.squeeze(dim=1).to(DEVICE)).sum().item()\n            val_acc = acc/val_num\n\n            if val_acc > best_acc:\n                best_acc = val_acc\n                best_model=net\n            print('[epoch %d] running loss: %.3f test accuracy: %.3f \\n' %\n                 (epoch+1, running_loss/step, val_acc))\n                \n                \n            #可视化过程\n            \n    #模型保存\n    print('Train finished!\\n Best acc is %.3f'%(best_acc))\n    torch.save(net, 'last_vgg')\n    torch.save(best_model,'best_vgg')\n    \nnet = VGG(make_layers(cfgs['A'],True))\ntrain(net)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T12:54:22.445670Z","iopub.execute_input":"2022-09-26T12:54:22.446035Z","iopub.status.idle":"2022-09-26T12:57:38.469499Z","shell.execute_reply.started":"2022-09-26T12:54:22.446003Z","shell.execute_reply":"2022-09-26T12:57:38.468041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = MyDataset(test_images, test_labels, transform=data_transform['test'])\ntest_loader = DataLoader(dataset=test_dataset,\n                       batch_size=2048,\n                       shuffle=False,\n                       num_workers=0)\nresult = None\nwith torch.no_grad():\n    for test_data in test_loader:\n        test_image,a  = test_data\n        outputs = net(test_image.to(DEVICE))\n        predict_y = torch.max(outputs, dim=1)[1]\n        \n        if result is None:\n            result = predict_y\n        else:\n            result = torch.cat((result, predict_y),dim=0)\n            \nresult=result.to('cpu').numpy()\nprint(len(result))\n\n    \n\n                               \n                               \n        \n    \n\n        \n    \n\n  ","metadata":{"execution":{"iopub.status.busy":"2022-09-26T13:36:30.569034Z","iopub.execute_input":"2022-09-26T13:36:30.569417Z","iopub.status.idle":"2022-09-26T13:36:32.113223Z","shell.execute_reply.started":"2022-09-26T13:36:30.569385Z","shell.execute_reply":"2022-09-26T13:36:32.112124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId':range(1,len(result)+1),'Label':result})\nsubmission.to_csv('submission_mnist.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T13:39:21.908973Z","iopub.execute_input":"2022-09-26T13:39:21.909388Z","iopub.status.idle":"2022-09-26T13:39:21.943487Z","shell.execute_reply.started":"2022-09-26T13:39:21.909352Z","shell.execute_reply":"2022-09-26T13:39:21.942581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict\n\n    \n","metadata":{"execution":{"iopub.status.busy":"2022-09-26T12:46:08.012228Z","iopub.execute_input":"2022-09-26T12:46:08.012814Z","iopub.status.idle":"2022-09-26T12:46:08.022178Z","shell.execute_reply.started":"2022-09-26T12:46:08.012778Z","shell.execute_reply":"2022-09-26T12:46:08.021541Z"},"trusted":true},"execution_count":null,"outputs":[]}]}