def main():
"""Create the model and start the training."""
args = get_arguments()
# Default image.
image_batch = tf.constant(0, tf.float32, shape=[1, 321, 321, 3])
# Create network.
net = DeepLabResNetModel({'data': image_batch})
var_list = tf.global_variables()
# Set up tf session and initialize variables.
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
init = tf.global_variables_initializer()
sess.run(init)
# Loading .npy weights.
net.load(args.npy_path, sess)
# Saver for converting the loaded weights into .ckpt.
saver = tf.train.Saver(var_list=var_list, write_version=1)
save(saver, sess, args.save_dir)
python类global_variables()的实例源码
BidirectionNet_tfidf.py 文件源码
项目:Sohu-LuckData-Image-Text-Matching-Competition
作者: WeitaoVan
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def build_matchnet(self):
self.sentence_fc2 = self.sentencenet(self.tfidf_feat, reuse=False)
#self.sentence_fc2 = self.sentence_concat(self.tfidf_feat, self.lda_feat, reuse=False)
self.image_fc2 = self.imagenet(self.image_feat, skip=self.is_skip, reuse=False)
# compute loss
if self.is_training:
# triplet loss
#sentence_fc2_neg = self.sentencenet(self.sentence_feat_neg, reuse=True)
#image_fc2_neg = self.imagenet(self.image_feat_neg, skip=self.is_skip, reuse=True)
#self.image_center_triplet_loss = self.triplet_loss(self.image_fc2, self.sentence_fc2, sentence_fc2_neg)
#self.sentence_center_triplet_loss = self.triplet_loss(self.sentence_fc2, self.image_fc2, image_fc2_neg)
# top k triplet loss
self.sentence_center_triplet_loss, self.image_center_triplet_loss = self.top_K_loss(
self.sentence_fc2, self.image_fc2)
self.reg_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
# reg loss and total loss
self.total_loss = tf.add_n([self.image_center_triplet_loss, self.sentence_center_triplet_loss] + self.reg_loss)
self.saver = tf.train.Saver(max_to_keep=30)
self.t_var = tf.trainable_variables()
self.g_var = tf.global_variables()
self.img_var = [var for var in self.t_var if 'image' in var.name]
BidirectionNet_4wtfidf.py 文件源码
项目:Sohu-LuckData-Image-Text-Matching-Competition
作者: WeitaoVan
项目源码
文件源码
阅读 32
收藏 0
点赞 0
评论 0
def build_matchnet(self):
self.sentence_fc2 = self.sentencenet(self.tfidf_feat, reuse=False)
#self.sentence_fc2 = self.sentence_concat(self.tfidf_feat, self.lda_feat, reuse=False)
self.image_fc2 = self.imagenet(self.image_feat, skip=self.is_skip, reuse=False)
# compute loss
if self.is_training:
# triplet loss
#sentence_fc2_neg = self.sentencenet(self.sentence_feat_neg, reuse=True)
#image_fc2_neg = self.imagenet(self.image_feat_neg, skip=self.is_skip, reuse=True)
#self.image_center_triplet_loss = self.triplet_loss(self.image_fc2, self.sentence_fc2, sentence_fc2_neg)
#self.sentence_center_triplet_loss = self.triplet_loss(self.sentence_fc2, self.image_fc2, image_fc2_neg)
# top k triplet loss
self.sentence_center_triplet_loss, self.image_center_triplet_loss = self.top_K_loss(
self.sentence_fc2, self.image_fc2)
self.reg_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
# reg loss and total loss
self.total_loss = tf.add_n([self.image_center_triplet_loss, self.sentence_center_triplet_loss] + self.reg_loss)
self.saver = tf.train.Saver(max_to_keep=30)
self.t_var = tf.trainable_variables()
self.g_var = tf.global_variables()
self.img_var = [var for var in self.t_var if 'image' in var.name]
def restore_inception_resnet_variables_from_weight(sess, weights_path):
adam_vars = [var for var in tf.global_variables()
if 'Adam' in var.name or
'beta1_power' in var.name or
'beta2_power' in var.name]
uninit_vars = tf.get_collection(
tf.GraphKeys.GLOBAL_VARIABLES, scope='InceptionResnetV2/Conv2d_1a_3x3') + adam_vars
init_op = tf.variables_initializer(uninit_vars)
variables_to_restore = slim.get_variables_to_restore(
exclude=['InceptionResnetV2/Conv2d_1a_3x3'])
for var in uninit_vars:
if var in variables_to_restore:
variables_to_restore.remove(var)
saver = tf.train.Saver(variables_to_restore)
print 'Initializing new variables to train from downloaded inception resnet weights'
sess.run(init_op)
saver.restore(sess, weights_path)
return 0
tf_regression.py 文件源码
项目:tensorflow_to_lambda_serverless
作者: jacopotagliabue
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def train(self, train_X, train_Y, learning_rate, training_epochs, model_output_dir=None):
n_samples = train_X.shape[0]
# Mean squared error
cost = tf.reduce_sum(tf.pow(self.model - self.vars['Y'], 2)) / (2 * n_samples)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# Launch the graph
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver(tf.global_variables())
# Fit all training data
for epoch in range(training_epochs):
for x, y in zip(train_X, train_Y):
sess.run(optimizer, feed_dict={self.vars['X']: x, self.vars['Y']: y})
# Save model locally
saver.save(sess, model_output_dir + 'model.ckpt')
return
def print_all_variables(train_only=False):
"""Print all trainable and non-trainable variables
without tl.layers.initialize_global_variables(sess)
Parameters
----------
train_only : boolean
If True, only print the trainable variables, otherwise, print all variables.
"""
if train_only:
t_vars = tf.trainable_variables()
print(" [*] printing trainable variables")
else:
try: # TF1.0
t_vars = tf.global_variables()
except: # TF0.12
t_vars = tf.all_variables()
print(" [*] printing global variables")
for idx, v in enumerate(t_vars):
print(" var {:3}: {:15} {}".format(idx, str(v.get_shape()), v.name))
def get_variables_with_name(name, train_only=True, printable=False):
"""Get variable list by a given name scope.
>>> dense_vars = tl.layers.get_variable_with_name('dense', True, True)
"""
print(" [*] geting variables with %s" % name)
# tvar = tf.trainable_variables() if train_only else tf.all_variables()
if train_only:
t_vars = tf.trainable_variables()
else:
try: # TF1.0
t_vars = tf.global_variables()
except: # TF0.12
t_vars = tf.all_variables()
d_vars = [var for var in t_vars if name in var.name]
if printable:
for idx, v in enumerate(d_vars):
print(" got {:3}: {:15} {}".format(idx, v.name, str(v.get_shape())))
return d_vars
def build_discriminator(x_data, x_generated, keep_prob):
x_data=tf.unstack(x_data,seq_size,1);
x_generated=list(x_generated);
x_in = tf.concat([x_data, x_generated],1);
x_in=tf.unstack(x_in,seq_size,0);
lstm_cell = tf.contrib.rnn.MultiRNNCell([tf.contrib.rnn.DropoutWrapper(tf.contrib.rnn.BasicLSTMCell(n_hidden), output_keep_prob=keep_prob) for _ in range(d_num_layers)]);
with tf.variable_scope("dis") as dis:
weights=tf.Variable(tf.random_normal([n_hidden, 1]));
biases=tf.Variable(tf.random_normal([1]));
outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x_in, dtype=tf.float32);
res=tf.matmul(outputs[-1], weights) + biases;
y_data = tf.nn.sigmoid(tf.slice(res, [0, 0], [batch_size, -1], name=None));
y_generated = tf.nn.sigmoid(tf.slice(res, [batch_size, 0], [-1, -1], name=None));
d_params=[v for v in tf.global_variables() if v.name.startswith(dis.name)];
with tf.name_scope("desc_params"):
for param in d_params:
variable_summaries(param);
return y_data, y_generated, d_params;
def initialize():
new_variables = set(tf.global_variables()) - ALREADY_INITIALIZED
get_session().run(tf.variables_initializer(new_variables))
ALREADY_INITIALIZED.update(new_variables)
def savable_variables(self):
"""Returns a list/dict of savable variables to pass to tf.train.Saver."""
return tf.global_variables()
def get_post_init_ops(self):
# Copy initialized values for variables on GPU 0 to other GPUs.
global_vars = tf.global_variables()
var_by_name = dict([(v.name, v) for v in global_vars])
post_init_ops = []
for v in global_vars:
split_name = v.name.split('/')
# TODO(b/62630508): use more specific prefix than v or v0.
if split_name[0] == 'v0' or not v.name.startswith('v'):
continue
split_name[0] = 'v0'
copy_from = var_by_name['/'.join(split_name)]
post_init_ops.append(v.assign(copy_from.read_value()))
return post_init_ops
def savable_variables(self):
"""Return the set of variables used for saving/loading the model."""
params = []
for v in tf.global_variables():
split_name = v.name.split('/')
if split_name[0] == 'v0' or not v.name.startswith('v'):
params.append(v)
return params
def get_post_init_ops(self):
"""Copy initialized values for variables to other devices."""
global_vars = tf.global_variables()
var_by_name = dict([(v.name, v) for v in global_vars])
post_init_ops = []
for v in global_vars:
split_name = v.name.split('/')
# TODO(b/62630508): use more specific prefix than v or v0.
if split_name[0] == 'v0' or not v.name.startswith('v'):
continue
split_name[0] = 'v0'
copy_from = var_by_name['/'.join(split_name)]
post_init_ops.append(v.assign(copy_from.read_value()))
return post_init_ops
def savable_variables(self):
"""Return the set of variables used for saving/loading the model."""
params = []
for v in tf.global_variables():
split_name = v.name.split('/')
if split_name[0] == 'v0' or not v.name.startswith('v'):
params.append(v)
return params
def initialize(self, sess):
# Initial file lists are empty
np_paths = []
ss_paths = []
# Fresh train directly from ImageNet weights
print('Loading initial model weights from {:s}'.format(self.pretrained_model))
variables = tf.global_variables()
# Initialize all variables first
sess.run(tf.variables_initializer(variables, name='init'))
var_keep_dic = self.get_variables_in_checkpoint_file(self.pretrained_model)
# Get the variables to restore, ignoring the variables to fix
variables_to_restore = self.net.get_variables_to_restore(variables, var_keep_dic)
restorer = tf.train.Saver(variables_to_restore)
restorer.restore(sess, self.pretrained_model)
print('Loaded.')
# Need to fix the variables before loading, so that the RGB weights are changed to BGR
# For VGG16 it also changes the convolutional weights fc6 and fc7 to
# fully connected weights
self.net.fix_variables(sess, self.pretrained_model)
print('Fixed.')
last_snapshot_iter = 0
rate = cfg.TRAIN.LEARNING_RATE
stepsizes = list(cfg.TRAIN.STEPSIZE)
return rate, last_snapshot_iter, stepsizes, np_paths, ss_paths
def vars(self):
return [var for var in tf.global_variables() if self.name in var.name]
def vars(self):
return [var for var in tf.global_variables() if self.name in var.name]
def vars(self):
return [var for var in tf.global_variables() if self.name in var.name]
def vars(self):
return [var for var in tf.global_variables() if self.name in var.name]
def vars(self):
return [var for var in tf.global_variables() if self.name in var.name]