{"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\nimport keras","metadata":{"execution":{"iopub.status.busy":"2021-07-30T02:36:46.459775Z","iopub.execute_input":"2021-07-30T02:36:46.460154Z","iopub.status.idle":"2021-07-30T02:36:46.464782Z","shell.execute_reply.started":"2021-07-30T02:36:46.460119Z","shell.execute_reply":"2021-07-30T02:36:46.463988Z"},"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-30T02:36:46.466073Z","iopub.execute_input":"2021-07-30T02:36:46.466570Z","iopub.status.idle":"2021-07-30T02:36:46.511474Z","shell.execute_reply.started":"2021-07-30T02:36:46.466518Z","shell.execute_reply":"2021-07-30T02:36:46.510482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 32\ntarget_size = (380, 380)\n# target_size = (299, 299)\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-30T02:36:46.512957Z","iopub.execute_input":"2021-07-30T02:36:46.513433Z","iopub.status.idle":"2021-07-30T02:36:46.527395Z","shell.execute_reply.started":"2021-07-30T02:36:46.513385Z","shell.execute_reply":"2021-07-30T02:36:46.526104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import cv2\n# import numpy as np\n# def 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-30T02:36:46.528996Z","iopub.execute_input":"2021-07-30T02:36:46.529302Z","iopub.status.idle":"2021-07-30T02:36:46.533353Z","shell.execute_reply.started":"2021-07-30T02:36:46.529265Z","shell.execute_reply":"2021-07-30T02:36:46.532245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n## v0\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)\n## V1\n# pos = 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-30T02:36:46.534956Z","iopub.execute_input":"2021-07-30T02:36:46.535257Z","iopub.status.idle":"2021-07-30T02:36:46.550180Z","shell.execute_reply.started":"2021-07-30T02:36:46.535227Z","shell.execute_reply":"2021-07-30T02:36:46.549170Z"},"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-30T02:36:46.551848Z","iopub.execute_input":"2021-07-30T02:36:46.552437Z","iopub.status.idle":"2021-07-30T02:36:46.583328Z","shell.execute_reply.started":"2021-07-30T02:36:46.552396Z","shell.execute_reply":"2021-07-30T02:36:46.581404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## M_1","metadata":{}},{"cell_type":"code","source":"model = keras.models.load_model(\"../input/b4cvk20726/B4(oversample)4Fold_cv_BC_2552.h5\",compile=False)\ntta_steps = 5\npredictions = []\n\nfor i in range(tta_steps):\n    preds = model.predict(test_generator)\n    predictions.append(preds)\n\npred_M1 = np.mean(predictions, axis=0)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T02:36:46.585056Z","iopub.execute_input":"2021-07-30T02:36:46.585564Z","iopub.status.idle":"2021-07-30T02:37:07.971155Z","shell.execute_reply.started":"2021-07-30T02:36:46.585514Z","shell.execute_reply":"2021-07-30T02:37:07.970137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## M_2","metadata":{}},{"cell_type":"code","source":"model = keras.models.load_model(\"../input/m1-test123-m2sample/B4_labeln2_cv.h5\",compile=False)\ntta_steps = 5\npredictions = []\n\nfor i in range(tta_steps):\n    preds = model.predict(test_generator)\n    predictions.append(preds)\n\npred_M2 = np.mean(predictions, axis=0)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T02:37:07.973388Z","iopub.execute_input":"2021-07-30T02:37:07.973813Z","iopub.status.idle":"2021-07-30T02:37:29.952419Z","shell.execute_reply.started":"2021-07-30T02:37:07.973762Z","shell.execute_reply":"2021-07-30T02:37:29.951295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predmean = (pred_M1 + pred_M2) / 2.","metadata":{"execution":{"iopub.status.busy":"2021-07-30T02:37:29.954246Z","iopub.execute_input":"2021-07-30T02:37:29.954679Z","iopub.status.idle":"2021-07-30T02:37:29.960182Z","shell.execute_reply.started":"2021-07-30T02:37:29.954631Z","shell.execute_reply":"2021-07-30T02:37:29.958854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.around(predmean, decimals=2)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T02:37:29.963206Z","iopub.execute_input":"2021-07-30T02:37:29.963649Z","iopub.status.idle":"2021-07-30T02:37:29.976874Z","shell.execute_reply.started":"2021-07-30T02:37:29.963612Z","shell.execute_reply":"2021-07-30T02:37:29.975818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"perdict = (predmean>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]\n\nsubmission['labels'] = np.array(answer)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-07-30T02:37:29.978070Z","iopub.execute_input":"2021-07-30T02:37:29.978547Z","iopub.status.idle":"2021-07-30T02:37:29.996887Z","shell.execute_reply.started":"2021-07-30T02:37:29.978489Z","shell.execute_reply":"2021-07-30T02:37:29.995697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T02:37:29.998331Z","iopub.execute_input":"2021-07-30T02:37:29.998736Z","iopub.status.idle":"2021-07-30T02:37:30.016477Z","shell.execute_reply.started":"2021-07-30T02:37:29.998690Z","shell.execute_reply":"2021-07-30T02:37:30.015458Z"},"trusted":true},"execution_count":null,"outputs":[]}]}