application_vnd.jupyter.stderr_output_1.png image_output_1.png
import matplotlib.pyplot as plt

import numpy as np

import os

import tensorflow as tf

config = {'SEED': 42, 'DEBUG': False, 'test_size': 0.1, 'img_size': 128, 'batch_size': 32, 'num_labels': 0, 'epochs': 10, 'device': 'GPU'}
debug_config = {}

def set_seed(SEED):
    os.environ['PYTHONHASHSEED'] = str(SEED)
    np.random.seed(SEED)
    tf.random.set_seed(SEED)
set_seed(config['SEED'])

def get_device(device):
    if device == 'TPU':
        try:
            tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()
            strategy = tf.distribute.TPUStrategy(tpu)
        except ValueError:
    if device == 'GPU':
        strategy = tf.distribute.MirroredStrategy()
    return strategy
strategy = get_device(config['device'])
config['batch_size'] = config['batch_size'] * strategy.num_replicas_in_sync

train_ds = tf.data.Dataset.from_tensor_slices((X_train.spec_path.values, X_train.genre_id.values))
valid_ds = tf.data.Dataset.from_tensor_slices((X_valid.spec_path.values, X_valid.genre_id.values))

AUTOTUNE = tf.data.experimental.AUTOTUNE

def process_data_train(image_path, label):
    img = tf.io.read_file(image_path)
    img = tf.image.decode_jpeg(img, channels=3)
    img = tf.image.random_brightness(img, 0.1)
    img = tf.image.resize(img, [config['img_size'], config['img_size']])
    return (img, label)
def process_data_valid(image_path, label):
    img = tf.io.read_file(image_path)
    img = tf.image.decode_jpeg(img, channels=3)
    img = tf.image.resize(img, [config['img_size'], config['img_size']])
    return (img, label)
train_ds = train_ds.map(process_data_train, num_parallel_calls=AUTOTUNE)
valid_ds = valid_ds.map(process_data_valid, num_parallel_calls=AUTOTUNE)

def configure_for_performance(ds, batch_size = 32):
    ds = ds.cache('/kaggle/dump.tfcache') 
    ds = ds.shuffle(buffer_size=32)
    ds = ds.batch(batch_size)
    ds = ds.prefetch(buffer_size=AUTOTUNE)
    return ds

train_ds_batch = configure_for_performance(train_ds, config['batch_size'])
valid_ds_batch = valid_ds.batch(config['batch_size']*2)

with strategy.scope():
    model = tf.keras.models.Sequential([tf.keras.layers.Conv2D(16, kernel_size=5, activation='relu', input_shape=(config['img_size'], config['img_size'], 3)), tf.keras.layers.Conv2D(32, kernel_size=3, activation='relu'), tf.keras.layers.Conv2D(64, kernel_size=3, strides=2, activation='relu'), tf.keras.layers.Conv2D(128, kernel_size=3, activation='relu'), tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(config['num_labels'], activation='softmax')])
    model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(), optimizer='adam', metrics='accuracy')

model.summary()

best_weight_path = 'best_model.hdf5'
last_weight_path = 'last_model.hdf5'
checkpoint = tf.keras.callbacks.ModelCheckpoint(best_weight_path, monitor='val_loss', verbose=1, save_best_only=True, mode='min', save_weights_only=False)
checkpoint_last = tf.keras.callbacks.ModelCheckpoint(last_weight_path, monitor='val_loss', verbose=1, save_best_only=False, mode='min', save_weights_only=False)
early = tf.keras.callbacks.EarlyStopping(monitor='val_loss', mode='min', patience=5)
reduceLROnPlat = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.8, patience=2, verbose=1, mode='auto', epsilon=0.0001, cooldown=5, min_lr=1e-05)
callbacks_list = [checkpoint, checkpoint_last, early, reduceLROnPlat]

history = model.fit(train_ds_batch, callbacks=callbacks_list, epochs=config['epochs'], validation_data=valid_ds_batch)