def __call__(self, inputs):

        self.deconv_filter = normal_initializer((self.kernel_size, self.kernel_size,
                                                 self.output_filters, self.input_filters), 
                                                name="deconv_filter" )
        self.deconv_bias = zero_initializer ((self.output_filters), name="deconv_bias")

        # input height and width
        input_height = inputs.get_shape().as_list()[1]
        input_width = inputs.get_shape().as_list()[2]

        
        if self.kernel_padding == 'SAME':
            output_height = input_height * self.kernel_stride
            output_width = input_width * self.kernel_stride
        elif self.kernel_padding == 'VALID':
            output_height = (input_height - 1) * self.kernel_stride + self.kernel_size
            output_width = (input_width - 1) * self.kernel_stride + self.kernel_size
        else:
            raise Exception('No such padding')

        self.deconv_output = tf.nn.conv2d_transpose(inputs, self.deconv_filter, 
                                                    [tf.shape(inputs)[0], output_height, output_width, self.output_filters], 
                                                    [1, self.kernel_stride, self.kernel_stride, 1], self.kernel_padding)
        # Add bias and apply activation
        if self.act is None:
            self.total_output = self.deconv_output
        else:    
            self.total_output = self.act(self.deconv_output + self.deconv_bias)

        return self.total_output
Example #2
0
    def decoder(self, inputs, last_conv_dims):

        # apply fully connected layer
        weights_decoder = normal_initializer((FLAGS.code_size, 
                                              tf.cast(last_conv_dims[0]*last_conv_dims[1]*last_conv_dims[2], tf.int32)))
        bias_decoder = zero_initializer((tf.cast(last_conv_dims[0]*last_conv_dims[1]*last_conv_dims[2], tf.int32)))
        decode_layer = tf.nn.relu(tf.matmul(inputs, weights_decoder) + bias_decoder)
        print(decode_layer.shape)
        
        
        
        # reshape to send as input to transposed convolutional layer
        
        deconv_input = tf.reshape(decode_layer, (-1,last_conv_dims[0],last_conv_dims[1],last_conv_dims[2]))

        print(deconv_input.shape)

        # transpose convolutional layer
        deconv1 = DeconvLayer (input_filters=tf.cast(deconv_input.shape[3], tf.int32), output_filters=16, act=tf.nn.relu,
                               kernel_size=3, kernel_stride=2, kernel_padding="SAME")
        deconv1_act = deconv1.__call__(deconv_input)
        print(deconv1_act.shape)
        # transpose convolutional layer
        deconv2 = DeconvLayer (input_filters=16, output_filters=8, act=tf.nn.relu,
                               kernel_size=3, kernel_stride=2, kernel_padding="SAME")
        deconv2_act = deconv2.__call__(deconv1_act)
        print(deconv2_act.shape)
        # transpose convolutional layer
        deconv3 = DeconvLayer (input_filters=8, output_filters=1, act=None,
                               kernel_size=3, kernel_stride=2, kernel_padding="SAME")
        deconv3_act = deconv3.__call__(deconv2_act)
        print(deconv3_act.shape)

        return deconv3_act
Example #3
0
    def forward(self, inputs, ema=None):
        hps = self.hps
        bsmm = hps.bsmm
        xgroup = hps.x_group_size
        xgroups = len(inputs) // xgroup
        sproj = hps.sproj_out

        self.inputs = inputs

        if sproj is not None:
            inputs = [sproj.gather(h) for h in inputs]

        with tf.variable_scope(self.scope):

            w = hps.get_variable("w", bsmm["y"].w_shape, normal_initializer())
            g = hps.get_variable("g", [hps.nvocab], ones_initializer())
            b = hps.get_variable("b", [hps.nvocab], zeros_initializer())

            self.params = [w, g, b]

            if ema is not None:
                w = ema.average(w)
                g = ema.average(g)
                b = ema.average(b)

            #w = ew.float_cast(w, dtype=hps.dtype)
            w = bsmm["y"].l2_normalize(w, dtype=hps.dtype)

            # compute the fc matmul in groups for better memory efficiency.
            ygroups = []
            for i in range(xgroups):
                x = tf.concat(inputs[i * xgroup:(i + 1) * xgroup],
                              1 - hps.axis)

                # (nsteps x nbatch, nvocab) = (nsteps x nbatch, hidden) . (nhidden, nvocab)
                ygroups.append(bsmm["y"](x, w, dw_dtype=hps.dw_dtype))

            y = tf.concat(ygroups, 1 - hps.axis)

            # cast to float32 before entering cost function
            y = ew.float_cast(y, dtype=tf.float32, dx_dtype=hps.dx_dtype)

            if hps.axis == 0:
                y = tf.transpose(y)

            if (hps.nvocab % 32) != 0:
                y = tf.slice(y, [0, 0], [-1, hps.nvocab])

            self.outputs = y * g + b

            outputs = tf.stop_gradient(self.outputs)

        return outputs
    def __call__(self, inputs):
        
        self.conv_filter = normal_initializer((self.kernel_size, self.kernel_size, 
                                               self.input_filters, self.output_filters), name="conv_filter" )
        self.conv_bias = zero_initializer ((self.output_filters), name="conv_bias")
  
        self.conv_output = tf.nn.conv2d(inputs, self.conv_filter, [1, self.kernel_stride, self.kernel_stride, 1]
                                        , self.kernel_padding)
        # Add bias and apply activation
        self.total_output = self.act(self.conv_output + self.conv_bias)

        return self._call(self.total_output)
