Create test_api.py
Browse files- test_api.py +95 -0
test_api.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
import torch
|
| 4 |
+
import time
|
| 5 |
+
import numpy as np
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from AdaIN import AdaINNet
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from torchvision.utils import save_image
|
| 10 |
+
from torchvision.transforms import ToPILImage
|
| 11 |
+
from utils import adaptive_instance_normalization, grid_image, transform,linear_histogram_matching, Range
|
| 12 |
+
from glob import glob
|
| 13 |
+
|
| 14 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def style_transfer(content_tensor, style_tensor, encoder, decoder, alpha=1.0):
|
| 18 |
+
"""
|
| 19 |
+
Given content image and style image, generate feature maps with encoder, apply
|
| 20 |
+
neural style transfer with adaptive instance normalization, generate output image
|
| 21 |
+
with decoder
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
content_tensor (torch.FloatTensor): Content image
|
| 25 |
+
style_tensor (torch.FloatTensor): Style Image
|
| 26 |
+
encoder: Encoder (vgg19) network
|
| 27 |
+
decoder: Decoder network
|
| 28 |
+
alpha (float, default=1.0): Weight of style image feature
|
| 29 |
+
|
| 30 |
+
Return:
|
| 31 |
+
output_tensor (torch.FloatTensor): Style Transfer output image
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
content_enc = encoder(content_tensor)
|
| 35 |
+
style_enc = encoder(style_tensor)
|
| 36 |
+
|
| 37 |
+
transfer_enc = adaptive_instance_normalization(content_enc, style_enc)
|
| 38 |
+
|
| 39 |
+
mix_enc = alpha * transfer_enc + (1-alpha) * content_enc
|
| 40 |
+
return decoder(mix_enc)
|
| 41 |
+
|
| 42 |
+
def run_adain(content_dir, style_dir, vgg_pth='vgg_normalized.pth', decoder_pth='decoder.pth', alpha=1.0):
|
| 43 |
+
content_pths = [Path(f) for f in glob(content_dir+'/*')]
|
| 44 |
+
style_pths = [Path(f) for f in glob(style_dir+'/*')]
|
| 45 |
+
|
| 46 |
+
assert len(content_pths) > 0, 'Failed to load content image'
|
| 47 |
+
assert len(style_pths) > 0, 'Failed to load style image'
|
| 48 |
+
|
| 49 |
+
# Prepare directory for saving results
|
| 50 |
+
out_dir = tempfile.mkdtemp()
|
| 51 |
+
os.makedirs(out_dir, exist_ok=True)
|
| 52 |
+
|
| 53 |
+
# Load AdaIN model
|
| 54 |
+
vgg = torch.load(vgg_pth)
|
| 55 |
+
model = AdaINNet(vgg).to(device)
|
| 56 |
+
model.decoder.load_state_dict(torch.load(decoder_pth))
|
| 57 |
+
model.eval()
|
| 58 |
+
|
| 59 |
+
# Prepare image transform
|
| 60 |
+
t = transform(512)
|
| 61 |
+
|
| 62 |
+
# Timer
|
| 63 |
+
times = []
|
| 64 |
+
|
| 65 |
+
for content_pth in content_pths:
|
| 66 |
+
content_img = Image.open(content_pth)
|
| 67 |
+
content_tensor = t(content_img).unsqueeze(0).to(device)
|
| 68 |
+
|
| 69 |
+
for style_pth in style_pths:
|
| 70 |
+
|
| 71 |
+
style_tensor = t(Image.open(style_pth)).unsqueeze(0).to(device)
|
| 72 |
+
|
| 73 |
+
# Start time
|
| 74 |
+
tic = time.perf_counter()
|
| 75 |
+
|
| 76 |
+
# Execute style transfer
|
| 77 |
+
with torch.no_grad():
|
| 78 |
+
out_tensor = style_transfer(content_tensor, style_tensor, model.encoder, model.decoder, alpha).cpu()
|
| 79 |
+
|
| 80 |
+
# End time
|
| 81 |
+
toc = time.perf_counter()
|
| 82 |
+
print("Content: " + content_pth.stem + ". Style: " \
|
| 83 |
+
+ style_pth.stem + '. Alpha: ' + str(alpha) + '. Style Transfer time: %.4f seconds' % (toc-tic))
|
| 84 |
+
times.append(toc-tic)
|
| 85 |
+
|
| 86 |
+
# Save image
|
| 87 |
+
out_pth = out_dir + content_pth.stem + '_style_' + style_pth.stem + '_alpha' + str(alpha)
|
| 88 |
+
out_pth += content_pth.suffix
|
| 89 |
+
save_image(out_tensor, out_pth)
|
| 90 |
+
|
| 91 |
+
# Remove runtime of first iteration because it is flawed for some unknown reason
|
| 92 |
+
if len(times) > 1:
|
| 93 |
+
times.pop(0)
|
| 94 |
+
avg = sum(times)/len(times)
|
| 95 |
+
print("Average style transfer time: %.4f seconds" % (avg))
|