{"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 numpy as np\nimport pandas as pd\nimport cv2\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set_style('darkgrid')\n\nimport tensorflow\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential,load_model\nfrom tensorflow.keras.layers import Dense,GlobalAveragePooling2D,Flatten,Conv2D,BatchNormalization,Dropout,MaxPooling2D,Activation\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator as Imgen\n\nfrom PIL import Image\nfrom sklearn.metrics import confusion_matrix,classification_report\n\nphysical_devices = tensorflow.config.list_physical_devices('GPU')\ntensorflow.config.experimental.set_memory_growth(physical_devices[0], True)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-14T14:58:01.039719Z","iopub.execute_input":"2021-07-14T14:58:01.040085Z","iopub.status.idle":"2021-07-14T14:58:06.235127Z","shell.execute_reply.started":"2021-07-14T14:58:01.040055Z","shell.execute_reply":"2021-07-14T14:58:06.234122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(\"../input/plant-pathology-2021-fgvc8/train.csv\") as f:\n    lines = f.readlines()\n\nclasses = {}\ntrain_x, tmp_y = [], []\nfor id, line in enumerate(lines):\n    line = line.replace('\\n', '')\n    if id>0:\n        train_x.append(line.split(',')[0])\n        cs = line.split(',')[1].split(' ')\n        for c in cs:\n            if c not in classes:\n                classes.update( { c: len(classes)})\n                \n        tmp_y.append(cs)\n\n\ntrain_y = []\ntrain_y_columns = []\nfor c in classes:\n    train_y_columns.append(c)\n\n\nfor iid, y in enumerate(tmp_y):\n    labels = [train_x[iid]]\n    for id, label in enumerate(classes):\n        \n        if label in y:\n            labels = labels + [1]\n        else:\n            labels = labels + [0]\n    \n    train_y.append(labels)\n       \n#print(list(zip(train_x, train_y)))    \n#lst = list(zip(train_x, train_y))\n\n    \ntrain_df = pd.DataFrame(train_y, columns =['image'] + train_y_columns )\n","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:58:06.239157Z","iopub.execute_input":"2021-07-14T14:58:06.2395Z","iopub.status.idle":"2021-07-14T14:58:06.540964Z","shell.execute_reply.started":"2021-07-14T14:58:06.239467Z","shell.execute_reply":"2021-07-14T14:58:06.54013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:58:06.542468Z","iopub.execute_input":"2021-07-14T14:58:06.542819Z","iopub.status.idle":"2021-07-14T14:58:06.568024Z","shell.execute_reply.started":"2021-07-14T14:58:06.542787Z","shell.execute_reply":"2021-07-14T14:58:06.566965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for cname in classes:\n    print(train_df[cname].value_counts())\n    print('-------------------------------------------')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:58:06.56961Z","iopub.execute_input":"2021-07-14T14:58:06.570041Z","iopub.status.idle":"2021-07-14T14:58:06.58812Z","shell.execute_reply.started":"2021-07-14T14:58:06.569998Z","shell.execute_reply":"2021-07-14T14:58:06.587057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classes)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:58:06.866144Z","iopub.execute_input":"2021-07-14T14:58:06.866418Z","iopub.status.idle":"2021-07-14T14:58:06.870573Z","shell.execute_reply.started":"2021-07-14T14:58:06.866393Z","shell.execute_reply":"2021-07-14T14:58:06.869771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for testimg in ['800113bb65efe69e.jpg', '80070f7fb5e2ccaa.jpg', '80077517781fb94f.jpg', '800cbf0ff87721f8.jpg']:\n    img = cv2.imread('../input/plant-pathology-2021-fgvc8/train_images/{}'.format(testimg))\n    print(img.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:58:08.046863Z","iopub.execute_input":"2021-07-14T14:58:08.047175Z","iopub.status.idle":"2021-07-14T14:58:08.609478Z","shell.execute_reply.started":"2021-07-14T14:58:08.047147Z","shell.execute_reply":"2021-07-14T14:58:08.608627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')\nsample.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:58:09.553269Z","iopub.execute_input":"2021-07-14T14:58:09.5536Z","iopub.status.idle":"2021-07-14T14:58:09.569795Z","shell.execute_reply.started":"2021-07-14T14:58:09.553572Z","shell.execute_reply":"2021-07-14T14:58:09.568895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:58:10.601602Z","iopub.execute_input":"2021-07-14T14:58:10.601999Z","iopub.status.idle":"2021-07-14T14:58:10.61249Z","shell.execute_reply.started":"2021-07-14T14:58:10.601966Z","shell.execute_reply":"2021-07-14T14:58:10.611356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = (448,448)\nbatch_size=64\nepochs = 30","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:58:11.595715Z","iopub.execute_input":"2021-07-14T14:58:11.596055Z","iopub.status.idle":"2021-07-14T14:58:11.600092Z","shell.execute_reply.started":"2021-07-14T14:58:11.596026Z","shell.execute_reply":"2021-07-14T14:58:11.599033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ../input/detector/","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:58:13.040196Z","iopub.execute_input":"2021-07-14T14:58:13.040549Z","iopub.status.idle":"2021-07-14T14:58:13.732064Z","shell.execute_reply.started":"2021-07-14T14:58:13.040516Z","shell.execute_reply":"2021-07-14T14:58:13.731182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"det_model_path = \"../input/detector/detector.h5\"\ndet_image_size = (224,224)\ndet_model = load_model(det_model_path)\n\ndef detect_leaf(image):\n    #print('image', image.shape)\n    image = cv2.resize(image, det_image_size)\n    # make bounding box predictions on the input image\n    preds = det_model.predict( np.array([image])/255) [0]\n    (startX, startY, endX, endY) = preds\n    (h, w) = image.shape[:2]\n\n    # scale the predicted bounding box coordinates based on the image\n    # dimensions\n    startX = int(startX * w)\n    startY = int(startY * h)\n    endX = int(endX * w)\n    endY = int(endY * h)\n    #print( (startX, startY, endX, endY))\n    crop = image[startY:endY, startX:endX]\n    crop = cv2.resize(crop, train_size)\n    #print('crop', crop.shape)\n    return crop","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:58:15.83078Z","iopub.execute_input":"2021-07-14T14:58:15.831149Z","iopub.status.idle":"2021-07-14T14:58:19.390544Z","shell.execute_reply.started":"2021-07-14T14:58:15.831114Z","shell.execute_reply":"2021-07-14T14:58:19.389524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = Imgen( preprocessing_function=detect_leaf,\n                 rotation_range=4,\n                  shear_range=0.2,\n                  zoom_range=0.2,\n                  horizontal_flip=True,\n                  validation_split=0.2,\n                  rescale=1./255\n                 )","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:58:19.394247Z","iopub.execute_input":"2021-07-14T14:58:19.394541Z","iopub.status.idle":"2021-07-14T14:58:19.402132Z","shell.execute_reply.started":"2021-07-14T14:58:19.394512Z","shell.execute_reply":"2021-07-14T14:58:19.401178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = datagen.flow_from_dataframe(\n    train_df,\n    directory = '../input/plant-pathology-2021-fgvc8/train_images', \n    x_col = 'image',\n    y_col = train_y_columns,\n    subset=\"training\",\n    color_mode=\"rgb\",\n    target_size = train_size,\n    class_mode=\"raw\",\n    batch_size=batch_size,\n    shuffle=True,\n    seed=123,\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:58:19.406124Z","iopub.execute_input":"2021-07-14T14:58:19.406374Z","iopub.status.idle":"2021-07-14T14:58:46.787815Z","shell.execute_reply.started":"2021-07-14T14:58:19.406351Z","shell.execute_reply":"2021-07-14T14:58:46.786874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_ds = datagen.flow_from_dataframe(\n    train_df,\n    directory = '../input/plant-pathology-2021-fgvc8/train_images',\n    x_col = 'image',\n    y_col = train_y_columns,\n    subset=\"validation\",\n    color_mode=\"rgb\",\n    target_size = train_size,\n    class_mode=\"raw\",\n    batch_size=batch_size,\n    shuffle=True,\n    seed=123,\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:58:46.789524Z","iopub.execute_input":"2021-07-14T14:58:46.789844Z","iopub.status.idle":"2021-07-14T14:58:53.179914Z","shell.execute_reply.started":"2021-07-14T14:58:46.789814Z","shell.execute_reply":"2021-07-14T14:58:53.178691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x,y = next(train_ds)\nx.shape\ny.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:58:53.181404Z","iopub.execute_input":"2021-07-14T14:58:53.182022Z","iopub.status.idle":"2021-07-14T14:59:15.43322Z","shell.execute_reply.started":"2021-07-14T14:58:53.181957Z","shell.execute_reply":"2021-07-14T14:59:15.432233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot function\ndef plot_images(img,labels):\n    plt.figure(figsize=(20,8))\n    for i in range(10):\n        plt.subplot(2,5,i+1)\n        #plt.imshow(img[i])\n        plt.imshow((img[i]*255).astype(np.uint8))\n        txt = ''\n        for id, lab in enumerate(labels[i]):\n            if lab == 1:\n                for cn in classes:\n                    if classes[cn] == id:\n                        txt += cn + ','\n                \n        plt.title(txt)\n        plt.axis('off')\n\nx,y = next(train_ds)\nplot_images(x,y)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:59:15.435057Z","iopub.execute_input":"2021-07-14T14:59:15.43547Z","iopub.status.idle":"2021-07-14T14:59:31.108035Z","shell.execute_reply.started":"2021-07-14T14:59:15.435431Z","shell.execute_reply":"2021-07-14T14:59:31.10581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import BatchNormalization\n\n'''\nmodel = Sequential([\n    \n    Conv2D(32,(3,3),activation='relu',input_shape=(train_size[1],train_size[0],3)),\n    MaxPooling2D((2,2)),\n    #BatchNormalization(),\n    Conv2D(64,(3,3),activation='relu'),\n    MaxPooling2D((2,2)),\n    #BatchNormalization(),\n    Conv2D(64,(3,3),activation='relu'),\n    MaxPooling2D((2,2)),\n    #BatchNormalization(),\n    Conv2D(128,(3,3),activation='relu'),\n    MaxPooling2D((2,2)),\n    #BatchNormalization(),\n    tensorflow.keras.layers.GlobalAveragePooling2D(),\n    Dense(12,activation='softmax')\n    \n])\n'''\nmodel = Sequential([\n    \n    Conv2D(32,(3,3),activation='relu',input_shape=(train_size[1],train_size[0],3)),\n    MaxPooling2D((2,2)),\n    \n    Conv2D(64,(3,3),activation='relu'),\n    MaxPooling2D((2,2)),\n    \n    Conv2D(64,(3,3),activation='relu'),\n    MaxPooling2D((2,2)),\n    \n    Conv2D(128,(3,3),activation='relu'),\n    MaxPooling2D((2,2)),\n    \n    tensorflow.keras.layers.GlobalAveragePooling2D(),\n    Dense(len(train_y_columns),activation='sigmoid')\n    \n])","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:59:31.109939Z","iopub.execute_input":"2021-07-14T14:59:31.110252Z","iopub.status.idle":"2021-07-14T14:59:31.18038Z","shell.execute_reply.started":"2021-07-14T14:59:31.11022Z","shell.execute_reply":"2021-07-14T14:59:31.179594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:59:31.18188Z","iopub.execute_input":"2021-07-14T14:59:31.182224Z","iopub.status.idle":"2021-07-14T14:59:31.194658Z","shell.execute_reply.started":"2021-07-14T14:59:31.182187Z","shell.execute_reply":"2021-07-14T14:59:31.193851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sudo apt install graphviz\n# pip install pydot\ntensorflow.keras.utils.plot_model(model,\n                      show_shapes=True,\n                      show_dtype=True,\n                      show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:59:31.196665Z","iopub.execute_input":"2021-07-14T14:59:31.197195Z","iopub.status.idle":"2021-07-14T14:59:31.657514Z","shell.execute_reply.started":"2021-07-14T14:59:31.197155Z","shell.execute_reply":"2021-07-14T14:59:31.65666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:59:31.660655Z","iopub.execute_input":"2021-07-14T14:59:31.660949Z","iopub.status.idle":"2021-07-14T14:59:31.675212Z","shell.execute_reply.started":"2021-07-14T14:59:31.660918Z","shell.execute_reply":"2021-07-14T14:59:31.674359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_calls = [keras.callbacks.EarlyStopping(monitor='val_accuracy',patience=3),\n            keras.callbacks.ModelCheckpoint(\"Model_xcp.h5\",verbose=1,save_best_only=True)]","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:59:31.678106Z","iopub.execute_input":"2021-07-14T14:59:31.678443Z","iopub.status.idle":"2021-07-14T14:59:31.685069Z","shell.execute_reply.started":"2021-07-14T14:59:31.678361Z","shell.execute_reply":"2021-07-14T14:59:31.684305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tensorflow.keras.models.load_model('../input/trainedlocal/last_crop.h5')\n#hist = model.fit(train_ds,epochs=epochs,validation_data=val_ds,callbacks=my_calls)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:59:31.687979Z","iopub.execute_input":"2021-07-14T14:59:31.688243Z","iopub.status.idle":"2021-07-14T14:59:31.857715Z","shell.execute_reply.started":"2021-07-14T14:59:31.688219Z","shell.execute_reply":"2021-07-14T14:59:31.856954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('final_crop.h5')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:57:18.925298Z","iopub.execute_input":"2021-07-14T14:57:18.925688Z","iopub.status.idle":"2021-07-14T14:57:18.990671Z","shell.execute_reply.started":"2021-07-14T14:57:18.925603Z","shell.execute_reply":"2021-07-14T14:57:18.989361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nplt.figure(figsize=(15,6))\n\nplt.subplot(1,2,1)\nplt.plot(hist.epoch,hist.history['accuracy'],label = 'Training')\nplt.plot(hist.epoch,hist.history['val_accuracy'],label = 'validation')\n\nplt.title(\"Accuracy\")\nplt.legend()\n\nplt.subplot(1,2,2)\nplt.plot(hist.epoch,hist.history['loss'],label = 'Training')\nplt.plot(hist.epoch,hist.history['val_loss'],label = 'validation')\n\nplt.title(\"Loss\")\nplt.legend()\nplt.show()\n'''","metadata":{"execution":{"iopub.status.busy":"2021-07-14T14:59:44.166515Z","iopub.execute_input":"2021-07-14T14:59:44.166864Z","iopub.status.idle":"2021-07-14T14:59:44.653356Z","shell.execute_reply.started":"2021-07-14T14:59:44.166834Z","shell.execute_reply":"2021-07-14T14:59:44.651882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_images(img,labels):\n        \n    plt.figure(figsize=(20,8))\n    for i in range(3):\n        plt.subplot(2,5,i+1)\n        \n        plt.imshow((img[i]).astype(np.uint8))\n        txt = labels[i]\n        for id, lab in enumerate(labels[i]):\n            if lab == 1:\n                for cn in classes:\n                    if classes[cn] == id:\n                        txt += cn + ', '\n                \n        plt.title(txt)\n        plt.axis('off')\n\ndef get_cname(pred):        \n    txt = ''\n    detected = False\n    for id, p in enumerate(pred):\n        if p>0.35:\n            lab = 1\n        else:\n            lab = 0\n            \n        if lab == 1:\n            detected = True\n            for cn in classes:\n                if classes[cn] == id:\n                    txt += cn + ' '        \n\n                    \n    return txt\n\n\ntest_imgs = []\nimg_names = []\npredicts = []\nfor imgname in sample[\"image\"]:\n    img_names.append(imgname)\n    img_path = os.path.join('../input/plant-pathology-2021-fgvc8/test_images', imgname)\n    print(img_path)\n    \n    img = cv2.imread(img_path)  \n    test_imgs.append( img )\n    \n    crop = detect_leaf(img)\n    \n    crop = cv2.resize(crop, train_size)\n    \n    crop = np.array([crop])/255\n    pred = model.predict( crop )\n    predicted = get_cname(pred[0])\n    predicts.append(predicted)\n    \n#plot_images(test_imgs,predicts)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T15:00:53.127683Z","iopub.execute_input":"2021-07-14T15:00:53.128036Z","iopub.status.idle":"2021-07-14T15:00:57.937894Z","shell.execute_reply.started":"2021-07-14T15:00:53.128007Z","shell.execute_reply":"2021-07-14T15:00:57.937088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('../input/plant-pathology-2021-fgvc8/sample_submission.csv') as s:\n    lines = s.readlines()\n\nwith open('submission.csv', 'w') as file:    \n    for id, line in enumerate(lines):\n        if id==0:\n            file.write(line)\n        else:\n            imgfile = line.split(',')[0]\n            newline = '{},{}\\n'.format(imgfile, predicts[id-1] )\n            file.write(newline)\n\n!cat submission.csv            ","metadata":{"execution":{"iopub.status.busy":"2021-07-14T15:01:30.962171Z","iopub.execute_input":"2021-07-14T15:01:30.962544Z","iopub.status.idle":"2021-07-14T15:01:31.634358Z","shell.execute_reply.started":"2021-07-14T15:01:30.962509Z","shell.execute_reply":"2021-07-14T15:01:31.633322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\napi_token= {\"username\":\"chenghsuntseng\",\"key\":\"44cfe92076e822340cdabfa0083ae642\"} #請換成你自己的kaggle認證#請換成你自己的kaggle認證\nimport json\nimport zipfile\nimport os\n\n\nif not os.path.exists(\"/root/.kaggle\"):\n    os.makedirs(\"/root/.kaggle\")\n \nwith open('/root/.kaggle/kaggle.json', 'w') as file:\n    json.dump(api_token, file)\n!chmod 600 /root/.kaggle/kaggle.json\n\n\nif not os.path.exists(\"/kaggle\"):\n    os.makedirs(\"/kaggle\")\n\n\n!kaggle competitions submit -c plant-pathology-2021-fgvc8 -f 'submission.csv' -m 'V1_crop'\n'''","metadata":{"execution":{"iopub.status.busy":"2021-07-14T15:01:37.186414Z","iopub.execute_input":"2021-07-14T15:01:37.186777Z","iopub.status.idle":"2021-07-14T15:01:41.3208Z","shell.execute_reply.started":"2021-07-14T15:01:37.186721Z","shell.execute_reply":"2021-07-14T15:01:41.319661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}