Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

import tensorflow as tf 

import numpy as np 

import hyperchamber as hc 

from hypergan.generators.common import * 

 

from .base_generator import BaseGenerator 

 

class ResizeConvGenerator(BaseGenerator): 

 

def required(self): 

return "final_depth activation depth_increase".split() 

 

def depths(self, initial_width=4): 

gan = self.gan 

ops = self.ops 

config = self.config 

final_depth = config.final_depth-config.depth_increase 

depths = [] 

 

target_w = gan.width() 

 

w = initial_width 

#ontehuas 

i = 0 

 

depths.append(final_depth) 

while w < target_w: 

w*=2 

i+=1 

depths.append(final_depth + i*config.depth_increase) 

depths = depths[1:] 

depths.reverse() 

return depths 

 

def build(self, net): 

gan = self.gan 

ops = self.ops 

config = self.config 

 

nets = [] 

 

activation = ops.lookup(config.activation) 

final_activation = ops.lookup(config.final_activation) 

block = config.block or standard_block 

 

shape = ops.shape(net) 

if len(shape) == 4 and shape[2] == 1: 

primes = config.initial_dimensions or [4, 4] 

print("[generator] Reshaping network based on primes.", primes) 

net = ops.reshape(net, [shape[0], primes[0], primes[1], -1]) 

print("[generator] Reshaped network layer to ", net) 

 

 

if config.skip_linear: 

print("[generator] Skipping linear", net) 

 

net = self.layer_filter(net) 

if config.concat_linear: 

size = ops.shape(net)[1]*ops.shape(net)[2]*config.concat_linear_filters 

net2 = tf.reshape(net, [ops.shape(net)[0], -1]) 

net2 = tf.slice(net2, [0,0], [ops.shape(net)[0], config.concat_linear]) 

net2 = ops.linear(net2, size) 

net2 = tf.reshape(net2, [ops.shape(net)[0], ops.shape(net)[1], ops.shape(net)[2], config.concat_linear_filters]) 

if config.concat_linear_regularize: 

net2 = self.layer_regularizer(net2) 

net2 = config.activation(net2) 

net = tf.concat([net, net2], axis=3) 

net = ops.conv2d(net, 3, 3, 1, 1, ops.shape(net)[3]//(config.extra_layers_reduction or 1)) 

for i in range(config.extra_layers or 0): 

net = self.layer_regularizer(net) 

net = activation(net) 

net = ops.conv2d(net, 3, 3, 1, 1, ops.shape(net)[3]//(config.extra_layers_reduction or 1)) 

else: 

net = ops.reshape(net, [ops.shape(net)[0], -1]) 

primes = config.initial_dimensions or [4, 4] 

depths = self.depths(primes[0]) 

initial_depth = depths[0] 

new_shape = [ops.shape(net)[0], primes[0], primes[1], initial_depth] 

net = ops.linear(net, initial_depth*primes[0]*primes[1]) 

net = ops.reshape(net, new_shape) 

 

shape = ops.shape(net) 

 

depths = self.depths(initial_width = shape[1]) 

print("[generator] Initial depth", shape) 

 

if config.relation_layer: 

net = self.layer_regularizer(net) 

net = activation(net) 

net = self.relation_layer(net) 

print("[generator] relational layer", net) 

else: 

pass 

 

depth_reduction = np.float32(config.depth_reduction) 

shape = ops.shape(net) 

 

net = self.layer_filter(net) 

for i, depth in enumerate(depths[1:]): 

s = ops.shape(net) 

resize = [min(s[1]*2, gan.height()), min(s[2]*2, gan.width())] 

net = self.layer_regularizer(net) 

 

if config.skip_connection: 

sliced = tf.slice(net, [0,0,0,0], [-1, -1, -1, gan.channels()]) 

sliced = final_activation(sliced) 

gan.skip_connections.set(config.skip_connection, sliced, shape=ops.shape(sliced)) 

 

net = activation(net) 

 

if block != 'deconv': 

net = ops.resize_images(net, resize, config.resize_image_type or 1) 

net = self.layer_filter(net) 

net = block(self, net, depth, filter=3) 

else: 

net = ops.deconv2d(net, 5, 5, 2, 2, depth) 

net = self.layer_filter(net) 

 

 

size = resize[0]*resize[1]*depth 

print("[generator] layer", net, size) 

 

net = self.layer_regularizer(net) 

net = activation(net) 

resize = [gan.height(), gan.width()] 

 

if block != 'deconv': 

net = ops.resize_images(net, resize, config.resize_image_type or 1) 

net = self.layer_filter(net) 

net = block(self, net, gan.channels(), filter=config.final_filter or 3) 

else: 

net = ops.deconv2d(net, 5, 5, 2, 2, gan.channels()) 

net = self.layer_filter(net) 

 

 

if final_activation: 

net = self.layer_regularizer(net) 

net = final_activation(net) 

if config.final_layer_filter: 

net = config.final_layer_filter(gan, config, net) 

 

self.sample = net 

return self.sample