{"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 tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np\nimport shutil\nimport os","metadata":{"execution":{"iopub.status.busy":"2021-07-26T14:58:53.614597Z","iopub.execute_input":"2021-07-26T14:58:53.615011Z","iopub.status.idle":"2021-07-26T14:58:53.619529Z","shell.execute_reply.started":"2021-07-26T14:58:53.614978Z","shell.execute_reply":"2021-07-26T14:58:53.618737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')\nprint(train.shape)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-26T14:58:53.660903Z","iopub.execute_input":"2021-07-26T14:58:53.661449Z","iopub.status.idle":"2021-07-26T14:58:53.693912Z","shell.execute_reply.started":"2021-07-26T14:58:53.661400Z","shell.execute_reply":"2021-07-26T14:58:53.693153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 32\ntarget_size = (380, 380)\nbatch_size = 16\ntest_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-26T14:58:53.695340Z","iopub.execute_input":"2021-07-26T14:58:53.695776Z","iopub.status.idle":"2021-07-26T14:58:53.711602Z","shell.execute_reply.started":"2021-07-26T14:58:53.695741Z","shell.execute_reply":"2021-07-26T14:58:53.710194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\ndef get_cut_image(image):\n    img = cv2.blur(image,(3,3))\n    copy = np.uint8(img)\n    canny = cv2.Canny(copy, 145, 165)\n    box = np.argwhere(canny>0)\n    y1,x1 = box.min(axis=0)\n    y2,x2 = box.max(axis=0)\n    cut_img = img[y1:y2, x1:x2]\n    cut_img = cv2.resize(cut_img, target_size)\n    cut_img = cut_img.astype(\"float32\")*(1.)/255\n    return np.array(cut_img)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T14:58:53.714114Z","iopub.execute_input":"2021-07-26T14:58:53.714563Z","iopub.status.idle":"2021-07-26T14:58:53.722570Z","shell.execute_reply.started":"2021-07-26T14:58:53.714512Z","shell.execute_reply":"2021-07-26T14:58:53.721171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\npre = ImageDataGenerator(\n    rescale=1./255,\n    brightness_range=[0.5, 1.5],\n    rotation_range=45,\n    shear_range=0.2,\n    zoom_range=0.2, \n    horizontal_flip=True,\n    vertical_flip=True,\n    width_shift_range=0.2,\n    height_shift_range=0.2)\n# pos = ImageDataGenerator(\n# #     rescale=1./255,\n#     brightness_range=[0.5, 1.5],\n#     rotation_range=45,\n#     shear_range=0.2,\n#     zoom_range=0.2, \n#     horizontal_flip=True,\n#     width_shift_range=0.2,\n#     height_shift_range=0.2,\n#     preprocessing_function = get_cut_image)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T14:58:53.724640Z","iopub.execute_input":"2021-07-26T14:58:53.725120Z","iopub.status.idle":"2021-07-26T14:58:53.742897Z","shell.execute_reply.started":"2021-07-26T14:58:53.725073Z","shell.execute_reply":"2021-07-26T14:58:53.741577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # # TTA\npos = ImageDataGenerator(\n#     rescale=1./255,\n    brightness_range=[0.5, 1.5],\n    rotation_range=15, #45\n    shear_range=0.2,\n    zoom_range=0.3, #0.2 \n    featurewise_center=False, #\n    featurewise_std_normalization=False, #\n    horizontal_flip=True,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    vertical_flip=True,\n    validation_split= 0.2,\n    preprocessing_function = get_cut_image)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T14:58:53.744441Z","iopub.execute_input":"2021-07-26T14:58:53.744939Z","iopub.status.idle":"2021-07-26T14:58:53.755467Z","shell.execute_reply.started":"2021-07-26T14:58:53.744897Z","shell.execute_reply":"2021-07-26T14:58:53.754456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = pos.flow_from_dataframe(\n                  submission,\n                  directory = test_img,\n                  x_col = 'image',\n                  y_col = 'labels',\n                  class_mode = \"raw\",\n                  batch_size=batch_size,\n                  target_size = target_size,\n                  color_mode=\"rgb\",\n                  shuffle = False,\n                  seed = seed,)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T14:58:53.756764Z","iopub.execute_input":"2021-07-26T14:58:53.757410Z","iopub.status.idle":"2021-07-26T14:58:53.775892Z","shell.execute_reply.started":"2021-07-26T14:58:53.757373Z","shell.execute_reply":"2021-07-26T14:58:53.774807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\ndef load_model():\n    model_pre = keras.models.load_model(\"../input/k3-0726/B4(oversample)4Fold_cv_BC_2553.h5\",compile=False) #compile=False\n    return model_pre","metadata":{"execution":{"iopub.status.busy":"2021-07-26T14:58:53.777290Z","iopub.execute_input":"2021-07-26T14:58:53.777589Z","iopub.status.idle":"2021-07-26T14:58:53.785198Z","shell.execute_reply.started":"2021-07-26T14:58:53.777560Z","shell.execute_reply":"2021-07-26T14:58:53.783705Z"},"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-26T14:58:53.787429Z","iopub.execute_input":"2021-07-26T14:58:53.787747Z","iopub.status.idle":"2021-07-26T14:59:06.231658Z","shell.execute_reply.started":"2021-07-26T14:58:53.787718Z","shell.execute_reply":"2021-07-26T14:59:06.230693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## TTA","metadata":{}},{"cell_type":"code","source":"model = load_model()\ntta_steps = 5\npredictions = []\n\nfor i in range(tta_steps):\n    preds = model.predict(test_generator)\n    predictions.append(preds)\n\npred = np.mean(predictions, axis=0)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T14:59:06.233456Z","iopub.execute_input":"2021-07-26T14:59:06.233851Z","iopub.status.idle":"2021-07-26T14:59:28.959311Z","shell.execute_reply.started":"2021-07-26T14:59:06.233807Z","shell.execute_reply":"2021-07-26T14:59:28.958134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"perdict = (pred>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":{"execution":{"iopub.status.busy":"2021-07-26T14:59:28.960607Z","iopub.execute_input":"2021-07-26T14:59:28.961113Z","iopub.status.idle":"2021-07-26T14:59:28.967410Z","shell.execute_reply.started":"2021-07-26T14:59:28.961073Z","shell.execute_reply":"2021-07-26T14:59:28.966428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.around(pred, decimals=3, out=None)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T14:59:28.969083Z","iopub.execute_input":"2021-07-26T14:59:28.969507Z","iopub.status.idle":"2021-07-26T14:59:28.983859Z","shell.execute_reply.started":"2021-07-26T14:59:28.969465Z","shell.execute_reply":"2021-07-26T14:59:28.983013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['labels'] = np.array(answer)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-07-26T14:59:28.985165Z","iopub.execute_input":"2021-07-26T14:59:28.985649Z","iopub.status.idle":"2021-07-26T14:59:29.003461Z","shell.execute_reply.started":"2021-07-26T14:59:28.985611Z","shell.execute_reply":"2021-07-26T14:59:29.002465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T14:59:29.004946Z","iopub.execute_input":"2021-07-26T14:59:29.005727Z","iopub.status.idle":"2021-07-26T14:59:29.011411Z","shell.execute_reply.started":"2021-07-26T14:59:29.005690Z","shell.execute_reply":"2021-07-26T14:59:29.010573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}