предварительная обработка изображения в Tensorflow vs Caffe - PullRequest
0 голосов
/ 09 сентября 2018

Я пытаюсь имитировать набор преобразований, которым подвергается входное изображение в Caffe.io.transformer в тензорном потоке.Вот соответствующий фрагмент кода для caffe (для справки data.shape: 3,224, 224):

    self._transformer = caffe.io.Transformer({self._dataLayerName: self._net.blobs[self._dataLayerName].data.shape})
    self._transformer.set_transpose(self._dataLayerName, (2,0,1))
    self._transformer.set_mean(self._dataLayerName, [[[131.26315308]],[[140.62084961]],[[142.71440125]]])
    self._transformer.set_raw_scale(self._dataLayerName,255)
    self._transformer.set_channel_swap(self._dataLayerName,(2,1,0))

Вот моя попытка воспроизвести это в тензорном потоке:

   def preprocess_image(image, height, width, central_fraction=0.875):
    image = tf.to_float(image)
    if height and width:
      # Resize the image to the specified height and width.
      image = tf.expand_dims(image, 0)
      image = tf.image.resize_bilinear(image, [height, width],
                   align_corners=False)
      image = tf.squeeze(image, [0])
    if central_fraction:
      # Crop the central region of the image with an area containing 87.5% of the original image.
      image = tf.image.central_crop(image, central_fraction=central_fraction)

    # Channel last to channel first
    image = tf.transpose(image, [2, 0, 1])

    # Mean subtraction
    image = tf.subtract(image, [[[131.26315308]],[[140.62084961]],[[142.71440125]]])

    # BGR to RGB
    image = image[..., ::-1]

    return image

  preprocess_image(image, 256, 256)
...