Example #5
0
    def encoder(self, inputs):
        # convolutional layer
        conv1 = ConvLayer(input_filters=tf.cast(inputs.shape[3], tf.int32), output_filters=8, act=tf.nn.relu,
                          kernel_size=3, kernel_stride=1, kernel_padding="SAME")
        conv1_act = conv1.__call__(inputs)
        print(conv1_act.shape)
        # convolutional and pooling layer
        conv_pool1 = ConvPoolLayer(input_filters=8, output_filters=8, act=tf.nn.relu,
                                   kernel_size=3, kernel_stride=1, kernel_padding="SAME",
                                   pool_size=3, pool_stride=2, pool_padding="SAME")
        conv_pool1_act = conv_pool1.__call__(conv1_act)
        print(conv_pool1_act.shape)
        # convolutional layer
        conv2 = ConvLayer(input_filters=8, output_filters=16, act=tf.nn.relu,
                          kernel_size=3, kernel_stride=1, kernel_padding="SAME")
        conv2_act = conv2.__call__(conv_pool1_act)
        print(conv2_act.shape)
        # convolutional and pooling layer
        conv_pool2 = ConvPoolLayer(input_filters=16, output_filters=16, act=tf.nn.relu,
                                   kernel_size=3, kernel_stride=1, kernel_padding="SAME",
                                   pool_size=3, pool_stride=2, pool_padding="SAME")
        conv_pool2_act = conv_pool2.__call__(conv2_act)
        print(conv_pool2_act.shape)
        
        conv3 = ConvLayer(input_filters=16, output_filters=32, act=tf.nn.relu,
                          kernel_size=3, kernel_stride=1, kernel_padding="SAME")
        conv3_act = conv3.__call__(conv_pool2_act)
        print(conv3_act.shape)
        
        conv_pool3 = ConvPoolLayer(input_filters=32, output_filters=32, act=tf.nn.relu,
                                   kernel_size=3, kernel_stride=1, kernel_padding="SAME",
                                   pool_size=3, pool_stride=2, pool_padding="SAME")
        conv_pool3_act = conv_pool3.__call__(conv3_act)
        print(conv_pool3_act.shape)
        
        last_conv_dims = conv_pool3_act.shape[1:]
        # make output of pooling flatten

        flatten = tf.reshape(conv_pool3_act, [-1,last_conv_dims[0]*last_conv_dims[1]*last_conv_dims[2]])
        print(flatten.shape)
        weights_encoder = normal_initializer((tf.cast(flatten.shape[1], tf.int32), FLAGS.code_size))
        bias_encoder = zero_initializer((FLAGS.code_size))
        # apply fully connected layer

        dense = tf.matmul(flatten, weights_encoder) + bias_encoder
        print(dense.shape)

        return dense, last_conv_dims
