Пример #1
0
def run_learn(pixels, label):
    cnn_dir = os.environ['VIRTUAL_ENV'] + "/mnist_convnet_model"
    mnist_dataset = mnist.load_mnist(train_dir=os.environ['VIRTUAL_ENV'] +
                                     '/MNIST-data')

    train_data = concat(mnist_dataset.train.images, pixels)
    train_labels = concat(mnist_dataset.train.labels, label)

    eval_data = concat(mnist_dataset.test.images, pixels)
    eval_labels = concat(mnist_dataset.test.labels, label)

    estimator = learn.Estimator(model_fn=cnn_model_fn, model_dir=cnn_dir)
    dataset_classifier = learn.SKCompat(estimator)

    tensors_to_log = {"probabilities": "softmax_tensor"}
    logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log,
                                              every_n_iter=500)

    dataset_classifier.fit(x=train_data,
                           y=train_labels,
                           batch_size=128,
                           steps=5000,
                           monitors=[logging_hook])

    metrics = {
        "accuracy":
        learn.MetricSpec(metric_fn=tf.metrics.accuracy,
                         prediction_key="classes")
    }

    eval_results = dataset_classifier.score(x=eval_data,
                                            y=eval_labels,
                                            metrics=metrics)
    return eval_results
Пример #2
0
def mnist(layers, activation="sigmoid", batch_size=128, mode="train"):
    """Mnist classification with a multi-layer perceptron."""

    if activation == "sigmoid":
        activation_op = tf.sigmoid
    elif activation == "relu":
        activation_op = tf.nn.relu
    else:
        raise ValueError("{} activation not supported".format(activation))

    # Data.
    data = mnist_dataset.load_mnist()
    data = getattr(data, mode)
    images = tf.constant(data.images, dtype=tf.float32, name="MNIST_images")
    images = tf.reshape(images, [-1, 28, 28, 1])
    labels = tf.constant(data.labels, dtype=tf.int64, name="MNIST_labels")

    # Network.
    mlp = snt.nets.MLP(list(layers) + [10],
                       activation=activation_op,
                       initializers=_nn_initializers)
    network = snt.Sequential([snt.BatchFlatten(), mlp])

    def build():
        indices = tf.random_uniform([batch_size], 0, data.num_examples,
                                    tf.int64)
        batch_images = tf.gather(images, indices)
        batch_labels = tf.gather(labels, indices)
        output = network(batch_images)
        return _xent_loss(output, batch_labels)

    return build
def mnist(train=True):
  data = mnist_dataset.load_mnist()
  mode = "train" if train else "test"
  data = getattr(data, mode)
  imgs = data.images
  imgs = np.reshape(imgs, newshape=[-1, 28, 28, 1])
  labels = data.labels
  return Dataset(imgs.astype("float32"), labels.astype("int32"))
def mnist_conv(batch_norm=True,
               batch_size=128,
               mode="train"):

  # Data.
  data = mnist_dataset.load_mnist()
  data = getattr(data, mode)
  images = tf.constant(data.images, dtype=tf.float32, name="MNIST_images")
  images = tf.reshape(images, [-1, 28, 28, 1])
  labels = tf.constant(data.labels, dtype=tf.int64, name="MNIST_labels")

  def network(inputs, training=True):

      def _conv_activation(x):
          return tf.nn.max_pool(tf.nn.relu(x),
                                ksize=[1, 2, 2, 1],
                                strides=[1, 2, 2, 1],
                                padding="VALID")
      def conv_layer(inputs, strides, c_h, c_w, output_channels, padding, name):
          n_channels = int(inputs.get_shape()[-1])
          with tf.variable_scope(name) as scope:
              kernel1 = tf.get_variable('weights1',
                                        shape=[c_h, c_w, n_channels, output_channels],
                                        dtype=tf.float32,
                                        initializer=tf.random_normal_initializer(stddev=0.01)
                                        )
              
              biases1 = tf.get_variable('biases1', [output_channels], initializer=tf.constant_initializer(0.0))
          inputs = tf.nn.conv2d(inputs, kernel1, [1, strides, strides, 1], padding)
          inputs = tf.nn.bias_add(inputs, biases1)
          if batch_norm:
              inputs = tf.layers.batch_normalization(inputs, training=training)
          inputs = _conv_activation(inputs)
          return inputs

      inputs = conv_layer(inputs, 1, 3, 3, 16, "VALID", 'conv_layer1')
      inputs = conv_layer(inputs, 1, 5, 5, 32, "VALID", 'conv_layer2')
      inputs = tf.reshape(inputs, [batch_size, -1])
      fc_shape2 = int(inputs.get_shape()[1])
      weights = tf.get_variable("fc_weights",
                                shape=[fc_shape2, 10],
                                dtype=tf.float32,
                                initializer=tf.random_normal_initializer(stddev=0.01))
      bias = tf.get_variable("fc_bias",
                             shape=[10, ],
                             dtype=tf.float32,
                             initializer=tf.constant_initializer(0.0))
      return tf.nn.relu(tf.nn.bias_add(tf.matmul(inputs, weights), bias))

  def build():
    indices = tf.random_uniform([batch_size], 0, data.num_examples, tf.int64)
    batch_images = tf.gather(images, indices)
    batch_labels = tf.gather(labels, indices)
    output = network(batch_images)
    return _xent_loss(output, batch_labels)

  return build
