{"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":"markdown","source":"# 0)Describe","metadata":{}},{"cell_type":"markdown","source":"The competition is a image class game.(CE or LAA)\n\nThe tiff file is so big that we can not easily read it.We have mounted a same dataset of jpg.format to train our model.\n\nPaddle is an Open-Source Deep Learning Platform Originated from Industrial Practice.We will use paddle to build our Model.\n","metadata":{}},{"cell_type":"markdown","source":"# 1)Dataset","metadata":{}},{"cell_type":"code","source":"# Author: Yinhaojie\n# Date: 2022-8-30\n# Email: 1250225532@qq.com\n# Describe: A baseline of Mayo Clinic - STRIP AI made by paddle\n!pip install /kaggle/input/paddlefloat012/paddle_bfloat-0.1.2-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl\n!pip install /kaggle/input/astor081py2py3noneanywhl/astor-0.8.1-py2.py3-none-any.whl\n#!pip install /kaggle/input/paddle-bfloat017cp37cp37m/paddle_bfloat-0.1.7-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl\n!pip install /kaggle/input/paddle230gpupost101/paddlepaddle_gpu-2.3.0.post101-cp37-cp37m-linux_x86_64.whl\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:30:24.835759Z","iopub.execute_input":"2022-09-28T07:30:24.836486Z","iopub.status.idle":"2022-09-28T07:30:53.873199Z","shell.execute_reply.started":"2022-09-28T07:30:24.83645Z","shell.execute_reply":"2022-09-28T07:30:53.872028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import paddle\npaddle.utils.run_check()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:30:53.876702Z","iopub.execute_input":"2022-09-28T07:30:53.877122Z","iopub.status.idle":"2022-09-28T07:30:53.93246Z","shell.execute_reply.started":"2022-09-28T07:30:53.877079Z","shell.execute_reply":"2022-09-28T07:30:53.931591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import paddle\nimport numpy as np\nimport PIL.Image as Image\nfrom paddle.vision import transforms\nimport os, glob\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom paddle.io import Dataset\nimport cv2\nfrom paddle.vision.transforms import Compose, Resize, ToTensor, Normalize \nfrom paddle.io import Dataset, DataLoader  \nfrom tqdm import tqdm\nimport tifffile","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:30:53.935407Z","iopub.execute_input":"2022-09-28T07:30:53.935709Z","iopub.status.idle":"2022-09-28T07:30:53.941961Z","shell.execute_reply.started":"2022-09-28T07:30:53.93568Z","shell.execute_reply":"2022-09-28T07:30:53.940833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paddle.set_device('gpu:0')","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:30:53.943737Z","iopub.execute_input":"2022-09-28T07:30:53.944638Z","iopub.status.idle":"2022-09-28T07:30:53.953518Z","shell.execute_reply.started":"2022-09-28T07:30:53.9446Z","shell.execute_reply":"2022-09-28T07:30:53.952571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1.1 Path for dataset.","metadata":{}},{"cell_type":"code","source":"TRAIN_CSV = \"../input/mayo-clinic-strip-ai/train.csv\"\nTEST_CSV = \"../input/mayo-clinic-strip-ai/test.csv\"\nSAMPLE_SUB_CSV = \"../input/mayo-clinic-strip-ai/sample_submission.csv\"\nOTHERS_CSV = \"../input/mayo-clinic-strip-ai/other.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:30:53.954891Z","iopub.execute_input":"2022-09-28T07:30:53.955462Z","iopub.status.idle":"2022-09-28T07:30:53.962837Z","shell.execute_reply.started":"2022-09-28T07:30:53.955315Z","shell.execute_reply":"2022-09-28T07:30:53.961667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train\ntrain_df = pd.read_csv(TRAIN_CSV)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:30:53.964459Z","iopub.execute_input":"2022-09-28T07:30:53.965087Z","iopub.status.idle":"2022-09-28T07:30:53.98807Z","shell.execute_reply.started":"2022-09-28T07:30:53.965028Z","shell.execute_reply":"2022-09-28T07:30:53.987229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape\ntrain_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:30:53.989648Z","iopub.execute_input":"2022-09-28T07:30:53.990312Z","iopub.status.idle":"2022-09-28T07:30:54.004856Z","shell.execute_reply.started":"2022-09-28T07:30:53.990273Z","shell.execute_reply":"2022-09-28T07:30:54.003955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1.2 Read a picture from dataset","metadata":{}},{"cell_type":"code","source":"Image.MAX_IMAGE_PIXELS = None   #Make PIL get picture.   !!!This is important for datareader","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:30:54.010016Z","iopub.execute_input":"2022-09-28T07:30:54.010314Z","iopub.status.idle":"2022-09-28T07:30:54.015996Z","shell.execute_reply.started":"2022-09-28T07:30:54.010272Z","shell.execute_reply":"2022-09-28T07:30:54.014786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# example = \"../input/mayo-clinic-strip-ai/train/\"+str(train_df[\"image_id\"].iloc[0])+\".tif\"\n# img=cv2.resize(tifffile.imread(example),(640,640))\n# print(img.size)\n# plt.figure(\"example\")\n# plt.imshow(img)\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:30:54.017465Z","iopub.execute_input":"2022-09-28T07:30:54.018419Z","iopub.status.idle":"2022-09-28T07:30:54.02576Z","shell.execute_reply.started":"2022-09-28T07:30:54.018384Z","shell.execute_reply":"2022-09-28T07:30:54.024604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1.3 DataLoder","metadata":{}},{"cell_type":"markdown","source":"### 1.3.1 Dataset Clas","metadata":{}},{"cell_type":"code","source":"class MyDataset(Dataset):\n    def __init__(self,data_df,mode='train_data',transform=None):\n        super(MyDataset,self).__init__()\n        self.data = data_df\n        self.transform=transform\n        self.model=mode\n        \n    def __getitem__(self, index):\n\n        #When model is train,get the label.  \n        #When model is test,make label equal zero and We will get the predict label by our Model. \n        \n        if self.model=='train_data':\n            \n            image_file=\"/kaggle/input/mayo-convert-tif-to-jpg/train/\"+self.data.iloc[index,0]+\".jpg\"\n            label = self.data.iloc[index,4]\n            img =Image.open(image_file)\n            img=img.resize((320,320))\n            img=np.array(img)\n\n        else:\n            \n            image_file=\"../input/mayo-clinic-strip-ai/test/\"+self.data.iloc[index,0]+\".tif\"\n            label = 0  \n            img=cv2.resize(tifffile.imread(image_file),(320,320))\n                \n        if self.transform is not None:   \n            img=self.transform(img)\n        \n        if label=='LAA':\n            label=0\n        elif label=='CE':\n            label=1\n            \n        return img, label\n\n    def __len__(self):\n        \n        return len(self.data)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:30:54.027478Z","iopub.execute_input":"2022-09-28T07:30:54.027927Z","iopub.status.idle":"2022-09-28T07:30:54.040321Z","shell.execute_reply.started":"2022-09-28T07:30:54.027846Z","shell.execute_reply":"2022-09-28T07:30:54.039302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1.3.2 Dataset","metadata":{}},{"cell_type":"code","source":"#The output is HWC which we can not use. We transorm it to CHW\ntransform = Compose([ToTensor(data_format=\"CHW\")]) \ntrain_data = MyDataset(train_df,mode='train_data',transform=transform)\ntrain_dataloader = DataLoader(train_data, batch_size=16, shuffle=False, drop_last=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:30:54.042233Z","iopub.execute_input":"2022-09-28T07:30:54.042641Z","iopub.status.idle":"2022-09-28T07:30:54.053066Z","shell.execute_reply.started":"2022-09-28T07:30:54.042605Z","shell.execute_reply":"2022-09-28T07:30:54.05195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1.3.3 extraction of DataLoader","metadata":{}},{"cell_type":"code","source":"# for batch_id, data in enumerate(train_dataloader()):\n#     features, labels = data\n#     print(features)\n#     print(labels)\n#     del features\n#     del labels\n#     break","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:30:54.054728Z","iopub.execute_input":"2022-09-28T07:30:54.05516Z","iopub.status.idle":"2022-09-28T07:30:54.062521Z","shell.execute_reply.started":"2022-09-28T07:30:54.055123Z","shell.execute_reply":"2022-09-28T07:30:54.061826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2)Model","metadata":{}},{"cell_type":"code","source":"model = paddle.vision.models.resnet18(pretrained=False, num_classes=2)\n# 定义优化器\nopt = paddle.optimizer.Adam(learning_rate=1e-6, parameters=model.parameters(), weight_decay=paddle.regularizer.L2Decay(1e-4))\n# 定义损失函数\nloss_fn = paddle.nn.CrossEntropyLoss()\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:30:54.063826Z","iopub.execute_input":"2022-09-28T07:30:54.064958Z","iopub.status.idle":"2022-09-28T07:30:54.104573Z","shell.execute_reply.started":"2022-09-28T07:30:54.064866Z","shell.execute_reply":"2022-09-28T07:30:54.104006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3)Train","metadata":{}},{"cell_type":"code","source":"paddle.set_device('gpu:0')\n# 整体训练流程\nfor epoch_id in tqdm(range(1)):\n    model.train()\n    for batch_id, data in enumerate(train_dataloader()):\n        # 读取数据\n        features, labels = data\n        # 前向传播\n        predicts = model(features)\n        # 损失计算\n        #print(predicts)\n        loss = loss_fn(predicts, labels)\n        # 反向传播\n        avg_loss = paddle.mean(loss)\n        avg_loss.backward()\n        # 更新\n        opt.step()\n        # 清零梯度\n        opt.clear_grad()\n        # 打印损失\n        if batch_id % 2 == 0:\n            print('epoch_id:{}, batch_id:{}, loss:{}'.format(epoch_id, batch_id, avg_loss.numpy()))","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:30:54.105947Z","iopub.execute_input":"2022-09-28T07:30:54.106769Z","iopub.status.idle":"2022-09-28T07:48:03.34715Z","shell.execute_reply.started":"2022-09-28T07:30:54.10673Z","shell.execute_reply":"2022-09-28T07:48:03.346196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paddle.save(model.state_dict(), 'My.model')","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:48:03.348435Z","iopub.execute_input":"2022-09-28T07:48:03.349538Z","iopub.status.idle":"2022-09-28T07:48:03.4483Z","shell.execute_reply.started":"2022-09-28T07:48:03.349495Z","shell.execute_reply":"2022-09-28T07:48:03.447283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4)Test","metadata":{}},{"cell_type":"code","source":"#test\ntest_df = pd.read_csv(TEST_CSV)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:48:03.450069Z","iopub.execute_input":"2022-09-28T07:48:03.450425Z","iopub.status.idle":"2022-09-28T07:48:03.470752Z","shell.execute_reply.started":"2022-09-28T07:48:03.450389Z","shell.execute_reply":"2022-09-28T07:48:03.469914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = MyDataset(test_df,mode='test_data',transform=transform)\ntest_dataloader = DataLoader(test_data, batch_size=4, shuffle=False, drop_last=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:48:03.473556Z","iopub.execute_input":"2022-09-28T07:48:03.473859Z","iopub.status.idle":"2022-09-28T07:48:03.478615Z","shell.execute_reply.started":"2022-09-28T07:48:03.473833Z","shell.execute_reply":"2022-09-28T07:48:03.477616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_record=[]","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:48:03.480314Z","iopub.execute_input":"2022-09-28T07:48:03.480947Z","iopub.status.idle":"2022-09-28T07:48:03.488039Z","shell.execute_reply.started":"2022-09-28T07:48:03.480897Z","shell.execute_reply":"2022-09-28T07:48:03.487139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for batch_id, data in enumerate(test_dataloader()):\n    features, labels = data\n    pre_label=model(features)\n    #print(pre_label)\n    test_record.append(pre_label)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:48:03.489602Z","iopub.execute_input":"2022-09-28T07:48:03.49008Z","iopub.status.idle":"2022-09-28T07:49:31.989003Z","shell.execute_reply.started":"2022-09-28T07:48:03.490044Z","shell.execute_reply":"2022-09-28T07:49:31.987973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5)creat sub_file","metadata":{}},{"cell_type":"code","source":"#Sample submission file\nsubmission = pd.read_csv(SAMPLE_SUB_CSV)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:49:31.990704Z","iopub.execute_input":"2022-09-28T07:49:31.991092Z","iopub.status.idle":"2022-09-28T07:49:32.01518Z","shell.execute_reply.started":"2022-09-28T07:49:31.991055Z","shell.execute_reply":"2022-09-28T07:49:32.014147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"iter=len(test_record)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T08:21:18.145485Z","iopub.execute_input":"2022-09-28T08:21:18.146657Z","iopub.status.idle":"2022-09-28T08:21:18.152201Z","shell.execute_reply.started":"2022-09-28T08:21:18.146587Z","shell.execute_reply":"2022-09-28T08:21:18.150814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_record[0][0]","metadata":{"execution":{"iopub.status.busy":"2022-09-28T08:28:15.054348Z","iopub.execute_input":"2022-09-28T08:28:15.055044Z","iopub.status.idle":"2022-09-28T08:28:15.062761Z","shell.execute_reply.started":"2022-09-28T08:28:15.055005Z","shell.execute_reply":"2022-09-28T08:28:15.061706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    print(i)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T08:25:45.606055Z","iopub.execute_input":"2022-09-28T08:25:45.606609Z","iopub.status.idle":"2022-09-28T08:25:45.612935Z","shell.execute_reply.started":"2022-09-28T08:25:45.606568Z","shell.execute_reply":"2022-09-28T08:25:45.611885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(test_record)):\n    #print(iter)\n    submission.iloc[i*4+0,1]=np.array(test_record[i][0][0])\n    submission.iloc[i*4+0,2]=np.array(test_record[i][0][1])\n    \n    submission.iloc[i*4+1,1]=np.array(test_record[i][1][0])\n    submission.iloc[i*4+1,2]=np.array(test_record[i][1][1])\n    \n    submission.iloc[i*4+2,1]=np.array(test_record[i][2][0])\n    submission.iloc[i*4+2,2]=np.array(test_record[i][2][1])\n    \n    submission.iloc[i*4+3,1]=np.array(test_record[i][3][0])\n    submission.iloc[i*4+3,2]=np.array(test_record[i][3][1])\n    ","metadata":{"execution":{"iopub.status.busy":"2022-09-28T08:29:31.018446Z","iopub.execute_input":"2022-09-28T08:29:31.018812Z","iopub.status.idle":"2022-09-28T08:29:31.03389Z","shell.execute_reply.started":"2022-09-28T08:29:31.018783Z","shell.execute_reply":"2022-09-28T08:29:31.032605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for i in range(len(submission)):\n#     submission.iloc[i,1]=np.array(test_record[i][0][0])\n#     submission.iloc[i,2]=np.array(test_record[i][0][1])\n    ","metadata":{"execution":{"iopub.status.busy":"2022-09-28T08:15:53.374063Z","iopub.execute_input":"2022-09-28T08:15:53.374726Z","iopub.status.idle":"2022-09-28T08:15:53.396923Z","shell.execute_reply.started":"2022-09-28T08:15:53.374691Z","shell.execute_reply":"2022-09-28T08:15:53.395497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(submission)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T08:29:42.896961Z","iopub.execute_input":"2022-09-28T08:29:42.89775Z","iopub.status.idle":"2022-09-28T08:29:42.905027Z","shell.execute_reply.started":"2022-09-28T08:29:42.897709Z","shell.execute_reply":"2022-09-28T08:29:42.903871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T08:29:55.551836Z","iopub.execute_input":"2022-09-28T08:29:55.552996Z","iopub.status.idle":"2022-09-28T08:29:55.565635Z","shell.execute_reply.started":"2022-09-28T08:29:55.552947Z","shell.execute_reply":"2022-09-28T08:29:55.564571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T08:30:29.470372Z","iopub.execute_input":"2022-09-28T08:30:29.470781Z","iopub.status.idle":"2022-09-28T08:30:29.481494Z","shell.execute_reply.started":"2022-09-28T08:30:29.470746Z","shell.execute_reply":"2022-09-28T08:30:29.480279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reference\n\n[1] [Document of paddle](https://www.paddlepaddle.org.cn/en)\n\n[2] [Example of paddle API](https://aistudio.baidu.com/aistudio/projectdetail/2301079?contributionType=1)\n\n[3] [Baseline of paddle regular season: Cat twelve categories.](https://aistudio.baidu.com/aistudio/projectdetail/4243146)\n\n[4] [Deep Residual Learning for Image Recognition](https://arxiv.org/abs/1512.03385)","metadata":{}}]}