class LogROCCallback(object): """save roc graphs periodically in TensorBoard. write TensorBoard event file, holding the roc graph for every epoch logging_dir : str this function can only be executed after 'eval_metric.py', since that function is responsible for the graph creation where the tensorboard file will be created roc_path : list[str] list of paths to future roc's class_names : list[str] list of class names. """ def __init__(self, logging_dir=None, prefix='val', roc_path=None, class_names=None): self.prefix = prefix self.roc_path = roc_path self.class_names = class_names try: from tensorboard import SummaryWriter self.summary_writer = SummaryWriter(logging_dir) except ImportError: logging.error( 'You can install tensorboard via `pip install tensorboard`.') def __call__(self, param): """Callback to log ROC graph as an image in TensorBoard.""" for class_name in self.class_names: roc = os.path.join(self.roc_path, 'roc_' + class_name + '.png') if not os.path.exists(roc): continue im = scipy.misc.imread(roc) self.summary_writer.add_image(self.prefix + '_' + class_name, im)
class LogROCCallback(object): """save roc graphs periodically in TensorBoard. write TensorBoard event file, holding the roc graph for every epoch logging_dir : str this function can only be executed after 'eval_metric.py', since that function is responsible for the graph creation where the tensorboard file will be created roc_path : list[str] list of paths to future roc's class_names : list[str] list of class names. """ def __init__(self, logging_dir=None, prefix='val', roc_path=None, class_names=None): self.prefix = prefix self.roc_path = roc_path self.class_names = class_names try: from tensorboard import SummaryWriter self.summary_writer = SummaryWriter(logging_dir) except ImportError: logging.error('You can install tensorboard via `pip install tensorboard`.') def __call__(self, param): """Callback to log ROC graph as an image in TensorBoard.""" for class_name in self.class_names: roc = os.path.join(self.roc_path, 'roc_'+class_name+'.png') if not os.path.exists(roc): continue im = scipy.misc.imread(roc) self.summary_writer.add_image(self.prefix+'_'+class_name, im)
class LogDetectionsCallback(object): """ TODO complete """ def __init__(self, logging_dir=None, prefix='val', images_path=None, class_names=None, batch_size=None, mean_pixels=None, det_thresh=0.5): self.logging_dir = logging_dir self.prefix = prefix if not os.path.exists(images_path): os.mkdir(images_path) self.images_path = images_path self.class_names = class_names self.batch_size = batch_size self.mean_pixels = mean_pixels self.det_thresh = det_thresh try: from tensorboard import SummaryWriter self.summary_writer = SummaryWriter(logging_dir) except ImportError: logging.error( 'You can install tensorboard via `pip install tensorboard`.') def __call__(self, param): """Callback to log detections and gt-boxes as an image in TensorBoard.""" if param.locals is None: return result = [] pad = param.locals['eval_batch'].pad images = param.locals['eval_batch'].data[0][0:self.batch_size - pad].asnumpy() labels = param.locals['eval_batch'].label[0][0:self.batch_size - pad].asnumpy() outputs = [ out[0:out.shape[0] - pad] for out in param.locals['self'].get_outputs() ] # 'det' variable can be in different positions depending with train/test symbols if len(outputs) > 1: det_idx = [ idx for idx, f in enumerate(param.locals['self'].output_names) if f.startswith('det') ][0] detections = outputs[det_idx].asnumpy() else: detections = outputs[0].asnumpy() for i in range(detections.shape[0]): det = detections[i, :, :] det = det[np.where(det[:, 0] >= 0)[0]] label = labels[i, :, :] label = label[np.where(label[:, 0] >= 0)[0]] img = images[i, :, :, :] + np.reshape(self.mean_pixels, (3, 1, 1)) img = img.astype(np.uint8) img = img.transpose([1, 2, 0]) img[:, :, (0, 1, 2)] = img[:, :, (2, 1, 0)] self._visualize_detection_and_labels( img, det, label=label, classes=self.class_names, thresh=self.det_thresh, plt_path=os.path.join(self.images_path, 'image' + str(i) + '.png')) # save to tensorboard img_det_graph = scipy.misc.imread( os.path.join(self.images_path, 'image' + str(i) + '.png')) self.summary_writer.add_image('image' + str(i) + '.png', img_det_graph) return result def _visualize_detection_and_labels(self, img, dets, label, classes=[], thresh=None, plt_path=None): """ visualize detections in one image Parameters: ---------- img : numpy.array image, in bgr format dets : numpy.array ssd detections, numpy.array([[id, score, x1, y1, x2, y2]...]) each row is one object classes : tuple or list of str class names thresh : float score threshold """ fig = plt.figure() plt.imshow(img) height = img.shape[0] width = img.shape[1] colors = dict() # Visualize ground-truth boxes gt_color = (1.0, 0.0, 0.0) for i in range(label.shape[0]): cls_id = int(label[i, 0]) if cls_id >= 0: xmin = int(label[i, 1] * width) ymin = int(label[i, 2] * height) xmax = int(label[i, 3] * width) ymax = int(label[i, 4] * height) rect = plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, fill=False, edgecolor=gt_color, linewidth=2) plt.gca().add_patch(rect) class_name = str(cls_id) if classes and len(classes) > cls_id: class_name = classes[cls_id] plt.gca().text(xmin, ymin - 2, 'gt', bbox=dict(facecolor=gt_color, alpha=0.5), fontsize=8, color='white') # visualize predictions for i in range(dets.shape[0]): cls_id = int(dets[i, 0]) if cls_id >= 0: score = dets[i, 1] if score > thresh: if cls_id not in colors: colors[cls_id] = (random.random(), random.random(), random.random()) xmin = int(dets[i, 2] * width) ymin = int(dets[i, 3] * height) xmax = int(dets[i, 4] * width) ymax = int(dets[i, 5] * height) rect = plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, fill=False, edgecolor=colors[cls_id], linewidth=3.5) plt.gca().add_patch(rect) class_name = str(cls_id) if classes and len(classes) > cls_id: class_name = classes[cls_id] plt.gca().text(xmin, ymin - 2, '{:s} {:.3f}'.format(class_name, score), bbox=dict(facecolor=colors[cls_id], alpha=0.5), fontsize=8, color='white') plt.savefig(plt_path) plt.close(fig)
# LOGGING progress.update(progress.value + 1, loss_discriminator=loss_discriminator.data.cpu().numpy()[0], loss_generator=loss_generator.data.cpu().numpy()[0], accuracy_discriminator=accuracy_discriminator.data.cpu().numpy()[0], accuracy_generator=accuracy_generator.data.cpu().numpy()[0] ) if j % logging_step == 0: # LOSS ACCURACY writer.add_scalar('loss_discriminator', loss_discriminator.data[0], i * batch_number + j) writer.add_scalar('loss_generator', loss_generator.data[0], i * batch_number + j) writer.add_scalar('accuracy_discriminator', accuracy_discriminator.data[0], i * batch_number + j) writer.add_scalar('accuracy_generator', accuracy_generator.data[0], i * batch_number + j) # PARAMS for name, param in net.named_parameters(): writer.add_histogram(name, param.clone().cpu().data.numpy(), i * batch_number + j) # IMGS if j % logging_image_step == 0: net.train(False) # genero rumore data_batch = Variable(torch.randn(len(data_batch), 100), requires_grad=False).cuda() # calcolo uscita net(data_batch) for (name, imgs) in net.imgs.iteritems(): imgs = imgs.view(imgs.size()[0] * imgs.size()[1], 1, imgs.size()[2], imgs.size()[3]).cpu() grid = make_grid(imgs, nrow=10) writer.add_image(name, grid, i * batch_number + j) progress.finish()
import torch import torchvision.utils as vutils import numpy as np import torchvision.models as models from datetime import datetime from tensorboard import SummaryWriter resnet18 = models.resnet18(True) writer = SummaryWriter('runs/' + datetime.now().strftime('%B%d %H:%M:%S')) sample_rate = 44100 freqs = [262, 294, 330, 349, 392, 440, 440, 440, 440, 440, 440] for n_iter in range(100): M_global = torch.rand(1) # value to keep writer.add_scalar('M_global', M_global[0], n_iter) x = torch.rand(32, 3, 64, 64) # output from network if n_iter % 10 == 0: x = vutils.make_grid(x, normalize=True, scale_each=True) writer.add_image('Image', x, n_iter) x = torch.zeros(sample_rate * 2) for i in range(x.size(0)): x[i] = np.cos( freqs[n_iter // 10] * np.pi * float(i) / float(sample_rate)) # sound amplitude should in [-1, 1] writer.add_audio('Audio', x, n_iter) for name, param in resnet18.named_parameters(): writer.add_histogram(name, param.clone().cpu().data.numpy(), n_iter) writer.add_text('Text', 'text logged at step:' + str(n_iter), n_iter) writer.add_text('another Text', 'another text logged at step:' + str(n_iter), n_iter) writer.close()
print( '[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f D(x): %.4f D(G(z)): %.4f / %.4f' % (epoch, opt.niter, i, len(dataloader), errD.data[0], errG.data[0], D_x, D_G_z1, D_G_z2)) niter = epoch * len(dataloader) + i writer.add_scalar('Loss/D', errD.data[0], niter) writer.add_scalar('Loss/G', errG.data[0], niter) writer.add_scalar('D(x)', D_x, niter) writer.add_scalar('D(G(z1))', D_G_z1, niter) writer.add_scalar('D(G(z2))', D_G_z2, niter) if i % 100 == 0: vutils.save_image(real_cpu, '%s/real_samples.png' % opt.outf, normalize=True) writer.add_image('real_samples', vutils.make_grid(real_cpu, normalize=True), niter) fake = netG(fixed_noise) vutils.save_image(fake.data, '%s/fake_samples_epoch_%03d.png' % (opt.outf, epoch), normalize=True) writer.add_image('fake_samples', vutils.make_grid(fake.data, normalize=True), niter) # do checkpointing torch.save(netG.state_dict(), '%s/netG_epoch_%d.pth' % (opt.outf, epoch)) torch.save(netD.state_dict(), '%s/netD_epoch_%d.pth' % (opt.outf, epoch))
optimizer_feat = torch.optim.Adam(res101.parameters(), lr=1e-4) for t in range(10): for i, (img, label) in enumerate(loader): img = img.cuda() label = label[0].cuda() label = Variable(label) input = Variable(img) feats = res101(input) output = seg(feats) seg.zero_grad() res101.zero_grad() loss = criterion(output, label) loss.backward() optimizer_feat.step() optimizer_seg.step() ## see input = make_image_grid(img, mean, std) label = make_label_grid(label.data) label = Colorize()(label).type(torch.FloatTensor) output = make_label_grid(torch.max(output, dim=1)[1].data) output = Colorize()(output).type(torch.FloatTensor) writer.add_image('image', input, i) writer.add_image('label', label, i) writer.add_image('pred', output, i) writer.add_scalar('loss', loss.data[0], i) print "epoch %d step %d, loss=%.4f" % (t, i, loss.data.cpu()[0])
LD_LG = L_D_val-L_G_val log_variable(M_global, L_D_val, L_G_val, kt, LD_LG) if n_iter%10000==0: opt.lr = opt.lr/2 for param_group in optimD.param_groups: param_group['lr'] = opt.lr#param_group['lr']/2 for param_group in optimG.param_groups: param_group['lr'] = opt.lr#param_group['lr']/2 if n_iter%1000==1: writer.add_scalar('misc/learning', opt.lr, n_iter) print('dumping histogram') xG = netG(Variable(z).cuda()) reconstructed, z = netD(xR) x = torch.cat([vutils.make_grid(reconstructed.data.cpu()/2+0.5, normalize=True, scale_each=True), vutils.make_grid(reconstructed.data.cpu()/2+0.5, normalize=False, scale_each=False)], 2) writer.add_image('reconstructed real image', x, n_iter) reconstructed, z = netD(xG) x = torch.cat([vutils.make_grid(reconstructed.data.cpu()/2+0.5, normalize=True, scale_each=True), vutils.make_grid(reconstructed.data.cpu()/2+0.5, normalize=False, scale_each=False)], 2) writer.add_image('reconstructed fake image', x, n_iter) x = torch.cat([vutils.make_grid(xG.data.cpu()/2+0.5, normalize=True, scale_each=True), vutils.make_grid(xG.data.cpu()/2+0.5, normalize=False, scale_each=False)], 2) writer.add_image('generated fake image', x, n_iter) x = torch.cat([vutils.make_grid(netG(fixedNoise).data.cpu()/2+0.5, normalize=True, scale_each=True), vutils.make_grid(netG(fixedNoise).data.cpu()/2+0.5, normalize=False, scale_each=False)], 2) writer.add_image('generated fake image with fixed noise', x, n_iter) torch.save(netD, 'netD'+socket.gethostname()+'.pth') torch.save(netG, 'netG'+socket.gethostname()+'.pth') for name, param in netG.named_parameters(): if 'bn' in name: continue writer.add_histogram('weight_G/'+name, param.clone().cpu().data.numpy(), n_iter) writer.add_histogram('grad_G/'+name, param.grad.clone().cpu().data.numpy(), n_iter)
out_c = numpy.where(out_temp == index_color, color.reshape(3, 1, 1), out_c) outc_v.append(out_c) # LABELS labels_batch = torch.unsqueeze(labels_batch, 1) labels_batch = labels_batch.data.float().cpu().numpy() labels_batch = labels_batch / 12.0 * 255.0 label_v.append(labels_batch) if j % 9 == 0 and j != 0: outg_v = numpy.asarray(outg_v).squeeze(1) label_v = numpy.asarray(label_v).squeeze(1) outc_v = numpy.asarray(outc_v).squeeze(1) grid = make_grid(torch.FloatTensor(outc_v), nrow=int((len(outg_v))**0.5)) writer.add_image("out_test_color", grid, index_log) grid = make_grid(torch.FloatTensor(outg_v), nrow=int((len(outg_v))**0.5)) writer.add_image("out_test_gray", grid, index_log) grid = make_grid(torch.FloatTensor(label_v), nrow=int((len(outg_v))**0.5)) writer.add_image("label_test_gray", grid, index_log) index_log += 1 outg_v = [] label_v = [] outc_v = [] #writer.add_image("out_gray", grid, i * batch_number + j)
# re-parameterize std = logvar.mul(0.5).exp_() noise.resize_(bsize_now, nz).normal_(0, 1) output = decoder( (Variable(noise).mul(std).add_(mu)).view(bsize_now, nz, 1, 1)) loss = loss_function(output, Variable(input), mu, logvar, bsize, img_size) encoder.zero_grad() decoder.zero_grad() loss.backward() optimizer_de.step() optimizer_en.step() print 'epoch %d step %d, err_d=%.4f' % (epoch, i, loss.data[0]) if i % 100 == 0: # ########################## # # Visualization # ########################## images = make_grid(output.data[:8]) writer.add_image('output', images, i) images = make_grid(input[:8]) writer.add_image('images', images, i) writer.add_scalar('error', loss.data[0], i) del mu, logvar, std, output, loss gc.collect() torch.save(decoder.state_dict(), '%s/decoder-epoch-%d-step-%d.pth' % (check_root, epoch, i)) torch.save(encoder.state_dict(), '%s/encoder-epoch-%d-step-%d.pth' % (check_root, epoch, i))
err_d_fake.backward() optimizerD.step() err_d = err_d_fake + err_d_real ############################ # (2) Update G network: maximize log(D(G(z))) ########################### output = net_d(input_fake) output = F.sigmoid(output) label.fill_(1) err_g = criterion(output, Variable(label)) net_g.zero_grad() err_g.backward() optimizerG.step() if i % 100 == 0: ########################## # Visualization ########################## images = make_grid((input_fake.data[:8] + 1) / 2) writer.add_image('images', images, i) writer.add_scalar('error D', err_d.data[0], i) writer.add_scalar('error G', err_g.data[0], i) print 'epoch %d step %d, err_d=%.4f, err_g=%.4f' % ( epoch, i, err_d.data[0], err_g.data[0]) torch.save(net_g.state_dict(), '%s/NetG-epoch-%d-step-%d.pth' % (check_root, epoch, i)) torch.save(net_d.state_dict(), '%s/NetD-epoch-%d-step-%d.pth' % (check_root, epoch, i))
class LogDetectionsCallback(object): """ TODO complete """ def __init__(self, logging_dir=None, prefix='val', images_path=None, class_names=None, batch_size=None, mean_pixels=None, det_thresh=0.5): self.logging_dir = logging_dir self.prefix = prefix if not os.path.exists(images_path): os.mkdir(images_path) self.images_path = images_path self.class_names = class_names self.batch_size = batch_size self.mean_pixels = mean_pixels self.det_thresh = det_thresh try: from tensorboard import SummaryWriter self.summary_writer = SummaryWriter(logging_dir) except ImportError: logging.error('You can install tensorboard via `pip install tensorboard`.') def __call__(self, param): """Callback to log detections and gt-boxes as an image in TensorBoard.""" if param.locals is None: return result = [] pad = param.locals['eval_batch'].pad images = param.locals['eval_batch'].data[0][0:self.batch_size-pad].asnumpy() labels = param.locals['eval_batch'].label[0][0:self.batch_size - pad].asnumpy() outputs = [out[0:out.shape[0] - pad] for out in param.locals['self'].get_outputs()] # 'det' variable can be in different positions depending with train/test symbols if len(outputs) > 1: det_idx = [idx for idx,f in enumerate(param.locals['self'].output_names) if f.startswith('det')][0] detections = outputs[det_idx].asnumpy() else: detections = outputs[0].asnumpy() for i in range(detections.shape[0]): det = detections[i, :, :] det = det[np.where(det[:, 0] >= 0)[0]] label = labels[i,:,:] label = label[np.where(label[:, 0] >= 0)[0]] img = images[i,:,:,:] + np.reshape(self.mean_pixels, (3,1,1)) img = img.astype(np.uint8) img = img.transpose([1,2,0]) img[:, :, (0, 1, 2)] = img[:, :, (2, 1, 0)] self._visualize_detection_and_labels(img, det, label=label, classes=self.class_names, thresh=self.det_thresh, plt_path=os.path.join(self.images_path, 'image'+str(i)+'.png')) # save to tensorboard img_det_graph = scipy.misc.imread(os.path.join(self.images_path, 'image'+str(i)+'.png')) self.summary_writer.add_image('image'+str(i)+'.png', img_det_graph) return result def _visualize_detection_and_labels(self, img, dets, label, classes=[], thresh=None, plt_path=None): """ visualize detections in one image Parameters: ---------- img : numpy.array image, in bgr format dets : numpy.array ssd detections, numpy.array([[id, score, x1, y1, x2, y2]...]) each row is one object classes : tuple or list of str class names thresh : float score threshold """ fig = plt.figure() plt.imshow(img) height = img.shape[0] width = img.shape[1] colors = dict() # Visualize ground-truth boxes gt_color = (1.0, 0.0, 0.0) for i in range(label.shape[0]): cls_id = int(label[i, 0]) if cls_id >= 0: xmin = int(label[i, 1] * width) ymin = int(label[i, 2] * height) xmax = int(label[i, 3] * width) ymax = int(label[i, 4] * height) rect = plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, fill=False, edgecolor=gt_color, linewidth=2) plt.gca().add_patch(rect) class_name = str(cls_id) if classes and len(classes) > cls_id: class_name = classes[cls_id] plt.gca().text(xmin, ymin - 2, 'gt', bbox=dict(facecolor=gt_color, alpha=0.5), fontsize=8, color='white') # visualize predictions for i in range(dets.shape[0]): cls_id = int(dets[i, 0]) if cls_id >= 0: score = dets[i, 1] if score > thresh: if cls_id not in colors: colors[cls_id] = (random.random(), random.random(), random.random()) xmin = int(dets[i, 2] * width) ymin = int(dets[i, 3] * height) xmax = int(dets[i, 4] * width) ymax = int(dets[i, 5] * height) rect = plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, fill=False, edgecolor=colors[cls_id], linewidth=3.5) plt.gca().add_patch(rect) class_name = str(cls_id) if classes and len(classes) > cls_id: class_name = classes[cls_id] plt.gca().text(xmin, ymin - 2, '{:s} {:.3f}'.format(class_name, score), bbox=dict(facecolor=colors[cls_id], alpha=0.5), fontsize=8, color='white') plt.savefig(plt_path) plt.close(fig)
msk = deconv(feats) msk = functional.upsample(msk, scale_factor=4) prior = functional.sigmoid(msk) loss += criterion(msk, lbl) deconv.zero_grad() feature.zero_grad() loss.backward() optimizer_feature.step() optimizer_deconv.step() # visulize image = make_image_grid(inputs.data[:, :3], mean, std) writer.add_image('Image', torchvision.utils.make_grid(image), ib) msk = functional.sigmoid(msk) mask1 = msk.data mask1 = mask1.repeat(1, 3, 1, 1) writer.add_image('Image2', torchvision.utils.make_grid(mask1), ib) print('loss: %.4f (epoch: %d, step: %d)' % (loss.data[0], it, ib)) writer.add_scalar('M_global', loss.data[0], istep) istep += 1 del inputs, msk, lbl, loss, feats, mask1, image gc.collect() if ib % 1000 == 0: filename = ('%s/deconv-epoch-%d-step-%d.pth' % (check_root, it, ib)) torch.save(deconv.state_dict(), filename) filename = ('%s/feature-epoch-%d-step-%d.pth' %
class LogMetricsCallback(object): """Log metrics periodically in TensorBoard. This callback works almost same as `callback.Speedometer`, but write TensorBoard event file for visualization. For more usage, please refer https://github.com/dmlc/tensorboard Parameters ---------- logging_dir : str TensorBoard event file directory. After that, use `tensorboard --logdir=path/to/logs` to launch TensorBoard visualization. prefix : str Prefix for a metric name of `scalar` value. You might want to use this param to leverage TensorBoard plot feature, where TensorBoard plots different curves in one graph when they have same `name`. The follow example shows the usage(how to compare a train and eval metric in a same graph). Examples -------- >>> # log train and eval metrics under different directories. >>> training_log = 'logs/train' >>> evaluation_log = 'logs/eval' >>> # in this case, each training and evaluation metric pairs has same name, you can add a prefix >>> # to make it separate. >>> batch_end_callbacks = [mx.tensorboard.LogMetricsCallback(training_log)] >>> eval_end_callbacks = [mx.tensorboard.LogMetricsCallback(evaluation_log)] >>> # run >>> model.fit(train, >>> ... >>> batch_end_callback = batch_end_callbacks, >>> eval_end_callback = eval_end_callbacks) >>> # Then use `tensorboard --logdir=logs/` to launch TensorBoard visualization. """ def __init__(self, logging_dir, score_store=False, prefix=None): self.prefix = prefix self.step = 0 self.score_store = score_store try: self.summary_writer = SummaryWriter(logging_dir) except ImportError: logging.error( 'You can install tensorboard via `pip install tensorboard`.') def __call__(self, param): """Callback to log training speed and metrics in TensorBoard.""" self.step += 1 if param.eval_metric is None: return name_value = param.eval_metric.get_name_value() if self.step % 20 == 0: for name, value in name_value: if self.prefix is not None: name = '%s-%s' % (self.prefix, name) self.summary_writer.add_scalar(name, value, self.step) if self.step % 1000 == 0: im_ori = param.locals['data_batch'].label[0].asnumpy() im_rec = (param.locals['rec_img'])[0].asnumpy() im_ori = imageFromTensor(im_ori) im_rec = imageFromTensor(im_rec) self.summary_writer.add_image('im_ori', im_ori, self.step) self.summary_writer.add_image('im_rec', im_rec, self.step) if self.score_store: facenet_scores = param.locals['facenet_scores'] self.summary_writer.add_scalar('scores_mean', facenet_scores.mean(), self.step) self.summary_writer.add_histogram('facenet_scores', facenet_scores, self.step)
_, out = torch.max(out, 1) out = torch.unsqueeze(out, 1) out = out.data.float().cpu().numpy() # OUT out_g = out / 12.0 * 255.0 # OUT COLOR out_c = numpy.zeros((len(out), 3, 224, 224)) # replico 3 volte in out out_temp = numpy.repeat(out, 3, axis=1) for index_color, color in enumerate(dataset.labels): out_c = numpy.where(out_temp == index_color, numpy.asarray(color).reshape(3, 1, 1), out_c) # LABELS labels_batch = torch.unsqueeze(labels_batch, 1) labels_batch = labels_batch.data.float().cpu().numpy() labels_batch = labels_batch / 12.0 * 255.0 grid = make_grid(torch.FloatTensor(out_c), nrow=3) writer.add_image("out_test_color", grid, step_index) grid = make_grid(torch.FloatTensor(out_g), nrow=3) writer.add_image("out_test_gray", grid, step_index) grid = make_grid(torch.FloatTensor(labels_batch), nrow=3) writer.add_image("label_test_gray", grid, step_index) step_index += 1
def DCGAN(epoch, noise_size, batch_size, save_period, dataset): if dataset == 'MNIST': '''location of tensorboard save file''' logdir = 'tensorboard/MNIST/' summary_writer = SummaryWriter(logdir) train_iter, train_data_number = Mnist_Data_Processing(batch_size) #all elif dataset == 'CIFAR10': '''location of tensorboard save file''' logdir = 'tensorboard/CIFAR10/' summary_writer = SummaryWriter(logdir) train_iter, train_data_number = Image_Data_Processing( batch_size, "CIFAR10") #class by class elif dataset == 'ImageNet': '''location of tensorboard save file''' logdir = 'tensorboard/IMAGENET/' summary_writer = SummaryWriter(logdir) train_iter, train_data_number = Image_Data_Processing( batch_size, "ImageNet") #face else: print "no input data!!!" # No need, but must be declared. label = mx.nd.zeros((batch_size, )) '''Network''' generator = Generator() discriminator = Discriminator() context = mx.gpu(0) '''In the code below, the 'inputs_need_grad' parameter in the 'mod.bind' function is very important.''' # =============module G============= modG = mx.mod.Module(symbol=generator, data_names=['noise'], label_names=None, context=context) modG.bind(data_shapes=[('noise', (batch_size, noise_size, 1, 1))], label_shapes=None, for_training=True) if dataset == 'MNIST': try: # load the saved modG data modG.load_params("MNIST_Weights/modG-10.params") except: pass if dataset == 'CIFAR10': try: # load the saved modG data modG.load_params("CIFAR10_Weights/modG-300.params") except: pass if dataset == 'ImageNet': try: #pass # load the saved modG data modG.load_params("ImageNet_Weights/modG-1000.params") except: pass modG.init_params(initializer=mx.initializer.Normal(sigma=0.02)) modG.init_optimizer(optimizer='adam', optimizer_params={ 'learning_rate': 0.0002, 'beta1': 0.5 }) # =============module discriminator[0],discriminator[1]============= modD_0 = mx.mod.Module(symbol=discriminator[0], data_names=['data'], label_names=None, context=context) modD_0.bind(data_shapes=train_iter.provide_data, label_shapes=None, for_training=True, inputs_need_grad=True) if dataset == 'MNIST': try: # load the saved modG data modD_0.load_params("MNIST_Weights/modD_0-10.params") except: pass if dataset == 'CIFAR10': try: # load the saved modG data modD_0.load_params("CIFAR10_Weights/modD_0-200.params") except: pass if dataset == 'ImageNet': #pass try: # load the saved modG data modD_0.load_params("ImageNet_Weights/modD_0-1000.params") except: pass modD_0.init_params(initializer=mx.initializer.Normal(sigma=0.02)) modD_0.init_optimizer(optimizer='adam', optimizer_params={ 'learning_rate': 0.0002, 'beta1': 0.5 }) """ Parameters shared_module : Module Default is `None`. This is used in bucketing. When not `None`, the shared module essentially corresponds to a different bucket -- a module with different symbol but with the same sets of parameters (e.g. unrolled RNNs with different lengths). In here, for sharing the Discriminator parameters, we must to use shared_module=modD_0 """ modD_1 = mx.mod.Module(symbol=discriminator[1], data_names=['data'], label_names=None, context=context) modD_1.bind(data_shapes=train_iter.provide_data, label_shapes=None, for_training=True, inputs_need_grad=True, shared_module=modD_0) # =============generate image============= column_size = 10 row_size = 10 test_mod = mx.mod.Module(symbol=generator, data_names=['noise'], label_names=None, context=context) test_mod.bind(data_shapes=[ mx.io.DataDesc(name='noise', shape=(column_size * row_size, noise_size, 1, 1)) ], label_shapes=None, shared_module=modG, for_training=False, grad_req='null') '''############Although not required, the following code should be declared.#################''' '''make evaluation method 1 - Using existing ones. metrics = { 'acc': Accuracy, 'accuracy': Accuracy, 'ce': CrossEntropy, 'f1': F1, 'mae': MAE, 'mse': MSE, 'rmse': RMSE, 'top_k_accuracy': TopKAccuracy }''' metric = mx.metric.create(['acc', 'mse']) '''make evaluation method 2 - Making new things.''' ''' Custom evaluation metric that takes a NDArray function. Parameters: •feval (callable(label, pred)) – Customized evaluation function. •name (str, optional) – The name of the metric. •allow_extra_outputs (bool) – If true, the prediction outputs can have extra outputs. This is useful in RNN, where the states are also produced in outputs for forwarding. ''' def zero(label, pred): return 0 null = mx.metric.CustomMetric(zero) ####################################training loop############################################ # =============train=============== for epoch in xrange(1, epoch + 1, 1): Max_cost_0 = 0 Max_cost_1 = 0 Min_cost = 0 total_batch_number = np.ceil(train_data_number / (batch_size * 1.0)) train_iter.reset() for batch in train_iter: noise = mx.random.uniform(low=-1.0, high=1.0, shape=(batch_size, noise_size, 1, 1), ctx=context) modG.forward(data_batch=mx.io.DataBatch(data=[noise], label=None), is_train=True) modG_output = modG.get_outputs() ################################updating only parameters related to modD.######################################## # update discriminator on noise data '''MAX : modD_1 : cost : (-mx.symbol.log(1-discriminator2)) - noise data Discriminator update , bigger and bigger -> smaller and smaller discriminator2''' modD_1.forward(data_batch=mx.io.DataBatch(data=modG_output, label=None), is_train=True) '''Max_Cost of noise data Discriminator''' Max_cost_1 += modD_1.get_outputs()[0].asnumpy().astype(np.float32) modD_1.backward() modD_1.update() # updating discriminator on real data '''MAX : modD_0 : cost: (-mx.symbol.log(discriminator2)) real data Discriminator update , bigger and bigger discriminator2''' modD_0.forward(data_batch=batch, is_train=True) '''Max_Cost of real data Discriminator''' Max_cost_0 += modD_0.get_outputs()[0].asnumpy().astype(np.float32) modD_0.backward() modD_0.update() ################################updating only parameters related to modG.######################################## # update generator on noise data '''MIN : modD_0 : cost : (-mx.symbol.log(discriminator2)) - noise data Discriminator update , bigger and bigger discriminator2''' modD_0.forward(data_batch=mx.io.DataBatch(data=modG_output, label=None), is_train=True) modD_0.backward() '''Max_Cost of noise data Generator''' Min_cost += modD_0.get_outputs()[0].asnumpy().astype(np.float32) diff_v = modD_0.get_input_grads() modG.backward(diff_v) modG.update() '''tensorboard part''' Max_C = ((Max_cost_0 + Max_cost_1) / total_batch_number * 1.0).mean() Min_C = (Min_cost / total_batch_number * 1.0).mean() arg_params, aux_params = modG.get_params() #write scalar values summary_writer.add_scalar(name="Max_cost", scalar_value=Max_C, global_step=epoch) summary_writer.add_scalar(name="Min_cost", scalar_value=Min_C, global_step=epoch) #write matrix values summary_writer.add_histogram( name="g1_weight", values=arg_params["g1_weight"].asnumpy().ravel()) summary_writer.add_histogram( name="g2_weight", values=arg_params["g2_weight"].asnumpy().ravel()) summary_writer.add_histogram( name="g3_weight", values=arg_params["g3_weight"].asnumpy().ravel()) summary_writer.add_histogram( name="g4_weight", values=arg_params["g4_weight"].asnumpy().ravel()) summary_writer.add_histogram( name="g5_weight", values=arg_params["g5_weight"].asnumpy().ravel()) # cost print print "epoch : {}".format(epoch) print "Max Discriminator Cost : {}".format(Max_C) print "Min Generator Cost : {}".format(Min_C) #Save the data if epoch % save_period == 0: # write image values generate_image = modG_output[0][0].asnumpy() # only one image generate_image = (generate_image + 1.0) * 127.5 ''' Args: tag: A name for the generated node. Will also serve as a series name in TensorBoard. tensor: A 3-D `uint8` or `float32` `Tensor` of shape `[height, width, channels]` where `channels` is 1, 3, or 4. ''' generate_image = generate_image.astype( np.uint8 ) # only dtype uint8 , Only this is done...- Should be improved. summary_writer.add_image( tag='generate_image_epoch_{}'.format(epoch), img_tensor=generate_image.transpose(1, 2, 0)) print('Saving weights') if dataset == "MNIST": modG.save_params("MNIST_Weights/modG-{}.params".format(epoch)) modD_0.save_params( "MNIST_Weights/modD_0-{}.params".format(epoch)) elif dataset == "CIFAR10": modG.save_params( "CIFAR10_Weights/modG-{}.params".format(epoch)) modD_0.save_params( "CIFAR10_Weights/modD_0-{}.params".format(epoch)) elif dataset == 'ImageNet': modG.save_params( "ImageNet_Weights/modG-{}.params".format(epoch)) modD_0.save_params( "ImageNet_Weights/modD_0-{}.params".format(epoch)) '''test_method-2''' test = mx.random.uniform(low=-1.0, high=1.0, shape=(column_size * row_size, noise_size, 1, 1), ctx=context) test_mod.forward( data_batch=mx.io.DataBatch(data=[test], label=None)) result = test_mod.get_outputs()[0] result = result.asnumpy() '''range adjustment -1 ~ 1 -> 0 ~ 2 -> 0 ~1 -> 0 ~ 255 ''' # result = np.clip((result + 1.0) * (255.0 / 2.0), 0, 255).astype(np.uint8) result = ((result + 1.0) * 127.5).astype(np.uint8) '''Convert the image size to 4 times''' result = np.asarray([[ cv2.resize(i, None, fx=2, fy=2, interpolation=cv2.INTER_AREA) for i in im ] for im in result]) result = result.transpose((0, 2, 3, 1)) '''visualization''' fig, ax = plt.subplots(row_size, column_size, figsize=(column_size, row_size)) fig.suptitle('generator') for j in xrange(row_size): for i in xrange(column_size): ax[j][i].set_axis_off() if dataset == "MNIST": ax[j][i].imshow(result[i + j * column_size], cmap='gray') elif dataset == "CIFAR10": ax[j][i].imshow(result[i + j * column_size]) elif dataset == 'ImageNet': ax[j][i].imshow(result[i + j * column_size]) if dataset == "MNIST": fig.savefig( "Generate_Image/DCGAN_MNIST_Epoch_{}.png".format(epoch)) elif dataset == "CIFAR10": fig.savefig( "Generate_Image/DCGAN_CIFAR10_Epoch_{}.png".format(epoch)) elif dataset == 'ImageNet': fig.savefig( "Generate_Image/DCGAN_ImageNet_Epoch_{}.png".format(epoch)) plt.close(fig) print "Optimization complete." '''tensorboard_part''' summary_writer.close() #################################Generating Image#################################### '''load method1 - load the training mod.get_params() directly''' #arg_params, aux_params = mod.get_params() '''Annotate only when running test data. and Uncomment only if it is 'load method2' ''' #test_mod.set_params(arg_params=arg_params, aux_params=aux_params) '''test_method-1''' ''' noise = noise_iter.next() test_mod.forward(noise, is_train=False) result = test_mod.get_outputs()[0] result = result.asnumpy() print np.shape(result) ''' '''load method2 - using the shared_module''' """ Parameters shared_module : Module Default is `None`. This is used in bucketing. When not `None`, the shared module essentially corresponds to a different bucket -- a module with different symbol but with the same sets of parameters (e.g. unrolled RNNs with different lengths). """ '''test_method-2''' test = mx.random.uniform(low=-1.0, high=1.0, shape=(column_size * row_size, noise_size, 1, 1), ctx=context) test_mod.forward(data_batch=mx.io.DataBatch(data=[test], label=None)) result = test_mod.get_outputs()[0] result = result.asnumpy() '''range adjustment -1 ~ 1 -> 0 ~ 2 -> 0 ~1 -> 0 ~ 255 ''' #result = np.clip((result + 1.0) * (255.0 / 2.0), 0, 255).astype(np.uint8) result = ((result + 1.0) * 127.5).astype(np.uint8) '''Convert the image size to 4 times''' result = np.asarray([[ cv2.resize(i, None, fx=2, fy=2, interpolation=cv2.INTER_AREA) for i in im ] for im in result]) result = result.transpose((0, 2, 3, 1)) '''visualization''' fig, ax = plt.subplots(row_size, column_size, figsize=(column_size, row_size)) fig.suptitle('generator') for j in xrange(row_size): for i in xrange(column_size): ax[j][i].set_axis_off() if dataset == "MNIST": ax[j][i].imshow(result[i + j * column_size], cmap='gray') elif dataset == "CIFAR10": ax[j][i].imshow(result[i + j * column_size]) elif dataset == 'ImageNet': ax[j][i].imshow(result[i + j * column_size]) if dataset == "MNIST": fig.savefig("Generate_Image/DCGAN_MNIST_Final.png") elif dataset == "CIFAR10": fig.savefig("Generate_Image/DCGAN_CIFAR10_Final.png") elif dataset == 'ImageNet': fig.savefig("Generate_Image/DCGAN_ImageNet_Final.png") plt.show(fig)
input = Variable(data).cuda() output = pcnn(input) loss = criterion(F.log_softmax(output), Variable(ids[:, 0]).cuda()) pcnn.zero_grad() loss.backward() optimizer.step() if i % 100 == 0: # ########################## # # Visualization # ########################## _, temp = torch.max(output, 1) images = make_label_grid(temp.data.float().unsqueeze(1)[:8] / 255) writer.add_image('output', images, i) images = make_label_grid(data[:8]) writer.add_image('images', images, i) writer.add_scalar('error', loss.data[0], i) print 'epoch %d step %d, err_d=%.4f' % (epoch, i, loss.data[0]) if i % 1000 == 0: output = data.cuda() output[:, :, 14:, :] = 0 for j in range(14, 32): for k in range(32): temp = pcnn(Variable(output, volatile=True)) _, temp = torch.max(temp, 1) output[:, :, j, k] = temp.data.float().unsqueeze(1)[:, :, j, k] / 255 # ##########################