-
Notifications
You must be signed in to change notification settings - Fork 11
/
load_data.py
89 lines (72 loc) · 2.45 KB
/
load_data.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
import numpy as np
import os
import h5py
import glob
import scipy.misc
from scipy.misc import imread, imresize
def load_images():
train_all=glob.glob("cityscapes/train/*.jpg")
train_img_A = []
train_img_B = []
for file in train_all:
full_image = imread(file)
img_A = full_image[:, :full_image.shape[1]/2, :]
img_B = full_image[:, full_image.shape[1]/2:, :]
train_img_A.append(img_A)
train_img_B.append(img_B)
train_A = np.asarray(train_img_A)
train_B = np.asarray(train_img_B)
print train_A.shape
print train_B.shape
test_all = glob.glob("cityscapes/val/*.jpg")
test_img_A = []
test_img_B = []
for file in test_all:
full_image = imread(file)
img_A = full_image[:, :full_image.shape[1]/2, :]
img_B = full_image[:, full_image.shape[1]/2:, :]
test_img_A.append(img_A)
test_img_B.append(img_B)
test_A = np.asarray(test_img_A)
test_B = np.asarray(test_img_B)
print test_A.shape
print test_B.shape
return train_A, train_B, test_A, test_B
train_all = glob.glob("cityscapes/train/*.jpg")
batch_size = 1
def load_batch_image(idx):
full_image = imread(train_all[idx])
img_A = np.asarray(full_image[:, :full_image.shape[1]/2, :])/255.
img_B = np.asarray(full_image[:, full_image.shape[1]/2:, :])/255.
return img_A, img_B
test_all = glob.glob("cityscapes/val/*.jpg")
def load_test_image(idx):
full_image = imread(train_all[idx])
img_A = np.asarray(full_image[:, :full_image.shape[1]/2, :])/255.
return img_A
def save_images(image, size, img_path):
return imsave(inverse_transform(image), size, img_path)
def imsave(image, img_size, img_path):
image = np.squeeze(merge(image, img_size))
return scipy.misc.imsave(img_path, image)
def inverse_transform(image):
return (image+1.)/2.
def merge(images, size):
h, w = images.shape[1], images.shape[2]
if (images.shape[3] in (3,4)):
c = images.shape[3]
img = np.zeros((h * size[0], w * size[1], c))
for idx, image in enumerate(images):
i = idx % size[1]
j = idx // size[1]
img[j * h:j * h + h, i * w:i * w + w, :] = image
return img
elif images.shape[3]==1:
img = np.zeros((h * size[0], w * size[1]))
for idx, image in enumerate(images):
i = idx % size[1]
j = idx // size[1]
img[j * h:j * h + h, i * w:i * w + w] = image[:, :, 0]
return img
else:
raise ValueError('In merge function, the first argument must have dimensions: HxW or HxWx3 or HxWx4')