Example #6
0
    def __call__(self, inputs):

        #####################################################################
        # TODO: Define Filters and Bias                                     #
        # Filter kernel size is self.kernel_size                            #
        # Number of input channels is self.input_filters                    #
        # Number of desired output filters is self.output_filters           #
        # Define filter tensor with proper size using normal initializer    #
        # Define bias tensor as well using zero initializer                 #
        #####################################################################

        self.conv_filter = normal_initializer(
            shape=(self.kernel_size, self.kernel_size, self.input_filters,
                   self.output_filters),
            name='conv_w')
        self.conv_bias = zero_initializer(shape=(self.output_filters, ),
                                          name='conv_b')

        #####################################################################
        #                           END OF YOUR CODE                        #
        #####################################################################

        #######################################################################
        # TODO: Apply Convolution, Bias and Activation Function               #
        # Use tf.nn.conv2d and give it following inputs                       #
        #   1. Input tensor                                                   #
        #   2. Filter you have defined in above empty part                    #
        #   3. Stride tensor showing stride size for each dimension           #
        #   4. Padding type based on self.kernel_padding                      #
        # Add bias after filtering by convolutions                            #
        # Finally apply activation function and store it as self.total_output #
        #######################################################################

        self.conv_output = tf.nn.conv2d(
            input=inputs,
            filter=self.conv_filter,
            strides=[1, self.kernel_stride, self.kernel_stride, 1],
            padding=self.kernel_padding)

        self.total_output = self.act(self.conv_output + self.conv_bias)

        #######################################################################
        #                           END OF YOUR CODE                          #
        #######################################################################

        return self._call(self.total_output)
Example #7
0
    def __call__(self, inputs):

        # Define Filters and Bias
        # Note that tensor shape of this filter is different from that of the filter in ConvLayer

        self.deconv_filter = normal_initializer(shape=[self.kernel_size, self.kernel_size, self.output_filters, self.input_filters])
        self.deconv_bias = zero_initializer(shape=[self.output_filters])

        # input height and width
        input_height = inputs.get_shape().as_list()[1]
        input_width = inputs.get_shape().as_list()[2]


        # Calculate Output Shape
        # The formula to calculate output shapes depends on type of padding

        if self.kernel_padding == 'SAME':
            output_height = self.kernel_stride*input_height
            output_width = self.kernel_stride*input_width
        elif self.kernel_padding == 'VALID':
            output_height = self.kernel_stride*(input_height-1)+self.kernel_size
            output_width = self.kernel_stride*(input_width-1)+self.kernel_size
        else:
            raise Exception('No such padding')


        # Apply Transposed Convolution, Bias and Activation Function
        # Use tf.nn.conv2d_transpose and give it following inputs
        #   1. Input tensor
        #   2. Filter you have defined above
        #   3. Output shape you have calculated above
        #   4. Stride tensor showing stride size for each dimension
        #   5. Padding type based on self.kernel_padding

        batch_size = tf.shape(inputs)[0]
        Depth = inputs.get_shape().as_list()[3]
        deconv_shape = tf.stack([batch_size, output_height, output_width, self.output_filters])
        
        self.deconv_output = tf.nn.conv2d_transpose(inputs, self.deconv_filter, output_shape=deconv_shape, strides=[1, self.kernel_stride, self.kernel_stride, 1], padding=self.kernel_padding)
        # Add bias and apply activation
        self.total_output = self.act(tf.add(self.deconv_output, self.deconv_bias))

        return self.total_output
Example #8
0
    def __call__(self, inputs):

        # Define Filters and Bias
        # Define filter tensor with proper size using normal initializer
        # Define bias tensor as well using zero initializer

        self.conv_filter = normal_initializer(shape=[self.kernel_size, self.kernel_size, self.input_filters, self.output_filters])
        self.conv_bias = zero_initializer(shape=[self.output_filters])



        # Apply Convolution, Bias and Activation Function
        # Use tf.nn.conv2d and give it following inputs
        #   1. Input tensor
        #   2. Filter you have defined in above empty part
        #   3. Stride tensor showing stride size for each dimension
        #   4. Padding type based on self.kernel_padding

        self.conv_output = tf.nn.conv2d(inputs, self.conv_filter, strides=[1, self.kernel_stride, self.kernel_stride, 1], padding=self.kernel_padding)
        # Add bias and apply activation
        self.total_output = self.act(tf.add(self.conv_output, self.conv_bias))

        return self._call(self.total_output)
