caffe调整图像大小使所有值都为0

caffe resize image make all values 0

我在使用caffe从图像生成HDF5数据文件时遇到了这个问题。

caffe.io.load_image 将图像加载到标准化范围 0-1 中的变量中。

调整大小,但 img 中的所有值都转换为零

    img = caffe.io.load_image( patht_to_file )
    print img.shape
    print img.dtype
    img = caffe.io.resize( img, (3,SIZE, SIZE) ) #resizes but all values in img converted to zero
    print img.shape
    print img.dtype

我得到的输出是

  (240, 320, 3) 
  float32
  (3, 58, 58) 
  float64

img 的某些值更改为全 0 谁能帮我解决这个问题。

我想要相同的 float32 改变 resize 命令的顺序给出正确的输出 将实数非零值赋给 var img 但是订单和类型不是我需要的

img = caffe.io.resize( img, (SIZE, SIZE, 3) ) # Gives real non zero values to var img
print img.shape
print img.dtype

输出

(240, 320, 3) 
float32
(58, 58, 3) 
float64

我需要形状为 3,58,58 而不将所有值都转换为零,即真实数据

出了什么问题?

你的img是一个三维数组,但是不同的维度有不同的含义。前两个 (240,320) 空间 维度,而第三个 (3) 是 "channel"(或 "feature")维度。当 resize-ing 图像时,您想要更改 spatial 维度甚至混合(插值)相邻值,但您始终希望保持通道完整:您不希望混合 "red" 值以确定相邻像素的 "green" 值。

在 caffe 中,为了获取形状为 (240,320,3) 的输入图像并将其转换为形状 (3,58,58),您需要做 两个 事情:
1. 使用 caffe.io.resize_imagespatial 维度从 (240,320) 更改为 (58,58)。此阶段将导致 img 形状 (58,58,3)
2. 第二阶段是移动/transpose img 的维度,以便通道维度成为第一个:img.transpose((2,0,1)).

解决方案:

# first, resize the spatial dimensions, do not touch the channels
img = caffe.io.resize_image( img, (SIZE,SIZE), interp_order=3 )
# transpose the dimensions from H-W-C to C-H-W
img = img.transpose( (2,0,1) )

有关详细信息,请参阅 resize_image

PS,
将 RGB 图像更改为 BGR 也很常见。你跳过了这个阶段。是故意的吗?