{"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 REQUIRED LIBRARIES:\n\nimport numpy as np\nimport pandas as pd\nimport os\nfrom re import search\nimport shutil\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nimport cv2\nimport seaborn as sns\n\n\nfrom sklearn.preprocessing import MultiLabelBinarizer\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.applications import ResNet50, ResNet50V2\nfrom tensorflow.keras.callbacks import ModelCheckpoint,EarlyStopping\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Input, Dense, ZeroPadding2D, Dropout, Activation, Flatten, Conv2D, MaxPooling2D, ReLU, BatchNormalization, AveragePooling2D, GlobalAveragePooling2D, add","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#IMAGE PATH & DATAFRAME:\n\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')\ntrain.head","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.DataFrame(train,columns = ['image','labels'])\ntrain['labels'].value_counts()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['labels'] = train['labels'].apply(lambda s: s.split(' '))\ntrain[:10]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use the Image Data Generator to import the images from the dataset\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ndatagen = ImageDataGenerator(rescale = 1/255.,\n    rotation_range = 10,#Performing Rotation\n    width_shift_range = 0.2,\n    height_shift_range = 0.2,\n    brightness_range = [0.2,1.0],\n    shear_range = 0.2,\n    zoom_range = 0.2,\n    horizontal_flip=True,\n    vertical_flip=True,\n    validation_split= 0.2)\n\n\nHEIGHT = 224\nWIDTH=224\nSEED = 100\nBATCH_SIZE=32\ntrain_ds = datagen.flow_from_dataframe(\n    train,\n    directory = '../input/resized-plant2021/img_sz_512',# We are using the resized images otherwise it will take a lot of time to train \n    x_col = 'image',\n    y_col = 'labels',\n    subset=\"training\",\n    color_mode=\"rgb\",\n    target_size = (HEIGHT,WIDTH),\n    class_mode=\"categorical\",\n    batch_size=BATCH_SIZE,\n    shuffle=True,\n    seed=SEED,\n)\n\n\nval_ds = datagen.flow_from_dataframe(\n    train,\n    directory = '../input/resized-plant2021/img_sz_512',# We are using the resized images otherwise it will take a lot of time to train \n    x_col = 'image',\n    y_col = 'labels',\n    subset=\"validation\",\n    color_mode=\"rgb\",\n    target_size = (HEIGHT,WIDTH),\n    class_mode=\"categorical\",\n    batch_size=BATCH_SIZE,\n    shuffle=True,\n    seed=SEED,\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example = next(train_ds)\nprint(example[0].shape)\nplt.imshow(example[0][0,:,:,:])\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Convolution Block\ndef Convolution_Block(Input, filters, k, s, stage, block):\n    \n    filter1, filter2, filter3 = filters\n    base_name = str(stage) + block + \"_branch\"\n    conv_name_base = \"res\" + base_name\n    bn_name_base = \"bn\" + base_name\n        \n    #Block 1\n    x = Conv2D(filters=filter1, kernel_size=(1, 1), \n              strides=(s, s),\n              name=conv_name_base + \"2a\", \n              kernel_initializer='he_normal')(Input)\n\n    x = BatchNormalization(axis=1, name=bn_name_base + \"2a\")(x)\n    x = Activation('relu')(x)\n    \n    #Block 2\n    x = Conv2D(filters=filter2, kernel_size=(k, k), \n              padding='same',\n              name=conv_name_base + \"2b\", \n              kernel_initializer='he_normal')(x)\n\n    x = BatchNormalization(axis=1, name=bn_name_base + \"2b\")(x)\n    x = Activation('relu')(x)\n    \n    #Block 3\n    x = Conv2D(filters=filter3, kernel_size=(1, 1), \n              name=conv_name_base + \"2c\", \n              kernel_initializer='he_normal')(x)\n\n    x = BatchNormalization(axis=1,name=bn_name_base + \"2c\")(x)\n    \n    #Residual Connection\n    skip_connection = Conv2D(filters=filter3, kernel_size=(1, 1), \n                             strides=(s, s),\n                             name=conv_name_base + \"1\", \n                             kernel_initializer='he_normal')(Input)\n    \n    skip_connection = BatchNormalization(axis=1, name=bn_name_base + \"1\")(skip_connection)\n    \n    x = add([x, skip_connection])\n    x = Activation('relu')(x)\n    \n    return x","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Identity Block\ndef Identity_Block(Input, filters, k, stage, block):\n    \n    filter1, filter2, filter3 = filters\n    base_name = str(stage) + block + \"_branch\"\n    conv_name_base = \"res\" + base_name\n    bn_name_base = \"bn\" + base_name\n    \n    #Block 1\n    x = Conv2D(filters=filter1, kernel_size=(1, 1), \n              name=conv_name_base + \"2a\", \n              kernel_initializer='he_normal')(Input)\n\n    x = BatchNormalization(axis=1, name=bn_name_base + \"2a\")(x)\n    x = Activation('relu')(x)\n    \n    #Block 2\n    x = Conv2D(filters=filter2, kernel_size=(k, k), \n              padding='same',\n              name=conv_name_base + \"2b\", \n              kernel_initializer='he_normal')(x)\n\n    x = BatchNormalization(axis=1, name=bn_name_base + \"2b\")(x)\n    x = Activation('relu')(x)\n    \n    #Block 3\n    x = Conv2D(filters=filter3, kernel_size=(1, 1), \n              name=conv_name_base + \"2c\", \n              kernel_initializer='he_normal')(x)\n\n    x = BatchNormalization(axis=1, name=bn_name_base + \"2c\")(x)\n    \n    #Residual Connection\n    x = add([x, Input])\n    x = Activation('relu')(x)\n    \n    return x","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input = Input(shape=(HEIGHT,WIDTH,3));\n\n# #Initial Block\n# x = ZeroPadding2D(padding=(3, 3), name='conv1_pad')(input)\n# x = Conv2D(filters=64, kernel_size=(7, 7), \n#           strides=(2, 2), padding='valid',\n#           name='conv1',\n#           kernel_initializer='he_normal')(x)\n\n# x = BatchNormalization(axis=1, name='bn_conv1')(x)\n# x = Activation('relu')(x)\n# x = ZeroPadding2D(padding=(1, 1), name='pool1_pad')(x)\n# x = MaxPooling2D((3, 3), strides=(2, 2))(x)\n\n# #Block 1\n# x = Convolution_Block(x, [64, 64, 256], 3, s=1, stage=2, block=\"a\")\n# x = Identity_Block(x, [64, 64, 256], 3, stage=2, block=\"b\")\n# x = Identity_Block(x, [64, 64, 256], 3, stage=2, block=\"c\")\n\n# #Block 2\n# x = Convolution_Block(x, [128, 128, 512], 3, s=2, stage=3, block=\"a\")\n# x = Identity_Block(x, [128, 128, 512], 3, stage=3, block=\"b\")\n# x = Identity_Block(x, [128, 128, 512], 3, stage=3, block=\"c\")\n# x = Identity_Block(x, [128, 128, 512], 3, stage=3, block=\"d\")\n\n# #Block 3\n# x = Convolution_Block(x, [256, 256, 1024], 3, s=2, stage=4, block=\"a\")\n# x = Identity_Block(x, [256, 256, 1024], 3, stage=4, block=\"b\")\n# x = Identity_Block(x, [256, 256, 1024], 3, stage=4, block=\"c\")\n# x = Identity_Block(x, [256, 256, 1024], 3, stage=4, block=\"d\")\n# x = Identity_Block(x, [256, 256, 1024], 3, stage=4, block=\"e\")\n# x = Identity_Block(x, [256, 256, 1024], 3, stage=4, block=\"f\")\n\n# #Block 4\n# x = Convolution_Block(x, [512, 512, 2048], 3, s=2, stage=5, block=\"a\")\n# x = Identity_Block(x, [512, 512, 2048], 3, stage=5, block=\"b\")\n# x = Identity_Block(x, [512, 512, 2048], 3, stage=5, block=\"c\")\n\n# #Block 5\n# x = GlobalAveragePooling2D()(x)\n# # block5_flatten = Flatten()(block5_avg_pooling)\n\n# x = Dense(64, activation='relu')(x)\n# # block5_dropout1 = Dropout(0.2)(block5_dense1)\n\n# x = Dense(16, activation='relu')(x)\n# # block5_dropout2 = Dropout(0.2)(block5_dense2)\n\n# output = Dense(6, name='model_output', \n#                activation='softmax')(x)\n\n# model = Model(input, output)\n\n\npretrained_model = ResNet50(input_shape=(HEIGHT,WIDTH,3), include_top=False, weights='../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')\nx = pretrained_model.output\nx = GlobalAveragePooling2D()(x)\n#fully connected layer\nx = Dense(64, activation='relu')(x)\nx = Dense(16, activation='relu')(x)\n# finally, the softmax for the classifier \nx = Dense(6, activation='softmax')(x)\nmodel = Model(pretrained_model.input, x)\n\n# Compile the Model\nmodel.compile(optimizer=tf.keras.optimizers.SGD(lr=0.001, decay=1e-4, momentum=0.9, nesterov=True),\n    loss='binary_crossentropy',\n    metrics=['accuracy'])\n\nmodel.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint=ModelCheckpoint(r'D:\\Python37\\Projects\\Foliar diseases in apple trees\\models\\apple2.h5',\n                          monitor='val_loss',\n                          mode='min',\n                          save_best_only=True,\n                          verbose=1)\nearlystop=EarlyStopping(monitor='val_loss',\n                       min_delta=0,\n                       patience=5,\n                       verbose=1,\n                       restore_best_weights=True)\n\ncallbacks=[checkpoint,earlystop]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resNet_model=model.fit(train_ds,\n                        validation_data=val_ds,\n                        epochs=15,\n                        shuffle=True,\n#                         steps_per_epoch=train_ds.samples//128,\n#                         validation_steps=val_ds.samples//128,\n                        batch_size=BATCH_SIZE,\n                        callbacks=callbacks)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_history = resNet_model.history\n\nplt.figure()\nplt.plot(model_history['accuracy'])\nplt.plot(model_history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'])\nplt.savefig('accuracy')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure()\nplt.plot(model_history['loss'])\nplt.plot(model_history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'])\nplt.savefig('loss')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/plant-pathology-2021-fgvc8/sample_submission.csv')\nsubmission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(\n    rescale = 1/255.0\n)\n\ntest_generator = test_datagen.flow_from_dataframe(\n    submission,\n    directory=\"../input/plant-pathology-2021-fgvc8/test_images\",\n    x_col='image',\n    y_col=None,\n    class_mode=None,\n    color_mode=\"rgb\",\n    target_size=(HEIGHT,WIDTH),\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example = next(test_generator)\nfor i in range(len(example)):\n    print(example[i].shape)\n    plt.imshow(example[i][:,:,:])\n    plt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(test_generator)\nprint(preds)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = preds.tolist()\n\nindices = []\nfor pred in preds:\n    temp = []\n    for category in pred:\n        if category>=0.23:\n            temp.append(pred.index(category))\n    if temp!=[]:\n        indices.append(temp)\n    else:\n        temp.append(np.argmax(pred))\n        indices.append(temp)\n    \nprint(indices)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = (train_ds.class_indices)\nlabels = dict((v,k) for k,v in labels.items())\nprint(labels)\n\ntestlabels = []\n\nfor image in indices:\n    temp = []\n    for i in image:\n        temp.append(str(labels[i]))\n    testlabels.append(' '.join(temp))\n\nprint(testlabels)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['labels'] = testlabels\nsubmission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}