{"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 tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Activation, Dense, Flatten, BatchNormalization, Conv2D, MaxPool2D, Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.metrics import categorical_crossentropy\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"id":"COj2-_dfKW3r","execution":{"iopub.status.busy":"2022-10-09T09:35:17.035696Z","iopub.execute_input":"2022-10-09T09:35:17.036162Z","iopub.status.idle":"2022-10-09T09:35:23.215649Z","shell.execute_reply.started":"2022-10-09T09:35:17.036085Z","shell.execute_reply":"2022-10-09T09:35:23.214614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport json\nimport os\nfrom tqdm import tqdm\nfrom keras.models import Sequential\nfrom keras.layers import Activation, Dropout, Flatten, Dense\nfrom keras.layers import Input, Dense, Activation, BatchNormalization, Flatten, Conv2D\nfrom keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalAveragePooling2D\nfrom keras.models import Model\nfrom keras.models import model_from_json","metadata":{"id":"VUUXRUUVKXAX","execution":{"iopub.status.busy":"2022-10-09T09:35:23.217644Z","iopub.execute_input":"2022-10-09T09:35:23.218395Z","iopub.status.idle":"2022-10-09T09:35:23.391833Z","shell.execute_reply.started":"2022-10-09T09:35:23.218347Z","shell.execute_reply":"2022-10-09T09:35:23.390708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_t = pd.read_csv(\"../input/datathonindoml-2022/train_labels.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-10-09T09:35:23.393135Z","iopub.execute_input":"2022-10-09T09:35:23.394589Z","iopub.status.idle":"2022-10-09T09:35:23.413157Z","shell.execute_reply.started":"2022-10-09T09:35:23.394549Z","shell.execute_reply":"2022-10-09T09:35:23.412300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from typing import Tuple\n\ndef resize_with_pad(image: np.array, \n                    new_shape: Tuple[int, int], \n                    padding_color: Tuple[int] = (255, 255, 255)) -> np.array:\n    original_shape = (image.shape[1], image.shape[0])\n    ratio = float(max(new_shape))/max(original_shape)\n    new_size = tuple([int(x*ratio) for x in original_shape])\n    image = cv2.resize(image, new_size)\n    delta_w = new_shape[0] - new_size[0]\n    delta_h = new_shape[1] - new_size[1]\n    top, bottom = delta_h//2, delta_h-(delta_h//2)\n    left, right = delta_w//2, delta_w-(delta_w//2)\n    image = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=padding_color)\n    return image","metadata":{"id":"O9jHTf80KXKv","execution":{"iopub.status.busy":"2022-10-09T09:35:23.416677Z","iopub.execute_input":"2022-10-09T09:35:23.416963Z","iopub.status.idle":"2022-10-09T09:35:23.426238Z","shell.execute_reply.started":"2022-10-09T09:35:23.416936Z","shell.execute_reply":"2022-10-09T09:35:23.424981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test(model_path,data_path,weight_path):\n    json_file = open(model_path, 'r')\n    loaded_model_json = json_file.read()\n    json_file.close()\n    loaded_model = model_from_json(loaded_model_json)\n    loaded_model.load_weights(weight_path)\n    loaded_model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.0005),metrics=['accuracy'])\n    X_val=[]\n    y_val=[]\n    path = data_path\n    for f in os.listdir(path):\n        y_val.append(os.path.splitext(f)[0])\n        temp2 = cv2.imread(path+f)\n        temp2 = cv2.cvtColor(temp2,cv2.COLOR_BGR2YUV)\n        temp2[:,:,0] = cv2.equalizeHist(temp2[:,:,0])\n        temp2 = cv2.cvtColor(temp2, cv2.COLOR_YUV2BGR)\n        temp2 = resize_with_pad(temp2, (256,256))\n        temp2 = cv2.cvtColor(temp2, cv2.COLOR_BGR2GRAY)\n        temp2 = np.expand_dims(temp2,axis=2)\n        X_val.append(temp2)\n    X_val = np.array(X_val)\n    X_val = X_val.astype('float32')\n    X_val/=255\n    model=loaded_model\n    y_pred=model.predict(X_val)\n    prediction=[np.argmax(element) for element in y_pred]\n    save = pd.DataFrame({'id':y_val,'label':prediction})\n    save.to_csv('submission.csv',index=False)","metadata":{"id":"r3ubzgSJkFcf","execution":{"iopub.status.busy":"2022-10-09T09:35:23.427679Z","iopub.execute_input":"2022-10-09T09:35:23.428060Z","iopub.status.idle":"2022-10-09T09:35:23.439018Z","shell.execute_reply.started":"2022-10-09T09:35:23.428023Z","shell.execute_reply":"2022-10-09T09:35:23.437965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test('../input/models/model.json',\"../input/datathonindoml-2022/validation/validation/\",\"../input/models/weights.best.hdf5\")","metadata":{"execution":{"iopub.status.busy":"2022-10-09T09:35:23.441188Z","iopub.execute_input":"2022-10-09T09:35:23.441634Z","iopub.status.idle":"2022-10-09T09:35:47.503523Z","shell.execute_reply.started":"2022-10-09T09:35:23.441595Z","shell.execute_reply":"2022-10-09T09:35:47.502538Z"},"trusted":true},"execution_count":null,"outputs":[]}]}