{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Introduction\n\nWe  will try to retrain a pretrained inception_v3 model on the images. We will apply augmentations to the images and we will rescale to images to 300x300 pixels.\nThe augmentations will be performed using Keras preprocessing libraries and the Data Generator objects."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow  as  tf\nimport tensorflow.keras  as  keras","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Load the data\n\n* Load the train.csv file that contains the image labels\n* Load the labels map file"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import  json\n\ndf=pd.read_csv(\"../input/cassava-leaf-disease-classification/train.csv\")\ndf.head()\ndf[\"label\"]=df[\"label\"].astype(str)\ntrain_ratio=0.9\ndf_train=df[:int(train_ratio*len(df))]\ndf_valid=df[int(train_ratio*len(df)):]\n\nf=open(\"../input/cassava-leaf-disease-classification/label_num_to_disease_map.json\")\nlabels=json.loads(f.read())","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Labels Distribution\n\n* Are the data ballanced?"},{"metadata":{"trusted":true},"cell_type":"code","source":"group=df_train.groupby([\"label\"]).agg(\"count\")\nfrom matplotlib import  pyplot as plt\nfrom matplotlib.pyplot import figure\nfigure(num=None, figsize=(10, 10), dpi=80, facecolor='w', edgecolor='k')\nplt.pie(group.values,labels=group.index)\nplt.legend([v for k,v in labels.items()])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Plot Images\n\n* Lets plot some images  in order to see how the look like."},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nfrom PIL import Image\n\n\ndef plot_images(df,label=0,num_images=10,title=\"\"):\n    fig=figure(num=None, figsize=(10, 10), dpi=80, facecolor='w', edgecolor='k')\n    df=df[df[\"label\"]==label]\n    i=0\n    size = np.sqrt(num_images)\n    for index,row in df.iterrows():\n        i=i+1\n        if i>num_images:\n            break\n        plt.subplot(size, size,i)\n        img = Image.open('../input/cassava-leaf-disease-classification/train_images/'+row[\"image_id\"])\n        plt.imshow(img)\n    fig.suptitle(title, fontsize=20)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"k=\"0\"\nv=labels[k]\nplot_images(df_train,label=k,num_images=16,title=v)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"k=\"1\"\nv=labels[k]\nplot_images(df_train,label=k,num_images=16,title=v)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"k=\"2\"\nv=labels[k]\nplot_images(df_train,label=k,num_images=16,title=v)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"k=\"3\"\nv=labels[k]\nplot_images(df_train,label=k,num_images=16,title=v)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"k=\"4\"\nv=labels[k]\nplot_images(df_train,label=k,num_images=16,title=v)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Directories for the Training"},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nimg_size=300\nbatch_size=32\n\ntrain_datagen = ImageDataGenerator(\n      rescale=1./255,\n      rotation_range=40,\n      width_shift_range=0.2,\n      height_shift_range=0.2,\n      shear_range=0.2,\n      zoom_range=[1.0,2.0],\n      horizontal_flip=True,\n      vertical_flip=True,\n      fill_mode='nearest')\ntrain_generator = train_datagen.flow_from_dataframe(\n        dataframe = df_train,\n        directory='../input/cassava-leaf-disease-classification/train_images',\n        x_col = 'image_id',\n        y_col = 'label',     \n        target_size=(img_size, img_size),\n        batch_size=batch_size,\n        class_mode='sparse') \nvalid_datagen = ImageDataGenerator(\n      rescale=1./255)\nvalid_generator = valid_datagen.flow_from_dataframe(\n        dataframe = df_valid,\n        directory='../input/cassava-leaf-disease-classification/train_images',\n        x_col = 'image_id',\n        y_col = 'label',     \n        target_size=(img_size, img_size),\n        batch_size=batch_size,\n        class_mode='sparse') ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras import layers\nfrom tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping\nfrom tensorflow.keras.applications import EfficientNetB3\npre_trained_model = InceptionV3(input_shape = (img_size, img_size, 3), \n                                include_top = False, \n                                weights = None)\npre_trained_model.load_weights('../input/inceptionv3/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5')\n\ninput_shape=(img_size,img_size,3)\nfor layer in pre_trained_model.layers:\n    layer.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"last_layer = pre_trained_model.get_layer('mixed10')\nlast_output = last_layer.output\nfrom tensorflow.keras.optimizers import RMSprop\n\n# x = layers.Flatten()(last_output)\nx = layers.GlobalAveragePooling2D()(last_output)\nx = layers.Dense(1024, activation='relu')(x)\nx = layers.BatchNormalization()(x)\nx = layers.Activation(\"relu\")(x) \nx = layers.Dropout(0.2)(x)                  \nx = layers.Dense  (5, activation='softmax')(x)           \n\nmodel = Model( pre_trained_model.input, x) \n\n\ncallbacks = [ReduceLROnPlateau(monitor='val_loss', patience=1, verbose=1, factor=0.5),\n             EarlyStopping(monitor='val_loss', patience=3),\n             ModelCheckpoint(filepath='best_model.h5', monitor='val_loss', save_best_only=True)]\n\nmodel.compile(optimizer = RMSprop(lr=0.0001), \n              loss = 'sparse_categorical_crossentropy', \n              metrics = ['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(\n      train_generator,\n      steps_per_epoch=8,  \n      epochs=10,\n      verbose=1,\n      validation_data=valid_generator,\n      validation_steps=8,callbacks=callbacks)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history_frame = pd.DataFrame(history.history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history_frame","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.preprocessing import image\nimport os\n\n\nimage_ids=[]\nlabels=[]\nimgs=[]\nfor file in os.listdir('../input/cassava-leaf-disease-classification/test_images/'):\n    img = image.load_img('../input/cassava-leaf-disease-classification/test_images/'+file, target_size=(img_size, img_size))\n    x = image.img_to_array(img)\n    x=x/255.0\n    imgs.append(x)\n    x = np.expand_dims(x, axis=0)\n    images = np.vstack([x])\n    image_ids.append(file)\nimgs=np.array(imgs)\nclasses = model.predict(imgs)\nlabels=np.argmax(classes,axis=1)\nsubmission_df=pd.DataFrame()\nsubmission_df[\"image_id\"]=image_ids\nsubmission_df[\"label\"]=labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df.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}