text_plain_output_1.png
import numpy as np

import os

import pandas as pd

import random

import tensorflow as tf

import tensorflow_addons as tfa

train = pd.read_csv('../input/digit-recognizer/train.csv')
test = pd.read_csv('../input/digit-recognizer/test.csv')

train.shape

Y_train = train['label']
X_train = train.drop('label', axis=1).values
test = test.values

RANDOM_STATE = 75
def random_seed(seed):
    random.seed(RANDOM_STATE)
    os.environ['PYTHONHASHSEED'] = str(RANDOM_STATE)
    np.random.seed(RANDOM_STATE)
    tf.random.set_seed(RANDOM_STATE)

AUTOTUNE = tf.data.experimental.AUTOTUNE
EPOCHS = 25
SHUFFLE_BUFFER_SIZE = 5000
BATCH_SIZE = 64

@tf.function
def reshape(image, label):
    return (tf.reshape(image, (28, 28, 1)), label)

@tf.function
def normalize(image, label):
    image = tf.cast(image, tf.float32)
    return (image / 255.0, label)

@tf.function
def rotate_tf(image):
    random_angles = tf.random.uniform(shape=(), minval=-np.pi / 15, maxval=np.pi / 15)
    return tfa.image.rotate(image, random_angles)

@tf.function
def augment(image, label):
    image = rotate_tf(image)
    return (image, label)

def get_data_set(images, labels, is_training=False):
    dataset = tf.data.Dataset.from_tensor_slices((images, labels))
    dataset = dataset.map(reshape, num_parallel_calls=AUTOTUNE)
    dataset = dataset.map(normalize, num_parallel_calls=AUTOTUNE)
    if is_training:
        dataset = dataset.map(augment, num_parallel_calls=AUTOTUNE)
    dataset = dataset.shuffle(SHUFFLE_BUFFER_SIZE)
    dataset = dataset.batch(BATCH_SIZE)
    dataset = dataset.prefetch(AUTOTUNE)
    return dataset

train_dataset = get_data_set(X_train, y_train, is_training=True)
test_dataset = get_data_set(X_val, y_val)
sample_x, sample_y = next(iter(train_dataset))
print(sample_x.shape)
print(sample_y.shape)