Пример #1
0
 def build(self, input_shape):
     input_row, input_col, input_depth = input_shape[1:]
     self.output_row = conv_utils.conv_output_length(
         input_row, self.kernel_size[0], self.padding, self.strides[0])
     self.output_col = conv_utils.conv_output_length(
         input_col, self.kernel_size[1], self.padding, self.strides[1])
     self.kernel_shape = (self.output_row * self.output_col,
                          self.kernel_size[0] * self.kernel_size[1], 1)
     # print("Kernel shape", self.kernel_shape)
     self.splitaxis = split_axis(input_shape)
     self.kernels = [
         self.add_weight(shape=self.kernel_shape,
                         initializer=self.kernel_initializer,
                         name='kernel' + str(i),
                         regularizer=self.kernel_regularizer,
                         constraint=None)
         for i in range(len(self.splitaxis))
     ]
     if self.use_bias:
         self.bias = self.add_weight(shape=(self.output_row,
                                            self.output_col, input_depth),
                                     initializer=self.bias_initializer,
                                     name='bias',
                                     regularizer=None,
                                     constraint=None)
     else:
         self.bias = None
Пример #2
0
    def compute_output_shape(self, input_shape):
        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],
                                             padding=self.padding,
                                             stride=self.strides[0],
                                             dilation=self.dilation_rate)

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

        if self.data_format == 'channels_first':
            output_shape = (input_shape[0], input_shape[1], rows, cols)
        else:
            output_shape = (input_shape[0], rows, cols, input_shape[3])

        return output_shape
  def _compute_output_shape(self, input_shape):
    if isinstance(input_shape, list):
      input_shape = input_shape[0]
    input_shape = tensor_shape.TensorShape(input_shape).as_list()
    if self.data_format == 'channels_first':
      rows = input_shape[3]
      cols = input_shape[4]
    elif self.data_format == 'channels_last':
      rows = input_shape[2]
      cols = input_shape[3]
    rows = conv_utils.conv_output_length(
        rows,
        self.kernel_size[0],
        padding=self.padding,
        stride=self.strides[0],
        dilation=self.dilation_rate[0])
    cols = conv_utils.conv_output_length(
        cols,
        self.kernel_size[1],
        padding=self.padding,
        stride=self.strides[1],
        dilation=self.dilation_rate[1])
    if self.return_sequences:
      if self.data_format == 'channels_first':
        output_shape = [input_shape[0], input_shape[1],
                        self.filters, rows, cols]
      elif self.data_format == 'channels_last':
        output_shape = [input_shape[0], input_shape[1],
                        rows, cols, self.filters]
    else:
      if self.data_format == 'channels_first':
        output_shape = [input_shape[0], self.filters, rows, cols]
      elif self.data_format == 'channels_last':
        output_shape = [input_shape[0], rows, cols, self.filters]

    if self.return_state:
      if self.data_format == 'channels_first':
        output_shapes = [output_shape] + [(input_shape[0],
                                           self.filters,
                                           rows,
                                           cols) for _ in range(2)]
      elif self.data_format == 'channels_last':
        output_shapes = [output_shape] + [(input_shape[0],
                                           rows,
                                           cols,
                                           self.filters) for _ in range(2)]
      return [tensor_shape.TensorShape(shape) for shape in output_shapes]
    return tensor_shape.TensorShape(output_shape)
    def compute_output_shape(self, input_shape):
        if isinstance(input_shape, list):
            input_shape = input_shape[0]
        input_shape = tensor_shape.TensorShape(input_shape).as_list()
        if self.data_format == 'channels_first':
            rows = input_shape[3]
            cols = input_shape[4]
        elif self.data_format == 'channels_last':
            rows = input_shape[2]
            cols = input_shape[3]
        rows = conv_utils.conv_output_length(rows,
                                             self.kernel_size[0],
                                             padding=self.padding,
                                             stride=self.strides[0],
                                             dilation=self.dilation_rate[0])
        cols = conv_utils.conv_output_length(cols,
                                             self.kernel_size[1],
                                             padding=self.padding,
                                             stride=self.strides[1],
                                             dilation=self.dilation_rate[1])
        if self.return_sequences:
            if self.data_format == 'channels_first':
                output_shape = [
                    input_shape[0], input_shape[1], self.filters, rows, cols
                ]
            elif self.data_format == 'channels_last':
                output_shape = [
                    input_shape[0], input_shape[1], rows, cols, self.filters
                ]
        else:
            if self.data_format == 'channels_first':
                output_shape = [input_shape[0], self.filters, rows, cols]
            elif self.data_format == 'channels_last':
                output_shape = [input_shape[0], rows, cols, self.filters]

        if self.return_state:
            if self.data_format == 'channels_first':
                output_shapes = [output_shape] + [
                    (input_shape[0], self.filters, rows, cols)
                    for _ in range(2)
                ]
            elif self.data_format == 'channels_last':
                output_shapes = [output_shape] + [
                    (input_shape[0], rows, cols, self.filters)
                    for _ in range(2)
                ]
            return [tensor_shape.TensorShape(shape) for shape in output_shapes]
        return tensor_shape.TensorShape(output_shape)
