本文整理汇总了Python中tensorboardX.SummaryWriter.export_scalars_to_json方法的典型用法代码示例。如果您正苦于以下问题:Python SummaryWriter.export_scalars_to_json方法的具体用法?Python SummaryWriter.export_scalars_to_json怎么用?Python SummaryWriter.export_scalars_to_json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorboardX.SummaryWriter
的用法示例。
在下文中一共展示了SummaryWriter.export_scalars_to_json方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from tensorboardX import SummaryWriter [as 别名]
# 或者: from tensorboardX.SummaryWriter import export_scalars_to_json [as 别名]
class TBVisualizer:
def __init__(self, opt):
self._opt = opt
self._save_path = os.path.join(opt.checkpoints_dir, opt.name)
self._log_path = os.path.join(self._save_path, 'loss_log2.txt')
self._tb_path = os.path.join(self._save_path, 'summary.json')
self._writer = SummaryWriter(self._save_path)
with open(self._log_path, "a") as log_file:
now = time.strftime("%c")
log_file.write('================ Training Loss (%s) ================\n' % now)
def __del__(self):
self._writer.close()
def display_current_results(self, visuals, it, is_train, save_visuals=False):
for label, image_numpy in visuals.items():
sum_name = '{}/{}'.format('Train' if is_train else 'Test', label)
self._writer.add_image(sum_name, image_numpy, it)
if save_visuals:
util.save_image(image_numpy,
os.path.join(self._opt.checkpoints_dir, self._opt.name,
'event_imgs', sum_name, '%08d.png' % it))
self._writer.export_scalars_to_json(self._tb_path)
def plot_scalars(self, scalars, it, is_train):
for label, scalar in scalars.items():
sum_name = '{}/{}'.format('Train' if is_train else 'Test', label)
self._writer.add_scalar(sum_name, scalar, it)
def print_current_train_errors(self, epoch, i, iters_per_epoch, errors, t, visuals_were_stored):
log_time = time.strftime("[%d/%m/%Y %H:%M:%S]")
visuals_info = "v" if visuals_were_stored else ""
message = '%s (T%s, epoch: %d, it: %d/%d, t/smpl: %.3fs) ' % (log_time, visuals_info, epoch, i, iters_per_epoch, t)
for k, v in errors.items():
message += '%s:%.3f ' % (k, v)
print(message)
with open(self._log_path, "a") as log_file:
log_file.write('%s\n' % message)
def print_current_validate_errors(self, epoch, errors, t):
log_time = time.strftime("[%d/%m/%Y %H:%M:%S]")
message = '%s (V, epoch: %d, time_to_val: %ds) ' % (log_time, epoch, t)
for k, v in errors.items():
message += '%s:%.3f ' % (k, v)
print(message)
with open(self._log_path, "a") as log_file:
log_file.write('%s\n' % message)
def save_images(self, visuals):
for label, image_numpy in visuals.items():
image_name = '%s.png' % label
save_path = os.path.join(self._save_path, "samples", image_name)
util.save_image(image_numpy, save_path)
示例2: one_stage_train
# 需要导入模块: from tensorboardX import SummaryWriter [as 别名]
# 或者: from tensorboardX.SummaryWriter import export_scalars_to_json [as 别名]
def one_stage_train(myModel, data_reader_trn, my_optimizer,
loss_criterion, snapshot_dir, log_dir,
i_iter, start_epoch, best_val_accuracy=0, data_reader_eval=None,
scheduler=None):
report_interval = cfg.training_parameters.report_interval
snapshot_interval = cfg.training_parameters.snapshot_interval
max_iter = cfg.training_parameters.max_iter
avg_accuracy = 0
accuracy_decay = 0.99
best_epoch = 0
writer = SummaryWriter(log_dir)
best_iter = i_iter
iepoch = start_epoch
snapshot_timer = Timer('m')
report_timer = Timer('s')
while i_iter < max_iter:
iepoch += 1
for i, batch in enumerate(data_reader_trn):
i_iter += 1
if i_iter > max_iter:
break
scheduler.step(i_iter)
my_optimizer.zero_grad()
add_graph = False
scores, total_loss, n_sample = compute_a_batch(batch, myModel, eval_mode=False,
loss_criterion=loss_criterion,
add_graph=add_graph, log_dir=log_dir)
total_loss.backward()
accuracy = scores / n_sample
avg_accuracy += (1 - accuracy_decay) * (accuracy - avg_accuracy)
clip_gradients(myModel, i_iter, writer)
my_optimizer.step()
if i_iter % report_interval == 0:
save_a_report(i_iter, total_loss.data[0], accuracy, avg_accuracy, report_timer,
writer, data_reader_eval,myModel, loss_criterion)
if i_iter % snapshot_interval == 0 or i_iter == max_iter:
best_val_accuracy, best_epoch, best_iter = save_a_snapshot(snapshot_dir, i_iter, iepoch, myModel,
my_optimizer, loss_criterion, best_val_accuracy,
best_epoch, best_iter, snapshot_timer,
data_reader_eval)
writer.export_scalars_to_json(os.path.join(log_dir, "all_scalars.json"))
writer.close()
print("best_acc:%.6f after epoch: %d/%d at iter %d" % (best_val_accuracy, best_epoch, iepoch, best_iter))
sys.stdout.flush()
示例3: STGANAgent
# 需要导入模块: from tensorboardX import SummaryWriter [as 别名]
# 或者: from tensorboardX.SummaryWriter import export_scalars_to_json [as 别名]
#.........这里部分代码省略.........
self.optimizer_D.zero_grad()
d_loss.backward(retain_graph=True)
self.optimizer_D.step()
# summarize
scalars = {}
scalars['D/loss'] = d_loss.item()
scalars['D/loss_adv'] = d_loss_adv.item()
scalars['D/loss_cls'] = d_loss_cls.item()
scalars['D/loss_real'] = d_loss_real.item()
scalars['D/loss_fake'] = d_loss_fake.item()
scalars['D/loss_gp'] = d_loss_gp.item()
# =================================================================================== #
# 3. Train the generator #
# =================================================================================== #
if (i + 1) % self.config.n_critic == 0:
# original-to-target domain
x_fake = self.G(x_real, attr_diff)
out_src, out_cls = self.D(x_fake)
g_loss_adv = - torch.mean(out_src)
g_loss_cls = self.classification_loss(out_cls, label_trg)
# target-to-original domain
x_reconst = self.G(x_fake, c_org - c_org)
g_loss_rec = torch.mean(torch.abs(x_real - x_reconst))
# backward and optimize
g_loss = g_loss_adv + self.config.lambda3 * g_loss_rec + self.config.lambda2 * g_loss_cls
self.optimizer_G.zero_grad()
g_loss.backward()
self.optimizer_G.step()
# summarize
scalars['G/loss'] = g_loss.item()
scalars['G/loss_adv'] = g_loss_adv.item()
scalars['G/loss_cls'] = g_loss_cls.item()
scalars['G/loss_rec'] = g_loss_rec.item()
self.current_iteration += 1
# =================================================================================== #
# 4. Miscellaneous #
# =================================================================================== #
if self.current_iteration % self.config.summary_step == 0:
et = time.time() - start_time
et = str(datetime.timedelta(seconds=et))[:-7]
print('Elapsed [{}], Iteration [{}/{}]'.format(et, self.current_iteration, self.config.max_iters))
for tag, value in scalars.items():
self.writer.add_scalar(tag, value, self.current_iteration)
if self.current_iteration % self.config.sample_step == 0:
self.G.eval()
with torch.no_grad():
x_sample = x_sample.to(self.device)
x_fake_list = [x_sample]
for c_trg_sample in c_sample_list:
attr_diff = c_trg_sample.to(self.device) - c_org_sample.to(self.device)
attr_diff = attr_diff * self.config.thres_int
x_fake_list.append(self.G(x_sample, attr_diff.to(self.device)))
x_concat = torch.cat(x_fake_list, dim=3)
self.writer.add_image('sample', make_grid(self.denorm(x_concat.data.cpu()), nrow=1),
self.current_iteration)
save_image(self.denorm(x_concat.data.cpu()),
os.path.join(self.config.sample_dir, 'sample_{}.jpg'.format(self.current_iteration)),
nrow=1, padding=0)
if self.current_iteration % self.config.checkpoint_step == 0:
self.save_checkpoint()
self.lr_scheduler_G.step()
self.lr_scheduler_D.step()
def test(self):
self.load_checkpoint()
self.G.to(self.device)
tqdm_loader = tqdm(self.data_loader.test_loader, total=self.data_loader.test_iterations,
desc='Testing at checkpoint {}'.format(self.config.checkpoint))
self.G.eval()
with torch.no_grad():
for i, (x_real, c_org) in enumerate(tqdm_loader):
x_real = x_real.to(self.device)
c_trg_list = self.create_labels(c_org, self.config.attrs)
x_fake_list = [x_real]
for c_trg in c_trg_list:
attr_diff = c_trg - c_org
x_fake_list.append(self.G(x_real, attr_diff.to(self.device)))
x_concat = torch.cat(x_fake_list, dim=3)
result_path = os.path.join(self.config.result_dir, 'sample_{}.jpg'.format(i + 1))
save_image(self.denorm(x_concat.data.cpu()), result_path, nrow=1, padding=0)
def finalize(self):
print('Please wait while finalizing the operation.. Thank you')
self.writer.export_scalars_to_json(os.path.join(self.config.summary_dir, 'all_scalars.json'))
self.writer.close()