def generate(self, source, destination = None, samples = 20, grid_width=480, grid_height=240): # Load the input and output of the graph input = self.graph.get_tensor_by_name("en_input:0" ) output = self.graph.get_tensor_by_name("decoder/main_out:0") # For generating a single image for a source image if os.path.isfile(source): image = loader.load_image(source, self.image_size, self.image_size) batch = np.asarray([image for _ in range(self.batch_size)]) preds = self.session.run(output, feed_dict = {input: batch}) batch[0] = utils.add_border(batch[0],color = [1.0,0.0,0.0]) grid = np.concatenate((batch[0], preds[0]), axis=1) grid = (grid * 255.0).astype(np.uint8) # For generating a grid of images from a directory of images elif os.path.isdir(source): data = loader.DataSet(images_dir = source, hard_load = False, width = self.image_size, height = self.image_size) batch,_= data.next_batch(self.batch_size) preds = self.session.run(output, feed_dict={input: batch}) for i in range(samples): batch[i] = utils.add_border(batch[i],color = [1.0,0.0,0.0]) grid = self.construct_image_grid(batch,preds,samples,grid_width,grid_height) else: print(source,"must be an image pathname or a directory of images") sys.exit() if destination: cv2.imwrite(destination, grid) else: cv2.imshow("images", grid) cv2.waitKey()
def construct_image_grid(self, batch, preds, samples, grid_width, grid_height): for i in range(samples): batch[i] = utils.add_border(batch[i],color = [1.0,0.0,0.0]) N = samples >> 1 grid = [np.concatenate(tuple(batch[ :N] ), axis = 1), np.concatenate(tuple(preds[ :N] ), axis = 1), np.concatenate(tuple(batch[N:N << 1]), axis = 1), np.concatenate(tuple(preds[N:N << 1]), axis = 1)] grid = np.concatenate(tuple(grid), axis = 0) grid = cv2.resize(grid, (grid_width, grid_height), interpolation = cv2.INTER_AREA) grid = (grid * 255.0).astype(np.uint8) return grid
def plot_tsne_images(data_x, data_y, tx, ty, out_dir, DATASET, args, seed): width = 1333 height = 1000 max_dim = 250 colors = ['red', 'yellow', 'green', 'blue', 'orange', 'indianred']#, 'cyan', 'pink', 'orange', 'brown'] full_image = Image.new('RGB', (width, height), color=(255,255,255)) for idx, x in enumerate(data_x): tile = Image.fromarray(np.uint8(x * 255)) #tile = Image.open(img) rs = max(4, tile.width / max_dim, tile.height / max_dim) tile = tile.resize((int(tile.width / rs), int(tile.height / rs)), Image.ANTIALIAS) tile = add_border(tile, border=2, color=colors[data_y[idx]]) full_image.paste(tile, (int((width-max_dim) * tx[idx]), int((height-max_dim) * ty[idx]))) plt.figure(figsize = (8,6)) plt.imshow(full_image) full_image.save(os.path.join(out_dir, f"{DATASET}_tsne_images_{seed}.png"))