-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.xml
231 lines (160 loc) · 43.9 KB
/
search.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>Hello World</title>
<url>/2020/11/17/hello-world/</url>
<content><![CDATA[<h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/writing.html">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/server.html">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/generating.html">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>系统搭建Quick Start</category>
</categories>
<tags>
<tag>pip源</tag>
</tags>
</entry>
<entry>
<title>OpenCV+Python+Windows下的环境搭建</title>
<url>/2016/12/18/OpenCV+Python+Windows%E4%B8%8B%E7%9A%84%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/</url>
<content><![CDATA[<p>直接下载 安装OpenCV-2.4.5.exe ,实际上就是解压缩的过程。进入OpenCV的安装目录下找到:<code>\build\python\2.7\cv2.pyd</code>,将<strong>cv2.pyd</strong>复制到<strong>Python</strong>的子目录下:<code>\Lib\site-packages\</code>,至此环境就搭建好了。</p>
<p>OpenCV自带了很多示例程序,进入OpenCV安装目录下的子目录:<code>\samples\python2\</code> ,可以看到很多以.py为后缀名的文件;用python随便打开一个.py文件,按F5键运行,看看效果</p>
]]></content>
<tags>
<tag>OpenCV</tag>
<tag>Python</tag>
<tag>Windows</tag>
</tags>
</entry>
<entry>
<title>Ubuntu环境变量配置</title>
<url>/2015/01/19/Ubuntu%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E9%85%8D%E7%BD%AE/</url>
<content><![CDATA[<p>设置 Linux 环境变量可以通过 export 实现,也可以通过修改几个文件来实现,有必要弄清楚这两种方法以及这几个文件的区别。</p>
<h4 id="全局环境变量"><a href="#全局环境变量" class="headerlink" title="全局环境变量"></a>全局环境变量</h4><ul>
<li>etc/profile: 此文件为系统的每个用户设置环境信息。当用户登录时,该文件被执行一次,并从 /etc/profile.d 目录的配置文件中搜集shell 的设置。一般用于设置所有用户使用的全局变量。</li>
<li>/etc/bashrc: 当 bash shell 被打开时,该文件被读取。也就是说,每次新打开一个终端 shell,该文件就会被读取。</li>
</ul>
<h4 id="用户变量"><a href="#用户变量" class="headerlink" title="用户变量"></a>用户变量</h4><ul>
<li><del>/.bash_profile 或 ~/.profile: 只对单个用户生效,当用户登录时该文件仅执行一次。用户可使用该文件添加自己使用的 shell 变量信息。另外在不同的LINUX操作系统下,这个文件可能是不同的,可能是 ~/.bash_profile, ~/.bash_login 或 ~/.profile 其中的一种或几种,如果存在几种的话,那么执行的顺序便是:</del>/.bash_profile、 ~/.bash_login、 ~/.profile。比如 Ubuntu 系统一般是 ~/.profile 文件。</li>
<li>~/.bashrc: 只对单个用户生效,当登录以及每次打开新的 shell 时,该文件被读取。</li>
</ul>
<h4 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h4><p>此外,修改 /etc/environment 这个文件也能实现环境变量的设置。/etc/environment 设置的也是全局变量,从文件本身的作用上来说, /etc/environment 设置的是整个系统的环境,而/etc/profile是设置所有用户的环境。有几点需注意:</p>
<ul>
<li>系统先读取 etc/profile 再读取 /etc/environment(还是反过来?)</li>
<li>/etc/environment 中不能包含命令,即直接通过 <code>VAR="..."</code> 的方式设置,不使用 export 。</li>
<li>使用 <code>source /etc/environment</code> 可以使变量设置在当前窗口立即生效,需注销/重启之后,才能对每个新终端窗口都生效。</li>
</ul>
<h4 id="修改-Linux-环境变量实例"><a href="#修改-Linux-环境变量实例" class="headerlink" title="修改 Linux 环境变量实例"></a>修改 Linux 环境变量实例</h4><p>以 Ubuntu 为例,修改 ~/.profile 文件:</p>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">vim ~/.profile</span><br></pre></td></tr></table></figure>
<p>如果该文件存在,则在文件的最后看到如下代码,PATH 变量的值使用冒号(:)隔开的:</p>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> <span class="built_in">set</span> PATH so it includes user<span class="string">'s private bin if it existsif [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH"fi</span></span></span><br></pre></td></tr></table></figure>
<p>在最后加上代码 <code>PATH="$PATH:/usr/local/hadoop/bin"</code>,注意等号(=)两边不要有空格,即:</p>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> <span class="built_in">set</span> PATH so it includes user<span class="string">'s private bin if it existsif [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH"fiPATH="$PATH:/usr/local/hadoop/bin"</span></span></span><br></pre></td></tr></table></figure>
<p>因为这个文件是在用户登陆是才读取一次的,所以需要重启才会生效(修改 /etc/profile、/etc/environment 也是如此)。但可以使用命令 <code>source ./.profile</code> 使其立即生效。通过 <code>echo $PATH</code> 可以看到修改后的变量值:</p>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">source ./.profileecho $PATH</span><br></pre></td></tr></table></figure>
<h2 id="通过-Shell-命令-export-修改-Linux-环境变量"><a href="#通过-Shell-命令-export-修改-Linux-环境变量" class="headerlink" title="通过 Shell 命令 export 修改 Linux 环境变量"></a>通过 Shell 命令 export 修改 Linux 环境变量</h2><p>另一种修改 Linux 环境变量的方式就是通过 Shell 命令 export,注意变量名不要有美元号 $,赋值语句中才需要有:</p>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">export PATH=$PATH:/usr/local/hadoop/bin</span><br></pre></td></tr></table></figure>
<p>*export 方式只对当前终端 Shell 有效使用 export 设置的变量,只对当前终端 Shell 有效,也就是说如果新打开一个终端,那这个 export 设置的变量在新终端中使无法读取到的。适合设置一些临时变量。</p>
<p>根据变量所需,选择设置方式,例如 JAVA_HOME 这类变量,就适合将其设为为全局变量,可在 /etc/environment 中设置。</p>
<h4 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h4><p><a href="http://www.powerxing.com/linux-environment-variable/">http://www.powerxing.com/linux-environment-variable/</a></p>
]]></content>
<categories>
<category>系统搭建</category>
</categories>
<tags>
<tag>Ubuntu</tag>
<tag>环境变量</tag>
</tags>
</entry>
<entry>
<title>VCForPython27的安装路径</title>
<url>/2020/11/17/VCForPython27%E7%9A%84%E5%AE%89%E8%A3%85%E8%B7%AF%E5%BE%84/</url>
<content><![CDATA[<p><code>VCForPython27(Microsoft Visual C++ Compiler for Python 2.7)</code></p>
<p><code>C:\Users\hss\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC</code></p>
]]></content>
<categories>
<category>系统搭建</category>
</categories>
<tags>
<tag>VCForPython27</tag>
</tags>
</entry>
<entry>
<title>不能使用im2patch、patch2im的原因</title>
<url>/2016/09/19/%E4%B8%8D%E8%83%BD%E4%BD%BF%E7%94%A8im2patch%E3%80%81patch2im%E7%9A%84%E5%8E%9F%E5%9B%A0/</url>
<content><![CDATA[<p>patch2im对图像恢复时候严格按照im2patch的截取快的位置信息进行配准恢复。而实际实验中im2patch截取的块大小是33×33,而恢复时所操作的块大小为21×21。所以只能按照卷积论文中的构造训练集的代码进行改写。</p>
]]></content>
<categories>
<category>图像处理</category>
</categories>
<tags>
<tag>patch</tag>
<tag>img</tag>
</tags>
</entry>
<entry>
<title>pip源修改</title>
<url>/2020/11/17/pip%E6%BA%90%E9%85%8D%E7%BD%AE%E4%BF%AE%E6%94%B9/</url>
<content><![CDATA[<h3 id="Mac"><a href="#Mac" class="headerlink" title="Mac"></a>Mac</h3><p>使用Terminal在当前用户目录下通过命令<code>mkdir .pip</code>新建一个**.pip<strong>文件夹,然后在</strong>.pip<strong>文件夹内使用命令<code>touch pip.conf</code>新建一个文件</strong>pip.conf<strong>,在文件</strong>pip.conf**中以下内容:</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">[<span class="keyword">global</span>]</span><br><span class="line">index-url = http://pypi.douban.com/simple</span><br><span class="line">[install]</span><br><span class="line">trusted-host=pypi.douban.com</span><br></pre></td></tr></table></figure>
<h3 id="Windows"><a href="#Windows" class="headerlink" title="Windows"></a>Windows</h3><p>在当前用户目录下<code>C:\Users\hss</code>新建一个<strong>pip</strong>文件夹,然后在该文件夹内新家一个文件<strong>pip.ini</strong> ,并在文件<strong>pip.ini</strong>中写入以下内容:</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">[<span class="keyword">global</span>]</span><br><span class="line">index-url = http://pypi.douban.com/simple</span><br><span class="line">[install]</span><br><span class="line">trusted-host=pypi.douban.com</span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>系统搭建</category>
</categories>
<tags>
<tag>pip源</tag>
</tags>
</entry>
<entry>
<title>我的第一篇博文</title>
<url>/2015/12/26/%E6%88%91%E7%9A%84%E7%AC%AC%E4%B8%80%E7%AF%87%E5%8D%9A%E6%96%87/</url>
<content><![CDATA[<p>今天正式开始了,这是我的第一篇文章。</p>
]]></content>
<categories>
<category>个人随想</category>
</categories>
<tags>
<tag>个人随想</tag>
</tags>
</entry>
<entry>
<title>实验流程分析</title>
<url>/2016/10/10/%E5%AE%9E%E9%AA%8C%E6%B5%81%E7%A8%8B%E5%88%86%E6%9E%90/</url>
<content><![CDATA[<h2 id="准备数据集"><a href="#准备数据集" class="headerlink" title="准备数据集"></a>准备数据集</h2><h2 id="训练集"><a href="#训练集" class="headerlink" title="训练集"></a>训练集</h2><pre><code>使用SRCNN论文中的**generate_train.m**和直接生成。只需加一句`image = image*2-1`将数据归一化到(-1,1)之间。最后生成的数据结构为**train.h5[data,label]**:</code></pre>
<h2 id="测试集"><a href="#测试集" class="headerlink" title="测试集"></a>测试集</h2><pre><code>由于只对一张图像进行测试,因此需要自己写程序进行测试集的构造。大致流程与训练集的构造过程相仿。都是提取灰度值、归一化像素值到(-1,1)之间、放大倍数的剪切、放大倍数的缩放、开始提取`33×33、21×21`的图像块。最后形成的数据结构为:`data(:, :, 1, count)、label(:, :, 1, count)`。程序为**im2patch_test.m**,代码如下所示:</code></pre>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">clc,clear all;</span><br><span class="line"></span><br><span class="line">%% 初始化参数</span><br><span class="line">filename = 'C:\Users\hss\Documents\MATLAB\SR\data\Test\Set5\butterfly_GT.bmp'</span><br><span class="line">scale = <span class="number">3</span>;</span><br><span class="line">size_patch = <span class="number">33</span>;</span><br><span class="line">size_label = <span class="number">21</span>;</span><br><span class="line">padding = <span class="built_in">abs</span>(size_patch - size_label)/<span class="number">2</span>;</span><br><span class="line">stride = <span class="number">14</span>;</span><br><span class="line"></span><br><span class="line">%% 读取图像、进行切割、转化为灰度图、像素值归一化到(<span class="number">-1</span>,<span class="number">1</span>)</span><br><span class="line">image = imread(filename);</span><br><span class="line">image = modcrop(image, scale);</span><br><span class="line">image = rgb2ycbcr(image);</span><br><span class="line">image = image(:,:,<span class="number">1</span>);</span><br><span class="line">image = im2double(image);</span><br><span class="line">image = image*<span class="number">2</span><span class="number">-1</span>;</span><br><span class="line"></span><br><span class="line">%% 进行放缩得到与原来大小一致的低分辨图像(插值图像)</span><br><span class="line">[m,n] = size(image);</span><br><span class="line">im_b = imresize(imresize(image,1/scale,'bicubic'),[m,n],'bicubic');</span><br><span class="line"></span><br><span class="line">%% 对清晰图像和插值图像进行外扩以便于构建测试集、并获取外扩图像的尺寸</span><br><span class="line">im_input = padarray(im_b,[padding,padding],'symmetric');</span><br><span class="line">im_label = padarray(image,[padding,padding],'symmetric');</span><br><span class="line">[hei,wid] = size(im_input);</span><br><span class="line"></span><br><span class="line">%% 初始化数据块、标签块、标签块矩阵(存放所有的标签块权值)、计数器</span><br><span class="line">data = zeros(size_patch,size_patch,<span class="number">1</span>,<span class="number">1</span>);</span><br><span class="line">label = zeros(size_label,size_label,<span class="number">1</span>,<span class="number">1</span>);</span><br><span class="line">count = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">%% 开始循环</span><br><span class="line">temp_x = <span class="number">1</span>:stride:hei-size_patch + <span class="number">1</span>;</span><br><span class="line">x = [temp_x hei-size_patch+<span class="number">1</span>];</span><br><span class="line">temp_y = <span class="number">1</span>:stride:wid-size_patch + <span class="number">1</span>;</span><br><span class="line">y = [temp_y wid-size_patch+<span class="number">1</span>];</span><br><span class="line">count = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">for</span> i = <span class="number">1</span>:size(x,<span class="number">2</span>)</span><br><span class="line"> <span class="keyword">for</span> j = <span class="number">1</span>:size(y,<span class="number">2</span>) </span><br><span class="line"> data(:, :, <span class="number">1</span>, count) = im_input(x(i) : x(i)+size_patch<span class="number">-1</span>, y(j) : y(j)+size_patch<span class="number">-1</span>);</span><br><span class="line"> label(:, :, <span class="number">1</span>, count) = im_label(x(i)+padding : x(i)+padding+size_label<span class="number">-1</span>, y(j)+padding : y(j)+padding+size_label<span class="number">-1</span>);</span><br><span class="line"> count = count + <span class="number">1</span>;</span><br><span class="line"> end</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line">%% 将数据、标签、标签权值矩阵进行持久化存储,image像素值归一化到(<span class="number">0</span>,<span class="number">255</span>)</span><br><span class="line">save('test_butterfly.mat','data','label');</span><br><span class="line">image = (image+<span class="number">1</span>)/<span class="number">2</span>; </span><br><span class="line">image = uint8(image * <span class="number">255</span>);</span><br><span class="line">save('ground_image_butterfly.mat','image')</span><br><span class="line">imshow(image)</span><br></pre></td></tr></table></figure>
<h2 id="图像的恢复"><a href="#图像的恢复" class="headerlink" title="图像的恢复"></a>图像的恢复</h2><pre><code>一边训练一边恢复、因此恢复图像的程序使用python编写。大致思路与构造测试集时相仿。为了方便使用将其写成一个函数,该函数需要接收预测数据`pred_patch`、图像的剪切后大小`image_size`。具体程序如下:</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">patch2im</span>(<span class="params">pred_patch,image_size</span>):</span> </span><br><span class="line"> size_patch=<span class="number">33</span></span><br><span class="line"> size_label=<span class="number">21</span></span><br><span class="line"> stride=<span class="number">14</span> </span><br><span class="line"> <span class="comment"># 初始化参数</span></span><br><span class="line"> <span class="keyword">from</span> numpy <span class="keyword">import</span> zeros</span><br><span class="line"> padding = <span class="built_in">abs</span>(size_patch - size_label)/<span class="number">2</span> </span><br><span class="line"> hei = image_size[<span class="number">0</span>] + <span class="number">2</span>*padding</span><br><span class="line"> wid = image_size[<span class="number">1</span>] + <span class="number">2</span>*padding </span><br><span class="line"> </span><br><span class="line"> <span class="comment">#开始处理图像块</span></span><br><span class="line"> count = <span class="number">0</span></span><br><span class="line"> x = <span class="built_in">range</span>(<span class="number">0</span>, hei - size_patch, stride)</span><br><span class="line"> x.append(hei - size_patch)</span><br><span class="line"> y = <span class="built_in">range</span>(<span class="number">0</span>, wid - size_patch , stride) </span><br><span class="line"> y.append(wid - size_patch)</span><br><span class="line"> x = <span class="built_in">sorted</span>(x)</span><br><span class="line"> y = <span class="built_in">sorted</span>(y)</span><br><span class="line"> </span><br><span class="line"> sum_patch = zeros([hei ,wid])</span><br><span class="line"> weight = zeros([hei, wid])</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(x)):</span><br><span class="line"> <span class="keyword">for</span> j <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(y)): </span><br><span class="line"> im_patch = zeros([hei,wid]) </span><br><span class="line"> im_patch[x[i]+padding : x[i]+padding+size_label, y[j]+padding : y[j]+padding+size_label] = pred_patch[:,:,count].reshape([size_label,size_label])</span><br><span class="line"> sum_patch = sum_patch + im_patch </span><br><span class="line"> count = count +<span class="number">1</span></span><br><span class="line"></span><br><span class="line"> weight_temp = zeros([hei, wid])</span><br><span class="line"> weight_temp[x[i]+padding : x[i]+padding+size_label, y[j]+padding : y[j]+padding+size_label]=<span class="number">1</span></span><br><span class="line"> weight = weight + weight_temp </span><br><span class="line"> </span><br><span class="line"> im = (sum_patch/weight)</span><br><span class="line"> im = shave(im,[padding,padding]) </span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> im</span><br></pre></td></tr></table></figure>
<h2 id="去除多余边缘的函数"><a href="#去除多余边缘的函数" class="headerlink" title="去除多余边缘的函数"></a>去除多余边缘的函数</h2><figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">shave</span>(<span class="params">I,border=<span class="literal">None</span></span>):</span></span><br><span class="line"> I = I[border[<span class="number">0</span>]:I.shape[<span class="number">0</span>]-border[<span class="number">0</span>],border[<span class="number">1</span>]:I.shape[<span class="number">1</span>]-border[<span class="number">1</span>]]</span><br><span class="line"> <span class="keyword">return</span> I</span><br></pre></td></tr></table></figure>
<h2 id="计算PSNR的函数"><a href="#计算PSNR的函数" class="headerlink" title="计算PSNR的函数"></a>计算PSNR的函数</h2><figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">compute_psnr</span>(<span class="params">im1,im2</span>):</span></span><br><span class="line"> <span class="keyword">from</span> numpy <span class="keyword">import</span> sqrt, square, mean, log10</span><br><span class="line"> diff = (im1 - im2).reshape(<span class="number">-1</span>,<span class="number">1</span>)</span><br><span class="line"> rmse = sqrt(mean(square(diff)))</span><br><span class="line"> psnr = <span class="number">20</span>*log10(<span class="number">255.0</span>/rmse)</span><br><span class="line"> <span class="keyword">return</span> psnr</span><br></pre></td></tr></table></figure>
<h2 id="网络的构建"><a href="#网络的构建" class="headerlink" title="网络的构建"></a>网络的构建</h2><pre><code>网络输入层接收`shape=(None,1,33,33)`类型的输入,4层中间层采用`tanh`函数,输出层采用441输出,激活函数仍是`tanh`。具体代码如下:</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">build_mlp</span>(<span class="params">input_var = <span class="literal">None</span></span>):</span></span><br><span class="line"> l_in = InputLayer(shape=(<span class="literal">None</span>,<span class="number">1</span>,<span class="number">33</span>,<span class="number">33</span>),input_var=input_var)</span><br><span class="line"> l_hid1 = batch_norm(DenseLayer(l_in,num_units=<span class="number">1024</span>,nonlinearity=lasagne.nonlinearities.tanh)) </span><br><span class="line"> l_hid2 = batch_norm(DenseLayer(l_hid1,num_units=<span class="number">1024</span>,nonlinearity=lasagne.nonlinearities.tanh))</span><br><span class="line"> l_hid3 = batch_norm(DenseLayer(l_hid2,num_units=<span class="number">1024</span>,nonlinearity=lasagne.nonlinearities.tanh))</span><br><span class="line"> l_hid4 = batch_norm(DenseLayer(l_hid3,num_units=<span class="number">1024</span>,nonlinearity=lasagne.nonlinearities.tanh))</span><br><span class="line"> l_out = DenseLayer(l_hid4, num_units=<span class="number">441</span>,nonlinearity=lasagne.nonlinearities.tanh)</span><br><span class="line"> <span class="keyword">return</span> l_out</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="定义训练函数"><a href="#定义训练函数" class="headerlink" title="定义训练函数"></a>定义训练函数</h2><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">input_var = theano.tensor.ftensor4(<span class="string">'inputs'</span>)</span><br><span class="line">target_var = theano.tensor.fmatrix(<span class="string">'targets'</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span> <span class="string">"Building model and compiling functions..."</span></span><br><span class="line">network = build_mlp(input_var)</span><br><span class="line"></span><br><span class="line">prediction = lasagne.layers.get_output(network)</span><br><span class="line">loss = lasagne.objectives.squared_error(prediction,target_var).mean()</span><br><span class="line"></span><br><span class="line">learning_rate = <span class="number">0.01</span> </span><br><span class="line">Layers=lasagne.layers.get_all_layers(network)</span><br><span class="line"><span class="comment">#updates={}</span></span><br><span class="line">updates = collections.OrderedDict()</span><br><span class="line">updates.update(lasagne.updates.sgd(</span><br><span class="line"> loss, </span><br><span class="line"> Layers[<span class="number">-1</span>].get_params(trainable=<span class="literal">True</span>),</span><br><span class="line"> learning_rate*<span class="number">0.1</span></span><br><span class="line"> ))</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(Layers)<span class="number">-1</span>): </span><br><span class="line"> updates.update(lasagne.updates.sgd(</span><br><span class="line"> loss, </span><br><span class="line"> Layers[i].get_params(trainable=<span class="literal">True</span>),</span><br><span class="line"> learning_rate</span><br><span class="line"> ))</span><br><span class="line"></span><br><span class="line">train_fn = theano.function([input_var,target_var],loss,updates = updates)</span><br></pre></td></tr></table></figure>
<h2 id="定义测试函数"><a href="#定义测试函数" class="headerlink" title="定义测试函数"></a>定义测试函数</h2><figure class="highlight python"><table><tr><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>
<pre><code>使用SRCNN论文中的程序(generate_train.m)直接上生成训练集(train.h5)。数据结构为[data,label]。同样的利用generate_test.m、generate_val.m依次生成测试集(test.h5)和验证集(val.h5)。本次实验中并不需要验证集和测试集。而是利用im2patch.m来构造测试集。</code></pre>
<p>‘网络的输出结果为(-1,1),因此在恢复图像时需要将恢复的图像进行加一除二其映射回来’</p>
<p>构造的训练集</p>
<p>代码:不同的网络层设置不同的学习率</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">network = build_mlp(input_var)</span><br><span class="line"></span><br><span class="line">prediction = lasagne.layers.get_output(network)</span><br><span class="line">loss = lasagne.objectives.squared_error(prediction,target_var).mean()</span><br><span class="line"></span><br><span class="line">learning_rate = 0.01 </span><br><span class="line">Layers=lasagne.layers.get_all_layers(network)</span><br><span class="line">#updates={}</span><br><span class="line">updates = collections.OrderedDict()</span><br><span class="line">updates.update(lasagne.updates.sgd(</span><br><span class="line"> loss, </span><br><span class="line"> Layers[-1].get_params(trainable=True),</span><br><span class="line"> learning_rate*0.1</span><br><span class="line"> ))</span><br><span class="line">for i in range(len(Layers)-1): </span><br><span class="line"> updates.update(lasagne.updates.sgd(</span><br><span class="line"> loss, </span><br><span class="line"> Layers[i].get_params(trainable=True),</span><br><span class="line"> learning_rate</span><br><span class="line"> ))</span><br><span class="line"></span><br><span class="line">train_fn = theano.function([input_var,target_var],loss,updates = updates)</span><br></pre></td></tr></table></figure>
<p>用python打印灰度图像</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">import matplotlib.pyplot as plt</span><br><span class="line">plt.imshow(img, cmap = plt.get_cmap('gray'))</span><br></pre></td></tr></table></figure>
<p>matlab输出打印变量值</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">i = 100;</span><br></pre></td></tr></table></figure>
<p>测试过程分为测试数据的构建和对预测数据进行恢复,先使用im2patch_test.m程序产生save(‘test_butterfly.mat’,’data’,’label’)、save(‘ground_image_butterfly.mat’,’image’)。im2patch的大概流程是将图像</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#coding:utf-8</span><br><span class="line">"""</span><br><span class="line">Created on Mon Apr 18 19:44:02 2016</span><br><span class="line"></span><br><span class="line">@author: hss</span><br><span class="line">"""</span><br><span class="line"></span><br><span class="line">#!/usr/bin/env python</span><br><span class="line">import h5py</span><br><span class="line">import scipy</span><br><span class="line">import lasagne, theano, time, numpy, collections</span><br><span class="line">from lasagne.layers import InputLayer, DenseLayer, batch_norm</span><br><span class="line">import matplotlib.pyplot as plt</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def load_dataset(): </span><br><span class="line"> dataTrain = h5py.File('train.h5','r')</span><br><span class="line"> X_train = dataTrain['data']</span><br><span class="line"> Y_train = dataTrain['label']</span><br><span class="line"> X_train = numpy.array(X_train).reshape(-1,1,33,33).astype("float32")</span><br><span class="line"> Y_train = numpy.array(Y_train).reshape(-1,441).astype("float32")</span><br><span class="line"> </span><br><span class="line"> #测试集</span><br><span class="line"> data_test = scipy.io.loadmat('test_butterfly.mat') </span><br><span class="line"> X_test = data_test['data'].reshape(-1,1,33,33).astype('float32')</span><br><span class="line"> Y_test = data_test['label'].reshape(-1, 441).astype('float32')</span><br><span class="line"> </span><br><span class="line"> return X_train, Y_train, X_test, Y_test</span><br><span class="line"> </span><br><span class="line">def patch2im(pred_patch,image_size): </span><br><span class="line"> size_patch=33</span><br><span class="line"> size_label=21</span><br><span class="line"> stride=14 </span><br><span class="line"> # 初始化参数</span><br><span class="line"> from numpy import zeros</span><br><span class="line"> padding = abs(size_patch - size_label)/2 </span><br><span class="line"> hei = image_size[0] + 2*padding</span><br><span class="line"> wid = image_size[1] + 2*padding </span><br><span class="line"> </span><br><span class="line"> #开始处理图像块</span><br><span class="line"> count = 0</span><br><span class="line"> x = range(0, hei - size_patch, stride)</span><br><span class="line"> x.append(hei - size_patch)</span><br><span class="line"> y = range(0, wid - size_patch , stride) </span><br><span class="line"> y.append(wid - size_patch)</span><br><span class="line"> x = sorted(x)</span><br><span class="line"> y = sorted(y)</span><br><span class="line"> </span><br><span class="line"> sum_patch = zeros([hei ,wid])</span><br><span class="line"> weight = zeros([hei, wid])</span><br><span class="line"> </span><br><span class="line"> for i in range(len(x)):</span><br><span class="line"> for j in range(len(y)): </span><br><span class="line"> im_patch = zeros([hei,wid]) </span><br><span class="line"> im_patch[x[i]+padding : x[i]+padding+size_label, y[j]+padding : y[j]+padding+size_label] = pred_patch[:,:,count].reshape([size_label,size_label])</span><br><span class="line"> sum_patch = sum_patch + im_patch </span><br><span class="line"> count = count +1</span><br><span class="line"></span><br><span class="line"> weight_temp = zeros([hei, wid])</span><br><span class="line"> weight_temp[x[i]+padding : x[i]+padding+size_label, y[j]+padding : y[j]+padding+size_label]=1</span><br><span class="line"> weight = weight + weight_temp </span><br><span class="line"> </span><br><span class="line"> im = (sum_patch/weight)</span><br><span class="line"> im = shave(im,[padding,padding]) </span><br><span class="line"> </span><br><span class="line"> return im</span><br><span class="line"> </span><br><span class="line">def iterate_minibatches(inputs, targets, batchsize, shuffle=False):</span><br><span class="line"> import numpy as np</span><br><span class="line"> assert len(inputs) == len(targets)</span><br><span class="line"> if shuffle:</span><br><span class="line"> indices = np.arange(len(inputs))</span><br><span class="line"> np.random.shuffle(indices)</span><br><span class="line"> for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):</span><br><span class="line"> if shuffle:</span><br><span class="line"> excerpt = indices[start_idx:start_idx + batchsize]</span><br><span class="line"> else:</span><br><span class="line"> excerpt = slice(start_idx, start_idx + batchsize)</span><br><span class="line"> yield inputs[excerpt], targets[excerpt]</span><br><span class="line"> </span><br><span class="line">def build_mlp(input_var = None):</span><br><span class="line"> l_in = InputLayer(shape=(None,1,33,33),input_var=input_var)</span><br><span class="line"> l_hid1 = batch_norm(DenseLayer(l_in,num_units=1024,nonlinearity=lasagne.nonlinearities.tanh)) </span><br><span class="line"> l_hid2 = batch_norm(DenseLayer(l_hid1,num_units=1024,nonlinearity=lasagne.nonlinearities.tanh))</span><br><span class="line"> l_hid3 = batch_norm(DenseLayer(l_hid2,num_units=1024,nonlinearity=lasagne.nonlinearities.tanh))</span><br><span class="line"> l_hid4 = batch_norm(DenseLayer(l_hid3,num_units=1024,nonlinearity=lasagne.nonlinearities.tanh))</span><br><span class="line"> l_out = DenseLayer(l_hid4, num_units=441,nonlinearity=lasagne.nonlinearities.tanh)</span><br><span class="line"> return l_out</span><br><span class="line"></span><br><span class="line">def compute_psnr(im1,im2):</span><br><span class="line"> from numpy import sqrt, square, mean, log10</span><br><span class="line"> diff = (im1 - im2).reshape(-1,1)</span><br><span class="line"> rmse = sqrt(mean(square(diff)))</span><br><span class="line"> psnr = 20*log10(255.0/rmse)</span><br><span class="line"> return psnr</span><br><span class="line"></span><br><span class="line">def shave(I,border=None):</span><br><span class="line"> I = I[border[0]:I.shape[0]-border[0],border[1]:I.shape[1]-border[1]]</span><br><span class="line"> return I</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">#定义图像块聚集函数,可以将预测数据直接聚集为完整图像。该函数需要pre_data做参数</span><br><span class="line">##迭代10万次,预计需要4天半,5.3好 14:12开始,预计到5.7早上结束</span><br><span class="line">##预计到10号早上</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">input_var = theano.tensor.ftensor4('inputs')</span><br><span class="line">target_var = theano.tensor.fmatrix('targets')</span><br><span class="line"></span><br><span class="line">print "Building model and compiling functions..."</span><br><span class="line">network = build_mlp(input_var)</span><br><span class="line"></span><br><span class="line">prediction = lasagne.layers.get_output(network)</span><br><span class="line">loss = lasagne.objectives.squared_error(prediction,target_var).mean()</span><br><span class="line"></span><br><span class="line">learning_rate = 0.01 </span><br><span class="line">Layers=lasagne.layers.get_all_layers(network)</span><br><span class="line">#updates={}</span><br><span class="line">updates = collections.OrderedDict()</span><br><span class="line">updates.update(lasagne.updates.sgd(</span><br><span class="line"> loss, </span><br><span class="line"> Layers[-1].get_params(trainable=True),</span><br><span class="line"> learning_rate*0.1</span><br><span class="line"> ))</span><br><span class="line">for i in range(len(Layers)-1): </span><br><span class="line"> updates.update(lasagne.updates.sgd(</span><br><span class="line"> loss, </span><br><span class="line"> Layers[i].get_params(trainable=True),</span><br><span class="line"> learning_rate</span><br><span class="line"> ))</span><br><span class="line"></span><br><span class="line">train_fn = theano.function([input_var,target_var],loss,updates = updates)</span><br><span class="line">test_prediction = lasagne.layers.get_output(network, deterministic=True)</span><br><span class="line">test_fn = theano.function([input_var],test_prediction)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">print 'loading data........'</span><br><span class="line">X_train, Y_train, X_test, Y_test = load_dataset()</span><br><span class="line">print"Starting training..."</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">num_epochs=123456</span><br><span class="line">epoch = 0</span><br><span class="line">flag_loop = True</span><br><span class="line">while (epoch<num_epochs)and(flag_loop==True):</span><br><span class="line"> epoch+=1 </span><br><span class="line"> train_err = 0</span><br><span class="line"> train_batch = 0</span><br><span class="line"> start_time = time.time()</span><br><span class="line"> for batch in iterate_minibatches(X_train, Y_train, 128, shuffle=True):</span><br><span class="line"> inputs, targets = batch</span><br><span class="line"> train_err += train_fn(inputs, targets)</span><br><span class="line"> train_batch += 1</span><br><span class="line"> print(" training loss:\t\t{:.6f}".format(train_err / train_batch))</span><br><span class="line"> with open(r'D:\net\train_err.txt','a') as f:</span><br><span class="line"> f.write('第%d次训练误差:%f\t\t'%(epoch,train_err/train_batch)+'\n')</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> psnr_initial = 0</span><br><span class="line"> decrease_rate = 0.1</span><br><span class="line"> decrease_num = 0</span><br><span class="line"> pred_patch=numpy.zeros([21,21,len(X_test)]).astype('float32')</span><br><span class="line"> # 每训练10次之后开始执行预测、并计算psnr</span><br><span class="line"> if epoch%10 == 0:</span><br><span class="line"> count = 0</span><br><span class="line"> for batch in iterate_minibatches(X_test,Y_test, 1, shuffle=False):</span><br><span class="line"> inputs,targets = batch</span><br><span class="line"> pred_patch[:,:,count] = test_fn(inputs).reshape(-1, 21, 21).astype('float32')</span><br><span class="line"> count = count + 1</span><br><span class="line"> </span><br><span class="line"> img = patch2im(pred_patch, [255,255])</span><br><span class="line"> img = (img + 1)/2</span><br><span class="line"> img = img*255</span><br><span class="line"> </span><br><span class="line"> ground_image = scipy.io.loadmat('ground_image_butterfly.mat')</span><br><span class="line"> ground_image = ground_image['image']</span><br><span class="line"></span><br><span class="line"> psnr = compute_psnr(img, ground_image).astype('float32') </span><br><span class="line"> print "第%d次验证的PSNR值:\t\t%f"%(epoch,psnr) </span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> with open(r'D:\net\psnr_log.txt','a') as f:</span><br><span class="line"> f.write('第%d次验证的PSNR值:%f \t\t'%(epoch,psnr)+'\n')</span><br><span class="line"> </span><br><span class="line"> if psnr < psnr_initial:</span><br><span class="line"> if psnr < decrease_rate*psnr_initial:</span><br><span class="line"> decrease_num = decrease_num + 1</span><br><span class="line"> else:</span><br><span class="line"> decrease_num = 0 </span><br><span class="line"> #保存网络权值参数</span><br><span class="line"> param_values = lasagne.layers.get_all_param_values(network)</span><br><span class="line"> numpy.save("param_values(%d).npy"%(epoch),param_values)</span><br><span class="line"> print "saving params of network "</span><br><span class="line"> else:</span><br><span class="line"> decrease_num = 0</span><br><span class="line"> #保存网络权值参数</span><br><span class="line"> param_values = lasagne.layers.get_all_param_values(network)</span><br><span class="line"> numpy.save('D:/net/'+'param_values(%d)_0.01_(%f).npy'%(epoch,psnr),param_values)</span><br><span class="line"></span><br><span class="line"> if decrease_num == 15:</span><br><span class="line"> flag_loop = False</span><br><span class="line"> if (epoch%100) == 1:</span><br><span class="line"> numpy.save('D:/image/'+'epoch_(%d).npy'%(epoch),img)</span><br><span class="line"></span><br><span class="line">##迭代10万次,预计需要4天半,5.3好 14:12开始,预计到5.7早上结束</span><br><span class="line">##预计到10号早上</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category>图像处理</category>
</categories>
<tags>
<tag>训练</tag>
<tag>测试</tag>
<tag>网络</tag>
</tags>
</entry>
</search>