我正在尝试模拟张量流中Caffe.io.transformer
中输入图像经过的一组变换。这是caffe的相关代码段(用于参考数据。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)