PageSourceSearch

https://cogito2012.github.io/2017/08/20/Caffe%E7%9A%84%E9%9D%99%E6…8%B0%83%E7%94%A8protobuf%E6%96%B9%E5%BC%8F/

html cogito2012.github.io collected 2026-10-03 08:48:59 UTC 17,522 bytes, 363 lines download raw bytes

1<!DOCTYPE html>
2<html>
3<head>
4  <meta charset="utf-8">
5  
6  <title>Caffe使用问题解决之protobuf的静态调用 | Cogito2012&#39;s Blog</title>
7  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
8  <meta name="description" content="问题来源
9问题原因
10解决办法
11参考资源">
12<meta property="og:type" content="article">
13<meta property="og:title" content="Caffe使用问题解决之protobuf的静态调用">
14<meta property="og:url" content="https://github.com/Cogito2012/2017/08/20/Caffe的静态调用protobuf方式/index.html">
15<meta property="og:site_name" content="Cogito2012's Blog">
16<meta property="og:description" content="问题来源
17问题原因
18解决办法
19参考资源">
20<meta property="og:updated_time" content="2017-08-21T04:56:16.786Z">
21<meta name="twitter:card" content="summary">
22<meta name="twitter:title" content="Caffe使用问题解决之protobuf的静态调用">
23<meta name="twitter:description" content="问题来源
24问题原因
25解决办法
26参考资源">
27  
28  
29    <link rel="icon" href="/favicon.png">
30  
31  <link rel="stylesheet" href="/css/style.css">
32</head>
33<body>
34  <div id="container">
35    <div class="left-col">
36    <div class="overlay"></div>
37<div class="intrude-less">
38	<header id="header" class="inner">
39		<a href="/" class="profilepic">
40			
41			<img lazy-src="/assets/img/head.jpg" class="js-avatar">
42			
43		</a>
44
45		<hgroup>
46		  <h1 class="header-author"><a href="/">Wentao Bao</a></h1>
47		</hgroup>
48
49		
50		<p class="header-subtitle">不难,要你干嘛?!</p>
51		
52
53		
54			<div class="switch-btn">
55				<div class="icon">
56					<div class="icon-ctn">
57						<div class="icon-wrap icon-house" data-idx="0">
58							<div class="birdhouse"></div>
59							<div class="birdhouse_holes"></div>
60						</div>
61						<div class="icon-wrap icon-ribbon hide" data-idx="1">
62							<div class="ribbon"></div>
63						</div>
64						
65						
66						<div class="icon-wrap icon-me hide" data-idx="3">
67							<div class="user"></div>
68							<div class="shoulder"></div>
69						</div>
70						
71					</div>
72					
73				</div>
74				<div class="tips-box hide">
75					<div class="tips-arrow"></div>
76					<ul class="tips-inner">
77						<li>菜单</li>
78						<li>标签</li>
79						
80						
81						<li>关于我</li>
82						
83					</ul>
84				</div>
85			</div>
86		
87
88		<div class="switch-area">
89			<div class="switch-wrap">
90				<section class="switch-part switch-part1">
91					<nav class="header-menu">
92						<ul>
93						
94							<li><a href="/">主页</a></li>
95				        
96							<li><a href="/archives">所有文章</a></li>
97				        
98							<li><a href="/tags/随笔">随笔</a></li>
99				        
100							<li><a href="/tags/技术">技术</a></li>
101				        
102						</ul>
103					</nav>
104					<nav class="header-nav">
105						<div class="social">
106							
107								<a class="github" target="_blank" href="https://github.com/Cogito2012" title="github">github</a>
108					        
109								<a class="weibo" target="_blank" href="http://weibo.com/u/3187172412" title="weibo">weibo</a>
110					        
111								<a class="zhihu" target="_blank" href="http://www.zhihu.com/people/Cogito2012" title="zhihu">zhihu</a>
112					        
113						</div>
114					</nav>
115				</section>
116				
117				
118				<section class="switch-part switch-part2">
119					<div class="widget tagcloud" id="js-tagcloud">
120						
121					</div>
122				</section>
123				
124				
125				
126
127				
128				
129				<section class="switch-part switch-part3">
130				
131					<div id="js-aboutme">武汉大学IIP</div>
132				</section>
133				
134			</div>
135		</div>
136	</header>				
137</div>
138    </div>
139    <div class="mid-col">
140      <nav id="mobile-nav">
141  	<div class="overlay">
142  		<div class="slider-trigger"></div>
143  		<h1 class="header-author js-mobile-header hide">Wentao Bao</h1>
144  	</div>
145	<div class="intrude-less">
146		<header id="header" class="inner">
147			<div class="profilepic">
148				<img lazy-src="/assets/img/head.jpg" class="js-avatar">
149			</div>
150			<hgroup>
151			  <h1 class="header-author">Wentao Bao</h1>
152			</hgroup>
153			
154			<p class="header-subtitle">不难,要你干嘛?!</p>
155			
156			<nav class="header-menu">
157				<ul>
158				
159					<li><a href="/">主页</a></li>
160		        
161					<li><a href="/archives">所有文章</a></li>
162		        
163					<li><a href="/tags/随笔">随笔</a></li>
164		        
165					<li><a href="/tags/技术">技术</a></li>
166		        
167		        <div class="clearfix"></div>
168				</ul>
169			</nav>
170			<nav class="header-nav">
171				<div class="social">
172					
173						<a class="github" target="_blank" href="https://github.com/Cogito2012" title="github">github</a>
174			        
175						<a class="weibo" target="_blank" href="http://weibo.com/u/3187172412" title="weibo">weibo</a>
176			        
177						<a class="zhihu" target="_blank" href="http://www.zhihu.com/people/Cogito2012" title="zhihu">zhihu</a>
178			        
179				</div>
180			</nav>
181		</header>				
182	</div>
183</nav>
184      <div class="body-wrap"><article id="post-Caffe的静态调用protobuf方式" class="article article-type-post" itemscope itemprop="blogPost">
185  
186    <div class="article-meta">
187      <a href="/2017/08/20/Caffe的静态调用protobuf方式/" class="article-date">
188  	<time datetime="2017-08-20T08:45:13.580Z" itemprop="datePublished">2017-08-20</time>
189</a>
190    </div>
191  
192  <div class="article-inner">
193    
194      <input type="hidden" class="isFancy" />
195    
196    
197      <header class="article-header">
198        
199  
200    <h1 class="article-title" itemprop="name">
201      Caffe使用问题解决之protobuf的静态调用
202    </h1>
203  
204
205      </header>
206      
207      <div class="article-info article-info-post">
208        
209        
210
211        <div class="clearfix"></div>
212      </div>
213      
214    
215    <div class="article-entry" itemprop="articleBody">
216      
217        <hr>
218<blockquote>
219<ul>
220<li>问题来源</li>
221<li>问题原因</li>
222<li>解决办法</li>
223<li>参考资源</li>
224</ul>
225</blockquote>
226<a id="more"></a>
227<h2 id="问题来源"><a href="#问题来源" class="headerlink" title="问题来源"></a>问题来源</h2><p>在Linux系统中使用Matlab的Faster RCNN做目标检测时,想必大家都会对于这样一个问题表示深恶痛绝:启动Matlab运行Faster RCNN的demo或者其它训练脚本后,如果不是正常结束运行(比如matlab代码出bug,或者调试过程中止),再次运行该matlab脚本时,会出现如下报错:<br><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"># errors in Matlab:</div><div class="line">[libprotobuf ERROR google/protobuf/descriptor_database.cc:57] File already exists in database: caffe.proto</div><div class="line">[libprotobuf FATAL google/protobuf/descriptor.cc:954] CHECK failed: generated_database_-&gt;Add(encoded_file_descriptor, size): </div><div class="line"></div><div class="line"># errors in terminal:</div><div class="line">Caught "std::exception" Exception message is:</div><div class="line">CHECK failed: generated_database_-&gt;Add(encoded_file_descriptor, size):</div></pre></td></tr></table></figure></p>
228<p>这个问题不仅仅是Matlab版的Faster RCNN中会出现,而且是在官方的BVLC/caffe中也同样存在,只要有多个程序同时运行时调用caffe,就会报上述<code>libprotobuf ERROR</code>错误。</p>
229<h2 id="问题原因"><a href="#问题原因" class="headerlink" title="问题原因"></a>问题原因</h2><p>原因是因为在Linux操作系统中,以Ubuntu14.04为例,在配置caffe的系统环境时需要安装Google的protobuf库,官网给出的安装方式为:<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sudo apt-get install libprotobuf-dev protobuf-compiler</div></pre></td></tr></table></figure></p>
230<p>这样安装默认的protobuf的configure选项是生成动态链接库(学过一点C/C++的应该都知道动态库和静态库),即libprotobuf.so文件,无法满足同一个库文件被多个进程同时调用的要求,因此需要改成静态调用protobuf,也就是安装protobuf时需要生成动态链接库libprotobuf.a文件,然后在caffe里面以静态调用的方式使用protobuf。</p>
231<h2 id="解决办法">
231<a href="#解决办法" class="headerlink" title="解决办法"></a>解决办法</h2><h3 id="protobuf的编译安装"><a href="#protobuf的编译安装" class="headerlink" title="protobuf的编译安装"></a>protobuf的编译安装</h3><p>以protobuf-2.5.0版本为例,首先从google官网下载protobuf源码包:<br>下载地址:<a href="http://protobuf.googlecode.com/files/protobuf-2.3.0.zip" target="_blank" rel="external">http://protobuf.googlecode.com/files/protobuf-2.3.0.zip</a>.<br>附赠网盘地址:链接:<a href="http://pan.baidu.com/s/1eSnHBEy" target="_blank" rel="external">http://pan.baidu.com/s/1eSnHBEy</a> 密码:skrc</p>
232<p>然后解压,修改配置文件<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">sudo tar zxvf protobuf<span class="number">-2.5</span><span class="number">.0</span>.tar.gz</div><div class="line">cd protobuf<span class="number">-2.5</span><span class="number">.0</span></div><div class="line">sudo gedit ./configure <span class="comment"># 或者 sudo vim ./configure</span></div></pre></td></tr></table></figure></p>
233<p>然后查找到./configure文件的CFLAGS和CXXFLAGS,添加<code>-fPIC</code>选项,即做如下更改:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> <span class="built_in">test</span> <span class="string">"x<span class="variable">$&#123;ac_cv_env_CFLAGS_set&#125;</span>"</span> = <span class="string">"x"</span>; <span class="keyword">then</span> :</div><div class="line"> CFLAGS=<span class="string">"-fPIC"</span> </div><div class="line"><span class="keyword">fi</span> </div><div class="line"><span class="keyword">if</span> <span class="built_in">test</span> <span class="string">"x<span class="variable">$&#123;ac_cv_env_CXXFLAGS_set&#125;</span>"</span> = <span class="string">"x"</span>; <span class="keyword">then</span> :   </div><div class="line"> CXXFLAGS=<span class="string">"-fPIC"</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure></p>
234<p>然后添加<code>--disable-shared</code>选项进行protobuf的编译和安装:<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">sudo sh ./configrue --disable-shared</div><div class="line">sudo make -j8</div><div class="line">sudo make check -j8</div><div class="line">sudo make install -j8</div></pre></td></tr></table></figure></p>
235<p>最后确保上述命令输出没有错误后,刷新系统的共享库,并检查protobuf是否安装到系统目录:<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">sudo ldconfig <span class="comment"># refresh shared library cache  </span></div><div class="line">protoc --version</div></pre></td></tr></table></figure></p>
236<h3 id="Caffe的编译前更改"><a href="#Caffe的编译前更改" class="headerlink" title="Caffe的编译前更改"></a>Caffe的编译前更改</h3><p>在按照官网给的caffe编译命令之前,需要更改Makefile文件,以支持protobuf的动态加载<br>首先,命令<figure class="highlight plain"><figcaption><span>gedit Makefile```打开Makefile文件,查找`LDFLAGS`的定义位置,添加如下一行选项:</span></figcaption><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">```python</div><div class="line">LDFLAGS += -Wl,-Bstatic -lprotobuf -Wl,-Bdynamic</div></pre></td></tr></table></figure></p>
237<p>然后,查找<code>LIBRARIES</code>的定义位置,将<code>protobuf</code>一项删除,删除后的<code>LIBRARIES</code>定义为:<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">LIBRARIES += glog gflags boost_system boost_filesystem m hdf5_hl hdf5</div></pre></td></tr></table></figure></p>
238<p>最后,查找<code>CXXFLAGS</code>的定义位置,添加<code>-fPIC</code>选项<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">CXXFLAGS += -MMD -MP -fPIC</div></pre></td></tr></table></figure></p>
239<p>###Caffe的编译<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">sudo make clean <span class="comment"># if failed before</span></div><div class="line">sudo make -j8</div><div class="line">sudo make matcaffe</div></pre></td></tr></table></figure></p>
240<h2 id="参考资源"><a href="#参考资源" class="headerlink" title="参考资源"></a>参考资源</h2><p>[1]. <a href="http://blog.csdn.net/fengbingchun/article/details/72318839" target="_blank" rel="external">http://blog.csdn.net/fengbingchun/article/details/72318839</a><br>[2]. <a href="http://blog.csdn.net/linyushan11/article/details/10378419" target="_blank" rel="external">http://blog.csdn.net/linyushan11/article/details/10378419</a><br>[3]. <a href="http://caffe.berkeleyvision.org/install_apt.html" target="_blank" rel="external">http://caffe.berkeleyvision.org/install_apt.html</a><br>[4]. <a href="https://github.com/BVLC/caffe/issues/1917">https://github.com/BVLC/caffe/issues/1917</a></p>
241
242      
243    </div>
244    
245  </div>
246  
247    
248<nav id="article-nav">
249  
250  
251    <a href="/2017/05/16/联合使用Group和Stack绘制柱状图/" id="article-nav-older" class="article-nav-link-wrap">
252      <div class="article-nav-title">SCI论文数据可视化--联合使用Group和Stack绘制柱状图</div>
253      <strong class="article-nav-caption">></strong>
254    </a>
255  
256</nav>
257
258  
259</article>
260
261
262<div class="share">
263	<!-- JiaThis Button BEGIN -->
264	<div class="jiathis_style">
265		<span class="jiathis_txt">分享到:</span>
266		<a class="jiathis_button_tsina"></a>
267		<a class="jiathis_button_cqq"></a>
268		<a class="jiathis_button_douban"></a>
269		<a class="jiathis_button_weixin"></a>
270		<a class="jiathis_button_tumblr"></a>
271		<a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
272	</div>
273	
273<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=1405949716054953" charset="utf-8"></script>
273
274	<!-- JiaThis Button END -->
275</div>
276
277
278
279<div class="duoshuo">
280	<!-- 多说评论框 start -->
281	<div class="ds-thread" data-thread-key="Caffe的静态调用protobuf方式" data-title="Caffe使用问题解决之protobuf的静态调用" data-url="https://github.com/Cogito2012/2017/08/20/Caffe的静态调用protobuf方式/"></div>
282	<!-- 多说评论框 end -->
283	<!-- 多说公共JS代码 start (一个网页只需插入一次) -->
284	
284<script type="text/javascript">
285	var duoshuoQuery = {short_name:"Cogito2012"};
286	(function() {
287		var ds = document.createElement('script');
288		ds.type = 'text/javascript';ds.async = true;
289		ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
290		ds.charset = 'UTF-8';
291		(document.getElementsByTagName('head')[0] 
292		 || document.getElementsByTagName('body')[0]).appendChild(ds);
293	})();
294	</script>
294
295	<!-- 多说公共JS代码 end -->
296</div>
297
298
299
300
301</div>
302      <footer id="footer">
303  <div class="outer">
304    <div id="footer-info">
305    	<div class="footer-left">
306    		&copy; 2017 Wentao Bao
307    	</div>
308      	<div class="footer-right">
309      		<a href="http://hexo.io/" target="_blank">Hexo</a>  Theme <a href="https://github.com/litten/hexo-theme-yilia" target="_blank">Yilia</a> by Litten
310      	</div>
311    </div>
312  </div>
313</footer>
314    </div>
315    
316  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
317
318
319<script>
320	var yiliaConfig = {
321		fancybox: true,
322		mathjax: true,
323		animate: true,
324		isHome: false,
325		isPost: true,
326		isArchive: false,
327		isTag: false,
328		isCategory: false,
329		open_in_new: false
330	}
331</script>
vendor: 1 bytes, line 331
331
332<script src="http://7.url.cn/edu/jslib/comb/require-2.1.6,jquery-1.9.1.min.js"></script>
vendor: 1 bytes, line 332
332
333<script src="/js/main.js"></script>
333
334
335
336
337
338
339
340<script type="text/x-mathjax-config">
341MathJax.Hub.Config({
342    tex2jax: {
343        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
344        processEscapes: true,
345        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
346    }
347});
348
349MathJax.Hub.Queue(function() {
350    var all = MathJax.Hub.getAllJax(), i;
351    for(i=0; i < all.length; i += 1) {
352        all[i].SourceElement().parentNode.className += ' has-jax';                 
353    }       
354});
355</script>
355
356
357<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
358</script>
358
359
360
361  </div>
362</body>
363</html>

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.