{"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 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-20T10:32:24.111252Z","iopub.execute_input":"2021-07-20T10:32:24.111594Z","iopub.status.idle":"2021-07-20T10:32:24.117617Z","shell.execute_reply.started":"2021-07-20T10:32:24.111566Z","shell.execute_reply":"2021-07-20T10:32:24.116615Z"},"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-20T10:32:24.119472Z","iopub.execute_input":"2021-07-20T10:32:24.120074Z","iopub.status.idle":"2021-07-20T10:32:24.130200Z","shell.execute_reply.started":"2021-07-20T10:32:24.120029Z","shell.execute_reply":"2021-07-20T10:32:24.129303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir='../input/plant-pathology-2021-fgvc8/train.csv'","metadata":{"execution":{"iopub.status.busy":"2021-07-20T10:32:24.132069Z","iopub.execute_input":"2021-07-20T10:32:24.132591Z","iopub.status.idle":"2021-07-20T10:32:24.139805Z","shell.execute_reply.started":"2021-07-20T10:32:24.132556Z","shell.execute_reply":"2021-07-20T10:32:24.138858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img = '../input/plant-pathology-2021-fgvc8/test_images'\nsubmission = pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-20T10:32:24.141655Z","iopub.execute_input":"2021-07-20T10:32:24.142007Z","iopub.status.idle":"2021-07-20T10:32:24.164552Z","shell.execute_reply.started":"2021-07-20T10:32:24.141979Z","shell.execute_reply":"2021-07-20T10:32:24.163595Z"},"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-20T10:32:24.165635Z","iopub.execute_input":"2021-07-20T10:32:24.165904Z","iopub.status.idle":"2021-07-20T10:32:24.177246Z","shell.execute_reply.started":"2021-07-20T10:32:24.165878Z","shell.execute_reply":"2021-07-20T10:32:24.176195Z"},"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-20T10:32:24.178473Z","iopub.execute_input":"2021-07-20T10:32:24.178758Z","iopub.status.idle":"2021-07-20T10:32:24.186197Z","shell.execute_reply.started":"2021-07-20T10:32:24.178733Z","shell.execute_reply":"2021-07-20T10:32:24.185565Z"},"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-20T10:32:24.187250Z","iopub.execute_input":"2021-07-20T10:32:24.187631Z","iopub.status.idle":"2021-07-20T10:32:24.214338Z","shell.execute_reply.started":"2021-07-20T10:32:24.187604Z","shell.execute_reply":"2021-07-20T10:32:24.213328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\ndef load_model():\n    model_pre = keras.models.load_model(\"../input/n-2-0720/N_2_0720.h5\")\n    return model_pre","metadata":{"execution":{"iopub.status.busy":"2021-07-20T10:32:24.215547Z","iopub.execute_input":"2021-07-20T10:32:24.215823Z","iopub.status.idle":"2021-07-20T10:32:24.219994Z","shell.execute_reply.started":"2021-07-20T10:32:24.215796Z","shell.execute_reply":"2021-07-20T10:32:24.219035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model()\npred = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2021-07-20T10:32:24.222025Z","iopub.execute_input":"2021-07-20T10:32:24.222474Z","iopub.status.idle":"2021-07-20T10:32:33.070517Z","shell.execute_reply.started":"2021-07-20T10:32:24.222444Z","shell.execute_reply":"2021-07-20T10:32:33.069713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"perdict = (pred>0.36)\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":{"execution":{"iopub.status.busy":"2021-07-20T10:32:33.071874Z","iopub.execute_input":"2021-07-20T10:32:33.072165Z","iopub.status.idle":"2021-07-20T10:32:33.078294Z","shell.execute_reply.started":"2021-07-20T10:32:33.072127Z","shell.execute_reply":"2021-07-20T10:32:33.077420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2021-07-20T10:32:33.079424Z","iopub.execute_input":"2021-07-20T10:32:33.079760Z","iopub.status.idle":"2021-07-20T10:32:33.090636Z","shell.execute_reply.started":"2021-07-20T10:32:33.079732Z","shell.execute_reply":"2021-07-20T10:32:33.089656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['labels'] = np.array(answer)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-07-20T10:32:33.092884Z","iopub.execute_input":"2021-07-20T10:32:33.093299Z","iopub.status.idle":"2021-07-20T10:32:33.103695Z","shell.execute_reply.started":"2021-07-20T10:32:33.093259Z","shell.execute_reply":"2021-07-20T10:32:33.102890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.around(pred, decimals=3, out=None)","metadata":{"execution":{"iopub.status.busy":"2021-07-20T10:32:33.105826Z","iopub.execute_input":"2021-07-20T10:32:33.106132Z","iopub.status.idle":"2021-07-20T10:32:33.112077Z","shell.execute_reply.started":"2021-07-20T10:32:33.106081Z","shell.execute_reply":"2021-07-20T10:32:33.111091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-20T10:32:33.113321Z","iopub.execute_input":"2021-07-20T10:32:33.113590Z","iopub.status.idle":"2021-07-20T10:32:33.122805Z","shell.execute_reply.started":"2021-07-20T10:32:33.113564Z","shell.execute_reply":"2021-07-20T10:32:33.121887Z"},"trusted":true},"execution_count":null,"outputs":[]}]}