def obj_box_crop(<EOL>im, classes=None, coords=None, wrg=<NUM_LIT:100>, hrg=<NUM_LIT:100>, is_rescale=False, is_center=False, is_random=False,<EOL>thresh_wh=<NUM_LIT>, thresh_wh2=<NUM_LIT><EOL>):

if classes is None:<EOL><INDENT>classes = []<EOL><DEDENT>if coords is None:<EOL><INDENT>coords = []<EOL><DEDENT>h, w = im.shape[<NUM_LIT:0>], im.shape[<NUM_LIT:1>]<EOL>if (h <= hrg) or (w <= wrg):<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>if is_random:<EOL><INDENT>h_offset = int(np.random.uniform(<NUM_LIT:0>, h - hrg) - <NUM_LIT:1>)<EOL>w_offset = int(np.random.uniform(<NUM_LIT:0>, w - wrg) - <NUM_LIT:1>)<EOL>h_end = hrg + h_offset<EOL>w_end = wrg + w_offset<EOL>im_new = im[h_offset:h_end, w_offset:w_end]<EOL><DEDENT>else:  <EOL><INDENT>h_offset = int(np.floor((h - hrg) / <NUM_LIT>))<EOL>w_offset = int(np.floor((w - wrg) / <NUM_LIT>))<EOL>h_end = h_offset + hrg<EOL>w_end = w_offset + wrg<EOL>im_new = im[h_offset:h_end, w_offset:w_end]<EOL><DEDENT>def _get_coord(coord):<EOL><INDENT>"""<STR_LIT>"""<EOL>if is_center:<EOL><INDENT>coord = obj_box_coord_centroid_to_upleft(coord)<EOL><DEDENT>x = coord[<NUM_LIT:0>] - w_offset<EOL>y = coord[<NUM_LIT:1>] - h_offset<EOL>w = coord[<NUM_LIT:2>]<EOL>h = coord[<NUM_LIT:3>]<EOL>if x < <NUM_LIT:0>:<EOL><INDENT>if x + w <= <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT>w = w + x<EOL>x = <NUM_LIT:0><EOL><DEDENT>elif x > im_new.shape[<NUM_LIT:1>]:  <EOL><INDENT>return None<EOL><DEDENT>if y < <NUM_LIT:0>:<EOL><INDENT>if y + h <= <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT>h = h + y<EOL>y = <NUM_LIT:0><EOL><DEDENT>elif y > im_new.shape[<NUM_LIT:0>]:  <EOL><INDENT>return None<EOL><DEDENT>if (x is not None) and (x + w > im_new.shape[<NUM_LIT:1>]):  <EOL><INDENT>w = im_new.shape[<NUM_LIT:1>] - x<EOL><DEDENT>if (y is not None) and (y + h > im_new.shape[<NUM_LIT:0>]):  <EOL><INDENT>h = im_new.shape[<NUM_LIT:0>] - y<EOL><DEDENT>if (w / (h + <NUM_LIT:1.>) > thresh_wh2) or (h / (w + <NUM_LIT:1.>) > thresh_wh2):  <EOL><INDENT>return None<EOL><DEDENT>if (w / (im_new.shape[<NUM_LIT:1>] * <NUM_LIT:1.>) < thresh_wh) or (h / (im_new.shape[<NUM_LIT:0>] * <NUM_LIT:1.>) <<EOL>thresh_wh):  <EOL><INDENT>return None<EOL><DEDENT>coord = [x, y, w, h]<EOL>if is_center:<EOL><INDENT>coord = obj_box_coord_upleft_to_centroid(coord)<EOL><DEDENT>return coord<EOL><DEDENT>coords_new = list()<EOL>classes_new = list()<EOL>for i, _ in enumerate(coords):<EOL><INDENT>coord = coords[i]<EOL>if len(coord) != <NUM_LIT:4>:<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>if is_rescale:<EOL><INDENT>coord = obj_box_coord_scale_to_pixelunit(coord, im.shape)<EOL>coord = _get_coord(coord)<EOL>if coord is not None:<EOL><INDENT>coord = obj_box_coord_rescale(coord, im_new.shape)<EOL>coords_new.append(coord)<EOL>classes_new.append(classes[i])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>coord = _get_coord(coord)<EOL>if coord is not None:<EOL><INDENT>coords_new.append(coord)<EOL>classes_new.append(classes[i])<EOL><DEDENT><DEDENT><DEDENT>return im_new, classes_new, coords_new<EOL>

Randomly or centrally crop an image, and compute the new bounding box coordinates.
    Objects outside the cropped image will be removed.

    Parameters
    -----------
    im : numpy.array
        An image with dimension of [row, col, channel] (default).
    classes : list of int or None
        Class IDs.
    coords : list of list of 4 int/float or None
        Coordinates [[x, y, w, h], [x, y, w, h], ...]
    wrg hrg and is_random : args
        See ``tl.prepro.crop``.
    is_rescale : boolean
        Set to True, if the input coordinates are rescaled to [0, 1]. Default is False.
    is_center : boolean, default False
        Set to True, if the x and y of coordinates are the centroid (i.e. darknet format). Default is False.
    thresh_wh : float
        Threshold, remove the box if its ratio of width(height) to image size less than the threshold.
    thresh_wh2 : float
        Threshold, remove the box if its ratio of width to height or vice verse higher than the threshold.

    Returns
    -------
    numpy.array
        A processed image
    list of int
        A list of classes
    list of list of 4 numbers
        A list of new bounding boxes.


def obj_box_shift(<EOL>im, classes=None, coords=None, wrg=<NUM_LIT:0.1>, hrg=<NUM_LIT:0.1>, row_index=<NUM_LIT:0>, col_index=<NUM_LIT:1>, channel_index=<NUM_LIT:2>, fill_mode='<STR_LIT>',<EOL>cval=<NUM_LIT:0.>, order=<NUM_LIT:1>, is_rescale=False, is_center=False, is_random=False, thresh_wh=<NUM_LIT>, thresh_wh2=<NUM_LIT><EOL>):

if classes is None:<EOL><INDENT>classes = []<EOL><DEDENT>if coords is None:<EOL><INDENT>coords = []<EOL><DEDENT>imh, imw = im.shape[row_index], im.shape[col_index]<EOL>if (hrg >= <NUM_LIT:1.0>) and (hrg <= <NUM_LIT:0.>) and (wrg >= <NUM_LIT:1.0>) and (wrg <= <NUM_LIT:0.>):<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>if is_random:<EOL><INDENT>tx = np.random.uniform(-hrg, hrg) * imh<EOL>ty = np.random.uniform(-wrg, wrg) * imw<EOL><DEDENT>else:<EOL><INDENT>tx, ty = hrg * imh, wrg * imw<EOL><DEDENT>translation_matrix = np.array([[<NUM_LIT:1>, <NUM_LIT:0>, tx], [<NUM_LIT:0>, <NUM_LIT:1>, ty], [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:1>]])<EOL>transform_matrix = translation_matrix  <EOL>im_new = affine_transform(im, transform_matrix, channel_index, fill_mode, cval, order)<EOL>def _get_coord(coord):<EOL><INDENT>"""<STR_LIT>"""<EOL>if is_center:<EOL><INDENT>coord = obj_box_coord_centroid_to_upleft(coord)<EOL><DEDENT>x = coord[<NUM_LIT:0>] - ty  <EOL>y = coord[<NUM_LIT:1>] - tx  <EOL>w = coord[<NUM_LIT:2>]<EOL>h = coord[<NUM_LIT:3>]<EOL>if x < <NUM_LIT:0>:<EOL><INDENT>if x + w <= <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT>w = w + x<EOL>x = <NUM_LIT:0><EOL><DEDENT>elif x > im_new.shape[<NUM_LIT:1>]:  <EOL><INDENT>return None<EOL><DEDENT>if y < <NUM_LIT:0>:<EOL><INDENT>if y + h <= <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT>h = h + y<EOL>y = <NUM_LIT:0><EOL><DEDENT>elif y > im_new.shape[<NUM_LIT:0>]:  <EOL><INDENT>return None<EOL><DEDENT>if (x is not None) and (x + w > im_new.shape[<NUM_LIT:1>]):  <EOL><INDENT>w = im_new.shape[<NUM_LIT:1>] - x<EOL><DEDENT>if (y is not None) and (y + h > im_new.shape[<NUM_LIT:0>]):  <EOL><INDENT>h = im_new.shape[<NUM_LIT:0>] - y<EOL><DEDENT>if (w / (h + <NUM_LIT:1.>) > thresh_wh2) or (h / (w + <NUM_LIT:1.>) > thresh_wh2):  <EOL><INDENT>return None<EOL><DEDENT>if (w / (im_new.shape[<NUM_LIT:1>] * <NUM_LIT:1.>) < thresh_wh) or (h / (im_new.shape[<NUM_LIT:0>] * <NUM_LIT:1.>) <<EOL>thresh_wh):  <EOL><INDENT>return None<EOL><DEDENT>coord = [x, y, w, h]<EOL>if is_center:<EOL><INDENT>coord = obj_box_coord_upleft_to_centroid(coord)<EOL><DEDENT>return coord<EOL><DEDENT>coords_new = list()<EOL>classes_new = list()<EOL>for i, _ in enumerate(coords):<EOL><INDENT>coord = coords[i]<EOL>if len(coord) != <NUM_LIT:4>:<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>if is_rescale:<EOL><INDENT>coord = obj_box_coord_scale_to_pixelunit(coord, im.shape)<EOL>coord = _get_coord(coord)<EOL>if coord is not None:<EOL><INDENT>coord = obj_box_coord_rescale(coord, im_new.shape)<EOL>coords_new.append(coord)<EOL>classes_new.append(classes[i])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>coord = _get_coord(coord)<EOL>if coord is not None:<EOL><INDENT>coords_new.append(coord)<EOL>classes_new.append(classes[i])<EOL><DEDENT><DEDENT><DEDENT>return im_new, classes_new, coords_new<EOL>

Shift an image randomly or non-randomly, and compute the new bounding box coordinates.
    Objects outside the cropped image will be removed.

    Parameters
    -----------
    im : numpy.array
        An image with dimension of [row, col, channel] (default).
    classes : list of int or None
        Class IDs.
    coords : list of list of 4 int/float or None
        Coordinates [[x, y, w, h], [x, y, w, h], ...]
    wrg, hrg row_index col_index channel_index is_random fill_mode cval and order : see ``tl.prepro.shift``.
    is_rescale : boolean
        Set to True, if the input coordinates are rescaled to [0, 1]. Default is False.
    is_center : boolean
        Set to True, if the x and y of coordinates are the centroid (i.e. darknet format). Default is False.
    thresh_wh : float
        Threshold, remove the box if its ratio of width(height) to image size less than the threshold.
    thresh_wh2 : float
        Threshold, remove the box if its ratio of width to height or vice verse higher than the threshold.

    Returns
    -------
    numpy.array
        A processed image
    list of int
        A list of classes
    list of list of 4 numbers
        A list of new bounding boxes.


def obj_box_zoom(<EOL>im, classes=None, coords=None, zoom_range=(<NUM_LIT>,<EOL><NUM_LIT>), row_index=<NUM_LIT:0>, col_index=<NUM_LIT:1>, channel_index=<NUM_LIT:2>, fill_mode='<STR_LIT>',<EOL>cval=<NUM_LIT:0.>, order=<NUM_LIT:1>, is_rescale=False, is_center=False, is_random=False, thresh_wh=<NUM_LIT>, thresh_wh2=<NUM_LIT><EOL>):

