Spaces:
Running
on
Zero
Running
on
Zero
init
Browse files- utils/img.py +28 -0
utils/img.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import cv2
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn.functional as F
|
| 7 |
+
import torchvision.transforms as T
|
| 8 |
+
from einops import rearrange
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def create_coordinate(h, w, start=0, end=1, device="cuda", dtype=torch.float32):
|
| 12 |
+
# Create a grid of coordinates
|
| 13 |
+
x = torch.linspace(start, end, h, device=device, dtype=dtype)
|
| 14 |
+
y = torch.linspace(start, end, w, device=device, dtype=dtype)
|
| 15 |
+
# Create a 2D map using meshgrid
|
| 16 |
+
xx, yy = torch.meshgrid(x, y, indexing="ij")
|
| 17 |
+
# Stack the x and y coordinates to create the final map
|
| 18 |
+
coord_map = torch.stack([xx, yy], axis=-1)[None, ...]
|
| 19 |
+
coords = rearrange(coord_map, "b h w c -> b (h w) c", h=h, w=w)
|
| 20 |
+
return coords
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class PILToTensor:
|
| 24 |
+
"""Convert PIL Image to Tensor"""
|
| 25 |
+
|
| 26 |
+
def __call__(self, image):
|
| 27 |
+
image = T.functional.pil_to_tensor(image)
|
| 28 |
+
return image
|