{"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":"import pandas as pd\nimport numpy as np\ntrain = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')\nprint(train.shape)\ntrain","metadata":{"execution":{"iopub.status.busy":"2021-07-30T00:43:01.6811Z","iopub.execute_input":"2021-07-30T00:43:01.681432Z","iopub.status.idle":"2021-07-30T00:43:01.809974Z","shell.execute_reply.started":"2021-07-30T00:43:01.681362Z","shell.execute_reply":"2021-07-30T00:43:01.809033Z"},"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","metadata":{"execution":{"iopub.status.busy":"2021-07-30T00:43:01.81151Z","iopub.execute_input":"2021-07-30T00:43:01.811855Z","iopub.status.idle":"2021-07-30T00:43:01.831546Z","shell.execute_reply.started":"2021-07-30T00:43:01.811819Z","shell.execute_reply":"2021-07-30T00:43:01.830798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nfrom keras.preprocessing.image import ImageDataGenerator\ndef get_cut_image(image):\n    copy = np.uint8(image)\n    canny = cv2.Canny(copy, 70, 105)\n    box = np.argwhere(canny>0)\n    y1,x1 = box.min(axis=0)\n    y2,x2 = box.max(axis=0)\n    cut_img = image[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)\n\npos = ImageDataGenerator(\n    rescale=1./255,\n    brightness_range=[0.5, 1.5],\n    rotation_range=215, #45\n    shear_range=0.2,\n    zoom_range=0.2, #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)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T00:43:01.833145Z","iopub.execute_input":"2021-07-30T00:43:01.833487Z","iopub.status.idle":"2021-07-30T00:43:06.361844Z","shell.execute_reply.started":"2021-07-30T00:43:01.833453Z","shell.execute_reply":"2021-07-30T00:43:06.361006Z"},"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-30T00:43:06.363293Z","iopub.execute_input":"2021-07-30T00:43:06.363665Z","iopub.status.idle":"2021-07-30T00:43:06.378558Z","shell.execute_reply.started":"2021-07-30T00:43:06.363625Z","shell.execute_reply":"2021-07-30T00:43:06.377546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nmodel = keras.models.load_model(\"../input/fgvc8finetune/prefinetunrB44.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_1 = np.median(predictions, axis=0)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T00:43:06.379949Z","iopub.execute_input":"2021-07-30T00:43:06.380364Z","iopub.status.idle":"2021-07-30T00:43:26.842022Z","shell.execute_reply.started":"2021-07-30T00:43:06.380326Z","shell.execute_reply":"2021-07-30T00:43:26.840868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.load_model(\"../input/fgvc8finetune/prefinetunrB44.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_2 = np.median(predictions, axis=0)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T00:43:26.843897Z","iopub.execute_input":"2021-07-30T00:43:26.844311Z","iopub.status.idle":"2021-07-30T00:43:38.480134Z","shell.execute_reply.started":"2021-07-30T00:43:26.844266Z","shell.execute_reply":"2021-07-30T00:43:38.479264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predmean = (pred_1 + pred_2) / 2.","metadata":{"execution":{"iopub.status.busy":"2021-07-30T00:43:38.483291Z","iopub.execute_input":"2021-07-30T00:43:38.483544Z","iopub.status.idle":"2021-07-30T00:43:38.489774Z","shell.execute_reply.started":"2021-07-30T00:43:38.483519Z","shell.execute_reply":"2021-07-30T00:43:38.488933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.around(predmean, decimals=3, out=None)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T00:43:38.493603Z","iopub.execute_input":"2021-07-30T00:43:38.493852Z","iopub.status.idle":"2021-07-30T00:43:38.503597Z","shell.execute_reply.started":"2021-07-30T00:43:38.493829Z","shell.execute_reply":"2021-07-30T00:43:38.502669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"perdict = (predmean>0.32)\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-30T00:43:38.506075Z","iopub.execute_input":"2021-07-30T00:43:38.506431Z","iopub.status.idle":"2021-07-30T00:43:38.515106Z","shell.execute_reply.started":"2021-07-30T00:43:38.506397Z","shell.execute_reply":"2021-07-30T00:43:38.514202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['labels'] = np.array(answer)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-07-30T00:43:38.517641Z","iopub.execute_input":"2021-07-30T00:43:38.518339Z","iopub.status.idle":"2021-07-30T00:43:38.532061Z","shell.execute_reply.started":"2021-07-30T00:43:38.51788Z","shell.execute_reply":"2021-07-30T00:43:38.531022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-30T00:43:38.533664Z","iopub.execute_input":"2021-07-30T00:43:38.534141Z","iopub.status.idle":"2021-07-30T00:43:38.543142Z","shell.execute_reply.started":"2021-07-30T00:43:38.534101Z","shell.execute_reply":"2021-07-30T00:43:38.542256Z"},"trusted":true},"execution_count":null,"outputs":[]}]}