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 4.6 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
  1. # Copyright 2020 Erik Härkönen. All rights reserved.
  2. # This file is licensed to you under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License. You may obtain a copy
  4. # of the License at http://www.apache.org/licenses/LICENSE-2.0
  5. # Unless required by applicable law or agreed to in writing, software distributed under
  6. # the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
  7. # OF ANY KIND, either express or implied. See the License for the specific language
  8. # governing permissions and limitations under the License.
  9. import hashlib
  10. import string
  11. import sys
  12. sys.path.append('./models/stylegan2')
  13. import numpy as np
  14. # StyleGAN1/2 classes
  15. out_classes = {
  16. 1: {
  17. "celebahq": "https://drive.google.com/uc?id=1MGqJl28pN4t7SAtSrPdSRJSQJqahkzUf",
  18. "ffhq": "https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ",
  19. "wikiart": "https://drive.google.com/uc?id=1cJQtMeTy_QldOP7n64F8stCDXY6Esup9",
  20. "anime": "https://mega.nz/#!vawjXISI!F7s13yRicxDA3QYqYDL2kjnc2K7Zk3DwCIYETREmBP4"
  21. },
  22. 2: {
  23. "cars": "gdrive:networks/stylegan2-car-config-f.pkl",
  24. "cats": "gdrive:networks/stylegan2-cat-config-f.pkl",
  25. "ffhq": "gdrive:networks/stylegan2-ffhq-config-f.pkl",
  26. "horse": "gdrive:networks/stylegan2-horse-config-f.pkl",
  27. "ukiyoe": "gdrive:networks/ukiyoe-256-slim-diffAug-002789.pkl",
  28. "beetles": "gdrive:networks/beetles.pkl",
  29. "anime": "mega:file/2020-01-11-skylion-stylegan2-animeportraits-networksnapshot-024664.pkl.xz"
  30. }
  31. }
  32. def prettify_name(name):
  33. valid = "-_%s%s" % (string.ascii_letters, string.digits)
  34. return ''.join(map(lambda c: c if c in valid else '_', name))
  35. # Add padding to sequence of images
  36. # Used in conjunction with np.hstack/np.vstack
  37. # By default: adds one 64th of the width of horizontal padding
  38. def pad_frames(strip, pad_fract_horiz=64, pad_fract_vert=0, pad_value=None):
  39. dtype = strip[0].dtype
  40. if pad_value is None:
  41. if dtype in [np.float32, np.float64]:
  42. pad_value = 1.0
  43. else:
  44. pad_value = np.iinfo(dtype).max
  45. frames = [strip[0]]
  46. for frame in strip[1:]:
  47. if pad_fract_horiz > 0:
  48. frames.append(pad_value * np.ones((frame.shape[0], frame.shape[1] // pad_fract_horiz, 3), dtype=dtype))
  49. elif pad_fract_vert > 0:
  50. frames.append(pad_value * np.ones((frame.shape[0] // pad_fract_vert, frame.shape[1], 3), dtype=dtype))
  51. frames.append(frame)
  52. return frames
  53. def centre_strip_stylegan(Gs, Gs_kwargs, z, lat_comp, lat_mean, lat_stdev, idx, sigma, num_frames, layer_start, layer_end):
  54. input_shape = Gs.input_shape[1]
  55. num_layers = Gs.components.mapping.output_shape[1]
  56. w_avg = Gs.get_var('dlatent_avg')
  57. w = Gs.components.mapping.run(z, None, dlatent_broadcast=None)
  58. w = w.reshape((1, input_shape))
  59. sigma_range = np.linspace(-sigma, sigma, num_frames)
  60. dotp = np.sum((w - lat_mean) * lat_comp[idx] / (np.linalg.norm(lat_comp[idx]) + 1e-8), axis=-1, keepdims=True)
  61. zeroing_offset_lat = dotp * lat_comp[idx] / (np.linalg.norm(lat_comp[idx] + 1e-8))
  62. batch_frames = []
  63. for j in range(len(sigma_range)):
  64. ws = Gs.components.mapping.run(z, None)
  65. ws = ws.reshape((num_layers, 1, input_shape))
  66. s = sigma_range[j]
  67. delta = lat_comp[idx] * s * lat_stdev[idx]
  68. for k in range(layer_start, layer_end):
  69. ws[k] = ws[k] - zeroing_offset_lat + delta
  70. ws = w_avg + (ws - w_avg) * Gs_kwargs.truncation_psi
  71. imgs = Gs.components.synthesis.run(ws.reshape((1, num_layers, input_shape)), **Gs_kwargs)
  72. batch_frames.append(imgs[0])
  73. return batch_frames
  74. def get_hash(url):
  75. url_ffhq = 'https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ' # karras2019stylegan-ffhq-1024x1024.pkl
  76. url_celebahq = 'https://drive.google.com/uc?id=1MGqJl28pN4t7SAtSrPdSRJSQJqahkzUf' # karras2019stylegan-celebahq-1024x1024.pkl
  77. url_bedrooms = 'https://drive.google.com/uc?id=1MOSKeGF0FJcivpBI7s63V9YHloUTORiF' # karras2019stylegan-bedrooms-256x256.pkl
  78. url_cars = 'https://drive.google.com/uc?id=1MJ6iCfNtMIRicihwRorsM3b7mmtmK9c3' # karras2019stylegan-cars-512x384.pkl
  79. url_cats = 'https://drive.google.com/uc?id=1MQywl0FNt6lHu8E_EUqnRbviagS7fbiJ' # karras2019stylegan-cats-256x256.pkl
  80. url_wikiart = 'https://drive.google.com/uc?id=1cJQtMeTy_QldOP7n64F8stCDXY6Esup9' # network-snapshot-011125.pkl
  81. url = url.encode("utf-8")
  82. return hashlib.md5(url).hexdigest()
Tip!

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

Comments

Loading...