MCPcopy Create free account
hub / github.com/tensorpack/tensorpack / Model

Class Model

examples/GAN/InfoGAN-mnist.py:107–209  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

105
106
107class Model(GANModelDesc):
108 def inputs(self):
109 return [tf.TensorSpec((None, 28, 28), tf.float32, 'input')]
110
111 def generator(self, z):
112 l = FullyConnected('fc0', z, 1024, activation=BNReLU)
113 l = FullyConnected('fc1', l, 128 * 7 * 7, activation=BNReLU)
114 l = tf.reshape(l, [-1, 7, 7, 128])
115 l = Conv2DTranspose('deconv1', l, 64, 4, 2, activation=BNReLU)
116 l = Conv2DTranspose('deconv2', l, 1, 4, 2, activation=tf.identity)
117 l = tf.sigmoid(l, name='gen')
118 return l
119
120 @auto_reuse_variable_scope
121 def discriminator(self, imgs):
122 with argscope(Conv2D, kernel_size=4, strides=2):
123 l = (LinearWrap(imgs)
124 .Conv2D('conv0', 64)
125 .tf.nn.leaky_relu()
126 .Conv2D('conv1', 128)
127 .BatchNorm('bn1')
128 .tf.nn.leaky_relu()
129 .FullyConnected('fc1', 1024)
130 .BatchNorm('bn2')
131 .tf.nn.leaky_relu()())
132
133 logits = FullyConnected('fct', l, 1)
134 encoder = (LinearWrap(l)
135 .FullyConnected('fce1', 128)
136 .BatchNorm('bne')
137 .tf.nn.leaky_relu()
138 .FullyConnected('fce-out', DIST_PARAM_DIM)())
139 return logits, encoder
140
141 def build_graph(self, real_sample):
142 real_sample = tf.expand_dims(real_sample, -1)
143
144 # sample the latent code:
145 zc = shapeless_placeholder(sample_prior(BATCH), 0, name='z_code')
146 z_noise = shapeless_placeholder(
147 tf.random_uniform([BATCH, NOISE_DIM], -1, 1), 0, name='z_noise')
148 z = tf.concat([zc, z_noise], 1, name='z')
149
150 with argscope([Conv2D, Conv2DTranspose, FullyConnected],
151 kernel_initializer=tf.truncated_normal_initializer(stddev=0.02)):
152 with tf.variable_scope('gen'):
153 fake_sample = self.generator(z)
154 fake_sample_viz = tf.cast((fake_sample) * 255.0, tf.uint8, name='viz')
155 tf.summary.image('gen', fake_sample_viz, max_outputs=30)
156
157 # may need to investigate how bn stats should be updated across two discrim
158 with tf.variable_scope('discrim'):
159 real_pred, _ = self.discriminator(real_sample)
160 fake_pred, dist_param = self.discriminator(fake_sample)
161
162 """
163 Mutual information between x (i.e. zc in this case) and some
164 information s (the generated samples in this case):

Callers 2

sampleFunction · 0.70
InfoGAN-mnist.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…