2020人工神经网络第一次作业-参考答案第八部分

本文是 2020人工神经网络第一次作业 的参考答案第八部分

 

08 第八题参考答案


1.题目分析

(1) 数据下载

https://www.cosy.sbg.ac.at/~pmeerw/Watermarking/lena.html可以下载到 lena512灰度图 。图片的尺寸为512×512。

▲ Lena-Gray BMP图片

▲ Lena-Gray BMP图片

(2) 图片分割与合成

利用神经网络对图片进行压缩,需要将图片分割成相同大小的小块,边长为 n b n_b nb。然后每一图像小块构成一个长度为 n b 2 n_b^2 nb2的一维向量,作为网络的输入。在 Lena图像分解成小块与从小块合成 中给出了图片分割相应的Python程序。

▲ 分割成32×32小块的Lena灰度图片

▲ 分割成32×32小块的Lena灰度图片

2.构建网络

用于图片压缩的网络由单隐层网络组成。输入层和输出层的节点个数相同,等于图像块的数量长度。隐层的节点数量小于输入层节点个数。

隐层节点的传递函数采用双曲正切: tanh ⁡ ( x ) = 1 − e − x 1 + e + x \tanh \left( x \right) = {{1 - e^{ - x} } \over {1 + e^{ + x} }} tanh(x)=1+e+x1ex

输出层的神经元传递函数采用线性传递函数。

▲ 神经网络

▲ 神经网络

在实验中,图像块的边长取8,图像块数据长度为64。因此神经网络的输入、输出节点的个数为64。

上述网络Python实现程序参见后面附录中的:作业1-8中的程序

3.网络训练

下面对于隐层节点的个数分别取128、64、16、8、2进行训练,并进行恢复。

(1) 隐层节点:128

下图显示在隐层节点为128时,随着训练步骤增加,图像恢复的效果。

▲ 随着网络训练图片恢复情况

▲ 隐层节点128

▲ 网络训练MSE变化情况

▲ 网络训练MSE变化情况

隐层节点个数分别取64,16,8,2的训练和图片恢复效果如下图所示:

▲ 隐层节点:64

▲ 隐层节点:64

▲ 隐层节点:16

▲ 隐层节点:16

▲ 隐层节点:8

▲ 隐层节点:8

▲ 隐层节点:2

▲ 隐层节点:2

4.不同隐层节点对应精度

下面给出了不同隐层节点网络下,在训练5000次之后图像恢复误差。

  • 学习率: η = 0.1 \eta = 0.1 η=0.1
  • 训练循环次数: N = 5000 N = 5000 N=5000
隐层节点压缩倍数MSE
641.00.05665
322.00.08341
164.00.0802
88.00.1152
4160.1167

5.不同图像块大小

下面给出了图像块边长分别是4和16情况下图像压缩的情况。

(1) 边长:4

  • 隐层节点个数:8
  • 学习速率:0.1
  • 训练循环:5000
    ▲ 图像块边长:4
    ▲ 图像块边长:4

(2) 边长:16

  • 隐层节点个数:16
  • 学习速率:0.05
  • 训练循环:5000

▲ 图像块边长:16

▲ 图像块边长:16

 

02 压缩不同图像


使用Lena图片训练网络。让后使用该网络压缩另外的图片。

1.Lena图片训练网络

  • 图片边长:8
  • 中间隐层节点:16
  • 学习速率:0.1
  • 训练循环:5000

▲ 压缩Lena图像

▲ 压缩Lena图像

2.压缩别的图片

使用前面使用Lena训练的网络和参数,对于下面图片的灰度部分进行压缩和回复。

▲ 压缩新的图像

▲ 压缩新的图像


 

※ 作业1-8中的程序


1.单隐层BP网络主程序

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# HW18BP.PY                    -- by Dr. ZhuoQing 2020-11-19
#
# Note:
#============================================================

from headm import *
from bp1tanh                import *
import hw18sub
from PIL                    import Image

