text_plain_output_1.png
from kaggle_datasets import KaggleDatasets

from keras.applications import VGG19,ResNet50,Xception,InceptionResNetV2,InceptionV3,ResNet152V2

import efficientnet.tfkeras as efn

import numpy as np

import os

import pandas as pd

import tensorflow as tf

def seed_everything(seed=0):
    random.seed(seed)
    np.random.seed(seed)
    tf.random.set_seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    os.environ['TF_DETERMINISTIC_OPS'] = '1'
seed = 42

train = pd.read_csv('../input/segment-road/Train (1).csv')
test = pd.read_csv('../input/segment-road/Test (1).csv')
sub = pd.read_csv('../input/segment-road/SampleSubmission.csv')

def auto_select_accelerator():
    """
    Reference: 
        * https://www.kaggle.com/mgornergoogle/getting-started-with-100-flowers-on-tpu
        * https://www.kaggle.com/xhlulu/ranzcr-efficientnet-tpu-training
    """
    try:
        tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
        tf.config.experimental_connect_to_cluster(tpu)
        tf.tpu.experimental.initialize_tpu_system(tpu)
        strategy = tf.distribute.experimental.TPUStrategy(tpu)
    except ValueError:
        strategy = tf.distribute.get_strategy()
    return strategy
def build_decoder(with_labels=True, target_size=(256, 256), ext='png'):
    def decode(path):
        file_bytes = tf.io.read_file(path)
        if ext == 'png':
            img = tf.image.decode_png(file_bytes, channels=3)
        elif ext in ['jpg', 'jpeg']:
            img = tf.image.decode_jpeg(file_bytes, channels=3)
        else:
            raise ValueError('Image extension not supported')
        img = tf.cast(img, tf.float32) / 255.0
        img = tf.image.resize(img, target_size)
        return img
    def decode_with_labels(path, label):
        return (decode(path), label)
    return decode_with_labels if with_labels else decode
def build_augmenter(with_labels=True):
    def augment(image):
        image = tf.image.random_flip_left_right(image)
        image = tf.image.random_flip_up_down(image)
        image = tf.image.random_brightness(image, max_delta=0.1)
        image = tf.image.random_saturation(image, lower=0.75, upper=1.5)
        image = tf.image.random_hue(image, max_delta=0.15)
        image = tf.image.random_contrast(image, lower=0.75, upper=1.5)
        image = tf.image.rot90(image)
        image = tf.image.transpose(image)
        return image
    def augment_with_labels(img, label):
        return (augment(img), label)
    return augment_with_labels if with_labels else augment
def build_dataset(paths, labels=None, bsize=32, cache=True, decode_fn=None, augment_fn=None, augment=True, repeat=True, shuffle=1024, cache_dir=''):
    if cache_dir != '' and cache is True:
        os.makedirs(cache_dir, exist_ok=True)
    if decode_fn is None:
        decode_fn = build_decoder(labels is not None)
    if augment_fn is None:
        augment_fn = build_augmenter(labels is not None)
    AUTO = tf.data.experimental.AUTOTUNE
    slices = paths if labels is None else (paths, labels)
    dset = tf.data.Dataset.from_tensor_slices(slices)
    dset = dset.map(decode_fn, num_parallel_calls=AUTO)
    dset = dset.cache(cache_dir) if cache else dset
    dset = dset.map(augment_fn, num_parallel_calls=AUTO) if augment else dset
    dset = dset.repeat() if repeat else dset
    dset = dset.shuffle(shuffle) if shuffle else dset
    dset = dset.batch(bsize).prefetch(AUTO)
    return dset

COMPETITION_NAME = 'road-segment-dataset'
strategy = auto_select_accelerator()
BATCH_SIZE = strategy.num_replicas_in_sync * 8
GCS_DS_PATH = KaggleDatasets().get_gcs_path(COMPETITION_NAME)

paths = []
labels = []
for i in range(len(train['file_path'].values)):
    name = train['Image_ID'].values[i]
    label = train['Target'].values[i]
    paths.append(GCS_DS_PATH + '/Images/' + name + '.png')
    labels.append(label)
