Ejemplo n.º 1
0
 def testFunctionalConv2DTransposeNoReuse(self):
     height, width = 7, 9
     images = random_ops.random_uniform((5, height, width, 3), seed=1)
     conv_layers.conv2d_transpose(images, 32, [3, 3])
     self.assertEqual(len(variables.trainable_variables()), 2)
     conv_layers.conv2d_transpose(images, 32, [3, 3])
     self.assertEqual(len(variables.trainable_variables()), 4)
Ejemplo n.º 2
0
 def testFunctionalConv2DTransposeNoReuse(self):
   height, width = 7, 9
   images = random_ops.random_uniform((5, height, width, 3), seed=1)
   conv_layers.conv2d_transpose(images, 32, [3, 3])
   self.assertEqual(len(variables.trainable_variables()), 2)
   conv_layers.conv2d_transpose(images, 32, [3, 3])
   self.assertEqual(len(variables.trainable_variables()), 4)
Ejemplo n.º 3
0
  def testInvalidKernelSize(self):
    height, width = 7, 9
    images = random_ops.random_uniform((5, height, width, 3), seed=1)
    with self.assertRaisesRegexp(ValueError, 'kernel_size'):
      conv_layers.conv2d_transpose(images, 32, (1, 2, 3))

    with self.assertRaisesRegexp(ValueError, 'kernel_size'):
      conv_layers.conv2d_transpose(images, 32, None)
Ejemplo n.º 4
0
  def testInvalidStrides(self):
    height, width = 7, 9
    images = random_ops.random_uniform((5, height, width, 3), seed=1)
    with self.assertRaisesRegexp(ValueError, 'strides'):
      conv_layers.conv2d_transpose(images, 32, 3, strides=(1, 2, 3))

    with self.assertRaisesRegexp(ValueError, 'strides'):
      conv_layers.conv2d_transpose(images, 32, 3, strides=None)
Ejemplo n.º 5
0
    def testInvalidKernelSize(self):
        height, width = 7, 9
        images = random_ops.random_uniform((5, height, width, 3), seed=1)
        with self.assertRaisesRegexp(ValueError, 'kernel_size'):
            conv_layers.conv2d_transpose(images, 32, (1, 2, 3))

        with self.assertRaisesRegexp(ValueError, 'kernel_size'):
            conv_layers.conv2d_transpose(images, 32, None)
Ejemplo n.º 6
0
    def testInvalidStrides(self):
        height, width = 7, 9
        images = random_ops.random_uniform((5, height, width, 3), seed=1)
        with self.assertRaisesRegexp(ValueError, 'strides'):
            conv_layers.conv2d_transpose(images, 32, 3, strides=(1, 2, 3))

        with self.assertRaisesRegexp(ValueError, 'strides'):
            conv_layers.conv2d_transpose(images, 32, 3, strides=None)
Ejemplo n.º 7
0
 def testFunctionalConv2DTransposeReuseFromScope(self):
   with variable_scope.variable_scope('scope'):
     height, width = 7, 9
     images = random_ops.random_uniform((5, height, width, 3), seed=1)
     conv_layers.conv2d_transpose(images, 32, [3, 3], name='deconv1')
     self.assertEqual(len(variables.trainable_variables()), 2)
   with variable_scope.variable_scope('scope', reuse=True):
     conv_layers.conv2d_transpose(images, 32, [3, 3], name='deconv1')
     self.assertEqual(len(variables.trainable_variables()), 2)
Ejemplo n.º 8
0
 def testFunctionalConv2DTransposeNoReuse(self):
   height, width = 7, 9
   images = random_ops.random_uniform((5, height, width, 3), seed=1)
   conv_layers.conv2d_transpose(images, 32, [3, 3])
   self.assertEqual(
       len(ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)), 2)
   conv_layers.conv2d_transpose(images, 32, [3, 3])
   self.assertEqual(
       len(ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES)), 4)
