我们从Python开源项目中,提取了以下43个代码示例,用于说明如何使用tensorflow.contrib.slim.dropout()。
def densenet_block(inputs, layer_num, growth, bc_mode, scope, is_training, keep_prob): with tf.variable_scope(scope, 'block1', [inputs]): currents = inputs for idx in xrange(layer_num): if not bc_mode: new_feature = slim.conv2d(currents, growth, [3, 3], scope='conv_{:d}'.format(idx)) new_feature = slim.dropout(new_feature, keep_prob=keep_prob, is_training=is_training, scope='dropout_{:d}'.format(idx)) else: new_feature = slim.conv2d(currents, growth*4, [1, 1], scope='bottom_{:d}'.format(idx)) new_feature = slim.dropout(new_feature, keep_prob=keep_prob, is_training=is_training, scope='dropout_b_{:d}'.format(idx)) new_feature = slim.conv2d(new_feature, growth, [3, 3], scope='conv_{:d}'.format(idx)) new_feature = slim.dropout(new_feature, keep_prob=keep_prob, is_training=is_training, scope='dropout_{:d}'.format(idx)) currents = tf.concat([currents, new_feature], axis=3) return currents
def build_inception_v1(self, prediction_fn=tf.nn.relu, scope='InceptionV1'): """ build basic inception v1 model """ # input features [batch_size, height, width, channels] self.x = tf.placeholder(tf.float32, shape=[None, 224, 224, 3], name='input_layer') self.y = tf.placeholder(tf.float32, [None, self.num_classes], name='output_layer') # learning_rate placeholder self.learning_rate = tf.placeholder(tf.float32, name='learning_rate') # dropout layer: keep probability, vgg default value:0.5 self.keep_prob = tf.placeholder(tf.float32, name='keep_prob') with tf.variable_scope(name_or_scope=scope, reuse=False) as scope: net, ent_point_nets = self.inception_v1_base(self.x, scope=scope) with tf.variable_scope('Logits'): net = slim.avg_pool2d(net, kernel_size=[7, 7], stride=1, scope='MaxPool_0a_7x7') net = slim.dropout(net, self.keep_prob, scope='Dropout_0b') # translate [1, 1, 1024] -> [1024] net = net[:, 0, 0, :] self.logits = slim.fully_connected(net, num_outputs=self.num_classes) self.read_out_logits = prediction_fn(self.logits, name='Predictions')
def __init__(self): self.raw_input_image = tf.placeholder(tf.float32, [None, 784]) self.input_images = tf.reshape(self.raw_input_image, [-1, 28, 28, 1]) self.raw_input_label = tf.placeholder("float", [None, 10]) self.input_labels = tf.cast(self.raw_input_label,tf.int32) self.dropout = cfg.KEEP_PROB with tf.variable_scope("Lenet") as scope: self.train_digits = self.construct_net(True) scope.reuse_variables() self.pred_digits = self.construct_net(False) self.prediction = tf.argmax(self.pred_digits, 1) self.correct_prediction = tf.equal(tf.argmax(self.pred_digits, 1), tf.argmax(self.input_labels, 1)) self.train_accuracy = tf.reduce_mean(tf.cast(self.correct_prediction, "float")) self.loss = slim.losses.softmax_cross_entropy(self.train_digits, self.input_labels) self.lr = cfg.LEARNING_RATE self.train_op = tf.train.AdamOptimizer(self.lr).minimize(self.loss)
def fc_layers(net, scope, end_points_collection, num_classes=10, is_training=True, dropout_keep_prob=0.5, name_prefix=None): def full_scope_name(scope_name): return scope_name if name_prefix is None else '%s_%s' % (name_prefix, scope_name) with slim.arg_scope([slim.fully_connected, slim.dropout], outputs_collections=[end_points_collection]): net = slim.fully_connected(net, num_classes, activation_fn=None, scope=full_scope_name('fc9')) return net, end_points_collection
def fc_layers(net, scope, end_points_collection, num_classes=10, is_training=True, dropout_keep_prob=0.5, name_prefix=None): def full_scope_name(scope_name): return scope_name if name_prefix is None else '%s_%s' % (name_prefix, scope_name) with slim.arg_scope([slim.fully_connected, slim.dropout], outputs_collections=[end_points_collection]): ''' with dropout: accuracy: 0.7, data: 6.3M without dropout: accuracy: 0.71, data: 6.3M ''' # net = slim.dropout(net, dropout_keep_prob, is_training=is_training, # scope=full_scope_name('dropout3')) net = slim.fully_connected(net, num_classes, activation_fn=None, scope=full_scope_name('fc4')) return net, end_points_collection
def fc_layers(net, scope, end_points_collection, num_classes=10, is_training=True, dropout_keep_prob=0.5, name_prefix=None): def full_scope_name(scope_name): return scope_name if name_prefix is None else '%s_%s' % (name_prefix, scope_name) with slim.arg_scope([slim.fully_connected, slim.dropout], outputs_collections=[end_points_collection]): ''' with droupout accuracy: 0.68, data: 4.2M without droupout accuracy: 0.71, data: 4.2M ''' # net = slim.dropout(net, dropout_keep_prob, is_training=is_training, # scope=full_scope_name('dropout3')) net = slim.fully_connected(net, num_classes, activation_fn=None, scope=full_scope_name('fc4')) return net, end_points_collection
def fc_layers(net, scope, end_points_collection, num_classes=10, is_training=True, dropout_keep_prob=0.5, name_prefix=None): def full_scope_name(scope_name): return scope_name if name_prefix is None else '%s_%s' % (name_prefix, scope_name) with slim.arg_scope([slim.fully_connected, slim.dropout], outputs_collections=[end_points_collection]): net = slim.dropout(net, dropout_keep_prob, is_training=is_training, scope=full_scope_name('dropout3')) net = slim.fully_connected(net, num_classes, activation_fn=None, scope=full_scope_name('fc4')) return net, end_points_collection
def vgg16(inputs, num_classes, batch_size): with slim.arg_scope([slim.conv2d, slim.fully_connected], activation_fn=tf.nn.relu, weights_initializer=tf.truncated_normal_initializer(0.0, 0.01)): net = slim.repeat(inputs, 2, slim.conv2d, 64, [3, 3], padding="SAME", scope='conv1') net = slim.max_pool2d(net, [2, 2], scope='pool1') net = slim.repeat(net, 2, slim.conv2d, 128, [3, 3], padding="SAME", scope='conv2') net = slim.max_pool2d(net, [2, 2], scope='pool2') net = slim.repeat(net, 3, slim.conv2d, 256, [3, 3], padding="SAME", scope='conv3') net = slim.max_pool2d(net, [2, 2], scope='pool3') net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], padding="SAME", scope='conv4') net = slim.max_pool2d(net, [2, 2], scope='pool4') net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], padding="SAME", scope='conv5') net = slim.max_pool2d(net, [2, 2], scope='pool5') net = tf.reshape(net, (batch_size, 7 * 7 * 512)) net = slim.fully_connected(net, 4096, scope='fc6') net = slim.dropout(net, 0.5, scope='dropout6') net = slim.fully_connected(net, 4096, scope='fc7') net = slim.dropout(net, 0.5, scope='dropout7') net = slim.fully_connected(net, 1000, activation_fn=None, scope='fc8') return net
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 _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 conv_net_kelz(inputs): """Builds the ConvNet from Kelz 2016.""" with slim.arg_scope( [slim.conv2d, slim.fully_connected], activation_fn=tf.nn.relu, weights_initializer=tf.contrib.layers.variance_scaling_initializer( factor=2.0, mode='FAN_AVG', uniform=True)): net = slim.conv2d(inputs, 32, [3, 3], scope='conv1') net = slim.conv2d( net, 32, [3, 3], scope='conv2', normalizer_fn=slim.batch_norm) net = slim.max_pool2d(net, [1, 2], stride=[1, 2], scope='pool2') net = slim.dropout(net, 0.25, scope='dropout2') net = slim.conv2d(net, 64, [3, 3], scope='conv3') net = slim.max_pool2d(net, [1, 2], stride=[1, 2], scope='pool3') net = slim.dropout(net, 0.25, scope='dropout3') # Flatten while preserving batch and time dimensions. dims = tf.shape(net) net = tf.reshape(net, (dims[0], dims[1], net.shape[2].value * net.shape[3].value), 'flatten4') net = slim.fully_connected(net, 512, scope='fc5') net = slim.dropout(net, 0.5, scope='dropout5') return net
def squeezenet_inference(inputs, is_training, keep_prob): nets = slim.conv2d(inputs, 64, [3, 3], scope='conv1') nets = slim.max_pool2d(nets, [3, 3], padding='SAME', scope='pool1') # 56*48*64 nets = fire_module(nets, 16, 64, scope='fire2') nets = fire_module(nets, 16, 64, scope='fire3') nets = slim.max_pool2d(nets, [3, 3], padding='SAME', scope='pool1') # 28*24*128 nets = fire_module(nets, 32, 128, scope='fire4') nets = fire_module(nets, 32, 128, scope='fire5') nets = slim.max_pool2d(nets, [3, 3], padding='SAME', scope='pool5') # 14*12*256 nets = fire_module(nets, 48, 192, scope='fire6') nets = fire_module(nets, 48, 192, scope='fire7') nets = slim.max_pool2d(nets, [3, 3], padding='SAME', scope='pool6') # 7*6*384 nets = fire_module(nets, 64, 256, scope='fire8') nets = fire_module(nets, 64, 256, scope='fire9') # 7*6*512 nets = slim.dropout(nets, keep_prob, is_training=is_training, scope='dropout9') nets = slim.avg_pool2d(nets, [7, 6], scope='pool9') # 1*1*512 return nets
def transition_block(inputs, reduction, scope, is_training, keep_prob): """Call H_l composite function with 1x1 kernel and after average pooling """ with tf.variable_scope(scope, 'trans1', [inputs]): # call composite function with 1x1 kernel out_features = int(int(inputs.get_shape()[-1]) * reduction) nets = slim.conv2d(inputs, out_features, [1, 1], scope='conv') nets = slim.dropout(nets, keep_prob=keep_prob, is_training=is_training, scope='dropout') # run average pooling nets = slim.avg_pool2d(nets, [2, 2], scope='pool') return nets
def inference(images, keep_probability, phase_train=True, bottleneck_layer_size=128, weight_decay=0.0, reuse=None): batch_norm_params = { # Decay for the moving averages. 'decay': 0.995, # epsilon to prevent 0s in variance. 'epsilon': 0.001, # force in-place updates of mean and variance estimates 'updates_collections': None, # Moving averages ends up in the trainable variables collection 'variables_collections': [ tf.GraphKeys.TRAINABLE_VARIABLES ], } with slim.arg_scope([slim.conv2d, slim.fully_connected], weights_initializer=slim.xavier_initializer_conv2d(uniform=True), weights_regularizer=slim.l2_regularizer(weight_decay), normalizer_fn=slim.batch_norm, normalizer_params=batch_norm_params): with tf.variable_scope('squeezenet', [images], reuse=reuse): with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=phase_train): net = slim.conv2d(images, 96, [7, 7], stride=2, scope='conv1') net = slim.max_pool2d(net, [3, 3], stride=2, scope='maxpool1') net = fire_module(net, 16, 64, scope='fire2') net = fire_module(net, 16, 64, scope='fire3') net = fire_module(net, 32, 128, scope='fire4') net = slim.max_pool2d(net, [2, 2], stride=2, scope='maxpool4') net = fire_module(net, 32, 128, scope='fire5') net = fire_module(net, 48, 192, scope='fire6') net = fire_module(net, 48, 192, scope='fire7') net = fire_module(net, 64, 256, scope='fire8') net = slim.max_pool2d(net, [3, 3], stride=2, scope='maxpool8') net = fire_module(net, 64, 256, scope='fire9') net = slim.dropout(net, keep_probability) net = slim.conv2d(net, 1000, [1, 1], activation_fn=None, normalizer_fn=None, scope='conv10') net = slim.avg_pool2d(net, net.get_shape()[1:3], scope='avgpool10') net = tf.squeeze(net, [1, 2], name='logits') net = slim.fully_connected(net, bottleneck_layer_size, activation_fn=None, scope='Bottleneck', reuse=False) return net, None
def hidden_layers_starting_at(self, layer, layer_sizes, opts=None): # TODO: opts=None => will force exception on old calls.... if not isinstance(layer_sizes, list): layer_sizes = map(int, layer_sizes.split(",")) assert len(layer_sizes) > 0 for i, size in enumerate(layer_sizes): layer = slim.fully_connected(scope="h%d" % i, inputs=layer, num_outputs=size, weights_regularizer=tf.contrib.layers.l2_regularizer(0.01), activation_fn=tf.nn.relu) if opts.use_dropout: layer = slim.dropout(layer, is_training=IS_TRAINING, scope="do%d" % i) return layer
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 regression_model(inputs, is_training=True, scope="deep_regression"): """Creates the regression model. Args: inputs: A node that yields a `Tensor` of size [batch_size, dimensions]. is_training: Whether or not we're currently training the model. scope: An optional variable_op scope for the model. Returns: predictions: 1-D `Tensor` of shape [batch_size] of responses. end_points: A dict of end points representing the hidden layers. """ with tf.variable_scope(scope, 'deep_regression', [inputs]): end_points = {} # Set the default weight _regularizer and acvitation for each fully_connected layer. with slim.arg_scope([slim.fully_connected], activation_fn=tf.nn.relu, weights_regularizer=slim.l2_regularizer(0.01)): # Creates a fully connected layer from the inputs with 32 hidden units. net = slim.fully_connected(inputs, 32, scope='fc1') end_points['fc1'] = net # Adds a dropout layer to prevent over-fitting. net = slim.dropout(net, 0.8, is_training=is_training) # Adds another fully connected layer with 16 hidden units. net = slim.fully_connected(net, 16, scope='fc2') end_points['fc2'] = net # Creates a fully-connected layer with a single hidden unit. Note that the # layer is made linear by setting activation_fn=None. predictions = slim.fully_connected(net, 1, activation_fn=None, scope='prediction') end_points['out'] = predictions return predictions, end_points
def __dropout(self,net): net_shape = net.get_shape().as_list() noise_shape = [net_shape[0],1,1,net_shape[-1]] return slim.dropout(net, noise_shape=noise_shape)
def get_model(self,inputs, weight_decay=0.0005,is_training=False): # End_points collect relevant activations for external use. arg_scope = self.__arg_scope(weight_decay=weight_decay) with slim.arg_scope(arg_scope): end_points = {} with tf.variable_scope('vgg_16', [inputs]): # Original VGG-16 blocks. net = slim.repeat(inputs, 2, slim.conv2d, 64, [3, 3], scope='conv1') end_points['block1'] = net net = slim.max_pool2d(net, [2, 2], scope='pool1') # Block 2. net = slim.repeat(net, 2, slim.conv2d, 128, [3, 3], scope='conv2') end_points['block2'] = net net = slim.max_pool2d(net, [2, 2], scope='pool2') # Block 3. net = slim.repeat(net, 3, slim.conv2d, 256, [3, 3], scope='conv3') end_points['block3'] = net net = slim.max_pool2d(net, [2, 2], scope='pool3') # Block 4. net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv4') end_points['block4'] = net net = slim.max_pool2d(net, [2, 2], scope='pool4') # Block 5. net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv5') end_points['block5'] = net net = slim.max_pool2d(net, [3, 3], stride=1, scope='pool5') # Additional SSD blocks. keep_prob=0.8 with slim.arg_scope([slim.conv2d], activation_fn=None): with slim.arg_scope([slim.batch_norm], activation_fn=tf.nn.relu, is_training=is_training,updates_collections=None): with slim.arg_scope([slim.dropout], is_training=is_training,keep_prob=keep_prob): with tf.variable_scope(self.model_name): return self.__additional_ssd_block(end_points, net)
def hidden_layers_on(self, layer, layer_sizes): if not isinstance(layer_sizes, list): layer_sizes = map(int, layer_sizes.split(",")) assert len(layer_sizes) > 0 for i, size in enumerate(layer_sizes): layer = slim.fully_connected(scope="h%d" % i, inputs=layer, num_outputs=size, weights_regularizer=tf.contrib.layers.l2_regularizer(0.01), activation_fn=tf.nn.relu) # if opts.use_dropout: # layer = slim.dropout(layer, is_training=IS_TRAINING, scope="do%d" % i) return layer
def create_inner_block( incoming, scope, nonlinearity=tf.nn.elu, weights_initializer=tf.truncated_normal_initializer(1e-3), bias_initializer=tf.zeros_initializer(), regularizer=None, increase_dim=False, summarize_activations=True): n = incoming.get_shape().as_list()[-1] stride = 1 if increase_dim: n *= 2 stride = 2 incoming = slim.conv2d( incoming, n, [3, 3], stride, activation_fn=nonlinearity, padding="SAME", normalizer_fn=_batch_norm_fn, weights_initializer=weights_initializer, biases_initializer=bias_initializer, weights_regularizer=regularizer, scope=scope + "/1") if summarize_activations: tf.summary.histogram(incoming.name + "/activations", incoming) incoming = slim.dropout(incoming, keep_prob=0.6) incoming = slim.conv2d( incoming, n, [3, 3], 1, activation_fn=None, padding="SAME", normalizer_fn=None, weights_initializer=weights_initializer, biases_initializer=bias_initializer, weights_regularizer=regularizer, scope=scope + "/2") return incoming
def _network_factory(num_classes, is_training, weight_decay=1e-8): def factory_fn(image, reuse, l2_normalize): with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=is_training): with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.batch_norm, slim.layer_norm], reuse=reuse): features, logits = _create_network( image, num_classes, l2_normalize=l2_normalize, reuse=reuse, create_summaries=is_training, weight_decay=weight_decay) return features, logits return factory_fn
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): # Collect outputs for conv2d and max_pool2d. with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d], outputs_collections=[end_points_collection]): net = slim.conv2d(inputs, 64, [11, 11], 4, padding='VALID', scope='conv1') net = slim.max_pool2d(net, [3, 3], 2, scope='pool1') net = slim.conv2d(net, 192, [5, 5], scope='conv2') net = slim.max_pool2d(net, [3, 3], 2, scope='pool2') net = slim.conv2d(net, 384, [3, 3], scope='conv3') net = slim.conv2d(net, 384, [3, 3], scope='conv4') net = slim.conv2d(net, 256, [3, 3], scope='conv5') net = slim.max_pool2d(net, [3, 3], 2, scope='pool5') with slim.arg_scope([slim.conv2d], weights_initializer=trunc_normal(0.005), biases_initializer=tf.constant_initializer(0.1), outputs_collections=[end_points_collection]): net = slim.conv2d(net, 4096, [5, 5], padding='VALID', scope='fc6') net = slim.dropout(net, dropout_keep_prob, is_training=is_training, scope='dropout6') net = slim.conv2d(net, 4096, [1, 1], scope='fc7') net = slim.dropout(net, dropout_keep_prob, is_training=is_training, scope='dropout7') return net, end_points_collection
def create_base(self, inputs, is_training): """Creates a base part of the Model (no gradients, losses or summaries).""" with tf.name_scope('Model'): with slim.arg_scope([slim.fully_connected], activation_fn=tf.nn.relu, # weights_regularizer=slim.l2_regularizer(0.01), # weights_initializer=initializers.xavier_initializer(seed=self._config.random_seed), # biases_initializer=tf.constant_initializer(0.1) ): # first fully connected layer net = slim.fully_connected(inputs, self._config.mlp_params['hidden_sizes'][0], scope='fc1') # dropout1 net = slim.dropout(net, self._config.keep_prob, is_training=is_training, scope='dropout1') # second fully connected layer net = slim.fully_connected(net, self._config.mlp_params['hidden_sizes'][1], scope='fc2') # dropout2 net = slim.dropout(net, self._config.keep_prob, is_training=is_training, scope='dropout2') # final fully-connected dense layer logits = slim.fully_connected(net, self._config.num_classes, activation_fn=None, scope='fc3') with tf.name_scope('output'): predicted_classes = tf.to_int32(tf.argmax(logits, dimension=1), name='y') return logits, predicted_classes
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 create_model(self, model_input, vocab_size, keep_prob, num_mixtures=None, l2_penalty=1e-8, **unused_params): num_mixtures = num_mixtures or FLAGS.moe_num_mixtures new_model_input = slim.dropout(model_input,keep_prob=keep_prob) #new_model_input = model_input gate_activations = slim.fully_connected( new_model_input, vocab_size * (num_mixtures + 1), activation_fn=None, biases_initializer=None, weights_regularizer=slim.l2_regularizer(l2_penalty), scope="gates") expert_activations = slim.fully_connected( new_model_input, vocab_size * num_mixtures, activation_fn=None, weights_regularizer=slim.l2_regularizer(l2_penalty), scope="experts") gating_distribution = tf.nn.softmax(tf.reshape( gate_activations, [-1, num_mixtures + 1])) # (Batch * #Labels) x (num_mixtures + 1) expert_distribution = tf.nn.sigmoid(tf.reshape( expert_activations, [-1, num_mixtures])) # (Batch * #Labels) x num_mixtures final_probabilities_by_class_and_batch = tf.reduce_sum( gating_distribution[:, :num_mixtures] * expert_distribution, 1) final_probabilities = tf.reshape(final_probabilities_by_class_and_batch, [-1, vocab_size]) return {"predictions": final_probabilities, "features": model_input}
def create_model(self, model_input, vocab_size, keep_prob, num_mixtures=None, l2_penalty=1e-8, **unused_params): num_mixtures = num_mixtures or FLAGS.moe_num_mixtures new_model_input = slim.dropout(model_input,keep_prob=keep_prob) #new_model_input = model_input gate_activations = slim.fully_connected( new_model_input, vocab_size * (num_mixtures + 1), activation_fn=None, biases_initializer=None, weights_regularizer=slim.l2_regularizer(l2_penalty), scope="gates") expert_activations = slim.fully_connected( new_model_input, vocab_size * num_mixtures, activation_fn=None, weights_regularizer=slim.l2_regularizer(l2_penalty), scope="experts") gating_distribution = tf.nn.softmax(tf.reshape( gate_activations, [-1, num_mixtures + 1])) # (Batch * #Labels) x (num_mixtures + 1) expert_distribution = tf.nn.sigmoid(tf.reshape( expert_activations, [-1, num_mixtures])) # (Batch * #Labels) x num_mixtures final_probabilities_by_class_and_batch = tf.reduce_sum( gating_distribution[:, :num_mixtures] * expert_distribution, 1) final_probabilities = tf.reshape(final_probabilities_by_class_and_batch, [-1, vocab_size]) return {"predictions": final_probabilities, "zhaofeatures": model_input}
def compute(self, inputs): """Compute a batch of outputs of the neural network from a batch of inputs. Args: inputs: a tensorflow tensor, a batch of input images. Each image is of size InputReaderCifar10.IMAGE_SIZE x InputReaderCifar10.IMAGE_SIZE x InputReaderCifar10.NUM_CHANNELS. Returns: net: a tensorflow op, output of the network. embedding: a tensorflow op, output of the embedding layer (the second last fully connected layer). """ hparams = self._hparams net = None num_pool_conv_layers = len(hparams.nums_conv_filters) for i in xrange(num_pool_conv_layers): net = slim.conv2d(inputs if i == 0 else net, hparams.nums_conv_filters[i], [hparams.conv_filter_sizes[i], hparams.conv_filter_sizes[i]], padding="SAME", biases_initializer=tf.constant_initializer(0.1 * i), scope="conv_{0}".format(i)) net = slim.max_pool2d(net, [hparams.pooling_size, hparams.pooling_size], hparams.pooling_stride, scope="pool_{0}".format(i)) net = slim.flatten(net, scope="flatten") net = slim.fully_connected(net, 384, biases_initializer=tf.constant_initializer(0.1), scope="fc_{0}".format(num_pool_conv_layers)) net = slim.dropout(net, hparams.dropout_prob, scope="dropout_{0}".format(num_pool_conv_layers)) embedding = slim.fully_connected(net, 192, biases_initializer=tf.constant_initializer(0.1), scope="fc_{0}".format(num_pool_conv_layers + 1)) net = slim.fully_connected(embedding, InputReaderCifar10.NUM_CLASSES, activation_fn=None, biases_initializer=tf.constant_initializer(0.0), scope="fc_{0}".format(num_pool_conv_layers + 2)) return net, embedding
def inference(images, num_classes, is_traiing=True, scope='inception_v3'): """Build Inception v3 model architecture. See here for reference: http://arxiv.org/abs/1512.00567 Args: images: Images returned from inputs() or distorted_inputs(). num_classes: number of classes for_training: If set to `True`, build the inference model for training. Kernels that operate differently for inference during training e.g. dropout, are appropriately configured. restore_logits: whether or not the logits layers should be restored. Useful for fine-tuning a model with different num_classes. scope: optional prefix string identifying the ImageNet tower. Returns: Logits. 2-D float Tensor. Auxiliary Logits. 2-D float Tensor of side-head. Used for training only. """ # Parameters for BatchNorm. batch_norm_params = { # Decay for the moving averages. 'decay': BATCHNORM_MOVING_AVERAGE_DECAY, # epsilon to prevent 0s in variance. 'epsilon': 0.001, # calculate moving average or using exist one 'is_training': is_traiing } # Set weight_decay for weights in Conv and FC layers. with slim.arg_scope([slim.conv2d, slim.fully_connected], weights_regularizer=slim.l2_regularizer(FLAGS.weight_decay)): with slim.arg_scope([slim.conv2d], weights_initializer=slim.variance_scaling_initializer(), activation_fn=tf.nn.relu, normalizer_fn=slim.batch_norm, normalizer_params=batch_norm_params): logits, endpoints = inception_v3( images, num_classes=num_classes, dropout_keep_prob=0.8, is_training=is_traiing, scope=scope ) # Add summaries for viewing model statistics on TensorBoard. _activation_summaries(endpoints) # Grab the logits associated with the side head. Employed during training. auxiliary_logits = endpoints['aux_logits'] return logits, auxiliary_logits
def inference(images, num_classes, is_training=True, scope='squeeze'): """ Args: images: Images returned from inputs() or distorted_inputs(). num_classes: number of classes for_training: If set to `True`, build the inference model for training. Kernels that operate differently for inference during training e.g. dropout, are appropriately configured. restore_logits: whether or not the logits layers should be restored. Useful for fine-tuning a model with different num_classes. scope: optional prefix string identifying the ImageNet tower. Returns: Logits. 2-D float Tensor. Auxiliary Logits. 2-D float Tensor of side-head. Used for training only. """ # Parameters for BatchNorm. batch_norm_params = { # Decay for the moving averages. 'decay': BATCHNORM_MOVING_AVERAGE_DECAY, # epsilon to prevent 0s in variance. 'epsilon': 0.001, # calculate moving average or using exist one 'is_training': is_training } # Set weight_decay for weights in Conv and FC layers. with slim.arg_scope([slim.conv2d, slim.fully_connected], weights_regularizer=slim.l2_regularizer(FLAGS.weight_decay)): with slim.arg_scope([slim.conv2d], weights_initializer=slim.variance_scaling_initializer(), activation_fn=tf.nn.relu, normalizer_fn=slim.batch_norm, normalizer_params=batch_norm_params): logits, endpoints = squeezenet( images, num_classes=num_classes, keep_prob=0.5, is_training=is_training, scope=scope ) # Add summaries for viewing model statistics on TensorBoard. _activation_summaries(endpoints) return logits
def vgg_16(inputs, num_classes=1000, is_training=True, dropout_keep_prob=0.5, spatial_squeeze=True, scope='vgg_16'): """Oxford Net VGG 16-Layers version D Example. Note: All the fully_connected layers have been transformed to conv2d layers. To use in classification mode, resize input to 224x224. Args: inputs: a tensor of size [batch_size, height, width, channels]. num_classes: number of predicted classes. is_training: whether or not the model is being trained. dropout_keep_prob: the probability that activations are kept in the dropout layers during training. spatial_squeeze: whether or not should squeeze the spatial dimensions of the outputs. Useful to remove unnecessary dimensions for classification. scope: Optional scope for the variables. Returns: the last op containing the log predictions and end_points dict. """ with tf.name_scope(scope, 'vgg_16', [inputs]) as sc: end_points_collection = sc + '_end_points' # Collect outputs for conv2d, fully_connected and max_pool2d. with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d], outputs_collections=end_points_collection): net = slim.repeat(inputs, 2, slim.conv2d, 64, [3, 3], scope='conv1') net = slim.max_pool2d(net, [2, 2], scope='pool1') net = slim.repeat(net, 2, slim.conv2d, 128, [3, 3], scope='conv2') net = slim.max_pool2d(net, [2, 2], scope='pool2') net = slim.repeat(net, 3, slim.conv2d, 256, [3, 3], scope='conv3') net = slim.max_pool2d(net, [2, 2], scope='pool3') net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv4') net = slim.max_pool2d(net, [2, 2], scope='pool4') net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv5') net = slim.max_pool2d(net, [2, 2], scope='pool5') # Use conv2d instead of fully_connected layers. net = slim.conv2d(net, 4096, [3, 3], padding='VALID', scope='fc6') net = slim.dropout(net, dropout_keep_prob, is_training=is_training, scope='dropout6') net = slim.conv2d(net, 4096, [1, 1], scope='fc7') net = slim.dropout(net, dropout_keep_prob, is_training=is_training, scope='dropout7') net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, normalizer_fn=None, scope='fc8') # Convert end_points_collection into a end_point dict. end_points = slim.utils.convert_collection_to_dict(end_points_collection) if spatial_squeeze: net = tf.squeeze(net, [1, 2], name='fc8/squeezed') end_points[sc + '/fc8'] = net return net, end_points
def inference(images, num_classes, is_training=True, scope='vgg_16'): """Build Inception v3 model architecture. See here for reference: http://arxiv.org/abs/1512.00567 Args: images: Images returned from inputs() or distorted_inputs(). num_classes: number of classes for_training: If set to `True`, build the inference model for training. Kernels that operate differently for inference during training e.g. dropout, are appropriately configured. restore_logits: whether or not the logits layers should be restored. Useful for fine-tuning a model with different num_classes. scope: optional prefix string identifying the ImageNet tower. Returns: Logits. 2-D float Tensor. Auxiliary Logits. 2-D float Tensor of side-head. Used for training only. """ # Parameters for BatchNorm. batch_norm_params = { # Decay for the moving averages. 'decay': BATCHNORM_MOVING_AVERAGE_DECAY, # epsilon to prevent 0s in variance. 'epsilon': 0.001, # calculate moving average or using exist one 'is_training': is_training } # Set weight_decay for weights in Conv and FC layers. with slim.arg_scope([slim.conv2d, slim.fully_connected], weights_regularizer=slim.l2_regularizer(FLAGS.weight_decay)): with slim.arg_scope([slim.conv2d], weights_initializer=slim.variance_scaling_initializer(), activation_fn=tf.nn.relu, normalizer_fn=slim.batch_norm, normalizer_params=batch_norm_params): logits, endpoints = vgg_16( images, num_classes=num_classes, dropout_keep_prob=0.8, is_training=is_training, scope=scope ) # Add summaries for viewing model statistics on TensorBoard. _activation_summaries(endpoints) return logits
def VGG16(inputs, outputs, loss_weight, labels): """ Spatial stream based on VGG16 """ with slim.arg_scope([slim.conv2d, slim.fully_connected], activation_fn=tf.nn.relu, weights_initializer=tf.truncated_normal_initializer(0.0, 0.01), weights_regularizer=slim.l2_regularizer(0.0005)): # conv1_1 = slim.conv2d(tf.concat(3, [inputs, outputs]), 64, [3, 3], scope='conv1_1') conv1_1 = slim.conv2d(inputs, 64, [3, 3], scope='conv1_1') conv1_2 = slim.conv2d(conv1_1, 64, [3, 3], scope='conv1_2') pool1 = slim.max_pool2d(conv1_2, [2, 2], scope='pool1') conv2_1 = slim.conv2d(pool1, 128, [3, 3], scope='conv2_1') conv2_2 = slim.conv2d(conv2_1, 128, [3, 3], scope='conv2_2') pool2 = slim.max_pool2d(conv2_2, [2, 2], scope='pool2') conv3_1 = slim.conv2d(pool2, 256, [3, 3], scope='conv3_1') conv3_2 = slim.conv2d(conv3_1, 256, [3, 3], scope='conv3_2') conv3_3 = slim.conv2d(conv3_2, 256, [3, 3], scope='conv3_3') pool3 = slim.max_pool2d(conv3_3, [2, 2], scope='pool3') conv4_1 = slim.conv2d(pool3, 512, [3, 3], scope='conv4_1') conv4_2 = slim.conv2d(conv4_1, 512, [3, 3], scope='conv4_2') conv4_3 = slim.conv2d(conv4_2, 512, [3, 3], scope='conv4_3') pool4 = slim.max_pool2d(conv4_3, [2, 2], scope='pool4') conv5_1 = slim.conv2d(pool4, 512, [3, 3], scope='conv5_1') conv5_2 = slim.conv2d(conv5_1, 512, [3, 3], scope='conv5_2') conv5_3 = slim.conv2d(conv5_2, 512, [3, 3], scope='conv5_3') pool5 = slim.max_pool2d(conv5_3, [2, 2], scope='pool5') flatten5 = slim.flatten(pool5, scope='flatten5') fc6 = slim.fully_connected(flatten5, 4096, scope='fc6') dropout6 = slim.dropout(fc6, 0.9, scope='dropout6') fc7 = slim.fully_connected(dropout6, 4096, scope='fc7') dropout7 = slim.dropout(fc7, 0.9, scope='dropout7') fc8 = slim.fully_connected(dropout7, 101, activation_fn=None, scope='fc8') prob = tf.nn.softmax(fc8) predictions = tf.argmax(prob, 1) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(fc8, labels) actionLoss = tf.reduce_mean(cross_entropy) zeroCon = tf.constant(0) losses = [zeroCon, zeroCon, zeroCon, zeroCon, zeroCon, zeroCon, actionLoss] flows_all = [zeroCon, zeroCon, zeroCon, zeroCon, zeroCon, zeroCon, prob] slim.losses.add_loss(actionLoss) return losses, flows_all, predictions
def conv_net_on(self, input_layer, opts): # TODO: reinclude batch_norm config, hasn't been helping at all... # convert input_layer from uint8 (0, 255) to float32 (0.0, 1.0) input_layer = tf.to_float(input_layer) / 255 # whiten image, per channel, using batch_normalisation layer with # params calculated directly from batch. axis = list(range(input_layer.get_shape().ndims - 1)) batch_mean, batch_var = tf.nn.moments(input_layer, axis) # calcs moments per channel whitened_input_layer = tf.nn.batch_normalization(input_layer, batch_mean, batch_var, scale=None, offset=None, variance_epsilon=1e-6) model = slim.conv2d(whitened_input_layer, num_outputs=8, kernel_size=[5, 5], scope='conv1a') # model = slim.conv2d(whitened_input_layer, num_outputs=8, kernel_size=[5, 5], scope='conv1b') model = slim.max_pool2d(model, kernel_size=[2, 2], scope='pool1') self.pool1 = model print >>sys.stderr, "pool1", util.shape_and_product_of(model) model = slim.conv2d(model, num_outputs=16, kernel_size=[5, 5], scope='conv2a') # model = slim.conv2d(model, num_outputs=16, kernel_size=[5, 5], scope='conv2b') model = slim.max_pool2d(model, kernel_size=[2, 2], scope='pool2') self.pool2 = model print >>sys.stderr, "pool2", util.shape_and_product_of(model) model = slim.conv2d(model, num_outputs=32, kernel_size=[3, 3], scope='conv3a') # model = slim.conv2d(model, num_outputs=32, kernel_size=[3, 3], scope='conv3b') model = slim.max_pool2d(model, kernel_size=[2, 2], scope='pool3') self.pool3 = model print >>sys.stderr, "pool3", util.shape_and_product_of(model) # a final unpooled conv net just to drop params down. maybe pool here too actually? # model = slim.conv2d(model, num_outputs=32, kernel_size=[3, 3], scope='conv4a') # model = slim.conv2d(model, num_outputs=32, kernel_size=[3, 3], scope='conv3b') # model = slim.max_pool2d(model, kernel_size=[2, 2], scope='pool4') # self.pool3 = model # print >>sys.stderr, "pool4", util.shape_and_product_of(model) # do simple maxout on output to reduce dimensionality down for the upcoming # fully connected layers. see https://arxiv.org/abs/1302.4389 # model = tf.reshape(model, (-1, 15, 20, 8, 4)) # (?, 15, 20, 32) -> (?, 15, 20, 8, 4) # model = tf.reduce_max(model, reduction_indices=4) # (?, 15, 20, 8) # print >>sys.stderr, "maxout", util.shape_and_product_of(model) model = slim.flatten(model, scope='flat') if opts.use_dropout: model = slim.dropout(model, is_training=IS_TRAINING, scope="drop" % i) return model
def build_nin_model(self): # input features self.x = tf.placeholder(tf.float32, shape=[None, self.input_height, self.input_width, self.input_channels], name='input_layer') self.y = tf.placeholder(tf.float32, [None, self.num_classes], name='output_layer') # learning_rate placeholder self.learning_rate = tf.placeholder(tf.float32, name='learning_rate') # dropout layer: keep probability, vgg default value:0.5 self.keep_prob = tf.placeholder(tf.float32, name='keep_prob') print('x:' + str(self.x.get_shape().as_list())) self.nin_lay_1 = self.mlp_conv(self.x, kernel_size=11, stride=2, num_filters=96, micro_layer_size=[96, 96], name='nin_lay_1') # add dropout dropout = slim.dropout(self.nin_lay_1, keep_prob=self.keep_prob) self.maxpooling_1 = slim.max_pool2d(dropout, kernel_size=3, stride=2, padding='SAME') print('maxpooling_1:' + str(self.maxpooling_1.get_shape().as_list())) self.nin_lay_2 = self.mlp_conv(self.maxpooling_1, kernel_size=5, stride=1, num_filters=256, micro_layer_size=[256, 256], name='nin_lay_2') # add dropout dropout = slim.dropout(self.nin_lay_2, keep_prob=self.keep_prob) self.maxpooling_2 = slim.max_pool2d(dropout, kernel_size=3, stride=2, padding='SAME') print('maxpooling_2:' + str(self.maxpooling_2.get_shape().as_list())) self.nin_lay_3 = self.mlp_conv(self.maxpooling_2, kernel_size=3, stride=1, num_filters=384, micro_layer_size=[384, 384], name='nin_lay_3') # NO dropout self.maxpooling_3 = slim.max_pool2d(self.nin_lay_3, kernel_size=3, stride=2, padding='SAME') print('maxpooling_3:' + str(self.maxpooling_3.get_shape().as_list())) self.nin_lay_4 = self.mlp_conv(self.maxpooling_3, kernel_size=3, stride=1, num_filters=1024, micro_layer_size=[1024, self.num_classes], name='nin_lay_4') self.maxpooling_4 = slim.max_pool2d(self.nin_lay_4, kernel_size=3, stride=2, padding='SAME') print('maxpooling_4:' + str(self.maxpooling_4.get_shape().as_list())) # golbal average pooling self.digits = self.golbal_average_pooling(self.nin_lay_4) self.digits = self.digits[:, 0, 0, :] print('golbal_average_pooling:' + str(self.digits.get_shape().as_list())) # softmax self.read_out_logits = tf.nn.softmax(self.digits) print('read_out_logits:' + str(self.read_out_logits.get_shape().as_list()))
def build(self, is_train=True): n = self.a_dim conv_info = self.conv_info # build loss and accuracy {{{ def build_loss(logits, labels): # Cross-entropy loss loss = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels) # Classification accuracy correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) return tf.reduce_mean(loss), accuracy # }}} # Classifier: takes images as input and outputs class label [B, m] def C(img, q, scope='Classifier'): with tf.variable_scope(scope) as scope: log.warn(scope.name) conv_1 = conv2d(img, conv_info[0], is_train, s_h=3, s_w=3, name='conv_1') conv_2 = conv2d(conv_1, conv_info[1], is_train, s_h=3, s_w=3, name='conv_2') conv_3 = conv2d(conv_2, conv_info[2], is_train, name='conv_3') conv_4 = conv2d(conv_3, conv_info[3], is_train, name='conv_4') conv_q = tf.concat([tf.reshape(conv_4, [self.batch_size, -1]), q], axis=1) fc_1 = fc(conv_q, 256, name='fc_1') fc_2 = fc(fc_1, 256, name='fc_2') fc_2 = slim.dropout(fc_2, keep_prob=0.5, is_training=is_train, scope='fc_3/') fc_3 = fc(fc_2, n, activation_fn=None, name='fc_3') return fc_3 logits = C(self.img, self.q, scope='Classifier') self.all_preds = tf.nn.softmax(logits) self.loss, self.accuracy = build_loss(logits, self.a) # Add summaries def draw_iqa(img, q, target_a, pred_a): fig, ax = tfplot.subplots(figsize=(6, 6)) ax.imshow(img) ax.set_title(question2str(q)) ax.set_xlabel(answer2str(target_a)+answer2str(pred_a, 'Predicted')) return fig try: tfplot.summary.plot_many('IQA/', draw_iqa, [self.img, self.q, self.a, self.all_preds], max_outputs=3, collections=["plot_summaries"]) except: pass tf.summary.scalar("loss/accuracy", self.accuracy) tf.summary.scalar("loss/cross_entropy", self.loss) log.warn('Successfully loaded the model.')
def misconception_model(input, window_size, depths, strides, objective_functions, is_training, sub_count=128, sub_layers=2, keep_prob=0.5): """ A misconception tower. Args: input: a tensor of size [batch_size, 1, width, depth]. window_size: the width of the conv and pooling filters to apply. depth: the depth of the output tensor. levels: the height of the tower in misconception layers. objective_functions: a list of objective functions to add to the top of the network. is_training: whether the network is training. Returns: a tensor of size [batch_size, num_classes]. """ layers = [] with slim.arg_scope([slim.batch_norm], decay=0.999): with slim.arg_scope([slim.fully_connected], activation_fn=tf.nn.relu): net = input layers.append(net) for depth, stride in zip(depths, strides): net = misconception_with_bypass(net, window_size, stride, depth, is_training) layers.append(net) outputs = [] for ofunc in objective_functions: onet = net for _ in range(sub_layers - 1): onet = slim.conv2d( onet, sub_count, [1, 1], activation_fn=tf.nn.relu, normalizer_fn=slim.batch_norm, normalizer_params={'is_training': is_training}) # Don't use batch norm on last layer, just use dropout. onet = slim.conv2d(onet, sub_count, [1, 1], normalizer_fn=None) # Global average pool n = int(onet.get_shape().dims[1]) onet = slim.avg_pool2d(onet, [1, n], stride=[1, n]) onet = slim.flatten(onet) # onet = slim.dropout(onet, keep_prob, is_training=is_training) outputs.append(ofunc.build(onet)) return outputs, layers
def misconception_fishing(input, window_size, depths, strides, objective_function, is_training, pre_count=128, post_count=128, post_layers=1, keep_prob=0.5, internal_keep_prob=0.5, other_objectives=()): _, layers = misconception_model( input, window_size, depths, strides, other_objectives, is_training, sub_count=post_count, sub_layers=2) expanded_layers = [] for i, lyr in enumerate(layers): lyr = slim.conv2d( lyr, pre_count, [1, 1], activation_fn=tf.nn.relu, normalizer_fn=slim.batch_norm, normalizer_params={'is_training': is_training}) expanded_layers.append(utility.repeat_tensor(lyr, 2**i)) embedding = tf.add_n(expanded_layers) for _ in range(post_layers - 1): embedding = slim.conv2d( embedding, post_count, [1, 1], activation_fn=tf.nn.relu, normalizer_fn=slim.batch_norm, normalizer_params={'is_training': is_training}) embedding = slim.conv2d( embedding, post_count, [1, 1], activation_fn=tf.nn.relu, normalizer_fn=None) embedding = slim.dropout(embedding, keep_prob, is_training=is_training) fishing_outputs = tf.squeeze( slim.conv2d( embedding, 1, [1, 1], activation_fn=None, normalizer_fn=None), squeeze_dims=[1, 3]) return objective_function.build(fishing_outputs)
def misconception_fishing_2(input, window_size, depths, strides, objective_function, is_training, pre_count=128, post_count=128, post_layers=1, keep_prob=0.5, internal_keep_prob=0.5, other_objectives=()): dt = tf.exp(input[:, 0, :, 0]) - 1 dt = tf.maximum(dt, 12 * 60 * 60) dt = 0.5 * (dt[:, 1:] + dt[:, :-1]) _, layers = misconception_model( input, window_size, depths, strides, other_objectives, is_training, sub_count=post_count, sub_layers=2) expanded_layers = [] for i, lyr in enumerate(layers): lyr = slim.conv2d( lyr, pre_count, [1, 1], activation_fn=tf.nn.relu, normalizer_fn=slim.batch_norm, normalizer_params={'is_training': is_training}) expanded_layers.append(utility.repeat_tensor(lyr, 2**i)) embedding = tf.add_n(expanded_layers) for _ in range(post_layers - 1): embedding = slim.conv2d( embedding, post_count, [1, 1], activation_fn=tf.nn.relu, normalizer_fn=slim.batch_norm, normalizer_params={'is_training': is_training}) embedding = slim.conv2d( embedding, post_count, [1, 1], activation_fn=tf.nn.relu, normalizer_fn=None) embedding = slim.dropout(embedding, keep_prob, is_training=is_training) fishing_outputs = tf.squeeze( slim.conv2d( embedding, 1, [1, 1], activation_fn=None, normalizer_fn=None), squeeze_dims=[1, 3]) return objective_function.build(fishing_outputs, dt)
def misconception_with_fishing_ranges(self, input, mmsis, is_training): """ A misconception tower with additional fishing range classification. Args: input: a tensor of size [batch_size, 1, width, depth]. window_size: the width of the conv and pooling filters to apply. stride: the downsampling to apply when filtering. depth: the depth of the output tensor. levels: The height of the tower in misconception layers. Returns: a tensor of size [batch_size, num_classes]. """ with slim.arg_scope([slim.fully_connected], activation_fn=tf.nn.elu): net = input # Then a tower for classification. multiscale_layers = [] for i in range(self.levels): with tf.variable_scope("layer_%d" % i): multiscale_layers.append(utility.repeat_tensor(net, 2**i)) net = layers.misconception_with_bypass( net, self.window_size, self.stride, self.feature_depth, is_training) # TODO: We currently don't use the last year for fishing classification # Since we don't use this for vessel classification currently, perhaps # we should rememdy that... net = slim.flatten(net) net = slim.dropout(net, 0.5, is_training=is_training) net = slim.fully_connected(net, 100) net = slim.dropout(net, 0.5, is_training=is_training) concatenated_multiscale_embedding = tf.concat(3, multiscale_layers) fishing_outputs = tf.squeeze( slim.conv2d( concatenated_multiscale_embedding, 1, [1, 1], activation_fn=None), squeeze_dims=[1, 3]) for of in self.classification_training_objectives: of.build(net) self.fishing_localisation_objective.build(fishing_outputs)