資源簡(jiǎn)介
這是基于VGG19網(wǎng)絡(luò)的一個(gè)圖像風(fēng)格轉(zhuǎn)換,需要下載VGG19的權(quán)重文件,拷入到工程目錄下即可,代碼直接運(yùn)行就能跑。

代碼片段和文件信息
import?tensorflow?as?tf
import?numpy?as?np
import?os
import?cv2
IMAGE_W?=?600
IMAGE_H?=?600
Ratio?=?None
INI_NOISE_RATIO?=?0.7
style_STRENGTH?=?500
ITERATION?=?2000
CONTENT_layerS?=?[(‘conv4_2‘?1.)]
style_layerS?=?[(‘conv1_1‘?2.)?(‘conv2_1‘?1.)?(‘conv3_1‘?0.5)?(‘conv4_1‘?0.25)?(‘conv5_1‘?0.125)]
layers?=?[‘conv1_1‘?‘conv1_2‘?‘conv2_1‘?‘conv2_2‘?‘conv3_1‘?‘conv3_2‘?‘conv3_3‘?‘conv3_4‘
??????????‘conv4_1‘?‘conv4_2‘?‘conv4_3‘?‘conv4_4‘?‘conv5_1‘?‘conv5_2‘?‘conv5_3‘?‘conv5_4‘?]
def?vgg19(input?model_path=None):
????‘‘‘
????Build?the?VGG19?network?which?is?initialized?with?the?pre-trained?VGG19?model.
????:param?input:?The?input?image.
????:param?model_path:Which?path?the?VGG19?model?is?stored.
????:return:A?python?dict?which?contains?all?the?layers?needed.
????‘‘‘
????if?model_path?is?None:
????????model_path?=?‘vgg19.npy‘
????if?os.path.isfile(model_path)?is?False:
????????raise?FileNotFoundError(‘vgg19.npy?cannot?be?found!!!‘)
????wDict?=?np.load(model_path?encoding=“bytes“).item()
????net?=?{}
????net[‘input‘]?=?input
????#?conv1_1
????weight1_1?=?tf.Variable(initial_value=wDict[‘conv1_1‘][0]?trainable=False)
????bias1_1?=?tf.Variable(wDict[‘conv1_1‘][1]?trainable=False)
????net[‘conv1_1‘]?=?tf.nn.relu(tf.nn.conv2d(net[‘input‘]?weight1_1?[1?1?1?1]?‘SAME‘)?+?bias1_1)
????#?conv1_2
????weight1_2?=?tf.Variable(wDict[‘conv1_2‘][0]?trainable=False)
????bias1_2?=?tf.Variable(wDict[‘conv1_2‘][1]?trainable=False)
????net[‘conv1_2‘]?=?tf.nn.relu(tf.nn.conv2d(net[‘conv1_1‘]?weight1_2?[1?1?1?1]?‘SAME‘)?+?bias1_2)
????#?pool1
????net[‘pool1‘]?=?tf.nn.avg_pool(net[‘conv1_2‘]?[1?2?2?1]?[1?2?2?1]?‘SAME‘)
????#?conv2_1
????weight2_1?=?tf.Variable(wDict[‘conv2_1‘][0]?trainable=False)
????bias2_1?=?tf.Variable(wDict[‘conv2_2‘][1]?trainable=False)
????net[‘conv2_1‘]?=?tf.nn.relu(tf.nn.conv2d(net[‘pool1‘]?weight2_1?[1?1?1?1]?‘SAME‘)?+?bias2_1)
????#?conv2_2
????weight2_2?=?tf.Variable(wDict[‘conv2_2‘][0]?trainable=False)
????bias2_2?=?tf.Variable(wDict[‘conv2_2‘][1]?trainable=False)
????net[‘conv2_2‘]?=?tf.nn.relu(tf.nn.conv2d(net[‘conv2_1‘]?weight2_2?[1?1?1?1]?‘SAME‘)?+?bias2_2)
????#?pool2
????net[‘pool2‘]?=?tf.nn.avg_pool(net[‘conv2_2‘]?[1?2?2?1]?[1?2?2?1]?‘SAME‘)
????#?conv3_1
????weight3_1?=?tf.Variable(wDict[‘conv3_1‘][0]?trainable=False)
????bias3_1?=?tf.Variable(wDict[‘conv3_1‘][1]?trainable=False)
????net[‘conv3_1‘]?=?tf.nn.relu(tf.nn.conv2d(net[‘pool2‘]?weight3_1?[1?1?1?1]?‘SAME‘)?+?bias3_1)
????#?conv3_2
????weight3_2?=?tf.Variable(wDict[‘conv3_2‘][0]?trainable=False)
????bias3_2?=?tf.Variable(wDict[‘conv3_2‘][1]?trainable=False)
????net[‘conv3_2‘]?=?tf.nn.relu(tf.nn.conv2d(net[‘conv3_1‘]?weight3_2?[1?1?1?1]?‘SAME‘)?+?bias3_2)
????#?conv3_3
????weight3_3?=?tf.Variable(wDict[‘conv3_3‘][0]?trainable=False)
????bias3_3?=?tf.Variable(wDict[‘conv3_3‘][1]?trainable=False)
??
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????153??2018-06-03?19:33??Transfer\.idea\codest
?????文件????????198??2018-06-03?19:33??Transfer\.idea\codest
?????文件?????????84??2018-06-03?19:33??Transfer\.idea\dictionaries\Yel.xm
?????文件????????185??2018-06-03?19:33??Transfer\.idea\misc.xm
?????文件????????268??2018-06-03?19:33??Transfer\.idea\modules.xm
?????文件????????455??2018-06-03?19:33??Transfer\.idea\Transfer.iml
?????文件??????13130??2018-06-13?20:59??Transfer\.idea\workspace.xm
?????文件?????120217??2018-03-29?16:17??Transfer\content.jpg
?????文件?????202404??2018-03-29?16:17??Transfer\st
?????文件??????10707??2018-06-03?19:40??Transfer\st
?????目錄??????????0??2018-06-03?19:33??Transfer\.idea\codest
?????目錄??????????0??2018-06-03?19:33??Transfer\.idea\dictionaries
?????目錄??????????0??2018-06-13?20:59??Transfer\.idea
?????目錄??????????0??2018-06-03?15:07??Transfer\model
?????目錄??????????0??2018-06-13?20:56??Transfer
-----------?---------??----------?-----??----
???????????????347801????????????????????15
評(píng)論
共有 條評(píng)論