Example #9
0
    def encoder(self, inputs):

        # Build Convolutional Part of Encoder
        # Put sequential layers:
        #       ConvLayer1 ==> ConvPoolLayer1 ==> ConvLayer2 ==> ConvPoolLayer2 ==> ConvLayer3 ==> ConvPoolLayer3
        # Settings of layers:
        # For all ConvLayers: filter size = 3, filter stride = 1, padding type = SAME
        # For all ConvPoolLayers:
        #   Conv    : filter size = 3, filter stride = 1, padding type = SAME
        #   Pooling :   pool size = 3,   pool stride = 2, padding type = SAME
        # Number of Filters:
        #       num_channel defined in FLAGS (input) ==> 8 ==> 8 ==> 16 ==> 16 ==> 32 ==> 32

        # convolutional layer
        conv1_class = ConvLayer(input_filters=FLAGS.num_channel,
                                output_filters=8,
                                act=tf.nn.relu,
                                kernel_size=3,
                                kernel_stride=1,
                                kernel_padding='SAME')
        conv1 = conv1_class(inputs=inputs)
        print(conv1.shape)
        # convolutional and pooling layer
        conv_pool1_class = ConvPoolLayer(input_filters=8,
                                         output_filters=8,
                                         act=tf.nn.relu,
                                         kernel_size=3,
                                         kernel_stride=1,
                                         kernel_padding='SAME',
                                         pool_size=3,
                                         pool_stride=2,
                                         pool_padding='SAME')
        conv_pool1 = conv_pool1_class(inputs=conv1)
        print(conv_pool1.shape)
        # convolutional layer
        conv2_class = ConvLayer(input_filters=8,
                                output_filters=16,
                                act=tf.nn.relu,
                                kernel_size=3,
                                kernel_stride=1,
                                kernel_padding='SAME')
        conv2 = conv2_class(inputs=conv_pool1)
        print(conv2.shape)
        # convolutional and pooling layer
        conv_pool2_class = ConvPoolLayer(input_filters=16,
                                         output_filters=16,
                                         act=tf.nn.relu,
                                         kernel_size=3,
                                         kernel_stride=1,
                                         kernel_padding='SAME',
                                         pool_size=3,
                                         pool_stride=2,
                                         pool_padding='SAME')
        conv_pool2 = conv_pool2_class(inputs=conv2)
        print(conv_pool2.shape)

        conv3_class = ConvLayer(input_filters=16,
                                output_filters=32,
                                act=tf.nn.relu,
                                kernel_size=3,
                                kernel_stride=1,
                                kernel_padding='SAME')
        conv3 = conv3_class(inputs=conv_pool2)
        print(conv3.shape)

        conv_pool3_class = ConvPoolLayer(input_filters=32,
                                         output_filters=32,
                                         act=tf.nn.relu,
                                         kernel_size=3,
                                         kernel_stride=1,
                                         kernel_padding='SAME',
                                         pool_size=3,
                                         pool_stride=2,
                                         pool_padding='SAME')
        conv_pool3 = conv_pool3_class(inputs=conv3)
        print(conv_pool3.shape)

        # Make Output Flatten and Apply Transformation
        # Num of features for dense is defined by code_size in FLAG

        # make output of pooling flatten
        WholeShape = tf.shape(conv_pool3)
        NumSamples = WholeShape[0]
        last_conv_dims = tf.constant(value=(4, 4, 32),
                                     dtype=tf.int32,
                                     shape=(3, ))  #WholeShape[1:]
        FlattedShape = tf.reduce_prod(last_conv_dims)

        flatten = tf.reshape(conv_pool3, shape=[NumSamples, FlattedShape])
        print(flatten.shape)

        # apply fully connected layer
        W_Trans = normal_initializer(shape=[FlattedShape, FLAGS.code_size])
        B_Trans = zero_initializer(shape=[FLAGS.code_size])
        dense = tf.nn.xw_plus_b(flatten, W_Trans, B_Trans)
        print(dense.shape)

        return dense, last_conv_dims
