We can use different libraries to easily handle our dataset. In this tutorial, you will learn about dataset preparation in .h5 format using python h5py library. You can find the code below.
from keras.preprocessing.image import ImageDataGenerator,img_to_array,load_img
from keras.preprocessing import image
import os
import matplotlib.pyplot as plt
import scipy.misc
import numpy as np
import glob,os
import h5py
import cv2
Path = os.getcwd()
train_cat_path = "E:\\Projects\\Dataset\\Cat\\"
train_dog_path = "E:\\Projects\\Dataset\\Dog\\"
train_cat_imgs = np.zeros((504,128,128),np.uint8)
train_dog_imgs = np.zeros((504,128,128),np.uint8)
os.chdir(train_cat_path)
i = 0;
for file in glob.glob("*.png"):
filename = os.path.join(train_cat_path,file)
ds = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(ds, (128, 128))
train_cat_imgs[i,:,:] = img
i = i +1
os.chdir(train_dog_path)
i = 0;
for file in glob.glob("*.png"):
filename = os.path.join(train_dog_path,file)
ds = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(ds, (128, 128))
train_dog_imgs[i,:,:] = img
i = i +1
with h5py.File('train_cat_dog.h5', 'w') as hf:
hf.create_dataset("train_cat_imgs", data=train_cat_imgs)
hf.create_dataset("train_dog_imgs", data=train_dog_imgs)
Comments
Post a Comment