Ejemplo n.º 9
0
 def testFunctionalConv2DTransposeReuseFromScope(self):
     with variable_scope.variable_scope('scope'):
         height, width = 7, 9
         images = random_ops.random_uniform((5, height, width, 3), seed=1)
         conv_layers.conv2d_transpose(images, 32, [3, 3], name='deconv1')
         self.assertEqual(len(variables.trainable_variables()), 2)
     with variable_scope.variable_scope('scope', reuse=True):
         conv_layers.conv2d_transpose(images, 32, [3, 3], name='deconv1')
         self.assertEqual(len(variables.trainable_variables()), 2)
Ejemplo n.º 10
0
 def testConv2DTransposeFloat16(self):
   height, width = 7, 9
   images = random_ops.random_uniform((5, height, width, 4), dtype='float16')
   output = conv_layers.conv2d_transpose(images, 32, [3, 3],
                                         activation=nn_ops.relu)
   self.assertListEqual(output.get_shape().as_list(),
                        [5, height + 2, width + 2, 32])
Ejemplo n.º 11
0
 def testConv2DTransposeFloat16(self):
   height, width = 7, 9
   images = random_ops.random_uniform((5, height, width, 4), dtype='float16')
   output = conv_layers.conv2d_transpose(images, 32, [3, 3],
                                         activation=nn_ops.relu)
   self.assertListEqual(output.get_shape().as_list(),
                        [5, height + 2, width + 2, 32])
Ejemplo n.º 12
0
 def testFunctionalConv2DTransposeInitializerFromScope(self):
   with self.test_session() as sess:
     with variable_scope.variable_scope(
         'scope', initializer=init_ops.ones_initializer()):
       height, width = 7, 9
       images = random_ops.random_uniform((5, height, width, 3), seed=1)
       conv_layers.conv2d_transpose(images, 32, [3, 3], name='deconv1')
       weights = variables.trainable_variables()
       # Check the names of weights in order.
       self.assertTrue('kernel' in weights[0].name)
       self.assertTrue('bias' in weights[1].name)
       sess.run(variables.global_variables_initializer())
       weights = sess.run(weights)
       # Check that the kernel weights got initialized to ones (from scope)
       self.assertAllClose(weights[0], np.ones((3, 3, 32, 3)))
       # Check that the bias still got initialized to zeros.
       self.assertAllClose(weights[1], np.zeros((32)))
Ejemplo n.º 13
0
 def testFunctionalConv2DTransposeInitializerFromScope(self):
   with self.test_session() as sess:
     with variable_scope.variable_scope(
         'scope', initializer=init_ops.ones_initializer()):
       height, width = 7, 9
       images = random_ops.random_uniform((5, height, width, 3), seed=1)
       conv_layers.conv2d_transpose(images, 32, [3, 3], name='deconv1')
       weights = variables.trainable_variables()
       # Check the names of weights in order.
       self.assertTrue('kernel' in weights[0].name)
       self.assertTrue('bias' in weights[1].name)
       sess.run(variables.global_variables_initializer())
       weights = sess.run(weights)
       # Check that the kernel weights got initialized to ones (from scope)
       self.assertAllClose(weights[0], np.ones((3, 3, 32, 3)))
       # Check that the bias still got initialized to zeros.
       self.assertAllClose(weights[1], np.zeros((32)))
Ejemplo n.º 14
0
    def _upsample(self, inputs, k):
        x = inputs
        for i in reversed(range(0, k)):
            x = conv2d_transpose(inputs=x, filters=self.num_classes * 2 ** i, kernel_size=4, strides=2, padding='same')
            x = dropout(x, rate=self.dropout_prob)
            x = self._add_common_layers(x)

        return x
