text_plain_output_2.png application_vnd.jupyter.stderr_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)
for image, label in train_ds.take(1):
    plt.imshow(image.numpy().astype('uint8'))
    plt.show()
    print('Image shape: ', image.numpy().shape)
    print('Label: ', label.numpy())