falseu
commited on
Commit
·
f0d859a
1
Parent(s):
7930ce0
readme
Browse files- README.md +23 -31
- test_style_transfer.py +0 -58
README.md
CHANGED
@@ -1,14 +1,15 @@
|
|
1 |
2022-AdaIN-pytorch
|
2 |
============================
|
3 |
-
This is an unofficial Pytorch implementation of the paper, Style Transfer with Adaptive Instance Normalization [arxiv](https://arxiv.org/abs/1703.06868). I referred to the [official implementation](https://github.com/xunhuang1995/AdaIN-style) in Torch. I used pretrained weights for vgg19 and decoder from [naoto0804](https://github.com/naoto0804/pytorch-AdaIN).
|
4 |
|
5 |
-
|
6 |
----------------------------
|
7 |
-
* Python 3.7
|
8 |
* PyTorch 1.10
|
9 |
* Pillow
|
10 |
* TorchVision
|
11 |
* Numpy
|
|
|
12 |
* tqdm
|
13 |
|
14 |
|
@@ -17,7 +18,7 @@ Usage
|
|
17 |
|
18 |
### Training
|
19 |
|
20 |
-
The encoder uses pretrained vgg19 network. Download the [weight
|
21 |
Run the script train.py
|
22 |
```
|
23 |
$ python train.py --content_dir $CONTENT_DIR --style_dir STYLE_DIR --cuda
|
@@ -40,18 +41,13 @@ optional arguments:
|
|
40 |
|
41 |
### Test Image Style Transfer
|
42 |
|
43 |
-
Download
|
44 |
|
45 |
-
To test basic style transfer, run the script test_image.py.
|
46 |
|
47 |
```
|
48 |
$ python test.py --content_image $IMG --style_image $STYLE --decoder_weight $WEIGHT --cuda
|
49 |
|
50 |
-
usage: test_style_transfer.py [-h] [--content_image CONTENT_IMAGE] [--content_dir CONTENT_DIR]
|
51 |
-
[--style_image STYLE_IMAGE] [--style_dir STYLE_DIR]
|
52 |
-
[--decoder_weight DECODER_WEIGHT] [--alpha {Alpha Range}]
|
53 |
-
[--cuda] [--grid_pth GRID_PTH]
|
54 |
-
|
55 |
optional arguments:
|
56 |
-h, --help show this help message and exit
|
57 |
--content_image CONTENT_IMAGE
|
@@ -67,21 +63,17 @@ optional arguments:
|
|
67 |
Alpha [0.0, 1.0] controls style transfer level
|
68 |
--cuda Use CUDA
|
69 |
--grid_pth GRID_PTH
|
70 |
-
Specify a grid image path (default=None) if generate a grid image
|
|
|
71 |
```
|
72 |
|
73 |
### Test Image Interpolation Style Transfer
|
74 |
|
75 |
-
To test style transfer interpolation, run the script test_interpolate.py.
|
76 |
|
77 |
```
|
78 |
$ python test_interpolation.py --content_image $IMG --style_image $STYLE --decoder_weight $WEIGHT --cuda
|
79 |
|
80 |
-
usage: test_style_transfer.py [-h] [--content_image CONTENT_IMAGE] [--style_image STYLE_IMAGE]
|
81 |
-
[--decoder_weight DECODER_WEIGHT] [--alpha {Alpha Range}]
|
82 |
-
[--interpolation_weights INTERPOLATION_WEIGHTS]
|
83 |
-
[--cuda] [--grid_pth GRID_PTH]
|
84 |
-
|
85 |
optional arguments:
|
86 |
-h, --help show this help message and exit
|
87 |
--content_image CONTENT_IMAGE
|
@@ -92,25 +84,26 @@ optional arguments:
|
|
92 |
--alpha {Alpha Range}
|
93 |
Alpha [0.0, 1.0] controls style transfer level
|
94 |
--interpolation_weights INTERPOLATION_WEIGHTS
|
95 |
-
Interpolation weight of each style image, separated by comma.
|
|
|
96 |
--cuda Use CUDA
|
97 |
--grid_pth GRID_PTH
|
98 |
-
Specify a grid image path (default=None) to perform interpolation style
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
```
|
100 |
|
101 |
### Test Video Style Transfer
|
102 |
-
----------------------------
|
103 |
|
104 |
To test video style transfer, run the script test_video.py.
|
105 |
|
106 |
-
|
107 |
```
|
108 |
$ python test_video.py --content_video $VID --style_image $STYLE --decoder_weight $WEIGHT --cuda
|
109 |
|
110 |
-
usage: test_style_transfer.py [-h] [--content_video CONTENT_VID] [--style_image STYLE_IMAGE]
|
111 |
-
[--decoder_weight DECODER_WEIGHT] [--alpha {Alpha Range}]
|
112 |
-
[--cuda]
|
113 |
-
|
114 |
optional arguments:
|
115 |
-h, --help show this help message and exit
|
116 |
--content_image CONTENT_IMAGE
|
@@ -124,9 +117,8 @@ optional arguments:
|
|
124 |
```
|
125 |
|
126 |
|
127 |
-
|
128 |
----------------------------
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
-[3]: [Pretrained weights](https://github.com/naoto0804/pytorch-AdaIN)
|
|
|
1 |
2022-AdaIN-pytorch
|
2 |
============================
|
3 |
+
This is an unofficial Pytorch implementation of the paper, 'Style Transfer with Adaptive Instance Normalization' [arxiv](https://arxiv.org/abs/1703.06868). I referred to the [official implementation](https://github.com/xunhuang1995/AdaIN-style) in Torch. I used pretrained weights for vgg19 and decoder from [naoto0804](https://github.com/naoto0804/pytorch-AdaIN).
|
4 |
|
5 |
+
Requirements
|
6 |
----------------------------
|
7 |
+
* Python 3.7+
|
8 |
* PyTorch 1.10
|
9 |
* Pillow
|
10 |
* TorchVision
|
11 |
* Numpy
|
12 |
+
* imageio
|
13 |
* tqdm
|
14 |
|
15 |
|
|
|
18 |
|
19 |
### Training
|
20 |
|
21 |
+
The encoder uses pretrained vgg19 network. Download the [vgg19 weight](https://drive.google.com/file/d/1UcSl-Zn3byEmn15NIPXMf9zaGCKc2gfx/view?usp=sharing). The decoder is trained on MSCOCO and wikiart dataset.
|
22 |
Run the script train.py
|
23 |
```
|
24 |
$ python train.py --content_dir $CONTENT_DIR --style_dir STYLE_DIR --cuda
|
|
|
41 |
|
42 |
### Test Image Style Transfer
|
43 |
|
44 |
+
Download [vgg19 weight](https://drive.google.com/file/d/1UcSl-Zn3byEmn15NIPXMf9zaGCKc2gfx/view?usp=sharing), [decoder weight](https://drive.google.com/file/d/18JpLtMOapA-vwBz-LRomyTl24A9GwhTF/view?usp=sharing) under main folder.
|
45 |
|
46 |
+
To test basic style transfer, run the script test_image.py. Specify '--content_image' or 'style_img' to the image path. Specify '--content_dir' or '--style_dir' to iterate all images under this directory. Specify '--grid_pth' to collect all outputs in a grid image.
|
47 |
|
48 |
```
|
49 |
$ python test.py --content_image $IMG --style_image $STYLE --decoder_weight $WEIGHT --cuda
|
50 |
|
|
|
|
|
|
|
|
|
|
|
51 |
optional arguments:
|
52 |
-h, --help show this help message and exit
|
53 |
--content_image CONTENT_IMAGE
|
|
|
63 |
Alpha [0.0, 1.0] controls style transfer level
|
64 |
--cuda Use CUDA
|
65 |
--grid_pth GRID_PTH
|
66 |
+
Specify a grid image path (default=None) if generate a grid image
|
67 |
+
that contains all style transferred images
|
68 |
```
|
69 |
|
70 |
### Test Image Interpolation Style Transfer
|
71 |
|
72 |
+
To test style transfer interpolation, run the script test_interpolate.py. Specify 'style_image' with multiple paths separated by comma. Specify '--interpolation_weights' to interpolate once. Specify '--grid_pth' to interpolate with different built-in weights.
|
73 |
|
74 |
```
|
75 |
$ python test_interpolation.py --content_image $IMG --style_image $STYLE --decoder_weight $WEIGHT --cuda
|
76 |
|
|
|
|
|
|
|
|
|
|
|
77 |
optional arguments:
|
78 |
-h, --help show this help message and exit
|
79 |
--content_image CONTENT_IMAGE
|
|
|
84 |
--alpha {Alpha Range}
|
85 |
Alpha [0.0, 1.0] controls style transfer level
|
86 |
--interpolation_weights INTERPOLATION_WEIGHTS
|
87 |
+
Interpolation weight of each style image, separated by comma.
|
88 |
+
Do not specify if input grid_pth.
|
89 |
--cuda Use CUDA
|
90 |
--grid_pth GRID_PTH
|
91 |
+
Specify a grid image path (default=None) to perform interpolation style
|
92 |
+
|
93 |
+
transfer multiple times with different built-in weights and generate a
|
94 |
+
|
95 |
+
grid image that contains all style transferred images. Provide 4 style
|
96 |
+
|
97 |
+
images. Do not specify if input interpolation_weights.
|
98 |
```
|
99 |
|
100 |
### Test Video Style Transfer
|
|
|
101 |
|
102 |
To test video style transfer, run the script test_video.py.
|
103 |
|
|
|
104 |
```
|
105 |
$ python test_video.py --content_video $VID --style_image $STYLE --decoder_weight $WEIGHT --cuda
|
106 |
|
|
|
|
|
|
|
|
|
107 |
optional arguments:
|
108 |
-h, --help show this help message and exit
|
109 |
--content_image CONTENT_IMAGE
|
|
|
117 |
```
|
118 |
|
119 |
|
120 |
+
References
|
121 |
----------------------------
|
122 |
+
* X. Huang and S. Belongie. "Arbitrary Style Transfer in Real-time with Adaptive Instance Normalization.", in ICCV, 2017. [arxiv](https://arxiv.org/abs/1703.06868)
|
123 |
+
* [Original implementation in Torch](https://github.com/xunhuang1995/AdaIN-style)
|
124 |
+
* [Pretrained weights](https://github.com/naoto0804/pytorch-AdaIN)
|
|
test_style_transfer.py
DELETED
@@ -1,58 +0,0 @@
|
|
1 |
-
import argparse
|
2 |
-
import os
|
3 |
-
import numpy as np
|
4 |
-
from AdaIN import StyleTransferNet
|
5 |
-
from PIL import Image
|
6 |
-
import torch
|
7 |
-
import torchvision.transforms as transforms
|
8 |
-
from torchvision.utils import save_image
|
9 |
-
|
10 |
-
class AlphaRange(object):
|
11 |
-
def __init__(self, start, end):
|
12 |
-
self.start = start
|
13 |
-
self.end = end
|
14 |
-
def __eq__(self, other):
|
15 |
-
return self.start <= other <= self.end
|
16 |
-
def __str__(self):
|
17 |
-
return 'Alpha Range'
|
18 |
-
|
19 |
-
parser = argparse.ArgumentParser()
|
20 |
-
parser.add_argument('--input_image', type=str, help='test image')
|
21 |
-
parser.add_argument('--style_image', type=str, help='style image')
|
22 |
-
parser.add_argument('--weight', type=str, help='decoder weight file')
|
23 |
-
parser.add_argument('--alpha', type=float, default=1.0, choices=[AlphaRange(0.0, 1.0)], help='Level of style transfer, value between 0 and 1')
|
24 |
-
parser.add_argument('--cuda', action='store_true', help='Using GPU to train')
|
25 |
-
|
26 |
-
|
27 |
-
if __name__ == '__main__':
|
28 |
-
opt =parser.parse_args()
|
29 |
-
input_image = Image.open(opt.input_image)
|
30 |
-
style_image = Image.open(opt.style_image)
|
31 |
-
output_format = opt.input_image[opt.input_image.find('.'):]
|
32 |
-
out_dir = './results/'
|
33 |
-
os.makedirs(out_dir, exist_ok=True)
|
34 |
-
with torch.no_grad():
|
35 |
-
vgg_model = torch.load('vgg_normalized.pth')
|
36 |
-
|
37 |
-
net = StyleTransferNet(vgg_model)
|
38 |
-
net.decoder.load_state_dict(torch.load(opt.weight))
|
39 |
-
|
40 |
-
net.eval()
|
41 |
-
|
42 |
-
input_image = transforms.Resize(512)(input_image)
|
43 |
-
style_image = transforms.Resize(512)(style_image)
|
44 |
-
|
45 |
-
input_tensor = transforms.ToTensor()(input_image).unsqueeze(0)
|
46 |
-
style_tensor = transforms.ToTensor()(style_image).unsqueeze(0)
|
47 |
-
|
48 |
-
|
49 |
-
if torch.cuda.is_available() and opt.cuda:
|
50 |
-
net.cuda()
|
51 |
-
input_tensor = input_tensor.cuda()
|
52 |
-
style_tensor = style_tensor.cuda()
|
53 |
-
out_tensor = net([input_tensor, style_tensor], alpha = opt.alpha)
|
54 |
-
|
55 |
-
|
56 |
-
save_image(out_tensor, out_dir + opt.input_image[opt.input_image.rfind('/')+1: opt.input_image.find('.')]
|
57 |
-
+"_style_"+ opt.style_image[opt.style_image.rfind('/')+1: opt.style_image.find('.')]
|
58 |
-
+ output_format)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|