BLOCK_SIZE          = 4

x_train = hw18sub.lena2block(BLOCK_SIZE, hw18sub.lena_gray)
y_train = x_train.T
temp_file = r'd:\temp\1.bmp'
imageid = 13

#------------------------------------------------------------
'''
new_file = r'd:\temp\love.bmp'
x_train = hw18sub.lena2block(BLOCK_SIZE, new_file)

parameters = tspload('bp8_16', 'parameters').tolist()

A2, cache = forward_propagate(x_train, parameters)
imagedata = hw18sub.block2lena(A2.T)
newimage = Image.fromarray(imagedata)
newimage.save(temp_file)
tspshowimage(imageid, 0, 0, 512, 512, temp_file)
tsprv()
exit()
'''
#------------------------------------------------------------
# Define the training
DISP_STEP           = 100

#------------------------------------------------------------
pltgif = PlotGIF()
textid = 14

#------------------------------------------------------------
tsprefreshimagebuffer(imageid)

def lena2bmp(parameters):
    A2, cache = forward_propagate(x_train, parameters)
    imagedata = hw18sub.block2lena(A2.T)
    newimage = Image.fromarray(imagedata)
    newimage.save(temp_file)
    tspshowimage(imageid, 0, 0, 512, 512, temp_file)
    tsprv()

    pltgif.appendrange(imageid)

#------------------------------------------------------------
def train(X, Y, num_iterations, learning_rate, print_cost=False, Hn=10):
    n_x = X.shape[1]
    n_y = n_x
    n_h = Hn

    lr = learning_rate

    parameters = initialize_parameters(n_x, n_h, n_y)
    XX,YY = x_train, y_train #shuffledata(x_train, y_train)

    costdim = []

    for i in range(0, num_iterations):
        A2, cache = forward_propagate(XX, parameters)
        cost = calculate_cost(A2, YY, parameters)
        grads = backward_propagate(parameters, cache, XX, YY)
        parameters = update_parameters(parameters, grads, lr)

        if print_cost and i % DISP_STEP == 0:
            printf('Cost after iteration:%i: %f'%(i, cost))
            costdim.append(cost)

            textstr = 'Step:%d,MSE=%4.3f'%(i, cost)
            tspsettext(textid, textstr)
            lena2bmp(parameters)

    return parameters, costdim

#------------------------------------------------------------
parameters, costdim = train(x_train, y_train, 5000, 0.1, True, 8)

tspsave('bp8_16',parameters=parameters)

lena2bmp(parameters)

pltgif.save(r'd:\temp\1.gif')

stepdim = array(range(len(costdim))) * DISP_STEP

plt.plot(stepdim, costdim)
plt.xlabel("Step")
plt.ylabel("Error")
plt.grid(True)
plt.tight_layout()
plt.show()

#------------------------------------------------------------
#        END OF FILE : HW18BP.PY
#============================================================

2.对于Lena灰度图像进行分块和合并的子程序

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# HW18SUB.PY                  -- by Dr. ZhuoQing 2020-11-23
#
# Note:
#============================================================

from headm import *
from PIL                    import Image

#------------------------------------------------------------
lena_gray = r'D:\Temp\lena_gray.bmp'

#------------------------------------------------------------
def lena2block(blocksize,image_file):
    img = Image.open(image_file).convert('RGB')

    imgdata = array(img)[:,:,0].astype(float32)
    imgheight = imgdata.shape[0]
    imgwidth = imgdata.shape[1]
    imgdata = (imgdata - 128) / 256

    printf(imgdata.shape)

    outdata = []
    for i in range(0, imgheight, blocksize):
        for j in range(0, imgwidth, blocksize):
            blockdata = imgdata[i:i+blocksize, j:j+blocksize].reshape(1, -1)

            if len(outdata) == 0: outdata = blockdata
            else: outdata = vstack((outdata, blockdata))

    return outdata

