将图像和对应的标签转化成会h5py文件

只是简单的记录一下自己写的小程序,程序比较简单,有问题可以问一下,但是就不做过多的解释了。

 

我的数据集的格式是,每个文件夹名称也即是图像对应的标签

相关代码:

# -*- coding: utf-8 -*-
# @Time    : 2019/6/23 11:40
# @Author  : YYLin
# @Email   : 854280599@qq.com
# @File    : save_and_load_h5py.py
import os
import numpy as np
import cv2
import h5py


# 加载数据集中的文件
def save_image_to_h5py(path):
    img_list = []
    label_list = []
    dir_counter = 0
    num_for_test = 0

    for child_dir in os.listdir(path):
        child_path = os.path.join(path, child_dir)
        # print('文件中的子文件名是:\n', child_path)
        # 总共有9个文件夹 第一个文件夹加载10文件 其他文件夹中加载1个文件
        for dir_image in os.listdir(child_path):
            # print('dir_image中图像的名称是:\n', dir_image)
            img = cv2.imread(os.path.join(child_path, dir_image))
            # img =cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#单通道,分辨率会下降
            img_list.append(img)
            label_list.append(dir_counter)

            if num_for_test > 10:
                break
            num_for_test = num_for_test + 1

    # 返回的img_list转成了 np.array的格式
        dir_counter += 1

    img_np = np.array(img_list)
    label_np = np.array(label_list)
    print('数据集中原始的标签顺序是:\n', label_np)

    f = h5py.File('hdf5_file.h5', 'w')
    f['image'] = img_np
    f['labels'] = label_np
    f.close()


save_image_to_h5py('../Dataset/baidu/train_image/train')


# 加载hdpy成np的形式
def load_h5py_to_np(path):
    h5_file = h5py.File(path, 'r')
    print('打印一下h5py中有哪些关键字', h5_file.keys())
    permutation = np.random.permutation(len(h5_file['labels']))
    shuffled_image = h5_file['image'][:][permutation, :, :, :]
    shuffled_label = h5_file['labels'][:][permutation]
    print('经过打乱之后数据集中的标签顺序是:\n', shuffled_label, len(h5_file['labels']))
    return shuffled_image, shuffled_label


images, labels = load_h5py_to_np('hdf5_file.h5')

for i, image in enumerate(images):
    cv2.imwrite("filename.png", image)

 

  • 12
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
### 回答1: TensorFlow和h5py对应版本如下: TensorFlow 1.. - h5py 2.6. TensorFlow 1.1. - h5py 2.7. TensorFlow 1.2. - h5py 2.7.1 TensorFlow 1.3. - h5py 2.7.1 TensorFlow 1.4. - h5py 2.7.1 TensorFlow 1.5. - h5py 2.7.1 TensorFlow 1.6. - h5py 2.7.1 TensorFlow 1.7. - h5py 2.7.1 TensorFlow 1.8. - h5py 2.7.1 TensorFlow 1.9. - h5py 2.8. TensorFlow 1.10. - h5py 2.8. TensorFlow 1.11. - h5py 2.8. TensorFlow 1.12. - h5py 2.8. TensorFlow 1.13. - h5py 2.9. TensorFlow 1.14. - h5py 2.9. TensorFlow 1.15. - h5py 2.10. TensorFlow 2.. - h5py 2.10. TensorFlow 2.1. - h5py 2.10. TensorFlow 2.2. - h5py 2.10. TensorFlow 2.3. - h5py 2.10. TensorFlow 2.4. - h5py 2.10. TensorFlow 2.5. - h5py 3.1. 注意:这只是一个大致的对应关系,具体版本可能有所不同。建议在使用时查看官方文档或者社区推荐的版本。 ### 回答2: TensorFlow是由Google开发的开源机器学习框架,目前已经为最流行的深度学习框架之一。h5py是一个开源的Python库,它提供了一种方便的方式来访问HDF5文件,这种文件格式非常适合存储大规模的科学数据。 TensorFlow和h5py都是用Python编写的,并且广泛应用于机器学习、深度学习和数据科学领域。在使用这两个库的时候,我们需要注意它们的版本兼容性。 TensorFlow和h5py的版本兼容性: 在安装TensorFlow和h5py之前,我们需要了解它们的版本兼容性。根据具体的版本号,TensorFlow和h5py之间可能存在兼容性问题。下面是TensorFlow和h5py的一些版本兼容性说明: 1. TensorFlow和h5py的最新版本是兼容的。换句话说,如果你下载了最新版本的TensorFlow,那么你可以安装最新版本的h5py,这样可以保证两者的兼容性。 2. TensorFlow 1.x和h5py 2.x是兼容的。这意味着,如果你使用了TensorFlow 1.x,那么你可以安装h5py 2.x,并且可以正常运行代码。 3. TensorFlow 2.x和h5py 3.x是兼容的。这意味着,如果你使用了TensorFlow 2.x,那么你可以安装h5py 3.x,并且可以正常运行代码。 需要注意的是,如果你在使用TensorFlow和h5py时遇到了版本兼容性问题,那么可能需要升级或降级其中的一个库,才能正常使用。为了避免这种情况,我们建议在安装新版库之前,先了解其版本兼容性,以免产生不必要的问题。 ### 回答3: TensorFlow和h5py是两种非常常用的机器学习工具,两者都有对应的版本。下面就分别对这两种工具进行说明。 1. TensorFlow版本 TensorFlow的版本主要包括以下几个方面: 1)官方发布的版本号,例如1.0.0、1.1.0、2.0.0等。这些版本一般都是稳定的,可以直接使用。 2)Nightly Builds版本,如1.13.0.dev20190121、1.14.0.dev20190311等。这些版本是每天构建的,其中包含了最新代码的实时版本。 3)最新的GitHub版本,可以从官方GitHub上获取最新代码,但因为没有经过严格测试,可能不稳定。 在TensorFlow中,不同版本之间存在一些差别,在使用时需要注意一下。 2. h5py版本 h5py的版本分为两个部分,分别表示h5py的版本号和HDF5的版本号,形式为: h5py_major.h5py_minor.hdf5_major.hdf5_minor 例如h5py 2.10.0需要HDF5 1.10.x作为后端。 由于h5py是基于HDF5库进行开发的,因此h5py版本的更新通常是由HDF5库的版本更新导致的。因此,在使用h5py时,需要事先确定所需的HDF5版本号,以便安装相应版本的h5py。 总之,在使用机器学习工具时,了解版本信息非常重要。无论是TensorFlow还是h5py都需要选择相应版本来进行使用和安装。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值