0

I have multiple 3D matrices that need to be stored in a hdf5 file. I have looked everywhere but i am confused about the process of storing data in hdf5 file. Can i store multiple 3D matrices in a single dataset in hdf5 file or do i have to create separate dataset for each 3D matrix? I need this dataset for training a 3D CNN model. If both ways are possible, which is the best for training purpose?

Additional information: Matrix size is 12x24x12 and a single data takes less than 100 kb storage space. Dataset is created in matlab and the resultant file will be used in python (where the model will be trained).

1 Answers1

2

In matlab you specify -v3.7 to save stuff in hdf5. You save your matrices like this:

mat0 = zeros(12,24,12);
mat1 = ones(12,24,12);
mat2 = 2*ones(12,24,12);
save data.mat mat0 mat1 mat2 -v7.3

In python you read them like this:

import numpy as np
import h5py
f = h5py.File('/home/innereye/data.mat','r')
mat1 = f.get('mat1')
mat1 = np.array(mat1)

you have a lot of material on Stackoverflow about reading mat files with python, for example, here and here.

Yuval Harpaz
  • 1,416
  • 1
  • 12
  • 16
  • I have to create a matrix at a time, save it in a file and then create another. By your method second matrix overrides first matrix. I tried different .mat files for different matrices but then i have to call them separately in python. Any method which prevents overriding or allows me to call a folder full of .mat files in python? – PlasticMan Feb 04 '20 at 18:00
  • There are many solutions here, but you should be clearer about what you want. You asked about hdf5 which is not the default way of saving matlab variables. You did not mention overriding issues. Please take a look at https://stackoverflow.com/help/how-to-ask page, particularly at "how to create a minimal...example. You are basically expected to write your own solution, and ask us to help with things in your code that don't work. – Yuval Harpaz Feb 05 '20 at 07:43