{"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\nimport tensorflow as tf\nimport tensorflow.keras as keras\nimport PIL\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nimport random\nfrom tqdm import tqdm\nimport tensorflow_addons as tfa\nimport random\nfrom sklearn.preprocessing import MultiLabelBinarizer\nfrom sklearn.metrics import f1_score\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport keras\nfrom keras.preprocessing import image\nfrom keras.models import Sequential\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array, smart_resize\nfrom keras.layers import Dense, Dropout, Flatten, BatchNormalization, Activation\nfrom keras.constraints import maxnorm\nfrom keras.layers.convolutional import Conv2D, MaxPooling2D\nfrom tensorflow.keras.optimizers import Adam\nimport cv2\nfrom PIL import Image\nfrom keras.preprocessing.image import load_img, img_to_array\nfrom keras.models import load_model\nfrom keras.metrics import AUC\nfrom tqdm.auto import tqdm\nsns.set_style('darkgrid')\npd.set_option(\"display.max_columns\", None)","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:10:01.433135Z","iopub.execute_input":"2022-05-24T01:10:01.433512Z","iopub.status.idle":"2022-05-24T01:10:07.214747Z","shell.execute_reply.started":"2022-05-24T01:10:01.433434Z","shell.execute_reply":"2022-05-24T01:10:07.213821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.applications.resnet50 import preprocess_input","metadata":{"execution":{"iopub.status.busy":"2022-05-13T08:24:44.528676Z","iopub.execute_input":"2022-05-13T08:24:44.529042Z","iopub.status.idle":"2022-05-13T08:24:44.533282Z","shell.execute_reply.started":"2022-05-13T08:24:44.529007Z","shell.execute_reply":"2022-05-13T08:24:44.532067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_dir= '../input/resized-plant2021/img_sz_384'\ntrain_dir= \"../input/plant-pathology-2021-fgvc8/train_images\"\ntest_dir =  '../input/plant-pathology-2021-fgvc8/test_images'\ntrain = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:10:16.858776Z","iopub.execute_input":"2022-05-24T01:10:16.859093Z","iopub.status.idle":"2022-05-24T01:10:16.898072Z","shell.execute_reply.started":"2022-05-24T01:10:16.859064Z","shell.execute_reply":"2022-05-24T01:10:16.897196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['labels'] = train['labels'].apply(lambda string: string.split(' '))","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:10:19.761232Z","iopub.execute_input":"2022-05-24T01:10:19.761587Z","iopub.status.idle":"2022-05-24T01:10:19.784141Z","shell.execute_reply.started":"2022-05-24T01:10:19.761554Z","shell.execute_reply":"2022-05-24T01:10:19.783315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = list(train['labels'])\nmlb = MultiLabelBinarizer()\ntrainx = pd.DataFrame(mlb.fit_transform(s), columns=mlb.classes_, index=train.index)\nprint(trainx.columns)","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:10:22.251914Z","iopub.execute_input":"2022-05-24T01:10:22.252234Z","iopub.status.idle":"2022-05-24T01:10:22.283087Z","shell.execute_reply.started":"2022-05-24T01:10:22.252203Z","shell.execute_reply":"2022-05-24T01:10:22.282251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.concat([train['image'], trainx], axis=1)\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:10:25.290427Z","iopub.execute_input":"2022-05-24T01:10:25.290921Z","iopub.status.idle":"2022-05-24T01:10:25.313942Z","shell.execute_reply.started":"2022-05-24T01:10:25.290884Z","shell.execute_reply":"2022-05-24T01:10:25.313225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_datagen = ImageDataGenerator(\n    rescale=1/255.0,\n    rotation_range=5,\n    zoom_range=0.1,\n    shear_range=0.05,\n    horizontal_flip=True\n    \n)\ntest_image_datagen = ImageDataGenerator(\n    rescale=1/255.0 \n)","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:10:32.64888Z","iopub.execute_input":"2022-05-24T01:10:32.64921Z","iopub.status.idle":"2022-05-24T01:10:32.65439Z","shell.execute_reply.started":"2022-05-24T01:10:32.64918Z","shell.execute_reply":"2022-05-24T01:10:32.653357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#数据集分割\nlabels_train,labels_test= train_test_split(labels, test_size=0.1, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:10:36.000486Z","iopub.execute_input":"2022-05-24T01:10:36.000918Z","iopub.status.idle":"2022-05-24T01:10:36.012216Z","shell.execute_reply.started":"2022-05-24T01:10:36.000865Z","shell.execute_reply":"2022-05-24T01:10:36.011093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#新数据增强\nIMAGE = (256, 256)\nBATCH_SIZE = 64\n\ntrain_data = train_image_datagen.flow_from_dataframe(\n    dataframe=labels_train,\n    directory= '../input/resized-plant2021/img_sz_512',\n    x_col=\"image\",\n    y_col=labels_train.columns.tolist()[1:],\n    color_mode=\"rgb\",\n    target_size = IMAGE,\n    class_mode=\"raw\",\n    #class_mode=\"categorical\",\n    #subset = \"training\",\n    batch_size=BATCH_SIZE,\n    seed=0,\n    shuffle=False\n)\n\ntest_data = test_image_datagen.flow_from_dataframe(\n    dataframe=labels_test,\n    directory= '../input/resized-plant2021/img_sz_512',\n    x_col=\"image\",\n    y_col=labels_test.columns.tolist()[1:],\n    color_mode=\"rgb\",\n    target_size = IMAGE,\n    class_mode=\"raw\",\n    #class_mode=\"categorical\",\n    #subset = \"validation\",\n    batch_size=BATCH_SIZE,\n    seed=0,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:11:18.929264Z","iopub.execute_input":"2022-05-24T01:11:18.929602Z","iopub.status.idle":"2022-05-24T01:11:24.636639Z","shell.execute_reply.started":"2022-05-24T01:11:18.929568Z","shell.execute_reply":"2022-05-24T01:11:24.635808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modeling","metadata":{}},{"cell_type":"code","source":"inputs = tf.keras.Input(shape=(256, 256, 3))\n\n#x = tf.keras.applications.ResNet50(include_top=False,weights=\"imagenet\")(inputs)\nx = tf.keras.applications.ResNet50(include_top=False)(inputs)\n#ResNet50(input_shape=[124,124, 3],include_top=False, weights='imagenet')\n\n#x = tf.keras.applications.MobileNet(include_top=False)(inputs)\n#model = tf.keras.applications.MobileNet(input_shape=(150,150,3),include_top=False,weights=\"imagenet\")\n\n#x = tf.keras.applications.MobileNetV2(include_top=False,weights=\"imagenet\")(inputs)\n#x = tf.keras.applications.MobileNetV2(include_top=False)(inputs)\n\nx = tf.keras.layers.GlobalAveragePooling2D()(x)\noutputs = tf.keras.layers.Dense(6, activation='sigmoid')(x)\n\nmodel = tf.keras.models.Model(inputs, outputs)\nmodel.summary()\ntf.keras.utils.plot_model(model, show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:11:31.90899Z","iopub.execute_input":"2022-05-24T01:11:31.90934Z","iopub.status.idle":"2022-05-24T01:11:36.727632Z","shell.execute_reply.started":"2022-05-24T01:11:31.909306Z","shell.execute_reply":"2022-05-24T01:11:36.726491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_addons as tfa \n    #tf.keras.metrics.CategoricalAccuracy(name='categorical_accuracy')\nmetrics = [tfa.metrics.F1Score(num_classes = 6,average = \"macro\",name = \"f1_score\",threshold=0.5)]","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:11:43.002759Z","iopub.execute_input":"2022-05-24T01:11:43.003116Z","iopub.status.idle":"2022-05-24T01:11:43.016495Z","shell.execute_reply.started":"2022-05-24T01:11:43.003082Z","shell.execute_reply":"2022-05-24T01:11:43.015571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy', \n              optimizer=tf.keras.optimizers.Adam(lr=1e-4),\n              metrics=metrics)","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:11:47.066388Z","iopub.execute_input":"2022-05-24T01:11:47.066757Z","iopub.status.idle":"2022-05-24T01:11:47.08574Z","shell.execute_reply.started":"2022-05-24T01:11:47.066725Z","shell.execute_reply":"2022-05-24T01:11:47.084842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rlp = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_f1_score',\n                                           mode='max',\n                                           patience=2, \n                                           verbose=0, \n                                           factor=0.01)\nearlystop = tf.keras.callbacks.EarlyStopping(monitor='val_f1_score', \n                                             mode='max',\n                                             patience=5, \n                                             verbose=1, \n                                             restore_best_weights=True)\ncallbacks=[rlp,earlystop]","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:11:49.971592Z","iopub.execute_input":"2022-05-24T01:11:49.971929Z","iopub.status.idle":"2022-05-24T01:11:49.978202Z","shell.execute_reply.started":"2022-05-24T01:11:49.971899Z","shell.execute_reply":"2022-05-24T01:11:49.97686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_history = model.fit(train_data, \n                          validation_data=test_data, \n                          validation_steps = np.ceil(test_data.n / BATCH_SIZE),\n                          epochs=30, \n                          callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:14:33.179388Z","iopub.execute_input":"2022-05-24T01:14:33.179765Z","iopub.status.idle":"2022-05-24T01:30:54.147147Z","shell.execute_reply.started":"2022-05-24T01:14:33.179731Z","shell.execute_reply":"2022-05-24T01:30:54.146205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.load_weights('../input/MobileNetV2-data/mobilenetV2image512 (1).h5')","metadata":{"execution":{"iopub.status.busy":"2022-05-23T12:13:01.005081Z","iopub.execute_input":"2022-05-23T12:13:01.005407Z","iopub.status.idle":"2022-05-23T12:13:01.242456Z","shell.execute_reply.started":"2022-05-23T12:13:01.005379Z","shell.execute_reply":"2022-05-23T12:13:01.241627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(test_data, steps=31, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:30:59.245765Z","iopub.execute_input":"2022-05-24T01:30:59.246102Z","iopub.status.idle":"2022-05-24T01:31:08.900476Z","shell.execute_reply.started":"2022-05-24T01:30:59.24607Z","shell.execute_reply":"2022-05-24T01:31:08.899611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true=labels_test.iloc[:,1:].to_numpy()\ny_pred=(preds>0.5)*1\nf1_score(y_true, y_pred, average='macro')","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:31:11.796848Z","iopub.execute_input":"2022-05-24T01:31:11.797191Z","iopub.status.idle":"2022-05-24T01:31:11.813122Z","shell.execute_reply.started":"2022-05-24T01:31:11.797161Z","shell.execute_reply":"2022-05-24T01:31:11.812018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.sum(abs(y_true-y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:42:15.042634Z","iopub.execute_input":"2022-05-24T01:42:15.042981Z","iopub.status.idle":"2022-05-24T01:42:15.048956Z","shell.execute_reply.started":"2022-05-24T01:42:15.042948Z","shell.execute_reply":"2022-05-24T01:42:15.047904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#y_pred=(preds>0.5)*1\n#y_true=labels_test.iloc[:,1:].to_numpy()\n#np.array_equal(preds,preds30)\n#preds\n#preds30=preds\n#preds30\n#preds31=preds","metadata":{"execution":{"iopub.status.busy":"2022-05-23T13:56:58.587445Z","iopub.execute_input":"2022-05-23T13:56:58.587807Z","iopub.status.idle":"2022-05-23T13:56:58.597419Z","shell.execute_reply.started":"2022-05-23T13:56:58.587775Z","shell.execute_reply":"2022-05-23T13:56:58.596593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fix, ax = plt.subplots(figsize=(20, 6))\npd.DataFrame(model_history.history)[['loss', 'val_loss']].plot(ax=ax, title='Model ResNet50 Loss Curve')","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:39:11.712043Z","iopub.execute_input":"2022-05-24T01:39:11.712366Z","iopub.status.idle":"2022-05-24T01:39:11.981877Z","shell.execute_reply.started":"2022-05-24T01:39:11.712335Z","shell.execute_reply":"2022-05-24T01:39:11.981049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fix, ax = plt.subplots(figsize=(20, 6))\npd.DataFrame(model_history.history)[['f1_score', 'val_f1_score']].plot(ax=ax, title='Model ResNet50 F1_score Curve')","metadata":{"execution":{"iopub.status.busy":"2022-05-24T01:39:15.822015Z","iopub.execute_input":"2022-05-24T01:39:15.822353Z","iopub.status.idle":"2022-05-24T01:39:16.094567Z","shell.execute_reply.started":"2022-05-24T01:39:15.82232Z","shell.execute_reply":"2022-05-24T01:39:16.093586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(model_history.history).to_csv('train_history_resnet50.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-23T15:42:36.737804Z","iopub.execute_input":"2022-05-23T15:42:36.738219Z","iopub.status.idle":"2022-05-23T15:42:37.066379Z","shell.execute_reply.started":"2022-05-23T15:42:36.738183Z","shell.execute_reply":"2022-05-23T15:42:37.065481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('resnet50image512.h5')","metadata":{"execution":{"iopub.status.busy":"2022-05-23T15:42:41.115269Z","iopub.execute_input":"2022-05-23T15:42:41.1156Z","iopub.status.idle":"2022-05-23T15:42:41.364965Z","shell.execute_reply.started":"2022-05-23T15:42:41.115572Z","shell.execute_reply":"2022-05-23T15:42:41.364069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2022-05-23T15:42:58.226513Z","iopub.execute_input":"2022-05-23T15:42:58.226858Z","iopub.status.idle":"2022-05-23T15:42:58.231172Z","shell.execute_reply.started":"2022-05-23T15:42:58.226825Z","shell.execute_reply":"2022-05-23T15:42:58.230124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"submissions = pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')\nsubmissions.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-23T15:43:02.108241Z","iopub.execute_input":"2022-05-23T15:43:02.108575Z","iopub.status.idle":"2022-05-23T15:43:02.131676Z","shell.execute_reply.started":"2022-05-23T15:43:02.108544Z","shell.execute_reply":"2022-05-23T15:43:02.130924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)\n\ntest_generator = test_data_generator.flow_from_dataframe(\n    submissions,\n    directory = '../input/plant-pathology-2021-fgvc8/test_images',\n    x_col=\"image\",\n    y_col=None,\n    target_size=(256, 256),\n    color_mode=\"rgb\",\n    classes=None,\n    class_mode=None,\n    shuffle=False,\n    batch_size=32\n)\n\npredictions = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2022-05-23T15:43:08.466578Z","iopub.execute_input":"2022-05-23T15:43:08.466936Z","iopub.status.idle":"2022-05-23T15:43:10.474062Z","shell.execute_reply.started":"2022-05-23T15:43:08.466906Z","shell.execute_reply":"2022-05-23T15:43:10.473184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"verdict = (predictions>0.25)","metadata":{"execution":{"iopub.status.busy":"2022-05-23T15:43:15.276866Z","iopub.execute_input":"2022-05-23T15:43:15.277203Z","iopub.status.idle":"2022-05-23T15:43:15.283508Z","shell.execute_reply.started":"2022-05-23T15:43:15.277173Z","shell.execute_reply":"2022-05-23T15:43:15.282677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for x in verdict:\n    count = 0\n    for i in range(len(x)):\n        if x[i]==False:\n            count=count+1\n    if count==len(x):\n        x[2]=True","metadata":{"execution":{"iopub.status.busy":"2022-05-23T15:43:20.534194Z","iopub.execute_input":"2022-05-23T15:43:20.534624Z","iopub.status.idle":"2022-05-23T15:43:20.539639Z","shell.execute_reply.started":"2022-05-23T15:43:20.534585Z","shell.execute_reply":"2022-05-23T15:43:20.538783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for x in verdict:\n    if x[2]:\n        for i in range(len(x)):\n            x[i]=False\n        x[2]=True","metadata":{"execution":{"iopub.status.busy":"2022-05-23T15:43:26.294407Z","iopub.execute_input":"2022-05-23T15:43:26.294753Z","iopub.status.idle":"2022-05-23T15:43:26.30016Z","shell.execute_reply.started":"2022-05-23T15:43:26.29471Z","shell.execute_reply":"2022-05-23T15:43:26.298451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = labels.columns.tolist()[1:]\nlabel","metadata":{"execution":{"iopub.status.busy":"2022-05-23T15:43:29.527351Z","iopub.execute_input":"2022-05-23T15:43:29.52768Z","iopub.status.idle":"2022-05-23T15:43:29.534768Z","shell.execute_reply.started":"2022-05-23T15:43:29.52765Z","shell.execute_reply":"2022-05-23T15:43:29.53373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_lists = []\nfor i in range(verdict.shape[0]):\n    tmp = []\n    for j, c in enumerate(label):\n        if verdict[i, j]:\n            tmp.append(c)\n    pred_lists.append(tmp)\n\npred_lists = [' '.join(t) for t in pred_lists]\npred_lists","metadata":{"execution":{"iopub.status.busy":"2022-05-23T15:43:37.707458Z","iopub.execute_input":"2022-05-23T15:43:37.707798Z","iopub.status.idle":"2022-05-23T15:43:37.714518Z","shell.execute_reply.started":"2022-05-23T15:43:37.707768Z","shell.execute_reply":"2022-05-23T15:43:37.713563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions['labels'] = np.array(pred_lists)","metadata":{"execution":{"iopub.status.busy":"2022-05-23T15:43:43.269155Z","iopub.execute_input":"2022-05-23T15:43:43.269649Z","iopub.status.idle":"2022-05-23T15:43:43.27455Z","shell.execute_reply.started":"2022-05-23T15:43:43.269606Z","shell.execute_reply":"2022-05-23T15:43:43.273504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions","metadata":{"execution":{"iopub.status.busy":"2022-05-23T15:43:48.636065Z","iopub.execute_input":"2022-05-23T15:43:48.636422Z","iopub.status.idle":"2022-05-23T15:43:48.646165Z","shell.execute_reply.started":"2022-05-23T15:43:48.63639Z","shell.execute_reply":"2022-05-23T15:43:48.645046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions.to_csv('submission.csv', index=False)  ","metadata":{"execution":{"iopub.status.busy":"2022-05-23T15:43:55.749152Z","iopub.execute_input":"2022-05-23T15:43:55.749479Z","iopub.status.idle":"2022-05-23T15:43:55.760302Z","shell.execute_reply.started":"2022-05-23T15:43:55.74945Z","shell.execute_reply":"2022-05-23T15:43:55.759271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}