Pytorch + HDF5 appending spectograms

Hi everyone,

My goal:
To load spectograms 1 by 1 (It is because my preprocessing - Has to be done this way) into a HDF5 file then load this file into Pytorch with Customdataset (I am also struggling with this) method and Dataloader. Could you please help me understand the following error code?

Yes I know that it’s probably of the diferring dimensions of the maxshape (96, 96, 3)/ tuple object/ plots (96,96,3). I have no idea how to resolve this conflict. :frowning:

My code:

“”"
import h5py

hdf5_dir = “G:/Drive/Folder/spectograms/”

h5_file = h5py.File(hdf5_dir + “train_combined.h5”, “w”)

h5_file.close()

h5_file = h5py.File(hdf5_dir + “train_combined.h5”, “a”)

def store_single_image_to_hdf5(image, label, h5_file):

if not h5_file.keys():

    dataset = h5_file.create_dataset(

    "image", data=image, compression="gzip", chunks=True, maxshape=(None, 96, 96, 3)

    )

    meta_set = h5_file.create_dataset(

        "label", data=label, compression="gzip", chunks=True, maxshape=(None, 1)

    )

else:

    h5_file["image"].resize((h5_file["image"].shape[0] + image.shape[0]), axis=0)

    h5_file["image"][-image.shape[0]:] = image

    h5_file["label"].resize((h5_file["label"].shape[0] + label.shape[0]), axis=0)

    h5_file["label"][-label.shape[0]:] = label

“”"

Error message:
“”"
~\AppData\Local\Temp/ipykernel_15372/2618725217.py in plot_specgram**(waveform, sample_rate, title, xlim, ylim, folder, file_name, x_train_or_x_test)** 41 #image_array = torch.from_numpy(image_array) 42 —> 43 store_single_image_to_hdf5**(** image_array**,** label**,** h5_file**)** ~\AppData\Local\Temp/ipykernel_15372/112584867.py in store_single_image_to_hdf5**(image, label, h5_file)** 10 def store_single_image_to_hdf5**(** image**,** label**,** h5_file**)** : 11 if not h5_file**.** keys**(** ) : —> 12 dataset = h5_file.create_dataset( 13 “image” , data**=** image**,** compression**=** “gzip” , chunks**=** True , maxshape**=** ( None , 96 , 96 , 3 ) 14 ) ~\AppData\Local\Programs\Orange\lib\site-packages\h5py_hl\group.py in create_dataset**(self, name, shape, dtype, data, kwds) 147 group = self**.** require_group**(** parent_path**)** 148 → 149 dsid = dataset**.** make_new_dset**(** group**,** shape**,** dtype**,** data**,** name**,** ****** kwds**)** 150 dset = dataset**.** Dataset**(** dsid**)** 151 return dset ~\AppData\Local\Programs\Orange\lib\site-packages\h5py_hl\dataset.py in make_new_dset**(parent, shape, dtype, data, name, chunks, compression, shuffle, fletcher32, maxshape, compression_opts, fillvalue, scaleoffset, track_times, external, track_order, dcpl, allow_unknown_filter)** 107 compression_opts = compression 108 compression = ‘gzip’ → 109 dcpl = filters.fill_dcpl( 110 dcpl or h5p**.** create**(** h5p**.** DATASET_CREATE**)** , shape**,** dtype**,** 111 chunks**,** compression**,** compression_opts**,** shuffle**,** fletcher32**,** ~\AppData\Local\Programs\Orange\lib\site-packages\h5py_hl\filters.py in fill_dcpl**(plist, shape, dtype, chunks, compression, compression_opts, shuffle, fletcher32, maxshape, scaleoffset, external, allow_unknown_filter)** 175 176 rq_tuple**(** chunks**,** ‘chunks’ ) → 177 rq_tuple**(** maxshape**,** ‘maxshape’ ) 178 179 if compression is not None : ~\AppData\Local\Programs\Orange\lib\site-packages\h5py_hl\filters.py in rq_tuple**(tpl, name)** 172 raise TypeError**(** ‘"%s" argument must be None or a sequence object’ % name**)** 173 if len**(** tpl**)** != len**(** shape**)** : → 174 raise ValueError**(** ‘"%s" must have same rank as dataset shape’ % name**)** 175 176 rq_tuple**(** chunks**,** ‘chunks’ ) ValueError : “maxshape” must have same rank as dataset shape
“”"