.png 到 .h5 文件的转换代码 运行ning 非常慢。请建议我如何在 GPU 中 运行

.png to .h5 file conversion code is running very slow. Please suggest how can I run it in GPU

我正在使用以下代码将 .png 图像转换为单个 .h5 文件。这段代码工作正常,但转换速度很慢。我有 40GB 大小为 224 x 224 像素的数据,需要将其转换为 .h5。请告诉我为什么我的代码 运行ning 非常慢。如果我想 运行 GPU 中的代码,我的代码需要进行哪些更改。请提出建议。

import cv2
import datetime as dt
import h5py
import matplotlib.pyplot as plt
import matplotlib.pylab as plb
import numpy as np
import os
import pandas as pd
from glob import glob


start = dt.datetime.now()

PATH = os.path.abspath(os.path.join('/home/sd/Downloads/', 'he'))

SOURCE_IMAGES = os.path.join(PATH, "patch", "training_data")

images = glob(os.path.join(SOURCE_IMAGES, "*.png"))
images.sort()


NUM_IMAGES = len(images)
HEIGHT = 224
WIDTH = 224
CHANNELS = 3
SHAPE = (HEIGHT, WIDTH, CHANNELS)
    #Now we will write the h5 file
train_shape = (len(images), HEIGHT, WIDTH, CHANNELS)

hf=h5py.File('data.h5', 'w')
hf.create_dataset("train_img", shape=train_shape, maxshape=train_shape, compression='gzip', compression_opts=9)

for i, img in enumerate(images):
    s=dt.datetime.now()
    img=cv2.imread(images[i])
    img= cv2.resize(img, (WIDTH,HEIGHT), interpolation=cv2.INTER_CUBIC)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    hf["train_img"][i, ...] = img[None]
    e=dt.datetime.now()
hf.close()

把这一行hf.create_dataset("train_img", shape=train_shape, maxshape=train_shape, compression='gzip', compression_opts=9)改成hf.create_dataset("train_img", shape=train_shape, maxshape=train_shape, np.int8)