Пример #5
0
def mnist(layers,  # pylint: disable=invalid-name
          activation="relu",
          batch_size=128,
          mode="train"):
    """Mnist classification with a multi-layer perceptron."""

    if activation == "sigmoid":
        activation_op = tf.sigmoid
    elif activation == "relu":
        activation_op = tf.nn.relu
    else:
        raise ValueError("{} activation not supported".format(activation))

    # Data.
    proxy1 = 'http://proxy:8080'
    proxy2 = 'https://proxy:8080'
    os.environ['http_proxy'] = proxy1
    os.environ['HTTP_PROXY'] = proxy1
    os.environ['https_proxy'] = proxy2
    os.environ['HTTPS_PROXY'] = proxy2
    data = mnist_dataset.load_mnist()
    data = getattr(data, mode)
    images = tf.constant(data.images, dtype=tf.float32, name="MNIST_images")
    images = tf.reshape(images, [-1, 28, 28, 1])
    labels = tf.constant(data.labels, dtype=tf.int64, name="MNIST_labels")

    # Network.
    mlp = snt.nets.MLP(list(layers) + [10],
                     activation=activation_op,
                     initializers=_nn_initializers)
    network = snt.Sequential([snt.BatchFlatten(), mlp])

    def build():
        indices = tf.random_uniform([batch_size], 0, data.num_examples, tf.int64)
        batch_images = tf.gather(images, indices)
        batch_labels = tf.gather(labels, indices)
        output = network(batch_images)
        return _xent_loss(output, batch_labels)

    def convex_loss():
        v = tf.get_variable("v", shape=[1, 10], dtype=tf.float32,
                                initializer=tf.random_normal_initializer(stddev=0.01))

        # Non-trainable variables.
        target = tf.get_variable("target", shape=[1, 10], dtype=tf.float32,
                                     initializer=tf.random_normal_initializer(stddev=0.01), trainable=False)

        return tf.reduce_mean(tf.clip_by_value(tf.square(v - target), 0, 10))


    return collections.OrderedDict([('Opt_loss', build), ('Aux_loss', convex_loss)])
Пример #6
0
def get_mnist(images_shape=get_input_shape_ones(), mode="train"):
    """ Returns the details about MNIST dataset,
      the reshaped images and the labels

  Args:
      images_shape: shape of the images
      mode: select iamges for training or testing

  Returns:
      A tuple where the first element contains meta informarmation
      about the dataset, the second element contains the images
      and the third element contains the labels
  """

    data = mnist_dataset.load_mnist()
    data = getattr(data, mode)

    images = np.reshape(data.images, images_shape)
    labels = to_categorical(data.labels, FLAGS.num_classes)
    return data, images, labels
Пример #7
0
# min_after_dequeue is the minimum number elements in the queue
# after a dequeue, which ensures sufficient mixing of elements.
min_after_dequeue = 3000

# If `enqueue_many` is `False`, `tensors` is assumed to represent a
# single example.  An input tensor with shape `[x, y, z]` will be output
# as a tensor with shape `[batch_size, x, y, z]`.
#
# If `enqueue_many` is `True`, `tensors` is assumed to represent a
# batch of examples, where the first dimension is indexed by example,
# and all members of `tensors` should have the same size in the
# first dimension.  If an input tensor has shape `[*, x, y, z]`, the
# output will have shape `[batch_size, x, y, z]`.
enqueue_many = True

