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
python类get_model_variables()的实例源码
export_model.py 文件源码
项目:mlc2017-online
作者: machine-learning-challenge
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def build_prediction_graph(self, serialized_examples):
model_input_raw, labels_batch = (
self.reader.prepare_serialized_examples(serialized_examples))
model_input = model_input_raw
with tf.variable_scope("tower"):
result = self.model.create_model(
model_input,
num_classes=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"]
prediction, index = tf.nn.top_k(predictions, 1)
return prediction, index
export_model.py 文件源码
项目:mlc2017-online
作者: machine-learning-challenge
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def build_prediction_graph(self, serialized_examples):
image_id, model_input_raw, labels_batch = (
self.reader.prepare_serialized_examples(serialized_examples))
model_input = model_input_raw
with tf.variable_scope("tower"):
result = self.model.create_model(
model_input,
num_classes=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"]
prediction, index = tf.nn.top_k(predictions, 1)
return image_id, prediction, index
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
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
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
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
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
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
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
def save_model_for_prediction(self, save_ckpt_fn, vars_to_save=None):
"""Save model data only needed for prediction.
Args:
save_ckpt_fn: checkpoint file to save.
vars_to_save: a list of variables to save.
"""
if vars_to_save is None:
vars_to_save = slim.get_model_variables()
vars_restore_to_exclude = []
for scope in self.dm_model.restore_scope_exclude:
vars_restore_to_exclude.extend(slim.get_variables(scope))
# remove not restored variables.
vars_to_save = [
v for v in vars_to_save if v not in vars_restore_to_exclude
]
base_model.save_model(save_ckpt_fn, self.sess, vars_to_save)
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
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
export_model.py 文件源码
项目:Youtube8mdataset_kagglechallenge
作者: jasonlee27
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
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
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
def build_prediction_graph(self, serialized_examples):
model_input_raw, labels_batch = (
self.reader.prepare_serialized_examples(serialized_examples))
model_input = model_input_raw
with tf.variable_scope("tower"):
result = self.model.create_model(
model_input,
num_classes=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"]
prediction, index = tf.nn.top_k(predictions, 1)
return prediction, index
def build_prediction_graph(self, serialized_examples):
image_id, model_input_raw, labels_batch = (
self.reader.prepare_serialized_examples(serialized_examples))
model_input = model_input_raw
with tf.variable_scope("tower"):
result = self.model.create_model(
model_input,
num_classes=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"]
prediction, index = tf.nn.top_k(predictions, 1)
return image_id, prediction, index
def create_init_fn_to_restore(master_checkpoint, train_dir):
"""Creates an init operations to restore weights from various checkpoints.
master_checkpoint is path to a checkpoint which contains all weights for
the whole model.
"""
if master_checkpoint is None:
return None
# Warn the user if a checkpoint exists in the train_dir. Then we'll be
# ignoring the checkpoint path anyway.
if tf.train.latest_checkpoint(train_dir):
tf.logging.info(
'Ignoring --checkpoint_path because a checkpoint already exists in %s'
% train_dir)
return None
if tf.gfile.IsDirectory(master_checkpoint):
checkpoint_path = tf.train.latest_checkpoint(master_checkpoint)
else:
checkpoint_path = master_checkpoint
tf.logging.info('Fine-tuning from %s' % checkpoint_path)
return slim.assign_from_checkpoint_fn(checkpoint_path, slim.get_model_variables())
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)
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
export_model.py 文件源码
项目:tutorial_mnist
作者: machine-learning-challenge
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def build_prediction_graph(self, serialized_examples):
model_input_raw, labels_batch = (
self.reader.prepare_serialized_examples(serialized_examples))
model_input = model_input_raw
with tf.variable_scope("tower"):
result = self.model.create_model(
model_input,
num_classes=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"]
prediction, index = tf.nn.top_k(predictions, 1)
return prediction, index
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
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,
keep_prob=1.0)
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
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
def disp_model_info():
with tf.Graph().as_default():
# Dummy placeholders for arbitrary number of 1d inputs and outputs
inputs = tf.placeholder(tf.float32, shape=(None, 1))
outputs = tf.placeholder(tf.float32, shape=(None, 1))
# Build model
predictions, end_points = regression_model(inputs)
# Print name and shape of each tensor.
print("Layers")
for k, v in end_points.items():
print('name = {}, shape = {}'.format(v.name, v.get_shape()))
# Print name and shape of parameter nodes (values not yet initialized)
print("\n")
print("Parameters")
for v in slim.get_model_variables():
print('name = {}, shape = {}'.format(v.name, v.get_shape()))
print("\n")
print("Local Parameters")
for v in slim.get_local_variables():
print('name = {}, shape = {}'.format(v.name, v.get_shape()))
return
def use_inceptionv4(self):
image_size = inception.inception_v4.default_image_size
img_path = "../../data/misec_images/EnglishCockerSpaniel_simon.jpg"
checkpoint_path = "../../data/trained_models/inception_v4/inception_v4.ckpt"
with tf.Graph().as_default():
image_string = tf.read_file(img_path)
image = tf.image.decode_jpeg(image_string, channels=3)
processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False)
processed_images = tf.expand_dims(processed_image, 0)
# Create the model, use the default arg scope to configure the batch norm parameters.
with slim.arg_scope(inception.inception_v4_arg_scope()):
logits, _ = inception.inception_v4(processed_images, num_classes=1001, is_training=False)
probabilities = tf.nn.softmax(logits)
init_fn = slim.assign_from_checkpoint_fn(
checkpoint_path,
slim.get_model_variables('InceptionV4'))
with tf.Session() as sess:
init_fn(sess)
np_image, probabilities = sess.run([image, probabilities])
probabilities = probabilities[0, 0:]
sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])]
self.disp_names(sorted_inds,probabilities)
plt.figure()
plt.imshow(np_image.astype(np.uint8))
plt.axis('off')
plt.title(img_path)
plt.show()
return
def use_vgg16(self):
with tf.Graph().as_default():
image_size = vgg.vgg_16.default_image_size
img_path = "../../data/misec_images/First_Student_IC_school_bus_202076.jpg"
checkpoint_path = "../../data/trained_models/vgg16/vgg_16.ckpt"
image_string = tf.read_file(img_path)
image = tf.image.decode_jpeg(image_string, channels=3)
processed_image = vgg_preprocessing.preprocess_image(image, image_size, image_size, is_training=False)
processed_images = tf.expand_dims(processed_image, 0)
# Create the model, use the default arg scope to configure the batch norm parameters.
with slim.arg_scope(vgg.vgg_arg_scope()):
# 1000 classes instead of 1001.
logits, _ = vgg.vgg_16(processed_images, num_classes=1000, is_training=False)
probabilities = tf.nn.softmax(logits)
init_fn = slim.assign_from_checkpoint_fn(
checkpoint_path,
slim.get_model_variables('vgg_16'))
with tf.Session() as sess:
init_fn(sess)
np_image, probabilities = sess.run([image, probabilities])
probabilities = probabilities[0, 0:]
sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])]
self.disp_names(sorted_inds,probabilities,include_background=False)
plt.figure()
plt.imshow(np_image.astype(np.uint8))
plt.axis('off')
plt.title(img_path)
plt.show()
return
def _add_variables_summaries(learning_rate):
summaries = []
for variable in slim.get_model_variables():
summaries.append(tf.summary.histogram(variable.op.name, variable))
summaries.append(tf.summary.scalar('training/Learning Rate', learning_rate))
return summaries
def create_resnet_model(img_dim):
pre_image = tf.placeholder(tf.float32, [None, None, 3])
processed_image = cnn_preprocessing.preprocess_for_eval(pre_image/255.0, img_dim, img_dim)
images = tf.placeholder(tf.float32, [None, img_dim, img_dim, 3])
# mean = tf.constant([123.68, 116.779, 103.939], dtype=tf.float32, shape=[1, 1, 1, 3], name='img_mean')
# processed_images = images - mean
with slim.arg_scope(resnet_utils.resnet_arg_scope()):
probs, endpoints = resnet_v2.resnet_v2_152(images, num_classes=1001, is_training = False)
print endpoints['resnet_v2_152/block4']
init_fn = slim.assign_from_checkpoint_fn(
'Data/CNNModels/resnet_v2_152.ckpt',
slim.get_model_variables('resnet_v2_152'))
sess = tf.Session()
init_fn(sess)
return {
'images_placeholder' : images,
'block4' : endpoints['resnet_v2_152/block4'],
'session' : sess,
'processed_image' : processed_image,
'pre_image' : pre_image,
'probs' : probs
}
def _add_variables_summaries(learning_rate):
summaries = []
for variable in slim.get_model_variables():
summaries.append(tf.summary.histogram(variable.op.name, variable))
summaries.append(tf.summary.scalar('training/Learning Rate', learning_rate))
return summaries
def build_finetunable_model(inputs, cfg):
with slim.arg_scope([slim.conv2d],
activation_fn=tf.nn.relu,
normalizer_fn=slim.batch_norm,
weights_regularizer=slim.l2_regularizer(0.00004),
biases_regularizer=slim.l2_regularizer(0.00004)) as scope:
batch_norm_params = {
'decay': cfg.BATCHNORM_MOVING_AVERAGE_DECAY,
'epsilon': 0.001,
'variables_collections' : [],
'is_training' : False
}
with slim.arg_scope([slim.conv2d], normalizer_params=batch_norm_params):
features, _ = model.inception_resnet_v2(inputs, reuse=False, scope='InceptionResnetV2')
# Save off the original variables (for ease of restoring)
model_variables = slim.get_model_variables()
inception_vars = {var.op.name:var for var in model_variables}
batch_norm_params = {
'decay': cfg.BATCHNORM_MOVING_AVERAGE_DECAY,
'epsilon': 0.001,
'variables_collections' : [tf.GraphKeys.MOVING_AVERAGE_VARIABLES],
'is_training' : True
}
with slim.arg_scope([slim.conv2d], normalizer_params=batch_norm_params):
# Add on the detection heads
locs, confs, _ = model.build_detection_heads(features, cfg.NUM_BBOXES_PER_CELL)
model_variables = slim.get_model_variables()
detection_vars = {var.op.name:var for var in model_variables if var.op.name not in inception_vars}
return locs, confs, inception_vars, detection_vars