def _anchor_target_layer(self, rpn_cls_score, name):
with tf.variable_scope(name) as scope:
rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights = tf.py_func(
anchor_target_layer,
[rpn_cls_score, self._gt_boxes, self._im_info, self._feat_stride, self._anchors, self._num_anchors],
[tf.float32, tf.float32, tf.float32, tf.float32],
name="anchor_target")
rpn_labels.set_shape([1, 1, None, None])
rpn_bbox_targets.set_shape([1, None, None, self._num_anchors * 4])
rpn_bbox_inside_weights.set_shape([1, None, None, self._num_anchors * 4])
rpn_bbox_outside_weights.set_shape([1, None, None, self._num_anchors * 4])
rpn_labels = tf.to_int32(rpn_labels, name="to_int32")
self._anchor_targets['rpn_labels'] = rpn_labels
self._anchor_targets['rpn_bbox_targets'] = rpn_bbox_targets
self._anchor_targets['rpn_bbox_inside_weights'] = rpn_bbox_inside_weights
self._anchor_targets['rpn_bbox_outside_weights'] = rpn_bbox_outside_weights
self._score_summaries.update(self._anchor_targets)
return rpn_labels
python类py_func()的实例源码
def _anchor_target_layer(self, rpn_cls_score, name):
with tf.variable_scope(name) as scope:
rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights = tf.py_func(
anchor_target_layer,
[rpn_cls_score, self._gt_boxes, self._im_info, self._feat_stride, self._anchors, self._num_anchors],
[tf.float32, tf.float32, tf.float32, tf.float32],
name="anchor_target")
rpn_labels.set_shape([1, 1, None, None])
rpn_bbox_targets.set_shape([1, None, None, self._num_anchors * 4])
rpn_bbox_inside_weights.set_shape([1, None, None, self._num_anchors * 4])
rpn_bbox_outside_weights.set_shape([1, None, None, self._num_anchors * 4])
rpn_labels = tf.to_int32(rpn_labels, name="to_int32")
self._anchor_targets['rpn_labels'] = rpn_labels
self._anchor_targets['rpn_bbox_targets'] = rpn_bbox_targets
self._anchor_targets['rpn_bbox_inside_weights'] = rpn_bbox_inside_weights
self._anchor_targets['rpn_bbox_outside_weights'] = rpn_bbox_outside_weights
self._score_summaries.update(self._anchor_targets)
return rpn_labels
def _proposal_target_layer(self, rois, roi_scores, name):
with tf.variable_scope(name) as scope:
rois, roi_scores, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights = tf.py_func(
proposal_target_layer,
[rois, roi_scores, self._gt_boxes, self._num_classes],
[tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32],
name="proposal_target")
rois.set_shape([cfg.TRAIN.BATCH_SIZE, 5])
roi_scores.set_shape([cfg.TRAIN.BATCH_SIZE])
labels.set_shape([cfg.TRAIN.BATCH_SIZE, 1])
bbox_targets.set_shape([cfg.TRAIN.BATCH_SIZE, self._num_classes * 4])
bbox_inside_weights.set_shape([cfg.TRAIN.BATCH_SIZE, self._num_classes * 4])
bbox_outside_weights.set_shape([cfg.TRAIN.BATCH_SIZE, self._num_classes * 4])
self._proposal_targets['rois'] = rois
self._proposal_targets['labels'] = tf.to_int32(labels, name="to_int32")
self._proposal_targets['bbox_targets'] = bbox_targets
self._proposal_targets['bbox_inside_weights'] = bbox_inside_weights
self._proposal_targets['bbox_outside_weights'] = bbox_outside_weights
self._score_summaries.update(self._proposal_targets)
return rois, roi_scores
def sequence_to_pianoroll_op(sequence_tensor, hparams):
"""Transforms a serialized NoteSequence to a pianoroll."""
def sequence_to_pianoroll_fn(sequence_tensor):
sequence = preprocess_sequence(sequence_tensor)
return sequence_to_pianoroll(
sequence,
frames_per_second=hparams_frames_per_second(hparams),
min_pitch=constants.MIN_MIDI_PITCH,
max_pitch=constants.MAX_MIDI_PITCH,
min_frame_occupancy_for_label=hparams.min_frame_occupancy_for_label,
onset_mode=hparams.onset_mode, onset_length_ms=hparams.onset_length,
onset_delay_ms=hparams.onset_delay)
res, weighted_res, onsets = tf.py_func(
sequence_to_pianoroll_fn, [sequence_tensor],
[tf.float32, tf.float32, tf.float32],
name='sequence_to_pianoroll_op')
res.set_shape([None, constants.MIDI_PITCHES])
weighted_res.set_shape([None, constants.MIDI_PITCHES])
onsets.set_shape([None, constants.MIDI_PITCHES])
return res, weighted_res, onsets
def tf_ispecgram(spec,
n_fft=512,
hop_length=None,
mask=True,
pad=True,
log_mag=True,
re_im=False,
dphase=True,
mag_only=False,
num_iters=1000):
dims = spec.get_shape().as_list()
# Add back in nyquist frequency
x = spec if not pad else tf.concat(
[spec, tf.zeros([dims[0], 1, dims[2], dims[3]])], 1)
audio = tf.py_func(batch_ispecgram, [
x, n_fft, hop_length, mask, log_mag, re_im, dphase, mag_only, num_iters
], tf.float32)
return audio
#---------------------------------------------------
# Summaries
#---------------------------------------------------
def _flat_map(self, example_serialized, features=None):
"""
Flat maps the example serialized.
Simple example:
def _parse(line):
a, b = [np.int32(x) for x in line.split()]
return [a, a*2, a*3], [b, b*2, b*3]
t_input, t_ouptut = tf.py_func(_parse, [line], [tf.int32, tf.int32],
stateful=True, name='py_parse_example')
v = (t_intput, t_output)
return Dataset.from_tensor_slices(v)
:param example_serialized:
:param features: do not use this as it is deprecated after 1.2
:return: a dataset
"""
pass
# TODO remove features in TF 1.3
def _map(self, example_serialized, features=None):
"""
Maps a example_serialized read from the dataset into the final set of tf.Tensors
to return to the model.
Simple example:
def _parse(line, features=None):
a, b = [np.int32(x) for x in line.split()]
return a, b
t_input, t_ouptut = tf.py_func(_parse, [line], [tf.int32, tf.int32],
stateful=True, name='py_parse_example')
t_ouptut = tf.add(t_ouptut, 1)
return t_input, t_ouptut
:param example_serialized: the example serialized
:param features: do not use this as it is deprecated after 1.2
:return: a tuple of the tensors to return when get_next is called. Usually (inputs,outputs)
"""
pass
network.py 文件源码
项目:Automatic_Group_Photography_Enhancement
作者: Yuliang-Zou
项目源码
文件源码
阅读 43
收藏 0
点赞 0
评论 0
def proposal_target_layer(self, input, classes, name):
if isinstance(input[0], tuple):
input[0] = input[0][0]
with tf.variable_scope(name) as scope:
# (Yuliang)
rois,labels,eye,smile,bbox_targets,bbox_inside_weights,bbox_outside_weights = tf.py_func(proposal_target_layer_py,[input[0],input[1],classes],[tf.float32,tf.float32,tf.float32,tf.float32,tf.float32,tf.float32,tf.float32])
rois = tf.reshape(rois,[-1,5] , name = 'rois')
labels = tf.convert_to_tensor(tf.cast(labels,tf.int32), name = 'labels')
# (Yuliang)
eye = tf.convert_to_tensor(tf.cast(eye,tf.int32), name = 'eye')
smile = tf.convert_to_tensor(tf.cast(smile,tf.int32), name = 'smile')
bbox_targets = tf.convert_to_tensor(bbox_targets, name = 'bbox_targets')
bbox_inside_weights = tf.convert_to_tensor(bbox_inside_weights, name = 'bbox_inside_weights')
bbox_outside_weights = tf.convert_to_tensor(bbox_outside_weights, name = 'bbox_outside_weights')
# (Yuliang)
return rois, labels, eye, smile, bbox_targets, bbox_inside_weights, bbox_outside_weights
# return rois, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights
# (Yuliang)
# Since we have 2 more outputs in proposal_target_layer, we now want to
# exclude them so that we can feed into the roi_pooling layer
def _flat_map(self, example_serialized, features=None):
"""
Flat maps the example serialized.
Simple example:
def _parse(line):
a, b = [np.int32(x) for x in line.split()]
return [a, a*2, a*3], [b, b*2, b*3]
t_input, t_ouptut = tf.py_func(_parse, [line], [tf.int32, tf.int32],
stateful=True, name='py_parse_example')
v = (t_intput, t_output)
return Dataset.from_tensor_slices(v)
:param example_serialized:
:param features: do not use this as it is deprecated after 1.2
:return: a dataset
"""
pass
# TODO remove features in TF 1.3
def _map(self, example_serialized, features=None):
"""
Maps a example_serialized read from the dataset into the final set of tf.Tensors
to return to the model.
Simple example:
def _parse(line, features=None):
a, b = [np.int32(x) for x in line.split()]
return a, b
t_input, t_ouptut = tf.py_func(_parse, [line], [tf.int32, tf.int32],
stateful=True, name='py_parse_example')
t_ouptut = tf.add(t_ouptut, 1)
return t_input, t_ouptut
:param example_serialized: the example serialized
:param features: do not use this as it is deprecated after 1.2
:return: a tuple of the tensors to return when get_next is called. Usually (inputs,outputs)
"""
pass
distributed_training_test.py 文件源码
项目:tensorport-template
作者: tensorport
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def _map(self, example_serialized):
def _parse(line):
input = np.float32(line)
# a simple equation of the input to generate the output
output = input + 10 + input * 2
# generate 2 inputs and 1 output
return input, np.float32(input * 3), np.float32(output)
input_1, input_2, output = tf.py_func(func=_parse,
inp=[example_serialized],
Tout=[tf.float32, tf.float32, tf.float32],
stateful=True)
# set shapes for data
input_1 = tf.reshape(input_1, [1])
input_2 = tf.reshape(input_2, [1])
output = tf.reshape(output, [1])
# we could perform this operation here or in the graph
input = tf.concat([input_1, input_2], axis=0)
return input, output
def sample_with_gt_wrapper(boxes, scores, gt_boxes, is_training=True, scope='SampleBoxesWithGT'):
with tf.name_scope(scope) as sc:
boxes, scores, batch_inds, mask_boxes, mask_scores, mask_batch_inds = \
tf.py_func(sample.sample_rpn_outputs_wrt_gt_boxes,
[boxes, scores, gt_boxes, is_training],
[tf.float32, tf.float32, tf.int32, tf.float32, tf.float32, tf.int32])
boxes = tf.convert_to_tensor(boxes, name='Boxes')
scores = tf.convert_to_tensor(scores, name='Scores')
batch_inds = tf.convert_to_tensor(batch_inds, name='BatchInds')
mask_boxes = tf.convert_to_tensor(mask_boxes, name='MaskBoxes')
mask_scores = tf.convert_to_tensor(mask_scores, name='MaskScores')
mask_batch_inds = tf.convert_to_tensor(mask_batch_inds, name='MaskBatchInds')
return boxes, scores, batch_inds, mask_boxes, mask_scores, mask_batch_inds
def assign_boxes(gt_boxes, tensors, layers, scope='AssignGTBoxes'):
with tf.name_scope(scope) as sc:
min_k = layers[0]
max_k = layers[-1]
assigned_layers = \
tf.py_func(assign.assign_boxes,
[ gt_boxes, min_k, max_k ],
tf.int32)
assigned_layers = tf.reshape(assigned_layers, [-1])
assigned_tensors = []
for t in tensors:
split_tensors = []
for l in layers:
tf.cast(l, tf.int32)
inds = tf.where(tf.equal(assigned_layers, l))
inds = tf.reshape(inds, [-1])
split_tensors.append(tf.gather(t, inds))
assigned_tensors.append(split_tensors)
return assigned_tensors + [assigned_layers]
def anchor_target_layer(self, input, _feat_strides, anchor_sizes, name):
if isinstance(input[0], tuple):
input[0] = input[0][0]
with tf.variable_scope(name) as scope:
# 'rpn_cls_score', 'gt_boxes', 'gt_ishard', 'dontcare_areas', 'im_info'
rpn_labels,rpn_bbox_targets,rpn_bbox_inside_weights,rpn_bbox_outside_weights = \
tf.py_func(anchor_target_layer_py,
[input[0],input[1],input[2],input[3],input[4],input[5],input[6],input[7],input[8], _feat_strides, anchor_sizes],
[tf.float32,tf.float32,tf.float32,tf.float32])
rpn_labels = tf.convert_to_tensor(tf.cast(rpn_labels,tf.int32), name = 'rpn_labels') # shape is (1 x H x W x A, 2)
rpn_bbox_targets = tf.convert_to_tensor(rpn_bbox_targets, name = 'rpn_bbox_targets') # shape is (1 x H x W x A, 4)
rpn_bbox_inside_weights = tf.convert_to_tensor(rpn_bbox_inside_weights , name = 'rpn_bbox_inside_weights') # shape is (1 x H x W x A, 4)
rpn_bbox_outside_weights = tf.convert_to_tensor(rpn_bbox_outside_weights , name = 'rpn_bbox_outside_weights') # shape is (1 x H x W x A, 4)
return rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights
def proposal_target_layer(self, input, classes, name):
if isinstance(input[0], tuple):
input[0] = input[0][0]
with tf.variable_scope(name) as scope:
#inputs: 'rpn_rois','gt_boxes', 'gt_ishard', 'dontcare_areas'
rois_P2,rois_P3,rois_P4,rois_P5,rois_P6,labels,bbox_targets,bbox_inside_weights,bbox_outside_weights,rois \
= tf.py_func(proposal_target_layer_py,
[input[0],input[1],input[2],input[3],classes],
[tf.float32,tf.float32,tf.float32,tf.float32,tf.float32,tf.float32,tf.float32,tf.float32,tf.float32,tf.float32])
# rois_Px <- (1 x H x W x A(x), 5) e.g. [0, x1, y1, x2, y2]
# rois = tf.convert_to_tensor(rois, name='rois')
rois = tf.reshape(rois, [-1, 5], name='rois') # goes to roi_pooling
rois_P2 = tf.reshape(rois_P2, [-1, 5], name='rois_P2') # goes to roi_pooling
rois_P3 = tf.reshape(rois_P3, [-1, 5], name='rois_P3') # goes to roi_pooling
rois_P4 = tf.reshape(rois_P4, [-1, 5], name='rois_P4') # goes to roi_pooling
rois_P5 = tf.reshape(rois_P5, [-1, 5], name='rois_P5') # goes to roi_pooling
rois_P6 = tf.reshape(rois_P6, [-1, 5], name='rois_P6') # goes to roi_pooling
labels = tf.convert_to_tensor(tf.cast(labels,tf.int32), name = 'labels') # goes to FRCNN loss
bbox_targets = tf.convert_to_tensor(bbox_targets, name = 'bbox_targets') # goes to FRCNN loss
bbox_inside_weights = tf.convert_to_tensor(bbox_inside_weights, name = 'bbox_inside_weights')
bbox_outside_weights = tf.convert_to_tensor(bbox_outside_weights, name = 'bbox_outside_weights')
self.layers['rois'] = rois
return rois_P2, rois_P3, rois_P4, rois_P5, rois_P6, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights, rois
def anchor_target_layer(self, input, _feat_stride, anchor_scales, name):
if isinstance(input[0], tuple):
input[0] = input[0][0]
with tf.variable_scope(name) as scope:
# 'rpn_cls_score', 'gt_boxes', 'gt_ishard', 'dontcare_areas', 'im_info'
rpn_labels,rpn_bbox_targets,rpn_bbox_inside_weights,rpn_bbox_outside_weights = \
tf.py_func(anchor_target_layer_py,
[input[0],input[1],input[2],input[3],input[4], _feat_stride, anchor_scales],
[tf.float32,tf.float32,tf.float32,tf.float32])
rpn_labels = tf.convert_to_tensor(tf.cast(rpn_labels,tf.int32), name = 'rpn_labels') # shape is (1 x H x W x A, 2)
rpn_bbox_targets = tf.convert_to_tensor(rpn_bbox_targets, name = 'rpn_bbox_targets') # shape is (1 x H x W x A, 4)
rpn_bbox_inside_weights = tf.convert_to_tensor(rpn_bbox_inside_weights , name = 'rpn_bbox_inside_weights') # shape is (1 x H x W x A, 4)
rpn_bbox_outside_weights = tf.convert_to_tensor(rpn_bbox_outside_weights , name = 'rpn_bbox_outside_weights') # shape is (1 x H x W x A, 4)
return rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights
def proposal_target_layer(self, input, classes, name):
if isinstance(input[0], tuple):
input[0] = input[0][0]
with tf.variable_scope(name) as scope:
#inputs: 'rpn_rois','gt_boxes', 'gt_ishard', 'dontcare_areas'
rois,labels,bbox_targets,bbox_inside_weights,bbox_outside_weights \
= tf.py_func(proposal_target_layer_py,
[input[0],input[1],input[2],input[3],classes],
[tf.float32,tf.float32,tf.float32,tf.float32,tf.float32])
# rois <- (1 x H x W x A, 5) e.g. [0, x1, y1, x2, y2]
# rois = tf.convert_to_tensor(rois, name='rois')
rois = tf.reshape(rois, [-1, 5], name='rois') # goes to roi_pooling
labels = tf.convert_to_tensor(tf.cast(labels,tf.int32), name = 'labels') # goes to FRCNN loss
bbox_targets = tf.convert_to_tensor(bbox_targets, name = 'bbox_targets') # goes to FRCNN loss
bbox_inside_weights = tf.convert_to_tensor(bbox_inside_weights, name = 'bbox_inside_weights')
bbox_outside_weights = tf.convert_to_tensor(bbox_outside_weights, name = 'bbox_outside_weights')
self.layers['rois'] = rois
return rois, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights
def _anchor_target_layer(self, rpn_cls_score, name):
with tf.variable_scope(name) as scope:
rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights = tf.py_func(
anchor_target_layer,
[rpn_cls_score, self._gt_boxes, self._im_info, self._feat_stride, self._anchors, self._num_anchors],
[tf.float32, tf.float32, tf.float32, tf.float32],
name="anchor_target")
rpn_labels.set_shape([1, 1, None, None])
rpn_bbox_targets.set_shape([1, None, None, self._num_anchors * 4])
rpn_bbox_inside_weights.set_shape([1, None, None, self._num_anchors * 4])
rpn_bbox_outside_weights.set_shape([1, None, None, self._num_anchors * 4])
rpn_labels = tf.to_int32(rpn_labels, name="to_int32")
self._anchor_targets['rpn_labels'] = rpn_labels
self._anchor_targets['rpn_bbox_targets'] = rpn_bbox_targets
self._anchor_targets['rpn_bbox_inside_weights'] = rpn_bbox_inside_weights
self._anchor_targets['rpn_bbox_outside_weights'] = rpn_bbox_outside_weights
self._score_summaries.update(self._anchor_targets)
return rpn_labels
def _proposal_target_layer(self, rois, roi_scores, name):
with tf.variable_scope(name) as scope:
rois, roi_scores, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights = tf.py_func(
proposal_target_layer,
[rois, roi_scores, self._gt_boxes, self._num_classes],
[tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32],
name="proposal_target")
rois.set_shape([cfg.TRAIN.BATCH_SIZE, 5])
roi_scores.set_shape([cfg.TRAIN.BATCH_SIZE])
labels.set_shape([cfg.TRAIN.BATCH_SIZE, 1])
bbox_targets.set_shape([cfg.TRAIN.BATCH_SIZE, self._num_classes * 4])
bbox_inside_weights.set_shape([cfg.TRAIN.BATCH_SIZE, self._num_classes * 4])
bbox_outside_weights.set_shape([cfg.TRAIN.BATCH_SIZE, self._num_classes * 4])
self._proposal_targets['rois'] = rois
self._proposal_targets['labels'] = tf.to_int32(labels, name="to_int32")
self._proposal_targets['bbox_targets'] = bbox_targets
self._proposal_targets['bbox_inside_weights'] = bbox_inside_weights
self._proposal_targets['bbox_outside_weights'] = bbox_outside_weights
self._score_summaries.update(self._proposal_targets)
return rois, roi_scores
def _real_predict(self, X, xform=None, crop_bbox=None):
tic = time.time()
img_orig = data.load_image(X, preprocessor=self.preprocessor)
img_orig = np.asarray(img_orig.transpose(1, 2, 0), dtype=np.uint8)
X = data.load_image(X, preprocessor=self.preprocessor)
X = self.standardizer(X, False)
X = X.transpose(1, 2, 0)
X = np.expand_dims(X, 0)
raw_output_up = tf.nn.softmax(self.predictions)
raw_output_up = tf.py_func(
dense_crf, [raw_output_up, tf.expand_dims(img_orig, axis=0), self.num_classes], tf.float32)
raw_output_up = tf.argmax(raw_output_up, dimension=3)
predictions = self.sess.run(
raw_output_up, {self.inputs: X})
predictions = predictions.transpose(0, 2, 1)
print('took %6.1f seconds' % (time.time() - tic))
return predictions
def step(self, action, mode):
qvel, qpos = [], []
if mode == 'tensorflow':
if self.random_initialization:
state, reward, done, qval, qpos = tf.py_func(self._step, inp=[action], Tout=[tf.float32, tf.float32, tf.bool, tf.float32, tf.float32], name='env_step_func')
else:
state, reward, done = tf.py_func(self._step, inp=[action],
Tout=[tf.float32, tf.float32, tf.bool],
name='env_step_func')
state = tf.reshape(state, shape=(self.state_size,))
done.set_shape(())
else:
if self.random_initialization:
state, reward, done, qvel, qpos = self._step(action)
else:
state, reward, done = self._step(action)
return state, reward, done, 0., qvel, qpos
def __init__(self, ops, signals):
super(SimProcessBuilder, self).__init__(ops, signals)
logger.debug("process %s", [op.process for op in ops])
logger.debug("input %s", [op.input for op in ops])
logger.debug("output %s", [op.output for op in ops])
logger.debug("t %s", [op.t for op in ops])
# if we have a custom tensorflow implementation for this process type,
# then we build that. otherwise we'll execute the process step
# function externally (using `tf.py_func`), so we just need to set up
# the inputs/outputs for that.
if isinstance(ops[0].process, self.TF_PROCESS_IMPL):
# note: we do this two-step check (even though it's redundant) to
# make sure that TF_PROCESS_IMPL is kept up to date
if type(ops[0].process) == Lowpass:
self.built_process = LowpassBuilder(ops, signals)
elif isinstance(ops[0].process, LinearFilter):
self.built_process = LinearFilterBuilder(ops, signals)
else:
self.built_process = GenericProcessBuilder(ops, signals)
def build_step(self, signals):
time = signals.time if self.time_input else []
inputs = ([] if self.input_data is None
else signals.gather(self.input_data))
with tf.device("/cpu:0"):
node_outputs = tf.py_func(
self.merged_func, [time, inputs], self.output_dtype,
name=self.merged_func.__name__)
node_outputs.set_shape(self.output_shape)
if self.output_data is not None:
signals.scatter(self.output_data, node_outputs)
# note: we only need to run the node for side effects, not the
# assignment operator. if the result of the assignment is actually
# used anywhere, then it will be run as part of the normal graph.
return node_outputs
def build_step(self, signals):
J = signals.gather(self.J_data)
states = [signals.gather(x) for x in self.state_data]
states_dtype = [x.dtype for x in self.state_data]
# note: we need to make sure that the previous call to this function
# has completed before the next starts, since we don't know that the
# functions are thread safe
with tf.control_dependencies(self.prev_result), tf.device("/cpu:0"):
ret = tf.py_func(
self.neuron_step_math, [signals.dt, J] + states,
[self.output_data.dtype] + states_dtype,
name=self.neuron_step_math.__name__)
neuron_out, state_out = ret[0], ret[1:]
self.prev_result = [neuron_out]
neuron_out.set_shape(
self.output_data.shape + (signals.minibatch_size,))
signals.scatter(self.output_data, neuron_out)
for i, s in enumerate(self.state_data):
state_out[i].set_shape(s.shape + (signals.minibatch_size,))
signals.scatter(s, state_out[i])
def create_training(self, image_size = [151,151]):
"""Create the cost function and trainer"""
self.phi_input = tf.stop_gradient(tf.placeholder("float32", [None, image_size[0], image_size[1], 1]));
def cost(output, phi_in):
#return np.array([self.cost(o, phi_in) for o in output]);
return np.sum(self.cost_func(output, phi_in));
def cost_grad(op, grad):
#print op
output = op.inputs[0];
phi = op.inputs[1];
grad = tf.py_func(self.cost_func_grad, [output, phi], [tf.float32])[0];
#return [self.cost_func_grad(output, phi_in, epsilon = 0.01), np.zeros((phi_in.shape))];
return [grad, None];
self.cost_tf = py_func(cost, [self.output, self.phi_input], [tf.float32], grad = cost_grad)[0];
#self.cost_tf = tf.py_func(cost, [self.output, self.phi_input], [tf.float64])[0];
#self.phi = tf.py_func(phi_func, [self.output], [tf.float64]);
#self.cost = tf.reduce_mean(tf.squared_difference(self.phi_input, self.phi));
self.train_tf = tf.train.RMSPropOptimizer(0.00025,0.99,0.0,1e-6).minimize(self.cost_tf)
def __call__(self, inputs, state):
'''Step once given the input and return score and next state'''
cell_state, fst_states, state_probs, num_fst_states = state
cell_out, cell_state = self.dec_cell(inputs, cell_state)
def fst_costs_env(states, probs, num, inp):
'''Python function'''
return fst_costs(states, probs, num, inp, self.fst, self.max_states)
func_appl = tf.py_func(fst_costs_env,
[fst_states, state_probs, num_fst_states,
tf.argmax(inputs, 1)],
[tf.int32, tf.float32, tf.int32, tf.float32],
stateful=False)
next_state, next_state_probs, next_num_states, lm_scores = func_appl
next_state.set_shape(fst_states.shape)
next_num_states.set_shape(num_fst_states.shape)
next_state_probs.set_shape(state_probs.shape)
lm_scores.set_shape(cell_out.shape)
fin_score = tf.nn.log_softmax(
cell_out) + 0.5 * tf.nn.log_softmax(lm_scores)
return fin_score, (cell_state, next_state, next_state_probs, next_num_states)
def __init__(self, model_path, embedding_size, language, nlp):
# Step 1: restore the meta graph
with tf.Graph().as_default() as graph:
saver = tf.train.import_meta_graph(model_path + "model.ckpt.meta")
self.graph = graph
# get tensors for inputs and outputs by name
self.decoder_prediction = graph.get_tensor_by_name('decoder_prediction:0')
self.intent = graph.get_tensor_by_name('intent:0')
self.words_inputs = graph.get_tensor_by_name('words_inputs:0')
self.encoder_inputs_actual_length = graph.get_tensor_by_name('encoder_inputs_actual_length:0')
# redefine the py_func that is not serializable
def static_wrapper(words):
return spacy_wrapper(embedding_size, language, nlp, words)
after_py_func = tf.py_func(static_wrapper, [self.words_inputs], tf.float32, stateful=False)
# Step 2: restore weights
self.sess = tf.Session()
self.sess.run(tf.tables_initializer())
saver.restore(self.sess, model_path + "model.ckpt")
def anchor_target_layer(self, input, _feat_stride, anchor_scales, name):
if isinstance(input[0], tuple):
input[0] = input[0][0]
with tf.variable_scope(name) as scope:
# 'rpn_cls_score', 'gt_boxes', 'gt_ishard', 'dontcare_areas', 'im_info'
rpn_labels,rpn_bbox_targets,rpn_bbox_inside_weights,rpn_bbox_outside_weights = \
tf.py_func(anchor_target_layer_py,
[input[0],input[1],input[2],input[3],input[4], _feat_stride, anchor_scales],
[tf.float32,tf.float32,tf.float32,tf.float32])
rpn_labels = tf.convert_to_tensor(tf.cast(rpn_labels,tf.int32), name = 'rpn_labels') # shape is (1 x H x W x A, 2)
rpn_bbox_targets = tf.convert_to_tensor(rpn_bbox_targets, name = 'rpn_bbox_targets') # shape is (1 x H x W x A, 4)
rpn_bbox_inside_weights = tf.convert_to_tensor(rpn_bbox_inside_weights , name = 'rpn_bbox_inside_weights') # shape is (1 x H x W x A, 4)
rpn_bbox_outside_weights = tf.convert_to_tensor(rpn_bbox_outside_weights , name = 'rpn_bbox_outside_weights') # shape is (1 x H x W x A, 4)
return rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights
def proposal_target_layer(self, input, classes, name):
if isinstance(input[0], tuple):
input[0] = input[0][0]
with tf.variable_scope(name) as scope:
# inputs: 'rpn_rois','gt_boxes', 'gt_ishard', 'dontcare_areas'
rois,labels,bbox_targets,bbox_inside_weights,bbox_outside_weights \
= tf.py_func(proposal_target_layer_py,
[input[0],input[1],input[2],input[3],classes],
[tf.float32,tf.float32,tf.float32,tf.float32,tf.float32])
# rois <- (1 x H x W x A, 5) e.g. [0, x1, y1, x2, y2]
# rois = tf.convert_to_tensor(rois, name='rois')
rois = tf.reshape(rois, [-1, 5], name='rois') # goes to roi_pooling
labels = tf.convert_to_tensor(tf.cast(labels,tf.int32), name = 'labels') # goes to FRCNN loss
bbox_targets = tf.convert_to_tensor(bbox_targets, name = 'bbox_targets') # goes to FRCNN loss
bbox_inside_weights = tf.convert_to_tensor(bbox_inside_weights, name = 'bbox_inside_weights')
bbox_outside_weights = tf.convert_to_tensor(bbox_outside_weights, name = 'bbox_outside_weights')
self.layers['rois'] = rois
return rois, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights
def get_mu_tensor(self):
const_fact = self._dist_to_opt_avg**2 * self._h_min**2 / 2 / self._grad_var
coef = tf.Variable([-1.0, 3.0, 0.0, 1.0], dtype=tf.float32, name="cubic_solver_coef")
coef = tf.scatter_update(coef, tf.constant(2), -(3 + const_fact) )
roots = tf.py_func(np.roots, [coef], Tout=tf.complex64, stateful=False)
# filter out the correct root
root_idx = tf.logical_and(tf.logical_and(tf.greater(tf.real(roots), tf.constant(0.0) ),
tf.less(tf.real(roots), tf.constant(1.0) ) ), tf.less(tf.abs(tf.imag(roots) ), 1e-5) )
# in case there are two duplicated roots satisfying the above condition
root = tf.reshape(tf.gather(tf.gather(roots, tf.where(root_idx) ), tf.constant(0) ), shape=[] )
tf.assert_equal(tf.size(root), tf.constant(1) )
dr = self._h_max / self._h_min
mu = tf.maximum(tf.real(root)**2, ( (tf.sqrt(dr) - 1)/(tf.sqrt(dr) + 1) )**2)
return mu