forked from CompVis/taming-transformers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_checkpoints.py
197 lines (167 loc) · 5.44 KB
/
upload_checkpoints.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from typing import Optional, List, Dict
from PIL import Image
import io
import albumentations
from typing import Optional, Tuple, Iterator, List
import logging
import os
import glob
import urllib.parse
import argparse
import torch
from google.cloud import storage
from taming.models.vqgan import VQModel
from omegaconf import OmegaConf
import glob
import numpy as np
from taming.data.utils import custom_collate
import shutil
GCS_UPLOAD_TIMEOUT_SECS = 300
def get_default_storage_client():
return storage.Client()
def get_blob(
bucket_name: str,
blob_name: str,
download_blob: bool = False,
client: Optional[storage.Client] = None,
) -> storage.blob.Blob:
if not client:
client = get_default_storage_client()
bucket = client.bucket(bucket_name)
# Construct a client side representation of a blob.
# Note `Bucket.blob` differs from `Bucket.get_blob` as it doesn't retrieve
# any content from Google Cloud Storage. As we don't need additional data,
# using `Bucket.blob` is preferred here.
if download_blob:
blob = bucket.get_blob(blob_name)
else:
blob = bucket.blob(blob_name)
return blob
def get_gcs_path(bucket_name: str, object_relative_path: str) -> str:
return f"gs://{bucket_name}/{object_relative_path}"
def upload_blob(
bucket_name: str,
destination_blob_name: str,
source_file_name: str,
client: Optional[storage.Client] = None,
) -> str:
"""
Uploads a file to the bucket.
The ID of your GCS bucket
bucket_name = "your-bucket-name"
The path to your file to upload
source_file_name = "local/path/to/file"
The ID of your GCS object
destination_blob_name = "storage-object-name"
Returns the URI of the uploaded file.
"""
blob = get_blob(bucket_name, destination_blob_name, client=client)
blob.upload_from_filename(source_file_name, timeout=GCS_UPLOAD_TIMEOUT_SECS)
logging.debug(f"File {source_file_name} uploaded to {destination_blob_name}.")
return get_gcs_path(bucket_name, destination_blob_name)
def get_parser(**parser_kwargs):
parser = argparse.ArgumentParser(**parser_kwargs)
parser.add_argument(
"-i",
"--input_dir",
type=str,
default="",
help="path for input directory containing images",
)
parser.add_argument(
"-o",
"--output_gcs_path",
type=str,
default="",
help="gcs path to upload the images directory to",
)
parser.add_argument(
"-m",
"--model",
type=str,
help="full path of model checkpoint",
)
parser.add_argument(
"-c",
"--config_path",
type=str,
help="full path of yaml config the model was trained with",
)
parser.add_argument(
"-b",
"--batch_size",
type=int,
default=1,
help="batch size to use for forward pass"
)
return parser
def batch(image, processor):
if not image.mode == "RGB":
image = image.convert("RGB")
image = np.array(image).astype(np.uint8)
processed = processor(image=image)
return {
"image": (processed["image"]/127.5 - 1.0).astype(np.float32)
}
def sample(model, batch):
x = model.get_input(batch, 'image')
xrec, _ = model(x)
if x.shape[1] > 3:
assert xrec.shape[1] > 3
x = model.to_rgb(x)
xrec = model.to_rgb(xrec)
return x, xrec
def save_image(x, path):
c,h,w = x.shape
assert c==3
x = ((x.detach().cpu().numpy().transpose(1,2,0)+1.0)*127.5).clip(0,255).astype(np.uint8)
Image.fromarray(x).save(path)
def decode_gcs_uri(uri: str) -> Tuple[str, str]:
p = urllib.parse.urlparse(uri)
bucket_name = p.netloc
object_path = p.path.lstrip("/")
if p.query:
object_path += "?" + p.query
if p.fragment:
object_path += "#" + p.fragment
return bucket_name, object_path
if __name__=="__main__":
parser = get_parser()
args, unknown = parser.parse_known_args()
input_dir = args.input_dir
'''
ckpt_path = args.model
print('Found args: ', args, unknown)
state_dict = torch.load(ckpt_path, map_location='cpu')['state_dict']
config_path = args.config_path
config = OmegaConf.load(config_path)
model = VQModel(**config.model.params)
missing, unexpected = model.load_state_dict(state_dict)
input_dir = args.input_dir
output_dir = '/tmp/webclip_images/'
images_fns = glob.glob(input_dir + "/*.png")
print("Found image files: ", images_fns)
rescaler = albumentations.SmallestMaxSize(max_size=336)
cropper = albumentations.RandomCrop(
height=336,
width=336)
preprocessor = albumentations.Compose([rescaler, cropper])
size=336
rescaler = albumentations.Resize(height=size, width=size)
preprocessor = albumentations.Compose([rescaler])
for fn in images_fns:
img_num = fn.split('/')[-1]
pil_image = Image.open(fn)
print("image size: ", pil_image.size)
inp = batch(pil_image, processor=preprocessor)
x, xrec = sample(model, custom_collate([inp]))
save_image(x[0], output_dir + img_num + '.original.png')
save_image(xrec[0], output_dir + img_num)
shutil.make_archive('zipped_imgs', 'zip', output_dir)
'''
images_fns = glob.glob(input_dir + "*.ckpt")
images_fns = [input_dir]
print(images_fns)
for fn in images_fns:
bucket, object = decode_gcs_uri(args.output_gcs_path + fn)
upload_blob(bucket, object, fn)