Verified Commit fd2724c9 authored by Kiryuu Sakuya's avatar Kiryuu Sakuya 🎵
Browse files

Add exam.5

parent 0d607f02
import numpy as np
def VGGPreprocessing(originImgMatrix):
\"The only preprocessing we do is subtracting the mean RGB value, \\
computed on the training set, from each pixel.\\
原论文中对输入的RGB矩阵做了一个减去均值的预处理,该函数实现这个预处理\"
if type(originImgMatrix) is not np.ndarray:
originImgMatrix = np.ndarray(originImgMatrix)
# 矩阵X*Y*3
# axis=0,代表第一维,即把X(行)消除了,所以返回的是每一列RGB的均值,形状是(Y*3)
# axis=1, 代表第二维,即把Y(列)消除了,所以返回的是全图的RGB的均值,形状是(3,)
originImgMatrix_RGBMean = np.mean(originImgMatrix, axis=(0, 1))
# 直接减就行
subtract_Img = originImgMatrix - originImgMatrix_RGBMean
return subtract_Img
def VGGPreprocessingBatch(batch_originImgMatrix):
for index, img in enumerate(batch_originImgMatrix):
batch_originImgMatrix[index] = VGGPreprocessing(img)
return batch_originImgMatrix
import numpy as np
def VGGPreprocessingBatch(batch_originImgMatrix):
'''
你需要对batch中的每一个img的数据作如下预处理:
各个像素点上rgb三个通道上的值,均减去该图片上三个通道分别的均值
例如整张img r通道均值为2, g通道均值为1, b通道均值为3
某像素点为[5,1,0], 则处理后,该像素点为[3,0,-3]
:param batch_originImgMatrix: 一个数组或者是一个numpy.ndarray,shape是(batchSize,imgSize,imgSize,3)
:return: 返回处理正确后的数据,shape不变,返回类型为numpy.ndarray
'''
#********** Begin **********#
#********** End **********#
import numpy as np
from VGGPreprocessForUsers import VGGPreprocessingBatch as userV
if __name__ == '__main__':
# 预期输出
'''
[[[ -1 0 0]
[ 1 2 3]
[ -1 -1 -1]]
[[ 0 1 2]
[ 3 4 5]
[ -5 -5 -5]]
[[ 1 2 3]
[ 4 5 6]
[-11 -8 -5]]]'''
# ----------------------example---------------------------
a = np.array([[[1, 2, 3], [4, 5, 6], [1, 1, 1]],
[[7, 8, 9], [10, 11, 12], [2, 2, 2]],
[[13, 14, 15], [16, 17, 18], [0, 3, 6]]])
print('---原始矩阵---')
print(a)
print('---处理后矩阵---')
c = userV(a)
print(c,end='')
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment