我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tensorflow.contrib.slim.flatten()。
def create_model(self, model_input, num_classes=2, l2_penalty=1e-8, **unused_params): """Creates a logistic model. Args: model_input: 'batch' x 'num_features' matrix of input features. vocab_size: The number of classes in the dataset. Returns: A dictionary with a tensor containing the probability predictions of the model in the 'predictions' key. The dimensions of the tensor are batch_size x num_classes.""" net = slim.flatten(model_input) output = slim.fully_connected( net, num_classes - 1, activation_fn=tf.nn.sigmoid, weights_regularizer=slim.l2_regularizer(l2_penalty)) return {"predictions": output}
def create_model(self, model_input, num_classes=10, l2_penalty=1e-8, **unused_params): """Creates a logistic model. Args: model_input: 'batch' x 'num_features' matrix of input features. num_classes: The number of classes in the dataset. Returns: A dictionary with a tensor containing the probability predictions of the model in the 'predictions' key. The dimensions of the tensor are batch_size x num_classes.""" net = slim.flatten(model_input) output = slim.fully_connected( net, num_classes, activation_fn=None, weights_regularizer=slim.l2_regularizer(l2_penalty)) return {"predictions": output}
def encoder(self, images, is_training): activation_fn = leaky_relu # tf.nn.relu weight_decay = 0.0 with tf.variable_scope('encoder'): with slim.arg_scope([slim.batch_norm], is_training=is_training): with slim.arg_scope([slim.conv2d, slim.fully_connected], weights_initializer=tf.truncated_normal_initializer(stddev=0.1), weights_regularizer=slim.l2_regularizer(weight_decay), normalizer_fn=slim.batch_norm, normalizer_params=self.batch_norm_params): net = images net = slim.conv2d(net, 32, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_1a') net = slim.repeat(net, 3, conv2d_block, 0.1, 32, [4, 4], 1, activation_fn=activation_fn, scope='Conv2d_1b') net = slim.conv2d(net, 64, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_2a') net = slim.repeat(net, 3, conv2d_block, 0.1, 64, [4, 4], 1, activation_fn=activation_fn, scope='Conv2d_2b') net = slim.conv2d(net, 128, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_3a') net = slim.repeat(net, 3, conv2d_block, 0.1, 128, [4, 4], 1, activation_fn=activation_fn, scope='Conv2d_3b') net = slim.conv2d(net, 256, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_4a') net = slim.repeat(net, 3, conv2d_block, 0.1, 256, [4, 4], 1, activation_fn=activation_fn, scope='Conv2d_4b') net = slim.flatten(net) fc1 = slim.fully_connected(net, self.latent_variable_dim, activation_fn=None, normalizer_fn=None, scope='Fc_1') fc2 = slim.fully_connected(net, self.latent_variable_dim, activation_fn=None, normalizer_fn=None, scope='Fc_2') return fc1, fc2
def encoder(self, images, is_training): activation_fn = leaky_relu # tf.nn.relu weight_decay = 0.0 with tf.variable_scope('encoder'): with slim.arg_scope([slim.batch_norm], is_training=is_training): with slim.arg_scope([slim.conv2d, slim.fully_connected], weights_initializer=tf.truncated_normal_initializer(stddev=0.1), weights_regularizer=slim.l2_regularizer(weight_decay), normalizer_fn=slim.batch_norm, normalizer_params=self.batch_norm_params): net = slim.conv2d(images, 32, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_1') net = slim.conv2d(net, 64, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_2') net = slim.conv2d(net, 128, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_3') net = slim.conv2d(net, 256, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_4') net = slim.flatten(net) fc1 = slim.fully_connected(net, self.latent_variable_dim, activation_fn=None, normalizer_fn=None, scope='Fc_1') fc2 = slim.fully_connected(net, self.latent_variable_dim, activation_fn=None, normalizer_fn=None, scope='Fc_2') return fc1, fc2
def encoder(self, images, is_training): activation_fn = leaky_relu # tf.nn.relu weight_decay = 0.0 with tf.variable_scope('encoder'): with slim.arg_scope([slim.batch_norm], is_training=is_training): with slim.arg_scope([slim.conv2d, slim.fully_connected], weights_initializer=tf.truncated_normal_initializer(stddev=0.1), weights_regularizer=slim.l2_regularizer(weight_decay), normalizer_fn=slim.batch_norm, normalizer_params=self.batch_norm_params): net = slim.conv2d(images, 32, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_1') net = slim.conv2d(net, 64, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_2') net = slim.conv2d(net, 128, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_3') net = slim.conv2d(net, 256, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_4') net = slim.conv2d(net, 512, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_5') net = slim.flatten(net) fc1 = slim.fully_connected(net, self.latent_variable_dim, activation_fn=None, normalizer_fn=None, scope='Fc_1') fc2 = slim.fully_connected(net, self.latent_variable_dim, activation_fn=None, normalizer_fn=None, scope='Fc_2') return fc1, fc2
def __create_network(self, scope, img_shape=(80, 80)): with tf.variable_scope(self.task_name): with tf.variable_scope(scope): with tf.variable_scope('input_data'): self.inputs = tf.placeholder(shape=[None, *img_shape, cfg.HIST_LEN], dtype=tf.float32) with tf.variable_scope('networks'): with tf.variable_scope('conv_1'): self.conv_1 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.inputs, num_outputs=32, kernel_size=[8, 8], stride=4, padding='SAME', trainable=self.is_train) with tf.variable_scope('conv_2'): self.conv_2 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.conv_1, num_outputs=64, kernel_size=[4, 4], stride=2, padding='SAME', trainable=self.is_train) with tf.variable_scope('conv_3'): self.conv_3 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.conv_2, num_outputs=64, kernel_size=[3, 3], stride=1, padding='SAME', trainable=self.is_train) with tf.variable_scope('f_c'): self.fc = slim.fully_connected(slim.flatten(self.conv_3), 512, activation_fn=tf.nn.elu, trainable=self.is_train)
def build_model(self, inputs, learner_type=commons.LearnerType.Classifier): _, endpoints = self.incep4_model.build_model(inputs) # get feature output. basenet_output = endpoints[self.incep4_model.net_params.output_layer_name] if len(basenet_output.get_shape()) > 2: basenet_output_flat = slim.flatten( basenet_output, scope="baseoutput_flatten") else: basenet_output_flat = basenet_output # add ft layer. new_logits = slim.fully_connected( basenet_output_flat, self.net_params.cls_num, activation_fn=None, scope="ft/logits") # monitor ft layer output. base_model.add_tensor_summary( new_logits.name, new_logits, use_histogram=True, use_sparsity=True) return new_logits, endpoints
def create_network(self, input, trainable): if trainable: wr = slim.l2_regularizer(self.regularization) else: wr = None # the input is stack of black and white frames. # put the stack in the place of channel (last in tf) input_t = tf.transpose(input, [0, 2, 3, 1]) net = slim.conv2d(input_t, 8, (7, 7), data_format="NHWC", activation_fn=tf.nn.relu, stride=3, weights_regularizer=wr, trainable=trainable) net = slim.max_pool2d(net, 2, 2) net = slim.conv2d(net, 16, (3, 3), data_format="NHWC", activation_fn=tf.nn.relu, weights_regularizer=wr, trainable=trainable) net = slim.max_pool2d(net, 2, 2) net = slim.flatten(net) net = slim.fully_connected(net, 256, activation_fn=tf.nn.relu, weights_regularizer=wr, trainable=trainable) q_state_action_values = slim.fully_connected(net, self.dim_actions, activation_fn=None, weights_regularizer=wr, trainable=trainable) return q_state_action_values
def __call__(self, inputs, state, scope=None): batch_size = tf.shape(inputs)[0] if self._apply_to == 'input': inputs = slim.flatten(inputs) if self._shape == -1 else tf.reshape(inputs, [batch_size] + self._shape) return self._cell(inputs, state) elif self._apply_to == 'output': output, res_state = self._cell(inputs, state) output = slim.flatten(output) if self._shape == -1 else tf.reshape(output, [batch_size] + self._shape) return output, res_state elif self._apply_to == 'state': output, res_state = self._cell(inputs, state) res_state = slim.flatten(res_state) if self._shape == -1 else tf.reshape(res_state, [batch_size] + self._shape) return output, res_state else: raise ValueError('Unknown apply_to: "{}"'.format(self._apply_to))
def cnn_layers(inputs, scope, end_points_collection, dropout_keep_prob=0.8, is_training=True): with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d], outputs_collections=[end_points_collection]): net = slim.conv2d(inputs, 48, [5, 5], scope='conv1') net = slim.max_pool2d(net, [2, 2], 2, scope='pool1') net = slim.conv2d(net, 64, [5, 5], scope='conv2') net = slim.max_pool2d(net, [2, 2], 2, scope='pool2') net = slim.conv2d(net, 128, [5, 5], scope='conv3') net = slim.max_pool2d(net, [2, 2], 2, scope='pool3') net = slim.conv2d(net, 160, [5, 5], scope='conv4') net = slim.conv2d(net, 192, [5, 5], scope='conv5') net = slim.conv2d(net, 192, [5, 5], scope='conv6') net = slim.conv2d(net, 192, [5, 5], scope='conv7') net = slim.flatten(net) # By removing the fc layer, we'll get much smaller model with almost the same performance # net = slim.fully_connected(net, 3072, scope='fc8') return net, end_points_collection
def cnn_layers(inputs, scope, end_points_collection, dropout_keep_prob=0.8, is_training=True): with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d], outputs_collections=[end_points_collection]): net = slim.conv2d(inputs, 32, [5, 5], scope='conv1') net = slim.max_pool2d(net, [2, 2], 2, scope='pool1') net = slim.conv2d(net, 64, [5, 5], scope='conv2') net = slim.max_pool2d(net, [2, 2], 2, scope='pool2') net = slim.conv2d(net, 64, [5, 5], scope='conv3') net = slim.max_pool2d(net, [2, 2], 2, scope='pool3') net = slim.conv2d(net, 64, [5, 5], scope='conv4') net = slim.conv2d(net, 64, [5, 5], scope='conv5') net = slim.conv2d(net, 64, [5, 5], scope='conv6') net = slim.conv2d(net, 64, [5, 5], scope='conv7') net = slim.flatten(net) net = slim.fully_connected(net, 256, scope='fc3') return net, end_points_collection
def cnn_layers(inputs, scope, end_points_collection, dropout_keep_prob=0.8, is_training=True): with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d], outputs_collections=[end_points_collection]): net = slim.conv2d(inputs, 32, [5, 5], scope='conv1') net = slim.max_pool2d(net, [2, 2], 2, scope='pool1') net = slim.conv2d(net, 64, [5, 5], scope='conv2') net = slim.max_pool2d(net, [2, 2], 2, scope='pool2') net = slim.conv2d(net, 64, [5, 5], scope='conv3') net = slim.max_pool2d(net, [2, 2], 2, scope='pool3') net = slim.conv2d(net, 64, [5, 5], scope='conv4') net = slim.conv2d(net, 64, [5, 5], scope='conv5') net = slim.conv2d(net, 64, [5, 5], scope='conv6') net = slim.conv2d(net, 64, [5, 5], scope='conv7') net = slim.flatten(net) net = slim.fully_connected(net, 128, scope='fc3') return net, end_points_collection
def create_model(self, model_input, num_classes=2, l2_penalty=1e-8, **unused_params): net = slim.conv2d(model_input, 64, [3, 3], scope='conv1_1') # net = slim.conv2d(net, 64, [3, 3], scope='conv1_2') net = slim.max_pool2d(net, [2, 2], scope='pool1') # net = slim.conv2d(net, 128, [3, 3], scope='conv2_1') # net = slim.conv2d(net, 128, [3, 3], scope='conv2_2') # net = slim.max_pool2d(net, [2, 2], scope='pool2') # net = slim.conv2d(net, 258, [3, 3], scope='conv3_1') # net = slim.conv2d(net, 258, [3, 3], scope='conv3_2') # net = slim.max_pool2d(net, [2, 2], scope='pool3') net = slim.flatten(net) output = slim.fully_connected( net, num_classes - 1, activation_fn=tf.nn.sigmoid, weights_regularizer=slim.l2_regularizer(l2_penalty)) return {"predictions": output}
def create_base(self, inputs, is_training): params = self._config.cnn_params print("input dimension = {}".format(inputs.get_shape())) with tf.name_scope('Model'): with slim.arg_scope([slim.conv2d, slim.fully_connected], activation_fn=tf.nn.relu, # normalizer_fn=slim.batch_norm, # normalizer_params={'is_training': is_training} # weights_initializer=initializer = tf.contrib.layers.xavier_initializer(seed = 10) ): # inputs is 2D with dimension (3 x feature_len) net = slim.conv2d(inputs, params['num_filters'][0], [3,5], scope='conv1') net = slim.conv2d(net, params['num_filters'][1], [3, 5], scope='conv2') net = slim.conv2d(net, params['num_filters'][2], [3, 5], scope='conv3') net = slim.flatten(net, scope='flatten1') net = slim.fully_connected(net, params['num_fc_1'], scope='fc1') net = slim.dropout(net, self._config.keep_prob, is_training=is_training, scope='dropout1') logits = slim.fully_connected(net, self._config.num_classes, activation_fn=None, scope='fc2') with tf.name_scope('output'): predicted_classes = tf.to_int32(tf.argmax(logits, dimension=1), name='y') return logits, predicted_classes
def discriminator_from_params(x, params): with tf.variable_scope('Discriminator'): c1 = conv2d(x, [5, 5], [1, 2, 2, 1], 16, scope='conv1', params=params[0:2]) c2 = conv2d(c1, [5, 5], [1, 2, 2, 1], 64, scope='conv2', params=params[2:4]) f0 = slim.flatten(c2) f1 = dense(f0, 100, scope='dense1', params=params[4:6]) f2 = dense(f1, 10, scope='dense2', params=params[6:8]) return f2 # hid = dense(x, n_hid, scope='l1', params=params[:2], normalized=True) # hid = tf.nn.relu(hid) # #hid = tf.tanh(hid) # hid = dense(hid, n_hid, scope='l2', params=params[2:4], normalized=True) # hid = tf.nn.relu(hid) # #hid = tf.tanh(hid) # out = tf.nn.sigmoid(dense(hid, 1, scope='d_out', params=params[4:])) # #
def generative_network(z, zdim): """Generative network to parameterize generative model. It takes latent variables as input and outputs the likelihood parameters. logits = neural_network(z) Args: z = tensor input d = latent variable dimension """ with slim.arg_scope([slim.conv2d_transpose], activation_fn=tf.nn.elu, normalizer_fn=slim.batch_norm, normalizer_params={'scale': True}): net = tf.reshape(z, [N_MINIBATCH, 1, 1, zdim]) net = slim.conv2d_transpose(net, 128, 3, padding='VALID') net = slim.conv2d_transpose(net, 64, 5, padding='VALID') net = slim.conv2d_transpose(net, 32, 5, stride=2) net = slim.conv2d_transpose(net, 1, 5, stride=2, activation_fn=None) net = slim.flatten(net) #net = slim.nn.sigmoid(net) return net
def content_extractor(self, images, reuse=False): # images: (batch, 32, 32, 3) or (batch, 32, 32, 1) if images.get_shape()[3] == 1: # For mnist dataset, replicate the gray scale image 3 times. images = tf.image.grayscale_to_rgb(images) with tf.variable_scope('content_extractor', reuse=reuse): with slim.arg_scope([slim.conv2d], padding='SAME', activation_fn=None, stride=2, weights_initializer=tf.contrib.layers.xavier_initializer()): with slim.arg_scope([slim.batch_norm], decay=0.95, center=True, scale=True, activation_fn=tf.nn.relu, is_training=(self.mode=='train' or self.mode=='pretrain')): net = slim.conv2d(images, 64, [3, 3], scope='conv1') # (batch_size, 16, 16, 64) net = slim.batch_norm(net, scope='bn1') net = slim.conv2d(net, 128, [3, 3], scope='conv2') # (batch_size, 8, 8, 128) net = slim.batch_norm(net, scope='bn2') net = slim.conv2d(net, 256, [3, 3], scope='conv3') # (batch_size, 4, 4, 256) net = slim.batch_norm(net, scope='bn3') net = slim.conv2d(net, 128, [4, 4], padding='VALID', scope='conv4') # (batch_size, 1, 1, 128) net = slim.batch_norm(net, activation_fn=tf.nn.tanh, scope='bn4') if self.mode == 'pretrain': net = slim.conv2d(net, 10, [1, 1], padding='VALID', scope='out') net = slim.flatten(net) return net
def discriminator(self, images, reuse=False): # images: (batch, 32, 32, 1) with tf.variable_scope('discriminator', reuse=reuse): with slim.arg_scope([slim.conv2d], padding='SAME', activation_fn=None, stride=2, weights_initializer=tf.contrib.layers.xavier_initializer()): with slim.arg_scope([slim.batch_norm], decay=0.95, center=True, scale=True, activation_fn=tf.nn.relu, is_training=(self.mode=='train')): net = slim.conv2d(images, 128, [3, 3], activation_fn=tf.nn.relu, scope='conv1') # (batch_size, 16, 16, 128) net = slim.batch_norm(net, scope='bn1') net = slim.conv2d(net, 256, [3, 3], scope='conv2') # (batch_size, 8, 8, 256) net = slim.batch_norm(net, scope='bn2') net = slim.conv2d(net, 512, [3, 3], scope='conv3') # (batch_size, 4, 4, 512) net = slim.batch_norm(net, scope='bn3') net = slim.conv2d(net, 1, [4, 4], padding='VALID', scope='conv4') # (batch_size, 1, 1, 1) net = slim.flatten(net) return net
def __init__(self, name, hidden_layers, input_dims, output_dims): # tf self.sess = tf.get_default_session() with tf.variable_scope(name): self.obs = tf.placeholder(shape=[None, input_dims], dtype=tf.float32) flat_input_state = slim.flatten(self.obs, scope='flat') if hidden_layers == "": self.logits = slim.fully_connected( inputs=flat_input_state, num_outputs=output_dims, activation_fn=None, weights_initializer=tf.zeros_initializer) else: final_hidden = self.hidden_layers_starting_at(flat_input_state, hidden_layers) self.logits = slim.fully_connected( inputs=final_hidden, num_outputs=output_dims, activation_fn=None)
def reparameterize(encoded, num_discrete, tau, hard=False, rnd_sample=None, eps=1e-20): eshp = encoded.get_shape().as_list() print("encoded = ", eshp) num_normal = eshp[1] - num_discrete print 'num_normal = ', num_normal logits_normal = encoded[:, 0:num_normal] logits_gumbel = encoded[:, num_normal:eshp[1]] # we reparameterize using both the N(0, I) and the gumbel(0, 1) z_discrete, kl_discrete = gumbel_reparmeterization(logits_gumbel, tau, rnd_sample, hard) z_n, kl_n = gaussian_reparmeterization(logits_normal) # merge and pad appropriately z = tf.concat([z_n, z_discrete], axis=1) return [slim.flatten(z), slim.flatten(z_n), slim.flatten(z_discrete), slim.flatten(tf.nn.softmax(logits_gumbel)), kl_n, kl_discrete]
def __init__(self, nb_actions): self.state = tf.placeholder(tf.float32, [None, 84, 84, 4]) cnn_1 = slim.conv2d(self.state, 16, [8,8], stride=4, scope='cnn_1', activation_fn=nn.relu) cnn_2 = slim.conv2d(cnn_1, 32, [4,4], stride=2, scope='cnn_2', activation_fn=nn.relu) flatten = slim.flatten(cnn_2) fcc_1 = slim.fully_connected(flatten, 256, scope='fcc_1', activation_fn=nn.relu) self.advantage = slim.fully_connected(fcc_1, nb_actions, scope='advantage', activation_fn=None) self.value_state = slim.fully_connected(fcc_1, 1, scope='value_state', activation_fn=None) self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name)
def _encoder(self, x, is_training=None): net = self.arch['encoder'] for i, (o, k, s) in enumerate(zip(net['output'], net['kernel'], net['stride'])): x = conv2d_nchw_layernorm( x, o, k, s, lrelu, name='Conv2d-{}'.format(i) ) x = slim.flatten(x) z_mu = tf.layers.dense(x, self.arch['z_dim']) z_lv = tf.layers.dense(x, self.arch['z_dim']) return z_mu, z_lv
def loss(self, x, y): with tf.name_scope('loss'): z_mu, z_lv = self._encode(x) z = GaussianSampleLayer(z_mu, z_lv) xh = self._generate(z, y) D_KL = tf.reduce_mean( GaussianKLD( slim.flatten(z_mu), slim.flatten(z_lv), slim.flatten(tf.zeros_like(z_mu)), slim.flatten(tf.zeros_like(z_lv)), ) ) logPx = tf.reduce_mean( GaussianLogDensity( slim.flatten(x), slim.flatten(xh), tf.zeros_like(slim.flatten(xh))), ) loss = dict() loss['G'] = - logPx + D_KL loss['D_KL'] = D_KL loss['logP'] = logPx tf.summary.scalar('KL-div', D_KL) tf.summary.scalar('logPx', logPx) tf.summary.histogram('xh', xh) tf.summary.histogram('x', x) return loss
def _head_to_tail(self, pool5, is_training, reuse=False): with tf.variable_scope(self._scope, self._scope, reuse=reuse): pool5_flat = slim.flatten(pool5, scope='flatten') fc6 = slim.fully_connected(pool5_flat, 4096, scope='fc6') if is_training: fc6 = slim.dropout(fc6, keep_prob=0.5, is_training=True, scope='dropout6') fc7 = slim.fully_connected(fc6, 4096, scope='fc7') if is_training: fc7 = slim.dropout(fc7, keep_prob=0.5, is_training=True, scope='dropout7') return fc7
def forward(self): temp = tf.transpose( self.inp.out, [0,3,1,2]) self.out = slim.flatten( temp, scope = self.scope)
def __init__(self, namespace, actor): super(CriticNetwork, self).__init__(namespace) # input state to the critic is the _same_ state given to the actor. # input action to the critic is simply the output action of the actor. # even though when training we explicitly provide a new value for the # input action (via the input_action placeholder) we need to be stop the gradient # flowing to the actor since there is a path through the actor to the input_state # too, hence we need to be explicit about cutting it (otherwise training the # critic will attempt to train the actor too. self.input_state = actor.input_state self.input_action = tf.stop_gradient(actor.output_action) with tf.variable_scope(namespace): if opts.use_raw_pixels: conv_net = self.simple_conv_net_on(self.input_state, opts) # TODO: use base_network helper hidden1 = slim.fully_connected(conv_net, 200, scope='hidden1') hidden2 = slim.fully_connected(hidden1, 50, scope='hidden2') concat_inputs = tf.concat(1, [hidden2, self.input_action]) final_hidden = slim.fully_connected(concat_inputs, 50, scope="hidden3") else: # stack of hidden layers on flattened input; (batch,2,2,7) -> (batch,28) flat_input_state = slim.flatten(self.input_state, scope='flat') concat_inputs = tf.concat(1, [flat_input_state, self.input_action]) final_hidden = self.hidden_layers_starting_at(concat_inputs, opts.critic_hidden_layers) # output from critic is a single q-value self.q_value = slim.fully_connected(scope='q_value', inputs=final_hidden, num_outputs=1, weights_regularizer=tf.contrib.layers.l2_regularizer(0.01), activation_fn=None)
def input_state_network(self, input_state, opts): # TODO: use in lrpg and ddpg too if opts.use_raw_pixels: input_state = self.simple_conv_net_on(input_state, opts) flattened_input_state = slim.flatten(input_state, scope='flat') return self.hidden_layers_starting_at(flattened_input_state, opts.hidden_layers, opts)
def iter_func(self, state): sc = predictron_arg_scope() with tf.variable_scope('value'): value_net = slim.fully_connected(slim.flatten(state), 32, scope='fc0') value_net = layers.batch_norm(value_net, activation_fn=tf.nn.relu, scope='fc0/preact') value_net = slim.fully_connected(value_net, self.maze_size, activation_fn=None, scope='fc1') with slim.arg_scope(sc): net = slim.conv2d(state, 32, [3, 3], scope='conv1') net = layers.batch_norm(net, activation_fn=tf.nn.relu, scope='conv1/preact') net_flatten = slim.flatten(net, scope='conv1/flatten') with tf.variable_scope('reward'): reward_net = slim.fully_connected(net_flatten, 32, scope='fc0') reward_net = layers.batch_norm(reward_net, activation_fn=tf.nn.relu, scope='fc0/preact') reward_net = slim.fully_connected(reward_net, self.maze_size, activation_fn=None, scope='fc1') with tf.variable_scope('gamma'): gamma_net = slim.fully_connected(net_flatten, 32, scope='fc0') gamma_net = layers.batch_norm(gamma_net, activation_fn=tf.nn.relu, scope='fc0/preact') gamma_net = slim.fully_connected(gamma_net, self.maze_size, activation_fn=tf.nn.sigmoid, scope='fc1') with tf.variable_scope('lambda'): lambda_net = slim.fully_connected(net_flatten, 32, scope='fc0') lambda_net = layers.batch_norm(lambda_net, activation_fn=tf.nn.relu, scope='fc0/preact') lambda_net = slim.fully_connected(lambda_net, self.maze_size, activation_fn=tf.nn.sigmoid, scope='fc1') net = slim.conv2d(net, 32, [3, 3], scope='conv2') net = layers.batch_norm(net, activation_fn=tf.nn.relu, scope='conv2/preact') net = slim.conv2d(net, 32, [3, 3], scope='conv3') net = layers.batch_norm(net, activation_fn=tf.nn.relu, scope='conv3/preact') return net, reward_net, gamma_net, lambda_net, value_net
def _head_to_tail(self, pool5, is_training, reuse=None): with tf.variable_scope(self._scope, self._scope, reuse=reuse): pool5_flat = slim.flatten(pool5, scope='flatten') fc6 = slim.fully_connected(pool5_flat, 4096, scope='fc6') if is_training: fc6 = slim.dropout(fc6, keep_prob=0.5, is_training=True, scope='dropout6') fc7 = slim.fully_connected(fc6, 4096, scope='fc7') if is_training: fc7 = slim.dropout(fc7, keep_prob=0.5, is_training=True, scope='dropout7') return fc7
def my_cnn(images, num_classes, is_training): # is_training is not used... with slim.arg_scope([slim.max_pool2d], kernel_size=[3, 3], stride=2): net = slim.conv2d(images, 64, [5, 5]) net = slim.max_pool2d(net) net = slim.conv2d(net, 64, [5, 5]) net = slim.max_pool2d(net) net = slim.flatten(net) net = slim.fully_connected(net, 192) net = slim.fully_connected(net, num_classes, activation_fn=None) return net
def __init__(self, cell, shape='flatten', apply_to='output'): self._cell = cell self._shape = shape self._apply_to = apply_to
def construct_net(self,is_trained = True): with slim.arg_scope([slim.conv2d], padding='VALID', weights_initializer=tf.truncated_normal_initializer(stddev=0.01), weights_regularizer=slim.l2_regularizer(0.0005)): net = slim.conv2d(self.input_images,6,[5,5],1,padding='SAME',scope='conv1') net = slim.max_pool2d(net, [2, 2], scope='pool2') net = slim.conv2d(net,16,[5,5],1,scope='conv3') net = slim.max_pool2d(net, [2, 2], scope='pool4') net = slim.conv2d(net,120,[5,5],1,scope='conv5') net = slim.flatten(net, scope='flat6') net = slim.fully_connected(net, 84, scope='fc7') net = slim.dropout(net, self.dropout,is_training=is_trained, scope='dropout8') digits = slim.fully_connected(net, 10, scope='fc9') return digits
def cnn_layers(inputs, scope, end_points_collection, dropout_keep_prob=0.8, is_training=True): with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d], outputs_collections=[end_points_collection]): net = slim.conv2d(inputs, 32, [5, 5], scope='conv1') net = slim.max_pool2d(net, [2, 2], 2, scope='pool1') net = slim.conv2d(net, 64, [5, 5], scope='conv2') net = slim.max_pool2d(net, [2, 2], 2, scope='pool2') net = slim.flatten(net) net = slim.fully_connected(net, 1024, scope='fc3') return net, end_points_collection
def _encoder(self, x, is_training): n_layer = len(self.arch['encoder']['output']) subnet = self.arch['encoder'] with slim.arg_scope( [slim.batch_norm], scale=True, updates_collections=None, decay=0.9, epsilon=1e-5, is_training=is_training, reuse=None): with slim.arg_scope( [slim.conv2d], weights_regularizer=slim.l2_regularizer(subnet['l2-reg']), normalizer_fn=slim.batch_norm, activation_fn=lrelu): for i in range(n_layer): x = slim.conv2d( x, subnet['output'][i], subnet['kernel'][i], subnet['stride'][i]) x = slim.flatten(x) with slim.arg_scope( [slim.fully_connected], num_outputs=self.arch['z_dim'], weights_regularizer=slim.l2_regularizer(subnet['l2-reg']), normalizer_fn=None, activation_fn=None): z_mu = slim.fully_connected(x) z_lv = slim.fully_connected(x) return z_mu, z_lv
def _classifier(self, x, is_training): n_layer = len(self.arch['classifier']['output']) subnet = self.arch['classifier'] with slim.arg_scope( [slim.batch_norm], scale=True, scope='BN', updates_collections=None, # decay=0.9, epsilon=1e-5, # [TODO] Test these hyper-parameters is_training=is_training): with slim.arg_scope( [slim.conv2d], weights_regularizer=slim.l2_regularizer(subnet['l2-reg']), normalizer_fn=slim.batch_norm, activation_fn=lrelu): for i in range(n_layer): x = slim.conv2d( x, subnet['output'][i], subnet['kernel'][i], subnet['stride'][i]) tf.summary.image( 'down-sample{:d}'.format(i), tf.transpose(x[:, :, :, 0:3], [2, 1, 0, 3])) x = slim.flatten(x) with slim.arg_scope( [slim.fully_connected], num_outputs=self.arch['y_dim'], weights_regularizer=slim.l2_regularizer(subnet['l2-reg']), normalizer_fn=None, activation_fn=None): y_logit = slim.fully_connected(x) # z_mu = slim.fully_connected(x) # z_lv = slim.fully_connected(x) # return z_mu, z_lv return y_logit
def test_fc_cnn(): x = tf.placeholder(name='x', shape=[50, 32, 32, 1], dtype=tf.float32) c1 = conv2d(x, [5, 5], [1, 2, 2, 1], 16, scope='conv1') c2 = conv2d(c1, [5, 5], [1, 2, 2, 1], 64, scope='conv2') f0 = slim.flatten(c2) f1 = dense(f0, 100, scope='dense1') f2 = dense(f1, 10, scope='dense2') return f2 # [TODO] Need to test BN # d_params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope.name)
def discriminator(x): with tf.variable_scope('Discriminator'): c1 = conv2d(x, [5, 5], [1, 2, 2, 1], 16, scope='conv1') c2 = conv2d(c1, [5, 5], [1, 2, 2, 1], 64, scope='conv2') f0 = slim.flatten(c2) f1 = dense(f0, 100, scope='dense1') f2 = dense(f1, 10, scope='dense2') return f2
def resnet_model(image,reuse): with tf.variable_scope("model",reuse=reuse): with slim.arg_scope(resnet_v1.resnet_arg_scope()): outputs,_ = resnet_v1.resnet_v1_50(image) #outputs,_ = inception_resnet_v2(image) outputs = slim.flatten(outputs) outputs = slim.fully_connected(outputs,256) logits = slim.fully_connected(outputs,num_classes,activation_fn=None) return outputs,logits # -------- train -----------------------------------
def flatten_fully_connected(inputs, num_outputs, activation_fn=tf.nn.relu, normalizer_fn=None, normalizer_params=None, weights_initializer=slim.xavier_initializer(), weights_regularizer=None, biases_initializer=tf.zeros_initializer(), biases_regularizer=None, reuse=None, variables_collections=None, outputs_collections=None, trainable=True, scope=None): with tf.variable_scope(scope, 'flatten_fully_connected', [inputs]): if inputs.shape.ndims > 2: inputs = slim.flatten(inputs) return slim.fully_connected(inputs, num_outputs, activation_fn, normalizer_fn, normalizer_params, weights_initializer, weights_regularizer, biases_initializer, biases_regularizer, reuse, variables_collections, outputs_collections, trainable, scope)
def build_mnist_model(self, input, use_unpooling): """ Build autoencoder model for mnist dataset as described in the Stacked What-Where autoencoders paper :param input: 4D tensor of source data of shae [batch_size, w, h, channels] :param use_unpooling: indicate whether unpooling layer should be used instead of naive upsampling :return: tuple of tensors: train - train operation encode - bottleneck tensor of the autoencoder network decode - reconstruction of the input """ # Encoder. (16)5c-(32)3c-Xp net = slim.conv2d(input, 16, [5, 5]) net = slim.conv2d(net, 32, [3, 3]) if use_unpooling: encode, mask = max_pool_with_argmax(net, FLAGS.pool_size) net = unpool(encode, mask, stride=FLAGS.pool_size) else: encode = slim.max_pool2d(net, kernel_size=[FLAGS.pool_size, FLAGS.pool_size], stride=FLAGS.pool_size) net = upsample(encode, stride=FLAGS.pool_size) # Decoder net = slim.conv2d_transpose(net, 16, [3, 3]) net = slim.conv2d_transpose(net, 1, [5, 5]) decode = net loss_l2 = tf.nn.l2_loss(slim.flatten(input) - slim.flatten(net)) # Optimizer train = tf.train.AdamOptimizer(learning_rate=FLAGS.learning_rate).minimize(loss_l2) return train, encode, decode
def inference_network(x, xwidth=28, xheight=28, zdim=2): """Inference network to parameterize variational model. It takes data as input and outputs the variational parameters. mu, sigma = neural_network(x) """ with slim.arg_scope([slim.conv2d, slim.fully_connected], activation_fn=tf.nn.elu, normalizer_fn=slim.batch_norm, normalizer_params={'scale': True}): net = tf.reshape(x, [N_MINIBATCH, 28, 28, 1]) net = slim.conv2d(net, 32, 5, stride=2) net = slim.conv2d(net, 64, 5, stride=2) net = slim.conv2d(net, 128, 5, padding='VALID') net = slim.dropout(net, 0.9) net = slim.flatten(net) params = slim.fully_connected(net, zdim * 2, activation_fn=None) mu = params[:, :zdim] #sigma = tf.nn.softplus(params[:, zdim:]) sigma = params[:, zdim:] return mu, sigma ########################################## # make variational lower bound objective # ##########################################
def build_encoder(net, layer_config, i=1, reuse=False): if i == len(layer_config): return net cfg = layer_config[i] cfg.shape = net.get_shape().as_list() name = cfg.enc_op_name if reuse else None cfg.ein = net if cfg.type == FC: if len(cfg.shape) > 2: net = slim.flatten(net) net = slim.fully_connected(net, cfg.size, activation_fn=cfg.activation, scope=name, reuse=reuse) elif cfg.type == CONV: net = slim.conv2d(net, cfg.size, [cfg.kernel, cfg.kernel], stride=cfg.stride, activation_fn=cfg.activation, padding=PADDING, scope=name, reuse=reuse) elif cfg.type == POOL_ARG: net, cfg.argmax = nut.max_pool_with_argmax(net, cfg.kernel) # if not reuse: # mask = nut.fake_arg_max_of_max_pool(cfg.shape, cfg.kernel) # cfg.argmax_dummy = tf.constant(mask.flatten(), shape=mask.shape) elif cfg.type == POOL: net = slim.max_pool2d(net, kernel_size=[cfg.kernel, cfg.kernel], stride=cfg.kernel) elif cfg.type == DO: net = tf.nn.dropout(net, keep_prob=cfg.keep_prob) elif cfg.type == LOSS: cfg.arg1 = net elif cfg.type == INPUT: assert False if not reuse: cfg.enc_op_name = net.name.split('/')[0] if not reuse: ut.print_info('\rencoder_%d\t%s\t%s' % (i, str(net), cfg.enc_op_name), color=CONFIG_COLOR) return build_encoder(net, layer_config, i + 1, reuse=reuse)