Ejemplo n.º 1
0
    def build(self, input_shape):

        self.h_i, self.w_i, self.ch_i, self.n_i = input_shape[1:5]

        self.h_j, self.w_j = [conv_utils.conv_output_length(input_shape[i + 1],
                                                            self.kernel_size[i],
                                                            padding=self.padding,
                                                            stride=self.strides[i],
                                                            dilation=self.dilation_rate[i]) for i in (0, 1)]

        self.ah_j, self.aw_j = [conv_utils.conv_output_length(input_shape[i + 1],
                                                              self.kernel_size[i],
                                                              padding=self.padding,
                                                              stride=1,
                                                              dilation=self.dilation_rate[i]) for i in (0, 1)]

        self.w_shape = self.kernel_size + (self.ch_i, self.n_i,
                                           self.ch_j, self.n_j)

        self.w = self.add_weight(shape=self.w_shape,
                                 initializer=self.kernel_initializer,
                                 name='kernel',
                                 regularizer=self.kernel_regularizer,
                                 constraint=self.kernel_constraint)

        self.built = True
Ejemplo n.º 2
0
 def compute_output_shape(self, input_shape):
     if self.data_format == 'channels_last':
         space = input_shape[1:-1]
         new_space = []
         for i in range(len(space)):
             new_dim = conv_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)
         out_filters = input_shape[3] * self.depth_multiplier
         return (input_shape[0], ) + tuple(new_space) + (out_filters, )
     if self.data_format == 'channels_first':
         space = input_shape[2:]
         new_space = []
         for i in range(len(space)):
             new_dim = conv_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)
         out_filters = input_shape[1] * self.depth_multiplier
         return (input_shape[0], out_filters) + tuple(new_space)
Ejemplo n.º 3
0
    def compute_output_shape(self, input_shape):
        if self.data_format == 'channels_last':
            space = input_shape[1:-1]
            new_space = []
            for i in range(len(space)):
                new_dim = conv_utils.conv_output_length(space[i],
                                                        self.kernel_size[i],
                                                        padding=self.padding,
                                                        stride=self.strides[i])
                new_space.append(new_dim)
            if (self.output_format == 'image'):
                return (input_shape[0], ) + tuple(new_space) + (
                    self.filters, ) + (1, )
            else:
                return (input_shape[0], ) + tuple(new_space) + (self.filters, )

        if self.data_format == 'channels_first':
            raise NotImplementedError(
                "Output formate image/signal not handled")
            space = input_shape[2:]
            new_space = []
            for i in range(len(space)):
                new_dim = conv_utils.conv_output_length(space[i],
                                                        self.kernel_size[i],
                                                        padding=self.padding,
                                                        stride=self.strides[i])
                new_space.append(new_dim)
            return (input_shape[0], self.filters) + tuple(new_space)
Ejemplo n.º 4
0
    def compute_output_shape(self, input_shape):
        if self.data_format == 'channels_first':
            depth = input_shape[2]
            rows = input_shape[3]
            cols = input_shape[4]
            out_filters = self.groups * self.depth_multiplier
        elif self.data_format == 'channels_last':
            depth = input_shape[1]
            rows = input_shape[2]
            cols = input_shape[3]
            out_filters = self.groups * self.depth_multiplier

        depth = conv_utils.conv_output_length(depth, self.kernel_size[0],
                                              self.padding, self.strides[0])

        rows = conv_utils.conv_output_length(rows, self.kernel_size[1],
                                             self.padding, self.strides[1])

        cols = conv_utils.conv_output_length(cols, self.kernel_size[2],
                                             self.padding, self.strides[2])

        if self.data_format == 'channels_first':
            return (input_shape[0], out_filters, depth, rows, cols)

        elif self.data_format == 'channels_last':
            return (input_shape[0], depth, rows, cols, out_filters)
Ejemplo n.º 5
0
 def compute_output_shape(self, input_shape):
     input_shapes = input_shape
     assert (input_shapes[0] == input_shapes[1])
     input_shape = input_shapes[0]
     if self.data_format == 'channels_last':
         space = input_shape[1:-1]
         new_space = []
         for i in range(len(space)):
             new_dim = conv_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)
         single_shape = (input_shape[0], ) + tuple(new_space) + (
             self.filters, )
     elif self.data_format == 'channels_first':
         space = input_shape[2:]
         new_space = []
         for i in range(len(space)):
             new_dim = conv_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)
         single_shape = (input_shape[0], self.filters) + tuple(new_space)
     else:
         raise ValueError('Invalid data format: ' + self.data_format)
     return [single_shape] * 2