Ejemplo n.º 15
0
def build_generator(noise, caption, reuse=False):
    with tf.variable_scope("generator") as scope:
        if reuse:
            tf.get_variable_scope().reuse_variables()

        #
        t_noise = noise
        t_txt = caption

        t_txt = dense(inputs=t_txt, units=128, activation=my_leaky_relu)

        t_input = tf.concat(values=[t_noise, t_txt], axis=1)

        t0 = dense(inputs=t_input, units=128 * 8 * 4 * 4)

        t0 = batch_normalization(inputs=t0)

        t0 = tf.reshape(tensor=t0, shape=[-1, 4, 4, 128 * 8])

        t = conv2d(inputs=t0,
                   filters=256,
                   kernel_size=[1, 1],
                   strides=[1, 1],
                   padding="valid",
                   activation=my_leaky_relu)

        t = batch_normalization(inputs=t)

        t = conv2d(inputs=t,
                   filters=256,
                   kernel_size=[3, 3],
                   strides=[1, 1],
                   padding="same",
                   activation=my_leaky_relu)

        t = batch_normalization(inputs=t)

        t = conv2d(inputs=t,
                   filters=128 * 8,
                   kernel_size=[3, 3],
                   strides=[1, 1],
                   padding="same")

        t = batch_normalization(inputs=t)

        t1 = tf.add(t0, t)

        t2 = conv2d_transpose(inputs=t1,
                              filters=128 * 4,
                              kernel_size=[3, 3],
                              strides=[2, 2],
                              padding="same")

        t2 = batch_normalization(inputs=t2)

        t = conv2d(inputs=t2,
                   filters=128,
                   kernel_size=[1, 1],
                   strides=[1, 1],
                   padding="valid",
                   activation=my_leaky_relu)

        t = batch_normalization(inputs=t)

        t = conv2d(inputs=t,
                   filters=128,
                   kernel_size=[3, 3],
                   strides=[1, 1],
                   padding="same",
                   activation=my_leaky_relu)

        t = batch_normalization(inputs=t)

        t = conv2d(inputs=t,
                   filters=128 * 4,
                   kernel_size=[3, 3],
                   strides=[1, 1],
                   padding="same")

        t = batch_normalization(inputs=t)

        t3 = tf.add(t2, t)

        t4 = conv2d_transpose(inputs=t3,
                              filters=128 * 2,
                              kernel_size=[3, 3],
                              strides=[2, 2],
                              padding="same",
                              activation=my_leaky_relu)

        t4 = batch_normalization(inputs=t4)

        t5 = conv2d_transpose(inputs=t4,
                              filters=128,
                              kernel_size=[3, 3],
                              strides=[2, 2],
                              padding="same",
                              activation=my_leaky_relu)

        t5 = batch_normalization(inputs=t5)

        t_output = conv2d_transpose(inputs=t5,
                                    filters=3,
                                    kernel_size=[3, 3],
                                    strides=[2, 2],
                                    padding="same",
                                    activation=tf.tanh)

        print("\nGenerator Output Shape: {}".format(t_output.shape))
        return t_output
