放弃使用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. MEF基本概念

    基本概念: Managed Extensibility Framework 或 MEF 是一个用于创建可扩展的轻型应用程序的库,在.NET 4.0发布 Container:容器,使用Compositi ...

  2. var和let的区别

    //var 和let的区别 通过var定义的变量,作用域是整个封闭的函数,是全域的, 通过let定义的变量,作用域是在块级或者是子块中 for (let i = 0; i < 10; i++) ...

  3. 模块 –OS & OS.PATH

    模块-Os模块: os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 In [25]: os.getcwd() Out[25]: 'C:\\Users\\***' os.c ...

  4. linux 删除命令注意事项(大神勿看)

    在做软连接的时候,想到一个问题,如果删除软连接,会不会删除源文件. 如果删除是真删除的话,一旦操作错误那就后悔去吧. 效果是这样的:#rm -rf pp/          如果pp是软连文件夹,那么 ...

  5. AC自动机笔记

    AC自动机 #include<iostream> #include<cstring> #include<cstdio> #include<cmath> ...

  6. Java默认方法

    示例1 interface InterfaceA { default void say() { System.out.println("InterfaceA"); } } publ ...

  7. JavaScript系列——数组元素左右移动N位算法实现

    引言 在自己刚刚毕业不久的时候,去了一家公司面试,面试官现场考了我这道题,我记忆深刻,当时没有想到思路,毫无疑问被面试官当成菜鸟了.最近刚好在研究数组的各种算法实现,就想到这道题,可以拿来实现一下,纪 ...

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

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

  9. 紫书 习题 11-9 UVa 12549 (二分图最小点覆盖)

    用到了二分图的一些性质, 最大匹配数=最小点覆盖 貌似在白书上有讲 还不是很懂, 自己看着别人的博客用网络流写了一遍 反正以后学白书应该会系统学二分图的,紫书上没讲深. 目前就这样吧. #includ ...

  10. hive导入导出数据案例

    查询数据: use ods;set /user.password=ODS-SH;select * from base_cdma_all limit 10; use tag_bonc;select * ...