Ejemplo n.º 6
0
    def compute_output_shape(self, input_shapes):
        if K.image_data_format() == 'channels_first':
            space = input_shapes[2:]
            new_space = []
            for i in range(len(space)):
                new_dim = conv_utils.conv_output_length(
                    space[i],
                    self._kernel_shape[i],
                    padding=self._padding,
                    stride=self._strides[i],
                    dilation=self._rates[i])
                new_space.append(new_dim)

        if K.image_data_format() == 'channels_last':
            space = input_shapes[1:-1]
            new_space = []
            for i in range(len(space)):
                new_dim = conv_utils.conv_output_length(
                    space[i],
                    self._kernel_shape[i],
                    padding=self._padding,
                    stride=self._strides[i],
                    dilation=self._rates[i])
                new_space.append(new_dim)

        return ((input_shapes[0], ) + tuple(new_space) +
                (np.product(self._kernel_shape) * self._depth, ))
Ejemplo n.º 7
0
    def compute_output_shape(self, input_shape):
        if self.data_format == 'channels_last':
            space = input_shape[1:-1]
            new_space = []
            for i in range(len(space)):
                new_dim = conv_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 (input_shape[0],) + tuple(new_space) + (self.filters,)
        elif self.data_format == 'channels_first':
            space = input_shape[2:]
            new_space = []
            for i in range(len(space)):
                new_dim = conv_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 (input_shape[0], self.filters) + tuple(new_space)
        else:
            raise ValueError('Invalid `data_format`. Expected one of: "channels_first", "channels_last".')
Ejemplo n.º 8
0
 def compute_output_shape(self, input_shape):
     kernel_size = list(self.kernel_size)
     if self.kernel_size[0] % 2:
         kernel_size[0] = kernel_size[0] * 2 - 1
     else:
         kernel_size[0] = kernel_size[0] * 2
     kernel_size = tuple(kernel_size)
     if self.data_format == 'channels_last':
         space = input_shape[1:-1]
         new_space = []
         for i in range(len(space)):
             new_dim = conv_utils.conv_output_length(
                 space[i],
                 kernel_size[i],
                 padding=self.padding,
                 stride=self.strides[i],
                 dilation=self.dilation_rate[i])
             new_space.append(new_dim)
         return (input_shape[0], ) + tuple(new_space) + (self.filters, )
     if self.data_format == 'channels_first':
         space = input_shape[2:]
         new_space = []
         for i in range(len(space)):
             new_dim = conv_utils.conv_output_length(
                 space[i],
                 kernel_size[i],
                 padding=self.padding,
                 stride=self.strides[i],
                 dilation=self.dilation_rate[i])
             new_space.append(new_dim)
         return (input_shape[0], self.filters) + tuple(new_space)
Ejemplo n.º 9
0
 def compute_output_shape(self, input_shape):
     sample_space = (-1, ) + tuple(input_shape[1:-3])
     if self.data_format == 'channels_last':
         space = input_shape[-3:-1]
         new_space = []
         for i in range(len(space)):
             new_dim = conv_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 sample_space + tuple(new_space) + (self.filters, )
     if self.data_format == 'channels_first':
         space = input_shape[-2:]
         new_space = []
         for i in range(len(space)):
             new_dim = conv_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 sample_space + (self.filters, ) + tuple(new_space)
 def compute_output_shape(self, input_shape):
     if self.data_format == 'channels_last':
         space = input_shape[0][1:-1]
         new_space = []
         for i in range(len(space)):
             new_dim = conv_utils.conv_output_length(
                 space[i],
                 self.kernel_size[i],
                 padding='same',
                 stride=self.strides[i],
                 dilation=self.dilation_rate[i])
             new_space.append(new_dim)
         new_shape = (input_shape[0][0], ) + tuple(new_space) + (
             self.filters, )
         return [new_shape, new_shape]
     if self.data_format == 'channels_first':
         space = input_shape[2:]
         new_space = []
         for i in range(len(space)):
             new_dim = conv_utils.conv_output_length(
                 space[i],
                 self.kernel_size[i],
                 padding='same',
                 stride=self.strides[i],
                 dilation=self.dilation_rate[i])
             new_space.append(new_dim)
         new_shape = (input_shape[0], self.filters) + tuple(new_space)
         return [new_shape, new_shape]
