简体   繁体   English

如何使用 h5py 构建数据集?

[英]How to build a dataset with h5py?

I have the following dataset :我有以下数据集:

class category : 1,2,3 each class contain rgb images 320x240班级类别:1,2,3 每个班级包含 320x240 的 rgb 图像

dataset
   |---- training_set
         |---- 1
               |--- rgb_1.png
               |--- rgb_2.png
         |---- 2
               |--- rgb_1.png
               |--- rgb_2.png
         |---- 3
               |--- rgb_1.png
               |--- rgb_2.png
   |---- test_set
         |---- 1
               |--- rgb_1.png
               |--- rgb_2.png 
         |---- 2
               |--- rgb_1.png
               |--- rgb_2.png
         |---- 3
               |--- rgb_1.png
               |--- rgb_2.png

Because when I training the model In google colab it is very slow than my computer, I think because the dataset is in google drive, so I try another solution to create the dataset using h5py.因为当我在 google colab 中训练模型时,它比我的电脑慢,我认为是因为数据集在 google drive 中,所以我尝试了另一种解决方案,使用 h5py 创建数据集。

Please, How to build this dataset with h5py ?请问,如何用 h5py 构建这个数据集?

The short answer is use create_dataset() .简短的回答是使用create_dataset() Everything you need is in the link provided by @jakub.您需要的一切都在@jakub 提供的链接中。 To populate the dataset, you need to create a NumPy array from your image.要填充数据集,您需要从您的图像创建一个 NumPy 数组。 I don't know the best way to do that.我不知道这样做的最佳方法。 You will need to figure that part out.你需要弄清楚那部分。 For my example, I used cv2.imread() .对于我的示例,我使用了cv2.imread()

You can get started with this simple piece of code:您可以从这段简单的代码开始:

import h5py, cv2
import numpy as np
# Create an array for image data
img_arr = cv2.imread(imgFile)
# returns a np array 
with h5py.File('imagedata.h5','w') as h5f :
    dset = h5f.create_dataset("image1", data=img_arr)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM