超分辨率neural-enhance增強庫操作指南

1、運行代碼:
python enhance.py --type=photo --zoom=4 back.jpg
(zoom=4:放大四倍;back.jpg:要進行超分辨率的圖片)
原分辨率增強
python enhance.py --type=photo --model=repair --zoom=1 back.jpg
注:需要在Terminal上運行,最好將你的圖片放入neural-enhance文件夾中

2、運行前所需配好的環境(window下):
python版本最好爲3.4,若沒有也沒關係,筆者的版本爲python 3.6.1
查看Numpy版本,最好安裝1.15.1,以防不匹配(沒有不影響)
查看SciPy版本,最好安裝1.0.1,以免不匹配(沒有不影響)
如何安裝?以Numpy爲例,打開conda,輸入

pip uninstall numpy=1.15.1

下載訓練集:
ne1x-photo-repair-0.3.pkl.bz2 (https://github.com/alexjc/neural-enhance/releases/download/v0.3/ne1x-photo-repair-0.3.pkl.bz2
ne2x-photo-default-0.3.pkl.bz2 (https://github.com/alexjc/neural-enhance/releases/download/v0.3/ne2x-photo-default-0.3.pkl.bz2
ne4x-photo-default-0.3.pkl.bz2 (https://github.com/alexjc/neural-enhance/releases/download/v0.3/ne4x-photo-default-0.3.pkl.bz2
vgg19_conv.pkl.bz2 (https://github.com/alexjc/neural-enhance/releases/download/v0.0/vgg19_conv.pkl.bz2

3、可能會遇到的問題:(若沒遇到可自動忽略)

1. cpu不支持

· theano.gof.opt.LocalMetaOptimizerSkipAssertionError: AbstractConv2d Theano optimization failed: there is no implementation available supporting the requested options. Did you exclude both “conv_dnn” and “conv_gemm” from the optimizer? If on GPU, is cuDNN available and does the GPU support it? If on CPU, do you have a BLAS library installed Theano can link against? On the CPU we do not support float16.

–意思是你的theano沒有裝到位,需要修改配置和環境變量
解決方法:
安裝Theano和Lasagne
打開conda,分別執行這兩條命名:

pip install -r https://raw.githubusercontent.com/Lasagne/Lasagne/master/requirements.txt
pip install https://github.com/Lasagne/Lasagne/archive/master.zip

此時,基本框架已經安裝完成。官方給出了測試的代碼,如果使用CPU,會打印出“Used the cpu”,如果使用GPU,會打印出“Used the gpu”

#!/user/bin/env python
# _*_coding:utf-8 _*_

from theano import function, config, shared, tensor
import numpy
import time

vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000

rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], tensor.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in range(iters):
    r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, tensor.Elemwise) and
              ('Gpu' not in type(x.op).__name__)
              for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')

接下來就是在C:\Users\“你的用戶名”目錄下新建一個名爲.theanorc的.txt文件,我裏面的內容爲

[cxx]
flags=C:\Users\“你的用戶名”\Anaconda3\envs\theano\MinGW\bin
[global]
floatX = float32
device = cpu
optimizer = None
[blas]
ldflags = -lopenblas

以上可是cpu正常運行

2. 有些庫過時(以scipy.ndimage.imread爲例)

back.jpg Traceback (most recent call last):
File “enhance.py”, line 583, in
img = scipy.ndimage.imread(filename, mode=‘RGB’)
AttributeError: module ‘scipy.ndimage’ has no attribute ‘imread’

源代碼是2017年開源的,故有些庫的安裝需要倒退,採用較老的庫,或者安裝最新庫,然後使用新庫相同功能的函數
scipy.ndimage.imread(*args, **kwds)
imread is deprecated! imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imread instead.
在1.2.0版本用imageio.imread代替
Read an image from a file as an array.從文件中把圖片讀成數組

解決方法:

pip install imageio

接着調用imageio庫替換scipy.ndimage.imread

import imageio
fname = "images/" + my_image
image = np.array(imageio.imread(fname))

這是最後出來的結果:
ne1x===
ne1x表示增強庫超分辨率跑出來的結果,筆者覺得一般,沒提高多少像素
ne4x表示放大四倍+超分辨率跑出來的結果,筆者覺得這個效果比1x的要好的多

自此,基本的問題就解決了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章