Ejemplo n.º 11
0
    def compute_output_shape(self, input_shape):
        if self.data_format == "channels_first":
            rows = input_shape[2]
            cols = input_shape[3]
            out_filters = input_shape[1] * self.depth_multiplier
        elif self.data_format == "channels_last":
            rows = input_shape[1]
            cols = input_shape[2]
            out_filters = input_shape[3] * self.depth_multiplier

        rows = conv_utils.conv_output_length(
            rows,
            self.kernel_size[0],
            self.padding,
            self.strides[0],
            self.dilation_rate[0],
        )
        cols = conv_utils.conv_output_length(
            cols,
            self.kernel_size[1],
            self.padding,
            self.strides[1],
            self.dilation_rate[1],
        )
        if self.data_format == "channels_first":
            return (input_shape[0], out_filters, rows, cols)
        elif self.data_format == "channels_last":
            return (input_shape[0], rows, cols, out_filters)
Ejemplo n.º 12
0
    def compute_output_shape(self, input_shape):
        if self.data_format == 'channels_last':
            space = input_shape[1:-1]
            new_space = []
            for i in range(len(space)):
                new_dim = conv_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 (input_shape[0], ) + tuple(new_space) + (self.filters, )

        if self.data_format == 'channels_first':
            space = input_shape[2:]
            new_space = []
            for i in range(len(space)):
                new_dim = conv_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 (input_shape[0], self.filters) + tuple(new_space)

        else:
            raise ValueError(
                'The data format should be defined. Found `None`.')
Ejemplo n.º 13
0
 def compute_output_shape(self, input_shape):
     input_shape = tf.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 = conv_utils.conv_output_length(
         len_dim1, self.pool_size[0], self.padding, self.strides[0]
     )
     len_dim2 = conv_utils.conv_output_length(
         len_dim2, self.pool_size[1], self.padding, self.strides[1]
     )
     len_dim3 = conv_utils.conv_output_length(
         len_dim3, self.pool_size[2], self.padding, self.strides[2]
     )
     if self.data_format == "channels_first":
         return tf.TensorShape(
             [input_shape[0], input_shape[1], len_dim1, len_dim2, len_dim3]
         )
     else:
         return tf.TensorShape(
             [input_shape[0], len_dim1, len_dim2, len_dim3, input_shape[4]]
         )
Ejemplo n.º 14
0
 def compute_output_shape(self, input_shape):
     if self.data_format == 'channels_last':
         space = input_shape[1:-1]
         new_space = []
         for i in range(len(space)):
             new_dim = conv_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 (input_shape[0],) + tuple(new_space) + (self.filters,)
     if self.data_format == 'channels_first':
         space = input_shape[2:]
         new_space = []
         for i in range(len(space)):
             new_dim = conv_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 (input_shape[0], self.filters) + tuple(new_space)
