本文整理汇总了Python中tensorflow.contrib.slim.get_model_variables方法的典型用法代码示例。如果您正苦于以下问题:Python slim.get_model_variables方法的具体用法?Python slim.get_model_variables怎么用?Python slim.get_model_variables使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.contrib.slim
的用法示例。
在下文中一共展示了slim.get_model_variables方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_ckpt
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_model_variables [as 别名]
def load_ckpt(ckpt_name, var_scope_name, scope, constructor, input_tensor, label_offset, load_weights, **kwargs):
"""
Arguments
ckpt_name file name of the checkpoint
var_scope_name name of the variable scope
scope arg_scope
constructor constructor of the model
input_tensor tensor of input image
label_offset whether it is 1000 classes or 1001 classes, if it is 1001, remove class 0
load_weights whether to load weights
kwargs
is_training
create_aux_logits
"""
with slim.arg_scope(scope):
logits, endpoints = constructor(\
input_tensor, num_classes=1000+label_offset, \
scope=var_scope_name, **kwargs)
if load_weights:
init_fn = slim.assign_from_checkpoint_fn(\
ckpt_name, slim.get_model_variables(var_scope_name))
init_fn(K.get_session())
return logits, endpoints
示例2: build_prediction_graph
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_model_variables [as 别名]
def build_prediction_graph(self, serialized_examples):
video_id, model_input_raw, labels_batch, num_frames = (
self.reader.prepare_serialized_examples(serialized_examples))
feature_dim = len(model_input_raw.get_shape()) - 1
model_input = tf.nn.l2_normalize(model_input_raw, feature_dim)
with tf.name_scope("model"):
result = self.model.create_model(
model_input,
num_frames=num_frames,
vocab_size=self.reader.num_classes,
labels=labels_batch,
is_training=False)
for variable in slim.get_model_variables():
tf.summary.histogram(variable.op.name, variable)
predictions = result["predictions"]
top_predictions, top_indices = tf.nn.top_k(predictions,
_TOP_PREDICTIONS_IN_OUTPUT)
return video_id, top_indices, top_predictions
示例3: classify
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_model_variables [as 别名]
def classify(model_range, seg_range, feature_lr, classifier_lr):
feat_opt = tf.train.AdamOptimizer(feature_lr)
clas_opt = tf.train.AdamOptimizer(classifier_lr)
for model in model_range:
for seg in seg_range:
with tf.variable_scope('classifier-{}-{}'.format(model, seg)):
self.preds[(model, seg)] = slim.conv2d(self.feature, 500, [1, 1])
self.clas_vars[(model, seg)] = slim.get_model_variables()[-2:]
with tf.variable_scope('losses-{}-{}'.format(model, seg)):
self.losses[(model, seg)] = self.loss(self.labels, self.preds[(model, seg)])
grad = tf.gradients(self.losses[(model, seg)], self.feat_vars + self.clas_vars[(model, seg)])
train_op_feat = feat_opt.apply_gradients(zip(grad[:-2], self.feat_vars))
train_op_clas = clas_opt.apply_gradients(zip(grad[-2:], self.clas_vars[(model, seg)]))
self.train_ops[(model, seg)] = tf.group(train_op_feat, train_op_clas)
return self.losses, self.train_ops
示例4: _get_init_fn
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_model_variables [as 别名]
def _get_init_fn():
vgg_checkpoint_path = "vgg_19.ckpt"
if tf.gfile.IsDirectory(vgg_checkpoint_path):
checkpoint_path = tf.train.latest_checkpoint(vgg_checkpoint_path)
else:
checkpoint_path = vgg_checkpoint_path
variables_to_restore = []
for var in slim.get_model_variables():
tf.logging.info('model_var: %s' % var)
excluded = False
for exclusion in ['vgg_19/fc']:
if var.op.name.startswith(exclusion):
excluded = True
tf.logging.info('exclude:%s' % exclusion)
break
if not excluded:
variables_to_restore.append(var)
tf.logging.info('Fine-tuning from %s' % checkpoint_path)
return slim.assign_from_checkpoint_fn(
checkpoint_path,
variables_to_restore,
ignore_missing_vars=True)
示例5: get_init_fn
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_model_variables [as 别名]
def get_init_fn(checkpoints_dir, model_name='inception_v1.ckpt'):
"""Returns a function run by the chief worker to warm-start the training.
"""
checkpoint_exclude_scopes=["InceptionV1/Logits", "InceptionV1/AuxLogits"]
exclusions = [scope.strip() for scope in checkpoint_exclude_scopes]
variables_to_restore = []
for var in slim.get_model_variables():
excluded = False
for exclusion in exclusions:
if var.op.name.startswith(exclusion):
excluded = True
break
if not excluded:
variables_to_restore.append(var)
return slim.assign_from_checkpoint_fn(
os.path.join(checkpoints_dir, model_name),
variables_to_restore)
示例6: prepare_inception_score_classifier
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_model_variables [as 别名]
def prepare_inception_score_classifier(classifier_name, num_classes, images, return_saver=True):
network_fn = nets_factory.get_network_fn(
classifier_name,
num_classes=num_classes,
weight_decay=0.0,
is_training=False,
)
# Note: you may need to change the prediction_fn here.
try:
logits, end_points = network_fn(images, prediction_fn=tf.sigmoid, create_aux_logits=False)
except TypeError:
tf.logging.warning('Cannot specify prediction_fn=tf.sigmoid, create_aux_logits=False.')
logits, end_points = network_fn(images, )
variables_to_restore = slim.get_model_variables(scope=nets_factory.scopes_map[classifier_name])
predictions = end_points['Predictions']
if return_saver:
saver = tf.train.Saver(variables_to_restore)
return predictions, end_points, saver
else:
return predictions, end_points
示例7: __add_summaries
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_model_variables [as 别名]
def __add_summaries(self,end_points,learning_rate,total_loss):
for end_point in end_points:
x = end_points[end_point]
tf.summary.histogram('activations/' + end_point, x)
tf.summary.scalar('sparsity/' + end_point, tf.nn.zero_fraction(x))
for loss in tf.get_collection(tf.GraphKeys.LOSSES):
tf.summary.scalar('losses/%s' % loss.op.name, loss)
# Add total_loss to summary.
tf.summary.scalar('total_loss', total_loss)
# Add summaries for variables.
for variable in slim.get_model_variables():
tf.summary.histogram(variable.op.name, variable)
tf.summary.scalar('learning_rate', learning_rate)
return
示例8: get_init_fn
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_model_variables [as 别名]
def get_init_fn(self, checkpoint_path):
"""Returns a function run by the chief worker to warm-start the training."""
checkpoint_exclude_scopes=["InceptionV4/Logits", "InceptionV4/AuxLogits"]
exclusions = [scope.strip() for scope in checkpoint_exclude_scopes]
variables_to_restore = []
for var in slim.get_model_variables():
excluded = False
for exclusion in exclusions:
if var.op.name.startswith(exclusion):
excluded = True
break
if not excluded:
variables_to_restore.append(var)
return slim.assign_from_checkpoint_fn(
checkpoint_path,
variables_to_restore)
示例9: __add_summaries
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_model_variables [as 别名]
def __add_summaries(self,end_points,learning_rate,total_loss):
# Add summaries for end_points (activations).
for end_point in end_points:
x = end_points[end_point]
tf.summary.histogram('activations/' + end_point, x)
tf.summary.scalar('sparsity/' + end_point,
tf.nn.zero_fraction(x))
# Add summaries for losses and extra losses.
tf.summary.scalar('total_loss', total_loss)
for loss in tf.get_collection('EXTRA_LOSSES'):
tf.summary.scalar(loss.op.name, loss)
# Add summaries for variables.
for variable in slim.get_model_variables():
tf.summary.histogram(variable.op.name, variable)
return
示例10: build_prediction_graph
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_model_variables [as 别名]
def build_prediction_graph(self, serialized_examples):
video_id, model_input_raw, labels_batch, num_frames = (
self.reader.prepare_serialized_examples(serialized_examples))
feature_dim = len(model_input_raw.get_shape()) - 1
model_input = tf.nn.l2_normalize(model_input_raw, feature_dim)
with tf.variable_scope("tower"):
result = self.model.create_model(
model_input,
num_frames=num_frames,
vocab_size=self.reader.num_classes,
labels=labels_batch,
is_training=False)
for variable in slim.get_model_variables():
tf.summary.histogram(variable.op.name, variable)
predictions = result["predictions"]
top_predictions, top_indices = tf.nn.top_k(predictions,
_TOP_PREDICTIONS_IN_OUTPUT)
return video_id, top_indices, top_predictions
示例11: build_prediction_graph
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_model_variables [as 别名]
def build_prediction_graph(self, serialized_examples):
video_id, model_input_raw, labels_batch, num_frames = (
self.reader.prepare_serialized_examples(serialized_examples))
feature_dim = len(model_input_raw.get_shape()) - 1
model_input = tf.nn.l2_normalize(model_input_raw, feature_dim)
with tf.variable_scope("tower"):
layers_keep_probs=tf.Variable([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], tf.float32, name="layers_keep_probs")
result = self.model.create_model(
model_input,
num_frames=num_frames,
vocab_size=self.reader.num_classes,
labels=labels_batch,
is_training=False,
layers_keep_probs=layers_keep_probs)
for variable in slim.get_model_variables():
tf.summary.histogram(variable.op.name, variable)
predictions = result["predictions"]
top_predictions, top_indices = tf.nn.top_k(predictions,
_TOP_PREDICTIONS_IN_OUTPUT)
return video_id, top_indices, top_predictions
示例12: build_prediction_graph
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_model_variables [as 别名]
def build_prediction_graph(self, serialized_examples):
video_id, model_input_raw, labels_batch, num_frames = (
self.reader.prepare_serialized_examples(serialized_examples))
feature_dim = len(model_input_raw.get_shape()) - 1
model_input = tf.nn.l2_normalize(model_input_raw, feature_dim)
with tf.variable_scope("tower"):
result = self.model.create_model(
model_input,
num_frames=num_frames,
vocab_size=self.reader.num_classes,
labels=labels_batch,
is_training=False)
for variable in slim.get_model_variables():
tf.summary.histogram(variable.op.name, variable)
predictions = result["predictions"]
top_predictions, top_indices = tf.nn.top_k(predictions,
_TOP_PREDICTIONS_IN_OUTPUT)
return video_id, top_indices, top_predictions
示例13: test_clean_accuracy
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_model_variables [as 别名]
def test_clean_accuracy(self):
"""Check model is accurate on unperturbed images."""
input_dir = FLAGS.input_image_dir
metadata_file_path = FLAGS.metadata_file_path
num_images = 16
batch_shape = (num_images, 299, 299, 3)
images, labels = load_images(
input_dir, metadata_file_path, batch_shape)
num_classes = 1001
tf.logging.set_verbosity(tf.logging.INFO)
with tf.Graph().as_default():
# Prepare graph
x_input = tf.placeholder(tf.float32, shape=batch_shape)
y_label = tf.placeholder(tf.int32, shape=(num_images,))
model = InceptionModel(num_classes)
logits = model.get_logits(x_input)
acc = _top_1_accuracy(logits, y_label)
# Run computation
saver = tf.train.Saver(slim.get_model_variables())
session_creator = tf.train.ChiefSessionCreator(
scaffold=tf.train.Scaffold(saver=saver),
checkpoint_filename_with_path=FLAGS.checkpoint_path,
master=FLAGS.master)
with tf.train.MonitoredSession(
session_creator=session_creator) as sess:
acc_val = sess.run(acc, feed_dict={
x_input: images, y_label: labels})
tf.logging.info('Accuracy: %s', acc_val)
assert acc_val > 0.8
示例14: get_tuned_variables
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_model_variables [as 别名]
def get_tuned_variables():
exclusions = [scope.strip() for scope in CHECKPOINT_EXCLUDE_SCOPES.split(',')]
variables_to_restore = []
for var in slim.get_model_variables():
excluded = False
for exclusion in exclusions:
if var.op.name.startswith(exclusion):
excluded = True
break
if not excluded:
variables_to_restore.append(var)
return variables_to_restore
# 获取所有需要训练的变量列表
示例15: test
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_model_variables [as 别名]
def test(self):
trg_images, trg_labels = self.load_mnist(self.mnist_dir, split='test')
# build a graph
model = self.model
model.build_model()
config = tf.ConfigProto()
config.allow_soft_placement = True
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
tf.global_variables_initializer().run()
print ('Loading model.')
variables_to_restore = slim.get_model_variables()
restorer = tf.train.Saver(variables_to_restore)
restorer.restore(sess, self.trained_model)
trg_acc, trg_entr = sess.run(fetches=[model.trg_accuracy, model.trg_entropy],
feed_dict={model.trg_images: trg_images[:],
model.trg_labels: trg_labels[:]})
print ('test acc [%.3f]' %(trg_acc))
print ('entropy [%.3f]' %(trg_entr))
with open('test_'+ str(model.alpha) +'_' + model.method + '.txt', "a") as resfile:
resfile.write(str(trg_acc)+'\t'+str(trg_entr)+'\n')
#~ print confusion_matrix(trg_labels, trg_pred)