Unverified Commit 9219d4ed authored by Bharath Ramsundar's avatar Bharath Ramsundar Committed by GitHub
Browse files

Merge pull request #1450 from peastman/bugs

Fixed errors in computing layer output shapes and loading images
parents cce55b72 8a1ad36d
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -369,6 +369,7 @@ class ImageLoader(DataLoader):
      remainder = []
      for input_file in input_files:
        filename, extension = os.path.splitext(input_file)
        extension = extension.lower()
        # TODO(rbharath): Add support for more extensions
        if os.path.isdir(input_file):
          dirfiles = [
@@ -386,6 +387,7 @@ class ImageLoader(DataLoader):
          ]
          for zip_file in zip_files:
            _, extension = os.path.splitext(zip_file)
            extension = extension.lower()
            if extension in [".png", ".tif"]:
              image_files.append(zip_file)
        elif extension in [".png", ".tif"]:
@@ -405,6 +407,7 @@ class ImageLoader(DataLoader):
    images = []
    for image_file in image_files:
      _, extension = os.path.splitext(image_file)
      extension = extension.lower()
      if extension == ".png":
        image = misc.imread(image_file)
        images.append(image)
+37 −6
Original line number Diff line number Diff line
@@ -446,6 +446,16 @@ class SharedVariableScope(Layer):
      return self._shared_with._get_scope_name()


def _conv_size(width, size, stride, padding):
  """Compute the output size of a convolutional layer."""
  if padding.lower() == 'valid':
    return 1 + (width - size) // stride
  elif padding.lower() == 'same':
    return 1 + (width - 1) // stride
  else:
    raise ValueError('Unknown padding type: %s' % padding)


class Conv1D(Layer):
  """A 1D convolution on the input.

@@ -485,7 +495,6 @@ class Conv1D(Layer):
      `(10, 128)` for sequences of 10 vectors of 128-dimensional vectors,
      or `(None, 128)` for variable-length sequences of 128-dimensional vectors.

      TODO(LESWING): Calculate output shape at construction time
      Arguments:
          filters: Integer, the dimensionality of the output space
              (i.e. the number output of filters in the convolution).
@@ -541,6 +550,17 @@ class Conv1D(Layer):
    self.kernel_constraint = kernel_constraint
    self.bias_constraint = bias_constraint
    super(Conv1D, self).__init__(in_layers, **kwargs)
    try:
      parent_shape = self.in_layers[0].shape
      if isinstance(strides, int):
        strides = (strides,)
      if isinstance(kernel_size, int):
        kernel_size = (kernel_size,)
      self._shape = (parent_shape[0],
                     _conv_size(parent_shape[1], kernel_size[0], strides[0],
                                padding), filters)
    except:
      pass

  def _build_layer(self):
    return tf.keras.layers.Conv1D(
@@ -1983,8 +2003,13 @@ class Conv2D(SharedVariableScope):
      strides = stride
      if isinstance(stride, int):
        strides = (stride, stride)
      self._shape = (parent_shape[0], parent_shape[1] // strides[0],
                     parent_shape[2] // strides[1], num_outputs)
      if isinstance(kernel_size, int):
        kernel_size = (kernel_size, kernel_size)
      self._shape = (parent_shape[0],
                     _conv_size(parent_shape[1], kernel_size[0], strides[0],
                                padding),
                     _conv_size(parent_shape[2], kernel_size[1], strides[1],
                                padding), num_outputs)
    except:
      pass

@@ -2100,9 +2125,15 @@ class Conv3D(SharedVariableScope):
      strides = stride
      if isinstance(stride, int):
        strides = (stride, stride, stride)
      self._shape = (parent_shape[0], parent_shape[1] // strides[0],
                     parent_shape[2] // strides[1],
                     parent_shape[3] // strides[2], num_outputs)
      if isinstance(kernel_size, int):
        kernel_size = (kernel_size, kernel_size, kernel_size)
      self._shape = (parent_shape[0],
                     _conv_size(parent_shape[1], kernel_size[0], strides[0],
                                padding),
                     _conv_size(parent_shape[2], kernel_size[1], strides[1],
                                padding),
                     _conv_size(parent_shape[3], kernel_size[2], strides[2],
                                padding), num_outputs)
    except:
      pass