caffe中将某个目录下的jpg图像转换为hdf5类型

网上相关资料很少,今天做了将文件夹下的所有.jpg格式的图片转换成hdf5格式,中途踩了很多坑,最后在老师的指导下写出来了,特此记录。
二话不说,直接上代码,很好理解。我的图片命名是x1_y1_x2_y2.jpg的形式,用CNN做回归,找到2个坐标的x,y值。建议用python做文本处理,非常快,很容易上手。
主要看data和label具体怎么写,怎么定义,怎么转换,这个是关键。所有的图片都读取完,一次性create_dataset,不然会出错。

import numpy as np
import h5py
import cv2
import os
import random
import code
import string

def store_hdf5(filename, mapping):
    """Function to store data mapping to a hdf5 file

    Args:
        filename (str): The output filename
        mapping (dic): A dictionary containing mapping from name to numpy data The complete mapping will be stored as single datasets in the h5py file.
    print("Storing hdf5 file %s" % filename)
    with h5py.File(filename, 'w') as hf:
        for label, data in mapping.items():
            print("  adding dataset %s with shape %s" % (label, data.shape))
            hf.create_dataset(label, data=data)

    print("  finished")

if __name__=&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值