{"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":"!pip install -qq /kaggle/input/keras-cv-attention-models/keras_cv_attention_models-1.3.9-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:08:16.675033Z","iopub.execute_input":"2023-03-23T01:08:16.675529Z","iopub.status.idle":"2023-03-23T01:08:46.3631Z","shell.execute_reply.started":"2023-03-23T01:08:16.675483Z","shell.execute_reply":"2023-03-23T01:08:46.361444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport tensorflow_addons as tfa\nimport matplotlib.pyplot as plt\nimport matplotlib as mpl\n\nfrom tqdm.notebook import tqdm\nfrom multiprocessing import cpu_count\nfrom kaggle_datasets import KaggleDatasets\nfrom sklearn.model_selection import train_test_split\nfrom keras_cv_attention_models import convnext\n\nimport os\nimport time\nimport pickle\nimport math\nimport random\nimport sys\nimport cv2\nimport gc\nimport datetime\nimport seaborn as sns\n\nprint(f'Tensorflow Version: {tf.__version__}')\nprint(f'Python Version: {sys.version}')","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:08:46.36653Z","iopub.execute_input":"2023-03-23T01:08:46.367065Z","iopub.status.idle":"2023-03-23T01:08:56.192915Z","shell.execute_reply.started":"2023-03-23T01:08:46.367004Z","shell.execute_reply":"2023-03-23T01:08:56.191574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"now = datetime.datetime.now().strftime(\"%d-%b-%Y %H-%M-%S\")\nnp.save(now, np.array([now]))","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:08:56.194392Z","iopub.execute_input":"2023-03-23T01:08:56.195098Z","iopub.status.idle":"2023-03-23T01:08:56.201575Z","shell.execute_reply.started":"2023-03-23T01:08:56.195057Z","shell.execute_reply":"2023-03-23T01:08:56.199913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"policy = tf.keras.mixed_precision.Policy('float32')\ntf.keras.mixed_precision.set_global_policy(policy)\n\nprint(f'Compute dtype: {tf.keras.mixed_precision.global_policy().compute_dtype}')\nprint(f'Variable dtype: {tf.keras.mixed_precision.global_policy().variable_dtype}')","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:08:56.204926Z","iopub.execute_input":"2023-03-23T01:08:56.205315Z","iopub.status.idle":"2023-03-23T01:08:56.224107Z","shell.execute_reply.started":"2023-03-23T01:08:56.205275Z","shell.execute_reply":"2023-03-23T01:08:56.223197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mpl.rcParams.update(mpl.rcParamsDefault)\nmpl.rcParams['xtick.labelsize'] = 16\nmpl.rcParams['ytick.labelsize'] = 16\nmpl.rcParams['axes.labelsize'] = 18\nmpl.rcParams['axes.titlesize'] = 24","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:08:56.226417Z","iopub.execute_input":"2023-03-23T01:08:56.227261Z","iopub.status.idle":"2023-03-23T01:08:56.236468Z","shell.execute_reply.started":"2023-03-23T01:08:56.227211Z","shell.execute_reply":"2023-03-23T01:08:56.235422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    TPU = tf.distribute.cluster_resolver.TPUClusterResolver()  # TPU detection. No parameters necessary if TPU_NAME environment variable is set. On Kaggle this is always the case.\n    print('Running on TPU ', TPU.master())\nexcept ValueError:\n    print('Running on GPU')\n    TPU = None\n\nif TPU:\n    IS_TPU = True\n    tf.config.experimental_connect_to_cluster(TPU)\n    tf.tpu.experimental.initialize_tpu_system(TPU)\n    STRATEGY = tf.distribute.experimental.TPUStrategy(TPU)\nelse:\n    IS_TPU = False\n    STRATEGY = tf.distribute.get_strategy() # default distribution strategy in Tensorflow. Works on CPU and single GPU.\n\nN_REPLICAS = STRATEGY.num_replicas_in_sync\nprint(f'N_REPLICAS: {N_REPLICAS}, IS_TPU: {IS_TPU}')","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:08:56.237815Z","iopub.execute_input":"2023-03-23T01:08:56.238627Z","iopub.status.idle":"2023-03-23T01:09:00.92905Z","shell.execute_reply.started":"2023-03-23T01:08:56.238584Z","shell.execute_reply":"2023-03-23T01:09:00.927736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"GCS_DS_PATH = KaggleDatasets().get_gcs_path('rsna-preprocessing-tfrecords-640x512-dataset-pub')","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:00.930513Z","iopub.execute_input":"2023-03-23T01:09:00.931713Z","iopub.status.idle":"2023-03-23T01:09:21.517997Z","shell.execute_reply.started":"2023-03-23T01:09:00.931668Z","shell.execute_reply":"2023-03-23T01:09:21.516709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SEED = 43\nDEBUG = False\n\nIMG_HEIGHT = 1344\nIMG_WIDTH = 768\nN_CHANNELS = 1\nINPUT_SHAPE = (IMG_HEIGHT, IMG_WIDTH, 1)\nN_SAMPLES_TFRECORDS = 548\n\nLR_MAX = 5e-6 * N_REPLICAS\nWD_RATIO = 0.01\n\nN_WARMUP_EPOCHS = 5\nN_EPOCHS = 15\n\nBATCH_SIZE = 8 * N_REPLICAS\n\nIS_INTERACTIVE = os.environ['KAGGLE_KERNEL_RUN_TYPE'] == 'Interactive'\nVERBOSE = 1 if IS_INTERACTIVE else 2\n\nAUTO = tf.data.experimental.AUTOTUNE\n\nprint(f'BATCH_SIZE: {BATCH_SIZE}')","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:21.519577Z","iopub.execute_input":"2023-03-23T01:09:21.520049Z","iopub.status.idle":"2023-03-23T01:09:21.529675Z","shell.execute_reply.started":"2023-03-23T01:09:21.519999Z","shell.execute_reply":"2023-03-23T01:09:21.528344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seed_everything(seed=SEED):\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    random.seed(seed)\n    np.random.seed(seed)\n    tf.random.set_seed(seed)\n\nseed_everything()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:21.531422Z","iopub.execute_input":"2023-03-23T01:09:21.53176Z","iopub.status.idle":"2023-03-23T01:09:21.563727Z","shell.execute_reply.started":"2023-03-23T01:09:21.531728Z","shell.execute_reply":"2023-03-23T01:09:21.562578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\n\ndisplay(train.head())\ndisplay(train.info())","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:21.57053Z","iopub.execute_input":"2023-03-23T01:09:21.571058Z","iopub.status.idle":"2023-03-23T01:09:21.765209Z","shell.execute_reply.started":"2023-03-23T01:09:21.571003Z","shell.execute_reply":"2023-03-23T01:09:21.763831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(data=train[\"cancer\"])","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:21.766641Z","iopub.execute_input":"2023-03-23T01:09:21.767095Z","iopub.status.idle":"2023-03-23T01:09:22.387091Z","shell.execute_reply.started":"2023-03-23T01:09:21.767059Z","shell.execute_reply":"2023-03-23T01:09:22.386187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tf_rand_int(minval, maxval, dtype=tf.int64):\n    minval = tf.cast(minval, dtype)\n    maxval = tf.cast(maxval, dtype)\n    return tf.random.uniform(shape=(), minval=minval, maxval=maxval, dtype=dtype)","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:22.388542Z","iopub.execute_input":"2023-03-23T01:09:22.389132Z","iopub.status.idle":"2023-03-23T01:09:22.396526Z","shell.execute_reply.started":"2023-03-23T01:09:22.389077Z","shell.execute_reply":"2023-03-23T01:09:22.39507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def benchmark_dataset(dataset, num_epochs=3, n_steps_per_epoch=10, bs=BATCH_SIZE):\n    start_time = time.perf_counter()\n    for epoch_num in range(num_epochs):\n        for idx, (inputs, labels) in enumerate(dataset.take(n_steps_per_epoch + 1)):\n            if idx == 0:\n                epoch_start = time.perf_counter()\n            elif idx == 1 and epoch_num == 0:\n                image = inputs['image']\n                print(f'image shape: {image.shape}, labels shape: {labels.shape}, image dtype: {image.dtype}, labels dtype: {labels.dtype}')\n            else:\n                pass\n        \n        epoch_t = time.perf_counter() - epoch_start\n        mean_step_t = round(epoch_t / n_steps_per_epoch * 1000, 1)\n        n_imgs_per_s = int(1 / (mean_step_t / 1000) * bs)\n        print(f'epoch {epoch_num} took: {round(epoch_t, 2)} sec, mean step duration: {mean_step_t}ms, images/s: {n_imgs_per_s}')","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:22.397822Z","iopub.execute_input":"2023-03-23T01:09:22.399342Z","iopub.status.idle":"2023-03-23T01:09:22.414289Z","shell.execute_reply.started":"2023-03-23T01:09:22.399293Z","shell.execute_reply":"2023-03-23T01:09:22.412929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_batch(dataset, n_rows=16, n_cols=4):\n    inputs, targets = next(iter(dataset))\n    images = inputs['image'].numpy().squeeze()\n    fig, axes = plt.subplots(nrows=n_rows, ncols=n_cols, figsize=(n_cols*4, n_rows*7))\n    for r in range(n_rows):\n        for c in range(n_cols):\n            idx = r * n_cols + c\n            # Image\n            img = images[idx]\n            axes[r, c].imshow(img)\n            # Target\n            target = targets[idx]\n            axes[r, c].set_title(f'target: {target}', fontsize=16, pad=5)\n        \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:22.415975Z","iopub.execute_input":"2023-03-23T01:09:22.416686Z","iopub.status.idle":"2023-03-23T01:09:22.426747Z","shell.execute_reply.started":"2023-03-23T01:09:22.41664Z","shell.execute_reply":"2023-03-23T01:09:22.425436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_image(record_bytes):\n    features = tf.io.parse_single_example(record_bytes, {\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'target': tf.io.FixedLenFeature([], tf.int64),\n        'patient_id': tf.io.FixedLenFeature([], tf.int64),\n    })\n    image = img = tf.image.decode_jpeg(features['image'], channels=N_CHANNELS, dct_method='INTEGER_ACCURATE')\n    image = tf.reshape(image, [IMG_HEIGHT, IMG_WIDTH, N_CHANNELS])\n\n    target = features['target']\n    \n    return { 'image': image }, target","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:22.428483Z","iopub.execute_input":"2023-03-23T01:09:22.429364Z","iopub.status.idle":"2023-03-23T01:09:22.438851Z","shell.execute_reply.started":"2023-03-23T01:09:22.429186Z","shell.execute_reply":"2023-03-23T01:09:22.437654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def augment_image(X, y):\n    image = X['image']\n    \n    image = tf.image.random_brightness(image, 0.10)\n    \n    image = tf.image.random_contrast(image, 0.90, 1.10)\n    \n    image = tf.image.random_jpeg_quality(image, 75, 100)\n    \n    ratio = tf.random.uniform([], 0.75, 1.00)\n    img_height_crop = tf.cast(ratio * IMG_HEIGHT, tf.int32)\n    img_width_crop = tf.cast(ratio * IMG_WIDTH, tf.int32)\n    img_height_offset = tf_rand_int(0, IMG_HEIGHT - img_height_crop)\n    img_width_offset = 0\n    image = tf.slice(image, [img_height_offset, img_width_offset, 0], [img_height_crop, img_width_crop, N_CHANNELS])\n    image = tf.image.resize(image, [IMG_HEIGHT, IMG_WIDTH], method=tf.image.ResizeMethod.BILINEAR)\n    image = tf.clip_by_value(image, 0, 255)\n    image = tf.cast(image, tf.uint8)\n    \n    return { 'image': image }, y","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:22.440678Z","iopub.execute_input":"2023-03-23T01:09:22.441069Z","iopub.status.idle":"2023-03-23T01:09:22.456356Z","shell.execute_reply.started":"2023-03-23T01:09:22.441033Z","shell.execute_reply":"2023-03-23T01:09:22.455011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def undersample_majority(X, y):\n    return y == 1 or tf.random.uniform([]) > 0.66","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:22.457937Z","iopub.execute_input":"2023-03-23T01:09:22.459153Z","iopub.status.idle":"2023-03-23T01:09:22.467159Z","shell.execute_reply.started":"2023-03-23T01:09:22.459077Z","shell.execute_reply":"2023-03-23T01:09:22.465868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TFRECORDS_FILE_PATHS = sorted(tf.io.gfile.glob(f'{GCS_DS_PATH}/*.tfrecords'))\nprint(f'Found {len(TFRECORDS_FILE_PATHS)} TFRecords')","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:22.468679Z","iopub.execute_input":"2023-03-23T01:09:22.469821Z","iopub.status.idle":"2023-03-23T01:09:22.629054Z","shell.execute_reply.started":"2023-03-23T01:09:22.469781Z","shell.execute_reply":"2023-03-23T01:09:22.627667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TFRECORDS_TRAIN, TFRECORDS_VAL = train_test_split(TFRECORDS_FILE_PATHS, train_size=0.80, random_state=SEED, shuffle=True)\nprint(f'# TFRECORDS_TRAIN: {len(TFRECORDS_TRAIN)}, # TFRECORDS_VAL: {len(TFRECORDS_VAL)}')","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:22.63066Z","iopub.execute_input":"2023-03-23T01:09:22.631974Z","iopub.status.idle":"2023-03-23T01:09:22.643995Z","shell.execute_reply.started":"2023-03-23T01:09:22.631913Z","shell.execute_reply":"2023-03-23T01:09:22.642158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_dataset(tfrecords, bs=BATCH_SIZE, val=False, debug=True):\n    ignore_order = tf.data.Options()\n    ignore_order.experimental_deterministic = False\n    \n    dataset = tf.data.TFRecordDataset(tfrecords, num_parallel_reads=AUTO, compression_type='GZIP')\n    \n    dataset = dataset.map(decode_image, num_parallel_calls=AUTO)\n\n    if not val:\n        dataset = dataset.filter(undersample_majority)\n        dataset = dataset.map(augment_image, num_parallel_calls=AUTO)\n        dataset = dataset.with_options(ignore_order)\n        if not debug:\n            dataset = dataset.shuffle(1024)\n        dataset = dataset.repeat()        \n\n    dataset = dataset.batch(bs, drop_remainder=not val)\n    dataset = dataset.prefetch(AUTO)\n    \n    return dataset","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:22.645828Z","iopub.execute_input":"2023-03-23T01:09:22.647011Z","iopub.status.idle":"2023-03-23T01:09:22.656135Z","shell.execute_reply.started":"2023-03-23T01:09:22.646955Z","shell.execute_reply":"2023-03-23T01:09:22.655211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = get_dataset(TFRECORDS_TRAIN, val=False, debug=False)\nval_dataset = get_dataset(TFRECORDS_VAL, val=True, debug=False)\n\nTRAIN_STEPS_PER_EPOCH = len(TFRECORDS_TRAIN) * N_SAMPLES_TFRECORDS // BATCH_SIZE\nVAL_STEPS_PER_EPOCH = len(TFRECORDS_VAL) * N_SAMPLES_TFRECORDS // BATCH_SIZE\nprint(f'TRAIN_STEPS_PER_EPOCH: {TRAIN_STEPS_PER_EPOCH}, VAL_STEPS_PER_EPOCH: {VAL_STEPS_PER_EPOCH}')","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:22.657773Z","iopub.execute_input":"2023-03-23T01:09:22.658165Z","iopub.status.idle":"2023-03-23T01:09:23.137708Z","shell.execute_reply.started":"2023-03-23T01:09:22.6581Z","shell.execute_reply":"2023-03-23T01:09:23.136337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_batch, y_batch = next(iter(get_dataset(TFRECORDS_TRAIN, val=False)))\nimage = X_batch['image'].numpy()\nprint(f'image shape: {image.shape}, y_batch shape: {y_batch.shape}')\nprint(f'image dtype: {image.dtype}, y_batch dtype: {y_batch.dtype}')\nprint(f'image min: {image.min():.2f}, max: {image.max():.2f}')","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:23.13928Z","iopub.execute_input":"2023-03-23T01:09:23.140359Z","iopub.status.idle":"2023-03-23T01:09:32.863091Z","shell.execute_reply.started":"2023-03-23T01:09:23.140317Z","shell.execute_reply":"2023-03-23T01:09:32.861522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"benchmark_dataset(get_dataset(TFRECORDS_TRAIN, val=False))","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:32.86509Z","iopub.execute_input":"2023-03-23T01:09:32.865563Z","iopub.status.idle":"2023-03-23T01:09:40.742624Z","shell.execute_reply.started":"2023-03-23T01:09:32.865503Z","shell.execute_reply":"2023-03-23T01:09:40.741073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_batch(get_dataset(TFRECORDS_TRAIN, bs=64, val=False))","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:09:40.744465Z","iopub.execute_input":"2023-03-23T01:09:40.745324Z","iopub.status.idle":"2023-03-23T01:10:02.236284Z","shell.execute_reply.started":"2023-03-23T01:09:40.74528Z","shell.execute_reply":"2023-03-23T01:10:02.232908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = 128\ntrain_labels = []\nfor _, labels in tqdm(get_dataset(TFRECORDS_TRAIN, val=False).take(N), total=N):\n    train_labels += labels.numpy().tolist()\n    \ndisplay(pd.concat((\n        pd.Series(train_labels).value_counts(normalize=True).to_frame('Train Label Ratio'),\n        pd.Series(train_labels).value_counts().to_frame('Train Label Count'),\n    ), axis=1)\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:10:02.238017Z","iopub.execute_input":"2023-03-23T01:10:02.238651Z","iopub.status.idle":"2023-03-23T01:10:17.092996Z","shell.execute_reply.started":"2023-03-23T01:10:02.238603Z","shell.execute_reply":"2023-03-23T01:10:17.091583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_labels = []\nfor _, labels in tqdm(get_dataset(TFRECORDS_VAL, val=True), total=VAL_STEPS_PER_EPOCH):\n    val_labels += labels.numpy().tolist()\n    \ndisplay(pd.concat((\n        pd.Series(val_labels).value_counts(normalize=True).to_frame('Val Label Ratio'),\n        pd.Series(val_labels).value_counts().to_frame('Val Label Count'),\n    ), axis=1)\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:10:17.0946Z","iopub.execute_input":"2023-03-23T01:10:17.094941Z","iopub.status.idle":"2023-03-23T01:10:27.168404Z","shell.execute_reply.started":"2023-03-23T01:10:17.094907Z","shell.execute_reply":"2023-03-23T01:10:27.167166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class pF1(tf.keras.metrics.Metric):\n    def __init__(self, name='pF1', **kwargs):\n        super(pF1, self).__init__(name=name, **kwargs)\n        self.tc = self.add_weight(name='tc', initializer='zeros')\n        self.tp = self.add_weight(name='tp', initializer='zeros')\n        self.fp = self.add_weight(name='fp', initializer='zeros')\n\n    def update_state(self, y_true, y_pred, sample_weight=None):\n        self.tc.assign_add(tf.cast(tf.reduce_sum(y_true), tf.float32))\n        self.tp.assign_add(tf.cast(tf.reduce_sum((y_pred[y_true == 1])), tf.float32))\n        self.fp.assign_add(tf.cast(tf.reduce_sum((y_pred[y_true == 0])), tf.float32))\n\n    def result(self):\n        if self.tc == 0 or (self.tp + self.fp) == 0:\n            return 0.0\n        else:\n            precision = self.tp / (self.tp + self.fp)\n            recall = self.tp / (self.tc)\n            return 2 * (precision * recall) / (precision + recall)\n\n    def reset_state(self):\n        self.tc.assign(0)\n        self.tp.assign(0)\n        self.fp.assign(0)","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:10:27.17009Z","iopub.execute_input":"2023-03-23T01:10:27.170504Z","iopub.status.idle":"2023-03-23T01:10:27.182071Z","shell.execute_reply.started":"2023-03-23T01:10:27.170467Z","shell.execute_reply":"2023-03-23T01:10:27.181181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize(image):\n    image = tf.repeat(image, repeats=3, axis=3)\n    image = tf.cast(image, tf.float32)\n    image = tf.keras.applications.imagenet_utils.preprocess_input(image, mode='torch')\n\n    return image","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:10:27.189168Z","iopub.execute_input":"2023-03-23T01:10:27.189558Z","iopub.status.idle":"2023-03-23T01:10:27.199215Z","shell.execute_reply.started":"2023-03-23T01:10:27.189522Z","shell.execute_reply":"2023-03-23T01:10:27.19765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model():\n    print(f'Compute dtype: {tf.keras.mixed_precision.global_policy().compute_dtype}')\n    print(f'Variable dtype: {tf.keras.mixed_precision.global_policy().variable_dtype}')\n    \n    with STRATEGY.scope():\n        seed_everything()\n        \n        image = tf.keras.layers.Input(INPUT_SHAPE, name='image', dtype=tf.uint8)\n        \n        image_norm = normalize(image)\n        \n        image_norm = normalize(image)\n\n        x = convnext.ConvNeXtV2Tiny(\n            input_shape=(IMG_HEIGHT, IMG_WIDTH, 3),\n            pretrained='imagenet21k-ft1k',\n            num_classes=0,\n        )(image_norm)\n        \n        x = tf.keras.layers.GlobalAveragePooling2D()(x)\n        x = tf.keras.layers.Dropout(0.30)(x)\n        outputs = tf.keras.layers.Dense(1, activation='sigmoid')(x)\n\n        optimizer = tfa.optimizers.AdamW(learning_rate=LR_MAX, weight_decay=LR_MAX*WD_RATIO, epsilon=1e-6)\n\n        loss = tf.keras.losses.BinaryCrossentropy(from_logits=False)\n        \n        metrics = [\n            pF1(),\n            tfa.metrics.F1Score(num_classes=1, threshold=0.50),\n            tf.keras.metrics.Precision(),\n            tf.keras.metrics.Recall(),\n            tf.keras.metrics.AUC(),\n            tf.keras.metrics.BinaryAccuracy(),\n        ]\n\n        model = tf.keras.models.Model(inputs=image, outputs=outputs)\n        \n        model.compile(optimizer=optimizer, loss=loss, metrics=metrics)\n\n        return model","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:10:27.202409Z","iopub.execute_input":"2023-03-23T01:10:27.202795Z","iopub.status.idle":"2023-03-23T01:10:27.215988Z","shell.execute_reply.started":"2023-03-23T01:10:27.202758Z","shell.execute_reply":"2023-03-23T01:10:27.214724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.backend.clear_session()\ntf.config.optimizer.set_jit(True)\n\nmodel = get_model()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:10:27.218417Z","iopub.execute_input":"2023-03-23T01:10:27.218754Z","iopub.status.idle":"2023-03-23T01:10:45.857436Z","shell.execute_reply.started":"2023-03-23T01:10:27.21872Z","shell.execute_reply":"2023-03-23T01:10:45.8564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:10:45.858799Z","iopub.execute_input":"2023-03-23T01:10:45.859507Z","iopub.status.idle":"2023-03-23T01:10:45.932581Z","shell.execute_reply.started":"2023-03-23T01:10:45.859466Z","shell.execute_reply":"2023-03-23T01:10:45.928638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model, show_shapes=True, show_dtype=True, show_layer_names=True, expand_nested=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:10:45.934002Z","iopub.execute_input":"2023-03-23T01:10:45.9344Z","iopub.status.idle":"2023-03-23T01:10:46.544181Z","shell.execute_reply.started":"2023-03-23T01:10:45.934364Z","shell.execute_reply":"2023-03-23T01:10:46.542412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = model.evaluate(\n        get_dataset(TFRECORDS_VAL, val=True),\n        verbose=VERBOSE,\n        steps=VAL_STEPS_PER_EPOCH,\n    )\n","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:10:46.546244Z","iopub.execute_input":"2023-03-23T01:10:46.546638Z","iopub.status.idle":"2023-03-23T01:11:50.201918Z","shell.execute_reply.started":"2023-03-23T01:10:46.546598Z","shell.execute_reply":"2023-03-23T01:11:50.200142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_preds = model.predict(\n        get_dataset(TFRECORDS_TRAIN, val=True),\n        verbose=VERBOSE,\n        steps=128,\n    ).squeeze()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:11:50.205885Z","iopub.execute_input":"2023-03-23T01:11:50.207288Z","iopub.status.idle":"2023-03-23T01:12:39.10377Z","shell.execute_reply.started":"2023-03-23T01:11:50.207213Z","shell.execute_reply":"2023-03-23T01:12:39.102401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(pd.Series(train_preds).describe().to_frame('Value').round(2))","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:12:39.106275Z","iopub.execute_input":"2023-03-23T01:12:39.1068Z","iopub.status.idle":"2023-03-23T01:12:39.128647Z","shell.execute_reply.started":"2023-03-23T01:12:39.106757Z","shell.execute_reply":"2023-03-23T01:12:39.127455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,8))\nplt.title(f'Training Predictions Initialized Model')\npd.Series(train_preds).plot(kind='hist', bins=32)\nplt.xticks(np.arange(0, 1.1, 0.1))\nplt.xlim(0, 1)\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:12:39.130298Z","iopub.execute_input":"2023-03-23T01:12:39.131185Z","iopub.status.idle":"2023-03-23T01:12:39.541117Z","shell.execute_reply.started":"2023-03-23T01:12:39.131136Z","shell.execute_reply":"2023-03-23T01:12:39.539687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def lrfn(current_step, num_warmup_steps, lr_max, num_cycles=0.50, num_training_steps=N_EPOCHS):\n    \n    if current_step < num_warmup_steps:\n        return lr_max * 0.10 ** (num_warmup_steps - current_step)\n    else:\n        progress = float(current_step - num_warmup_steps) / float(max(1, num_training_steps - num_warmup_steps))\n\n        return max(0.0, 0.5 * (1.0 + math.cos(math.pi * float(num_cycles) * 2.0 * progress))) * lr_max","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:12:39.54288Z","iopub.execute_input":"2023-03-23T01:12:39.543285Z","iopub.status.idle":"2023-03-23T01:12:39.552001Z","shell.execute_reply.started":"2023-03-23T01:12:39.543245Z","shell.execute_reply":"2023-03-23T01:12:39.550502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_lr_schedule(lr_schedule, epochs):\n    fig = plt.figure(figsize=(20, 10))\n    plt.plot([None] + lr_schedule + [None])\n    x = np.arange(1, epochs + 1)\n    x_axis_labels = [i if epochs <= 40 or i % 5 == 0 or i == 1 else None for i in range(1, epochs + 1)]\n    plt.xlim([1, epochs])\n    plt.xticks(x, x_axis_labels) # set tick step to 1 and let x axis start at 1\n    \n    plt.ylim([0, max(lr_schedule) * 1.1])\n\n    schedule_info = f'start: {lr_schedule[0]:.1E}, max: {max(lr_schedule):.1E}, final: {lr_schedule[-1]:.1E}'\n    plt.title(f'Step Learning Rate Schedule, {schedule_info}', size=18, pad=12)\n\n    for x, val in enumerate(lr_schedule):\n        if epochs <= 40 or x % 5 == 0 or x is epochs - 1:\n            if x < len(lr_schedule) - 1:\n                if lr_schedule[x - 1] < val:\n                    ha = 'right'\n                else:\n                    ha = 'left'\n            elif x == 0:\n                ha = 'right'\n            else:\n                ha = 'left'\n            plt.plot(x + 1, val, 'o', color='black');\n            offset_y = (max(lr_schedule) - min(lr_schedule)) * 0.02\n            plt.annotate(f'{val:.1E}', xy=(x + 1, val + offset_y), size=12, ha=ha)\n    \n    plt.xlabel('Epoch', size=16, labelpad=5)\n    plt.ylabel('Learning Rate', size=16, labelpad=5)\n    plt.grid()\n    plt.show()\n\nLR_SCHEDULE = [lrfn(step, num_warmup_steps=N_WARMUP_EPOCHS, lr_max=LR_MAX, num_cycles=0.50) for step in range(N_EPOCHS)]\nplot_lr_schedule(LR_SCHEDULE, epochs=N_EPOCHS)","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:12:39.553844Z","iopub.execute_input":"2023-03-23T01:12:39.554304Z","iopub.status.idle":"2023-03-23T01:12:40.051484Z","shell.execute_reply.started":"2023-03-23T01:12:39.554259Z","shell.execute_reply":"2023-03-23T01:12:40.05018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr_callback = tf.keras.callbacks.LearningRateScheduler(lambda step: LR_SCHEDULE[step], verbose=0)","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:12:40.053557Z","iopub.execute_input":"2023-03-23T01:12:40.054037Z","iopub.status.idle":"2023-03-23T01:12:40.059982Z","shell.execute_reply.started":"2023-03-23T01:12:40.053984Z","shell.execute_reply":"2023-03-23T01:12:40.058571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class WeightDecayCallback(tf.keras.callbacks.Callback):\n    def __init__(self, wd_ratio=WD_RATIO):\n        self.step_counter = 0\n        self.wd_ratio = wd_ratio\n    \n    def on_epoch_begin(self, epoch, logs=None):\n        model.optimizer.weight_decay = model.optimizer.learning_rate * self.wd_ratio\n        print(f'learning rate: {model.optimizer.learning_rate.numpy():.2e}, weight decay: {model.optimizer.weight_decay.numpy():.2e}')","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:12:40.06141Z","iopub.execute_input":"2023-03-23T01:12:40.062594Z","iopub.status.idle":"2023-03-23T01:12:40.073105Z","shell.execute_reply.started":"2023-03-23T01:12:40.062546Z","shell.execute_reply":"2023-03-23T01:12:40.072069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n        train_dataset,\n        steps_per_epoch = TRAIN_STEPS_PER_EPOCH,\n        validation_data = val_dataset,\n        epochs = N_EPOCHS,\n        verbose = VERBOSE,\n        callbacks = [\n            lr_callback,\n            WeightDecayCallback(),\n        ],\n        class_weight = {\n            0: 1.0,\n            1: 5.0,\n        },\n    )","metadata":{"execution":{"iopub.status.busy":"2023-03-23T01:12:40.074978Z","iopub.execute_input":"2023-03-23T01:12:40.075746Z","iopub.status.idle":"2023-03-23T02:54:18.276726Z","shell.execute_reply.started":"2023-03-23T01:12:40.075706Z","shell.execute_reply":"2023-03-23T02:54:18.275328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights('model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:54:18.27915Z","iopub.execute_input":"2023-03-23T02:54:18.279642Z","iopub.status.idle":"2023-03-23T02:54:19.567519Z","shell.execute_reply.started":"2023-03-23T02:54:18.279588Z","shell.execute_reply":"2023-03-23T02:54:19.566421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true_val = []\ny_pred_val = []\nfor X_batch, y_batch in tqdm(get_dataset(TFRECORDS_VAL, val=True), total=VAL_STEPS_PER_EPOCH):\n    y_true_val += y_batch.numpy().tolist()\n    y_pred_val += model.predict_on_batch(X_batch).squeeze().tolist()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:54:19.569332Z","iopub.execute_input":"2023-03-23T02:54:19.569807Z","iopub.status.idle":"2023-03-23T02:57:05.624833Z","shell.execute_reply.started":"2023-03-23T02:54:19.569756Z","shell.execute_reply":"2023-03-23T02:57:05.623292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pfbeta(labels, predictions, beta=1):\n    y_true_count = 0\n    ctp = 0\n    cfp = 0\n\n    for idx in range(len(labels)):\n        prediction = min(max(predictions[idx], 0), 1)\n        if (labels[idx]):\n            y_true_count += 1\n            ctp += prediction\n        else:\n            cfp += prediction\n\n    beta_squared = beta * beta\n    c_precision = ctp / (ctp + cfp)\n    c_recall = ctp / y_true_count\n    if (c_precision > 0 and c_recall > 0):\n        result = (1 + beta_squared) * (c_precision * c_recall) / (beta_squared * c_precision + c_recall)\n        return result\n    else:\n        return 0","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:57:05.62678Z","iopub.execute_input":"2023-03-23T02:57:05.627152Z","iopub.status.idle":"2023-03-23T02:57:05.636955Z","shell.execute_reply.started":"2023-03-23T02:57:05.627103Z","shell.execute_reply":"2023-03-23T02:57:05.635421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pf1_by_threshold = []\nthresholds = np.arange(0, 1.01, 0.01)\nfor t in tqdm(thresholds):\n    pf1_by_threshold.append(\n        pfbeta(y_true_val, y_pred_val > t)\n    )\n    \nplt.figure(figsize=(15,8))\nplt.title('F1 By Threshold', size=24)\nplt.plot(pf1_by_threshold, label='F1 Score')\n\narg_max = np.argmax(pf1_by_threshold)\nval_max = np.max(pf1_by_threshold)\nthreshold_best = thresholds[arg_max]\nplt.scatter(arg_max, val_max, color='red', label=f'Best Threshold {threshold_best:.2f}, pF1 Score: {val_max:.2f}')\n\nplt.xticks(np.arange(0, 110, 10), [f'{t:.2f}' for t in np.arange(0, 1.1, 0.1)])\nplt.yticks(np.arange(0, 1.1, 0.1))\nplt.xlim(0, 100)\nplt.ylim(0, 1)\nplt.xlabel('Threshold')\nplt.ylabel('pF1 Score')\nplt.legend(fontsize=12)\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:57:05.638981Z","iopub.execute_input":"2023-03-23T02:57:05.640346Z","iopub.status.idle":"2023-03-23T02:57:12.676755Z","shell.execute_reply.started":"2023-03-23T02:57:05.640283Z","shell.execute_reply":"2023-03-23T02:57:12.675164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_history_metric(metric, f_best=np.argmax, ylim=None, yscale=None, yticks=None):\n    plt.figure(figsize=(20, 10))\n    \n    values = history.history[metric]\n    N_EPOCHS = len(values)\n    val = 'val' in ''.join(history.history.keys())\n    if N_EPOCHS <= 20:\n        x = np.arange(1, N_EPOCHS + 1)\n    else:\n        x = [1, 5] + [10 + 5 * idx for idx in range((N_EPOCHS - 10) // 5 + 1)]\n\n    x_ticks = np.arange(1, N_EPOCHS+1)\n\n    if val:\n        val_values = history.history[f'val_{metric}']\n        val_argmin = f_best(val_values)\n        plt.plot(x_ticks, val_values, label=f'val')\n\n    plt.plot(x_ticks, values, label=f'train')\n    argmin = f_best(values)\n    plt.scatter(argmin + 1, values[argmin], color='red', s=75, marker='o', label=f'train_best')\n    if val:\n        plt.scatter(val_argmin + 1, val_values[val_argmin], color='purple', s=75, marker='o', label=f'val_best')\n\n    plt.title(f'Model {metric}', fontsize=24, pad=10)\n    plt.ylabel(metric, fontsize=20, labelpad=10)\n\n    if ylim:\n        plt.ylim(ylim)\n\n    if yscale is not None:\n        plt.yscale(yscale)\n        \n    if yticks is not None:\n        plt.yticks(yticks, fontsize=16)\n\n    plt.xlabel('epoch', fontsize=20, labelpad=10)        \n    plt.tick_params(axis='x', labelsize=8)\n    plt.xticks(x, fontsize=16) # set tick step to 1 and let x axis start at 1\n    plt.yticks(fontsize=16)\n    \n    plt.legend(prop={'size': 10})\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:57:12.678474Z","iopub.execute_input":"2023-03-23T02:57:12.679485Z","iopub.status.idle":"2023-03-23T02:57:12.695684Z","shell.execute_reply.started":"2023-03-23T02:57:12.679428Z","shell.execute_reply":"2023-03-23T02:57:12.694226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history_metric('loss', f_best=np.argmin)","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:57:12.697554Z","iopub.execute_input":"2023-03-23T02:57:12.697946Z","iopub.status.idle":"2023-03-23T02:57:13.172105Z","shell.execute_reply.started":"2023-03-23T02:57:12.697906Z","shell.execute_reply":"2023-03-23T02:57:13.170662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history_metric('pF1', ylim=[0,1], yticks=np.arange(0.0, 1.1, 0.1))","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:57:13.173722Z","iopub.execute_input":"2023-03-23T02:57:13.174127Z","iopub.status.idle":"2023-03-23T02:57:13.625194Z","shell.execute_reply.started":"2023-03-23T02:57:13.174072Z","shell.execute_reply":"2023-03-23T02:57:13.623971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history_metric('f1_score', ylim=[0,1], yticks=np.arange(0.0, 1.1, 0.1))","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:57:13.626601Z","iopub.execute_input":"2023-03-23T02:57:13.626976Z","iopub.status.idle":"2023-03-23T02:57:14.078523Z","shell.execute_reply.started":"2023-03-23T02:57:13.62694Z","shell.execute_reply":"2023-03-23T02:57:14.077209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history_metric('precision', ylim=[0,1], yticks=np.arange(0.0, 1.1, 0.1))","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:57:14.080207Z","iopub.execute_input":"2023-03-23T02:57:14.080678Z","iopub.status.idle":"2023-03-23T02:57:14.532388Z","shell.execute_reply.started":"2023-03-23T02:57:14.080619Z","shell.execute_reply":"2023-03-23T02:57:14.530897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history_metric('recall', ylim=[0,1], yticks=np.arange(0.0, 1.1, 0.1))","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:57:14.53409Z","iopub.execute_input":"2023-03-23T02:57:14.534531Z","iopub.status.idle":"2023-03-23T02:57:14.997559Z","shell.execute_reply.started":"2023-03-23T02:57:14.534486Z","shell.execute_reply":"2023-03-23T02:57:14.996182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history_metric('auc', ylim=[0,1], yticks=np.arange(0.0, 1.1, 0.1))","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:57:14.999296Z","iopub.execute_input":"2023-03-23T02:57:14.99978Z","iopub.status.idle":"2023-03-23T02:57:15.447077Z","shell.execute_reply.started":"2023-03-23T02:57:14.999725Z","shell.execute_reply":"2023-03-23T02:57:15.445706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history_metric('binary_accuracy', ylim=[0,1], yticks=np.arange(0.0, 1.1, 0.1))","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:57:15.448889Z","iopub.execute_input":"2023-03-23T02:57:15.449414Z","iopub.status.idle":"2023-03-23T02:57:15.919724Z","shell.execute_reply.started":"2023-03-23T02:57:15.449358Z","shell.execute_reply":"2023-03-23T02:57:15.918429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series(y_true_val).value_counts().plot(kind=\"bar\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:57:15.921136Z","iopub.execute_input":"2023-03-23T02:57:15.922186Z","iopub.status.idle":"2023-03-23T02:57:16.116452Z","shell.execute_reply.started":"2023-03-23T02:57:15.922142Z","shell.execute_reply":"2023-03-23T02:57:16.115146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.distplot(pd.Series(y_pred_val), kde=True, rug=False, hist=False, kde_kws=dict(fill=True))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:57:16.118049Z","iopub.execute_input":"2023-03-23T02:57:16.118407Z","iopub.status.idle":"2023-03-23T02:57:16.464095Z","shell.execute_reply.started":"2023-03-23T02:57:16.118371Z","shell.execute_reply":"2023-03-23T02:57:16.462795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = [[0,1][i>0.5] for i in y_pred_val]","metadata":{"execution":{"iopub.status.busy":"2023-03-23T03:04:37.667795Z","iopub.execute_input":"2023-03-23T03:04:37.668445Z","iopub.status.idle":"2023-03-23T03:04:37.680539Z","shell.execute_reply.started":"2023-03-23T03:04:37.668378Z","shell.execute_reply":"2023-03-23T03:04:37.678673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = tf.constant(labels,dtype = tf.int32)\npredictions = tf.constant(predictions,dtype = tf.int32)\nres = tf.math.confusion_matrix(labels,predictions)\nres","metadata":{"execution":{"iopub.status.busy":"2023-03-23T03:08:45.61422Z","iopub.execute_input":"2023-03-23T03:08:45.61468Z","iopub.status.idle":"2023-03-23T03:08:45.631434Z","shell.execute_reply.started":"2023-03-23T03:08:45.614642Z","shell.execute_reply":"2023-03-23T03:08:45.629991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax= plt.subplot()\nsns.heatmap(res, annot=True, fmt='g', ax=ax);  #annot=True to annotate cells, ftm='g' to disable scientific notation\n\n# labels, title and ticks\nax.set_title('Confusion Matrix')\nax.xaxis.set_ticklabels(['True', 'False']) \nax.yaxis.set_ticklabels(['Negative', 'Positive'])\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T03:18:35.275086Z","iopub.execute_input":"2023-03-23T03:18:35.275654Z","iopub.status.idle":"2023-03-23T03:18:35.53642Z","shell.execute_reply.started":"2023-03-23T03:18:35.275607Z","shell.execute_reply":"2023-03-23T03:18:35.534954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series(y_pred_val).value_counts().boxplot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:57:16.909336Z","iopub.status.idle":"2023-03-23T02:57:16.909804Z","shell.execute_reply.started":"2023-03-23T02:57:16.90958Z","shell.execute_reply":"2023-03-23T02:57:16.909607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.swarmplot(pd.Series(y_pred_val))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:57:16.911712Z","iopub.status.idle":"2023-03-23T02:57:16.9122Z","shell.execute_reply.started":"2023-03-23T02:57:16.91195Z","shell.execute_reply":"2023-03-23T02:57:16.911977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.stripplot(pd.Series(y_pred_val))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:57:16.913761Z","iopub.status.idle":"2023-03-23T02:57:16.914223Z","shell.execute_reply.started":"2023-03-23T02:57:16.913988Z","shell.execute_reply":"2023-03-23T02:57:16.914011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series(y_pred_val).value_counts().plot(kind=\"bar\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-23T02:57:16.91565Z","iopub.status.idle":"2023-03-23T02:57:16.916084Z","shell.execute_reply.started":"2023-03-23T02:57:16.915873Z","shell.execute_reply":"2023-03-23T02:57:16.915897Z"},"trusted":true},"execution_count":null,"outputs":[]}]}