放弃使用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. 05:Cave Cows 1 洞穴里的牛之一

    总时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  262144kB 描述 很少人知道其实奶牛非常喜欢到洞穴里面去探险.     洞窟里有N(1≤N≤100)个洞室,由 ...

  2. 【原创】TimeSten安装与配置

    1.安装TimeSten 2.安装时要指定TNS_ADMIN_LOCATION,即tnsnames.ora的路径,因为tt会根据这个连接Oracle.C:\TimesTen\tt1122_32\net ...

  3. PCL例程调试错误之缺少flann-config.cmake

    编译环境和PCL版本为:win7-x64 + MSVC2013 + PCL1.8.0-win32-MSVC2013. 调试PCL官网例程Cluster Recognition and 6DOF Pos ...

  4. Ubantu 14.04下安装高版本cmake

    Ubantu14.04 下自带的cmake版本比较低(默认为2.8),这里我们从源码编译高版本cmake: 先卸载电脑上安装的cmake (如何已安装的话): sudo apt-get autorem ...

  5. HDU-5693 D Game 动态规划 两次动规

    题目链接:https://cn.vjudge.net/problem/HDU-5693 题意 中文题 这个游戏是这样的,首先度度熊拥有一个公差集合{D},然后它依次写下N个数字排成一行.游戏规则很简单 ...

  6. IdentityServer4-前后端分离之Vue

    原文:IdentityServer4-前后端分离之Vue 前言 之前文章讲到如何使用Node.js+Express构建JavaScript客户端,实现前后端分离.本节将介绍如何使用Vue实现前后端分离 ...

  7. 按shift键调出命令行的脚本

    打开Notepad++,粘贴以下命令,并将文件命名为opencmdhere.reg(注意:文件编码格式为UCS-2 Little Endian,否则会导致中文乱码),再双击打开即可 Windows R ...

  8. dashboard安装

    1,安装程序包 # yum install -y openstack-dashboard 2,修改配置文件 # vim /etc/openstack-dashboard/local_settings ...

  9. P2899 [USACO08JAN]手机网络Cell Phone Network

    P2899 [USACO08JAN]手机网络Cell Phone Networ题目描述 Farmer John has decided to give each of his cows a cell ...

  10. 这两道题目很相似 最优还钱方式 & 除法推导

    http://www.cnblogs.com/grandyang/p/6108158.html http://www.cnblogs.com/grandyang/p/5880133.html 都是根据 ...