{"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-25T03:30:35.687451Z","iopub.execute_input":"2022-05-25T03:30:35.687891Z","iopub.status.idle":"2022-05-25T03:30:35.700353Z","shell.execute_reply.started":"2022-05-25T03:30:35.68786Z","shell.execute_reply":"2022-05-25T03:30:35.698804Z"},"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-25T03:30:52.920069Z","iopub.execute_input":"2022-05-25T03:30:52.920484Z","iopub.status.idle":"2022-05-25T03:30:52.926491Z","shell.execute_reply.started":"2022-05-25T03:30:52.920454Z","shell.execute_reply":"2022-05-25T03:30:52.925258Z"},"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-25T03:30:55.041104Z","iopub.execute_input":"2022-05-25T03:30:55.04153Z","iopub.status.idle":"2022-05-25T03:30:55.070323Z","shell.execute_reply.started":"2022-05-25T03:30:55.041497Z","shell.execute_reply":"2022-05-25T03:30:55.069214Z"},"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-25T03:30:57.900844Z","iopub.execute_input":"2022-05-25T03:30:57.901185Z","iopub.status.idle":"2022-05-25T03:30:57.920345Z","shell.execute_reply.started":"2022-05-25T03:30:57.901156Z","shell.execute_reply":"2022-05-25T03:30:57.919248Z"},"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-25T03:31:02.721312Z","iopub.execute_input":"2022-05-25T03:31:02.721868Z","iopub.status.idle":"2022-05-25T03:31:02.768539Z","shell.execute_reply.started":"2022-05-25T03:31:02.721823Z","shell.execute_reply":"2022-05-25T03:31:02.767265Z"},"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-25T03:31:06.184307Z","iopub.execute_input":"2022-05-25T03:31:06.184779Z","iopub.status.idle":"2022-05-25T03:31:06.203814Z","shell.execute_reply.started":"2022-05-25T03:31:06.184746Z","shell.execute_reply":"2022-05-25T03:31:06.202456Z"},"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-25T03:31:09.436754Z","iopub.execute_input":"2022-05-25T03:31:09.437151Z","iopub.status.idle":"2022-05-25T03:31:09.443856Z","shell.execute_reply.started":"2022-05-25T03:31:09.437121Z","shell.execute_reply":"2022-05-25T03:31:09.441902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_train,labels_test= train_test_split(labels, test_size=0.1, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T03:31:12.004006Z","iopub.execute_input":"2022-05-25T03:31:12.004448Z","iopub.status.idle":"2022-05-25T03:31:12.01726Z","shell.execute_reply.started":"2022-05-25T03:31:12.004414Z","shell.execute_reply":"2022-05-25T03:31:12.015969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE = (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-25T03:31:14.595116Z","iopub.execute_input":"2022-05-25T03:31:14.595556Z","iopub.status.idle":"2022-05-25T03:31:23.625121Z","shell.execute_reply.started":"2022-05-25T03:31:14.595523Z","shell.execute_reply":"2022-05-25T03:31:23.623841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modeling","metadata":{}},{"cell_type":"code","source":"inputs = tf.keras.Input(shape=(256, 256, 3))\nx = tf.keras.applications.ResNet50(include_top=False)(inputs)\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-25T03:31:27.969506Z","iopub.execute_input":"2022-05-25T03:31:27.969949Z","iopub.status.idle":"2022-05-25T03:31:31.626684Z","shell.execute_reply.started":"2022-05-25T03:31:27.969919Z","shell.execute_reply":"2022-05-25T03:31:31.625449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\nmodel.load_weights('../input/newdataresnet50/resnet50image512 (1).h5')","metadata":{"execution":{"iopub.status.busy":"2022-05-25T03:31:36.176332Z","iopub.execute_input":"2022-05-25T03:31:36.176753Z","iopub.status.idle":"2022-05-25T03:31:38.059308Z","shell.execute_reply.started":"2022-05-25T03:31:36.176717Z","shell.execute_reply":"2022-05-25T03:31:38.05816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ntrain_log = pd.read_csv('../input/train-history-resnet50/train_history_resnet50 (1).csv')\ntrain_log.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-25T03:31:44.68953Z","iopub.execute_input":"2022-05-25T03:31:44.689947Z","iopub.status.idle":"2022-05-25T03:31:44.718444Z","shell.execute_reply.started":"2022-05-25T03:31:44.689916Z","shell.execute_reply":"2022-05-25T03:31:44.717439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nF1_score = train_log['f1_score']\nval_F1_score = train_log['val_f1_score']\nloss = train_log['loss']\nval_loss = train_log['val_loss']\nepochs = range(1, len(F1_score) + 1)\n\nplt.plot(epochs, F1_score)\nplt.plot(epochs, val_F1_score)\nplt.title('ResNet50 F1-score Curve')\nplt.ylabel('F1-score')\nplt.xlabel('epoch')\nplt.legend(['train','val'],loc = 'upper left')\nplt.xlim(0,29)\nplt.show()\n\nplt.plot(epochs, loss)\nplt.plot(epochs, val_loss)\nplt.title('ResNet50 Loss Curve')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train','val'],loc = 'upper left')\nplt.xlim(0,29)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-25T03:31:56.320015Z","iopub.execute_input":"2022-05-25T03:31:56.320416Z","iopub.status.idle":"2022-05-25T03:31:56.850041Z","shell.execute_reply.started":"2022-05-25T03:31:56.320371Z","shell.execute_reply":"2022-05-25T03:31:56.848854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**模型预测**","metadata":{}},{"cell_type":"code","source":"preds = model.predict(test_data, steps=31, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T03:32:02.646076Z","iopub.execute_input":"2022-05-25T03:32:02.646462Z","iopub.status.idle":"2022-05-25T03:32:15.51374Z","shell.execute_reply.started":"2022-05-25T03:32:02.64643Z","shell.execute_reply":"2022-05-25T03:32:15.512649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> **计算其他指标值**","metadata":{}},{"cell_type":"code","source":"y_true=labels_test.iloc[:,1:].to_numpy()\ny_pred=(preds>0.5)*1","metadata":{"execution":{"iopub.status.busy":"2022-05-25T03:32:19.314024Z","iopub.execute_input":"2022-05-25T03:32:19.314409Z","iopub.status.idle":"2022-05-25T03:32:19.320265Z","shell.execute_reply.started":"2022-05-25T03:32:19.314348Z","shell.execute_reply":"2022-05-25T03:32:19.319163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.sum(abs(y_true-y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-05-25T03:32:21.770386Z","iopub.execute_input":"2022-05-25T03:32:21.770765Z","iopub.status.idle":"2022-05-25T03:32:21.779894Z","shell.execute_reply.started":"2022-05-25T03:32:21.770734Z","shell.execute_reply":"2022-05-25T03:32:21.778446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**计算6类标签中假阳性（FP），假阴性（FN），真阳性（TP），真阴性（TN）数量**","metadata":{}},{"cell_type":"code","source":"fp=np.logical_and(y_true==0, y_pred==1)*1\nfn=np.logical_and(y_true==1, y_pred==0)*1\n\ntp=np.logical_and(y_true==1, y_pred==1)*1\ntn=np.logical_and(y_true==0, y_pred==0)*1","metadata":{"execution":{"iopub.status.busy":"2022-05-25T03:32:25.596911Z","iopub.execute_input":"2022-05-25T03:32:25.597442Z","iopub.status.idle":"2022-05-25T03:32:25.606458Z","shell.execute_reply.started":"2022-05-25T03:32:25.597329Z","shell.execute_reply":"2022-05-25T03:32:25.604532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(fp.sum(axis=0))\nprint(fn.sum(axis=0))\nprint(tp.sum(axis=0))\nprint(tn.sum(axis=0))","metadata":{"execution":{"iopub.status.busy":"2022-05-25T03:32:28.654991Z","iopub.execute_input":"2022-05-25T03:32:28.655409Z","iopub.status.idle":"2022-05-25T03:32:28.665138Z","shell.execute_reply.started":"2022-05-25T03:32:28.655346Z","shell.execute_reply":"2022-05-25T03:32:28.663342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**计算6类标签每类的F1-score、precision和recall值**","metadata":{}},{"cell_type":"code","source":"precision=tp.sum(axis=0)/ (tp.sum(axis=0)+fp.sum(axis=0))\nrecall=tp.sum(axis=0)/ (tp.sum(axis=0)+fn.sum(axis=0))\nf1=2*tp.sum(axis=0)/ (2*tp.sum(axis=0)+fp.sum(axis=0)+fn.sum(axis=0))","metadata":{"execution":{"iopub.status.busy":"2022-05-25T03:32:31.667107Z","iopub.execute_input":"2022-05-25T03:32:31.667528Z","iopub.status.idle":"2022-05-25T03:32:31.677217Z","shell.execute_reply.started":"2022-05-25T03:32:31.667492Z","shell.execute_reply":"2022-05-25T03:32:31.676149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(precision)\nprint(recall)\nprint(f1)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T03:32:36.350482Z","iopub.execute_input":"2022-05-25T03:32:36.350919Z","iopub.status.idle":"2022-05-25T03:32:36.359372Z","shell.execute_reply.started":"2022-05-25T03:32:36.350874Z","shell.execute_reply":"2022-05-25T03:32:36.358117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**计算macro、micro形式下的三种值**","metadata":{}},{"cell_type":"code","source":"macro_precision=precision.mean()\nmacro_recall=recall.mean()\nmacro_f1=f1.mean()\nprint(macro_precision)\nprint(macro_recall)\nprint(macro_f1)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T03:32:39.367659Z","iopub.execute_input":"2022-05-25T03:32:39.368113Z","iopub.status.idle":"2022-05-25T03:32:39.376568Z","shell.execute_reply.started":"2022-05-25T03:32:39.368067Z","shell.execute_reply":"2022-05-25T03:32:39.375467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"micro_precision=tp.sum()/ (tp.sum()+fp.sum())\nmicro_recall=tp.sum()/ (tp.sum()+fn.sum())\nmicro_f1=2*tp.sum()/ (2*tp.sum()+fp.sum()+fn.sum())\nprint(micro_precision)\nprint(micro_recall)\nprint(micro_f1)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T03:32:42.452554Z","iopub.execute_input":"2022-05-25T03:32:42.452955Z","iopub.status.idle":"2022-05-25T03:32:42.464815Z","shell.execute_reply.started":"2022-05-25T03:32:42.452925Z","shell.execute_reply":"2022-05-25T03:32:42.463421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**验证上述micro、micro形式下的三种值是否算对**","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import f1_score\nfrom sklearn.metrics import precision_score\nfrom sklearn.metrics import recall_score","metadata":{"execution":{"iopub.status.busy":"2022-05-25T03:32:45.144543Z","iopub.execute_input":"2022-05-25T03:32:45.14507Z","iopub.status.idle":"2022-05-25T03:32:45.150192Z","shell.execute_reply.started":"2022-05-25T03:32:45.145036Z","shell.execute_reply":"2022-05-25T03:32:45.148973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(precision_score(y_true, y_pred, average='macro'))\nprint(recall_score(y_true, y_pred, average='macro'))\nprint(f1_score(y_true, y_pred, average='macro'))","metadata":{"execution":{"iopub.status.busy":"2022-05-25T03:32:52.786235Z","iopub.execute_input":"2022-05-25T03:32:52.786644Z","iopub.status.idle":"2022-05-25T03:32:52.811275Z","shell.execute_reply.started":"2022-05-25T03:32:52.786611Z","shell.execute_reply":"2022-05-25T03:32:52.810173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(precision_score(y_true, y_pred, average='micro'))\nprint(recall_score(y_true, y_pred, average='micro'))\nprint(f1_score(y_true, y_pred, average='micro'))","metadata":{"execution":{"iopub.status.busy":"2022-05-25T03:32:54.779295Z","iopub.execute_input":"2022-05-25T03:32:54.779729Z","iopub.status.idle":"2022-05-25T03:32:54.803527Z","shell.execute_reply.started":"2022-05-25T03:32:54.779689Z","shell.execute_reply":"2022-05-25T03:32:54.802191Z"},"trusted":true},"execution_count":null,"outputs":[]}]}