{"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-26T13:11:20.099539Z","iopub.execute_input":"2021-07-26T13:11:20.099917Z","iopub.status.idle":"2021-07-26T13:11:20.105754Z","shell.execute_reply.started":"2021-07-26T13:11:20.099885Z","shell.execute_reply":"2021-07-26T13:11:20.104853Z"},"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-26T13:11:20.108225Z","iopub.execute_input":"2021-07-26T13:11:20.108770Z","iopub.status.idle":"2021-07-26T13:11:20.143804Z","shell.execute_reply.started":"2021-07-26T13:11:20.108720Z","shell.execute_reply":"2021-07-26T13:11:20.142846Z"},"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-26T13:11:20.145358Z","iopub.execute_input":"2021-07-26T13:11:20.145707Z","iopub.status.idle":"2021-07-26T13:11:20.158943Z","shell.execute_reply.started":"2021-07-26T13:11:20.145673Z","shell.execute_reply":"2021-07-26T13:11:20.157865Z"},"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-26T13:11:20.160879Z","iopub.execute_input":"2021-07-26T13:11:20.161215Z","iopub.status.idle":"2021-07-26T13:11:20.167649Z","shell.execute_reply.started":"2021-07-26T13:11:20.161181Z","shell.execute_reply":"2021-07-26T13:11:20.166505Z"},"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)\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)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T13:11:20.169376Z","iopub.execute_input":"2021-07-26T13:11:20.169990Z","iopub.status.idle":"2021-07-26T13:11:20.179757Z","shell.execute_reply.started":"2021-07-26T13:11:20.169941Z","shell.execute_reply":"2021-07-26T13:11:20.178947Z"},"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-26T13:11:20.181470Z","iopub.execute_input":"2021-07-26T13:11:20.181985Z","iopub.status.idle":"2021-07-26T13:11:20.197370Z","shell.execute_reply.started":"2021-07-26T13:11:20.181949Z","shell.execute_reply":"2021-07-26T13:11:20.196435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Efficientnetb4","metadata":{}},{"cell_type":"code","source":"model = keras.models.load_model(\"../input/07300946/posrandomsampleB4.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_EffB4 = np.mean(predictions, axis=0)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T13:11:20.198712Z","iopub.execute_input":"2021-07-26T13:11:20.199332Z","iopub.status.idle":"2021-07-26T13:11:35.730482Z","shell.execute_reply.started":"2021-07-26T13:11:20.199295Z","shell.execute_reply":"2021-07-26T13:11:35.729602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Xception","metadata":{}},{"cell_type":"code","source":"model = keras.models.load_model(\"../input/prexception/5.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_Xception = np.mean(predictions, axis=0)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T13:11:35.731807Z","iopub.execute_input":"2021-07-26T13:11:35.732145Z","iopub.status.idle":"2021-07-26T13:11:51.768228Z","shell.execute_reply.started":"2021-07-26T13:11:35.732108Z","shell.execute_reply":"2021-07-26T13:11:51.767198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"predmean = (pred_EffB4 + pred_Xception) / 2.","metadata":{"execution":{"iopub.status.busy":"2021-07-26T13:11:51.770631Z","iopub.execute_input":"2021-07-26T13:11:51.771017Z","iopub.status.idle":"2021-07-26T13:11:51.775083Z","shell.execute_reply.started":"2021-07-26T13:11:51.770977Z","shell.execute_reply":"2021-07-26T13:11:51.774246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.around(predmean, decimals=2)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T13:11:51.776950Z","iopub.execute_input":"2021-07-26T13:11:51.777547Z","iopub.status.idle":"2021-07-26T13:11:51.788478Z","shell.execute_reply.started":"2021-07-26T13:11:51.777512Z","shell.execute_reply":"2021-07-26T13:11:51.787450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"perdict = (predmean>0.35)\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-26T13:11:51.789994Z","iopub.execute_input":"2021-07-26T13:11:51.790636Z","iopub.status.idle":"2021-07-26T13:11:51.798539Z","shell.execute_reply.started":"2021-07-26T13:11:51.790563Z","shell.execute_reply":"2021-07-26T13:11:51.797736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['labels'] = np.array(answer)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-07-26T13:12:45.883471Z","iopub.execute_input":"2021-07-26T13:12:45.883869Z","iopub.status.idle":"2021-07-26T13:12:45.897371Z","shell.execute_reply.started":"2021-07-26T13:12:45.883829Z","shell.execute_reply":"2021-07-26T13:12:45.896218Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T13:13:29.145718Z","iopub.execute_input":"2021-07-26T13:13:29.146076Z","iopub.status.idle":"2021-07-26T13:13:29.154256Z","shell.execute_reply.started":"2021-07-26T13:13:29.146045Z","shell.execute_reply":"2021-07-26T13:13:29.153435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}