{"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":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -V","metadata":{"execution":{"iopub.status.busy":"2022-09-09T14:57:53.052232Z","iopub.execute_input":"2022-09-09T14:57:53.052553Z","iopub.status.idle":"2022-09-09T14:57:54.028594Z","shell.execute_reply.started":"2022-09-09T14:57:53.052523Z","shell.execute_reply":"2022-09-09T14:57:54.027747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvcc --version","metadata":{"execution":{"iopub.status.busy":"2022-09-09T14:57:54.031450Z","iopub.execute_input":"2022-09-09T14:57:54.031826Z","iopub.status.idle":"2022-09-09T14:57:55.017308Z","shell.execute_reply.started":"2022-09-09T14:57:54.031782Z","shell.execute_reply":"2022-09-09T14:57:55.016374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\n!pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/1.8.1/MindSpore/gpu/x86_64/cuda-10.1/mindspore_gpu-1.8.1-cp37-cp37m-linux_x86_64.whl --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple\n!pip install mindvision\n!pip install -U opencv-python","metadata":{"execution":{"iopub.status.busy":"2022-09-09T14:57:55.019560Z","iopub.execute_input":"2022-09-09T14:57:55.019839Z","iopub.status.idle":"2022-09-09T14:59:38.538019Z","shell.execute_reply.started":"2022-09-09T14:57:55.019807Z","shell.execute_reply":"2022-09-09T14:59:38.536810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nfrom PIL import Image\nimport os\nfrom mindspore.dataset import GeneratorDataset,transforms,vision\nfrom mindspore import context\nimport mindspore.nn as nn\nimport mindspore.ops as ops\nimport mindspore as ms\nfrom mindspore.nn import Accuracy\nfrom mindvision.engine.callback import LossMonitor\nimport mindspore.nn as nn\nfrom mindvision.engine.callback import LossMonitor\nimport mindspore as ms\n\nfrom keras.utils import np_utils\n\n\n\ncontext.set_context(mode=context.PYNATIVE_MODE, device_target=\"GPU\")","metadata":{"execution":{"iopub.status.busy":"2022-09-09T14:59:38.539915Z","iopub.execute_input":"2022-09-09T14:59:38.540300Z","iopub.status.idle":"2022-09-09T14:59:45.425193Z","shell.execute_reply.started":"2022-09-09T14:59:38.540259Z","shell.execute_reply":"2022-09-09T14:59:45.424220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd_train = pd.read_csv('../input/digit-recognizer/train.csv')\npd_test = pd.read_csv('../input/digit-recognizer/test.csv')\n\nx_train = pd_train.drop(['label'], axis=1)\ny_train = pd_train['label']\nx_test = pd_test\n\nx_train = x_train / 255.0\nx_test = x_test / 255.0","metadata":{"execution":{"iopub.status.busy":"2022-09-09T14:59:45.428107Z","iopub.execute_input":"2022-09-09T14:59:45.428364Z","iopub.status.idle":"2022-09-09T14:59:50.361232Z","shell.execute_reply.started":"2022-09-09T14:59:45.428337Z","shell.execute_reply":"2022-09-09T14:59:50.360223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, Y_train, Y_val = train_test_split(x_train, y_train, test_size=0.2, shuffle=True, stratify=y_train, random_state=34)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T14:59:50.362887Z","iopub.execute_input":"2022-09-09T14:59:50.363304Z","iopub.status.idle":"2022-09-09T14:59:50.706881Z","shell.execute_reply.started":"2022-09-09T14:59:50.363258Z","shell.execute_reply":"2022-09-09T14:59:50.706083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.array(X_train).reshape(-1,28,28,1)\nX_val = np.array(X_val).reshape(-1,28,28,1)\nY_train = np.array(Y_train).astype(np.int64)\nY_val = np.array(Y_val).astype(np.int64)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T14:59:50.708281Z","iopub.execute_input":"2022-09-09T14:59:50.708631Z","iopub.status.idle":"2022-09-09T14:59:50.807898Z","shell.execute_reply.started":"2022-09-09T14:59:50.708590Z","shell.execute_reply":"2022-09-09T14:59:50.807120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Transform = transforms.Compose(\n    [\n    vision.ToTensor(),\n    ])","metadata":{"execution":{"iopub.status.busy":"2022-09-09T14:59:50.809742Z","iopub.execute_input":"2022-09-09T14:59:50.810260Z","iopub.status.idle":"2022-09-09T14:59:50.815042Z","shell.execute_reply.started":"2022-09-09T14:59:50.810222Z","shell.execute_reply":"2022-09-09T14:59:50.813937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class GetData:\n    def __init__(self,Images, Labels,Transform):\n        \n        self.images = Images\n        self.labels = Labels   \n        self.transform = Transform\n        \n    def __len__(self):\n        return len(self.images)\n\n    def __getitem__(self, index):       \n        x=np.array(self.images[index])\n        x=self.transform(x)\n        return x,ms.Tensor(self.labels[index])","metadata":{"execution":{"iopub.status.busy":"2022-09-09T14:59:50.816480Z","iopub.execute_input":"2022-09-09T14:59:50.816943Z","iopub.status.idle":"2022-09-09T14:59:50.825766Z","shell.execute_reply.started":"2022-09-09T14:59:50.816813Z","shell.execute_reply":"2022-09-09T14:59:50.824985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH = 64\n\n\ntrainset = GetData(X_train, Y_train, Transform)\ntr_set = GeneratorDataset(trainset,[\"image\", \"label\"])\ntr_set = tr_set.batch(BATCH, drop_remainder=True)\n\nvalset = GetData(X_val, Y_val, Transform)\nval_set = GeneratorDataset(valset,[\"image\", \"label\"])\nval_set = val_set.batch(BATCH, drop_remainder=True)\n\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-09T14:59:50.826817Z","iopub.execute_input":"2022-09-09T14:59:50.827095Z","iopub.status.idle":"2022-09-09T14:59:50.838847Z","shell.execute_reply.started":"2022-09-09T14:59:50.827071Z","shell.execute_reply":"2022-09-09T14:59:50.838211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class LeNet5(nn.Cell):\n    \"\"\"\n    LeNet-5\n    \"\"\"\n    def __init__(self, num_class=10, num_channel=1):\n        super(LeNet5, self).__init__()\n        self.conv1 = nn.Conv2d(num_channel, 6, 5, pad_mode='valid')\n        self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')\n        self.fc1 = nn.Dense(256, 120)\n        self.fc2 = nn.Dense(120, 84)\n        self.fc3 = nn.Dense(84, num_class)\n        self.relu = nn.ReLU()\n        self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)\n        self.flatten = nn.Flatten()\n\n    def construct(self, x):\n        x = self.conv1(x)\n        x = self.relu(x)\n        x = self.max_pool2d(x)\n        x = self.conv2(x)\n        x = self.relu(x)\n        x = self.max_pool2d(x)\n        x = self.flatten(x)\n        x = self.fc1(x)\n        x = self.relu(x)\n        x = self.fc2(x)\n        x = self.relu(x)\n        x = self.fc3(x)\n        return x\n\nnetwork = LeNet5(num_class=10)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-09T14:59:50.840194Z","iopub.execute_input":"2022-09-09T14:59:50.840720Z","iopub.status.idle":"2022-09-09T14:59:50.869945Z","shell.execute_reply.started":"2022-09-09T14:59:50.840684Z","shell.execute_reply":"2022-09-09T14:59:50.869230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.826075,"end_time":"2022-09-07T09:04:54.115666","exception":false,"start_time":"2022-09-07T09:04:53.289591","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-09T14:59:50.871072Z","iopub.execute_input":"2022-09-09T14:59:50.871401Z","iopub.status.idle":"2022-09-09T14:59:50.880377Z","shell.execute_reply.started":"2022-09-09T14:59:50.871364Z","shell.execute_reply":"2022-09-09T14:59:50.879239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\n\nnet_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True,reduction='mean')\n\nnet_opt = nn.Adam(network.trainable_params(), learning_rate=0.1)\n\n\n\n\nmodel = ms.Model(network, loss_fn=net_loss, optimizer=net_opt, metrics={\"Accuracy\": Accuracy()})\n\n\nmodel.fit(100, train_dataset=tr_set, valid_dataset=val_set,callbacks=[LossMonitor(0.1,500)])\n\n","metadata":{"papermill":{"duration":0.019386,"end_time":"2022-09-07T09:04:54.141384","exception":false,"start_time":"2022-09-07T09:04:54.121998","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-09-09T14:59:50.881648Z","iopub.execute_input":"2022-09-09T14:59:50.882451Z","iopub.status.idle":"2022-09-09T14:59:51.533390Z","shell.execute_reply.started":"2022-09-09T14:59:50.882416Z","shell.execute_reply":"2022-09-09T14:59:51.531812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ny_pred = model.predict(ms.Tensor(np.array(x_test).reshape(-1,28,28,1).transpose(0,3,1,2),ms.float32)).asnumpy()\ny_pred = np.argmax(y_pred,1)\npd_submit = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\npd_submit['Label'] = y_pred\npd_submit.to_csv('dub.csv', header=True, index=False)\npd_submit","metadata":{"execution":{"iopub.status.busy":"2022-09-09T15:02:49.137694Z","iopub.status.idle":"2022-09-09T15:02:49.139521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}