data = mnist.load_mnist()
x_train_batch, y_train_batch = tf.train.shuffle_batch(
    tensors=[data.train.images, data.train.labels.astype(np.int32)],
    batch_size=batch_size,
    capacity=capacity,
    min_after_dequeue=min_after_dequeue,
    enqueue_many=enqueue_many,
    num_threads=8)

x_train_batch = tf.cast(x_train_batch, tf.float32)
x_train_batch = tf.reshape(x_train_batch, shape=batch_shape)

y_train_batch = tf.cast(y_train_batch, tf.int32)
y_train_batch = tf.one_hot(y_train_batch, classes)

x_batch_shape = x_train_batch.get_shape().as_list()
def main():
    # user options
    batch_size = 128
    val_in_train = False  # not sure how the validation part works during fit.
    use_model_checkpt = False

    # demo processing
    sess = tf.Session()
    KB.set_session(sess)

    gdev_list = get_available_gpus()
    ngpus = len(gdev_list)
    batch_size = batch_size * ngpus

    data = mnist.load_mnist()
    X_train = data.train.images
    # X_test = data.test.images
    train_samples = X_train.shape[0]  # 60000
    # test_samples = X_test.shape[0]  # 10000
    height_nrows = 28
    width_ncols = 28
    batch_shape = [batch_size, height_nrows, width_ncols, 1]
    epochs = 5
    steps_per_epoch = train_samples / batch_size
    # validations_steps = test_samples / batch_size
    nclasses = 10

    # The capacity variable controls the maximum queue size
    # allowed when prefetching data for training.
    capacity = 10000

    # min_after_dequeue is the minimum number elements in the queue
    # after a dequeue, which ensures sufficient mixing of elements.
    min_after_dequeue = 3000

    # If `enqueue_many` is `False`, `tensors` is assumed to represent a
    # single example.  An input tensor with shape `[x, y, z]` will be output
    # as a tensor with shape `[batch_size, x, y, z]`.
    #
    # If `enqueue_many` is `True`, `tensors` is assumed to represent a
    # batch of examples, where the first dimension is indexed by example,
    # and all members of `tensors` should have the same size in the
    # first dimension.  If an input tensor has shape `[*, x, y, z]`, the
    # output will have shape `[batch_size, x, y, z]`.
    enqueue_many = True

    x_train_batch, y_train_batch = tf.train.shuffle_batch(
        tensors=[data.train.images,
                 data.train.labels.astype(np.int32)],
        batch_size=batch_size,
        capacity=capacity,
        min_after_dequeue=min_after_dequeue,
        enqueue_many=enqueue_many,
        num_threads=8)

    x_train_batch = tf.cast(x_train_batch, tf.float32)
    x_train_batch = tf.reshape(x_train_batch, shape=batch_shape)

    y_train_batch = tf.cast(y_train_batch, tf.int32)
    y_train_batch = tf.one_hot(y_train_batch, nclasses)

    x_train_input = Input(tensor=x_train_batch)

    # x_test_batch, y_test_batch = tf.train.batch(
    #     tensors=[data.test.images, data.test.labels.astype(np.int32)],
    #     batch_size=batch_size,
    #     capacity=capacity,
    #     enqueue_many=enqueue_many,
    #     num_threads=8)

    # I like the non-functional definition of model more.
    # model_init = make_model(x_train_input, nclasses)
    # x_train_out = model_init.output
    # train_model = Model(inputs=[x_train_input], outputs=[x_train_out])

    x_train_out = cnn_layers(x_train_input, nclasses)
    train_model = Model(inputs=[x_train_input], outputs=[x_train_out])
    if ngpus > 1:
        train_model = make_parallel(train_model, gdev_list)

    lr = 2e-3 * ngpus
    train_model.compile(optimizer=RMSprop(lr=lr, decay=1e-5),
                        loss='categorical_crossentropy',
                        metrics=['accuracy'],
                        target_tensors=[y_train_batch])

    if ngpus > 1:
        print_mgpu_modelsummary(train_model)
    else:
        train_model.summary()

    # Callbacks
    if use_model_checkpt:
        mon = 'val_acc' if val_in_train else 'acc'
        checkpoint = ModelCheckpoint('saved_wt.h5',
                                     monitor=mon,
                                     verbose=0,
                                     save_best_only=True,
                                     save_weights_only=True)
        checkpoint = [checkpoint]
    else:
        checkpoint = []

    callbacks = checkpoint
    # Training slower with callback. Multigpu slower with callback during
    # training than 1 GPU. Again, mnist is too trivial of a model and dataset
    # to benchmark or stress GPU compute capabilities. I set up this example
    # to illustrate potential for speedup of multigpu case trying to use mnist
    # as a stressor.
    # It's like comparing a 5 ft race between a person and a truck. A truck is
    # obviously faster than a person but in a 5 ft race the person will likely
    # win due to slower startup for the truck.
    # I will re-implement this with Cifar that should be a better benchmark.

    # Start the queue runners.
    tf.train.start_queue_runners(sess=sess)

    # Fit the model using data from the TFRecord data tensors.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess, coord)

    start_time = time.time()
    train_model.fit(
        # validation_data=(x_test_batch, y_test_batch)
        # if val_in_train else None, # validation data is not used???
        # validations_steps if val_in_train else None,
        # validation_steps=val_in_train,
        steps_per_epoch=steps_per_epoch,
        epochs=epochs,
        callbacks=callbacks)
    elapsed_time = time.time() - start_time
    print('[{}] finished in {} ms'.format('TRAINING',
                                          int(elapsed_time * 1000)))

    if not checkpoint:  # empty list
        train_model.save_weights('./saved_wt.h5')

    # Clean up the TF session.
    coord.request_stop()
    coord.join(threads)

    KB.clear_session()

    # Second Session. Demonstrate that the model works and is independent of
    # the TFRecord pipeline, and to test loading trained model without tensors.
    x_test = np.reshape(data.validation.images,
                        (data.validation.images.shape[0], 28, 28, 1))
    y_test = data.validation.labels
    x_test_inp = KL.Input(shape=(x_test.shape[1:]))
    test_out = cnn_layers(x_test_inp, nclasses)
    test_model = Model(inputs=x_test_inp, outputs=test_out)

    test_model.load_weights('saved_wt.h5')
    test_model.compile(optimizer='rmsprop',
                       loss='categorical_crossentropy',
                       metrics=['accuracy'])
    test_model.summary()

    loss, acc = test_model.evaluate(x_test, to_categorical(y_test))
    print('\nTest loss: {0}'.format(loss))
    print('\nTest accuracy: {0}'.format(acc))
