InvokeAI 2.2.4.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- backend/__init__.py +0 -0
- backend/invoke_ai_web_server.py +1724 -0
- backend/modules/__init__.py +0 -0
- backend/modules/create_cmd_parser.py +55 -0
- backend/modules/get_canvas_generation_mode.py +117 -0
- backend/modules/parameters.py +71 -0
- backend/modules/parse_seed_weights.py +47 -0
- configs/INITIAL_MODELS.yaml +80 -0
- configs/models.yaml.example +29 -0
- configs/sd-concepts.txt +803 -0
- configs/stable-diffusion/v1-finetune.yaml +110 -0
- configs/stable-diffusion/v1-finetune_style.yaml +103 -0
- configs/stable-diffusion/v1-inference.yaml +79 -0
- configs/stable-diffusion/v1-inpainting-inference.yaml +79 -0
- configs/stable-diffusion/v1-m1-finetune.yaml +110 -0
- docs/assets/invoke-web-server-1.png +0 -0
- docs/assets/invoke_ai_banner.png +0 -0
- invokeai-2.2.4.5.data/data/assets/caution.png +0 -0
- invokeai-2.2.4.5.data/data/configs/INITIAL_MODELS.yaml +80 -0
- invokeai-2.2.4.5.data/data/configs/models.yaml.example +29 -0
- invokeai-2.2.4.5.data/data/configs/sd-concepts.txt +803 -0
- invokeai-2.2.4.5.data/data/configs/stable-diffusion/v1-finetune.yaml +110 -0
- invokeai-2.2.4.5.data/data/configs/stable-diffusion/v1-finetune_style.yaml +103 -0
- invokeai-2.2.4.5.data/data/configs/stable-diffusion/v1-inference.yaml +79 -0
- invokeai-2.2.4.5.data/data/configs/stable-diffusion/v1-inpainting-inference.yaml +79 -0
- invokeai-2.2.4.5.data/data/configs/stable-diffusion/v1-m1-finetune.yaml +110 -0
- invokeai-2.2.4.5.data/data/frontend/dist/assets/Inter-Bold.790c108b.ttf +0 -0
- invokeai-2.2.4.5.data/data/frontend/dist/assets/Inter.b9a8e5e2.ttf +0 -0
- invokeai-2.2.4.5.data/data/frontend/dist/assets/favicon.0d253ced.ico +0 -0
- invokeai-2.2.4.5.data/data/frontend/dist/assets/index-legacy-b741077f.js +48 -0
- invokeai-2.2.4.5.data/data/frontend/dist/assets/index.0dadf5d0.css +1 -0
- invokeai-2.2.4.5.data/data/frontend/dist/assets/index.21ff81eb.js +625 -0
- invokeai-2.2.4.5.data/data/frontend/dist/assets/logo.13003d72.png +0 -0
- invokeai-2.2.4.5.data/data/frontend/dist/assets/polyfills-legacy-dde3a68a.js +4 -0
- invokeai-2.2.4.5.data/data/frontend/dist/assets/polyfills.1ff60148.js +1 -0
- invokeai-2.2.4.5.data/data/frontend/dist/index.html +23 -0
- invokeai-2.2.4.5.data/data/scripts/configure_invokeai.py +797 -0
- invokeai-2.2.4.5.data/data/scripts/create_root.py +43 -0
- invokeai-2.2.4.5.data/data/scripts/dream.py +10 -0
- invokeai-2.2.4.5.data/data/scripts/images2prompt.py +30 -0
- invokeai-2.2.4.5.data/data/scripts/invoke.py +5 -0
- invokeai-2.2.4.5.data/data/scripts/legacy_api.py +681 -0
- invokeai-2.2.4.5.data/data/scripts/load_models.py +12 -0
- invokeai-2.2.4.5.data/data/scripts/merge_embeddings.py +124 -0
- invokeai-2.2.4.5.data/data/scripts/preload_models.py +12 -0
- invokeai-2.2.4.5.data/data/scripts/sd-metadata.py +22 -0
- invokeai-2.2.4.5.dist-info/LICENSE +21 -0
- invokeai-2.2.4.5.dist-info/LICENSE-ModelWeights.txt +294 -0
- invokeai-2.2.4.5.dist-info/METADATA +288 -0
- invokeai-2.2.4.5.dist-info/RECORD +169 -0
- invokeai-2.2.4.5.dist-info/WHEEL +5 -0
- invokeai-2.2.4.5.dist-info/entry_points.txt +4 -0
- invokeai-2.2.4.5.dist-info/top_level.txt +6 -0
- invokeai-2.2.4.5.dist-info/zip-safe +1 -0
- ldm/__init__.py +0 -0
- ldm/data/__init__.py +0 -0
- ldm/data/base.py +31 -0
- ldm/data/imagenet.py +466 -0
- ldm/data/lsun.py +126 -0
- ldm/data/personalized.py +202 -0
- ldm/data/personalized_style.py +169 -0
- ldm/generate.py +1159 -0
- ldm/invoke/CLI.py +960 -0
- ldm/invoke/__init__.py +3 -0
- ldm/invoke/args.py +1257 -0
- ldm/invoke/concepts_lib.py +169 -0
- ldm/invoke/conditioning.py +289 -0
- ldm/invoke/devices.py +27 -0
- ldm/invoke/generator/__init__.py +4 -0
- ldm/invoke/generator/base.py +335 -0
- ldm/invoke/generator/embiggen.py +501 -0
- ldm/invoke/generator/img2img.py +97 -0
- ldm/invoke/generator/inpaint.py +358 -0
- ldm/invoke/generator/omnibus.py +175 -0
- ldm/invoke/generator/txt2img.py +88 -0
- ldm/invoke/generator/txt2img2img.py +182 -0
- ldm/invoke/globals.py +31 -0
- ldm/invoke/image_util.py +70 -0
- ldm/invoke/log.py +66 -0
- ldm/invoke/model_cache.py +449 -0
- ldm/invoke/patchmatch.py +44 -0
- ldm/invoke/pngwriter.py +113 -0
- ldm/invoke/prompt_parser.py +655 -0
- ldm/invoke/readline.py +442 -0
- ldm/invoke/restoration/__init__.py +4 -0
- ldm/invoke/restoration/base.py +38 -0
- ldm/invoke/restoration/codeformer.py +103 -0
- ldm/invoke/restoration/codeformer_arch.py +276 -0
- ldm/invoke/restoration/gfpgan.py +84 -0
- ldm/invoke/restoration/outcrop.py +108 -0
- ldm/invoke/restoration/outpaint.py +92 -0
- ldm/invoke/restoration/realesrgan.py +90 -0
- ldm/invoke/restoration/vqgan_arch.py +435 -0
- ldm/invoke/seamless.py +30 -0
- ldm/invoke/server.py +282 -0
- ldm/invoke/server_legacy.py +246 -0
- ldm/invoke/txt2mask.py +136 -0
- ldm/lr_scheduler.py +143 -0
- ldm/models/__init__.py +0 -0
- ldm/models/autoencoder.py +596 -0
- ldm/models/diffusion/__init__.py +0 -0
- ldm/models/diffusion/classifier.py +355 -0
- ldm/models/diffusion/cross_attention_control.py +443 -0
- ldm/models/diffusion/cross_attention_map_saving.py +95 -0
- ldm/models/diffusion/ddim.py +111 -0
- ldm/models/diffusion/ddpm.py +2263 -0
- ldm/models/diffusion/ksampler.py +312 -0
- ldm/models/diffusion/plms.py +146 -0
- ldm/models/diffusion/sampler.py +450 -0
- ldm/models/diffusion/shared_invokeai_diffusion.py +244 -0
- ldm/modules/__init__.py +0 -0
- ldm/modules/attention.py +274 -0
- ldm/modules/diffusionmodules/__init__.py +0 -0
- ldm/modules/diffusionmodules/model.py +894 -0
- ldm/modules/diffusionmodules/openaimodel.py +1036 -0
- ldm/modules/diffusionmodules/util.py +314 -0
- ldm/modules/distributions/__init__.py +0 -0
- ldm/modules/distributions/distributions.py +104 -0
- ldm/modules/ema.py +88 -0
- ldm/modules/embedding_manager.py +413 -0
- ldm/modules/encoders/__init__.py +0 -0
- ldm/modules/encoders/modules.py +772 -0
- ldm/modules/image_degradation/__init__.py +6 -0
- ldm/modules/image_degradation/bsrgan.py +846 -0
- ldm/modules/image_degradation/bsrgan_light.py +751 -0
- ldm/modules/image_degradation/utils_image.py +1024 -0
- ldm/modules/losses/__init__.py +1 -0
- ldm/modules/losses/contperceptual.py +175 -0
- ldm/modules/losses/vqperceptual.py +240 -0
- ldm/modules/x_transformer.py +752 -0
- ldm/simplet2i.py +13 -0
- ldm/util.py +264 -0
- scripts/configure_invokeai.py +797 -0
- scripts/create_root.py +43 -0
- scripts/dream.py +10 -0
- scripts/images2prompt.py +30 -0
- scripts/invoke.py +5 -0
- scripts/legacy_api.py +681 -0
- scripts/load_models.py +12 -0
- scripts/merge_embeddings.py +124 -0
- scripts/orig_scripts/img2img.py +296 -0
- scripts/orig_scripts/inpaint.py +98 -0
- scripts/orig_scripts/knn2img.py +397 -0
- scripts/orig_scripts/sample_diffusion.py +313 -0
- scripts/orig_scripts/train_searcher.py +147 -0
- scripts/orig_scripts/txt2img.py +324 -0
- scripts/preload_models.py +12 -0
- scripts/sd-metadata.py +22 -0
- vendored/clip/clip/__init__.py +1 -0
- vendored/clip/clip/bpe_simple_vocab_16e6.txt.gz +0 -0
- vendored/clip/clip/clip.py +237 -0
- vendored/clip/clip/model.py +436 -0
- vendored/clip/clip/simple_tokenizer.py +132 -0
- vendored/clip/hubconf.py +42 -0
- vendored/clip/setup.py +21 -0
- vendored/clip/tests/test_consistency.py +25 -0
- vendored/clipseg/datasets/coco_wrapper.py +99 -0
- vendored/clipseg/datasets/pascal_zeroshot.py +60 -0
- vendored/clipseg/datasets/pfe_dataset.py +129 -0
- vendored/clipseg/datasets/phrasecut.py +380 -0
- vendored/clipseg/datasets/utils.py +68 -0
- vendored/clipseg/evaluation_utils.py +292 -0
- vendored/clipseg/general_utils.py +272 -0
- vendored/clipseg/metrics.py +271 -0
- vendored/clipseg/models/clipseg.py +538 -0
- vendored/clipseg/models/vitseg.py +286 -0
- vendored/clipseg/score.py +453 -0
- vendored/clipseg/setup.py +30 -0
- vendored/clipseg/training.py +266 -0
|
@@ -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,
|