paths = np.array(paths)
labels = np.array(labels)

def get_model(name):
    if name == 'effnetb0':
        with strategy.scope():
            base_model = efn.EfficientNetB0(input_shape=(im_size, im_size, 3), weights='noisy-student', include_top=False)
    elif name == 'effnetb1':
        with strategy.scope():
            base_model = efn.EfficientNetB1(input_shape=(im_size, im_size, 3), weights='noisy-student', include_top=False)
    elif name == 'effnetb2':
        with strategy.scope():
            base_model = efn.EfficientNetB2(input_shape=(im_size, im_size, 3), weights='noisy-student', include_top=False)
    elif name == 'effnetb3':
        with strategy.scope():
            base_model = efn.EfficientNetB3(input_shape=(im_size, im_size, 3), weights='noisy-student', include_top=False)
    elif name == 'effnetb4':
        with strategy.scope():
            base_model = efn.EfficientNetB4(input_shape=(im_size, im_size, 3), weights='noisy-student', include_top=False)
    elif name == 'effnetb5':
        with strategy.scope():
            base_model = efn.EfficientNetB5(input_shape=(im_size, im_size, 3), weights='noisy-student', include_top=False)
    elif name == 'effnetb6':
        with strategy.scope():
            base_model = efn.EfficientNetB6(input_shape=(im_size, im_size, 3), weights='noisy-student', include_top=False)
    elif name == 'effnetb7':
        with strategy.scope():
            base_model = efn.EfficientNetB7(input_shape=(im_size, im_size, 3), weights='noisy-student', include_top=False)
    elif name == 'resnet':
        with strategy.scope():
            base_model = ResNet50(input_shape=(im_size, im_size, 3), weights='imagenet', include_top=False)
    elif name == 'xception':
        with strategy.scope():
            base_model = Xception(input_shape=(im_size, im_size, 3), weights='imagenet', include_top=False)
    elif name == 'inception':
        with strategy.scope():
            base_model = InceptionV3(input_shape=(im_size, im_size, 3), weights='imagenet', include_top=False)
    elif name == 'inceptionresnet':
        with strategy.scope():
            base_model = InceptionResNetV2(input_shape=(im_size, im_size, 3), weights='imagenet', include_top=False)
    with strategy.scope():
        model = tf.keras.Sequential([base_model, tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(1, activation='sigmoid')])
        model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001), loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=0.001), metrics=[tf.keras.metrics.AUC()])
    return model

test_paths = []
for i in range(len(test['file_path'].values)):
    name = test['Image_ID'].values[i]
    test_paths.append(GCS_DS_PATH + '/Images/' + name + '.png')

IMSIZES = (224, 240, 260, 300, 380, 456, 512, 600)
im_size = 250
decoder = build_decoder(with_labels=True, target_size=(im_size, im_size))
test_decoder = build_decoder(with_labels=False, target_size=(im_size, im_size))
train_dataset = build_dataset(train_paths, train_labels, bsize=BATCH_SIZE, decode_fn=decoder)
valid_dataset = build_dataset(valid_paths, valid_labels, bsize=BATCH_SIZE, decode_fn=decoder, repeat=False, shuffle=False, augment=False)
test_decoder = build_decoder(with_labels=False, target_size=(im_size, im_size))
dtest = build_dataset(test_paths, bsize=BATCH_SIZE, repeat=False, shuffle=False, augment=False, cache=False, decode_fn=test_decoder)

model_name = 'effnetb1'
model = get_model(model_name)
model.summary()

train_paths = np.array(train_paths)
steps_per_epoch = train_paths.shape[0] // BATCH_SIZE
checkpoint = tf.keras.callbacks.ModelCheckpoint(f'{model_name}_best_auc.h5', save_best_only=True, monitor='val_auc', mode='max')
lr_reducer = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_auc', patience=3, min_lr=1e-07, mode='min')

history = model.fit(train_dataset, epochs=30, verbose=1, callbacks=[checkpoint, lr_reducer], steps_per_epoch=steps_per_epoch, validation_data=valid_dataset)