Instructions to use zeyuren2002/EvalMDE with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use zeyuren2002/EvalMDE with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("zeyuren2002/EvalMDE", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
File size: 9,161 Bytes
87a49e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | """ basic augmentations
"""
import random
import numpy as np
import torch
from torchvision import transforms
import torch.nn.functional as F
import torchvision.transforms.functional as TF
import logging
logger = logging.getLogger('root')
def resize(sample, new_H, new_W):
_, orig_H, orig_W = sample.img.shape
sample.img = F.interpolate(sample.img.unsqueeze(0), size=(new_H, new_W), mode='bilinear', align_corners=False, antialias=True).squeeze(0)
if sample.depth is not None:
sample.depth = F.interpolate(sample.depth.unsqueeze(0), size=(new_H, new_W), mode='nearest').squeeze(0)
if sample.depth_mask is not None:
sample.depth_mask = F.interpolate(sample.depth_mask.unsqueeze(0).float(), size=(new_H, new_W), mode='nearest').squeeze(0) > 0.5
if sample.normal is not None:
sample.normal = F.interpolate(sample.normal.unsqueeze(0), size=(new_H, new_W), mode='nearest').squeeze(0)
if sample.normal_mask is not None:
sample.normal_mask = F.interpolate(sample.normal_mask.unsqueeze(0).float(), size=(new_H, new_W), mode='nearest').squeeze(0) > 0.5
if sample.intrins is not None:
# NOTE: top-left is (0,0)
sample.intrins[0, 0] = sample.intrins[0, 0] * (new_W / orig_W) # fx
sample.intrins[1, 1] = sample.intrins[1, 1] * (new_H / orig_H) # fy
sample.intrins[0, 2] = (sample.intrins[0, 2] + 0.5) * (new_W / orig_W) - 0.5 # cx
sample.intrins[1, 2] = (sample.intrins[1, 2] + 0.5) * (new_H / orig_H) - 0.5 # cy
return sample
def pad(sample, lrtb):
l, r, t, b = lrtb
sample.img = F.pad(sample.img, (l, r, t, b), mode="constant", value=0)
if sample.depth is not None:
sample.depth = F.pad(sample.depth, (l, r, t, b), mode="constant", value=0)
if sample.depth_mask is not None:
sample.depth_mask = F.pad(sample.depth_mask, (l, r, t, b), mode="constant", value=False)
if sample.normal is not None:
sample.normal = F.pad(sample.normal, (l, r, t, b), mode="constant", value=0)
if sample.normal_mask is not None:
sample.normal_mask = F.pad(sample.normal_mask, (l, r, t, b), mode="constant", value=False)
if sample.intrins is not None:
sample.intrins[0, 2] = sample.intrins[0, 2] + l
sample.intrins[1, 2] = sample.intrins[1, 2] + t
return sample
def crop(sample, y, H, x, W):
sample.img = sample.img[:, y:y+H, x:x+W]
if sample.depth is not None:
sample.depth = sample.depth[:, y:y+H, x:x+W]
if sample.depth_mask is not None:
sample.depth_mask = sample.depth_mask[:, y:y+H, x:x+W]
if sample.normal is not None:
sample.normal = sample.normal[:, y:y+H, x:x+W]
if sample.normal_mask is not None:
sample.normal_mask = sample.normal_mask[:, y:y+H, x:x+W]
if sample.intrins is not None:
sample.intrins[0, 2] = sample.intrins[0, 2] - x
sample.intrins[1, 2] = sample.intrins[1, 2] - y
return sample
class ToTensor():
""" numpy arrays to torch tensors
"""
def __call__(self, sample):
sample.img = torch.from_numpy(sample.img).permute(2, 0, 1) # (3, H, W)
if sample.depth is not None:
sample.depth = torch.from_numpy(sample.depth).permute(2, 0, 1) # (1, H, W)
if sample.depth_mask is not None:
sample.depth_mask = torch.from_numpy(sample.depth_mask).permute(2, 0, 1) # (1, H, W)
if sample.normal is not None:
sample.normal = torch.from_numpy(sample.normal).permute(2, 0, 1) # (3, H, W)
if sample.normal_mask is not None:
sample.normal_mask = torch.from_numpy(sample.normal_mask).permute(2, 0, 1) # (1, H, W)
if sample.intrins is not None:
sample.intrins = torch.from_numpy(sample.intrins) # (3, 3)
return sample
class RandomIntrins():
""" randomize intrinsics
sample.img is a torch tensor of shape (3, H, W), normalized to [0, 1]
"""
def __call__(self, sample):
assert 'crop_H' in sample.info.keys()
assert 'crop_W' in sample.info.keys()
crop_H = sample.info['crop_H']
crop_W = sample.info['crop_W']
# height-based resizing
_, orig_H, orig_W = sample.img.shape
new_H = random.randrange(min(orig_H, crop_H), max(orig_H, crop_H)+1)
new_W = round((new_H / orig_H) * orig_W)
sample = resize(sample, new_H=new_H, new_W=new_W)
# pad if necessary
orig_H, orig_W = sample.img.shape[1], sample.img.shape[2]
l, r, t, b = 0, 0, 0, 0
if crop_H > orig_H:
t = b = crop_H - orig_H
if crop_W > orig_W:
l = r = crop_W - orig_W
sample = pad(sample, (l, r, t, b))
# crop
assert sample.img.shape[1] >= crop_H
assert sample.img.shape[2] >= crop_W
x = random.randint(0, sample.img.shape[2] - crop_W)
y = random.randint(0, sample.img.shape[1] - crop_H)
sample = crop(sample, y=y, H=crop_H, x=x, W=crop_W)
return sample
class Resize():
""" resize to (H, W)
sample.img is a torch tensor of shape (3, H, W), normalized to [0, 1]
"""
def __init__(self, H=480, W=640):
self.H = H
self.W = W
def __call__(self, sample):
return resize(sample, new_H=self.H, new_W=self.W)
class RandomCrop():
""" random crop
sample.img is a torch tensor of shape (3, H, W), normalized to [0, 1]
"""
def __init__(self, H=416, W=544):
self.H = H
self.W = W
def __call__(self, sample):
assert sample.img.shape[1] >= self.H
assert sample.img.shape[2] >= self.W
x = random.randint(0, sample.img.shape[2] - self.W)
y = random.randint(0, sample.img.shape[1] - self.H)
return crop(sample, y=y, H=self.H, x=x, W=self.W)
class NyuCrop():
""" crop image border for NYUv2 images
W = 43:608 / H = 45:472
sample.img is a torch tensor of shape (3, H, W), normalized to [0, 1]
"""
def __call__(self, sample):
return crop(sample, y=45, H=472-45, x=43, W=608-43)
class HorizontalFlip():
""" random horizontal flipping
sample.img is a torch tensor of shape (3, H, W), normalized to [0, 1]
"""
def __init__(self, p=0.5):
self.p = p
def __call__(self, sample):
if random.random() < self.p:
sample.img = TF.hflip(sample.img)
if sample.depth is not None:
sample.depth = TF.hflip(sample.depth)
if sample.depth_mask is not None:
sample.depth_mask = TF.hflip(sample.depth_mask)
if sample.normal is not None:
sample.normal = TF.hflip(sample.normal)
sample.normal[0, :, :] = -sample.normal[0, :, :]
if sample.normal_mask is not None:
sample.normal_mask = TF.hflip(sample.normal_mask)
if sample.intrins is not None:
# NOTE: top-left is (0,0)
_, H, W = sample.img.shape
sample.intrins[0, 2] = sample.intrins[0, 2] + 0.5 # top-left is (0.5, 0.5)
sample.intrins[0, 2] = W - sample.intrins[0, 2]
sample.intrins[0, 2] = sample.intrins[0, 2] - 0.5 # top-left is (0, 0)
sample.flipped = True
return sample
class ColorAugmentation():
""" color augmentation
sample.img is a torch tensor of shape (3, H, W), normalized to [0, 1]
"""
def __init__(self, gamma_range=(0.9, 1.1),
brightness_range=(0.75, 1.25),
color_range=(0.9, 1.1),
p=0.5):
self.gamma_range = gamma_range
self.brightness_range = brightness_range
self.color_range = color_range
self.p = p
def __call__(self, sample):
if random.random() < self.p:
# gamma augmentation
gamma = random.uniform(*self.gamma_range)
sample.img = sample.img ** gamma
# brightness augmentation
brightness = random.uniform(*self.brightness_range)
sample.img = sample.img * brightness
# color augmentation
colors = np.random.uniform(*self.color_range, size=3).astype(np.float32)
colors = torch.from_numpy(colors).view(3, 1, 1)
sample.img = sample.img * colors
# clip
sample.img = torch.clip(sample.img, 0, 1)
return sample
class Normalize():
""" mean & std: for image normalization
sample.img is a torch tensor of shape (3, H, W), normalized to [0, 1]
"""
def __init__(self, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)):
self.normalize = transforms.Normalize(mean=mean, std=std)
def __call__(self, sample):
sample.img = self.normalize(torch.clip(sample.img, min=0.0, max=1.0))
return sample
class ToDict():
def __call__(self, sample):
data_dict = {}
for k, v in vars(sample).items():
if v is not None:
data_dict[k] = v
return data_dict
|