{"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":"# from google.colab import drive\n# drive.mount('/content/drive')","metadata":{"id":"r2mXLt5-v2s0","executionInfo":{"status":"ok","timestamp":1626054058063,"user_tz":-480,"elapsed":13322,"user":{"displayName":"程神","photoUrl":"","userId":"04369975984541515920"}},"outputId":"aab7f7e9-af9e-461d-81cf-550909532ce6","execution":{"iopub.status.busy":"2021-07-19T08:58:52.77495Z","iopub.execute_input":"2021-07-19T08:58:52.775336Z","iopub.status.idle":"2021-07-19T08:58:52.779302Z","shell.execute_reply.started":"2021-07-19T08:58:52.775283Z","shell.execute_reply":"2021-07-19T08:58:52.77813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MultiLabelBinarizer\nfrom tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport albumentations\nimport pandas as pd\nimport numpy as np\nimport shutil\nimport os\nfrom sklearn.model_selection import KFold, StratifiedKFold","metadata":{"execution":{"iopub.status.busy":"2021-07-19T08:58:52.783647Z","iopub.execute_input":"2021-07-19T08:58:52.783917Z","iopub.status.idle":"2021-07-19T08:58:52.791309Z","shell.execute_reply.started":"2021-07-19T08:58:52.783891Z","shell.execute_reply":"2021-07-19T08:58:52.79029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG():\n    img_size = 299\n    seed = 42\n    classes = [\n        'complex', \n        'frog_eye_leaf_spot', \n        'powdery_mildew', \n        'rust', \n        'scab',\n        'healthy']\n    batch_size=32","metadata":{"execution":{"iopub.status.busy":"2021-07-19T08:58:52.792869Z","iopub.execute_input":"2021-07-19T08:58:52.793452Z","iopub.status.idle":"2021-07-19T08:58:52.802541Z","shell.execute_reply.started":"2021-07-19T08:58:52.79342Z","shell.execute_reply":"2021-07-19T08:58:52.801752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir='../input/plant-pathology-2021-fgvc8/test_images'","metadata":{"execution":{"iopub.status.busy":"2021-07-19T08:58:52.804197Z","iopub.execute_input":"2021-07-19T08:58:52.804679Z","iopub.status.idle":"2021-07-19T08:58:52.8133Z","shell.execute_reply.started":"2021-07-19T08:58:52.804606Z","shell.execute_reply":"2021-07-19T08:58:52.812467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntest_img = '../input/plant-pathology-2021-fgvc8/test_images'\nsubmission = pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')\nsubmission.head()","metadata":{"id":"W8yV7Pan4lZO","executionInfo":{"status":"ok","timestamp":1626064616942,"user_tz":-480,"elapsed":350,"user":{"displayName":"程神","photoUrl":"","userId":"04369975984541515920"}},"outputId":"0a4a1c2d-45d1-4a2a-f8b0-247a3146b2c9","execution":{"iopub.status.busy":"2021-07-19T08:58:52.81468Z","iopub.execute_input":"2021-07-19T08:58:52.815125Z","iopub.status.idle":"2021-07-19T08:58:52.835891Z","shell.execute_reply.started":"2021-07-19T08:58:52.815068Z","shell.execute_reply":"2021-07-19T08:58:52.835155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['labels'] = submission['labels'].apply(lambda s: s.split(' '))\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-19T08:58:52.837177Z","iopub.execute_input":"2021-07-19T08:58:52.837458Z","iopub.status.idle":"2021-07-19T08:58:52.849683Z","shell.execute_reply.started":"2021-07-19T08:58:52.83743Z","shell.execute_reply":"2021-07-19T08:58:52.848971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n''' ImageDataGenerator參考網頁-> https://keras.io/zh/preprocessing/image/ '''\ndatagen = ImageDataGenerator(\n    rescale = 1/255.,\n    rotation_range = 15,\n    width_shift_range=0.2, \n    height_shift_range=0.2,\n    zoom_range = 0.2,\n    shear_range=0.3, #（以弧度逆时针方向剪切角度）。\n    horizontal_flip=True,\n    vertical_flip=True,\n    validation_split= 0.2,)","metadata":{"execution":{"iopub.status.busy":"2021-07-19T08:58:52.850926Z","iopub.execute_input":"2021-07-19T08:58:52.851478Z","iopub.status.idle":"2021-07-19T08:58:52.860983Z","shell.execute_reply.started":"2021-07-19T08:58:52.85144Z","shell.execute_reply":"2021-07-19T08:58:52.859943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = datagen.flow_from_dataframe(\n                  submission,\n                  directory = test_img,\n                  x_col = 'image',\n                  y_col = 'labels',\n                  class_mode = \"categorical\",\n                  batch_size=4,\n                  target_size = (CFG.img_size,CFG.img_size),\n                  color_mode=\"rgb\",\n                  shuffle = False,\n                  seed = CFG.seed)","metadata":{"execution":{"iopub.status.busy":"2021-07-19T08:58:52.862221Z","iopub.execute_input":"2021-07-19T08:58:52.862776Z","iopub.status.idle":"2021-07-19T08:58:52.888453Z","shell.execute_reply.started":"2021-07-19T08:58:52.86274Z","shell.execute_reply":"2021-07-19T08:58:52.887723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_addons as tfa\nfrom tensorflow import keras\n#加載所有模型並將它們存儲在列表中\nmodel_files = os.listdir('../input/xception-skf')\nmodels = []\n\nfor model_file in model_files:\n  print(f'loading model {model_file}')\n  models.append(keras.models.load_model(f'../input/xception-skf/{model_file}', compile = True))","metadata":{"execution":{"iopub.status.busy":"2021-07-19T08:58:52.889623Z","iopub.execute_input":"2021-07-19T08:58:52.889955Z","iopub.status.idle":"2021-07-19T08:59:15.441126Z","shell.execute_reply.started":"2021-07-19T08:58:52.889926Z","shell.execute_reply":"2021-07-19T08:59:15.440202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict=[]\nfor model in models:\n    pred = model.predict(test_generator)\n    pred =np.around(pred, decimals=3, out=None)\n    predict.append( pred)","metadata":{"execution":{"iopub.status.busy":"2021-07-19T08:59:15.443658Z","iopub.execute_input":"2021-07-19T08:59:15.443939Z","iopub.status.idle":"2021-07-19T08:59:27.797507Z","shell.execute_reply.started":"2021-07-19T08:59:15.44391Z","shell.execute_reply":"2021-07-19T08:59:27.796573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict","metadata":{"execution":{"iopub.status.busy":"2021-07-19T08:59:27.799166Z","iopub.execute_input":"2021-07-19T08:59:27.799444Z","iopub.status.idle":"2021-07-19T08:59:27.807906Z","shell.execute_reply.started":"2021-07-19T08:59:27.799416Z","shell.execute_reply":"2021-07-19T08:59:27.806919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\npredict= np.mean(predict, axis=0)","metadata":{"execution":{"iopub.status.busy":"2021-07-19T08:59:27.809081Z","iopub.execute_input":"2021-07-19T08:59:27.809349Z","iopub.status.idle":"2021-07-19T08:59:27.815191Z","shell.execute_reply.started":"2021-07-19T08:59:27.809323Z","shell.execute_reply":"2021-07-19T08:59:27.814259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predict)","metadata":{"execution":{"iopub.status.busy":"2021-07-19T08:59:27.816282Z","iopub.execute_input":"2021-07-19T08:59:27.8167Z","iopub.status.idle":"2021-07-19T08:59:27.827943Z","shell.execute_reply.started":"2021-07-19T08:59:27.816672Z","shell.execute_reply":"2021-07-19T08:59:27.826685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.around(predict, decimals=3, out=None)","metadata":{"execution":{"iopub.status.busy":"2021-07-19T08:59:27.829398Z","iopub.execute_input":"2021-07-19T08:59:27.829854Z","iopub.status.idle":"2021-07-19T08:59:27.842149Z","shell.execute_reply.started":"2021-07-19T08:59:27.829816Z","shell.execute_reply":"2021-07-19T08:59:27.840953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"perdict = (predict>0.33)\nn_label = ['complex', 'frog_eye_leaf_spot', 'healthy', 'powdery_mildew', 'rust', 'scab']\nanswer = []\n\nfor i in range(perdict.shape[0]):\n    temp = []\n    for j, k in enumerate(n_label):\n        if perdict[i, j]:\n            temp.append(k)\n    answer.append(temp)\n    \nanswer = [' '.join(n) for n in answer]","metadata":{"id":"BfdudNBq4lZT","executionInfo":{"status":"ok","timestamp":1626083357195,"user_tz":-480,"elapsed":902,"user":{"displayName":"程神","photoUrl":"","userId":"04369975984541515920"}},"execution":{"iopub.status.busy":"2021-07-19T09:00:39.04525Z","iopub.execute_input":"2021-07-19T09:00:39.045617Z","iopub.status.idle":"2021-07-19T09:00:39.052261Z","shell.execute_reply.started":"2021-07-19T09:00:39.045588Z","shell.execute_reply":"2021-07-19T09:00:39.051316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"answer","metadata":{"execution":{"iopub.status.busy":"2021-07-19T09:00:41.362303Z","iopub.execute_input":"2021-07-19T09:00:41.362663Z","iopub.status.idle":"2021-07-19T09:00:41.3681Z","shell.execute_reply.started":"2021-07-19T09:00:41.362631Z","shell.execute_reply":"2021-07-19T09:00:41.367183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['labels'] = np.array(answer)\nsubmission","metadata":{"id":"Qbv9SGm74lZT","executionInfo":{"status":"ok","timestamp":1626083392686,"user_tz":-480,"elapsed":354,"user":{"displayName":"程神","photoUrl":"","userId":"04369975984541515920"}},"outputId":"39b336bd-9ce8-4508-9736-df91dc6d5154","execution":{"iopub.status.busy":"2021-07-19T09:00:42.661687Z","iopub.execute_input":"2021-07-19T09:00:42.662085Z","iopub.status.idle":"2021-07-19T09:00:42.676502Z","shell.execute_reply.started":"2021-07-19T09:00:42.662041Z","shell.execute_reply":"2021-07-19T09:00:42.675408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"id":"H7KCvpCL4lZT","executionInfo":{"status":"ok","timestamp":1626083400440,"user_tz":-480,"elapsed":349,"user":{"displayName":"程神","photoUrl":"","userId":"04369975984541515920"}},"execution":{"iopub.status.busy":"2021-07-19T09:00:46.362986Z","iopub.execute_input":"2021-07-19T09:00:46.36336Z","iopub.status.idle":"2021-07-19T09:00:46.369188Z","shell.execute_reply.started":"2021-07-19T09:00:46.36333Z","shell.execute_reply":"2021-07-19T09:00:46.368014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !cp /content/B4.h5 /content/B4_0.h5","metadata":{"id":"7K5vtSc-LS5V","executionInfo":{"status":"ok","timestamp":1626084327368,"user_tz":-480,"elapsed":1380,"user":{"displayName":"程神","photoUrl":"","userId":"04369975984541515920"}},"execution":{"iopub.status.busy":"2021-07-19T08:59:27.886823Z","iopub.execute_input":"2021-07-19T08:59:27.887258Z","iopub.status.idle":"2021-07-19T08:59:27.896407Z","shell.execute_reply.started":"2021-07-19T08:59:27.887219Z","shell.execute_reply":"2021-07-19T08:59:27.895555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !mv /content/B4_0.h5 /content/drive/MyDrive/apple","metadata":{"id":"fKu8VYrlLgL9","executionInfo":{"status":"ok","timestamp":1626084390630,"user_tz":-480,"elapsed":1016,"user":{"displayName":"程神","photoUrl":"","userId":"04369975984541515920"}},"execution":{"iopub.status.busy":"2021-07-19T08:59:27.897742Z","iopub.execute_input":"2021-07-19T08:59:27.898308Z","iopub.status.idle":"2021-07-19T08:59:27.907665Z","shell.execute_reply.started":"2021-07-19T08:59:27.898267Z","shell.execute_reply":"2021-07-19T08:59:27.906697Z"},"trusted":true},"execution_count":null,"outputs":[]}]}