Example #10
0
    def decoder(self, inputs, last_conv_dims):

        # Apply Transformation and Reshape to Original
        # Num of output features in this transformation can be calculated using last_conv_dims
        # Please note that number of input features is code_size stored in FLAGS

        # apply fully connected layer
        FlattedShape = tf.reduce_prod(last_conv_dims)

        W_InvTrans = normal_initializer(shape=[FLAGS.code_size, FlattedShape])
        B_InvTrans = zero_initializer(shape=[FlattedShape])

        decode_layer = tf.nn.relu(
            tf.nn.xw_plus_b(inputs, W_InvTrans, B_InvTrans))

        print(decode_layer.shape)

        # reshape to send as input to transposed convolutional layer

        deconv_input = tf.reshape(decode_layer,
                                  shape=[
                                      -1, last_conv_dims[0], last_conv_dims[1],
                                      last_conv_dims[2]
                                  ])

        print(deconv_input.shape)

        # Apply 3 Transposed Convolution Sequentially
        # Put sequential layers:
        #       DeconvLayer ==> Deconv Layer ==> Deconv Layer
        # For all layers use:
        #       filter size = 3, filter stride = 2, padding type = SAME
        # Apply tf.nn.relu as activation function for first two layers
        # Multiply all the numbers in last_conv_dims to find num of output features
        # Number of filters:
        #       num_channel defined in FLAGS (input of first deconv) ==> 16 ==> 8 ==> 1

        # transpose convolutional layer
        deconv1_class = DeconvLayer(input_filters=last_conv_dims[2],
                                    output_filters=16,
                                    act=tf.nn.relu,
                                    kernel_size=3,
                                    kernel_stride=2,
                                    kernel_padding='SAME')
        deconv1 = deconv1_class(inputs=deconv_input)
        print(deconv1.shape)
        # transpose convolutional layer
        deconv2_class = DeconvLayer(input_filters=16,
                                    output_filters=8,
                                    act=tf.nn.relu,
                                    kernel_size=3,
                                    kernel_stride=2,
                                    kernel_padding='SAME')
        deconv2 = deconv2_class(inputs=deconv1)
        print(deconv2.shape)
        # transpose convolutional layer
        deconv3_class = DeconvLayer(input_filters=8,
                                    output_filters=1,
                                    act=tf.identity,
                                    kernel_size=3,
                                    kernel_stride=2,
                                    kernel_padding='SAME')
        deconv3 = deconv3_class(inputs=deconv2)
        print(deconv3.shape)

        return deconv3
Example #11
0
    def encoder(self, inputs):
        #############################################################################################################
        # TODO: Build Convolutional Part of Encoder                                                                 #
        # Put sequential layers:                                                                                    #
        #       ConvLayer1 ==> ConvPoolLayer1 ==> ConvLayer2 ==> ConvPoolLayer2 ==> ConvLayer3 ==> ConvPoolLayer3   #
        # Settings of layers:                                                                                       #
        # For all ConvLayers: filter size = 3, filter stride = 1, padding type = SAME                               #
        # For all ConvPoolLayers:                                                                                   #
        #   Conv    : filter size = 3, filter stride = 1, padding type = SAME                                       #
        #   Pooling :   pool size = 3,   pool stride = 2, padding type = SAME                                       #
        # Number of Filters:                                                                                        #
        #       num_channel defined in FLAGS (input) ==> 8 ==> 8 ==> 16 ==> 16 ==> 32 ==> 32                        #
        #############################################################################################################
        relu = tf.nn.relu
        # convolutional layer
        cl1 = ConvLayer(FLAGS.num_channel, 8, relu, 3, 1, 'SAME')
        conv1 = cl1(inputs)
        print(conv1.shape)
        # convolutional and pooling layer
        cl2 = ConvPoolLayer(8, 8, relu, 3, 1, 'SAME', 3, 2, 'SAME')
        conv_pool1 = cl2(conv1)
        print(conv_pool1.shape)
        # convolutional layer
        cl3 = ConvLayer(8, 16, relu, 3, 1, 'SAME')
        conv2 = cl3(conv_pool1)
        print(conv2.shape)
        # convolutional and pooling layer
        cl4 = ConvPoolLayer(16, 16, relu, 3, 1, 'SAME', 3, 2, 'SAME')
        conv_pool2 = cl4(conv2)
        print(conv_pool2.shape)

        cl5 = ConvLayer(16, 32, relu, 3, 1, 'SAME')
        conv3 = cl5(conv_pool2)
        print(conv3.shape)

        cl6 = ConvPoolLayer(32, 32, tf.nn.relu, 3, 1, 'SAME', 3, 2, 'SAME')
        conv_pool3 = cl6(conv3)
        print(conv_pool3.shape)
        ##########################################################################
        #                           END OF YOUR CODE                             #
        ##########################################################################

        ##########################################################################
        # TODO: Make Output Flatten and Apply Transformation                     #
        # Please save the last three dimensions of output of the above code      #
        # Save these numbers in a variable called last_conv_dims                 #
        # Multiply all these dimensions to find num of features if flatten       #
        # Use tf.reshape to make a tensor flat                                   #
        # Define some weights and bias and apply linear transformation           #
        # Use normal and zero initializer for weights and bias respectively      #
        # Please store output of transformation in a variable called dense       #
        # Num of features for dense is defined by code_size in FLAG              #
        # Note that there is no need apply any kind of activation function       #
        ##########################################################################

        # make output of pooling flatten
        dim = np.prod(conv_pool3.shape[1:])
        flatten = tf.reshape(conv_pool3, [-1, dim])
        print(flatten.shape)

        # apply fully connected layer
        W_fc = normal_initializer(shape=(dim.__int__(), FLAGS.code_size))
        B_fc = zero_initializer(shape=FLAGS.code_size)
        dense = tf.matmul(flatten, W_fc) + B_fc
        print(dense.shape)

        ##########################################################################
        #                           END OF YOUR CODE                             #
        ##########################################################################

        last_conv_dims = conv_pool3.shape[1:]
        return dense, last_conv_dims
