Esempio n. 1
0
def get_data(dataset):
    """ Prepare the mnist or fashion-mnist data to feed to the model.
    
    Args:
        dataset (string): one of the possible_datasets
    
    Returns:
        A tuple with the clean training data, clean testing data, and noisy
        testing data.
    """

    if dataset not in possible_datasets:
        datasets_output = ', '.join(possible_datasets)
        raise ValueError('dataset must be one of: {}'.format(datasets_output))

    if dataset == 'mnist':
        (clean_train, __), (clean_test, __) = mnist.load_data()
    elif dataset == 'fashion-mnist':
        (clean_train, __), (clean_test, __) = fashion_mnist.load_data()

    clean_train = clean_train.astype('float32') / 255.
    clean_train = clean_train.reshape(data_shape)
    clean_test = clean_test.astype('float32') / 255.
    clean_test = clean_test.reshape(data_shape)

    noisy_test = add_gaussian_noise_np(clean_test, 0.0, 0.4)
    return clean_train, clean_test, noisy_test
Esempio n. 2
0
def main():
    (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()

    pixels = x_train[object_to_predict]

    y_train = utils.to_categorical(y_train, 10)
    x_train = x_train / 255

    classes = [
        "t-shirt", "trouser", "pullover", "dress", "coat", "sandal", "shirt",
        "sneaker", "bag", "ankle boot"
    ]

    model = load_model("fashion_model.h5")

    image = Image.fromarray(pixels, "L")

    prediction = model.predict(
        numpy.reshape(x_train[object_to_predict], (1, 784)))

    print("Object", classes[numpy.argmax(y_train[object_to_predict])],
          "recognised as", classes[numpy.argmax(prediction)], "with accuracy",
          numpy.max(prediction * 100), "percents")

    image.show()
Esempio n. 3
0
def main():
    (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()

    x_train = x_train.reshape(60000, 784)
    x_train = x_train / 255
    x_test = x_test.reshape(10000, 784)
    x_test = x_test / 255
    y_train = utils.to_categorical(y_train, 10)
    y_test = utils.to_categorical(y_test, 10)

    model = Sequential()
    model.add(Dense(784, input_dim=784, activation="relu"))
    model.add(Dense(10, activation="softmax"))

    model.compile(loss="categorical_crossentropy",
                  optimizer="SGD",
                  metrics=["accuracy"])
    model.summary()
    callback = [
        TensorBoard(log_dir='logs', histogram_freq=1, write_images=True)
    ]

    model.fit(x_train,
              y_train,
              batch_size=200,
              epochs=300,
              verbose=1,
              validation_split=0.2,
              callbacks=callback)

    model.save("fashion_model.h5")

    score = model.evaluate(x_test, y_test, verbose=1)
    print("Accuracy on test data is", score[1] * 100, "percent")
Esempio n. 4
0
 def prepare_data(self):
     (x_train, y_train), (_, _) = fashion_mnist.load_data()
     x_train = x_train.reshape((-1, 28, 28, 1))
     x_train, y_train = x_train.astype('float32') / 255.0, \
                        tf.keras.utils.to_categorical(y_train.astype('float32'), 10)
     (x_train, x_eval) = x_train[5000:], x_train[:5000]
     (y_train, y_eval) = y_train[5000:], y_train[:5000]
     train_data, eval_data = (x_train, y_train), (x_eval, y_eval)
     return train_data, eval_data
Esempio n. 5
0
def load_real_samples():
    # load dataset
    (trainX, trainy), (_, _) = load_data()
    # expand to 3d, e.g. add channels
    X = expand_dims(trainX, axis=-1)
    # convert from ints to floats
    X = X.astype('float32')
    # scale from [0,255] to [-1,1]
    X = (X - 127.5) / 127.5
    # trainy = expand_dims(trainy, axis=-1)
    print(X.shape, trainy.shape)
    return [X, trainy]
Esempio n. 6
0
def load_image_dataset(dataset_name='mnist'):
    """
    Loads the dataset by name.

    Args:
        dataset_name: string, either "mnist", "cifar10", "cifar100", "fmnist"

    Returns:
        (X_train, y_train), (X_test, y_test)
    """

    allowed_names = ['mnist', 'cifar10', 'cifar100', 'fmnist']

    if dataset_name not in allowed_names:
        raise ValueError("Dataset name provided is wrong. Must be one of ",
                         allowed_names)

    #  print(directory)
    if dataset_name == 'mnist':
        (X_train, y_train), (X_test, y_test) = mnist.load_data()
    elif dataset_name == 'fmnist':
        (X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()
    elif dataset_name == 'cifar10':
        (X_train, y_train), (X_test, y_test) = cifar10.load_data()
    elif dataset_name == 'cifar100':
        (X_train, y_train), (X_test, y_test) = cifar100.load_data()
    else:
        raise ValueError(
            '%s is not a valid dataset name. Available choices are : %s' %
            (dataset_name, str(allowed_names)))

    if dataset_name in ['mnist', 'fmnist']:
        X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
        X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
        X_train = X_train.astype('float32') / 255.
        X_test = X_test.astype('float32') / 255.

    elif dataset_name in ['cifar10', 'cifar100']:
        X_train = X_train.astype('float32') / 255.
        X_test = X_test.astype('float32') / 255.

    if dataset_name == 'cifar100':
        num_classes = 100
    else:
        num_classes = 10

    y_train = tf.keras.utils.to_categorical(y_train, num_classes)
    y_test = tf.keras.utils.to_categorical(y_test, num_classes)

    return (X_train, y_train), (X_test, y_test)
Esempio n. 7
0
def main(args):
    if args.dataset == "mnist":
        from tensorflow.python.keras.datasets import mnist

        (x_train, y_train), (x_test, y_test) = mnist.load_data()
    elif args.dataset == "fashion_mnist":
        from tensorflow.python.keras.datasets import fashion_mnist

        (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
    elif args.dataset == "cifar10":
        from tensorflow.python.keras.datasets import cifar10

        (x_train, y_train), (x_test, y_test) = cifar10.load_data()
    else:
        sys.exit("Unknown dataset {}".format(args.dataset))

    convert(x_train, y_train, args, "train")
    convert(x_test, y_test, args, "test")
Esempio n. 8
0
def train(model_name):
    # load the data
    (train_images, train_labels), (test_images,
                                   test_labels) = fashion_mnist.load_data()

    # create a TensorBoard callback
    model_dir = root_dir(os.path.join('training', model_name))
    tensorboard_callback = TensorBoard(log_dir=model_dir)

    # create a checkpoint callback
    checkpoint_path = root_dir(os.path.join(model_dir, 'weights.ckpt'))
    checkpoint_callback = ModelCheckpoint(checkpoint_path,
                                          save_weights_only=True)

    # train the model
    model = create_model()
    model.fit(train_images,
              train_labels,
              epochs=5,
              callbacks=[checkpoint_callback, tensorboard_callback],
              validation_data=(test_images, test_labels))
Esempio n. 9
0
    def __init__(self):
        num_classes = 10
        img_rows, img_cols = 28, 28

        (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()

        if K.image_data_format() == 'channels_first':
            x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
            x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
            input_shape = (1, img_rows, img_cols)
        else:
            x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
            x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
            input_shape = (img_rows, img_cols, 1)

        x_train = x_train.astype('float32') / 255
        x_test = x_test.astype('float32') / 255
        y_train = to_categorical(y_train, num_classes)
        y_test = to_categorical(y_test, num_classes)

        super().__init__(x_train, x_test, y_train, y_test, input_shape,
                         num_classes, 'fashion_mnist')
Esempio n. 10
0
# Initialize wandb
wandb.init(project="example")
config = wandb.config

# Track hyperparameters
config.dropout = 0.2
config.hidden_layer_size = 128
config.layer_1_size = 16
config.layer_2_size = 32
config.learn_rate = 0.01
config.decay = 1e-6
config.momentum = 0.9
config.epochs = 8

(X_train_orig, y_train_orig), (X_test, y_test) = fashion_mnist.load_data()

# Reducing the dataset size to 10,000 examples for faster train time
true = list(
    map(lambda x: True if random.random() < 0.167 else False, range(60000)))
ind = []
for i, x in enumerate(true):
    if x == True: ind.append(i)

X_train = X_train_orig[ind, :, :]
y_train = y_train_orig[ind]

img_width = 28
img_height = 28
labels = [
    "T-shirt/top", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt",
Esempio n. 11
0
# >The original labels are integers from 0 to 9, so, for example, a label of 5 becomes 0000010000 when onehot encoded, but note the difference between the index and the label stored at that index:

# In[66]:

import tensorflow as tf
from tensorflow.python.keras.datasets import fashion_mnist

width, height, = 28, 28
# total classes
n_classes = 10

# #### loading the dataset

# In[ ]:

(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()

# #### Split feature training set into training and validation sets

# In[ ]:

split = 50000
(y_train, y_valid) = y_train[:split], y_train[split:]

# #### one-hot encode the labels using TensorFlow then convert back to numpy for display

# In[68]:

y_train_ohe = tf.one_hot(y_train, depth=n_classes).numpy()
y_valid_ohe = tf.one_hot(y_valid, depth=n_classes).numpy()
y_test_ohe = tf.one_hot(y_test, depth=n_classes).numpy()
    def train(self):

        # Load the dataset
        if 'mnist' in self.img_dataset:
            (X_train, y_train), (_, _) = mnist.load_data()
        elif 'cifar' in self.img_dataset:
            (X_train, y_train), (_, _) = cifar.load_data('fine')
            X_train = X_train / 255.0
            classes2labels = {c: l for c, l in enumerate(self.train_labels)}
        else:
            raise ValueError(
                "Unknown dataset {}! Please use either 'fasion_mnist' or 'cifar100'."
                .format(self.img_dataset))

        # Configure input
        X_train = (X_train.astype(np.float32) - 127.5) / 127.5
        # if self.channels == 1:
        #     X_train = np.expand_dims(X_train, axis=3)

        for n, i in enumerate(self.index):
            y_train = np.where(y_train == n, i, y_train)

        y_train = y_train.reshape(-1, 1)

        # Adversarial ground truths
        true = np.ones((self.batch_size, 1))
        fake = np.zeros((self.batch_size, 1))

        for epoch in range(self.epochs):

            # ---------------------
            #  Train Discriminator
            # ---------------------

            # Select a random half batch of images
            idx = np.random.randint(0, X_train.shape[0], self.batch_size)
            # with np.random.randint we can pick the same image more than once: is this a feature or a bug?

            imgs, labels = X_train[idx], y_train[idx]
            shuffled_labels = np.zeros_like(labels)
            for i, label in enumerate(labels):
                modified_index = np.delete(self.index,
                                           np.where(self.index == label))
                shuffled_labels[i, 0] = np.random.choice(modified_index, 1)

            # Sample noise as generator input
            noise = np.random.normal(0, 1,
                                     (self.batch_size, self.embeddings_dim))

            # Generate a half batch of new images
            gen_imgs = self.generator.predict([noise, labels])
            """
            Train the discriminator to do three tasks:
            1. recognize real images for a concept as true images for that concept
            2. recognize generated images for a concept as fake images for that concept
            3. recognize real images for a concept as fake for a different concept
            this way the generator cannot just fool the discriminator by generating sensible images in the abstract,
            but has to generate credible images for a specific concept.
            """

            d_loss_real = self.discriminator.train_on_batch([imgs, labels],
                                                            true)
            d_loss_fake = self.discriminator.train_on_batch([gen_imgs, labels],
                                                            fake)
            d_loss_unrelated = self.discriminator.train_on_batch(
                [imgs, shuffled_labels], fake)

            # this is the original loss, which could be extended to
            # d_loss = np.add(d_loss_real, d_loss_fake, d_loss_unrelated) / 3
            # to keep its spirit.
            # d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

            # this is the loss proposed in Reed et al 2018
            d_loss = np.log(d_loss_real) + (np.log(1 - d_loss_unrelated) +
                                            np.log(1 - d_loss_fake)) / 2

            # ---------------------
            #  Train Generator
            # ---------------------

            # Condition on labels
            sampled_labels = np.random.choice(self.index,
                                              self.batch_size).reshape(-1, 1)

            # Train the generator
            g_loss = self.gan.train_on_batch([noise, sampled_labels], true)

            # If at save interval => save generated image samples
            if epoch % self.sample_interval == 0:
                print("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" %
                      (epoch, d_loss[0], 100 * d_loss[1], g_loss))
                self.sample_images(epoch)

        self.save()
Esempio n. 13
0
    generator.summary()

    save_file = osp.join(osp.abspath(osp.dirname(__file__)), args.saver)
    checkpoint = tf.train.Checkpoint(discriminator=discriminator, generator=generator)


    opti_d = tf.keras.optimizers.Adam(0.0001)
    opti_g = tf.keras.optimizers.Adam(0.0001)
    summary_file = osp.join(osp.abspath(osp.dirname(__file__)), args.logs)
    summary = tf.summary.create_file_writer(os.path.join(summary_file, args.log_file))

    latent_dim = args.l_dim
    batch_size = args.b_size
    epochs = args.epoch

    (train_x, train_y), (test_x, test_y) = load_data()
    train_x = (train_x - 127.0) / 127.0
    test_x = (test_x - 127.0) / 127.0
    train_x, test_x = np.expand_dims(train_x, axis=-1), np.expand_dims(test_x, axis=-1)
    train_y, test_y = np.expand_dims(train_y, axis=-1), np.expand_dims(test_y, axis=-1)
    train_data = tf.data.Dataset.from_tensor_slices((train_x, train_y)).shuffle(1000).batch(batch_size)

    train_d_loss = tf.keras.metrics.Mean(name='train_d_loss')
    train_g_loss = tf.keras.metrics.Mean(name='train_g_loss')

    for itr in range(epochs):
        train_d_loss.reset_states()
        train_g_loss.reset_states()

        for (x, y) in train_data:
    def __init__(self):

        # 返回两个元组
        # x_train: (60000, 784), y_train:(60000, 1)
        (self.x_train, self.y_train), (self.x_test, self.y_test) = fashion_mnist.load_data()