#------------------------------------------------------------
def block2lena(blockdata):
    blocknum = blockdata.shape[0]
    blocklen = blockdata.shape[1]
    block_size = int(sqrt(blocklen))
    image_block_size = int(sqrt(blocknum))

    block_line = []
    for i in range(image_block_size):
        block_row = hstack([b.reshape(block_size, block_size) \
                            for b in blockdata[i*image_block_size:(i+1)*image_block_size]])
        block_line.append(block_row)

    imagedata = vstack(block_line)
    imagedata = (imagedata * 256 + 128)
    imagedata[imagedata < 0] = 0
    imagedata[imagedata > 255] = 255
    return imagedata.astype(uint8)

#------------------------------------------------------------
#        END OF FILE : HW18SUB.PY
#============================================================

3.BP网络子程序

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# BP1TANH.PY                    -- by Dr. ZhuoQing 2020-11-17
#
# Note:
#============================================================

from headm import *

#------------------------------------------------------------
# Samples data construction

random.seed(int(time.time()))

#------------------------------------------------------------
def shuffledata(X, Y):
    id = list(range(X.shape[0]))
    random.shuffle(id)
    return X[id], (Y.T[id]).T

#------------------------------------------------------------
# Define and initialization NN
def initialize_parameters(n_x, n_h, n_y):
    W1 = random.randn(n_h, n_x) * 0.5          # dot(W1,X.T)
    W2 = random.randn(n_y, n_h) * 0.5          # dot(W2,Z1)
    b1 = zeros((n_h, 1))                       # Column vector
    b2 = zeros((n_y, 1))                       # Column vector

    parameters = {'W1':W1,
                  'b1':b1,
                  'W2':W2,
                  'b2':b2}

    return parameters

#------------------------------------------------------------
# Forward propagattion
# X:row->sample;
# Z2:col->sample
def forward_propagate(X, parameters):
    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']

    Z1 = dot(W1, X.T) + b1                    # X:row-->sample; Z1:col-->sample
    A1 = (1-exp(-Z1))/(1+exp(-Z1))

    Z2 = dot(W2, A1) + b2                     # Z2:col-->sample
    A2 = Z2                                   # Linear output

    cache = {'Z1':Z1,
             'A1':A1,
             'Z2':Z2,
             'A2':A2}
    return Z2, cache

#------------------------------------------------------------
# Calculate the cost
# A2,Y: col->sample
def calculate_cost(A2, Y, parameters):
    err = [x1-x2 for x1,x2 in zip(A2.T, Y.T)]
    cost = [dot(e,e) for e in err]
    return mean(cost)

#------------------------------------------------------------
# Backward propagattion
def backward_propagate(parameters, cache, X, Y):
    m = X.shape[0]                  # Number of the samples

    W1 = parameters['W1']
    W2 = parameters['W2']
    A1 = cache['A1']
    A2 = cache['A2']

    dZ2 = (A2 - Y)
    dW2 = dot(dZ2, A1.T) / m
    db2 = sum(dZ2, axis=1, keepdims=True) / m

    dZ1 = dot(W2.T, dZ2) * (1-pow(A1,2))
    dW1 = dot(dZ1, X) / m
    db1 = sum(dZ1, axis=1, keepdims=True) / m

    grads = {'dW1':dW1,
             'db1':db1,
             'dW2':dW2,
             'db2':db2}

    return grads

#------------------------------------------------------------
# Update the parameters
def update_parameters(parameters, grads, learning_rate):
    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']

    dW1 = grads['dW1']
    db1 = grads['db1']
    dW2 = grads['dW2']
    db2 = grads['db2']

    W1 = W1 - learning_rate * dW1
    W2 = W2 - learning_rate * dW2
    b1 = b1 - learning_rate * db1
    b2 = b2 - learning_rate * db2

    parameters = {'W1':W1,
                  'b1':b1,
                  'W2':W2,
                  'b2':b2}

    return parameters

#------------------------------------------------------------
#        END OF FILE : BP1TANH.PY
#============================================================
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页