Exemplo n.º 1
0
def print_samples(images,
                  forward,
                  model_name,
                  epoch,
                  suffix='',
                  columns=1,
                  directory='plots'):
    """ 
	images is an 4D array of images: (index, channel, height, width)
	forward is a theano.function which maps images to outputs
	note: images can be tensor4 or np.ndarray
	"""

    where = directory + "/samples_" + model_name
    if (not os.path.exists(where)):
        os.makedirs(where)

    if (not isinstance(images, np.ndarray)):
        images = np.array(images.eval())

    # now images is np.ndarray
    n_images = images.shape[0]
    print "==> Printing %d images to %s" % (n_images, where)

    # we set random_greyscale flag false when showing greyscale images
    grey_images = get_greyscale(images, rgb=True)
    out_images = forward(images)

    all_images = np.array([]).reshape((0, ) + images.shape[1:])
    for index in xrange(n_images):
        all_images = np.concatenate([all_images, images[index:(index + 1)]],
                                    axis=0)
        all_images = np.concatenate(
            [all_images, grey_images[index:(index + 1)]], axis=0)
        all_images = np.concatenate(
            [all_images, out_images[index:(index + 1)]], axis=0)

    R = all_images[..., 0, ..., ...]
    G = all_images[..., 1, ..., ...]
    B = all_images[..., 2, ..., ...]

    all_images = tile_raster_images((R, G, B, None), (32, 32),
                                    (-(-len(images) // columns), 3 * columns),
                                    tile_spacing=(1, 1))
    image = Image.fromarray(all_images)

    image_name = "epoch %d" % epoch
    image.save(where + '/' + image_name + suffix + ".png")
Exemplo n.º 2
0
def print_samples(images, forward, model_name, epoch, suffix = '', columns = 1, directory = 'plots'):
	""" 
	images is an 4D array of images: (index, channel, height, width)
	forward is a theano.function which maps images to outputs
	note: images can be tensor4 or np.ndarray
	"""

	where = directory + "/samples_" + model_name
	if (not os.path.exists(where)):
		os.makedirs(where)
	
	if (not isinstance(images, np.ndarray)):
		images = np.array(images.eval())

	# now images is np.ndarray
	n_images = images.shape[0]
	print "==> Printing %d images to %s" % (n_images, where)
	
	# we set random_greyscale flag false when showing greyscale images
	grey_images = get_greyscale(images, rgb = True)
	out_images = forward(images)

	all_images = np.array([]).reshape((0,) + images.shape[1:])
	for index in xrange(n_images):
		all_images = np.concatenate([all_images, images[index:(index + 1)]], axis = 0)
		all_images = np.concatenate([all_images, grey_images[index:(index + 1)]], axis = 0)
		all_images = np.concatenate([all_images, out_images[index:(index + 1)]], axis = 0)
		
	R = all_images[..., 0, ..., ...]
	G = all_images[..., 1, ..., ...]
	B = all_images[..., 2, ..., ...]
	
	all_images = tile_raster_images(
		(R, G, B, None), 
		(32, 32), 
		(-(-len(images) // columns), 3 * columns),
		tile_spacing = (1, 1)
	)
	image = Image.fromarray(all_images)
	
	image_name = "epoch %d" % epoch
	image.save(where + '/' + image_name + suffix + ".png")
Exemplo n.º 3
0
def plot_filters(filters,
                 model_name,
                 epoch,
                 suffix='',
                 max_num_filters=100,
                 columns=1,
                 repeat=5,
                 directory='plots'):
    """ Plots filters of convolution layers
		
	filters is 4D nd.array (filter_index, channels, width, height), channels = 1 or 3
	one filters size = repeat * kernerl size
	"""

    where = directory + "/filters_" + model_name
    if (not os.path.exists(where)):
        os.makedirs(where)

    num_filters = min(filters.shape[0], max_num_filters)
    print "==> Printing %d images to %s" % (num_filters, where)

    filters = filters[0:num_filters]
    filters = filters.repeat(repeat, axis=2).repeat(repeat, axis=3)

    if (filters.shape[1] == 3):
        R = filters[..., 0, ..., ...]
        G = filters[..., 1, ..., ...]
        B = filters[..., 2, ..., ...]
    else:
        R = filters[..., 0, ..., ...]
        G = R
        B = R

    filters = tile_raster_images((R, G, B, None),
                                 filters.shape[2:],
                                 (-(-filters.shape[0] // columns), columns),
                                 tile_spacing=(2, 2))
    image = Image.fromarray(filters)

    image_name = "epoch %d" % epoch
    image.save(where + '/' + image_name + suffix + ".png")
Exemplo n.º 4
0
def plot_filters(filters, model_name, epoch, suffix = '', max_num_filters = 100, columns = 1, repeat = 5, directory = 'plots'):
	""" Plots filters of convolution layers
		
	filters is 4D nd.array (filter_index, channels, width, height), channels = 1 or 3
	one filters size = repeat * kernerl size
	"""
	
	where = directory + "/filters_" + model_name
	if (not os.path.exists(where)):
		os.makedirs(where)

	num_filters = min(filters.shape[0], max_num_filters)
	print "==> Printing %d images to %s" % (num_filters, where)
	
	filters = filters[0:num_filters]
	filters = filters.repeat(repeat, axis = 2).repeat(repeat, axis = 3)
	
	if (filters.shape[1] == 3):
		R = filters[..., 0, ..., ...]
		G = filters[..., 1, ..., ...]
		B = filters[..., 2, ..., ...]
	else:
		R = filters[..., 0, ..., ...]
		G = R
		B = R
	
	filters = tile_raster_images(
		(R, G, B, None), 
		filters.shape[2:], 
		(-(-filters.shape[0] // columns), columns),
		tile_spacing = (2, 2)
	)
	image = Image.fromarray(filters)
	
	image_name = "epoch %d" % epoch
	image.save(where + '/' + image_name + suffix + ".png")