Python tensorflow.get_variable() Examples
The following are 30
code examples of tensorflow.get_variable().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow
, or try the search function
.
Example #1
Source File: actor.py From neural-combinatorial-optimization-rl-tensorflow with MIT License | 6 votes |
def build_permutation(self): with tf.variable_scope("encoder"): with tf.variable_scope("embedding"): # Embed input sequence W_embed =tf.get_variable("weights", [1,self.input_dimension+2, self.input_embed], initializer=self.initializer) # +2 for TW feat. here too embedded_input = tf.nn.conv1d(self.input_, W_embed, 1, "VALID", name="embedded_input") # Batch Normalization embedded_input = tf.layers.batch_normalization(embedded_input, axis=2, training=self.is_training, name='layer_norm', reuse=None) with tf.variable_scope("dynamic_rnn"): # Encode input sequence cell1 = LSTMCell(self.num_neurons, initializer=self.initializer) # BNLSTMCell(self.num_neurons, self.training) or cell1 = DropoutWrapper(cell1, output_keep_prob=0.9) # Return the output activations [Batch size, Sequence Length, Num_neurons] and last hidden state as tensors. encoder_output, encoder_state = tf.nn.dynamic_rnn(cell1, embedded_input, dtype=tf.float32) with tf.variable_scope('decoder'): # Ptr-net returns permutations (self.positions), with their log-probability for backprop self.ptr = Pointer_decoder(encoder_output, self.config) self.positions, self.log_softmax, self.attending, self.pointing = self.ptr.loop_decode(encoder_state) variable_summaries('log_softmax',self.log_softmax, with_max_min = True)
Example #2
Source File: model.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def set_input_shape(self, input_shape): batch_size, rows, cols, input_channels = input_shape kernel_shape = tuple(self.kernel_shape) + (input_channels, self.output_channels) assert len(kernel_shape) == 4 assert all(isinstance(e, int) for e in kernel_shape), kernel_shape with tf.variable_scope(self.name): init = tf.truncated_normal(kernel_shape, stddev=0.1) self.kernels = self.get_variable(self.w_name, init) self.b = self.get_variable( 'b', .1 + np.zeros((self.output_channels,)).astype('float32')) input_shape = list(input_shape) self.input_shape = input_shape input_shape[0] = 1 dummy_batch = tf.zeros(input_shape) dummy_output = self.fprop(dummy_batch) output_shape = [int(e) for e in dummy_output.get_shape()] output_shape[0] = 1 self.output_shape = tuple(output_shape)
Example #3
Source File: test_runner.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def setUp(self): super(TestRunnerMultiGPU, self).setUp() self.sess = tf.Session() inputs = [] outputs = [] self.niter = 10 niter = self.niter # A Simple graph with `niter` sub-graphs. with tf.variable_scope(None, 'runner'): for i in range(niter): v = tf.get_variable('v%d' % i, shape=(100, 10)) w = tf.get_variable('w%d' % i, shape=(100, 1)) inputs += [{'v': v, 'w': w}] outputs += [{'v': v, 'w': w}] self.runner = RunnerMultiGPU(inputs, outputs, sess=self.sess)
Example #4
Source File: real_nvp_utils.py From DOTA_models with Apache License 2.0 | 6 votes |
def variable_on_cpu(name, shape, initializer, trainable=True): """Helper to create a Variable stored on CPU memory. Args: name: name of the variable shape: list of ints initializer: initializer for Variable trainable: boolean defining if the variable is for training Returns: Variable Tensor """ var = tf.get_variable( name, shape, initializer=initializer, trainable=trainable) return var # layers
Example #5
Source File: wrapped_units.py From DOTA_models with Apache License 2.0 | 6 votes |
def _apply_with_captured_variables(self, function): """Applies a function using previously-captured variables. Args: function: Function to apply using captured variables. The function should take one argument, its enclosing variable scope. Returns: Results of function application. """ def _custom_getter(getter, *args, **kwargs): """Retrieves the normal or moving-average variables.""" return self._component.get_variable(var_params=getter(*args, **kwargs)) with tf.variable_scope( 'cell', reuse=True, custom_getter=_custom_getter) as scope: return function(scope)
Example #6
Source File: gru_cell.py From DOTA_models with Apache License 2.0 | 6 votes |
def __call__(self, inputs, state, scope=None): """GRU cell with layer normalization.""" input_dim = inputs.get_shape().as_list()[1] num_units = self._num_units with tf.variable_scope(scope or "gru_cell"): with tf.variable_scope("gates"): w_h = tf.get_variable( "w_h", [num_units, 2 * num_units], initializer=self._w_h_initializer()) w_x = tf.get_variable( "w_x", [input_dim, 2 * num_units], initializer=self._w_x_initializer(input_dim)) z_and_r = (_layer_norm(tf.matmul(state, w_h), scope="layer_norm/w_h") + _layer_norm(tf.matmul(inputs, w_x), scope="layer_norm/w_x")) z, r = tf.split(tf.sigmoid(z_and_r), 2, 1) with tf.variable_scope("candidate"): w = tf.get_variable( "w", [input_dim, num_units], initializer=self._w_initializer) u = tf.get_variable( "u", [num_units, num_units], initializer=self._u_initializer) h_hat = (r * _layer_norm(tf.matmul(state, u), scope="layer_norm/u") + _layer_norm(tf.matmul(inputs, w), scope="layer_norm/w")) new_h = (1 - z) * state + z * self._activation(h_hat) return new_h, new_h
Example #7
Source File: baseop.py From Traffic_sign_detection_YOLO with MIT License | 6 votes |
def wrap_variable(self, var): """wrap layer.w into variables""" val = self.lay.w.get(var, None) if val is None: shape = self.lay.wshape[var] args = [0., 1e-2, shape] if 'moving_mean' in var: val = np.zeros(shape) elif 'moving_variance' in var: val = np.ones(shape) else: val = np.random.normal(*args) self.lay.w[var] = val.astype(np.float32) self.act = 'Init ' if not self.var: return val = self.lay.w[var] self.lay.w[var] = tf.constant_initializer(val) if var in self._SLIM: return with tf.variable_scope(self.scope): self.lay.w[var] = tf.get_variable(var, shape = self.lay.wshape[var], dtype = tf.float32, initializer = self.lay.w[var])
Example #8
Source File: 11_w2v_visual.py From deep-learning-note with MIT License | 6 votes |
def _create_loss(self): """ Step 4: define the loss function """ with tf.name_scope('loss'): # construct variables for NCE loss nce_weight = tf.get_variable('nce_weight', shape=[self.vocab_size, self.embed_size], initializer=tf.truncated_normal_initializer( stddev=1.0 / (self.embed_size ** 0.5))) nce_bias = tf.get_variable('nce_bias', initializer=tf.zeros([VOCAB_SIZE])) # define loss function to be NCE loss function self.loss = tf.reduce_mean(tf.nn.nce_loss(weights=nce_weight, biases=nce_bias, labels=self.target_words, inputs=self.embed, num_sampled=self.num_sampled, num_classes=self.vocab_size), name='loss')
Example #9
Source File: encoder.py From neural-combinatorial-optimization-rl-tensorflow with MIT License | 6 votes |
def encode(self, inputs): # Tensor blocks holding the input sequences [Batch Size, Sequence Length, Features] #self.input_ = tf.placeholder(tf.float32, [self.batch_size, self.max_length, self.input_dimension], name="input_raw") with tf.variable_scope("embedding"): # Embed input sequence W_embed =tf.get_variable("weights",[1,self.input_dimension, self.input_embed], initializer=self.initializer) self.embedded_input = tf.nn.conv1d(inputs, W_embed, 1, "VALID", name="embedded_input") # Batch Normalization self.enc = tf.layers.batch_normalization(self.embedded_input, axis=2, training=self.is_training, name='layer_norm', reuse=None) with tf.variable_scope("stack"): # Blocks for i in range(self.num_stacks): # num blocks with tf.variable_scope("block_{}".format(i)): ### Multihead Attention self.enc = multihead_attention(self.enc, num_units=self.input_embed, num_heads=self.num_heads, dropout_rate=0.1, is_training=self.is_training) ### Feed Forward self.enc = feedforward(self.enc, num_units=[4*self.input_embed, self.input_embed], is_training=self.is_training) # Return the output activations [Batch size, Sequence Length, Num_neurons] as tensors. self.encoder_output = self.enc ### NOTE: encoder_output is the ref for attention ### return self.encoder_output
Example #10
Source File: neural_gpu.py From DOTA_models with Apache License 2.0 | 6 votes |
def conv_linear(args, kw, kh, nin, nout, rate, do_bias, bias_start, prefix): """Convolutional linear map.""" if not isinstance(args, (list, tuple)): args = [args] with tf.variable_scope(prefix): with tf.device("/cpu:0"): k = tf.get_variable("CvK", [kw, kh, nin, nout]) if len(args) == 1: arg = args[0] else: arg = tf.concat(axis=3, values=args) res = tf.nn.convolution(arg, k, dilation_rate=(rate, 1), padding="SAME") if not do_bias: return res with tf.device("/cpu:0"): bias_term = tf.get_variable( "CvB", [nout], initializer=tf.constant_initializer(bias_start)) bias_term = tf.reshape(bias_term, [1, 1, 1, nout]) return res + bias_term
Example #11
Source File: optimization_test.py From BERT-Classification-Tutorial with Apache License 2.0 | 6 votes |
def test_adam(self): with self.test_session() as sess: w = tf.get_variable( "w", shape=[3], initializer=tf.constant_initializer([0.1, -0.2, -0.1])) x = tf.constant([0.4, 0.2, -0.5]) loss = tf.reduce_mean(tf.square(x - w)) tvars = tf.trainable_variables() grads = tf.gradients(loss, tvars) global_step = tf.train.get_or_create_global_step() optimizer = optimization.AdamWeightDecayOptimizer(learning_rate=0.2) train_op = optimizer.apply_gradients(zip(grads, tvars), global_step) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) sess.run(init_op) for _ in range(100): sess.run(train_op) w_np = sess.run(w) self.assertAllClose(w_np.flat, [0.4, 0.2, -0.5], rtol=1e-2, atol=1e-2)
Example #12
Source File: memory.py From DOTA_models with Apache License 2.0 | 5 votes |
def __init__(self, key_dim, memory_size, vocab_size, choose_k=256, alpha=0.1, correct_in_top=1, age_noise=8.0, var_cache_device='', nn_device=''): self.key_dim = key_dim self.memory_size = memory_size self.vocab_size = vocab_size self.choose_k = min(choose_k, memory_size) self.alpha = alpha self.correct_in_top = correct_in_top self.age_noise = age_noise self.var_cache_device = var_cache_device # Variables are cached here. self.nn_device = nn_device # Device to perform nearest neighbour matmul. caching_device = var_cache_device if var_cache_device else None self.update_memory = tf.constant(True) # Can be fed "false" if needed. self.mem_keys = tf.get_variable( 'memkeys', [self.memory_size, self.key_dim], trainable=False, initializer=tf.random_uniform_initializer(-0.0, 0.0), caching_device=caching_device) self.mem_vals = tf.get_variable( 'memvals', [self.memory_size], dtype=tf.int32, trainable=False, initializer=tf.constant_initializer(0, tf.int32), caching_device=caching_device) self.mem_age = tf.get_variable( 'memage', [self.memory_size], dtype=tf.float32, trainable=False, initializer=tf.constant_initializer(0.0), caching_device=caching_device) self.recent_idx = tf.get_variable( 'recent_idx', [self.vocab_size], dtype=tf.int32, trainable=False, initializer=tf.constant_initializer(0, tf.int32)) # variable for projecting query vector into memory key self.query_proj = tf.get_variable( 'memory_query_proj', [self.key_dim, self.key_dim], dtype=tf.float32, initializer=tf.truncated_normal_initializer(0, 0.01), caching_device=caching_device)
Example #13
Source File: wrapped_units.py From DOTA_models with Apache License 2.0 | 5 votes |
def _append_base_layers(self, hidden_layers): """Appends layers defined by the base class to the |hidden_layers|.""" last_layer = hidden_layers[-1] logits = tf.nn.xw_plus_b(last_layer, self._component.get_variable('weights_softmax'), self._component.get_variable('bias_softmax')) return hidden_layers + [last_layer, logits]
Example #14
Source File: layers.py From ARU-Net with GNU General Public License v2.0 | 5 votes |
def feat_norm(input, dimZ): beta = tf.get_variable('beta', shape=(dimZ,), initializer=tf.constant_initializer(value=0.0)) gamma = tf.get_variable('gamma', shape=(dimZ,), initializer=tf.constant_initializer(value=1.0)) output,_, _ = tf.nn.fused_batch_norm(input, gamma, beta) return output
Example #15
Source File: network_units.py From DOTA_models with Apache License 2.0 | 5 votes |
def create(self, fixed_embeddings, linked_embeddings, context_tensor_arrays, attention_tensor, during_training, stride=None): """Requires |stride|; otherwise see base class.""" # TODO(googleuser): Normalize the arguments to create(). 'stride' # is unused by the recurrent network units, while 'context_tensor_arrays' # and 'attenion_tensor_array' is unused by bulk network units. b/33587044 if stride is None: raise ValueError("PairwiseConvNetwork needs 'stride'") input_tensor = get_input_tensor_with_stride(fixed_embeddings, linked_embeddings, stride) # TODO(googleuser): Add dropout. del context_tensor_arrays, attention_tensor, during_training # Unused. num_steps = tf.shape(input_tensor)[1] arg1 = tf.expand_dims(input_tensor, 1) arg1 = tf.tile(arg1, tf.stack([1, num_steps, 1, 1])) arg2 = tf.expand_dims(input_tensor, 2) arg2 = tf.tile(arg2, tf.stack([1, 1, num_steps, 1])) conv = tf.concat([arg1, arg2], 3) for i in xrange(self._num_layers): with tf.variable_scope('conv%d' % i, reuse=True) as scope: conv = tf.nn.conv2d( conv, self._component.get_variable('weights'), [1, 1, 1, 1], padding='SAME') conv = tf.nn.bias_add(conv, self._component.get_variable('biases')) if i in self._relu_layers: conv = tf.nn.relu(conv, name=scope.name) return [tf.reshape(conv, [-1, num_steps], name='reshape_activations')]
Example #16
Source File: component.py From DOTA_models with Apache License 2.0 | 5 votes |
def get_variable(self, var_name=None, var_params=None): """Returns either the original or averaged version of a given variable. If the master.read_from_avg flag is set to True, and the ExponentialMovingAverage (EMA) object has been attached, then this will ask the EMA object for the given variable. This is to allow executing inference from the averaged version of parameters. Arguments: var_name: Name of the variable. var_params: tf.Variable for which to retrieve an average. Only one of |var_name| or |var_params| needs to be provided. If both are provided, |var_params| takes precedence. Returns: tf.Variable object corresponding to original or averaged version. """ if var_params: var_name = var_params.name else: check.NotNone(var_name, 'specify at least one of var_name or var_params') var_params = tf.get_variable(var_name) if self.moving_average and self.master.read_from_avg: logging.info('Retrieving average for: %s', var_name) var_params = self.moving_average.average(var_params) assert var_params logging.info('Returning: %s', var_params.name) return var_params
Example #17
Source File: graph_builder.py From DOTA_models with Apache License 2.0 | 5 votes |
def _AddVariable(self, shape, dtype, name, initializer=None): if name in self.variables: return self.variables[name] self.variables[name] = tf.get_variable(name, shape, dtype, initializer) if initializer is not None: self.inits[name] = state_ops.init_variable(self.variables[name], initializer) return self.variables[name]
Example #18
Source File: deep_cnn.py From DOTA_models with Apache License 2.0 | 5 votes |
def _variable_on_cpu(name, shape, initializer): """Helper to create a Variable stored on CPU memory. Args: name: name of the variable shape: list of ints initializer: initializer for Variable Returns: Variable Tensor """ with tf.device('/cpu:0'): var = tf.get_variable(name, shape, initializer=initializer) return var
Example #19
Source File: variables.py From DOTA_models with Apache License 2.0 | 5 votes |
def __call__(self, op): device_string = '' if self._num_ps > 0: task_id = self._next_task_id self._next_task_id = (self._next_task_id + 1) % self._num_ps device_string = '%s/task:%d' % (self._ps_device, task_id) device_string += '/%s' % self._placement return device_string # TODO(sguada) Remove once get_variable is able to colocate op.devices.
Example #20
Source File: hyperparams_builder_test.py From DOTA_models with Apache License 2.0 | 5 votes |
def _assert_variance_in_range(self, initializer, shape, variance, tol=1e-2): with tf.Graph().as_default() as g: with self.test_session(graph=g) as sess: var = tf.get_variable( name='test', shape=shape, dtype=tf.float32, initializer=initializer) sess.run(tf.global_variables_initializer()) values = sess.run(var) self.assertAllClose(np.var(values), variance, tol, tol)
Example #21
Source File: neural_gpu.py From DOTA_models with Apache License 2.0 | 5 votes |
def layer_norm(x, nmaps, prefix, epsilon=1e-5): """Layer normalize the 4D tensor x, averaging over the last dimension.""" with tf.variable_scope(prefix): scale = tf.get_variable("layer_norm_scale", [nmaps], initializer=tf.ones_initializer()) bias = tf.get_variable("layer_norm_bias", [nmaps], initializer=tf.zeros_initializer()) mean, variance = tf.nn.moments(x, [3], keep_dims=True) norm_x = (x - mean) / tf.sqrt(variance + epsilon) return norm_x * scale + bias
Example #22
Source File: network_units.py From DOTA_models with Apache License 2.0 | 5 votes |
def create(self, fixed_embeddings, linked_embeddings, context_tensor_arrays, attention_tensor, during_training, stride=None): """Requires |stride|; otherwise see base class.""" if stride is None: raise RuntimeError("ConvNetwork needs 'stride' and must be called in the " "bulk feature extractor component.") input_tensor = get_input_tensor_with_stride(fixed_embeddings, linked_embeddings, stride) # TODO(googleuser): Add context and attention. del context_tensor_arrays, attention_tensor # On CPU, add a dimension so that the 'image' has shape # [stride, 1, num_steps, D]. conv = tf.expand_dims(input_tensor, 1) for i in range(len(self._depths) - 1): with tf.variable_scope('conv%d' % i, reuse=True) as scope: if during_training: conv.set_shape([None, 1, None, self._depths[i]]) conv = self._maybe_apply_dropout(conv, stride) conv = tf.nn.conv2d( conv, self._component.get_variable('weights'), [1, 1, 1, 1], padding='SAME') conv = tf.nn.bias_add(conv, self._component.get_variable('biases')) if i < (len(self._weights) - 1) or not self._output_dim: conv = self._nonlinearity(conv, name=scope.name) return [ tf.reshape( conv, [-1, self._depths[-1]], name='reshape_activations') ]
Example #23
Source File: resnet_model.py From DOTA_models with Apache License 2.0 | 5 votes |
def _fully_connected(self, x, out_dim): """FullyConnected layer for final output.""" x = tf.reshape(x, [self.hps.batch_size, -1]) w = tf.get_variable( 'DW', [x.get_shape()[1], out_dim], initializer=tf.uniform_unit_scaling_initializer(factor=1.0)) b = tf.get_variable('biases', [out_dim], initializer=tf.constant_initializer()) return tf.nn.xw_plus_b(x, w, b)
Example #24
Source File: cifar10.py From DOTA_models with Apache License 2.0 | 5 votes |
def _variable_on_cpu(name, shape, initializer): """Helper to create a Variable stored on CPU memory. Args: name: name of the variable shape: list of ints initializer: initializer for Variable Returns: Variable Tensor """ with tf.device('/cpu:0'): dtype = tf.float16 if FLAGS.use_fp16 else tf.float32 var = tf.get_variable(name, shape, initializer=initializer, dtype=dtype) return var
Example #25
Source File: critic.py From neural-combinatorial-optimization-rl-tensorflow with MIT License | 5 votes |
def predict_rewards(self,input_): with tf.variable_scope("encoder"): Encoder = Attentive_encoder(self.config) encoder_output = Encoder.encode(input_) frame = tf.reduce_mean(encoder_output, 1) # [Batch size, Sequence Length, Num_neurons] to [Batch size, Num_neurons] with tf.variable_scope("ffn"): # ffn 1 h0 = tf.layers.dense(frame, self.num_neurons, activation=tf.nn.relu, kernel_initializer=self.initializer) # ffn 2 w1 =tf.get_variable("w1", [self.num_neurons, 1], initializer=self.initializer) b1 = tf.Variable(self.init_baseline, name="b1") self.predictions = tf.squeeze(tf.matmul(h0, w1)+b1)
Example #26
Source File: layers.py From ARU-Net with GNU General Public License v2.0 | 5 votes |
def deconv2d_bn_lrn_drop(scope_or_name, inputs, kernel_shape, out_shape, subS=2, activation=tf.nn.relu, use_bn=False, use_mvn=False, is_training=True, use_lrn=False, keep_prob=1.0, dropout_maps=False, initOpt=0): with tf.variable_scope(scope_or_name): if initOpt == 0: stddev = np.sqrt(2.0 / (kernel_shape[0] * kernel_shape[1] * kernel_shape[2] + kernel_shape[3])) if initOpt == 1: stddev = 5e-2 if initOpt == 2: stddev = min(np.sqrt(2.0 / (kernel_shape[0] * kernel_shape[1] * kernel_shape[2])),5e-2) kernel = tf.get_variable("weights", kernel_shape, initializer=tf.random_normal_initializer(stddev=stddev)) bias = tf.get_variable("bias", kernel_shape[2], initializer=tf.constant_initializer(value=0.1)) conv=tf.nn.conv2d_transpose(inputs, kernel, out_shape, strides=[1, subS, subS, 1], padding='SAME', name='conv') outputs = tf.nn.bias_add(conv, bias, name='preActivation') if use_bn: # outputs = tf.layers.batch_normalization(outputs, axis=3, training=is_training, name="batchNorm") outputs = batch_norm(outputs, is_training=is_training, scale=True, fused=True, scope="batchNorm") if use_mvn: outputs = feat_norm(outputs, kernel_shape[3]) if activation: outputs = activation(outputs, name='activation') if use_lrn: outputs = tf.nn.local_response_normalization(outputs, name='localResponseNorm') if dropout_maps: conv_shape = tf.shape(outputs) n_shape = tf.stack([conv_shape[0], 1, 1, conv_shape[3]]) outputs = tf.nn.dropout(outputs, keep_prob, noise_shape=n_shape) else: outputs = tf.nn.dropout(outputs, keep_prob) return outputs
Example #27
Source File: resnet_v1_test.py From DOTA_models with Apache License 2.0 | 5 votes |
def testConv2DSameOdd(self): n, n2 = 5, 3 # Input image. x = create_test_input(1, n, n, 1) # Convolution kernel. w = create_test_input(1, 3, 3, 1) w = tf.reshape(w, [3, 3, 1, 1]) tf.get_variable('Conv/weights', initializer=w) tf.get_variable('Conv/biases', initializer=tf.zeros([1])) tf.get_variable_scope().reuse_variables() y1 = slim.conv2d(x, 1, [3, 3], stride=1, scope='Conv') y1_expected = tf.to_float([[14, 28, 43, 58, 34], [28, 48, 66, 84, 46], [43, 66, 84, 102, 55], [58, 84, 102, 120, 64], [34, 46, 55, 64, 30]]) y1_expected = tf.reshape(y1_expected, [1, n, n, 1]) y2 = resnet_utils.subsample(y1, 2) y2_expected = tf.to_float([[14, 43, 34], [43, 84, 55], [34, 55, 30]]) y2_expected = tf.reshape(y2_expected, [1, n2, n2, 1]) y3 = resnet_utils.conv2d_same(x, 1, 3, stride=2, scope='Conv') y3_expected = y2_expected y4 = slim.conv2d(x, 1, [3, 3], stride=2, scope='Conv') y4_expected = y2_expected with self.test_session() as sess: sess.run(tf.global_variables_initializer()) self.assertAllClose(y1.eval(), y1_expected.eval()) self.assertAllClose(y2.eval(), y2_expected.eval()) self.assertAllClose(y3.eval(), y3_expected.eval()) self.assertAllClose(y4.eval(), y4_expected.eval())
Example #28
Source File: resnet_v1_test.py From DOTA_models with Apache License 2.0 | 5 votes |
def testConv2DSameEven(self): n, n2 = 4, 2 # Input image. x = create_test_input(1, n, n, 1) # Convolution kernel. w = create_test_input(1, 3, 3, 1) w = tf.reshape(w, [3, 3, 1, 1]) tf.get_variable('Conv/weights', initializer=w) tf.get_variable('Conv/biases', initializer=tf.zeros([1])) tf.get_variable_scope().reuse_variables() y1 = slim.conv2d(x, 1, [3, 3], stride=1, scope='Conv') y1_expected = tf.to_float([[14, 28, 43, 26], [28, 48, 66, 37], [43, 66, 84, 46], [26, 37, 46, 22]]) y1_expected = tf.reshape(y1_expected, [1, n, n, 1]) y2 = resnet_utils.subsample(y1, 2) y2_expected = tf.to_float([[14, 43], [43, 84]]) y2_expected = tf.reshape(y2_expected, [1, n2, n2, 1]) y3 = resnet_utils.conv2d_same(x, 1, 3, stride=2, scope='Conv') y3_expected = y2_expected y4 = slim.conv2d(x, 1, [3, 3], stride=2, scope='Conv') y4_expected = tf.to_float([[48, 37], [37, 22]]) y4_expected = tf.reshape(y4_expected, [1, n2, n2, 1]) with self.test_session() as sess: sess.run(tf.global_variables_initializer()) self.assertAllClose(y1.eval(), y1_expected.eval()) self.assertAllClose(y2.eval(), y2_expected.eval()) self.assertAllClose(y3.eval(), y3_expected.eval()) self.assertAllClose(y4.eval(), y4_expected.eval())
Example #29
Source File: resnet_v2_test.py From DOTA_models with Apache License 2.0 | 5 votes |
def testConv2DSameOdd(self): n, n2 = 5, 3 # Input image. x = create_test_input(1, n, n, 1) # Convolution kernel. w = create_test_input(1, 3, 3, 1) w = tf.reshape(w, [3, 3, 1, 1]) tf.get_variable('Conv/weights', initializer=w) tf.get_variable('Conv/biases', initializer=tf.zeros([1])) tf.get_variable_scope().reuse_variables() y1 = slim.conv2d(x, 1, [3, 3], stride=1, scope='Conv') y1_expected = tf.to_float([[14, 28, 43, 58, 34], [28, 48, 66, 84, 46], [43, 66, 84, 102, 55], [58, 84, 102, 120, 64], [34, 46, 55, 64, 30]]) y1_expected = tf.reshape(y1_expected, [1, n, n, 1]) y2 = resnet_utils.subsample(y1, 2) y2_expected = tf.to_float([[14, 43, 34], [43, 84, 55], [34, 55, 30]]) y2_expected = tf.reshape(y2_expected, [1, n2, n2, 1]) y3 = resnet_utils.conv2d_same(x, 1, 3, stride=2, scope='Conv') y3_expected = y2_expected y4 = slim.conv2d(x, 1, [3, 3], stride=2, scope='Conv') y4_expected = y2_expected with self.test_session() as sess: sess.run(tf.global_variables_initializer()) self.assertAllClose(y1.eval(), y1_expected.eval()) self.assertAllClose(y2.eval(), y2_expected.eval()) self.assertAllClose(y3.eval(), y3_expected.eval()) self.assertAllClose(y4.eval(), y4_expected.eval())
Example #30
Source File: resnet_v2_test.py From DOTA_models with Apache License 2.0 | 5 votes |
def testConv2DSameEven(self): n, n2 = 4, 2 # Input image. x = create_test_input(1, n, n, 1) # Convolution kernel. w = create_test_input(1, 3, 3, 1) w = tf.reshape(w, [3, 3, 1, 1]) tf.get_variable('Conv/weights', initializer=w) tf.get_variable('Conv/biases', initializer=tf.zeros([1])) tf.get_variable_scope().reuse_variables() y1 = slim.conv2d(x, 1, [3, 3], stride=1, scope='Conv') y1_expected = tf.to_float([[14, 28, 43, 26], [28, 48, 66, 37], [43, 66, 84, 46], [26, 37, 46, 22]]) y1_expected = tf.reshape(y1_expected, [1, n, n, 1]) y2 = resnet_utils.subsample(y1, 2) y2_expected = tf.to_float([[14, 43], [43, 84]]) y2_expected = tf.reshape(y2_expected, [1, n2, n2, 1]) y3 = resnet_utils.conv2d_same(x, 1, 3, stride=2, scope='Conv') y3_expected = y2_expected y4 = slim.conv2d(x, 1, [3, 3], stride=2, scope='Conv') y4_expected = tf.to_float([[48, 37], [37, 22]]) y4_expected = tf.reshape(y4_expected, [1, n2, n2, 1]) with self.test_session() as sess: sess.run(tf.global_variables_initializer()) self.assertAllClose(y1.eval(), y1_expected.eval()) self.assertAllClose(y2.eval(), y2_expected.eval()) self.assertAllClose(y3.eval(), y3_expected.eval()) self.assertAllClose(y4.eval(), y4_expected.eval())