{"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":{},"cell_type":"markdown","source":"## Importing necessary libraries"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport tensorflow as tf\nimport seaborn as sns\nimport numpy as np\nfrom tqdm import tqdm\nimport os\nimport pandas as pd\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Reading the csv file and visualizing the number of images in each class"},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/cassava-leaf-disease-classification/train.csv\",dtype=str)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(df.label)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Splitting Dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_df,valid_df = train_test_split(df,test_size = 0.2,stratify = df.label,random_state=42)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Creating a image Datagenerator"},{"metadata":{},"cell_type":"markdown","source":"Here I have created two image data generator for each training data and validation data with image size as (224,224) and (256,256) respectively. so while training the each generator return a list of two images with size(224,224) and (256,256) respectively and the label of that images."},{"metadata":{"trusted":true},"cell_type":"code","source":"train_imgen = tf.keras.preprocessing.image.ImageDataGenerator(rescale = 1./255, \n                                   shear_range = 0.2, \n                                   zoom_range = 0.2,\n                                   rotation_range=5.,\n                                   horizontal_flip = True)\n\ntest_imgen = tf.keras.preprocessing.image.ImageDataGenerator(rescale = 1./255)\n\n\n\ndef generate_generator_multiple(generator,df):\n    train_dir=\"../input/cassava-leaf-disease-classification/train_images/\"\n    genX1 = generator.flow_from_dataframe(train_df, directory=train_dir, x_col='image_id', y_col='label',\n                                         target_size=(224, 224),\n                                        class_mode='categorical', \n                                          batch_size=32)\n    \n    genX2 = generator.flow_from_dataframe(train_df, directory=train_dir, x_col='image_id', y_col='label',\n                                         target_size=(256, 256),\n                                        class_mode='categorical', \n                                          batch_size=32)\n\n    while True:\n            X1i = genX1.next()\n            X2i = genX2.next()\n            yield [X1i[0], X2i[0]], X2i[1]  #this will return a list of two images and the corresponding label\n            \n            \ntrain_generator=generate_generator_multiple(train_imgen,train_df)       \n     \nvalid_generator=generate_generator_multiple(test_imgen,valid_df)              ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### step size for training and valid images"},{"metadata":{"trusted":true},"cell_type":"code","source":"step_size_train = train_df.shape[0] // 32\nstep_size_valid = valid_df.shape[0] // 32","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Callbacks"},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\nreducelr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss',patience = 1,verbose=1)\nearlystop = tf.keras.callbacks.EarlyStopping(\n    monitor='val_loss',patience=2, verbose=1, mode='auto')\n\ncheckpoint = tf.keras.callbacks.ModelCheckpoint(\n            os.path.join(\"./model.h5\"),\n            monitor='train_loss', verbose=0,\n            save_best_only=True, save_weights_only=False,\n            mode='auto', save_freq='epoch'\n        )\ncallbacks_list = [reducelr,earlystop,checkpoint]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Building the model (xception with imagenet and image_size as (224,224) and xception with imagenet and image_size as (256,256)) "},{"metadata":{"trusted":true},"cell_type":"code","source":"base_model0 = tf.keras.applications.Xception(weights='imagenet',include_top=False,input_shape=(None,None,3))\nfor layer in base_model0.layers:\n    layer._name = layer._name+\"_01\"\nbase_model1 = tf.keras.applications.Xception(weights='imagenet',include_top=False,input_shape=(None,None,3))\nfor layer in base_model1.layers:\n    layer._name = layer._name+\"_02\"\n    \n    \n#base_model2 = Xception(weights='imagenet',include_top=False,input_shape=(None,None,3))\n#base_model3 = Xception(weights='imagenet',include_top=False,input_shape=(None,None,3))\nx0 = base_model0.output\nx0 = tf.keras.layers.GlobalAveragePooling2D()(x0)\nx1 = base_model1.output\nx1 = tf.keras.layers.GlobalAveragePooling2D()(x1)\n#x2 = base_model2.output\n#x2 = layers.GlobalAveragePooling2D()(x2)\n#x3 = base_model3.output\n#x3 = layers.GlobalAveragePooling2D()(x3)\nx = tf.keras.layers.concatenate([x0,x1])\nx = tf.keras.layers.Dense(5,activation=\"softmax\")(x)\nmodel = tf.keras.Model(inputs=(base_model0.input, base_model1.input), outputs=x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tf.keras.utils.plot_model(\n    model,\n    to_file=\"model.png\",\n    show_shapes=False,\n    show_layer_names=True,\n    rankdir=\"TB\",\n    expand_nested=False,\n    dpi=96,\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Calculating the class weight for each class"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.utils import class_weight\nclass_labels = sorted(df.label.unique())\nclass_weights = class_weight.compute_class_weight(\"balanced\", class_labels, df.label.values)\nclass_weights_dict = {i : class_weights[i] for i,label in enumerate(class_labels)}\nprint(class_weights_dict)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),loss=tf.keras.losses.CategoricalCrossentropy(),metrics=[\"accuracy\",\"AUC\"])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## training the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(train_generator,epochs=10,steps_per_epoch=step_size_train,validation_data=valid_generator,validation_steps=step_size_valid,class_weight=class_weights_dict,callbacks=callbacks_list)\n","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}