├── .gitignore ├── examples ├── beili.jpg ├── 1-output.jpg ├── 1-style.jpg ├── 2-output.jpg ├── 2-style1.jpg ├── 2-style2.jpg ├── 1-content.jpg ├── 2-content.jpg └── tweaks │ ├── cwe01.jpg │ ├── swe02.jpg │ ├── swe20.jpg │ ├── swe14_pavg.jpg │ ├── swe14_pmax.jpg │ ├── cwe10_default.jpg │ └── swe14_pmax_pcyuv.jpg ├── requirements.txt ├── .editorconfig ├── begin.py ├── vgg.py ├── README.md ├── neural_style.py ├── stylize.py └── LICENSE.txt /.gitignore: -------------------------------------------------------------------------------- 1 | *.mat 2 | env/ 3 | .idea 4 | __pycache__ 5 | *.pyc 6 | -------------------------------------------------------------------------------- /examples/beili.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenqiwenqi1/neural-style/HEAD/examples/beili.jpg -------------------------------------------------------------------------------- /examples/1-output.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenqiwenqi1/neural-style/HEAD/examples/1-output.jpg -------------------------------------------------------------------------------- /examples/1-style.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenqiwenqi1/neural-style/HEAD/examples/1-style.jpg -------------------------------------------------------------------------------- /examples/2-output.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenqiwenqi1/neural-style/HEAD/examples/2-output.jpg -------------------------------------------------------------------------------- /examples/2-style1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenqiwenqi1/neural-style/HEAD/examples/2-style1.jpg -------------------------------------------------------------------------------- /examples/2-style2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenqiwenqi1/neural-style/HEAD/examples/2-style2.jpg -------------------------------------------------------------------------------- /examples/1-content.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenqiwenqi1/neural-style/HEAD/examples/1-content.jpg -------------------------------------------------------------------------------- /examples/2-content.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenqiwenqi1/neural-style/HEAD/examples/2-content.jpg -------------------------------------------------------------------------------- /examples/tweaks/cwe01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenqiwenqi1/neural-style/HEAD/examples/tweaks/cwe01.jpg -------------------------------------------------------------------------------- /examples/tweaks/swe02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenqiwenqi1/neural-style/HEAD/examples/tweaks/swe02.jpg -------------------------------------------------------------------------------- /examples/tweaks/swe20.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenqiwenqi1/neural-style/HEAD/examples/tweaks/swe20.jpg -------------------------------------------------------------------------------- /examples/tweaks/swe14_pavg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenqiwenqi1/neural-style/HEAD/examples/tweaks/swe14_pavg.jpg -------------------------------------------------------------------------------- /examples/tweaks/swe14_pmax.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenqiwenqi1/neural-style/HEAD/examples/tweaks/swe14_pmax.jpg -------------------------------------------------------------------------------- /examples/tweaks/cwe10_default.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenqiwenqi1/neural-style/HEAD/examples/tweaks/cwe10_default.jpg -------------------------------------------------------------------------------- /examples/tweaks/swe14_pmax_pcyuv.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wenqiwenqi1/neural-style/HEAD/examples/tweaks/swe14_pmax_pcyuv.jpg -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | numpy 2 | Pillow # provides PIL 3 | scipy 4 | tensorflow-gpu >= 1.0 # installs tensorflow with GPU support, should still work even without GPU 5 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | insert_final_newline = true 7 | indent_style = space 8 | trim_trailing_whitespace = true 9 | 10 | [*.py] 11 | indent_size = 4 12 | 13 | [*.md] 14 | trim_trailing_whitespace = false 15 | -------------------------------------------------------------------------------- /begin.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import numpy as np 4 | import scipy.misc 5 | 6 | from stylize import stylize 7 | 8 | import math 9 | from argparse import ArgumentParser 10 | 11 | from PIL import Image 12 | # default arguments 13 | CONTENT_WEIGHT = 5e0 14 | CONTENT_WEIGHT_BLEND = 1 15 | STYLE_WEIGHT = 5e2 16 | TV_WEIGHT = 1e2 17 | STYLE_LAYER_WEIGHT_EXP = 1 18 | LEARNING_RATE = 1e1 19 | BETA1 = 0.9 20 | BETA2 = 0.999 21 | EPSILON = 1e-08 22 | STYLE_SCALE = 1.0 23 | ITERATIONS = 1000 24 | VGG_PATH = 'imagenet-vgg-verydeep-19.mat' 25 | POOLING = 'max' 26 | 27 | def imread(path): #读取图片 28 | img = scipy.misc.imread(path).astype(np.float) 29 | if len(img.shape) == 2: 30 | # grayscale 31 | img = np.dstack((img,img,img)) 32 | elif img.shape[2] == 4: 33 | # PNG with alpha channel 34 | img = img[:,:,:3] 35 | return img 36 | 37 | content='examples/beili.jpg' #此处为内容图片路径,可修改 38 | styles=['examples/1-style.jpg'] #此处为风格图片路径,可修改 39 | 40 | content_image = imread(content) #读取content图片 41 | style_images = [imread(style) for style in styles] #读取style图片,可以有多个 42 | initial_noiseblend = 1.0 43 | initial = content_image 44 | style_blend_weights = [1.0/len(style_images) for _ in style_images] 45 | for iteration, image in stylize( 46 | network=VGG_PATH, 47 | initial=initial, 48 | initial_noiseblend=initial_noiseblend, 49 | content=content_image, 50 | styles=style_images, 51 | preserve_colors=None, 52 | iterations=ITERATIONS, 53 | content_weight=CONTENT_WEIGHT, 54 | content_weight_blend=CONTENT_WEIGHT_BLEND, 55 | style_weight=STYLE_WEIGHT, 56 | style_layer_weight_exp=STYLE_LAYER_WEIGHT_EXP, 57 | style_blend_weights=style_blend_weights, 58 | tv_weight=TV_WEIGHT, 59 | learning_rate=LEARNING_RATE, 60 | beta1=BETA1, 61 | beta2=BETA2, 62 | epsilon=EPSILON, 63 | pooling=POOLING, 64 | print_iterations=None, 65 | checkpoint_iterations=None 66 | ): 67 | print(iteration) 68 | -------------------------------------------------------------------------------- /vgg.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import scipy.io 4 | 5 | VGG19_LAYERS = ( 6 | 'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1', 7 | 8 | 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2', 9 | 10 | 'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3', 11 | 'relu3_3', 'conv3_4', 'relu3_4', 'pool3', 12 | 13 | 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3', 14 | 'relu4_3', 'conv4_4', 'relu4_4', 'pool4', 15 | 16 | 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3', 17 | 'relu5_3', 'conv5_4', 'relu5_4' 18 | ) 19 | 20 | ##我们需要的信息是每层神经网络的kernels和bias 21 | def load_net(data_path): 22 | data = scipy.io.loadmat(data_path) 23 | if not all(i in data for i in ('layers', 'classes', 'normalization')): #判断这几个变量名是否在字典里 24 | raise ValueError("You're using the wrong VGG19 data. Please follow the instructions in the README to download the correct data.") 25 | mean = data['normalization'][0][0][0] 26 | mean_pixel = np.mean(mean, axis=(0, 1)) #先按0轴求均值,再按1轴求均值。0轴为行,1轴为列 27 | weights = data['layers'][0] 28 | return weights, mean_pixel 29 | 30 | def net_preloaded(weights, input_image, pooling): 31 | net = {} 32 | current = input_image 33 | for i, name in enumerate(VGG19_LAYERS): 34 | kind = name[:4] 35 | if kind == 'conv': 36 | kernels, bias = weights[i][0][0][0][0] 37 | # matconvnet: weights are [width, height, in_channels, out_channels] 38 | # tensorflow: weights are [height, width, in_channels, out_channels] 39 | kernels = np.transpose(kernels, (1, 0, 2, 3)) #因为tf和mat的weights位置不一样,所以要进行转置 40 | bias = bias.reshape(-1) 41 | current = _conv_layer(current, kernels, bias) 42 | elif kind == 'relu': 43 | current = tf.nn.relu(current) 44 | elif kind == 'pool': 45 | current = _pool_layer(current, pooling) 46 | net[name] = current 47 | 48 | assert len(net) == len(VGG19_LAYERS) 49 | return net 50 | 51 | def _conv_layer(input, weights, bias): 52 | conv = tf.nn.conv2d(input, tf.constant(weights), strides=(1, 1, 1, 1), 53 | padding='SAME') 54 | return tf.nn.bias_add(conv, bias) 55 | 56 | 57 | def _pool_layer(input, pooling): 58 | if pooling == 'avg': 59 | return tf.nn.avg_pool(input, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1), 60 | padding='SAME') 61 | else: 62 | return tf.nn.max_pool(input, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1), 63 | padding='SAME') 64 | 65 | def preprocess(image, mean_pixel): 66 | return image - mean_pixel 67 | 68 | 69 | def unprocess(image, mean_pixel): 70 | return image + mean_pixel 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 本代码加入了中文注释,更易理解,同时新加入begin.py可直接运行调试。 2 | # neural-style 3 | 4 | An implementation of [neural style][paper] in TensorFlow. 5 | 6 | This implementation is a lot simpler than a lot of the other ones out there, 7 | thanks to TensorFlow's really nice API and [automatic differentiation][ad]. 8 | 9 | TensorFlow doesn't support [L-BFGS][l-bfgs] (which is what the original authors 10 | used), so we use [Adam][adam]. This may require a little bit more 11 | hyperparameter tuning to get nice results. 12 | 13 | ## Related Projects 14 | 15 | See [here][lengstrom-fast-style-transfer] for an implementation of [fast 16 | (feed-forward) neural style][fast-neural-style] in TensorFlow. 17 | 18 | **[Try neural style](https://tenso.rs/demos/fast-neural-style/) client-side in 19 | your web browser without installing any software (using 20 | [TensorFire](https://tenso.rs/)).** 21 | 22 | ## Running 23 | 24 | `python neural_style.py --content --styles