Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

utils.py 3.4 KB

You have to be logged in to leave a comment. Sign In
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  1. """
  2. Some codes from https://github.com/Newmu/dcgan_code
  3. """
  4. from __future__ import division
  5. import math
  6. import json
  7. import random
  8. import pprint
  9. import scipy.misc
  10. import numpy as np
  11. import os
  12. from time import gmtime, strftime
  13. pp = pprint.PrettyPrinter()
  14. get_stddev = lambda x, k_h, k_w: 1/math.sqrt(k_w*k_h*x.get_shape()[-1])
  15. # -----------------------------
  16. # new added functions for pix2pix
  17. def load_data_pair(image_path, flip=False, is_test=False,img_size = 256):
  18. img_A, img_B = load_image_pair(image_path)
  19. img_A, img_B = preprocess_A_and_B(img_A, img_B, img_size=img_size)
  20. img_A = img_A/127.5 - 1.
  21. img_B = img_B/127.5 - 1.
  22. if len(img_A.shape)<3:
  23. img_A = np.expand_dims(img_A, axis=2)
  24. img_B = np.expand_dims(img_B, axis=2)
  25. img_AB = np.concatenate((img_A, img_B), axis=2)
  26. return img_AB
  27. def load_image_pair(image_path):
  28. input_img = imread(image_path)
  29. w = int(input_img.shape[1])
  30. w2 = int(w/2)
  31. img_A = input_img[:, 0:w2]
  32. img_B = input_img[:, w2:w]
  33. return img_A, img_B
  34. def preprocess_A_and_B(img_A, img_B, img_size=256):
  35. img_A = scipy.misc.imresize(img_A, [img_size, img_size])
  36. img_B = scipy.misc.imresize(img_B, [img_size, img_size])
  37. return img_A, img_B
  38. def load_data(image_path, flip=False, is_test=False, image_size = 128):
  39. img = load_image(image_path)
  40. img = preprocess_img(img, img_size=image_size, flip=flip, is_test=is_test)
  41. img = img/127.5 - 1.
  42. if len(img.shape)<3:
  43. img = np.expand_dims(img, axis=2)
  44. return img
  45. def load_image(image_path):
  46. img = imread(image_path)
  47. return img
  48. def preprocess_img(img, img_size=128, flip=False, is_test=False):
  49. img = scipy.misc.imresize(img, [img_size, img_size])
  50. if (not is_test) and flip and np.random.random() > 0.5:
  51. img = np.fliplr(img)
  52. return img
  53. # -----------------------------
  54. def get_image(image_path, image_size, is_crop=True, resize_w=64, is_grayscale = False):
  55. return transform(imread(image_path, is_grayscale), image_size, is_crop, resize_w)
  56. def save_images(images, size, image_path):
  57. dir = os.path.dirname(image_path)
  58. if not os.path.exists(dir):
  59. os.makedirs(dir)
  60. return imsave(inverse_transform(images), size, image_path)
  61. def imread(path, is_grayscale = False):
  62. if (is_grayscale):
  63. return scipy.misc.imread(path, flatten = True)#.astype(np.float)
  64. else:
  65. return scipy.misc.imread(path)#.astype(np.float)
  66. def merge_images(images, size):
  67. return inverse_transform(images)
  68. def merge(images, size):
  69. h, w = images.shape[1], images.shape[2]
  70. if len(images.shape) < 4:
  71. img = np.zeros((h * size[0], w * size[1], 1))
  72. images = np.expand_dims(images, axis = 3)
  73. else:
  74. img = np.zeros((h * size[0], w * size[1], images.shape[3]))
  75. for idx, image in enumerate(images):
  76. i = idx % size[1]
  77. j = idx // size[1]
  78. img[j*h:j*h+h, i*w:i*w+w, :] = image
  79. if images.shape[3] ==1:
  80. return np.concatenate([img,img,img],axis=2)
  81. else:
  82. return img.astype(np.uint8)
  83. def imsave(images, size, path):
  84. return scipy.misc.imsave(path, merge(images, size))
  85. def transform(image, npx=64, is_crop=True, resize_w=64):
  86. # npx : # of pixels width/height of image
  87. if is_crop:
  88. cropped_image = center_crop(image, npx, resize_w=resize_w)
  89. else:
  90. cropped_image = image
  91. return np.array(cropped_image)/127.5 - 1.
  92. def inverse_transform(images):
  93. return ((images+1.)*127.5)#/2.
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...