Esempio n. 1
0
 def compute_output_shape(self, input_shape):
     input_shape = tensor_shape.TensorShape(input_shape).as_list()
     if self.data_format == 'channels_last':
         space = input_shape[1:-1]
         new_space = []
         for i in range(len(space)):
             new_dim = utils.conv_output_length(
                 space[i],
                 self.kernel_size[i],
                 padding=self.padding,
                 stride=self.strides[i],
                 dilation=self.dilation_rate[i])
             new_space.append(new_dim)
         return tensor_shape.TensorShape([input_shape[0]] + new_space +
                                         [self.filters])
     else:
         space = input_shape[2:]
         new_space = []
         for i in range(len(space)):
             new_dim = utils.conv_output_length(
                 space[i],
                 self.kernel_size[i],
                 padding=self.padding,
                 stride=self.strides[i],
                 dilation=self.dilation_rate[i])
             new_space.append(new_dim)
         return tensor_shape.TensorShape([input_shape[0], self.filters] +
                                         new_space)
Esempio n. 2
0
 def compute_output_shape(self, input_shape):
   input_shape = tensor_shape.TensorShape(input_shape).as_list()
   if self.data_format == 'channels_last':
     space = input_shape[1:-1]
     new_space = []
     for i in range(len(space)):
       new_dim = utils.conv_output_length(
           space[i],
           self.kernel_size[i],
           padding=self.padding,
           stride=self.strides[i],
           dilation=self.dilation_rate[i])
       new_space.append(new_dim)
     return tensor_shape.TensorShape([input_shape[0]] + new_space +
                                     [self.filters])
   else:
     space = input_shape[2:]
     new_space = []
     for i in range(len(space)):
       new_dim = utils.conv_output_length(
           space[i],
           self.kernel_size[i],
           padding=self.padding,
           stride=self.strides[i],
           dilation=self.dilation_rate[i])
       new_space.append(new_dim)
     return tensor_shape.TensorShape([input_shape[0], self.filters] +
                                     new_space)
Esempio n. 3
0
def _pooling_output_shape(input_shape, pool_size):
    # channels_last

    rows = input_shape[1]
    cols = input_shape[2]
    rows = utils.conv_output_length(rows, pool_size[0], 'valid', pool_size[0])
    cols = utils.conv_output_length(cols, pool_size[1], 'valid', pool_size[1])
    return [input_shape[0], rows, cols, input_shape[3]]
Esempio n. 4
0
 def _compute_output_shape(self, input_shape):
   input_shape = tensor_shape.TensorShape(input_shape).as_list()
   if self.data_format == 'channels_first':
     rows = input_shape[2]
     cols = input_shape[3]
   else:
     rows = input_shape[1]
     cols = input_shape[2]
   rows = utils.conv_output_length(rows, self.pool_size[0], self.padding,
                                   self.strides[0])
   cols = utils.conv_output_length(cols, self.pool_size[1], self.padding,
                                   self.strides[1])
   if self.data_format == 'channels_first':
     return tensor_shape.TensorShape(
         [input_shape[0], input_shape[1], rows, cols])
   else:
     return tensor_shape.TensorShape(
         [input_shape[0], rows, cols, input_shape[3]])
Esempio n. 5
0
 def compute_output_shape(self, input_shape):
     input_shape = tensor_shape.TensorShape(input_shape).as_list()
     if self.data_format == 'channels_first':
         rows = input_shape[2]
         cols = input_shape[3]
     else:
         rows = input_shape[1]
         cols = input_shape[2]
     rows = utils.conv_output_length(rows, self.pool_size[0], self.padding,
                                     self.strides[0])
     cols = utils.conv_output_length(cols, self.pool_size[1], self.padding,
                                     self.strides[1])
     if self.data_format == 'channels_first':
         return tensor_shape.TensorShape(
             [input_shape[0], input_shape[1], rows, cols])
     else:
         return tensor_shape.TensorShape(
             [input_shape[0], rows, cols, input_shape[3]])
Esempio n. 6
0
 def testConvOutputLength(self):
   self.assertEqual(4, utils.conv_output_length(4, 2, 'same', 1, 1))
   self.assertEqual(2, utils.conv_output_length(4, 2, 'same', 2, 1))
   self.assertEqual(3, utils.conv_output_length(4, 2, 'valid', 1, 1))
   self.assertEqual(2, utils.conv_output_length(4, 2, 'valid', 2, 1))
   self.assertEqual(5, utils.conv_output_length(4, 2, 'full', 1, 1))
   self.assertEqual(3, utils.conv_output_length(4, 2, 'full', 2, 1))
   self.assertEqual(2, utils.conv_output_length(5, 2, 'valid', 2, 2))
 def testConvOutputLength(self):
     self.assertEqual(4, utils.conv_output_length(4, 2, 'same', 1, 1))
     self.assertEqual(2, utils.conv_output_length(4, 2, 'same', 2, 1))
     self.assertEqual(3, utils.conv_output_length(4, 2, 'valid', 1, 1))
     self.assertEqual(2, utils.conv_output_length(4, 2, 'valid', 2, 1))
     self.assertEqual(5, utils.conv_output_length(4, 2, 'full', 1, 1))
     self.assertEqual(3, utils.conv_output_length(4, 2, 'full', 2, 1))
     self.assertEqual(2, utils.conv_output_length(5, 2, 'valid', 2, 2))
