text_plain_output_1.png
from tensorflow.keras import models, layers

from tensorflow.keras.applications import Xception

from tensorflow.keras.optimizers import Adam

import matplotlib.pyplot as plt

import pandas as pd

import seaborn as sns

import tensorflow as tf

try:
    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
except ValueError:
    tpu = None
if tpu:
    tf.config.experimental_connect_to_cluster(tpu)
    tf.tpu.experimental.initialize_tpu_system(tpu)
    strategy = tf.distribute.experimental.TPUStrategy(tpu)
else:
    strategy = tf.distribute.get_strategy()
AUTO = tf.data.experimental.AUTOTUNE
REPLICAS = strategy.num_replicas_in_sync

WORK_DIR = '../input/ranzcr-clip-catheter-line-classification'
os.listdir(WORK_DIR)

train = pd.read_csv(os.path.join(WORK_DIR, 'train.csv'))
train_images = '../input/ranzcr-clip-catheter-line-classification' + '/train/' + train['StudyInstanceUID'] + '.jpg'
ss = pd.read_csv(os.path.join(WORK_DIR, 'sample_submission.csv'))
test_images = '../input/ranzcr-clip-catheter-line-classification' + '/test/' + ss['StudyInstanceUID'] + '.jpg'
label_cols = ss.columns[1:]
labels = train[label_cols].values
train_annot = pd.read_csv(os.path.join(WORK_DIR, 'train_annotations.csv'))

sns.set_style("whitegrid")
fig = plt.figure(figsize = (15, 12), dpi = 300)
plt.suptitle('Labels count', fontfamily = 'serif', size = 15)

for ind, i in enumerate(label_cols):
    fig.add_subplot(4, 3, ind + 1)

    sns.countplot(train[i], edgecolor = 'black',
                  palette = reversed(sns.color_palette('viridis', 2)))
    
    plt.xlabel('')
    plt.ylabel('')
    plt.xticks(fontfamily = 'serif', size = 10)
    plt.yticks(fontfamily = 'serif', size = 10)
    plt.title(i, fontfamily = 'serif', size = 10)
plt.show()

sample = train.sample(9)
for ind, image_id in enumerate(sample.StudyInstanceUID):
    image = image_id + '.jpg'
    img = cv2.imread(os.path.join(WORK_DIR, 'train', image))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    plt.axis('off')

BATCH_SIZE = 8 * REPLICAS
STEPS_PER_EPOCH = len(train) * 0.8 / BATCH_SIZE
VALIDATION_STEPS = len(train) * 0.2 / BATCH_SIZE
EPOCHS = 30
TARGET_SIZE = 750

def build_decoder(with_labels=True, target_size=(TARGET_SIZE, TARGET_SIZE), ext='jpg'):
    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(img):
        img = tf.image.random_flip_left_right(img)
        img = tf.image.random_flip_up_down(img)
        img = tf.image.adjust_brightness(img, 0.1)
        return img
    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

def create_model():
    conv_base = Xception(include_top=False, weights='imagenet', input_shape=(TARGET_SIZE, TARGET_SIZE, 3))
    model = conv_base.output
    model = layers.GlobalAveragePooling2D()(model)
    model = layers.Dropout(0.2)(model)
    model = layers.Dense(11, activation='sigmoid')(model)
    model = models.Model(conv_base.input, model)
    model.compile(optimizer=Adam(lr=0.001), loss='binary_crossentropy', metrics=[tf.keras.metrics.AUC(multi_label=True)])
    return model

with strategy.scope():
    model = create_model()
model.summary()