{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport json\nfrom imgaug import augmenters as iaa\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.layers import Conv2D, Dropout, MaxPool2D, BatchNormalization, Flatten, Dense, Add, Input\nfrom keras import models, Model\nfrom keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping\nfrom keras.optimizers import Adam\n# import keras.backend as K","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# K.set_floatx('float16')\n# K.floatx()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"root_path = r\"../input/cassava-leaf-disease-classification\" ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(os.path.join(root_path,\"train.csv\"))\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open (os.path.join(root_path, \"label_num_to_disease_map.json\")) as json_file:\n    label_mapping_json = json.load(json_file)\nlabel_mapping_json","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.label = train_df.label.astype(str)\ntrain_df.label = train_df.label.map(label_mapping_json)\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.label.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"augmentation = iaa.Sequential([\n    iaa.Multiply((0.5, 1.5)),\n    iaa.Affine(scale={\"x\": (1, 1.2), \"y\":(1, 1.2)}),\n    iaa.Sometimes(0.4,\n                 iaa.GaussianBlur(sigma=(0,2))),\n    iaa.Sometimes(0.3,\n                 iaa.Grayscale(alpha=(0.0, 1.0))),\n    iaa.Sometimes(0.3,\n                 iaa.SigmoidContrast(gain=(3, 6), cutoff=(0.4, 0.6))),\n    iaa.Sometimes(0.5,\n                 iaa.Dropout(p=(0, 0.2))),\n    iaa.Cutout(nb_iterations=(1, 4), size=(0.05,0.4), squared=False, fill_mode=\"gaussian\", fill_per_channel=True),\n    \n    iaa.PadToFixedSize(width=512, height=512),\n    iaa.CropToFixedSize(width=512, height=512),\n])\n\n\n\ndef augment_image(image):\n    image = (image*255).astype(\"uint8\")\n    image = augmentation.augment_image(image)\n    \n    return image.astype(\"float32\")\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"input_height, input_width, input_channels = 512, 512, 3\nbatch_size = 5\ninput_shape = (input_height, input_width, input_channels)\n\n\nt_gen = ImageDataGenerator(\n    rotation_range=45,\n    width_shift_range=0.25,\n    height_shift_range=0.25,\n    brightness_range=[0.8, 1],\n    shear_range=0.25,\n    zoom_range=0.25,\n    channel_shift_range=0,\n    fill_mode='constant',\n    cval=0.0,\n    horizontal_flip=True,\n    vertical_flip=True,\n    rescale=1/255,\n    preprocessing_function=augment_image,\n    data_format=None,\n    validation_split=0.2,\n    dtype=None,)\n\n\nv_gen = ImageDataGenerator(\n    rescale=1/255,\n    preprocessing_function=None,\n    data_format=None,\n    validation_split=0.2,\n    dtype=None,)\n\n\ntrain_gen = t_gen.flow_from_dataframe(train_df,\n    directory=os.path.join(root_path, \"train_images\"),\n    x_col='image_id',\n    y_col='label',\n    weight_col=None,\n    target_size=(input_height, input_width),\n    color_mode='rgb',\n    classes=None,\n    class_mode='categorical',\n    batch_size=batch_size,\n    shuffle=True,\n    seed=0,\n    save_to_dir=None,\n    save_prefix='',\n    save_format='png',\n    subset=\"training\",\n    interpolation='nearest',\n    validate_filenames=True,)\n\nvalid_gen = v_gen.flow_from_dataframe(train_df,\n    directory=os.path.join(root_path, \"train_images\"),\n    x_col='image_id',\n    y_col='label',\n    weight_col=None,\n    target_size=(input_height, input_width),\n    color_mode='rgb',\n    classes=None,\n    class_mode='categorical',\n    batch_size=batch_size,\n    shuffle=True,\n    seed=0,\n    save_to_dir=None,\n    save_prefix='',\n    save_format='png',\n    subset=\"validation\",\n    interpolation='nearest',\n    validate_filenames=True,)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def casava_model(input_shape, pre_trained_weights=False):\n    \n    inputs = Input(input_shape)\n    x = Conv2D(64, (1,1), activation=\"relu\",strides=(2, 2), padding=\"same\", name=\"conv2d_11\")(inputs)\n    x = Conv2D(64, (5,5), activation=\"relu\",strides=(2, 2), padding=\"same\", name=\"conv2d_12\")(x)\n    x = Conv2D(128, (5,5), activation=\"relu\",strides=(2, 2), padding=\"same\", name=\"conv2d_13\")(x)\n    x = MaxPool2D(pool_size=(3, 3),strides=(2, 2), padding='same', name=\"maxpool2d_1\")(x)\n    \n    x_shortcut = x\n    x = BatchNormalization()(x)\n    \n#     x_shortcut = x\n    x_shortcut = Conv2D(256, (3,3), activation=\"relu\", padding=\"same\", name=\"conv2d_s1\")(x_shortcut)\n    x_shortcut = MaxPool2D(pool_size=(2, 2),strides=(2, 2), padding='same', name=\"maxpool2d_s1\")(x_shortcut)\n    \n    x = Conv2D(128, (1,1), activation=\"relu\", padding=\"same\", name=\"conv2d_21\")(x)\n    x = Conv2D(128, (3,3), activation=\"relu\", padding=\"same\", name=\"conv2d_22\")(x)\n    x = Conv2D(256, (3,3), activation=\"relu\", padding=\"same\", name=\"conv2d_23\")(x)\n    x = MaxPool2D(pool_size=(2, 2),strides=(2, 2), padding='same', name=\"maxpool2d_2\")(x)\n    x = BatchNormalization()(x)\n    x = Dropout(0.25)(x)\n\n    x = Add()([x, x_shortcut])\n    x_shortcut = x\n    x_shortcut = Conv2D(512, (3,3), activation=\"relu\", padding=\"same\", name=\"conv2d_s2\")(x_shortcut)\n    x_shortcut = MaxPool2D(pool_size=(2, 2),strides=(2, 2), padding='same', name=\"maxpool2d_s2\")(x_shortcut)\n    \n    x = Conv2D(256, (1,1), activation=\"relu\", padding=\"same\", name=\"conv2d_31\")(x)\n    x = Conv2D(256, (3,3), activation=\"relu\", padding=\"same\", name=\"conv2d_32\")(x)\n    x = Conv2D(512, (3,3), activation=\"relu\", padding=\"same\", name=\"conv2d_33\")(x)\n    x = MaxPool2D(pool_size=(2, 2),strides=(2, 2), padding='same', name=\"maxpool2d_3\")(x)\n    \n    x_shortcut = x\n    x = BatchNormalization()(x)\n    \n    x = Add()([x, x_shortcut])\n#     x_shortcut = x\n    x_shortcut = Conv2D(1024, (3,3), activation=\"relu\", padding=\"same\", name=\"conv2d_s3\")(x_shortcut)\n    x_shortcut = MaxPool2D(pool_size=(2, 2),strides=(2, 2), padding='same', name=\"maxpool2d_s3\")(x_shortcut)\n    \n    x = Conv2D(512, (1,1), activation=\"relu\", padding=\"same\", name=\"conv2d_41\")(x)\n    x = Conv2D(512, (3,3), activation=\"relu\", padding=\"same\", name=\"conv2d_42\")(x)\n    x = Conv2D(1024, (3,3), activation=\"relu\", padding=\"same\", name=\"conv2d_43\")(x)\n    x = MaxPool2D(pool_size=(2, 2),strides=(2, 2), padding='same', name=\"maxpool2d_4\")(x)\n    x = BatchNormalization()(x)\n    \n    x = Add()([x, x_shortcut])\n    x_shortcut = x\n    x_shortcut = Conv2D(2048, (3,3), activation=\"relu\", padding=\"same\", name=\"conv2d_s4\")(x_shortcut)\n    x_shortcut = MaxPool2D(pool_size=(2, 2),strides=(2, 2), padding='same', name=\"maxpool2d_s4\")(x_shortcut)\n    \n    x = Conv2D(1024, (1,1), activation=\"relu\", padding=\"same\", name=\"conv2d_51\")(x)\n    x = Conv2D(1024, (3,3), activation=\"relu\", padding=\"same\", name=\"conv2d_52\")(x)\n    x = Conv2D(2048, (3,3), activation=\"relu\", padding=\"same\", name=\"conv2d_53\")(x)\n    x = MaxPool2D(pool_size=(2, 2),strides=(2, 2), padding='same', name=\"maxpool2d_5\")(x)\n    x = BatchNormalization()(x)\n    \n    x = Add()([x, x_shortcut])\n    \n    x = Conv2D(2048, (3,3), activation=\"relu\", padding=\"same\", name=\"conv2d_61\")(x)\n    x = Conv2D(2048, (3,3), activation=\"relu\", padding=\"same\", name=\"conv2d_62\")(x)\n    x = Conv2D(4096, (3,3), activation=\"relu\", padding=\"same\", name=\"conv2d_63\")(x)\n    x = MaxPool2D(pool_size=(2, 2),strides=(2, 2), padding='same', name=\"maxpool2d_6\")(x)\n    x = BatchNormalization()(x)\n    \n    x = Dropout(0.25)(x)\n    x = Flatten()(x)\n    x = Dense(2048, activation=\"relu\", name=\"dense_1\")(x)\n    output = Dense(5, activation=\"softmax\", name=\"dense_2\")(x)\n    \n    model = Model(inputs, output)\n    \n    adam = Adam(learning_rate=1e-04, epsilon=1e-07,decay=1e-12)\n    model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])\n    model.summary()\n    \n    if pre_trained_weights:\n        model.load_weights(pre_trained_weights, by_name=True, skip_mismatch=True)\n    \n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weights_path = \"../input/fork-of-fork-of-cassava-leaf-disease-classificatio/Casava_Model_05.weights\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = casava_model(input_shape, weights_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_name = \"Casava_Model_05\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_checkpoint = ModelCheckpoint(\"{}.hdf5\".format(model_name), monitor='val_loss', verbose=1,  save_best_only=True, \n                                   save_weights_only=False)\nreduce_lr = ReduceLROnPlateau(monitor='val_loss',  factor=0.2, patience=5, verbose=1, mode='auto', min_delta=1e-4,\n                              cooldown=0,  min_lr=1e-14)\nearly_stop = EarlyStopping(monitor='val_loss', min_delta=1e-8, patience=5, mode='auto', verbose=1, restore_best_weights=True)\n\nmodel.fit_generator(train_gen,\n    steps_per_epoch=2000,\n    epochs=50,\n    verbose=1,\n    callbacks=[model_checkpoint, reduce_lr, early_stop],\n    validation_data=valid_gen,\n    validation_steps=200,\n    validation_freq=1,\n    class_weight=None,\n    max_queue_size=10,\n    workers=1,\n    use_multiprocessing=True,\n    shuffle=True,\n    initial_epoch=0,)\n\nmodel.save_weights(\"{}.weights\".format(model_name))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\nimport glob","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = cv2.imread(\"../input/cassava-leaf-disease-classification/test_images/2216849948.jpg\")\nplt.imshow(img)\n\nx = cv2.resize(img, (input_height, input_width))\nx = np.reshape(x, (1,)+x.shape)\nres = model.predict(x).argmax()\nprint(\"Predicted Class:\", res)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"files = glob.glob(os.path.join(root_path, \"test_images/*.jpg\"))\nprint(\"Number of Files:\", len(files))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_test = []\ny_pred = []\nfor file in files:\n    img = cv2.imread(file)\n    x = cv2.resize(img, (input_height, input_width))\n    x = np.reshape(x, (1,)+x.shape)\n    res = model.predict(x).argmax()\n    \n    x_test.append(os.path.basename(file))\n    y_pred.append(res)\n    \nprint(\"Predicted on Test Images\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output = pd.DataFrame()\noutput[\"image_id\"] = x_test\noutput[\"label\"] = y_pred\noutput.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}