清明上河圖代碼,我們?nèi)绾瓮ㄟ^
420
2023-10-25
清明上河圖寬24.8厘米、長(zhǎng)528.7厘米 ,絹本設(shè)色 。作品以長(zhǎng)卷 形式,采用散點(diǎn)透視 構(gòu)圖法,生動(dòng)記錄了中國(guó)十二世紀(jì)北宋 都城東京(又稱汴京 ,今河南開封 )的城市面貌和當(dāng)時(shí)社會(huì)各階層人民的生活狀況,是北宋時(shí)期都城汴京當(dāng)年繁榮的見證,也是北宋城市經(jīng)濟(jì)情況的寫照。
這在中國(guó)乃至世界繪畫史上都是獨(dú)一無二的。在五米多長(zhǎng)的畫卷里,共繪了數(shù)量龐大的各色人物,牛、騾、驢等牲畜,車、轎、大小船只,房屋、橋梁、城樓 等各有特色,體現(xiàn)了宋代建筑的特征。具有很高的歷史價(jià)值和藝術(shù)價(jià)值。《清明上河圖》雖然場(chǎng)面熱鬧,但表現(xiàn)的并非繁榮市景,而是一幅帶有憂患意識(shí)的"盛世危圖",官兵懶散稅務(wù)重。
而我們今天的項(xiàng)目就是通過對(duì)算法的改造,實(shí)現(xiàn)屬于自己的清明上河圖。
下面我們將利用vgg19模型訓(xùn)練畫作,詳細(xì)步驟如下,并且我在每個(gè)代碼上面都注釋了方便查看:
首先我們導(dǎo)入先關(guān)的庫(kù):
import tensorflow as tf
import numpy as np
import scipy.io
import scipy.misc
import os
import time
接著定義一些變量方便調(diào)用:CONTENT_IMG = '1.png'
STYLE_IMG = 'sty.jpg'
OUTPUT_DIR = 'neural_style_transfer_tensorflow/'
再創(chuàng)建一個(gè)目錄用來保存圖片:
if not os.path.exists(OUTPUT_DIR):
os.mkdir(OUTPUT_DIR)
定義生成圖像的長(zhǎng)寬通道等信息:
IMAGE_W = 400
IMAGE_H = 300
COLOR_C = 3
NOISE_RATIO = 0.7
BETA = 5
ALPHA = 100
再接著定義模型路徑
VGG_MODEL = 'imagenet-vgg-verydeep-19.mat'
生成一個(gè)參數(shù)矩陣,作為圖像的處理過程之一,對(duì)像素值運(yùn)算:
MEAN_VALUES = np.array([123.68, 116.779, 103.939]).reshape((1, 1, 1, 3))
再 接著定義讀取模型函數(shù),下面我都有所注解:
def load_vgg_model(path):
'''
Details of the VGG19 model:
- 0 is conv1_1 (3, 3, 3, 64)
- 1 is relu
- 2 is conv1_2 (3, 3, 64, 64)
- 3 is relu
- 4 is maxpool
- 5 is conv2_1 (3, 3, 64, 128)
- 6 is relu
- 7 is conv2_2 (3, 3, 128, 128)
- 8 is relu
- 9 is maxpool
- 10 is conv3_1 (3, 3, 128, 256)
- 11 is relu
- 12 is conv3_2 (3, 3, 256, 256)
- 13 is relu
- 14 is conv3_3 (3, 3, 256, 256)
- 15 is relu
- 16 is conv3_4 (3, 3, 256, 256)
- 17 is relu
- 18 is maxpool
- 19 is conv4_1 (3, 3, 256, 512)
- 20 is relu
- 21 is conv4_2 (3, 3, 512, 512)
- 22 is relu
- 23 is conv4_3 (3, 3, 512, 512)
- 24 is relu
- 25 is conv4_4 (3, 3, 512, 512)
- 26 is relu
- 27 is maxpool
- 28 is conv5_1 (3, 3, 512, 512)
- 29 is relu
- 30 is conv5_2 (3, 3, 512, 512)
- 31 is relu
- 32 is conv5_3 (3, 3, 512, 512)
- 33 is relu
- 34 is conv5_4 (3, 3, 512, 512)
- 35 is relu
- 36 is maxpool
- 37 is fullyconnected (7, 7, 512, 4096)
- 38 is relu
- 39 is fullyconnected (1, 1, 4096, 4096)
- 40 is relu
- 41 is fullyconnected (1, 1, 4096, 1000)
- 42 is softmax
'''
vgg = scipy.io.loadmat(path)
vgg_layers = vgg['layers']
#加載vgg模型獲取模型各層參數(shù)和名稱
def _weights(layer, expected_layer_name):
W = vgg_layers[0][layer][0][0][2][0][0]
b = vgg_layers[0][layer][0][0][2][0][1]
layer_name = vgg_layers[0][layer][0][0][0][0]
assert layer_name == expected_layer_name
return W, b
#將加載的變量初始化成tf可運(yùn)算的張量類型,函數(shù)返回值為激活函數(shù)的輸出
def _conv2d_relu(prev_layer, layer, layer_name):
W, b = _weights(layer, layer_name)
W = tf.constant(W)
b = tf.constant(np.reshape(b, (b.size)))
return tf.nn.relu(tf.nn.conv2d(prev_layer, filter=W, strides=[1, 1, 1, 1], padding='SAME') + b)
#定義池化層函數(shù)
def _avgpool(prev_layer):
return tf.nn.avg_pool(prev_layer, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#將各層輸出值都放到列表中方便加載,形成字典
graph = {}
graph['input'] = tf.Variable(np.zeros((1, IMAGE_H, IMAGE_W, COLOR_C)), dtype='float32')
#定義['conv1_1']為vgg模型的第0層,輸入層為上一層的['input' ]
graph['conv1_1'] = _conv2d_relu(graph['input'], 0, 'conv1_1')
graph['conv1_2'] = _conv2d_relu(graph['conv1_1'], 2, 'conv1_2')
graph['avgpool1'] = _avgpool(graph['conv1_2'])
graph['conv2_1'] = _conv2d_relu(graph['avgpool1'], 5, 'conv2_1')
graph['conv2_2'] = _conv2d_relu(graph['conv2_1'], 7, 'conv2_2')
graph['avgpool2'] = _avgpool(graph['conv2_2'])
graph['conv3_1'] = _conv2d_relu(graph['avgpool2'], 10, 'conv3_1')
graph['conv3_2'] = _conv2d_relu(graph['conv3_1'], 12, 'conv3_2')
graph['conv3_3'] = _conv2d_relu(graph['conv3_2'], 14, 'conv3_3')
graph['conv3_4'] = _conv2d_relu(graph['conv3_3'], 16, 'conv3_4')
graph['avgpool3'] = _avgpool(graph['conv3_4'])
graph['conv4_1'] = _conv2d_relu(graph['avgpool3'], 19, 'conv4_1')
graph['conv4_2'] = _conv2d_relu(graph['conv4_1'], 21, 'conv4_2')
graph['conv4_3'] = _conv2d_relu(graph['conv4_2'], 23, 'conv4_3')
graph['conv4_4'] = _conv2d_relu(graph['conv4_3'], 25, 'conv4_4')
graph['avgpool4'] = _avgpool(graph['conv4_4'])
graph['conv5_1'] = _conv2d_relu(graph['avgpool4'], 28, 'conv5_1')
graph['conv5_2'] = _conv2d_relu(graph['conv5_1'], 30, 'conv5_2')
graph['conv5_3'] = _conv2d_relu(graph['conv5_2'], 32, 'conv5_3')
graph['conv5_4'] = _conv2d_relu(graph['conv5_3'], 34, 'conv5_4')
graph['avgpool5'] = _avgpool(graph['conv5_4'])
return graph
為了實(shí)現(xiàn)自己的項(xiàng)目效果,設(shè)定損失函數(shù):
#定義內(nèi)容損失函數(shù),變量為tf計(jì)算圖和vgg模型參數(shù),返回值為損失值
def content_loss_func(sess, model):
#p就是model['conv4_2'])參數(shù),x是model['conv4_2'])
def _content_loss(p, x):
#p的值為Tensor("Relu_9:0", shape=(1, 75, 100, 512), dtype=float32),故N為512,M為75*100,分別為卷積核個(gè)數(shù),卷積核大小的寬*100
N = p.shape[3]
M = p.shape[1] * p.shape[2]
return (1 / (4 * N * M)) * tf.reduce_sum(tf.pow(x - p, 2))
return _content_loss(sess.run(model['conv4_2']), model['conv4_2'])
STYLE_LAYERS = [('conv1_1', 0.5), ('conv2_1', 1.0), ('conv3_1', 1.5), ('conv4_1', 3.0), ('conv5_1', 4.0)]
#返回值為_style_loss的值*0.5,1,1.5,4的加和
def style_loss_func(sess, model):
def _gram_matrix(F, N, M):
Ft = tf.reshape(F, (M, N))
return tf.matmul(tf.transpose(Ft), Ft)
#a,x都為'conv1_1', conv2_1', 'conv3_1', 'conv4_1','conv5_1'中的參數(shù)遍歷
def _style_loss(a, x):
#同內(nèi)容損失函數(shù)
N = a.shape[3]
M = a.shape[1] * a.shape[2]
A = _gram_matrix(a, N, M)
G = _gram_matrix(x, N, M)
return (1 / (4 * N ** 2 * M ** 2)) * tf.reduce_sum(tf.pow(G - A, 2))
return sum([_style_loss(sess.run(model[layer_name]), model[layer_name]) * w for layer_name, w in STYLE_LAYERS])
再定義生成圖片,讀取圖片,保存圖片函數(shù):
#產(chǎn)生噪聲圖片
def generate_noise_image(content_image, noise_ratio=NOISE_RATIO):
#隨機(jī)產(chǎn)生矩陣圖片,矩陣元素內(nèi)容符合標(biāo)準(zhǔn)正太分布
noise_image = np.random.uniform(-20, 20, (1, IMAGE_H, IMAGE_W, COLOR_C)).astype('float32')
#將產(chǎn)生的矩陣內(nèi)各元素與神經(jīng)網(wǎng)絡(luò)加和
input_image = noise_image * noise_ratio + content_image * (1 - noise_ratio)
return input_image
#讀取圖片,改變尺寸,變成1行多列矩陣,將矩陣與初始值相減返回
def load_image(path):
image = scipy.misc.imread(path)
image = scipy.misc.imresize(image, (IMAGE_H, IMAGE_W))
#image.shape為[800,600,3],則(1, ) + image.shape)為[1,800,600,3]
image = np.reshape(image, ((1, ) + image.shape))
#MEAN_VALUES = np.array([123.68, 116.779, 103.939]).reshape((1, 1, 1, 3))
#其中image為三通道矩陣,MEAN_VALUES為三維矩陣可以相減
image = image - MEAN_VALUES
return image
#保存圖片
def save_image(path, image):
image = image + MEAN_VALUES
#參見上面圖像加載時(shí)多加了1維,故形成時(shí)要減少維度,
image = image[0]
#截取所有數(shù)值在0-255之間的,因?yàn)橄袼刂当仨毷沁@個(gè)范圍。而參數(shù)運(yùn)算后可能會(huì)超過這個(gè)值
image = np.clip(image, 0, 255).astype('uint8')
#保存
scipy.misc.imsave(path, image)
下面是訓(xùn)練加載:
#啟動(dòng)計(jì)算圖
with tf.Session as sess:
#讀取圖片,返回值為減去MEAN_VALUES的矩陣,矩陣形狀為[1,800,600,3]
content_image = load_image(CONTENT_IMG)
style_image = load_image(STYLE_IMG)
#加載vgg19模型,返回值為一個(gè)字典,里面為各網(wǎng)絡(luò)層參數(shù),輸入和輸出
model = load_vgg_model(VGG_MODEL)
#產(chǎn)生噪聲圖片,返回值為隨機(jī)矩陣加上網(wǎng)絡(luò)層參數(shù)的新矩陣
input_image = generate_noise_image(content_image)
#變量初始化
sess.run(tf.global_variables_initializer)
#從網(wǎng)絡(luò)層input層開始運(yùn)算內(nèi)容圖片矩陣
sess.run(model['input'].assign(content_image))
content_loss = content_loss_func(sess, model)
# 從網(wǎng)絡(luò)層input層開始運(yùn)算內(nèi)容圖片矩陣
sess.run(model['input'].assign(style_image))
style_loss = style_loss_func(sess, model)
#總損失為內(nèi)容損失加上風(fēng)格損失
total_loss = BETA * content_loss + ALPHA * style_loss
#建立優(yōu)化器以調(diào)整參數(shù)
optimizer = tf.train.AdamOptimizer(2.0)
#優(yōu)化器調(diào)整參數(shù),使得損失為最小
train = optimizer.minimize(total_loss)
sess.run(tf.global_variables_initializer)
# 從網(wǎng)絡(luò)層input層開始運(yùn)算形成新的圖片
sess.run(model['input'].assign(input_image))
ITERATIONS = 2000
#訓(xùn)練2000輪
for i in range(ITERATIONS):
sess.run(train)
print('Iteration %d' % i)
print('Cost: ', sess.run(total_loss))
if i % 100 == 0:
#每一百次加載一次網(wǎng)絡(luò)參數(shù)以保存圖片
output_image = sess.run(model['input'])
print('Iteration %d' % i)
print('Cost: ', sess.run(total_loss))
save_image(os.path.join(OUTPUT_DIR, 'output_%d.jpg' % i), output_image)
最終得到的效果如圖所示:
左邊是電視里找的圖片,右邊是模擬的圖片,由此可見生成的效果還是可以的。而這個(gè)程序的主要思路就是在一個(gè)生成隨機(jī)矩陣的基礎(chǔ)上,通過加載網(wǎng)絡(luò)層訓(xùn)練參數(shù),然后生成的矩陣值按比例乘以網(wǎng)絡(luò)參數(shù),然后把矩陣保存為圖片即可達(dá)到模擬生成的效果。而其中參數(shù)的調(diào)整是基于深層次網(wǎng)絡(luò)提取的圖像特征按公式運(yùn)算,通過優(yōu)化器優(yōu)化參數(shù),通過訓(xùn)練次數(shù)的增加,參數(shù)也在逐漸改善,最終形成自己需要的圖片效果。
【來源:CSDN】
版權(quán)歸原作者所有,向原創(chuàng)致敬
發(fā)表評(píng)論
暫時(shí)沒有評(píng)論,來搶沙發(fā)吧~