本文整理汇总了Python中model.config.cfg.RNG_SEED属性的典型用法代码示例。如果您正苦于以下问题:Python cfg.RNG_SEED属性的具体用法?Python cfg.RNG_SEED怎么用?Python cfg.RNG_SEED使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类model.config.cfg
的用法示例。
在下文中一共展示了cfg.RNG_SEED属性的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: construct_graph
# 需要导入模块: from model.config import cfg [as 别名]
# 或者: from model.config.cfg import RNG_SEED [as 别名]
def construct_graph(self):
# Set the random seed
torch.manual_seed(cfg.RNG_SEED)
# Build the main computation graph
self.net.create_architecture(self.imdb.num_classes, tag='default')
# Define the loss
# loss = layers['total_loss']
# Set learning rate and momentum
lr = cfg.TRAIN.LEARNING_RATE
params = []
for key, value in dict(self.net.named_parameters()).items():
if value.requires_grad:
if 'bias' in key:
params += [{'params':[value],'lr':lr*(cfg.TRAIN.DOUBLE_BIAS + 1), 'weight_decay': cfg.TRAIN.BIAS_DECAY and cfg.TRAIN.WEIGHT_DECAY or 0}]
else:
params += [{'params':[value],'lr':lr, 'weight_decay': cfg.TRAIN.WEIGHT_DECAY}]
self.optimizer = torch.optim.SGD(params, momentum=cfg.TRAIN.MOMENTUM)
# Write the train and validation information to tensorboard
self.writer = tb.writer.FileWriter(self.tbdir)
# self.valwriter = tb.writer.FileWriter(self.tbvaldir)
return lr, self.optimizer
开发者ID:Sunarker,项目名称:Collaborative-Learning-for-Weakly-Supervised-Object-Detection,代码行数:24,代码来源:train_val.py
示例2: construct_graph
# 需要导入模块: from model.config import cfg [as 别名]
# 或者: from model.config.cfg import RNG_SEED [as 别名]
def construct_graph(self):
# Set the random seed
torch.manual_seed(cfg.RNG_SEED)
# Build the main computation graph
self.net.create_architecture(self.imdb.num_classes, tag='default',
anchor_scales=cfg.ANCHOR_SCALES,
anchor_ratios=cfg.ANCHOR_RATIOS)
# Define the loss
# loss = layers['total_loss']
# Set learning rate and momentum
lr = cfg.TRAIN.LEARNING_RATE
params = []
for key, value in dict(self.net.named_parameters()).items():
if value.requires_grad:
if 'bias' in key:
params += [{'params':[value],'lr':lr*(cfg.TRAIN.DOUBLE_BIAS + 1), 'weight_decay': cfg.TRAIN.BIAS_DECAY and cfg.TRAIN.WEIGHT_DECAY or 0}]
else:
params += [{'params':[value],'lr':lr, 'weight_decay': cfg.TRAIN.WEIGHT_DECAY}]
self.optimizer = torch.optim.SGD(params, momentum=cfg.TRAIN.MOMENTUM)
# Write the train and validation information to tensorboard
self.writer = tb.writer.FileWriter(self.tbdir)
self.valwriter = tb.writer.FileWriter(self.tbvaldir)
return lr, self.optimizer
示例3: test_net
# 需要导入模块: from model.config import cfg [as 别名]
# 或者: from model.config.cfg import RNG_SEED [as 别名]
def test_net(sess, net, imdb, weights_filename, thresh=0.05):
np.random.seed(cfg.RNG_SEED)
"""Test a SSH network on an image database."""
num_images = len(imdb.image_index)
# all detections are collected into:
# all_boxes[cls][image] = N x 5 array of detections in
# (x1, y1, x2, y2, score)
all_boxes = [[] for _ in range(num_images)]
output_dir = get_output_dir(imdb, weights_filename)
# timers
_t = {'im_detect': Timer(), 'misc': Timer()}
for i in range(num_images):
im = cv2.imread(imdb.image_path_at(i))
_t['im_detect'].tic()
scores, boxes = im_detect(sess, net, im)
_t['im_detect'].toc()
_t['misc'].tic()
inds = np.where(scores[:, 0] > thresh)[0]
scores = scores[inds, 0]
boxes = boxes[inds, :]
dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
keep = nms(dets, cfg.TEST.NMS)
dets = dets[keep, :]
all_boxes[i] = det
_t['misc'].toc()
print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \
.format(i + 1, num_images, _t['im_detect'].average_time,
_t['misc'].average_time))
det_file = os.path.join(output_dir, 'detections.pkl')
with open(det_file, 'wb') as f:
pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)
print('Evaluating detections')
imdb.evaluate_detections(all_boxes, output_dir)
示例4: construct_graph
# 需要导入模块: from model.config import cfg [as 别名]
# 或者: from model.config.cfg import RNG_SEED [as 别名]
def construct_graph(self, sess):
with sess.graph.as_default():
# Set the random seed for tensorflow
tf.set_random_seed(cfg.RNG_SEED)
# Build the main computation graph
layers = self.net.create_architecture('TRAIN', self.imdb.num_classes, tag='default',
anchor_scales=cfg.ANCHOR_SCALES,
anchor_ratios=cfg.ANCHOR_RATIOS)
# Define the loss
loss = layers['total_loss']
# Set learning rate and momentum
lr = tf.Variable(cfg.TRAIN.LEARNING_RATE, trainable=False)
self.optimizer = tf.train.MomentumOptimizer(lr, cfg.TRAIN.MOMENTUM)
# Compute the gradients with regard to the loss
gvs = self.optimizer.compute_gradients(loss)
# Double the gradient of the bias if set
if cfg.TRAIN.DOUBLE_BIAS:
final_gvs = []
with tf.variable_scope('Gradient_Mult') as scope:
for grad, var in gvs:
scale = 1.
if cfg.TRAIN.DOUBLE_BIAS and '/biases:' in var.name:
scale *= 2.
if not np.allclose(scale, 1.0):
grad = tf.multiply(grad, scale)
final_gvs.append((grad, var))
train_op = self.optimizer.apply_gradients(final_gvs)
else:
train_op = self.optimizer.apply_gradients(gvs)
# We will handle the snapshots ourselves
self.saver = tf.train.Saver(max_to_keep=100000)
# Write the train and validation information to tensorboard
self.writer = tf.summary.FileWriter(self.tbdir, sess.graph)
self.valwriter = tf.summary.FileWriter(self.tbvaldir)
return lr, train_op
示例5: test_net_base
# 需要导入模块: from model.config import cfg [as 别名]
# 或者: from model.config.cfg import RNG_SEED [as 别名]
def test_net_base(sess, net, imdb, roidb, weights_filename, visualize=False):
np.random.seed(cfg.RNG_SEED)
"""Test a Fast R-CNN network on an image database."""
num_images = len(roidb)
output_dir = get_output_dir(imdb, weights_filename)
output_dir_image = os.path.join(output_dir, 'images')
if visualize and not os.path.exists(output_dir_image):
os.makedirs(output_dir_image)
all_scores = [[] for _ in range(num_images)]
# timers
_t = {'score' : Timer()}
for i in range(num_images):
_t['score'].tic()
all_scores[i], blobs = im_detect(sess, imdb, net, [roidb[i]])
_t['score'].toc()
print('score: {:d}/{:d} {:.3f}s' \
.format(i + 1, num_images, _t['score'].average_time))
if visualize and i % 10 == 0:
basename = os.path.basename(imdb.image_path_at(i)).split('.')[0]
im_vis, wrong = draw_predicted_boxes_test(blobs['data'], all_scores[i], blobs['gt_boxes'])
if wrong:
out_image = os.path.join(output_dir_image, basename + '.jpg')
print(out_image)
cv2.imwrite(out_image, im_vis)
res_file = os.path.join(output_dir, 'results.pkl')
with open(res_file, 'wb') as f:
pickle.dump(all_scores, f, pickle.HIGHEST_PROTOCOL)
print('Evaluating detections')
mcls_sc, mcls_ac, mcls_ap, mins_sc, mins_ac, mins_ap = imdb.evaluate(all_scores, output_dir)
eval_file = os.path.join(output_dir, 'results.txt')
with open(eval_file, 'w') as f:
f.write('{:.3f} {:.3f} {:.3f} {:.3f}'.format(mins_ap, mins_ac, mcls_ap, mcls_ac))
示例6: test_net_memory
# 需要导入模块: from model.config import cfg [as 别名]
# 或者: from model.config.cfg import RNG_SEED [as 别名]
def test_net_memory(sess, net, imdb, roidb, weights_filename, visualize=False, iter=0):
np.random.seed(cfg.RNG_SEED)
"""Test a Fast R-CNN network on an image database."""
num_images = len(roidb)
output_dir = get_output_dir(imdb, weights_filename + "_iter%02d" % iter)
output_dir_image = os.path.join(output_dir, 'images')
if visualize and not os.path.exists(output_dir_image):
os.makedirs(output_dir_image)
all_scores = [[] for _ in range(num_images)]
# timers
_t = {'score' : Timer()}
for i in range(num_images):
_t['score'].tic()
all_scores[i], blobs = im_detect_iter(sess, imdb, net, [roidb[i]], iter)
_t['score'].toc()
print('score: {:d}/{:d} {:.3f}s' \
.format(i + 1, num_images, _t['score'].average_time))
if visualize and i % 10 == 0:
basename = os.path.basename(imdb.image_path_at(i)).split('.')[0]
im_vis, wrong = draw_predicted_boxes_test(blobs['data'], all_scores[i], blobs['gt_boxes'])
if wrong:
out_image = os.path.join(output_dir_image, basename + '.jpg')
print(out_image)
cv2.imwrite(out_image, im_vis)
res_file = os.path.join(output_dir, 'results.pkl')
with open(res_file, 'wb') as f:
pickle.dump(all_scores, f, pickle.HIGHEST_PROTOCOL)
print('Evaluating detections')
mcls_sc, mcls_ac, mcls_ap, mins_sc, mins_ac, mins_ap = imdb.evaluate(all_scores, output_dir)
eval_file = os.path.join(output_dir, 'results.txt')
with open(eval_file, 'w') as f:
f.write('{:.3f} {:.3f} {:.3f} {:.3f}'.format(mins_ap, mins_ac, mcls_ap, mcls_ac))
示例7: test_net
# 需要导入模块: from model.config import cfg [as 别名]
# 或者: from model.config.cfg import RNG_SEED [as 别名]
def test_net(net, imdb, weights_filename, max_per_image=100, thresh=0.):
np.random.seed(cfg.RNG_SEED)
"""Test a Fast R-CNN network on an image database."""
num_images = len(imdb.image_index)
# all detections are collected into:
# all_boxes[cls][image] = N x 5 array of detections in
# (x1, y1, x2, y2, score)
all_boxes = [[[] for _ in range(num_images)]
for _ in range(imdb.num_classes)]
output_dir = get_output_dir(imdb, weights_filename)
# timers
_t = {'im_detect' : Timer(), 'misc' : Timer()}
for i in range(num_images):
im = cv2.imread(imdb.image_path_at(i))
_t['im_detect'].tic()
scores, boxes = im_detect(net, im)
_t['im_detect'].toc()
_t['misc'].tic()
# skip j = 0, because it's the background class
for j in range(1, imdb.num_classes):
inds = np.where(scores[:, j] > thresh)[0]
cls_scores = scores[inds, j]
cls_boxes = boxes[inds, j*4:(j+1)*4]
cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \
.astype(np.float32, copy=False)
keep = nms(torch.from_numpy(cls_dets), cfg.TEST.NMS).numpy() if cls_dets.size > 0 else []
cls_dets = cls_dets[keep, :]
all_boxes[j][i] = cls_dets
# Limit to max_per_image detections *over all classes*
if max_per_image > 0:
image_scores = np.hstack([all_boxes[j][i][:, -1]
for j in range(1, imdb.num_classes)])
if len(image_scores) > max_per_image:
image_thresh = np.sort(image_scores)[-max_per_image]
for j in range(1, imdb.num_classes):
keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
all_boxes[j][i] = all_boxes[j][i][keep, :]
_t['misc'].toc()
print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \
.format(i + 1, num_images, _t['im_detect'].average_time(),
_t['misc'].average_time()))
det_file = os.path.join(output_dir, 'detections.pkl')
with open(det_file, 'wb') as f:
pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)
print('Evaluating detections')
imdb.evaluate_detections(all_boxes, output_dir)
示例8: convert_from_depre
# 需要导入模块: from model.config import cfg [as 别名]
# 或者: from model.config.cfg import RNG_SEED [as 别名]
def convert_from_depre(net, imdb, input_dir, output_dir, snapshot, max_iters):
if not osp.exists(output_dir):
os.makedirs(output_dir)
tfconfig = tf.ConfigProto(allow_soft_placement=True)
tfconfig.gpu_options.allow_growth = True
sess = tf.Session(config=tfconfig)
num_classes = imdb.num_classes
with sess.graph.as_default():
tf.set_random_seed(cfg.RNG_SEED)
layers = net.create_architecture(sess, 'TRAIN', num_classes, tag='default',
anchor_scales=cfg.ANCHOR_SCALES,
anchor_ratios=cfg.ANCHOR_RATIOS)
loss = layers['total_loss']
# Learning rate should be reduced already
lr = tf.Variable(cfg.TRAIN.LEARNING_RATE * cfg.TRAIN.GAMMA, trainable=False)
momentum = cfg.TRAIN.MOMENTUM
optimizer = tf.train.MomentumOptimizer(lr, momentum)
gvs = optimizer.compute_gradients(loss)
if cfg.TRAIN.DOUBLE_BIAS:
final_gvs = []
with tf.variable_scope('Gradient_Mult') as scope:
for grad, var in gvs:
scale = 1.
if cfg.TRAIN.DOUBLE_BIAS and '/biases:' in var.name:
scale *= 2.
if not np.allclose(scale, 1.0):
grad = tf.multiply(grad, scale)
final_gvs.append((grad, var))
train_op = optimizer.apply_gradients(final_gvs)
else:
train_op = optimizer.apply_gradients(gvs)
checkpoint = osp.join(input_dir, snapshot + '.ckpt')
variables = tf.global_variables()
name2var = {convert_names(v.name): v for v in variables}
target_names = get_variables_in_checkpoint_file(checkpoint)
restorer = tf.train.Saver(name2var)
saver = tf.train.Saver()
print('Importing...')
restorer.restore(sess, checkpoint)
checkpoint = osp.join(output_dir, snapshot + '.ckpt')
print('Exporting...')
saver.save(sess, checkpoint)
# also copy the pkl file
index = osp.join(input_dir, snapshot + '.pkl')
outdex = osp.join(output_dir, snapshot + '.pkl')
shutil.copy(index, outdex)
sess.close()
示例9: construct_graph
# 需要导入模块: from model.config import cfg [as 别名]
# 或者: from model.config.cfg import RNG_SEED [as 别名]
def construct_graph(self, sess):
with sess.graph.as_default():
# Set the random seed for tensorflow
tf.set_random_seed(cfg.RNG_SEED)
# Build the main computation graph
layers = self.net.create_architecture('TRAIN', self.imdb.num_classes, tag='default',
anchor_width=cfg.CTPN.ANCHOR_WIDTH,
anchor_h_ratio_step=cfg.CTPN.H_RADIO_STEP,
num_anchors=cfg.CTPN.NUM_ANCHORS)
# Define the loss
total_loss = layers['total_loss']
# Set learning rate and momentum
lr = tf.Variable(cfg.TRAIN.LEARNING_RATE, trainable=False)
if cfg.TRAIN.OPTIMIZER == 'Adam':
self.optimizer = tf.train.AdamOptimizer(lr)
elif cfg.TRAIN.OPTIMIZER == 'Momentum':
self.optimizer = tf.train.MomentumOptimizer(lr, cfg.TRAIN.MOMENTUM)
elif cfg.TRAIN.OPTIMIZER == 'RMS':
self.optimizer = tf.train.RMSPropOptimizer(lr)
else:
raise NotImplementedError
global_step = tf.Variable(0, trainable=False)
with_clip = False
if with_clip:
tvars = tf.trainable_variables()
grads, norm = tf.clip_by_global_norm(tf.gradients(total_loss, tvars), 10.0)
train_op = self.optimizer.apply_gradients(list(zip(grads, tvars)), global_step=global_step)
else:
# required by tf.layers.batch_normalization()
# add update ops(for moving_mean and moving_variance) as a dependency to the train_op
# https://www.tensorflow.org/api_docs/python/tf/layers/batch_normalization
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
train_op = self.optimizer.minimize(total_loss, global_step=global_step)
# We will handle the snapshots ourselves
self.saver = tf.train.Saver(max_to_keep=100000)
# Write the train and validation information to tensorboard
self.writer = tf.summary.FileWriter(self.tbdir, sess.graph)
self.valwriter = tf.summary.FileWriter(self.tbvaldir)
return lr, train_op
示例10: test_net
# 需要导入模块: from model.config import cfg [as 别名]
# 或者: from model.config.cfg import RNG_SEED [as 别名]
def test_net(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.0):
np.random.seed(cfg.RNG_SEED)
"""Test a Fast R-CNN network on an image database."""
num_images = len(imdb.image_index)
# all detections are collected into:
# all_boxes[cls][image] = N x 5 array of detections in
# (x1, y1, x2, y2, score)
all_boxes = [[[] for _ in range(num_images)]
for _ in range(imdb.num_classes)]
output_dir = get_output_dir(imdb, weights_filename)
if os.path.isfile(os.path.join(output_dir, 'detections.pkl')):
all_boxes=pickle.load(open(os.path.join(output_dir, 'detections.pkl'),'r'))
else:
# timers
_t = {'im_detect' : Timer(), 'misc' : Timer()}
for i in range(num_images):
im = cv2.imread(imdb.image_path_at(i))
_t['im_detect'].tic()
scores, boxes,_ ,_ = im_detect(sess, net, im)
_t['im_detect'].toc()
_t['misc'].tic()
# skip j = 0, because it's the background class
for j in range(1, imdb.num_classes):
inds = np.where(scores[:, j] > thresh)[0]
cls_scores = scores[inds, j]
cls_boxes = boxes[inds, j*4:(j+1)*4]
cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \
.astype(np.float32, copy=False)
keep = nms(cls_dets, cfg.TEST.NMS)
cls_dets = cls_dets[keep, :]
all_boxes[j][i] = cls_dets
# Limit to max_per_image detections *over all classes*
if max_per_image > 0:
image_scores = np.hstack([all_boxes[j][i][:, -1]
for j in range(1, imdb.num_classes)])
if len(image_scores) > max_per_image:
image_thresh = np.sort(image_scores)[-max_per_image]
for j in range(1, imdb.num_classes):
keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
all_boxes[j][i] = all_boxes[j][i][keep, :]
_t['misc'].toc()
print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \
.format(i + 1, num_images, _t['im_detect'].average_time,
_t['misc'].average_time))
det_file = os.path.join(output_dir, 'detections_{:f}.pkl'.format(10))
with open(det_file, 'wb') as f:
pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)
print('Evaluating detections')
imdb.evaluate_detections(all_boxes, output_dir)
示例11: test_net
# 需要导入模块: from model.config import cfg [as 别名]
# 或者: from model.config.cfg import RNG_SEED [as 别名]
def test_net(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.):
np.random.seed(cfg.RNG_SEED)
"""Test a Fast R-CNN network on an image database."""
num_images = len(imdb.image_index)
# all detections are collected into:
# all_boxes[cls][image] = N x 5 array of detections in
# (x1, y1, x2, y2, score)
all_boxes = [[[] for _ in range(num_images)]
for _ in range(imdb.num_classes)]
output_dir = get_output_dir(imdb, weights_filename)
# timers
_t = {'im_detect' : Timer(), 'misc' : Timer()}
for i in range(num_images):
im = cv2.imread(imdb.image_path_at(i))
_t['im_detect'].tic()
scores, boxes = im_detect(sess, net, im)
_t['im_detect'].toc()
_t['misc'].tic()
# skip j = 0, because it's the background class
for j in range(1, imdb.num_classes):
inds = np.where(scores[:, j] > thresh)[0]
cls_scores = scores[inds, j]
cls_boxes = boxes[inds, j*4:(j+1)*4]
cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \
.astype(np.float32, copy=False)
keep = nms(cls_dets, cfg.TEST.NMS)
cls_dets = cls_dets[keep, :]
all_boxes[j][i] = cls_dets
# Limit to max_per_image detections *over all classes*
if max_per_image > 0:
image_scores = np.hstack([all_boxes[j][i][:, -1]
for j in range(1, imdb.num_classes)])
if len(image_scores) > max_per_image:
image_thresh = np.sort(image_scores)[-max_per_image]
for j in range(1, imdb.num_classes):
keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
all_boxes[j][i] = all_boxes[j][i][keep, :]
_t['misc'].toc()
print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \
.format(i + 1, num_images, _t['im_detect'].average_time,
_t['misc'].average_time))
det_file = os.path.join(output_dir, 'detections.pkl')
with open(det_file, 'wb') as f:
pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)
print('Evaluating detections')
imdb.evaluate_detections(all_boxes, output_dir)
示例12: construct_graph
# 需要导入模块: from model.config import cfg [as 别名]
# 或者: from model.config.cfg import RNG_SEED [as 别名]
def construct_graph(self, sess):
with sess.graph.as_default():
# Set the random seed for tensorflow
tf.set_random_seed(cfg.RNG_SEED)
# Build the main computation graph
layers = self.net.create_architecture('TRAIN', self.imdb.num_classes, tag='default')
# Define the loss
loss = layers['total_loss']
# Set learning rate and momentum
lr = tf.Variable(cfg.TRAIN.RATE, trainable=False)
self.optimizer = tf.train.MomentumOptimizer(lr, cfg.TRAIN.MOMENTUM)
# Compute the gradients with regard to the loss
gvs = self.optimizer.compute_gradients(loss)
grad_summaries = []
for grad, var in gvs:
if 'SMN' not in var.name and 'GMN' not in var.name:
continue
grad_summaries.append(tf.summary.histogram('TRAIN/' + var.name, var))
if grad is not None:
grad_summaries.append(tf.summary.histogram('GRAD/' + var.name, grad))
# Double the gradient of the bias if set
if cfg.TRAIN.DOUBLE_BIAS:
final_gvs = []
with tf.variable_scope('Gradient_Mult') as scope:
for grad, var in gvs:
scale = 1.
if cfg.TRAIN.DOUBLE_BIAS and '/biases:' in var.name:
scale *= 2.
if not np.allclose(scale, 1.0):
grad = tf.multiply(grad, scale)
final_gvs.append((grad, var))
train_op = self.optimizer.apply_gradients(final_gvs)
else:
train_op = self.optimizer.apply_gradients(gvs)
self.summary_grads = tf.summary.merge(grad_summaries)
# We will handle the snapshots ourselves
self.saver = tf.train.Saver(max_to_keep=100000)
# Write the train and validation information to tensorboard
self.writer = tf.summary.FileWriter(self.tbdir, sess.graph)
self.valwriter = tf.summary.FileWriter(self.tbvaldir)
return lr, train_op
示例13: construct_graph
# 需要导入模块: from model.config import cfg [as 别名]
# 或者: from model.config.cfg import RNG_SEED [as 别名]
def construct_graph(self, sess):
with sess.graph.as_default():
# Set the random seed for tensorflow
tf.set_random_seed(cfg.RNG_SEED)
# Build the main computation graph
layers = self.net.create_architecture('TRAIN', self.imdb.num_classes, tag='default')
# Define the loss
loss = layers['total_loss']
# Set learning rate and momentum
lr = tf.Variable(cfg.TRAIN.RATE, trainable=False)
self.optimizer = tf.train.MomentumOptimizer(lr, cfg.TRAIN.MOMENTUM)
# Compute the gradients with regard to the loss
gvs = self.optimizer.compute_gradients(loss)
grad_summaries = []
for grad, var in gvs:
if grad == None:
continue
grad_summaries.append(tf.summary.histogram('TRAIN/' + var.name, var))
grad_summaries.append(tf.summary.histogram('GRAD/' + var.name, grad))
# Double the gradient of the bias if set
if cfg.TRAIN.DOUBLE_BIAS:
final_gvs = []
with tf.variable_scope('Gradient_Mult') as scope:
for grad, var in gvs:
scale = 1.
if cfg.TRAIN.DOUBLE_BIAS and '/biases:' in var.name:
scale *= 2.
if not np.allclose(scale, 1.0):
grad = tf.multiply(grad, scale)
final_gvs.append((grad, var))
train_op = self.optimizer.apply_gradients(final_gvs)
else:
train_op = self.optimizer.apply_gradients(gvs)
self.summary_grads = tf.summary.merge(grad_summaries)
# We will handle the snapshots ourselves
self.saver = tf.train.Saver(max_to_keep=100000)
# Write the train and validation information to tensorboard
self.writer = tf.summary.FileWriter(self.tbdir, sess.graph)
self.valwriter = tf.summary.FileWriter(self.tbvaldir)
return lr, train_op
示例14: construct_graph
# 需要导入模块: from model.config import cfg [as 别名]
# 或者: from model.config.cfg import RNG_SEED [as 别名]
def construct_graph(self):
# Set the random seed
torch.manual_seed(cfg.RNG_SEED)
# Build the main computation graph
self.net.create_architecture(self.imdb.num_classes, tag='default',
anchor_scales=cfg.ANCHOR_SCALES,
anchor_ratios=cfg.ANCHOR_RATIOS)
# Define the loss
# loss = layers['total_loss']
# Set learning rate and momentum
lr = cfg.TRAIN.LEARNING_RATE
params = []
for key, value in dict(self.net.named_parameters()).items():
if value.requires_grad:
if 'mask' in key:
if 'bias' in key:
params += [{'params': [value], 'lr': 10*lr * (cfg.TRAIN.DOUBLE_BIAS + 1),
'weight_decay': cfg.TRAIN.BIAS_DECAY and cfg.TRAIN.WEIGHT_DECAY or 0}]
else:
params += [{'params': [value], 'lr': 10*lr, 'weight_decay': cfg.TRAIN.WEIGHT_DECAY}]
elif 'lightrcnn' in key:
if 'bias' in key:
params += [{'params': [value], 'lr': 10 * lr * (cfg.TRAIN.DOUBLE_BIAS + 1),
'weight_decay': cfg.TRAIN.BIAS_DECAY and cfg.TRAIN.WEIGHT_DECAY or 0}]
else:
params += [{'params': [value], 'lr': 10 * lr, 'weight_decay': cfg.TRAIN.WEIGHT_DECAY}]
else:
if 'bias' in key:
params += [{'params': [value], 'lr': lr * (cfg.TRAIN.DOUBLE_BIAS + 1),
'weight_decay': cfg.TRAIN.BIAS_DECAY and cfg.TRAIN.WEIGHT_DECAY or 0}]
else:
params += [{'params': [value], 'lr': lr, 'weight_decay': cfg.TRAIN.WEIGHT_DECAY}]
# if 'features' in key or 'classifier' in key or 'net' in key:
# if 'bias' in key:
# params += [{'params':[value],'lr':lr*(cfg.TRAIN.DOUBLE_BIAS + 1), 'weight_decay': cfg.TRAIN.BIAS_DECAY and cfg.TRAIN.WEIGHT_DECAY or 0}]
# else:
# params += [{'params':[value],'lr':lr, 'weight_decay': cfg.TRAIN.WEIGHT_DECAY}]
# elif 'dec_channel' in key or 'global' in key:
# if 'bias' in key:
# params += [{'params': [value], 'lr': lr * (cfg.TRAIN.DOUBLE_BIAS + 1)*10,
# 'weight_decay': cfg.TRAIN.BIAS_DECAY and cfg.TRAIN.WEIGHT_DECAY or 0}]
# else:
# params += [{'params': [value], 'lr': lr*10, 'weight_decay': cfg.TRAIN.WEIGHT_DECAY}]
self.optimizer = torch.optim.SGD(params, momentum=cfg.TRAIN.MOMENTUM)
# Write the train and validation information to tensorboard
self.writer = tb.writer.FileWriter(self.tbdir)
self.valwriter = tb.writer.FileWriter(self.tbvaldir)
return lr, self.optimizer