{"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 5GB 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 Libraries"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nimport matplotlib.pyplot as plt\nfrom keras.preprocessing.image import ImageDataGenerator\nimport seaborn as sns\nsns.set()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Loading the Training Data Images"},{"metadata":{"trusted":true},"cell_type":"code","source":"#we also apply some amount of image augmentation in order to increase the robustness of the model\n\ntrain_gen=ImageDataGenerator(rescale=1./255,\n                            horizontal_flip=True,\n                            vertical_flip=False,\n                            rotation_range=20)\n\ntrain_data=train_gen.flow_from_directory('/kaggle/input/dlai3-phase3/DLAI3_Phase3/DLAI3_Phase3',\n                                        target_size=(512,512),\n                                        color_mode='rgb',\n                                        batch_size=128)\n#training_images=tf.image.grayscale_to_rgb(images=train_data)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Importing the Validation Dataset *(**sorting is required : shuffle=False )*"},{"metadata":{"trusted":true},"cell_type":"code","source":"#creating a transfer learning model\n#downloading a siutable model here we use Xception Module\n#removing the top FCN (fully connected layers) as its not required for our specific problem\nxcp_framework=keras.applications.Xception(weights='imagenet',\n                                              include_top=False,\n                                              input_shape=(512,512,3))\n#freezing the bottom layers so as to not trian them.\nfor layer in xcp_framework.layers:\n    \n    layer.trainable=False\n    \nrecall=keras.metrics.Recall()\ncallback=tf.keras.callbacks.EarlyStopping(monitor='loss',patience=2)\n\nmy_model=keras.models.Sequential()\nmy_model.add(xcp_framework)\nmy_model.add(keras.layers.Flatten())\nmy_model.add(keras.layers.Dropout(0.5))\nmy_model.add(keras.layers.BatchNormalization())\nmy_model.add(keras.layers.Dense(512,activation='relu',kernel_initializer='he_uniform'))\nmy_model.add(keras.layers.Dropout(0.2))\nmy_model.add(keras.layers.Dense(3,activation='softmax'))\nmy_model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['categorical_accuracy'])   \n    \nhistory=my_model.fit(train_data,batch_size=512,epochs=10,callbacks=[callback])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Plotting the Training loss & Training Accuracy vs Epochs**"},{"metadata":{"trusted":true},"cell_type":"code","source":"n=len(history.history['loss'])\nplt.plot(np.arange(0,n),history.history['loss'])\nplt.plot(np.arange(0,n),history.history['categorical_accuracy'])\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Predicting the Classes using the method model.predict_classes()"},{"metadata":{"trusted":true},"cell_type":"code","source":"#we dont apply augmentation to the test set as it may alter our results to some extent.\n#the image_directory doesnt have ordered files, \n#so shuffle is set to False, to retrieve files in alphanumeric ordering\n\nvalid_gen=ImageDataGenerator(rescale=1./255,\n                            horizontal_flip=False,\n                            vertical_flip=False\n                            )\nvalid_data=valid_gen.flow_from_directory('/kaggle/input/dlai3-phase3/VALIDATE',\n                                        target_size=(512,512),\n                                        color_mode='rgb',\n                                        batch_size=128,\n                                        shuffle=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classes=my_model.predict(valid_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"final_predict=np.argmax(classes,axis=-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"clas_pred=pd.DataFrame({'Code':final_predict})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"clas_pred.head(10)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We need to change our Label Encoding of the predictions according that required by the sample submissions.\nWe first check the encoding of the **flow_from_directory : train_data.class_indices** method"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.class_indices","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"final=clas_pred.replace(to_replace=[0,1,2],value=[2,0,1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"final.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"final.to_csv('submission_finale.csv',index=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"clas_pred.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classes[1][0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"my_list=classes.copy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(classes.shape[0]):\n    for j in range(classes.shape[1]):\n        if classes[i][0]>=0.3:\n            my_list[i][0]=1\n            my_list[i][1]=0\n            my_list[i][2]=0\n            \n        else:\n            pass\n            \n            \n                \n                \n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pr_trade=np.argmax(my_list,axis=-1)\npr_pred=pd.DataFrame({'Code':pr_trade})\npr_encoded=pr_pred.replace(to_replace=[0,1,2],value=[2,0,1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pr_encoded.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pr_encoded.to_csv('finale.csv',index=True)","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}