{"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":"markdown","source":"#### For those who want to use competition loss using keras. Note tf.keras.losses.BinaryCrossentropy(from_logits=False,reduction=tf.keras.losses.Reduction.None) is not working. \n\n#### Check out the issur here. https://github.com/tensorflow/tensorflow/issues/27190\n\n\n\n#### Reference: https://www.kaggle.com/code/yosukeyama/rsna2022-comp-metric/notebook?scriptVersionId=103365834","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nimport torch\nimport torch.nn as nn","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-13T09:34:47.817256Z","iopub.execute_input":"2022-10-13T09:34:47.817678Z","iopub.status.idle":"2022-10-13T09:34:47.823759Z","shell.execute_reply.started":"2022-10-13T09:34:47.817646Z","shell.execute_reply":"2022-10-13T09:34:47.822268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\nloss_fn = nn.BCELoss(reduction=\"none\") \n\ncompetition_weights = {\n    '-' : torch.tensor([7, 1, 1, 1, 1, 1, 1, 1], dtype=torch.float, device=device),\n    '+' : torch.tensor([14, 2, 2, 2, 2, 2, 2, 2], dtype=torch.float, device=device),\n}\n\ntrain = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train.csv')\ntargets = ['patient_overall', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7']\nlabels = torch.tensor(train[targets].values)\nmean_values = train.mean(axis=0).values\nmean_values = torch.tensor(np.vstack([mean_values]*len(train)))\n\ndef competiton_loss_row_norm(y_hat, y):\n    loss = loss_fn(y_hat, y)\n    weights = y * competition_weights['+'] + (1 - y) * competition_weights['-']\n    loss = (loss * weights).sum(axis=1)\n    w_sum = weights.sum(axis=1)\n    loss = torch.div(loss, w_sum)\n    return loss.mean()\n\nprint(\"The overall loss is\",competiton_loss_row_norm(mean_values, labels.double()))\n","metadata":{"execution":{"iopub.status.busy":"2022-10-13T09:34:47.829091Z","iopub.execute_input":"2022-10-13T09:34:47.829927Z","iopub.status.idle":"2022-10-13T09:34:47.86336Z","shell.execute_reply.started":"2022-10-13T09:34:47.829882Z","shell.execute_reply":"2022-10-13T09:34:47.862048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train.csv')\ntargets = ['patient_overall', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7']\nlabels = tf.constant(train[targets].values)\nmean_values = train.mean(axis=0).values\nmean_values = tf.constant(np.vstack([mean_values]*len(train)))\n\ncompetition_weights = {\n    '-' : tf.constant([7, 1, 1, 1, 1, 1, 1, 1],dtype= tf.float16),\n    '+' : tf.constant([14, 2, 2, 2, 2, 2, 2, 2],dtype= tf.float16),\n}\n\ndef loss_fn(pred, y): return -(tf.math.log(pred)*y + (1-y)*tf.math.log(1-pred))\n\ndef competiton_loss(y_hat, y):\n    loss = loss_fn(y_hat, y)\n    weights = y * competition_weights['+'] + (1 - y) * competition_weights['-']\n    loss = tf.reduce_sum(loss * weights,axis=1)\n    \n    w_sum = tf.reduce_sum(weights,axis=1)\n    loss = loss/w_sum\n    return tf.math.reduce_mean(loss)\n\nprint(competiton_loss(tf.cast(mean_values,tf.float16), tf.cast(labels,tf.float16)))\n","metadata":{"execution":{"iopub.status.busy":"2022-10-13T09:34:47.865681Z","iopub.execute_input":"2022-10-13T09:34:47.866428Z","iopub.status.idle":"2022-10-13T09:34:47.896527Z","shell.execute_reply.started":"2022-10-13T09:34:47.866389Z","shell.execute_reply":"2022-10-13T09:34:47.895304Z"},"trusted":true},"execution_count":null,"outputs":[]}]}