Пример #5
0
 def compute_output_shape(self, input_shape):
     input_shape = tensor_shape.TensorShape(input_shape).as_list()
     length = conv_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]])
Пример #6
0
 def build(self, input_shape):
   input_dim = input_shape[2]
   if input_dim is None:
     raise ValueError('Axis 2 of input should be fully-defined. '
                      'Found shape:', input_shape)
   output_length = conv_utils.conv_output_length(
       input_shape[1], self.kernel_size[0], self.padding, self.strides[0])
   self.kernel_shape = (output_length, self.kernel_size[0] * input_dim,
                        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_length, self.filters),
         initializer=self.bias_initializer,
         name='bias',
         regularizer=self.bias_regularizer,
         constraint=self.bias_constraint)
   else:
     self.bias = None
   self.input_spec = InputSpec(ndim=3, axes={2: input_dim})
   self.built = True
Пример #7
0
 def build(self, input_shape):
     input_dim = input_shape[2]
     if input_dim is None:
         raise ValueError(
             'Axis 2 of input should be fully-defined. '
             'Found shape:', input_shape)
     output_length = conv_utils.conv_output_length(input_shape[1],
                                                   self.kernel_size[0],
                                                   self.padding,
                                                   self.strides[0])
     self.kernel_shape = (output_length, self.kernel_size[0] * input_dim,
                          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_length, self.filters),
                                     initializer=self.bias_initializer,
                                     name='bias',
                                     regularizer=self.bias_regularizer,
                                     constraint=self.bias_constraint)
     else:
         self.bias = None
     self.input_spec = InputSpec(ndim=3, axes={2: input_dim})
     self.built = True
Пример #8
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)
Пример #9
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.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 tf.TensorShape([input_shape[0], self.filters, rows, cols])
    else:
      return tf.TensorShape([input_shape[0], rows, cols, self.filters])
Пример #10
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)
Пример #11
0
    def build(self, input_shape):
        input_shape = tensor_shape.TensorShape(input_shape).as_list()
        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
Пример #12
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 = 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 tensor_shape.TensorShape(
             [input_shape[0], input_shape[1], rows, cols])
     else:
         return tensor_shape.TensorShape(
             [input_shape[0], rows, cols, input_shape[3]])
Пример #13
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 = 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 tensor_shape.TensorShape(
         [input_shape[0], input_shape[1], rows, cols])
   else:
     return tensor_shape.TensorShape(
         [input_shape[0], rows, cols, input_shape[3]])
Пример #14
0
  def build(self, input_shape):
    input_shape = tensor_shape.TensorShape(input_shape).as_list()
    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
