Spaces:
Running
on
Zero
Running
on
Zero
Upload 2 files
Browse files- image_datasets/canny_dataset.py +59 -0
- image_datasets/dataset.py +45 -0
image_datasets/canny_dataset.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import torch
|
6 |
+
from torch.utils.data import Dataset, DataLoader
|
7 |
+
import json
|
8 |
+
import random
|
9 |
+
import cv2
|
10 |
+
|
11 |
+
|
12 |
+
def canny_processor(image, low_threshold=100, high_threshold=200):
|
13 |
+
image = np.array(image)
|
14 |
+
image = cv2.Canny(image, low_threshold, high_threshold)
|
15 |
+
image = image[:, :, None]
|
16 |
+
image = np.concatenate([image, image, image], axis=2)
|
17 |
+
canny_image = Image.fromarray(image)
|
18 |
+
return canny_image
|
19 |
+
|
20 |
+
|
21 |
+
def c_crop(image):
|
22 |
+
width, height = image.size
|
23 |
+
new_size = min(width, height)
|
24 |
+
left = (width - new_size) / 2
|
25 |
+
top = (height - new_size) / 2
|
26 |
+
right = (width + new_size) / 2
|
27 |
+
bottom = (height + new_size) / 2
|
28 |
+
return image.crop((left, top, right, bottom))
|
29 |
+
|
30 |
+
class CustomImageDataset(Dataset):
|
31 |
+
def __init__(self, img_dir, img_size=512):
|
32 |
+
self.images = [os.path.join(img_dir, i) for i in os.listdir(img_dir) if '.jpg' in i or '.png' in i]
|
33 |
+
self.images.sort()
|
34 |
+
self.img_size = img_size
|
35 |
+
|
36 |
+
def __len__(self):
|
37 |
+
return len(self.images)
|
38 |
+
|
39 |
+
def __getitem__(self, idx):
|
40 |
+
try:
|
41 |
+
img = Image.open(self.images[idx])
|
42 |
+
img = c_crop(img)
|
43 |
+
img = img.resize((self.img_size, self.img_size))
|
44 |
+
hint = canny_processor(img)
|
45 |
+
img = torch.from_numpy((np.array(img) / 127.5) - 1)
|
46 |
+
img = img.permute(2, 0, 1)
|
47 |
+
hint = torch.from_numpy((np.array(hint) / 127.5) - 1)
|
48 |
+
hint = hint.permute(2, 0, 1)
|
49 |
+
json_path = self.images[idx].split('.')[0] + '.json'
|
50 |
+
prompt = json.load(open(json_path))['caption']
|
51 |
+
return img, hint, prompt
|
52 |
+
except Exception as e:
|
53 |
+
print(e)
|
54 |
+
return self.__getitem__(random.randint(0, len(self.images) - 1))
|
55 |
+
|
56 |
+
|
57 |
+
def loader(train_batch_size, num_workers, **args):
|
58 |
+
dataset = CustomImageDataset(**args)
|
59 |
+
return DataLoader(dataset, batch_size=train_batch_size, num_workers=num_workers)
|
image_datasets/dataset.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import torch
|
6 |
+
from torch.utils.data import Dataset, DataLoader
|
7 |
+
import json
|
8 |
+
import random
|
9 |
+
|
10 |
+
def c_crop(image):
|
11 |
+
width, height = image.size
|
12 |
+
new_size = min(width, height)
|
13 |
+
left = (width - new_size) / 2
|
14 |
+
top = (height - new_size) / 2
|
15 |
+
right = (width + new_size) / 2
|
16 |
+
bottom = (height + new_size) / 2
|
17 |
+
return image.crop((left, top, right, bottom))
|
18 |
+
|
19 |
+
class CustomImageDataset(Dataset):
|
20 |
+
def __init__(self, img_dir, img_size=512):
|
21 |
+
self.images = [os.path.join(img_dir, i) for i in os.listdir(img_dir) if '.jpg' in i or '.png' in i]
|
22 |
+
self.images.sort()
|
23 |
+
self.img_size = img_size
|
24 |
+
|
25 |
+
def __len__(self):
|
26 |
+
return len(self.images)
|
27 |
+
|
28 |
+
def __getitem__(self, idx):
|
29 |
+
try:
|
30 |
+
img = Image.open(self.images[idx])
|
31 |
+
img = c_crop(img)
|
32 |
+
img = img.resize((self.img_size, self.img_size))
|
33 |
+
img = torch.from_numpy((np.array(img) / 127.5) - 1)
|
34 |
+
img = img.permute(2, 0, 1)
|
35 |
+
json_path = self.images[idx].split('.')[0] + '.json'
|
36 |
+
prompt = json.load(open(json_path))['caption']
|
37 |
+
return img, prompt
|
38 |
+
except Exception as e:
|
39 |
+
print(e)
|
40 |
+
return self.__getitem__(random.randint(0, len(self.images) - 1))
|
41 |
+
|
42 |
+
|
43 |
+
def loader(train_batch_size, num_workers, **args):
|
44 |
+
dataset = CustomImageDataset(**args)
|
45 |
+
return DataLoader(dataset, batch_size=train_batch_size, num_workers=num_workers, shuffle=True)
|