前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >生成h5文件_h5实现文件下载

生成h5文件_h5实现文件下载

作者头像
全栈程序员站长
发布2022-09-30 10:15:18
8370
发布2022-09-30 10:15:18
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

生成训练h5文件

代码语言:javascript
复制
import h5py
import os
import cv2
import math
import numpy as np
import random
import 

root_path = "/home/tyd/caffe_case/HDF5/image"

with open("/home/tyd/caffe_case/HDF5/hdf5.txt","r") as f:
    lines = f.readlines()

num = len(lines)
random.shuffle(lines)

imgAccu = 0
imgs = np.zeros([num,3,224,224])
labels = np.zeros([num,10])
for i in range(num):
    line=lines[i]
    segments=re.split('\s+',line)[:-1]
    print segments[0]
    img = cv2.imread(os.path.join(root_path,segments[0]))
    img = cv2.resize(img,(224,224))
    img = img.transpose(2,0,1)
    imgs[i,:,:,:]=img.astype(np.float32)
    for j in range(10):
        lables[i,j]=float(segments[j+1])*224/256
batchSize = 1
batchNum = int(math.ceil(1.0*num/batchSize))

imgsMean = np.mean(imgs,axis=0)
imgs = (imgs -imgsMean)/255.0
labelsMean = np.mean(labels,axis=0)
labels = (labels-labelsMean)/10

if os.path.exists('trainlist.txt'):
    os.remove('trainlist.txt')
    
if os.path.exists('testlist.txt'):
    os.remove('testlist.txt')
comp_kwargs={'compression':'gzip','compression_opts':1}
for i in range(batchNum):
    start = i*batchSize
    end=min((i+1)*batchSize,num)
    if i<batchNum-1:
        filename='/home/tyd/caffe_case/HDF5/h5/train{0}.h5'.format(i)
    else:
        filename='/home/tyd/caffe_case/HDF5/h5/test{0}.h5'.format(i-batchNum+1)
    print filename
    with h5py.File(filename,'w') as f:
        f.create_dataset('data',data=np.array((imgs[start:end]-imgsMean)/255.0).astype(np.float32),**comp_kwargs)
        f.create_dataset('label',data=np.array(labels[start:end]).astype(np.float32),**comp_kwargs)
        
    if i < batchNum-1:
        with open('/home/tyd/caffe_case/HDF5/h5/trainlist.txt','a') as f:
            f.write(os.path.join(os.getcwd(),'train{0}.h5'.format(i)+'\n')
    else:
        with open('/home/tyd/caffe_case/HDF5/h5/testlist.txt','a') as f:
            f.write(os.path.join(os.getcwd(),'train{0}.h5'.format(i-batchNum+1)+'\n')
imgsMean = np.mean(imgsMean,axis=(1,2))
with open('mean.txt','w') as f:
    f.write(str(imgsMean[0])+'\n'+str(imgsMean[2]))

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月9日 下,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档