if classes is None:<EOL><INDENT>classes = []<EOL><DEDENT>if coords is None:<EOL><INDENT>coords = []<EOL><DEDENT>if len(zoom_range) != <NUM_LIT:2>:<EOL><INDENT>raise Exception('<STR_LIT>' '<STR_LIT>', zoom_range)<EOL><DEDENT>if is_random:<EOL><INDENT>if zoom_range[<NUM_LIT:0>] == <NUM_LIT:1> and zoom_range[<NUM_LIT:1>] == <NUM_LIT:1>:<EOL><INDENT>zx, zy = <NUM_LIT:1>, <NUM_LIT:1><EOL>tl.logging.info("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>zx, zy = np.random.uniform(zoom_range[<NUM_LIT:0>], zoom_range[<NUM_LIT:1>], <NUM_LIT:2>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>zx, zy = zoom_range<EOL><DEDENT>zoom_matrix = np.array([[zx, <NUM_LIT:0>, <NUM_LIT:0>], [<NUM_LIT:0>, zy, <NUM_LIT:0>], [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:1>]])<EOL>h, w = im.shape[row_index], im.shape[col_index]<EOL>transform_matrix = transform_matrix_offset_center(zoom_matrix, h, w)<EOL>im_new = affine_transform(im, transform_matrix, channel_index, fill_mode, cval, order)<EOL>def _get_coord(coord):<EOL><INDENT>"""<STR_LIT>"""<EOL>if is_center:<EOL><INDENT>coord = obj_box_coord_centroid_to_upleft(coord)<EOL><DEDENT>x = (coord[<NUM_LIT:0>] - im.shape[<NUM_LIT:1>] / <NUM_LIT:2>) / zy + im.shape[<NUM_LIT:1>] / <NUM_LIT:2>  <EOL>y = (coord[<NUM_LIT:1>] - im.shape[<NUM_LIT:0>] / <NUM_LIT:2>) / zx + im.shape[<NUM_LIT:0>] / <NUM_LIT:2>  <EOL>w = coord[<NUM_LIT:2>] / zy  <EOL>h = coord[<NUM_LIT:3>] / zx  <EOL>if x < <NUM_LIT:0>:<EOL><INDENT>if x + w <= <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT>w = w + x<EOL>x = <NUM_LIT:0><EOL><DEDENT>elif x > im_new.shape[<NUM_LIT:1>]:  <EOL><INDENT>return None<EOL><DEDENT>if y < <NUM_LIT:0>:<EOL><INDENT>if y + h <= <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT>h = h + y<EOL>y = <NUM_LIT:0><EOL><DEDENT>elif y > im_new.shape[<NUM_LIT:0>]:  <EOL><INDENT>return None<EOL><DEDENT>if (x is not None) and (x + w > im_new.shape[<NUM_LIT:1>]):  <EOL><INDENT>w = im_new.shape[<NUM_LIT:1>] - x<EOL><DEDENT>if (y is not None) and (y + h > im_new.shape[<NUM_LIT:0>]):  <EOL><INDENT>h = im_new.shape[<NUM_LIT:0>] - y<EOL><DEDENT>if (w / (h + <NUM_LIT:1.>) > thresh_wh2) or (h / (w + <NUM_LIT:1.>) > thresh_wh2):  <EOL><INDENT>return None<EOL><DEDENT>if (w / (im_new.shape[<NUM_LIT:1>] * <NUM_LIT:1.>) < thresh_wh) or (h / (im_new.shape[<NUM_LIT:0>] * <NUM_LIT:1.>) <<EOL>thresh_wh):  <EOL><INDENT>return None<EOL><DEDENT>coord = [x, y, w, h]<EOL>if is_center:<EOL><INDENT>coord = obj_box_coord_upleft_to_centroid(coord)<EOL><DEDENT>return coord<EOL><DEDENT>coords_new = list()<EOL>classes_new = list()<EOL>for i, _ in enumerate(coords):<EOL><INDENT>coord = coords[i]<EOL>if len(coord) != <NUM_LIT:4>:<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>if is_rescale:<EOL><INDENT>coord = obj_box_coord_scale_to_pixelunit(coord, im.shape)<EOL>coord = _get_coord(coord)<EOL>if coord is not None:<EOL><INDENT>coord = obj_box_coord_rescale(coord, im_new.shape)<EOL>coords_new.append(coord)<EOL>classes_new.append(classes[i])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>coord = _get_coord(coord)<EOL>if coord is not None:<EOL><INDENT>coords_new.append(coord)<EOL>classes_new.append(classes[i])<EOL><DEDENT><DEDENT><DEDENT>return im_new, classes_new, coords_new<EOL>

Zoom in and out of a single image, randomly or non-randomly, and compute the new bounding box coordinates.
    Objects outside the cropped image will be removed.

    Parameters
    -----------
    im : numpy.array
        An image with dimension of [row, col, channel] (default).
    classes : list of int or None
        Class IDs.
    coords : list of list of 4 int/float or None
        Coordinates [[x, y, w, h], [x, y, w, h], ...].
    zoom_range row_index col_index channel_index is_random fill_mode cval and order : see ``tl.prepro.zoom``.
    is_rescale : boolean
        Set to True, if the input coordinates are rescaled to [0, 1]. Default is False.
    is_center : boolean
        Set to True, if the x and y of coordinates are the centroid. (i.e. darknet format). Default is False.
    thresh_wh : float
        Threshold, remove the box if its ratio of width(height) to image size less than the threshold.
    thresh_wh2 : float
        Threshold, remove the box if its ratio of width to height or vice verse higher than the threshold.

    Returns
    -------
    numpy.array
        A processed image
    list of int
        A list of classes
    list of list of 4 numbers
        A list of new bounding boxes.


def pad_sequences(sequences, maxlen=None, dtype='<STR_LIT>', padding='<STR_LIT>', truncating='<STR_LIT>', value=<NUM_LIT:0.>):

lengths = [len(s) for s in sequences]<EOL>nb_samples = len(sequences)<EOL>if maxlen is None:<EOL><INDENT>maxlen = np.max(lengths)<EOL><DEDENT>sample_shape = tuple()<EOL>for s in sequences:<EOL><INDENT>if len(s) > <NUM_LIT:0>:<EOL><INDENT>sample_shape = np.asarray(s).shape[<NUM_LIT:1>:]<EOL>break<EOL><DEDENT><DEDENT>x = (np.ones((nb_samples, maxlen) + sample_shape) * value).astype(dtype)<EOL>for idx, s in enumerate(sequences):<EOL><INDENT>if len(s) == <NUM_LIT:0>:<EOL><INDENT>continue  <EOL><DEDENT>if truncating == '<STR_LIT>':<EOL><INDENT>trunc = s[-maxlen:]<EOL><DEDENT>elif truncating == '<STR_LIT>':<EOL><INDENT>trunc = s[:maxlen]<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>' % truncating)<EOL><DEDENT>trunc = np.asarray(trunc, dtype=dtype)<EOL>if trunc.shape[<NUM_LIT:1>:] != sample_shape:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>' %<EOL>(trunc.shape[<NUM_LIT:1>:], idx, sample_shape)<EOL>)<EOL><DEDENT>if padding == '<STR_LIT>':<EOL><INDENT>x[idx, :len(trunc)] = trunc<EOL><DEDENT>elif padding == '<STR_LIT>':<EOL><INDENT>x[idx, -len(trunc):] = trunc<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>' % padding)<EOL><DEDENT><DEDENT>return x.tolist()<EOL>

Pads each sequence to the same length:
    the length of the longest sequence.
    If maxlen is provided, any sequence longer
    than maxlen is truncated to maxlen.
    Truncation happens off either the beginning (default) or
    the end of the sequence.
    Supports post-padding and pre-padding (default).

    Parameters
    ----------
    sequences : list of list of int
        All sequences where each row is a sequence.
    maxlen : int
        Maximum length.
    dtype : numpy.dtype or str
        Data type to cast the resulting sequence.
    padding : str
        Either 'pre' or 'post', pad either before or after each sequence.
    truncating : str
        Either 'pre' or 'post', remove values from sequences larger than maxlen either in the beginning or in the end of the sequence
    value : float
        Value to pad the sequences to the desired value.

    Returns
    ----------
    x : numpy.array
        With dimensions (number_of_sequences, maxlen)

    Examples
    ----------
    >>> sequences = [[1,1,1,1,1],[2,2,2],[3,3]]
    >>> sequences = pad_sequences(sequences, maxlen=None, dtype='int32',
    ...                  padding='post', truncating='pre', value=0.)
    [[1 1 1 1 1]
     [2 2 2 0 0]
     [3 3 0 0 0]]


sequences_out = copy.deepcopy(sequences)<EOL>for i, _ in enumerate(sequences):<EOL><INDENT>for j in range(<NUM_LIT:1>, len(sequences[i])):<EOL><INDENT>if sequences[i][-j] != pad_id:<EOL><INDENT>sequences_out[i] = sequences_out[i][<NUM_LIT:0>:-j + <NUM_LIT:1>]<EOL>break<EOL><DEDENT><DEDENT><DEDENT>return sequences_out<EOL>

Remove padding.

    Parameters
    -----------
    sequences : list of list of int
        All sequences where each row is a sequence.
    pad_id : int
        The pad ID.

    Returns
    ----------
    list of list of int
        The processed sequences.

    Examples
    ----------
    >>> sequences = [[2,3,4,0,0], [5,1,2,3,4,0,0,0], [4,5,0,2,4,0,0,0]]
    >>> print(remove_pad_sequences(sequences, pad_id=0))
    [[2, 3, 4], [5, 1, 2, 3, 4], [4, 5, 0, 2, 4]]


def process_sequences(sequences, end_id=<NUM_LIT:0>, pad_val=<NUM_LIT:0>, is_shorten=True, remain_end_id=False):

max_length = <NUM_LIT:0><EOL>for _, seq in enumerate(sequences):<EOL><INDENT>is_end = False<EOL>for i_w, n in enumerate(seq):<EOL><INDENT>if n == end_id and is_end == False:  <EOL><INDENT>is_end = True<EOL>if max_length < i_w:<EOL><INDENT>max_length = i_w<EOL><DEDENT>if remain_end_id is False:<EOL><INDENT>seq[i_w] = pad_val  <EOL><DEDENT><DEDENT>elif is_end ==True:<EOL><INDENT>seq[i_w] = pad_val<EOL><DEDENT><DEDENT><DEDENT>if remain_end_id is True:<EOL><INDENT>max_length += <NUM_LIT:1><EOL><DEDENT>if is_shorten:<EOL><INDENT>for i, seq in enumerate(sequences):<EOL><INDENT>sequences[i] = seq[:max_length]<EOL><DEDENT><DEDENT>return sequences<EOL>

Set all tokens(ids) after END token to the padding value, and then shorten (option) it to the maximum sequence length in this batch.

    Parameters
    -----------
    sequences : list of list of int
        All sequences where each row is a sequence.
    end_id : int
        The special token for END.
    pad_val : int
        Replace the `end_id` and the IDs after `end_id` to this value.
    is_shorten : boolean
        Shorten the sequences. Default is True.
    remain_end_id : boolean
        Keep an `end_id` in the end. Default is False.

    Returns
    ----------
    list of list of int
        The processed sequences.

    Examples
    ---------
    >>> sentences_ids = [[4, 3, 5, 3, 2, 2, 2, 2],  <-- end_id is 2
    ...                  [5, 3, 9, 4, 9, 2, 2, 3]]  <-- end_id is 2
    >>> sentences_ids = precess_sequences(sentences_ids, end_id=vocab.end_id, pad_val=0, is_shorten=True)
    [[4, 3, 5, 3, 0], [5, 3, 9, 4, 9]]


def sequences_add_start_id(sequences, start_id=<NUM_LIT:0>, remove_last=False):

sequences_out = [[] for _ in range(len(sequences))]  <EOL>for i, _ in enumerate(sequences):<EOL><INDENT>if remove_last:<EOL><INDENT>sequences_out[i] = [start_id] + sequences[i][:-<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>sequences_out[i] = [start_id] + sequences[i]<EOL><DEDENT><DEDENT>return sequences_out<EOL>

Add special start token(id) in the beginning of each sequence.

    Parameters
    ------------
    sequences : list of list of int
        All sequences where each row is a sequence.
    start_id : int
        The start ID.
    remove_last : boolean
        Remove the last value of each sequences. Usually be used for removing the end ID.

    Returns
    ----------
    list of list of int
        The processed sequences.

    Examples
    ---------
    >>> sentences_ids = [[4,3,5,3,2,2,2,2], [5,3,9,4,9,2,2,3]]
    >>> sentences_ids = sequences_add_start_id(sentences_ids, start_id=2)
    [[2, 4, 3, 5, 3, 2, 2, 2, 2], [2, 5, 3, 9, 4, 9, 2, 2, 3]]
    >>> sentences_ids = sequences_add_start_id(sentences_ids, start_id=2, remove_last=True)
    [[2, 4, 3, 5, 3, 2, 2, 2], [2, 5, 3, 9, 4, 9, 2, 2]]

    For Seq2seq

    >>> input = [a, b, c]
    >>> target = [x, y, z]
    >>> decode_seq = [start_id, a, b] <-- sequences_add_start_id(input, start_id, True)


sequences_out = [[] for _ in range(len(sequences))]  <EOL>for i, _ in enumerate(sequences):<EOL><INDENT>sequences_out[i] = sequences[i] + [end_id]<EOL><DEDENT>return sequences_out<EOL>

Add special end token(id) in the end of each sequence.

    Parameters
    -----------
    sequences : list of list of int
        All sequences where each row is a sequence.
    end_id : int
        The end ID.

    Returns
    ----------
    list of list of int
        The processed sequences.

    Examples
    ---------
    >>> sequences = [[1,2,3],[4,5,6,7]]
    >>> print(sequences_add_end_id(sequences, end_id=999))
    [[1, 2, 3, 999], [4, 5, 6, 999]]


def sequences_add_end_id_after_pad(sequences, end_id=<NUM_LIT>, pad_id=<NUM_LIT:0>):

<EOL>sequences_out = copy.deepcopy(sequences)<EOL>for i, v in enumerate(sequences):<EOL><INDENT>for j, _v2 in enumerate(v):<EOL><INDENT>if sequences[i][j] == pad_id:<EOL><INDENT>sequences_out[i][j] = end_id<EOL>break<EOL><DEDENT><DEDENT><DEDENT>return sequences_out<EOL>

Add special end token(id) in the end of each sequence.

    Parameters
    -----------
    sequences : list of list of int
        All sequences where each row is a sequence.
    end_id : int
        The end ID.
    pad_id : int
        The pad ID.

    Returns
    ----------
    list of list of int
        The processed sequences.

    Examples
    ---------
    >>> sequences = [[1,2,0,0], [1,2,3,0], [1,2,3,4]]
    >>> print(sequences_add_end_id_after_pad(sequences, end_id=99, pad_id=0))
    [[1, 2, 99, 0], [1, 2, 3, 99], [1, 2, 3, 4]]


mask = np.ones_like(sequences)<EOL>for i, seq in enumerate(sequences):<EOL><INDENT>for i_w in reversed(range(len(seq))):<EOL><INDENT>if seq[i_w] == pad_val:<EOL><INDENT>mask[i, i_w] = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>break  <EOL><DEDENT><DEDENT><DEDENT>return mask<EOL>

Return mask for sequences.

    Parameters
    -----------
    sequences : list of list of int
        All sequences where each row is a sequence.
    pad_val : int
        The pad value.

    Returns
    ----------
    list of list of int
        The mask.

    Examples
    ---------
    >>> sentences_ids = [[4, 0, 5, 3, 0, 0],
    ...                  [5, 3, 9, 4, 9, 0]]
    >>> mask = sequences_get_mask(sentences_ids, pad_val=0)
    [[1 1 1 1 0 0]
     [1 1 1 1 1 0]]


def keypoint_random_crop(image, annos, mask=None, size=(<NUM_LIT>, <NUM_LIT>)):

_target_height = size[<NUM_LIT:0>]<EOL>_target_width = size[<NUM_LIT:1>]<EOL>target_size = (_target_width, _target_height)<EOL>if len(np.shape(image)) == <NUM_LIT:2>:<EOL><INDENT>image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)<EOL><DEDENT>height, width, _ = np.shape(image)<EOL>for _ in range(<NUM_LIT:50>):<EOL><INDENT>x = random.randrange(<NUM_LIT:0>, width - target_size[<NUM_LIT:0>]) if width > target_size[<NUM_LIT:0>] else <NUM_LIT:0><EOL>y = random.randrange(<NUM_LIT:0>, height - target_size[<NUM_LIT:1>]) if height > target_size[<NUM_LIT:1>] else <NUM_LIT:0><EOL>for joint in annos:<EOL><INDENT>if x <= joint[<NUM_LIT:0>][<NUM_LIT:0>] < x + target_size[<NUM_LIT:0>] and y <= joint[<NUM_LIT:0>][<NUM_LIT:1>] < y + target_size[<NUM_LIT:1>]:<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>def pose_crop(image, annos, mask, x, y, w, h):  <EOL><INDENT>target_size = (w, h)<EOL>img = image<EOL>resized = img[y:y + target_size[<NUM_LIT:1>], x:x + target_size[<NUM_LIT:0>], :]<EOL>resized_mask = mask[y:y + target_size[<NUM_LIT:1>], x:x + target_size[<NUM_LIT:0>]]<EOL>adjust_joint_list = []<EOL>for joint in annos:<EOL><INDENT>adjust_joint = []<EOL>for point in joint:<EOL><INDENT>if point[<NUM_LIT:0>] < -<NUM_LIT:10> or point[<NUM_LIT:1>] < -<NUM_LIT:10>:<EOL><INDENT>adjust_joint.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>new_x, new_y = point[<NUM_LIT:0>] - x, point[<NUM_LIT:1>] - y<EOL>if new_x > w - <NUM_LIT:1> or new_y > h - <NUM_LIT:1>:<EOL><INDENT>adjust_joint.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>adjust_joint.append((new_x, new_y))<EOL><DEDENT>adjust_joint_list.append(adjust_joint)<EOL><DEDENT>return resized, adjust_joint_list, resized_mask<EOL><DEDENT>return pose_crop(image, annos, mask, x, y, target_size[<NUM_LIT:0>], target_size[<NUM_LIT:1>])<EOL>

Randomly crop an image and corresponding keypoints without influence scales, given by ``keypoint_random_resize_shortestedge``.

    Parameters
    -----------
    image : 3 channel image
        The given image for augmentation.
    annos : list of list of floats
        The keypoints annotation of people.
    mask : single channel image or None
        The mask if available.
    size : tuple of int
        The size of returned image.

    Returns
    ----------
    preprocessed image, annotation, mask


def keypoint_resize_random_crop(image, annos, mask=None, size=(<NUM_LIT>, <NUM_LIT>)):

if len(np.shape(image)) == <NUM_LIT:2>:<EOL><INDENT>image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)<EOL><DEDENT>def resize_image(image, annos, mask, target_width, target_height):<EOL><INDENT>"""<STR_LIT>"""<EOL>y, x, _ = np.shape(image)<EOL>ratio_y = target_height / y<EOL>ratio_x = target_width / x<EOL>new_joints = []<EOL>for people in annos:<EOL><INDENT>new_keypoints = []<EOL>for keypoints in people:<EOL><INDENT>if keypoints[<NUM_LIT:0>] < <NUM_LIT:0> or keypoints[<NUM_LIT:1>] < <NUM_LIT:0>:<EOL><INDENT>new_keypoints.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>pts = (int(keypoints[<NUM_LIT:0>] * ratio_x + <NUM_LIT:0.5>), int(keypoints[<NUM_LIT:1>] * ratio_y + <NUM_LIT:0.5>))<EOL>if pts[<NUM_LIT:0>] > target_width - <NUM_LIT:1> or pts[<NUM_LIT:1>] > target_height - <NUM_LIT:1>:<EOL><INDENT>new_keypoints.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>new_keypoints.append(pts)<EOL><DEDENT>new_joints.append(new_keypoints)<EOL><DEDENT>annos = new_joints<EOL>new_image = cv2.resize(image, (target_width, target_height), interpolation=cv2.INTER_AREA)<EOL>if mask is not None:<EOL><INDENT>new_mask = cv2.resize(mask, (target_width, target_height), interpolation=cv2.INTER_AREA)<EOL>return new_image, annos, new_mask<EOL><DEDENT>else:<EOL><INDENT>return new_image, annos, None<EOL><DEDENT><DEDENT>_target_height = size[<NUM_LIT:0>]<EOL>_target_width = size[<NUM_LIT:1>]<EOL>if len(np.shape(image)) == <NUM_LIT:2>:<EOL><INDENT>image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)<EOL><DEDENT>height, width, _ = np.shape(image)<EOL>if height <= width:<EOL><INDENT>ratio = _target_height / height<EOL>new_width = int(ratio * width)<EOL>if height == width:<EOL><INDENT>new_width = _target_height<EOL><DEDENT>image, annos, mask = resize_image(image, annos, mask, new_width, _target_height)<EOL>if new_width > _target_width:<EOL><INDENT>crop_range_x = np.random.randint(<NUM_LIT:0>, new_width - _target_width)<EOL><DEDENT>else:<EOL><INDENT>crop_range_x = <NUM_LIT:0><EOL><DEDENT>image = image[:, crop_range_x:crop_range_x + _target_width, :]<EOL>if mask is not None:<EOL><INDENT>mask = mask[:, crop_range_x:crop_range_x + _target_width]<EOL><DEDENT>new_joints = []<EOL>for people in annos:<EOL><INDENT>new_keypoints = []<EOL>for keypoints in people:<EOL><INDENT>if keypoints[<NUM_LIT:0>] < -<NUM_LIT:10> or keypoints[<NUM_LIT:1>] < -<NUM_LIT:10>:<EOL><INDENT>new_keypoints.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>top = crop_range_x + _target_width - <NUM_LIT:1><EOL>if keypoints[<NUM_LIT:0>] >= crop_range_x and keypoints[<NUM_LIT:0>] <= top:<EOL><INDENT>pts = (int(keypoints[<NUM_LIT:0>] - crop_range_x), int(keypoints[<NUM_LIT:1>]))<EOL><DEDENT>else:<EOL><INDENT>pts = (-<NUM_LIT:1000>, -<NUM_LIT:1000>)<EOL><DEDENT>new_keypoints.append(pts)<EOL><DEDENT>new_joints.append(new_keypoints)<EOL><DEDENT>annos = new_joints<EOL><DEDENT>if height > width:<EOL><INDENT>ratio = _target_width / width<EOL>new_height = int(ratio * height)<EOL>image, annos, mask = resize_image(image, annos, mask, _target_width, new_height)<EOL>if new_height > _target_height:<EOL><INDENT>crop_range_y = np.random.randint(<NUM_LIT:0>, new_height - _target_height)<EOL><DEDENT>else:<EOL><INDENT>crop_range_y = <NUM_LIT:0><EOL><DEDENT>image = image[crop_range_y:crop_range_y + _target_width, :, :]<EOL>if mask is not None:<EOL><INDENT>mask = mask[crop_range_y:crop_range_y + _target_width, :]<EOL><DEDENT>new_joints = []<EOL>for people in annos:  <EOL><INDENT>new_keypoints = []<EOL>for keypoints in people:<EOL><INDENT>if keypoints[<NUM_LIT:0>] < <NUM_LIT:0> or keypoints[<NUM_LIT:1>] < <NUM_LIT:0>:<EOL><INDENT>new_keypoints.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>bot = crop_range_y + _target_height - <NUM_LIT:1><EOL>if keypoints[<NUM_LIT:1>] >= crop_range_y and keypoints[<NUM_LIT:1>] <= bot:<EOL><INDENT>pts = (int(keypoints[<NUM_LIT:0>]), int(keypoints[<NUM_LIT:1>] - crop_range_y))<EOL><DEDENT>else:<EOL><INDENT>pts = (-<NUM_LIT:1000>, -<NUM_LIT:1000>)<EOL><DEDENT>new_keypoints.append(pts)<EOL><DEDENT>new_joints.append(new_keypoints)<EOL><DEDENT>annos = new_joints<EOL><DEDENT>if mask is not None:<EOL><INDENT>return image, annos, mask<EOL><DEDENT>else:<EOL><INDENT>return image, annos, None<EOL><DEDENT>

Reszie the image to make either its width or height equals to the given sizes.
    Then randomly crop image without influence scales.
    Resize the image match with the minimum size before cropping, this API will change the zoom scale of object.

    Parameters
    -----------
    image : 3 channel image
        The given image for augmentation.
    annos : list of list of floats
        The keypoints annotation of people.
    mask : single channel image or None
        The mask if available.
    size : tuple of int
        The size (height, width) of returned image.

    Returns
    ----------
    preprocessed image, annos, mask


def keypoint_random_rotate(image, annos, mask=None, rg=<NUM_LIT>):

def _rotate_coord(shape, newxy, point, angle):<EOL><INDENT>angle = -<NUM_LIT:1> * angle / <NUM_LIT> * math.pi<EOL>ox, oy = shape<EOL>px, py = point<EOL>ox /= <NUM_LIT:2><EOL>oy /= <NUM_LIT:2><EOL>qx = math.cos(angle) * (px - ox) - math.sin(angle) * (py - oy)<EOL>qy = math.sin(angle) * (px - ox) + math.cos(angle) * (py - oy)<EOL>new_x, new_y = newxy<EOL>qx += ox - new_x<EOL>qy += oy - new_y<EOL>return int(qx + <NUM_LIT:0.5>), int(qy + <NUM_LIT:0.5>)<EOL><DEDENT>def _largest_rotated_rect(w, h, angle):<EOL><INDENT>"""<STR_LIT>"""<EOL>angle = angle / <NUM_LIT> * math.pi<EOL>if w <= <NUM_LIT:0> or h <= <NUM_LIT:0>:<EOL><INDENT>return <NUM_LIT:0>, <NUM_LIT:0><EOL><DEDENT>width_is_longer = w >= h<EOL>side_long, side_short = (w, h) if width_is_longer else (h, w)<EOL>sin_a, cos_a = abs(math.sin(angle)), abs(math.cos(angle))<EOL>if side_short <= <NUM_LIT> * sin_a * cos_a * side_long:<EOL><INDENT>x = <NUM_LIT:0.5> * side_short<EOL>wr, hr = (x / sin_a, x / cos_a) if width_is_longer else (x / cos_a, x / sin_a)<EOL><DEDENT>else:<EOL><INDENT>cos_2a = cos_a * cos_a - sin_a * sin_a<EOL>wr, hr = (w * cos_a - h * sin_a) / cos_2a, (h * cos_a - w * sin_a) / cos_2a<EOL><DEDENT>return int(np.round(wr)), int(np.round(hr))<EOL><DEDENT>img_shape = np.shape(image)<EOL>height = img_shape[<NUM_LIT:0>]<EOL>width = img_shape[<NUM_LIT:1>]<EOL>deg = np.random.uniform(-rg, rg)<EOL>img = image<EOL>center = (img.shape[<NUM_LIT:1>] * <NUM_LIT:0.5>, img.shape[<NUM_LIT:0>] * <NUM_LIT:0.5>)  <EOL>rot_m = cv2.getRotationMatrix2D((int(center[<NUM_LIT:0>]), int(center[<NUM_LIT:1>])), deg, <NUM_LIT:1>)<EOL>ret = cv2.warpAffine(img, rot_m, img.shape[<NUM_LIT:1>::-<NUM_LIT:1>], flags=cv2.INTER_AREA, borderMode=cv2.BORDER_CONSTANT)<EOL>if img.ndim == <NUM_LIT:3> and ret.ndim == <NUM_LIT:2>:<EOL><INDENT>ret = ret[:, :, np.newaxis]<EOL><DEDENT>neww, newh = _largest_rotated_rect(ret.shape[<NUM_LIT:1>], ret.shape[<NUM_LIT:0>], deg)<EOL>neww = min(neww, ret.shape[<NUM_LIT:1>])<EOL>newh = min(newh, ret.shape[<NUM_LIT:0>])<EOL>newx = int(center[<NUM_LIT:0>] - neww * <NUM_LIT:0.5>)<EOL>newy = int(center[<NUM_LIT:1>] - newh * <NUM_LIT:0.5>)<EOL>img = ret[newy:newy + newh, newx:newx + neww]<EOL>adjust_joint_list = []<EOL>for joint in annos:  <EOL><INDENT>adjust_joint = []<EOL>for point in joint:<EOL><INDENT>if point[<NUM_LIT:0>] < -<NUM_LIT:100> or point[<NUM_LIT:1>] < -<NUM_LIT:100>:<EOL><INDENT>adjust_joint.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>x, y = _rotate_coord((width, height), (newx, newy), point, deg)<EOL>if x > neww - <NUM_LIT:1> or y > newh - <NUM_LIT:1>:<EOL><INDENT>adjust_joint.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>if x < <NUM_LIT:0> or y < <NUM_LIT:0>:<EOL><INDENT>adjust_joint.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>adjust_joint.append((x, y))<EOL><DEDENT>adjust_joint_list.append(adjust_joint)<EOL><DEDENT>joint_list = adjust_joint_list<EOL>if mask is not None:<EOL><INDENT>msk = mask<EOL>center = (msk.shape[<NUM_LIT:1>] * <NUM_LIT:0.5>, msk.shape[<NUM_LIT:0>] * <NUM_LIT:0.5>)  <EOL>rot_m = cv2.getRotationMatrix2D((int(center[<NUM_LIT:0>]), int(center[<NUM_LIT:1>])), deg, <NUM_LIT:1>)<EOL>ret = cv2.warpAffine(msk, rot_m, msk.shape[<NUM_LIT:1>::-<NUM_LIT:1>], flags=cv2.INTER_AREA, borderMode=cv2.BORDER_CONSTANT)<EOL>if msk.ndim == <NUM_LIT:3> and msk.ndim == <NUM_LIT:2>:<EOL><INDENT>ret = ret[:, :, np.newaxis]<EOL><DEDENT>neww, newh = _largest_rotated_rect(ret.shape[<NUM_LIT:1>], ret.shape[<NUM_LIT:0>], deg)<EOL>neww = min(neww, ret.shape[<NUM_LIT:1>])<EOL>newh = min(newh, ret.shape[<NUM_LIT:0>])<EOL>newx = int(center[<NUM_LIT:0>] - neww * <NUM_LIT:0.5>)<EOL>newy = int(center[<NUM_LIT:1>] - newh * <NUM_LIT:0.5>)<EOL>msk = ret[newy:newy + newh, newx:newx + neww]<EOL>return img, joint_list, msk<EOL><DEDENT>else:<EOL><INDENT>return img, joint_list, None<EOL><DEDENT>

Rotate an image and corresponding keypoints.

    Parameters
    -----------
    image : 3 channel image
        The given image for augmentation.
    annos : list of list of floats
        The keypoints annotation of people.
    mask : single channel image or None
        The mask if available.
    rg : int or float
        Degree to rotate, usually 0 ~ 180.

    Returns
    ----------
    preprocessed image, annos, mask


def keypoint_random_flip(<EOL>image, annos, mask=None, prob=<NUM_LIT:0.5>, flip_list=(<NUM_LIT:0>, <NUM_LIT:1>, <NUM_LIT:5>, <NUM_LIT:6>, <NUM_LIT:7>, <NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:4>, <NUM_LIT:11>, <NUM_LIT:12>, <NUM_LIT>, <NUM_LIT:8>, <NUM_LIT:9>, <NUM_LIT:10>, <NUM_LIT:15>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT:16>, <NUM_LIT>)<EOL>):

_prob = np.random.uniform(<NUM_LIT:0>, <NUM_LIT:1.0>)<EOL>if _prob < prob:<EOL><INDENT>return image, annos, mask<EOL><DEDENT>_, width, _ = np.shape(image)<EOL>image = cv2.flip(image, <NUM_LIT:1>)<EOL>mask = cv2.flip(mask, <NUM_LIT:1>)<EOL>new_joints = []<EOL>for people in annos:  <EOL><INDENT>new_keypoints = []<EOL>for k in flip_list:<EOL><INDENT>point = people[k]<EOL>if point[<NUM_LIT:0>] < <NUM_LIT:0> or point[<NUM_LIT:1>] < <NUM_LIT:0>:<EOL><INDENT>new_keypoints.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>if point[<NUM_LIT:0>] > image.shape[<NUM_LIT:1>] - <NUM_LIT:1> or point[<NUM_LIT:1>] > image.shape[<NUM_LIT:0>] - <NUM_LIT:1>:<EOL><INDENT>new_keypoints.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>if (width - point[<NUM_LIT:0>]) > image.shape[<NUM_LIT:1>] - <NUM_LIT:1>:<EOL><INDENT>new_keypoints.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>new_keypoints.append((width - point[<NUM_LIT:0>], point[<NUM_LIT:1>]))<EOL><DEDENT>new_joints.append(new_keypoints)<EOL><DEDENT>annos = new_joints<EOL>return image, annos, mask<EOL>

Flip an image and corresponding keypoints.

    Parameters
    -----------
    image : 3 channel image
        The given image for augmentation.
    annos : list of list of floats
        The keypoints annotation of people.
    mask : single channel image or None
        The mask if available.
    prob : float, 0 to 1
        The probability to flip the image, if 1, always flip the image.
    flip_list : tuple of int
        Denotes how the keypoints number be changed after flipping which is required for pose estimation task.
        The left and right body should be maintained rather than switch.
        (Default COCO format).
        Set to an empty tuple if you don't need to maintain left and right information.

    Returns
    ----------
    preprocessed image, annos, mask


def keypoint_random_resize(image, annos, mask=None, zoom_range=(<NUM_LIT>, <NUM_LIT>)):

height = image.shape[<NUM_LIT:0>]<EOL>width = image.shape[<NUM_LIT:1>]<EOL>_min, _max = zoom_range<EOL>scalew = np.random.uniform(_min, _max)<EOL>scaleh = np.random.uniform(_min, _max)<EOL>neww = int(width * scalew)<EOL>newh = int(height * scaleh)<EOL>dst = cv2.resize(image, (neww, newh), interpolation=cv2.INTER_AREA)<EOL>if mask is not None:<EOL><INDENT>mask = cv2.resize(mask, (neww, newh), interpolation=cv2.INTER_AREA)<EOL><DEDENT>adjust_joint_list = []<EOL>for joint in annos:  <EOL><INDENT>adjust_joint = []<EOL>for point in joint:<EOL><INDENT>if point[<NUM_LIT:0>] < -<NUM_LIT:100> or point[<NUM_LIT:1>] < -<NUM_LIT:100>:<EOL><INDENT>adjust_joint.append((-<NUM_LIT:1000>, -<NUM_LIT:1000>))<EOL>continue<EOL><DEDENT>adjust_joint.append((int(point[<NUM_LIT:0>] * scalew + <NUM_LIT:0.5>), int(point[<NUM_LIT:1>] * scaleh + <NUM_LIT:0.5>)))<EOL><DEDENT>adjust_joint_list.append(adjust_joint)<EOL><DEDENT>if mask is not None:<EOL><INDENT>return dst, adjust_joint_list, mask<EOL><DEDENT>else:<EOL><INDENT>return dst, adjust_joint_list, None<EOL><DEDENT>

Randomly resize an image and corresponding keypoints.
    The height and width of image will be changed independently, so the scale will be changed.

    Parameters
    -----------
    image : 3 channel image
        The given image for augmentation.
    annos : list of list of floats
        The keypoints annotation of people.
    mask : single channel image or None
        The mask if available.
    zoom_range : tuple of two floats
        The minimum and maximum factor to zoom in or out, e.g (0.5, 1) means zoom out 1~2 times.

    Returns
    ----------
    preprocessed image, annos, mask


return self._s<EOL>


return args.update({'<STR_LIT>': self.project_name})<EOL>

Fill in project_name for all studies, architectures and parameters.


return pickle.dumps(ps, protocol=pickle.HIGHEST_PROTOCOL)<EOL>


return pickle.loads(ps)<EOL>


def save_model(self, network=None, model_name='<STR_LIT>', **kwargs):

kwargs.update({'<STR_LIT>': model_name})<EOL>self._fill_project_info(kwargs)  <EOL>params = network.get_all_params()<EOL>s = time.time()<EOL>kwargs.update({'<STR_LIT>': network.all_graphs, '<STR_LIT:time>': datetime.utcnow()})<EOL>try:<EOL><INDENT>params_id = self.model_fs.put(self._serialization(params))<EOL>kwargs.update({'<STR_LIT>': params_id, '<STR_LIT:time>': datetime.utcnow()})<EOL>self.db.Model.insert_one(kwargs)<EOL>print("<STR_LIT>".format(round(time.time() - s, <NUM_LIT:2>)))<EOL>return True<EOL><DEDENT>except Exception as e:<EOL><INDENT>exc_type, exc_obj, exc_tb = sys.exc_info()<EOL>fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[<NUM_LIT:1>]<EOL>logging.info("<STR_LIT>".format(exc_type, exc_obj, fname, exc_tb.tb_lineno, e))<EOL>print("<STR_LIT>")<EOL>return False<EOL><DEDENT>

Save model architecture and parameters into database, timestamp will be added automatically.

        Parameters
        ----------
        network : TensorLayer layer
            TensorLayer layer instance.
        model_name : str
            The name/key of model.
        kwargs : other events
            Other events, such as name, accuracy, loss, step number and etc (optinal).

        Examples
        ---------
        Save model architecture and parameters into database.
        >>> db.save_model(net, accuracy=0.8, loss=2.3, name='second_model')

        Load one model with parameters from database (run this in other script)
        >>> net = db.find_top_model(sess=sess, accuracy=0.8, loss=2.3)

        Find and load the latest model.
        >>> net = db.find_top_model(sess=sess, sort=[("time", pymongo.DESCENDING)])
        >>> net = db.find_top_model(sess=sess, sort=[("time", -1)])

        Find and load the oldest model.
        >>> net = db.find_top_model(sess=sess, sort=[("time", pymongo.ASCENDING)])
        >>> net = db.find_top_model(sess=sess, sort=[("time", 1)])

        Get model information
        >>> net._accuracy
        ... 0.8

        Returns
        ---------
        boolean : True for success, False for fail.


def find_top_model(self, sess, sort=None, model_name='<STR_LIT>', **kwargs):

<EOL>kwargs.update({'<STR_LIT>': model_name})<EOL>self._fill_project_info(kwargs)<EOL>s = time.time()<EOL>d = self.db.Model.find_one(filter=kwargs, sort=sort)<EOL>_temp_file_name = '<STR_LIT>'<EOL>if d is not None:<EOL><INDENT>params_id = d['<STR_LIT>']<EOL>graphs = d['<STR_LIT>']<EOL>_datetime = d['<STR_LIT:time>']<EOL>exists_or_mkdir(_temp_file_name, False)<EOL>with open(os.path.join(_temp_file_name, '<STR_LIT>'), '<STR_LIT:wb>') as file:<EOL><INDENT>pickle.dump(graphs, file, protocol=pickle.HIGHEST_PROTOCOL)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>print("<STR_LIT>".format(kwargs))<EOL>return False<EOL><DEDENT>try:<EOL><INDENT>params = self._deserialization(self.model_fs.get(params_id).read())<EOL>np.savez(os.path.join(_temp_file_name, '<STR_LIT>'), params=params)<EOL>network = load_graph_and_params(name=_temp_file_name, sess=sess)<EOL>del_folder(_temp_file_name)<EOL>pc = self.db.Model.find(kwargs)<EOL>print(<EOL>"<STR_LIT>".<EOL>format(kwargs, sort, _datetime, round(time.time() - s, <NUM_LIT:2>))<EOL>)<EOL>for key in d:<EOL><INDENT>network.__dict__.update({"<STR_LIT>" % key: d[key]})<EOL><DEDENT>params_id_list = pc.distinct('<STR_LIT>')<EOL>n_params = len(params_id_list)<EOL>if n_params != <NUM_LIT:1>:<EOL><INDENT>print("<STR_LIT>".format(n_params))<EOL><DEDENT>return network<EOL><DEDENT>except Exception as e:<EOL><INDENT>exc_type, exc_obj, exc_tb = sys.exc_info()<EOL>fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[<NUM_LIT:1>]<EOL>logging.info("<STR_LIT>".format(exc_type, exc_obj, fname, exc_tb.tb_lineno, e))<EOL>return False<EOL><DEDENT>

Finds and returns a model architecture and its parameters from the database which matches the requirement.

        Parameters
        ----------
        sess : Session
            TensorFlow session.
        sort : List of tuple
            PyMongo sort comment, search "PyMongo find one sorting" and `collection level operations <http://api.mongodb.com/python/current/api/pymongo/collection.html>`__ for more details.
        model_name : str or None
            The name/key of model.
        kwargs : other events
            Other events, such as name, accuracy, loss, step number and etc (optinal).

        Examples
        ---------
        - see ``save_model``.

        Returns
        ---------
        network : TensorLayer layer
            Note that, the returned network contains all information of the document (record), e.g. if you saved accuracy in the document, you can get the accuracy by using ``net._accuracy``.


self._fill_project_info(kwargs)<EOL>self.db.Model.delete_many(kwargs)<EOL>logging.info("<STR_LIT>")<EOL>

Delete model.

        Parameters
        -----------
        kwargs : logging information
            Find items to delete, leave it empty to delete all log.


def save_dataset(self, dataset=None, dataset_name=None, **kwargs):

self._fill_project_info(kwargs)<EOL>if dataset_name is None:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>kwargs.update({'<STR_LIT>': dataset_name})<EOL>s = time.time()<EOL>try:<EOL><INDENT>dataset_id = self.dataset_fs.put(self._serialization(dataset))<EOL>kwargs.update({'<STR_LIT>': dataset_id, '<STR_LIT:time>': datetime.utcnow()})<EOL>self.db.Dataset.insert_one(kwargs)<EOL>print("<STR_LIT>".format(round(time.time() - s, <NUM_LIT:2>)))<EOL>return True<EOL><DEDENT>except Exception as e:<EOL><INDENT>exc_type, exc_obj, exc_tb = sys.exc_info()<EOL>fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[<NUM_LIT:1>]<EOL>logging.info("<STR_LIT>".format(exc_type, exc_obj, fname, exc_tb.tb_lineno, e))<EOL>print("<STR_LIT>")<EOL>return False<EOL><DEDENT>

Saves one dataset into database, timestamp will be added automatically.

        Parameters
        ----------
        dataset : any type
            The dataset you want to store.
        dataset_name : str
            The name of dataset.
        kwargs : other events
            Other events, such as description, author and etc (optinal).

        Examples
        ----------
        Save dataset
        >>> db.save_dataset([X_train, y_train, X_test, y_test], 'mnist', description='this is a tutorial')

        Get dataset
        >>> dataset = db.find_top_dataset('mnist')

        Returns
        ---------
        boolean : Return True if save success, otherwise, return False.


def find_top_dataset(self, dataset_name=None, sort=None, **kwargs):

self._fill_project_info(kwargs)<EOL>if dataset_name is None:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>kwargs.update({'<STR_LIT>': dataset_name})<EOL>s = time.time()<EOL>d = self.db.Dataset.find_one(filter=kwargs, sort=sort)<EOL>if d is not None:<EOL><INDENT>dataset_id = d['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>".format(kwargs))<EOL>return False<EOL><DEDENT>try:<EOL><INDENT>dataset = self._deserialization(self.dataset_fs.get(dataset_id).read())<EOL>pc = self.db.Dataset.find(kwargs)<EOL>print("<STR_LIT>".format(kwargs, round(time.time() - s, <NUM_LIT:2>)))<EOL>dataset_id_list = pc.distinct('<STR_LIT>')<EOL>n_dataset = len(dataset_id_list)<EOL>if n_dataset != <NUM_LIT:1>:<EOL><INDENT>print("<STR_LIT>".format(n_dataset))<EOL><DEDENT>return dataset<EOL><DEDENT>except Exception as e:<EOL><INDENT>exc_type, exc_obj, exc_tb = sys.exc_info()<EOL>fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[<NUM_LIT:1>]<EOL>logging.info("<STR_LIT>".format(exc_type, exc_obj, fname, exc_tb.tb_lineno, e))<EOL>return False<EOL><DEDENT>

Finds and returns a dataset from the database which matches the requirement.

        Parameters
        ----------
        dataset_name : str
            The name of dataset.
        sort : List of tuple
            PyMongo sort comment, search "PyMongo find one sorting" and `collection level operations <http://api.mongodb.com/python/current/api/pymongo/collection.html>`__ for more details.
        kwargs : other events
            Other events, such as description, author and etc (optinal).

        Examples
        ---------
        Save dataset
        >>> db.save_dataset([X_train, y_train, X_test, y_test], 'mnist', description='this is a tutorial')

        Get dataset
        >>> dataset = db.find_top_dataset('mnist')
        >>> datasets = db.find_datasets('mnist')

        Returns
        --------
        dataset : the dataset or False
            Return False if nothing found.


def find_datasets(self, dataset_name=None, **kwargs):

self._fill_project_info(kwargs)<EOL>if dataset_name is None:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>kwargs.update({'<STR_LIT>': dataset_name})<EOL>s = time.time()<EOL>pc = self.db.Dataset.find(kwargs)<EOL>if pc is not None:<EOL><INDENT>dataset_id_list = pc.distinct('<STR_LIT>')<EOL>dataset_list = []<EOL>for dataset_id in dataset_id_list:  <EOL><INDENT>tmp = self.dataset_fs.get(dataset_id).read()<EOL>dataset_list.append(self._deserialization(tmp))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>print("<STR_LIT>".format(kwargs))<EOL>return False<EOL><DEDENT>print("<STR_LIT>".format(len(dataset_list), round(time.time() - s, <NUM_LIT:2>)))<EOL>return dataset_list<EOL>

Finds and returns all datasets from the database which matches the requirement.
        In some case, the data in a dataset can be stored separately for better management.

        Parameters
        ----------
        dataset_name : str
            The name/key of dataset.
        kwargs : other events
            Other events, such as description, author and etc (optional).

        Returns
        --------
        params : the parameters, return False if nothing found.


self._fill_project_info(kwargs)<EOL>self.db.Dataset.delete_many(kwargs)<EOL>logging.info("<STR_LIT>")<EOL>

Delete datasets.

        Parameters
        -----------
        kwargs : logging information
            Find items to delete, leave it empty to delete all log.


self._fill_project_info(kwargs)<EOL>kwargs.update({'<STR_LIT:time>': datetime.utcnow()})<EOL>_result = self.db.TrainLog.insert_one(kwargs)<EOL>_log = self._print_dict(kwargs)<EOL>logging.info("<STR_LIT>" + _log)<EOL>

Saves the training log, timestamp will be added automatically.

        Parameters
        -----------
        kwargs : logging information
            Events, such as accuracy, loss, step number and etc.

        Examples
        ---------
        >>> db.save_training_log(accuracy=0.33, loss=0.98)


self._fill_project_info(kwargs)<EOL>kwargs.update({'<STR_LIT:time>': datetime.utcnow()})<EOL>_result = self.db.ValidLog.insert_one(kwargs)<EOL>_log = self._print_dict(kwargs)<EOL>logging.info("<STR_LIT>" + _log)<EOL>

Saves the validation log, timestamp will be added automatically.

        Parameters
        -----------
        kwargs : logging information
            Events, such as accuracy, loss, step number and etc.

        Examples
        ---------
        >>> db.save_validation_log(accuracy=0.33, loss=0.98)


self._fill_project_info(kwargs)<EOL>self.db.TrainLog.delete_many(kwargs)<EOL>logging.info("<STR_LIT>")<EOL>

Deletes training log.

        Parameters
        -----------
        kwargs : logging information
            Find items to delete, leave it empty to delete all log.

        Examples
        ---------
        Save training log
        >>> db.save_training_log(accuracy=0.33)
        >>> db.save_training_log(accuracy=0.44)

        Delete logs that match the requirement
        >>> db.delete_training_log(accuracy=0.33)

        Delete all logs
        >>> db.delete_training_log()


self._fill_project_info(kwargs)<EOL>self.db.ValidLog.delete_many(kwargs)<EOL>logging.info("<STR_LIT>")<EOL>

Deletes validation log.

        Parameters
        -----------
        kwargs : logging information
            Find items to delete, leave it empty to delete all log.

        Examples
        ---------
        - see ``save_training_log``.


def create_task(self, task_name=None, script=None, hyper_parameters=None, saved_result_keys=None, **kwargs):

if not isinstance(task_name, str):  <EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>if not isinstance(script, str):  <EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>if hyper_parameters is None:<EOL><INDENT>hyper_parameters = {}<EOL><DEDENT>if saved_result_keys is None:<EOL><INDENT>saved_result_keys = []<EOL><DEDENT>self._fill_project_info(kwargs)<EOL>kwargs.update({'<STR_LIT:time>': datetime.utcnow()})<EOL>kwargs.update({'<STR_LIT>': hyper_parameters})<EOL>kwargs.update({'<STR_LIT>': saved_result_keys})<EOL>_script = open(script, '<STR_LIT:rb>').read()<EOL>kwargs.update({'<STR_LIT:status>': '<STR_LIT>', '<STR_LIT>': _script, '<STR_LIT:result>': {}})<EOL>self.db.Task.insert_one(kwargs)<EOL>logging.info("<STR_LIT>".format(task_name, script))<EOL>

Uploads a task to the database, timestamp will be added automatically.

        Parameters
        -----------
        task_name : str
            The task name.
        script : str
            File name of the python script.
        hyper_parameters : dictionary
            The hyper parameters pass into the script.
        saved_result_keys : list of str
            The keys of the task results to keep in the database when the task finishes.
        kwargs : other parameters
            Users customized parameters such as description, version number.

        Examples
        -----------
        Uploads a task
        >>> db.create_task(task_name='mnist', script='example/tutorial_mnist_simple.py', description='simple tutorial')

        Finds and runs the latest task
        >>> db.run_top_task(sess=sess, sort=[("time", pymongo.DESCENDING)])
        >>> db.run_top_task(sess=sess, sort=[("time", -1)])

        Finds and runs the oldest task
        >>> db.run_top_task(sess=sess, sort=[("time", pymongo.ASCENDING)])
        >>> db.run_top_task(sess=sess, sort=[("time", 1)])


def run_top_task(self, task_name=None, sort=None, **kwargs):

if not isinstance(task_name, str):  <EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>self._fill_project_info(kwargs)<EOL>kwargs.update({'<STR_LIT:status>': '<STR_LIT>'})<EOL>task = self.db.Task.find_one_and_update(kwargs, {'<STR_LIT>': {'<STR_LIT:status>': '<STR_LIT>'}}, sort=sort)<EOL>try:<EOL><INDENT>if task is None:<EOL><INDENT>logging.info("<STR_LIT>".format(task_name, sort))<EOL>return False<EOL><DEDENT>else:<EOL><INDENT>logging.info("<STR_LIT>".format(task_name, sort))<EOL><DEDENT>_datetime = task['<STR_LIT:time>']<EOL>_script = task['<STR_LIT>']<EOL>_id = task['<STR_LIT>']<EOL>_hyper_parameters = task['<STR_LIT>']<EOL>_saved_result_keys = task['<STR_LIT>']<EOL>logging.info("<STR_LIT>")<EOL>for key in _hyper_parameters:<EOL><INDENT>globals()[key] = _hyper_parameters[key]<EOL>logging.info("<STR_LIT>".format(key, _hyper_parameters[key]))<EOL><DEDENT>s = time.time()<EOL>logging.info("<STR_LIT>".format(task_name, sort, _datetime))<EOL>_script = _script.decode('<STR_LIT:utf-8>')<EOL>with tf.Graph().as_default():  <EOL><INDENT>exec(_script, globals())<EOL><DEDENT>_ = self.db.Task.find_one_and_update({'<STR_LIT>': _id}, {'<STR_LIT>': {'<STR_LIT:status>': '<STR_LIT>'}})<EOL>__result = {}<EOL>for _key in _saved_result_keys:<EOL><INDENT>logging.info("<STR_LIT>".format(_key, globals()[_key], type(globals()[_key])))<EOL>__result.update({"<STR_LIT:%s>" % _key: globals()[_key]})<EOL><DEDENT>_ = self.db.Task.find_one_and_update(<EOL>{<EOL>'<STR_LIT>': _id<EOL>}, {'<STR_LIT>': {<EOL>'<STR_LIT:result>': __result<EOL>}}, return_document=pymongo.ReturnDocument.AFTER<EOL>)<EOL>logging.info(<EOL>"<STR_LIT>".<EOL>format(task_name, sort, _datetime,<EOL>time.time() - s)<EOL>)<EOL>return True<EOL><DEDENT>except Exception as e:<EOL><INDENT>exc_type, exc_obj, exc_tb = sys.exc_info()<EOL>fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[<NUM_LIT:1>]<EOL>logging.info("<STR_LIT>".format(exc_type, exc_obj, fname, exc_tb.tb_lineno, e))<EOL>logging.info("<STR_LIT>")<EOL>_ = self.db.Task.find_one_and_update({'<STR_LIT>': _id}, {'<STR_LIT>': {'<STR_LIT:status>': '<STR_LIT>'}})<EOL>return False<EOL><DEDENT>

Finds and runs a pending task that in the first of the sorting list.

        Parameters
        -----------
        task_name : str
            The task name.
        sort : List of tuple
            PyMongo sort comment, search "PyMongo find one sorting" and `collection level operations <http://api.mongodb.com/python/current/api/pymongo/collection.html>`__ for more details.
        kwargs : other parameters
            Users customized parameters such as description, version number.

        Examples
        ---------
        Monitors the database and pull tasks to run
        >>> while True:
        >>>     print("waiting task from distributor")
        >>>     db.run_top_task(task_name='mnist', sort=[("time", -1)])
        >>>     time.sleep(1)

        Returns
        --------
        boolean : True for success, False for fail.


self._fill_project_info(kwargs)<EOL>self.db.Task.delete_many(kwargs)<EOL>logging.info("<STR_LIT>")<EOL>

Delete tasks.

        Parameters
        -----------
        kwargs : logging information
            Find items to delete, leave it empty to delete all log.

        Examples
        ---------
        >>> db.delete_tasks()


def check_unfinished_task(self, task_name=None, **kwargs):

if not isinstance(task_name, str):  <EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>self._fill_project_info(kwargs)<EOL>kwargs.update({'<STR_LIT>': [{'<STR_LIT:status>': '<STR_LIT>'}, {'<STR_LIT:status>': '<STR_LIT>'}]})<EOL>task = self.db.Task.find(kwargs)<EOL>task_id_list = task.distinct('<STR_LIT>')<EOL>n_task = len(task_id_list)<EOL>if n_task == <NUM_LIT:0>:<EOL><INDENT>logging.info("<STR_LIT>".format(task_name))<EOL>return False<EOL><DEDENT>else:<EOL><INDENT>logging.info("<STR_LIT>".format(n_task, task_name))<EOL>return True<EOL><DEDENT>

Finds and runs a pending task.

        Parameters
        -----------
        task_name : str
            The task name.
        kwargs : other parameters
            Users customized parameters such as description, version number.

        Examples
        ---------
        Wait until all tasks finish in user's local console

        >>> while not db.check_unfinished_task():
        >>>     time.sleep(1)
        >>> print("all tasks finished")
        >>> sess = tf.InteractiveSession()
        >>> net = db.find_top_model(sess=sess, sort=[("test_accuracy", -1)])
        >>> print("the best accuracy {} is from model {}".format(net._test_accuracy, net._name))

        Returns
        --------
        boolean : True for success, False for fail.


def ramp(x, v_min=<NUM_LIT:0>, v_max=<NUM_LIT:1>, name=None):

return tf.clip_by_value(x, clip_value_min=v_min, clip_value_max=v_max, name=name)<EOL>

Ramp activation function.

    Parameters
    ----------
    x : Tensor
        input.
    v_min : float
        cap input to v_min as a lower bound.
    v_max : float
        cap input to v_max as a upper bound.
    name : str
        The function name (optional).

    Returns
    -------
    Tensor
        A ``Tensor`` in the same type as ``x``.


@deprecated(date="<STR_LIT>", instructions="<STR_LIT>")<EOL>def leaky_relu(x, alpha=<NUM_LIT>, name="<STR_LIT>"):

if not (<NUM_LIT:0> < alpha <= <NUM_LIT:1>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>with tf.name_scope(name, "<STR_LIT>") as name_scope:<EOL><INDENT>x = tf.convert_to_tensor(x, name="<STR_LIT>")<EOL>return tf.maximum(x, alpha * x, name=name_scope)<EOL><DEDENT>

leaky_relu can be used through its shortcut: :func:`tl.act.lrelu`.

    This function is a modified version of ReLU, introducing a nonzero gradient for negative input. Introduced by the paper:
    `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__

    The function return the following results:
      - When x < 0: ``f(x) = alpha_low * x``.
      - When x >= 0: ``f(x) = x``.

    Parameters
    ----------
    x : Tensor
        Support input type ``float``, ``double``, ``int32``, ``int64``, ``uint8``, ``int16``, or ``int8``.
    alpha : float
        Slope.
    name : str
        The function name (optional).

    Examples
    --------
    >>> import tensorlayer as tl
    >>> net = tl.layers.DenseLayer(net, 100, act=lambda x : tl.act.lrelu(x, 0.2), name='dense')

    Returns
    -------
    Tensor
        A ``Tensor`` in the same type as ``x``.

    References
    ----------
    - `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__


def leaky_relu6(x, alpha=<NUM_LIT>, name="<STR_LIT>"):

if not isinstance(alpha, tf.Tensor) and not (<NUM_LIT:0> < alpha <= <NUM_LIT:1>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>with tf.name_scope(name, "<STR_LIT>") as name_scope:<EOL><INDENT>x = tf.convert_to_tensor(x, name="<STR_LIT>")<EOL>return tf.minimum(tf.maximum(x, alpha * x), <NUM_LIT:6>, name=name_scope)<EOL><DEDENT>

:func:`leaky_relu6` can be used through its shortcut: :func:`tl.act.lrelu6`.

    This activation function is a modified version :func:`leaky_relu` introduced by the following paper:
    `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__

    This activation function also follows the behaviour of the activation function :func:`tf.nn.relu6` introduced by the following paper:
    `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__

    The function return the following results:
      - When x < 0: ``f(x) = alpha_low * x``.
      - When x in [0, 6]: ``f(x) = x``.
      - When x > 6: ``f(x) = 6``.

    Parameters
    ----------
    x : Tensor
        Support input type ``float``, ``double``, ``int32``, ``int64``, ``uint8``, ``int16``, or ``int8``.
    alpha : float
        Slope.
    name : str
        The function name (optional).

    Examples
    --------
    >>> import tensorlayer as tl
    >>> net = tl.layers.DenseLayer(net, 100, act=lambda x : tl.act.leaky_relu6(x, 0.2), name='dense')

    Returns
    -------
    Tensor
        A ``Tensor`` in the same type as ``x``.

    References
    ----------
    - `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
    - `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__


def leaky_twice_relu6(x, alpha_low=<NUM_LIT>, alpha_high=<NUM_LIT>, name="<STR_LIT>"):

if not isinstance(alpha_high, tf.Tensor) and not (<NUM_LIT:0> < alpha_high <= <NUM_LIT:1>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not isinstance(alpha_low, tf.Tensor) and not (<NUM_LIT:0> < alpha_low <= <NUM_LIT:1>):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>with tf.name_scope(name, "<STR_LIT>") as name_scope:<EOL><INDENT>x = tf.convert_to_tensor(x, name="<STR_LIT>")<EOL>x_is_above_0 = tf.minimum(x, <NUM_LIT:6> * (<NUM_LIT:1> - alpha_high) + alpha_high * x)<EOL>x_is_below_0 = tf.minimum(alpha_low * x, <NUM_LIT:0>)<EOL>return tf.maximum(x_is_above_0, x_is_below_0, name=name_scope)<EOL><DEDENT>

:func:`leaky_twice_relu6` can be used through its shortcut: :func:`:func:`tl.act.ltrelu6`.

    This activation function is a modified version :func:`leaky_relu` introduced by the following paper:
    `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__

    This activation function also follows the behaviour of the activation function :func:`tf.nn.relu6` introduced by the following paper:
    `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__

    This function push further the logic by adding `leaky` behaviour both below zero and above six.

    The function return the following results:
      - When x < 0: ``f(x) = alpha_low * x``.
      - When x in [0, 6]: ``f(x) = x``.
      - When x > 6: ``f(x) = 6 + (alpha_high * (x-6))``.

    Parameters
    ----------
    x : Tensor
        Support input type ``float``, ``double``, ``int32``, ``int64``, ``uint8``, ``int16``, or ``int8``.
    alpha_low : float
        Slope for x < 0: ``f(x) = alpha_low * x``.
    alpha_high : float
        Slope for x < 6: ``f(x) = 6 (alpha_high * (x-6))``.
    name : str
        The function name (optional).

    Examples
    --------
    >>> import tensorlayer as tl
    >>> net = tl.layers.DenseLayer(net, 100, act=lambda x : tl.act.leaky_twice_relu6(x, 0.2, 0.2), name='dense')

    Returns
    -------
    Tensor
        A ``Tensor`` in the same type as ``x``.

    References
    ----------
    - `Rectifier Nonlinearities Improve Neural Network Acoustic Models [A. L. Maas et al., 2013] <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`__
    - `Convolutional Deep Belief Networks on CIFAR-10 [A. Krizhevsky, 2010] <http://www.cs.utoronto.ca/~kriz/conv-cifar10-aug2010.pdf>`__


with tf.name_scope(name):<EOL><INDENT>x = tf.nn.sigmoid(x) * x<EOL><DEDENT>return x<EOL>

Swish function.

     See `Swish: a Self-Gated Activation Function <https://arxiv.org/abs/1710.05941>`__.

    Parameters
    ----------
    x : Tensor
        input.
    name: str
        function name (optional).

    Returns
    -------
    Tensor
        A ``Tensor`` in the same type as ``x``.


with tf.get_default_graph().gradient_override_map({"<STR_LIT>": "<STR_LIT>"}):<EOL><INDENT>return tf.sign(x, name='<STR_LIT>')<EOL><DEDENT>

Sign function.

    Clip and binarize tensor using the straight through estimator (STE) for the gradient, usually be used for
    quantizing values in `Binarized Neural Networks`: https://arxiv.org/abs/1602.02830.

    Parameters
    ----------
    x : Tensor
        input.

    Examples
    --------
    >>> net = tl.layers.DenseLayer(net, 100, act=lambda x : tl.act.lrelu(x, 0.2), name='dense')

    Returns
    -------
    Tensor
        A ``Tensor`` in the same type as ``x``.

    References
    ----------
    - `Rectifier Nonlinearities Improve Neural Network Acoustic Models, Maas et al. (2013)`
       http://web.stanford.edu/~awni/papers/relu_hybrid_icml2013_final.pdf

    - `BinaryNet: Training Deep Neural Networks with Weights and Activations Constrained to +1 or -1, Courbariaux et al. (2016)`
       https://arxiv.org/abs/1602.02830


<EOL>return tf.clip_by_value(x, -<NUM_LIT:1>, <NUM_LIT:1>, name=name)<EOL>

Hard tanh activation function.

    Which is a ramp function with low bound of -1 and upper bound of 1, shortcut is `htanh`.

    Parameters
    ----------
    x : Tensor
        input.
    name : str
        The function name (optional).

    Returns
    -------
    Tensor
        A ``Tensor`` in the same type as ``x``.


@deprecated(date="<STR_LIT>", instructions="<STR_LIT>")<EOL>def pixel_wise_softmax(x, name='<STR_LIT>'):

with tf.name_scope(name):<EOL><INDENT>return tf.nn.softmax(x)<EOL><DEDENT>

Return the softmax outputs of images, every pixels have multiple label, the sum of a pixel is 1.

    Usually be used for image segmentation.

    Parameters
    ----------
    x : Tensor
        input.
            - For 2d image, 4D tensor (batch_size, height, weight, channel), where channel >= 2.
            - For 3d image, 5D tensor (batch_size, depth, height, weight, channel), where channel >= 2.
    name : str
        function name (optional)

    Returns
    -------
    Tensor
        A ``Tensor`` in the same type as ``x``.

    Examples
    --------
    >>> outputs = pixel_wise_softmax(network.outputs)
    >>> dice_loss = 1 - dice_coe(outputs, y_, epsilon=1e-5)

    References
    ----------
    - `tf.reverse <https://www.tensorflow.org/versions/master/api_docs/python/array_ops.html#reverse>`__


def discount_episode_rewards(rewards=None, gamma=<NUM_LIT>, mode=<NUM_LIT:0>):

if rewards is None:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>discounted_r = np.zeros_like(rewards, dtype=np.float32)<EOL>running_add = <NUM_LIT:0><EOL>for t in reversed(xrange(<NUM_LIT:0>, rewards.size)):<EOL><INDENT>if mode == <NUM_LIT:0>:<EOL><INDENT>if rewards[t] != <NUM_LIT:0>: running_add = <NUM_LIT:0><EOL><DEDENT>running_add = running_add * gamma + rewards[t]<EOL>discounted_r[t] = running_add<EOL><DEDENT>return discounted_r<EOL>

Take 1D float array of rewards and compute discounted rewards for an
    episode. When encount a non-zero value, consider as the end a of an episode.

    Parameters
    ----------
    rewards : list
        List of rewards
    gamma : float
        Discounted factor
    mode : int
        Mode for computing the discount rewards.
            - If mode == 0, reset the discount process when encount a non-zero reward (Ping-pong game).
            - If mode == 1, would not reset the discount process.

    Returns
    --------
    list of float
        The discounted rewards.

    Examples
    ----------
    >>> rewards = np.asarray([0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1])
    >>> gamma = 0.9
    >>> discount_rewards = tl.rein.discount_episode_rewards(rewards, gamma)
    >>> print(discount_rewards)
    [ 0.72899997  0.81        0.89999998  1.          0.72899997  0.81
    0.89999998  1.          0.72899997  0.81        0.89999998  1.        ]
    >>> discount_rewards = tl.rein.discount_episode_rewards(rewards, gamma, mode=1)
    >>> print(discount_rewards)
    [ 1.52110755  1.69011939  1.87791049  2.08656716  1.20729685  1.34144104
    1.49048996  1.65610003  0.72899997  0.81        0.89999998  1.        ]


def cross_entropy_reward_loss(logits, actions, rewards, name=None):

cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=actions, logits=logits, name=name)<EOL>return tf.reduce_sum(tf.multiply(cross_entropy, rewards))<EOL>

Calculate the loss for Policy Gradient Network.

    Parameters
    ----------
    logits : tensor
        The network outputs without softmax. This function implements softmax inside.
    actions : tensor or placeholder
        The agent actions.
    rewards : tensor or placeholder
        The rewards.

    Returns
    --------
    Tensor
        The TensorFlow loss function.

    Examples
    ----------
    >>> states_batch_pl = tf.placeholder(tf.float32, shape=[None, D])
    >>> network = InputLayer(states_batch_pl, name='input')
    >>> network = DenseLayer(network, n_units=H, act=tf.nn.relu, name='relu1')
    >>> network = DenseLayer(network, n_units=3, name='out')
    >>> probs = network.outputs
    >>> sampling_prob = tf.nn.softmax(probs)
    >>> actions_batch_pl = tf.placeholder(tf.int32, shape=[None])
    >>> discount_rewards_batch_pl = tf.placeholder(tf.float32, shape=[None])
    >>> loss = tl.rein.cross_entropy_reward_loss(probs, actions_batch_pl, discount_rewards_batch_pl)
    >>> train_op = tf.train.RMSPropOptimizer(learning_rate, decay_rate).minimize(loss)


def log_weight(probs, weights, name='<STR_LIT>'):

with tf.variable_scope(name):<EOL><INDENT>exp_v = tf.reduce_mean(tf.log(probs) * weights)<EOL>return exp_v<EOL><DEDENT>

Log weight.

    Parameters
    -----------
    probs : tensor
        If it is a network output, usually we should scale it to [0, 1] via softmax.
    weights : tensor
        The weights.

    Returns
    --------
    Tensor
        The Tensor after appling the log weighted expression.


def choice_action_by_probs(probs=(<NUM_LIT:0.5>, <NUM_LIT:0.5>), action_list=None):

if action_list is None:<EOL><INDENT>n_action = len(probs)<EOL>action_list = np.arange(n_action)<EOL><DEDENT>else:<EOL><INDENT>if len(action_list) != len(probs):<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT><DEDENT>return np.random.choice(action_list, p=probs)<EOL>

Choice and return an an action by given the action probability distribution.

    Parameters
    ------------
    probs : list of float.
        The probability distribution of all actions.
    action_list : None or a list of int or others
        A list of action in integer, string or others. If None, returns an integer range between 0 and len(probs)-1.

    Returns
    --------
    float int or str
        The chosen action.

    Examples
    ----------
    >>> for _ in range(5):
    >>>     a = choice_action_by_probs([0.2, 0.4, 0.4])
    >>>     print(a)
    0
    1
    1
    2
    1
    >>> for _ in range(3):
    >>>     a = choice_action_by_probs([0.5, 0.5], ['a', 'b'])
    >>>     print(a)
    a
    b
    b


return imageio.imread(os.path.join(path, image))<EOL>

Read one image.

    Parameters
    -----------
    image : str
        The image file name.
    path : str
        The image folder path.

    Returns
    -------
    numpy.array
        The image.


def read_images(img_list, path='<STR_LIT>', n_threads=<NUM_LIT:10>, printable=True):

imgs = []<EOL>for idx in range(<NUM_LIT:0>, len(img_list), n_threads):<EOL><INDENT>b_imgs_list = img_list[idx:idx + n_threads]<EOL>b_imgs = tl.prepro.threading_data(b_imgs_list, fn=read_image, path=path)<EOL>imgs.extend(b_imgs)<EOL>if printable:<EOL><INDENT>tl.logging.info('<STR_LIT>' % (len(imgs), path))<EOL><DEDENT><DEDENT>return imgs<EOL>

Returns all images in list by given path and name of each image file.

    Parameters
    -------------
    img_list : list of str
        The image file names.
    path : str
        The image folder path.
    n_threads : int
        The number of threads to read image.
    printable : boolean
        Whether to print information when reading images.

    Returns
    -------
    list of numpy.array
        The images.


try:  <EOL><INDENT>imageio.imwrite(image_path, image)<EOL><DEDENT>except Exception:  <EOL><INDENT>imageio.imwrite(image_path, image[:, :, <NUM_LIT:0>])<EOL><DEDENT>

Save a image.

    Parameters
    -----------
    image : numpy array
        [w, h, c]
    image_path : str
        path


def save_images(images, size, image_path='<STR_LIT>'):

if len(images.shape) == <NUM_LIT:3>:  <EOL><INDENT>images = images[:, :, :, np.newaxis]<EOL><DEDENT>def merge(images, size):<EOL><INDENT>h, w = images.shape[<NUM_LIT:1>], images.shape[<NUM_LIT:2>]<EOL>img = np.zeros((h * size[<NUM_LIT:0>], w * size[<NUM_LIT:1>], <NUM_LIT:3>), dtype=images.dtype)<EOL>for idx, image in enumerate(images):<EOL><INDENT>i = idx % size[<NUM_LIT:1>]<EOL>j = idx // size[<NUM_LIT:1>]<EOL>img[j * h:j * h + h, i * w:i * w + w, :] = image<EOL><DEDENT>return img<EOL><DEDENT>def imsave(images, size, path):<EOL><INDENT>if np.max(images) <= <NUM_LIT:1> and (-<NUM_LIT:1> <= np.min(images) < <NUM_LIT:0>):<EOL><INDENT>images = ((images + <NUM_LIT:1>) * <NUM_LIT>).astype(np.uint8)<EOL><DEDENT>elif np.max(images) <= <NUM_LIT:1> and np.min(images) >= <NUM_LIT:0>:<EOL><INDENT>images = (images * <NUM_LIT:255>).astype(np.uint8)<EOL><DEDENT>return imageio.imwrite(path, merge(images, size))<EOL><DEDENT>if len(images) > size[<NUM_LIT:0>] * size[<NUM_LIT:1>]:<EOL><INDENT>raise AssertionError("<STR_LIT>".format(len(images)))<EOL><DEDENT>return imsave(images, size, image_path)<EOL>

Save multiple images into one single image.

    Parameters
    -----------
    images : numpy array
        (batch, w, h, c)
    size : list of 2 ints
        row and column number.
        number of images should be equal or less than size[0] * size[1]
    image_path : str
        save path

    Examples
    ---------
    >>> import numpy as np
    >>> import tensorlayer as tl
    >>> images = np.random.rand(64, 100, 100, 3)
    >>> tl.visualize.save_images(images, [8, 8], 'temp.png')


def draw_boxes_and_labels_to_image(<EOL>image, classes, coords, scores, classes_list, is_center=True, is_rescale=True, save_name=None<EOL>):

if len(coords) != len(classes):<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>if len(scores) > <NUM_LIT:0> and len(scores) != len(classes):<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>image = image.copy()<EOL>imh, imw = image.shape[<NUM_LIT:0>:<NUM_LIT:2>]<EOL>thick = int((imh + imw) // <NUM_LIT>)<EOL>for i, _v in enumerate(coords):<EOL><INDENT>if is_center:<EOL><INDENT>x, y, x2, y2 = tl.prepro.obj_box_coord_centroid_to_upleft_butright(coords[i])<EOL><DEDENT>else:<EOL><INDENT>x, y, x2, y2 = coords[i]<EOL><DEDENT>if is_rescale:  <EOL><INDENT>x, y, x2, y2 = tl.prepro.obj_box_coord_scale_to_pixelunit([x, y, x2, y2], (imh, imw))<EOL><DEDENT>cv2.rectangle(<EOL>image,<EOL>(int(x), int(y)),<EOL>(int(x2), int(y2)),  <EOL>[<NUM_LIT:0>, <NUM_LIT:255>, <NUM_LIT:0>],<EOL>thick<EOL>)<EOL>cv2.putText(<EOL>image,<EOL>classes_list[classes[i]] + (("<STR_LIT>" % (scores[i])) if (len(scores) != <NUM_LIT:0>) else "<STR_LIT:U+0020>"),<EOL>(int(x), int(y)),  <EOL><NUM_LIT:0>,<EOL><NUM_LIT> * imh,  <EOL>[<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT>],  <EOL>int(thick / <NUM_LIT:2>) + <NUM_LIT:1><EOL>)  <EOL><DEDENT>if save_name is not None:<EOL><INDENT>save_image(image, save_name)<EOL><DEDENT>return image<EOL>

Draw bboxes and class labels on image. Return or save the image with bboxes, example in the docs of ``tl.prepro``.

    Parameters
    -----------
    image : numpy.array
        The RGB image [height, width, channel].
    classes : list of int
        A list of class ID (int).
    coords : list of int
        A list of list for coordinates.
            - Should be [x, y, x2, y2] (up-left and botton-right format)
            - If [x_center, y_center, w, h] (set is_center to True).
    scores : list of float
        A list of score (float). (Optional)
    classes_list : list of str
        for converting ID to string on image.
    is_center : boolean
        Whether the coordinates is [x_center, y_center, w, h]
            - If coordinates are [x_center, y_center, w, h], set it to True for converting it to [x, y, x2, y2] (up-left and botton-right) internally.
            - If coordinates are [x1, x2, y1, y2], set it to False.
    is_rescale : boolean
        Whether to rescale the coordinates from pixel-unit format to ratio format.
            - If True, the input coordinates are the portion of width and high, this API will scale the coordinates to pixel unit internally.
            - If False, feed the coordinates with pixel unit format.
    save_name : None or str
        The name of image file (i.e. image.png), if None, not to save image.

    Returns
    -------
    numpy.array
        The saved image.

    References
    -----------
    - OpenCV rectangle and putText.
    - `scikit-image <http://scikit-image.org/docs/dev/api/skimage.draw.html#skimage.draw.rectangle>`__.


def draw_mpii_pose_to_image(image, poses, save_name='<STR_LIT>'):

<EOL>image = image.copy()<EOL>imh, imw = image.shape[<NUM_LIT:0>:<NUM_LIT:2>]<EOL>thick = int((imh + imw) // <NUM_LIT>)<EOL>radius = int(thick * <NUM_LIT>)<EOL>if image.max() < <NUM_LIT:1>:<EOL><INDENT>image = image * <NUM_LIT:255><EOL><DEDENT>for people in poses:<EOL><INDENT>joint_pos = people['<STR_LIT>']<EOL>lines = [<EOL>[(<NUM_LIT:0>, <NUM_LIT:1>), [<NUM_LIT:100>, <NUM_LIT:255>, <NUM_LIT:100>]],<EOL>[(<NUM_LIT:1>, <NUM_LIT:2>), [<NUM_LIT:50>, <NUM_LIT:255>, <NUM_LIT:50>]],<EOL>[(<NUM_LIT:2>, <NUM_LIT:6>), [<NUM_LIT:0>, <NUM_LIT:255>, <NUM_LIT:0>]],  <EOL>[(<NUM_LIT:3>, <NUM_LIT:4>), [<NUM_LIT:100>, <NUM_LIT:100>, <NUM_LIT:255>]],<EOL>[(<NUM_LIT:4>, <NUM_LIT:5>), [<NUM_LIT:50>, <NUM_LIT:50>, <NUM_LIT:255>]],<EOL>[(<NUM_LIT:6>, <NUM_LIT:3>), [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:255>]],  <EOL>[(<NUM_LIT:6>, <NUM_LIT:7>), [<NUM_LIT:255>, <NUM_LIT:255>, <NUM_LIT:100>]],<EOL>[(<NUM_LIT:7>, <NUM_LIT:8>), [<NUM_LIT:255>, <NUM_LIT>, <NUM_LIT:50>]],  <EOL>[(<NUM_LIT:8>, <NUM_LIT:9>), [<NUM_LIT:255>, <NUM_LIT:200>, <NUM_LIT:100>]],  <EOL>[(<NUM_LIT:10>, <NUM_LIT:11>), [<NUM_LIT:255>, <NUM_LIT:100>, <NUM_LIT:255>]],<EOL>[(<NUM_LIT:11>, <NUM_LIT:12>), [<NUM_LIT:255>, <NUM_LIT:50>, <NUM_LIT:255>]],<EOL>[(<NUM_LIT:12>, <NUM_LIT:8>), [<NUM_LIT:255>, <NUM_LIT:0>, <NUM_LIT:255>]],  <EOL>[(<NUM_LIT:8>, <NUM_LIT>), [<NUM_LIT:0>, <NUM_LIT:255>, <NUM_LIT:255>]],<EOL>[(<NUM_LIT>, <NUM_LIT>), [<NUM_LIT:100>, <NUM_LIT:255>, <NUM_LIT:255>]],<EOL>[(<NUM_LIT>, <NUM_LIT:15>), [<NUM_LIT:200>, <NUM_LIT:255>, <NUM_LIT:255>]]  <EOL>]<EOL>for line in lines:<EOL><INDENT>start, end = line[<NUM_LIT:0>]<EOL>if (start in joint_pos) and (end in joint_pos):<EOL><INDENT>cv2.line(<EOL>image,<EOL>(int(joint_pos[start][<NUM_LIT:0>]), int(joint_pos[start][<NUM_LIT:1>])),<EOL>(int(joint_pos[end][<NUM_LIT:0>]), int(joint_pos[end][<NUM_LIT:1>])),  <EOL>line[<NUM_LIT:1>],<EOL>thick<EOL>)<EOL><DEDENT><DEDENT>for pos in joint_pos.items():<EOL><INDENT>_, pos_loc = pos  <EOL>pos_loc = (int(pos_loc[<NUM_LIT:0>]), int(pos_loc[<NUM_LIT:1>]))<EOL>cv2.circle(image, center=pos_loc, radius=radius, color=(<NUM_LIT:200>, <NUM_LIT:200>, <NUM_LIT:200>), thickness=-<NUM_LIT:1>)<EOL><DEDENT>head_rect = people['<STR_LIT>']<EOL>if head_rect:  <EOL><INDENT>cv2.rectangle(<EOL>image,<EOL>(int(head_rect[<NUM_LIT:0>]), int(head_rect[<NUM_LIT:1>])),<EOL>(int(head_rect[<NUM_LIT:2>]), int(head_rect[<NUM_LIT:3>])),  <EOL>[<NUM_LIT:0>, <NUM_LIT>, <NUM_LIT:0>],<EOL>thick<EOL>)<EOL><DEDENT><DEDENT>if save_name is not None:<EOL><INDENT>save_image(image, save_name)<EOL><DEDENT>return image<EOL>

Draw people(s) into image using MPII dataset format as input, return or save the result image.

    This is an experimental API, can be changed in the future.

    Parameters
    -----------
    image : numpy.array
        The RGB image [height, width, channel].
    poses : list of dict
        The people(s) annotation in MPII format, see ``tl.files.load_mpii_pose_dataset``.
    save_name : None or str
        The name of image file (i.e. image.png), if None, not to save image.

    Returns
    --------
    numpy.array
        The saved image.

    Examples
    --------
    >>> import pprint
    >>> import tensorlayer as tl
    >>> img_train_list, ann_train_list, img_test_list, ann_test_list = tl.files.load_mpii_pose_dataset()
    >>> image = tl.vis.read_image(img_train_list[0])
    >>> tl.vis.draw_mpii_pose_to_image(image, ann_train_list[0], 'image.png')
    >>> pprint.pprint(ann_train_list[0])

    References
    -----------
    - `MPII Keyponts and ID <http://human-pose.mpi-inf.mpg.de/#download>`__


def frame(I=None, second=<NUM_LIT:5>, saveable=True, name='<STR_LIT>', cmap=None, fig_idx=<NUM_LIT>):

import matplotlib.pyplot as plt<EOL>if saveable is False:<EOL><INDENT>plt.ion()<EOL><DEDENT>plt.figure(fig_idx)  <EOL>if len(I.shape) and I.shape[-<NUM_LIT:1>] == <NUM_LIT:1>:  <EOL><INDENT>I = I[:, :, <NUM_LIT:0>]<EOL><DEDENT>plt.imshow(I, cmap)<EOL>plt.title(name)<EOL>if saveable:<EOL><INDENT>plt.savefig(name + '<STR_LIT>', format='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>plt.draw()<EOL>plt.pause(second)<EOL><DEDENT>

Display a frame. Make sure OpenAI Gym render() is disable before using it.

    Parameters
    ----------
    I : numpy.array
        The image.
    second : int
        The display second(s) for the image(s), if saveable is False.
    saveable : boolean
        Save or plot the figure.
    name : str
        A name to save the image, if saveable is True.
    cmap : None or str
        'gray' for greyscale, None for default, etc.
    fig_idx : int
        matplotlib figure index.

    Examples
    --------
    >>> env = gym.make("Pong-v0")
    >>> observation = env.reset()
    >>> tl.visualize.frame(observation)


def CNN2d(CNN=None, second=<NUM_LIT:10>, saveable=True, name='<STR_LIT>', fig_idx=<NUM_LIT>):

import matplotlib.pyplot as plt<EOL>n_mask = CNN.shape[<NUM_LIT:3>]<EOL>n_row = CNN.shape[<NUM_LIT:0>]<EOL>n_col = CNN.shape[<NUM_LIT:1>]<EOL>n_color = CNN.shape[<NUM_LIT:2>]<EOL>row = int(np.sqrt(n_mask))<EOL>col = int(np.ceil(n_mask / row))<EOL>plt.ion()  <EOL>fig = plt.figure(fig_idx)<EOL>count = <NUM_LIT:1><EOL>for _ir in range(<NUM_LIT:1>, row + <NUM_LIT:1>):<EOL><INDENT>for _ic in range(<NUM_LIT:1>, col + <NUM_LIT:1>):<EOL><INDENT>if count > n_mask:<EOL><INDENT>break<EOL><DEDENT>fig.add_subplot(col, row, count)<EOL>if n_color == <NUM_LIT:1>:<EOL><INDENT>plt.imshow(np.reshape(CNN[:, :, :, count - <NUM_LIT:1>], (n_row, n_col)), cmap='<STR_LIT>', interpolation="<STR_LIT>")<EOL><DEDENT>elif n_color == <NUM_LIT:3>:<EOL><INDENT>plt.imshow(<EOL>np.reshape(CNN[:, :, :, count - <NUM_LIT:1>], (n_row, n_col, n_color)), cmap='<STR_LIT>', interpolation="<STR_LIT>"<EOL>)<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>plt.gca().xaxis.set_major_locator(plt.NullLocator())  <EOL>plt.gca().yaxis.set_major_locator(plt.NullLocator())<EOL>count = count + <NUM_LIT:1><EOL><DEDENT><DEDENT>if saveable:<EOL><INDENT>plt.savefig(name + '<STR_LIT>', format='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>plt.draw()<EOL>plt.pause(second)<EOL><DEDENT>

Display a group of RGB or Greyscale CNN masks.

    Parameters
    ----------
    CNN : numpy.array
        The image. e.g: 64 5x5 RGB images can be (5, 5, 3, 64).
    second : int
        The display second(s) for the image(s), if saveable is False.
    saveable : boolean
        Save or plot the figure.
    name : str
        A name to save the image, if saveable is True.
    fig_idx : int
        The matplotlib figure index.

    Examples
    --------
    >>> tl.visualize.CNN2d(network.all_params[0].eval(), second=10, saveable=True, name='cnn1_mnist', fig_idx=2012)


def images2d(images=None, second=<NUM_LIT:10>, saveable=True, name='<STR_LIT>', dtype=None, fig_idx=<NUM_LIT>):

import matplotlib.pyplot as plt<EOL>if dtype:<EOL><INDENT>images = np.asarray(images, dtype=dtype)<EOL><DEDENT>n_mask = images.shape[<NUM_LIT:0>]<EOL>n_row = images.shape[<NUM_LIT:1>]<EOL>n_col = images.shape[<NUM_LIT:2>]<EOL>n_color = images.shape[<NUM_LIT:3>]<EOL>row = int(np.sqrt(n_mask))<EOL>col = int(np.ceil(n_mask / row))<EOL>plt.ion()  <EOL>fig = plt.figure(fig_idx)<EOL>count = <NUM_LIT:1><EOL>for _ir in range(<NUM_LIT:1>, row + <NUM_LIT:1>):<EOL><INDENT>for _ic in range(<NUM_LIT:1>, col + <NUM_LIT:1>):<EOL><INDENT>if count > n_mask:<EOL><INDENT>break<EOL><DEDENT>fig.add_subplot(col, row, count)<EOL>if n_color == <NUM_LIT:1>:<EOL><INDENT>plt.imshow(np.reshape(images[count - <NUM_LIT:1>, :, :], (n_row, n_col)), cmap='<STR_LIT>', interpolation="<STR_LIT>")<EOL><DEDENT>elif n_color == <NUM_LIT:3>:<EOL><INDENT>plt.imshow(images[count - <NUM_LIT:1>, :, :], cmap='<STR_LIT>', interpolation="<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>plt.gca().xaxis.set_major_locator(plt.NullLocator())  <EOL>plt.gca().yaxis.set_major_locator(plt.NullLocator())<EOL>count = count + <NUM_LIT:1><EOL><DEDENT><DEDENT>if saveable:<EOL><INDENT>plt.savefig(name + '<STR_LIT>', format='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>plt.draw()<EOL>plt.pause(second)<EOL><DEDENT>

Display a group of RGB or Greyscale images.

    Parameters
    ----------
    images : numpy.array
        The images.
    second : int
        The display second(s) for the image(s), if saveable is False.
    saveable : boolean
        Save or plot the figure.
    name : str
        A name to save the image, if saveable is True.
    dtype : None or numpy data type
        The data type for displaying the images.
    fig_idx : int
        matplotlib figure index.

    Examples
    --------
    >>> X_train, y_train, X_test, y_test = tl.files.load_cifar10_dataset(shape=(-1, 32, 32, 3), plotable=False)
    >>> tl.visualize.images2d(X_train[0:100,:,:,:], second=10, saveable=False, name='cifar10', dtype=np.uint8, fig_idx=20212)


def tsne_embedding(embeddings, reverse_dictionary, plot_only=<NUM_LIT>, second=<NUM_LIT:5>, saveable=False, name='<STR_LIT>', fig_idx=<NUM_LIT>):

import matplotlib.pyplot as plt<EOL>def plot_with_labels(low_dim_embs, labels, figsize=(<NUM_LIT>, <NUM_LIT>), second=<NUM_LIT:5>, saveable=True, name='<STR_LIT>', fig_idx=<NUM_LIT>):<EOL><INDENT>if low_dim_embs.shape[<NUM_LIT:0>] < len(labels):<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>if saveable is False:<EOL><INDENT>plt.ion()<EOL>plt.figure(fig_idx)<EOL><DEDENT>plt.figure(figsize=figsize)  <EOL>for i, label in enumerate(labels):<EOL><INDENT>x, y = low_dim_embs[i, :]<EOL>plt.scatter(x, y)<EOL>plt.annotate(label, xy=(x, y), xytext=(<NUM_LIT:5>, <NUM_LIT:2>), textcoords='<STR_LIT>', ha='<STR_LIT:right>', va='<STR_LIT>')<EOL><DEDENT>if saveable:<EOL><INDENT>plt.savefig(name + '<STR_LIT>', format='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>plt.draw()<EOL>plt.pause(second)<EOL><DEDENT><DEDENT>try:<EOL><INDENT>from sklearn.manifold import TSNE<EOL>from six.moves import xrange<EOL>tsne = TSNE(perplexity=<NUM_LIT:30>, n_components=<NUM_LIT:2>, init='<STR_LIT>', n_iter=<NUM_LIT>)<EOL>low_dim_embs = tsne.fit_transform(embeddings[:plot_only, :])<EOL>labels = [reverse_dictionary[i] for i in xrange(plot_only)]<EOL>plot_with_labels(low_dim_embs, labels, second=second, saveable=saveable, name=name, fig_idx=fig_idx)<EOL><DEDENT>except ImportError:<EOL><INDENT>_err = "<STR_LIT>"<EOL>tl.logging.error(_err)<EOL>raise ImportError(_err)<EOL><DEDENT>

Visualize the embeddings by using t-SNE.

    Parameters
    ----------
    embeddings : numpy.array
        The embedding matrix.
    reverse_dictionary : dictionary
        id_to_word, mapping id to unique word.
    plot_only : int
        The number of examples to plot, choice the most common words.
    second : int
        The display second(s) for the image(s), if saveable is False.
    saveable : boolean
        Save or plot the figure.
    name : str
        A name to save the image, if saveable is True.
    fig_idx : int
        matplotlib figure index.

    Examples
    --------
    >>> see 'tutorial_word2vec_basic.py'
    >>> final_embeddings = normalized_embeddings.eval()
    >>> tl.visualize.tsne_embedding(final_embeddings, labels, reverse_dictionary,
    ...                   plot_only=500, second=5, saveable=False, name='tsne')


def draw_weights(W=None, second=<NUM_LIT:10>, saveable=True, shape=None, name='<STR_LIT>', fig_idx=<NUM_LIT>):

if shape is None:<EOL><INDENT>shape = [<NUM_LIT>, <NUM_LIT>]<EOL><DEDENT>import matplotlib.pyplot as plt<EOL>if saveable is False:<EOL><INDENT>plt.ion()<EOL><DEDENT>fig = plt.figure(fig_idx)  <EOL>n_units = W.shape[<NUM_LIT:1>]<EOL>num_r = int(np.sqrt(n_units))  <EOL>num_c = int(np.ceil(n_units / num_r))<EOL>count = int(<NUM_LIT:1>)<EOL>for _row in range(<NUM_LIT:1>, num_r + <NUM_LIT:1>):<EOL><INDENT>for _col in range(<NUM_LIT:1>, num_c + <NUM_LIT:1>):<EOL><INDENT>if count > n_units:<EOL><INDENT>break<EOL><DEDENT>fig.add_subplot(num_r, num_c, count)<EOL>feature = W[:, count - <NUM_LIT:1>] / np.sqrt((W[:, count - <NUM_LIT:1>]**<NUM_LIT:2>).sum())<EOL>plt.imshow(<EOL>np.reshape(feature, (shape[<NUM_LIT:0>], shape[<NUM_LIT:1>])), cmap='<STR_LIT>', interpolation="<STR_LIT>"<EOL>)  <EOL>plt.gca().xaxis.set_major_locator(plt.NullLocator())  <EOL>plt.gca().yaxis.set_major_locator(plt.NullLocator())<EOL>count = count + <NUM_LIT:1><EOL><DEDENT><DEDENT>if saveable:<EOL><INDENT>plt.savefig(name + '<STR_LIT>', format='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>plt.draw()<EOL>plt.pause(second)<EOL><DEDENT>

Visualize every columns of the weight matrix to a group of Greyscale img.

    Parameters
    ----------
    W : numpy.array
        The weight matrix
    second : int
        The display second(s) for the image(s), if saveable is False.
    saveable : boolean
        Save or plot the figure.
    shape : a list with 2 int or None
        The shape of feature image, MNIST is [28, 80].
    name : a string
        A name to save the image, if saveable is True.
    fig_idx : int
        matplotlib figure index.

    Examples
    --------
    >>> tl.visualize.draw_weights(network.all_params[0].eval(), second=10, saveable=True, name='weight_of_1st_layer', fig_idx=2012)


def generate_skip_gram_batch(data, batch_size, num_skips, skip_window, data_index=<NUM_LIT:0>):

<EOL>if batch_size % num_skips != <NUM_LIT:0>:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>if num_skips > <NUM_LIT:2> * skip_window:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>batch = np.ndarray(shape=(batch_size), dtype=np.int32)<EOL>labels = np.ndarray(shape=(batch_size, <NUM_LIT:1>), dtype=np.int32)<EOL>span = <NUM_LIT:2> * skip_window + <NUM_LIT:1>  <EOL>buffer = collections.deque(maxlen=span)<EOL>for _ in range(span):<EOL><INDENT>buffer.append(data[data_index])<EOL>data_index = (data_index + <NUM_LIT:1>) % len(data)<EOL><DEDENT>for i in range(batch_size // num_skips):<EOL><INDENT>target = skip_window  <EOL>targets_to_avoid = [skip_window]<EOL>for j in range(num_skips):<EOL><INDENT>while target in targets_to_avoid:<EOL><INDENT>target = random.randint(<NUM_LIT:0>, span - <NUM_LIT:1>)<EOL><DEDENT>targets_to_avoid.append(target)<EOL>batch[i * num_skips + j] = buffer[skip_window]<EOL>labels[i * num_skips + j, <NUM_LIT:0>] = buffer[target]<EOL><DEDENT>buffer.append(data[data_index])<EOL>data_index = (data_index + <NUM_LIT:1>) % len(data)<EOL><DEDENT>return batch, labels, data_index<EOL>

Generate a training batch for the Skip-Gram model.

    See `Word2Vec example <https://github.com/tensorlayer/tensorlayer/blob/master/example/tutorial_word2vec_basic.py>`__.

    Parameters
    ----------
    data : list of data
        To present context, usually a list of integers.
    batch_size : int
        Batch size to return.
    num_skips : int
        How many times to reuse an input to generate a label.
    skip_window : int
        How many words to consider left and right.
    data_index : int
        Index of the context location. This code use `data_index` to instead of yield like ``tl.iterate``.

    Returns
    -------
    batch : list of data
        Inputs.
    labels : list of data
        Labels
    data_index : int
        Index of the context location.

    Examples
    --------
    Setting num_skips=2, skip_window=1, use the right and left words.
    In the same way, num_skips=4, skip_window=2 means use the nearby 4 words.

    >>> data = [1,2,3,4,5,6,7,8,9,10,11]
    >>> batch, labels, data_index = tl.nlp.generate_skip_gram_batch(data=data, batch_size=8, num_skips=2, skip_window=1, data_index=0)
    >>> print(batch)
    [2 2 3 3 4 4 5 5]
    >>> print(labels)
    [[3]
    [1]
    [4]
    [2]
    [5]
    [3]
    [4]
    [6]]


if a is None:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>b = np.copy(a)<EOL>try:<EOL><INDENT>if temperature == <NUM_LIT:1>:<EOL><INDENT>return np.argmax(np.random.multinomial(<NUM_LIT:1>, a, <NUM_LIT:1>))<EOL><DEDENT>if temperature is None:<EOL><INDENT>return np.argmax(a)<EOL><DEDENT>else:<EOL><INDENT>a = np.log(a) / temperature<EOL>a = np.exp(a) / np.sum(np.exp(a))<EOL>return np.argmax(np.random.multinomial(<NUM_LIT:1>, a, <NUM_LIT:1>))<EOL><DEDENT><DEDENT>except Exception:<EOL><INDENT>message = "<STR_LIT>"<EOL>warnings.warn(message, Warning)<EOL>return np.argmax(np.random.multinomial(<NUM_LIT:1>, b, <NUM_LIT:1>))<EOL><DEDENT>

Sample an index from a probability array.

    Parameters
    ----------
    a : list of float
        List of probabilities.
    temperature : float or None
        The higher the more uniform. When a = [0.1, 0.2, 0.7],
            - temperature = 0.7, the distribution will be sharpen [0.05048273,  0.13588945,  0.81362782]
            - temperature = 1.0, the distribution will be the same [0.1,    0.2,    0.7]
            - temperature = 1.5, the distribution will be filtered [0.16008435,  0.25411807,  0.58579758]
            - If None, it will be ``np.argmax(a)``

    Notes
    ------
    - No matter what is the temperature and input list, the sum of all probabilities will be one. Even if input list = [1, 100, 200], the sum of all probabilities will still be one.
    - For large vocabulary size, choice a higher temperature or ``tl.nlp.sample_top`` to avoid error.


if a is None:<EOL><INDENT>a = []<EOL><DEDENT>idx = np.argpartition(a, -top_k)[-top_k:]<EOL>probs = a[idx]<EOL>probs = probs / np.sum(probs)<EOL>choice = np.random.choice(idx, p=probs)<EOL>return choice<EOL>

Sample from ``top_k`` probabilities.

    Parameters
    ----------
    a : list of float
        List of probabilities.
    top_k : int
        Number of candidates to be considered.


def process_sentence(sentence, start_word="<STR_LIT>", end_word="<STR_LIT>"):

if start_word is not None:<EOL><INDENT>process_sentence = [start_word]<EOL><DEDENT>else:<EOL><INDENT>process_sentence = []<EOL><DEDENT>process_sentence.extend(nltk.tokenize.word_tokenize(sentence.lower()))<EOL>if end_word is not None:<EOL><INDENT>process_sentence.append(end_word)<EOL><DEDENT>return process_sentence<EOL>

Seperate a sentence string into a list of string words, add start_word and end_word,
    see ``create_vocab()`` and ``tutorial_tfrecord3.py``.

    Parameters
    ----------
    sentence : str
        A sentence.
    start_word : str or None
        The start word. If None, no start word will be appended.
    end_word : str or None
        The end word. If None, no end word will be appended.

    Returns
    ---------
    list of str
        A list of strings that separated into words.

    Examples
    -----------
    >>> c = "how are you?"
    >>> c = tl.nlp.process_sentence(c)
    >>> print(c)
    ['<S>', 'how', 'are', 'you', '?', '</S>']

    Notes
    -------
    - You have to install the following package.
    - `Installing NLTK <http://www.nltk.org/install.html>`__
    - `Installing NLTK data <http://www.nltk.org/data.html>`__


def create_vocab(sentences, word_counts_output_file, min_word_count=<NUM_LIT:1>):

tl.logging.info("<STR_LIT>")<EOL>counter = Counter()<EOL>for c in sentences:<EOL><INDENT>counter.update(c)<EOL><DEDENT>tl.logging.info("<STR_LIT>" % len(counter))<EOL>word_counts = [x for x in counter.items() if x[<NUM_LIT:1>] >= min_word_count]<EOL>word_counts.sort(key=lambda x: x[<NUM_LIT:1>], reverse=True)<EOL>word_counts = [("<STR_LIT>", <NUM_LIT:0>)] + word_counts  <EOL>tl.logging.info("<STR_LIT>" % len(word_counts))<EOL>with tf.gfile.FastGFile(word_counts_output_file, "<STR_LIT:w>") as f:<EOL><INDENT>f.write("<STR_LIT:\n>".join(["<STR_LIT>" % (w, c) for w, c in word_counts]))<EOL><DEDENT>tl.logging.info("<STR_LIT>" % word_counts_output_file)<EOL>reverse_vocab = [x[<NUM_LIT:0>] for x in word_counts]<EOL>unk_id = len(reverse_vocab)<EOL>vocab_dict = dict([(x, y) for (y, x) in enumerate(reverse_vocab)])<EOL>vocab = SimpleVocabulary(vocab_dict, unk_id)<EOL>return vocab<EOL>

Creates the vocabulary of word to word_id.

    See ``tutorial_tfrecord3.py``.

    The vocabulary is saved to disk in a text file of word counts. The id of each
    word in the file is its corresponding 0-based line number.

    Parameters
    ------------
    sentences : list of list of str
        All sentences for creating the vocabulary.
    word_counts_output_file : str
        The file name.
    min_word_count : int
        Minimum number of occurrences for a word.

    Returns
    --------
    :class:`SimpleVocabulary`
        The simple vocabulary object, see :class:`Vocabulary` for more.

    Examples
    --------
    Pre-process sentences

    >>> captions = ["one two , three", "four five five"]
    >>> processed_capts = []
    >>> for c in captions:
    >>>     c = tl.nlp.process_sentence(c, start_word="<S>", end_word="</S>")
    >>>     processed_capts.append(c)
    >>> print(processed_capts)
    ...[['<S>', 'one', 'two', ',', 'three', '</S>'], ['<S>', 'four', 'five', 'five', '</S>']]

    Create vocabulary

    >>> tl.nlp.create_vocab(processed_capts, word_counts_output_file='vocab.txt', min_word_count=1)
    Creating vocabulary.
      Total words: 8
      Words in vocabulary: 8
      Wrote vocabulary file: vocab.txt

    Get vocabulary object

    >>> vocab = tl.nlp.Vocabulary('vocab.txt', start_word="<S>", end_word="</S>", unk_word="<UNK>")
    INFO:tensorflow:Initializing vocabulary from file: vocab.txt
    [TL] Vocabulary from vocab.txt : <S> </S> <UNK>
    vocabulary with 10 words (includes start_word, end_word, unk_word)
        start_id: 2
        end_id: 3
        unk_id: 9
        pad_id: 0


with open(filename, "<STR_LIT:r>") as f:<EOL><INDENT>words = f.read()<EOL>return words<EOL><DEDENT>

Read context from file without any preprocessing.

    Parameters
    ----------
    filename : str
        A file path (like .txt file)

    Returns
    --------
    str
        The context in a string.


if replace is None:<EOL><INDENT>replace = ['<STR_LIT:\n>', '<STR_LIT>']<EOL><DEDENT>with tf.gfile.GFile(filename, "<STR_LIT:r>") as f:<EOL><INDENT>try:  <EOL><INDENT>context_list = f.read().replace(*replace).split()<EOL><DEDENT>except Exception:  <EOL><INDENT>f.seek(<NUM_LIT:0>)<EOL>replace = [x.encode('<STR_LIT:utf-8>') for x in replace]<EOL>context_list = f.read().replace(*replace).split()<EOL><DEDENT>return context_list<EOL><DEDENT>

Read list format context from a file.

    For customized read_words method, see ``tutorial_generate_text.py``.

    Parameters
    ----------
    filename : str
        a file path.
    replace : list of str
        replace original string by target string.

    Returns
    -------
    list of str
        The context in a list (split using space).