0

我正在尝试使用 Python 代码 caffe.io.oversample 函数(而不是分类器.py)仅返回带有 Caffe 过采样的中心裁剪。我试图修改代码以仅返回中心作物,但它仍然返回 10 而不是 1 作物。我已经重建了 caffe 和 pycaffe 但是情况仍然是一样的。如何让 python 代码只返回一种作物?我很困惑。我以前用过matcaffe,但我不知道python,我只是边走边想。谢谢!

修改部分:

def oversample(images, crop_dims, flow=False):
  """
  Crop images into the four corners, center, and their mirrored versions.

  Take
  image: iterable of (H x W x K) ndarrays
  crop_dims: (height, width) tuple for the crops.

  Give
  crops: (10*N x H x W x K) ndarray of crops for number of inputs N.
  """
  # Dimensions and center.
  im_shape = np.array(images[0].shape)
  crop_dims = np.array(crop_dims)
  center = im_shape[:2] / 2.0

  # Make crop coordinates
  # Take center crop.

          crop = np.tile(center, (1, 2))[0] + np.concatenate([
              -self.crop_dims / 2.0,
              self.crop_dims / 2.0
          ])
          crops = images[:, crop[0]:crop[2], crop[1]:crop[3], :]

  return crops

其原文:

def oversample(images, crop_dims, flow=False):
  """
  Crop images into the four corners, center, and their mirrored versions.

  Take
  image: iterable of (H x W x K) ndarrays
  crop_dims: (height, width) tuple for the crops.

  Give
  crops: (10*N x H x W x K) ndarray of crops for number of inputs N.
  """
  # Dimensions and center.
  im_shape = np.array(images[0].shape)
  crop_dims = np.array(crop_dims)
  im_center = im_shape[:2] / 2.0

  # Make crop coordinates
  h_indices = (0, im_shape[0] - crop_dims[0])
  w_indices = (0, im_shape[1] - crop_dims[1])
  crops_ix = np.empty((5, 4), dtype=int)
  curr = 0
  for i in h_indices:
      for j in w_indices:
          crops_ix[curr] = (i, j, i + crop_dims[0], j + crop_dims[1])
          curr += 1
  crops_ix[4] = np.tile(im_center, (1, 2)) + np.concatenate([
      -crop_dims / 2.0,
       crop_dims / 2.0
  ])
  crops_ix = np.tile(crops_ix, (2, 1))

  # Extract crops
  crops = np.empty((10 * len(images), crop_dims[0], crop_dims[1],
                        im_shape[-1]), dtype=np.float32)
  ix = 0
  for im in images:
      for crop in crops_ix:
          crops[ix] = im[crop[0]:crop[2], crop[1]:crop[3], :]
          ix += 1
      crops[ix-5:ix] = crops[ix-5:ix, :, ::-1, :]  # flip for mirrors
      if flow:  #if using a flow input, should flip first channel which  corresponds to x-flow
        crops[ix-5:ix,:,:,0] = 1-crops[ix-5:ix,:,:,0]
  return crops
4

1 回答 1

0

我试图做同样的事情。最后,我不得不使用数组切片来裁剪图像:

def crop_center(img, cropx, cropy):
    _,x,y = img.shape
    startx = x//2-(cropx//2)
    starty = y//2-(cropy//2)
    return img[:, starty:starty + cropy, startx:startx + cropx]

该函数的输入是一个 (C, W, H) 数组。

于 2018-05-06T09:51:29.350 回答