-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples.py
96 lines (76 loc) · 3.41 KB
/
examples.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
import os, sys
import glob
import pickle as pkl
import numpy as np
import PIL.Image as Image
def resize_mscoco():
'''
function used to create the dataset,
Resize original MS_COCO Image into 64x64 images
'''
### PATH need to be fixed
data_path = "/datasets/coco/coco/images/train2014"
save_dir = "/Tmp/64_64/train2014/"
if not os.path.exists(save_dir):
os.makedirs(save_dir)
preserve_ratio = True
image_size = (64, 64)
# crop_size = (32, 32)
imgs = glob.glob(data_path + "/*.jpg")
for i, img_path in enumerate(imgs):
img = Image.open(img_path)
print(i, len(imgs), img_path)
if img.size[0] != image_size[0] or img.size[1] != image_size[1]:
if not preserve_ratio:
img = img.resize((image_size), Image.ANTIALIAS)
else:
### Resize based on the smallest dimension
scale = image_size[0] / float(np.min(img.size))
new_size = (int(np.floor(scale * img.size[0])) + 1, int(np.floor(scale * img.size[1]) + 1))
img = img.resize((new_size), Image.ANTIALIAS)
### Crop the 64/64 center
tocrop = np.array(img)
center = (int(np.floor(tocrop.shape[0] / 2.)), int(np.floor(tocrop.shape[1] / 2.)))
print(tocrop.shape, center, (center[0] - 32, center[0] + 32), (center[1] - 32, center[1] + 32))
if len(tocrop.shape) == 3:
tocrop = tocrop[center[0] - 32:center[0] + 32, center[1] - 32:center[1] + 32, :]
else:
tocrop = tocrop[center[0] - 32:center[0] + 32, center[1] - 32:center[1] + 32]
img = Image.fromarray(tocrop)
img.save(save_dir + os.path.basename(img_path))
def show_examples(batch_idx, batch_size,
### PATH need to be fixed
mscoco="inpainting/", split="train2014",
caption_path="dict_key_imgID_value_caps_train_and_valid.pkl"):
'''
Show an example of how to read the dataset
'''
data_path = os.path.join(mscoco, split)
caption_path = os.path.join(mscoco, caption_path)
# with open(caption_path) as fd:
# caption_dict = pkl.load(fd)
print(data_path + "/*.jpg")
imgs = glob.glob(data_path + "/*.jpg")
batch_imgs = imgs[batch_idx * batch_size:(batch_idx + 1) * batch_size]
for i, img_path in enumerate(batch_imgs):
img = Image.open(img_path)
img_array = np.array(img)
print(img_array.shape)
cap_id = os.path.basename(img_path)[:-4]
### Get input/target from the images
center = (int(np.floor(img_array.shape[0] / 2.)), int(np.floor(img_array.shape[1] / 2.)))
if len(img_array.shape) == 3:
input = np.copy(img_array)
input[center[0] - 16:center[0] + 16, center[1] - 16:center[1] + 16, :] = 0
target = img_array[center[0] - 16:center[0] + 16, center[1] - 16:center[1] + 16, :]
else:
input = np.copy(img_array)
input[center[0] - 16:center[0] + 16, center[1] - 16:center[1] + 16, :] = 0
target = img_array[center[0] - 16:center[0] + 16, center[1] - 16:center[1] + 16]
Image.fromarray(img_array).show()
Image.fromarray(input).show()
Image.fromarray(target).show()
# print(i, caption_dict[cap_id])
if __name__ == '__main__':
# resize_mscoco()
show_examples(57, 16)