Пример #9
0
correct = True

for m in must:
    if m is None:
        correct = False
        break

#Modify message here
if correct is False:
    sys.exit(
        "Usage : python " + sys.argv[0] +
        " --alg <HMM algorithm> --num_hmms <Number of HMMs> --num_states <Number of states in each HMM> --frac_vertical <Number of HMMs to look at column information> --batch_size <mini-batch size> --num_epochs <Number of epochs to do training> [--init_file <optional file to initialize from> --saver_prefix [prefix of file in which session is saved]]"
    )

#Prepare datasets
datasets = mnist.load_mnist()
training_vectors = datasets[0].images
training_labels = datasets[0].labels
training_vectors = training_vectors.reshape(
    (training_vectors.shape[0], 28, 28))
training_vectors = np.array([v.T for v in training_vectors], dtype=np.float64)
training_labels = one_hot(training_labels)
test_vectors = datasets[2].images
test_labels = datasets[2].labels
test_vectors = np.array([v.T for v in test_vectors], dtype=np.float64)
test_labels = one_hot(test_labels)

Net = HMM.HMM_NN(num_dims=28, num_states=num_states, num_hmms=num_hmms)
#Train
if num_epochs != 0:
    Net.run_batch(training_vectors,
Пример #10
0
    def __init__(self, args):
        args['var_count'] = 4
        self.full = args['full']
        super(Mnist, self).__init__(args=args)
        self.conv = False if 'conv' not in args else args['conv']

        def get_data(data, mode='train'):
            mode_data = getattr(data, mode)
            images = tf.constant(mode_data.images,
                                 dtype=tf.float32,
                                 name="MNIST_images_" + mode)
            if self.conv:
                images = tf.reshape(images, [-1, 28, 28, 1])
            if self.allow_gradients_of_gradients:
                labels = tf.one_hot(mode_data.labels,
                                    10,
                                    name="MNIST_labels_" + mode)
            else:
                labels = tf.constant(mode_data.labels,
                                     dtype=tf.int64,
                                     name="MNIST_labels_" + mode)
            return images, labels

        data = mnist_dataset.load_mnist()
        self.training_data, self.test_data, self.validation_data = dict(
        ), dict(), dict()
        self.training_data['images'], self.training_data['labels'] = get_data(
            data, 'train')
        self.test_data['images'], self.test_data['labels'] = get_data(
            data, 'test')
        self.validation_data['images'], self.validation_data[
            'labels'] = get_data(data, 'validation')
        with tf.variable_scope(self.variable_scope):
            with tf.variable_scope('network_variables'):
                if self.conv:
                    if self.full:
                        filters = 32
                        f1 = 1024
                    else:
                        filters = 16
                        f1 = 512
                    self.create_variable('w_1', dims=[5, 5, 1, filters])
                    self.create_variable('b_1', dims=[filters])

                    self.create_variable('w_2',
                                         dims=[5, 5, filters, filters * 2])
                    self.create_variable('b_2', dims=[filters * 2])

                    self.create_variable('w_3', dims=[7 * 7 * filters * 2, f1])
                    self.create_variable('b_3', dims=[f1])

                    self.create_variable('w_out', dims=[f1, 10])
                    self.create_variable('b_out', dims=[10])
                else:
                    if self.full:
                        f1 = 40
                    else:
                        f1 = 20
                    self.create_variable(
                        'w_1',
                        dims=[
                            self.training_data['images'].get_shape()[1].value,
                            f1
                        ])
                    self.create_variable('b_1', dims=[1, f1])

                    # self.create_variable('w_2', dims=[20, 20])
                    # self.create_variable('b_2', dims=[20])
                    #
                    # self.create_variable('w_3', dims=[20, 20])
                    # self.create_variable('b_3', dims=[20])

                    self.create_variable('w_out', dims=[f1, 10])
                    self.create_variable('b_out', dims=[1, 10])
                self.weight_norm_loss = 0
                for variable in self.variables:
                    self.weight_norm_loss += tf.nn.l2_loss(variable)
        self.end_init()
Пример #11
0
#!/usr/bin/env python
#coding=utf-8

import core

from tensorflow.contrib.learn.python.learn.datasets.mnist import load_mnist

load_mnist()

x_data = core.np.float32(core.np.random.rand(2, 100))
y_data = core.np.dot([0.100, 0.200], x_data) + 0.300

b = core.tf.Variable(core.tf.zeros([1]))
w = core.tf.Variable(core.tf.random_uniform([1, 2], -1.0, 1.0))
y = core.tf.matmul(w, x_data) + b

loss = core.tf.reduce_mean(core.tf.square(y - y_data))
optimazer = core.tf.train.GradientDescentOptimizer(0.5)
train = optimazer.minimize(loss)

init = core.tf.initialize_all_variables()
sess = core.tf.Session()
sess.run(init)

for step in range(0, 201):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(w), sess.run(b))
def main():
    # user options
    batch_size = 128
    val_in_train = False  # not sure how the validation part works during fit.
    use_model_checkpt = False

    # demo processing
    sess = tf.Session()
    KB.set_session(sess)

    gdev_list = get_available_gpus()
    ngpus = len(gdev_list)
    batch_size = batch_size * ngpus

    data = mnist.load_mnist()
    X_train = data.train.images
    # X_test = data.test.images
    train_samples = X_train.shape[0]  # 60000
    # test_samples = X_test.shape[0]  # 10000
    height_nrows = 28
    width_ncols = 28
    batch_shape = [batch_size, height_nrows, width_ncols, 1]
    epochs = 5
    steps_per_epoch = train_samples // batch_size
    # validations_steps = test_samples / batch_size
    nclasses = 10

    # The capacity variable controls the maximum queue size
    # allowed when prefetching data for training.
    capacity = 10000

    # min_after_dequeue is the minimum number elements in the queue
    # after a dequeue, which ensures sufficient mixing of elements.
    min_after_dequeue = 3000

    # If `enqueue_many` is `False`, `tensors` is assumed to represent a
    # single example.  An input tensor with shape `[x, y, z]` will be output
    # as a tensor with shape `[batch_size, x, y, z]`.
    #
    # If `enqueue_many` is `True`, `tensors` is assumed to represent a
    # batch of examples, where the first dimension is indexed by example,
    # and all members of `tensors` should have the same size in the
    # first dimension.  If an input tensor has shape `[*, x, y, z]`, the
    # output will have shape `[batch_size, x, y, z]`.
    enqueue_many = True

    x_train_batch, y_train_batch = tf.train.shuffle_batch(
        tensors=[data.train.images, data.train.labels.astype(np.int32)],
        batch_size=batch_size,
        capacity=capacity,
        min_after_dequeue=min_after_dequeue,
        enqueue_many=enqueue_many,
        num_threads=8)

    x_train_batch = tf.cast(x_train_batch, tf.float32)
    x_train_batch = tf.reshape(x_train_batch, shape=batch_shape)

    y_train_batch = tf.cast(y_train_batch, tf.int32)
    y_train_batch = tf.one_hot(y_train_batch, nclasses)

    x_train_input = Input(tensor=x_train_batch)

    # x_test_batch, y_test_batch = tf.train.batch(
    #     tensors=[data.test.images, data.test.labels.astype(np.int32)],
    #     batch_size=batch_size,
    #     capacity=capacity,
    #     enqueue_many=enqueue_many,
    #     num_threads=8)

    # I like the non-functional definition of model more.
    # model_init = make_model(x_train_input, nclasses)
    # x_train_out = model_init.output
    # train_model = Model(inputs=[x_train_input], outputs=[x_train_out])

    x_train_out = cnn_layers(x_train_input, nclasses)
    train_model = Model(inputs=[x_train_input], outputs=[x_train_out])
    if ngpus > 1:
        train_model = make_parallel(train_model, gdev_list)

    lr = 2e-3 * ngpus
    train_model.compile(optimizer=RMSprop(lr=lr, decay=1e-5),
                        loss='categorical_crossentropy',
                        metrics=['accuracy'],
                        target_tensors=[y_train_batch])

    if ngpus > 1:
        print_mgpu_modelsummary(train_model)
    else:
        train_model.summary()

    # Callbacks
    if use_model_checkpt:
        mon = 'val_acc' if val_in_train else 'acc'
        checkpoint = ModelCheckpoint(
            'saved_wt.h5', monitor=mon, verbose=0,
            save_best_only=True,
            save_weights_only=True)
        checkpoint = [checkpoint]
    else:
        checkpoint = []

    callbacks = checkpoint
    # Training slower with callback. Multigpu slower with callback during
    # training than 1 GPU. Again, mnist is too trivial of a model and dataset
    # to benchmark or stress GPU compute capabilities. I set up this example
    # to illustrate potential for speedup of multigpu case trying to use mnist
    # as a stressor.
    # It's like comparing a 5 ft race between a person and a truck. A truck is
    # obviously faster than a person but in a 5 ft race the person will likely
    # win due to slower startup for the truck.
    # I will re-implement this with Cifar that should be a better benchmark.

    # Start the queue runners.
    tf.train.start_queue_runners(sess=sess)

    # Fit the model using data from the TFRecord data tensors.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess, coord)

    start_time = time.time()
    train_model.fit(
        # validation_data=(x_test_batch, y_test_batch)
        # if val_in_train else None, # validation data is not used???
        # validations_steps if val_in_train else None,
        # validation_steps=val_in_train,
        steps_per_epoch=steps_per_epoch,
        epochs=epochs,
        callbacks=callbacks)
    elapsed_time = time.time() - start_time
    print('[{}] finished in {} s'.format('TRAINING', round(elapsed_time, 3)))

    if not checkpoint:  # empty list
        train_model.save_weights('./saved_wt.h5')

    # Clean up the TF session.
    coord.request_stop()
    coord.join(threads)

    KB.clear_session()

    # Second Session. Demonstrate that the model works and is independent of
    # the TFRecord pipeline, and to test loading trained model without tensors.
    x_test = np.reshape(data.validation.images,
                        (data.validation.images.shape[0], 28, 28, 1))
    y_test = data.validation.labels
    x_test_inp = KL.Input(shape=(x_test.shape[1:]))
    test_out = cnn_layers(x_test_inp, nclasses)
    test_model = Model(inputs=x_test_inp, outputs=test_out)

    test_model.load_weights('saved_wt.h5')
    test_model.compile(optimizer='rmsprop',
                       loss='categorical_crossentropy',
                       metrics=['accuracy'])
    test_model.summary()

    loss, acc = test_model.evaluate(x_test, to_categorical(y_test))
    print('\nTest loss: {0}'.format(loss))
    print('\nTest accuracy: {0}'.format(acc))
