def calculate_loss_distill_boost(self, predictions, labels_distill, labels, **unused_params):
with tf.name_scope("loss_distill_boost"):
print("loss_distill_boost")
epsilon = 10e-6
float_labels = tf.cast(labels, tf.float32)
batch_size = tf.shape(float_labels)[0]
float_labels_distill = tf.cast(labels_distill, tf.float32)
error = tf.negative(float_labels * tf.log(float_labels_distill + epsilon) + (
1 - float_labels) * tf.log(1 - float_labels_distill + epsilon))
error = tf.reduce_sum(error,axis=1,keep_dims=True)
alpha = error / tf.reduce_sum(error) * tf.cast(batch_size,dtype=tf.float32)
alpha = tf.clip_by_value(alpha, 0.5, 5)
alpha = alpha / tf.reduce_sum(alpha) * tf.cast(batch_size,dtype=tf.float32)
cross_entropy_loss = float_labels * tf.log(predictions + epsilon) + (
1 - float_labels) * tf.log(1 - predictions + epsilon)
cross_entropy_loss = tf.negative(cross_entropy_loss * alpha)
return tf.reduce_mean(tf.reduce_sum(cross_entropy_loss, 1))
python类clip_by_value()的实例源码
def get_optimizer(self, learning_rate = 0.001):
with tf.name_scope('loss'):
input_shape = tf.shape(self.inputs)
ones = tf.ones([input_shape[0], input_shape[1]])
loss = tf.contrib.seq2seq.sequence_loss(self.logits, self.targets,
ones)
#-----------------------------------------------------------------------
# Build the optimizer
#-----------------------------------------------------------------------
with tf.name_scope('optimizer'):
optimizer = tf.train.AdamOptimizer(learning_rate)
gradients = optimizer.compute_gradients(loss)
capped_gradients = [(tf.clip_by_value(grad, -1., 1.), var) \
for grad, var in gradients if grad is not None]
optimizer_op = optimizer.apply_gradients(capped_gradients)
return optimizer_op, loss
def _fc(self, x, fan_in, fan_out, layer_name, activation=None, L2=1, use_bias=True,
wmin=None,wmax=None,analysis=False):
show_weight = self.flags.visualize and 'weight' in self.flags.visualize
if wmin is not None or wmax is not None:
use_bias = False
assert wmin is not None and wmax is not None
with tf.variable_scope(layer_name.split('/')[-1]):
w,b = self._get_fc_weights(fan_in, fan_out, layer_name)
if wmin is not None:
wr = wmax-wmin
w = self._activate(w,'sigmoid')*wr+wmin
#w = tf.clip_by_value(w,wmin,wmax)
net = tf.matmul(x,w)
if use_bias:
net = tf.nn.bias_add(net, b)
net = self._activate(net, activation)
if show_weight:
tf.summary.histogram(name='W', values=w, collections=[tf.GraphKeys.WEIGHTS])
if use_bias:
tf.summary.histogram(name='bias', values=b, collections=[tf.GraphKeys.WEIGHTS])
if analysis:
net1 = tf.expand_dims(x,2)*tf.expand_dims(w,0)
#net1 = tf.reshape(net1,[tf.shape(x)[0],fan_in*fan_out])
return net,net1
return net
def get_trainer(cost, learning_rate=.001, grad_clips=(-1., 1.), logger=logger,
**kwargs):
"""Return opertation that trains parameters, given cost tensor."""
opt = tf.train.AdamOptimizer(learning_rate)
if grad_clips is None: return opt.minimize(cost, **kwargs)
grads_vars = []
for grad_var in opt.compute_gradients(cost, **kwargs):
if grad_var[0] is None:
if logger is not None:
logger.info('No gradient for variable {}', grad_var[1].name)
continue
grads_vars.append((tf.clip_by_value(grad_var[0], -1., 1.), grad_var[1]))
return opt.apply_gradients(grads_vars)
def recode_cost(self, inputs, variation, eps=1e-5, **kwargs):
"""
Cost for given input batch of samples, under current params.
"""
h = self.get_h_inputs(inputs)
z_mu = tf.matmul(h, self.params['Mhz']) + self.params['bMhz']
z_sig = tf.matmul(h, self.params['Shz']) + self.params['bShz']
# KL divergence between latent space induced by encoder and ...
lat_loss = -tf.reduce_sum(1 + z_sig - z_mu**2 - tf.exp(z_sig), 1)
z = z_mu + tf.sqrt(tf.exp(z_sig)) * variation
h = self.get_h_latents(z)
x_mu = self.decoding(tf.matmul(h, self.params['Mhx']) + self.params['bMhx'])
x_sig = self.decoding(tf.matmul(h, self.params['Shx']) + self.params['bShx'])
# x_sig = tf.clip_by_value(x_mu * (1 - x_mu), .05, 1)
# decoding likelihood term
like_loss = tf.reduce_sum(tf.log(x_sig + eps) +
(inputs - x_mu)**2 / x_sig, 1)
# # Mean cross entropy between input and encode-decoded input.
# like_loss = 2 * tf.reduce_sum(functions.cross_entropy(inputs, x_mu), 1)
return .5 * tf.reduce_mean(like_loss + lat_loss)
def compute_gradients(self, loss, *args, **kwargs):
train_vars = None
if self.trainable_names is not None:
log.info('All trainable vars:\n'+str([var.name for var in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)]))
train_vars = []
for scope_name in self.trainable_names:
new_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope_name)
if len(new_vars) == 0:
raise ValueError('The scope name, {}, you specified does not contain any trainable variables.'.format(scope_name))
train_vars.extend(new_vars)
log.info('Variables to be trained:\n'+str([var.name for var in train_vars]))
if train_vars is not None:
self.var_list = train_vars
gvs = self._optimizer.compute_gradients(loss,
var_list=train_vars,
*args, **kwargs)
if self.clip:
# gradient clipping. Some gradients returned are 'None' because
# no relation between the variable and loss; so we skip those.
gvs = [(tf.clip_by_value(grad, -1., 1.), var)
for grad, var in gvs if grad is not None]
return gvs
def adjust_saturation(image, saturation_factor, name=None):
with ops.op_scope([image], name, 'adjust_saturation') as name:
# Remember original dtype to so we can convert back if needed
orig_dtype = image.dtype
flt_image = tf.image.convert_image_dtype(image, tf.float32)
hsv = gen_image_ops.rgb_to_hsv(flt_image)
hue = tf.slice(hsv, [0, 0, 0, 0], [-1, -1, -1, 1])
saturation = tf.slice(hsv, [0, 0, 0, 1], [-1, -1, -1, 1])
value = tf.slice(hsv, [0, 0, 0, 2], [-1, -1, -1, 1])
saturation *= saturation_factor
saturation = clip_ops.clip_by_value(saturation, 0.0, 1.0)
hsv_altered = tf.concat(3, [hue, saturation, value])
rgb_altered = gen_image_ops.hsv_to_rgb(hsv_altered)
return tf.image.convert_image_dtype(rgb_altered, orig_dtype)
utils_combine.py 文件源码
项目:adversarial-deep-structural-networks
作者: wentaozhu
项目源码
文件源码
阅读 34
收藏 0
点赞 0
评论 0
def buildmodel(X, paras):
hconv1 = convlayer(X, paras['wconv1'], paras['bconv1'], flag='maxpool')
hconv2 = convlayer(hconv1, paras['wconv2'], paras['bconv2'], flag='maxpool')
hconv3 = tf.nn.conv2d(hconv2, paras['wconv3'], strides=[1,1,1,1], padding='VALID')
hconv3bias = tf.nn.bias_add(hconv3, paras['bconv3'])
hconv3tan = tf.nn.tanh(hconv3bias)
hconv4 = tf.nn.conv2d_transpose(hconv3tan, paras['wconv4'], [batchsize,boxheight,boxwidth,2],
strides=[1,1,1,1], padding='VALID')
hconv4 = tf.reshape(hconv4, [-1,boxheight*boxwidth*2])
hconv4bias = tf.nn.bias_add(hconv4, paras['bconv4'])
hconv4bias = tf.reshape(hconv4bias, [-1, boxheight, boxwidth, 2])
hconv4bias = tf.reshape(hconv4bias, [-1,2])
hconv4soft = tf.nn.softmax(hconv4bias)
hconv4clip = tf.clip_by_value(hconv4soft, 1e-6, 1.)
hconv4clip = (tf.reshape(hconv4clip, [-1, boxheight, boxwidth, 2]))
return hconv4clip
def buildmodel(X, paras):
hconv1 = convlayer(X, paras['wconv1'], paras['bconv1'], flag='maxpool')
hconv2 = convlayer(hconv1, paras['wconv2'], paras['bconv2'], flag='maxpool')
hconv3 = tf.nn.conv2d(hconv2, paras['wconv3'], strides=[1,1,1,1], padding='VALID')
hconv3bias = tf.nn.bias_add(hconv3, paras['bconv3'])
hconv3tan = tf.nn.tanh(hconv3bias)
hconv4 = tf.nn.conv2d_transpose(hconv3tan, paras['wconv4'], [batchsize,boxheight,boxwidth,2],
strides=[1,1,1,1], padding='VALID')
hconv4 = tf.reshape(hconv4, [-1,boxheight*boxwidth*2])
hconv4bias = tf.nn.bias_add(hconv4, paras['bconv4'])
hconv4bias = tf.reshape(hconv4bias, [-1, boxheight, boxwidth, 2])
hconv4bias = tf.reshape(hconv4bias, [-1,2])
hconv4soft = tf.nn.softmax(hconv4bias)
hconv4clip = tf.clip_by_value(hconv4soft, 1e-6, 1.)
hconv4clip = (tf.reshape(hconv4clip, [-1, boxheight, boxwidth, 2]))
return hconv4clip
def forward(self,z):
if not self.ar:
mu,log_sigma = self._get_mu_and_sigma(z)
else:
# permute z
z = tf.reshape(z,[-1]+[1]*self.hps.z_size)
perm = np.random.permutation(self.hps.z_size)+1
z = tf.transpose(z,np.append([0],perm))
z = tf.reshape(z,[-1,self.hps.z_size])
mu,log_sigma = ar_layer(z,self.hps,n_hidden=self.n_hidden)
log_sigma = tf.clip_by_value(log_sigma,-5,5)
if not self.hps.ignore_sigma_flow:
y = z * tf.exp(log_sigma) + mu
log_det = -1 * log_sigma
else:
y = z + mu
log_det = 0.0
return y,log_det
def pwlin_grid(r_,rvar_,theta_,dtheta = .75):
"""piecewise linear with noise-adaptive grid spacing.
returns xhat,dxdr
where
q = r/dtheta/sqrt(rvar)
xhat = r * interp(q,theta)
all but the last dimensions of theta must broadcast to r_
e.g. r.shape = (500,1000) is compatible with theta.shape=(500,1,7)
"""
ntheta = int(theta_.get_shape()[-1])
scale_ = dtheta / tf.sqrt(rvar_)
ars_ = tf.clip_by_value( tf.expand_dims( tf.abs(r_)*scale_,-1),0.0, ntheta-1.0 )
centers_ = tf.constant( np.arange(ntheta),dtype=tf.float32 )
outer_distance_ = tf.maximum(0., 1.0-tf.abs(ars_ - centers_) ) # new dimension for distance to closest bin centers (or center)
gain_ = tf.reduce_sum( theta_ * outer_distance_,axis=-1) # apply the gain (learnable)
xhat_ = gain_ * r_
dxdr_ = tf.gradients(xhat_,r_)[0]
return (xhat_,dxdr_)
def interp1d_(xin_,xp,yp_):
"""
Interpolate a uniformly sampled piecewise linear function. Mapping elements
from xin_ to the result. Input values will be clipped to range of xp.
xin_ : input tensor (real)
xp : x grid (constant -- must be a 1d numpy array, uniformly spaced)
yp_ : tensor of the result values at the gridpoints xp
"""
import tensorflow as tf
x_ = tf.clip_by_value(xin_,xp.min(),xp.max())
dx = xp[1]-xp[0]
assert len(xp.shape)==1,'only 1d interpolation'
assert xp.shape[0]==int(yp_.get_shape()[0])
assert abs(np.diff(xp)/dx - 1.0).max() < 1e-6,'must be uniformly sampled'
newshape = [ ]
x1_ = tf.expand_dims(x_,-1)
dt = yp_.dtype
wt_ = tf.maximum(tf.constant(0.,dtype=dt), 1-abs(x1_ - tf.constant(xp,dtype=dt))/dx )
y_ = tf.reduce_sum(wt_ * yp_,axis=-1)
return y_
def attack_single_step(self, x, eta, y):
"""
Given the original image and the perturbation computed so far, computes
a new perturbation.
:param x: A tensor with the original input.
:param eta: A tensor the same shape as x that holds the perturbation.
:param y: A tensor with the target labels or ground-truth labels.
"""
import tensorflow as tf
from cleverhans.utils_tf import model_loss, clip_eta
adv_x = x + eta
preds = self.model.get_probs(adv_x)
loss = model_loss(y, preds)
if self.targeted:
loss = -loss
grad, = tf.gradients(loss, adv_x)
scaled_signed_grad = self.eps_iter * tf.sign(grad)
adv_x = adv_x + scaled_signed_grad
if self.clip_min is not None and self.clip_max is not None:
adv_x = tf.clip_by_value(adv_x, self.clip_min, self.clip_max)
eta = adv_x - x
eta = clip_eta(eta, self.ord, self.eps)
return x, eta
def attack(self, x, y):
"""
This method creates a symbolic graph that given an input image,
first randomly perturbs the image. The
perturbation is bounded to an epsilon ball. Then multiple steps of
gradient descent is performed to increase the probability of a target
label or decrease the probability of the ground-truth label.
:param x: A tensor with the input image.
"""
import tensorflow as tf
from cleverhans.utils_tf import clip_eta
eta = tf.random_uniform(tf.shape(x), -self.eps, self.eps)
eta = clip_eta(eta, self.ord, self.eps)
for i in range(self.nb_iter):
x, eta = self.attack_single_step(x, eta, y)
adv_x = x + eta
if self.clip_min is not None and self.clip_max is not None:
adv_x = tf.clip_by_value(adv_x, self.clip_min, self.clip_max)
return adv_x
def expand_bboxes(xmin, xmax, ymin, ymax, cfg):
"""
Expand the bboxes.
"""
w = xmax - xmin
h = ymax - ymin
w = w * cfg.WIDTH_EXPANSION_FACTOR
h = h * cfg.HEIGHT_EXPANSION_FACTOR
half_w = w / 2.
half_h = h / 2.
xmin = tf.clip_by_value(xmin - half_w, 0, 1)
xmax = tf.clip_by_value(xmax + half_w, 0, 1)
ymin = tf.clip_by_value(ymin - half_h, 0, 1)
ymax = tf.clip_by_value(ymax + half_h, 0, 1)
return tf.tuple([xmin, xmax, ymin, ymax])
def demo(lr_image, hr_image):
model_sr = LapSRN(mode = 'demo')
hr_images_fake, residuals = model_sr.construct_net(lr_image, hr_image)
ckpt_path = tf.train.latest_checkpoint('checkpoint')
print(ckpt_path)
restorer = tf.train.Saver(tf.global_variables())
with tf.Session() as sess:
restorer.restore(sess, ckpt_path)
hr_image_fake_level_2 = hr_images_fake['hr_image_fake_level_1']+residuals['residual_level_1']
hr_image_fake_level_2 = tf.clip_by_value(hr_image_fake_level_2, 0, 1)
hr_image_fake_level_2 = sess.run(hr_image_fake_level_2)
hr_image_fake_level_2 = hr_image_fake_level_2.squeeze()
lr_image = sess.run(lr_image)
lr_image = lr_image.squeeze()
hr_image = sess.run(hr_image)
psnr_value = psnr(hr_image.squeeze(), hr_image_fake_level_2.squeeze())
print(psnr_value)
imshow(hr_image.squeeze())
imshow(hr_image_fake_level_2)
def demo(img_path):
lr_img, hr_img = imgread(img_path)
model = pix2pix_model(cfg)
model.test_model(lr_img, hr_img)
ckpt_path = tf.train.latest_checkpoint('checkpoint')
restorer = tf.train.Saver(tf.global_variables())
with tf.Session() as sess:
restorer.restore(sess, ckpt_path)
hr_image_fake = model.fake_hr_image
hr_image_fake = tf.clip_by_value(hr_image_fake, 0, 1)
hr_image_fake = sess.run(hr_image_fake)
hr_image_fake = hr_image_fake.squeeze()
hr_image = sess.run(hr_img)
psnr_value = psnr(hr_image.squeeze(), hr_image_fake.squeeze())
print(psnr_value)
imshow(hr_image_fake)
imshow(hr_image.squeeze())
model.py 文件源码
项目:tensorflow-action-conditional-video-prediction
作者: williamd4112
项目源码
文件源码
阅读 36
收藏 0
点赞 0
评论 0
def _create_optimizer(self):
lr = self.optimizer_args['lr'] if self.optimizer_args else 1e-4
with tf.variable_scope('optimize', reuse=not self.is_train) as scope:
# Setup global_step, optimizer
self.global_step = tf.get_variable('global_step', shape=(), initializer=tf.constant_initializer(0.0), trainable=False)
self.learning_rate = tf.train.exponential_decay(lr, self.global_step, 1e5, 0.9, staircase=True)
self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, name='optimizer')
# According to original paper code, learning rate of bias is 2x of base learning rate
grads_vars = self.optimizer.compute_gradients(self.loss)
bias_pattern = re.compile('.*/b')
grads_vars_mult = []
for grad, var in grads_vars:
if bias_pattern.match(var.op.name):
grads_vars_mult.append((grad * 2.0, var))
else:
grads_vars_mult.append((grad, var))
# According to original paper, gradient should be clipped with [-0.1, 0.1]
grads_clip = [(tf.clip_by_value(grad, -0.1, 0.1), var) for grad, var in grads_vars_mult]
self.train = self.optimizer.apply_gradients(grads_clip, global_step=self.global_step)
def _create_optimizer(self):
lr = self.optimizer_args['lr'] if self.optimizer_args else 1e-4
with tf.variable_scope('optimize', reuse=not self.is_train) as scope:
# Setup global_step, optimizer
self.global_step = tf.get_variable('global_step', shape=(), initializer=tf.constant_initializer(0.0), trainable=False)
self.learning_rate = tf.train.exponential_decay(lr, self.global_step, 1e5, 0.9, staircase=True)
self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, name='optimizer')
# According to original paper code, learning rate of bias is 2x of base learning rate
grads_vars = self.optimizer.compute_gradients(self.loss)
bias_pattern = re.compile('.*/b')
grads_vars_mult = []
for grad, var in grads_vars:
if bias_pattern.match(var.op.name):
grads_vars_mult.append((grad * 2.0, var))
else:
grads_vars_mult.append((grad, var))
# According to original paper, gradient should be clipped with [-0.1, 0.1]
grads_clip = [(tf.clip_by_value(grad, -0.1, 0.1), var) for grad, var in grads_vars_mult]
self.train = self.optimizer.apply_gradients(grads_clip, global_step=self.global_step)
def _create_optimizer(self):
lr = self.optimizer_args['lr'] if self.optimizer_args else 1e-4
with tf.variable_scope('optimize', reuse=not self.is_train) as scope:
# Setup global_step, optimizer
self.global_step = tf.get_variable('global_step', shape=(), initializer=tf.constant_initializer(0.0), trainable=False)
self.learning_rate = tf.train.exponential_decay(lr, self.global_step, 1e5, 0.9, staircase=True)
self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, name='optimizer')
# According to original paper code, learning rate of bias is 2x of base learning rate
grads_vars = self.optimizer.compute_gradients(self.loss)
bias_pattern = re.compile('.*/b')
grads_vars_mult = []
for grad, var in grads_vars:
if bias_pattern.match(var.op.name):
grads_vars_mult.append((grad * 2.0, var))
else:
grads_vars_mult.append((grad, var))
# According to original paper, gradient should be clipped with [-0.1, 0.1]
grads_clip = [(tf.clip_by_value(grad, -0.1, 0.1), var) for grad, var in grads_vars_mult]
self.train = self.optimizer.apply_gradients(grads_clip, global_step=self.global_step)
def train(self, optimizer,
training_set: Iterable[Tuple[QASetting, List[Answer]]],
batch_size: int, max_epochs=10, hooks=tuple(),
l2=0.0, clip=None, clip_op=tf.clip_by_value, summary_writer=None, **kwargs):
"""
This method trains the reader (and changes its state).
Args:
optimizer: TF optimizer
training_set: the training instances.
batch_size: size of training batches
max_epochs: maximum number of epochs
hooks: TrainingHook implementations that are called after epochs and batches
l2: whether to use l2 regularization
clip: whether to apply gradient clipping and at which value
clip_op: operation to perform for clipping
"""
batches, loss, min_op, summaries = self._setup_training(
batch_size, clip, optimizer, training_set, summary_writer, l2, clip_op, **kwargs)
self._train_loop(min_op, loss, batches, hooks, max_epochs, summaries, summary_writer, **kwargs)
def distance_biases(time_steps, window_size=10, reuse=False):
"""
Return a 2-d tensor with the values of the distance biases to be applied
on the intra-attention matrix of size sentence_size
Args:
time_steps: tensor scalar
window_size: window size
reuse: reuse variables
Returns:
2-d tensor (time_steps, time_steps)
"""
with tf.variable_scope('distance-bias', reuse=reuse):
# this is d_{i-j}
distance_bias = tf.get_variable('dist_bias', [window_size], initializer=tf.zeros_initializer())
r = tf.range(0, time_steps)
r_matrix = tf.tile(tf.reshape(r, [1, -1]), tf.stack([time_steps, 1]))
raw_idxs = r_matrix - tf.reshape(r, [-1, 1])
clipped_idxs = tf.clip_by_value(raw_idxs, 0, window_size - 1)
values = tf.nn.embedding_lookup(distance_bias, clipped_idxs)
return values
def relu(x, alpha=0., max_value=None):
'''Rectified linear unit
# Arguments
alpha: slope of negative section.
max_value: saturation threshold.
'''
if alpha != 0.:
negative_part = tf.nn.relu(-x)
x = tf.nn.relu(x)
if max_value is not None:
max_value = _to_tensor(max_value, x.dtype.base_dtype)
zero = _to_tensor(0., x.dtype.base_dtype)
x = tf.clip_by_value(x, zero, max_value)
if alpha != 0.:
alpha = _to_tensor(alpha, x.dtype.base_dtype)
x -= alpha * negative_part
return x
def categorical_crossentropy(output, target, from_logits=False):
'''Categorical crossentropy between an output tensor
and a target tensor, where the target is a tensor of the same
shape as the output.
'''
# Note: tf.nn.softmax_cross_entropy_with_logits
# expects logits, Keras expects probabilities.
if not from_logits:
# scale preds so that the class probas of each sample sum to 1
output /= tf.reduce_sum(output,
reduction_indices=len(output.get_shape()) - 1,
keep_dims=True)
# manual computation of crossentropy
epsilon = _to_tensor(_EPSILON, output.dtype.base_dtype)
output = tf.clip_by_value(output, epsilon, 1. - epsilon)
return - tf.reduce_sum(target * tf.log(output),
reduction_indices=len(output.get_shape()) - 1)
else:
return tf.nn.softmax_cross_entropy_with_logits(output, target)
def sparse_categorical_crossentropy(output, target, from_logits=False):
'''Categorical crossentropy between an output tensor
and a target tensor, where the target is an integer tensor.
'''
# Note: tf.nn.softmax_cross_entropy_with_logits
# expects logits, Keras expects probabilities.
if not from_logits:
epsilon = _to_tensor(_EPSILON, output.dtype.base_dtype)
output = tf.clip_by_value(output, epsilon, 1 - epsilon)
output = tf.log(output)
output_shape = output.get_shape()
res = tf.nn.sparse_softmax_cross_entropy_with_logits(
tf.reshape(output, [-1, int(output_shape[-1])]),
cast(flatten(target), 'int64'))
if len(output_shape) == 3:
# if our output includes timesteps we need to reshape
return tf.reshape(res, [-1, int(output_shape[-2])])
else:
return res
def extract_batch(dataset, config):
with tf.device("/cpu:0"):
bboxer = PriorBoxGrid(config)
data_provider = slim.dataset_data_provider.DatasetDataProvider(
dataset, num_readers=2,
common_queue_capacity=512, common_queue_min=32)
if args.segment:
im, bbox, gt, seg = data_provider.get(['image', 'object/bbox', 'object/label',
'image/segmentation'])
else:
im, bbox, gt = data_provider.get(['image', 'object/bbox', 'object/label'])
seg = tf.expand_dims(tf.zeros(tf.shape(im)[:2]), 2)
im = tf.to_float(im)/255
bbox = yxyx_to_xywh(tf.clip_by_value(bbox, 0.0, 1.0))
im, bbox, gt, seg = data_augmentation(im, bbox, gt, seg, config)
inds, cats, refine = bboxer.encode_gt_tf(bbox, gt)
return tf.train.shuffle_batch([im, inds, refine, cats, seg],
args.batch_size, 2048, 64, num_threads=4)
def _create(self):
d_loss = gan.graph.d_loss
g_loss = gan.graph.g_loss
g_lr = np.float32(config.g_learn_rate)
d_lr = np.float32(config.d_learn_rate)
gan.graph.d_vars = d_vars
g_defk = {k[2:]: v for k, v in config.items() if k[2:] in inspect.getargspec(config.g_trainer).args and k.startswith("d_")}
d_defk = {k[2:]: v for k, v in config.items() if k[2:] in inspect.getargspec(config.d_trainer).args and k.startswith("g_")}
g_optimizer = config.g_trainer(g_lr, **g_defk)
d_optimizer = config.d_trainer(d_lr, **d_defk)
if(config.clipped_gradients):
g_optimizer = capped_optimizer(g_optimizer, config.clipped_gradients, g_loss, g_vars)
d_optimizer = capped_optimizer(d_optimizer, config.clipped_gradients, d_loss, d_vars)
else:
g_optimizer = g_optimizer.minimize(g_loss, var_list=g_vars)
d_optimizer = d_optimizer.minimize(d_loss, var_list=d_vars)
gan.graph.clip = [tf.assign(d,tf.clip_by_value(d, -config.d_clipped_weights, config.d_clipped_weights)) for d in d_vars]
return g_optimizer, d_optimizer
def setup_critic_optimizer(self):
logger.info('setting up critic optimizer')
normalized_critic_target_tf = tf.clip_by_value(normalize(self.critic_target, self.ret_rms), self.return_range[0], self.return_range[1])
self.critic_loss = tf.reduce_mean(tf.square(self.normalized_critic_tf - normalized_critic_target_tf))
if self.critic_l2_reg > 0.:
critic_reg_vars = [var for var in self.critic.trainable_vars if 'kernel' in var.name and 'output' not in var.name]
for var in critic_reg_vars:
logger.info(' regularizing: {}'.format(var.name))
logger.info(' applying l2 regularization with {}'.format(self.critic_l2_reg))
critic_reg = tc.layers.apply_regularization(
tc.layers.l2_regularizer(self.critic_l2_reg),
weights_list=critic_reg_vars
)
self.critic_loss += critic_reg
critic_shapes = [var.get_shape().as_list() for var in self.critic.trainable_vars]
critic_nb_params = sum([reduce(lambda x, y: x * y, shape) for shape in critic_shapes])
logger.info(' critic shapes: {}'.format(critic_shapes))
logger.info(' critic params: {}'.format(critic_nb_params))
self.critic_grads = U.flatgrad(self.critic_loss, self.critic.trainable_vars, clip_norm=self.clip_norm)
self.critic_optimizer = MpiAdam(var_list=self.critic.trainable_vars,
beta1=0.9, beta2=0.999, epsilon=1e-08)
def clip(x, min_value, max_value):
"""Element-wise value clipping.
If min_value > max_value, clipping range is [min_value,min_value].
# Arguments
x: Tensor or variable.
min_value: Tensor, float, int, or None.
If min_value is None, defaults to -infinity.
max_value: Tensor, float, int, or None.
If max_value is None, defaults to infinity.
# Returns
A tensor.
"""
if max_value is None:
max_value = np.inf
if min_value is None:
min_value = -np.inf
min_value = _to_tensor(min_value, x.dtype.base_dtype)
max_value = _to_tensor(max_value, x.dtype.base_dtype)
max_value = tf.maximum(min_value, max_value)
return tf.clip_by_value(x, min_value, max_value)
def create_network(self,state_dim,action_dim,scope):
with tf.variable_scope(scope):
layer1_size = LAYER1_SIZE
layer2_size = LAYER2_SIZE
state_input = tf.placeholder("float",[None,state_dim])
W1 = self.variable([state_dim,layer1_size],state_dim)
b1 = self.variable([layer1_size],state_dim)
W2 = self.variable([layer1_size,layer2_size],layer1_size)
b2 = self.variable([layer2_size],layer1_size)
W3 = tf.Variable(tf.random_uniform([layer2_size,action_dim],-3e-3,3e-3))
b3 = tf.Variable(tf.random_uniform([action_dim],-3e-3,3e-3))
layer1 = tf.nn.relu(tf.matmul(state_input,W1) + b1)
layer2 = tf.nn.relu(tf.matmul(layer1,W2) + b2)
action_output = tf.clip_by_value(tf.nn.relu(tf.matmul(layer2,W3) + b3),0.0,1.0)
return state_input,action_output,[W1,b1,W2,b2,W3,b3]