質問編集履歴

1

補足しました。

2019/10/19 03:18

投稿

RyotaYamada
RyotaYamada

スコア6

test CHANGED
File without changes
test CHANGED
@@ -8,6 +8,38 @@
8
8
 
9
9
  ```
10
10
 
11
+ [*] Reading checkpoint...
12
+
13
+ [!] Load failed...
14
+
15
+ Processing image: ./datasets/voice/testA\at.jpg
16
+
17
+ Traceback (most recent call last):
18
+
19
+ File "main.py", line 53, in <module>
20
+
21
+ tf.app.run()
22
+
23
+ File "C:\Users\mounf\Anaconda3\envs\cycleganvoicechangerproject\lib\site-packages\tensorflow\python\platform\app.py", line 126, in run
24
+
25
+ _sys.exit(main(argv))
26
+
27
+ File "main.py", line 50, in main
28
+
29
+ else model.test(args)
30
+
31
+ File "C:\Users\mounf\PycharmProjects\nets\model.py", line 252, in test
32
+
33
+ sample_image = [load_test_data(sample_file, args.fine_size)]
34
+
35
+ File "C:\Users\mounf\PycharmProjects\nets\utils.py", line 46, in load_test_data
36
+
37
+ img = imread(image_path)
38
+
39
+ File "C:\Users\mounf\PycharmProjects\nets\utils.py", line 88, in imread
40
+
41
+ return _imread(path, mode='RGB').astype(np.float)
42
+
11
43
  File "C:\Users\mounf\Anaconda3\envs\cycleganvoicechangerproject\lib\site-packages\imageio\core\functions.py", line 260, in imread
12
44
 
13
45
  'Invalid keyword argument "mode", ' 'perhaps you mean "pilmode"?'
@@ -18,6 +50,8 @@
18
50
 
19
51
 
20
52
 
53
+
54
+
21
55
  ```
22
56
 
23
57
 
@@ -28,6 +62,306 @@
28
62
 
