{"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 sklearn.preprocessing import MultiLabelBinarizer\nfrom tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport albumentations\nimport pandas as pd\nimport numpy as np\nimport shutil\nimport os\nfrom sklearn.model_selection import KFold, StratifiedKFold","metadata":{"execution":{"iopub.status.busy":"2021-07-22T02:56:59.855648Z","iopub.execute_input":"2021-07-22T02:56:59.856032Z","iopub.status.idle":"2021-07-22T02:56:59.861621Z","shell.execute_reply.started":"2021-07-22T02:56:59.856001Z","shell.execute_reply":"2021-07-22T02:56:59.860333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport numpy as np\nimport pandas as pd\ntrain = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-22T02:56:59.870805Z","iopub.execute_input":"2021-07-22T02:56:59.871423Z","iopub.status.idle":"2021-07-22T02:56:59.910783Z","shell.execute_reply.started":"2021-07-22T02:56:59.871387Z","shell.execute_reply":"2021-07-22T02:56:59.909761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG():\n    img_size = 299\n    seed = 42\n    classes = ['complex', 'frog_eye_leaf_spot', 'healthy', 'powdery_mildew', 'rust', 'scab']\n    batch_size=16","metadata":{"execution":{"iopub.status.busy":"2021-07-22T02:56:59.912647Z","iopub.execute_input":"2021-07-22T02:56:59.913109Z","iopub.status.idle":"2021-07-22T02:56:59.918700Z","shell.execute_reply.started":"2021-07-22T02:56:59.913040Z","shell.execute_reply":"2021-07-22T02:56:59.917228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')\n\n#train,test = train[0:int(len(paths)*0.8)],train[int(len(paths)*0.8):]\ntrain = pd.DataFrame(train,columns = ['image','labels'])\ntrain['labels'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-22T02:56:59.921665Z","iopub.execute_input":"2021-07-22T02:56:59.922232Z","iopub.status.idle":"2021-07-22T02:56:59.940765Z","shell.execute_reply.started":"2021-07-22T02:56:59.922185Z","shell.execute_reply":"2021-07-22T02:56:59.939588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir='../input/plant-pathology-2021-fgvc8/train_images'\n# submission_dir='../input/plant-pathology-2021-fgvc8/test_images'","metadata":{"execution":{"iopub.status.busy":"2021-07-22T02:56:59.942593Z","iopub.execute_input":"2021-07-22T02:56:59.942926Z","iopub.status.idle":"2021-07-22T02:56:59.954253Z","shell.execute_reply.started":"2021-07-22T02:56:59.942894Z","shell.execute_reply":"2021-07-22T02:56:59.953227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_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-22T02:56:59.955703Z","iopub.execute_input":"2021-07-22T02:56:59.956004Z","iopub.status.idle":"2021-07-22T02:56:59.978663Z","shell.execute_reply.started":"2021-07-22T02:56:59.955967Z","shell.execute_reply":"2021-07-22T02:56:59.977716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['labels'] = submission['labels'].apply(lambda s: s.split(' '))\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-22T02:56:59.980070Z","iopub.execute_input":"2021-07-22T02:56:59.980394Z","iopub.status.idle":"2021-07-22T02:56:59.993138Z","shell.execute_reply.started":"2021-07-22T02:56:59.980363Z","shell.execute_reply":"2021-07-22T02:56:59.992026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MultiLabelBinarizer\nmb = MultiLabelBinarizer().fit(train.labels.apply(lambda x : x.split()))\nlabels = pd.DataFrame(mb.transform(train.labels.apply(lambda x : x.split())), columns = mb.classes_)\nnew_train = pd.concat([train['image'], labels], axis=1)\nnew_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-22T02:56:59.994433Z","iopub.execute_input":"2021-07-22T02:56:59.994721Z","iopub.status.idle":"2021-07-22T02:57:00.284970Z","shell.execute_reply.started":"2021-07-22T02:56:59.994693Z","shell.execute_reply":"2021-07-22T02:57:00.283990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers, optimizers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import Xception\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom sklearn.model_selection import KFold, StratifiedKFold\nfrom sklearn.preprocessing import MultiLabelBinarizer","metadata":{"execution":{"iopub.status.busy":"2021-07-22T02:57:00.288193Z","iopub.execute_input":"2021-07-22T02:57:00.288598Z","iopub.status.idle":"2021-07-22T02:57:00.294253Z","shell.execute_reply.started":"2021-07-22T02:57:00.288564Z","shell.execute_reply":"2021-07-22T02:57:00.293294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nimport cv2\nimport numpy as np\ndef get_cut_image(image):\n    img = cv2.GaussianBlur(image,(3,3),0)\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, (CFG.img_size,CFG.img_size))\n    cut_img = cut_img.astype(\"float32\")*(1.)/255\n    return np.array(cut_img)\ndatagen = 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,\n    preprocessing_function = get_cut_image)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T02:57:00.295832Z","iopub.execute_input":"2021-07-22T02:57:00.296146Z","iopub.status.idle":"2021-07-22T02:57:00.309747Z","shell.execute_reply.started":"2021-07-22T02:57:00.296113Z","shell.execute_reply":"2021-07-22T02:57:00.308677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = datagen.flow_from_dataframe(\n                  submission,\n                  directory = test_img,\n                  x_col = 'image',\n                  y_col = 'labels',\n                  class_mode = \"categorical\",\n                  batch_size=4,\n                  target_size = (CFG.img_size,CFG.img_size),\n                  color_mode=\"rgb\",\n                  shuffle = False,\n                  seed = CFG.seed,\n                  preprocessing_function = get_cut_image\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T02:57:00.311123Z","iopub.execute_input":"2021-07-22T02:57:00.311454Z","iopub.status.idle":"2021-07-22T02:57:00.340722Z","shell.execute_reply.started":"2021-07-22T02:57:00.311424Z","shell.execute_reply":"2021-07-22T02:57:00.339610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\ndef load_model():\n    model_pre = keras.models.load_model(\"../input/0720-cv-n-2/0720_Xception_CV_N_2.h5\")\n    return model_pre","metadata":{"execution":{"iopub.status.busy":"2021-07-22T02:57:00.342357Z","iopub.execute_input":"2021-07-22T02:57:00.342764Z","iopub.status.idle":"2021-07-22T02:57:00.349059Z","shell.execute_reply.started":"2021-07-22T02:57:00.342720Z","shell.execute_reply":"2021-07-22T02:57:00.347733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model()","metadata":{"execution":{"iopub.status.busy":"2021-07-22T02:57:00.350116Z","iopub.execute_input":"2021-07-22T02:57:00.350440Z","iopub.status.idle":"2021-07-22T02:57:06.390820Z","shell.execute_reply.started":"2021-07-22T02:57:00.350409Z","shell.execute_reply":"2021-07-22T02:57:06.389822Z"},"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-22T02:57:06.392649Z","iopub.execute_input":"2021-07-22T02:57:06.392944Z","iopub.status.idle":"2021-07-22T02:57:09.028222Z","shell.execute_reply.started":"2021-07-22T02:57:06.392916Z","shell.execute_reply":"2021-07-22T02:57:09.027184Z"},"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-22T02:57:09.029913Z","iopub.execute_input":"2021-07-22T02:57:09.030520Z","iopub.status.idle":"2021-07-22T02:57:09.037939Z","shell.execute_reply.started":"2021-07-22T02:57:09.030426Z","shell.execute_reply":"2021-07-22T02:57:09.036578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.around(pred, decimals=3, out=None)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T02:57:09.039522Z","iopub.execute_input":"2021-07-22T02:57:09.040041Z","iopub.status.idle":"2021-07-22T02:57:09.053873Z","shell.execute_reply.started":"2021-07-22T02:57:09.040004Z","shell.execute_reply":"2021-07-22T02:57:09.053177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['labels'] = np.array(answer)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-07-22T02:57:09.054859Z","iopub.execute_input":"2021-07-22T02:57:09.055269Z","iopub.status.idle":"2021-07-22T02:57:09.070102Z","shell.execute_reply.started":"2021-07-22T02:57:09.055231Z","shell.execute_reply":"2021-07-22T02:57:09.069408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T02:57:09.071355Z","iopub.execute_input":"2021-07-22T02:57:09.071640Z","iopub.status.idle":"2021-07-22T02:57:09.077222Z","shell.execute_reply.started":"2021-07-22T02:57:09.071612Z","shell.execute_reply":"2021-07-22T02:57:09.076190Z"},"trusted":true},"execution_count":null,"outputs":[]}]}