Ejemplo n.º 16
0
    def __init__(self,
                 in_shape,
                 lr=0.001,
                 embedding_dim=50,
                 num_projections=20):
        '''
        classes:  List of class names or integers corrisponding to each class being classified
                  by the network. ie: ['left', 'straight', 'right'] or [0, 1, 2]
        '''
        # number of elements in the embedding vector
        self.embedding_dim = embedding_dim
        # number of test embeddings to project in tensorboard
        self.num_projections = num_projections
        # Define model
        tf.reset_default_graph()
        self.x = tf.placeholder(tf.float32,
                                shape=[
                                    None,
                                ] + in_shape,
                                name="x")
        self.y = tf.placeholder(tf.float32,
                                shape=[
                                    None,
                                ] + in_shape,
                                name="y")

        self._training = tf.placeholder(tf.bool)
        self.training = tf.get_variable("training",
                                        dtype=tf.bool,
                                        initializer=True,
                                        trainable=False)
        self.set_training = self.training.assign(self._training)

        self._beta = tf.placeholder(dtype=tf.float32)
        self.beta = tf.get_variable("beta",
                                    dtype=tf.float32,
                                    initializer=0.,
                                    trainable=False)
        self.update_beta = self.beta.assign(self._beta)

        self._embeddings = tf.placeholder(tf.float32,
                                          shape=[None, self.embedding_dim])
        self.embeddings = tf.get_variable(
            "embeddings",
            dtype=tf.float32,
            shape=[self.num_projections, self.embedding_dim],
            initializer=tf.zeros_initializer(),
            trainable=False)
        self.update_embeddings = self.embeddings.assign(self._embeddings)

        paddings = tf.constant([[0, 0], [4, 4], [0, 0], [0, 0]])
        relu = tf.nn.relu
        sigmoid = tf.nn.sigmoid
        with tf.name_scope("encoder"):
            # Padding invector so reconstruction returns to the correct size.
            #x_padded = tf.pad(self.x, paddings, "SYMMETRIC")
            # Encoder            in     num   shape  stride   pad
            enc = conv2d(self.x,
                         32, (5, 5), (2, 2),
                         "valid",
                         activation=relu,
                         kernel_initializer=xavier())  # 59x79
            print(f"enc1: {np.shape(enc)}")
            enc = conv2d(enc,
                         62, (5, 5), (2, 2),
                         "valid",
                         activation=relu,
                         kernel_initializer=xavier())  # 28x38
            print(f"enc2: {np.shape(enc)}")
            enc = conv2d(enc,
                         128, (4, 4), (2, 2),
                         "valid",
                         activation=relu,
                         kernel_initializer=xavier())  # 13x18
            print(f"enc3: {np.shape(enc)}")
            enc = conv2d(enc,
                         256, (4, 4), (2, 2),
                         "valid",
                         activation=relu,
                         kernel_initializer=xavier())  # 5x8
            print(f"enc4: {np.shape(enc)}")
            enc = flatten(enc)

        with tf.name_scope("sampling"):
            # VAE sampling
            '''
            Note: exp(log(log_sigma / 2)) = sigma
            '''
            self.mu = dense(enc,
                            self.embedding_dim,
                            activation=None,
                            kernel_initializer=xavier(),
                            name="mu")
            self.log_sigma = dense(enc,
                                   self.embedding_dim,
                                   activation=None,
                                   kernel_initializer=xavier(),
                                   name="log_sigma")
            eps = tf.random_normal(shape=tf.shape(self.mu),
                                   mean=0.0,
                                   stddev=1.0,
                                   dtype=tf.float32,
                                   name="eps")
            self.noisy_sigma = tf.exp(self.log_sigma) * eps
            self.z = tf.add(self.mu, self.noisy_sigma, name="z")

            #tf.summary.histogram("z", self.z)
            #tf.summary.histogram("log_sigma", self.log_sigma)
            #tf.summary.histogram("mu", self.mu)
            #tf.summary.histogram("eps", eps)

        with tf.name_scope("decoder"):
            # Decoder    in          num
            dec = dense(self.z, (256 * 5 * 7),
                        activation=relu,
                        kernel_initializer=xavier())
            print(f"dec4: {np.shape(dec)}")
            dec = tf.reshape(dec, (-1, 5, 7, 256))
            print(f"dec3: {np.shape(dec)}")
            #                        in num  shape  stride   pad
            dec = conv2d_transpose(dec,
                                   128, (4, 4), (2, 2),
                                   "valid",
                                   activation=relu)
            print(f"dec2: {np.shape(dec)}")
            dec = conv2d_transpose(dec,
                                   64, (4, 4), (2, 2),
                                   "valid",
                                   activation=relu)
            print(f"dec1: {np.shape(dec)}")
            dec = conv2d_transpose(dec,
                                   32, (6, 6), (2, 2),
                                   "valid",
                                   activation=relu)
            print(f"dec0: {np.shape(dec)}")
            self.dec = conv2d_transpose(dec,
                                        3, (10, 18), (2, 2),
                                        "valid",
                                        activation=sigmoid,
                                        name="reconstruction")
            print(f"out: {np.shape(self.dec)}")