Ejemplo n.º 15
0
    def build(self, input_shape):
        # input shape
        if self.data_format == 'channels_first':
            chn_dim = input_shape[1]
            len_dim1 = input_shape[2]
            len_dim2 = input_shape[3]
            len_dim3 = input_shape[4]
        elif self.data_format == 'channels_last':
            len_dim1 = input_shape[1]
            len_dim2 = input_shape[2]
            len_dim3 = input_shape[3]
            chn_dim = input_shape[4]

        odim = (len_dim1, len_dim2, len_dim3, chn_dim)

        # computer the out shape
        len_dim1 = conv_utils.conv_output_length(len_dim1, self.pool_size[0],
                                                 self.padding, self.strides[0])
        len_dim2 = conv_utils.conv_output_length(len_dim2, self.pool_size[1],
                                                 self.padding, self.strides[1])
        len_dim3 = conv_utils.conv_output_length(len_dim3, self.pool_size[2],
                                                 self.padding, self.strides[2])

        if self.data_format == 'channels_first':
            self.output_dim = (input_shape[0], input_shape[1], len_dim1,
                               len_dim2, len_dim3)

            shape_2d_0 = (chn_dim, odim[0] * odim[1], odim[2])
            shape_1d_1 = (chn_dim, odim[0] * len_dim2 * len_dim3)
            shape_f = (chn_dim, len_dim1, len_dim2, len_dim3)

        elif self.data_format == 'channels_last':
            self.output_dim = (input_shape[0], len_dim1, len_dim2, len_dim3,
                               input_shape[4])
            shape_2d_0 = (odim[0] * odim[1], odim[2], chn_dim)
            shape_2d_1 = (odim[0], len_dim2 * len_dim3, chn_dim)
            shape_f = (len_dim1, len_dim2, len_dim3, chn_dim)

        # a list of layers needed for 3D MaxPooling
        (p1, p2, p3) = self.pool_size

        self.layers = []
        self.layers.append(Reshape(shape_2d_0))
        self.layers.append(
            MaxPooling2D(
                pool_size=(p2, p3),  #strides=self.strides,          
                padding=self.padding,
                data_format=self.data_format))

        self.layers.append(Reshape(shape_2d_1))
        self.layers.append(
            MaxPooling2D(
                pool_size=(p1, 1),  #strides=self.strides,
                padding=self.padding,
                data_format=self.data_format))

        self.layers.append(Reshape(shape_f))

        super(MaxPooling3D, self).build(input_shape)
Ejemplo n.º 16
0
 def compute_output_shape(self, input_shape):
     padding = 'same'
     rows = input_shape[1]
     cols = input_shape[2]
     rows = conv_utils.conv_output_length(rows, self.pool_size[0], padding,
                                          self.strides[0])
     cols = conv_utils.conv_output_length(cols, self.pool_size[1], padding,
                                          self.strides[1])
     return (input_shape[0], rows, cols, input_shape[3])
Ejemplo n.º 17
0
 def test_output_shape(self):
     result = self.model.predict(self.data)
     final_x_length = conv_output_length(self.conv_x_length,
                                         self.mask_kernel[0], self.padding,
                                         self.mask_strides[0])
     final_y_length = conv_output_length(self.conv_y_length,
                                         self.mask_kernel[1], self.padding,
                                         self.mask_strides[1])
     feature_size = self.filters * final_x_length * final_y_length
     self.assertEqual(result.shape, (self.batch_size, feature_size))
Ejemplo n.º 18
0
 def _get_output_shape(self, input_shape):
     input_row, input_col, _ = self._get_input_shape(input_shape)
     output_row = conv_utils.conv_output_length(input_row,
                                                self.kernel_size[0],
                                                self.padding,
                                                self.strides[0])
     output_col = conv_utils.conv_output_length(input_col,
                                                self.kernel_size[1],
                                                self.padding,
                                                self.strides[1])
     return (input_shape[0], output_row, output_col, self.filters)
Ejemplo n.º 19
0
def test_conv_input_length():
    assert conv_utils.conv_input_length(None, 7, 'same', 1) is None
    assert conv_utils.conv_input_length(112, 7, 'same', 1) == 112
    assert conv_utils.conv_input_length(112, 7, 'same', 2) == 223
    assert conv_utils.conv_input_length(28, 5, 'valid', 1) == 32
    assert conv_utils.conv_input_length(14, 5, 'valid', 2) == 31
    assert conv_utils.conv_input_length(36, 5, 'full', 1) == 32
    assert conv_utils.conv_input_length(18, 5, 'full', 2) == 31

    with pytest.raises(AssertionError):
        conv_utils.conv_output_length(18, 5, 'diagonal', 2)
Ejemplo n.º 20
0
 def test_output_shape(self):
     result = self.model.predict(self.data)
     x_window_length = conv_output_length(self.x, self.kernel[0],
                                          self.padding, self.strides[0])
     y_window_length = conv_output_length(self.y, self.kernel[1],
                                          self.padding, self.strides[1])
     z_window_length = conv_output_length(self.z, self.kernel[2],
                                          self.padding, self.strides[2])
     self.assertEqual(result.shape,
                      (self.batch_size, x_window_length, y_window_length,
                       z_window_length, self.filters))
