-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_lpips.py
More file actions
150 lines (117 loc) · 6.41 KB
/
evaluate_lpips.py
File metadata and controls
150 lines (117 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import argparse
import os
import sys
from skimage import io, transform
from torch.utils.data import DataLoader
import torch
torch.cuda.empty_cache()
import gc
gc.collect()
import re
import numpy as np
from scipy import misc
from PIL import Image
from torch.autograd import Variable
import glob
from torchvision import transforms
import matplotlib as plt
import cv2
import lpips
# https://github.com/safwankdb/ReCoNet-PyTorch/blob/master/testwarp.py
def get_subdirectories(directory_path):
subdirectories = []
for entry in os.scandir(directory_path):
if entry.is_dir():
subdirectories.append(entry.name)
return subdirectories
def main():
parser = argparse.ArgumentParser(description='parser for evaluating a model')
parser.add_argument("--frames", type=str, required=True,
help="folder that contains the images")
# parser.add_argument("--model", type=str, required=True,
# help="the name of the model")
# parser.add_argument("--opticflow", type=str, required=True,
# help="folder that contains the optic flow")
parser.add_argument("--cuda", type=int, default=1, required=False,
help="use cuda")
parser.add_argument("--image-size", type=int, default=256,
help="the image size")
args = parser.parse_args()
if args.cuda and not torch.cuda.is_available():
print("ERROR: cuda is not available, try running on CPU")
sys.exit(1)
# set up midas
device = torch.device("cuda" if args.cuda else "cpu")
print("Device: ", torch.cuda.get_device_name(0))
print("Running on ", device)
mse_loss = torch.nn.MSELoss()
lpips_sim = lpips.LPIPS(net='squeeze').to(device)
# INPUT_DIRS = ['game/input/ambush_2', 'game/input/ambush_4', 'game/input/ambush_5', 'game/input/ambush_6', 'game/input/ambush_7']
# INPUT_DIRS = ['../test_mpi/alley_2', '../test_mpi/ambush_5', '../test_mpi/bandage_2', '../test_mpi/market_6', '../test_mpi/temple_2']
INPUT_DIRS = ['../../unity_games_test/game_1_demo_scene_1', '../../unity_games_test/game_1_demo_scene_2', '../../unity_games_test/game_1_demo_scene_3',
'../../unity_games_test/game_2_fontaine_scene_1', '../../unity_games_test/game_2_fontaine_scene_2', '../../unity_games_test/game_2_fontaine_scene_3',
'../../unity_games_test/game_3_dead_scene_1', '../../unity_games_test/game_3_dead_scene_2', '../../unity_games_test/game_3_dead_scene_3',
'../../unity_games_test/game_4_seed_scene_1', '../../unity_games_test/game_4_seed_scene_2', '../../unity_games_test/game_4_seed_scene_3']
# INPUT_DIRS = ['../../unity_games_test/game_1_demo_scene_1', '../../unity_games_test/game_1_demo_scene_2', '../../unity_games_test/game_1_demo_scene_3',
# '../../unity_games_test/game_2_fontaine_scene_1', '../../unity_games_test/game_2_fontaine_scene_2', '../../unity_games_test/game_2_fontaine_scene_3',
# '../../unity_games_test/game_4_seed_scene_1', '../../unity_games_test/game_4_seed_scene_2', '../../unity_games_test/game_4_seed_scene_3']
# INPUT_DIRS = ['../../unity_games_test/game_1_demo_scene_1']
# HPC
# INPUT_DIRS = ['../../../../fastdata/acp20ei/sintel/input/ambush_2', '../../../../fastdata/acp20ei/sintel/input/ambush_4', '../../../../fastdata/acp20ei/sintel/input/ambush_5', '../../../../fastdata/acp20ei/sintel/input/ambush_6', '../../../../fastdata/acp20ei/sintel/input/ambush_7']
sum = 0.
#############################################################################
for input_dir in INPUT_DIRS:
# retrieve the original frames
# original_frames_path = 'game/input/ambush_2_frames/*.png'
original_frames_path = input_dir + '/*.jpg'
original_frames = []
for img in sorted(glob.glob(original_frames_path)):
image = Image.open(img).convert('RGB')
#if isinstance(image, Image.Image):
image = transforms.Resize((args.image_size,args.image_size))(image)
image = transforms.ToTensor()(image)
image = image.unsqueeze(0).to(device)
original_frames.append(image)
print('original frames: ', len(original_frames))
# retrive content images
# style = 'composition_vii'
scene_name = input_dir.replace('../../unity_games_test/','')
# dir_name = input_dir.replace('../test_mpi/', 'game/output/').replace(scene_name,'')
# uncomment 2 lines below for HPC
# scene_name = input_dir.replace('../../../../fastdata/acp20ei/sintel/input/','')
# dir_name = '../sintel_output/'
test_frames_path = args.frames
all_scenes = get_subdirectories(args.frames)
for scene in all_scenes:
if scene_name in str(scene):
test_frames_path += scene
# test_frames_path = args.frames
#test_frames_path = dir_name + args.model.replace('saved_models/','').replace('.pth', '_test_' + scene_name)
# test_frames_path = 'game/fast-multi-style-results' + style + scene_name + '_frames'
#test_frames_path = input_dir # args.frames
print(test_frames_path)
content_images = []
for img in sorted(glob.glob(test_frames_path + '/*.jpg')):
image = Image.open(img).convert('RGB')
#if isinstance(image, Image.Image):
image = transforms.Resize((args.image_size,args.image_size))(image)
image = transforms.ToTensor()(image)
image = image.unsqueeze(0).to(device)
content_images.append(image)
print('content images: ', len(content_images))
dist = 0.
for itr, (org, stylized) in enumerate(zip(original_frames, content_images)):
# print(itr)
# if(itr==0):
# continue
# else:
### scale to [-1, 1]
org = org * 2.0 - 1
stylized = stylized * 2.0 - 1
dist += lpips_sim.forward(org, stylized)
# print(dist)
#print(warped)
# io.imsave(args.opticflow + "warped.png", warped.squeeze().permute(1,2,0).cpu().numpy())
print('Average lpips error: ', dist.item()/len(original_frames))
sum += dist/len(original_frames)
print('Average lpips error over all directories: ', round(sum.item()/len(INPUT_DIRS),4))