Esempio n. 8
0
 def _conv_output_shape(self, input_shape, kernel_size):
     # channels_last
     space = input_shape[1:-1]
     new_space = []
     for i in range(len(space)):
         new_dim = utils.conv_output_length(space[i],
                                            kernel_size[i],
                                            padding='same',
                                            stride=1,
                                            dilation=1)
         new_space.append(new_dim)
     return ([input_shape[0]] + new_space + [self.filters])
Esempio n. 9
0
 def _compute_output_shape(self, input_shape):
   input_shape = tensor_shape.TensorShape(input_shape).as_list()
   if self.data_format == 'channels_first':
     len_dim1 = input_shape[2]
     len_dim2 = input_shape[3]
     len_dim3 = input_shape[4]
   else:
     len_dim1 = input_shape[1]
     len_dim2 = input_shape[2]
     len_dim3 = input_shape[3]
   len_dim1 = utils.conv_output_length(len_dim1, self.pool_size[0],
                                       self.padding, self.strides[0])
   len_dim2 = utils.conv_output_length(len_dim2, self.pool_size[1],
                                       self.padding, self.strides[1])
   len_dim3 = utils.conv_output_length(len_dim3, self.pool_size[2],
                                       self.padding, self.strides[2])
   if self.data_format == 'channels_first':
     return tensor_shape.TensorShape(
         [input_shape[0], input_shape[1], len_dim1, len_dim2, len_dim3])
   else:
     return tensor_shape.TensorShape(
         [input_shape[0], len_dim1, len_dim2, len_dim3, input_shape[4]])
Esempio n. 10
0
 def compute_output_shape(self, input_shape):
     input_shape = tensor_shape.TensorShape(input_shape).as_list()
     if self.data_format == 'channels_first':
         len_dim1 = input_shape[2]
         len_dim2 = input_shape[3]
         len_dim3 = input_shape[4]
     else:
         len_dim1 = input_shape[1]
         len_dim2 = input_shape[2]
         len_dim3 = input_shape[3]
     len_dim1 = utils.conv_output_length(len_dim1, self.pool_size[0],
                                         self.padding, self.strides[0])
     len_dim2 = utils.conv_output_length(len_dim2, self.pool_size[1],
                                         self.padding, self.strides[1])
     len_dim3 = utils.conv_output_length(len_dim3, self.pool_size[2],
                                         self.padding, self.strides[2])
     if self.data_format == 'channels_first':
         return tensor_shape.TensorShape(
             [input_shape[0], input_shape[1], len_dim1, len_dim2, len_dim3])
     else:
         return tensor_shape.TensorShape(
             [input_shape[0], len_dim1, len_dim2, len_dim3, input_shape[4]])
Esempio n. 11
0
 def _compute_output_shape(self, input_shape):
     input_shape = tensor_shape.TensorShape(input_shape).as_list()
     space = input_shape[1:-2]
     new_space = []
     for i in range(len(space)):
         new_dim = utils.conv_output_length(space[i],
                                            self.kernel_size[i],
                                            padding=self.padding,
                                            stride=self.strides[i])
         new_space.append(new_dim)
     output_shape = tensor_shape.TensorShape([input_shape[0]] + new_space +
                                             [self.dim, self.filters])
     return output_shape
Esempio n. 12
0
 def compute_output_shape(self, input_shape):
     """Computes the output shape of the layer.
     Args:
       input_shape: Shape tuple (tuple of integers) or list of shape tuples
         (one per output tensor of the layer). Shape tuples can include None for
         free dimensions, instead of an integer.
     Returns:
       output_shape: A tuple representing the output shape.
     """
     input_shape = tf.TensorShape(input_shape).as_list()
     if self.data_format == "channels_last":
         space = input_shape[1:-1]
         new_space = []
         for i in range(len(space)):
             new_dim = tf_layers_util.conv_output_length(
                 space[i],
                 self.kernel_size[i],
                 padding=self.padding,
                 stride=self.strides[i],
                 dilation=self.dilation_rate[i],
             )
             new_space.append(new_dim)
         return tf.TensorShape([input_shape[0]] + new_space + [self.filters])
     else:
         space = input_shape[2:]
         new_space = []
         for i in range(len(space)):
             new_dim = tf_layers_util.conv_output_length(
                 space[i],
                 self.kernel_size[i],
                 padding=self.padding,
                 stride=self.strides[i],
                 dilation=self.dilation_rate[i],
             )
             new_space.append(new_dim)
         return tf.TensorShape([input_shape[0], self.filters] + new_space)
