{"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_minor":4,"nbformat":4,"cells":[{"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\ncnt=0\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        if(cnt<5):\n            print(os.path.join(dirname, filename))\n        cnt=cnt+1\n\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-08T16:45:41.982013Z","iopub.execute_input":"2022-10-08T16:45:41.982364Z","iopub.status.idle":"2022-10-08T16:45:44.452806Z","shell.execute_reply.started":"2022-10-08T16:45:41.982335Z","shell.execute_reply":"2022-10-08T16:45:44.450615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:45:44.454546Z","iopub.execute_input":"2022-10-08T16:45:44.455146Z","iopub.status.idle":"2022-10-08T16:45:44.463842Z","shell.execute_reply.started":"2022-10-08T16:45:44.455106Z","shell.execute_reply":"2022-10-08T16:45:44.462774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/datathonindoml-2022/train_labels.csv\")\ncolumns = 4\nrows = 1\n\nclass_dictionary = {\n    0:'memo',\n    1:'form',\n    2:'email',\n    3:'handwritten',\n    4:'advertisement',\n    5:'scientific_report',\n    6:'scientific_publication',\n    7:'specification',\n    8:'file_folder',\n    9:'news_article',\n    10:'budget',\n    11:'invoice',\n    12:'presentation',\n    13:'questionaire',\n    14:'resume',\n    15:'letter'\n}","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:45:44.468074Z","iopub.execute_input":"2022-10-08T16:45:44.468650Z","iopub.status.idle":"2022-10-08T16:45:44.487093Z","shell.execute_reply.started":"2022-10-08T16:45:44.468618Z","shell.execute_reply":"2022-10-08T16:45:44.486277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df.head())","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:45:44.491851Z","iopub.execute_input":"2022-10-08T16:45:44.493953Z","iopub.status.idle":"2022-10-08T16:45:44.504540Z","shell.execute_reply.started":"2022-10-08T16:45:44.493919Z","shell.execute_reply":"2022-10-08T16:45:44.503647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:45:44.508560Z","iopub.execute_input":"2022-10-08T16:45:44.510624Z","iopub.status.idle":"2022-10-08T16:45:44.520881Z","shell.execute_reply.started":"2022-10-08T16:45:44.510590Z","shell.execute_reply":"2022-10-08T16:45:44.519764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = \"../input/datathonindoml-2022\"\ntrain_dir = os.path.join(base_dir, 'train')\nvalidation_dir = os.path.join(base_dir, 'validation')","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:45:44.525576Z","iopub.execute_input":"2022-10-08T16:45:44.528359Z","iopub.status.idle":"2022-10-08T16:45:44.537642Z","shell.execute_reply.started":"2022-10-08T16:45:44.528321Z","shell.execute_reply":"2022-10-08T16:45:44.536433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for ind in df.index:\n    path = os.path.join(\"./trainclass\",str(df['label'][ind]))\n    if not os.path.exists(path):\n        os.makedirs(path)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:45:44.538749Z","iopub.execute_input":"2022-10-08T16:45:44.539394Z","iopub.status.idle":"2022-10-08T16:45:44.730959Z","shell.execute_reply.started":"2022-10-08T16:45:44.539362Z","shell.execute_reply":"2022-10-08T16:45:44.730125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:46:11.643442Z","iopub.execute_input":"2022-10-08T16:46:11.643821Z","iopub.status.idle":"2022-10-08T16:46:11.648827Z","shell.execute_reply.started":"2022-10-08T16:46:11.643789Z","shell.execute_reply":"2022-10-08T16:46:11.647738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for ind in df.index:\n    path = os.path.join(\"./trainclass\",str(df['label'][ind]))\n    shutil.copy('../input/datathonindoml-2022/train/train/'+str(df['id'][ind])+'.tif', './trainclass/'+str(df['label'][ind]))\n    ","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:46:14.776447Z","iopub.execute_input":"2022-10-08T16:46:14.776825Z","iopub.status.idle":"2022-10-08T16:47:25.559171Z","shell.execute_reply.started":"2022-10-08T16:46:14.776794Z","shell.execute_reply":"2022-10-08T16:47:25.558208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n# Add our data-augmentation parameters to ImageDataGenerator\ntrain_datagen = ImageDataGenerator(rescale = 1./255.,rotation_range = 40, width_shift_range = 0.2, height_shift_range = 0.2, shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True,validation_split=0.2)\n\n# Note that the validation data should not be augmented!\nval_datagen = ImageDataGenerator( rescale = 1.0/255. )\n\n","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:49:02.903167Z","iopub.execute_input":"2022-10-08T16:49:02.903767Z","iopub.status.idle":"2022-10-08T16:49:03.861966Z","shell.execute_reply.started":"2022-10-08T16:49:02.903709Z","shell.execute_reply":"2022-10-08T16:49:03.861008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain_data = train_datagen.flow_from_directory('./trainclass',target_size=(256,256),batch_size=128,subset=\"training\",color_mode=\"grayscale\")\n\ntest_val_data = train_datagen.flow_from_directory('./trainclass',target_size=(256,256),batch_size=128,subset=\"validation\",color_mode=\"grayscale\")\n\nvalidation_data = val_datagen.flow_from_directory('../input/datathonindoml-2022/validation',target_size=(256,256),batch_size=128,color_mode=\"grayscale\")","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:49:09.691030Z","iopub.execute_input":"2022-10-08T16:49:09.691402Z","iopub.status.idle":"2022-10-08T16:49:10.663227Z","shell.execute_reply.started":"2022-10-08T16:49:09.691364Z","shell.execute_reply":"2022-10-08T16:49:10.662221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:53:10.855975Z","iopub.execute_input":"2022-10-08T16:53:10.856336Z","iopub.status.idle":"2022-10-08T16:53:10.861233Z","shell.execute_reply.started":"2022-10-08T16:53:10.856306Z","shell.execute_reply":"2022-10-08T16:53:10.860250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Conv2D, MaxPool2D, Flatten, Dense, InputLayer, BatchNormalization, Dropout\n\n# build a sequential model\nmodel = Sequential()\nmodel.add(InputLayer(input_shape=(256, 256, 1)))\n\n# 1st conv block\nmodel.add(Conv2D(25, (5, 5), activation='relu', strides=(1, 1), padding='same'))\nmodel.add(MaxPool2D(pool_size=(2, 2), padding='same'))\n# 2nd conv block\nmodel.add(Conv2D(50, (5, 5), activation='relu', strides=(2, 2), padding='same'))\nmodel.add(MaxPool2D(pool_size=(2, 2), padding='same'))\nmodel.add(BatchNormalization())\n# 3rd conv block\nmodel.add(Conv2D(70, (3, 3), activation='relu', strides=(2, 2), padding='same'))\nmodel.add(MaxPool2D(pool_size=(2, 2), padding='valid'))\nmodel.add(BatchNormalization())\n# ANN block\nmodel.add(Flatten())\nmodel.add(Dense(units=100, activation='relu'))\nmodel.add(Dense(units=100, activation='relu'))\nmodel.add(Dropout(0.25))\n# output layer\nmodel.add(Dense(units=16, activation='softmax'))\n\n# compile model\nmodel.compile(loss='categorical_crossentropy', optimizer=\"adam\", metrics=['accuracy'])\n# fit on data for 30 epochs\n","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:55:47.777822Z","iopub.execute_input":"2022-10-08T16:55:47.778195Z","iopub.status.idle":"2022-10-08T16:55:47.866063Z","shell.execute_reply.started":"2022-10-08T16:55:47.778165Z","shell.execute_reply":"2022-10-08T16:55:47.865187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit_generator(train_data, epochs=30, validation_data=test_val_data)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:55:50.666435Z","iopub.execute_input":"2022-10-08T16:55:50.666818Z","iopub.status.idle":"2022-10-08T17:44:01.073363Z","shell.execute_reply.started":"2022-10-08T16:55:50.666786Z","shell.execute_reply":"2022-10-08T17:44:01.072391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"Models/Model_1_CNN.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-10-08T18:25:35.313977Z","iopub.execute_input":"2022-10-08T18:25:35.314667Z","iopub.status.idle":"2022-10-08T18:25:35.376773Z","shell.execute_reply.started":"2022-10-08T18:25:35.314632Z","shell.execute_reply":"2022-10-08T18:25:35.375862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(validation_data,verbose=1,batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T18:41:19.017459Z","iopub.execute_input":"2022-10-08T18:41:19.017853Z","iopub.status.idle":"2022-10-08T18:41:21.367007Z","shell.execute_reply.started":"2022-10-08T18:41:19.017820Z","shell.execute_reply":"2022-10-08T18:41:21.366106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_class = np.argmax(predictions,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T18:41:31.747029Z","iopub.execute_input":"2022-10-08T18:41:31.747399Z","iopub.status.idle":"2022-10-08T18:41:31.753294Z","shell.execute_reply.started":"2022-10-08T18:41:31.747369Z","shell.execute_reply":"2022-10-08T18:41:31.752129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fnames = [i.split(\"/\")[1].split(\".tif\")[0] for i in validation_data.filenames]\n\nsubmission = pd.DataFrame({\"id\": fnames,\"label\":pred_class},columns=[\"id\",\"label\"])\n\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-08T18:42:12.791849Z","iopub.execute_input":"2022-10-08T18:42:12.792215Z","iopub.status.idle":"2022-10-08T18:42:12.805152Z","shell.execute_reply.started":"2022-10-08T18:42:12.792185Z","shell.execute_reply":"2022-10-08T18:42:12.803941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T18:43:27.741851Z","iopub.execute_input":"2022-10-08T18:43:27.742389Z","iopub.status.idle":"2022-10-08T18:43:27.751594Z","shell.execute_reply.started":"2022-10-08T18:43:27.742355Z","shell.execute_reply":"2022-10-08T18:43:27.750609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}