{"cells":[{"metadata":{"id":"f16kwJp0YXGu"},"cell_type":"markdown","source":"**In this notebook, I done classifing the four disease of casava leaf disease**"},{"metadata":{"_uuid":"7b88cbdc-c525-4dfa-bb40-fed23c0ac2ef","_cell_guid":"ba273dd3-0722-495e-bfb9-9c07e9db3ca5","trusted":true,"id":"bWgvkFbTQwZ6"},"cell_type":"code","source":"# Importing the required packages\nimport pandas  as pd\nimport numpy as np\nimport matplotlib.pyplot  as plt\n\nimport tensorflow as tf \nfrom tensorflow.keras import applications\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense, Dropout, BatchNormalization, GlobalAveragePooling2D","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"HmySC0LhQwZ-"},"cell_type":"code","source":"train_csv_path = \"../input/cassava-leaf-disease-classification/train.csv\"\nlabel_json_path = \"../input/cassava-leaf-disease-classification/label_num_to_disease_map.json\"\nimages_dir_path = \"../input/cassava-leaf-disease-classification/train_images\"","execution_count":null,"outputs":[]},{"metadata":{"id":"ws8Wd-cTY2cp"},"cell_type":"markdown","source":"The dataset has 5 classes [4 diseases and 1 Healthy]. Lets omit the Healthy class for disease only classification."},{"metadata":{"id":"_a-mV8XqH4vk","trusted":true},"cell_type":"code","source":"label_class = pd.read_json(label_json_path, orient='index')\nlabel_class = label_class[:4]\nlabel_class = label_class.values.flatten().tolist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"KSbAsnETQwZ_"},"cell_type":"code","source":"train_csv = pd.read_csv(train_csv_path)\ntrain_csv['label'] = train_csv['label'].astype('string')\n\ntrain_csv = train_csv[train_csv['label']!='4']","execution_count":null,"outputs":[]},{"metadata":{"id":"5hrQStBwZSB6"},"cell_type":"markdown","source":"The give dataset is highly imbalanced. I tried to balance it out by calculating weight of class and use it to penalize the loss during training."},{"metadata":{"id":"0_Xejhr0G3OQ","outputId":"4c8c1564-1696-40a9-f2a9-6f01dd056b66","trusted":true},"cell_type":"code","source":"total = len(train_csv)\nlabel_number = dict(train_csv.label.value_counts())\n\nweight_of_class = {}\n\nfor i in range(4):\n    weight_of_class[i] = (total - label_number[str(i)])/total\n    print(f\"Weight of {label_class[i]} : {weight_of_class[i]}\")","execution_count":null,"outputs":[]},{"metadata":{"id":"EKRlOSzVg7lG","trusted":true},"cell_type":"code","source":"# Declaring some constants\nBATCH_SIZE = 16\nIMG_SIZE = 320\nEPOCHS = 16\nlr = 5e-5","execution_count":null,"outputs":[]},{"metadata":{"id":"qcbWU_0jQwaA"},"cell_type":"markdown","source":"# **Data Agumentation and Pre-Processing**"},{"metadata":{"trusted":true,"id":"-YHIXcjaQwaA"},"cell_type":"code","source":"# Data agumentation and pre-processing using tensorflow\ntrain_gen = ImageDataGenerator(\n                                rotation_range=270,\n                                width_shift_range=0.1,\n                                height_shift_range=0.1,\n                                brightness_range=[0.1,0.9],\n                                shear_range=25,\n                                zoom_range=0.3,\n                                channel_shift_range=0.1,\n                                horizontal_flip=True,\n                                vertical_flip=True,\n                                rescale=1/255,\n                                validation_split=0.15\n                               )\n                                    \n    \nvalid_gen = ImageDataGenerator(rescale=1/255,\n                               validation_split = 0.15\n                              )\n","execution_count":null,"outputs":[]},{"metadata":{"id":"8JuAz3g4QwaA"},"cell_type":"markdown","source":"# **Loading data to Generator**"},{"metadata":{"trusted":true,"id":"ceDp6KDlQwaA","outputId":"08484698-d981-4a16-9619-f389448fd0fe"},"cell_type":"code","source":"train_generator = train_gen.flow_from_dataframe(\n                            dataframe=train_csv,\n                            directory = images_dir_path,\n                            x_col = \"image_id\",\n                            y_col = \"label\",\n                            target_size = (IMG_SIZE, IMG_SIZE),\n                            class_mode = \"sparse\",\n                            batch_size = BATCH_SIZE,\n                            shuffle = True,\n                            subset = \"training\",\n)\n\nvalid_generator = valid_gen.flow_from_dataframe(\n                            dataframe=train_csv,\n                            directory = images_dir_path,\n                            x_col = \"image_id\",\n                            y_col = \"label\",\n                            target_size = (IMG_SIZE, IMG_SIZE),\n                            class_mode = \"sparse\",\n                            batch_size = BATCH_SIZE,\n                            shuffle = True,\n                            subset = \"validation\"\n)","execution_count":null,"outputs":[]},{"metadata":{"id":"mFvNrXLlQwaB"},"cell_type":"markdown","source":"# **Building The Model**"},{"metadata":{"trusted":true,"id":"aMc-vWOkQwaB"},"cell_type":"code","source":"def build_model():\n    # Loading the InceptionV3 architecture with imagenet weights as base\n    base = applications.InceptionV3(include_top=False, weights='imagenet',input_shape=[IMG_SIZE,IMG_SIZE,3])\n\n    model = tf.keras.Sequential()\n    model.add(base)\n    model.add(BatchNormalization(axis=-1))\n    model.add(GlobalAveragePooling2D())\n    model.add(Dropout(0.5))\n    model.add(Dense(512, activation='relu'))\n    model.add(Dropout(0.1))\n    model.add(Dense(1024, activation='relu'))\n    model.add(Dropout(0.5))\n    model.add(Dense(5, activation='softmax'))\n\n    model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(), optimizer=tf.keras.optimizers.Adam(learning_rate=lr), metrics=['acc'])\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"JAG8xoy4QwaB"},"cell_type":"code","source":"# A callback to save the model\ncallback0 = tf.keras.callbacks.ModelCheckpoint(\"/content/drive/MyDrive/casavaModel/CasavaLeafDiseaseModel_ckpt.h5\", \n                                               monitor='val_loss',save_best_only=True)","execution_count":null,"outputs":[]},{"metadata":{"id":"0ltMH4vCJpmP","outputId":"c5bc8338-0d33-4606-9e94-46c42fae2a0f","trusted":true},"cell_type":"code","source":"model = build_model()\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"id":"NEm5-iCXQwaC"},"cell_type":"markdown","source":"# **Training The Model**"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Training the model with class_weight parameter\nhis = model.fit(train_generator, validation_data=valid_generator, epochs=EPOCHS, callbacks=[callback0], class_weight=weight_of_class)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"stats = pd.DataFrame(his.history)\nloss_stats = stats[['loss', 'val_loss']]\nacc_stats = stats[['acc', 'val_acc']]\nloss_stats.plot()\nacc_stats.plot()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"In [here](http://https://www.kaggle.com/manojkumars00/cassava-leaf-disease-healthy-unhealthy) I classified the data to healthy and Unhealthy.\n\nIf the prediction is Unhealthy it is further given to the above trained model to predict what is the disease.\n\nThe trained models are used in below notebook for casava leaf disease classification.\nhttps://www.kaggle.com/manojkumars00/casava-leaf-disease-simple-classification\nCheck it out."}],"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}