zarroug commited on
Commit
58b56b7
·
verified ·
1 Parent(s): deac5ca

Upload 14 files

Browse files
clip/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from .clip import *
clip/bpe_simple_vocab_16e6.txt.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:924691ac288e54409236115652ad4aa250f48203de50a9e4722a6ecd48d6804a
3
+ size 1356917
clip/clip.py ADDED
@@ -0,0 +1,241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import hashlib
2
+ import os
3
+ import urllib
4
+ import warnings
5
+ from typing import Any, Union, List
6
+
7
+ import torch
8
+ from PIL import Image
9
+ from torchvision.transforms import Compose, Resize, CenterCrop, ToTensor, Normalize
10
+ from tqdm import tqdm
11
+
12
+ from .model import build_model
13
+ from .simple_tokenizer import SimpleTokenizer as _Tokenizer
14
+
15
+ try:
16
+ from torchvision.transforms import InterpolationMode
17
+ BICUBIC = InterpolationMode.BICUBIC
18
+ except ImportError:
19
+ BICUBIC = Image.BICUBIC
20
+
21
+
22
+
23
+ __all__ = ["available_models", "load", "tokenize"]
24
+ _tokenizer = _Tokenizer()
25
+
26
+ _MODELS = {
27
+ "RN50": "https://openaipublic.azureedge.net/clip/models/afeb0e10f9e5a86da6080e35cf09123aca3b358a0c3e3b6c78a7b63bc04b6762/RN50.pt",
28
+ "RN101": "https://openaipublic.azureedge.net/clip/models/8fa8567bab74a42d41c5915025a8e4538c3bdbe8804a470a72f30b0d94fab599/RN101.pt",
29
+ "RN50x4": "https://openaipublic.azureedge.net/clip/models/7e526bd135e493cef0776de27d5f42653e6b4c8bf9e0f653bb11773263205fdd/RN50x4.pt",
30
+ "RN50x16": "https://openaipublic.azureedge.net/clip/models/52378b407f34354e150460fe41077663dd5b39c54cd0bfd2b27167a4a06ec9aa/RN50x16.pt",
31
+ "RN50x64": "https://openaipublic.azureedge.net/clip/models/be1cfb55d75a9666199fb2206c106743da0f6468c9d327f3e0d0a543a9919d9c/RN50x64.pt",
32
+ "ViT-B/32": "https://openaipublic.azureedge.net/clip/models/40d365715913c9da98579312b702a82c18be219cc2a73407c4526f58eba950af/ViT-B-32.pt",
33
+ "ViT-B/16": "https://openaipublic.azureedge.net/clip/models/5806e77cd80f8b59890b7e101eabd078d9fb84e6937f9e85e4ecb61988df416f/ViT-B-16.pt",
34
+ "ViT-L/14": "https://openaipublic.azureedge.net/clip/models/b8cca3fd41ae0c99ba7e8951adf17d267cdb84cd88be6f7c2e0eca1737a03836/ViT-L-14.pt",
35
+ "ViT-L/14@336px": "https://openaipublic.azureedge.net/clip/models/3035c92b350959924f9f00213499208652fc7ea050643e8b385c2dac08641f02/ViT-L-14-336px.pt",
36
+ }
37
+
38
+
39
+ def _download(url: str, root: str):
40
+ os.makedirs(root, exist_ok=True)
41
+ filename = os.path.basename(url)
42
+
43
+ expected_sha256 = url.split("/")[-2]
44
+ download_target = os.path.join(root, filename)
45
+
46
+ if os.path.exists(download_target) and not os.path.isfile(download_target):
47
+ raise RuntimeError(f"{download_target} exists and is not a regular file")
48
+
49
+ if os.path.isfile(download_target):
50
+ if hashlib.sha256(open(download_target, "rb").read()).hexdigest() == expected_sha256:
51
+ return download_target
52
+ else:
53
+ warnings.warn(f"{download_target} exists, but the SHA256 checksum does not match; re-downloading the file")
54
+
55
+ with urllib.request.urlopen(url) as source, open(download_target, "wb") as output:
56
+ with tqdm(total=int(source.info().get("Content-Length")), ncols=80, unit='iB', unit_scale=True, unit_divisor=1024) as loop:
57
+ while True:
58
+ buffer = source.read(8192)
59
+ if not buffer:
60
+ break
61
+
62
+ output.write(buffer)
63
+ loop.update(len(buffer))
64
+
65
+ if hashlib.sha256(open(download_target, "rb").read()).hexdigest() != expected_sha256:
66
+ raise RuntimeError("Model has been downloaded but the SHA256 checksum does not not match")
67
+
68
+ return download_target
69
+
70
+
71
+ def _convert_image_to_rgb(image):
72
+ return image.convert("RGB")
73
+
74
+
75
+ def _transform(n_px):
76
+ return Compose([
77
+ Resize(n_px, interpolation=BICUBIC),
78
+ CenterCrop(n_px),
79
+ _convert_image_to_rgb,
80
+ ToTensor(),
81
+ Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)),
82
+ ])
83
+
84
+
85
+ def available_models() -> List[str]:
86
+ """Returns the names of available CLIP models"""
87
+ return list(_MODELS.keys())
88
+
89
+
90
+ def load(name: str, device: Union[str, torch.device] = "cuda" if torch.cuda.is_available() else "cpu", jit: bool = False, download_root: str = None):
91
+ """Load a CLIP model
92
+
93
+ Parameters
94
+ ----------
95
+ name : str
96
+ A model name listed by `clip.available_models()`, or the path to a model checkpoint containing the state_dict
97
+
98
+ device : Union[str, torch.device]
99
+ The device to put the loaded model
100
+
101
+ jit : bool
102
+ Whether to load the optimized JIT model or more hackable non-JIT model (default).
103
+
104
+ download_root: str
105
+ path to download the model files; by default, it uses "~/.cache/clip"
106
+
107
+ Returns
108
+ -------
109
+ model : torch.nn.Module
110
+ The CLIP model
111
+
112
+ preprocess : Callable[[PIL.Image], torch.Tensor]
113
+ A torchvision transform that converts a PIL image into a tensor that the returned model can take as its input
114
+ """
115
+ if name in _MODELS:
116
+ model_path = _download(_MODELS[name], download_root or os.path.expanduser("~/.cache/clip"))
117
+ elif os.path.isfile(name):
118
+ model_path = name
119
+ else:
120
+ raise RuntimeError(f"Model {name} not found; available models = {available_models()}")
121
+
122
+ with open(model_path, 'rb') as opened_file:
123
+ try:
124
+ # loading JIT archive
125
+ model = torch.jit.load(opened_file, map_location=device if jit else "cpu").eval()
126
+ state_dict = None
127
+ except RuntimeError:
128
+ # loading saved state dict
129
+ if jit:
130
+ warnings.warn(f"File {model_path} is not a JIT archive. Loading as a state dict instead")
131
+ jit = False
132
+ state_dict = torch.load(opened_file, map_location="cpu")
133
+
134
+ if not jit:
135
+ model = build_model(state_dict or model.state_dict()).to(device)
136
+ if str(device) == "cpu":
137
+ model.float()
138
+ return model, _transform(model.visual.input_resolution)
139
+
140
+ # patch the device names
141
+ device_holder = torch.jit.trace(lambda: torch.ones([]).to(torch.device(device)), example_inputs=[])
142
+ device_node = [n for n in device_holder.graph.findAllNodes("prim::Constant") if "Device" in repr(n)][-1]
143
+
144
+ def _node_get(node: torch._C.Node, key: str):
145
+ """Gets attributes of a node which is polymorphic over return type.
146
+
147
+ From https://github.com/pytorch/pytorch/pull/82628
148
+ """
149
+ sel = node.kindOf(key)
150
+ return getattr(node, sel)(key)
151
+
152
+ def patch_device(module):
153
+ try:
154
+ graphs = [module.graph] if hasattr(module, "graph") else []
155
+ except RuntimeError:
156
+ graphs = []
157
+
158
+ if hasattr(module, "forward1"):
159
+ graphs.append(module.forward1.graph)
160
+
161
+ for graph in graphs:
162
+ for node in graph.findAllNodes("prim::Constant"):
163
+ if "value" in node.attributeNames() and str(_node_get(node, "value")).startswith("cuda"):
164
+ node.copyAttributes(device_node)
165
+
166
+ model.apply(patch_device)
167
+ patch_device(model.encode_image)
168
+ patch_device(model.encode_text)
169
+
170
+ # patch dtype to float32 on CPU
171
+ if str(device) == "cpu":
172
+ float_holder = torch.jit.trace(lambda: torch.ones([]).float(), example_inputs=[])
173
+ float_input = list(float_holder.graph.findNode("aten::to").inputs())[1]
174
+ float_node = float_input.node()
175
+
176
+ def patch_float(module):
177
+ try:
178
+ graphs = [module.graph] if hasattr(module, "graph") else []
179
+ except RuntimeError:
180
+ graphs = []
181
+
182
+ if hasattr(module, "forward1"):
183
+ graphs.append(module.forward1.graph)
184
+
185
+ for graph in graphs:
186
+ for node in graph.findAllNodes("aten::to"):
187
+ inputs = list(node.inputs())
188
+ for i in [1, 2]: # dtype can be the second or third argument to aten::to()
189
+ if _node_get(inputs[i].node(), "value") == 5:
190
+ inputs[i].node().copyAttributes(float_node)
191
+
192
+ model.apply(patch_float)
193
+ patch_float(model.encode_image)
194
+ patch_float(model.encode_text)
195
+
196
+ model.float()
197
+
198
+ return model, _transform(model.input_resolution.item())
199
+
200
+
201
+ def tokenize(texts: Union[str, List[str]], context_length: int = 77, truncate: bool = False) -> Union[torch.IntTensor, torch.LongTensor]:
202
+ """
203
+ Returns the tokenized representation of given input string(s)
204
+
205
+ Parameters
206
+ ----------
207
+ texts : Union[str, List[str]]
208
+ An input string or a list of input strings to tokenize
209
+
210
+ context_length : int
211
+ The context length to use; all CLIP models use 77 as the context length
212
+
213
+ truncate: bool
214
+ Whether to truncate the text in case its encoding is longer than the context length
215
+
216
+ Returns
217
+ -------
218
+ A two-dimensional tensor containing the resulting tokens, shape = [number of input strings, context_length].
219
+ We return LongTensor when torch version is <1.8.0, since older index_select requires indices to be long.
220
+ """
221
+ if isinstance(texts, str):
222
+ texts = [texts]
223
+
224
+ sot_token = _tokenizer.encoder["<|startoftext|>"]
225
+ eot_token = _tokenizer.encoder["<|endoftext|>"]
226
+ all_tokens = [[sot_token] + _tokenizer.encode(text) + [eot_token] for text in texts]
227
+ #if packaging.version.parse(torch.__version__) < packaging.version.parse("1.8.0"):
228
+ # result = torch.zeros(len(all_tokens), context_length, dtype=torch.long)
229
+ #else:
230
+ result = torch.zeros(len(all_tokens), context_length, dtype=torch.int)
231
+
232
+ for i, tokens in enumerate(all_tokens):
233
+ if len(tokens) > context_length:
234
+ if truncate:
235
+ tokens = tokens[:context_length]
236
+ tokens[-1] = eot_token
237
+ else:
238
+ raise RuntimeError(f"Input {texts[i]} is too long for context length {context_length}")
239
+ result[i, :len(tokens)] = torch.tensor(tokens)
240
+
241
+ return result
clip/clipseg.py ADDED
@@ -0,0 +1,538 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ from os.path import basename, dirname, join, isfile
3
+ import torch
4
+ from torch import nn
5
+ from torch.nn import functional as nnf
6
+ from torch.nn.modules.activation import ReLU
7
+
8
+
9
+ def get_prompt_list(prompt):
10
+ if prompt == 'plain':
11
+ return ['{}']
12
+ elif prompt == 'fixed':
13
+ return ['a photo of a {}.']
14
+ elif prompt == 'shuffle':
15
+ return ['a photo of a {}.', 'a photograph of a {}.', 'an image of a {}.', '{}.']
16
+ elif prompt == 'shuffle+':
17
+ return ['a photo of a {}.', 'a photograph of a {}.', 'an image of a {}.', '{}.',
18
+ 'a cropped photo of a {}.', 'a good photo of a {}.', 'a photo of one {}.',
19
+ 'a bad photo of a {}.', 'a photo of the {}.']
20
+ else:
21
+ raise ValueError('Invalid value for prompt')
22
+
23
+
24
+ def forward_multihead_attention(x, b, with_aff=False, attn_mask=None):
25
+ """
26
+ Simplified version of multihead attention (taken from torch source code but without tons of if clauses).
27
+ The mlp and layer norm come from CLIP.
28
+ x: input.
29
+ b: multihead attention module.
30
+ """
31
+
32
+ x_ = b.ln_1(x)
33
+ q, k, v = nnf.linear(x_, b.attn.in_proj_weight, b.attn.in_proj_bias).chunk(3, dim=-1)
34
+ tgt_len, bsz, embed_dim = q.size()
35
+
36
+ head_dim = embed_dim // b.attn.num_heads
37
+ scaling = float(head_dim) ** -0.5
38
+
39
+ q = q.contiguous().view(tgt_len, bsz * b.attn.num_heads, b.attn.head_dim).transpose(0, 1)
40
+ k = k.contiguous().view(-1, bsz * b.attn.num_heads, b.attn.head_dim).transpose(0, 1)
41
+ v = v.contiguous().view(-1, bsz * b.attn.num_heads, b.attn.head_dim).transpose(0, 1)
42
+
43
+ q = q * scaling
44
+
45
+ attn_output_weights = torch.bmm(q, k.transpose(1, 2)) # n_heads * batch_size, tokens^2, tokens^2
46
+ if attn_mask is not None:
47
+
48
+
49
+ attn_mask_type, attn_mask = attn_mask
50
+ n_heads = attn_output_weights.size(0) // attn_mask.size(0)
51
+ attn_mask = attn_mask.repeat(n_heads, 1)
52
+
53
+ if attn_mask_type == 'cls_token':
54
+ # the mask only affects similarities compared to the readout-token.
55
+ attn_output_weights[:, 0, 1:] = attn_output_weights[:, 0, 1:] * attn_mask[None,...]
56
+ # attn_output_weights[:, 0, 0] = 0*attn_output_weights[:, 0, 0]
57
+
58
+ if attn_mask_type == 'all':
59
+ # print(attn_output_weights.shape, attn_mask[:, None].shape)
60
+ attn_output_weights[:, 1:, 1:] = attn_output_weights[:, 1:, 1:] * attn_mask[:, None]
61
+
62
+
63
+ attn_output_weights = torch.softmax(attn_output_weights, dim=-1)
64
+
65
+ attn_output = torch.bmm(attn_output_weights, v)
66
+ attn_output = attn_output.transpose(0, 1).contiguous().view(tgt_len, bsz, embed_dim)
67
+ attn_output = b.attn.out_proj(attn_output)
68
+
69
+ x = x + attn_output
70
+ x = x + b.mlp(b.ln_2(x))
71
+
72
+ if with_aff:
73
+ return x, attn_output_weights
74
+ else:
75
+ return x
76
+
77
+
78
+ class CLIPDenseBase(nn.Module):
79
+
80
+ def __init__(self, version, reduce_cond, reduce_dim, prompt, n_tokens):
81
+ super().__init__()
82
+
83
+ import clip
84
+
85
+ # prec = torch.FloatTensor
86
+ self.clip_model, _ = clip.load(version, device='cpu', jit=False)
87
+ self.model = self.clip_model.visual
88
+
89
+ # if not None, scale conv weights such that we obtain n_tokens.
90
+ self.n_tokens = n_tokens
91
+
92
+ for p in self.clip_model.parameters():
93
+ p.requires_grad_(False)
94
+
95
+ # conditional
96
+ if reduce_cond is not None:
97
+ self.reduce_cond = nn.Linear(512, reduce_cond)
98
+ for p in self.reduce_cond.parameters():
99
+ p.requires_grad_(False)
100
+ else:
101
+ self.reduce_cond = None
102
+
103
+ self.film_mul = nn.Linear(512 if reduce_cond is None else reduce_cond, reduce_dim)
104
+ self.film_add = nn.Linear(512 if reduce_cond is None else reduce_cond, reduce_dim)
105
+
106
+ self.reduce = nn.Linear(768, reduce_dim)
107
+
108
+ self.prompt_list = get_prompt_list(prompt)
109
+
110
+ # precomputed prompts
111
+ import pickle
112
+ if isfile('precomputed_prompt_vectors.pickle'):
113
+ precomp = pickle.load(open('precomputed_prompt_vectors.pickle', 'rb'))
114
+ self.precomputed_prompts = {k: torch.from_numpy(v) for k, v in precomp.items()}
115
+ else:
116
+ self.precomputed_prompts = dict()
117
+
118
+ def rescaled_pos_emb(self, new_size):
119
+ assert len(new_size) == 2
120
+
121
+ a = self.model.positional_embedding[1:].T.view(1, 768, *self.token_shape)
122
+ b = nnf.interpolate(a, new_size, mode='bicubic', align_corners=False).squeeze(0).view(768, new_size[0]*new_size[1]).T
123
+ return torch.cat([self.model.positional_embedding[:1], b])
124
+
125
+ def visual_forward(self, x_inp, extract_layers=(), skip=False, mask=None):
126
+
127
+
128
+ with torch.no_grad():
129
+
130
+ inp_size = x_inp.shape[2:]
131
+
132
+ if self.n_tokens is not None:
133
+ stride2 = x_inp.shape[2] // self.n_tokens
134
+ conv_weight2 = nnf.interpolate(self.model.conv1.weight, (stride2, stride2), mode='bilinear', align_corners=True)
135
+ x = nnf.conv2d(x_inp, conv_weight2, bias=self.model.conv1.bias, stride=stride2, dilation=self.model.conv1.dilation)
136
+ else:
137
+ x = self.model.conv1(x_inp) # shape = [*, width, grid, grid]
138
+
139
+ x = x.reshape(x.shape[0], x.shape[1], -1) # shape = [*, width, grid ** 2]
140
+ x = x.permute(0, 2, 1) # shape = [*, grid ** 2, width]
141
+
142
+ x = torch.cat([self.model.class_embedding.to(x.dtype) + torch.zeros(x.shape[0], 1, x.shape[-1], dtype=x.dtype, device=x.device), x], dim=1) # shape = [*, grid ** 2 + 1, width]
143
+
144
+ standard_n_tokens = 50 if self.model.conv1.kernel_size[0] == 32 else 197
145
+
146
+ if x.shape[1] != standard_n_tokens:
147
+ new_shape = int(math.sqrt(x.shape[1]-1))
148
+ x = x + self.rescaled_pos_emb((new_shape, new_shape)).to(x.dtype)[None,:,:]
149
+ else:
150
+ x = x + self.model.positional_embedding.to(x.dtype)
151
+
152
+ x = self.model.ln_pre(x)
153
+
154
+ x = x.permute(1, 0, 2) # NLD -> LND
155
+
156
+ activations, affinities = [], []
157
+ for i, res_block in enumerate(self.model.transformer.resblocks):
158
+
159
+ if mask is not None:
160
+ mask_layer, mask_type, mask_tensor = mask
161
+ if mask_layer == i or mask_layer == 'all':
162
+ # import ipdb; ipdb.set_trace()
163
+ size = int(math.sqrt(x.shape[0] - 1))
164
+
165
+ attn_mask = (mask_type, nnf.interpolate(mask_tensor.unsqueeze(1).float(), (size, size)).view(mask_tensor.shape[0], size * size))
166
+
167
+ else:
168
+ attn_mask = None
169
+ else:
170
+ attn_mask = None
171
+
172
+ x, aff_per_head = forward_multihead_attention(x, res_block, with_aff=True, attn_mask=attn_mask)
173
+
174
+ if i in extract_layers:
175
+ affinities += [aff_per_head]
176
+
177
+ #if self.n_tokens is not None:
178
+ # activations += [nnf.interpolate(x, inp_size, mode='bilinear', align_corners=True)]
179
+ #else:
180
+ activations += [x]
181
+
182
+ if len(extract_layers) > 0 and i == max(extract_layers) and skip:
183
+ print('early skip')
184
+ break
185
+
186
+ x = x.permute(1, 0, 2) # LND -> NLD
187
+ x = self.model.ln_post(x[:, 0, :])
188
+
189
+ if self.model.proj is not None:
190
+ x = x @ self.model.proj
191
+
192
+ return x, activations, affinities
193
+
194
+ def sample_prompts(self, words, prompt_list=None):
195
+
196
+ prompt_list = prompt_list if prompt_list is not None else self.prompt_list
197
+
198
+ prompt_indices = torch.multinomial(torch.ones(len(prompt_list)), len(words), replacement=True)
199
+ prompts = [prompt_list[i] for i in prompt_indices]
200
+ return [promt.format(w) for promt, w in zip(prompts, words)]
201
+
202
+ def get_cond_vec(self, conditional, batch_size):
203
+ # compute conditional from a single string
204
+ if conditional is not None and type(conditional) == str:
205
+ cond = self.compute_conditional(conditional)
206
+ cond = cond.repeat(batch_size, 1)
207
+
208
+ # compute conditional from string list/tuple
209
+ elif conditional is not None and type(conditional) in {list, tuple} and type(conditional[0]) == str:
210
+ assert len(conditional) == batch_size
211
+ cond = self.compute_conditional(conditional)
212
+
213
+ # use conditional directly
214
+ elif conditional is not None and type(conditional) == torch.Tensor and conditional.ndim == 2:
215
+ cond = conditional
216
+
217
+ # compute conditional from image
218
+ elif conditional is not None and type(conditional) == torch.Tensor:
219
+ with torch.no_grad():
220
+ cond, _, _ = self.visual_forward(conditional)
221
+ else:
222
+ raise ValueError('invalid conditional')
223
+ return cond
224
+
225
+ def compute_conditional(self, conditional):
226
+ import clip
227
+
228
+ dev = next(self.parameters()).device
229
+
230
+ if type(conditional) in {list, tuple}:
231
+ text_tokens = clip.tokenize(conditional).to(dev)
232
+ cond = self.clip_model.encode_text(text_tokens)
233
+ else:
234
+ if conditional in self.precomputed_prompts:
235
+ cond = self.precomputed_prompts[conditional].float().to(dev)
236
+ else:
237
+ text_tokens = clip.tokenize([conditional]).to(dev)
238
+ cond = self.clip_model.encode_text(text_tokens)[0]
239
+
240
+ if self.shift_vector is not None:
241
+ return cond + self.shift_vector
242
+ else:
243
+ return cond
244
+
245
+
246
+ def clip_load_untrained(version):
247
+ assert version == 'ViT-B/16'
248
+ from clip.model import CLIP
249
+ from clip.clip import _MODELS, _download
250
+ model = torch.jit.load(_download(_MODELS['ViT-B/16'])).eval()
251
+ state_dict = model.state_dict()
252
+
253
+ vision_width = state_dict["visual.conv1.weight"].shape[0]
254
+ vision_layers = len([k for k in state_dict.keys() if k.startswith("visual.") and k.endswith(".attn.in_proj_weight")])
255
+ vision_patch_size = state_dict["visual.conv1.weight"].shape[-1]
256
+ grid_size = round((state_dict["visual.positional_embedding"].shape[0] - 1) ** 0.5)
257
+ image_resolution = vision_patch_size * grid_size
258
+ embed_dim = state_dict["text_projection"].shape[1]
259
+ context_length = state_dict["positional_embedding"].shape[0]
260
+ vocab_size = state_dict["token_embedding.weight"].shape[0]
261
+ transformer_width = state_dict["ln_final.weight"].shape[0]
262
+ transformer_heads = transformer_width // 64
263
+ transformer_layers = len(set(k.split(".")[2] for k in state_dict if k.startswith(f"transformer.resblocks")))
264
+
265
+ return CLIP(embed_dim, image_resolution, vision_layers, vision_width, vision_patch_size,
266
+ context_length, vocab_size, transformer_width, transformer_heads, transformer_layers)
267
+
268
+
269
+ class CLIPDensePredT(CLIPDenseBase):
270
+
271
+ def __init__(self, version='ViT-B/32', extract_layers=(3, 6, 9), cond_layer=0, reduce_dim=128, n_heads=4, prompt='fixed',
272
+ extra_blocks=0, reduce_cond=None, fix_shift=False,
273
+ learn_trans_conv_only=False, limit_to_clip_only=False, upsample=False,
274
+ add_calibration=False, rev_activations=False, trans_conv=None, n_tokens=None, complex_trans_conv=False):
275
+
276
+ super().__init__(version, reduce_cond, reduce_dim, prompt, n_tokens)
277
+ # device = 'cpu'
278
+
279
+ self.extract_layers = extract_layers
280
+ self.cond_layer = cond_layer
281
+ self.limit_to_clip_only = limit_to_clip_only
282
+ self.process_cond = None
283
+ self.rev_activations = rev_activations
284
+
285
+ depth = len(extract_layers)
286
+
287
+ if add_calibration:
288
+ self.calibration_conds = 1
289
+
290
+ self.upsample_proj = nn.Conv2d(reduce_dim, 1, kernel_size=1) if upsample else None
291
+
292
+ self.add_activation1 = True
293
+
294
+ self.version = version
295
+
296
+ self.token_shape = {'ViT-B/32': (7, 7), 'ViT-B/16': (14, 14)}[version]
297
+
298
+ if fix_shift:
299
+ # self.shift_vector = nn.Parameter(torch.load(join(dirname(basename(__file__)), 'clip_text_shift_vector.pth')), requires_grad=False)
300
+ self.shift_vector = nn.Parameter(torch.load(join(dirname(basename(__file__)), 'shift_text_to_vis.pth')), requires_grad=False)
301
+ # self.shift_vector = nn.Parameter(-1*torch.load(join(dirname(basename(__file__)), 'shift2.pth')), requires_grad=False)
302
+ else:
303
+ self.shift_vector = None
304
+
305
+ if trans_conv is None:
306
+ trans_conv_ks = {'ViT-B/32': (32, 32), 'ViT-B/16': (16, 16)}[version]
307
+ else:
308
+ # explicitly define transposed conv kernel size
309
+ trans_conv_ks = (trans_conv, trans_conv)
310
+
311
+ if not complex_trans_conv:
312
+ self.trans_conv = nn.ConvTranspose2d(reduce_dim, 1, trans_conv_ks, stride=trans_conv_ks)
313
+ else:
314
+ assert trans_conv_ks[0] == trans_conv_ks[1]
315
+
316
+ tp_kernels = (trans_conv_ks[0] // 4, trans_conv_ks[0] // 4)
317
+
318
+ self.trans_conv = nn.Sequential(
319
+ nn.Conv2d(reduce_dim, reduce_dim, kernel_size=3, padding=1),
320
+ nn.ReLU(),
321
+ nn.ConvTranspose2d(reduce_dim, reduce_dim // 2, kernel_size=tp_kernels[0], stride=tp_kernels[0]),
322
+ nn.ReLU(),
323
+ nn.ConvTranspose2d(reduce_dim // 2, 1, kernel_size=tp_kernels[1], stride=tp_kernels[1]),
324
+ )
325
+
326
+ # self.trans_conv = nn.ConvTranspose2d(reduce_dim, 1, trans_conv_ks, stride=trans_conv_ks)
327
+
328
+ assert len(self.extract_layers) == depth
329
+
330
+ self.reduces = nn.ModuleList([nn.Linear(768, reduce_dim) for _ in range(depth)])
331
+ self.blocks = nn.ModuleList([nn.TransformerEncoderLayer(d_model=reduce_dim, nhead=n_heads) for _ in range(len(self.extract_layers))])
332
+ self.extra_blocks = nn.ModuleList([nn.TransformerEncoderLayer(d_model=reduce_dim, nhead=n_heads) for _ in range(extra_blocks)])
333
+
334
+ # refinement and trans conv
335
+
336
+ if learn_trans_conv_only:
337
+ for p in self.parameters():
338
+ p.requires_grad_(False)
339
+
340
+ for p in self.trans_conv.parameters():
341
+ p.requires_grad_(True)
342
+
343
+ self.prompt_list = get_prompt_list(prompt)
344
+
345
+
346
+ def forward(self, inp_image, conditional=None, return_features=False, mask=None):
347
+
348
+ assert type(return_features) == bool
349
+
350
+ inp_image = inp_image.to(self.model.positional_embedding.device)
351
+
352
+ if mask is not None:
353
+ raise ValueError('mask not supported')
354
+
355
+ # x_inp = normalize(inp_image)
356
+ x_inp = inp_image
357
+
358
+ bs, dev = inp_image.shape[0], x_inp.device
359
+
360
+ cond = self.get_cond_vec(conditional, bs)
361
+
362
+ visual_q, activations, _ = self.visual_forward(x_inp, extract_layers=[0] + list(self.extract_layers))
363
+
364
+ activation1 = activations[0]
365
+ activations = activations[1:]
366
+
367
+ _activations = activations[::-1] if not self.rev_activations else activations
368
+
369
+ a = None
370
+ for i, (activation, block, reduce) in enumerate(zip(_activations, self.blocks, self.reduces)):
371
+
372
+ if a is not None:
373
+ a = reduce(activation) + a
374
+ else:
375
+ a = reduce(activation)
376
+
377
+ if i == self.cond_layer:
378
+ if self.reduce_cond is not None:
379
+ cond = self.reduce_cond(cond)
380
+
381
+ a = self.film_mul(cond) * a + self.film_add(cond)
382
+
383
+ a = block(a)
384
+
385
+ for block in self.extra_blocks:
386
+ a = a + block(a)
387
+
388
+ a = a[1:].permute(1, 2, 0) # rm cls token and -> BS, Feats, Tokens
389
+
390
+ size = int(math.sqrt(a.shape[2]))
391
+
392
+ a = a.view(bs, a.shape[1], size, size)
393
+
394
+ a = self.trans_conv(a)
395
+
396
+ if self.n_tokens is not None:
397
+ a = nnf.interpolate(a, x_inp.shape[2:], mode='bilinear', align_corners=True)
398
+
399
+ if self.upsample_proj is not None:
400
+ a = self.upsample_proj(a)
401
+ a = nnf.interpolate(a, x_inp.shape[2:], mode='bilinear')
402
+
403
+ if return_features:
404
+ return a, visual_q, cond, [activation1] + activations
405
+ else:
406
+ return a,
407
+
408
+
409
+
410
+ class CLIPDensePredTMasked(CLIPDensePredT):
411
+
412
+ def __init__(self, version='ViT-B/32', extract_layers=(3, 6, 9), cond_layer=0, reduce_dim=128, n_heads=4,
413
+ prompt='fixed', extra_blocks=0, reduce_cond=None, fix_shift=False, learn_trans_conv_only=False,
414
+ refine=None, limit_to_clip_only=False, upsample=False, add_calibration=False, n_tokens=None):
415
+
416
+ super().__init__(version=version, extract_layers=extract_layers, cond_layer=cond_layer, reduce_dim=reduce_dim,
417
+ n_heads=n_heads, prompt=prompt, extra_blocks=extra_blocks, reduce_cond=reduce_cond,
418
+ fix_shift=fix_shift, learn_trans_conv_only=learn_trans_conv_only,
419
+ limit_to_clip_only=limit_to_clip_only, upsample=upsample, add_calibration=add_calibration,
420
+ n_tokens=n_tokens)
421
+
422
+ def visual_forward_masked(self, img_s, seg_s):
423
+ return super().visual_forward(img_s, mask=('all', 'cls_token', seg_s))
424
+
425
+ def forward(self, img_q, cond_or_img_s, seg_s=None, return_features=False):
426
+
427
+ if seg_s is None:
428
+ cond = cond_or_img_s
429
+ else:
430
+ img_s = cond_or_img_s
431
+
432
+ with torch.no_grad():
433
+ cond, _, _ = self.visual_forward_masked(img_s, seg_s)
434
+
435
+ return super().forward(img_q, cond, return_features=return_features)
436
+
437
+
438
+
439
+ class CLIPDenseBaseline(CLIPDenseBase):
440
+
441
+ def __init__(self, version='ViT-B/32', cond_layer=0,
442
+ extract_layer=9, reduce_dim=128, reduce2_dim=None, prompt='fixed',
443
+ reduce_cond=None, limit_to_clip_only=False, n_tokens=None):
444
+
445
+ super().__init__(version, reduce_cond, reduce_dim, prompt, n_tokens)
446
+ device = 'cpu'
447
+
448
+ # self.cond_layer = cond_layer
449
+ self.extract_layer = extract_layer
450
+ self.limit_to_clip_only = limit_to_clip_only
451
+ self.shift_vector = None
452
+
453
+ self.token_shape = {'ViT-B/32': (7, 7), 'ViT-B/16': (14, 14)}[version]
454
+
455
+ assert reduce2_dim is not None
456
+
457
+ self.reduce2 = nn.Sequential(
458
+ nn.Linear(reduce_dim, reduce2_dim),
459
+ nn.ReLU(),
460
+ nn.Linear(reduce2_dim, reduce_dim)
461
+ )
462
+
463
+ trans_conv_ks = {'ViT-B/32': (32, 32), 'ViT-B/16': (16, 16)}[version]
464
+ self.trans_conv = nn.ConvTranspose2d(reduce_dim, 1, trans_conv_ks, stride=trans_conv_ks)
465
+
466
+
467
+ def forward(self, inp_image, conditional=None, return_features=False):
468
+
469
+ inp_image = inp_image.to(self.model.positional_embedding.device)
470
+
471
+ # x_inp = normalize(inp_image)
472
+ x_inp = inp_image
473
+
474
+ bs, dev = inp_image.shape[0], x_inp.device
475
+
476
+ cond = self.get_cond_vec(conditional, bs)
477
+
478
+ visual_q, activations, affinities = self.visual_forward(x_inp, extract_layers=[self.extract_layer])
479
+
480
+ a = activations[0]
481
+ a = self.reduce(a)
482
+ a = self.film_mul(cond) * a + self.film_add(cond)
483
+
484
+ if self.reduce2 is not None:
485
+ a = self.reduce2(a)
486
+
487
+ # the original model would execute a transformer block here
488
+
489
+ a = a[1:].permute(1, 2, 0) # rm cls token and -> BS, Feats, Tokens
490
+
491
+ size = int(math.sqrt(a.shape[2]))
492
+
493
+ a = a.view(bs, a.shape[1], size, size)
494
+ a = self.trans_conv(a)
495
+
496
+ if return_features:
497
+ return a, visual_q, cond, activations
498
+ else:
499
+ return a,
500
+
501
+
502
+ class CLIPSegMultiLabel(nn.Module):
503
+
504
+ def __init__(self, model) -> None:
505
+ super().__init__()
506
+
507
+ from third_party.JoEm.data_loader import get_seen_idx, get_unseen_idx, VOC
508
+
509
+ self.pascal_classes = VOC
510
+
511
+ from clip.clipseg import CLIPDensePredT
512
+ from general_utils import load_model
513
+ # self.clipseg = load_model('rd64-vit16-neg0.2-phrasecut', strict=False)
514
+ self.clipseg = load_model(model, strict=False)
515
+
516
+ self.clipseg.eval()
517
+
518
+ def forward(self, x):
519
+
520
+ bs = x.shape[0]
521
+ out = torch.ones(21, bs, 352, 352).to(x.device) * -10
522
+
523
+ for class_id, class_name in enumerate(self.pascal_classes):
524
+
525
+ fac = 3 if class_name == 'background' else 1
526
+
527
+ with torch.no_grad():
528
+ pred = torch.sigmoid(self.clipseg(x, class_name)[0][:,0]) * fac
529
+
530
+ out[class_id] += pred
531
+
532
+
533
+ out = out.permute(1, 0, 2, 3)
534
+
535
+ return out
536
+
537
+ # construct output tensor
538
+
clip/model.py ADDED
@@ -0,0 +1,436 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections import OrderedDict
2
+ from typing import Tuple, Union
3
+
4
+ import numpy as np
5
+ import torch
6
+ import torch.nn.functional as F
7
+ from torch import nn
8
+
9
+
10
+ class Bottleneck(nn.Module):
11
+ expansion = 4
12
+
13
+ def __init__(self, inplanes, planes, stride=1):
14
+ super().__init__()
15
+
16
+ # all conv layers have stride 1. an avgpool is performed after the second convolution when stride > 1
17
+ self.conv1 = nn.Conv2d(inplanes, planes, 1, bias=False)
18
+ self.bn1 = nn.BatchNorm2d(planes)
19
+ self.relu1 = nn.ReLU(inplace=True)
20
+
21
+ self.conv2 = nn.Conv2d(planes, planes, 3, padding=1, bias=False)
22
+ self.bn2 = nn.BatchNorm2d(planes)
23
+ self.relu2 = nn.ReLU(inplace=True)
24
+
25
+ self.avgpool = nn.AvgPool2d(stride) if stride > 1 else nn.Identity()
26
+
27
+ self.conv3 = nn.Conv2d(planes, planes * self.expansion, 1, bias=False)
28
+ self.bn3 = nn.BatchNorm2d(planes * self.expansion)
29
+ self.relu3 = nn.ReLU(inplace=True)
30
+
31
+ self.downsample = None
32
+ self.stride = stride
33
+
34
+ if stride > 1 or inplanes != planes * Bottleneck.expansion:
35
+ # downsampling layer is prepended with an avgpool, and the subsequent convolution has stride 1
36
+ self.downsample = nn.Sequential(OrderedDict([
37
+ ("-1", nn.AvgPool2d(stride)),
38
+ ("0", nn.Conv2d(inplanes, planes * self.expansion, 1, stride=1, bias=False)),
39
+ ("1", nn.BatchNorm2d(planes * self.expansion))
40
+ ]))
41
+
42
+ def forward(self, x: torch.Tensor):
43
+ identity = x
44
+
45
+ out = self.relu1(self.bn1(self.conv1(x)))
46
+ out = self.relu2(self.bn2(self.conv2(out)))
47
+ out = self.avgpool(out)
48
+ out = self.bn3(self.conv3(out))
49
+
50
+ if self.downsample is not None:
51
+ identity = self.downsample(x)
52
+
53
+ out += identity
54
+ out = self.relu3(out)
55
+ return out
56
+
57
+
58
+ class AttentionPool2d(nn.Module):
59
+ def __init__(self, spacial_dim: int, embed_dim: int, num_heads: int, output_dim: int = None):
60
+ super().__init__()
61
+ self.positional_embedding = nn.Parameter(torch.randn(spacial_dim ** 2 + 1, embed_dim) / embed_dim ** 0.5)
62
+ self.k_proj = nn.Linear(embed_dim, embed_dim)
63
+ self.q_proj = nn.Linear(embed_dim, embed_dim)
64
+ self.v_proj = nn.Linear(embed_dim, embed_dim)
65
+ self.c_proj = nn.Linear(embed_dim, output_dim or embed_dim)
66
+ self.num_heads = num_heads
67
+
68
+ def forward(self, x):
69
+ x = x.flatten(start_dim=2).permute(2, 0, 1) # NCHW -> (HW)NC
70
+ x = torch.cat([x.mean(dim=0, keepdim=True), x], dim=0) # (HW+1)NC
71
+ x = x + self.positional_embedding[:, None, :].to(x.dtype) # (HW+1)NC
72
+ x, _ = F.multi_head_attention_forward(
73
+ query=x[:1], key=x, value=x,
74
+ embed_dim_to_check=x.shape[-1],
75
+ num_heads=self.num_heads,
76
+ q_proj_weight=self.q_proj.weight,
77
+ k_proj_weight=self.k_proj.weight,
78
+ v_proj_weight=self.v_proj.weight,
79
+ in_proj_weight=None,
80
+ in_proj_bias=torch.cat([self.q_proj.bias, self.k_proj.bias, self.v_proj.bias]),
81
+ bias_k=None,
82
+ bias_v=None,
83
+ add_zero_attn=False,
84
+ dropout_p=0,
85
+ out_proj_weight=self.c_proj.weight,
86
+ out_proj_bias=self.c_proj.bias,
87
+ use_separate_proj_weight=True,
88
+ training=self.training,
89
+ need_weights=False
90
+ )
91
+ return x.squeeze(0)
92
+
93
+
94
+ class ModifiedResNet(nn.Module):
95
+ """
96
+ A ResNet class that is similar to torchvision's but contains the following changes:
97
+ - There are now 3 "stem" convolutions as opposed to 1, with an average pool instead of a max pool.
98
+ - Performs anti-aliasing strided convolutions, where an avgpool is prepended to convolutions with stride > 1
99
+ - The final pooling layer is a QKV attention instead of an average pool
100
+ """
101
+
102
+ def __init__(self, layers, output_dim, heads, input_resolution=224, width=64):
103
+ super().__init__()
104
+ self.output_dim = output_dim
105
+ self.input_resolution = input_resolution
106
+
107
+ # the 3-layer stem
108
+ self.conv1 = nn.Conv2d(3, width // 2, kernel_size=3, stride=2, padding=1, bias=False)
109
+ self.bn1 = nn.BatchNorm2d(width // 2)
110
+ self.relu1 = nn.ReLU(inplace=True)
111
+ self.conv2 = nn.Conv2d(width // 2, width // 2, kernel_size=3, padding=1, bias=False)
112
+ self.bn2 = nn.BatchNorm2d(width // 2)
113
+ self.relu2 = nn.ReLU(inplace=True)
114
+ self.conv3 = nn.Conv2d(width // 2, width, kernel_size=3, padding=1, bias=False)
115
+ self.bn3 = nn.BatchNorm2d(width)
116
+ self.relu3 = nn.ReLU(inplace=True)
117
+ self.avgpool = nn.AvgPool2d(2)
118
+
119
+ # residual layers
120
+ self._inplanes = width # this is a *mutable* variable used during construction
121
+ self.layer1 = self._make_layer(width, layers[0])
122
+ self.layer2 = self._make_layer(width * 2, layers[1], stride=2)
123
+ self.layer3 = self._make_layer(width * 4, layers[2], stride=2)
124
+ self.layer4 = self._make_layer(width * 8, layers[3], stride=2)
125
+
126
+ embed_dim = width * 32 # the ResNet feature dimension
127
+ self.attnpool = AttentionPool2d(input_resolution // 32, embed_dim, heads, output_dim)
128
+
129
+ def _make_layer(self, planes, blocks, stride=1):
130
+ layers = [Bottleneck(self._inplanes, planes, stride)]
131
+
132
+ self._inplanes = planes * Bottleneck.expansion
133
+ for _ in range(1, blocks):
134
+ layers.append(Bottleneck(self._inplanes, planes))
135
+
136
+ return nn.Sequential(*layers)
137
+
138
+ def forward(self, x):
139
+ def stem(x):
140
+ x = self.relu1(self.bn1(self.conv1(x)))
141
+ x = self.relu2(self.bn2(self.conv2(x)))
142
+ x = self.relu3(self.bn3(self.conv3(x)))
143
+ x = self.avgpool(x)
144
+ return x
145
+
146
+ x = x.type(self.conv1.weight.dtype)
147
+ x = stem(x)
148
+ x = self.layer1(x)
149
+ x = self.layer2(x)
150
+ x = self.layer3(x)
151
+ x = self.layer4(x)
152
+ x = self.attnpool(x)
153
+
154
+ return x
155
+
156
+
157
+ class LayerNorm(nn.LayerNorm):
158
+ """Subclass torch's LayerNorm to handle fp16."""
159
+
160
+ def forward(self, x: torch.Tensor):
161
+ orig_type = x.dtype
162
+ ret = super().forward(x.type(torch.float32))
163
+ return ret.type(orig_type)
164
+
165
+
166
+ class QuickGELU(nn.Module):
167
+ def forward(self, x: torch.Tensor):
168
+ return x * torch.sigmoid(1.702 * x)
169
+
170
+
171
+ class ResidualAttentionBlock(nn.Module):
172
+ def __init__(self, d_model: int, n_head: int, attn_mask: torch.Tensor = None):
173
+ super().__init__()
174
+
175
+ self.attn = nn.MultiheadAttention(d_model, n_head)
176
+ self.ln_1 = LayerNorm(d_model)
177
+ self.mlp = nn.Sequential(OrderedDict([
178
+ ("c_fc", nn.Linear(d_model, d_model * 4)),
179
+ ("gelu", QuickGELU()),
180
+ ("c_proj", nn.Linear(d_model * 4, d_model))
181
+ ]))
182
+ self.ln_2 = LayerNorm(d_model)
183
+ self.attn_mask = attn_mask
184
+
185
+ def attention(self, x: torch.Tensor):
186
+ self.attn_mask = self.attn_mask.to(dtype=x.dtype, device=x.device) if self.attn_mask is not None else None
187
+ return self.attn(x, x, x, need_weights=False, attn_mask=self.attn_mask)[0]
188
+
189
+ def forward(self, x: torch.Tensor):
190
+ x = x + self.attention(self.ln_1(x))
191
+ x = x + self.mlp(self.ln_2(x))
192
+ return x
193
+
194
+
195
+ class Transformer(nn.Module):
196
+ def __init__(self, width: int, layers: int, heads: int, attn_mask: torch.Tensor = None):
197
+ super().__init__()
198
+ self.width = width
199
+ self.layers = layers
200
+ self.resblocks = nn.Sequential(*[ResidualAttentionBlock(width, heads, attn_mask) for _ in range(layers)])
201
+
202
+ def forward(self, x: torch.Tensor):
203
+ return self.resblocks(x)
204
+
205
+
206
+ class VisionTransformer(nn.Module):
207
+ def __init__(self, input_resolution: int, patch_size: int, width: int, layers: int, heads: int, output_dim: int):
208
+ super().__init__()
209
+ self.input_resolution = input_resolution
210
+ self.output_dim = output_dim
211
+ self.conv1 = nn.Conv2d(in_channels=3, out_channels=width, kernel_size=patch_size, stride=patch_size, bias=False)
212
+
213
+ scale = width ** -0.5
214
+ self.class_embedding = nn.Parameter(scale * torch.randn(width))
215
+ self.positional_embedding = nn.Parameter(scale * torch.randn((input_resolution // patch_size) ** 2 + 1, width))
216
+ self.ln_pre = LayerNorm(width)
217
+
218
+ self.transformer = Transformer(width, layers, heads)
219
+
220
+ self.ln_post = LayerNorm(width)
221
+ self.proj = nn.Parameter(scale * torch.randn(width, output_dim))
222
+
223
+ def forward(self, x: torch.Tensor):
224
+ x = self.conv1(x) # shape = [*, width, grid, grid]
225
+ x = x.reshape(x.shape[0], x.shape[1], -1) # shape = [*, width, grid ** 2]
226
+ x = x.permute(0, 2, 1) # shape = [*, grid ** 2, width]
227
+ x = torch.cat([self.class_embedding.to(x.dtype) + torch.zeros(x.shape[0], 1, x.shape[-1], dtype=x.dtype, device=x.device), x], dim=1) # shape = [*, grid ** 2 + 1, width]
228
+ x = x + self.positional_embedding.to(x.dtype)
229
+ x = self.ln_pre(x)
230
+
231
+ x = x.permute(1, 0, 2) # NLD -> LND
232
+ x = self.transformer(x)
233
+ x = x.permute(1, 0, 2) # LND -> NLD
234
+
235
+ x = self.ln_post(x[:, 0, :])
236
+
237
+ if self.proj is not None:
238
+ x = x @ self.proj
239
+
240
+ return x
241
+
242
+
243
+ class CLIP(nn.Module):
244
+ def __init__(self,
245
+ embed_dim: int,
246
+ # vision
247
+ image_resolution: int,
248
+ vision_layers: Union[Tuple[int, int, int, int], int],
249
+ vision_width: int,
250
+ vision_patch_size: int,
251
+ # text
252
+ context_length: int,
253
+ vocab_size: int,
254
+ transformer_width: int,
255
+ transformer_heads: int,
256
+ transformer_layers: int
257
+ ):
258
+ super().__init__()
259
+
260
+ self.context_length = context_length
261
+
262
+ if isinstance(vision_layers, (tuple, list)):
263
+ vision_heads = vision_width * 32 // 64
264
+ self.visual = ModifiedResNet(
265
+ layers=vision_layers,
266
+ output_dim=embed_dim,
267
+ heads=vision_heads,
268
+ input_resolution=image_resolution,
269
+ width=vision_width
270
+ )
271
+ else:
272
+ vision_heads = vision_width // 64
273
+ self.visual = VisionTransformer(
274
+ input_resolution=image_resolution,
275
+ patch_size=vision_patch_size,
276
+ width=vision_width,
277
+ layers=vision_layers,
278
+ heads=vision_heads,
279
+ output_dim=embed_dim
280
+ )
281
+
282
+ self.transformer = Transformer(
283
+ width=transformer_width,
284
+ layers=transformer_layers,
285
+ heads=transformer_heads,
286
+ attn_mask=self.build_attention_mask()
287
+ )
288
+
289
+ self.vocab_size = vocab_size
290
+ self.token_embedding = nn.Embedding(vocab_size, transformer_width)
291
+ self.positional_embedding = nn.Parameter(torch.empty(self.context_length, transformer_width))
292
+ self.ln_final = LayerNorm(transformer_width)
293
+
294
+ self.text_projection = nn.Parameter(torch.empty(transformer_width, embed_dim))
295
+ self.logit_scale = nn.Parameter(torch.ones([]) * np.log(1 / 0.07))
296
+
297
+ self.initialize_parameters()
298
+
299
+ def initialize_parameters(self):
300
+ nn.init.normal_(self.token_embedding.weight, std=0.02)
301
+ nn.init.normal_(self.positional_embedding, std=0.01)
302
+
303
+ if isinstance(self.visual, ModifiedResNet):
304
+ if self.visual.attnpool is not None:
305
+ std = self.visual.attnpool.c_proj.in_features ** -0.5
306
+ nn.init.normal_(self.visual.attnpool.q_proj.weight, std=std)
307
+ nn.init.normal_(self.visual.attnpool.k_proj.weight, std=std)
308
+ nn.init.normal_(self.visual.attnpool.v_proj.weight, std=std)
309
+ nn.init.normal_(self.visual.attnpool.c_proj.weight, std=std)
310
+
311
+ for resnet_block in [self.visual.layer1, self.visual.layer2, self.visual.layer3, self.visual.layer4]:
312
+ for name, param in resnet_block.named_parameters():
313
+ if name.endswith("bn3.weight"):
314
+ nn.init.zeros_(param)
315
+
316
+ proj_std = (self.transformer.width ** -0.5) * ((2 * self.transformer.layers) ** -0.5)
317
+ attn_std = self.transformer.width ** -0.5
318
+ fc_std = (2 * self.transformer.width) ** -0.5
319
+ for block in self.transformer.resblocks:
320
+ nn.init.normal_(block.attn.in_proj_weight, std=attn_std)
321
+ nn.init.normal_(block.attn.out_proj.weight, std=proj_std)
322
+ nn.init.normal_(block.mlp.c_fc.weight, std=fc_std)
323
+ nn.init.normal_(block.mlp.c_proj.weight, std=proj_std)
324
+
325
+ if self.text_projection is not None:
326
+ nn.init.normal_(self.text_projection, std=self.transformer.width ** -0.5)
327
+
328
+ def build_attention_mask(self):
329
+ # lazily create causal attention mask, with full attention between the vision tokens
330
+ # pytorch uses additive attention mask; fill with -inf
331
+ mask = torch.empty(self.context_length, self.context_length)
332
+ mask.fill_(float("-inf"))
333
+ mask.triu_(1) # zero out the lower diagonal
334
+ return mask
335
+
336
+ @property
337
+ def dtype(self):
338
+ return self.visual.conv1.weight.dtype
339
+
340
+ def encode_image(self, image):
341
+ return self.visual(image.type(self.dtype))
342
+
343
+ def encode_text(self, text):
344
+ x = self.token_embedding(text).type(self.dtype) # [batch_size, n_ctx, d_model]
345
+
346
+ x = x + self.positional_embedding.type(self.dtype)
347
+ x = x.permute(1, 0, 2) # NLD -> LND
348
+ x = self.transformer(x)
349
+ x = x.permute(1, 0, 2) # LND -> NLD
350
+ x = self.ln_final(x).type(self.dtype)
351
+
352
+ # x.shape = [batch_size, n_ctx, transformer.width]
353
+ # take features from the eot embedding (eot_token is the highest number in each sequence)
354
+ x = x[torch.arange(x.shape[0]), text.argmax(dim=-1)] @ self.text_projection
355
+
356
+ return x
357
+
358
+ def forward(self, image, text):
359
+ image_features = self.encode_image(image)
360
+ text_features = self.encode_text(text)
361
+
362
+ # normalized features
363
+ image_features = image_features / image_features.norm(dim=1, keepdim=True)
364
+ text_features = text_features / text_features.norm(dim=1, keepdim=True)
365
+
366
+ # cosine similarity as logits
367
+ logit_scale = self.logit_scale.exp()
368
+ logits_per_image = logit_scale * image_features @ text_features.t()
369
+ logits_per_text = logits_per_image.t()
370
+
371
+ # shape = [global_batch_size, global_batch_size]
372
+ return logits_per_image, logits_per_text
373
+
374
+
375
+ def convert_weights(model: nn.Module):
376
+ """Convert applicable model parameters to fp16"""
377
+
378
+ def _convert_weights_to_fp16(l):
379
+ if isinstance(l, (nn.Conv1d, nn.Conv2d, nn.Linear)):
380
+ l.weight.data = l.weight.data.half()
381
+ if l.bias is not None:
382
+ l.bias.data = l.bias.data.half()
383
+
384
+ if isinstance(l, nn.MultiheadAttention):
385
+ for attr in [*[f"{s}_proj_weight" for s in ["in", "q", "k", "v"]], "in_proj_bias", "bias_k", "bias_v"]:
386
+ tensor = getattr(l, attr)
387
+ if tensor is not None:
388
+ tensor.data = tensor.data.half()
389
+
390
+ for name in ["text_projection", "proj"]:
391
+ if hasattr(l, name):
392
+ attr = getattr(l, name)
393
+ if attr is not None:
394
+ attr.data = attr.data.half()
395
+
396
+ model.apply(_convert_weights_to_fp16)
397
+
398
+
399
+ def build_model(state_dict: dict):
400
+ vit = "visual.proj" in state_dict
401
+
402
+ if vit:
403
+ vision_width = state_dict["visual.conv1.weight"].shape[0]
404
+ vision_layers = len([k for k in state_dict.keys() if k.startswith("visual.") and k.endswith(".attn.in_proj_weight")])
405
+ vision_patch_size = state_dict["visual.conv1.weight"].shape[-1]
406
+ grid_size = round((state_dict["visual.positional_embedding"].shape[0] - 1) ** 0.5)
407
+ image_resolution = vision_patch_size * grid_size
408
+ else:
409
+ counts: list = [len(set(k.split(".")[2] for k in state_dict if k.startswith(f"visual.layer{b}"))) for b in [1, 2, 3, 4]]
410
+ vision_layers = tuple(counts)
411
+ vision_width = state_dict["visual.layer1.0.conv1.weight"].shape[0]
412
+ output_width = round((state_dict["visual.attnpool.positional_embedding"].shape[0] - 1) ** 0.5)
413
+ vision_patch_size = None
414
+ assert output_width ** 2 + 1 == state_dict["visual.attnpool.positional_embedding"].shape[0]
415
+ image_resolution = output_width * 32
416
+
417
+ embed_dim = state_dict["text_projection"].shape[1]
418
+ context_length = state_dict["positional_embedding"].shape[0]
419
+ vocab_size = state_dict["token_embedding.weight"].shape[0]
420
+ transformer_width = state_dict["ln_final.weight"].shape[0]
421
+ transformer_heads = transformer_width // 64
422
+ transformer_layers = len(set(k.split(".")[2] for k in state_dict if k.startswith("transformer.resblocks")))
423
+
424
+ model = CLIP(
425
+ embed_dim,
426
+ image_resolution, vision_layers, vision_width, vision_patch_size,
427
+ context_length, vocab_size, transformer_width, transformer_heads, transformer_layers
428
+ )
429
+
430
+ for key in ["input_resolution", "context_length", "vocab_size"]:
431
+ if key in state_dict:
432
+ del state_dict[key]
433
+
434
+ convert_weights(model)
435
+ model.load_state_dict(state_dict)
436
+ return model.eval()
clip/simple_tokenizer.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gzip
2
+ import html
3
+ import os
4
+ from functools import lru_cache
5
+
6
+ import ftfy
7
+ import regex as re
8
+
9
+
10
+ @lru_cache()
11
+ def default_bpe():
12
+ return os.path.join(os.path.dirname(os.path.abspath(__file__)), "bpe_simple_vocab_16e6.txt.gz")
13
+
14
+
15
+ @lru_cache()
16
+ def bytes_to_unicode():
17
+ """
18
+ Returns list of utf-8 byte and a corresponding list of unicode strings.
19
+ The reversible bpe codes work on unicode strings.
20
+ This means you need a large # of unicode characters in your vocab if you want to avoid UNKs.
21
+ When you're at something like a 10B token dataset you end up needing around 5K for decent coverage.
22
+ This is a signficant percentage of your normal, say, 32K bpe vocab.
23
+ To avoid that, we want lookup tables between utf-8 bytes and unicode strings.
24
+ And avoids mapping to whitespace/control characters the bpe code barfs on.
25
+ """
26
+ bs = list(range(ord("!"), ord("~")+1))+list(range(ord("¡"), ord("¬")+1))+list(range(ord("®"), ord("ÿ")+1))
27
+ cs = bs[:]
28
+ n = 0
29
+ for b in range(2**8):
30
+ if b not in bs:
31
+ bs.append(b)
32
+ cs.append(2**8+n)
33
+ n += 1
34
+ cs = [chr(n) for n in cs]
35
+ return dict(zip(bs, cs))
36
+
37
+
38
+ def get_pairs(word):
39
+ """Return set of symbol pairs in a word.
40
+ Word is represented as tuple of symbols (symbols being variable-length strings).
41
+ """
42
+ pairs = set()
43
+ prev_char = word[0]
44
+ for char in word[1:]:
45
+ pairs.add((prev_char, char))
46
+ prev_char = char
47
+ return pairs
48
+
49
+
50
+ def basic_clean(text):
51
+ text = ftfy.fix_text(text)
52
+ text = html.unescape(html.unescape(text))
53
+ return text.strip()
54
+
55
+
56
+ def whitespace_clean(text):
57
+ text = re.sub(r'\s+', ' ', text)
58
+ text = text.strip()
59
+ return text
60
+
61
+
62
+ class SimpleTokenizer(object):
63
+ def __init__(self, bpe_path: str = default_bpe()):
64
+ self.byte_encoder = bytes_to_unicode()
65
+ self.byte_decoder = {v: k for k, v in self.byte_encoder.items()}
66
+ merges = gzip.open(bpe_path).read().decode("utf-8").split('\n')
67
+ merges = merges[1:49152-256-2+1]
68
+ merges = [tuple(merge.split()) for merge in merges]
69
+ vocab = list(bytes_to_unicode().values())
70
+ vocab = vocab + [v+'</w>' for v in vocab]
71
+ for merge in merges:
72
+ vocab.append(''.join(merge))
73
+ vocab.extend(['<|startoftext|>', '<|endoftext|>'])
74
+ self.encoder = dict(zip(vocab, range(len(vocab))))
75
+ self.decoder = {v: k for k, v in self.encoder.items()}
76
+ self.bpe_ranks = dict(zip(merges, range(len(merges))))
77
+ self.cache = {'<|startoftext|>': '<|startoftext|>', '<|endoftext|>': '<|endoftext|>'}
78
+ self.pat = re.compile(r"""<\|startoftext\|>|<\|endoftext\|>|'s|'t|'re|'ve|'m|'ll|'d|[\p{L}]+|[\p{N}]|[^\s\p{L}\p{N}]+""", re.IGNORECASE)
79
+
80
+ def bpe(self, token):
81
+ if token in self.cache:
82
+ return self.cache[token]
83
+ word = tuple(token[:-1]) + ( token[-1] + '</w>',)
84
+ pairs = get_pairs(word)
85
+
86
+ if not pairs:
87
+ return token+'</w>'
88
+
89
+ while True:
90
+ bigram = min(pairs, key = lambda pair: self.bpe_ranks.get(pair, float('inf')))
91
+ if bigram not in self.bpe_ranks:
92
+ break
93
+ first, second = bigram
94
+ new_word = []
95
+ i = 0
96
+ while i < len(word):
97
+ try:
98
+ j = word.index(first, i)
99
+ new_word.extend(word[i:j])
100
+ i = j
101
+ except:
102
+ new_word.extend(word[i:])
103
+ break
104
+
105
+ if word[i] == first and i < len(word)-1 and word[i+1] == second:
106
+ new_word.append(first+second)
107
+ i += 2
108
+ else:
109
+ new_word.append(word[i])
110
+ i += 1
111
+ new_word = tuple(new_word)
112
+ word = new_word
113
+ if len(word) == 1:
114
+ break
115
+ else:
116
+ pairs = get_pairs(word)
117
+ word = ' '.join(word)
118
+ self.cache[token] = word
119
+ return word
120
+
121
+ def encode(self, text):
122
+ bpe_tokens = []
123
+ text = whitespace_clean(basic_clean(text)).lower()
124
+ for token in re.findall(self.pat, text):
125
+ token = ''.join(self.byte_encoder[b] for b in token.encode('utf-8'))
126
+ bpe_tokens.extend(self.encoder[bpe_token] for bpe_token in self.bpe(token).split(' '))
127
+ return bpe_tokens
128
+
129
+ def decode(self, tokens):
130
+ text = ''.join([self.decoder[token] for token in tokens])
131
+ text = bytearray([self.byte_decoder[c] for c in text]).decode('utf-8', errors="replace").replace('</w>', ' ')
132
+ return text
clip/vitseg.py ADDED
@@ -0,0 +1,286 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ from posixpath import basename, dirname, join
3
+ # import clip
4
+ from clip.model import convert_weights
5
+ import torch
6
+ import json
7
+ from torch import nn
8
+ from torch.nn import functional as nnf
9
+ from torch.nn.modules import activation
10
+ from torch.nn.modules.activation import ReLU
11
+ from torchvision import transforms
12
+
13
+ normalize = transforms.Normalize(mean=(0.48145466, 0.4578275, 0.40821073), std=(0.26862954, 0.26130258, 0.27577711))
14
+
15
+ from torchvision.models import ResNet
16
+
17
+
18
+ def process_prompts(conditional, prompt_list, conditional_map):
19
+ # DEPRECATED
20
+
21
+ # randomly sample a synonym
22
+ words = [conditional_map[int(i)] for i in conditional]
23
+ words = [syns[torch.multinomial(torch.ones(len(syns)), 1, replacement=True).item()] for syns in words]
24
+ words = [w.replace('_', ' ') for w in words]
25
+
26
+ if prompt_list is not None:
27
+ prompt_indices = torch.multinomial(torch.ones(len(prompt_list)), len(words), replacement=True)
28
+ prompts = [prompt_list[i] for i in prompt_indices]
29
+ else:
30
+ prompts = ['a photo of {}'] * (len(words))
31
+
32
+ return [promt.format(w) for promt, w in zip(prompts, words)]
33
+
34
+
35
+ class VITDenseBase(nn.Module):
36
+
37
+ def rescaled_pos_emb(self, new_size):
38
+ assert len(new_size) == 2
39
+
40
+ a = self.model.positional_embedding[1:].T.view(1, 768, *self.token_shape)
41
+ b = nnf.interpolate(a, new_size, mode='bicubic', align_corners=False).squeeze(0).view(768, new_size[0]*new_size[1]).T
42
+ return torch.cat([self.model.positional_embedding[:1], b])
43
+
44
+ def visual_forward(self, x_inp, extract_layers=(), skip=False, mask=None):
45
+
46
+ with torch.no_grad():
47
+
48
+ x_inp = nnf.interpolate(x_inp, (384, 384))
49
+
50
+ x = self.model.patch_embed(x_inp)
51
+ cls_token = self.model.cls_token.expand(x.shape[0], -1, -1) # stole cls_tokens impl from Phil Wang, thanks
52
+ if self.model.dist_token is None:
53
+ x = torch.cat((cls_token, x), dim=1)
54
+ else:
55
+ x = torch.cat((cls_token, self.model.dist_token.expand(x.shape[0], -1, -1), x), dim=1)
56
+ x = self.model.pos_drop(x + self.model.pos_embed)
57
+
58
+ activations = []
59
+ for i, block in enumerate(self.model.blocks):
60
+ x = block(x)
61
+
62
+ if i in extract_layers:
63
+ # permute to be compatible with CLIP
64
+ activations += [x.permute(1,0,2)]
65
+
66
+ x = self.model.norm(x)
67
+ x = self.model.head(self.model.pre_logits(x[:, 0]))
68
+
69
+ # again for CLIP compatibility
70
+ # x = x.permute(1, 0, 2)
71
+
72
+ return x, activations, None
73
+
74
+ def sample_prompts(self, words, prompt_list=None):
75
+
76
+ prompt_list = prompt_list if prompt_list is not None else self.prompt_list
77
+
78
+ prompt_indices = torch.multinomial(torch.ones(len(prompt_list)), len(words), replacement=True)
79
+ prompts = [prompt_list[i] for i in prompt_indices]
80
+ return [promt.format(w) for promt, w in zip(prompts, words)]
81
+
82
+ def get_cond_vec(self, conditional, batch_size):
83
+ # compute conditional from a single string
84
+ if conditional is not None and type(conditional) == str:
85
+ cond = self.compute_conditional(conditional)
86
+ cond = cond.repeat(batch_size, 1)
87
+
88
+ # compute conditional from string list/tuple
89
+ elif conditional is not None and type(conditional) in {list, tuple} and type(conditional[0]) == str:
90
+ assert len(conditional) == batch_size
91
+ cond = self.compute_conditional(conditional)
92
+
93
+ # use conditional directly
94
+ elif conditional is not None and type(conditional) == torch.Tensor and conditional.ndim == 2:
95
+ cond = conditional
96
+
97
+ # compute conditional from image
98
+ elif conditional is not None and type(conditional) == torch.Tensor:
99
+ with torch.no_grad():
100
+ cond, _, _ = self.visual_forward(conditional)
101
+ else:
102
+ raise ValueError('invalid conditional')
103
+ return cond
104
+
105
+ def compute_conditional(self, conditional):
106
+ import clip
107
+
108
+ dev = next(self.parameters()).device
109
+
110
+ if type(conditional) in {list, tuple}:
111
+ text_tokens = clip.tokenize(conditional).to(dev)
112
+ cond = self.clip_model.encode_text(text_tokens)
113
+ else:
114
+ if conditional in self.precomputed_prompts:
115
+ cond = self.precomputed_prompts[conditional].float().to(dev)
116
+ else:
117
+ text_tokens = clip.tokenize([conditional]).to(dev)
118
+ cond = self.clip_model.encode_text(text_tokens)[0]
119
+
120
+ return cond
121
+
122
+
123
+ class VITDensePredT(VITDenseBase):
124
+
125
+ def __init__(self, extract_layers=(3, 6, 9), cond_layer=0, reduce_dim=128, n_heads=4, prompt='fixed',
126
+ depth=3, extra_blocks=0, reduce_cond=None, fix_shift=False,
127
+ learn_trans_conv_only=False, refine=None, limit_to_clip_only=False, upsample=False,
128
+ add_calibration=False, process_cond=None, not_pretrained=False):
129
+ super().__init__()
130
+ # device = 'cpu'
131
+
132
+ self.extract_layers = extract_layers
133
+ self.cond_layer = cond_layer
134
+ self.limit_to_clip_only = limit_to_clip_only
135
+ self.process_cond = None
136
+
137
+ if add_calibration:
138
+ self.calibration_conds = 1
139
+
140
+ self.upsample_proj = nn.Conv2d(reduce_dim, 1, kernel_size=1) if upsample else None
141
+
142
+ self.add_activation1 = True
143
+
144
+ import timm
145
+ self.model = timm.create_model('vit_base_patch16_384', pretrained=True)
146
+ self.model.head = nn.Linear(768, 512 if reduce_cond is None else reduce_cond)
147
+
148
+ for p in self.model.parameters():
149
+ p.requires_grad_(False)
150
+
151
+ import clip
152
+ self.clip_model, _ = clip.load('ViT-B/16', device='cpu', jit=False)
153
+ # del self.clip_model.visual
154
+
155
+
156
+ self.token_shape = (14, 14)
157
+
158
+ # conditional
159
+ if reduce_cond is not None:
160
+ self.reduce_cond = nn.Linear(512, reduce_cond)
161
+ for p in self.reduce_cond.parameters():
162
+ p.requires_grad_(False)
163
+ else:
164
+ self.reduce_cond = None
165
+
166
+ # self.film = AVAILABLE_BLOCKS['film'](512, 128)
167
+ self.film_mul = nn.Linear(512 if reduce_cond is None else reduce_cond, reduce_dim)
168
+ self.film_add = nn.Linear(512 if reduce_cond is None else reduce_cond, reduce_dim)
169
+
170
+ # DEPRECATED
171
+ # self.conditional_map = {c['id']: c['synonyms'] for c in json.load(open(cond_map))}
172
+
173
+ assert len(self.extract_layers) == depth
174
+
175
+ self.reduces = nn.ModuleList([nn.Linear(768, reduce_dim) for _ in range(depth)])
176
+ self.blocks = nn.ModuleList([nn.TransformerEncoderLayer(d_model=reduce_dim, nhead=n_heads) for _ in range(len(self.extract_layers))])
177
+ self.extra_blocks = nn.ModuleList([nn.TransformerEncoderLayer(d_model=reduce_dim, nhead=n_heads) for _ in range(extra_blocks)])
178
+
179
+ trans_conv_ks = (16, 16)
180
+ self.trans_conv = nn.ConvTranspose2d(reduce_dim, 1, trans_conv_ks, stride=trans_conv_ks)
181
+
182
+ # refinement and trans conv
183
+
184
+ if learn_trans_conv_only:
185
+ for p in self.parameters():
186
+ p.requires_grad_(False)
187
+
188
+ for p in self.trans_conv.parameters():
189
+ p.requires_grad_(True)
190
+
191
+ if prompt == 'fixed':
192
+ self.prompt_list = ['a photo of a {}.']
193
+ elif prompt == 'shuffle':
194
+ self.prompt_list = ['a photo of a {}.', 'a photograph of a {}.', 'an image of a {}.', '{}.']
195
+ elif prompt == 'shuffle+':
196
+ self.prompt_list = ['a photo of a {}.', 'a photograph of a {}.', 'an image of a {}.', '{}.',
197
+ 'a cropped photo of a {}.', 'a good photo of a {}.', 'a photo of one {}.',
198
+ 'a bad photo of a {}.', 'a photo of the {}.']
199
+ elif prompt == 'shuffle_clip':
200
+ from models.clip_prompts import imagenet_templates
201
+ self.prompt_list = imagenet_templates
202
+
203
+ if process_cond is not None:
204
+ if process_cond == 'clamp' or process_cond[0] == 'clamp':
205
+
206
+ val = process_cond[1] if type(process_cond) in {list, tuple} else 0.2
207
+
208
+ def clamp_vec(x):
209
+ return torch.clamp(x, -val, val)
210
+
211
+ self.process_cond = clamp_vec
212
+
213
+ elif process_cond.endswith('.pth'):
214
+
215
+ shift = torch.load(process_cond)
216
+ def add_shift(x):
217
+ return x + shift.to(x.device)
218
+
219
+ self.process_cond = add_shift
220
+
221
+ import pickle
222
+ precomp = pickle.load(open('precomputed_prompt_vectors.pickle', 'rb'))
223
+ self.precomputed_prompts = {k: torch.from_numpy(v) for k, v in precomp.items()}
224
+
225
+
226
+ def forward(self, inp_image, conditional=None, return_features=False, mask=None):
227
+
228
+ assert type(return_features) == bool
229
+
230
+ # inp_image = inp_image.to(self.model.positional_embedding.device)
231
+
232
+ if mask is not None:
233
+ raise ValueError('mask not supported')
234
+
235
+ # x_inp = normalize(inp_image)
236
+ x_inp = inp_image
237
+
238
+ bs, dev = inp_image.shape[0], x_inp.device
239
+
240
+ inp_image_size = inp_image.shape[2:]
241
+
242
+ cond = self.get_cond_vec(conditional, bs)
243
+
244
+ visual_q, activations, _ = self.visual_forward(x_inp, extract_layers=[0] + list(self.extract_layers))
245
+
246
+ activation1 = activations[0]
247
+ activations = activations[1:]
248
+
249
+ a = None
250
+ for i, (activation, block, reduce) in enumerate(zip(activations[::-1], self.blocks, self.reduces)):
251
+
252
+ if a is not None:
253
+ a = reduce(activation) + a
254
+ else:
255
+ a = reduce(activation)
256
+
257
+ if i == self.cond_layer:
258
+ if self.reduce_cond is not None:
259
+ cond = self.reduce_cond(cond)
260
+
261
+ a = self.film_mul(cond) * a + self.film_add(cond)
262
+
263
+ a = block(a)
264
+
265
+ for block in self.extra_blocks:
266
+ a = a + block(a)
267
+
268
+ a = a[1:].permute(1, 2, 0) # rm cls token and -> BS, Feats, Tokens
269
+
270
+ size = int(math.sqrt(a.shape[2]))
271
+
272
+ a = a.view(bs, a.shape[1], size, size)
273
+
274
+ if self.trans_conv is not None:
275
+ a = self.trans_conv(a)
276
+
277
+ if self.upsample_proj is not None:
278
+ a = self.upsample_proj(a)
279
+ a = nnf.interpolate(a, x_inp.shape[2:], mode='bilinear')
280
+
281
+ a = nnf.interpolate(a, inp_image_size)
282
+
283
+ if return_features:
284
+ return a, visual_q, cond, [activation1] + activations
285
+ else:
286
+ return a,
ui/globals.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ui_restart_server = False
2
+
3
+ SELECTION_FACES_DATA = None
4
+ ui_SELECTED_INPUT_FACE_INDEX = 0
5
+
6
+ ui_selected_enhancer = None
7
+ ui_upscale = None
8
+ ui_blend_ratio = None
9
+ ui_input_thumbs = []
10
+ ui_target_thumbs = []
11
+ ui_camera_frame = None
12
+
13
+
14
+
15
+
16
+
ui/main.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import gradio as gr
4
+ import roop.globals
5
+ import roop.metadata
6
+ import roop.utilities as util
7
+ import ui.globals as uii
8
+
9
+ from ui.tabs.faceswap_tab import faceswap_tab
10
+ from ui.tabs.livecam_tab import livecam_tab
11
+ from ui.tabs.facemgr_tab import facemgr_tab
12
+ from ui.tabs.extras_tab import extras_tab
13
+ from ui.tabs.settings_tab import settings_tab
14
+
15
+ roop.globals.keep_fps = None
16
+ roop.globals.keep_frames = None
17
+ roop.globals.skip_audio = None
18
+ roop.globals.use_batch = None
19
+
20
+
21
+ def prepare_environment():
22
+ roop.globals.output_path = os.path.abspath(os.path.join(os.getcwd(), "output"))
23
+ os.makedirs(roop.globals.output_path, exist_ok=True)
24
+ if not roop.globals.CFG.use_os_temp_folder:
25
+ os.environ["TEMP"] = os.environ["TMP"] = os.path.abspath(os.path.join(os.getcwd(), "temp"))
26
+ os.makedirs(os.environ["TEMP"], exist_ok=True)
27
+ os.environ["GRADIO_TEMP_DIR"] = os.environ["TEMP"]
28
+ os.environ['GRADIO_ANALYTICS_ENABLED'] = '0'
29
+
30
+ def run():
31
+ from roop.core import decode_execution_providers, set_display_ui
32
+
33
+ prepare_environment()
34
+
35
+ set_display_ui(show_msg)
36
+ if roop.globals.CFG.provider == "cuda" and util.has_cuda_device() == False:
37
+ roop.globals.CFG.provider = "cpu"
38
+
39
+ roop.globals.execution_providers = decode_execution_providers([roop.globals.CFG.provider])
40
+ gputype = util.get_device()
41
+ if gputype == 'cuda':
42
+ util.print_cuda_info()
43
+
44
+ print(f'Using provider {roop.globals.execution_providers} - Device:{gputype}')
45
+
46
+ run_server = True
47
+ uii.ui_restart_server = False
48
+ mycss = """
49
+ span {color: var(--block-info-text-color)}
50
+ #fixedheight {
51
+ max-height: 238.4px;
52
+ overflow-y: auto !important;
53
+ }
54
+ .image-container.svelte-1l6wqyv {height: 100%}
55
+
56
+ """
57
+
58
+ while run_server:
59
+ server_name = roop.globals.CFG.server_name
60
+ if server_name is None or len(server_name) < 1:
61
+ server_name = None
62
+ server_port = roop.globals.CFG.server_port
63
+ if server_port <= 0:
64
+ server_port = None
65
+ ssl_verify = False if server_name == '0.0.0.0' else True
66
+ with gr.Blocks(title=f'{roop.metadata.name} {roop.metadata.version}', theme=roop.globals.CFG.selected_theme, css=mycss, delete_cache=(60, 86400)) as ui:
67
+ with gr.Row(variant='compact'):
68
+ gr.Markdown(f"### [{roop.metadata.name} {roop.metadata.version}](https://github.com/C0untFloyd/roop-unleashed)")
69
+ gr.HTML(util.create_version_html(), elem_id="versions")
70
+ faceswap_tab()
71
+ livecam_tab()
72
+ facemgr_tab()
73
+ extras_tab()
74
+ settings_tab()
75
+ launch_browser = roop.globals.CFG.launch_browser
76
+
77
+ uii.ui_restart_server = False
78
+ try:
79
+ ui.queue().launch(inbrowser=launch_browser, server_name=server_name, server_port=server_port, share=roop.globals.CFG.server_share, ssl_verify=ssl_verify, prevent_thread_lock=True, show_error=True)
80
+ except Exception as e:
81
+ print(f'Exception {e} when launching Gradio Server!')
82
+ uii.ui_restart_server = True
83
+ run_server = False
84
+ try:
85
+ while uii.ui_restart_server == False:
86
+ time.sleep(1.0)
87
+
88
+ except (KeyboardInterrupt, OSError):
89
+ print("Keyboard interruption in main thread... closing server.")
90
+ run_server = False
91
+ ui.close()
92
+
93
+
94
+ def show_msg(msg: str):
95
+ gr.Info(msg)
96
+
ui/tabs/extras_tab.py ADDED
@@ -0,0 +1,245 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import shutil
4
+ import roop.utilities as util
5
+ import roop.util_ffmpeg as ffmpeg
6
+ import roop.globals
7
+ from roop.utilities import clean_dir
8
+
9
+ frame_filters_map = {
10
+ "Colorize B/W Images (Deoldify Artistic)" : {"colorizer" : {"subtype": "deoldify_artistic"}},
11
+ "Colorize B/W Images (Deoldify Stable)" : {"colorizer" : {"subtype": "deoldify_stable"}},
12
+ "Background remove" : {"removebg" : {"subtype": ""}},
13
+ "Filter Stylize" : {"filter_generic" : {"subtype" : "stylize" }},
14
+ "Filter Detail Enhance" : {"filter_generic" : {"subtype" : "detailenhance" }},
15
+ "Filter Pencil Sketch" : {"filter_generic" : {"subtype" : "pencil" }},
16
+ "Filter Cartoon" : {"filter_generic" : {"subtype" : "cartoon" }},
17
+ "Filter C64" : {"filter_generic" : {"subtype" : "C64" }}
18
+ }
19
+
20
+ frame_upscalers_map = {
21
+ "ESRGAN x2" : {"upscale" : {"subtype": "esrganx2"}},
22
+ "ESRGAN x4" : {"upscale" : {"subtype": "esrganx4"}},
23
+ "LSDIR x4" : {"upscale" : {"subtype": "lsdirx4"}}
24
+ }
25
+
26
+ def extras_tab():
27
+ filternames = ["None"]
28
+ for f in frame_filters_map.keys():
29
+ filternames.append(f)
30
+ upscalernames = ["None"]
31
+ for f in frame_upscalers_map.keys():
32
+ upscalernames.append(f)
33
+
34
+ with gr.Tab("🎉 Extras"):
35
+ with gr.Row():
36
+ files_to_process = gr.Files(label='File(s) to process', file_count="multiple", file_types=["image", "video"])
37
+ with gr.Row(variant='panel'):
38
+ with gr.Accordion(label="Video/GIF", open=False):
39
+ with gr.Row(variant='panel'):
40
+ with gr.Column():
41
+ gr.Markdown("""
42
+ # Poor man's video editor
43
+ Re-encoding uses your configuration from the Settings Tab.
44
+ """)
45
+ with gr.Column():
46
+ cut_start_time = gr.Slider(0, 1000000, value=0, label="Start Frame", step=1.0, interactive=True)
47
+ with gr.Column():
48
+ cut_end_time = gr.Slider(1, 1000000, value=1, label="End Frame", step=1.0, interactive=True)
49
+ with gr.Column():
50
+ extras_chk_encode = gr.Checkbox(label='Re-encode videos (necessary for videos with different codecs)', value=False)
51
+ start_cut_video = gr.Button("Cut video")
52
+ start_extract_frames = gr.Button("Extract frames")
53
+ start_join_videos = gr.Button("Join videos")
54
+
55
+ with gr.Row(variant='panel'):
56
+ with gr.Column():
57
+ gr.Markdown("""
58
+ # Create video/gif from images
59
+ """)
60
+ with gr.Column():
61
+ extras_fps = gr.Slider(minimum=0, maximum=120, value=30, label="Video FPS", step=1.0, interactive=True)
62
+ extras_images_folder = gr.Textbox(show_label=False, placeholder="/content/", interactive=True)
63
+ with gr.Column():
64
+ extras_chk_creategif = gr.Checkbox(label='Create GIF from video', value=False)
65
+ extras_create_video=gr.Button("Create")
66
+ with gr.Row(variant='panel'):
67
+ with gr.Column():
68
+ gr.Markdown("""
69
+ # Create video from gif
70
+ """)
71
+ with gr.Column():
72
+ extras_video_fps = gr.Slider(minimum=0, maximum=120, value=0, label="Video FPS", step=1.0, interactive=True)
73
+ with gr.Column():
74
+ extras_create_video_from_gif=gr.Button("Create")
75
+ with gr.Row(variant='panel'):
76
+ with gr.Column(scale=2):
77
+ gr.Markdown("""
78
+ # Repair video
79
+
80
+ Uses FFMpeg to fix corrupt videos.
81
+ """)
82
+ with gr.Column():
83
+ extras_repair_video=gr.Button("Repair")
84
+
85
+
86
+ with gr.Row(variant='panel'):
87
+ with gr.Accordion(label="Full frame processing", open=True):
88
+ with gr.Row(variant='panel'):
89
+ filterselection = gr.Dropdown(filternames, value="None", label="Colorizer/FilterFX", interactive=True)
90
+ upscalerselection = gr.Dropdown(upscalernames, value="None", label="Enhancer", interactive=True)
91
+ with gr.Row(variant='panel'):
92
+ start_frame_process=gr.Button("Start processing")
93
+
94
+ with gr.Row():
95
+ gr.Button("👀 Open Output Folder", size='sm').click(fn=lambda: util.open_folder(roop.globals.output_path))
96
+ with gr.Row():
97
+ extra_files_output = gr.Files(label='Resulting output files', file_count="multiple")
98
+
99
+ start_cut_video.click(fn=on_cut_video, inputs=[files_to_process, cut_start_time, cut_end_time, extras_chk_encode], outputs=[extra_files_output])
100
+ start_extract_frames.click(fn=on_extras_extract_frames, inputs=[files_to_process], outputs=[extra_files_output])
101
+ start_join_videos.click(fn=on_join_videos, inputs=[files_to_process, extras_chk_encode], outputs=[extra_files_output])
102
+ extras_create_video.click(fn=on_extras_create_video, inputs=[files_to_process, extras_images_folder, extras_fps, extras_chk_creategif], outputs=[extra_files_output])
103
+ extras_create_video_from_gif.click(fn=on_extras_create_video_from_gif, inputs=[files_to_process, extras_video_fps], outputs=[extra_files_output])
104
+ extras_repair_video.click(fn=on_extras_repair_video, inputs=[files_to_process], outputs=[extra_files_output])
105
+ start_frame_process.click(fn=on_frame_process, inputs=[files_to_process, filterselection, upscalerselection], outputs=[extra_files_output])
106
+
107
+
108
+ def on_cut_video(files, cut_start_frame, cut_end_frame, reencode):
109
+ if files is None:
110
+ return None
111
+
112
+ resultfiles = []
113
+ for tf in files:
114
+ f = tf.name
115
+ destfile = util.get_destfilename_from_path(f, roop.globals.output_path, '_cut')
116
+ ffmpeg.cut_video(f, destfile, cut_start_frame, cut_end_frame, reencode)
117
+ if os.path.isfile(destfile):
118
+ resultfiles.append(destfile)
119
+ else:
120
+ gr.Error('Cutting video failed!')
121
+ return resultfiles
122
+
123
+
124
+ def on_join_videos(files, chk_encode):
125
+ if files is None:
126
+ return None
127
+
128
+ filenames = []
129
+ for f in files:
130
+ filenames.append(f.name)
131
+ destfile = util.get_destfilename_from_path(filenames[0], roop.globals.output_path, '_join')
132
+ sorted_filenames = util.sort_filenames_ignore_path(filenames)
133
+ ffmpeg.join_videos(sorted_filenames, destfile, not chk_encode)
134
+ resultfiles = []
135
+ if os.path.isfile(destfile):
136
+ resultfiles.append(destfile)
137
+ else:
138
+ gr.Error('Joining videos failed!')
139
+ return resultfiles
140
+
141
+ def on_extras_create_video_from_gif(files,fps):
142
+ if files is None:
143
+ return None
144
+
145
+ filenames = []
146
+ resultfiles = []
147
+ for f in files:
148
+ filenames.append(f.name)
149
+
150
+ destfilename = os.path.join(roop.globals.output_path, "img2video." + roop.globals.CFG.output_video_format)
151
+ ffmpeg.create_video_from_gif(filenames[0], destfilename)
152
+ if os.path.isfile(destfilename):
153
+ resultfiles.append(destfilename)
154
+ return resultfiles
155
+
156
+
157
+ def on_extras_repair_video(files):
158
+ if files is None:
159
+ return None
160
+
161
+ resultfiles = []
162
+ for tf in files:
163
+ f = tf.name
164
+ destfile = util.get_destfilename_from_path(f, roop.globals.output_path, '_repair')
165
+ ffmpeg.repair_video(f, destfile)
166
+ if os.path.isfile(destfile):
167
+ resultfiles.append(destfile)
168
+ else:
169
+ gr.Error('Repairing video failed!')
170
+ return resultfiles
171
+
172
+
173
+
174
+
175
+
176
+ def on_extras_create_video(files, images_path,fps, create_gif):
177
+ if images_path is None:
178
+ return None
179
+ resultfiles = []
180
+ if len(files) > 0 and util.is_video(files[0]) and create_gif:
181
+ destfilename = files[0]
182
+ else:
183
+ util.sort_rename_frames(os.path.dirname(images_path))
184
+ destfilename = os.path.join(roop.globals.output_path, "img2video." + roop.globals.CFG.output_video_format)
185
+ ffmpeg.create_video('', destfilename, fps, images_path)
186
+ if os.path.isfile(destfilename):
187
+ resultfiles.append(destfilename)
188
+ else:
189
+ return None
190
+ if create_gif:
191
+ gifname = util.get_destfilename_from_path(destfilename, './output', '.gif')
192
+ ffmpeg.create_gif_from_video(destfilename, gifname)
193
+ if os.path.isfile(destfilename):
194
+ resultfiles.append(gifname)
195
+ return resultfiles
196
+
197
+
198
+ def on_extras_extract_frames(files):
199
+ if files is None:
200
+ return None
201
+
202
+ resultfiles = []
203
+ for tf in files:
204
+ f = tf.name
205
+ resfolder = ffmpeg.extract_frames(f)
206
+ for file in os.listdir(resfolder):
207
+ outfile = os.path.join(resfolder, file)
208
+ if os.path.isfile(outfile):
209
+ resultfiles.append(outfile)
210
+ return resultfiles
211
+
212
+
213
+ def on_frame_process(files, filterselection, upscaleselection):
214
+ import pathlib
215
+ from roop.core import batch_process_with_options
216
+ from roop.ProcessEntry import ProcessEntry
217
+ from roop.ProcessOptions import ProcessOptions
218
+ from ui.main import prepare_environment
219
+
220
+
221
+ if files is None:
222
+ return None
223
+
224
+ if roop.globals.CFG.clear_output:
225
+ clean_dir(roop.globals.output_path)
226
+ prepare_environment()
227
+ list_files_process : list[ProcessEntry] = []
228
+
229
+ for tf in files:
230
+ list_files_process.append(ProcessEntry(tf.name, 0,0, 0))
231
+
232
+ processoroptions = {}
233
+ filter = next((x for x in frame_filters_map.keys() if x == filterselection), None)
234
+ if filter is not None:
235
+ processoroptions.update(frame_filters_map[filter])
236
+ filter = next((x for x in frame_upscalers_map.keys() if x == upscaleselection), None)
237
+ if filter is not None:
238
+ processoroptions.update(frame_upscalers_map[filter])
239
+ options = ProcessOptions(processoroptions, 0, 0, "all", 0, None, None, 0, 128, False, False)
240
+ batch_process_with_options(list_files_process, options, None)
241
+ outdir = pathlib.Path(roop.globals.output_path)
242
+ outfiles = [str(item) for item in outdir.rglob("*") if item.is_file()]
243
+ return outfiles
244
+
245
+
ui/tabs/facemgr_tab.py ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import cv2
4
+ import gradio as gr
5
+ import roop.utilities as util
6
+ import roop.globals
7
+ from roop.face_util import extract_face_images
8
+ from roop.capturer import get_video_frame, get_video_frame_total
9
+ from typing import List, Tuple, Optional
10
+ from roop.typing import Frame, Face, FaceSet
11
+
12
+ selected_face_index = -1
13
+ thumbs = []
14
+ images = []
15
+
16
+
17
+ def facemgr_tab() -> None:
18
+ with gr.Tab("👨‍👩‍👧‍👦 Face Management"):
19
+ with gr.Row():
20
+ gr.Markdown("""
21
+ # Create blending facesets
22
+ Add multiple reference images into a faceset file.
23
+ """)
24
+ with gr.Row():
25
+ videoimagefst = gr.Image(label="Cut face from video frame", height=576, interactive=False, visible=True, format="jpeg")
26
+ with gr.Row():
27
+ frame_num_fst = gr.Slider(1, 1, value=1, label="Frame Number", info='0:00:00', step=1.0, interactive=False)
28
+ fb_cutfromframe = gr.Button("Use faces from this frame", variant='secondary', interactive=False)
29
+ with gr.Row():
30
+ fb_facesetfile = gr.Files(label='Faceset', file_count='single', file_types=['.fsz'], interactive=True)
31
+ fb_files = gr.Files(label='Input Files', file_count="multiple", file_types=["image", "video"], interactive=True)
32
+ with gr.Row():
33
+ with gr.Column():
34
+ gr.Button("👀 Open Output Folder", size='sm').click(fn=lambda: util.open_folder(roop.globals.output_path))
35
+ with gr.Column():
36
+ gr.Markdown(' ')
37
+ with gr.Row():
38
+ faces = gr.Gallery(label="Faces in this Faceset", allow_preview=True, preview=True, height=128, object_fit="scale-down")
39
+ with gr.Row():
40
+ fb_remove = gr.Button("Remove selected", variant='secondary')
41
+ fb_update = gr.Button("Create/Update Faceset file", variant='primary')
42
+ fb_clear = gr.Button("Clear all", variant='stop')
43
+
44
+ fb_facesetfile.change(fn=on_faceset_changed, inputs=[fb_facesetfile], outputs=[faces])
45
+ fb_files.change(fn=on_fb_files_changed, inputs=[fb_files], outputs=[faces, videoimagefst, frame_num_fst, fb_cutfromframe])
46
+ fb_update.click(fn=on_update_clicked, outputs=[fb_facesetfile])
47
+ fb_remove.click(fn=on_remove_clicked, outputs=[faces])
48
+ fb_clear.click(fn=on_clear_clicked, outputs=[faces, fb_files, fb_facesetfile])
49
+ fb_cutfromframe.click(fn=on_cutfromframe_clicked, inputs=[fb_files, frame_num_fst], outputs=[faces])
50
+ frame_num_fst.release(fn=on_frame_num_fst_changed, inputs=[fb_files, frame_num_fst], outputs=[videoimagefst])
51
+ faces.select(fn=on_face_selected)
52
+
53
+
54
+ def on_faceset_changed(faceset, progress=gr.Progress()) -> List[Frame]:
55
+ global thumbs, images
56
+
57
+ if faceset is None:
58
+ return thumbs
59
+
60
+ thumbs.clear()
61
+ filename = faceset.name
62
+
63
+ if filename.lower().endswith('fsz'):
64
+ progress(0, desc="Retrieving faces from Faceset File", )
65
+ unzipfolder = os.path.join(os.environ["TEMP"], 'faceset')
66
+ if os.path.isdir(unzipfolder):
67
+ shutil.rmtree(unzipfolder)
68
+ util.mkdir_with_umask(unzipfolder)
69
+ util.unzip(filename, unzipfolder)
70
+ for file in os.listdir(unzipfolder):
71
+ if file.endswith(".png"):
72
+ SELECTION_FACES_DATA = extract_face_images(os.path.join(unzipfolder,file), (False, 0), 0.5)
73
+ if len(SELECTION_FACES_DATA) < 1:
74
+ gr.Warning(f"No face detected in {file}!")
75
+ for f in SELECTION_FACES_DATA:
76
+ image = f[1]
77
+ images.append(image)
78
+ thumbs.append(util.convert_to_gradio(image))
79
+
80
+ return thumbs
81
+
82
+
83
+ def on_fb_files_changed(inputfiles, progress=gr.Progress()) -> Tuple[List[Frame], Optional[gr.Image], Optional[gr.Slider], Optional[gr.Button]]:
84
+ global thumbs, images, total_frames, current_video_fps
85
+
86
+ if inputfiles is None or len(inputfiles) < 1:
87
+ return thumbs, None, None, None
88
+
89
+ progress(0, desc="Retrieving faces from images", )
90
+ slider = None
91
+ video_image = None
92
+ cut_button = None
93
+ for f in inputfiles:
94
+ source_path = f.name
95
+ if util.has_image_extension(source_path):
96
+ slider = gr.Slider(interactive=False)
97
+ video_image = gr.Image(interactive=False)
98
+ cut_button = gr.Button(interactive=False)
99
+ roop.globals.source_path = source_path
100
+ SELECTION_FACES_DATA = extract_face_images(roop.globals.source_path, (False, 0), 0.5)
101
+ for f in SELECTION_FACES_DATA:
102
+ image = f[1]
103
+ images.append(image)
104
+ thumbs.append(util.convert_to_gradio(image))
105
+ elif util.is_video(source_path) or source_path.lower().endswith('gif'):
106
+ total_frames = get_video_frame_total(source_path)
107
+ current_video_fps = util.detect_fps(source_path)
108
+ cut_button = gr.Button(interactive=True)
109
+ video_image, slider = display_video_frame(source_path, 1, total_frames)
110
+
111
+ return thumbs, video_image, slider, cut_button
112
+
113
+
114
+ def display_video_frame(filename: str, frame_num: int, total: int=0) -> Tuple[gr.Image, gr.Slider]:
115
+ global current_video_fps
116
+
117
+ current_frame = get_video_frame(filename, frame_num)
118
+ if current_video_fps == 0:
119
+ current_video_fps = 1
120
+ secs = (frame_num - 1) / current_video_fps
121
+ minutes = secs / 60
122
+ secs = secs % 60
123
+ hours = minutes / 60
124
+ minutes = minutes % 60
125
+ milliseconds = (secs - int(secs)) * 1000
126
+ timeinfo = f"{int(hours):0>2}:{int(minutes):0>2}:{int(secs):0>2}.{int(milliseconds):0>3}"
127
+ if total > 0:
128
+ return gr.Image(value=util.convert_to_gradio(current_frame), interactive=True), gr.Slider(info=timeinfo, minimum=1, maximum=total, interactive=True)
129
+ return gr.Image(value=util.convert_to_gradio(current_frame), interactive=True), gr.Slider(info=timeinfo, interactive=True)
130
+
131
+
132
+ def on_face_selected(evt: gr.SelectData) -> None:
133
+ global selected_face_index
134
+
135
+ if evt is not None:
136
+ selected_face_index = evt.index
137
+
138
+ def on_frame_num_fst_changed(inputfiles: List[gr.Files], frame_num: int) -> Frame:
139
+ filename = inputfiles[0].name
140
+ video_image, _ = display_video_frame(filename, frame_num, 0)
141
+ return video_image
142
+
143
+
144
+ def on_cutfromframe_clicked(inputfiles: List[gr.Files], frame_num: int) -> List[Frame]:
145
+ global thumbs
146
+
147
+ filename = inputfiles[0].name
148
+ SELECTION_FACES_DATA = extract_face_images(filename, (True, frame_num), 0.5)
149
+ for f in SELECTION_FACES_DATA:
150
+ image = f[1]
151
+ images.append(image)
152
+ thumbs.append(util.convert_to_gradio(image))
153
+ return thumbs
154
+
155
+
156
+ def on_remove_clicked() -> List[Frame]:
157
+ global thumbs, images, selected_face_index
158
+
159
+ if len(thumbs) > selected_face_index:
160
+ f = thumbs.pop(selected_face_index)
161
+ del f
162
+ f = images.pop(selected_face_index)
163
+ del f
164
+ return thumbs
165
+
166
+ def on_clear_clicked() -> Tuple[List[Frame], None, None]:
167
+ global thumbs, images
168
+
169
+ thumbs.clear()
170
+ images.clear()
171
+ return thumbs, None, None
172
+
173
+
174
+ def on_update_clicked() -> Optional[str]:
175
+ if len(images) < 1:
176
+ gr.Warning(f"No faces to create faceset from!")
177
+ return None
178
+
179
+ imgnames = []
180
+ for index,img in enumerate(images):
181
+ filename = os.path.join(roop.globals.output_path, f'{index}.png')
182
+ cv2.imwrite(filename, img)
183
+ imgnames.append(filename)
184
+
185
+ finalzip = os.path.join(roop.globals.output_path, 'faceset.fsz')
186
+ util.zip(imgnames, finalzip)
187
+ return finalzip
ui/tabs/faceswap_tab.py ADDED
@@ -0,0 +1,831 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import pathlib
4
+ import gradio as gr
5
+ import roop.utilities as util
6
+ import roop.globals
7
+ import ui.globals
8
+ from roop.face_util import extract_face_images, create_blank_image
9
+ from roop.capturer import get_video_frame, get_video_frame_total, get_image_frame
10
+ from roop.ProcessEntry import ProcessEntry
11
+ from roop.ProcessOptions import ProcessOptions
12
+ from roop.FaceSet import FaceSet
13
+ from roop.utilities import clean_dir
14
+
15
+ last_image = None
16
+
17
+
18
+ IS_INPUT = True
19
+ SELECTED_FACE_INDEX = 0
20
+
21
+ SELECTED_INPUT_FACE_INDEX = 0
22
+ SELECTED_TARGET_FACE_INDEX = 0
23
+
24
+ input_faces = None
25
+ target_faces = None
26
+ face_selection = None
27
+ previewimage = None
28
+
29
+ selected_preview_index = 0
30
+
31
+ is_processing = False
32
+
33
+ list_files_process : list[ProcessEntry] = []
34
+ no_face_choices = ["Use untouched original frame","Retry rotated", "Skip Frame", "Skip Frame if no similar face", "Use last swapped"]
35
+ swap_choices = ["First found", "All input faces", "All female", "All male", "All faces", "Selected face"]
36
+
37
+ current_video_fps = 50
38
+
39
+ manual_masking = False
40
+
41
+
42
+ def faceswap_tab():
43
+ global no_face_choices, previewimage
44
+
45
+ with gr.Tab("🎭 Face Swap"):
46
+ with gr.Row(variant='panel'):
47
+ with gr.Column(scale=2):
48
+ with gr.Row():
49
+ input_faces = gr.Gallery(label="Input faces gallery", allow_preview=False, preview=False, height=138, columns=64, object_fit="scale-down", interactive=False)
50
+ target_faces = gr.Gallery(label="Target faces gallery", allow_preview=False, preview=False, height=138, columns=64, object_fit="scale-down", interactive=False)
51
+ with gr.Row():
52
+ bt_move_left_input = gr.Button("⬅ Move left", size='sm')
53
+ bt_move_right_input = gr.Button("➡ Move right", size='sm')
54
+ bt_move_left_target = gr.Button("⬅ Move left", size='sm')
55
+ bt_move_right_target = gr.Button("➡ Move right", size='sm')
56
+ with gr.Row():
57
+ bt_remove_selected_input_face = gr.Button("❌ Remove selected", size='sm')
58
+ bt_clear_input_faces = gr.Button("💥 Clear all", variant='stop', size='sm')
59
+ bt_remove_selected_target_face = gr.Button("❌ Remove selected", size='sm')
60
+ bt_add_local = gr.Button('Add local files from', size='sm')
61
+
62
+ with gr.Row():
63
+ with gr.Column(scale=2):
64
+ with gr.Accordion(label="Advanced Masking", open=False):
65
+ chk_showmaskoffsets = gr.Checkbox(
66
+ label="Show mask overlay in preview",
67
+ value=False,
68
+ interactive=True,
69
+ )
70
+ chk_restoreoriginalmouth = gr.Checkbox(
71
+ label="Restore original mouth area",
72
+ value=False,
73
+ interactive=True,
74
+ )
75
+ mask_top = gr.Slider(
76
+ 0,
77
+ 1.0,
78
+ value=0,
79
+ label="Offset Face Top",
80
+ step=0.01,
81
+ interactive=True,
82
+ )
83
+ mask_bottom = gr.Slider(
84
+ 0,
85
+ 1.0,
86
+ value=0,
87
+ label="Offset Face Bottom",
88
+ step=0.01,
89
+ interactive=True,
90
+ )
91
+ mask_left = gr.Slider(
92
+ 0,
93
+ 1.0,
94
+ value=0,
95
+ label="Offset Face Left",
96
+ step=0.01,
97
+ interactive=True,
98
+ )
99
+ mask_right = gr.Slider(
100
+ 0,
101
+ 1.0,
102
+ value=0,
103
+ label="Offset Face Right",
104
+ step=0.01,
105
+ interactive=True,
106
+ )
107
+ mask_erosion = gr.Slider(
108
+ 1.0,
109
+ 3.0,
110
+ value=1.0,
111
+ label="Erosion Iterations",
112
+ step=1.00,
113
+ interactive=True,
114
+ )
115
+ mask_blur = gr.Slider(
116
+ 10.0,
117
+ 50.0,
118
+ value=20.0,
119
+ label="Blur size",
120
+ step=1.00,
121
+ interactive=True,
122
+ )
123
+ bt_toggle_masking = gr.Button(
124
+ "Toggle manual masking", variant="secondary", size="sm"
125
+ )
126
+ selected_mask_engine = gr.Dropdown(
127
+ ["None", "Clip2Seg", "DFL XSeg"],
128
+ value="None",
129
+ label="Face masking engine",
130
+ )
131
+ clip_text = gr.Textbox(
132
+ label="List of objects to mask and restore back on fake face",
133
+ value="cup,hands,hair,banana",
134
+ interactive=False,
135
+ )
136
+ bt_preview_mask = gr.Button(
137
+ "👥 Show Mask Preview", variant="secondary"
138
+ )
139
+ with gr.Column(scale=2):
140
+ local_folder = gr.Textbox(show_label=False, placeholder="/content/", interactive=True)
141
+ with gr.Row(variant='panel'):
142
+ bt_srcfiles = gr.Files(label='Source Images or Facesets', file_count="multiple", file_types=["image", ".fsz"], elem_id='filelist', height=233)
143
+ bt_destfiles = gr.Files(label='Target File(s)', file_count="multiple", file_types=["image", "video"], elem_id='filelist', height=233)
144
+ with gr.Row(variant='panel'):
145
+ gr.Markdown('')
146
+ forced_fps = gr.Slider(minimum=0, maximum=120, value=0, label="Video FPS", info='Overrides detected fps if not 0', step=1.0, interactive=True, container=True)
147
+
148
+ with gr.Column(scale=2):
149
+ previewimage = gr.Image(label="Preview Image", height=576, interactive=False, visible=True, format=get_gradio_output_format())
150
+ maskimage = gr.ImageEditor(label="Manual mask Image", sources=["clipboard"], transforms="", type="numpy",
151
+ brush=gr.Brush(color_mode="fixed", colors=["rgba(255, 255, 255, 1"]), interactive=True, visible=False)
152
+ with gr.Row(variant='panel'):
153
+ fake_preview = gr.Checkbox(label="Face swap frames", value=False)
154
+ bt_refresh_preview = gr.Button("🔄 Refresh", variant='secondary', size='sm')
155
+ bt_use_face_from_preview = gr.Button("Use Face from this Frame", variant='primary', size='sm')
156
+ with gr.Row():
157
+ preview_frame_num = gr.Slider(1, 1, value=1, label="Frame Number", info='0:00:00', step=1.0, interactive=True)
158
+ with gr.Row():
159
+ text_frame_clip = gr.Markdown('Processing frame range [0 - 0]')
160
+ set_frame_start = gr.Button("⬅ Set as Start", size='sm')
161
+ set_frame_end = gr.Button("➡ Set as End", size='sm')
162
+ with gr.Row(visible=False) as dynamic_face_selection:
163
+ with gr.Column(scale=2):
164
+ face_selection = gr.Gallery(label="Detected faces", allow_preview=False, preview=False, height=138, object_fit="cover", columns=32)
165
+ with gr.Column():
166
+ bt_faceselect = gr.Button("☑ Use selected face", size='sm')
167
+ bt_cancelfaceselect = gr.Button("Done", size='sm')
168
+ with gr.Column():
169
+ gr.Markdown(' ')
170
+
171
+ with gr.Row(variant='panel'):
172
+ with gr.Column(scale=1):
173
+ selected_face_detection = gr.Dropdown(swap_choices, value="First found", label="Specify face selection for swapping")
174
+ with gr.Column(scale=1):
175
+ num_swap_steps = gr.Slider(1, 5, value=1, step=1.0, label="Number of swapping steps", info="More steps may increase likeness")
176
+ with gr.Column(scale=2):
177
+ ui.globals.ui_selected_enhancer = gr.Dropdown(["None", "Codeformer", "DMDNet", "GFPGAN", "GPEN", "Restoreformer++"], value="None", label="Select post-processing")
178
+
179
+ with gr.Row(variant='panel'):
180
+ with gr.Column(scale=1):
181
+ max_face_distance = gr.Slider(0.01, 1.0, value=0.65, label="Max Face Similarity Threshold", info="0.0 = identical 1.0 = no similarity")
182
+ with gr.Column(scale=1):
183
+ ui.globals.ui_upscale = gr.Dropdown(["128px", "256px", "512px"], value="128px", label="Subsample upscale to", interactive=True)
184
+ with gr.Column(scale=2):
185
+ ui.globals.ui_blend_ratio = gr.Slider(0.0, 1.0, value=0.65, label="Original/Enhanced image blend ratio", info="Only used with active post-processing")
186
+
187
+ with gr.Row(variant='panel'):
188
+ with gr.Column(scale=1):
189
+ video_swapping_method = gr.Dropdown(["Extract Frames to media","In-Memory processing"], value="In-Memory processing", label="Select video processing method", interactive=True)
190
+ no_face_action = gr.Dropdown(choices=no_face_choices, value=no_face_choices[0], label="Action on no face detected", interactive=True)
191
+ vr_mode = gr.Checkbox(label="VR Mode", value=False)
192
+ with gr.Column(scale=1):
193
+ with gr.Group():
194
+ autorotate = gr.Checkbox(label="Auto rotate horizontal Faces", value=True)
195
+ roop.globals.skip_audio = gr.Checkbox(label="Skip audio", value=False)
196
+ roop.globals.keep_frames = gr.Checkbox(label="Keep Frames (relevant only when extracting frames)", value=False)
197
+ roop.globals.wait_after_extraction = gr.Checkbox(label="Wait for user key press before creating video ", value=False)
198
+
199
+ with gr.Row(variant='panel'):
200
+ with gr.Column():
201
+ bt_start = gr.Button("▶ Start", variant='primary')
202
+ with gr.Column():
203
+ bt_stop = gr.Button("⏹ Stop", variant='secondary', interactive=False)
204
+ gr.Button("👀 Open Output Folder", size='sm').click(fn=lambda: util.open_folder(roop.globals.output_path))
205
+ with gr.Column(scale=2):
206
+ output_method = gr.Dropdown(["File","Virtual Camera", "Both"], value="File", label="Select Output Method", interactive=True)
207
+ with gr.Row(variant='panel'):
208
+ with gr.Column():
209
+ resultfiles = gr.Files(label='Processed File(s)', interactive=False)
210
+ with gr.Column():
211
+ resultimage = gr.Image(type='filepath', label='Final Image', interactive=False )
212
+ resultvideo = gr.Video(label='Final Video', interactive=False, visible=False)
213
+
214
+ previewinputs = [preview_frame_num, bt_destfiles, fake_preview, ui.globals.ui_selected_enhancer, selected_face_detection,
215
+ max_face_distance, ui.globals.ui_blend_ratio, selected_mask_engine, clip_text, no_face_action, vr_mode, autorotate, maskimage, chk_showmaskoffsets, chk_restoreoriginalmouth, num_swap_steps, ui.globals.ui_upscale]
216
+ previewoutputs = [previewimage, maskimage, preview_frame_num]
217
+ input_faces.select(on_select_input_face, None, None).success(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs)
218
+
219
+ bt_move_left_input.click(fn=move_selected_input, inputs=[bt_move_left_input], outputs=[input_faces])
220
+ bt_move_right_input.click(fn=move_selected_input, inputs=[bt_move_right_input], outputs=[input_faces])
221
+ bt_move_left_target.click(fn=move_selected_target, inputs=[bt_move_left_target], outputs=[target_faces])
222
+ bt_move_right_target.click(fn=move_selected_target, inputs=[bt_move_right_target], outputs=[target_faces])
223
+
224
+ bt_remove_selected_input_face.click(fn=remove_selected_input_face, outputs=[input_faces])
225
+ bt_srcfiles.change(fn=on_srcfile_changed, show_progress='full', inputs=bt_srcfiles, outputs=[dynamic_face_selection, face_selection, input_faces, bt_srcfiles])
226
+
227
+ mask_top.release(fn=on_mask_top_changed, inputs=[mask_top], show_progress='hidden')
228
+ mask_bottom.release(fn=on_mask_bottom_changed, inputs=[mask_bottom], show_progress='hidden')
229
+ mask_left.release(fn=on_mask_left_changed, inputs=[mask_left], show_progress='hidden')
230
+ mask_right.release(fn=on_mask_right_changed, inputs=[mask_right], show_progress='hidden')
231
+ mask_erosion.release(fn=on_mask_erosion_changed, inputs=[mask_erosion], show_progress='hidden')
232
+ mask_blur.release(fn=on_mask_blur_changed, inputs=[mask_blur], show_progress='hidden')
233
+ selected_mask_engine.change(fn=on_mask_engine_changed, inputs=[selected_mask_engine], outputs=[clip_text], show_progress='hidden')
234
+
235
+ target_faces.select(on_select_target_face, None, None)
236
+ bt_remove_selected_target_face.click(fn=remove_selected_target_face, outputs=[target_faces])
237
+
238
+ forced_fps.change(fn=on_fps_changed, inputs=[forced_fps], show_progress='hidden')
239
+ bt_destfiles.change(fn=on_destfiles_changed, inputs=[bt_destfiles], outputs=[preview_frame_num, text_frame_clip], show_progress='hidden').success(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs, show_progress='hidden')
240
+ bt_destfiles.select(fn=on_destfiles_selected, outputs=[preview_frame_num, text_frame_clip, forced_fps], show_progress='hidden').success(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs, show_progress='hidden')
241
+ bt_destfiles.clear(fn=on_clear_destfiles, outputs=[target_faces, selected_face_detection])
242
+ resultfiles.select(fn=on_resultfiles_selected, inputs=[resultfiles], outputs=[resultimage, resultvideo])
243
+
244
+ face_selection.select(on_select_face, None, None)
245
+ bt_faceselect.click(fn=on_selected_face, outputs=[input_faces, target_faces, selected_face_detection])
246
+ bt_cancelfaceselect.click(fn=on_end_face_selection, outputs=[dynamic_face_selection, face_selection])
247
+
248
+ bt_clear_input_faces.click(fn=on_clear_input_faces, outputs=[input_faces])
249
+
250
+ bt_add_local.click(fn=on_add_local_folder, inputs=[local_folder], outputs=[bt_destfiles])
251
+ bt_preview_mask.click(fn=on_preview_mask, inputs=[preview_frame_num, bt_destfiles, clip_text, selected_mask_engine], outputs=[previewimage])
252
+
253
+ start_event = bt_start.click(fn=start_swap,
254
+ inputs=[output_method, ui.globals.ui_selected_enhancer, selected_face_detection, roop.globals.keep_frames, roop.globals.wait_after_extraction,
255
+ roop.globals.skip_audio, max_face_distance, ui.globals.ui_blend_ratio, selected_mask_engine, clip_text,video_swapping_method, no_face_action, vr_mode, autorotate, chk_restoreoriginalmouth, num_swap_steps, ui.globals.ui_upscale, maskimage],
256
+ outputs=[bt_start, bt_stop, resultfiles], show_progress='full')
257
+ after_swap_event = start_event.success(fn=on_resultfiles_finished, inputs=[resultfiles], outputs=[resultimage, resultvideo])
258
+
259
+ bt_stop.click(fn=stop_swap, cancels=[start_event, after_swap_event], outputs=[bt_start, bt_stop], queue=False)
260
+
261
+ bt_refresh_preview.click(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs)
262
+ bt_toggle_masking.click(fn=on_toggle_masking, inputs=[previewimage, maskimage], outputs=[previewimage, maskimage])
263
+ fake_preview.change(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs)
264
+ preview_frame_num.release(fn=on_preview_frame_changed, inputs=previewinputs, outputs=previewoutputs, show_progress='hidden', )
265
+ bt_use_face_from_preview.click(fn=on_use_face_from_selected, show_progress='full', inputs=[bt_destfiles, preview_frame_num], outputs=[dynamic_face_selection, face_selection, target_faces, selected_face_detection])
266
+ set_frame_start.click(fn=on_set_frame, inputs=[set_frame_start, preview_frame_num], outputs=[text_frame_clip])
267
+ set_frame_end.click(fn=on_set_frame, inputs=[set_frame_end, preview_frame_num], outputs=[text_frame_clip])
268
+
269
+
270
+ def on_mask_top_changed(mask_offset):
271
+ set_mask_offset(0, mask_offset)
272
+
273
+ def on_mask_bottom_changed(mask_offset):
274
+ set_mask_offset(1, mask_offset)
275
+
276
+ def on_mask_left_changed(mask_offset):
277
+ set_mask_offset(2, mask_offset)
278
+
279
+ def on_mask_right_changed(mask_offset):
280
+ set_mask_offset(3, mask_offset)
281
+
282
+ def on_mask_erosion_changed(mask_offset):
283
+ set_mask_offset(4, mask_offset)
284
+ def on_mask_blur_changed(mask_offset):
285
+ set_mask_offset(5, mask_offset)
286
+
287
+
288
+ def set_mask_offset(index, mask_offset):
289
+ global SELECTED_INPUT_FACE_INDEX
290
+
291
+ if len(roop.globals.INPUT_FACESETS) > SELECTED_INPUT_FACE_INDEX:
292
+ offs = roop.globals.INPUT_FACESETS[SELECTED_INPUT_FACE_INDEX].faces[0].mask_offsets
293
+ offs[index] = mask_offset
294
+ if offs[0] + offs[1] > 0.99:
295
+ offs[0] = 0.99
296
+ offs[1] = 0.0
297
+ if offs[2] + offs[3] > 0.99:
298
+ offs[2] = 0.99
299
+ offs[3] = 0.0
300
+ roop.globals.INPUT_FACESETS[SELECTED_INPUT_FACE_INDEX].faces[0].mask_offsets = offs
301
+
302
+ def on_mask_engine_changed(mask_engine):
303
+ if mask_engine == "Clip2Seg":
304
+ return gr.Textbox(interactive=True)
305
+ return gr.Textbox(interactive=False)
306
+
307
+
308
+ def on_add_local_folder(folder):
309
+ files = util.get_local_files_from_folder(folder)
310
+ if files is None:
311
+ gr.Warning("Empty folder or folder not found!")
312
+ return files
313
+
314
+
315
+ def on_srcfile_changed(srcfiles, progress=gr.Progress()):
316
+ global SELECTION_FACES_DATA, IS_INPUT, input_faces, face_selection, last_image
317
+
318
+ IS_INPUT = True
319
+
320
+ if srcfiles is None or len(srcfiles) < 1:
321
+ return gr.Column(visible=False), None, ui.globals.ui_input_thumbs, None
322
+
323
+ for f in srcfiles:
324
+ source_path = f.name
325
+ if source_path.lower().endswith('fsz'):
326
+ progress(0, desc="Retrieving faces from Faceset File")
327
+ unzipfolder = os.path.join(os.environ["TEMP"], 'faceset')
328
+ if os.path.isdir(unzipfolder):
329
+ files = os.listdir(unzipfolder)
330
+ for file in files:
331
+ os.remove(os.path.join(unzipfolder, file))
332
+ else:
333
+ os.makedirs(unzipfolder)
334
+ util.mkdir_with_umask(unzipfolder)
335
+ util.unzip(source_path, unzipfolder)
336
+ is_first = True
337
+ face_set = FaceSet()
338
+ for file in os.listdir(unzipfolder):
339
+ if file.endswith(".png"):
340
+ filename = os.path.join(unzipfolder,file)
341
+ progress(0, desc="Extracting faceset")
342
+ SELECTION_FACES_DATA = extract_face_images(filename, (False, 0))
343
+ for f in SELECTION_FACES_DATA:
344
+ face = f[0]
345
+ face.mask_offsets = (0,0,0,0,1,20)
346
+ face_set.faces.append(face)
347
+ if is_first:
348
+ image = util.convert_to_gradio(f[1])
349
+ ui.globals.ui_input_thumbs.append(image)
350
+ is_first = False
351
+ face_set.ref_images.append(get_image_frame(filename))
352
+ if len(face_set.faces) > 0:
353
+ if len(face_set.faces) > 1:
354
+ face_set.AverageEmbeddings()
355
+ roop.globals.INPUT_FACESETS.append(face_set)
356
+
357
+ elif util.has_image_extension(source_path):
358
+ progress(0, desc="Retrieving faces from image")
359
+ roop.globals.source_path = source_path
360
+ SELECTION_FACES_DATA = extract_face_images(roop.globals.source_path, (False, 0))
361
+ progress(0.5, desc="Retrieving faces from image")
362
+ for f in SELECTION_FACES_DATA:
363
+ face_set = FaceSet()
364
+ face = f[0]
365
+ face.mask_offsets = (0,0,0,0,1,20)
366
+ face_set.faces.append(face)
367
+ image = util.convert_to_gradio(f[1])
368
+ ui.globals.ui_input_thumbs.append(image)
369
+ roop.globals.INPUT_FACESETS.append(face_set)
370
+
371
+ progress(1.0)
372
+ return gr.Column(visible=False), None, ui.globals.ui_input_thumbs,None
373
+
374
+
375
+ def on_select_input_face(evt: gr.SelectData):
376
+ global SELECTED_INPUT_FACE_INDEX
377
+
378
+ SELECTED_INPUT_FACE_INDEX = evt.index
379
+
380
+
381
+ def remove_selected_input_face():
382
+ global SELECTED_INPUT_FACE_INDEX
383
+
384
+ if len(roop.globals.INPUT_FACESETS) > SELECTED_INPUT_FACE_INDEX:
385
+ f = roop.globals.INPUT_FACESETS.pop(SELECTED_INPUT_FACE_INDEX)
386
+ del f
387
+ if len(ui.globals.ui_input_thumbs) > SELECTED_INPUT_FACE_INDEX:
388
+ f = ui.globals.ui_input_thumbs.pop(SELECTED_INPUT_FACE_INDEX)
389
+ del f
390
+
391
+ return ui.globals.ui_input_thumbs
392
+
393
+ def move_selected_input(button_text):
394
+ global SELECTED_INPUT_FACE_INDEX
395
+
396
+ if button_text == "⬅ Move left":
397
+ if SELECTED_INPUT_FACE_INDEX <= 0:
398
+ return ui.globals.ui_input_thumbs
399
+ offset = -1
400
+ else:
401
+ if len(ui.globals.ui_input_thumbs) <= SELECTED_INPUT_FACE_INDEX:
402
+ return ui.globals.ui_input_thumbs
403
+ offset = 1
404
+
405
+ f = roop.globals.INPUT_FACESETS.pop(SELECTED_INPUT_FACE_INDEX)
406
+ roop.globals.INPUT_FACESETS.insert(SELECTED_INPUT_FACE_INDEX + offset, f)
407
+ f = ui.globals.ui_input_thumbs.pop(SELECTED_INPUT_FACE_INDEX)
408
+ ui.globals.ui_input_thumbs.insert(SELECTED_INPUT_FACE_INDEX + offset, f)
409
+ return ui.globals.ui_input_thumbs
410
+
411
+
412
+ def move_selected_target(button_text):
413
+ global SELECTED_TARGET_FACE_INDEX
414
+
415
+ if button_text == "⬅ Move left":
416
+ if SELECTED_TARGET_FACE_INDEX <= 0:
417
+ return ui.globals.ui_target_thumbs
418
+ offset = -1
419
+ else:
420
+ if len(ui.globals.ui_target_thumbs) <= SELECTED_TARGET_FACE_INDEX:
421
+ return ui.globals.ui_target_thumbs
422
+ offset = 1
423
+
424
+ f = roop.globals.TARGET_FACES.pop(SELECTED_TARGET_FACE_INDEX)
425
+ roop.globals.TARGET_FACES.insert(SELECTED_TARGET_FACE_INDEX + offset, f)
426
+ f = ui.globals.ui_target_thumbs.pop(SELECTED_TARGET_FACE_INDEX)
427
+ ui.globals.ui_target_thumbs.insert(SELECTED_TARGET_FACE_INDEX + offset, f)
428
+ return ui.globals.ui_target_thumbs
429
+
430
+
431
+
432
+
433
+ def on_select_target_face(evt: gr.SelectData):
434
+ global SELECTED_TARGET_FACE_INDEX
435
+
436
+ SELECTED_TARGET_FACE_INDEX = evt.index
437
+
438
+ def remove_selected_target_face():
439
+ if len(ui.globals.ui_target_thumbs) > SELECTED_TARGET_FACE_INDEX:
440
+ f = roop.globals.TARGET_FACES.pop(SELECTED_TARGET_FACE_INDEX)
441
+ del f
442
+ if len(ui.globals.ui_target_thumbs) > SELECTED_TARGET_FACE_INDEX:
443
+ f = ui.globals.ui_target_thumbs.pop(SELECTED_TARGET_FACE_INDEX)
444
+ del f
445
+ return ui.globals.ui_target_thumbs
446
+
447
+
448
+ def on_use_face_from_selected(files, frame_num):
449
+ global IS_INPUT, SELECTION_FACES_DATA
450
+
451
+ IS_INPUT = False
452
+ thumbs = []
453
+
454
+ roop.globals.target_path = files[selected_preview_index].name
455
+ if util.is_image(roop.globals.target_path) and not roop.globals.target_path.lower().endswith(('gif')):
456
+ SELECTION_FACES_DATA = extract_face_images(roop.globals.target_path, (False, 0))
457
+ if len(SELECTION_FACES_DATA) > 0:
458
+ for f in SELECTION_FACES_DATA:
459
+ image = util.convert_to_gradio(f[1])
460
+ thumbs.append(image)
461
+ else:
462
+ gr.Info('No faces detected!')
463
+ roop.globals.target_path = None
464
+
465
+ elif util.is_video(roop.globals.target_path) or roop.globals.target_path.lower().endswith(('gif')):
466
+ selected_frame = frame_num
467
+ SELECTION_FACES_DATA = extract_face_images(roop.globals.target_path, (True, selected_frame))
468
+ if len(SELECTION_FACES_DATA) > 0:
469
+ for f in SELECTION_FACES_DATA:
470
+ image = util.convert_to_gradio(f[1])
471
+ thumbs.append(image)
472
+ else:
473
+ gr.Info('No faces detected!')
474
+ roop.globals.target_path = None
475
+ else:
476
+ gr.Info('Unknown image/video type!')
477
+ roop.globals.target_path = None
478
+
479
+ if len(thumbs) == 1:
480
+ roop.globals.TARGET_FACES.append(SELECTION_FACES_DATA[0][0])
481
+ ui.globals.ui_target_thumbs.append(thumbs[0])
482
+ return gr.Row(visible=False), None, ui.globals.ui_target_thumbs, gr.Dropdown(value='Selected face')
483
+
484
+ return gr.Row(visible=True), thumbs, gr.Gallery(visible=True), gr.Dropdown(visible=True)
485
+
486
+
487
+ def on_select_face(evt: gr.SelectData): # SelectData is a subclass of EventData
488
+ global SELECTED_FACE_INDEX
489
+ SELECTED_FACE_INDEX = evt.index
490
+
491
+
492
+ def on_selected_face():
493
+ global IS_INPUT, SELECTED_FACE_INDEX, SELECTION_FACES_DATA
494
+
495
+ fd = SELECTION_FACES_DATA[SELECTED_FACE_INDEX]
496
+ image = util.convert_to_gradio(fd[1])
497
+ if IS_INPUT:
498
+ face_set = FaceSet()
499
+ fd[0].mask_offsets = (0,0,0,0,1,20)
500
+ face_set.faces.append(fd[0])
501
+ roop.globals.INPUT_FACESETS.append(face_set)
502
+ ui.globals.ui_input_thumbs.append(image)
503
+ return ui.globals.ui_input_thumbs, gr.Gallery(visible=True), gr.Dropdown(visible=True)
504
+ else:
505
+ roop.globals.TARGET_FACES.append(fd[0])
506
+ ui.globals.ui_target_thumbs.append(image)
507
+ return gr.Gallery(visible=True), ui.globals.ui_target_thumbs, gr.Dropdown(value='Selected face')
508
+
509
+ # bt_faceselect.click(fn=on_selected_face, outputs=[dynamic_face_selection, face_selection, input_faces, target_faces])
510
+
511
+ def on_end_face_selection():
512
+ return gr.Column(visible=False), None
513
+
514
+
515
+ def on_preview_frame_changed(frame_num, files, fake_preview, enhancer, detection, face_distance, blend_ratio,
516
+ selected_mask_engine, clip_text, no_face_action, vr_mode, auto_rotate, maskimage, show_face_area, restore_original_mouth, num_steps, upsample):
517
+ global SELECTED_INPUT_FACE_INDEX, manual_masking, current_video_fps
518
+
519
+ from roop.core import live_swap, get_processing_plugins
520
+
521
+ manual_masking = False
522
+ mask_offsets = (0,0,0,0)
523
+ if len(roop.globals.INPUT_FACESETS) > SELECTED_INPUT_FACE_INDEX:
524
+ if not hasattr(roop.globals.INPUT_FACESETS[SELECTED_INPUT_FACE_INDEX].faces[0], 'mask_offsets'):
525
+ roop.globals.INPUT_FACESETS[SELECTED_INPUT_FACE_INDEX].faces[0].mask_offsets = mask_offsets
526
+ mask_offsets = roop.globals.INPUT_FACESETS[SELECTED_INPUT_FACE_INDEX].faces[0].mask_offsets
527
+
528
+ timeinfo = '0:00:00'
529
+ if files is None or selected_preview_index >= len(files) or frame_num is None:
530
+ return None,None, gr.Slider(info=timeinfo)
531
+
532
+ filename = files[selected_preview_index].name
533
+ if util.is_video(filename) or filename.lower().endswith('gif'):
534
+ current_frame = get_video_frame(filename, frame_num)
535
+ if current_video_fps == 0:
536
+ current_video_fps = 1
537
+ secs = (frame_num - 1) / current_video_fps
538
+ minutes = secs / 60
539
+ secs = secs % 60
540
+ hours = minutes / 60
541
+ minutes = minutes % 60
542
+ milliseconds = (secs - int(secs)) * 1000
543
+ timeinfo = f"{int(hours):0>2}:{int(minutes):0>2}:{int(secs):0>2}.{int(milliseconds):0>3}"
544
+ else:
545
+ current_frame = get_image_frame(filename)
546
+ if current_frame is None:
547
+ return None, None, gr.Slider(info=timeinfo)
548
+
549
+ layers = None
550
+ if maskimage is not None:
551
+ layers = maskimage["layers"]
552
+
553
+ if not fake_preview or len(roop.globals.INPUT_FACESETS) < 1:
554
+ return gr.Image(value=util.convert_to_gradio(current_frame), visible=True), gr.ImageEditor(visible=False), gr.Slider(info=timeinfo)
555
+
556
+ roop.globals.face_swap_mode = translate_swap_mode(detection)
557
+ roop.globals.selected_enhancer = enhancer
558
+ roop.globals.distance_threshold = face_distance
559
+ roop.globals.blend_ratio = blend_ratio
560
+ roop.globals.no_face_action = index_of_no_face_action(no_face_action)
561
+ roop.globals.vr_mode = vr_mode
562
+ roop.globals.autorotate_faces = auto_rotate
563
+ roop.globals.subsample_size = int(upsample[:3])
564
+
565
+
566
+ mask_engine = map_mask_engine(selected_mask_engine, clip_text)
567
+
568
+ roop.globals.execution_threads = roop.globals.CFG.max_threads
569
+ mask = layers[0] if layers is not None else None
570
+ face_index = SELECTED_INPUT_FACE_INDEX
571
+ if len(roop.globals.INPUT_FACESETS) <= face_index:
572
+ face_index = 0
573
+
574
+ options = ProcessOptions(get_processing_plugins(mask_engine), roop.globals.distance_threshold, roop.globals.blend_ratio,
575
+ roop.globals.face_swap_mode, face_index, clip_text, maskimage, num_steps, roop.globals.subsample_size, show_face_area, restore_original_mouth)
576
+
577
+ current_frame = live_swap(current_frame, options)
578
+ if current_frame is None:
579
+ return gr.Image(visible=True), None, gr.Slider(info=timeinfo)
580
+ return gr.Image(value=util.convert_to_gradio(current_frame), visible=True), gr.ImageEditor(visible=False), gr.Slider(info=timeinfo)
581
+
582
+ def map_mask_engine(selected_mask_engine, clip_text):
583
+ if selected_mask_engine == "Clip2Seg":
584
+ mask_engine = "mask_clip2seg"
585
+ if clip_text is None or len(clip_text) < 1:
586
+ mask_engine = None
587
+ elif selected_mask_engine == "DFL XSeg":
588
+ mask_engine = "mask_xseg"
589
+ else:
590
+ mask_engine = None
591
+ return mask_engine
592
+
593
+
594
+ def on_toggle_masking(previewimage, mask):
595
+ global manual_masking
596
+
597
+ manual_masking = not manual_masking
598
+ if manual_masking:
599
+ layers = mask["layers"]
600
+ if len(layers) == 1:
601
+ layers = [create_blank_image(previewimage.shape[1],previewimage.shape[0])]
602
+ return gr.Image(visible=False), gr.ImageEditor(value={"background": previewimage, "layers": layers, "composite": None}, visible=True)
603
+ return gr.Image(visible=True), gr.ImageEditor(visible=False)
604
+
605
+ def gen_processing_text(start, end):
606
+ return f'Processing frame range [{start} - {end}]'
607
+
608
+ def on_set_frame(sender:str, frame_num):
609
+ global selected_preview_index, list_files_process
610
+
611
+ idx = selected_preview_index
612
+ if list_files_process[idx].endframe == 0:
613
+ return gen_processing_text(0,0)
614
+
615
+ start = list_files_process[idx].startframe
616
+ end = list_files_process[idx].endframe
617
+ if sender.lower().endswith('start'):
618
+ list_files_process[idx].startframe = min(frame_num, end)
619
+ else:
620
+ list_files_process[idx].endframe = max(frame_num, start)
621
+
622
+ return gen_processing_text(list_files_process[idx].startframe,list_files_process[idx].endframe)
623
+
624
+
625
+ def on_preview_mask(frame_num, files, clip_text, mask_engine):
626
+ from roop.core import live_swap, get_processing_plugins
627
+ global is_processing
628
+
629
+ if is_processing or files is None or selected_preview_index >= len(files) or clip_text is None or frame_num is None:
630
+ return None
631
+
632
+ filename = files[selected_preview_index].name
633
+ if util.is_video(filename) or filename.lower().endswith('gif'):
634
+ current_frame = get_video_frame(filename, frame_num
635
+ )
636
+ else:
637
+ current_frame = get_image_frame(filename)
638
+ if current_frame is None or mask_engine is None:
639
+ return None
640
+ if mask_engine == "Clip2Seg":
641
+ mask_engine = "mask_clip2seg"
642
+ if clip_text is None or len(clip_text) < 1:
643
+ mask_engine = None
644
+ elif mask_engine == "DFL XSeg":
645
+ mask_engine = "mask_xseg"
646
+ options = ProcessOptions(get_processing_plugins(mask_engine), roop.globals.distance_threshold, roop.globals.blend_ratio,
647
+ "all", 0, clip_text, None, 0, 128, False, False, True)
648
+
649
+ current_frame = live_swap(current_frame, options)
650
+ return util.convert_to_gradio(current_frame)
651
+
652
+
653
+ def on_clear_input_faces():
654
+ ui.globals.ui_input_thumbs.clear()
655
+ roop.globals.INPUT_FACESETS.clear()
656
+ return ui.globals.ui_input_thumbs
657
+
658
+ def on_clear_destfiles():
659
+ roop.globals.TARGET_FACES.clear()
660
+ ui.globals.ui_target_thumbs.clear()
661
+ return ui.globals.ui_target_thumbs, gr.Dropdown(value="First found")
662
+
663
+
664
+ def index_of_no_face_action(dropdown_text):
665
+ global no_face_choices
666
+
667
+ return no_face_choices.index(dropdown_text)
668
+
669
+ def translate_swap_mode(dropdown_text):
670
+ if dropdown_text == "Selected face":
671
+ return "selected"
672
+ elif dropdown_text == "First found":
673
+ return "first"
674
+ elif dropdown_text == "All input faces":
675
+ return "all_input"
676
+ elif dropdown_text == "All female":
677
+ return "all_female"
678
+ elif dropdown_text == "All male":
679
+ return "all_male"
680
+
681
+ return "all"
682
+
683
+
684
+ def start_swap( output_method, enhancer, detection, keep_frames, wait_after_extraction, skip_audio, face_distance, blend_ratio,
685
+ selected_mask_engine, clip_text, processing_method, no_face_action, vr_mode, autorotate, restore_original_mouth, num_swap_steps, upsample, imagemask, progress=gr.Progress()):
686
+ from ui.main import prepare_environment
687
+ from roop.core import batch_process_regular
688
+ global is_processing, list_files_process
689
+
690
+ if list_files_process is None or len(list_files_process) <= 0:
691
+ return gr.Button(variant="primary"), None, None
692
+
693
+ if roop.globals.CFG.clear_output:
694
+ clean_dir(roop.globals.output_path)
695
+
696
+ if not util.is_installed("ffmpeg"):
697
+ msg = "ffmpeg is not installed! No video processing possible."
698
+ gr.Warning(msg)
699
+
700
+ prepare_environment()
701
+
702
+ roop.globals.selected_enhancer = enhancer
703
+ roop.globals.target_path = None
704
+ roop.globals.distance_threshold = face_distance
705
+ roop.globals.blend_ratio = blend_ratio
706
+ roop.globals.keep_frames = keep_frames
707
+ roop.globals.wait_after_extraction = wait_after_extraction
708
+ roop.globals.skip_audio = skip_audio
709
+ roop.globals.face_swap_mode = translate_swap_mode(detection)
710
+ roop.globals.no_face_action = index_of_no_face_action(no_face_action)
711
+ roop.globals.vr_mode = vr_mode
712
+ roop.globals.autorotate_faces = autorotate
713
+ roop.globals.subsample_size = int(upsample[:3])
714
+ mask_engine = map_mask_engine(selected_mask_engine, clip_text)
715
+
716
+ if roop.globals.face_swap_mode == 'selected':
717
+ if len(roop.globals.TARGET_FACES) < 1:
718
+ gr.Error('No Target Face selected!')
719
+ return gr.Button(variant="primary"), None, None
720
+
721
+ is_processing = True
722
+ yield gr.Button(variant="secondary", interactive=False), gr.Button(variant="primary", interactive=True), None
723
+ roop.globals.execution_threads = roop.globals.CFG.max_threads
724
+ roop.globals.video_encoder = roop.globals.CFG.output_video_codec
725
+ roop.globals.video_quality = roop.globals.CFG.video_quality
726
+ roop.globals.max_memory = roop.globals.CFG.memory_limit if roop.globals.CFG.memory_limit > 0 else None
727
+
728
+ batch_process_regular(output_method, list_files_process, mask_engine, clip_text, processing_method == "In-Memory processing", imagemask, restore_original_mouth, num_swap_steps, progress, SELECTED_INPUT_FACE_INDEX)
729
+ is_processing = False
730
+ outdir = pathlib.Path(roop.globals.output_path)
731
+ outfiles = [str(item) for item in outdir.rglob("*") if item.is_file()]
732
+ if len(outfiles) > 0:
733
+ yield gr.Button(variant="primary", interactive=True),gr.Button(variant="secondary", interactive=False),gr.Files(value=outfiles)
734
+ else:
735
+ yield gr.Button(variant="primary", interactive=True),gr.Button(variant="secondary", interactive=False),None
736
+
737
+
738
+ def stop_swap():
739
+ roop.globals.processing = False
740
+ gr.Info('Aborting processing - please wait for the remaining threads to be stopped')
741
+ return gr.Button(variant="primary", interactive=True),gr.Button(variant="secondary", interactive=False),None
742
+
743
+
744
+ def on_fps_changed(fps):
745
+ global selected_preview_index, list_files_process
746
+
747
+ if len(list_files_process) < 1 or list_files_process[selected_preview_index].endframe < 1:
748
+ return
749
+ list_files_process[selected_preview_index].fps = fps
750
+
751
+
752
+ def on_destfiles_changed(destfiles):
753
+ global selected_preview_index, list_files_process, current_video_fps
754
+
755
+ if destfiles is None or len(destfiles) < 1:
756
+ list_files_process.clear()
757
+ return gr.Slider(value=1, maximum=1, info='0:00:00'), ''
758
+
759
+ for f in destfiles:
760
+ list_files_process.append(ProcessEntry(f.name, 0,0, 0))
761
+
762
+ selected_preview_index = 0
763
+ idx = selected_preview_index
764
+
765
+ filename = list_files_process[idx].filename
766
+
767
+ if util.is_video(filename) or filename.lower().endswith('gif'):
768
+ total_frames = get_video_frame_total(filename)
769
+ if total_frames is None or total_frames < 1:
770
+ total_frames = 1
771
+ gr.Warning(f"Corrupted video {filename}, can't detect number of frames!")
772
+ else:
773
+ current_video_fps = util.detect_fps(filename)
774
+ else:
775
+ total_frames = 1
776
+ list_files_process[idx].endframe = total_frames
777
+ if total_frames > 1:
778
+ return gr.Slider(value=1, maximum=total_frames, info='0:00:00'), gen_processing_text(list_files_process[idx].startframe,list_files_process[idx].endframe)
779
+ return gr.Slider(value=1, maximum=total_frames, info='0:00:00'), ''
780
+
781
+
782
+ def on_destfiles_selected(evt: gr.SelectData):
783
+ global selected_preview_index, list_files_process, current_video_fps
784
+
785
+ if evt is not None:
786
+ selected_preview_index = evt.index
787
+ idx = selected_preview_index
788
+ filename = list_files_process[idx].filename
789
+ fps = list_files_process[idx].fps
790
+ if util.is_video(filename) or filename.lower().endswith('gif'):
791
+ total_frames = get_video_frame_total(filename)
792
+ current_video_fps = util.detect_fps(filename)
793
+ if list_files_process[idx].endframe == 0:
794
+ list_files_process[idx].endframe = total_frames
795
+ else:
796
+ total_frames = 1
797
+
798
+ if total_frames > 1:
799
+ return gr.Slider(value=list_files_process[idx].startframe, maximum=total_frames, info='0:00:00'), gen_processing_text(list_files_process[idx].startframe,list_files_process[idx].endframe), fps
800
+ return gr.Slider(value=1, maximum=total_frames, info='0:00:00'), gen_processing_text(0,0), fps
801
+
802
+
803
+ def on_resultfiles_selected(evt: gr.SelectData, files):
804
+ selected_index = evt.index
805
+ filename = files[selected_index].name
806
+ return display_output(filename)
807
+
808
+ def on_resultfiles_finished(files):
809
+ selected_index = 0
810
+ if files is None or len(files) < 1:
811
+ return None, None
812
+
813
+ filename = files[selected_index].name
814
+ return display_output(filename)
815
+
816
+
817
+ def get_gradio_output_format():
818
+ if roop.globals.CFG.output_image_format == "jpg":
819
+ return "jpeg"
820
+ return roop.globals.CFG.output_image_format
821
+
822
+
823
+ def display_output(filename):
824
+ if util.is_video(filename) and roop.globals.CFG.output_show_video:
825
+ return gr.Image(visible=False), gr.Video(visible=True, value=filename)
826
+ else:
827
+ if util.is_video(filename) or filename.lower().endswith('gif'):
828
+ current_frame = get_video_frame(filename)
829
+ else:
830
+ current_frame = get_image_frame(filename)
831
+ return gr.Image(visible=True, value=util.convert_to_gradio(current_frame)), gr.Video(visible=False)
ui/tabs/livecam_tab.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import roop.globals
3
+ import ui.globals
4
+
5
+
6
+ camera_frame = None
7
+
8
+ def livecam_tab():
9
+ with gr.Tab("🎥 Live Cam"):
10
+ with gr.Row(variant='panel'):
11
+ gr.Markdown("""
12
+ This feature will allow you to use your physical webcam and apply the selected faces to the stream.
13
+ You can also forward the stream to a virtual camera, which can be used in video calls or streaming software.<br />
14
+ Supported are: v4l2loopback (linux), OBS Virtual Camera (macOS/Windows) and unitycapture (Windows).<br />
15
+ **Please note:** to change the face or any other settings you need to stop and restart a running live cam.
16
+ """)
17
+
18
+ with gr.Row(variant='panel'):
19
+ with gr.Column():
20
+ bt_start = gr.Button("▶ Start", variant='primary')
21
+ with gr.Column():
22
+ bt_stop = gr.Button("⏹ Stop", variant='secondary', interactive=False)
23
+ with gr.Column():
24
+ camera_num = gr.Slider(0, 8, value=0, label="Camera Number", step=1.0, interactive=True)
25
+ cb_obs = gr.Checkbox(label="Forward stream to virtual camera", interactive=True)
26
+ with gr.Column():
27
+ dd_reso = gr.Dropdown(choices=["640x480","1280x720", "1920x1080"], value="1280x720", label="Fake Camera Resolution", interactive=True)
28
+ cb_xseg = gr.Checkbox(label="Use DFL Xseg masking", interactive=True, value=True)
29
+ cb_mouthrestore = gr.Checkbox(label="Restore original mouth area", interactive=True, value=False)
30
+
31
+ with gr.Row():
32
+ fake_cam_image = gr.Image(label='Fake Camera Output', interactive=False, format="jpeg")
33
+
34
+ start_event = bt_start.click(fn=start_cam, inputs=[cb_obs, cb_xseg, cb_mouthrestore, camera_num, dd_reso, ui.globals.ui_selected_enhancer, ui.globals.ui_blend_ratio, ui.globals.ui_upscale],outputs=[bt_start, bt_stop,fake_cam_image])
35
+ bt_stop.click(fn=stop_swap, cancels=[start_event], outputs=[bt_start, bt_stop], queue=False)
36
+
37
+
38
+ def start_cam(stream_to_obs, use_xseg, use_mouthrestore, cam, reso, enhancer, blend_ratio, upscale):
39
+ from roop.virtualcam import start_virtual_cam
40
+ from roop.utilities import convert_to_gradio
41
+
42
+ roop.globals.selected_enhancer = enhancer
43
+ roop.globals.blend_ratio = blend_ratio
44
+ roop.globals.subsample_size = int(upscale[:3])
45
+ start_virtual_cam(stream_to_obs, use_xseg, use_mouthrestore, cam, reso)
46
+ while True:
47
+ yield gr.Button(interactive=False), gr.Button(interactive=True), convert_to_gradio(ui.globals.ui_camera_frame)
48
+
49
+
50
+ def stop_swap():
51
+ from roop.virtualcam import stop_virtual_cam
52
+ stop_virtual_cam()
53
+ return gr.Button(interactive=True), gr.Button(interactive=False)
54
+
55
+
56
+
57
+
ui/tabs/settings_tab.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import shutil
2
+ import os
3
+ import gradio as gr
4
+ import roop.globals
5
+ import ui.globals
6
+ from roop.utilities import clean_dir
7
+
8
+ available_themes = ["Default", "gradio/glass", "gradio/monochrome", "gradio/seafoam", "gradio/soft", "gstaff/xkcd", "freddyaboulton/dracula_revamped", "ysharma/steampunk"]
9
+ image_formats = ['jpg','png', 'webp']
10
+ video_formats = ['avi','mkv', 'mp4', 'webm']
11
+ video_codecs = ['libx264', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc']
12
+ providerlist = None
13
+
14
+ settings_controls = []
15
+
16
+ def settings_tab():
17
+ from roop.core import suggest_execution_providers
18
+ global providerlist
19
+
20
+ providerlist = suggest_execution_providers()
21
+ with gr.Tab("⚙ Settings"):
22
+ with gr.Row():
23
+ with gr.Column():
24
+ themes = gr.Dropdown(available_themes, label="Theme", info="Change needs complete restart", value=roop.globals.CFG.selected_theme)
25
+ with gr.Column():
26
+ settings_controls.append(gr.Checkbox(label="Public Server", value=roop.globals.CFG.server_share, elem_id='server_share', interactive=True))
27
+ settings_controls.append(gr.Checkbox(label='Clear output folder before each run', value=roop.globals.CFG.clear_output, elem_id='clear_output', interactive=True))
28
+ output_template = gr.Textbox(label="Filename Output Template", info="(file extension is added automatically)", lines=1, placeholder='{file}_{time}', value=roop.globals.CFG.output_template)
29
+ with gr.Column():
30
+ input_server_name = gr.Textbox(label="Server Name", lines=1, info="Leave blank to run locally", value=roop.globals.CFG.server_name)
31
+ with gr.Column():
32
+ input_server_port = gr.Number(label="Server Port", precision=0, info="Leave at 0 to use default", value=roop.globals.CFG.server_port)
33
+ with gr.Row():
34
+ with gr.Column():
35
+ settings_controls.append(gr.Dropdown(providerlist, label="Provider", value=roop.globals.CFG.provider, elem_id='provider', interactive=True))
36
+ chk_det_size = gr.Checkbox(label="Use default Det-Size", value=True, elem_id='default_det_size', interactive=True)
37
+ settings_controls.append(gr.Checkbox(label="Force CPU for Face Analyser", value=roop.globals.CFG.force_cpu, elem_id='force_cpu', interactive=True))
38
+ max_threads = gr.Slider(1, 32, value=roop.globals.CFG.max_threads, label="Max. Number of Threads", info='default: 3', step=1.0, interactive=True)
39
+ with gr.Column():
40
+ memory_limit = gr.Slider(0, 128, value=roop.globals.CFG.memory_limit, label="Max. Memory to use (Gb)", info='0 meaning no limit', step=1.0, interactive=True)
41
+ settings_controls.append(gr.Dropdown(image_formats, label="Image Output Format", info='default: png', value=roop.globals.CFG.output_image_format, elem_id='output_image_format', interactive=True))
42
+ with gr.Column():
43
+ settings_controls.append(gr.Dropdown(video_codecs, label="Video Codec", info='default: libx264', value=roop.globals.CFG.output_video_codec, elem_id='output_video_codec', interactive=True))
44
+ settings_controls.append(gr.Dropdown(video_formats, label="Video Output Format", info='default: mp4', value=roop.globals.CFG.output_video_format, elem_id='output_video_format', interactive=True))
45
+ video_quality = gr.Slider(0, 100, value=roop.globals.CFG.video_quality, label="Video Quality (crf)", info='default: 14', step=1.0, interactive=True)
46
+ with gr.Column():
47
+ with gr.Group():
48
+ settings_controls.append(gr.Checkbox(label='Use OS temp folder', value=roop.globals.CFG.use_os_temp_folder, elem_id='use_os_temp_folder', interactive=True))
49
+ settings_controls.append(gr.Checkbox(label='Show video in browser (re-encodes output)', value=roop.globals.CFG.output_show_video, elem_id='output_show_video', interactive=True))
50
+ button_apply_restart = gr.Button("Restart Server", variant='primary')
51
+ button_clean_temp = gr.Button("Clean temp folder")
52
+ button_apply_settings = gr.Button("Apply Settings")
53
+
54
+ chk_det_size.select(fn=on_option_changed)
55
+
56
+ # Settings
57
+ for s in settings_controls:
58
+ s.select(fn=on_settings_changed)
59
+ max_threads.input(fn=lambda a,b='max_threads':on_settings_changed_misc(a,b), inputs=[max_threads])
60
+ memory_limit.input(fn=lambda a,b='memory_limit':on_settings_changed_misc(a,b), inputs=[memory_limit])
61
+ video_quality.input(fn=lambda a,b='video_quality':on_settings_changed_misc(a,b), inputs=[video_quality])
62
+
63
+ # button_clean_temp.click(fn=clean_temp, outputs=[bt_srcfiles, input_faces, target_faces, bt_destfiles])
64
+ button_clean_temp.click(fn=clean_temp)
65
+ button_apply_settings.click(apply_settings, inputs=[themes, input_server_name, input_server_port, output_template])
66
+ button_apply_restart.click(restart)
67
+
68
+
69
+ def on_option_changed(evt: gr.SelectData):
70
+ attribname = evt.target.elem_id
71
+ if isinstance(evt.target, gr.Checkbox):
72
+ if hasattr(roop.globals, attribname):
73
+ setattr(roop.globals, attribname, evt.selected)
74
+ return
75
+ elif isinstance(evt.target, gr.Dropdown):
76
+ if hasattr(roop.globals, attribname):
77
+ setattr(roop.globals, attribname, evt.value)
78
+ return
79
+ raise gr.Error(f'Unhandled Setting for {evt.target}')
80
+
81
+
82
+ def on_settings_changed_misc(new_val, attribname):
83
+ if hasattr(roop.globals.CFG, attribname):
84
+ setattr(roop.globals.CFG, attribname, new_val)
85
+ else:
86
+ print("Didn't find attrib!")
87
+
88
+
89
+
90
+ def on_settings_changed(evt: gr.SelectData):
91
+ attribname = evt.target.elem_id
92
+ if isinstance(evt.target, gr.Checkbox):
93
+ if hasattr(roop.globals.CFG, attribname):
94
+ setattr(roop.globals.CFG, attribname, evt.selected)
95
+ return
96
+ elif isinstance(evt.target, gr.Dropdown):
97
+ if hasattr(roop.globals.CFG, attribname):
98
+ setattr(roop.globals.CFG, attribname, evt.value)
99
+ return
100
+
101
+ raise gr.Error(f'Unhandled Setting for {evt.target}')
102
+
103
+ def clean_temp():
104
+ from ui.main import prepare_environment
105
+
106
+ ui.globals.ui_input_thumbs.clear()
107
+ roop.globals.INPUT_FACESETS.clear()
108
+ roop.globals.TARGET_FACES.clear()
109
+ ui.globals.ui_target_thumbs = []
110
+ if not roop.globals.CFG.use_os_temp_folder:
111
+ clean_dir(os.environ["TEMP"])
112
+ prepare_environment()
113
+ gr.Info('Temp Files removed')
114
+ return None,None,None,None
115
+
116
+
117
+ def apply_settings(themes, input_server_name, input_server_port, output_template):
118
+ from ui.main import show_msg
119
+
120
+ roop.globals.CFG.selected_theme = themes
121
+ roop.globals.CFG.server_name = input_server_name
122
+ roop.globals.CFG.server_port = input_server_port
123
+ roop.globals.CFG.output_template = output_template
124
+ roop.globals.CFG.save()
125
+ show_msg('Settings saved')
126
+
127
+
128
+ def restart():
129
+ ui.globals.ui_restart_server = True