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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
- import argparse
- import torch
- from torch import nn
- from torchvision import transforms
- from PIL import Image
- import os
- import sys
- import pathlib
- import numpy as np
- import cv2
- import importlib
- import ssl
- from datasets import utils as ds_utils
- from runners import utils as rn_utils
- from external.Graphonomy import wrapper
- import face_alignment
- class InferenceWrapper(nn.Module):
- @staticmethod
- def get_args(args_dict):
- # Read and parse args of the module being loaded
- args_path = pathlib.Path(args_dict['project_dir']) / 'runs' / args_dict['experiment_name'] / 'args.txt'
- parser = argparse.ArgumentParser(conflict_handler='resolve')
- parser.add = parser.add_argument
- with open(args_path, 'rt') as args_file:
- lines = args_file.readlines()
- for line in lines:
- k, v, v_type = rn_utils.parse_args_line(line)
- parser.add('--%s' % k, type=v_type, default=v)
- args, _ = parser.parse_known_args()
- # Add args from args_dict that overwrite the default ones
- for k, v in args_dict.items():
- setattr(args, k, v)
- args.world_size = args.num_gpus
- return args
- def __init__(self, args_dict):
- super(InferenceWrapper, self).__init__()
- # Get a config for the network
- self.args = self.get_args(args_dict)
- self.to_tensor = transforms.ToTensor()
- # Load the model
- self.runner = importlib.import_module(f'runners.{self.args.runner_name}').RunnerWrapper(self.args, training=False)
- self.runner.eval()
- # Load pretrained weights
- checkpoints_dir = pathlib.Path(self.args.project_dir) / 'runs' / self.args.experiment_name / 'checkpoints'
- # Load pre-trained weights
- init_networks = rn_utils.parse_str_to_list(self.args.init_networks) if self.args.init_networks else {}
- networks_to_train = self.runner.nets_names_to_train
- if self.args.init_which_epoch != 'none' and self.args.init_experiment_dir:
- for net_name in init_networks:
- self.runner.nets[net_name].load_state_dict(torch.load(
- pathlib.Path(self.args.init_experiment_dir)
- / 'checkpoints'
- / f'{self.args.init_which_epoch}_{net_name}.pth',
- map_location='cpu'))
- for net_name in networks_to_train:
- if net_name not in init_networks and net_name in self.runner.nets.keys():
- self.runner.nets[net_name].load_state_dict(torch.load(
- checkpoints_dir
- / f'{self.args.which_epoch}_{net_name}.pth',
- map_location='cpu'))
-
- # Remove spectral norm to improve the performance
- self.runner.apply(rn_utils.remove_spectral_norm)
- # Stickman/facemasks drawer
- self.fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._2D, flip_input=True)
- self.net_seg = wrapper.SegmentationWrapper(self.args)
- if self.args.num_gpus > 0:
- self.cuda()
- def change_args(self, args_dict):
- self.args = self.get_args(args_dict)
- def preprocess_data(self, input_imgs, crop_data=True):
- imgs = []
- poses = []
- stickmen = []
- if len(input_imgs.shape) == 3:
- input_imgs = input_imgs[None]
- N = 1
- else:
- N = input_imgs.shape[0]
- for i in range(N):
- pose = self.fa.get_landmarks(input_imgs[i])[0]
- center = ((pose.min(0) + pose.max(0)) / 2).round().astype(int)
- size = int(max(pose[:, 0].max() - pose[:, 0].min(), pose[:, 1].max() - pose[:, 1].min()))
- center[1] -= size // 6
- if input_imgs is None:
- # Crop poses
- if crop_data:
- s = size * 2
- pose -= center - size
- else:
- # Crop images and poses
- img = Image.fromarray(input_imgs[i])
- if crop_data:
- img = img.crop((center[0]-size, center[1]-size, center[0]+size, center[1]+size))
- s = img.size[0]
- pose -= center - size
- img = img.resize((self.args.image_size, self.args.image_size), Image.BICUBIC)
- imgs.append((self.to_tensor(img) - 0.5) * 2)
- if crop_data:
- pose = pose / float(s)
- poses.append(torch.from_numpy((pose - 0.5) * 2).view(-1))
- poses = torch.stack(poses, 0)[None]
- if self.args.output_stickmen:
- stickmen = ds_utils.draw_stickmen(self.args, poses[0])
- stickmen = stickmen[None]
- if input_imgs is not None:
- imgs = torch.stack(imgs, 0)[None]
- if self.args.num_gpus > 0:
- poses = poses.cuda()
-
- if input_imgs is not None:
- imgs = imgs.cuda()
- if self.args.output_stickmen:
- stickmen = stickmen.cuda()
- segs = None
- if hasattr(self, 'net_seg') and not isinstance(imgs, list):
- segs = self.net_seg(imgs)[None]
- return poses, imgs, segs, stickmen
- def forward(self, data_dict, crop_data=True, no_grad=True):
- if 'target_imgs' not in data_dict.keys():
- data_dict['target_imgs'] = None
- # Inference without finetuning
- (source_poses,
- source_imgs,
- source_segs,
- source_stickmen) = self.preprocess_data(data_dict['source_imgs'], crop_data)
- (target_poses,
- target_imgs,
- target_segs,
- target_stickmen) = self.preprocess_data(data_dict['target_imgs'], crop_data)
- data_dict = {
- 'source_imgs': source_imgs,
- 'source_poses': source_poses,
- 'target_poses': target_poses}
- if len(target_imgs):
- data_dict['target_imgs'] = target_imgs
- if source_segs is not None:
- data_dict['source_segs'] = source_segs
- if target_segs is not None:
- data_dict['target_segs'] = target_segs
- if source_stickmen is not None:
- data_dict['source_stickmen'] = source_stickmen
- if target_stickmen is not None:
- data_dict['target_stickmen'] = target_stickmen
- if no_grad:
- with torch.no_grad():
- self.runner(data_dict)
- else:
- self.runner(data_dict)
- return self.runner.data_dict
|