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 2.5 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
  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. def load_data(image_path, flip=False, is_test=False, image_size = 128):
  16. img = load_image(image_path)
  17. img = preprocess_img(img, img_size=image_size, flip=flip, is_test=is_test)
  18. img = img/127.5 - 1.
  19. if len(img.shape)<3:
  20. img = np.expand_dims(img, axis=2)
  21. return img
  22. def load_image(image_path):
  23. img = imread(image_path)
  24. return img
  25. def preprocess_img(img, img_size=128, flip=False, is_test=False):
  26. img = scipy.misc.imresize(img, [img_size, img_size])
  27. if (not is_test) and flip and np.random.random() > 0.5:
  28. img = np.fliplr(img)
  29. return img
  30. def get_image(image_path, image_size, is_crop=True, resize_w=64, is_grayscale = False):
  31. return transform(imread(image_path, is_grayscale), image_size, is_crop, resize_w)
  32. def save_images(images, size, image_path):
  33. dir = os.path.dirname(image_path)
  34. if not os.path.exists(dir):
  35. os.makedirs(dir)
  36. return imsave(inverse_transform(images), size, image_path)
  37. def imread(path, is_grayscale = False):
  38. if (is_grayscale):
  39. return scipy.misc.imread(path, flatten = True)#.astype(np.float)
  40. else:
  41. return scipy.misc.imread(path)#.astype(np.float)
  42. def merge_images(images, size):
  43. return inverse_transform(images)
  44. def merge(images, size):
  45. h, w = images.shape[1], images.shape[2]
  46. if len(images.shape) < 4:
  47. img = np.zeros((h * size[0], w * size[1], 1))
  48. images = np.expand_dims(images, axis = 3)
  49. else:
  50. img = np.zeros((h * size[0], w * size[1], images.shape[3]))
  51. for idx, image in enumerate(images):
  52. i = idx % size[1]
  53. j = idx // size[1]
  54. img[j*h:j*h+h, i*w:i*w+w, :] = image
  55. if images.shape[3] ==1:
  56. return np.concatenate([img,img,img],axis=2)
  57. else:
  58. return img.astype(np.uint8)
  59. def imsave(images, size, path):
  60. return scipy.misc.imsave(path, merge(images, size))
  61. def transform(image, npx=64, is_crop=True, resize_w=64):
  62. # npx : # of pixels width/height of image
  63. if is_crop:
  64. cropped_image = center_crop(image, npx, resize_w=resize_w)
  65. else:
  66. cropped_image = image
  67. return np.array(cropped_image)/127.5 - 1.
  68. def inverse_transform(images):
  69. return ((images+1.)*127.5)
Tip!

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

Comments

Loading...