Search code examples
luahdf5torchtensor

Error when saving images to an HDF5 file in Lua


When trying to a bunch of images in Lua to an HDF5 file, I get the following error:

/home/ubuntu/torch/install/bin/luajit: /home/ubuntu/torch/install/share/lua/5.1/hdf5/group.lua:97: attempt to call method 'adjustForData' (a nil value)
stack traceback:
        /home/ubuntu/torch/install/share/lua/5.1/hdf5/group.lua:97: in function '_writeData'
        /home/ubuntu/torch/install/share/lua/5.1/hdf5/group.lua:307: in function '_write_or_append'
        /home/ubuntu/torch/install/share/lua/5.1/hdf5/group.lua:270: in function </home/ubuntu/torch/install/share/lua/5.1/hdf5/group.lua:269>
        /home/ubuntu/torch/install/share/lua/5.1/hdf5/file.lua:84: in function '_write_or_append'
        /home/ubuntu/torch/install/share/lua/5.1/hdf5/file.lua:58: in function 'write'

This is where the error occurs:

for i = 1, #input_images_caffe do
   newFile:write('images', input_images_caffe[i], 'w')
end

The images inside input_image_caffe, come from:

  local input_size = math.ceil(params.input_scale * params.image_size)
  local input_image_list = params.input_image:split(',')
  local input_images_caffe = {}
  local img_caffe
  for _, img_path in ipairs(input_image_list) do
    local img = image.load(img_path, 3)
    img = image.scale(img, input_size, 'bilinear')
    img_caffe = preprocess(img):float()
    table.insert(input_images_caffe, img_caffe)
  end

This function is used to preprocess the images:

function preprocess(img)
  local mean_pixel = torch.DoubleTensor({103.939, 116.779, 123.68})
  local perm = torch.LongTensor{3, 2, 1}
  img = img:index(1, perm):mul(256.0)
  mean_pixel = mean_pixel:view(3, 1, 1):expandAs(img)
  img:add(-1, mean_pixel)
  return img
end

Some examples of what input_images_caffe could contain:

{
  1 : FloatTensor - size: 3x405x512
  2 : FloatTensor - size: 3x512x393
}

Or:

{
  1 : FloatTensor - size: 3x405x512
}

The HDF5 is created with:

  local newFile = hdf5.open(params.output_hdf5, 'w')

And I am using the torch-hdf5 library:

https://github.com/deepmind/torch-hdf5

I am not sure what I am doing wrong here?


Solution

  • newFile:write('images', input_images_caffe[i], 'w')

    Try with newFile:write('images', input_images_caffe[i]) instead. The third parameter should be an (optional) options object, but you are passing a string, which doesn't have adjustForData method, hence the error you are getting.