Hello, everyone! I have downloaded the data set of cats and dogs from Kaggle in the from of a folder having “jpeg” images. Can anybody tell me how can I convert those images into an “.h5” file to be able to apply what I have learned from this course.
The ‘h5’ file is a file which stores machine learning data:
And I believe in you context it is so, not the images.
Hi @Khushi_Singh1,
A few years ago, I used this code to make h5 file.
import numpy as np
import h5py
import glob
from matplotlib.image import imread
image = {}
I=0
img_mask = '/Users/mubsi/Desktop/Total2/*.jpg'
img_names = glob.glob(img_mask)
for fn in img_names:
print(i, fn)
image[i] = imread(fn)
i = i + 1
with h5py.File('/Users/mubsi/Desktop/Test.h5', 'w' ) as hdf:
hdf.create_dataset('trainSet_x', shape=(47,64,64))
dset = hdf['trainSet_x']
for x in range(0, 47):
dset[x] = image[x]
Hope this helps,
Mubsi
2 Likes
In addition to Mubsi’s great example, here’s another thread which gives some code that’s pretty similar, but has a few other examples that may be useful. E.g. it shows how to create multiple datasets within a single h5 file. That’s implicit in what Mubsi shows, but this code actually writes it out explicitly.