#            self.dec  = relu(dec7, name="reconstruction")

        with tf.name_scope("loss"):
            # # VAE Loss
            # 1. Reconstruction loss: How far did we get from the actual image?
            #y_padded = tf.pad(self.y, paddings, "SYMMETRIC")
            self.rec_loss = tf.reduce_sum(tf.square(self.y - self.dec),
                                          axis=[1, 2, 3])
            # Cross entropy loss from:
            # https://stats.stackexchange.com/questions/332179/
            #         how-to-weight-kld-loss-vs-reconstruction-
            #         loss-in-variational-auto-encod
            # Must have self.dec activation as sigmoid
            #            clipped = tf.clip_by_value(self.dec, 1e-8, 1-1e-8)
            #            self.rec_loss = -tf.reduce_sum(self.y * tf.log(clipped)
            #                                      + (1-self.y) * tf.log(1-clipped), axis=[1,2,3])

            # 2. KL-Divergence: How far from the distribution 'z' is sampled
            #                   from the desired zero mean unit variance?
            self.kl_loss = 0.5 * tf.reduce_sum(
                tf.square(self.mu) +
                (tf.exp(2 * self.log_sigma)) - 2 * self.log_sigma - 1,
                axis=1)
            self.loss = tf.reduce_mean(self.rec_loss +
                                       self.kl_loss * self.beta,
                                       name='total_loss')

            tf.summary.scalar("total_loss", self.loss)
            tf.summary.scalar("kl_loss", tf.reduce_mean(self.kl_loss))
            tf.summary.scalar("rec_loss", tf.reduce_mean(self.rec_loss))
            tf.summary.scalar("beta", self.beta)

        update_tensor_dict(INPUTS, IMAGE_INPUT, self.x)
        update_tensor_dict(OUTPUTS, EMBEDDING, self.z)
        update_tensor_dict(OUTPUTS, RECONSTRUCTION, self.dec)

        optimizer = tf.train.AdamOptimizer(learning_rate=lr)
        self.train_step = optimizer.minimize(self.loss, name="train_step")

        self.init_vars = tf.global_variables_initializer()
        self.saver = tf.train.Saver()
Ejemplo n.º 17
0
 def testInvalidDataFormat(self):
   height, width = 7, 9
   images = random_ops.random_uniform((5, height, width, 3), seed=1)
   with self.assertRaisesRegexp(ValueError, 'data_format'):
     conv_layers.conv2d_transpose(images, 32, 3, data_format='invalid')
