File size: 4,769 Bytes
e508563 0ab58fa 5db6fa7 e508563 0ab58fa e508563 0ab58fa c4ee5c3 0ab58fa e508563 0ab58fa e508563 af47b42 e508563 5db6fa7 e508563 423e800 e508563 af47b42 e508563 423e800 e508563 5db6fa7 e508563 af47b42 e508563 5db6fa7 e508563 |
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 |
import base64
import functools
import io
import logging
import random
import beartype
import einops.layers.torch
import numpy as np
import requests
import torch
from jaxtyping import Int, Integer, UInt8, jaxtyped
from PIL import Image, ImageDraw
from torch import Tensor
from torchvision.transforms import v2
logger = logging.getLogger("data.py")
R2_URL = "https://pub-129e98faed1048af94c4d4119ea47be7.r2.dev"
@beartype.beartype
@functools.lru_cache(maxsize=512)
def get_img(i: int) -> Image.Image:
fpath = f"/images/ADE_val_{i + 1:08}.jpg"
url = R2_URL + fpath
logger.info("Getting image from '%s'.", url)
return Image.open(requests.get(url, stream=True).raw)
@beartype.beartype
@functools.lru_cache(maxsize=512)
def get_seg(i: int) -> Image.Image:
fpath = f"/annotations/ADE_val_{i + 1:08}.png"
url = R2_URL + fpath
logger.info("Getting annotations from '%s'.", url)
return Image.open(requests.get(url, stream=True).raw)
@jaxtyped(typechecker=beartype.beartype)
def make_colors() -> UInt8[np.ndarray, "n 3"]:
values = (0, 51, 102, 153, 204, 255)
colors = []
for r in values:
for g in values:
for b in values:
colors.append((r, g, b))
# Fixed seed
random.Random(42).shuffle(colors)
colors = np.array(colors, dtype=np.uint8)
# Fixed colors. Must be synced with Segmentation.elm.
colors[2] = np.array([201, 249, 255], dtype=np.uint8)
colors[2] = np.array([201, 249, 255], dtype=np.uint8)
colors[4] = np.array([151, 204, 4], dtype=np.uint8)
colors[13] = np.array([104, 139, 88], dtype=np.uint8)
colors[16] = np.array([54, 48, 32], dtype=np.uint8)
colors[21] = np.array([120, 202, 210], dtype=np.uint8) # water
colors[26] = np.array([45, 125, 210], dtype=np.uint8)
colors[29] = np.array([116, 142, 84], dtype=np.uint8)
colors[46] = np.array([238, 185, 2], dtype=np.uint8)
colors[52] = np.array([88, 91, 86], dtype=np.uint8)
colors[60] = np.array([72, 99, 156], dtype=np.uint8) # river
colors[72] = np.array([76, 46, 5], dtype=np.uint8)
colors[94] = np.array([12, 15, 10], dtype=np.uint8)
return colors
colors = make_colors()
resize_transform = v2.Compose([
v2.Resize((512, 512), interpolation=v2.InterpolationMode.NEAREST),
v2.CenterCrop((448, 448)),
])
@beartype.beartype
def to_sized(img_raw: Image.Image) -> Image.Image:
return resize_transform(img_raw)
u8_transform = v2.Compose([
v2.ToImage(),
einops.layers.torch.Rearrange("() width height -> width height"),
])
@beartype.beartype
def to_u8(seg_raw: Image.Image) -> UInt8[Tensor, "width height"]:
return u8_transform(seg_raw)
@jaxtyped(typechecker=beartype.beartype)
def upsample(
x_WH: Int[Tensor, "width_ps height_ps"],
) -> UInt8[Tensor, "width_px height_px"]:
return (
torch.nn.functional.interpolate(
x_WH.view((1, 1, 16, 16)).float(),
scale_factor=28,
)
.view((448, 448))
.type(torch.uint8)
)
@jaxtyped(typechecker=beartype.beartype)
def u8_to_overlay(
map: Integer[Tensor, "width_ps height_ps"],
img: Image.Image,
*,
opacity: float = 0.5,
) -> Image.Image:
iw_np, ih_np = map.shape
iw_px, ih_px = img.size
pw_px, ph_px = iw_px // iw_np, ih_px // ih_np
# Create a transparent overlay
overlay = Image.new("RGBA", img.size, (0, 0, 0, 0))
draw = ImageDraw.Draw(overlay)
# Using semi-transparent red (255, 0, 0, alpha)
for p, i in enumerate(map.view(-1).tolist()):
x_np, y_np = p % iw_np, p // ih_np
draw.rectangle(
[
(x_np * pw_px, y_np * ph_px),
(x_np * pw_px + pw_px, y_np * ph_px + ph_px),
],
fill=(*colors[i - 1], int(opacity * 256)),
)
# Composite the original image and the overlay
return Image.alpha_composite(img.convert("RGBA"), overlay)
@jaxtyped(typechecker=beartype.beartype)
def u8_to_img(map: UInt8[Tensor, "width height"]) -> Image.Image:
map = map.cpu().numpy()
width, height = map.shape
colored = np.zeros((width, height, 3), dtype=np.uint8)
for i, color in enumerate(colors):
colored[map == i + 1, :] = color
return Image.fromarray(colored)
@jaxtyped(typechecker=beartype.beartype)
def to_classes(map: Integer[Tensor, "width height"]) -> list[int]:
# Integer is any signed or unsigned int: https://docs.kidger.site/jaxtyping/api/array/#dtype
return list(set(map.view(-1).tolist()))
@beartype.beartype
def img_to_base64(img: Image.Image) -> str:
buf = io.BytesIO()
img.save(buf, format="webp", lossless=True)
b64 = base64.b64encode(buf.getvalue())
s64 = b64.decode("utf8")
return "data:image/webp;base64," + s64
|