encoder.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import tensorflow as tf
  2. from utils import logger
  3. import ops
  4. class Encoder(object):
  5. def __init__(self, name, is_train, norm='instance', activation='leaky',
  6. image_size=128, latent_dim=8):
  7. logger.info('Init Encoder %s', name)
  8. self.name = name
  9. self._is_train = is_train
  10. self._norm = norm
  11. self._activation = activation
  12. self._reuse = False
  13. self._image_size = image_size
  14. self._latent_dim = latent_dim
  15. def __call__(self, input):
  16. with tf.variable_scope(self.name, reuse=self._reuse):
  17. num_filters = [64, 128, 256, 512, 512, 512, 512]
  18. if self._image_size == 256:
  19. num_filters.append(512)
  20. E = input
  21. for i, n in enumerate(num_filters):
  22. E = ops.conv_block(E, n, 'C{}_{}'.format(n, i), 4, 2, self._is_train,
  23. self._reuse, norm=self._norm if i else None, activation='leaky')
  24. E = tf.reshape(E, [-1, 512])
  25. mu = ops.mlp(E, self._latent_dim, 'FC8_mu', self._is_train, self._reuse,
  26. norm=None, activation=None)
  27. log_sigma = ops.mlp(E, self._latent_dim, 'FC8_sigma', self._is_train, self._reuse,
  28. norm=None, activation=None)
  29. z = mu + tf.random_normal(shape=tf.shape(self._latent_dim)) * tf.exp(log_sigma)
  30. self._reuse = True
  31. self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, self.name)
  32. return z, mu, log_sigma