Example #12
0
    def decoder(self, inputs, last_conv_dims):
        #########################################################################################
        # TODO: Apply Transformation and Reshape to Original                                    #
        # Define some weights and biases and apply linear transformation                        #
        # Num of output features in this transformation can be calculated using last_conv_dims  #
        # Multiply all the numbers in last_conv_dims to find num of output features             #
        # Please note that number of input features is code_size stored in FLAGS                #
        # Use normal and zero initializer for weights and biases respectively                   #
        # Apply tf.nn.relu activation function                                                  #
        # Finally use last_conv_dims to reshape output of transformation                        #
        #########################################################################################

        # apply fully connected layer
        dim = last_conv_dims[0] * last_conv_dims[1] * last_conv_dims[2]
        # dim = np.prod(last_conv_dims)
        W_fc = normal_initializer(shape=(FLAGS.code_size, dim.__int__()))
        B_fc = zero_initializer(shape=dim)
        wxb = tf.matmul(inputs, W_fc) + B_fc
        decode_layer = tf.nn.relu(wxb)

        print(decode_layer.shape)

        # reshape to send as input to transposed convolutional layer

        deconv_input = tf.reshape(decode_layer, [-1, last_conv_dims[0], last_conv_dims[1], last_conv_dims[2]])

        print(deconv_input.shape)

        #########################################################################################
        #                                      END OF YOUR CODE                                 #
        #########################################################################################

        ###################################################################################
        # TODO: Apply 3 Transposed Convolution Sequentially                               #
        # Put sequential layers:                                                          #
        #       DeconvLayer ==> Deconv Layer ==> Deconv Layer                             #
        # For all layers use:                                                             #
        #       filter size = 3, filter stride = 2, padding type = SAME                   #
        # Apply tf.nn.relu as activation function for first two layers                    #
        # Note that use linear activation function for last layer                         #
        # Multiply all the numbers in last_conv_dims to find num of output features       #
        # Number of filters:                                                              #
        #       num_channel defined in FLAGS (input of first deconv) ==> 16 ==> 8 ==> 1   #
        # Save final output in deconv3                                                    #
        ###################################################################################
        relu = tf.nn.relu
        # transpose convolutional layer
        dc1 = DeconvLayer(32, 16, relu, 3, 2, 'SAME')
        deconv1 = dc1(deconv_input)
        print(deconv1.shape)
        # transpose convolutional layer
        dc2 = DeconvLayer(16, 8, relu, 3, 2, 'SAME')
        deconv2 = dc2(deconv1)
        print(deconv2.shape)
        # transpose convolutional layer
        dc3 = DeconvLayer(8, 1, lambda x: x, 3, 2, 'SAME')
        deconv3 = dc3(deconv2)
        print(deconv3.shape)

        ###################################################################################
        #                                  END OF YOUR CODE                               #
        ###################################################################################
        return deconv3
    def encoder(self, inputs):

        #############################################################################################################
        # TODO: Build Convolutional Part of Encoder                                                                 #
        # Put sequential layers:                                                                                    #
        #       ConvLayer1 ==> ConvPoolLayer1 ==> ConvLayer2 ==> ConvPoolLayer2 ==> ConvLayer3 ==> ConvPoolLayer3   #
        # Settings of layers:                                                                                       #
        # For all ConvLayers: filter size = 3, filter stride = 1, padding type = SAME                               #
        # For all ConvPoolLayers:                                                                                   #
        #   Conv    : filter size = 3, filter stride = 1, padding type = SAME                                       #
        #   Pooling :   pool size = 3,   pool stride = 2, padding type = SAME                                       #
        # Number of Filters:                                                                                        #
        #       num_channel defined in FLAGS (input) ==> 8 ==> 8 ==> 16 ==> 16 ==> 32 ==> 32                        #
        #############################################################################################################
        # convolutional layer
        relu = tf.nn.relu
        conv1 = ConvLayer(input_filters=FLAGS.num_channel,
                          output_filters=8,
                          act=relu,
                          kernel_size=3,
                          kernel_stride=1,
                          kernel_padding='SAME')(inputs)
        print(conv1.shape)
        # convolutional and pooling layer
        conv_pool1 = ConvPoolLayer(input_filters=8,
                                   output_filters=8,
                                   act=relu,
                                   kernel_size=3,
                                   kernel_stride=1,
                                   kernel_padding='SAME',
                                   pool_size=3,
                                   pool_stride=2,
                                   pool_padding='SAME')(conv1)
        print(conv_pool1.shape)
        # convolutional layer
        conv2 = ConvLayer(input_filters=8,
                          output_filters=16,
                          act=relu,
                          kernel_size=3,
                          kernel_stride=1,
                          kernel_padding='SAME')(conv_pool1)
        print(conv2.shape)
        # convolutional and pooling layer
        conv_pool2 = ConvPoolLayer(input_filters=16,
                                   output_filters=16,
                                   act=relu,
                                   kernel_size=3,
                                   kernel_stride=1,
                                   kernel_padding='SAME',
                                   pool_size=3,
                                   pool_stride=2,
                                   pool_padding='SAME')(conv2)
        print(conv_pool2.shape)

        conv3 = ConvLayer(input_filters=16,
                          output_filters=32,
                          act=relu,
                          kernel_size=3,
                          kernel_stride=1,
                          kernel_padding='SAME')(conv_pool2)
        print(conv3.shape)

        conv_pool3 = ConvPoolLayer(input_filters=32,
                                   output_filters=32,
                                   act=relu,
                                   kernel_size=3,
                                   kernel_stride=1,
                                   kernel_padding='SAME',
                                   pool_size=3,
                                   pool_stride=2,
                                   pool_padding='SAME')(conv3)
        print(conv_pool3.shape)
        ##########################################################################
        #                           END OF YOUR CODE                             #
        ##########################################################################

        ##########################################################################
        # TODO: Make Output Flatten and Apply Transformation                     #
        # Please save the last three dimensions of output of the above code      #
        # Save these numbers in a variable called last_conv_dims                 #
        # Multiply all these dimensions to find num of features if flatten       #
        # Use tf.reshape to make a tensor flat                                   #
        # Define some weights and bias and apply linear transformation           #
        # Use normal and zero initializer for weights and bias respectively      #
        # Please store output of transformation in a variable called dense       #
        # Num of features for dense is defined by code_size in FLAG              #
        # Note that there is no need apply any kind of activation function       #
        ##########################################################################

        # make output of pooling flatten
        last_conv_dims = conv_pool3.shape[1:]
        flatten_dim = np.prod(last_conv_dims)
        flatten = tf.reshape(conv_pool3,
                             [tf.shape(conv_pool3)[0], flatten_dim])
        print(flatten.shape)

        # apply fully connected layer

        dense = tf.matmul(flatten,
                          normal_initializer([
                              flatten_dim, FLAGS.code_size
                          ])) + zero_initializer([FLAGS.code_size])
        print(dense.shape)

        ##########################################################################
        #                           END OF YOUR CODE                             #
        ##########################################################################

        return dense, last_conv_dims
    def decoder(self, inputs, last_conv_dims):

        #########################################################################################
        # TODO: Apply Transformation and Reshape to Original                                    #
        # Define some weights and biases and apply linear transformation                        #
        # Num of output features in this transformation can be calculated using last_conv_dims  #
        # Multiply all the numbers in last_conv_dims to find num of output features             #
        # Please note that number of input features is code_size stored in FLAGS                #
        # Use normal and zero initializer for weights and biases respectively                   #
        # Apply tf.nn.relu activation function                                                  #
        # Finally use last_conv_dims to reshape output of transformation                        #
        #########################################################################################

        # apply fully connected layer

        decode_layer = tf.matmul(
            inputs,
            normal_initializer([FLAGS.code_size,
                                np.prod(last_conv_dims)])) + zero_initializer(
                                    [np.prod(last_conv_dims)])

        print(decode_layer.shape)

        # reshape to send as input to transposed convolutional layer

        deconv_input = tf.reshape(decode_layer, [
            tf.shape(inputs)[0], last_conv_dims[0], last_conv_dims[1],
            last_conv_dims[2]
        ])

        print(deconv_input.shape)

        #########################################################################################
        #                                      END OF YOUR CODE                                 #
        #########################################################################################

        ###################################################################################
        # TODO: Apply 3 Transposed Convolution Sequentially                               #
        # Put sequential layers:                                                          #
        #       DeconvLayer ==> Deconv Layer ==> Deconv Layer                             #
        # For all layers use:                                                             #
        #       filter size = 3, filter stride = 2, padding type = SAME                   #
        # Apply tf.nn.relu as activation function for first two layers                    #
        # Note that use linear activation function for last layer                         #
        # Multiply all the numbers in last_conv_dims to find num of output features       #
        # Number of filters:                                                              #
        #       num_channel defined in FLAGS (input of first deconv) ==> 16 ==> 8 ==> 1   #
        # Save final output in deconv3                                                    #
        ###################################################################################
        relu = tf.nn.relu
        # transpose convolutional layer
        print(last_conv_dims)
        deconv1 = DeconvLayer(input_filters=last_conv_dims[2],
                              output_filters=16,
                              act=relu,
                              kernel_size=3,
                              kernel_stride=2,
                              kernel_padding='SAME')(deconv_input)
        print(deconv1.shape)
        # transpose convolutional layer
        deconv2 = DeconvLayer(input_filters=16,
                              output_filters=8,
                              act=relu,
                              kernel_size=3,
                              kernel_stride=2,
                              kernel_padding='SAME')(deconv1)
        print(deconv2.shape)
        # transpose convolutional layer
        deconv3 = DeconvLayer(input_filters=8,
                              output_filters=1,
                              act=tf.keras.activations.linear,
                              kernel_size=3,
                              kernel_stride=2,
                              kernel_padding='SAME')(deconv2)
        print(deconv3.shape)

        ###################################################################################
        #                                  END OF YOUR CODE                               #
        ###################################################################################
        return deconv3
