{"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 efficientnet -q\n!pip install iterative-stratification","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport tensorflow_addons as tfa\nimport efficientnet.tfkeras as efn\nimport numpy as np\nimport pandas as pd\nfrom kaggle_datasets import KaggleDatasets\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom sklearn.model_selection import GroupKFold\nimport matplotlib.pyplot as plt\nimport glob\nfrom tqdm import tqdm\nfrom iterstrat.ml_stratifiers import MultilabelStratifiedKFold,MultilabelStratifiedShuffleSplit","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\ncount=0\ntotal = 0\nfor i in df2.index:\n    total+=1\n    if '../input/public-green-notfull/png_768/'+df2.loc[i,'ID']+'.png_' not in file_list:\n        count+=1\n        df2.drop(index=i,inplace=True)\n\nprint('total record img:'+str(total))\nprint('duplicate:'+str(count))\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\ncount=0\ntotal = 0\nfor i in df2.index:\n    total+=1\n    if '../input/public-green-notfull/png_768/'+df2.loc[i,'ID']+'.png_' not in file_list:\n        count+=1\n        df2.drop(index=i,inplace=True)\n\nprint('total record img:'+str(total))\nprint('duplicate:'+str(count))\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Bug version, tensor here is graph mode, isin will cause error\n'''\ndef map_loss(y_true,y_pred):\n    lss_fn = tfa.losses.SigmoidFocalCrossEntropy()\n    # y_true should be (BS,N_CLS)\n    bs = y_true.shape[0]\n    tp_mul = y_true * y_pred\n    y_pred_sort = tf.sort(y_pred,axis=-1,direction='DESCENDING',name=None)\n    #loss = tf.convert_to_tensor(np.zeros([1]),dtype=tf. float32)\n    loss = tf.convert_to_tensor(np.array([]),dtype=tf. float32)\n    for i in range(bs):\n        arr_nonzero=[]\n        nonzero=0\n        arr_pred = []\n        for k in tp_mul[i]:\n            if k>0:\n                arr_nonzero.append(k) \n                nonzero+=1\n        for k in y_pred_sort[i][:nonzero]:\n            arr_pred.append(k)\n        print(arr_pred,arr_nonzero)\n        alp = 2-np.mean(np.isin(arr_pred,arr_nonzero))\n        #loss+=alp*lss_fn(y_true[i,:],y_pred[i,:])\n        loss = tf.experimental.numpy.append(loss, alp*lss_fn(y_true[i,:],y_pred[i,:]), axis=None)\n    return loss\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fixed version\ndef map_loss(y_true,y_pred):\n    lss_fn = tfa.losses.SigmoidFocalCrossEntropy()\n    # y_true should be (BS,N_CLS)\n    bs = y_true.shape[0]\n    tp_mul = y_true * y_pred\n    y_pred_sort = tf.sort(y_pred,axis=-1,direction='DESCENDING',name=None)\n    #loss = tf.convert_to_tensor(np.zeros([1]),dtype=tf. float32)\n    loss = tf.convert_to_tensor(np.array([]),dtype=tf. float32)\n    \n    \n    for i in range(bs):\n        arr_nonzero=[]\n        nonzero=0\n        arr_pred = []\n        t_pred = tf.convert_to_tensor(np.array([0]),dtype=tf. float32)\n        t_nonzero = tf.convert_to_tensor(np.array([0]),dtype=tf. float32)\n        for k in tp_mul[i]:\n            if k>0:\n                arr_nonzero.append(k) \n                nonzero+=1\n                t_nonzero = t_nonzero + k\n        for k in y_pred_sort[i][:nonzero]:\n            arr_pred.append(k)\n            t_pred = t_pred + k\n        \n        cor_num=0.\n        total_num=0.\n        if t_pred==t_nonzero:\n            cor_num=1.\n            total_num=1.\n        else:\n            cor_num=0.\n            total_num=1.\n        '''\n        for k in range(len(arr_pred)):\n            total_num+=1\n            \n            for u in range(len(arr_nonzero)):\n                if tf.math.equal(arr_pred[k],arr_nonzero[u]):\n                    cor_num+=1\n                    break\n        '''\n        alp = 2-cor_num/total_num\n        #loss+=alp*lss_fn(y_true[i,:],y_pred[i,:])\n        loss = tf.experimental.numpy.append(loss, alp*lss_fn(y_true[i,:],y_pred[i,:]), axis=None)\n        loss = tf.reduce_mean(loss)\n    return loss","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def onehot(image,label):\n    CLASSES = 19\n    return image,tf.one_hot(label,CLASSES)\ndef cutmix(image, label, PROBABILITY = 1.0):\n    # input image - is a batch of images of size [n,dim,dim,3] not a single image of [dim,dim,3]\n    # output - a batch of images with cutmix applied\n    DIM = IMSIZE[IMS]\n    CLASSES = 19\n    \n    imgs = []; labs = []\n    for j in range(AUG_BATCH):\n        # DO CUTMIX WITH PROBABILITY DEFINED ABOVE\n        P = tf.cast( tf.random.uniform([],0,1)<=PROBABILITY, tf.int32)\n        # CHOOSE RANDOM IMAGE TO CUTMIX WITH\n        k = tf.cast( tf.random.uniform([],0,AUG_BATCH),tf.int32)\n        # CHOOSE RANDOM LOCATION\n        x = tf.cast( tf.random.uniform([],0,DIM),tf.int32)\n        y = tf.cast( tf.random.uniform([],0,DIM),tf.int32)\n        b = tf.random.uniform([],0,1) # this is beta dist with alpha=1.0\n        WIDTH = tf.cast( DIM * tf.math.sqrt(1-b),tf.int32) * P\n        ya = tf.math.maximum(0,y-WIDTH//2)\n        yb = tf.math.minimum(DIM,y+WIDTH//2)\n        xa = tf.math.maximum(0,x-WIDTH//2)\n        xb = tf.math.minimum(DIM,x+WIDTH//2)\n        # MAKE CUTMIX IMAGE\n        one = image[j,ya:yb,0:xa,:]\n        two = image[k,ya:yb,xa:xb,:]\n        three = image[j,ya:yb,xb:DIM,:]\n        middle = tf.concat([one,two,three],axis=1)\n        img = tf.concat([image[j,0:ya,:,:],middle,image[j,yb:DIM,:,:]],axis=0)\n        imgs.append(img)\n        # MAKE CUTMIX LABEL\n        a = tf.cast(WIDTH*WIDTH/DIM/DIM,tf.float32)\n        if len(label.shape)==1:\n            lab1 = tf.one_hot(label[j],CLASSES)\n            lab2 = tf.one_hot(label[k],CLASSES)\n        else:\n            lab1 = label[j,]\n            lab2 = label[k,]\n        lab1 = tf.cast(lab1,tf.float32)\n        lab2 = tf.cast(lab2,tf.float32)\n        labs.append((1-a)*lab1 + a*lab2)\n            \n    # RESHAPE HACK SO TPU COMPILER KNOWS SHAPE OF OUTPUT TENSOR (maybe use Python typing instead?)\n    image2 = tf.reshape(tf.stack(imgs),(AUG_BATCH,DIM,DIM,3))\n    label2 = tf.reshape(tf.stack(labs),(AUG_BATCH,CLASSES))\n    return image2,label2\n\n\ndef mixup(image, label, PROBABILITY = 1.0):\n    # input image - is a batch of images of size [n,dim,dim,3] not a single image of [dim,dim,3]\n    # output - a batch of images with mixup applied\n    DIM = IMSIZE[IMS]\n    CLASSES = 19\n    \n    imgs = []; labs = []\n    for j in range(AUG_BATCH):\n        # DO MIXUP WITH PROBABILITY DEFINED ABOVE\n        P = tf.cast( tf.random.uniform([],0,1)<=PROBABILITY, tf.float32)\n        # CHOOSE RANDOM\n        k = tf.cast( tf.random.uniform([],0,AUG_BATCH),tf.int32)\n        a = tf.random.uniform([],0,1)*P # this is beta dist with alpha=1.0\n        # MAKE MIXUP IMAGE\n        img1 = image[j,]\n        img2 = image[k,]\n        imgs.append((1-a)*img1 + a*img2)\n        # MAKE CUTMIX LABEL\n        if len(label.shape)==1:\n            lab1 = tf.one_hot(label[j],CLASSES)\n            lab2 = tf.one_hot(label[k],CLASSES)\n        else:\n            lab1 = label[j,]\n            lab2 = label[k,]\n        lab1 = tf.cast(lab1,tf.float32)\n        lab2 = tf.cast(lab2,tf.float32)\n        labs.append((1-a)*lab1 + a*lab2)\n            \n    # RESHAPE HACK SO TPU COMPILER KNOWS SHAPE OF OUTPUT TENSOR (maybe use Python typing instead?)\n    image2 = tf.reshape(tf.stack(imgs),(AUG_BATCH,DIM,DIM,3))\n    label2 = tf.reshape(tf.stack(labs),(AUG_BATCH,CLASSES))\n    return image2,label2\n\n\ndef transform(image,label):\n    # THIS FUNCTION APPLIES BOTH CUTMIX AND MIXUP\n    DIM = IMSIZE[IMS]\n    CLASSES = 19\n    SWITCH = 0.5\n    CUTMIX_PROB = 0.666\n    MIXUP_PROB = 0.666\n    # FOR SWITCH PERCENT OF TIME WE DO CUTMIX AND (1-SWITCH) WE DO MIXUP\n    image2, label2 = cutmix(image, label, CUTMIX_PROB)\n    image3, label3 = mixup(image, label, MIXUP_PROB)\n    imgs = []; labs = []\n    for j in range(AUG_BATCH):\n        P = tf.cast( tf.random.uniform([],0,1)<=SWITCH, tf.float32)\n        imgs.append(P*image2[j,]+(1-P)*image3[j,])\n        labs.append(P*label2[j,]+(1-P)*label3[j,])\n    # RESHAPE HACK SO TPU COMPILER KNOWS SHAPE OF OUTPUT TENSOR (maybe use Python typing instead?)\n    image4 = tf.reshape(tf.stack(imgs),(AUG_BATCH,DIM,DIM,3))\n    label4 = tf.reshape(tf.stack(labs),(AUG_BATCH,CLASSES))\n    return image4,label4\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def auto_select_accelerator():\n    try:\n        tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n        tf.config.experimental_connect_to_cluster(tpu)\n        tf.tpu.experimental.initialize_tpu_system(tpu)\n        strategy = tf.distribute.experimental.TPUStrategy(tpu)\n        print(\"Running on TPU:\", tpu.master())\n    except ValueError:\n        strategy = tf.distribute.get_strategy()\n    print(f\"Running on {strategy.num_replicas_in_sync} replicas\")\n    \n    return strategy\n\n\ndef build_decoder(with_labels=True, target_size=(256, 256), ext='jpg'):\n    def decode(path):\n        file_bytes = tf.io.read_file(path)\n\n        if ext == 'png':\n            img = tf.image.decode_png(file_bytes, channels=3)\n        elif ext in ['jpg', 'jpeg']:\n            img = tf.image.decode_jpeg(file_bytes, channels=3)\n        else:\n            raise ValueError(\"Image extension not supported\")\n        img = tf.cast(img, tf.float32) / 255.0\n        img = tf.image.resize(img, target_size)\n\n        return img\n    \n    def decode_with_labels(path, label):\n        return decode(path), label\n    \n    return decode_with_labels if with_labels else decode\n\n\ndef build_augmenter(with_labels=True):\n    def augment(img):\n        img = tf.image.random_flip_left_right(img)\n        img = tf.image.random_flip_up_down(img)\n        return img\n    \n    def augment_with_labels(img, label):\n        return augment(img), label\n    \n    return augment_with_labels if with_labels else augment\n\n\n\nDIM =600\nn_class = 19\ndef _parse_image_function(example_proto,augment = True):\n    image_feature_description = {\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'label': tf.io.FixedLenFeature([], tf.string)\n    }\n    single_example = tf.io.parse_single_example(example_proto, image_feature_description)\n    image = tf.reshape( tf.io.decode_raw(single_example['image'],out_type=np.dtype('uint8')), (DIM,DIM, 3))\n    mask =  tf.reshape(tf.io.decode_raw(single_example['label'],out_type=np.dtype('uint8')),[n_class])\n    image = tf.dtypes.cast(image, tf.float32)\n    mask = tf.dtypes.cast(mask, tf.float32)\n    image = image/255.\n    if augment: # https://www.kaggle.com/kool777/training-hubmap-eda-tf-keras-tpu\n\n        if tf.random.uniform(()) > 0.5:\n            image = tf.image.flip_left_right(image)\n            mask = tf.image.flip_left_right(mask)\n\n        if tf.random.uniform(()) > 0.4:\n            image = tf.image.flip_up_down(image)\n            mask = tf.image.flip_up_down(mask)\n\n        if tf.random.uniform(()) > 0.5:\n            image = tf.image.rot90(image, k=1)\n            mask = tf.image.rot90(mask, k=1)\n\n        if tf.random.uniform(()) > 0.45:\n            image = tf.image.random_saturation(image, 0.7, 1.3)\n\n        if tf.random.uniform(()) > 0.45:\n            image = tf.image.random_contrast(image, 0.8, 1.2)\n    \n    return tf.cast(image, tf.float32),tf.cast(mask, tf.float32)\n\n\n\n\ndef load_dataset(filenames, ordered=False, augment = False):\n    AUTO = tf.data.experimental.AUTOTUNE\n    ignore_order = tf.data.Options()\n    if not ordered:\n        ignore_order.experimental_deterministic = False\n    dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)\n    dataset = dataset.with_options(ignore_order)\n    dataset = dataset.map(lambda ex: _parse_image_function(ex, augment = augment), num_parallel_calls=AUTO)\n    return dataset\n\n\n\n\n\n\ndef build_dataset(paths, labels=None, bsize=128, cache=True,\n                  decode_fn=None, augment_fn=None,\n                  augment=True,augment_mixup_cutmix=False\n                  , repeat=True, shuffle=1024, \n                  cache_dir=\"\"):\n    if cache_dir != \"\" and cache is True:\n        os.makedirs(cache_dir, exist_ok=True)\n    \n    if decode_fn is None:\n        decode_fn = build_decoder(labels is not None)\n    \n    if augment_fn is None:\n        augment_fn = build_augmenter(True)\n    \n    AUTO = tf.data.experimental.AUTOTUNE\n    #slices = paths if labels is None else (paths, labels)\n    dset = load_dataset(paths)\n    #dset = tf.data.Dataset.from_tensor_slices(slices)\n    #dset = dset.map(decode_fn, num_parallel_calls=AUTO)\n    dset = dset.cache(cache_dir) if cache else dset\n    #dset = dset.map(transform, num_parallel_calls=AUTO) if augment else dset\n    dset = dset.map(augment_fn, num_parallel_calls=AUTO) if augment else dset\n    dset = dset.repeat() if repeat else dset\n    dset = dset.shuffle(shuffle) if shuffle else dset\n    dset = dset.batch(bsize)\n    dset = dset.map(transform,num_parallel_calls=AUTO) if augment_mixup_cutmix else dset\n    dset = dset.prefetch(AUTO)\n    \n    return dset","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def count_data_items(filenames):\n    img_num = 0\n    for i in filenames:\n        img_num +=int(i.split('_')[-2])\n    return img_num","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#COMPETITION_NAME = \"hpa-768768\"\nstrategy = auto_select_accelerator()\nBATCH_SIZE = strategy.num_replicas_in_sync * 16\nAUG_BATCH = BATCH_SIZE\n#GCS_DS_PATH = KaggleDatasets().get_gcs_path(COMPETITION_NAME)\n#GCS_DS_PATH2 = KaggleDatasets().get_gcs_path('public-green-notfull')\n#n_splits = 2","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#green\n#load_dir = f\"/kaggle/input/{COMPETITION_NAME}/\"\n#df = pd.read_csv('../input/classification-label-csv-green/df_green.csv')\n#df2 = pd.read_csv('../input/atlascsv/kaggle_2021_ex_p_green_onehot.csv') declare before\n#label_cols = df.columns[2:21]\n\n\n#paths = GCS_DS_PATH + '/' + df['ID'] + '.png'\n#labels = df[label_cols].values\n\n\n#paths2 = GCS_DS_PATH2 + '/png_768/' + df2['ID'] + '.png_'\n#labels2 = df2[label_cols].values","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#labels = labels.astype(np.float32)\n#labels2 = labels2.astype(np.float32)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#paths =np.concatenate((paths,paths2),axis=0)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"#labels = np.concatenate((labels,labels2),axis=0)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n(\n    train_paths, valid_paths, \n    train_labels, valid_labels\n) = train_test_split(paths, labels, test_size=0.2, random_state=42)\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#kf = MultilabelStratifiedShuffleSplit(n_splits=n_splits, test_size=0.2, random_state=100)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"'''\nfor train_index, test_index in kf.split(paths, labels):\n   train_paths, valid_paths = paths[train_index], paths[test_index]\n   train_labels, valid_labels = labels[train_index], labels[test_index]\n   break\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 'gs://kds-db891f03f8787fad574c283c8f45d40c0072ebcdc9e44f8b821c3788' hpatrain-green-imagetfrec\n#'gs://kds-38d7c7971dfd017bc732339d9814b3cdf2211a934a6c1423e5e7a80c' hpavalid-green-imagetfrec\n#'gs://kds-3867054d379fba84653fcf956ef637375f645217ab4358a185785aaf' hpatrain-ex08-green-imagetfrec\n#'gs://kds-45d6c0aafb360b188d1a74670585227dbcc2847490f9dec13e879934' hpatrain-ex-916-green-imagetfrec\n#'gs://kds-47b27aa31455ce4713b8c540ab3bb52d07f01b5fe2bf4be64c673821' hpatrain-ex-1724-green-imagetfrec\n#'gs://kds-833ca2c0d1e0e67d9c5b3ad7c0da7c3235f418ca65031f5e6df34fc3' hpatrain-ex-2532-green-imagetfrec\n# 'gs://kds-ee5eb0076b7380bde326f8d748a8103f22ed7b4f309d43ddaadb5049' hpatrain-ex-3338-green-imagetfrec","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_paths=[]\ntrain_paths.extend(tf.io.gfile.glob('gs://kds-db891f03f8787fad574c283c8f45d40c0072ebcdc9e44f8b821c3788/*'))\ntrain_paths.extend(tf.io.gfile.glob('gs://kds-3867054d379fba84653fcf956ef637375f645217ab4358a185785aaf/*'))\ntrain_paths.extend(tf.io.gfile.glob('gs://kds-45d6c0aafb360b188d1a74670585227dbcc2847490f9dec13e879934/*'))\ntrain_paths.extend(tf.io.gfile.glob('gs://kds-47b27aa31455ce4713b8c540ab3bb52d07f01b5fe2bf4be64c673821/*'))\ntrain_paths.extend(tf.io.gfile.glob('gs://kds-833ca2c0d1e0e67d9c5b3ad7c0da7c3235f418ca65031f5e6df34fc3/*'))\ntrain_paths.extend(tf.io.gfile.glob('gs://kds-ee5eb0076b7380bde326f8d748a8103f22ed7b4f309d43ddaadb5049/*'))\nvalid_paths=[]\nvalid_paths.extend(tf.io.gfile.glob('gs://kds-f126cc8401f164a946dd39442f9eb065fe400734970516b2593a0cca/*'))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMSIZE = (224, 240, 260, 300, 380, 456, 528, 600)\nIMS = 7\n\ndecoder = build_decoder(with_labels=True, target_size=(IMSIZE[IMS], IMSIZE[IMS]))\ntest_decoder = build_decoder(with_labels=False, target_size=(IMSIZE[IMS], IMSIZE[IMS]))\n\ntrain_dataset = build_dataset(\n    train_paths,  bsize=BATCH_SIZE,cache=False, decode_fn=decoder,augment_mixup_cutmix=True\n)\n\n''' dataset checking\nfrom tqdm import tqdm\nrow = 6; col = 4;\nrow = min(row,BATCH_SIZE//col)\nfor (img,label) in tqdm(train_dataset):\n    plt.figure(figsize=(15,int(15*row/col)))\n    for j in range(row*col):\n        plt.subplot(row,col,j+1)\n        plt.axis('off')\n        plt.imshow(img[j,])\n    plt.show()\n    break\n'''\n\nvalid_dataset = build_dataset(\n    valid_paths,  bsize=BATCH_SIZE, decode_fn=decoder,\n    repeat=False, shuffle=False,cache=False, augment=False\n)\n\n# test_dataset = build_dataset(\n#     test_paths, cache=False, bsize=BATCH_SIZE, decode_fn=test_decoder,\n#     repeat=False, shuffle=False, augment=False\n# )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    n_labels = 19\nexcept:\n    n_labels = 1\n    \nwith strategy.scope():\n    model = tf.keras.Sequential([\n        efn.EfficientNetB7(\n            input_shape=(IMSIZE[IMS], IMSIZE[IMS], 3),\n            weights='imagenet',\n            include_top=False),\n        tf.keras.layers.GlobalAveragePooling2D(),\n        tf.keras.layers.Dense(n_labels, activation='sigmoid')\n    ])\n    model.compile(\n        optimizer=tf.keras.optimizers.Adam(),\n        loss='binary_crossentropy',#tfa.losses.SigmoidFocalCrossEntropy(),\n        metrics=[tf.keras.metrics.AUC(multi_label=True)])\n        \n    model.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colour = '_green'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#steps_per_epoch = train_paths.shape[0] // BATCH_SIZE\nsteps_per_epoch = count_data_items(train_paths) // BATCH_SIZE\ncheckpoint = tf.keras.callbacks.ModelCheckpoint(\n    f'model{colour}.h5', save_best_only=True, monitor='val_loss', mode='min')\nlr_reducer = tf.keras.callbacks.ReduceLROnPlateau(\n    monitor=\"val_loss\", patience=3, min_lr=1e-6, mode='min')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_dataset, \n    epochs=30,\n    verbose=1,\n    callbacks=[checkpoint, lr_reducer],\n    steps_per_epoch=steps_per_epoch,\n    validation_data=valid_dataset)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist_df = pd.DataFrame(history.history)\nhist_df.to_csv(f'history{colour}.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}