Ejemplo n.º 21
0
def test_conv_input_length():
    assert conv_utils.conv_input_length(None, 7, 'same', 1) is None
    assert conv_utils.conv_input_length(112, 7, 'same', 1) == 112
    assert conv_utils.conv_input_length(112, 7, 'same', 2) == 223
    assert conv_utils.conv_input_length(28, 5, 'valid', 1) == 32
    assert conv_utils.conv_input_length(14, 5, 'valid', 2) == 31
    assert conv_utils.conv_input_length(36, 5, 'full', 1) == 32
    assert conv_utils.conv_input_length(18, 5, 'full', 2) == 31

    with pytest.raises(AssertionError):
        conv_utils.conv_output_length(18, 5, 'diagonal', 2)
Ejemplo n.º 22
0
 def compute_output_shape(self, input_shape):
     batch_size = input_shape[0]
     h_axis, w_axis = 1, 2
     stride_h, stride_w = self.strides
     height, width = input_shape[h_axis], input_shape[w_axis]
     kernel_h, kernel_w = self.kernelSize
     
     out_height = conv_utils.conv_output_length(height, kernel_h, self.padding, stride_h)
     out_width = conv_utils.conv_output_length(width, kernel_w, self.padding, stride_w)
     output_shape = (batch_size, out_height, out_width, self.nOutputPlane)
     #print(output_shape)
     return output_shape
 def compute_output_shape(self,input_shape):
     
     rows = input_shape[1]
     cols = input_shape[2]
     rows = conv_utils.conv_output_length(rows, self.kernel_size[0],
                                          self.padding,
                                          self.strides[0])
     cols = conv_utils.conv_output_length(cols, self.kernel_size[1],
                                          self.padding,
                                          self.strides[1])
     
     return (input_shape[0],rows,cols,self.filters)
Ejemplo n.º 24
0
 def compute_output_shape(self, input_shape):
     ''' in case your layer modifies the shape of its input,
     you should specify here the shape transformation logic,
     as done in this case'''
     assert isinstance(input_shape, list)
     '''if self.data_format == 'channels_last':
         shape = input_shape[1:-1]
         new_shape = [conv_utils.conv_output_length(
                 space[i],
                 self.kernel_size[i],
                 padding='same',
                 stride=self.strides[i],
                 dilation=self.dilation_rate[i]) for i in range(len(shape))]
         new_shape = (input_shape[0][0],)+tuple(new_shape)+(input_shape[0][-1],)
         
     if self.data_format == 'channels_first':
         shape = input_shape[2:]
         new_shape = [conv_utils.conv_output_length(
                 space[i],
                 self.kernel_size[i],
                 padding='same',
                 stride=self.strides[i],
                 dilation=self.dilation_rate[i]) for i in range(len(shape))]
         new_shape = (input_shape[0][0],)+tuple(new_shape)+(input_shape[0][-1],)
         
     return [new_shape,new_shape]'''
     if self.data_format == 'channels_last':
         space = input_shape[0][1:-1]
         new_space = []
         for i in range(len(space)):
             new_dim = conv_utils.conv_output_length(
                 space[i],
                 self.kernel_size[i],
                 padding='same',
                 stride=self.strides[i],
                 dilation=self.dilation_rate[i])
             new_space.append(new_dim)
         new_shape = (input_shape[0][0], ) + tuple(new_space) + (
             self.filters, )
         return [new_shape, new_shape]
     if self.data_format == 'channels_first':
         space = input_shape[2:]
         new_space = []
         for i in range(len(space)):
             new_dim = conv_utils.conv_output_length(
                 space[i],
                 self.kernel_size[i],
                 padding='same',
                 stride=self.strides[i],
                 dilation=self.dilation_rate[i])
             new_space.append(new_dim)
         new_shape = (input_shape[0], self.filters) + tuple(new_space)
         return [new_shape, new_shape]
Ejemplo n.º 25
0
    def compute_output_shape(self, input_shape):
        if self.activation.startswith("avgpool"):
            out_shape = (input_shape[0], 1, 1, input_shape[3])
            return out_shape

        if self.maxpool_params is not None:
            mp = self.maxpool_params
            rows = conv_utils.conv_output_length(input_shape[1], mp['pool_size'][0], mp['padding'], mp['strides'][0])
            cols = conv_utils.conv_output_length(input_shape[2], mp['pool_size'][1], mp['padding'], mp['strides'][1])
            return (input_shape[0], rows, cols, input_shape[3])

        return input_shape