Example #15
0
    def __call__(self, inputs):

        ############################################################################################
        # TODO: Define Filters and Bias                                                            #
        # Filter kernel size is self.kernel_size                                                   #
        # Number of input channels is self.input_filters                                           #
        # Number of desired output filters is self.output_filters                                  #
        # Define filter tensor with proper size using normal initializer                           #
        # Note that tensor shape of this filter is different from that of the filter in ConvLayer  #
        # Define bias tensor as well using zero initializer                                        #
        ############################################################################################
        self.deconv_filter = normal_initializer(shape=(self.kernel_size,
                                                       self.kernel_size,
                                                       self.output_filters,
                                                       self.input_filters))
        self.deconv_bias = zero_initializer(shape=(self.output_filters))
        ############################################################################################
        #                           END OF YOUR CODE                                               #
        ############################################################################################

        # input height and width
        input_height = inputs.get_shape().as_list()[1]
        input_width = inputs.get_shape().as_list()[2]

        ############################################################################
        # TODO: Calculate Output Shape                                             #
        # Use input height and width to set output height and width respectively   #
        # The formula to calculate output shapes depends on type of padding        #
        ############################################################################

        if self.kernel_padding == 'VALID':
            output_height = self.kernel_stride * (input_height -
                                                  1) + self.kernel_size
            output_width = self.kernel_stride * (input_width -
                                                 1) + self.kernel_size
        elif self.kernel_padding == 'SAME':
            output_height = self.kernel_stride * (input_height)
            output_width = self.kernel_stride * (input_width)
        else:
            raise Exception('No such padding')

        ############################################################################
        #                             END OF YOUR CODE                             #
        ############################################################################

        #########################################################################
        # TODO: Apply Transposed Convolution, Bias and Activation Function      #
        # Use tf.nn.conv2d_transpose and give it following inputs               #
        #   1. Input tensor                                                     #
        #   2. Filter you have defined above                                    #
        #   3. Output shape you have calculated above                           #
        #   4. Stride tensor showing stride size for each dimension             #
        #   5. Padding type based on self.kernel_padding                        #
        # Add bias after filtering by transposed convolutions                   #
        # Finally apply activation function and store it as self.total_output   #
        #########################################################################
        self.deconv_output = tf.nn.conv2d_transpose(
            value=inputs,
            filter=self.deconv_filter,
            output_shape=[
                tf.shape(inputs)[0], output_height, output_width,
                self.output_filters
            ],
            strides=[1, self.kernel_stride, self.kernel_stride, 1],
            padding=self.kernel_padding)
        # Add bias and apply activation
        self.total_output = self.act(self.deconv_output + self.deconv_bias)
        #########################################################################
        #                             END OF YOUR CODE                          #
        #########################################################################

        return self.total_output