質問するログイン新規登録

質問編集履歴

1

ソースコードを追加しました

2019/10/09 13:06

投稿

Mattcha
Mattcha

スコア8

title CHANGED
File without changes
body CHANGED
@@ -30,7 +30,149 @@
30
30
  ```GoogleColaboratory
31
31
  !python make_dataset.py ../../datasets/facades/ 3 --img_size 256
32
32
  ```
33
+ ※ソースコードを追加しました。(make_dataset.py)
33
34
 
35
+ ```Python
36
+ import os
37
+ import cv2
38
+ import h5py
39
+ import parmap
40
+ import argparse
41
+ import numpy as np
42
+ from pathlib import Path
43
+ from tqdm import tqdm as tqdm
44
+ import matplotlib.pylab as plt
45
+
46
+
47
+ def format_image(img_path, size, nb_channels):
48
+ """
49
+ Load img with opencv and reshape
50
+ """
51
+
52
+ if nb_channels == 1:
53
+ img = cv2.imread(img_path, 0)
54
+ img = np.expand_dims(img, axis=-1)
55
+ else:
56
+ img = cv2.imread(img_path)
57
+ img = img[:, :, ::-1] # GBR to RGB
58
+
59
+ w = img.shape[1]
60
+
61
+ # Slice image in 2 to get both parts
62
+ img_full = img[:, :w // 2, :]
63
+ img_sketch = img[:, w // 2:, :]
64
+
65
+ img_full = cv2.resize(img_full, (size, size), interpolation=cv2.INTER_AREA)
66
+ img_sketch = cv2.resize(img_sketch, (size, size), interpolation=cv2.INTER_AREA)
67
+
68
+ if nb_channels == 1:
69
+ img_full = np.expand_dims(img_full, -1)
70
+ img_sketch = np.expand_dims(img_sketch, -1)
71
+
72
+ img_full = np.expand_dims(img_full, 0).transpose(0, 3, 1, 2)
73
+ img_sketch = np.expand_dims(img_sketch, 0).transpose(0, 3, 1, 2)
74
+
75
+ return img_full, img_sketch
76
+
77
+
78
+ def build_HDF5(jpeg_dir, nb_channels, data_dir, size=256):
79
+ """
80
+ Gather the data in a single HDF5 file.
81
+ """
82
+
83
+ data_dir = os.path.join(data_dir, 'processed')
84
+
85
+ # Put train data in HDF5
86
+ file_name = os.path.basename(jpeg_dir.rstrip("/"))
87
+ hdf5_file = os.path.join(data_dir, "%s_data.h5" % file_name)
88
+ with h5py.File(hdf5_file, "w") as hfw:
89
+
90
+ for dset_type in ["train", "test", "val"]:
91
+
92
+ list_img = [img for img in Path(jpeg_dir).glob('%s/*.jpg' % dset_type)]
93
+ list_img = [str(img) for img in list_img]
94
+ list_img.extend(list(Path(jpeg_dir).glob('%s/*.png' % dset_type)))
95
+ list_img = list(map(str, list_img))
96
+ list_img = np.array(list_img)
97
+
98
+ data_full = hfw.create_dataset("%s_data_full" % dset_type,
99
+ (0, nb_channels, size, size),
100
+ maxshape=(None, 3, size, size),
101
+ dtype=np.uint8)
102
+
103
+ data_sketch = hfw.create_dataset("%s_data_sketch" % dset_type,
104
+ (0, nb_channels, size, size),
105
+ maxshape=(None, 3, size, size),
106
+ dtype=np.uint8)
107
+
108
+ num_files = len(list_img)
109
+ chunk_size = 100
110
+ num_chunks = num_files / chunk_size
111
+ arr_chunks = np.array_split(np.arange(num_files), num_chunks)
112
+
113
+ for chunk_idx in tqdm(arr_chunks):
114
+
115
+ list_img_path = list_img[chunk_idx].tolist()
116
+ output = parmap.map(format_image, list_img_path, size, nb_channels, pm_parallel=False)
117
+
118
+ arr_img_full = np.concatenate([o[0] for o in output], axis=0)
119
+ arr_img_sketch = np.concatenate([o[1] for o in output], axis=0)
120
+
121
+ # Resize HDF5 dataset
122
+ data_full.resize(data_full.shape[0] + arr_img_full.shape[0], axis=0)
123
+ data_sketch.resize(data_sketch.shape[0] + arr_img_sketch.shape[0], axis=0)
124
+
125
+ data_full[-arr_img_full.shape[0]:] = arr_img_full.astype(np.uint8)
126
+ data_sketch[-arr_img_sketch.shape[0]:] = arr_img_sketch.astype(np.uint8)
127
+
128
+ def check_HDF5(jpeg_dir, nb_channels):
129
+ """
130
+ Plot images with landmarks to check the processing
131
+ """
132
+
133
+ # Get hdf5 file
134
+ file_name = os.path.basename(jpeg_dir.rstrip("/"))
135
+ hdf5_file = os.path.join(data_dir, "%s_data.h5" % file_name)
136
+
137
+ with h5py.File(hdf5_file, "r") as hf:
138
+ data_full = hf["train_data_full"]
139
+ data_sketch = hf["train_data_sketch"]
140
+ for i in range(data_full.shape[0]):
141
+ plt.figure()
142
+ img = data_full[i, :, :, :].transpose(1,2,0)
143
+ img2 = data_sketch[i, :, :, :].transpose(1,2,0)
144
+ img = np.concatenate((img, img2), axis=1)
145
+ if nb_channels == 1:
146
+ plt.imshow(img[:, :, 0], cmap="gray")
147
+ else:
148
+ plt.imshow(img)
149
+ plt.show()
150
+ plt.clf()
151
+ plt.close()
152
+
153
+
154
+ if __name__ == '__main__':
155
+
156
+ parser = argparse.ArgumentParser(description='Build dataset')
157
+ parser.add_argument('jpeg_dir', type=str, help='path to jpeg images')
158
+ parser.add_argument('nb_channels', type=int, help='number of image channels')
159
+ parser.add_argument('--img_size', default=256, type=int,
160
+ help='Desired Width == Height')
161
+ parser.add_argument('--do_plot', action="store_true",
162
+ help='Plot the images to make sure the data processing went OK')
163
+ parser.add_argument('--data_dir', default='../../data', type=str, help='Data directory')
164
+ args = parser.parse_args()
165
+
166
+ build_HDF5(args.jpeg_dir,
167
+ args.nb_channels,
168
+ args.data_dir,
169
+ size=args.img_size)
170
+
171
+ if args.do_plot:
172
+ check_HDF5(args.jpeg_dir, args.nb_channels)
173
+
174
+
175
+ ```
34
176
  ### 試したこと
35
177
 
36
178
  データセットの作り方に問題があると考え調べてみたところ、データセットの画像には何かしら意味付けがされているのではないか、単に編集アプリで作るだけではいけないのではないかと考えました。が、理解が浅く、どうすればよいのか分からない状態です。