Ejemplo n.º 26
0
 def compute_output_shape(self, input_shape):
     height, width = input_shape[1:3]
     convolved_height = conv_output_length(input_length=height,
                                           filter_size=self.filter_size[0],
                                           padding=self.padding,
                                           stride=self.strides[0])
     convolved_width = conv_output_length(input_length=width,
                                          filter_size=self.filter_size[1],
                                          padding=self.padding,
                                          stride=self.strides[1])
     return (input_shape[0], convolved_height, convolved_width,
             self.num_capsule_types, self.num_caps_instantiations)
Ejemplo n.º 27
0
 def compute_output_shape(self, input_shape):
     if self.data_format == 'channels_first':
         rows = input_shape[2]
         cols = input_shape[3]
     elif self.data_format == 'channels_last':
         rows = input_shape[1]
         cols = input_shape[2]
     rows = conv_utils.conv_output_length(rows, self.strides, self.padding,
                                          self.strides)
     cols = conv_utils.conv_output_length(cols, self.strides, self.padding,
                                          self.strides)
     if self.data_format == 'channels_first':
         return (input_shape[0], input_shape[1], rows, cols)
     elif self.data_format == 'channels_last':
         return (input_shape[0], rows, cols, input_shape[3])
Ejemplo n.º 28
0
 def _compute_output_shape(input_shape):
     if self.data_format == "channels_first":
         rows = input_shape[2]
         cols = input_shape[3]
     elif self.data_format == "channels_last":
         rows = input_shape[1]
         cols = input_shape[2]
     rows = conv_utils.conv_output_length(rows, self.pool_size[0],
                                          self.padding, self.strides[0])
     cols = conv_utils.conv_output_length(cols, self.pool_size[1],
                                          self.padding, self.strides[1])
     if self.data_format == "channels_first":
         return (input_shape[0], input_shape[1], rows, cols)
     elif self.data_format == "channels_last":
         return (input_shape[0], rows, cols, input_shape[3])
Ejemplo n.º 29
0
	def compute_output_shape(self, input_shape):
		if self.data_format == 'channels_first':
			rows = input_shape[2]
			cols = input_shape[3]
		elif self.data_format == 'channels_last':
			rows = input_shape[1]
			cols = input_shape[2]

		rows = conv_utils.conv_output_length(rows, self.pool_size[0], padding='valid', stride=self.strides[0], dilation=self.dilation_rate)
		cols = conv_utils.conv_output_length(cols, self.pool_size[1], padding='valid', stride=self.strides[1], dilation=self.dilation_rate)

		if self.data_format == 'channels_first':
			return (input_shape[0], input_shape[1], rows, cols)
		elif self.data_format == 'channels_last':
			return (input_shape[0], rows, cols, input_shape[3])
Ejemplo n.º 30
0
 def get_output_shape(self, input_shape):
     if self.dim_ordering == 'tf':
         inp_rows = input_shape[0][1]
         inp_cols = input_shape[0][2]
     else:
         raise ValueError('Only support tensorflow.')
     rows = conv_output_length(inp_rows, self.kernel_size, self.border_mode,
                               1)
     cols = conv_output_length(inp_cols, self.kernel_size, self.border_mode,
                               1)
     out_r = conv_output_length(inp_rows, self.kernel_size,
                                self.border_mode, self.subsample[0])
     out_c = conv_output_length(inp_cols, self.kernel_size,
                                self.border_mode, self.subsample[1])
     return (input_shape[0][0], rows, cols, out_r * out_c)
Ejemplo n.º 31
0
 def compute_output_shape(self, input_shape):
     input_shape = tf.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 = conv_utils.conv_output_length(rows, self.pool_size[0],
                                          self.padding, self.strides[0])
     cols = conv_utils.conv_output_length(cols, self.pool_size[1],
                                          self.padding, self.strides[1])
     if self.data_format == "channels_first":
         return tf.TensorShape([input_shape[0], input_shape[1], rows, cols])
     else:
         return tf.TensorShape([input_shape[0], rows, cols, input_shape[3]])
    def compute_output_shape(self, input_shape):
        # Both components and input given
        if isinstance(input_shape, list) and len(input_shape) > 1:
            input_shape, kernel_shape = input_shape
        else:
            input_shape = input_shape
            kernel_shape = self.components.shape

        filters = kernel_shape[0] // self.n_replicas
        kernel_shape = kernel_shape[1:3]

        space = input_shape[1:-1]
        new_space = []
        for i in range(len(space)):
            new_dim = conv_utils.conv_output_length(
                space[i],
                kernel_shape[i],
                padding=self.padding,
                stride=self.strides[i],
                dilation=self.dilation_rate[i])
            new_space.append(new_dim)

        if self.n_replicas != 1:
            return (input_shape[0],) + tuple(new_space) + \
                   (filters, self.n_replicas)
        else:
            return (input_shape[0], ) + tuple(new_space) + (filters, )
