2020人工神经网络第一次作业-参考答案第八部分
本文是 2020人工神经网络第一次作业 的参考答案第八部分
➤08 第八题参考答案
1.题目分析
(1) 数据下载
从https://www.cosy.sbg.ac.at/~pmeerw/Watermarking/lena.html可以下载到 lena512灰度图 。图片的尺寸为512×512。
▲ Lena-Gray BMP图片
(2) 图片分割与合成
利用神经网络对图片进行压缩,需要将图片分割成相同大小的小块,边长为 n b n_b nb。然后每一图像小块构成一个长度为 n b 2 n_b^2 nb2的一维向量,作为网络的输入。在 Lena图像分解成小块与从小块合成 中给出了图片分割相应的Python程序。
▲ 分割成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+x1−e−x
输出层的神经元传递函数采用线性传递函数。
▲ 神经网络
在实验中,图像块的边长取8,图像块数据长度为64。因此神经网络的输入、输出节点的个数为64。
上述网络Python实现程序参见后面附录中的:作业1-8中的程序
3.网络训练
下面对于隐层节点的个数分别取128、64、16、8、2进行训练,并进行恢复。
(1) 隐层节点:128
下图显示在隐层节点为128时,随着训练步骤增加,图像恢复的效果。
▲ 隐层节点128
▲ 网络训练MSE变化情况
隐层节点个数分别取64,16,8,2的训练和图片恢复效果如下图所示:
▲ 隐层节点:64
▲ 隐层节点:16
▲ 隐层节点:8
▲ 隐层节点:2
4.不同隐层节点对应精度
下面给出了不同隐层节点网络下,在训练5000次之后图像恢复误差。
- 学习率: η = 0.1 \eta = 0.1 η=0.1
- 训练循环次数: N = 5000 N = 5000 N=5000
隐层节点 | 压缩倍数 | MSE |
---|---|---|
64 | 1.0 | 0.05665 |
32 | 2.0 | 0.08341 |
16 | 4.0 | 0.0802 |
8 | 8.0 | 0.1152 |
4 | 16 | 0.1167 |
5.不同图像块大小
下面给出了图像块边长分别是4和16情况下图像压缩的情况。
(1) 边长:4
- 隐层节点个数:8
- 学习速率:0.1
- 训练循环:5000
▲ 图像块边长:4
(2) 边长:16
- 隐层节点个数:16
- 学习速率:0.05
- 训练循环:5000
▲ 图像块边长:16
➤02 压缩不同图像
使用Lena图片训练网络。让后使用该网络压缩另外的图片。
1.Lena图片训练网络
- 图片边长:8
- 中间隐层节点:16
- 学习速率:0.1
- 训练循环:5000
▲ 压缩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
#============================================================
- 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
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
#============================================================
- 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
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
#============================================================
- 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
文章来源: zhuoqing.blog.csdn.net,作者:卓晴,版权归原作者所有,如需转载,请联系作者。
原文链接:zhuoqing.blog.csdn.net/article/details/109830492
- 点赞
- 收藏
- 关注作者
评论(0)