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
ldm/data/imagenet.py
ADDED
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
import os, yaml, pickle, shutil, tarfile, glob
|
|
2
|
+
import cv2
|
|
3
|
+
import albumentations
|
|
4
|
+
import PIL
|
|
5
|
+
import numpy as np
|
|
6
|
+
import torchvision.transforms.functional as TF
|
|
7
|
+
from omegaconf import OmegaConf
|
|
8
|
+
from functools import partial
|
|
9
|
+
from PIL import Image
|
|
10
|
+
from tqdm import tqdm
|
|
11
|
+
from torch.utils.data import Dataset, Subset
|
|
12
|
+
|
|
13
|
+
import taming.data.utils as tdu
|
|
14
|
+
from taming.data.imagenet import (
|
|
15
|
+
str_to_indices,
|
|
16
|
+
give_synsets_from_indices,
|
|
17
|
+
download,
|
|
18
|
+
retrieve,
|
|
19
|
+
)
|
|
20
|
+
from taming.data.imagenet import ImagePaths
|
|
21
|
+
|
|
22
|
+
from ldm.modules.image_degradation import (
|
|
23
|
+
degradation_fn_bsr,
|
|
24
|
+
degradation_fn_bsr_light,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def synset2idx(path_to_yaml='data/index_synset.yaml'):
|
|
29
|
+
with open(path_to_yaml) as f:
|
|
30
|
+
di2s = yaml.load(f)
|
|
31
|
+
return dict((v, k) for k, v in di2s.items())
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class ImageNetBase(Dataset):
|
|
35
|
+
def __init__(self, config=None):
|
|
36
|
+
self.config = config or OmegaConf.create()
|
|
37
|
+
if not type(self.config) == dict:
|
|
38
|
+
self.config = OmegaConf.to_container(self.config)
|
|
39
|
+
self.keep_orig_class_label = self.config.get(
|
|
40
|
+
'keep_orig_class_label', False
|
|
41
|
+
)
|
|
42
|
+
self.process_images = True # if False we skip loading & processing images and self.data contains filepaths
|
|
43
|
+
self._prepare()
|
|
44
|
+
self._prepare_synset_to_human()
|
|
45
|
+
self._prepare_idx_to_synset()
|
|
46
|
+
self._prepare_human_to_integer_label()
|
|
47
|
+
self._load()
|
|
48
|
+
|
|
49
|
+
def __len__(self):
|
|
50
|
+
return len(self.data)
|
|
51
|
+
|
|
52
|
+
def __getitem__(self, i):
|
|
53
|
+
return self.data[i]
|
|
54
|
+
|
|
55
|
+
def _prepare(self):
|
|
56
|
+
raise NotImplementedError()
|
|
57
|
+
|
|
58
|
+
def _filter_relpaths(self, relpaths):
|
|
59
|
+
ignore = set(
|
|
60
|
+
[
|
|
61
|
+
'n06596364_9591.JPEG',
|
|
62
|
+
]
|
|
63
|
+
)
|
|
64
|
+
relpaths = [
|
|
65
|
+
rpath for rpath in relpaths if not rpath.split('/')[-1] in ignore
|
|
66
|
+
]
|
|
67
|
+
if 'sub_indices' in self.config:
|
|
68
|
+
indices = str_to_indices(self.config['sub_indices'])
|
|
69
|
+
synsets = give_synsets_from_indices(
|
|
70
|
+
indices, path_to_yaml=self.idx2syn
|
|
71
|
+
) # returns a list of strings
|
|
72
|
+
self.synset2idx = synset2idx(path_to_yaml=self.idx2syn)
|
|
73
|
+
files = []
|
|
74
|
+
for rpath in relpaths:
|
|
75
|
+
syn = rpath.split('/')[0]
|
|
76
|
+
if syn in synsets:
|
|
77
|
+
files.append(rpath)
|
|
78
|
+
return files
|
|
79
|
+
else:
|
|
80
|
+
return relpaths
|
|
81
|
+
|
|
82
|
+
def _prepare_synset_to_human(self):
|
|
83
|
+
SIZE = 2655750
|
|
84
|
+
URL = 'https://heibox.uni-heidelberg.de/f/9f28e956cd304264bb82/?dl=1'
|
|
85
|
+
self.human_dict = os.path.join(self.root, 'synset_human.txt')
|
|
86
|
+
if (
|
|
87
|
+
not os.path.exists(self.human_dict)
|
|
88
|
+
or not os.path.getsize(self.human_dict) == SIZE
|
|
89
|
+
):
|
|
90
|
+
download(URL, self.human_dict)
|
|
91
|
+
|
|
92
|
+
def _prepare_idx_to_synset(self):
|
|
93
|
+
URL = 'https://heibox.uni-heidelberg.de/f/d835d5b6ceda4d3aa910/?dl=1'
|
|
94
|
+
self.idx2syn = os.path.join(self.root, 'index_synset.yaml')
|
|
95
|
+
if not os.path.exists(self.idx2syn):
|
|
96
|
+
download(URL, self.idx2syn)
|
|
97
|
+
|
|
98
|
+
def _prepare_human_to_integer_label(self):
|
|
99
|
+
URL = 'https://heibox.uni-heidelberg.de/f/2362b797d5be43b883f6/?dl=1'
|
|
100
|
+
self.human2integer = os.path.join(
|
|
101
|
+
self.root, 'imagenet1000_clsidx_to_labels.txt'
|
|
102
|
+
)
|
|
103
|
+
if not os.path.exists(self.human2integer):
|
|
104
|
+
download(URL, self.human2integer)
|
|
105
|
+
with open(self.human2integer, 'r') as f:
|
|
106
|
+
lines = f.read().splitlines()
|
|
107
|
+
assert len(lines) == 1000
|
|
108
|
+
self.human2integer_dict = dict()
|
|
109
|
+
for line in lines:
|
|
110
|
+
value, key = line.split(':')
|
|
111
|
+
self.human2integer_dict[key] = int(value)
|
|
112
|
+
|
|
113
|
+
def _load(self):
|
|
114
|
+
with open(self.txt_filelist, 'r') as f:
|
|
115
|
+
self.relpaths = f.read().splitlines()
|
|
116
|
+
l1 = len(self.relpaths)
|
|
117
|
+
self.relpaths = self._filter_relpaths(self.relpaths)
|
|
118
|
+
print(
|
|
119
|
+
'Removed {} files from filelist during filtering.'.format(
|
|
120
|
+
l1 - len(self.relpaths)
|
|
121
|
+
)
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
self.synsets = [p.split('/')[0] for p in self.relpaths]
|
|
125
|
+
self.abspaths = [os.path.join(self.datadir, p) for p in self.relpaths]
|
|
126
|
+
|
|
127
|
+
unique_synsets = np.unique(self.synsets)
|
|
128
|
+
class_dict = dict(
|
|
129
|
+
(synset, i) for i, synset in enumerate(unique_synsets)
|
|
130
|
+
)
|
|
131
|
+
if not self.keep_orig_class_label:
|
|
132
|
+
self.class_labels = [class_dict[s] for s in self.synsets]
|
|
133
|
+
else:
|
|
134
|
+
self.class_labels = [self.synset2idx[s] for s in self.synsets]
|
|
135
|
+
|
|
136
|
+
with open(self.human_dict, 'r') as f:
|
|
137
|
+
human_dict = f.read().splitlines()
|
|
138
|
+
human_dict = dict(line.split(maxsplit=1) for line in human_dict)
|
|
139
|
+
|
|
140
|
+
self.human_labels = [human_dict[s] for s in self.synsets]
|
|
141
|
+
|
|
142
|
+
labels = {
|
|
143
|
+
'relpath': np.array(self.relpaths),
|
|
144
|
+
'synsets': np.array(self.synsets),
|
|
145
|
+
'class_label': np.array(self.class_labels),
|
|
146
|
+
'human_label': np.array(self.human_labels),
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if self.process_images:
|
|
150
|
+
self.size = retrieve(self.config, 'size', default=256)
|
|
151
|
+
self.data = ImagePaths(
|
|
152
|
+
self.abspaths,
|
|
153
|
+
labels=labels,
|
|
154
|
+
size=self.size,
|
|
155
|
+
random_crop=self.random_crop,
|
|
156
|
+
)
|
|
157
|
+
else:
|
|
158
|
+
self.data = self.abspaths
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
class ImageNetTrain(ImageNetBase):
|
|
162
|
+
NAME = 'ILSVRC2012_train'
|
|
163
|
+
URL = 'http://www.image-net.org/challenges/LSVRC/2012/'
|
|
164
|
+
AT_HASH = 'a306397ccf9c2ead27155983c254227c0fd938e2'
|
|
165
|
+
FILES = [
|
|
166
|
+
'ILSVRC2012_img_train.tar',
|
|
167
|
+
]
|
|
168
|
+
SIZES = [
|
|
169
|
+
147897477120,
|
|
170
|
+
]
|
|
171
|
+
|
|
172
|
+
def __init__(self, process_images=True, data_root=None, **kwargs):
|
|
173
|
+
self.process_images = process_images
|
|
174
|
+
self.data_root = data_root
|
|
175
|
+
super().__init__(**kwargs)
|
|
176
|
+
|
|
177
|
+
def _prepare(self):
|
|
178
|
+
if self.data_root:
|
|
179
|
+
self.root = os.path.join(self.data_root, self.NAME)
|
|
180
|
+
else:
|
|
181
|
+
cachedir = os.environ.get(
|
|
182
|
+
'XDG_CACHE_HOME', os.path.expanduser('~/.cache')
|
|
183
|
+
)
|
|
184
|
+
self.root = os.path.join(cachedir, 'autoencoders/data', self.NAME)
|
|
185
|
+
|
|
186
|
+
self.datadir = os.path.join(self.root, 'data')
|
|
187
|
+
self.txt_filelist = os.path.join(self.root, 'filelist.txt')
|
|
188
|
+
self.expected_length = 1281167
|
|
189
|
+
self.random_crop = retrieve(
|
|
190
|
+
self.config, 'ImageNetTrain/random_crop', default=True
|
|
191
|
+
)
|
|
192
|
+
if not tdu.is_prepared(self.root):
|
|
193
|
+
# prep
|
|
194
|
+
print('Preparing dataset {} in {}'.format(self.NAME, self.root))
|
|
195
|
+
|
|
196
|
+
datadir = self.datadir
|
|
197
|
+
if not os.path.exists(datadir):
|
|
198
|
+
path = os.path.join(self.root, self.FILES[0])
|
|
199
|
+
if (
|
|
200
|
+
not os.path.exists(path)
|
|
201
|
+
or not os.path.getsize(path) == self.SIZES[0]
|
|
202
|
+
):
|
|
203
|
+
import academictorrents as at
|
|
204
|
+
|
|
205
|
+
atpath = at.get(self.AT_HASH, datastore=self.root)
|
|
206
|
+
assert atpath == path
|
|
207
|
+
|
|
208
|
+
print('Extracting {} to {}'.format(path, datadir))
|
|
209
|
+
os.makedirs(datadir, exist_ok=True)
|
|
210
|
+
with tarfile.open(path, 'r:') as tar:
|
|
211
|
+
tar.extractall(path=datadir)
|
|
212
|
+
|
|
213
|
+
print('Extracting sub-tars.')
|
|
214
|
+
subpaths = sorted(glob.glob(os.path.join(datadir, '*.tar')))
|
|
215
|
+
for subpath in tqdm(subpaths):
|
|
216
|
+
subdir = subpath[: -len('.tar')]
|
|
217
|
+
os.makedirs(subdir, exist_ok=True)
|
|
218
|
+
with tarfile.open(subpath, 'r:') as tar:
|
|
219
|
+
tar.extractall(path=subdir)
|
|
220
|
+
|
|
221
|
+
filelist = glob.glob(os.path.join(datadir, '**', '*.JPEG'))
|
|
222
|
+
filelist = [os.path.relpath(p, start=datadir) for p in filelist]
|
|
223
|
+
filelist = sorted(filelist)
|
|
224
|
+
filelist = '\n'.join(filelist) + '\n'
|
|
225
|
+
with open(self.txt_filelist, 'w') as f:
|
|
226
|
+
f.write(filelist)
|
|
227
|
+
|
|
228
|
+
tdu.mark_prepared(self.root)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
class ImageNetValidation(ImageNetBase):
|
|
232
|
+
NAME = 'ILSVRC2012_validation'
|
|
233
|
+
URL = 'http://www.image-net.org/challenges/LSVRC/2012/'
|
|
234
|
+
AT_HASH = '5d6d0df7ed81efd49ca99ea4737e0ae5e3a5f2e5'
|
|
235
|
+
VS_URL = 'https://heibox.uni-heidelberg.de/f/3e0f6e9c624e45f2bd73/?dl=1'
|
|
236
|
+
FILES = [
|
|
237
|
+
'ILSVRC2012_img_val.tar',
|
|
238
|
+
'validation_synset.txt',
|
|
239
|
+
]
|
|
240
|
+
SIZES = [
|
|
241
|
+
6744924160,
|
|
242
|
+
1950000,
|
|
243
|
+
]
|
|
244
|
+
|
|
245
|
+
def __init__(self, process_images=True, data_root=None, **kwargs):
|
|
246
|
+
self.data_root = data_root
|
|
247
|
+
self.process_images = process_images
|
|
248
|
+
super().__init__(**kwargs)
|
|
249
|
+
|
|
250
|
+
def _prepare(self):
|
|
251
|
+
if self.data_root:
|
|
252
|
+
self.root = os.path.join(self.data_root, self.NAME)
|
|
253
|
+
else:
|
|
254
|
+
cachedir = os.environ.get(
|
|
255
|
+
'XDG_CACHE_HOME', os.path.expanduser('~/.cache')
|
|
256
|
+
)
|
|
257
|
+
self.root = os.path.join(cachedir, 'autoencoders/data', self.NAME)
|
|
258
|
+
self.datadir = os.path.join(self.root, 'data')
|
|
259
|
+
self.txt_filelist = os.path.join(self.root, 'filelist.txt')
|
|
260
|
+
self.expected_length = 50000
|
|
261
|
+
self.random_crop = retrieve(
|
|
262
|
+
self.config, 'ImageNetValidation/random_crop', default=False
|
|
263
|
+
)
|
|
264
|
+
if not tdu.is_prepared(self.root):
|
|
265
|
+
# prep
|
|
266
|
+
print('Preparing dataset {} in {}'.format(self.NAME, self.root))
|
|
267
|
+
|
|
268
|
+
datadir = self.datadir
|
|
269
|
+
if not os.path.exists(datadir):
|
|
270
|
+
path = os.path.join(self.root, self.FILES[0])
|
|
271
|
+
if (
|
|
272
|
+
not os.path.exists(path)
|
|
273
|
+
or not os.path.getsize(path) == self.SIZES[0]
|
|
274
|
+
):
|
|
275
|
+
import academictorrents as at
|
|
276
|
+
|
|
277
|
+
atpath = at.get(self.AT_HASH, datastore=self.root)
|
|
278
|
+
assert atpath == path
|
|
279
|
+
|
|
280
|
+
print('Extracting {} to {}'.format(path, datadir))
|
|
281
|
+
os.makedirs(datadir, exist_ok=True)
|
|
282
|
+
with tarfile.open(path, 'r:') as tar:
|
|
283
|
+
tar.extractall(path=datadir)
|
|
284
|
+
|
|
285
|
+
vspath = os.path.join(self.root, self.FILES[1])
|
|
286
|
+
if (
|
|
287
|
+
not os.path.exists(vspath)
|
|
288
|
+
or not os.path.getsize(vspath) == self.SIZES[1]
|
|
289
|
+
):
|
|
290
|
+
download(self.VS_URL, vspath)
|
|
291
|
+
|
|
292
|
+
with open(vspath, 'r') as f:
|
|
293
|
+
synset_dict = f.read().splitlines()
|
|
294
|
+
synset_dict = dict(line.split() for line in synset_dict)
|
|
295
|
+
|
|
296
|
+
print('Reorganizing into synset folders')
|
|
297
|
+
synsets = np.unique(list(synset_dict.values()))
|
|
298
|
+
for s in synsets:
|
|
299
|
+
os.makedirs(os.path.join(datadir, s), exist_ok=True)
|
|
300
|
+
for k, v in synset_dict.items():
|
|
301
|
+
src = os.path.join(datadir, k)
|
|
302
|
+
dst = os.path.join(datadir, v)
|
|
303
|
+
shutil.move(src, dst)
|
|
304
|
+
|
|
305
|
+
filelist = glob.glob(os.path.join(datadir, '**', '*.JPEG'))
|
|
306
|
+
filelist = [os.path.relpath(p, start=datadir) for p in filelist]
|
|
307
|
+
filelist = sorted(filelist)
|
|
308
|
+
filelist = '\n'.join(filelist) + '\n'
|
|
309
|
+
with open(self.txt_filelist, 'w') as f:
|
|
310
|
+
f.write(filelist)
|
|
311
|
+
|
|
312
|
+
tdu.mark_prepared(self.root)
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
class ImageNetSR(Dataset):
|
|
316
|
+
def __init__(
|
|
317
|
+
self,
|
|
318
|
+
size=None,
|
|
319
|
+
degradation=None,
|
|
320
|
+
downscale_f=4,
|
|
321
|
+
min_crop_f=0.5,
|
|
322
|
+
max_crop_f=1.0,
|
|
323
|
+
random_crop=True,
|
|
324
|
+
):
|
|
325
|
+
"""
|
|
326
|
+
Imagenet Superresolution Dataloader
|
|
327
|
+
Performs following ops in order:
|
|
328
|
+
1. crops a crop of size s from image either as random or center crop
|
|
329
|
+
2. resizes crop to size with cv2.area_interpolation
|
|
330
|
+
3. degrades resized crop with degradation_fn
|
|
331
|
+
|
|
332
|
+
:param size: resizing to size after cropping
|
|
333
|
+
:param degradation: degradation_fn, e.g. cv_bicubic or bsrgan_light
|
|
334
|
+
:param downscale_f: Low Resolution Downsample factor
|
|
335
|
+
:param min_crop_f: determines crop size s,
|
|
336
|
+
where s = c * min_img_side_len with c sampled from interval (min_crop_f, max_crop_f)
|
|
337
|
+
:param max_crop_f: ""
|
|
338
|
+
:param data_root:
|
|
339
|
+
:param random_crop:
|
|
340
|
+
"""
|
|
341
|
+
self.base = self.get_base()
|
|
342
|
+
assert size
|
|
343
|
+
assert (size / downscale_f).is_integer()
|
|
344
|
+
self.size = size
|
|
345
|
+
self.LR_size = int(size / downscale_f)
|
|
346
|
+
self.min_crop_f = min_crop_f
|
|
347
|
+
self.max_crop_f = max_crop_f
|
|
348
|
+
assert max_crop_f <= 1.0
|
|
349
|
+
self.center_crop = not random_crop
|
|
350
|
+
|
|
351
|
+
self.image_rescaler = albumentations.SmallestMaxSize(
|
|
352
|
+
max_size=size, interpolation=cv2.INTER_AREA
|
|
353
|
+
)
|
|
354
|
+
|
|
355
|
+
self.pil_interpolation = (
|
|
356
|
+
False # gets reset later if incase interp_op is from pillow
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
if degradation == 'bsrgan':
|
|
360
|
+
self.degradation_process = partial(
|
|
361
|
+
degradation_fn_bsr, sf=downscale_f
|
|
362
|
+
)
|
|
363
|
+
|
|
364
|
+
elif degradation == 'bsrgan_light':
|
|
365
|
+
self.degradation_process = partial(
|
|
366
|
+
degradation_fn_bsr_light, sf=downscale_f
|
|
367
|
+
)
|
|
368
|
+
|
|
369
|
+
else:
|
|
370
|
+
interpolation_fn = {
|
|
371
|
+
'cv_nearest': cv2.INTER_NEAREST,
|
|
372
|
+
'cv_bilinear': cv2.INTER_LINEAR,
|
|
373
|
+
'cv_bicubic': cv2.INTER_CUBIC,
|
|
374
|
+
'cv_area': cv2.INTER_AREA,
|
|
375
|
+
'cv_lanczos': cv2.INTER_LANCZOS4,
|
|
376
|
+
'pil_nearest': PIL.Image.NEAREST,
|
|
377
|
+
'pil_bilinear': PIL.Image.BILINEAR,
|
|
378
|
+
'pil_bicubic': PIL.Image.BICUBIC,
|
|
379
|
+
'pil_box': PIL.Image.BOX,
|
|
380
|
+
'pil_hamming': PIL.Image.HAMMING,
|
|
381
|
+
'pil_lanczos': PIL.Image.LANCZOS,
|
|
382
|
+
}[degradation]
|
|
383
|
+
|
|
384
|
+
self.pil_interpolation = degradation.startswith('pil_')
|
|
385
|
+
|
|
386
|
+
if self.pil_interpolation:
|
|
387
|
+
self.degradation_process = partial(
|
|
388
|
+
TF.resize,
|
|
389
|
+
size=self.LR_size,
|
|
390
|
+
interpolation=interpolation_fn,
|
|
391
|
+
)
|
|
392
|
+
|
|
393
|
+
else:
|
|
394
|
+
self.degradation_process = albumentations.SmallestMaxSize(
|
|
395
|
+
max_size=self.LR_size, interpolation=interpolation_fn
|
|
396
|
+
)
|
|
397
|
+
|
|
398
|
+
def __len__(self):
|
|
399
|
+
return len(self.base)
|
|
400
|
+
|
|
401
|
+
def __getitem__(self, i):
|
|
402
|
+
example = self.base[i]
|
|
403
|
+
image = Image.open(example['file_path_'])
|
|
404
|
+
|
|
405
|
+
if not image.mode == 'RGB':
|
|
406
|
+
image = image.convert('RGB')
|
|
407
|
+
|
|
408
|
+
image = np.array(image).astype(np.uint8)
|
|
409
|
+
|
|
410
|
+
min_side_len = min(image.shape[:2])
|
|
411
|
+
crop_side_len = min_side_len * np.random.uniform(
|
|
412
|
+
self.min_crop_f, self.max_crop_f, size=None
|
|
413
|
+
)
|
|
414
|
+
crop_side_len = int(crop_side_len)
|
|
415
|
+
|
|
416
|
+
if self.center_crop:
|
|
417
|
+
self.cropper = albumentations.CenterCrop(
|
|
418
|
+
height=crop_side_len, width=crop_side_len
|
|
419
|
+
)
|
|
420
|
+
|
|
421
|
+
else:
|
|
422
|
+
self.cropper = albumentations.RandomCrop(
|
|
423
|
+
height=crop_side_len, width=crop_side_len
|
|
424
|
+
)
|
|
425
|
+
|
|
426
|
+
image = self.cropper(image=image)['image']
|
|
427
|
+
image = self.image_rescaler(image=image)['image']
|
|
428
|
+
|
|
429
|
+
if self.pil_interpolation:
|
|
430
|
+
image_pil = PIL.Image.fromarray(image)
|
|
431
|
+
LR_image = self.degradation_process(image_pil)
|
|
432
|
+
LR_image = np.array(LR_image).astype(np.uint8)
|
|
433
|
+
|
|
434
|
+
else:
|
|
435
|
+
LR_image = self.degradation_process(image=image)['image']
|
|
436
|
+
|
|
437
|
+
example['image'] = (image / 127.5 - 1.0).astype(np.float32)
|
|
438
|
+
example['LR_image'] = (LR_image / 127.5 - 1.0).astype(np.float32)
|
|
439
|
+
|
|
440
|
+
return example
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
class ImageNetSRTrain(ImageNetSR):
|
|
444
|
+
def __init__(self, **kwargs):
|
|
445
|
+
super().__init__(**kwargs)
|
|
446
|
+
|
|
447
|
+
def get_base(self):
|
|
448
|
+
with open('data/imagenet_train_hr_indices.p', 'rb') as f:
|
|
449
|
+
indices = pickle.load(f)
|
|
450
|
+
dset = ImageNetTrain(
|
|
451
|
+
process_images=False,
|
|
452
|
+
)
|
|
453
|
+
return Subset(dset, indices)
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
class ImageNetSRValidation(ImageNetSR):
|
|
457
|
+
def __init__(self, **kwargs):
|
|
458
|
+
super().__init__(**kwargs)
|
|
459
|
+
|
|
460
|
+
def get_base(self):
|
|
461
|
+
with open('data/imagenet_val_hr_indices.p', 'rb') as f:
|
|
462
|
+
indices = pickle.load(f)
|
|
463
|
+
dset = ImageNetValidation(
|
|
464
|
+
process_images=False,
|
|
465
|
+
)
|
|
466
|
+
return Subset(dset, indices)
|
ldm/data/lsun.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import numpy as np
|
|
3
|
+
import PIL
|
|
4
|
+
from PIL import Image
|
|
5
|
+
from torch.utils.data import Dataset
|
|
6
|
+
from torchvision import transforms
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class LSUNBase(Dataset):
|
|
10
|
+
def __init__(
|
|
11
|
+
self,
|
|
12
|
+
txt_file,
|
|
13
|
+
data_root,
|
|
14
|
+
size=None,
|
|
15
|
+
interpolation='bicubic',
|
|
16
|
+
flip_p=0.5,
|
|
17
|
+
):
|
|
18
|
+
self.data_paths = txt_file
|
|
19
|
+
self.data_root = data_root
|
|
20
|
+
with open(self.data_paths, 'r') as f:
|
|
21
|
+
self.image_paths = f.read().splitlines()
|
|
22
|
+
self._length = len(self.image_paths)
|
|
23
|
+
self.labels = {
|
|
24
|
+
'relative_file_path_': [l for l in self.image_paths],
|
|
25
|
+
'file_path_': [
|
|
26
|
+
os.path.join(self.data_root, l) for l in self.image_paths
|
|
27
|
+
],
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
self.size = size
|
|
31
|
+
self.interpolation = {
|
|
32
|
+
'linear': PIL.Image.LINEAR,
|
|
33
|
+
'bilinear': PIL.Image.BILINEAR,
|
|
34
|
+
'bicubic': PIL.Image.BICUBIC,
|
|
35
|
+
'lanczos': PIL.Image.LANCZOS,
|
|
36
|
+
}[interpolation]
|
|
37
|
+
self.flip = transforms.RandomHorizontalFlip(p=flip_p)
|
|
38
|
+
|
|
39
|
+
def __len__(self):
|
|
40
|
+
return self._length
|
|
41
|
+
|
|
42
|
+
def __getitem__(self, i):
|
|
43
|
+
example = dict((k, self.labels[k][i]) for k in self.labels)
|
|
44
|
+
image = Image.open(example['file_path_'])
|
|
45
|
+
if not image.mode == 'RGB':
|
|
46
|
+
image = image.convert('RGB')
|
|
47
|
+
|
|
48
|
+
# default to score-sde preprocessing
|
|
49
|
+
img = np.array(image).astype(np.uint8)
|
|
50
|
+
crop = min(img.shape[0], img.shape[1])
|
|
51
|
+
h, w, = (
|
|
52
|
+
img.shape[0],
|
|
53
|
+
img.shape[1],
|
|
54
|
+
)
|
|
55
|
+
img = img[
|
|
56
|
+
(h - crop) // 2 : (h + crop) // 2,
|
|
57
|
+
(w - crop) // 2 : (w + crop) // 2,
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
image = Image.fromarray(img)
|
|
61
|
+
if self.size is not None:
|
|
62
|
+
image = image.resize(
|
|
63
|
+
(self.size, self.size), resample=self.interpolation
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
image = self.flip(image)
|
|
67
|
+
image = np.array(image).astype(np.uint8)
|
|
68
|
+
example['image'] = (image / 127.5 - 1.0).astype(np.float32)
|
|
69
|
+
return example
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class LSUNChurchesTrain(LSUNBase):
|
|
73
|
+
def __init__(self, **kwargs):
|
|
74
|
+
super().__init__(
|
|
75
|
+
txt_file='data/lsun/church_outdoor_train.txt',
|
|
76
|
+
data_root='data/lsun/churches',
|
|
77
|
+
**kwargs
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class LSUNChurchesValidation(LSUNBase):
|
|
82
|
+
def __init__(self, flip_p=0.0, **kwargs):
|
|
83
|
+
super().__init__(
|
|
84
|
+
txt_file='data/lsun/church_outdoor_val.txt',
|
|
85
|
+
data_root='data/lsun/churches',
|
|
86
|
+
flip_p=flip_p,
|
|
87
|
+
**kwargs
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class LSUNBedroomsTrain(LSUNBase):
|
|
92
|
+
def __init__(self, **kwargs):
|
|
93
|
+
super().__init__(
|
|
94
|
+
txt_file='data/lsun/bedrooms_train.txt',
|
|
95
|
+
data_root='data/lsun/bedrooms',
|
|
96
|
+
**kwargs
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class LSUNBedroomsValidation(LSUNBase):
|
|
101
|
+
def __init__(self, flip_p=0.0, **kwargs):
|
|
102
|
+
super().__init__(
|
|
103
|
+
txt_file='data/lsun/bedrooms_val.txt',
|
|
104
|
+
data_root='data/lsun/bedrooms',
|
|
105
|
+
flip_p=flip_p,
|
|
106
|
+
**kwargs
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class LSUNCatsTrain(LSUNBase):
|
|
111
|
+
def __init__(self, **kwargs):
|
|
112
|
+
super().__init__(
|
|
113
|
+
txt_file='data/lsun/cat_train.txt',
|
|
114
|
+
data_root='data/lsun/cats',
|
|
115
|
+
**kwargs
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class LSUNCatsValidation(LSUNBase):
|
|
120
|
+
def __init__(self, flip_p=0.0, **kwargs):
|
|
121
|
+
super().__init__(
|
|
122
|
+
txt_file='data/lsun/cat_val.txt',
|
|
123
|
+
data_root='data/lsun/cats',
|
|
124
|
+
flip_p=flip_p,
|
|
125
|
+
**kwargs
|
|
126
|
+
)
|