Ejemplo n.º 18
0
    def __init__(self,
                 in_shape,
                 lr=0.001,
                 embedding_dim=50,
                 num_projections=20):
        '''
        classes:  List of class names or integers corrisponding to each class being classified
                  by the network. ie: ['left', 'straight', 'right'] or [0, 1, 2]
        '''
        # number of elements in the embedding vector
        self.embedding_dim = embedding_dim
        # number of test embeddings to project in tensorboard
        self.num_projections = num_projections
        # Define model
        tf.reset_default_graph()
        self.x = tf.placeholder(tf.float32,
                                shape=[
                                    None,
                                ] + in_shape,
                                name="x")
        self.y = tf.placeholder(tf.float32,
                                shape=[
                                    None,
                                ] + in_shape,
                                name="y")

        self._training = tf.placeholder(tf.bool)
        self.training = tf.get_variable("training",
                                        dtype=tf.bool,
                                        initializer=True,
                                        trainable=False)
        self.set_training = self.training.assign(self._training)

        self._embeddings = tf.placeholder(tf.float32,
                                          shape=[None, self.embedding_dim])
        self.embeddings = tf.get_variable(
            "embeddings",
            dtype=tf.float32,
            shape=[self.num_projections, self.embedding_dim],
            initializer=tf.zeros_initializer(),
            trainable=False)
        self.update_embeddings = self.embeddings.assign(self._embeddings)

        paddings = tf.constant([[0, 0], [4, 4], [0, 0], [0, 0]])
        relu = tf.nn.relu
        sigmoid = tf.nn.sigmoid
        with tf.name_scope("encoder"):
            # Padding invector so reconstruction returns to the correct size.
            x_padded = tf.pad(self.x, paddings, "SYMMETRIC")
            # Encoder            in     num   shape  stride   pad
            enc1 = conv2d(x_padded,
                          24, (5, 5), (2, 2),
                          "same",
                          activation=relu,
                          kernel_initializer=xavier(),
                          name="enc1")  # 64, 80,24
            enc2 = conv2d(enc1,
                          32, (5, 5), (2, 2),
                          "same",
                          activation=relu,
                          kernel_initializer=xavier(),
                          name="enc2")  # 32, 40,32
            enc3 = conv2d(enc2,
                          64, (5, 5), (2, 2),
                          "same",
                          activation=relu,
                          kernel_initializer=xavier(),
                          name="enc3")  # 16, 20,64
            enc4 = conv2d(enc3,
                          64, (3, 3), (2, 2),
                          "same",
                          activation=relu,
                          kernel_initializer=xavier(),
                          name="enc4")  #  8, 10,64
            enc5 = conv2d(enc4,
                          64, (3, 3), (1, 1),
                          "same",
                          activation=relu,
                          kernel_initializer=xavier(),
                          name="enc5")  #  8, 10,64
            enc5f = flatten(enc5)
            #                     in   num
            enc6 = dense(enc5f,
                         100,
                         activation=relu,
                         kernel_initializer=xavier(),
                         name="enc6")
            enc6d = dropout(enc6,
                            rate=0.1,
                            training=self.training,
                            name="enc6d")

        with tf.name_scope("embedding"):
            # Autoencoder embedding
            self.z = dense(enc6d,
                           self.embedding_dim,
                           activation=relu,
                           kernel_initializer=xavier(),
                           name="z")
            tf.summary.histogram("z", self.z)

        with tf.name_scope("decoder"):
            # Decoder    in          num
            dec1 = dense(self.z,
                         100,
                         activation=relu,
                         kernel_initializer=xavier(),
                         name="dec1")
            dec2 = dense(dec1, (8 * 10 * 64),
                         activation=relu,
                         kernel_initializer=xavier(),
                         name="dec2")
            dec2r = tf.reshape(dec2, (-1, 8, 10, 64))
            #                        in num  shape  stride   pad
            dec3 = conv2d_transpose(dec2r,
                                    64, (3, 3), (1, 1),
                                    "same",
                                    activation=relu,
                                    name="dec3")
            dec4 = conv2d_transpose(dec3,
                                    64, (3, 3), (2, 2),
                                    "same",
                                    activation=relu,
                                    name="dec4")
            dec5 = conv2d_transpose(dec4,
                                    32, (5, 5), (2, 2),
                                    "same",
                                    activation=relu,
                                    name="dec5")
            dec6 = conv2d_transpose(dec5,
                                    24, (5, 5), (2, 2),
                                    "same",
                                    activation=relu,
                                    name="dec6")

            dec7 = conv2d_transpose(dec6,
                                    3, (5, 5), (2, 2),
                                    "same",
                                    activation=None,
                                    name="dec8")
            self.dec = relu(dec7, name="reconstruction")

        with tf.name_scope("loss"):
            # # VAE Loss
            # 1. Reconstruction loss: How far did we get from the actual image?
            y_padded = tf.pad(self.y, paddings, "SYMMETRIC")
            self.rec_loss = tf.reduce_sum(tf.square(y_padded - self.dec),
                                          axis=[1, 2, 3])

            self.loss = tf.reduce_mean(self.rec_loss, name='total_loss')

            tf.summary.scalar("total_loss", self.loss)

        update_tensor_dict(INPUTS, IMAGE_INPUT, self.x)
        update_tensor_dict(OUTPUTS, EMBEDDING, self.z)
        update_tensor_dict(OUTPUTS, RECONSTRUCTION, self.dec)

        optimizer = tf.train.AdamOptimizer(learning_rate=lr)
        self.train_step = optimizer.minimize(self.loss, name="train_step")

        self.init_vars = tf.global_variables_initializer()
        self.saver = tf.train.Saver()
Ejemplo n.º 19
0
 def testInvalidDataFormat(self):
     height, width = 7, 9
     images = random_ops.random_uniform((5, height, width, 3), seed=1)
     with self.assertRaisesRegexp(ValueError, 'data_format'):
         conv_layers.conv2d_transpose(images, 32, 3, data_format='invalid')