class Logger(object): def __init__(self, vis_screen): self.viz = VisdomPlotter(env_name=vis_screen) self.hist_D = [] self.hist_G = [] self.hist_Dx = [] self.hist_DGx = [] def log_iteration_wgan(self, epoch, gen_iteration, d_loss, g_loss, real_loss, fake_loss): print( "Epoch: %d, Gen_iteration: %d, d_loss= %f, g_loss= %f, real_loss= %f, fake_loss = %f" % (epoch, gen_iteration, d_loss.data.cpu().mean(), g_loss.data.cpu().mean(), real_loss, fake_loss)) self.hist_D.append(d_loss.data.cpu().mean()) self.hist_G.append(g_loss.data.cpu().mean()) def log_iteration_gan(self, epoch, d_loss, g_loss, real_score, fake_score): print("Epoch: %d, d_loss= %f, g_loss= %f, D(X)= %f, D(G(X))= %f" % (epoch, d_loss.data.cpu().mean(), g_loss.data.cpu().mean(), real_score.data.cpu().mean(), fake_score.data.cpu().mean())) self.hist_D.append(d_loss.data.cpu().mean()) self.hist_G.append(g_loss.data.cpu().mean()) self.hist_Dx.append(real_score.data.cpu().mean()) self.hist_DGx.append(fake_score.data.cpu().mean()) def plot_epoch(self, epoch): self.viz.plot('Discriminator', 'train', epoch, np.array(self.hist_D).mean()) self.viz.plot('Generator', 'train', epoch, np.array(self.hist_G).mean()) self.hist_D = [] self.hist_G = [] def plot_epoch_w_scores(self, epoch): self.viz.plot('Discriminator', 'train', epoch, np.array(self.hist_D).mean()) self.viz.plot('Generator', 'train', epoch, np.array(self.hist_G).mean()) self.viz.plot('D(X)', 'train', epoch, np.array(self.hist_Dx).mean()) self.viz.plot('D(G(X))', 'train', epoch, np.array(self.hist_DGx).mean()) self.hist_D = [] self.hist_G = [] self.hist_Dx = [] self.hist_DGx = [] def draw(self, right_images, fake_images): self.viz.draw('generated images', fake_images.data.cpu().numpy()[:64] * 128 + 128) self.viz.draw('real images', right_images.data.cpu().numpy()[:64] * 128 + 128)
class Logger(object): def __init__(self, vis_screen): self.viz = VisdomPlotter(env_name=vis_screen) self.hist_D = [] self.hist_G = [] self.hist_Dx = [] self.hist_DGx = [] def log_iteration_wgan(self, epoch, gen_iteration, d_loss, g_loss, real_loss, fake_loss): print( "Epoch: %d, Gen_iteration: %d, d_loss= %f, g_loss= %f, real_loss= %f, fake_loss = %f" % (epoch, gen_iteration, d_loss.data.cpu().mean(), g_loss.data.cpu().mean(), real_loss, fake_loss)) self.hist_D.append(d_loss.data.cpu().mean()) self.hist_G.append(g_loss.data.cpu().mean()) def log_iteration_gan(self, epoch, d_loss, g_loss, real_score, fake_score): print("Epoch: %d, d_loss= %f, g_loss= %f, D(X)= %f, D(G(X))= %f" % (epoch, d_loss.data.cpu().mean(), g_loss.data.cpu().mean(), real_score.data.cpu().mean(), fake_score.data.cpu().mean())) self.hist_D.append(d_loss.data.cpu().mean()) self.hist_G.append(g_loss.data.cpu().mean()) self.hist_Dx.append(real_score.data.cpu().mean()) self.hist_DGx.append(fake_score.data.cpu().mean()) def plot_epoch(self, epoch): # self.viz.plot('Discriminator', 'train', epoch, np.array(self.hist_D).mean()) # self.viz.plot('Generator', 'train', epoch, np.array(self.hist_G).mean()) # self.hist_D = [] # self.hist_G = [] hist = {} hist['hist_D'] = self.hist_D hist['hist_G'] = self.hist_G np.save('training_hist.npy', hist) def plot_epoch_w_scores(self, epoch): self.viz.plot('Discriminator', 'train', epoch, np.array(self.hist_D).mean()) self.viz.plot('Generator', 'train', epoch, np.array(self.hist_G).mean()) self.viz.plot('D(X)', 'train', epoch, np.array(self.hist_Dx).mean()) self.viz.plot('D(G(X))', 'train', epoch, np.array(self.hist_DGx).mean()) self.hist_D = [] self.hist_G = [] self.hist_Dx = [] self.hist_DGx = [] def draw(self, right_images, fake_images, path='fake_image.jpg'): self.viz.draw('generated images', fake_images.data.cpu().numpy()[:64] * 128 + 128) self.viz.draw('real images', right_images.data.cpu().numpy()[:64] * 128 + 128) fake_images = fake_images.data.cpu().numpy()[:64] * 128 + 128 generated_images = fake_images num = generated_images.shape[0] height = int(np.math.sqrt(num)) width = int(np.math.ceil(float(num) / height)) shape = generated_images.shape[1:4] """ shape[0] - channels shape[1] - height shape[2] - width """ image = np.zeros((height * shape[1], width * shape[2], shape[0]), dtype=generated_images.dtype) for index, img in enumerate(generated_images): img = img.transpose(1, 2, 0) i = int(index / width) j = index % width image[i * shape[1]:(i + 1) * shape[1], j * shape[2]:(j + 1) * shape[2], :] = img[:, :, :] scipy.misc.imsave('Data/samples/' + path, image)
class Logger(object): def __init__(self, vis_screen, save_path): self.viz = VisdomPlotter(env_name=vis_screen) self.hist_D = [] self.hist_G = [] self.hist_Dx = [] self.hist_DGx = [] self.logger = logging.getLogger('lossesLogger') self.logFile = os.path.join('logs', save_path) if not os.path.exists(self.logFile): os.makedirs(self.logFile) handler = logging.FileHandler(self.logFile + '/logFile.log') handler.setLevel(logging.INFO) self.logger.addHandler(hdlr=handler) self.logger.setLevel(logging.INFO) def log_iteration_wgan(self, epoch, gen_iteration, d_loss, g_loss, real_loss, fake_loss): #print("Epoch type: {} Gen iter type: {} dloss_type:{} gloss_type: {}, realloss_type: {}, fakeloss_type: {}".format(type(epoch), type(gen_iteration), type(d_loss), type(g_loss), type(real_loss), type(fake_loss))) print( "Epoch: %d, Gen_iteration: %d, d_loss= %f, g_loss= %f, real_loss= %f, fake_loss = %f" % (epoch, gen_iteration, d_loss.data.cpu().mean(), g_loss.data.cpu().mean(), real_loss.data.cpu().numpy(), fake_loss.data.cpu().numpy())) self.hist_D.append(d_loss.data.cpu().mean()) self.hist_G.append(g_loss.data.cpu().mean()) self.logger.info( "Epoch: %d, Gen_iteration: %d, d_loss= %f, g_loss= %f, real_loss= %f, fake_loss = %f" % (epoch, gen_iteration, d_loss.data.cpu().mean(), g_loss.data.cpu().mean(), real_loss.data.cpu().numpy(), fake_loss.data.cpu().numpy())) def log_iteration_gan(self, epoch, d_loss, g_loss, real_score, fake_score): print("Epoch: %d, d_loss= %f, g_loss= %f, D(X)= %f, D(G(X))= %f" % (epoch, d_loss.data.cpu().mean(), g_loss.data.cpu().mean(), real_score.data.cpu().mean(), fake_score.data.cpu().mean())) self.hist_D.append(d_loss.data.cpu().mean()) self.hist_G.append(g_loss.data.cpu().mean()) self.hist_Dx.append(real_score.data.cpu().mean()) self.hist_DGx.append(fake_score.data.cpu().mean()) self.logger.info( "Epoch: %d, d_loss= %f, g_loss= %f, D(X)= %f, D(G(X))= %f" % (epoch, d_loss.data.cpu().mean(), g_loss.data.cpu().mean(), real_score.data.cpu().mean(), fake_score.data.cpu().mean())) def log_iteration_began(self, iteration, epoch, d_loss, g_loss, real_loss, fake_loss, lr): print( "Step: %d, Epochs: %d, Loss D: %.9f, real_loss: %.9f, fake_loss: %.9f, Loss G: %.9f, lr:%.9f" % (iteration, epoch, d_loss.data[0], real_loss.data[0], fake_loss.data[0], g_loss.data[0], lr)) self.hist_D.append(d_loss.data.cpu().mean()) self.hist_G.append(g_loss.data.cpu().mean()) self.logger.info( "Step: %d, Epochs: %d, Loss D: %.9f, real_loss: %.9f, fake_loss: %.9f, Loss G: %.9f, lr:%.9f" % (iteration, epoch, d_loss.data[0], real_loss.data[0], fake_loss.data[0], g_loss.data[0], lr)) def plot_epoch(self, epoch): self.viz.plot('Discriminator', 'train', epoch, np.array(self.hist_D).mean()) self.viz.plot('Generator', 'train', epoch, np.array(self.hist_G).mean()) self.hist_D = [] self.hist_G = [] def plot_epoch_w_scores(self, epoch): self.viz.plot('Discriminator', 'train', epoch, np.array(self.hist_D).mean()) self.viz.plot('Generator', 'train', epoch, np.array(self.hist_G).mean()) self.viz.plot('D(X)', 'train', epoch, np.array(self.hist_Dx).mean()) self.viz.plot('D(G(X))', 'train', epoch, np.array(self.hist_DGx).mean()) self.hist_D = [] self.hist_G = [] self.hist_Dx = [] self.hist_DGx = [] def draw(self, right_images, fake_images): self.viz.draw('generated images', fake_images.data.cpu().numpy()[:64] * 128 + 128) self.viz.draw('real images', right_images.data.cpu().numpy()[:64] * 128 + 128)
class Logger(object): def __init__(self, vis_screen): self.viz = VisdomPlotter(env_name=vis_screen) self.hist_D = [] self.hist_G = [] self.hist_Dx = [] self.hist_DGx = [] def log_iteration_wgan(self, epoch, gen_iteration, d_loss, g_loss, real_loss, fake_loss): print( "Epoch: %d, Gen_iteration: %d, d_loss= %f, g_loss= %f, real_loss= %f, fake_loss = %f" % (epoch, gen_iteration, d_loss.data.cpu().mean(), g_loss.data.cpu().mean(), real_loss, fake_loss)) self.hist_D.append(d_loss.data.cpu().mean()) self.hist_G.append(g_loss.data.cpu().mean()) def log_iteration_gan(self, epoch, d_loss, g_loss, real_score, fake_score): print("Epoch: %d, d_loss= %f, g_loss= %f, D(X)= %f, D(G(X))= %f" % (epoch, d_loss.data.cpu().mean(), g_loss.data.cpu().mean(), real_score.data.cpu().mean(), fake_score.data.cpu().mean())) self.hist_D.append(d_loss.data.cpu().mean()) self.hist_G.append(g_loss.data.cpu().mean()) self.hist_Dx.append(real_score.data.cpu().mean()) self.hist_DGx.append(fake_score.data.cpu().mean()) def plot_epoch(self, epoch): self.viz.plot('Discriminator', 'train', epoch, np.array(self.hist_D).mean()) self.viz.plot('Generator', 'train', epoch, np.array(self.hist_G).mean()) self.hist_D = [] self.hist_G = [] def plot_epoch_w_scores(self, epoch): self.viz.plot('Discriminator', 'train', epoch, np.array(self.hist_D).mean()) self.viz.plot('Generator', 'train', epoch, np.array(self.hist_G).mean()) self.viz.plot('D(X)', 'train', epoch, np.array(self.hist_Dx).mean()) self.viz.plot('D(G(X))', 'train', epoch, np.array(self.hist_DGx).mean()) self.hist_D = [] self.hist_G = [] self.hist_Dx = [] self.hist_DGx = [] def draw(self, right_images, fake_images): right_images = self.rescale(right_images) fake_images = self.rescale(fake_images) self.viz.draw('generated images', fake_images.data.cpu().numpy()[:16, :, :, :] * 255) self.viz.draw('real images', right_images.data.cpu().numpy()[:16, :, :, :] * 255) def rescale(self, images): images[:, 0, :, :] = images[:, 0, :, :] * 0.229 + 0.485 images[:, 1, :, :] = images[:, 1, :, :] * 0.224 + 0.456 images[:, 2, :, :] = images[:, 2, :, :] * 0.225 + 0.406 return images