{"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":"import os\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-10T07:31:15.781619Z","iopub.execute_input":"2022-09-10T07:31:15.782356Z","iopub.status.idle":"2022-09-10T07:31:15.80753Z","shell.execute_reply.started":"2022-09-10T07:31:15.782233Z","shell.execute_reply":"2022-09-10T07:31:15.80675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = os.listdir('../input/datathonindoml-2022/train/train/')","metadata":{"execution":{"iopub.status.busy":"2022-09-10T07:31:17.708992Z","iopub.execute_input":"2022-09-10T07:31:17.709428Z","iopub.status.idle":"2022-09-10T07:31:18.164689Z","shell.execute_reply.started":"2022-09-10T07:31:17.70939Z","shell.execute_reply":"2022-09-10T07:31:18.16359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tifffile","metadata":{"execution":{"iopub.status.busy":"2022-09-10T07:31:19.766446Z","iopub.execute_input":"2022-09-10T07:31:19.766869Z","iopub.status.idle":"2022-09-10T07:31:19.9709Z","shell.execute_reply.started":"2022-09-10T07:31:19.766835Z","shell.execute_reply":"2022-09-10T07:31:19.968715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = tifffile.imread('../input/datathonindoml-2022/train/train/'+images[9])\nimport matplotlib.pyplot as plt\nplt.imshow(img[100:900, 100:700]/255) \nimg.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-10T07:31:21.152011Z","iopub.execute_input":"2022-09-10T07:31:21.152353Z","iopub.status.idle":"2022-09-10T07:31:21.414377Z","shell.execute_reply.started":"2022-09-10T07:31:21.152325Z","shell.execute_reply":"2022-09-10T07:31:21.413443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('../input/datathonindoml-2022/train_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2022-09-10T07:31:23.066451Z","iopub.execute_input":"2022-09-10T07:31:23.069038Z","iopub.status.idle":"2022-09-10T07:31:23.086867Z","shell.execute_reply.started":"2022-09-10T07:31:23.069Z","shell.execute_reply":"2022-09-10T07:31:23.085713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-10T07:31:24.481822Z","iopub.execute_input":"2022-09-10T07:31:24.482681Z","iopub.status.idle":"2022-09-10T07:31:24.490879Z","shell.execute_reply.started":"2022-09-10T07:31:24.482646Z","shell.execute_reply":"2022-09-10T07:31:24.489597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Conv2D, Dense, LayerNormalization, Activation, MaxPool2D, GlobalAveragePooling2D, Add, Input, Flatten\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.regularizers import l2\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import Adam\ntf.config.run_functions_eagerly(True)\n\nn = 9 # 56 layers\nchannels = [ 16*1.5, 32*1.5, 64*1.5 ]\n\ninputs = Input(shape=(800, 600, 3))\nx = Conv2D(channels[0], kernel_size=(4, 4), padding=\"same\", kernel_initializer=\"he_normal\", kernel_regularizer=l2(1e-4))(inputs)\nx = LayerNormalization()(x)\nx = Activation(tf.nn.gelu)(x)\n\nfor c in channels:\n    for i in range(n):\n        subsampling = i == 0 and c > 16\n        strides = (2, 2) if subsampling else (1, 1)\n        y = Conv2D(c, kernel_size=(7, 7), padding=\"same\", strides=strides, kernel_initializer=\"he_normal\", kernel_regularizer=l2(1e-4))(x)\n        y = Activation(tf.nn.gelu)(y)\n        y = Conv2D(c, kernel_size=(2, 2), padding=\"same\", kernel_initializer=\"he_normal\", kernel_regularizer=l2(1e-4))(y)\n        y = LayerNormalization()(y)        \n        if subsampling:\n            x = Conv2D(c, kernel_size=(1, 1), strides=(2, 2), padding=\"same\", kernel_initializer=\"he_normal\", kernel_regularizer=l2(1e-4))(x)\n        x = Add()([x, y])\n        x = Activation(tf.nn.gelu)(x)\n\nx = GlobalAveragePooling2D()(x)\nx = Flatten()(x)\nx = Dense(3000, activation = tf.nn.relu)(x)\nx = Dense(1500, activation = tf.nn.relu)(x)\nx = Dense(1000, activation = tf.nn.relu)(x)\nx  = Dense(100, activation = tf.nn.relu)(x)\noutputs = Dense(16, activation=tf.nn.softmax, kernel_initializer=\"he_normal\")(x)\n\nmodel = Model(inputs=inputs, outputs=outputs)\nmodel.type = \"resnet\" + str(6 * n + 2)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T07:32:34.707399Z","iopub.execute_input":"2022-09-10T07:32:34.708393Z","iopub.status.idle":"2022-09-10T07:32:35.998395Z","shell.execute_reply.started":"2022-09-10T07:32:34.708344Z","shell.execute_reply":"2022-09-10T07:32:35.997452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()\n\nfrom tensorflow.keras.utils import plot_model\nplot_model(model, to_file='model.png')","metadata":{"execution":{"iopub.status.busy":"2022-09-10T07:32:36.277362Z","iopub.execute_input":"2022-09-10T07:32:36.278065Z","iopub.status.idle":"2022-09-10T07:32:39.019616Z","shell.execute_reply.started":"2022-09-10T07:32:36.278025Z","shell.execute_reply":"2022-09-10T07:32:39.018119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras.backend as K","metadata":{"execution":{"iopub.status.busy":"2022-09-10T07:32:55.927682Z","iopub.execute_input":"2022-09-10T07:32:55.928516Z","iopub.status.idle":"2022-09-10T07:32:55.93351Z","shell.execute_reply.started":"2022-09-10T07:32:55.928443Z","shell.execute_reply":"2022-09-10T07:32:55.932558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_f1(y_true, y_pred): \n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0.0, 1.0)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0.0, 1.0)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    recall = true_positives / (possible_positives + K.epsilon())\n    f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())\n    return f1_val","metadata":{"execution":{"iopub.status.busy":"2022-09-10T07:32:57.258639Z","iopub.execute_input":"2022-09-10T07:32:57.259435Z","iopub.status.idle":"2022-09-10T07:32:57.273311Z","shell.execute_reply.started":"2022-09-10T07:32:57.259388Z","shell.execute_reply":"2022-09-10T07:32:57.267665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import f1_score, confusion_matrix\nmodel.compile(loss = 'categorical_crossentropy', metrics = ['accuracy'], optimizer = Adam(learning_rate = 0.0003))\n\n#accuracy is positively correlated with f1-score. Hence, accuracy metric is a viable option","metadata":{"execution":{"iopub.status.busy":"2022-09-10T07:33:00.928285Z","iopub.execute_input":"2022-09-10T07:33:00.928649Z","iopub.status.idle":"2022-09-10T07:33:01.240295Z","shell.execute_reply.started":"2022-09-10T07:33:00.92862Z","shell.execute_reply":"2022-09-10T07:33:01.239198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(16):\n    path = './' + str(i)\n    os.mkdir(path)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T07:33:03.238232Z","iopub.execute_input":"2022-09-10T07:33:03.238713Z","iopub.status.idle":"2022-09-10T07:33:03.245Z","shell.execute_reply.started":"2022-09-10T07:33:03.238677Z","shell.execute_reply":"2022-09-10T07:33:03.243941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Dividing the images as per their labels\n\nfrom PIL import Image\nimport numpy as np\nfor i, j in zip(labels['id'],labels['label']):\n    path = './'+str(j)+'/'\n    img = tifffile.imread('../input/datathonindoml-2022/train/train/'+str(i)+'.tif')\n    img = img[100:900, 100:700]/255\n    image = Image.fromarray(img)\n    image.convert('RGB').save(path+str(i)+'.png')","metadata":{"execution":{"iopub.status.busy":"2022-09-10T07:33:35.197927Z","iopub.execute_input":"2022-09-10T07:33:35.198606Z","iopub.status.idle":"2022-09-10T07:46:23.283794Z","shell.execute_reply.started":"2022-09-10T07:33:35.198568Z","shell.execute_reply":"2022-09-10T07:46:23.282763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.rmdir('./.virtual_documents')\ntf.data.experimental.enable_debug_mode()\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory\n\nds_train = image_dataset_from_directory(\n                './',\n                labels = 'inferred',\n                label_mode='categorical',\n                color_mode = 'rgb',\n                batch_size = 3,\n                image_size = (800, 600),\n                shuffle = True,\n                seed = 123,\n                validation_split = 0.1,\n                subset = 'training',\n                \n)\n\nds_validation = image_dataset_from_directory(\n                './',\n                labels = 'inferred',\n                label_mode='categorical',\n                color_mode = 'rgb',\n                batch_size = 3,\n                image_size = (800, 600),\n                shuffle = True,\n                seed = 123,\n                validation_split = 0.1,\n                subset = 'validation',\n                \n)\n\nfrom tensorflow.keras.callbacks import EarlyStopping\nimport gc\ngc.collect()\nmodel.fit(ds_train,\n          epochs = 15,\n          batch_size = 3,\n          validation_data = ds_validation,\n          callbacks = [EarlyStopping(monitor='loss', patience = 2)])","metadata":{"execution":{"iopub.status.busy":"2022-09-10T08:41:03.502263Z","iopub.execute_input":"2022-09-10T08:41:03.503358Z","iopub.status.idle":"2022-09-10T08:41:03.592175Z","shell.execute_reply.started":"2022-09-10T08:41:03.503222Z","shell.execute_reply":"2022-09-10T08:41:03.59074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('./saved_model')\nmy_model = model.save('./saved_model/document_classifier.h5')\nimport pickle","metadata":{"execution":{"iopub.status.busy":"2022-08-23T18:06:53.124855Z","iopub.status.idle":"2022-08-23T18:06:53.125355Z","shell.execute_reply.started":"2022-08-23T18:06:53.125098Z","shell.execute_reply":"2022-08-23T18:06:53.125122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nmodel = keras.models.load_model('document_classifier')","metadata":{"execution":{"iopub.status.busy":"2022-08-23T18:06:53.128779Z","iopub.status.idle":"2022-08-23T18:06:53.129273Z","shell.execute_reply.started":"2022-08-23T18:06:53.12901Z","shell.execute_reply":"2022-08-23T18:06:53.129033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}