{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nfrom sklearn.metrics import accuracy_score\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import datasets, layers, models\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, LSTM, BatchNormalization\nfrom tensorflow.keras.callbacks import TensorBoard\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\nfrom PIL import Image \nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom tensorflow.keras.layers import Conv2D , MaxPool2D , Flatten\n\nfrom tensorflow.keras.layers import Input, Lambda, Dense, Flatten\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.applications.vgg16 import VGG16\nfrom tensorflow.keras.applications.vgg16 import preprocess_input\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img\nfrom tensorflow.keras.models import Sequential\nfrom glob import glob\nimport os, cv2, json","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"WORK_DIR = \"../input/cassava-leaf-disease-classification/\"\nos.listdir(WORK_DIR)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_labels = pd.read_csv(os.path.join(WORK_DIR, \"train.csv\"))\ntrain_labels.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(train_labels.label, edgecolor = 'black',\n              palette = sns.color_palette(\"viridis\", 5))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_labels.label = train_labels.label.astype('str')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IMG_SIZE = 512","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator = ImageDataGenerator(\n                                    #featurewise_center=False,                                    \n                                    #samplewise_center=False,\n                                    #featurewise_std_normalization=False,\n                                    #samplewise_std_normalization=False, \n                                    #zca_whitening=False,\n                                    #zca_epsilon=1e-06,\n                                    rotation_range=90,\n                                    width_shift_range=0.2,\n                                    height_shift_range=0.2,\n                                    #brightness_range=None,\n                                    shear_range=25,\n                                    zoom_range=0.3,\n                                    #channel_shift_range=0.0,\n                                    #fill_mode=\"nearest\",\n                                    #cval=0.0,\n                                    horizontal_flip=True,\n                                    vertical_flip=True,\n                                    #rescale=None,\n                                    #preprocessing_function=None,\n                                    #data_format=None,\n                                    validation_split=0.2,\n                                    #dtype=None,\n) \\\n        .flow_from_dataframe(\n                            train_labels,\n                            directory = WORK_DIR + \"train_images\",\n                            x_col = \"image_id\",\n                            y_col = \"label\",\n                            #weight_col = None,\n                            target_size = (IMG_SIZE, IMG_SIZE),\n                            #color_mode = \"rgb\",\n                            #classes = 'sparse',\n                            class_mode = \"categorical\",\n                            batch_size = 10,\n                            shuffle = True,\n                            #seed = 34,\n                            #save_to_dir = None,\n                            #save_prefix = \"\",\n                            #save_format = \"png\",\n                            subset = \"training\",\n                            #interpolation = \"nearest\",\n                            #validate_filenames = True\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"valid_generator = ImageDataGenerator(\n                                    validation_split = 0.2\n) \\\n        .flow_from_dataframe(\n                            train_labels,\n                            directory = WORK_DIR + \"train_images\",\n                            x_col = \"image_id\",\n                            y_col = \"label\",\n                            target_size = (IMG_SIZE, IMG_SIZE),\n                            class_mode = \"categorical\",\n                            batch_size = 10,\n                            shuffle = True,\n                            subset = \"validation\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"BATCH_SIZE = 10\nSTEPS_PER_EPOCH = len(train_generator) / BATCH_SIZE\nVALIDATION_STEPS = len(valid_generator) / BATCH_SIZE\nEPOCHS = 100","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"valid_generator.class_indices","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\ndef squeeze_excite_block(filters,input):                      \n    se = tf.keras.layers.GlobalAveragePooling2D()(input)\n    se = tf.keras.layers.Reshape((1, filters))(se) \n    se = tf.keras.layers.Dense(filters//16, activation='relu')(se)\n    se = tf.keras.layers.Dense(filters, activation='sigmoid')(se)\n    se = tf.keras.layers.multiply([input, se])\n    return se\ndef create_resblock(channels, inputs):\n    x = tf.keras.layers.BatchNormalization(momentum=0.9)(inputs)\n    x = tf.keras.layers.LeakyReLU(0)(x)\n    x = tf.keras.layers.Conv2D(channels, 3, padding='same', use_bias=False)(x)\n    x = tf.keras.layers.BatchNormalization(momentum=0.9)(x)\n    x = tf.keras.layers.LeakyReLU(0)(x)\n    x = tf.keras.layers.Conv2D(channels, 3, padding='same', use_bias=False)(x)\n    return x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def make_model():\n        s = tf.keras.Input(shape=(512,512,3)) \n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(s)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = squeeze_excite_block(128,x)\n        x = create_resblock(128,x)\n\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = squeeze_excite_block(128,x)\n        x = create_resblock(128,x)\n        x = tf.keras.layers.AveragePooling2D(2)(x)\n\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = squeeze_excite_block(128,x)\n        x = create_resblock(128,x)\n        x = tf.keras.layers.AveragePooling2D(2)(x)        \n\n\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = squeeze_excite_block(128,x)\n        x = create_resblock(128,x)\n        x = tf.keras.layers.AveragePooling2D(2)(x)\n\n        s = tf.keras.Input(shape=(512,512,3)) \n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(s)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = squeeze_excite_block(128,x)\n        x = create_resblock(128,x)\n        x = tf.keras.layers.AveragePooling2D(2)(x)\n        \n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = squeeze_excite_block(128,x)\n        x = create_resblock(128,x)\n        x = tf.keras.layers.AveragePooling2D(2)(x)\n\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = squeeze_excite_block(128,x)\n        x = create_resblock(128,x)\n        x = tf.keras.layers.AveragePooling2D(2)(x)        \n\n\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = squeeze_excite_block(128,x)\n        x = create_resblock(128,x)\n        x = tf.keras.layers.AveragePooling2D(2)(x)\n\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = squeeze_excite_block(128,x)\n        x = create_resblock(128,x)\n        x = tf.keras.layers.AveragePooling2D(2)(x)\n\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = squeeze_excite_block(128,x)\n        x = create_resblock(128,x)\n        x = tf.keras.layers.AveragePooling2D(2)(x)        \n\n\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = squeeze_excite_block(128,x)\n        x = create_resblock(128,x)\n        x = tf.keras.layers.AveragePooling2D(2)(x)\n\n        s = tf.keras.Input(shape=(512,512,3)) \n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(s)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = squeeze_excite_block(128,x)\n        x = create_resblock(128,x)\n        x = tf.keras.layers.AveragePooling2D(2)(x)\n        \n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = squeeze_excite_block(128,x)\n        x = create_resblock(128,x)\n        x = tf.keras.layers.AveragePooling2D(2)(x)\n\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = squeeze_excite_block(128,x)\n        x = create_resblock(128,x)\n        x = tf.keras.layers.AveragePooling2D(2)(x)        \n\n\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.Conv2D(128,(3,3),activation='relu',padding='same')(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = squeeze_excite_block(128,x)\n        x = create_resblock(128,x)\n        x = tf.keras.layers.AveragePooling2D(2)(x)\n        x = tf.keras.layers.concatenate([tf.keras.layers.GlobalMaxPooling2D()(x),\n                                         tf.keras.layers.GlobalAveragePooling2D()(x)])\n\n        x = tf.keras.layers.Dense(5,activation='softmax',use_bias=False,\n                                  kernel_regularizer=tf.keras.regularizers.l1(0.00025))(x)\n        return tf.keras.Model(inputs=s, outputs=x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model=make_model()\nmodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),loss='categorical_crossentropy',metrics=['accuracy'])\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',\n                                            patience=5,\n                                            verbose=1,\n                                            factor=0.25,\n                                            min_lr=0.00000003)\nes = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_check = ModelCheckpoint(\n                            \"./saved.h5\",\n                            monitor = \"val_loss\",\n                            verbose = 1,\n                            save_best_only = True,\n                            save_weights_only = False,\n                            mode = \"min\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit_generator(train_generator,steps_per_epoch = STEPS_PER_EPOCH,\n                              epochs = EPOCHS,validation_data = valid_generator,\n                              validation_steps = VALIDATION_STEPS,callbacks=[model_check,learning_rate_reduction,es])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = keras.models.load_model(\"./saved.h5\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = []\nsample_sub = pd.read_csv('../input/cassava-leaf-disease-classification/sample_submission.csv')\n\nfor image in sample_sub.image_id:\n    img = keras.preprocessing.image.load_img('../input/cassava-leaf-disease-classification/test_images/' + image)\n    img = keras.preprocessing.image.img_to_array(img)\n    img = keras.preprocessing.image.smart_resize(img, (331, 331))\n    img = np.expand_dims(img, 0)\n    prediction = model.predict(img)\n    preds.append(np.argmax(prediction))\n\nmy_submission = pd.DataFrame({'image_id': sample_sub.image_id, 'label': preds})\nmy_submission.to_csv('submission.csv', index=False) ","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}