29
63
  ```python
30
64
 
65
+ ♯utils.py
66
+
67
+ from __future__ import division
68
+
69
+ import math
70
+
71
+ import pprint
72
+
73
+ import scipy.misc
74
+
75
+ import numpy as np
76
+
77
+ import copy
78
+
79
+ try:
80
+
81
+ _imread = scipy.misc.imread
82
+
83
+ except AttributeError:
84
+
85
+ from imageio import imread as _imread
86
+
87
+
88
+
89
+ pp = pprint.PrettyPrinter()
90
+
91
+
92
+
93
+ get_stddev = lambda x, k_h, k_w: 1/math.sqrt(k_w*k_h*x.get_shape()[-1])
94
+
95
+
96
+
97
+ ♯new added functions for cyclegan
98
+
99
+ class ImagePool(object):
100
+
101
+ def __init__(self, maxsize=50):
102
+
103
+ self.maxsize = maxsize
104
+
105
+ self.num_img = 0
106
+
107
+ self.images = []
108
+
109
+
110
+
111
+ def __call__(self, image):
112
+
113
+ if self.maxsize <= 0:
114
+
115
+ return image
116
+
117
+ if self.num_img < self.maxsize:
118
+
119
+ self.images.append(image)
120
+
121
+ self.num_img += 1
122
+
123
+ return image
124
+
125
+ if np.random.rand() > 0.5:
126
+
127
+ idx = int(np.random.rand()*self.maxsize)
128
+
129
+ tmp1 = copy.copy(self.images[idx])[0]
130
+
131
+ self.images[idx][0] = image[0]
132
+
133
+ idx = int(np.random.rand()*self.maxsize)
134
+
135
+ tmp2 = copy.copy(self.images[idx])[1]
136
+
137
+ self.images[idx][1] = image[1]
138
+
139
+ return [tmp1, tmp2]
140
+
141
+ else:
142
+
143
+ return image
144
+
145
+
146
+
147
+ def load_test_data(image_path, fine_size=256):
148
+
149
+ img = imread(image_path)
150
+
151
+ img = scipy.misc.imresize(img, [fine_size, fine_size])
152
+
153
+ img = img/127.5 - 1
154
+
155
+ return img
156
+
157
+
158
+
159
+ def load_train_data(image_path, load_size=286, fine_size=256, is_testing=False):
160
+
161
+ img_A = imread(image_path[0])
162
+
163
+ img_B = imread(image_path[1])
164
+
165
+ if not is_testing:
166
+
167
+ img_A = scipy.misc.imresize(img_A, [load_size, load_size])
168
+
169
+ img_B = scipy.misc.imresize(img_B, [load_size, load_size])
170
+
171
+ h1 = int(np.ceil(np.random.uniform(1e-2, load_size-fine_size)))
172
+
173
+ w1 = int(np.ceil(np.random.uniform(1e-2, load_size-fine_size)))
174
+
175
+ img_A = img_A[h1:h1+fine_size, w1:w1+fine_size]
176
+
177
+ img_B = img_B[h1:h1+fine_size, w1:w1+fine_size]
178
+
179
+
180
+
181
+ if np.random.random() > 0.5:
182
+
183
+ img_A = np.fliplr(img_A)
184
+
185
+ img_B = np.fliplr(img_B)
186
+
187
+ else:
188
+
189
+ img_A = scipy.misc.imresize(img_A, [fine_size, fine_size])
190
+
191
+ img_B = scipy.misc.imresize(img_B, [fine_size, fine_size])
192
+
193
+
194
+
195
+ img_A = img_A/127.5 - 1.
196
+
197
+ img_B = img_B/127.5 - 1.
198
+
199
+
200
+
201
+ img_AB = np.concatenate((img_A, img_B), axis=2)
202
+
203
+ # img_AB shape: (fine_size, fine_size, input_c_dim + output_c_dim)
204
+
205
+ return img_AB
206
+
207
+
208
+
209
+ -----------------------------
210
+
211
+
212
+
213
+ def get_image(image_path, image_size, is_crop=True, resize_w=64, is_grayscale = False):
214
+
215
+ return transform(imread(image_path, is_grayscale), image_size, is_crop, resize_w)
216
+
217
+
218
+
219
+ def save_images(images, size, image_path):
220
+
221
+ return imsave(inverse_transform(images), size, image_path)
222
+
223
+
224
+
225
+ def imread(path, is_grayscale = False):
226
+
227
+ if (is_grayscale):
228
+
229
+ return _imread(path, flatten=True).astype(np.float)
230
+
231
+ else:
232
+
233
+ return _imread(path, mode='RGB').astype(np.float)
234
+
235
+
236
+
237
+ def merge_images(images, size):
238
+
239
+ return inverse_transform(images)
240
+
241
+
242
+
243
+ def merge(images, size):
244
+
245
+ h, w = images.shape[1], images.shape[2]
246
+
247
+ img = np.zeros((h * size[0], w * size[1], 3))
248
+
249
+ for idx, image in enumerate(images):
250
+
251
+ i = idx % size[1]
252
+
253
+ j = idx // size[1]
254
+
255
+ img[j*h:j*h+h, i*w:i*w+w, :] = image
256
+
257
+
258
+
259
+ return img
260
+
261
+
262
+
263
+ def imsave(images, size, path):
264
+
265
+ return scipy.misc.imsave(path, merge(images, size))
266
+
267
+
268
+
269
+ def center_crop(x, crop_h, crop_w,
270
+
271
+ resize_h=64, resize_w=64):
272
+
273
+ if crop_w is None:
274
+
275
+ crop_w = crop_h
276
+
277
+ h, w = x.shape[:2]
278
+
279
+ j = int(round((h - crop_h)/2.))
280
+
281
+ i = int(round((w - crop_w)/2.))
282
+
283
+ return scipy.misc.imresize(
284
+
285
+ x[j:j+crop_h, i:i+crop_w], [resize_h, resize_w])
286
+
287
+
288
+
289
+ def transform(image, npx=64, is_crop=True, resize_w=64):
290
+
291
+ # npx : # of pixels width/height of image
292
+
293
+ if is_crop:
294
+
295
+ cropped_image = center_crop(image, npx, resize_w=resize_w)
296
+
297
+ else:
298
+
299
+ cropped_image = image
300
+
301
+ return np.array(cropped_image)/127.5 - 1.
302
+
303
+
304
+
305
+ def inverse_transform(images):
306
+
307
+ return (images+1.)/2.
308
+
309
+
310
+
311
+
312
+
313
+ ♯---ここからfunction.py
314
+
315
+ ♯Images
316
+
317
+
318
+
319
+
320
+
321
+ def imread(uri, format=None, **kwargs):
322
+
323
+ """ imread(uri, format=None, **kwargs)
324
+
325
+
326
+
327
+ Reads an image from the specified file. Returns a numpy array, which
328
+
329
+ comes with a dict of meta data at its 'meta' attribute.
330
+
331
+
332
+
333
+ Note that the image data is returned as-is, and may not always have
334
+
335
+ a dtype of uint8 (and thus may differ from what e.g. PIL returns).
336
+
337
+
338
+
339
+ Parameters
340
+
341
+ ♯----------
342
+
343
+ uri : {str, pathlib.Path, bytes, file}
344
+
345
+ The resource to load the image from, e.g. a filename, pathlib.Path,
346
+
347
+ http address or file object, see the docs for more info.
348
+
349
+ format : str
350
+
351
+ The format to use to read the file. By default imageio selects
352
+
353
+ the appropriate for you based on the filename and its contents.
354
+
355
+ kwargs : ...
356
+
357
+ Further keyword arguments are passed to the reader. See :func:`.help`
358
+
359
+ to see what arguments are available for a particular format.
360
+
361
+ """
362
+
363
+
364
+
31
365
  if "mode" in kwargs:
32
366
 
33
367
  raise TypeError(
@@ -36,7 +370,15 @@
36
370
 
37
371
  )
38
372
 
373
+
374
+
375
+ ♯Get reader and read first
376
+
377
+ reader = read(uri, format, "i", **kwargs)
378
+
39
- ```
379
+ with reader:
380
+
381
+ return reader.get_data(0)
40
382
 
41
383
 
42
384