def decode(self, labels=None, condition=None, epsilon=None): """ 参数列表: 标签: Class label, could be none 条件参数: 2D or 4D tensor, condition for dynamic linear transformation epsilon: None or list. If specified, it should be a list with `num_levels` elements 返回值: x: 4D tensor, generated samples """ with tf.variable_scope("prior", reuse=tf.AUTO_REUSE): if labels is None: y_onehot = self.y elif len(labels.shape) == 1: y_onehot = tf.one_hot(labels, depth=self.num_classes, dtype=tf.float32) elif len(labels.shape) == 2: y_onehot = labels _, sample, get_eps = prior(y_onehot, self.hps) if epsilon is not None: eps = epsilon if len( epsilon) == self.hps.num_levels else [None] * ( self.hps.num_levels - 1) + epsilon else: eps = [None] * self.hps.num_levels z = sample(eps=eps[-1]) objective = tf.zeros(tf.shape(z)[0]) if self.hps.conditioning and condition is None: condition = y_onehot # with tf.variable_scope("cond_preprocess", reuse=tf.AUTO_REUSE): # condition = tf.layers.dense(condition, units=10, use_bias=False) z, objective = codec(z, cond=condition, hps=self.hps, reverse=True, objective=objective, eps=eps[:-1], reuse=tf.AUTO_REUSE) with tf.variable_scope("postprocess"): x = unsqueeze2d(z) x = tf.clip_by_value( tf.floor((x + .5) * self.num_bins) * (256. / self.num_bins), 0, 255) self.gen_x = tf.cast(x, 'uint8') return self.gen_x
def encode(self, inputs, labels, condition=None): ## Dequantization by adding uniform noise with tf.variable_scope("preprocess"): self.y = tf.one_hot(labels, depth=self.num_classes, dtype=tf.float32) inputs = tf.cast(inputs, 'float32') self.height, self.width, self.channels = inputs.get_shape( ).as_list()[1:] if self.hps.num_bits_x < 8: inputs = tf.floor(inputs / 2**(8 - self.hps.num_bits_x)) inputs = inputs / self.num_bins - 0.5 inputs = inputs + tf.random_uniform(tf.shape(inputs), 0, 1. / self.num_bins) objective = tf.zeros(tf.shape(inputs)[0]) objective += -np.log(self.num_bins) * np.prod( ops.shape(inputs)[1:]) inputs = squeeze2d(inputs) ## Encoder if self.hps.conditioning and condition is None: condition = self.y # with tf.variable_scope("cond_preprocess"): # condition = tf.layers.dense(condition, units=10, use_bias=False) z, objective, eps = codec(inputs, cond=condition, objective=objective, hps=self.hps, reverse=False) ## Prior with tf.variable_scope("prior"): self.hps.top_shape = z.get_shape().as_list()[1:] logp, sample, get_eps = prior(self.y, self.hps) obj = logp(z) eps.append(get_eps(z)) objective += obj self.objective = -objective # Class label predict with latent representation if self.hps.ycond: z_y = tf.reduce_mean(z, axis=[1, 2]) self.logits = linear_zeros(z_y, self.num_classes, name="classifier") return eps
def decode(self, labels=None, condition=None, epsilon=None): """ Args: labels: Class label, could be none condition: 2D or 4D tensor, condition for dynamic linear transformation epsilon: None or list. If specified, it should be a list with `num_levels` elements Returns: x: 4D tensor, generated samples """ with tf.variable_scope("prior", reuse=tf.AUTO_REUSE): if labels is None: y_onehot = self.y elif len(labels.shape) == 1: y_onehot = tf.one_hot(labels, depth=self.num_classes, dtype=tf.float32) elif len(labels.shape) == 2: y_onehot = labels _, sample, get_eps = prior(y_onehot, self.hps) if epsilon is not None: eps = epsilon if len(epsilon) == self.hps.num_levels else [None] * (self.hps.num_levels-1) + epsilon else: eps = [None] * self.hps.num_levels z = sample(eps=eps[-1]) objective = tf.zeros(tf.shape(z)[0]) if self.hps.conditioning and condition is None: condition = y_onehot # with tf.variable_scope("cond_preprocess", reuse=tf.AUTO_REUSE): # condition = tf.layers.dense(condition, units=10, use_bias=False) z, objective = codec(z, cond=condition, hps=self.hps, reverse=True, objective=objective, eps=eps[:-1], reuse=tf.AUTO_REUSE) with tf.variable_scope("postprocess"): x = unsqueeze2d(z) x = tf.clip_by_value(tf.floor((x+.5)*self.num_bins)*(256./self.num_bins), 0, 255) self.gen_x = tf.cast(x, 'uint8') return self.gen_x
def encode(self, inputs, labels, condition=None): ## Dequantization by adding uniform noise with tf.variable_scope("preprocess"): self.y = tf.one_hot(labels, depth=self.num_classes, dtype=tf.float32) inputs = tf.cast(inputs, 'float32') self.height, self.width, self.channels = inputs.get_shape().as_list()[1:] if self.hps.num_bits_x < 8: inputs = tf.floor(inputs/2**(8-self.hps.num_bits_x)) inputs = inputs / self.num_bins - 0.5 inputs = inputs + tf.random_uniform(tf.shape(inputs), 0, 1./self.num_bins) objective = tf.zeros(tf.shape(inputs)[0]) objective += -np.log(self.num_bins) * np.prod(ops.shape(inputs)[1:]) inputs = squeeze2d(inputs) ## Encoder if self.hps.conditioning and condition is None: condition = self.y # with tf.variable_scope("cond_preprocess"): # condition = tf.layers.dense(condition, units=10, use_bias=False) z, objective, eps = codec(inputs, cond=condition, objective=objective, hps=self.hps, reverse=False) ## Prior with tf.variable_scope("prior"): self.hps.top_shape = z.get_shape().as_list()[1:] logp, sample, get_eps = prior(self.y, self.hps) obj = logp(z) eps.append(get_eps(z)) objective += obj self.objective = -objective # Class label predict with latent representation if self.hps.ycond: z_y = tf.reduce_mean(z, axis=[1, 2]) self.logits = linear_zeros(z_y, self.num_classes, name="classifier") return eps
def encode(self, inputs, labels, condition=None): # line268 # Dequantization by adding uniform noise 加入均匀噪声来反量化 with tf.variable_scope("preprocess"): # 采用One-hot编码 self.y = tf.one_hot(labels, depth=self.num_classes, dtype=tf.float32) inputs = tf.cast(inputs, 'float32') # tf.cast()数据类型转换 self.height, self.width, self.channels = inputs.get_shape( ).as_list()[1:] # num_bits、num_bin:??? if self.hps.num_bits_x < 8: inputs = tf.floor(inputs / 2**(8 - self.hps.num_bits_x)) inputs = inputs / self.num_bins - 0.5 # 输入加入随机正态分布 inputs = inputs + tf.random_uniform(tf.shape(inputs), 0, 1. / self.num_bins) # objective:??? objective = tf.zeros(tf.shape(inputs)[0]) # np.prod:计算数组中所有元素的乘积 objective += -np.log(self.num_bins) * np.prod( ops.shape(inputs)[1:]) # 使用Squeezing操作(在进入Squeezing之前的操作未知) print("before inter squeeze2d, input.shape=" + inputs.shape()) inputs = squeeze2d(inputs) # inputs的shape为[图片数, 高度, 宽度, 通道数] # Encoder 编码 # 下面作用未知 if self.hps.conditioning and condition is None: condition = self.y # with tf.variable_scope("cond_preprocess"): # condition = tf.layers.dense(condition, units=10, use_bias=False) print("before inter model.codec, inputs.shape=" + inputs.shape()) z, objective, eps = codec(inputs, cond=condition, objective=objective, hps=self.hps, reverse=False) # line 11 # Prior 先验 with tf.variable_scope("prior"): self.hps.top_shape = z.get_shape().as_list()[1:] logp, sample, get_eps = prior(self.y, self.hps) obj = logp(z) eps.append(get_eps(z)) objective += obj self.objective = -objective # Class label predict with latent representation:潜变量标签预测 if self.hps.ycond: z_y = tf.reduce_mean(z, axis=[1, 2]) self.logits = linear_zeros(z_y, self.num_classes, name="classifier") return eps