Esempio n. 13
0
    def build(self, inputs):
        if not self.built:
            if self.n_filters is None:
                out_dim = inputs.shape[-1]
            else:
                out_dim = self.n_filters

            self.built = True

            self.conv_layers = []

            with self.session.as_default():
                with self.session.graph.as_default():

                    if self.dim == 1:
                        CNN = tf.keras.layers.Conv1D
                        conv_output_shapes = [[
                            int(inputs.shape[1]),
                            int(inputs.shape[2])
                        ]]
                    elif self.dim == 2:
                        CNN = tf.keras.layers.Conv2D
                        conv_output_shapes = [[
                            int(inputs.shape[1]),
                            int(inputs.shape[2]),
                            int(inputs.shape[3])
                        ]]
                    elif self.dim == 3:
                        CNN = tf.keras.layers.Conv3D
                        conv_output_shapes = [[
                            int(inputs.shape[1]),
                            int(inputs.shape[2]),
                            int(inputs.shape[3]),
                            int(inputs.shape[4])
                        ]]
                    else:
                        raise ValueError('dim must be in [1, 2, 3]')

                    for i in range(self.layers_inner):
                        if isinstance(self.stride, list):
                            cur_strides = self.stride[i]
                        else:
                            cur_strides = self.stride

                        if self.name:
                            name = self.name + '_i%d' % i
                        else:
                            name = None

                        l = CNN(out_dim,
                                self.kernel_size,
                                padding=self.padding,
                                strides=cur_strides,
                                use_bias=self.use_bias,
                                name=name)

                        if self.padding in ['causal', 'same'
                                            ] and self.stride == 1:
                            output_shape = conv_output_shapes[-1]
                        else:
                            output_shape = [
                                conv_output_length(x, self.kernel_size,
                                                   self.padding, self.stride)
                                for x in conv_output_shapes[-1]
                            ]

                        conv_output_shapes.append(output_shape)

                        self.conv_layers.append(l)

                    self.conv_output_shapes = conv_output_shapes

                    if self.project_inputs:
                        self.projection = tf.keras.layers.Dense(
                            self.conv_output_shapes[-1][0] * out_dim,
                            input_shape=[
                                self.conv_output_shapes[0][0] *
                                self.conv_output_shapes[0][1]
                            ])

            self.built = True
Esempio n. 14
0
 def _compute_output_shape(self, input_shape):
   input_shape = tensor_shape.TensorShape(input_shape).as_list()
   length = utils.conv_output_length(input_shape[1], self.pool_size[0],
                                     self.padding, self.strides[0])
   return tensor_shape.TensorShape([input_shape[0], length, input_shape[2]])
Esempio n. 15
0
 def compute_output_shape(self, input_shape):
     input_shape = tensor_shape.TensorShape(input_shape).as_list()
     length = utils.conv_output_length(input_shape[1], self.pool_size[0],
                                       self.padding, self.strides[0])
     return tensor_shape.TensorShape(
         [input_shape[0], length, input_shape[2]])
Esempio n. 16
0
    def build(self, inputs):
        if not self.built:
            if self.n_filters is None:
                out_dim = inputs.shape[-1]
            else:
                out_dim = self.n_filters

            self.built = True

            self.conv_1d_layers = []

            with self.session.as_default():
                with self.session.graph.as_default():

                    conv_output_shapes = [[
                        int(inputs.shape[1]),
                        int(inputs.shape[2])
                    ]]

                    for i in range(self.layers_inner):
                        if isinstance(self.stride, list):
                            cur_strides = self.stride[i]
                        else:
                            cur_strides = self.stride

                        if self.name:
                            name = self.name + '_i%d' % i
                        else:
                            name = None

                        l = tf.keras.layers.Conv1D(
                            out_dim,
                            self.kernel_size,
                            padding=self.padding,
                            kernel_initializer=self.kernel_initializer,
                            bias_initializer=self.bias_initializer,
                            kernel_regularizer=self.kernel_regularizer,
                            strides=cur_strides,
                            use_bias=self.use_bias,
                            name=name)

                        if self.padding in ['causal', 'same'
                                            ] and self.stride == 1:
                            output_shape = conv_output_shapes[-1]
                        else:
                            output_shape = [
                                conv_output_length(x, self.kernel_size,
                                                   self.padding, self.stride)
                                for x in conv_output_shapes[-1]
                            ]

                        conv_output_shapes.append(output_shape)

                        self.conv_1d_layers.append(l)

                    self.conv_output_shapes = conv_output_shapes

                    if self.project_inputs:
                        self.projection = tf.keras.layers.Dense(
                            self.conv_output_shapes[-1][0] * out_dim,
                            input_shape=[
                                self.conv_output_shapes[0][0] *
                                self.conv_output_shapes[0][1]
                            ],
                            kernel_initializer=self.kernel_initializer,
                            kernel_regularizer=self.kernel_regularizer,
                            bias_initializer=self.bias_initializer)

            self.built = True