{"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\nfrom sklearn.preprocessing import MultiLabelBinarizer\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.constraints import maxnorm\nfrom keras.layers.convolutional import Conv2D, MaxPooling2D\nfrom tensorflow.keras.optimizers import Adam\nfrom PIL import Image\nfrom keras.models import load_model\nfrom keras.metrics import AUC\nfrom tensorflow.keras.applications import ResNet50V2\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.layers import Input, Dense, Dropout, Flatten, MaxPooling2D,GlobalAveragePooling2D,BatchNormalization,Activation\nfrom tensorflow.keras import backend as K","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-28T08:30:28.109268Z","iopub.execute_input":"2021-11-28T08:30:28.109577Z","iopub.status.idle":"2021-11-28T08:30:33.906368Z","shell.execute_reply.started":"2021-11-28T08:30:28.109501Z","shell.execute_reply":"2021-11-28T08:30:33.905499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_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":"2021-11-28T08:30:33.909055Z","iopub.execute_input":"2021-11-28T08:30:33.909536Z","iopub.status.idle":"2021-11-28T08:30:33.944528Z","shell.execute_reply.started":"2021-11-28T08:30:33.909496Z","shell.execute_reply":"2021-11-28T08:30:33.943784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:31:17.722647Z","iopub.execute_input":"2021-11-28T08:31:17.723121Z","iopub.status.idle":"2021-11-28T08:31:17.751504Z","shell.execute_reply.started":"2021-11-28T08:31:17.723077Z","shell.execute_reply":"2021-11-28T08:31:17.750664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.DataFrame(train,columns = ['image','labels'])","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:31:24.000724Z","iopub.execute_input":"2021-11-28T08:31:24.001055Z","iopub.status.idle":"2021-11-28T08:31:24.005946Z","shell.execute_reply.started":"2021-11-28T08:31:24.001023Z","shell.execute_reply":"2021-11-28T08:31:24.005081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['labels'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:31:41.168699Z","iopub.execute_input":"2021-11-28T08:31:41.169038Z","iopub.status.idle":"2021-11-28T08:31:41.183069Z","shell.execute_reply.started":"2021-11-28T08:31:41.169006Z","shell.execute_reply":"2021-11-28T08:31:41.181914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,12))\nlabels = sns.barplot(train.labels.value_counts().index,train.labels.value_counts())\nfor item in labels.get_xticklabels():\n    item.set_rotation(45)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:31:41.480591Z","iopub.execute_input":"2021-11-28T08:31:41.480881Z","iopub.status.idle":"2021-11-28T08:31:41.897778Z","shell.execute_reply.started":"2021-11-28T08:31:41.480853Z","shell.execute_reply":"2021-11-28T08:31:41.897014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Separate the label base on space charactor\ntrain['labels'] = train['labels'].apply(lambda s: s.split(' '))\ntrain[:10]","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:31:50.996738Z","iopub.execute_input":"2021-11-28T08:31:50.997086Z","iopub.status.idle":"2021-11-28T08:31:51.019844Z","shell.execute_reply.started":"2021-11-28T08:31:50.997048Z","shell.execute_reply":"2021-11-28T08:31:51.019001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_gauss_noise(x,sigma2=0.05):\n    return x+np.random.normal(0, sigma2, x.shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:46:28.679926Z","iopub.execute_input":"2021-11-28T08:46:28.680281Z","iopub.status.idle":"2021-11-28T08:46:28.684957Z","shell.execute_reply.started":"2021-11-28T08:46:28.680252Z","shell.execute_reply":"2021-11-28T08:46:28.684126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rotation_range = 10,#Performing Rotation\n    width_shift_range = 0.1,\n    height_shift_range = 0.1,\n    brightness_range = None,\n    shear_range = 0.1,\n    zoom_range = 0.1,\n    rescale = 1./255,\n    horizontal_flip=True,\n    vertical_flip=True,\n    \n    validation_split= 0.1\n)\nbsize = 16","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:46:28.999097Z","iopub.execute_input":"2021-11-28T08:46:28.999396Z","iopub.status.idle":"2021-11-28T08:46:29.004320Z","shell.execute_reply.started":"2021-11-28T08:46:28.999369Z","shell.execute_reply":"2021-11-28T08:46:29.003440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = datagen.flow_from_dataframe(\n    train,\n    directory = '../input/resized-plant2021/img_sz_512',\n    x_col = 'image',\n    y_col = 'labels',\n    subset=\"training\",\n    color_mode=\"rgb\",\n    target_size = (224,224),\n    class_mode=\"categorical\",\n    batch_size=bsize,\n    shuffle=False,\n    seed=40,\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:46:30.887554Z","iopub.execute_input":"2021-11-28T08:46:30.888004Z","iopub.status.idle":"2021-11-28T08:47:27.547560Z","shell.execute_reply.started":"2021-11-28T08:46:30.887944Z","shell.execute_reply":"2021-11-28T08:47:27.546070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_data = datagen.flow_from_dataframe(\n    train,\n    directory = '../input/resized-plant2021/img_sz_512',\n    x_col = 'image',\n    y_col = 'labels',\n    subset=\"validation\",\n    color_mode=\"rgb\",\n    target_size = (224,224),\n    class_mode=\"categorical\",\n    batch_size=bsize,\n    shuffle=False,\n    seed=40,\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:47:27.549042Z","iopub.execute_input":"2021-11-28T08:47:27.549377Z","iopub.status.idle":"2021-11-28T08:47:35.025391Z","shell.execute_reply.started":"2021-11-28T08:47:27.549339Z","shell.execute_reply":"2021-11-28T08:47:35.024463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image\nimport random\n\nK.clear_session()\nrandom.seed(4487); tf.random.set_seed(4487)\n\ninput_shape= (224,224,3)\n# \nbase_model = ResNet50V2(input_shape=input_shape, include_top=False,weights= \"imagenet\")","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:47:35.027420Z","iopub.execute_input":"2021-11-28T08:47:35.027848Z","iopub.status.idle":"2021-11-28T08:47:39.055820Z","shell.execute_reply.started":"2021-11-28T08:47:35.027808Z","shell.execute_reply":"2021-11-28T08:47:39.054691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import MaxPooling2D,GlobalAveragePooling2D,BatchNormalization,Activation\nx = base_model.output\nx = GlobalAveragePooling2D()(x)\nx = Dense(64, activation='relu')(x)\nx = Dense(16, activation='relu')(x)\npredictions = Dense(6, activation='softmax')(x)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:47:39.057492Z","iopub.execute_input":"2021-11-28T08:47:39.057835Z","iopub.status.idle":"2021-11-28T08:47:39.089907Z","shell.execute_reply.started":"2021-11-28T08:47:39.057800Z","shell.execute_reply":"2021-11-28T08:47:39.089140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Model(inputs=base_model.input ,outputs = predictions)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:47:39.091786Z","iopub.execute_input":"2021-11-28T08:47:39.092158Z","iopub.status.idle":"2021-11-28T08:47:39.109775Z","shell.execute_reply.started":"2021-11-28T08:47:39.092123Z","shell.execute_reply":"2021-11-28T08:47:39.108992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential, Model\n\nmodel = tf.keras.Model(inputs=base_model.input ,outputs = predictions)\n\nf1 = tfa.metrics.F1Score(num_classes=6, average='macro')\nmodel.compile(optimizer=keras.optimizers.SGD(lr=0.03, decay=1e-4, momentum=0.8, nesterov=True), \n              loss=tfa.losses.SigmoidFocalCrossEntropy(), metrics=[f1])\n\nlrschedule = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', \n                                 factor=0.05, patience=5, verbose=1)\ncallbacks_list = [lrschedule]\n\nhistory = model.fit(\n            train_data,\n            validation_data=valid_data, \n            epochs=15,\n            callbacks=callbacks_list, \n            verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:51:52.972998Z","iopub.execute_input":"2021-11-28T08:51:52.973362Z","iopub.status.idle":"2021-11-28T10:02:10.113342Z","shell.execute_reply.started":"2021-11-28T08:51:52.973331Z","shell.execute_reply":"2021-11-28T10:02:10.112471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, f1score = model.evaluate_generator(valid_data,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T10:02:10.115395Z","iopub.execute_input":"2021-11-28T10:02:10.115776Z","iopub.status.idle":"2021-11-28T10:02:34.261814Z","shell.execute_reply.started":"2021-11-28T10:02:10.115734Z","shell.execute_reply":"2021-11-28T10:02:34.261044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('ResNet 50 v2.h5')","metadata":{"execution":{"iopub.status.busy":"2021-11-28T10:04:31.006934Z","iopub.execute_input":"2021-11-28T10:04:31.007289Z","iopub.status.idle":"2021-11-28T10:04:31.631157Z","shell.execute_reply.started":"2021-11-28T10:04:31.007260Z","shell.execute_reply":"2021-11-28T10:04:31.630222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}