放弃使用pytorch,学习caffe

本文仅记录个人观点,不免存在许多错误


Caffe 学习

caffe模型生成需要如下步骤

  • 编写network.prototxt
  • 编写solver.prototxt
  • caffe train -solver=solver.prototxt

network.prototxt编写

在caffe中,Net由Layer构成,其中数据由Blob进行传递

network编写就是组织layer

关于layer如何编写,参考caffe.proto

这里写出layer一般形式

layer{
name: "layer name"
type: "layer type"
bottom: "bottom blob"
top: "top blob"
param{
...
}
include{ phase: ... }
exclude{ phase: ... }
# 对某一type的layer参数, 这里以内积层为例
inner_product_param{
num_output: 64
weight_filler{
type: "xavier"
}
bias_filler{
type: "constant"
value: 0
}
axis=1
}
}

在这里简要说一下我们的目的,

通过中文分词rnn和贝叶斯分类器实现一个垃圾信息处理的功能

这里直接附上我的network好了,反正没人看: (

# project for chinese segmentation
# T: 64, batch: 64
# label[T*batch, 1, 1, 1] cont[T*batch, 1, 1, 1]=0 or 1
# data[T*batch, 1, 1, 1] ->
# embed[T*batch, 2000, 1, 1](drop&reshape) -> [T, batch, 2000, 1]
# lstm[T, batch, 256, 1](drop) ->
# ip[T, batch, 64, 1](relu) ->
# ip[T, batch, 5, 1] ->
# Accuracy & SoftMaxWithLoss
# for output: 0-none, 1-Signal, 2-Begin, 3-Middle, 4-End name: "Segment" # train data
layer{
name: "train_data"
type: "HDF5Data"
top: "data"
top: "label"
top: "cont"
include{ phase: TRAIN }
hdf5_data_param{
source: "/home/tanglizi/caffe/projects/data_segment/h5_test.txt"
batch_size: 4096
shuffle: true
}
}
# test data
layer{
name: "test_data"
type: "HDF5Data"
top: "data"
top: "label"
top: "cont"
include{ phase: TEST }
hdf5_data_param{
source: "/home/tanglizi/caffe/projects/data_segment/h5_test.txt"
batch_size: 4096
shuffle: true
}
} # embed
layer{
name: "embedding"
type: "Embed"
bottom: "data"
top: "embedding"
param{
lr_mult: 1
}
embed_param{
input_dim: 14000
num_output: 2000
weight_filler {
type: "uniform"
min: -0.08
max: 0.08
}
}
}
# embed-drop
layer{
name: "embed-drop"
type: "Dropout"
bottom: "embedding"
top: "embed-drop"
dropout_param{
dropout_ratio: 0.05
}
} # reshape
# embed
# [T*batch, 2000, 1, 1] ->
# [T, batch, 2000, 1]
layer{
name: "embed-reshape"
type: "Reshape"
bottom: "embed-drop"
top: "embed-reshaped"
reshape_param{
shape{
dim: 64
dim: 64
dim: 2000
}
}
} # label
layer{
name: "label-reshape"
type: "Reshape"
bottom: "label"
top: "label-reshaped"
reshape_param{
shape{
dim: 64
dim: 64
dim: 1
}
}
} # cont
layer{
name: "cont-reshape"
type: "Reshape"
bottom: "cont"
top: "cont-reshaped"
reshape_param{
shape{
dim: 64
dim: 64
}
}
} # lstm
layer{
name: "lstm"
type: "LSTM"
bottom: "embed-reshaped"
bottom: "cont-reshaped"
top: "lstm"
recurrent_param{
num_output: 256
weight_filler{
# type: "xavier"
type: "uniform"
min: -0.08
max: 0.08
}
bias_filler{
type: "constant"
value: 0
}
}
} # lstm-drop
layer{
name: "lstm1-drop"
type: "Dropout"
bottom: "lstm"
top: "lstm-drop"
dropout_param{
dropout_ratio: 0.05
}
} # connect
# ip1
layer{
name: "ip1"
type: "InnerProduct"
bottom: "lstm-drop"
top: "ip1"
param{
lr_mult: 1
decay_mult: 1
}
param{
lr_mult: 2
decay_mult: 0
}
inner_product_param{
num_output: 64
weight_filler{
type: "xavier"
}
bias_filler{
type: "constant"
value: 0
}
axis: 2
}
}
# relu
layer{
name: "relu1"
type: "ReLU"
bottom: "ip1"
top: "relu1"
relu_param{
negative_slope: 0
}
} # ip2
layer{
name: "ip2"
type: "InnerProduct"
bottom: "relu1"
top: "ip2"
param{
lr_mult: 1
}
param{
lr_mult: 2
}
inner_product_param{
num_output: 5
weight_filler{
type: "xavier"
}
bias_filler{
type: "constant"
value: 0
}
axis: 2
}
} # loss
layer{
name: "loss"
type: "SoftmaxWithLoss"
bottom: "ip2"
bottom: "label-reshaped"
top: "loss"
softmax_param{
axis: 2
}
} # accuracy
layer{
name: "accuracy"
type: "Accuracy"
bottom: "ip2"
bottom: "label-reshaped"
top: "accuracy"
accuracy_param{
axis: 2
}
}

solver.prototxt编写

solver用于调整caffe训练等操作的超参数

solver如何编写,参考caffe.proto

附上一般写法

net: "network.proto"

test_iter: 100
test_interval: 500 type: "Adam"
base_lr: 0.01
weight_decay: 0.0005 lr_policy: "inv" display: 100
max_iter: 10000 snapshot: 5000
snapshot_prefix: "/home/tanglizi/caffe/projects/segment/" solver_mode: CPU

训练模型

caffe train -solver=solver.prototxt

这时可能报错:

Message type "caffe.MultiPhaseSolverParameter" has no field named "net".

请注意不是没有net,而是其他参数设置有误

intel caffe特有的报错


Caffemodel 的使用

模型训练的结果很有问题,accuracy非常低,感觉又是network写错了

于是想看看其中发生了什么

caffemodel可以通过c++或python matlab接口来使用

接下来进入intel caffe 和intel devcloud大坑

pycaffe的使用

注意:以下python代码在devcloud进行

首先我们知道caffe模型就是训练好的一个神经网络

于是必然需要caffe.Net()来读取caffemodel和net.prototxt,需要caffe.io读取数据

import caffe
from caffe import io
# 这时报错:
#Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
#ImportError: cannot import name 'io'

连忙查看caffe里面有什么

dir(caffe)
# 显示 ['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
# 正常显示 ['AdaDeltaSolver', 'AdaGradSolver', 'AdamSolver', 'Classifier', 'Detector', 'Layer', 'NesterovSolver',
# 'Net', 'NetSpec', 'RMSPropSolver', 'SGDSolver', 'TEST', 'TRAIN', '__builtins__', '__doc__', '__file__', '__name__',
# '__package__', '__path__', '__version__', '_caffe', 'classifier', 'detector', 'get_solver', 'init_log', 'io', 'layer_type_list',
# 'layers', 'log', 'net_spec', 'params', 'proto', 'pycaffe', 'set_device', 'set_mode_cpu', 'set_mode_gpu', 'set_random_seed', 'to_proto']

淦,根本什么都没有

由于我们的项目需要必须在服务器上进行,所以不考虑在本地机器上运行

现在有两条路:重新编译一个caffe 或用c++实现

懒得搞事情,选择c++实现

c++中使用caffemodel

注:以下过程使用intel caffe

首先我们知道caffe模型就是训练好的一个神经网络

于是必然需要caffe.Net()来读取caffemodel和net.prototxt

// predict.cpp
#include <caffe/caffe.hpp>
boost::shared_ptr< Net<float> > net(new caffe::Net<float>(net, Caffe::TEST));
  1. 开始手动编译
    # 注意到caffe.hpp的位置,我们添加路径即可
clang++ -I <caffe path>/include -lboost_system predict.cpp -o predict
#不料报错
#/tmp/predict-fea879.o: In function 'main':
#predict.cpp:(.text+0x35b): undefined reference to 'caffe::Net<int>::Net(std::__cxx11::basic_string<char, std::char_traits<char>,
#std::allocator<char> > const&, caffe::Phase, int, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>,
#std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const*,
# caffe::Net<int> const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
#clang: error: linker command failed with exit code 1 (use -v to see invocation) # 看起来找不到libcaffe,添加路径即可
clang++ -I <caffe path>/include -lboost_system predict.cpp -o predict -L <caffe path>/build/lib -lcaffe
# 不料报错 错误相同
  1. 放弃手动编译,放在examples/下重新编译caffe

    不料报错 错误相同
  2. 放在tools/下(caffe.cpp的位置)重新编译caffe

    直接跳过跳过编译predict.cpp

    烦 放弃本地使用c++
  3. 在devcloud上手动编译

    不料报错 错误相同

    云上都编译不了我还干chua
  4. 重新编译intel caffe

    按照环境重新配置Makefile.config

    编译报错
In file included from .build_release/src/caffe/proto/caffe.pb.cc:5:0:
.build_release/src/caffe/proto/caffe.pb.h:12:2: error: #error This file was generated by a newer version of protoc which is
#error This file was generated by a newer version of protoc which is
.build_release/src/caffe/proto/caffe.pb.h:13:2: error: #error incompatible with your Protocol Buffer headers. Please update
#error incompatible with your Protocol Buffer headers. Please update
.build_release/src/caffe/proto/caffe.pb.h:14:2: error: #error your headers.
#error your headers.
.build_release/src/caffe/proto/caffe.pb.h:22:35: fatal error: google/protobuf/arena.h: No such file or directory
#include <google/protobuf/arena.h>

查了一下,此处需要libprotoc 2.6.1,然而devcloud上libprotoc 3.2.0

烦死了

于是查到这个文章,在此十分感谢 @大黄老鼠 同学!!!

好了现在完全放弃caffe了!

转战chainer!

记intel杯比赛中各种bug与debug【其二】:intel caffe的使用和大坑的更多相关文章

  1. 记intel杯比赛中各种bug与debug【其一】:安装intel caffe

    因为intel杯创新软件比赛过程中,并没有任何记录.现在用一点时间把全过程重演一次用作记录. 学习 pytorch 一段时间后,intel比赛突然不让用 pytoch 了,于是打算转战intel ca ...

  2. 记intel杯比赛中各种bug与debug【其三】:intel chainer的安装与使用

    现在在训练模型,闲着来写一篇 顺着这篇文章,顺利安装上intel chainer 再次感谢 大黄老鼠 intel chainer 使用 头一次使用chainer,本以为又入了一个大坑,实际尝试感觉非常 ...

  3. 记intel杯比赛中各种bug与debug【其四】:基于长短时记忆神经网络的中文分词的实现

    (标题长一点就能让外行人感觉到高大上) 直接切入主题好了,这个比赛还必须一个神经网络才可以 所以我们结合主题,打算写一个神经网络的中文分词 这里主要写一下数据的收集和处理,网络的设计,代码的编写和模型 ...

  4. 记intel杯比赛中各种bug与debug【其五】:朴素贝叶斯分类器的实现和针对性的优化

    咱这个项目最主要的就是这个了 贝叶斯分类器用于做可以统计概率的二元分类 典型的例子就是垃圾邮件过滤 理论基础 对于贝叶斯算法,这里附上两个链接,便于理解: 朴素贝叶斯分类器的应用-阮一峰的网络日志 基 ...

  5. SQL Server 字段类型 decimal(18,6)小数点前是几位?记一次数据库SP的BUG处理

    原文:SQL Server 字段类型 decimal(18,6)小数点前是几位?记一次数据库SP的BUG处理 SQL Server 字段类型 decimal(18,6)小数点前是几位? 不可否认,这是 ...

  6. gcc中的内嵌汇编语言(Intel i386平台)

    [转]http://bbs.chinaunix.net/thread-2149855-1-1.html 一.声明  虽然Linux的核心代码大部分是用C语言编写的,但是不可避免的其中还是有一部分是用汇 ...

  7. 那些盒模型在IE6中的BUG们,工程狮的你可曾遇到过?

    HTML5学堂 那些盒模型在IE6中的BUG们,工程狮的你可曾遇到过? IE6已经渐渐的开始退出浏览器的历史舞台.虽然当年IE6作为微软的一款利器击败网景,但之后也因为版本的持续不更新而被火狐和谷歌三 ...

  8. 转:移动开发中一些bug及解决方案

    网页开发要面对各种各样的浏览器,让人很头疼,而移动开发中,你不但要面对浏览器,还要面对各种版本的手机,iOS好一点,而安卓就五花八门了,你可能在开发中也被它们折磨过,或者正在被它们折磨,我在这里说几个 ...

  9. 写代码的心得,怎么减少编程中的 bug?

    遭遇 bug 的时候,理性的程序员会说:这个 bug 能复现吗? 自负型:这不可能,在我这是好好的. 经验型:不应该,以前怎么没问题? 幻想型:可能是数据有问题. 无辜型:我好几个星期都没碰这块代码了 ...

随机推荐

  1. jq 方法函数(淡入淡出,查找元素,过滤)遍历

    淡入淡出:fadeIn fadeOut fadeToggle fadeTo 淡入:fadeIn(speed[,callback])   速度和回调函数 回调函数可以写匿名函数,或者方法名不加括号. s ...

  2. Pyinstaller 0

    Pyinstaller 是一个小的可以打包我们所写的Python脚本,来生成相应的可执行文件. 它是怎么工作的? PyInstaller读取您编写的Python脚本.它会分析您的代码,以发现您的脚本执 ...

  3. 【原创】Spring连接、事务代码分析

    1.JdbcTemplate     当不使用事务时,jdbcTemplate的模板类,通过     Connection con = DataSourceUtils.getConnection(ge ...

  4. Html表单提交到Servlet输出到页面乱码

    Html使用的编码是UTF-8编码显示页面,之后使用form表单提交字段到Servlet中,Servlet将利用getParamer方法获得form提交的字段,之后通过Respone中的writer将 ...

  5. Sublime Text编辑器配置Python解释器简易教程

    前天在微信上遇到一个小伙伴问我一个关于Sublime text配置Python解释器的问题,可能是初学者,对这方面还不是很懂,想使用快捷键但是徒劳一场,因为缺少Python解释器,直接按下快捷键Ctr ...

  6. (WC2018模拟十二)【FJOI2016集训Day7T2】点对游戏

    题解: 还好...看懂题目就好做了.(Orzdyh) 首先选择的点是等概率随机的,也就是说每种选择结果的概率都是一样的,所以选择一个点的时候已经选择的点不会有影响,那么就可以直接算出点对个数再求总体的 ...

  7. php nusoap类的使用、用法、出错 及说明

    NuSOAP 是 PHP 环境下的 WEB 服务编程工具,用于创建或调用 WEB 服务它是一个开源软件,当前版本是 0.7.2 ,支 持 SOAP1.1 WSDL1.1 ,可以与其他支持 SOAP1. ...

  8. [zjoi2016]小星星 (容斥+DP)

    我们先用树形DP,求出选取集合S中的点,满足连通性的但是标号可重的方案数,贡献给F(i)(1\(\leq\)i\(\leq\)\(\mid S\mid\)),也就是我们要处理出F(i)代表取至多i个点 ...

  9. 洛谷 P1417 烹调方案 (01背包拓展)

    一看到这道题就是01背包 但是我注意到价值和当前的时间有关. 没有想太多,直接写,0分 然后发现输入方式不对-- 改了之后只有25分 我知道wa是因为时间会影响价值,但不知道怎么做. 后来看了题解,发 ...

  10. 紫书 例题 10-3 UVa 10375 (唯一分解定理)

    这道题感觉非常的秀 因为结果会很大,所以就质因数分解分开来算 非常的巧妙! #include<cstdio> #include<vector> #include<cstr ...