application_vnd.jupyter.stderr_output_3.png text_plain_output_2.png text_plain_output_1.png
import numpy as np # linear algebra

import tensorflow as tf

import tensorflow_datasets as tfds

def load_data():
    mnist_train = tfds.load('mnist', split='train', shuffle_files=True)
    x_train = np.zeros((60000, 28, 28, 1))
    y_train = np.zeros((60000, 1))
    i = 0
    for ex in mnist_train:
        x_train[i] = ex['image']
        y_train[i] = ex['label']
        i = i + 1
    mnist_train_c = tfds.load('mnist_corrupted/splatter', split='train', shuffle_files=True)
    x_test = np.zeros((60000, 28, 28, 1))
    y_test = np.zeros((60000, 1))
    i = 0
    for ex in mnist_train_c:
        x_test[i] = ex['image']
        y_test[i] = ex['label']
        i = i + 1
    x_train = 1 - x_train / 255.0
    x_train = x_train.astype(np.float32)
    y_train_oh = tf.keras.utils.to_categorical(y_train)
    x_test = 1 - x_test / 255.0
    x_test = x_test.astype(np.float32)
    y_test_oh = tf.keras.utils.to_categorical(y_test)
    return ((x_train, y_train, y_train_oh), (x_test, y_test, y_test_oh))

(x_train, y_train, y_train_oh), (x_test_c, y_test_c, y_test_oh_c) = load_data()