def y_real(self, xreal, y): # Run discriminator on real images yreal = self.discriminate(xreal, y) # Save images for later self._state_hooks['xreal'] = xreal self._state_hooks['real_images'] = format_images(xreal) return yreal
def save_images(self, source=False, real=False): # Generate images path = os.path.join(self.trainer.save_directory, 'generated_images') os.makedirs(path, exist_ok=True) # create directory if necessary self.image_count += 1 if(source): images = [Variable(self.y)] image_paths = [os.path.join(path, 'source.png'.format(self.image_count))] elif(real): images = [Variable(self.xreal)] image_paths = [os.path.join(path, 'real_target.png'.format(self.image_count))] else: images = self.generate() image_paths = [os.path.join(path, 'test_'+str(i)+'/target_generated{:08d}.png'.format(self.image_count)) for i in range(len(images))] for i in range(len(images)): image = images[i] # Reshape, scale, and cast the data so it can be saved grid = format_images(image).squeeze(0).permute(1, 2, 0) if grid.size(2) == 1: grid = grid.squeeze(2) array = grid.data.cpu().numpy() * 255. array = array.astype(np.uint8) # Save the image Image.fromarray(array).save(image_paths[i])
def generate(self, y): # Generate fake images from input xfake = self.generator(y) # Save images for later self._state_hooks['xfake'] = xfake self._state_hooks['y'] = y self._state_hooks['generated_images'] = format_images(xfake) # log the generated images return xfake
def save_images(self): # Generate images path = os.path.join(self.trainer.save_directory, 'generated_images') os.makedirs(path, exist_ok=True) # create directory if necessary image_path = os.path.join(path, '{:08d}.png'.format(self.image_count)) self.image_count += 1 generated = self.generate() # Reshape, scale, and cast the data so it can be saved grid = format_images(generated).squeeze(0).permute(1, 2, 0) if grid.size(2) == 1: grid = grid.squeeze(2) array = grid.data.cpu().numpy() * 255. array = array.astype(np.uint8) # Save the image Image.fromarray(array).save(image_path)