Пример #13
0
def mnist_conv(
        activation="relu",  # pylint: disable=invalid-name
        batch_norm=True,
        batch_size=128,
        mode="train"):
    """Mnist classification with a multi-layer perceptron."""

    if activation == "sigmoid":
        activation_op = tf.sigmoid
    elif activation == "relu":
        activation_op = tf.nn.relu
    else:
        raise ValueError("{} activation not supported".format(activation))

    # Data.
    data = mnist_dataset.load_mnist()
    data = getattr(data, mode)
    images = tf.constant(data.images, dtype=tf.float32, name="MNIST_images")
    images = tf.reshape(images, [-1, 28, 28, 1])
    labels = tf.constant(data.labels, dtype=tf.int64, name="MNIST_labels")
    if mode == 'train':
        is_training = True
    elif mode == 'test':
        is_training = False
    # Network.
    # mlp = snt.nets.MLP(list(layers) + [10],
    #                    activation=activation_op,
    #                    initializers=_nn_initializers)
    # network = snt.Sequential([snt.BatchFlatten(), mlp])

    def network(inputs, training=is_training):
        # pdb.set_trace()
        def _conv_activation(x):  # pylint: disable=invalid-name
            return tf.nn.max_pool(tf.nn.relu(x),
                                  ksize=[1, 2, 2, 1],
                                  strides=[1, 2, 2, 1],
                                  padding="VALID")

        def conv_layer(inputs, strides, c_h, c_w, output_channels, padding,
                       name):
            n_channels = int(inputs.get_shape()[-1])
            with tf.variable_scope(name) as scope:
                kernel1 = tf.get_variable(
                    'weights1',
                    shape=[c_h, c_w, n_channels, output_channels],
                    dtype=tf.float32,
                    initializer=tf.random_normal_initializer(stddev=0.01))

                biases1 = tf.get_variable(
                    'biases1', [output_channels],
                    initializer=tf.constant_initializer(0.0))
            inputs = tf.nn.conv2d(inputs, kernel1, [1, strides, strides, 1],
                                  padding)
            inputs = tf.nn.bias_add(inputs, biases1)
            if batch_norm:
                inputs = tf.layers.batch_normalization(inputs,
                                                       training=training)
            inputs = _conv_activation(inputs)
            return inputs

        if batch_norm:
            linear_activation = lambda x: tf.nn.relu(
                tf.layers.batch_normalization(x, training=training))
        else:
            linear_activation = tf.nn.relu
        # pdb.set_trace()
        # print(inputs.shape)
        inputs = conv_layer(inputs, 1, 3, 3, 16, "VALID", 'conv_layer1')
        # print(inputs.shape)
        inputs = conv_layer(inputs, 1, 5, 5, 32, "VALID", 'conv_layer2')
        # print(inputs.shape)
        # inputs = linear_activation(inputs)
        inputs = tf.reshape(inputs, [batch_size, -1])
        # print(inputs.shape)
        fc_shape1 = int(inputs.get_shape()[0])
        fc_shape2 = int(inputs.get_shape()[1])
        weights = tf.get_variable(
            "fc_weights",
            shape=[fc_shape2, 10],
            dtype=tf.float32,
            initializer=tf.random_normal_initializer(stddev=0.01))
        # print(weights.shape)
        bias = tf.get_variable("fc_bias",
                               shape=[
                                   10,
                               ],
                               dtype=tf.float32,
                               initializer=tf.constant_initializer(0.0))
        # print(bias.shape)
        return tf.nn.relu(tf.nn.bias_add(tf.matmul(inputs, weights), bias))

    def build():
        indices = tf.random_uniform([batch_size], 0, data.num_examples,
                                    tf.int64)
        batch_images = tf.gather(images, indices)
        batch_labels = tf.gather(labels, indices)
        output = network(batch_images)
        return _xent_loss(output, batch_labels)

    return build