def main(args):
if args.meta_file == None or not os.path.exists(args.meta_file):
print("Invalid tensorflow meta-graph file:", args.meta_file)
return
gpu_options = tf.GPUOptions(allow_growth=True)
sess = tf.Session(config=tf.ConfigProto(
gpu_options=gpu_options,
log_device_placement=False,
allow_soft_placement=True))
with sess.as_default():
# ---- load pretrained parameters ---- #
saver = tf.train.import_meta_graph(args.meta_file, clear_devices=True)
saver.restore(tf.get_default_session(), args.ckpt_file)
pretrained = {}
var_ = tf.get_collection(tf.GraphKeys.MODEL_VARIABLES)
print("total:", len(var_))
for v in var_:
print("process:", v.name)
# [notice: the name of parameter is like 'Resnet/conv2d/bias:0',
# here we should remove the prefix name, and get '/conv2d/bias:0']
v_name = v.name
pretrained[v_name] = sess.run([v])
np.save(args.save_path, pretrained)
print("done:", len(pretrained.keys()))
python类get_default_session()的实例源码
def load_model(model):
# Check if the model is a model directory (containing a metagraph and a checkpoint file)
# or if it is a protobuf file with a frozen graph
model_exp = os.path.expanduser(model)
if (os.path.isfile(model_exp)):
print('Model filename: %s' % model_exp)
with gfile.FastGFile(model_exp,'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
else:
print('Model directory: %s' % model_exp)
meta_file, ckpt_file = get_model_filenames(model_exp)
print('Metagraph file: %s' % meta_file)
print('Checkpoint file: %s' % ckpt_file)
saver = tf.train.import_meta_graph(os.path.join(model_exp, meta_file))
saver.restore(tf.get_default_session(), os.path.join(model_exp, ckpt_file))
def next_batch(self, batch_size=1, sess=None):
with tf.name_scope('Batch_getter') as scope:
if sess is None :
self.sess = tf.get_default_session()
else:
self.sess = sess
images, seqs, masks = self.sess.run([self.images , self.complete_seq, self.complete_mask], feed_dict={self.batch_size : batch_size})
'''
print(seqs)
print(str(seqs[0])[0])
process_seqs=[]
process_masks =[]
for index, c in enumerate(seqs):
process_seqs.extend([c.decode()])
process_masks.extend([masks[index].decode()])
'''
print(seqs)
c_seqs = self.finalize_seq(seqs)
c_masks = self.finalize_seq(masks)
print(c_seqs)
input_seqs = self.make_input_seq(c_seqs)
output_seqs = self.make_output_seq(c_seqs)
final_masks = self.make_input_seq(c_masks)
return images , input_seqs, output_seqs, final_masks
def test_variable_by_name(self):
with self.test_context():
name = 'variable'
variable = tf.get_variable(name, shape=())
self.assertTrue(gpflow.misc.is_initializable_tensor(variable))
def equal(found):
self.assertTrue(gpflow.misc.is_initializable_tensor(found))
self.assertEqual(found, variable)
def not_equal(found):
self.assertEqual(found, None)
fn = gpflow.misc.get_variable_by_name
graph = tf.Graph()
session = tf.get_default_session()
fake_name = "foo"
equal(fn(name))
equal(fn(name, graph=session.graph))
not_equal(fn(name, graph=graph))
not_equal(fn(fake_name))
not_equal(fn(fake_name, graph=graph))
def action_given(self, state, add_noise):
# NOTE: noise is added _outside_ tf graph. we do this simply because the noisy output
# is never used for any part of computation graph required for online training. it's
# only used during training after being the replay buffer.
actions = tf.get_default_session().run(self.output_action,
feed_dict={self.input_state: [state],
base_network.IS_TRAINING: False,
base_network.FLIP_HORIZONTALLY: False})
if add_noise:
if VERBOSE_DEBUG:
pre_noise = str(actions)
actions[0] += self.exploration_noise.sample()
actions = np.clip(1, -1, actions) # action output is _always_ (-1, 1)
if VERBOSE_DEBUG:
print "TRAIN action_given pre_noise %s post_noise %s" % (pre_noise, actions)
return map(float, np.squeeze(actions))
def _run_monitor(self, monitor, num_epochs=3, num_steps_per_epoch=10,
pass_max_steps=True):
if pass_max_steps:
max_steps = num_epochs * num_steps_per_epoch - 1
else:
max_steps = None
monitor.begin(max_steps=max_steps)
for epoch in xrange(num_epochs):
monitor.epoch_begin(epoch)
should_stop = False
step = epoch * num_steps_per_epoch
next_epoch_step = step + num_steps_per_epoch
while (not should_stop) and (step < next_epoch_step):
tensors = monitor.step_begin(step)
output = tf.get_default_session().run(tensors) if tensors else {}
output = dict(zip(
[t.name if isinstance(t, tf.Tensor) else t for t in tensors],
output))
should_stop = monitor.step_end(step=step, output=output)
monitor.post_step(step=step, session=None)
step += 1
monitor.epoch_end(epoch)
monitor.end()
def _run_monitor(self, monitor, num_epochs=3, num_steps_per_epoch=10,
pass_max_steps=True):
if pass_max_steps:
max_steps = num_epochs * num_steps_per_epoch - 1
else:
max_steps = None
monitor.begin(max_steps=max_steps)
for epoch in xrange(num_epochs):
monitor.epoch_begin(epoch)
should_stop = False
step = epoch * num_steps_per_epoch
next_epoch_step = step + num_steps_per_epoch
while (not should_stop) and (step < next_epoch_step):
tensors = monitor.step_begin(step)
output = tf.get_default_session().run(tensors) if tensors else {}
output = dict(zip(
[t.name if isinstance(t, tf.Tensor) else t for t in tensors],
output))
should_stop = monitor.step_end(step=step, output=output)
monitor.post_step(step=step, session=None)
step += 1
monitor.epoch_end(epoch)
monitor.end()
def verify_image_jpeg(imagepath, imageshape):
scope = inspect.stack()[0][3]
try:
graph = tf.get_default_graph()
path = graph.get_tensor_by_name(scope + '/path:0')
decode = graph.get_tensor_by_name(scope + '/decode_jpeg:0')
except KeyError:
tf.logging.debug('creating decode_jpeg tensor')
path = tf.placeholder(tf.string, name=scope + '/path')
imagefile = tf.read_file(path, name=scope + '/read_file')
decode = tf.image.decode_jpeg(imagefile, channels=3, name=scope + '/decode_jpeg')
try:
image = tf.get_default_session().run(decode, {path: imagepath})
except:
return False
return np.all(np.equal(image.shape[:2], imageshape[:2]))
def test(self, model, cases):
sess = tf.get_default_session()
guarantee_initialized_variables(sess)
embeds = model.compute(model.embeds, cases)
primitive_embeddings = RLongPrimitiveEmbeddings(6)
# compute object embedding after applying projection
object_projection_layer = model._object_projection_layer
W, b = object_projection_layer.get_weights() # shapes [10, 6] and [6]
object_embed = np.ones(10).dot(W) + b
assert_array_almost_equal(embeds[0],
np.concatenate((np.zeros(6), primitive_embeddings['r'], primitive_embeddings[-1]))
)
assert_array_almost_equal(embeds[1],
np.concatenate((np.zeros(6), np.zeros(6), primitive_embeddings['X1/1']))
)
assert_array_almost_equal(embeds[2],
np.concatenate((primitive_embeddings['b'], object_embed, object_embed))
)
def save_checkpoint(self, checkpoint_name):
tf.get_collection_ref("threshold")[:] = [float(self.threshold)]
tf.get_collection_ref("features")[:] = self.features.values()
tf.get_collection_ref("loss")[:] = [self.loss]
tf.get_collection_ref("prediction")[:] = [self.prediction]
os.makedirs(os.path.dirname(checkpoint_name), exist_ok=True)
saver = tf.train.Saver()
saver.save(tf.get_default_session(), checkpoint_name)
with open(os.path.join(os.path.dirname(checkpoint_name), "hparams.txt"), "w") as f:
f.write(repr(self.hparams.__dict__))
def predict_proba_with_loss(self, X, y):
feed_dict = {}
feed_dict[self.labels] = y
for key, tensor in self.features.items():
feed_dict[tensor] = X[key]
prediction, loss = tf.get_default_session().run(
[self.prediction, self.loss], feed_dict=feed_dict)
return np.reshape(prediction, [-1]), loss
def save_checkpoint(self, checkpoint_name):
tf.get_collection_ref("threshold")[:] = [float(self.threshold)]
tf.get_collection_ref("features")[:] = self.features.values()
tf.get_collection_ref("loss")[:] = [self.loss]
tf.get_collection_ref("prediction")[:] = [self.prediction]
os.makedirs(os.path.dirname(checkpoint_name), exist_ok=True)
saver = tf.train.Saver()
saver.save(tf.get_default_session(), checkpoint_name)
with open(os.path.join(os.path.dirname(checkpoint_name), "hparams.txt"), "w") as f:
f.write(repr(self.hparams.__dict__))
def predict_proba_with_loss(self, X, y):
feed_dict = {}
feed_dict[self.labels] = y
for key, tensor in self.features.items():
feed_dict[tensor] = X[key]
prediction, loss = tf.get_default_session().run(
[self.prediction, self.loss], feed_dict=feed_dict)
return np.reshape(prediction, [-1]), loss
def save_checkpoint(self, checkpoint_name):
tf.get_collection_ref("threshold")[:] = [float(self.threshold)]
tf.get_collection_ref("features")[:] = self.features.values()
tf.get_collection_ref("loss")[:] = [self.loss]
tf.get_collection_ref("prediction")[:] = [self.prediction]
os.makedirs(os.path.dirname(checkpoint_name), exist_ok=True)
saver = tf.train.Saver()
saver.save(tf.get_default_session(), checkpoint_name)
with open(os.path.join(os.path.dirname(checkpoint_name), "hparams.txt"), "w") as f:
f.write(repr(self.hparams.__dict__))
def save_checkpoint(self, checkpoint_name):
tf.get_collection_ref("threshold")[:] = [float(self.threshold)]
tf.get_collection_ref("features")[:] = self.features.values()
tf.get_collection_ref("loss")[:] = [self.loss]
tf.get_collection_ref("prediction")[:] = [self.prediction]
os.makedirs(os.path.dirname(checkpoint_name), exist_ok=True)
saver = tf.train.Saver()
saver.save(tf.get_default_session(), checkpoint_name)
with open(os.path.join(os.path.dirname(checkpoint_name), "hparams.txt"), "w") as f:
f.write(repr(self.hparams.__dict__))
def predict_proba_with_loss(self, X, y):
feed_dict = {}
feed_dict[self.labels] = y
for key, tensor in self.features.items():
feed_dict[tensor] = X[key]
prediction, loss = tf.get_default_session().run(
[self.prediction, self.loss], feed_dict=feed_dict)
return np.reshape(prediction, [-1]), loss
def act(self, ob, c, h):
sess = tf.get_default_session()
return sess.run([self.sample, self.vf] + self.state_out,
{self.x: [ob], self.state_in[0]: c, self.state_in[1]: h})
def value(self, ob, c, h):
sess = tf.get_default_session()
return sess.run(self.vf, {self.x: [ob], self.state_in[0]: c, self.state_in[1]: h})[0]
def act(self, ob, c, h):
sess = tf.get_default_session()
return sess.run([self.sample, self.vf] + self.state_out,
{self.x: [ob], self.state_in[0]: c, self.state_in[1]: h})
def act(self, ob):
sess = tf.get_default_session()
return sess.run([self.sample, self.vf],
{self.x: [ob]})