├── IMAGES ├── 0_1.gif ├── 1.gif ├── 1.png ├── 10.png ├── 1lanting_0.0.jpg ├── 1lanting_0.2.jpg ├── 1lanting_0.4.jpg ├── 1lanting_0.6.jpg ├── 1lanting_0.8.jpg ├── 1lanting_1.0.jpg ├── 2.png ├── 3.png ├── 4.png ├── 4_9.gif ├── 5.png ├── 6.png ├── 7.png ├── 8.png ├── 9.png ├── cin.jpg ├── content.jpg ├── content_dog.jpg ├── dog_0.0.jpg ├── dog_0.2.jpg ├── dog_0.4.jpg ├── dog_0.6.jpg ├── dog_0.8.jpg ├── dog_1.0.jpg ├── door.jpg ├── door_.jpg ├── lanting_0.0.jpg ├── lanting_0.2.jpg ├── lanting_0.4.jpg ├── lanting_0.6.jpg ├── lanting_0.8.jpg └── lanting_1.0.jpg ├── LICENSE ├── MSCOCO ├── COCO_train2014_000000000009.jpg ├── COCO_train2014_000000000025.jpg ├── COCO_train2014_000000000030.jpg ├── COCO_train2014_000000000034.jpg ├── COCO_train2014_000000000036.jpg └── COCO_train2014_000000000049.jpg ├── README.md ├── imgs ├── 11.jpg └── 5.jpg ├── main.py ├── network.py ├── ops.py ├── results ├── lanting_0.0.jpg ├── lanting_0.2.jpg ├── lanting_0.4.jpg ├── lanting_0.6.jpg ├── lanting_0.8.jpg ├── lanting_1.0.jpg ├── result0.0.jpg ├── result0.1.jpg ├── result0.2.jpg ├── result0.3.jpg ├── result0.4.jpg ├── result0.5.jpg ├── result0.6.jpg ├── result0.7.jpg ├── result0.8.jpg ├── result0.9.jpg └── result1.0.jpg ├── save_para └── README.txt ├── style_imgs ├── 1.png ├── 10.png ├── 2.png ├── 3.png ├── 4.png ├── 5.png ├── 6.png ├── 7.png ├── 8.png └── 9.png ├── train.py ├── utils.py └── vgg_para └── README.txt /IMAGES/0_1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/0_1.gif -------------------------------------------------------------------------------- /IMAGES/1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/1.gif -------------------------------------------------------------------------------- /IMAGES/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/1.png -------------------------------------------------------------------------------- /IMAGES/10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/10.png -------------------------------------------------------------------------------- /IMAGES/1lanting_0.0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/1lanting_0.0.jpg -------------------------------------------------------------------------------- /IMAGES/1lanting_0.2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/1lanting_0.2.jpg -------------------------------------------------------------------------------- /IMAGES/1lanting_0.4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/1lanting_0.4.jpg -------------------------------------------------------------------------------- /IMAGES/1lanting_0.6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/1lanting_0.6.jpg -------------------------------------------------------------------------------- /IMAGES/1lanting_0.8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/1lanting_0.8.jpg -------------------------------------------------------------------------------- /IMAGES/1lanting_1.0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/1lanting_1.0.jpg -------------------------------------------------------------------------------- /IMAGES/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/2.png -------------------------------------------------------------------------------- /IMAGES/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/3.png -------------------------------------------------------------------------------- /IMAGES/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/4.png -------------------------------------------------------------------------------- /IMAGES/4_9.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/4_9.gif -------------------------------------------------------------------------------- /IMAGES/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/5.png -------------------------------------------------------------------------------- /IMAGES/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/6.png -------------------------------------------------------------------------------- /IMAGES/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/7.png -------------------------------------------------------------------------------- /IMAGES/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/8.png -------------------------------------------------------------------------------- /IMAGES/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/9.png -------------------------------------------------------------------------------- /IMAGES/cin.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/cin.jpg -------------------------------------------------------------------------------- /IMAGES/content.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/content.jpg -------------------------------------------------------------------------------- /IMAGES/content_dog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/content_dog.jpg -------------------------------------------------------------------------------- /IMAGES/dog_0.0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/dog_0.0.jpg -------------------------------------------------------------------------------- /IMAGES/dog_0.2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/dog_0.2.jpg -------------------------------------------------------------------------------- /IMAGES/dog_0.4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/dog_0.4.jpg -------------------------------------------------------------------------------- /IMAGES/dog_0.6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/dog_0.6.jpg -------------------------------------------------------------------------------- /IMAGES/dog_0.8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/dog_0.8.jpg -------------------------------------------------------------------------------- /IMAGES/dog_1.0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/dog_1.0.jpg -------------------------------------------------------------------------------- /IMAGES/door.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/door.jpg -------------------------------------------------------------------------------- /IMAGES/door_.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/door_.jpg -------------------------------------------------------------------------------- /IMAGES/lanting_0.0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/lanting_0.0.jpg -------------------------------------------------------------------------------- /IMAGES/lanting_0.2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/lanting_0.2.jpg -------------------------------------------------------------------------------- /IMAGES/lanting_0.4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/lanting_0.4.jpg -------------------------------------------------------------------------------- /IMAGES/lanting_0.6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/lanting_0.6.jpg -------------------------------------------------------------------------------- /IMAGES/lanting_0.8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/lanting_0.8.jpg -------------------------------------------------------------------------------- /IMAGES/lanting_1.0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/IMAGES/lanting_1.0.jpg -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 MarTinGuo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MSCOCO/COCO_train2014_000000000009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/MSCOCO/COCO_train2014_000000000009.jpg -------------------------------------------------------------------------------- /MSCOCO/COCO_train2014_000000000025.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/MSCOCO/COCO_train2014_000000000025.jpg -------------------------------------------------------------------------------- /MSCOCO/COCO_train2014_000000000030.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/MSCOCO/COCO_train2014_000000000030.jpg -------------------------------------------------------------------------------- /MSCOCO/COCO_train2014_000000000034.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/MSCOCO/COCO_train2014_000000000034.jpg -------------------------------------------------------------------------------- /MSCOCO/COCO_train2014_000000000036.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/MSCOCO/COCO_train2014_000000000036.jpg -------------------------------------------------------------------------------- /MSCOCO/COCO_train2014_000000000049.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/MSCOCO/COCO_train2014_000000000049.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Conditional-Instance-Norm-for-n-Style-Transfer 2 | Implementation of the paper A Learned Representation for Artistic Style 3 | 4 | ## Introduction 5 | Simply implementing the paper [A Learned Representation for Artistic Style](https://arxiv.org/pdf/1610.07629.pdf) (Conditional instance normalization) 6 | ![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/cin.jpg) 7 | 8 | ``` python 9 | def conditional_instance_norm(x, scope_bn, y1=None, y2=None, alpha=1): 10 | mean, var = tf.nn.moments(x, axes=[1, 2], keep_dims=True) 11 | if y1==None: 12 | beta = tf.get_variable(name=scope_bn + 'beta', shape=[x.shape[-1]], initializer=tf.constant_initializer([0.]), trainable=True) 13 | gamma = tf.get_variable(name=scope_bn + 'gamma', shape=[x.shape[-1]], initializer=tf.constant_initializer([1.]), trainable=True) 14 | else: 15 | beta = tf.get_variable(name=scope_bn+'beta', shape=[y1.shape[-1], x.shape[-1]], initializer=tf.constant_initializer([0.]), trainable=True) # label_nums x C 16 | gamma = tf.get_variable(name=scope_bn+'gamma', shape=[y1.shape[-1], x.shape[-1]], initializer=tf.constant_initializer([1.]), trainable=True) # label_nums x C 17 | beta1 = tf.matmul(y1, beta) 18 | gamma1 = tf.matmul(y1, gamma) 19 | beta2 = tf.matmul(y2, beta) 20 | gamma2 = tf.matmul(y2, gamma) 21 | beta = alpha * beta1 + (1. - alpha) * beta2 22 | gamma = alpha * gamma1 + (1. - alpha) * gamma2 23 | x = tf.nn.batch_normalization(x, mean, var, beta, gamma, 1e-10) 24 | return x 25 | ``` 26 | 27 | ## How to use 28 | 1. Download the dataset [MSCOCO](http://images.cocodataset.org/zips/train2014.zip), and unzip the dataset to the folder 'MSCOCO' 29 | ``` 30 | ├── imgs 31 | ├── results 32 | ├── save_imgs 33 | ├── save_para 34 | ├── style_imgs 35 | ├── vgg_para 36 | ├── MSCOCO 37 |    ├── COCO_train2014_000000000009.jpg 38 | ├── COCO_train2014_000000000025.jpg 39 | ├── COCO_train2014_000000000030.jpg 40 | ├── COCO_train2014_000000000034.jpg 41 | ├── COCO_train2014_000000000036.jpg 42 | ├── COCO_train2014_000000000049.jpg 43 | ... 44 | ``` 45 | 2. Download the vgg16.npy, and put it into the folder 'vgg_para' 46 | 3. Execute the python file 'main.py' 47 | 48 | ## Requirement 49 | - python3.5 50 | - tensorflow1.4.0 51 | - scipy 52 | - numpy 53 | - pillow 54 | 55 | ## Results 56 | Style = alpha * style2 + (1 - alpha) * style1 57 | 58 | |Content|Style1|Style2|Result| 59 | |-|-|-|-| 60 | |![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/imgs/5.jpg)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/5.png)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/10.png)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/4_9.gif)| 61 | 62 | |Content|Style1|Style2|Result| 63 | |-|-|-|-| 64 | |![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/imgs/11.jpg)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/2.png)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/1.png)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/0_1.gif)| 65 | 66 | |Content|Style1|Style2| 67 | |-|-|-| 68 | |![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/content.jpg)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/7.png)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/4.png)| 69 | 70 | |alpha=0|alpha=0.6|alpha=1.0| 71 | |-|-|-| 72 | |![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/1lanting_0.0.jpg)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/1lanting_0.6.jpg)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/1lanting_1.0.jpg)| 73 | 74 | |Content|Style1|Style2| 75 | |-|-|-| 76 | |![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/content.jpg)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/6.png)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/2.png)| 77 | 78 | |alpha=0|alpha=0.6|alpha=1.0| 79 | |-|-|-| 80 | |![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/lanting_0.0.jpg)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/lanting_0.6.jpg)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/lanting_1.0.jpg)| 81 | 82 | |Content|Style1|Style2| 83 | |-|-|-| 84 | |![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/content_dog.jpg)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/7.png)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/10.png)| 85 | 86 | |alpha=0|alpha=0.2|alpha=0.4|alpha=0.6|alpha=0.8|alpha=1.0| 87 | |-|-|-|-|-|-| 88 | |![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/dog_0.0.jpg)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/dog_0.2.jpg)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/dog_0.4.jpg)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/dog_0.6.jpg)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/dog_0.8.jpg)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/dog_1.0.jpg)| 89 | 90 | |Content|style|result| 91 | |-|-|-| 92 | |![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/door.jpg)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/6.png)|![](https://github.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/blob/master/IMAGES/door_.jpg)| 93 | -------------------------------------------------------------------------------- /imgs/11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/imgs/11.jpg -------------------------------------------------------------------------------- /imgs/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/imgs/5.jpg -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from train import Train, Init, stylize 2 | import argparse 3 | 4 | parser = argparse.ArgumentParser() 5 | 6 | parser.add_argument("--IMG_H", type=int, default=256) 7 | parser.add_argument("--IMG_W", type=int, default=256) 8 | parser.add_argument("--IMG_C", type=int, default=3) 9 | parser.add_argument("--STYLE_H", type=int, default=512) 10 | parser.add_argument("--STYLE_W", type=int, default=512) 11 | parser.add_argument("--C_NUMS", type=int, default=10)#The number of style images 12 | parser.add_argument("--BATCH_SIZE", type=int, default=2) 13 | parser.add_argument("--LEARNING_RATE", type=float, default=0.001) 14 | parser.add_argument("--CONTENT_WEIGHT", type=float, default=1.0) 15 | parser.add_argument("--STYLE_WEIGHT", type=float, default=5.0) 16 | parser.add_argument("--PATH_CONTENT", type=str, default="./MSCOCO//") 17 | parser.add_argument("--PATH_STYLE", type=str, default="./style_imgs//") 18 | parser.add_argument("--PATH_MODEL", type=str, default="./save_para//")#The path of the model's weights 19 | parser.add_argument("--PATH_VGG16", type=str, default="./vgg_para//")#The path of pretrained model of VGG16 20 | parser.add_argument("--IS_TRAINED", type=bool, default=False) 21 | 22 | 23 | args = parser.parse_args() 24 | 25 | if not args.IS_TRAINED: 26 | Train(IMG_H=args.IMG_H, IMG_W=args.IMG_W, IMG_C=args.IMG_C, STYLE_H=args.STYLE_H, STYLE_W=args.STYLE_W, C_NUMS=args.C_NUMS, batch_size=args.BATCH_SIZE, learning_rate=args.LEARNING_RATE, 27 | content_weight=args.CONTENT_WEIGHT, style_weight=args.STYLE_WEIGHT, path_content=args.PATH_CONTENT, path_style=args.PATH_STYLE, 28 | model_path=args.PATH_MODEL, vgg_path=args.PATH_VGG16) 29 | else: 30 | parser.add_argument("--PATH_IMG", type=str, default="./imgs//5.jpg") 31 | parser.add_argument("--PATH_RESULTS", type=str, default="./results//") 32 | parser.add_argument("--LABEL_1", type=int, default=9)#Style 1 33 | parser.add_argument("--LABEL_2", type=int, default=4)#Style 2 34 | parser.add_argument("--ALPHA", type=float, default=0.5) 35 | args = parser.parse_args() 36 | target, sess, content, y1, y2, alpha = Init(args.C_NUMS, args.PATH_MODEL) 37 | #alpha * style2 + (1 - alpha) * style1 38 | for a in range(11): 39 | args.ALPHA = a / 10 40 | stylize(args.PATH_IMG, args.PATH_RESULTS, args.LABEL_1, args.LABEL_2, args.ALPHA, target, sess, content, y1, y2, alpha) 41 | -------------------------------------------------------------------------------- /network.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from ops import * 3 | 4 | def conv_(inputs, w, b): 5 | return tf.nn.conv2d(inputs, w, [1, 1, 1, 1], "SAME") + b 6 | 7 | def max_pooling(inputs): 8 | return tf.nn.max_pool(inputs, [1, 2, 2, 1], [1, 2, 2, 1], "SAME") 9 | 10 | def vggnet(inputs, vgg_path): 11 | inputs = tf.reverse(inputs, [-1]) - np.array([103.939, 116.779, 123.68]) 12 | para = np.load(vgg_path+"vgg16.npy", encoding="latin1").item() 13 | F = {} 14 | inputs = relu(conv_(inputs, para["conv1_1"][0], para["conv1_1"][1])) 15 | inputs = relu(conv_(inputs, para["conv1_2"][0], para["conv1_2"][1])) 16 | F["conv1_2"] = inputs 17 | inputs = max_pooling(inputs) 18 | inputs = relu(conv_(inputs, para["conv2_1"][0], para["conv2_1"][1])) 19 | inputs = relu(conv_(inputs, para["conv2_2"][0], para["conv2_2"][1])) 20 | F["conv2_2"] = inputs 21 | inputs = max_pooling(inputs) 22 | inputs = relu(conv_(inputs, para["conv3_1"][0], para["conv3_1"][1])) 23 | inputs = relu(conv_(inputs, para["conv3_2"][0], para["conv3_2"][1])) 24 | inputs = relu(conv_(inputs, para["conv3_3"][0], para["conv3_3"][1])) 25 | F["conv3_3"] = inputs 26 | inputs = max_pooling(inputs) 27 | inputs = relu(conv_(inputs, para["conv4_1"][0], para["conv4_1"][1])) 28 | inputs = relu(conv_(inputs, para["conv4_2"][0], para["conv4_2"][1])) 29 | inputs = relu(conv_(inputs, para["conv4_3"][0], para["conv4_3"][1])) 30 | F["conv4_3"] = inputs 31 | return F 32 | 33 | def transform(inputs, y1, y2, alpha): 34 | inputs = tf.reverse(inputs, [-1]) - np.array([103.939, 116.779, 123.68]) 35 | inputs = relu(conditional_instance_norm(conv("conv1", inputs, 9, 3, 32, 1), "cin1", y1, y2, alpha)) 36 | inputs = relu(conditional_instance_norm(conv("conv2", inputs, 3, 32, 64, 2), "cin2", y1, y2, alpha)) 37 | inputs = relu(conditional_instance_norm(conv("conv3", inputs, 3, 64, 128, 2), "cin3", y1, y2, alpha)) 38 | inputs = ResBlock("res1", inputs, 3, 128, 128, y1, y2, alpha) 39 | inputs = ResBlock("res2", inputs, 3, 128, 128, y1, y2, alpha) 40 | inputs = ResBlock("res3", inputs, 3, 128, 128, y1, y2, alpha) 41 | inputs = ResBlock("res4", inputs, 3, 128, 128, y1, y2, alpha) 42 | inputs = ResBlock("res5", inputs, 3, 128, 128, y1, y2, alpha) 43 | inputs = upsampling("up1", inputs, 128, 64, y1, y2, alpha) 44 | inputs = upsampling("up2", inputs, 64, 32, y1, y2, alpha) 45 | inputs = sigmoid(conditional_instance_norm(conv("last", inputs, 9, 32, 3, 1), "cinout", y1, y2, alpha)) * 255 46 | return inputs 47 | 48 | 49 | -------------------------------------------------------------------------------- /ops.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | 4 | 5 | def conditional_instance_norm(x, scope_bn, y1=None, y2=None, alpha=1): 6 | mean, var = tf.nn.moments(x, axes=[1, 2], keep_dims=True) 7 | if y1==None: 8 | beta = tf.get_variable(name=scope_bn + 'beta', shape=[x.shape[-1]], initializer=tf.constant_initializer([0.]), trainable=True) # label_nums x C 9 | gamma = tf.get_variable(name=scope_bn + 'gamma', shape=[x.shape[-1]], initializer=tf.constant_initializer([1.]), trainable=True) # label_nums x C 10 | else: 11 | beta = tf.get_variable(name=scope_bn+'beta', shape=[y1.shape[-1], x.shape[-1]], initializer=tf.constant_initializer([0.]), trainable=True) # label_nums x C 12 | gamma = tf.get_variable(name=scope_bn+'gamma', shape=[y1.shape[-1], x.shape[-1]], initializer=tf.constant_initializer([1.]), trainable=True) # label_nums x C 13 | beta1 = tf.matmul(y1, beta) 14 | gamma1 = tf.matmul(y1, gamma) 15 | beta2 = tf.matmul(y2, beta) 16 | gamma2 = tf.matmul(y2, gamma) 17 | beta = alpha * beta1 + (1. - alpha) * beta2 18 | gamma = alpha * gamma1 + (1. - alpha) * gamma2 19 | x = tf.nn.batch_normalization(x, mean, var, beta, gamma, 1e-10) 20 | return x 21 | 22 | 23 | def conv(name, inputs, k_size, nums_in, nums_out, strides): 24 | pad_size = k_size // 2 25 | inputs = tf.pad(inputs, [[0, 0], [pad_size, pad_size], [pad_size, pad_size], [0, 0]], mode="REFLECT") 26 | kernel = tf.get_variable(name+"W", [k_size, k_size, nums_in, nums_out], initializer=tf.truncated_normal_initializer(stddev=0.01)) 27 | bias = tf.get_variable(name+"B", [nums_out], initializer=tf.constant_initializer(0.)) 28 | return tf.nn.conv2d(inputs, kernel, [1, strides, strides, 1], "VALID") + bias 29 | 30 | def upsampling(name, inputs, nums_in, nums_out, y1, y2, alpha): 31 | inputs = tf.image.resize_nearest_neighbor(inputs, [tf.shape(inputs)[1] * 2, tf.shape(inputs)[2] * 2]) 32 | return conditional_instance_norm(conv(name, inputs, 3, nums_in, nums_out, 1), "cin"+name, y1, y2, alpha) 33 | 34 | def relu(inputs): 35 | return tf.nn.relu(inputs) 36 | 37 | def sigmoid(inputs): 38 | return tf.nn.sigmoid(inputs) 39 | 40 | def ResBlock(name, inputs, k_size, nums_in, nums_out, y1, y2, alpha): 41 | temp = inputs * 1.0 42 | inputs = conditional_instance_norm(conv("conv1_" + name, inputs, k_size, nums_in, nums_out, 1), "cin1"+name, y1, y2, alpha) 43 | inputs = relu(inputs) 44 | inputs = conditional_instance_norm(conv("conv2_" + name, inputs, k_size, nums_in, nums_out, 1), "cin2"+name, y1, y2, alpha) 45 | return inputs + temp 46 | 47 | def content_loss(phi_content, phi_target): 48 | return tf.nn.l2_loss(phi_content["conv2_2"] - phi_target["conv2_2"]) * 2 / tf.cast(tf.size(phi_content["conv2_2"]), dtype=tf.float32) 49 | 50 | def style_loss(phi_style, phi_target): 51 | layers = ["conv1_2", "conv2_2", "conv3_3", "conv4_3"] 52 | loss = 0 53 | for layer in layers: 54 | s_maps = phi_style[layer] 55 | G_s = gram(s_maps) 56 | t_maps = phi_target[layer] 57 | G_t = gram(t_maps) 58 | loss += tf.nn.l2_loss(G_s - G_t) * 2 / tf.cast(tf.size(G_t), dtype=tf.float32) 59 | return loss 60 | 61 | def gram(layer): 62 | shape = tf.shape(layer) 63 | num_images = shape[0] 64 | width = shape[1] 65 | height = shape[2] 66 | num_filters = shape[3] 67 | filters = tf.reshape(layer, tf.stack([num_images, -1, num_filters])) 68 | grams = tf.matmul(filters, filters, transpose_a=True) / tf.to_float(width * height * num_filters) 69 | return grams 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /results/lanting_0.0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/lanting_0.0.jpg -------------------------------------------------------------------------------- /results/lanting_0.2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/lanting_0.2.jpg -------------------------------------------------------------------------------- /results/lanting_0.4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/lanting_0.4.jpg -------------------------------------------------------------------------------- /results/lanting_0.6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/lanting_0.6.jpg -------------------------------------------------------------------------------- /results/lanting_0.8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/lanting_0.8.jpg -------------------------------------------------------------------------------- /results/lanting_1.0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/lanting_1.0.jpg -------------------------------------------------------------------------------- /results/result0.0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/result0.0.jpg -------------------------------------------------------------------------------- /results/result0.1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/result0.1.jpg -------------------------------------------------------------------------------- /results/result0.2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/result0.2.jpg -------------------------------------------------------------------------------- /results/result0.3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/result0.3.jpg -------------------------------------------------------------------------------- /results/result0.4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/result0.4.jpg -------------------------------------------------------------------------------- /results/result0.5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/result0.5.jpg -------------------------------------------------------------------------------- /results/result0.6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/result0.6.jpg -------------------------------------------------------------------------------- /results/result0.7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/result0.7.jpg -------------------------------------------------------------------------------- /results/result0.8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/result0.8.jpg -------------------------------------------------------------------------------- /results/result0.9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/result0.9.jpg -------------------------------------------------------------------------------- /results/result1.0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/results/result1.0.jpg -------------------------------------------------------------------------------- /save_para/README.txt: -------------------------------------------------------------------------------- 1 | save_para 2 | -------------------------------------------------------------------------------- /style_imgs/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/style_imgs/1.png -------------------------------------------------------------------------------- /style_imgs/10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/style_imgs/10.png -------------------------------------------------------------------------------- /style_imgs/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/style_imgs/2.png -------------------------------------------------------------------------------- /style_imgs/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/style_imgs/3.png -------------------------------------------------------------------------------- /style_imgs/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/style_imgs/4.png -------------------------------------------------------------------------------- /style_imgs/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/style_imgs/5.png -------------------------------------------------------------------------------- /style_imgs/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/style_imgs/6.png -------------------------------------------------------------------------------- /style_imgs/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/style_imgs/7.png -------------------------------------------------------------------------------- /style_imgs/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/style_imgs/8.png -------------------------------------------------------------------------------- /style_imgs/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MingtaoGuo/Conditional-Instance-Norm-for-n-Style-Transfer/05efb4cf86ba65b292bd0757281ca9ed1daa6e44/style_imgs/9.png -------------------------------------------------------------------------------- /train.py: -------------------------------------------------------------------------------- 1 | from network import vggnet, transform 2 | import tensorflow as tf 3 | from ops import content_loss, style_loss, gram 4 | from utils import random_batch, random_select_style 5 | from PIL import Image 6 | import numpy as np 7 | import scipy.misc as misc 8 | 9 | 10 | def Train(IMG_H = 256, IMG_W = 256, IMG_C = 3, STYLE_H=512, STYLE_W=512, C_NUMS = 10, batch_size = 2, learning_rate = 0.001, content_weight = 1.0, style_weight = 5.0, path_content = "./MSCOCO//", path_style = "./style_imgs//", model_path="./save_para//", vgg_path="./vgg_para//"): 11 | content = tf.placeholder(tf.float32, [batch_size, IMG_H, IMG_W, IMG_C]) 12 | style = tf.placeholder(tf.float32, [batch_size, STYLE_H, STYLE_W, IMG_C]) 13 | y = tf.placeholder(tf.float32, [1, C_NUMS]) 14 | y_ = tf.zeros([1, C_NUMS]) 15 | alpha = tf.constant([1.]) 16 | target = transform(content, y, y_, alpha) 17 | Phi_T = vggnet(target, vgg_path) 18 | Phi_C = vggnet(content, vgg_path) 19 | Phi_S = vggnet(style, vgg_path) 20 | Loss = content_loss(Phi_C, Phi_T) * content_weight + style_loss(Phi_S, Phi_T) * style_weight 21 | Style_loss = style_loss(Phi_S, Phi_T) 22 | Content_loss = content_loss(Phi_C, Phi_T) 23 | Opt = tf.train.AdamOptimizer(learning_rate).minimize(Loss) 24 | sess = tf.Session() 25 | sess.run(tf.global_variables_initializer()) 26 | 27 | saver = tf.train.Saver() 28 | for itr in range(40000): 29 | batch_content= random_batch(path_content, batch_size, [IMG_H, IMG_W, IMG_C]) 30 | batch_style, y_labels = random_select_style(path_style, batch_size, [STYLE_H, STYLE_W, IMG_C], C_NUMS) 31 | sess.run(Opt, feed_dict={content: batch_content, style: batch_style, y: y_labels}) 32 | if itr % 50 == 0: 33 | [loss, Target, CONTENT_LOSS, STYLE_LOSS] = sess.run([Loss, target, Content_loss, Style_loss], feed_dict={content: batch_content, style: batch_style, y: y_labels}) 34 | save_img = np.concatenate((batch_content[0, :, :, :], misc.imresize(batch_style[0, :, :, :], [IMG_H, IMG_W]), Target[0, :, :, :]), axis=1) 35 | print("Iteration: %d, Loss: %e, Content_loss: %e, Style_loss: %e"%(itr, loss, CONTENT_LOSS, STYLE_LOSS)) 36 | Image.fromarray(np.uint8(save_img)).save("./save_imgs//"+str(itr) + "_" + str(np.argmax(y_labels[0, :]))+".jpg") 37 | if itr % 500 == 0: 38 | saver.save(sess, model_path+"model.ckpt") 39 | 40 | def Init(c_nums = 10, model_path = "./save_para//"): 41 | content = tf.placeholder(tf.float32, [1, None, None, 3]) 42 | y1 = tf.placeholder(tf.float32, [1, c_nums]) 43 | y2 = tf.placeholder(tf.float32, [1, c_nums]) 44 | alpha = tf.placeholder(tf.float32) 45 | target = transform(content, y1, y2, alpha) 46 | sess = tf.Session() 47 | sess.run(tf.global_variables_initializer()) 48 | saver = tf.train.Saver() 49 | saver.restore(sess, model_path + ".\\model.ckpt") 50 | return target, sess, content, y1, y2, alpha 51 | 52 | def stylize(img_path,result_path, label1, label2, alpha, target, sess, content_ph, y1_ph, y2_ph, alpha_ph): 53 | img = np.array(Image.open(img_path)) 54 | Y1 = np.zeros([1, 10]) 55 | Y2 = np.zeros([1, 10]) 56 | Y1[0, label1] = 1 57 | Y2[0, label2] = 1 58 | img = sess.run(target, feed_dict={content_ph: img[np.newaxis, :, :, :], y1_ph: Y1, y2_ph: Y2, alpha_ph: alpha}) 59 | Image.fromarray(np.uint8(img[0, :, :, :])).save(result_path + "result"+str(alpha)+".jpg") 60 | 61 | 62 | if __name__ == "__main__": 63 | # Train() 64 | target, sess, content, y1, y2, alpha = Init() 65 | for alp in [0., 0.2, 0.4, 0.6, 0.8, 1.0]: 66 | stylize("C://Users//gmt//Desktop//content_dog.jpg", "./results//", 4, 5, alp, target, sess, content, y1, y2, alpha) 67 | 68 | 69 | -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | from PIL import Image 2 | import numpy as np 3 | import scipy.misc as misc 4 | import os 5 | 6 | def random_batch(path, batch_size, shape): 7 | filenames = os.listdir(path) 8 | rand_samples = np.random.randint(0, filenames.__len__(), [batch_size]) 9 | batch = np.zeros([batch_size, shape[0], shape[1], shape[2]]) 10 | c = 0 11 | for idx in rand_samples: 12 | try: 13 | batch[c, :, :, :] = misc.imresize(crop(np.array(Image.open(path + filenames[idx]))), [shape[0], shape[1]]) 14 | except: 15 | img = crop(np.array(Image.open(path + filenames[0]))) 16 | batch[c, :, :, :] = misc.imresize(img, [shape[0], shape[1]]) 17 | c += 1 18 | return batch 19 | 20 | def random_select_style(path, batch_size, shape, c_nums): 21 | filenames = os.listdir(path) 22 | rand_sample = np.random.randint(0, filenames.__len__()) 23 | img = misc.imresize(crop(np.array(Image.open(path + str(rand_sample + 1) + ".png"))), [shape[0], shape[1]]) 24 | batch = np.zeros([batch_size, shape[0], shape[1], shape[2]]) 25 | y = np.zeros([1, c_nums]) 26 | y[0, rand_sample] = 1 27 | for i in range(batch_size): 28 | batch[i, :, :, :] = img[:, :, :3] 29 | return batch, y 30 | 31 | def crop(img): 32 | h = img.shape[0] 33 | w = img.shape[1] 34 | if h < w: 35 | x = 0 36 | y = np.random.randint(0, w - h + 1) 37 | length = h 38 | elif h > w: 39 | x = np.random.randint(0, h - w + 1) 40 | y = 0 41 | length = w 42 | else: 43 | x = 0 44 | y = 0 45 | length = h 46 | return img[x:x+length, y:y+length, :] -------------------------------------------------------------------------------- /vgg_para/README.txt: -------------------------------------------------------------------------------- 1 | vgg16.npy --------------------------------------------------------------------------------