Пример #15
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 = 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 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]])
  def compute_output_shape(self, input_shape):
    if isinstance(input_shape, list):
      input_shape = input_shape[0]

    cell = self.cell
    if cell.data_format == 'channels_first':
      rows = input_shape[3]
      cols = input_shape[4]
    elif cell.data_format == 'channels_last':
      rows = input_shape[2]
      cols = input_shape[3]
    rows = conv_utils.conv_output_length(rows,
                                         cell.kernel_size[0],
                                         padding=cell.padding,
                                         stride=cell.strides[0],
                                         dilation=cell.dilation_rate[0])
    cols = conv_utils.conv_output_length(cols,
                                         cell.kernel_size[1],
                                         padding=cell.padding,
                                         stride=cell.strides[1],
                                         dilation=cell.dilation_rate[1])

    if cell.data_format == 'channels_first':
      output_shape = input_shape[:2] + (cell.filters, rows, cols)
    elif cell.data_format == 'channels_last':
      output_shape = input_shape[:2] + (rows, cols, cell.filters)

    if not self.return_sequences:
      output_shape = output_shape[:1] + output_shape[2:]

    if self.return_state:
      output_shape = [output_shape]
      if cell.data_format == 'channels_first':
        output_shape += [(input_shape[0], cell.filters, rows, cols)
                         for _ in range(2)]
      elif cell.data_format == 'channels_last':
        output_shape += [(input_shape[0], rows, cols, cell.filters)
                         for _ in range(2)]
    return output_shape
Пример #17
0
    def compute_output_shape(self, input_shape):
        if isinstance(input_shape, list):
            input_shape = input_shape[0]

        cell = self.cell
        if cell.data_format == 'channels_first':
            rows = input_shape[3]
            cols = input_shape[4]
        elif cell.data_format == 'channels_last':
            rows = input_shape[2]
            cols = input_shape[3]
        rows = conv_utils.conv_output_length(rows,
                                             cell.kernel_size[0],
                                             padding=cell.padding,
                                             stride=cell.strides[0],
                                             dilation=cell.dilation_rate[0])
        cols = conv_utils.conv_output_length(cols,
                                             cell.kernel_size[1],
                                             padding=cell.padding,
                                             stride=cell.strides[1],
                                             dilation=cell.dilation_rate[1])

        if cell.data_format == 'channels_first':
            output_shape = input_shape[:2] + (cell.filters, rows, cols)
        elif cell.data_format == 'channels_last':
            output_shape = input_shape[:2] + (rows, cols, cell.filters)

        if not self.return_sequences:
            output_shape = output_shape[:1] + output_shape[2:]

        if self.return_state:
            output_shape = [output_shape]
            if cell.data_format == 'channels_first':
                output_shape += [(input_shape[0], cell.filters, rows, cols)
                                 for _ in range(2)]
            elif cell.data_format == 'channels_last':
                output_shape += [(input_shape[0], rows, cols, cell.filters)
                                 for _ in range(2)]
        return output_shape
Пример #18
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 = 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 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]])
Пример #19
0
 def compute_output_shape(self, input_shape):
     length = conv_utils.conv_output_length(input_shape[1],
                                            self.kernel_size[0],
                                            self.padding, self.strides[0])
     return (input_shape[0], length, self.filters)
Пример #20
0
 def _compute_output_shape(self, input_shape):
   input_shape = tensor_shape.TensorShape(input_shape).as_list()
   length = conv_utils.conv_output_length(input_shape[1], self.kernel_size[0],
                                          self.padding, self.strides[0])
   return tensor_shape.TensorShape([input_shape[0], length, self.filters])
Пример #21
0
 def compute_output_shape(self, input_shape):
   length = conv_utils.conv_output_length(input_shape[1], self.kernel_size[0],
                                          self.padding, self.strides[0])
   return (input_shape[0], length, self.filters)