{"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-03T08:18:57.343513Z","iopub.execute_input":"2022-09-03T08:18:57.344824Z","iopub.status.idle":"2022-09-03T08:18:57.386895Z","shell.execute_reply.started":"2022-09-03T08:18:57.344714Z","shell.execute_reply":"2022-09-03T08:18:57.385851Z"},"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-03T08:18:57.550504Z","iopub.execute_input":"2022-09-03T08:18:57.551066Z","iopub.status.idle":"2022-09-03T08:18:58.290213Z","shell.execute_reply.started":"2022-09-03T08:18:57.551024Z","shell.execute_reply":"2022-09-03T08:18:58.288839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tifffile","metadata":{"execution":{"iopub.status.busy":"2022-09-03T08:18:58.293389Z","iopub.execute_input":"2022-09-03T08:18:58.294136Z","iopub.status.idle":"2022-09-03T08:18:58.43603Z","shell.execute_reply.started":"2022-09-03T08:18:58.29408Z","shell.execute_reply":"2022-09-03T08:18:58.434963Z"},"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-03T08:18:58.437428Z","iopub.execute_input":"2022-09-03T08:18:58.437818Z","iopub.status.idle":"2022-09-03T08:18:58.709899Z","shell.execute_reply.started":"2022-09-03T08:18:58.437781Z","shell.execute_reply":"2022-09-03T08:18:58.708811Z"},"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-03T08:19:01.705195Z","iopub.execute_input":"2022-09-03T08:19:01.7061Z","iopub.status.idle":"2022-09-03T08:19:01.726399Z","shell.execute_reply.started":"2022-09-03T08:19:01.706054Z","shell.execute_reply":"2022-09-03T08:19:01.725234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-03T08:19:03.070271Z","iopub.execute_input":"2022-09-03T08:19:03.070672Z","iopub.status.idle":"2022-09-03T08:19:03.078212Z","shell.execute_reply.started":"2022-09-03T08:19:03.070641Z","shell.execute_reply":"2022-09-03T08:19:03.076971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Conv2D, Dense, BatchNormalization, 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 = 7 # 44 layers\nchannels = [ 16*1.5, 32*1.5, 64*1.5 ]\n\ninputs = Input(shape=(800, 600, 3))\nx = Conv2D(channels[0], kernel_size=(7, 7), padding=\"same\", kernel_initializer=\"he_normal\", kernel_regularizer=l2(1e-4))(inputs)\nx = BatchNormalization()(x)\nx = Activation(tf.nn.relu)(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=(3, 3), padding=\"same\", strides=strides, kernel_initializer=\"he_normal\", kernel_regularizer=l2(1e-4))(x)\n        y = BatchNormalization()(y)\n        y = Activation(tf.nn.relu)(y)\n        y = Conv2D(c, kernel_size=(3, 3), padding=\"same\", kernel_initializer=\"he_normal\", kernel_regularizer=l2(1e-4))(y)\n        y = BatchNormalization()(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.relu)(x)\n\nx = GlobalAveragePooling2D()(x)\nx = Flatten()(x)\nx = Dense(3000, activation = tf.nn.relu)(x)\nx = Dense(1000, 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-03T08:19:05.255912Z","iopub.execute_input":"2022-09-03T08:19:05.256309Z","iopub.status.idle":"2022-09-03T08:19:14.473027Z","shell.execute_reply.started":"2022-09-03T08:19:05.256278Z","shell.execute_reply":"2022-09-03T08:19:14.472002Z"},"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-03T08:19:14.474896Z","iopub.execute_input":"2022-09-03T08:19:14.475254Z","iopub.status.idle":"2022-09-03T08:19:16.84294Z","shell.execute_reply.started":"2022-09-03T08:19:14.475226Z","shell.execute_reply":"2022-09-03T08:19:16.841832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras.backend as K","metadata":{"execution":{"iopub.status.busy":"2022-09-02T15:19:10.688736Z","iopub.execute_input":"2022-09-02T15:19:10.689065Z","iopub.status.idle":"2022-09-02T15:19:10.695761Z","shell.execute_reply.started":"2022-09-02T15:19:10.689031Z","shell.execute_reply":"2022-09-02T15:19:10.694968Z"},"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-02T15:19:10.69931Z","iopub.execute_input":"2022-09-02T15:19:10.699977Z","iopub.status.idle":"2022-09-02T15:19:10.708149Z","shell.execute_reply.started":"2022-09-02T15:19:10.699919Z","shell.execute_reply":"2022-09-02T15:19:10.707249Z"},"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-03T08:46:39.835406Z","iopub.execute_input":"2022-09-03T08:46:39.836508Z","iopub.status.idle":"2022-09-03T08:46:40.139623Z","shell.execute_reply.started":"2022-09-03T08:46:39.836442Z","shell.execute_reply":"2022-09-03T08:46:40.13859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"./\")","metadata":{"execution":{"iopub.status.busy":"2022-09-03T08:20:57.406683Z","iopub.execute_input":"2022-09-03T08:20:57.407639Z","iopub.status.idle":"2022-09-03T08:20:57.415313Z","shell.execute_reply.started":"2022-09-03T08:20:57.40759Z","shell.execute_reply":"2022-09-03T08:20:57.414015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(\"classes\")","metadata":{"execution":{"iopub.status.busy":"2022-09-03T08:22:19.090086Z","iopub.execute_input":"2022-09-03T08:22:19.090465Z","iopub.status.idle":"2022-09-03T08:22:19.095152Z","shell.execute_reply.started":"2022-09-03T08:22:19.090423Z","shell.execute_reply":"2022-09-03T08:22:19.094138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"./\")","metadata":{"execution":{"iopub.status.busy":"2022-09-03T08:22:34.169757Z","iopub.execute_input":"2022-09-03T08:22:34.170398Z","iopub.status.idle":"2022-09-03T08:22:34.178343Z","shell.execute_reply.started":"2022-09-03T08:22:34.17036Z","shell.execute_reply":"2022-09-03T08:22:34.177298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(16):\n    path = './classes/' + str(i)\n    os.mkdir(path)","metadata":{"execution":{"iopub.status.busy":"2022-09-03T08:22:43.90101Z","iopub.execute_input":"2022-09-03T08:22:43.901359Z","iopub.status.idle":"2022-09-03T08:22:43.907256Z","shell.execute_reply.started":"2022-09-03T08:22:43.901329Z","shell.execute_reply":"2022-09-03T08:22:43.906164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pip install tqdm","metadata":{"execution":{"iopub.status.busy":"2022-09-03T08:22:50.365538Z","iopub.execute_input":"2022-09-03T08:22:50.365893Z","iopub.status.idle":"2022-09-03T08:23:25.096005Z","shell.execute_reply.started":"2022-09-03T08:22:50.365862Z","shell.execute_reply":"2022-09-03T08:23:25.094706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Dividing the images as per their labels\nfrom PIL import Image\nimport numpy as np\nfor i, j in zip(labels['id'],labels['label']):\n    path = './classes/'+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-03T08:25:13.451641Z","iopub.execute_input":"2022-09-03T08:25:13.452068Z","iopub.status.idle":"2022-09-03T08:38:19.372147Z","shell.execute_reply.started":"2022-09-03T08:25:13.452029Z","shell.execute_reply":"2022-09-03T08:38:19.370971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image_dataset_from_directory\n\nds_train = image_dataset_from_directory(\n                './classes/',\n                labels = 'inferred',\n                label_mode='categorical',\n                color_mode = 'rgb',\n                batch_size = 5,\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                './classes/',\n                labels = 'inferred',\n                label_mode='categorical',\n                color_mode = 'rgb',\n                batch_size = 30,\n                image_size = (800, 600),\n                shuffle = True,\n                seed = 123,\n                validation_split = 0.1,\n                subset = 'validation',\n                \n)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-03T08:38:19.37517Z","iopub.execute_input":"2022-09-03T08:38:19.375865Z","iopub.status.idle":"2022-09-03T08:38:21.31944Z","shell.execute_reply.started":"2022-09-03T08:38:19.375821Z","shell.execute_reply":"2022-09-03T08:38:21.318332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\n\nmodel.fit(ds_train, \n          epochs = 30,\n          validation_data = ds_validation,\n          batch_size = 30,\n          callbacks = [EarlyStopping(monitor='loss', patience = 2)])\n","metadata":{"execution":{"iopub.status.busy":"2022-09-03T08:46:45.429367Z","iopub.execute_input":"2022-09-03T08:46:45.430107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_model = model.save('document_classifier')\nimport pickle\npickle.dump(my_model, './saved_model')","metadata":{"execution":{"iopub.status.busy":"2022-09-03T08:38:21.353137Z","iopub.status.idle":"2022-09-03T08:38:21.354016Z","shell.execute_reply.started":"2022-09-03T08:38:21.353718Z","shell.execute_reply":"2022-09-03T08:38:21.353747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}