Ejemplo n.º 33
0
    def compute_output_shape(self, input_shape):
        if self.data_format == 'channels_first':
            rows = input_shape[2]
            cols = input_shape[3]
        elif self.data_format == 'channels_last':
            rows = input_shape[1]
            cols = input_shape[2]

        rows = conv_utils.conv_output_length(rows, self.kernel_size[0],
                                             self.padding, self.strides[0])
        cols = conv_utils.conv_output_length(cols, self.kernel_size[1],
                                             self.padding, self.strides[1])

        if self.data_format == 'channels_first':
            return (input_shape[0], self.filters, rows, cols)
        elif self.data_format == 'channels_last':
            return (input_shape[0], rows, cols, self.filters)
Ejemplo n.º 34
0
    def compute_output_shape(self, input_shape):
        if self.data_format == 'channels_last':
            new_dim = conv_utils.conv_output_length(
                    input_shape[1],
                    self.kernel_size[0],
                    padding=self.padding,
                    stride=self.strides[0],
                    dilation=self.dilation_rate[0])

            return (input_shape[0],) + tuple([new_dim]) + (self.filters,)

        if self.data_format == 'channels_first':
            return tuple(23)
Ejemplo n.º 35
0
 def build(self, input_shape):
     if self.data_format == 'channels_last':
         input_row, input_col = input_shape[1:-1]
         input_filter = input_shape[3]
     else:
         input_row, input_col = input_shape[2:]
         input_filter = input_shape[1]
     if input_row is None or input_col is None:
         raise ValueError('The spatial dimensions of the inputs to '
                          ' a LocallyConnected2D layer '
                          'should be fully-defined, but layer received '
                          'the inputs shape ' + str(input_shape))
     output_row = conv_utils.conv_output_length(input_row, self.kernel_size[0],
                                                self.padding, self.strides[0])
     output_col = conv_utils.conv_output_length(input_col, self.kernel_size[1],
                                                self.padding, self.strides[1])
     self.output_row = output_row
     self.output_col = output_col
     self.kernel_shape = (output_row * output_col,
                          self.kernel_size[0] * self.kernel_size[1] * input_filter,
                          self.filters)
     self.kernel = self.add_weight(shape=self.kernel_shape,
                                   initializer=self.kernel_initializer,
                                   name='kernel',
                                   regularizer=self.kernel_regularizer,
                                   constraint=self.kernel_constraint)
     if self.use_bias:
         self.bias = self.add_weight(shape=(output_row, output_col, self.filters),
                                     initializer=self.bias_initializer,
                                     name='bias',
                                     regularizer=self.bias_regularizer,
                                     constraint=self.bias_constraint)
     else:
         self.bias = None
     if self.data_format == 'channels_first':
         self.input_spec = InputSpec(ndim=4, axes={1: input_filter})
     else:
         self.input_spec = InputSpec(ndim=4, axes={-1: input_filter})
     self.built = True
Ejemplo n.º 36
0
def test_conv_output_length():
    assert conv_utils.conv_output_length(None, 7, 'same', 1) is None
    assert conv_utils.conv_output_length(224, 7, 'same', 1) == 224
    assert conv_utils.conv_output_length(224, 7, 'same', 2) == 112
    assert conv_utils.conv_output_length(32, 5, 'valid', 1) == 28
    assert conv_utils.conv_output_length(32, 5, 'valid', 2) == 14
    assert conv_utils.conv_output_length(32, 5, 'causal', 1) == 32
    assert conv_utils.conv_output_length(32, 5, 'causal', 2) == 16
    assert conv_utils.conv_output_length(32, 5, 'full', 1) == 36
    assert conv_utils.conv_output_length(32, 5, 'full', 2) == 18

    with pytest.raises(AssertionError):
        conv_utils.conv_output_length(32, 5, 'diagonal', 2)