{"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-05-26T14:38:58.575885Z","iopub.execute_input":"2022-05-26T14:38:58.576995Z","iopub.status.idle":"2022-05-26T14:38:58.589239Z","shell.execute_reply.started":"2022-05-26T14:38:58.576952Z","shell.execute_reply":"2022-05-26T14:38:58.587749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport torch\nimport torch.nn as nn\nimport torch.functional as F","metadata":{"execution":{"iopub.status.busy":"2022-05-26T14:38:58.865676Z","iopub.execute_input":"2022-05-26T14:38:58.866531Z","iopub.status.idle":"2022-05-26T14:39:01.683021Z","shell.execute_reply.started":"2022-05-26T14:38:58.86649Z","shell.execute_reply":"2022-05-26T14:39:01.681897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/digit-recognizer/train.csv')\ntest_df  = pd.read_csv('../input/digit-recognizer/test.csv')\nlabel = train_df['label'].values\ntrain_df.drop(['label'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T14:39:01.685813Z","iopub.execute_input":"2022-05-26T14:39:01.690435Z","iopub.status.idle":"2022-05-26T14:39:09.885293Z","shell.execute_reply.started":"2022-05-26T14:39:01.690324Z","shell.execute_reply":"2022-05-26T14:39:09.883983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\nimport albumentations as A\nimport cv2\nimport time\ntransform = A.Compose([\n    A.Resize(width=224, height=224),\n#     A.HorizontalFlip(p=0.5),\n#     A.RandomBrightnessContrast(p=0.2),\n])\n\narray_list =[]\n\n\ntime1 = time.time()\nn=0\nfor i in range(7000):\n    img = train_df.iloc[i,:].values.reshape(28,28)\n    transformed = transform(image=img.astype(np.uint8))\n    data1transformed_image = transformed[\"image\"].astype(None)\n    data_img = data1transformed_image.reshape(224*224)\n#     pd_df = pd.concat([pd_df,pd.Series(data_img)],axis=1)\n    array_list.append(data_img)\n    gc.collect() \n    n+=1\n    if n>=1000 and  n%1000==0:print(n)\nprint(time.time()-time1)\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T14:40:58.543899Z","iopub.execute_input":"2022-05-26T14:40:58.545545Z","iopub.status.idle":"2022-05-26T15:11:18.196101Z","shell.execute_reply.started":"2022-05-26T14:40:58.545482Z","shell.execute_reply":"2022-05-26T15:11:18.195131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ttf = np.asarray(array_list)\nttf.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-26T15:11:31.682501Z","iopub.execute_input":"2022-05-26T15:11:31.682834Z","iopub.status.idle":"2022-05-26T15:11:32.774437Z","shell.execute_reply.started":"2022-05-26T15:11:31.682789Z","shell.execute_reply":"2022-05-26T15:11:32.773343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def to_OneHotEncoder(y,num_class):       #转换为独热编码\n    return np.eye(num_class)[y]","metadata":{"execution":{"iopub.status.busy":"2022-05-26T15:11:35.019902Z","iopub.execute_input":"2022-05-26T15:11:35.020225Z","iopub.status.idle":"2022-05-26T15:11:35.025524Z","shell.execute_reply.started":"2022-05-26T15:11:35.020192Z","shell.execute_reply":"2022-05-26T15:11:35.024479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df = ttf","metadata":{"execution":{"iopub.status.busy":"2022-05-26T13:26:33.831291Z","iopub.execute_input":"2022-05-26T13:26:33.831525Z","iopub.status.idle":"2022-05-26T13:26:33.836492Z","shell.execute_reply.started":"2022-05-26T13:26:33.831497Z","shell.execute_reply":"2022-05-26T13:26:33.835331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# label[:1200]","metadata":{"execution":{"iopub.status.busy":"2022-05-26T14:36:57.244394Z","iopub.execute_input":"2022-05-26T14:36:57.24464Z","iopub.status.idle":"2022-05-26T14:36:57.24792Z","shell.execute_reply.started":"2022-05-26T14:36:57.244612Z","shell.execute_reply":"2022-05-26T14:36:57.247268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\ny_train = to_OneHotEncoder(label[:7000],10)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T15:11:39.102277Z","iopub.execute_input":"2022-05-26T15:11:39.102599Z","iopub.status.idle":"2022-05-26T15:11:39.192517Z","shell.execute_reply.started":"2022-05-26T15:11:39.102566Z","shell.execute_reply":"2022-05-26T15:11:39.191518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# x_train = ttf/255\n# x_test = test_df/255\n\nx_train_tensor = torch.from_numpy(ttf/255).float()\nx_train_tensor = torch.reshape(x_train_tensor,(-1,1,224,224))\n\n# x_test_tensor = torch.from_numpy(x_test.to_numpy()).float()\n# x_test_tensor = torch.reshape(x_test_tensor,(-1,1,224,224))\n\ny_train = torch.from_numpy(y_train).float()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T15:11:40.068777Z","iopub.execute_input":"2022-05-26T15:11:40.069092Z","iopub.status.idle":"2022-05-26T15:11:42.50363Z","shell.execute_reply.started":"2022-05-26T15:11:40.069063Z","shell.execute_reply":"2022-05-26T15:11:42.502669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_tensor.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-26T14:34:19.933401Z","iopub.execute_input":"2022-05-26T14:34:19.933654Z","iopub.status.idle":"2022-05-26T14:34:19.93857Z","shell.execute_reply.started":"2022-05-26T14:34:19.933627Z","shell.execute_reply":"2022-05-26T14:34:19.937861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader,TensorDataset\n\ntrain_dataset = TensorDataset(x_train_tensor,y_train)\ntrain_dataload = DataLoader(train_dataset,batch_size=100,shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T15:11:47.328493Z","iopub.execute_input":"2022-05-26T15:11:47.328801Z","iopub.status.idle":"2022-05-26T15:11:47.334962Z","shell.execute_reply.started":"2022-05-26T15:11:47.328769Z","shell.execute_reply":"2022-05-26T15:11:47.33339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class AlexNet(nn.Module):\n    def __init__(self,num_classes=1000,init_weights =False):\n        super(AlexNet,self).__init__()\n        self.features = nn.Sequential(\n            nn.Conv2d(1,48,kernel_size=11,stride=4,padding=3), # padding和别人的不同 ,这里手写数字识别通道要改为1，一般RGB为3\n            nn.ReLU(inplace=True),\n            nn.MaxPool2d(kernel_size=3,stride=2),\n            \n            nn.Conv2d(48,128,kernel_size=5,stride=1,padding=2),\n            nn.ReLU(inplace=True),\n            nn.MaxPool2d(kernel_size=3,stride=2),\n            \n            nn.Conv2d(128,192,kernel_size=3,padding=1),\n            nn.ReLU(inplace=True),\n            \n            nn.Conv2d(192, 192, kernel_size=3, padding=1),\n            nn.ReLU(inplace=True),\n        \n            \n            nn.Conv2d(192,128,kernel_size=3,padding=1),\n            nn.ReLU(inplace=True),\n            nn.MaxPool2d(kernel_size=3,stride=2)\n        \n        )\n        self.classifier = nn.Sequential(\n            nn.Dropout(0.5),\n            nn.Linear(128*6*6,2048),\n            nn.ReLU(inplace=True),\n            nn.Dropout(0.5),\n            nn.Linear(2048,2048),\n            nn.ReLU(inplace=True),\n            nn.Linear(2048,num_classes),)\n        if init_weights==True:\n            self._init_weights()\n            \n            \n        \n    def forward(self,x):\n        x = self.features(x)\n        x = torch.flatten(x,start_dim=1)\n        x = self.classifier(x)\n        return x\n    def _init_weights(self): #卷积初始化 w-》kaiming  bias-》0  全连接初始化 0-0.01的正态分布\n        for m in self.modules():\n            if isinstance(m,nn.Conv2d):\n                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')\n                if m.bias is not None:\n                    nn.init.constant_(m.bias,0)\n            if isinstance(m,nn.Linear):\n                nn.init.normal_(m.weight,0,0.01)\n                nn.init.constant_(m.bias,0)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T15:11:49.218678Z","iopub.execute_input":"2022-05-26T15:11:49.219007Z","iopub.status.idle":"2022-05-26T15:11:49.235681Z","shell.execute_reply.started":"2022-05-26T15:11:49.218975Z","shell.execute_reply":"2022-05-26T15:11:49.234694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = AlexNet(num_classes=10,init_weights=True)\nmodel.to(device) #放入GPU训练\n\nepoch =23\nloss_fn = nn.CrossEntropyLoss() #交叉熵损失函数\noptimizer = torch.optim.Adam(model.parameters(),lr=0.0001) #Adam优化器","metadata":{"execution":{"iopub.status.busy":"2022-05-26T15:13:32.457798Z","iopub.execute_input":"2022-05-26T15:13:32.458082Z","iopub.status.idle":"2022-05-26T15:13:32.70803Z","shell.execute_reply.started":"2022-05-26T15:13:32.45805Z","shell.execute_reply":"2022-05-26T15:13:32.706795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train(train_dataloader,model,loss_fn,optimizer):\n    model.train()\n    running_loss =0\n    for batch,(x,y) in enumerate(train_dataloader):\n        x = x.to(device)\n        y = y.to(device)\n        y_hat = model(x)\n        loss = loss_fn(y_hat,y)\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        \n        running_loss += loss.item()\n        rate = (batch+1) /len(train_dataload)\n        \n        a= \"*\"*int(rate*50)\n        b= \".\"*int((1-rate)*50)\n        print(\"\\rtrain loss: {:^3.0f}%[{}->{}]{:.3f}\\n\".format(int(rate*100),a,b,loss),end=\"\" )","metadata":{"execution":{"iopub.status.busy":"2022-05-26T15:13:33.254791Z","iopub.execute_input":"2022-05-26T15:13:33.255082Z","iopub.status.idle":"2022-05-26T15:13:33.26407Z","shell.execute_reply.started":"2022-05-26T15:13:33.255051Z","shell.execute_reply":"2022-05-26T15:13:33.262625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test(img_tensorData,model):\n    model.eval()\n    with torch.no_grad():\n        prediction = model(img_tensorData)\n        \n    return prediction.cpu().numpy()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T15:13:35.732923Z","iopub.execute_input":"2022-05-26T15:13:35.733199Z","iopub.status.idle":"2022-05-26T15:13:35.739178Z","shell.execute_reply.started":"2022-05-26T15:13:35.733168Z","shell.execute_reply":"2022-05-26T15:13:35.737911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(epoch):\n    print(f\"目前正在运行第{i+1}个epoch\\n\")\n    train(train_dataload,model,loss_fn,optimizer)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T15:13:39.750375Z","iopub.execute_input":"2022-05-26T15:13:39.750823Z","iopub.status.idle":"2022-05-26T15:14:30.793642Z","shell.execute_reply.started":"2022-05-26T15:13:39.750789Z","shell.execute_reply":"2022-05-26T15:14:30.792116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model,'./checkpoint_model_epoch_1.pth.tar')","metadata":{"execution":{"iopub.status.busy":"2022-05-26T15:24:17.33408Z","iopub.execute_input":"2022-05-26T15:24:17.334696Z","iopub.status.idle":"2022-05-26T15:24:17.472645Z","shell.execute_reply.started":"2022-05-26T15:24:17.334645Z","shell.execute_reply":"2022-05-26T15:24:17.471605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model, './net.pkl')","metadata":{"execution":{"iopub.status.busy":"2022-05-26T15:58:25.101901Z","iopub.execute_input":"2022-05-26T15:58:25.102172Z","iopub.status.idle":"2022-05-26T15:58:25.261309Z","shell.execute_reply.started":"2022-05-26T15:58:25.102141Z","shell.execute_reply":"2022-05-26T15:58:25.260046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1 = torch.load('./net.pkl')","metadata":{"execution":{"iopub.status.busy":"2022-05-26T16:05:27.522684Z","iopub.execute_input":"2022-05-26T16:05:27.523048Z","iopub.status.idle":"2022-05-26T16:05:27.596532Z","shell.execute_reply.started":"2022-05-26T16:05:27.523007Z","shell.execute_reply":"2022-05-26T16:05:27.595611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#测试\nn=2\ndata1 = ttf[n]\n\nplt.imshow(data1.reshape(224,224),cmap='gray')\nprint(label[n])\n\ntest_pic = torch.tensor(ttf[n].reshape(1,1,224,224)).float()\ntest(test_pic.cuda(),model1).argmax()\n","metadata":{"execution":{"iopub.status.busy":"2022-05-26T16:05:41.759028Z","iopub.execute_input":"2022-05-26T16:05:41.759804Z","iopub.status.idle":"2022-05-26T16:05:41.988911Z","shell.execute_reply.started":"2022-05-26T16:05:41.759771Z","shell.execute_reply":"2022-05-26T16:05:41.988001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport os\nos.chdir('/kaggle/working')\nprint(os.getcwd())\nprint(os.listdir(\"/kaggle/working\"))\nfrom IPython.display import FileLink\nFileLink('./net.pkl')","metadata":{"execution":{"iopub.status.busy":"2022-05-26T15:58:53.19077Z","iopub.execute_input":"2022-05-26T15:58:53.191082Z","iopub.status.idle":"2022-05-26T15:58:53.205Z","shell.execute_reply.started":"2022-05-26T15:58:53.191049Z","shell.execute_reply":"2022-05-26T15:58:53.203789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}