{"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 matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np\nimport shutil\nimport os","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":0.038447,"end_time":"2021-07-26T14:12:47.155955","exception":false,"start_time":"2021-07-26T14:12:47.117508","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-28T00:47:50.032524Z","iopub.execute_input":"2021-07-28T00:47:50.032975Z","iopub.status.idle":"2021-07-28T00:47:50.045411Z","shell.execute_reply.started":"2021-07-28T00:47:50.032889Z","shell.execute_reply":"2021-07-28T00:47:50.043955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = ['complex', 'frog_eye_leaf_spot', 'powdery_mildew', 'rust', 'scab']\nseed = 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":{"papermill":{"duration":0.123535,"end_time":"2021-07-26T14:12:47.296425","exception":false,"start_time":"2021-07-26T14:12:47.17289","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-28T00:47:50.056290Z","iopub.execute_input":"2021-07-28T00:47:50.056654Z","iopub.status.idle":"2021-07-28T00:47:50.194982Z","shell.execute_reply.started":"2021-07-28T00:47:50.056624Z","shell.execute_reply":"2021-07-28T00:47:50.193915Z"},"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":{"papermill":{"duration":0.157264,"end_time":"2021-07-26T14:12:47.463281","exception":false,"start_time":"2021-07-26T14:12:47.306017","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-28T00:47:50.196530Z","iopub.execute_input":"2021-07-28T00:47:50.196855Z","iopub.status.idle":"2021-07-28T00:47:50.369269Z","shell.execute_reply.started":"2021-07-28T00:47:50.196807Z","shell.execute_reply":"2021-07-28T00:47:50.368222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\npos = ImageDataGenerator(\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)","metadata":{"papermill":{"duration":4.199941,"end_time":"2021-07-26T14:12:51.672603","exception":false,"start_time":"2021-07-26T14:12:47.472662","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-28T00:47:50.370957Z","iopub.execute_input":"2021-07-28T00:47:50.371260Z","iopub.status.idle":"2021-07-28T00:47:56.526628Z","shell.execute_reply.started":"2021-07-28T00:47:50.371232Z","shell.execute_reply":"2021-07-28T00:47:56.525776Z"},"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":{"papermill":{"duration":0.027267,"end_time":"2021-07-26T14:12:51.709065","exception":false,"start_time":"2021-07-26T14:12:51.681798","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-28T00:47:56.527966Z","iopub.execute_input":"2021-07-28T00:47:56.528383Z","iopub.status.idle":"2021-07-28T00:47:56.545991Z","shell.execute_reply.started":"2021-07-28T00:47:56.528353Z","shell.execute_reply":"2021-07-28T00:47:56.544663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\ndef load_model():\n    model_pre = keras.models.load_model(\"../input/b4-3f-cv-n0h0/B4_4Fold_cv_255_healthyO_3.h5\",compile=False) #compile=False\n    return model_pre","metadata":{"papermill":{"duration":0.015172,"end_time":"2021-07-26T14:12:51.733407","exception":false,"start_time":"2021-07-26T14:12:51.718235","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-28T00:47:56.547170Z","iopub.execute_input":"2021-07-28T00:47:56.547450Z","iopub.status.idle":"2021-07-28T00:47:56.551900Z","shell.execute_reply.started":"2021-07-28T00:47:56.547421Z","shell.execute_reply":"2021-07-28T00:47:56.551137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model()","metadata":{"execution":{"iopub.status.busy":"2021-07-28T00:47:56.552972Z","iopub.execute_input":"2021-07-28T00:47:56.553410Z","iopub.status.idle":"2021-07-28T00:48:04.066398Z","shell.execute_reply.started":"2021-07-28T00:47:56.553366Z","shell.execute_reply":"2021-07-28T00:48:04.064944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test_generator)","metadata":{"papermill":{"duration":18.183606,"end_time":"2021-07-26T14:13:09.926086","exception":false,"start_time":"2021-07-26T14:12:51.74248","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-28T00:48:04.067697Z","iopub.execute_input":"2021-07-28T00:48:04.067983Z","iopub.status.idle":"2021-07-28T00:48:09.716652Z","shell.execute_reply.started":"2021-07-28T00:48:04.067955Z","shell.execute_reply":"2021-07-28T00:48:09.715481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.around(pred, decimals=3, out=None)","metadata":{"papermill":{"duration":0.022063,"end_time":"2021-07-26T14:13:09.958064","exception":false,"start_time":"2021-07-26T14:13:09.936001","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-28T00:48:09.719559Z","iopub.execute_input":"2021-07-28T00:48:09.720064Z","iopub.status.idle":"2021-07-28T00:48:09.727446Z","shell.execute_reply.started":"2021-07-28T00:48:09.720019Z","shell.execute_reply":"2021-07-28T00:48:09.726132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"perdict = (pred>0.33)\nn_label = classes\nanswer = []\n\nfor i in range(perdict.shape[0]):\n    temp = []\n    for j, k in enumerate(n_label):\n\n        if perdict[i, j]:\n            temp.append(k)\n    if temp == []:\n        temp.append('healthy')\n    answer.append(temp)    \nanswer = [' '.join(n) for n in answer]","metadata":{"papermill":{"duration":0.02027,"end_time":"2021-07-26T14:13:09.991333","exception":false,"start_time":"2021-07-26T14:13:09.971063","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-28T00:48:09.729518Z","iopub.execute_input":"2021-07-28T00:48:09.730138Z","iopub.status.idle":"2021-07-28T00:48:09.752095Z","shell.execute_reply.started":"2021-07-28T00:48:09.730085Z","shell.execute_reply":"2021-07-28T00:48:09.751080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['labels'] = np.array(answer)\nsubmission","metadata":{"papermill":{"duration":0.024079,"end_time":"2021-07-26T14:13:10.05288","exception":false,"start_time":"2021-07-26T14:13:10.028801","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-28T00:48:09.754076Z","iopub.execute_input":"2021-07-28T00:48:09.754440Z","iopub.status.idle":"2021-07-28T00:48:09.779151Z","shell.execute_reply.started":"2021-07-28T00:48:09.754409Z","shell.execute_reply":"2021-07-28T00:48:09.778338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"papermill":{"duration":0.025405,"end_time":"2021-07-26T14:13:10.088877","exception":false,"start_time":"2021-07-26T14:13:10.063472","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-28T00:48:09.780261Z","iopub.execute_input":"2021-07-28T00:48:09.780713Z","iopub.status.idle":"2021-07-28T00:48:09.788661Z","shell.execute_reply.started":"2021-07-28T00:48:09.780670Z","shell.execute_reply":"2021-07-28T00:48:09.787608Z"},"trusted":true},"execution_count":null,"outputs":[]}]}