def encoder(pc_inp, n_pc_points=10240, n_filters=[64, 128, 256, 1024], filter_sizes=[1, 1, 1, 1], strides=[1, 1, 1, 1], weight_decay=0.001, b_norm=True, regularizer=None, non_linearity=tf.nn.leaky_relu, symmetry=tf.reduce_max, dropout_prob=None, pool_sizes=None, scope=None, reuse=False, padding='same', verbose=False, closing=None): n_layers = len(n_filters) assert n_layers == len(filter_sizes) assert n_layers == len(strides) #'n_filters': [64, 128, 128, 256, 128] x = pc_inp for i in xrange(n_layers): # conv name = "encoder_conv_layer_" + str(i) scope_i = expand_scope_by_name(scope, name) x = tflearn.layers.conv.conv_1d(x, nb_filter=n_filters[i], filter_size=filter_sizes[i], strides=strides[i], regularizer=regularizer, weight_decay=weight_decay, name=name, reuse=reuse, scope=scope_i, padding=padding) if verbose: print(name, 'conv params = ', np.prod( x.W.get_shape().as_list()) + np.prod(x.b.get_shape().as_list())) # bn if b_norm: name += '_bnorm' scope_i = expand_scope_by_name(scope, name) x = tflearn.layers.normalization.batch_normalization( x, name=name, reuse=reuse, scope=scope_i) if verbose: print('bnorm params = ', np.prod(x.beta.get_shape( ).as_list()) + np.prod(x.gamma.get_shape().as_list())) # non_linearity if non_linearity is not None: x = non_linearity(x) if symmetry is not None: x = symmetry(x, axis=1) if verbose: print(x) return x
def mlp_discriminator(in_signal, non_linearity=tf.nn.relu, reuse=False, scope=None, b_norm=True, dropout_prob=None): ''' used in nips submission. ''' encoder_args = { 'n_filters': [64, 128, 256, 256, 512], 'filter_sizes': [1, 1, 1, 1, 1], 'strides': [1, 1, 1, 1, 1] } encoder_args['reuse'] = reuse encoder_args['scope'] = scope encoder_args['non_linearity'] = non_linearity encoder_args['dropout_prob'] = dropout_prob encoder_args['b_norm'] = b_norm layer = encoder_with_convs_and_symmetry(in_signal, **encoder_args) name = 'decoding_logits' scope_e = expand_scope_by_name(scope, name) d_logit = decoder_with_fc_only(layer, layer_sizes=[128, 64, 1], b_norm=b_norm, reuse=reuse, scope=scope_e) d_prob = tf.nn.sigmoid(d_logit) return d_prob, d_logit
def decoder_with_convs_only(in_signal, n_filters, filter_sizes, strides, padding='same', b_norm=True, non_linearity=tf.nn.relu, conv_op=conv_1d, regularizer=None, weight_decay=0.001, dropout_prob=None, upsample_sizes=None, b_norm_finish=False, scope=None, reuse=False, verbose=False): if verbose: print 'Building Decoder' n_layers = len(n_filters) filter_sizes = replicate_parameter_for_all_layers(filter_sizes, n_layers) strides = replicate_parameter_for_all_layers(strides, n_layers) dropout_prob = replicate_parameter_for_all_layers(dropout_prob, n_layers) for i in xrange(n_layers): if i == 0: layer = in_signal name = 'decoder_conv_layer_' + str(i) scope_i = expand_scope_by_name(scope, name) layer = conv_op(layer, nb_filter=n_filters[i], filter_size=filter_sizes[i], strides=strides[i], padding=padding, regularizer=regularizer, weight_decay=weight_decay, name=name, reuse=reuse, scope=scope_i) if verbose: print name, 'conv params = ', np.prod(layer.W.get_shape().as_list()) + np.prod(layer.b.get_shape().as_list()), if (b_norm and i < n_layers - 1) or (i == n_layers - 1 and b_norm_finish): name += '_bnorm' scope_i = expand_scope_by_name(scope, name) layer = batch_normalization(layer, name=name, reuse=reuse, scope=scope_i) if verbose: print 'bnorm params = ', np.prod(layer.beta.get_shape().as_list()) + np.prod(layer.gamma.get_shape().as_list()) if non_linearity is not None and i < n_layers - 1: # Last layer doesn't have a non-linearity. layer = non_linearity(layer) if dropout_prob is not None and dropout_prob[i] > 0: layer = dropout(layer, 1.0 - dropout_prob[i]) if upsample_sizes is not None and upsample_sizes[i] is not None: layer = tf.tile(layer, multiples=[1, upsample_sizes[i], 1]) if verbose: print layer print 'output size:', np.prod(layer.get_shape().as_list()[1:]), '\n' return layer
def decoder(z_inp, layer_sizes=[256, 256, 6144], b_norm=True, non_linearity=tf.nn.relu, regularizer=None, weight_decay=0.001, reuse=False, scope=None, dropout_prob=None, b_norm_finish=False, verbose=False): if verbose: print('Building Decoder') layer = z_inp n_layers = len(layer_sizes) for i, filter_size in enumerate(layer_sizes): name = 'decoder_fc_' + str(i) scope_i = expand_scope_by_name(scope, name) layer = tflearn.layers.core.fully_connected(layer, filter_size, activation='linear', weights_init='xavier', name=name, regularizer=regularizer, weight_decay=weight_decay, reuse=reuse, scope=scope_i) if verbose: print(name, 'FC params = ', np.prod( layer.W.get_shape().as_list()) + np.prod(layer.b.get_shape().as_list())) if i < (n_layers - 1): layer = non_linearity(layer) return layer
def encoder_with_convs_and_symmetry(in_signal, n_filters=[64, 128, 256, 1024], filter_sizes=[1], strides=[1], b_norm=True, non_linearity=tf.nn.relu, regularizer=None, weight_decay=0.001, symmetry=tf.reduce_max, dropout_prob=None, pool=avg_pool_1d, pool_sizes=None, scope=None, reuse=False, padding='same', verbose=False, closing=None, conv_op=conv_1d): '''An Encoder (recognition network), which maps inputs onto a latent space. ''' if verbose: print 'Building Encoder' n_layers = len(n_filters) filter_sizes = replicate_parameter_for_all_layers(filter_sizes, n_layers) strides = replicate_parameter_for_all_layers(strides, n_layers) dropout_prob = replicate_parameter_for_all_layers(dropout_prob, n_layers) if n_layers < 2: raise ValueError('More than 1 layers are expected.') for i in xrange(n_layers): if i == 0: layer = in_signal name = 'encoder_conv_layer_' + str(i) scope_i = expand_scope_by_name(scope, name) layer = conv_op(layer, nb_filter=n_filters[i], filter_size=filter_sizes[i], strides=strides[i], regularizer=regularizer, weight_decay=weight_decay, name=name, reuse=reuse, scope=scope_i, padding=padding) if verbose: print name, 'conv params = ', np.prod( layer.W.get_shape().as_list()) + np.prod( layer.b.get_shape().as_list()), if b_norm: name += '_bnorm' scope_i = expand_scope_by_name(scope, name) layer = batch_normalization(layer, name=name, reuse=reuse, scope=scope_i) if verbose: print 'bnorm params = ', np.prod( layer.beta.get_shape().as_list()) + np.prod( layer.gamma.get_shape().as_list()) if non_linearity is not None: layer = non_linearity(layer) if pool is not None and pool_sizes is not None: if pool_sizes[i] is not None: layer = pool(layer, kernel_size=pool_sizes[i]) if dropout_prob is not None and dropout_prob[i] > 0: layer = dropout(layer, 1.0 - dropout_prob[i]) if verbose: print layer print 'output size:', np.prod( layer.get_shape().as_list()[1:]), '\n' if symmetry is not None: layer = symmetry(layer, axis=1) if verbose: print layer if closing is not None: layer = closing(layer) print layer return layer
def decoder_with_fc_only(latent_signal, layer_sizes=[], b_norm=True, non_linearity=tf.nn.relu, regularizer=None, weight_decay=0.001, reuse=False, scope=None, dropout_prob=None, b_norm_finish=False, verbose=False): '''A decoding network which maps points from the latent space back onto the data space. ''' if verbose: print 'Building Decoder' n_layers = len(layer_sizes) dropout_prob = replicate_parameter_for_all_layers(dropout_prob, n_layers) if n_layers < 2: raise ValueError( 'For an FC decoder with single a layer use simpler code.') for i in xrange(0, n_layers - 1): name = 'decoder_fc_' + str(i) scope_i = expand_scope_by_name(scope, name) if i == 0: layer = latent_signal layer = fully_connected(layer, layer_sizes[i], activation='linear', weights_init='xavier', name=name, regularizer=regularizer, weight_decay=weight_decay, reuse=reuse, scope=scope_i) if verbose: print name, 'FC params = ', np.prod( layer.W.get_shape().as_list()) + np.prod( layer.b.get_shape().as_list()), if b_norm: name += '_bnorm' scope_i = expand_scope_by_name(scope, name) layer = batch_normalization(layer, name=name, reuse=reuse, scope=scope_i) if verbose: print 'bnorm params = ', np.prod( layer.beta.get_shape().as_list()) + np.prod( layer.gamma.get_shape().as_list()) if non_linearity is not None: layer = non_linearity(layer) if dropout_prob is not None and dropout_prob[i] > 0: layer = dropout(layer, 1.0 - dropout_prob[i]) if verbose: print layer print 'output size:', np.prod( layer.get_shape().as_list()[1:]), '\n' # Last decoding layer never has a non-linearity. name = 'decoder_fc_' + str(n_layers - 1) scope_i = expand_scope_by_name(scope, name) layer = fully_connected(layer, layer_sizes[n_layers - 1], activation='linear', weights_init='xavier', name=name, regularizer=regularizer, weight_decay=weight_decay, reuse=reuse, scope=scope_i) if verbose: print name, 'FC params = ', np.prod( layer.W.get_shape().as_list()) + np.prod( layer.b.get_shape().as_list()), if b_norm_finish: name += '_bnorm' scope_i = expand_scope_by_name(scope, name) layer = batch_normalization(layer, name=name, reuse=reuse, scope=scope_i) if verbose: print 'bnorm params = ', np.prod( layer.beta.get_shape().as_list()) + np.prod( layer.gamma.get_shape().as_list()) if verbose: print layer print 'output size:', np.prod(layer.get_shape().as_list()[1:]), '\n' return layer