{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"},{"sourceId":8212560,"sourceType":"datasetVersion","datasetId":4867220},{"sourceId":8214224,"sourceType":"datasetVersion","datasetId":4868510},{"sourceId":207,"sourceType":"modelInstanceVersion","modelInstanceId":146}],"dockerImageVersionId":30498,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport datetime\nimport random\nimport shutil\nimport os, cv2, json\nfrom PIL import Image\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import models, layers\nfrom keras.models import Model\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau, TensorBoard\nfrom tensorflow.keras.applications import efficientnet_v2\nfrom keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import img_to_array\nfrom tensorflow.keras.preprocessing import image as kimage\nfrom tensorflow.keras.models import load_model","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-24T09:29:12.769210Z","iopub.execute_input":"2024-04-24T09:29:12.770091Z","iopub.status.idle":"2024-04-24T09:29:12.777453Z","shell.execute_reply.started":"2024-04-24T09:29:12.770055Z","shell.execute_reply":"2024-04-24T09:29:12.776442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"WORK_DIR = '../input/cassava-leaf-disease-classification/'","metadata":{"execution":{"iopub.status.busy":"2024-04-24T09:29:12.779049Z","iopub.execute_input":"2024-04-24T09:29:12.779348Z","iopub.status.idle":"2024-04-24T09:29:12.790648Z","shell.execute_reply.started":"2024-04-24T09:29:12.779325Z","shell.execute_reply":"2024-04-24T09:29:12.789896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SigmoidFocalCrossEntropy(tf.keras.losses.Loss):\n    def __init__(self, alpha=0.25, gamma=2.0, from_logits=False, **kwargs):\n        super().__init__(**kwargs)\n        self.alpha = alpha\n        self.gamma = gamma\n        self.from_logits = from_logits\n\n    def call(self, y_true, y_pred):\n        if self.from_logits:\n            y_pred = tf.sigmoid(y_pred)\n        y_pred = tf.clip_by_value(y_pred, tf.keras.backend.epsilon(), 1 - tf.keras.backend.epsilon())\n        cross_entropy = -y_true * tf.math.log(y_pred) - (1 - y_true) * tf.math.log(1 - y_pred)\n        weight = self.alpha * y_true + (1 - self.alpha) * (1 - y_true)\n        focal_loss = weight * ((1 - y_pred) ** self.gamma) * cross_entropy\n        return tf.reduce_sum(focal_loss, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T09:29:12.791587Z","iopub.execute_input":"2024-04-24T09:29:12.791839Z","iopub.status.idle":"2024-04-24T09:29:12.802368Z","shell.execute_reply.started":"2024-04-24T09:29:12.791817Z","shell.execute_reply":"2024-04-24T09:29:12.801584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"custom_objects = {\"SigmoidFocalCrossEntropy\": SigmoidFocalCrossEntropy}\n\nmodel = load_model('/kaggle/input/modeltest/my_model.h5', custom_objects=custom_objects)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T09:29:12.804030Z","iopub.execute_input":"2024-04-24T09:29:12.804319Z","iopub.status.idle":"2024-04-24T09:29:21.153871Z","shell.execute_reply.started":"2024-04-24T09:29:12.804296Z","shell.execute_reply":"2024-04-24T09:29:21.152793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(columns=['image_id','label'])\nfor image_name in os.listdir(WORK_DIR + 'test_images'):\n    image_path = os.path.join(WORK_DIR + 'test_images', image_name)\n    image = tf.keras.preprocessing.image.load_img(image_path)\n    resized_image = image.resize((224, 224))\n    numpied_image = np.expand_dims(resized_image, 0)\n    tensored_image = tf.cast(numpied_image, tf.float32)\n    y_pred = model.predict(tensored_image)\n    y_pred = np.argmax(y_pred, axis=-1)[0]\n    submission.loc[len(submission)] = [image_name, int(y_pred)]\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T09:29:21.155920Z","iopub.execute_input":"2024-04-24T09:29:21.156232Z","iopub.status.idle":"2024-04-24T09:29:24.646465Z","shell.execute_reply.started":"2024-04-24T09:29:21.156206Z","shell.execute_reply":"2024-04-24T09:29:24.645583Z"},"trusted":true},"execution_count":null,"outputs":[]}]}