tensorcv.utils package

Submodules

tensorcv.utils.common module

tensorcv.utils.common.apply_mask(input_matrix, mask)[source]

Get partition of input_matrix using index 1 in mask.

Parameters:
  • input_matrix (Tensor) – A Tensor

  • mask (int) – A Tensor of type int32 with indices in {0, 1}. Shape has to be the same as input_matrix.

Returns:

A Tensor with elements from data with entries in mask equal to 1.

tensorcv.utils.common.apply_mask_inverse(input_matrix, mask)[source]

Get partition of input_matrix using index 0 in mask.

Parameters:
  • input_matrix (Tensor) – A Tensor

  • mask (int) – A Tensor of type int32 with indices in {0, 1}. Shape has to be the same as input_matrix.

Returns:

A Tensor with elements from data with entries in mask equal to 0.

tensorcv.utils.common.get_tensors_by_names(names)[source]

Get a list of tensors by the input name list.

Parameters:names (str) – A str or a list of str
Returns:A list of tensors with name in input names.

Warning

If more than one tensor have the same name in the graph. This function will only return the tensor with name NAME:0.

tensorcv.utils.common.deconv_size(input_height, input_width, stride=2)[source]

Compute the feature size (height and width) after filtering with a specific stride. Mostly used for setting the shape for deconvolution.

Parameters:
  • input_height (int) – height of input feature

  • input_width (int) – width of input feature

  • stride (int) – stride of the filter

Returns:

(int, int) – Height and width of feature after filtering.

tensorcv.utils.common.match_tensor_save_name(tensor_names, save_names)[source]

Match tensor_names and corresponding save_names for saving the results of the tenors. If the number of tensors is less or equal to the length of save names, tensors will be saved using the corresponding names in save_names. Otherwise, tensors will be saved using their own names. Used for prediction or inference.

Parameters:
  • tensor_names (str) – List of tensor names

  • save_names (str) – List of names for saving tensors

Returns:

(list, list) – List of tensor names and list of names to save the tensors.

tensorcv.utils.default module

tensorcv.utils.default.get_default_session_config(memory_fraction=1)[source]

Default config of a TensorFlow session

Parameters:memory_fraction (float) – Memory fraction of GPU for this session
Returns:tf.ConfigProto() – Config of session.

tensorcv.utils.sesscreate module

class tensorcv.utils.sesscreate.NewSessionCreator(target='', graph=None, config=None)[source]

Bases: tensorflow.python.training.monitored_session.SessionCreator

tf.train.SessionCreator for a new session

__init__(target='', graph=None, config=None)[source]

Inits NewSessionCreator with targe, graph and config.

Parameters:
  • target – same as tf.Session.__init__().

  • graph – same as tf.Session.__init__().

  • config – same as tf.Session.__init__(). Default to utils.default.get_default_session_config().

create_session()[source]

Create session as well as initialize global and local variables

Returns:A tf.Session object containing nodes for all of the operations in the underlying TensorFlow graph.
class tensorcv.utils.sesscreate.ReuseSessionCreator(sess)[source]

Bases: tensorflow.python.training.monitored_session.SessionCreator

tf.train.SessionCreator for reuse an existed session

__init__(sess)[source]

Inits ReuseSessionCreator with an existed session.

Parameters:sess (tf.Session) – an existed tf.Session object
create_session()[source]

Create session by reusing an existing session

Returns:A reused tf.Session object containing nodes for all of the operations in the underlying TensorFlow graph.

tensorcv.utils.utils module

tensorcv.utils.utils.get_rng(obj=None)[source]

This function is copied from tensorpack. Get a good RNG seeded with time, pid and the object. :param obj: some object to use to generate random seed.

Returns:np.random.RandomState – the RNG.

tensorcv.utils.viz module

tensorcv.utils.viz.image_overlay(im_1, im_2, color=True, normalize=True)[source]

Overlay two images with the same size.

Parameters:
  • im_1 (np.ndarray) – image arrary

  • im_2 (np.ndarray) – image arrary

  • color (bool) – Whether convert intensity image to color image.

  • normalize (bool) – If both color and normalize are True, will normalize the intensity so that it has minimum 0 and maximum 1.

Returns:

np.ndarray – an overlay image of im_1*0.5 + im_2*0.5

tensorcv.utils.viz.intensity_to_rgb(intensity, cmap='jet', normalize=False)[source]

This function is copied from tensorpack. Convert a 1-channel matrix of intensities to an RGB image employing a colormap. This function requires matplotlib. See matplotlib colormaps for a list of available colormap.

Parameters:
  • intensity (np.ndarray) – array of intensities such as saliency.

  • cmap (str) – name of the colormap to use.

  • normalize (bool) – if True, will normalize the intensity so that it has minimum 0 and maximum 1.

Returns:

np.ndarray – an RGB float32 image in range [0, 255], a colored heatmap.

tensorcv.utils.viz.save_merge_images(images, merge_grid, save_path, color=False, tanh=False)[source]

Save multiple images with same size into one larger image.

The best size number is int(max(sqrt(image.shape[0]),sqrt(image.shape[1]))) + 1

Parameters:
  • images (np.ndarray) – A batch of image array to be merged with size [BATCH_SIZE, HEIGHT, WIDTH, CHANNEL].

  • merge_grid (list) – List of length 2. The grid size for merge images.

  • save_path (str) – Path for saving the merged image.

  • color (bool) – Whether convert intensity image to color image.

  • tanh (bool) – If True, will normalize the image in range [-1, 1] to [0, 1] (for GAN models).

Example

The batch_size is 64, then the size is recommended [8, 8]. The batch_size is 32, then the size is recommended [6, 6].

Module contents