{"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":"%matplotlib inline\nimport matplotlib\nimport matplotlib.pyplot as plt\nfrom IPython import display","metadata":{"execution":{"iopub.status.busy":"2022-03-02T07:08:27.403516Z","iopub.execute_input":"2022-03-02T07:08:27.404120Z","iopub.status.idle":"2022-03-02T07:08:27.447972Z","shell.execute_reply.started":"2022-03-02T07:08:27.403982Z","shell.execute_reply":"2022-03-02T07:08:27.446621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-02T07:08:27.450338Z","iopub.execute_input":"2022-03-02T07:08:27.451001Z","iopub.status.idle":"2022-03-02T07:08:27.459689Z","shell.execute_reply.started":"2022-03-02T07:08:27.450954Z","shell.execute_reply":"2022-03-02T07:08:27.457735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"df_train=pd.read_csv('../input/digit-recognizer/train.csv')\ndf_train","metadata":{"execution":{"iopub.status.busy":"2022-03-01T03:55:01.48905Z","iopub.execute_input":"2022-03-01T03:55:01.489334Z","iopub.status.idle":"2022-03-01T03:55:03.8116Z","shell.execute_reply.started":"2022-03-01T03:55:01.489303Z","shell.execute_reply":"2022-03-01T03:55:03.810679Z"}}},{"cell_type":"markdown","source":"train_array=df_train.to_numpy()\nprint(train_array.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-01T03:55:03.813565Z","iopub.execute_input":"2022-03-01T03:55:03.813785Z","iopub.status.idle":"2022-03-01T03:55:03.818476Z","shell.execute_reply.started":"2022-03-01T03:55:03.81376Z","shell.execute_reply":"2022-03-01T03:55:03.817897Z"}}},{"cell_type":"markdown","source":"train_label=train_array[:,0]\ntrain_data=train_array[:,1:].reshape((-1,28,28))\nprint(train_data.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-01T03:55:03.81947Z","iopub.execute_input":"2022-03-01T03:55:03.819689Z","iopub.status.idle":"2022-03-01T03:55:03.831758Z","shell.execute_reply.started":"2022-03-01T03:55:03.819666Z","shell.execute_reply":"2022-03-01T03:55:03.830873Z"}}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ../input/trident/tridentx-0.7.4.1-py3-none-any.whl\n#!pip3 install torch torchvision torchaudio --upgrade","metadata":{"execution":{"iopub.status.busy":"2022-03-02T07:08:27.463106Z","iopub.execute_input":"2022-03-02T07:08:27.466000Z","iopub.status.idle":"2022-03-02T07:08:39.231869Z","shell.execute_reply.started":"2022-03-02T07:08:27.465913Z","shell.execute_reply":"2022-03-02T07:08:39.230600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ['TRIDENT_BACKEND'] = 'pytorch'\nos.environ['TRIDENT_HOME'] = 'trident'\nimport trident as T\nfrom trident import *\nfrom trident.models import efficientnet","metadata":{"execution":{"iopub.status.busy":"2022-03-02T07:08:39.234651Z","iopub.execute_input":"2022-03-02T07:08:39.235075Z","iopub.status.idle":"2022-03-02T07:08:47.717315Z","shell.execute_reply.started":"2022-03-02T07:08:39.235026Z","shell.execute_reply":"2022-03-02T07:08:47.714728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_provider=T.load_mnist('mnist') #讀取mnist數據集\ndata_provider.image_transform_funcs=[\n    ToRGB(),\n    Resize((112,112)),\n    RandomTransform(rotation_range=40,zoom_range=0.1,shift_range=0.02,shear_range=0.02,random_flip=0.2),\n    RandomAdjustGamma(gamma_range=(0.6,1.5)),\n    RandomAdjustContrast(value_range=(0.6, 1.5)),\n    RandomAdjustHue(value_range=(-0.2, 0.2)),\n    SaltPepperNoise(prob=0.008),#椒鹽噪音     \n    Normalize(127.5,127.5)]\ndata,label=data_provider.next()\nprint(data.shape,label.shape)\n#data","metadata":{"execution":{"iopub.status.busy":"2022-03-02T07:08:47.720536Z","iopub.execute_input":"2022-03-02T07:08:47.721783Z","iopub.status.idle":"2022-03-02T07:08:51.965364Z","shell.execute_reply.started":"2022-03-02T07:08:47.721703Z","shell.execute_reply":"2022-03-02T07:08:51.964281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_provider.preview_images()","metadata":{"execution":{"iopub.status.busy":"2022-03-02T07:08:51.968108Z","iopub.execute_input":"2022-03-02T07:08:51.968600Z","iopub.status.idle":"2022-03-02T07:08:52.027209Z","shell.execute_reply.started":"2022-03-02T07:08:51.968557Z","shell.execute_reply":"2022-03-02T07:08:52.026188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\n\nnet1=efficientnet.EfficientNetB0(pretrained=True,classes=10,input_shape=(3,112,112))\nnet1.model[-1].add_noise=True\nnet1.model[-1].noise_intensity=0.12\nnet1.summary()\n\n\n\nnet1=net1\\\n    .with_optimizer(optimizer=DiffGrad,lr=1e-3,gradient_centralization='all')\\\n    .with_loss(CrossEntropyLoss)\\\n    .with_metric(accuracy)\\\n    .with_regularizer('l2',reg_weight=1e-5)\\\n    .with_learning_rate_scheduler(CosineLR(min_lr=1e-7,period=1000))\\\n    .with_model_save_path('Models/mnist_resnet50.pth')\\\n     .with_automatic_mixed_precision_training()\n\nplan=TrainingPlan()\\\n    .add_training_item(net1)\\\n    .with_data_loader(data_provider)\\\n    .repeat_epochs(20)\\\n    .with_batch_size(128)\\\n    .print_progress_scheduling(50,unit='batch')\\\n    .print_gpu_utilization(frequency=500, unit='batch') \\\n    .out_sample_evaluation_scheduling(frequency=200,unit='batch')\\\n    .display_loss_metric_curve_scheduling(frequency=500,unit='batch',imshow=True)\n\nplan.start_now()","metadata":{"execution":{"iopub.status.busy":"2022-03-02T08:38:15.737284Z","iopub.execute_input":"2022-03-02T08:38:15.737914Z","iopub.status.idle":"2022-03-02T08:41:06.756889Z","shell.execute_reply.started":"2022-03-02T08:38:15.737878Z","shell.execute_reply":"2022-03-02T08:41:06.755906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#讀取測試資料集\ntest_array=pd.read_csv('../input/digit-recognizer/test.csv').to_numpy()\nprint(test_array.shape)\n#把形狀轉成二維圖像以及型別轉為float32\ntest_array=test_array.reshape((-1,28,28,1)).astype(np.float32)\nprint(test_array.shape)\nprint(test_array.dtype)","metadata":{"execution":{"iopub.status.busy":"2022-03-02T08:41:12.089025Z","iopub.execute_input":"2022-03-02T08:41:12.089764Z","iopub.status.idle":"2022-03-02T08:41:13.895100Z","shell.execute_reply.started":"2022-03-02T08:41:12.089725Z","shell.execute_reply":"2022-03-02T08:41:13.894054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#切換為評估模式\nnet1.eval()\n\n#縮放 轉彩色 標準化\nresize_fn=Resize((112,112))   \ntorgb_fn=ToRGB()\nnorm_fn=Normalize(127.5,127.5)\nanswers={}\n\nfrom tqdm import tqdm\n\n#遞迴產生推論結果\nfor i in tqdm(range(len(test_array))):\n    img=test_array[i]\n    img=norm_fn(torgb_fn(resize_fn(img)))\n    #print(img.shape) \n    img=to_tensor(np.expand_dims(np.transpose(img,(2,1,0)),0))\n    #print(img.shape) \n    result=np.argmax(to_numpy(net1(img)[0]),-1)\n    answers[i]=result\n    \n    \n","metadata":{"execution":{"iopub.status.busy":"2022-03-02T08:41:43.823017Z","iopub.execute_input":"2022-03-02T08:41:43.823300Z","iopub.status.idle":"2022-03-02T08:53:50.057614Z","shell.execute_reply.started":"2022-03-02T08:41:43.823270Z","shell.execute_reply":"2022-03-02T08:53:50.056588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_rows=['ImageId,Label\\n']\nfor i in tqdm(range(len(test_array))):\n    submission_rows.append('{0},{1}\\n'.format(i,answers[i]))\n\n    \nwith open('./submission.csv','w',encoding='utf-8-sig') as f:\n    f.writelines(submission_rows)\n\n\n    ","metadata":{"execution":{"iopub.status.busy":"2022-03-02T08:54:08.791650Z","iopub.execute_input":"2022-03-02T08:54:08.792310Z","iopub.status.idle":"2022-03-02T08:54:08.872971Z","shell.execute_reply.started":"2022-03-02T08:54:08.792255Z","shell.execute_reply":"2022-03-02T08:54:08.871816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\nFileLink('./submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-02T08:56:34.345060Z","iopub.execute_input":"2022-03-02T08:56:34.345730Z","iopub.status.idle":"2022-03-02T08:56:34.352834Z","shell.execute_reply.started":"2022-03-02T08:56:34.345697Z","shell.execute_reply":"2022-03-02T08:56:34.351647Z"},"trusted":true},"execution_count":null,"outputs":[]}]}