{"cells":[{"metadata":{},"cell_type":"markdown","source":"Importing All the Required Files"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport tensorflow as tf\nimport matplotlib.image as mpimg\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"In this competition use of internet is not allowed while submitting the result, so we have to download the trained weights and upload them and then we have to submit our final output"},{"metadata":{},"cell_type":"markdown","source":"for saving the weights we will use h5py format which is used to store large amount of data here we will use it to save our trained weights "},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install h5py\nprint('Ready to save models in the h5 format.')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"the h5py is already installed which is good."},{"metadata":{},"cell_type":"markdown","source":"before training our model and saving the trained weights you only will find one file in present working dir that is '__notebook_source__.ipynb' one we saved the weights then we will find that saved weights in this working dir"},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir('/kaggle/working')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"num_test_images=len(os.listdir('../input/cassava-leaf-disease-classification/test_images'))\nnum_test_images","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"we only have one test image so we will not use imagedatagenerator for test image we will directly use cv2 and convert it into suitable form"},{"metadata":{"trusted":true},"cell_type":"code","source":"num_train_images=len(os.listdir('../input/cassava-leaf-disease-classification/train_images'))\nnum_train_images","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"we have 21397 trained images which is quite a large number"},{"metadata":{"trusted":true},"cell_type":"code","source":"from collections import Counter\ndata=pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\nprint(Counter(data['label']))\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"as you see there are-: \n<p>for class 0 (Cassava Bacterial Blight (CBB)): 1087<p>\n<p>for class 1 (Cassava Brown Streak Disease (CBSD)) : 2189,<p> \n<p>for class 2 (Cassava Green Mottle (CGM)): 2386,<p>\n<p>for class 3 (Cassava Mosaic Disease (CMD)): 13158, and<p> \n<p>for class 4 (Healthy): 2577 number of images<p>"},{"metadata":{},"cell_type":"markdown","source":"lets take look at the only test image we have"},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\nimage=cv2.imread('../input/cassava-leaf-disease-classification/test_images/2216849948.jpg')\nplt.figure(figsize=(20,10))\nplt.imshow(image)\nplt.axis('off')\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub=pd.read_csv('../input/cassava-leaf-disease-classification/sample_submission.csv')\ntrain=pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\ntrain['label']=train['label'].astype('str')\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"lets take a look at some example of trainig images."},{"metadata":{"trusted":true},"cell_type":"code","source":"plant = os.listdir('../input/cassava-leaf-disease-classification/train_images')\nplant_dir = '../input/cassava-leaf-disease-classification/train_images'\n\nplt.figure(figsize=(20, 10))\n\nfor i in range(9):\n    plt.subplot(3, 3, i + 1)\n    img = plt.imread(os.path.join(plant_dir,plant[i])) \n    plt.imshow(img)\n    plt.axis('off')   \nplt.tight_layout()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n        rescale=1./255,\n        shear_range=0.2,\n        zoom_range=0.2,\n        horizontal_flip=True)\n\ntrain_data=train_datagen.flow_from_dataframe(dataframe=train,\n                                             directory='../input/cassava-leaf-disease-classification/train_images',\n                                             x_col='image_id',\n                                             y_col='label',\n                                             batch_size=256,\n                                             target_size=(150,150)      \n                                            )\n\nvalidation_data=train_datagen.flow_from_dataframe(dataframe=train,\n                                             directory='../input/cassava-leaf-disease-classification/train_images',\n                                             x_col='image_id',\n                                             y_col='label',\n                                             batch_size=256,\n                                             target_size=(150,150)\n                                            )","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"use this part of code when try to trained the model first time when we dont have our own trained weights"},{"metadata":{"trusted":true},"cell_type":"code","source":"#pretrained_base= tf.keras.applications.EfficientNetB0(\n#    include_top=False,\n#    weights=\"imagenet\",\n#    input_shape=(150,150,3),\n#   classifier_activation=\"softmax\"\n#)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"use this part of when we have our own trained weights."},{"metadata":{"trusted":true},"cell_type":"code","source":"efficientnet0_base= tf.keras.applications.EfficientNetB0(\n    include_top=False,\n    weights=None,\n    input_shape=(150,150,3),\n    classifier_activation=\"softmax\"\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"our neutral network structure."},{"metadata":{"trusted":true},"cell_type":"code","source":"model=tf.keras.models.Sequential([    \n    efficientnet0_base,\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128,activation='relu'),\n    tf.keras.layers.Dense(5,activation='softmax')   \n\n])\n\nmodel.summary()\n\nmodel.compile(loss='categorical_crossentropy',optimizer=tf.keras.optimizers.Adam(lr=0.001),metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"loading our own trained weights."},{"metadata":{"trusted":true},"cell_type":"code","source":"model.load_weights('../input/best-model/best_model.hdf5')\nprint('weights_loaded_successfully')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"defining early stopping for overfitting this time only we only using 7 epoch so possibility of overfitting is less so better result increase the number of epoch. as the epoch number increases the training time also increases."},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\n\nearly_stopping = EarlyStopping(\n    min_delta=0.001,\n    patience=5,\n    restore_best_weights=True,\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"defining modelcheckpoint for saving the trained weights."},{"metadata":{"trusted":true},"cell_type":"code","source":"model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(\n    filepath='best_model.hdf5',\n    monitor='val_loss',\n    save_weights_only=True,\n    save_best_only=True,\n)\n\nprint('model checkpoint created successfully')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"fitting the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"history=model.fit(train_data,epochs=7,validation_data=validation_data,callbacks=[early_stopping,model_checkpoint_callback])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history_df=pd.DataFrame(history.history)\nhistory_df.loc[:,['loss','val_loss']].plot();\nprint(\"Minimum validation loss: {}\".format(history_df['val_loss'].min()))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"this graph will look better if we increase the epoch and other parameters"},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\ntest_img = cv2.imread('../input/cassava-leaf-disease-classification/test_images/2216849948.jpg')\ntest_img = cv2.resize(test_img,(150,150))\ntest_img = np.reshape(test_img,[1,150,150,3])\nlabel = model.predict(test_img)\nlabel=np.argmax(label)\nlabel","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"this is our prediction"},{"metadata":{"trusted":true},"cell_type":"code","source":"sub['label']=label\nsub.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"saving prediction in .csv file"},{"metadata":{},"cell_type":"markdown","source":"work in progress! \n\n"}],"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}