首页 > 其他分享 >cafffe 利用h5py生成多标签h5文件并训练

cafffe 利用h5py生成多标签h5文件并训练

时间:2022-11-10 15:05:12浏览次数:48  
标签:__ network h5 test range cafffe h5py label data


h5py生成多标签h5文件

import h5py
import numpy as np
def main():
f = h5py.File('train00.h5', 'w')
f.create_dataset('data', (1200, 128), dtype='f8')
f.create_dataset('label', (1200, 4), dtype='i')

for i in range(1200):
a = np.empty(128)
if i % 4 == 0:
for j in range(128):
a[j] = j / 128.0;
l = [1,0,0,0]
elif i % 4 == 1:
for j in range(128):
a[j] = (128 - j) / 128.0;
l = [1,0,1,0]
elif i % 4 == 2:
for j in range(128):
a[j] = (j % 6) / 128.0;
l = [0,1,1,0]
elif i % 4 == 3:
for j in range(128):
a[j] = (j % 4) * 4 / 128.0;
l = [1,0,1,1]
f['data'][i] = a
f['label'][i] = l
f.close()
with open('train.h5list','w') as f:
f.write('train00.h5')

with open('test.h5list','w') as f:
f.write('train00.h5')

if __name__=="__main__":
main()

训练数据

import caffe
from caffe import layers as L
from matplotlib import pyplot as plt
import os
import numpy as np

def change_env():
root = os.path.dirname(__file__)
os.chdir(root)
print ("current work root:->",root)

def net(hdf5, batch_size):
network = caffe.NetSpec()
network.data,network.label = L.HDF5Data(batch_size=batch_size,source=hdf5,ntop=2)
network.ip1 = L.InnerProduct(network.data,num_output=50,weight_filler=dict(type="xavier"))
network.relu1 = L.ReLU(network.ip1,in_place=True)
network.ip2 = L.InnerProduct(network.relu1,num_output=4,weight_filler=dict(type="xavier"))
network.loss = L.SigmoidCrossEntropyLoss(network.ip2,network.label)

return network.to_proto()

def file_write(path=""):
with open('auto_train01.prototxt', 'w') as f:
f.write(str(net('train.h5list', 100)))
with open('auto_test01.prototxt', 'w') as f:
f.write(str(net('test.h5list', 50)))

def main():
#change_env()
file_write()
solver = caffe.SGDSolver('auto_solver01.prototxt')
niter = 1001
test_interval = 10
train_loss = np.zeros(niter)
test_acc = np.zeros(int(np.ceil(niter * 1.0 / test_interval)))
train_acc = np.zeros(niter)

# iteration and save the loss, accuracy
for it in range(niter):
solver.step(1)
train_loss[it] = solver.net.blobs['loss'].data
solver.test_nets[0].forward(start='data')

if it % test_interval == 0:
print ('Iteration', it, 'testing...')
correct = 0
data = solver.test_nets[0].blobs['ip2'].data
label = solver.test_nets[0].blobs['label'].data
for test_it in range(100):
solver.test_nets[0].forward()
# Positive -> label 1, negative -> label 0
for i in range(len(data)):
for j in range(len(data[i])):
if data[i][j] > 0 and label[i][j] == 1:
correct += 1
elif data[i][j] <= 0 and label[i][j] == 0:
correct += 1
test_acc[int(it / test_interval)] = correct * 1.0 / (len(data) * len(data[0]) * 100)

#output graph
_, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(np.arange(niter), train_loss)
ax2.plot(test_interval * np.arange(len(test_acc)), test_acc, 'r')
ax1.set_xlabel('iteration')
ax1.set_ylabel('train loss')
ax2.set_ylabel('test accuracy')
_.savefig('converge01.png')

if __name__ == '__main__':
main()

cafffe 利用h5py生成多标签h5文件并训练_f5


标签:__,network,h5,test,range,cafffe,h5py,label,data
From: https://blog.51cto.com/u_15872074/5841633

相关文章

  • H5游戏开发-面向对象编程
    七、面向对象编程1.认识类与对象类是一种复杂的数据结构,他是将不同类型的数据与这些数据相关的操作封装在一起的集合体。对象是类的实例,是类描述的具体事物。(类是对象的......
  • H5游戏开发-TypeScript语言基础
    五、TypeScript语言基础1.认识TypeScriptTypeScript是一种由微软开发的开源的编程语言,它是js的一个超集,向js添加了静态类型和基于类的面向对象。2012年10月,微软发布了首......
  • H5游戏开发-搭建开发环境
    四、搭建开发环境1.下载最新版EgretLauncher,官网https://www.egret.com/EgretLauncher下载地址:https://docs.egret.com/engine  2.打开EgretLauncher的工具......
  • H5游戏开发-Egret引擎
    四、Egret引擎Egret致力于为移动领域提供完整高效的游戏及应用解决方案。该方案包含了开源游戏引擎,云平台,开发工具集,打包工具,加速器等等。旨在解决移动生态环境中各个环节......
  • H5游戏开发-H5游戏引擎与渠道分布
    二、H5游戏引擎1.主流的H5游戏引擎目前H5游戏市场上的主流引擎有白鹭引擎/Laya引擎/Cocos2d-html5引擎。其中在全渠道付费榜前30名的h5游戏中,超过50%用白鹭引擎。2.白......
  • H5游戏开发-游戏的介绍
    一、游戏的介绍1.游戏的种类1.端游(客户端游戏)电脑(网络)游戏。2.页游(网页游戏)web游戏,无端游戏,基于web浏览器的网络在线多人互动游戏。3.手游(手机游戏)运行在手机上的软......
  • 脚本-交叉编译bash5.0
    #!/bin/bash#通用交叉编译脚本##build编译环境#host编译出的程序的运行环境#target编译出的程序(编译器)编译出来的程序的运行环境#一般不需要PATH_NAME="b......
  • Go进阶53:从零Go实现Websocket-H5-RDP/VNC远程桌面客户端
    Go进阶53:从零Go实现Websocket-H5-RDP/VNC远程桌面客户端Go&Rust......
  • CH573F蓝牙从机(peripheral)例程讲解(服务修改篇)
    修改服务需要对蓝牙比较熟悉的工程师去完成,否则在例程功能满足自己需求时,不建议修改,很容易改错,下面就简单介绍下如何在别的char添加一个nofify属性。step1:给SimpleProfi......
  • 网站在线客服系统源码 | 全渠道在线客服代码下载 (最新版支持外贸商城/H5/小程序/公众
    gofly.v1kf.com是一款基于Golang+Vue开发的在线客服系统,软件著作权编号:2021SR1462600。一套可私有化部署的在线客服系统,编译后的二进制文件可直接使用无需搭开发环境,下载z......