Last active
June 20, 2019 15:35
-
-
Save AndrewBMartin/ab06f4708124ccb4cacc4b158c3cef12 to your computer and use it in GitHub Desktop.
Create a sprite for Tensorboard from an array of images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def create_sprite(data): | |
""" | |
Tile images into sprite image. | |
Add any necessary padding | |
""" | |
# For B&W or greyscale images | |
if len(data.shape) == 3: | |
data = np.tile(data[...,np.newaxis], (1,1,1,3)) | |
n = int(np.ceil(np.sqrt(data.shape[0]))) | |
padding = ((0, n ** 2 - data.shape[0]), (0, 0), (0, 0), (0, 0)) | |
data = np.pad(data, padding, mode='constant', | |
constant_values=0) | |
# Tile images into sprite | |
data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3, 4)) | |
# print(data.shape) => (n, image_height, n, image_width, 3) | |
data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:]) | |
# print(data.shape) => (n * image_height, n * image_width, 3) | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment