path = os.path.join(path, '<STR_LIT>')<EOL>_WMT_ENFR_TRAIN_URL = "<STR_LIT>"<EOL>_WMT_ENFR_DEV_URL = "<STR_LIT>"<EOL>def gunzip_file(gz_path, new_path):<EOL><INDENT>"""<STR_LIT>"""<EOL>logging.info("<STR_LIT>" % (gz_path, new_path))<EOL>with gzip.open(gz_path, "<STR_LIT:rb>") as gz_file:<EOL><INDENT>with open(new_path, "<STR_LIT:wb>") as new_file:<EOL><INDENT>for line in gz_file:<EOL><INDENT>new_file.write(line)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>def get_wmt_enfr_train_set(path):<EOL><INDENT>"""<STR_LIT>"""<EOL>filename = "<STR_LIT>"<EOL>maybe_download_and_extract(filename, path, _WMT_ENFR_TRAIN_URL, extract=True)<EOL>train_path = os.path.join(path, "<STR_LIT>")<EOL>gunzip_file(train_path + "<STR_LIT>", train_path + "<STR_LIT>")<EOL>gunzip_file(train_path + "<STR_LIT>", train_path + "<STR_LIT>")<EOL>return train_path<EOL><DEDENT>def get_wmt_enfr_dev_set(path):<EOL><INDENT>"""<STR_LIT>"""<EOL>filename = "<STR_LIT>"<EOL>dev_file = maybe_download_and_extract(filename, path, _WMT_ENFR_DEV_URL, extract=False)<EOL>dev_name = "<STR_LIT>"<EOL>dev_path = os.path.join(path, "<STR_LIT>")<EOL>if not (gfile.Exists(dev_path + "<STR_LIT>") and gfile.Exists(dev_path + "<STR_LIT>")):<EOL><INDENT>logging.info("<STR_LIT>" % dev_file)<EOL>with tarfile.open(dev_file, "<STR_LIT>") as dev_tar:<EOL><INDENT>fr_dev_file = dev_tar.getmember("<STR_LIT>" + dev_name + "<STR_LIT>")<EOL>en_dev_file = dev_tar.getmember("<STR_LIT>" + dev_name + "<STR_LIT>")<EOL>fr_dev_file.name = dev_name + "<STR_LIT>"  <EOL>en_dev_file.name = dev_name + "<STR_LIT>"<EOL>dev_tar.extract(fr_dev_file, path)<EOL>dev_tar.extract(en_dev_file, path)<EOL><DEDENT><DEDENT>return dev_path<EOL><DEDENT>logging.info("<STR_LIT>".format(path))<EOL>train_path = get_wmt_enfr_train_set(path)<EOL>dev_path = get_wmt_enfr_dev_set(path)<EOL>return train_path, dev_path<EOL>

Load WMT'15 English-to-French translation dataset.

    It will download the data from the WMT'15 Website (10^9-French-English corpus), and the 2013 news test from the same site as development set.
    Returns the directories of training data and test data.

    Parameters
    ----------
    path : str
        The path that the data is downloaded to, defaults is ``data/wmt_en_fr/``.

    References
    ----------
    - Code modified from /tensorflow/models/rnn/translation/data_utils.py

    Notes
    -----
    Usually, it will take a long time to download this dataset.


def load_voc_dataset(path='<STR_LIT:data>', dataset='<STR_LIT>', contain_classes_in_person=False):

path = os.path.join(path, '<STR_LIT>')<EOL>def _recursive_parse_xml_to_dict(xml):<EOL><INDENT>"""<STR_LIT>"""<EOL>if xml is not None:<EOL><INDENT>return {xml.tag: xml.text}<EOL><DEDENT>result = {}<EOL>for child in xml:<EOL><INDENT>child_result = _recursive_parse_xml_to_dict(child)<EOL>if child.tag != '<STR_LIT:object>':<EOL><INDENT>result[child.tag] = child_result[child.tag]<EOL><DEDENT>else:<EOL><INDENT>if child.tag not in result:<EOL><INDENT>result[child.tag] = []<EOL><DEDENT>result[child.tag].append(child_result[child.tag])<EOL><DEDENT><DEDENT>return {xml.tag: result}<EOL><DEDENT>import xml.etree.ElementTree as ET<EOL>if dataset == "<STR_LIT>":<EOL><INDENT>url = "<STR_LIT>"<EOL>tar_filename = "<STR_LIT>"<EOL>extracted_filename = "<STR_LIT>"  <EOL>logging.info("<STR_LIT>")<EOL><DEDENT>elif dataset == "<STR_LIT>":<EOL><INDENT>extracted_filename = "<STR_LIT>"  <EOL>logging.info("<STR_LIT>")<EOL>logging.info(<EOL>"<STR_LIT>"<EOL>)<EOL>import time<EOL>time.sleep(<NUM_LIT:3>)<EOL>if os.path.isdir(os.path.join(path, extracted_filename)) is False:<EOL><INDENT>logging.info("<STR_LIT>")<EOL>logging.info(<EOL>"<STR_LIT>"<EOL>)<EOL>logging.info("<STR_LIT>" % path)<EOL>exit()<EOL><DEDENT><DEDENT>elif dataset == "<STR_LIT>":<EOL><INDENT>url = "<STR_LIT>"<EOL>tar_filename = "<STR_LIT>"<EOL>extracted_filename = "<STR_LIT>"<EOL>logging.info("<STR_LIT>")<EOL><DEDENT>elif dataset == "<STR_LIT>":<EOL><INDENT>url = "<STR_LIT>"<EOL>tar_filename = "<STR_LIT>"<EOL>extracted_filename = "<STR_LIT>"<EOL>logging.info("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>if dataset != "<STR_LIT>":<EOL><INDENT>from sys import platform as _platform<EOL>if folder_exists(os.path.join(path, extracted_filename)) is False:<EOL><INDENT>logging.info("<STR_LIT>".format(extracted_filename, path))<EOL>maybe_download_and_extract(tar_filename, path, url, extract=True)<EOL>del_file(os.path.join(path, tar_filename))<EOL>if dataset == "<STR_LIT>":<EOL><INDENT>if _platform == "<STR_LIT:win32>":<EOL><INDENT>os.system("<STR_LIT>".format(path, path))<EOL><DEDENT>else:<EOL><INDENT>os.system("<STR_LIT>".format(path, path))<EOL><DEDENT><DEDENT>elif dataset == "<STR_LIT>":<EOL><INDENT>if _platform == "<STR_LIT:win32>":<EOL><INDENT>os.system("<STR_LIT>".format(path, path))<EOL><DEDENT>else:<EOL><INDENT>os.system("<STR_LIT>".format(path, path))<EOL><DEDENT><DEDENT>elif dataset == "<STR_LIT>":<EOL><INDENT>if _platform == "<STR_LIT:win32>":<EOL><INDENT>os.system("<STR_LIT>".format(path, path))<EOL><DEDENT>else:<EOL><INDENT>os.system("<STR_LIT>".format(path, path))<EOL><DEDENT><DEDENT>del_folder(os.path.join(path, '<STR_LIT>'))<EOL><DEDENT><DEDENT>classes = [<EOL>"<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>",<EOL>"<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT:train>", "<STR_LIT>"<EOL>]<EOL>if contain_classes_in_person:<EOL><INDENT>classes_in_person = ["<STR_LIT>", "<STR_LIT>", "<STR_LIT>"]<EOL><DEDENT>else:<EOL><INDENT>classes_in_person = []<EOL><DEDENT>classes += classes_in_person  <EOL>classes_dict = utils.list_string_to_dict(classes)<EOL>logging.info("<STR_LIT>".format(classes_dict))<EOL>folder_imgs = os.path.join(path, extracted_filename, "<STR_LIT>")<EOL>imgs_file_list = load_file_list(path=folder_imgs, regx='<STR_LIT>', printable=False)<EOL>logging.info("<STR_LIT>".format(len(imgs_file_list)))<EOL>imgs_file_list.sort(<EOL>key=lambda s: int(s.replace('<STR_LIT:.>', '<STR_LIT:U+0020>').replace('<STR_LIT:_>', '<STR_LIT>').split('<STR_LIT:U+0020>')[-<NUM_LIT:2>])<EOL>)  <EOL>imgs_file_list = [os.path.join(folder_imgs, s) for s in imgs_file_list]<EOL>if dataset != "<STR_LIT>":<EOL><INDENT>folder_semseg = os.path.join(path, extracted_filename, "<STR_LIT>")<EOL>imgs_semseg_file_list = load_file_list(path=folder_semseg, regx='<STR_LIT>', printable=False)<EOL>logging.info("<STR_LIT>".format(len(imgs_semseg_file_list)))<EOL>imgs_semseg_file_list.sort(<EOL>key=lambda s: int(s.replace('<STR_LIT:.>', '<STR_LIT:U+0020>').replace('<STR_LIT:_>', '<STR_LIT>').split('<STR_LIT:U+0020>')[-<NUM_LIT:2>])<EOL>)  <EOL>imgs_semseg_file_list = [os.path.join(folder_semseg, s) for s in imgs_semseg_file_list]<EOL>folder_insseg = os.path.join(path, extracted_filename, "<STR_LIT>")<EOL>imgs_insseg_file_list = load_file_list(path=folder_insseg, regx='<STR_LIT>', printable=False)<EOL>logging.info("<STR_LIT>".format(len(imgs_semseg_file_list)))<EOL>imgs_insseg_file_list.sort(<EOL>key=lambda s: int(s.replace('<STR_LIT:.>', '<STR_LIT:U+0020>').replace('<STR_LIT:_>', '<STR_LIT>').split('<STR_LIT:U+0020>')[-<NUM_LIT:2>])<EOL>)  <EOL>imgs_insseg_file_list = [os.path.join(folder_insseg, s) for s in imgs_insseg_file_list]<EOL><DEDENT>else:<EOL><INDENT>imgs_semseg_file_list = []<EOL>imgs_insseg_file_list = []<EOL><DEDENT>folder_ann = os.path.join(path, extracted_filename, "<STR_LIT>")<EOL>imgs_ann_file_list = load_file_list(path=folder_ann, regx='<STR_LIT>', printable=False)<EOL>logging.info(<EOL>"<STR_LIT>".format(len(imgs_ann_file_list))<EOL>)<EOL>imgs_ann_file_list.sort(<EOL>key=lambda s: int(s.replace('<STR_LIT:.>', '<STR_LIT:U+0020>').replace('<STR_LIT:_>', '<STR_LIT>').split('<STR_LIT:U+0020>')[-<NUM_LIT:2>])<EOL>)  <EOL>imgs_ann_file_list = [os.path.join(folder_ann, s) for s in imgs_ann_file_list]<EOL>if dataset == "<STR_LIT>":  <EOL><INDENT>imgs_file_list_new = []<EOL>for ann in imgs_ann_file_list:<EOL><INDENT>ann = os.path.split(ann)[-<NUM_LIT:1>].split('<STR_LIT:.>')[<NUM_LIT:0>]<EOL>for im in imgs_file_list:<EOL><INDENT>if ann in im:<EOL><INDENT>imgs_file_list_new.append(im)<EOL>break<EOL><DEDENT><DEDENT><DEDENT>imgs_file_list = imgs_file_list_new<EOL>logging.info("<STR_LIT>" % len(imgs_file_list_new))<EOL><DEDENT>def convert(size, box):<EOL><INDENT>dw = <NUM_LIT:1.> / size[<NUM_LIT:0>]<EOL>dh = <NUM_LIT:1.> / size[<NUM_LIT:1>]<EOL>x = (box[<NUM_LIT:0>] + box[<NUM_LIT:1>]) / <NUM_LIT><EOL>y = (box[<NUM_LIT:2>] + box[<NUM_LIT:3>]) / <NUM_LIT><EOL>w = box[<NUM_LIT:1>] - box[<NUM_LIT:0>]<EOL>h = box[<NUM_LIT:3>] - box[<NUM_LIT:2>]<EOL>x = x * dw<EOL>w = w * dw<EOL>y = y * dh<EOL>h = h * dh<EOL>return x, y, w, h<EOL><DEDENT>def convert_annotation(file_name):<EOL><INDENT>"""<STR_LIT>"""<EOL>in_file = open(file_name)<EOL>out_file = "<STR_LIT>"<EOL>tree = ET.parse(in_file)<EOL>root = tree.getroot()<EOL>size = root.find('<STR_LIT:size>')<EOL>w = int(size.find('<STR_LIT:width>').text)<EOL>h = int(size.find('<STR_LIT>').text)<EOL>n_objs = <NUM_LIT:0><EOL>for obj in root.iter('<STR_LIT:object>'):<EOL><INDENT>if dataset != "<STR_LIT>":<EOL><INDENT>difficult = obj.find('<STR_LIT>').text<EOL>cls = obj.find('<STR_LIT:name>').text<EOL>if cls not in classes or int(difficult) == <NUM_LIT:1>:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>else:<EOL><INDENT>cls = obj.find('<STR_LIT:name>').text<EOL>if cls not in classes:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>cls_id = classes.index(cls)<EOL>xmlbox = obj.find('<STR_LIT>')<EOL>b = (<EOL>float(xmlbox.find('<STR_LIT>').text), float(xmlbox.find('<STR_LIT>').text), float(xmlbox.find('<STR_LIT>').text),<EOL>float(xmlbox.find('<STR_LIT>').text)<EOL>)<EOL>bb = convert((w, h), b)<EOL>out_file += str(cls_id) + "<STR_LIT:U+0020>" + "<STR_LIT:U+0020>".join([str(a) for a in bb]) + '<STR_LIT:\n>'<EOL>n_objs += <NUM_LIT:1><EOL>if cls in "<STR_LIT>":<EOL><INDENT>for part in obj.iter('<STR_LIT>'):<EOL><INDENT>cls = part.find('<STR_LIT:name>').text<EOL>if cls not in classes_in_person:<EOL><INDENT>continue<EOL><DEDENT>cls_id = classes.index(cls)<EOL>xmlbox = part.find('<STR_LIT>')<EOL>b = (<EOL>float(xmlbox.find('<STR_LIT>').text), float(xmlbox.find('<STR_LIT>').text),<EOL>float(xmlbox.find('<STR_LIT>').text), float(xmlbox.find('<STR_LIT>').text)<EOL>)<EOL>bb = convert((w, h), b)<EOL>out_file += str(cls_id) + "<STR_LIT:U+0020>" + "<STR_LIT:U+0020>".join([str(a) for a in bb]) + '<STR_LIT:\n>'<EOL>n_objs += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>in_file.close()<EOL>return n_objs, out_file<EOL><DEDENT>logging.info("<STR_LIT>")<EOL>n_objs_list = []<EOL>objs_info_list = []  <EOL>objs_info_dicts = {}<EOL>for idx, ann_file in enumerate(imgs_ann_file_list):<EOL><INDENT>n_objs, objs_info = convert_annotation(ann_file)<EOL>n_objs_list.append(n_objs)<EOL>objs_info_list.append(objs_info)<EOL>with tf.gfile.GFile(ann_file, '<STR_LIT:r>') as fid:<EOL><INDENT>xml_str = fid.read()<EOL><DEDENT>xml = etree.fromstring(xml_str)<EOL>data = _recursive_parse_xml_to_dict(xml)['<STR_LIT>']<EOL>objs_info_dicts.update({imgs_file_list[idx]: data})<EOL><DEDENT>return imgs_file_list, imgs_semseg_file_list, imgs_insseg_file_list, imgs_ann_file_list, classes, classes_in_person, classes_dict, n_objs_list, objs_info_list, objs_info_dicts<EOL>

Pascal VOC 2007/2012 Dataset.

    It has 20 objects:
    aeroplane, bicycle, bird, boat, bottle, bus, car, cat, chair, cow, diningtable, dog, horse, motorbike, person, pottedplant, sheep, sofa, train, tvmonitor
    and additional 3 classes : head, hand, foot for person.

    Parameters
    -----------
    path : str
        The path that the data is downloaded to, defaults is ``data/VOC``.
    dataset : str
        The VOC dataset version, `2012`, `2007`, `2007test` or `2012test`. We usually train model on `2007+2012` and test it on `2007test`.
    contain_classes_in_person : boolean
        Whether include head, hand and foot annotation, default is False.

    Returns
    ---------
    imgs_file_list : list of str
        Full paths of all images.
    imgs_semseg_file_list : list of str
        Full paths of all maps for semantic segmentation. Note that not all images have this map!
    imgs_insseg_file_list : list of str
        Full paths of all maps for instance segmentation. Note that not all images have this map!
    imgs_ann_file_list : list of str
        Full paths of all annotations for bounding box and object class, all images have this annotations.
    classes : list of str
        Classes in order.
    classes_in_person : list of str
        Classes in person.
    classes_dict : dictionary
        Class label to integer.
    n_objs_list : list of int
        Number of objects in all images in ``imgs_file_list`` in order.
    objs_info_list : list of str
        Darknet format for the annotation of all images in ``imgs_file_list`` in order. ``[class_id x_centre y_centre width height]`` in ratio format.
    objs_info_dicts : dictionary
        The annotation of all images in ``imgs_file_list``, ``{imgs_file_list : dictionary for annotation}``,
        format from `TensorFlow/Models/object-detection <https://github.com/tensorflow/models/blob/master/object_detection/create_pascal_tf_record.py>`__.

    Examples
    ----------
    >>> imgs_file_list, imgs_semseg_file_list, imgs_insseg_file_list, imgs_ann_file_list,
    >>>     classes, classes_in_person, classes_dict,
    >>>     n_objs_list, objs_info_list, objs_info_dicts = tl.files.load_voc_dataset(dataset="2012", contain_classes_in_person=False)
    >>> idx = 26
    >>> print(classes)
    ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
    >>> print(classes_dict)
    {'sheep': 16, 'horse': 12, 'bicycle': 1, 'bottle': 4, 'cow': 9, 'sofa': 17, 'car': 6, 'dog': 11, 'cat': 7, 'person': 14, 'train': 18, 'diningtable': 10, 'aeroplane': 0, 'bus': 5, 'pottedplant': 15, 'tvmonitor': 19, 'chair': 8, 'bird': 2, 'boat': 3, 'motorbike': 13}
    >>> print(imgs_file_list[idx])
    data/VOC/VOC2012/JPEGImages/2007_000423.jpg
    >>> print(n_objs_list[idx])
    2
    >>> print(imgs_ann_file_list[idx])
    data/VOC/VOC2012/Annotations/2007_000423.xml
    >>> print(objs_info_list[idx])
    14 0.173 0.461333333333 0.142 0.496
    14 0.828 0.542666666667 0.188 0.594666666667
    >>> ann = tl.prepro.parse_darknet_ann_str_to_list(objs_info_list[idx])
    >>> print(ann)
    [[14, 0.173, 0.461333333333, 0.142, 0.496], [14, 0.828, 0.542666666667, 0.188, 0.594666666667]]
    >>> c, b = tl.prepro.parse_darknet_ann_list_to_cls_box(ann)
    >>> print(c, b)
    [14, 14] [[0.173, 0.461333333333, 0.142, 0.496], [0.828, 0.542666666667, 0.188, 0.594666666667]]

    References
    -------------
    - `Pascal VOC2012 Website <https://pjreddie.com/projects/pascal-voc-dataset-mirror/>`__.
    - `Pascal VOC2007 Website <https://pjreddie.com/projects/pascal-voc-dataset-mirror/>`__.


start_time = time.time()<EOL>path = os.path.join(path, '<STR_LIT>')<EOL>logging.info("<STR_LIT>".format(path, include_extra))<EOL>url = "<STR_LIT>"<EOL>np_file = os.path.join(path, "<STR_LIT>")<EOL>if file_exists(np_file) is False:<EOL><INDENT>filename = "<STR_LIT>"<EOL>filepath = maybe_download_and_extract(filename, path, url)<EOL>mat = sio.loadmat(filepath)<EOL>X_train = mat['<STR_LIT:X>'] / <NUM_LIT>  <EOL>X_train = np.transpose(X_train, (<NUM_LIT:3>, <NUM_LIT:0>, <NUM_LIT:1>, <NUM_LIT:2>))<EOL>y_train = np.squeeze(mat['<STR_LIT:y>'], axis=<NUM_LIT:1>)<EOL>y_train[y_train == <NUM_LIT:10>] = <NUM_LIT:0>  <EOL>np.savez(np_file, X=X_train, y=y_train)<EOL>del_file(filepath)<EOL><DEDENT>else:<EOL><INDENT>v = np.load(np_file)<EOL>X_train = v['<STR_LIT:X>']<EOL>y_train = v['<STR_LIT:y>']<EOL><DEDENT>logging.info("<STR_LIT>".format(len(y_train)))<EOL>np_file = os.path.join(path, "<STR_LIT>")<EOL>if file_exists(np_file) is False:<EOL><INDENT>filename = "<STR_LIT>"<EOL>filepath = maybe_download_and_extract(filename, path, url)<EOL>mat = sio.loadmat(filepath)<EOL>X_test = mat['<STR_LIT:X>'] / <NUM_LIT><EOL>X_test = np.transpose(X_test, (<NUM_LIT:3>, <NUM_LIT:0>, <NUM_LIT:1>, <NUM_LIT:2>))<EOL>y_test = np.squeeze(mat['<STR_LIT:y>'], axis=<NUM_LIT:1>)<EOL>y_test[y_test == <NUM_LIT:10>] = <NUM_LIT:0><EOL>np.savez(np_file, X=X_test, y=y_test)<EOL>del_file(filepath)<EOL><DEDENT>else:<EOL><INDENT>v = np.load(np_file)<EOL>X_test = v['<STR_LIT:X>']<EOL>y_test = v['<STR_LIT:y>']<EOL><DEDENT>logging.info("<STR_LIT>".format(len(y_test)))<EOL>if include_extra:<EOL><INDENT>logging.info("<STR_LIT>")<EOL>np_file = os.path.join(path, "<STR_LIT>")<EOL>if file_exists(np_file) is False:<EOL><INDENT>logging.info("<STR_LIT>")<EOL>filename = "<STR_LIT>"<EOL>filepath = maybe_download_and_extract(filename, path, url)<EOL>mat = sio.loadmat(filepath)<EOL>X_extra = mat['<STR_LIT:X>'] / <NUM_LIT><EOL>X_extra = np.transpose(X_extra, (<NUM_LIT:3>, <NUM_LIT:0>, <NUM_LIT:1>, <NUM_LIT:2>))<EOL>y_extra = np.squeeze(mat['<STR_LIT:y>'], axis=<NUM_LIT:1>)<EOL>y_extra[y_extra == <NUM_LIT:10>] = <NUM_LIT:0><EOL>np.savez(np_file, X=X_extra, y=y_extra)<EOL>del_file(filepath)<EOL><DEDENT>else:<EOL><INDENT>v = np.load(np_file)<EOL>X_extra = v['<STR_LIT:X>']<EOL>y_extra = v['<STR_LIT:y>']<EOL><DEDENT>logging.info("<STR_LIT>".format(len(y_extra), len(y_train)))<EOL>t = time.time()<EOL>X_train = np.concatenate((X_train, X_extra), <NUM_LIT:0>)<EOL>y_train = np.concatenate((y_train, y_extra), <NUM_LIT:0>)<EOL>logging.info("<STR_LIT>".format(len(y_extra), len(y_train), time.time() - t))<EOL><DEDENT>else:<EOL><INDENT>logging.info("<STR_LIT>")<EOL><DEDENT>logging.info("<STR_LIT>" % (str(X_train.shape[<NUM_LIT:1>:<NUM_LIT:4>]), len(y_train), len(y_test)))<EOL>logging.info("<STR_LIT>".format(int(time.time() - start_time)))<EOL>return X_train, y_train, X_test, y_test<EOL>

Load Cropped SVHN.

    The Cropped Street View House Numbers (SVHN) Dataset contains 32x32x3 RGB images.
    Digit '1' has label 1, '9' has label 9 and '0' has label 0 (the original dataset uses 10 to represent '0'), see `ufldl website <http://ufldl.stanford.edu/housenumbers/>`__.

    Parameters
    ----------
    path : str
        The path that the data is downloaded to.
    include_extra : boolean
        If True (default), add extra images to the training set.

    Returns
    -------
    X_train, y_train, X_test, y_test: tuple
        Return splitted training/test set respectively.

    Examples
    ---------
    >>> X_train, y_train, X_test, y_test = tl.files.load_cropped_svhn(include_extra=False)
    >>> tl.vis.save_images(X_train[0:100], [10, 10], 'svhn.png')


def _load_mnist_dataset(shape, path, name='<STR_LIT>', url='<STR_LIT>'):

path = os.path.join(path, name)<EOL>def load_mnist_images(path, filename):<EOL><INDENT>filepath = maybe_download_and_extract(filename, path, url)<EOL>logging.info(filepath)<EOL>with gzip.open(filepath, '<STR_LIT:rb>') as f:<EOL><INDENT>data = np.frombuffer(f.read(), np.uint8, offset=<NUM_LIT:16>)<EOL><DEDENT>data = data.reshape(shape)<EOL>return data / np.float32(<NUM_LIT>)<EOL><DEDENT>def load_mnist_labels(path, filename):<EOL><INDENT>filepath = maybe_download_and_extract(filename, path, url)<EOL>with gzip.open(filepath, '<STR_LIT:rb>') as f:<EOL><INDENT>data = np.frombuffer(f.read(), np.uint8, offset=<NUM_LIT:8>)<EOL><DEDENT>return data<EOL><DEDENT>logging.info("<STR_LIT>".format(name.upper(), path))<EOL>X_train = load_mnist_images(path, '<STR_LIT>')<EOL>y_train = load_mnist_labels(path, '<STR_LIT>')<EOL>X_test = load_mnist_images(path, '<STR_LIT>')<EOL>y_test = load_mnist_labels(path, '<STR_LIT>')<EOL>X_train, X_val = X_train[:-<NUM_LIT>], X_train[-<NUM_LIT>:]<EOL>y_train, y_val = y_train[:-<NUM_LIT>], y_train[-<NUM_LIT>:]<EOL>X_train = np.asarray(X_train, dtype=np.float32)<EOL>y_train = np.asarray(y_train, dtype=np.int32)<EOL>X_val = np.asarray(X_val, dtype=np.float32)<EOL>y_val = np.asarray(y_val, dtype=np.int32)<EOL>X_test = np.asarray(X_test, dtype=np.float32)<EOL>y_test = np.asarray(y_test, dtype=np.int32)<EOL>return X_train, y_train, X_val, y_val, X_test, y_test<EOL>

A generic function to load mnist-like dataset.

    Parameters:
    ----------
    shape : tuple
        The shape of digit images.
    path : str
        The path that the data is downloaded to.
    name : str
        The dataset name you want to use(the default is 'mnist').
    url : str
        The url of dataset(the default is 'http://yann.lecun.com/exdb/mnist/').


os.remove(filepath)<EOL>


shutil.rmtree(folderpath)<EOL>


path = os.path.join(path, '<STR_LIT>')<EOL>logging.info("<STR_LIT>".format(path))<EOL>filename = '<STR_LIT>'<EOL>url = '<STR_LIT>'<EOL>maybe_download_and_extract(filename, path, url, expected_bytes=<NUM_LIT>)<EOL>with zipfile.ZipFile(os.path.join(path, filename)) as f:<EOL><INDENT>word_list = f.read(f.namelist()[<NUM_LIT:0>]).split()<EOL>for idx, _ in enumerate(word_list):<EOL><INDENT>word_list[idx] = word_list[idx].decode()<EOL><DEDENT><DEDENT>return word_list<EOL>

Load Matt Mahoney's dataset.

    Download a text file from Matt Mahoney's website
    if not present, and make sure it's the right size.
    Extract the first file enclosed in a zip file as a list of words.
    This dataset can be used for Word Embedding.

    Parameters
    ----------
    path : str
        The path that the data is downloaded to, defaults is ``data/mm_test8/``.

    Returns
    --------
    list of str
        The raw text data e.g. [.... 'their', 'families', 'who', 'were', 'expelled', 'from', 'jerusalem', ...]

    Examples
    --------
    >>> words = tl.files.load_matt_mahoney_text8_dataset()
    >>> print('Data size', len(words))


def load_fashion_mnist_dataset(shape=(-<NUM_LIT:1>, <NUM_LIT>), path='<STR_LIT:data>'):

return _load_mnist_dataset(<EOL>shape, path, name='<STR_LIT>', url='<STR_LIT>'<EOL>)<EOL>

Load the fashion mnist.

    Automatically download fashion-MNIST dataset and return the training, validation and test set with 50000, 10000 and 10000 fashion images respectively, `examples <http://marubon-ds.blogspot.co.uk/2017/09/fashion-mnist-exploring.html>`__.

    Parameters
    ----------
    shape : tuple
        The shape of digit images (the default is (-1, 784), alternatively (-1, 28, 28, 1)).
    path : str
        The path that the data is downloaded to.

    Returns
    -------
    X_train, y_train, X_val, y_val, X_test, y_test: tuple
        Return splitted training/validation/test set respectively.

    Examples
    --------
    >>> X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_fashion_mnist_dataset(shape=(-1,784), path='datasets')
    >>> X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_fashion_mnist_dataset(shape=(-1, 28, 28, 1))


if not os.path.exists(path):<EOL><INDENT>if verbose:<EOL><INDENT>logging.info("<STR_LIT>" % path)<EOL><DEDENT>os.makedirs(path)<EOL>return False<EOL><DEDENT>else:<EOL><INDENT>if verbose:<EOL><INDENT>logging.info("<STR_LIT>" % path)<EOL><DEDENT>return True<EOL><DEDENT>

Check a folder by given name, if not exist, create the folder and return False,
    if directory exists, return True.

    Parameters
    ----------
    path : str
        A folder path.
    verbose : boolean
        If True (default), prints results.

    Returns
    --------
    boolean
        True if folder already exist, otherwise, returns False and create the folder.

    Examples
    --------
    >>> tl.files.exists_or_mkdir("checkpoints/train")


path = os.path.join(path, '<STR_LIT>')<EOL>logging.info("<STR_LIT>".format(path))<EOL>url = "<STR_LIT>"<EOL>tar_filename = "<STR_LIT>"<EOL>extracted_filename = "<STR_LIT>"<EOL>if folder_exists(os.path.join(path, extracted_filename)) is False:<EOL><INDENT>logging.info("<STR_LIT>".format(extracted_filename, path))<EOL>maybe_download_and_extract(tar_filename, path, url, extract=True)<EOL>del_file(os.path.join(path, tar_filename))<EOL><DEDENT>url = "<STR_LIT>"<EOL>tar_filename = "<STR_LIT>"<EOL>extracted_filename2 = "<STR_LIT>"<EOL>if folder_exists(os.path.join(path, extracted_filename2)) is False:<EOL><INDENT>logging.info("<STR_LIT>".format(extracted_filename, path))<EOL>maybe_download_and_extract(tar_filename, path, url, extract=True)<EOL>del_file(os.path.join(path, tar_filename))<EOL><DEDENT>logging.info("<STR_LIT>")<EOL>ann_train_list = []<EOL>ann_test_list = []<EOL>img_train_list = []<EOL>img_test_list = []<EOL>def save_joints():<EOL><INDENT>mat = sio.loadmat(os.path.join(path, extracted_filename, "<STR_LIT>"))<EOL>for _, (anno, train_flag) in enumerate(  <EOL>zip(mat['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>, <NUM_LIT:0>][<NUM_LIT:0>], mat['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>, <NUM_LIT:0>][<NUM_LIT:0>])):<EOL><INDENT>img_fn = anno['<STR_LIT:image>']['<STR_LIT:name>'][<NUM_LIT:0>, <NUM_LIT:0>][<NUM_LIT:0>]<EOL>train_flag = int(train_flag)<EOL>if train_flag:<EOL><INDENT>img_train_list.append(img_fn)<EOL>ann_train_list.append([])<EOL><DEDENT>else:<EOL><INDENT>img_test_list.append(img_fn)<EOL>ann_test_list.append([])<EOL><DEDENT>head_rect = []<EOL>if '<STR_LIT>' in str(anno['<STR_LIT>'].dtype):<EOL><INDENT>head_rect = zip(<EOL>[x1[<NUM_LIT:0>, <NUM_LIT:0>] for x1 in anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]], [y1[<NUM_LIT:0>, <NUM_LIT:0>] for y1 in anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]],<EOL>[x2[<NUM_LIT:0>, <NUM_LIT:0>] for x2 in anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]], [y2[<NUM_LIT:0>, <NUM_LIT:0>] for y2 in anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]]<EOL>)<EOL><DEDENT>else:<EOL><INDENT>head_rect = []  <EOL><DEDENT>if '<STR_LIT>' in str(anno['<STR_LIT>'].dtype):<EOL><INDENT>annopoints = anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]<EOL>head_x1s = anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]<EOL>head_y1s = anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]<EOL>head_x2s = anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]<EOL>head_y2s = anno['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]<EOL>for annopoint, head_x1, head_y1, head_x2, head_y2 in zip(annopoints, head_x1s, head_y1s, head_x2s,<EOL>head_y2s):<EOL><INDENT>if annopoint.size:<EOL><INDENT>head_rect = [<EOL>float(head_x1[<NUM_LIT:0>, <NUM_LIT:0>]),<EOL>float(head_y1[<NUM_LIT:0>, <NUM_LIT:0>]),<EOL>float(head_x2[<NUM_LIT:0>, <NUM_LIT:0>]),<EOL>float(head_y2[<NUM_LIT:0>, <NUM_LIT:0>])<EOL>]<EOL>annopoint = annopoint['<STR_LIT>'][<NUM_LIT:0>, <NUM_LIT:0>]<EOL>j_id = [str(j_i[<NUM_LIT:0>, <NUM_LIT:0>]) for j_i in annopoint['<STR_LIT:id>'][<NUM_LIT:0>]]<EOL>x = [x[<NUM_LIT:0>, <NUM_LIT:0>] for x in annopoint['<STR_LIT:x>'][<NUM_LIT:0>]]<EOL>y = [y[<NUM_LIT:0>, <NUM_LIT:0>] for y in annopoint['<STR_LIT:y>'][<NUM_LIT:0>]]<EOL>joint_pos = {}<EOL>for _j_id, (_x, _y) in zip(j_id, zip(x, y)):<EOL><INDENT>joint_pos[int(_j_id)] = [float(_x), float(_y)]<EOL><DEDENT>if '<STR_LIT>' in str(annopoint.dtype):<EOL><INDENT>vis = [v[<NUM_LIT:0>] if v.size > <NUM_LIT:0> else [<NUM_LIT:0>] for v in annopoint['<STR_LIT>'][<NUM_LIT:0>]]<EOL>vis = dict([(k, int(v[<NUM_LIT:0>])) if len(v) > <NUM_LIT:0> else v for k, v in zip(j_id, vis)])<EOL><DEDENT>else:<EOL><INDENT>vis = None<EOL><DEDENT>if ((is_16_pos_only ==True) and (len(joint_pos) == <NUM_LIT:16>)) or (is_16_pos_only == False):<EOL><INDENT>data = {<EOL>'<STR_LIT:filename>': img_fn,<EOL>'<STR_LIT:train>': train_flag,<EOL>'<STR_LIT>': head_rect,<EOL>'<STR_LIT>': vis,<EOL>'<STR_LIT>': joint_pos<EOL>}<EOL>if train_flag:<EOL><INDENT>ann_train_list[-<NUM_LIT:1>].append(data)<EOL><DEDENT>else:<EOL><INDENT>ann_test_list[-<NUM_LIT:1>].append(data)<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>save_joints()<EOL>logging.info("<STR_LIT>")<EOL>img_dir = os.path.join(path, extracted_filename2)<EOL>_img_list = load_file_list(path=os.path.join(path, extracted_filename2), regx='<STR_LIT>', printable=False)<EOL>for i, im in enumerate(img_train_list):<EOL><INDENT>if im not in _img_list:<EOL><INDENT>print('<STR_LIT>'.format(im, img_dir))<EOL>del img_train_list[i]<EOL>del ann_train_list[i]<EOL><DEDENT><DEDENT>for i, im in enumerate(img_test_list):<EOL><INDENT>if im not in _img_list:<EOL><INDENT>print('<STR_LIT>'.format(im, img_dir))<EOL>del img_train_list[i]<EOL>del ann_train_list[i]<EOL><DEDENT><DEDENT>n_train_images = len(img_train_list)<EOL>n_test_images = len(img_test_list)<EOL>n_images = n_train_images + n_test_images<EOL>logging.info("<STR_LIT>".format(n_images, n_train_images, n_test_images))<EOL>n_train_ann = len(ann_train_list)<EOL>n_test_ann = len(ann_test_list)<EOL>n_ann = n_train_ann + n_test_ann<EOL>logging.info("<STR_LIT>".format(n_ann, n_train_ann, n_test_ann))<EOL>n_train_people = len(sum(ann_train_list, []))<EOL>n_test_people = len(sum(ann_test_list, []))<EOL>n_people = n_train_people + n_test_people<EOL>logging.info("<STR_LIT>".format(n_people, n_train_people, n_test_people))<EOL>for i, value in enumerate(img_train_list):<EOL><INDENT>img_train_list[i] = os.path.join(img_dir, value)<EOL><DEDENT>for i, value in enumerate(img_test_list):<EOL><INDENT>img_test_list[i] = os.path.join(img_dir, value)<EOL><DEDENT>return img_train_list, ann_train_list, img_test_list, ann_test_list<EOL>

Load MPII Human Pose Dataset.

    Parameters
    -----------
    path : str
        The path that the data is downloaded to.
    is_16_pos_only : boolean
        If True, only return the peoples contain 16 pose keypoints. (Usually be used for single person pose estimation)

    Returns
    ----------
    img_train_list : list of str
        The image directories of training data.
    ann_train_list : list of dict
        The annotations of training data.
    img_test_list : list of str
        The image directories of testing data.
    ann_test_list : list of dict
        The annotations of testing data.

    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 Human Pose Dataset. CVPR 14 <http://human-pose.mpi-inf.mpg.de>`__
    - `MPII Human Pose Models. CVPR 16 <http://pose.mpi-inf.mpg.de>`__
    - `MPII Human Shape, Poselet Conditioned Pictorial Structures and etc <http://pose.mpi-inf.mpg.de/#related>`__
    - `MPII Keyponts and ID <http://human-pose.mpi-inf.mpg.de/#download>`__


def load_mnist_dataset(shape=(-<NUM_LIT:1>, <NUM_LIT>), path='<STR_LIT:data>'):

return _load_mnist_dataset(shape, path, name='<STR_LIT>', url='<STR_LIT>')<EOL>

Load the original mnist.

    Automatically download MNIST dataset and return the training, validation and test set with 50000, 10000 and 10000 digit images respectively.

    Parameters
    ----------
    shape : tuple
        The shape of digit images (the default is (-1, 784), alternatively (-1, 28, 28, 1)).
    path : str
        The path that the data is downloaded to.

    Returns
    -------
    X_train, y_train, X_val, y_val, X_test, y_test: tuple
        Return splitted training/validation/test set respectively.

    Examples
    --------
    >>> X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-1,784), path='datasets')
    >>> X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-1, 28, 28, 1))


path = os.path.join(path, '<STR_LIT>')<EOL>logging.info("<STR_LIT>".format(path))<EOL>filename = '<STR_LIT>'<EOL>url = '<STR_LIT>'<EOL>maybe_download_and_extract(filename, path, url, extract=True)<EOL>data_path = os.path.join(path, '<STR_LIT>', '<STR_LIT:data>')<EOL>train_path = os.path.join(data_path, "<STR_LIT>")<EOL>valid_path = os.path.join(data_path, "<STR_LIT>")<EOL>test_path = os.path.join(data_path, "<STR_LIT>")<EOL>word_to_id = nlp.build_vocab(nlp.read_words(train_path))<EOL>train_data = nlp.words_to_word_ids(nlp.read_words(train_path), word_to_id)<EOL>valid_data = nlp.words_to_word_ids(nlp.read_words(valid_path), word_to_id)<EOL>test_data = nlp.words_to_word_ids(nlp.read_words(test_path), word_to_id)<EOL>vocab_size = len(word_to_id)<EOL>return train_data, valid_data, test_data, vocab_size<EOL>

Load Penn TreeBank (PTB) dataset.

    It is used in many LANGUAGE MODELING papers,
    including "Empirical Evaluation and Combination of Advanced Language
    Modeling Techniques", "Recurrent Neural Network Regularization".
    It consists of 929k training words, 73k validation words, and 82k test
    words. It has 10k words in its vocabulary.

    Parameters
    ----------
    path : str
        The path that the data is downloaded to, defaults is ``data/ptb/``.

    Returns
    --------
    train_data, valid_data, test_data : list of int
        The training, validating and testing data in integer format.
    vocab_size : int
        The vocabulary size.

    Examples
    --------
    >>> train_data, valid_data, test_data, vocab_size = tl.files.load_ptb_dataset()

    References
    ---------------
    - ``tensorflow.models.rnn.ptb import reader``
    - `Manual download <http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz>`__

    Notes
    ------
    - If you want to get the raw data, see the source code.


<EOL>def atoi(text):<EOL><INDENT>return int(text) if text.isdigit() else text<EOL><DEDENT>return [atoi(c) for c in re.split('<STR_LIT>', text)]<EOL>

Sort list of string with number in human order.

    Examples
    ----------
    >>> l = ['im1.jpg', 'im31.jpg', 'im11.jpg', 'im21.jpg', 'im03.jpg', 'im05.jpg']
    >>> l.sort(key=tl.files.natural_keys)
    ['im1.jpg', 'im03.jpg', 'im05', 'im11.jpg', 'im21.jpg', 'im31.jpg']
    >>> l.sort() # that is what we dont want
    ['im03.jpg', 'im05', 'im1.jpg', 'im11.jpg', 'im21.jpg', 'im31.jpg']

    References
    ----------
    - `link <http://nedbatchelder.com/blog/200712/human_sorting.html>`__


def load_and_assign_npz(sess=None, name=None, network=None):

if network is None:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if sess is None:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not os.path.exists(name):<EOL><INDENT>logging.error("<STR_LIT>".format(name))<EOL>return False<EOL><DEDENT>else:<EOL><INDENT>params = load_npz(name=name)<EOL>assign_params(sess, params, network)<EOL>logging.info("<STR_LIT>".format(name))<EOL>return network<EOL><DEDENT>

Load model from npz and assign to a network.

    Parameters
    -------------
    sess : Session
        TensorFlow Session.
    name : str
        The name of the `.npz` file.
    network : :class:`Layer`
        The network to be assigned.

    Returns
    --------
    False or network
        Returns False, if the model is not exist.

    Examples
    --------
    - See ``tl.files.save_npz``


d = np.load(os.path.join(path, name))<EOL>return d['<STR_LIT>']<EOL>

Load the parameters of a Model saved by tl.files.save_npz().

    Parameters
    ----------
    path : str
        Folder path to `.npz` file.
    name : str
        The name of the `.npz` file.

    Returns
    --------
    list of array
        A list of parameters in order.

    Examples
    --------
    - See ``tl.files.save_npz``

    References
    ----------
    - `Saving dictionary using numpy <http://stackoverflow.com/questions/22315595/saving-dictionary-of-header-information-using-numpy-savez>`__


def load_cifar10_dataset(shape=(-<NUM_LIT:1>, <NUM_LIT:32>, <NUM_LIT:32>, <NUM_LIT:3>), path='<STR_LIT:data>', plotable=False):

path = os.path.join(path, '<STR_LIT>')<EOL>logging.info("<STR_LIT>".format(path))<EOL>def unpickle(file):<EOL><INDENT>fp = open(file, '<STR_LIT:rb>')<EOL>if sys.version_info.major == <NUM_LIT:2>:<EOL><INDENT>data = pickle.load(fp)<EOL><DEDENT>elif sys.version_info.major == <NUM_LIT:3>:<EOL><INDENT>data = pickle.load(fp, encoding='<STR_LIT>')<EOL><DEDENT>fp.close()<EOL>return data<EOL><DEDENT>filename = '<STR_LIT>'<EOL>url = '<STR_LIT>'<EOL>maybe_download_and_extract(filename, path, url, extract=True)<EOL>X_train = None<EOL>y_train = []<EOL>for i in range(<NUM_LIT:1>, <NUM_LIT:6>):<EOL><INDENT>data_dic = unpickle(os.path.join(path, '<STR_LIT>', "<STR_LIT>".format(i)))<EOL>if i == <NUM_LIT:1>:<EOL><INDENT>X_train = data_dic['<STR_LIT:data>']<EOL><DEDENT>else:<EOL><INDENT>X_train = np.vstack((X_train, data_dic['<STR_LIT:data>']))<EOL><DEDENT>y_train += data_dic['<STR_LIT>']<EOL><DEDENT>test_data_dic = unpickle(os.path.join(path, '<STR_LIT>', "<STR_LIT>"))<EOL>X_test = test_data_dic['<STR_LIT:data>']<EOL>y_test = np.array(test_data_dic['<STR_LIT>'])<EOL>if shape == (-<NUM_LIT:1>, <NUM_LIT:3>, <NUM_LIT:32>, <NUM_LIT:32>):<EOL><INDENT>X_test = X_test.reshape(shape)<EOL>X_train = X_train.reshape(shape)<EOL><DEDENT>elif shape == (-<NUM_LIT:1>, <NUM_LIT:32>, <NUM_LIT:32>, <NUM_LIT:3>):<EOL><INDENT>X_test = X_test.reshape(shape, order='<STR_LIT:F>')<EOL>X_train = X_train.reshape(shape, order='<STR_LIT:F>')<EOL>X_test = np.transpose(X_test, (<NUM_LIT:0>, <NUM_LIT:2>, <NUM_LIT:1>, <NUM_LIT:3>))<EOL>X_train = np.transpose(X_train, (<NUM_LIT:0>, <NUM_LIT:2>, <NUM_LIT:1>, <NUM_LIT:3>))<EOL><DEDENT>else:<EOL><INDENT>X_test = X_test.reshape(shape)<EOL>X_train = X_train.reshape(shape)<EOL><DEDENT>y_train = np.array(y_train)<EOL>if plotable:<EOL><INDENT>logging.info('<STR_LIT>')<EOL>fig = plt.figure(<NUM_LIT:1>)<EOL>logging.info('<STR_LIT>' % X_train[<NUM_LIT:0>].shape)<EOL>plt.ion()  <EOL>count = <NUM_LIT:1><EOL>for _ in range(<NUM_LIT:10>):  <EOL><INDENT>for _ in range(<NUM_LIT:10>):  <EOL><INDENT>_ = fig.add_subplot(<NUM_LIT:10>, <NUM_LIT:10>, count)<EOL>if shape == (-<NUM_LIT:1>, <NUM_LIT:3>, <NUM_LIT:32>, <NUM_LIT:32>):<EOL><INDENT>plt.imshow(np.transpose(X_train[count - <NUM_LIT:1>], (<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:0>)), interpolation='<STR_LIT>')<EOL><DEDENT>elif shape == (-<NUM_LIT:1>, <NUM_LIT:32>, <NUM_LIT:32>, <NUM_LIT:3>):<EOL><INDENT>plt.imshow(X_train[count - <NUM_LIT:1>], 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>plt.draw()  <EOL>plt.pause(<NUM_LIT:3>)  <EOL>logging.info("<STR_LIT>" % X_train.shape)<EOL>logging.info("<STR_LIT>" % y_train.shape)<EOL>logging.info("<STR_LIT>" % X_test.shape)<EOL>logging.info("<STR_LIT>" % y_test.shape)<EOL><DEDENT>X_train = np.asarray(X_train, dtype=np.float32)<EOL>X_test = np.asarray(X_test, dtype=np.float32)<EOL>y_train = np.asarray(y_train, dtype=np.int32)<EOL>y_test = np.asarray(y_test, dtype=np.int32)<EOL>return X_train, y_train, X_test, y_test<EOL>

Load CIFAR-10 dataset.

    It consists of 60000 32x32 colour images in 10 classes, with
    6000 images per class. There are 50000 training images and 10000 test images.

    The dataset is divided into five training batches and one test batch, each with
    10000 images. The test batch contains exactly 1000 randomly-selected images from
    each class. The training batches contain the remaining images in random order,
    but some training batches may contain more images from one class than another.
    Between them, the training batches contain exactly 5000 images from each class.

    Parameters
    ----------
    shape : tupe
        The shape of digit images e.g. (-1, 3, 32, 32) and (-1, 32, 32, 3).
    path : str
        The path that the data is downloaded to, defaults is ``data/cifar10/``.
    plotable : boolean
        Whether to plot some image examples, False as default.

    Examples
    --------
    >>> X_train, y_train, X_test, y_test = tl.files.load_cifar10_dataset(shape=(-1, 32, 32, 3))

    References
    ----------
    - `CIFAR website <https://www.cs.toronto.edu/~kriz/cifar.html>`__
    - `Data download link <https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz>`__
    - `<https://teratail.com/questions/28932>`__


if sess is None:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not os.path.exists(name):<EOL><INDENT>logging.error("<STR_LIT>".format(name))<EOL>return False<EOL><DEDENT>params = np.load(name)<EOL>if len(params.keys()) != len(set(params.keys())):<EOL><INDENT>raise Exception("<STR_LIT>" % name)<EOL><DEDENT>ops = list()<EOL>for key in params.keys():<EOL><INDENT>try:<EOL><INDENT>varlist = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=key)<EOL>if len(varlist) > <NUM_LIT:1>:<EOL><INDENT>raise Exception("<STR_LIT>" % key)<EOL><DEDENT>elif len(varlist) == <NUM_LIT:0>:<EOL><INDENT>raise KeyError<EOL><DEDENT>else:<EOL><INDENT>ops.append(varlist[<NUM_LIT:0>].assign(params[key]))<EOL>logging.info("<STR_LIT>" % key)<EOL><DEDENT><DEDENT>except KeyError:<EOL><INDENT>logging.info("<STR_LIT>" % key)<EOL><DEDENT><DEDENT>sess.run(ops)<EOL>logging.info("<STR_LIT>" % name)<EOL>

Restore the parameters saved by ``tl.files.save_npz_dict()``.

    Parameters
    ----------
    name : str
        The name of the `.npz` file.
    sess : Session
        TensorFlow Session.


return os.path.isfile(filepath)<EOL>

Check whether a file exists by given file path.


def save_ckpt(<EOL>sess=None, mode_name='<STR_LIT>', save_dir='<STR_LIT>', var_list=None, global_step=None, printable=False<EOL>):

if sess is None:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if var_list is None:<EOL><INDENT>var_list = []<EOL><DEDENT>ckpt_file = os.path.join(save_dir, mode_name)<EOL>if var_list == []:<EOL><INDENT>var_list = tf.global_variables()<EOL><DEDENT>logging.info("<STR_LIT>" % (ckpt_file, len(var_list)))<EOL>if printable:<EOL><INDENT>for idx, v in enumerate(var_list):<EOL><INDENT>logging.info("<STR_LIT>".format(idx, v.name, str(v.get_shape())))<EOL><DEDENT><DEDENT>saver = tf.train.Saver(var_list)<EOL>saver.save(sess, ckpt_file, global_step=global_step)<EOL>

Save parameters into `ckpt` file.

    Parameters
    ------------
    sess : Session
        TensorFlow Session.
    mode_name : str
        The name of the model, default is ``model.ckpt``.
    save_dir : str
        The path / file directory to the `ckpt`, default is ``checkpoint``.
    var_list : list of tensor
        The parameters / variables (tensor) to be saved. If empty, save all global variables (default).
    global_step : int or None
        Step number.
    printable : boolean
        Whether to print all parameters information.

    See Also
    --------
    load_ckpt


def load_file_list(path=None, regx='<STR_LIT>', printable=True, keep_prefix=False):

if path is None:<EOL><INDENT>path = os.getcwd()<EOL><DEDENT>file_list = os.listdir(path)<EOL>return_list = []<EOL>for _, f in enumerate(file_list):<EOL><INDENT>if re.search(regx, f):<EOL><INDENT>return_list.append(f)<EOL><DEDENT><DEDENT>if keep_prefix:<EOL><INDENT>for i, f in enumerate(return_list):<EOL><INDENT>return_list[i] = os.path.join(path, f)<EOL><DEDENT><DEDENT>if printable:<EOL><INDENT>logging.info('<STR_LIT>' % return_list)<EOL>logging.info('<STR_LIT>' % len(return_list))<EOL><DEDENT>return return_list<EOL>

r"""Return a file list in a folder by given a path and regular expression.

    Parameters
    ----------
    path : str or None
        A folder path, if `None`, use the current directory.
    regx : str
        The regx of file name.
    printable : boolean
        Whether to print the files infomation.
    keep_prefix : boolean
        Whether to keep path in the file name.

    Examples
    ----------
    >>> file_list = tl.files.load_file_list(path=None, regx='w1pre_[0-9]+\.(npz)')


def load_flickr1M_dataset(tag='<STR_LIT>', size=<NUM_LIT:10>, path="<STR_LIT:data>", n_threads=<NUM_LIT:50>, printable=False):

path = os.path.join(path, '<STR_LIT>')<EOL>logging.info("<STR_LIT>".format(size * <NUM_LIT:10>, size * <NUM_LIT>))<EOL>images_zip = [<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>'<EOL>]<EOL>tag_zip = '<STR_LIT>'<EOL>url = '<STR_LIT>'<EOL>for image_zip in images_zip[<NUM_LIT:0>:size]:<EOL><INDENT>image_folder = image_zip.split("<STR_LIT:.>")[<NUM_LIT:0>]<EOL>if folder_exists(os.path.join(path, image_folder)) is False:<EOL><INDENT>logging.info("<STR_LIT>".format(image_folder, path))<EOL>maybe_download_and_extract(image_zip, path, url, extract=True)<EOL>del_file(os.path.join(path, image_zip))<EOL>shutil.move(os.path.join(path, '<STR_LIT>'), os.path.join(path, image_folder))<EOL><DEDENT>else:<EOL><INDENT>logging.info("<STR_LIT>".format(image_folder, path))<EOL><DEDENT><DEDENT>if folder_exists(os.path.join(path, "<STR_LIT>")) is False:<EOL><INDENT>logging.info("<STR_LIT>".format(path))<EOL>maybe_download_and_extract(tag_zip, path, url, extract=True)<EOL>del_file(os.path.join(path, tag_zip))<EOL><DEDENT>else:<EOL><INDENT>logging.info("<STR_LIT>".format(path))<EOL><DEDENT>images_list = []<EOL>images_folder_list = []<EOL>for i in range(<NUM_LIT:0>, size):<EOL><INDENT>images_folder_list += load_folder_list(path=os.path.join(path, '<STR_LIT>' % i))<EOL><DEDENT>images_folder_list.sort(key=lambda s: int(s.split('<STR_LIT:/>')[-<NUM_LIT:1>]))  <EOL>for folder in images_folder_list[<NUM_LIT:0>:size * <NUM_LIT:10>]:<EOL><INDENT>tmp = load_file_list(path=folder, regx='<STR_LIT>', printable=False)<EOL>tmp.sort(key=lambda s: int(s.split('<STR_LIT:.>')[-<NUM_LIT:2>]))  <EOL>images_list.extend([os.path.join(folder, x) for x in tmp])<EOL><DEDENT>tag_list = []<EOL>tag_folder_list = load_folder_list(os.path.join(path, "<STR_LIT>"))<EOL>tag_folder_list.sort(key=lambda s: int(os.path.basename(s)))<EOL>for folder in tag_folder_list[<NUM_LIT:0>:size * <NUM_LIT:10>]:<EOL><INDENT>tmp = load_file_list(path=folder, regx='<STR_LIT>', printable=False)<EOL>tmp.sort(key=lambda s: int(s.split('<STR_LIT:.>')[-<NUM_LIT:2>]))  <EOL>tmp = [os.path.join(folder, s) for s in tmp]<EOL>tag_list += tmp<EOL><DEDENT>logging.info("<STR_LIT>".format(tag))<EOL>select_images_list = []<EOL>for idx, _val in enumerate(tag_list):<EOL><INDENT>tags = read_file(tag_list[idx]).split('<STR_LIT:\n>')<EOL>if tag in tags:<EOL><INDENT>select_images_list.append(images_list[idx])<EOL><DEDENT><DEDENT>logging.info("<STR_LIT>".format(tag))<EOL>images = visualize.read_images(select_images_list, '<STR_LIT>', n_threads=n_threads, printable=printable)<EOL>return images<EOL>

Load Flick1M dataset.

    Returns a list of images by a given tag from Flickr1M dataset,
    it will download Flickr1M from `the official website <http://press.liacs.nl/mirflickr/mirdownload.html>`__
    at the first time you use it.

    Parameters
    ------------
    tag : str or None
        What images to return.
            - If you want to get images with tag, use string like 'dog', 'red', see `Flickr Search <https://www.flickr.com/search/>`__.
            - If you want to get all images, set to ``None``.

    size : int
        integer between 1 to 10. 1 means 100k images ... 5 means 500k images, 10 means all 1 million images. Default is 10.
    path : str
        The path that the data is downloaded to, defaults is ``data/flickr25k/``.
    n_threads : int
        The number of thread to read image.
    printable : boolean
        Whether to print infomation when reading images, default is ``False``.

    Examples
    ----------
    Use 200k images

    >>> images = tl.files.load_flickr1M_dataset(tag='zebra', size=2)

    Use 1 Million images

    >>> images = tl.files.load_flickr1M_dataset(tag='zebra')


return os.path.isdir(folderpath)<EOL>

Check whether a folder exists by given folder path.


def save_npz(save_list=None, name='<STR_LIT>', sess=None):

logging.info("<STR_LIT>" % name)<EOL>if save_list is None:<EOL><INDENT>save_list = []<EOL><DEDENT>save_list_var = []<EOL>if sess:<EOL><INDENT>save_list_var = sess.run(save_list)<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>save_list_var.extend([v.eval() for v in save_list])<EOL><DEDENT>except Exception:<EOL><INDENT>logging.info(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT><DEDENT>np.savez(name, params=save_list_var)<EOL>save_list_var = None<EOL>del save_list_var<EOL>logging.info("<STR_LIT>")<EOL>

Input parameters and the file name, save parameters into .npz file. Use tl.utils.load_npz() to restore.

    Parameters
    ----------
    save_list : list of tensor
        A list of parameters (tensor) to be saved.
    name : str
        The name of the `.npz` file.
    sess : None or Session
        Session may be required in some case.

    Examples
    --------
    Save model to npz

    >>> tl.files.save_npz(network.all_params, name='model.npz', sess=sess)

    Load model from npz (Method 1)

    >>> load_params = tl.files.load_npz(name='model.npz')
    >>> tl.files.assign_params(sess, load_params, network)

    Load model from npz (Method 2)

    >>> tl.files.load_and_assign_npz(sess=sess, name='model.npz', network=network)

    Notes
    -----
    If you got session issues, you can change the value.eval() to value.eval(session=sess)

    References
    ----------
    `Saving dictionary using numpy <http://stackoverflow.com/questions/22315595/saving-dictionary-of-header-information-using-numpy-savez>`__


def roi_pooling(input, rois, pool_height, pool_width):

<EOL>out = roi_pooling_module.roi_pooling(input, rois, pool_height=pool_height, pool_width=pool_width)<EOL>output, argmax_output = out[<NUM_LIT:0>], out[<NUM_LIT:1>]<EOL>return output<EOL>

returns a tensorflow operation for computing the Region of Interest Pooling

@arg input: feature maps on which to perform the pooling operation
@arg rois: list of regions of interest in the format (feature map index, upper left, bottom right)
@arg pool_width: size of the pooling sections


@private_method<EOL><INDENT>def _to_b_h_w_n_c(self, x, x_shape):<DEDENT>

x = tf.reshape(x, (-<NUM_LIT:1>, x_shape[<NUM_LIT:4>], x_shape[<NUM_LIT:1>], x_shape[<NUM_LIT:2>], x_shape[<NUM_LIT:3>]))<EOL>x = tf.transpose(x, [<NUM_LIT:0>, <NUM_LIT:2>, <NUM_LIT:3>, <NUM_LIT:4>, <NUM_LIT:1>])<EOL>return x<EOL>


return tf.reshape(a, [-<NUM_LIT:1>])<EOL>


@private_method<EOL><INDENT>def _to_bc_h_w(self, x, x_shape):<DEDENT>

x = tf.transpose(x, [<NUM_LIT:0>, <NUM_LIT:3>, <NUM_LIT:1>, <NUM_LIT:2>])<EOL>x = tf.reshape(x, (-<NUM_LIT:1>, x_shape[<NUM_LIT:1>], x_shape[<NUM_LIT:2>]))<EOL>return x<EOL>


if data_format == '<STR_LIT>':<EOL><INDENT>return x * b<EOL><DEDENT>elif data_format == '<STR_LIT>':<EOL><INDENT>return x * _to_channel_first_bias(b)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>' % data_format)<EOL><DEDENT>

The multiplication counter part of tf.nn.bias_add.


def batch_normalization(x, mean, variance, offset, scale, variance_epsilon, data_format, name=None):

with ops.name_scope(name, '<STR_LIT>', [x, mean, variance, scale, offset]):<EOL><INDENT>inv = math_ops.rsqrt(variance + variance_epsilon)<EOL>if scale is not None:<EOL><INDENT>inv *= scale<EOL><DEDENT>a = math_ops.cast(inv, x.dtype)<EOL>b = math_ops.cast(offset - mean * inv if offset is not None else -mean * inv, x.dtype)<EOL>df = {'<STR_LIT>': '<STR_LIT>', '<STR_LIT>': '<STR_LIT>'}<EOL>return _bias_add(_bias_scale(x, a, df[data_format]), b, df[data_format])<EOL><DEDENT>

Data Format aware version of tf.nn.batch_normalization.


if data_format == '<STR_LIT>':<EOL><INDENT>return tf.add(x, b)<EOL><DEDENT>elif data_format == '<STR_LIT>':<EOL><INDENT>return tf.add(x, _to_channel_first_bias(b))<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>' % data_format)<EOL><DEDENT>

Alternative implementation of tf.nn.bias_add which is compatiable with tensorRT.


for i, layer in enumerate(self.all_layers):<EOL><INDENT>logging.info(<EOL>"<STR_LIT>".format(i, layer.name, str(layer.get_shape()), layer.dtype.name)<EOL>)<EOL><DEDENT>

Print all info of layers in the network.


def print_params(self, details=True, session=None):

for i, p in enumerate(self.all_params):<EOL><INDENT>if details:<EOL><INDENT>try:<EOL><INDENT>val = p.eval(session=session)<EOL>logging.info(<EOL>"<STR_LIT>".<EOL>format(i, p.name, str(val.shape), p.dtype.name, val.mean(), np.median(val), val.std())<EOL>)<EOL><DEDENT>except Exception as e:<EOL><INDENT>logging.info(str(e))<EOL>raise Exception(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>logging.info("<STR_LIT>".format(i, p.name, str(p.get_shape()), p.dtype.name))<EOL><DEDENT><DEDENT>logging.info("<STR_LIT>" % self.count_params())<EOL>

Print all info of parameters in the network


stack = inspect.stack()<EOL>if len(stack) < skip + <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>args, _, _, values = inspect.getargvalues(stack[skip][<NUM_LIT:0>])<EOL>params = {}<EOL>for arg in args:<EOL><INDENT>if values[arg] is not None and arg not in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>val = values[arg]<EOL>if inspect.isfunction(val):<EOL><INDENT>params[arg] = {"<STR_LIT>": val.__module__, "<STR_LIT>": val.__name__}<EOL><DEDENT>elif arg.endswith('<STR_LIT>'):<EOL><INDENT>continue<EOL><DEDENT>else:<EOL><INDENT>params[arg] = val<EOL><DEDENT><DEDENT><DEDENT>return params<EOL>

Get all arguments of current layer for saving the graph.


def transformer(U, theta, out_size, name='<STR_LIT>'):

def _repeat(x, n_repeats):<EOL><INDENT>with tf.variable_scope('<STR_LIT>'):<EOL><INDENT>rep = tf.transpose(tf.expand_dims(tf.ones(shape=tf.stack([<EOL>n_repeats,<EOL>])), <NUM_LIT:1>), [<NUM_LIT:1>, <NUM_LIT:0>])<EOL>rep = tf.cast(rep, '<STR_LIT>')<EOL>x = tf.matmul(tf.reshape(x, (-<NUM_LIT:1>, <NUM_LIT:1>)), rep)<EOL>return tf.reshape(x, [-<NUM_LIT:1>])<EOL><DEDENT><DEDENT>def _interpolate(im, x, y, out_size):<EOL><INDENT>with tf.variable_scope('<STR_LIT>'):<EOL><INDENT>num_batch = tf.shape(im)[<NUM_LIT:0>]<EOL>height = tf.shape(im)[<NUM_LIT:1>]<EOL>width = tf.shape(im)[<NUM_LIT:2>]<EOL>channels = tf.shape(im)[<NUM_LIT:3>]<EOL>x = tf.cast(x, '<STR_LIT>')<EOL>y = tf.cast(y, '<STR_LIT>')<EOL>height_f = tf.cast(height, '<STR_LIT>')<EOL>width_f = tf.cast(width, '<STR_LIT>')<EOL>out_height = out_size[<NUM_LIT:0>]<EOL>out_width = out_size[<NUM_LIT:1>]<EOL>zero = tf.zeros([], dtype='<STR_LIT>')<EOL>max_y = tf.cast(tf.shape(im)[<NUM_LIT:1>] - <NUM_LIT:1>, '<STR_LIT>')<EOL>max_x = tf.cast(tf.shape(im)[<NUM_LIT:2>] - <NUM_LIT:1>, '<STR_LIT>')<EOL>x = (x + <NUM_LIT:1.0>) * (width_f) / <NUM_LIT><EOL>y = (y + <NUM_LIT:1.0>) * (height_f) / <NUM_LIT><EOL>x0 = tf.cast(tf.floor(x), '<STR_LIT>')<EOL>x1 = x0 + <NUM_LIT:1><EOL>y0 = tf.cast(tf.floor(y), '<STR_LIT>')<EOL>y1 = y0 + <NUM_LIT:1><EOL>x0 = tf.clip_by_value(x0, zero, max_x)<EOL>x1 = tf.clip_by_value(x1, zero, max_x)<EOL>y0 = tf.clip_by_value(y0, zero, max_y)<EOL>y1 = tf.clip_by_value(y1, zero, max_y)<EOL>dim2 = width<EOL>dim1 = width * height<EOL>base = _repeat(tf.range(num_batch) * dim1, out_height * out_width)<EOL>base_y0 = base + y0 * dim2<EOL>base_y1 = base + y1 * dim2<EOL>idx_a = base_y0 + x0<EOL>idx_b = base_y1 + x0<EOL>idx_c = base_y0 + x1<EOL>idx_d = base_y1 + x1<EOL>im_flat = tf.reshape(im, tf.stack([-<NUM_LIT:1>, channels]))<EOL>im_flat = tf.cast(im_flat, '<STR_LIT>')<EOL>Ia = tf.gather(im_flat, idx_a)<EOL>Ib = tf.gather(im_flat, idx_b)<EOL>Ic = tf.gather(im_flat, idx_c)<EOL>Id = tf.gather(im_flat, idx_d)<EOL>x0_f = tf.cast(x0, '<STR_LIT>')<EOL>x1_f = tf.cast(x1, '<STR_LIT>')<EOL>y0_f = tf.cast(y0, '<STR_LIT>')<EOL>y1_f = tf.cast(y1, '<STR_LIT>')<EOL>wa = tf.expand_dims(((x1_f - x) * (y1_f - y)), <NUM_LIT:1>)<EOL>wb = tf.expand_dims(((x1_f - x) * (y - y0_f)), <NUM_LIT:1>)<EOL>wc = tf.expand_dims(((x - x0_f) * (y1_f - y)), <NUM_LIT:1>)<EOL>wd = tf.expand_dims(((x - x0_f) * (y - y0_f)), <NUM_LIT:1>)<EOL>output = tf.add_n([wa * Ia, wb * Ib, wc * Ic, wd * Id])<EOL>return output<EOL><DEDENT><DEDENT>def _meshgrid(height, width):<EOL><INDENT>with tf.variable_scope('<STR_LIT>'):<EOL><INDENT>x_t = tf.matmul(<EOL>tf.ones(shape=tf.stack([height, <NUM_LIT:1>])),<EOL>tf.transpose(tf.expand_dims(tf.linspace(-<NUM_LIT:1.0>, <NUM_LIT:1.0>, width), <NUM_LIT:1>), [<NUM_LIT:1>, <NUM_LIT:0>])<EOL>)<EOL>y_t = tf.matmul(tf.expand_dims(tf.linspace(-<NUM_LIT:1.0>, <NUM_LIT:1.0>, height), <NUM_LIT:1>), tf.ones(shape=tf.stack([<NUM_LIT:1>, width])))<EOL>x_t_flat = tf.reshape(x_t, (<NUM_LIT:1>, -<NUM_LIT:1>))<EOL>y_t_flat = tf.reshape(y_t, (<NUM_LIT:1>, -<NUM_LIT:1>))<EOL>ones = tf.ones_like(x_t_flat)<EOL>grid = tf.concat(axis=<NUM_LIT:0>, values=[x_t_flat, y_t_flat, ones])<EOL>return grid<EOL><DEDENT><DEDENT>def _transform(theta, input_dim, out_size):<EOL><INDENT>with tf.variable_scope('<STR_LIT>'):<EOL><INDENT>num_batch = tf.shape(input_dim)[<NUM_LIT:0>]<EOL>num_channels = tf.shape(input_dim)[<NUM_LIT:3>]<EOL>theta = tf.reshape(theta, (-<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:3>))<EOL>theta = tf.cast(theta, '<STR_LIT>')<EOL>out_height = out_size[<NUM_LIT:0>]<EOL>out_width = out_size[<NUM_LIT:1>]<EOL>grid = _meshgrid(out_height, out_width)<EOL>grid = tf.expand_dims(grid, <NUM_LIT:0>)<EOL>grid = tf.reshape(grid, [-<NUM_LIT:1>])<EOL>grid = tf.tile(grid, tf.stack([num_batch]))<EOL>grid = tf.reshape(grid, tf.stack([num_batch, <NUM_LIT:3>, -<NUM_LIT:1>]))<EOL>T_g = tf.matmul(theta, grid)<EOL>x_s = tf.slice(T_g, [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>], [-<NUM_LIT:1>, <NUM_LIT:1>, -<NUM_LIT:1>])<EOL>y_s = tf.slice(T_g, [<NUM_LIT:0>, <NUM_LIT:1>, <NUM_LIT:0>], [-<NUM_LIT:1>, <NUM_LIT:1>, -<NUM_LIT:1>])<EOL>x_s_flat = tf.reshape(x_s, [-<NUM_LIT:1>])<EOL>y_s_flat = tf.reshape(y_s, [-<NUM_LIT:1>])<EOL>input_transformed = _interpolate(input_dim, x_s_flat, y_s_flat, out_size)<EOL>output = tf.reshape(input_transformed, tf.stack([num_batch, out_height, out_width, num_channels]))<EOL>return output<EOL><DEDENT><DEDENT>with tf.variable_scope(name):<EOL><INDENT>output = _transform(theta, U, out_size)<EOL>return output<EOL><DEDENT>

Spatial Transformer Layer for `2D Affine Transformation <https://en.wikipedia.org/wiki/Affine_transformation>`__
    , see :class:`SpatialTransformer2dAffineLayer` class.

    Parameters
    ----------
    U : list of float
        The output of a convolutional net should have the
        shape [num_batch, height, width, num_channels].
    theta: float
        The output of the localisation network should be [num_batch, 6], value range should be [0, 1] (via tanh).
    out_size: tuple of int
        The size of the output of the network (height, width)
    name: str
        Optional function name

    Returns
    -------
    Tensor
        The transformed tensor.

    References
    ----------
    - `Spatial Transformer Networks <https://arxiv.org/abs/1506.02025>`__
    - `TensorFlow/Models <https://github.com/tensorflow/models/tree/master/transformer>`__

    Notes
    -----
    To initialize the network to the identity transform init.

    >>> import tensorflow as tf
    >>> # ``theta`` to
    >>> identity = np.array([[1., 0., 0.], [0., 1., 0.]])
    >>> identity = identity.flatten()
    >>> theta = tf.Variable(initial_value=identity)


@deprecated_alias(printable='<STR_LIT>', end_support_version=<NUM_LIT>)  <EOL>def get_variables_with_name(name=None, train_only=True, verbose=False):

if name is None:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>logging.info("<STR_LIT>" % name)<EOL>if train_only:<EOL><INDENT>t_vars = tf.trainable_variables()<EOL><DEDENT>else:<EOL><INDENT>t_vars = tf.global_variables()<EOL><DEDENT>d_vars = [var for var in t_vars if name in var.name]<EOL>if verbose:<EOL><INDENT>for idx, v in enumerate(d_vars):<EOL><INDENT>logging.info("<STR_LIT>".format(idx, v.name, str(v.get_shape())))<EOL><DEDENT><DEDENT>return d_vars<EOL>

Get a list of TensorFlow variables by a given name scope.

    Parameters
    ----------
    name : str
        Get the variables that contain this name.
    train_only : boolean
        If Ture, only get the trainable variables.
    verbose : boolean
        If True, print the information of all variables.

    Returns
    -------
    list of Tensor
        A list of TensorFlow variables

    Examples
    --------
    >>> import tensorlayer as tl
    >>> dense_vars = tl.layers.get_variables_with_name('dense', True, True)


return tf.clip_by_value(grad, -<NUM_LIT:1>, <NUM_LIT:1>)<EOL>

Clip and binarize tensor using the straight through estimator (STE) for the gradient.


def __call__(self, inputs, state, scope=None):

raise NotImplementedError("<STR_LIT>")<EOL>

Run this RNN cell on inputs, starting from the given state.


data_shape_size = data.get_shape().ndims<EOL>if data_shape_size == <NUM_LIT:3>:<EOL><INDENT>return tf.cast(tf.reduce_any(tf.not_equal(data, pad_val), axis=<NUM_LIT:2>), dtype=tf.int32)<EOL><DEDENT>elif data_shape_size == <NUM_LIT:2>:<EOL><INDENT>return tf.cast(tf.not_equal(data, pad_val), dtype=tf.int32)<EOL><DEDENT>elif data_shape_size == <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>" % (data_shape_size))<EOL><DEDENT>

Return tensor for mask, if input is ``tf.string``.


return tf.reduce_sum(tf.cast(tf.greater(data, tf.zeros_like(data)), tf.int32), <NUM_LIT:1>)<EOL>

An op to compute the length of a sequence, from input shape of [batch_size, n_step(max)],
    it can be used when the features of padding (on right hand side) are all zeros.

    Parameters
    -----------
    data : tensor
        [batch_size, n_step(max)] with zero padding on right hand side.

    Examples
    --------
    >>> data = [[1,2,0,0,0],
    ...         [1,2,3,0,0],
    ...         [1,2,6,1,0]]
    >>> o = retrieve_seq_length_op2(data)
    >>> sess = tf.InteractiveSession()
    >>> tl.layers.initialize_global_variables(sess)
    >>> print(o.eval())
    [2 3 4]


def __init__(<EOL>self, shape, filter_size, num_features, forget_bias=<NUM_LIT:1.0>, input_size=None, state_is_tuple=False,<EOL>act=tf.nn.tanh<EOL>):

<EOL>if input_size is not None:<EOL><INDENT>logging.warn("<STR_LIT>", self)<EOL><DEDENT>self.shape = shape<EOL>self.filter_size = filter_size<EOL>self.num_features = num_features<EOL>self._forget_bias = forget_bias<EOL>self._state_is_tuple = state_is_tuple<EOL>self._activation = act<EOL>


return self._num_units<EOL>


mask = disk(radius)<EOL>x = _binary_dilation(x, selem=mask)<EOL>return x<EOL>

Return fast binary morphological dilation of an image.
    see `skimage.morphology.binary_dilation <http://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.binary_dilation>`__.

    Parameters
    -----------
    x : 2D array
        A binary image.
    radius : int
        For the radius of mask.

    Returns
    -------
    numpy.array
        A processed binary image.


factor = np.random.uniform(<NUM_LIT:0>, <NUM_LIT:1>)<EOL>if prob >= factor:<EOL><INDENT>filp_matrix = np.array([[ <NUM_LIT:1.> , <NUM_LIT:0.>, <NUM_LIT:0.> ],[ <NUM_LIT:0.>, -<NUM_LIT:1.>, <NUM_LIT:0.> ],[ <NUM_LIT:0.>, <NUM_LIT:0.>, <NUM_LIT:1.> ]])<EOL>return filp_matrix<EOL><DEDENT>else:<EOL><INDENT>filp_matrix = np.array([[ <NUM_LIT:1.> , <NUM_LIT:0.>, <NUM_LIT:0.> ],[ <NUM_LIT:0.>, <NUM_LIT:1.>, <NUM_LIT:0.> ],[ <NUM_LIT:0.>, <NUM_LIT:0.>, <NUM_LIT:1.> ]])<EOL>return filp_matrix<EOL><DEDENT>

Create an affine transformation for image vertical flipping.
    NOTE: In OpenCV, x is width and y is height.

    Parameters
    ----------
    prob : float
        Probability to flip the image. 1.0 means always flip.

    Returns
    -------
    numpy.array
        An affine transform matrix.


def shift(<EOL>x, wrg=<NUM_LIT:0.1>, hrg=<NUM_LIT:0.1>, is_random=False, row_index=<NUM_LIT:0>, col_index=<NUM_LIT:1>, channel_index=<NUM_LIT:2>, fill_mode='<STR_LIT>', cval=<NUM_LIT:0.>,<EOL>order=<NUM_LIT:1><EOL>):

h, w = x.shape[row_index], x.shape[col_index]<EOL>if is_random:<EOL><INDENT>tx = np.random.uniform(-hrg, hrg) * h<EOL>ty = np.random.uniform(-wrg, wrg) * w<EOL><DEDENT>else:<EOL><INDENT>tx, ty = hrg * h, wrg * w<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>x = affine_transform(x, transform_matrix, channel_index, fill_mode, cval, order)<EOL>return x<EOL>

Shift an image randomly or non-randomly.

    Parameters
    -----------
    x : numpy.array
        An image with dimension of [row, col, channel] (default).
    wrg : float
        Percentage of shift in axis x, usually -0.25 ~ 0.25.
    hrg : float
        Percentage of shift in axis y, usually -0.25 ~ 0.25.
    is_random : boolean
        If True, randomly shift. Default is False.
    row_index col_index and channel_index : int
        Index of row, col and channel, default (0, 1, 2), for theano (1, 2, 0).
    fill_mode : str
        Method to fill missing pixel, default `nearest`, more options `constant`, `reflect` or `wrap`, see `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`__
    cval : float
        Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0.
    order : int
        The order of interpolation. The order has to be in the range 0-5. See ``tl.prepro.affine_transform`` and `scipy ndimage affine_transform <https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.interpolation.affine_transform.html>`__

    Returns
    -------
    numpy.array
        A processed image.


if isinstance(zoom_range, (float, int)):<EOL><INDENT>scale = zoom_range<EOL><DEDENT>elif isinstance(zoom_range, tuple):<EOL><INDENT>scale = np.random.uniform(zoom_range[<NUM_LIT:0>], zoom_range[<NUM_LIT:1>])<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>zoom_matrix = np.array([[scale, <NUM_LIT:0>, <NUM_LIT:0>],[<NUM_LIT:0>, scale, <NUM_LIT:0>],[<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:1>]])<EOL>return zoom_matrix<EOL>

Create an affine transform matrix for zooming/scaling an image's height and width.
    OpenCV format, x is width.

    Parameters
    -----------
    x : numpy.array
        An image with dimension of [row, col, channel] (default).
    zoom_range : float or tuple of 2 floats
        The zooming/scaling ratio, greater than 1 means larger.
            - float, a fixed ratio.
            - tuple of 2 floats, randomly sample a value as the ratio between these 2 values.

    Returns
    -------
    numpy.array
        An affine transform matrix.


coords_result_list = []<EOL>for coords in coords_list:<EOL><INDENT>coords = np.asarray(coords)<EOL>coords = coords.transpose([<NUM_LIT:1>, <NUM_LIT:0>])<EOL>coords = np.insert(coords, <NUM_LIT:2>, <NUM_LIT:1>, axis=<NUM_LIT:0>)<EOL>coords_result = np.matmul(transform_matrix, coords)<EOL>coords_result = coords_result[<NUM_LIT:0>:<NUM_LIT:2>, :].transpose([<NUM_LIT:1>, <NUM_LIT:0>])<EOL>coords_result_list.append(coords_result)<EOL><DEDENT>return coords_result_list<EOL>

Transform keypoint coordinates according to a given affine transform matrix.
    OpenCV format, x is width.

    Note that, for pose estimation task, flipping requires maintaining the left and right body information.
    We should not flip the left and right body, so please use ``tl.prepro.keypoint_random_flip``.

    Parameters
    -----------
    coords_list : list of list of tuple/list
        The coordinates
        e.g., the keypoint coordinates of every person in an image.
    transform_matrix : numpy.array
        Transform matrix, OpenCV format.

    Examples
    ---------
    >>> # 1. get all affine transform matrices
    >>> M_rotate = tl.prepro.affine_rotation_matrix(angle=20)
    >>> M_flip = tl.prepro.affine_horizontal_flip_matrix(prob=1)
    >>> # 2. combine all affine transform matrices to one matrix
    >>> M_combined = dot(M_flip).dot(M_rotate)
    >>> # 3. transfrom the matrix from Cartesian coordinate (the origin in the middle of image)
    >>> # to Image coordinate (the origin on the top-left of image)
    >>> transform_matrix = tl.prepro.transform_matrix_offset_center(M_combined, x=w, y=h)
    >>> # 4. then we can transfrom the image once for all transformations
    >>> result = tl.prepro.affine_transform_cv2(image, transform_matrix)  # 76 times faster
    >>> # 5. transform keypoint coordinates
    >>> coords = [[(50, 100), (100, 100), (100, 50), (200, 200)], [(250, 50), (200, 50), (200, 100)]]
    >>> coords_result = tl.prepro.affine_transform_keypoints(coords, transform_matrix)


if coords is None:<EOL><INDENT>coords = []<EOL><DEDENT>if shape is None:<EOL><INDENT>shape = [<NUM_LIT:100>, <NUM_LIT:200>]<EOL><DEDENT>imh, imw = shape[<NUM_LIT:0>], shape[<NUM_LIT:1>]<EOL>imh = imh * <NUM_LIT:1.0>  <EOL>imw = imw * <NUM_LIT:1.0><EOL>coords_new = list()<EOL>for coord in coords:<EOL><INDENT>if len(coord) != <NUM_LIT:4>:<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>x = coord[<NUM_LIT:0>] / imw<EOL>y = coord[<NUM_LIT:1>] / imh<EOL>w = coord[<NUM_LIT:2>] / imw<EOL>h = coord[<NUM_LIT:3>] / imh<EOL>coords_new.append([x, y, w, h])<EOL><DEDENT>return coords_new<EOL>

Scale down a list of coordinates from pixel unit to the ratio of image size i.e. in the range of [0, 1].

    Parameters
    ------------
    coords : list of list of 4 ints or None
        For coordinates of more than one images .e.g.[[x, y, w, h], [x, y, w, h], ...].
    shape : list of 2 int or None
        【height, width].

    Returns
    -------
    list of list of 4 numbers
        A list of new bounding boxes.

    Examples
    ---------
    >>> coords = obj_box_coords_rescale(coords=[[30, 40, 50, 50], [10, 10, 20, 20]], shape=[100, 100])
    >>> print(coords)
      [[0.3, 0.4, 0.5, 0.5], [0.1, 0.1, 0.2, 0.2]]
    >>> coords = obj_box_coords_rescale(coords=[[30, 40, 50, 50]], shape=[50, 100])
    >>> print(coords)
      [[0.3, 0.8, 0.5, 1.0]]
    >>> coords = obj_box_coords_rescale(coords=[[30, 40, 50, 50]], shape=[100, 200])
    >>> print(coords)
      [[0.15, 0.4, 0.25, 0.5]]

    Returns
    -------
    list of 4 numbers
        New coordinates.


annotations = annotations.split("<STR_LIT:\n>")<EOL>ann = []<EOL>for a in annotations:<EOL><INDENT>a = a.split()<EOL>if len(a) == <NUM_LIT:5>:<EOL><INDENT>for i, _v in enumerate(a):<EOL><INDENT>if i == <NUM_LIT:0>:<EOL><INDENT>a[i] = int(a[i])<EOL><DEDENT>else:<EOL><INDENT>a[i] = float(a[i])<EOL><DEDENT><DEDENT>ann.append(a)<EOL><DEDENT><DEDENT>return ann<EOL>

r"""Input string format of class, x, y, w, h, return list of list format.

    Parameters
    -----------
    annotations : str
        The annotations in darkent format "class, x, y, w, h ...." seperated by "\\n".

    Returns
    -------
    list of list of 4 numbers
        List of bounding box.


def obj_box_horizontal_flip(im, coords=None, is_rescale=False, is_center=False, is_random=False):

if coords is None:<EOL><INDENT>coords = []<EOL><DEDENT>def _flip(im, coords):<EOL><INDENT>im = flip_axis(im, axis=<NUM_LIT:1>, is_random=False)<EOL>coords_new = list()<EOL>for coord in coords:<EOL><INDENT>if len(coord) != <NUM_LIT:4>:<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>if is_rescale:<EOL><INDENT>if is_center:<EOL><INDENT>x = <NUM_LIT:1.> - coord[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>x = <NUM_LIT:1.> - coord[<NUM_LIT:0>] - coord[<NUM_LIT:2>]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if is_center:<EOL><INDENT>x = im.shape[<NUM_LIT:1>] - coord[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>x = im.shape[<NUM_LIT:1>] - coord[<NUM_LIT:0>] - coord[<NUM_LIT:2>]<EOL><DEDENT><DEDENT>coords_new.append([x, coord[<NUM_LIT:1>], coord[<NUM_LIT:2>], coord[<NUM_LIT:3>]])<EOL><DEDENT>return im, coords_new<EOL><DEDENT>if is_random:<EOL><INDENT>factor = np.random.uniform(-<NUM_LIT:1>, <NUM_LIT:1>)<EOL>if factor > <NUM_LIT:0>:<EOL><INDENT>return _flip(im, coords)<EOL><DEDENT>else:<EOL><INDENT>return im, coords<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return _flip(im, coords)<EOL><DEDENT>

Left-right flip the image and coordinates for object detection.

    Parameters
    ----------
    im : numpy.array
        An image with dimension of [row, col, channel] (default).
    coords : list of list of 4 int/float or None
        Coordinates [[x, y, w, h], [x, y, w, h], ...].
    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.
    is_random : boolean
        If True, randomly flip. Default is False.

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

    Examples
    --------
    >>> im = np.zeros([80, 100])    # as an image with shape width=100, height=80
    >>> im, coords = obj_box_left_right_flip(im, coords=[[0.2, 0.4, 0.3, 0.3], [0.1, 0.5, 0.2, 0.3]], is_rescale=True, is_center=True, is_random=False)
    >>> print(coords)
      [[0.8, 0.4, 0.3, 0.3], [0.9, 0.5, 0.2, 0.3]]
    >>> im, coords = obj_box_left_right_flip(im, coords=[[0.2, 0.4, 0.3, 0.3]], is_rescale=True, is_center=False, is_random=False)
    >>> print(coords)
      [[0.5, 0.4, 0.3, 0.3]]
    >>> im, coords = obj_box_left_right_flip(im, coords=[[20, 40, 30, 30]], is_rescale=False, is_center=True, is_random=False)
    >>> print(coords)
      [[80, 40, 30, 30]]
    >>> im, coords = obj_box_left_right_flip(im, coords=[[20, 40, 30, 30]], is_rescale=False, is_center=False, is_random=False)
    >>> print(coords)
      [[50, 40, 30, 30]]


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 respective_zoom(x, h_range=(<NUM_LIT>, <NUM_LIT>), w_range=(<NUM_LIT>, <NUM_LIT>), flags=None, border_mode='<STR_LIT>'):

zoom_matrix = affine_respective_zoom_matrix(h_range=h_range, w_range=w_range)<EOL>h, w = x.shape[<NUM_LIT:0>], x.shape[<NUM_LIT:1>]<EOL>transform_matrix = transform_matrix_offset_center(zoom_matrix, h, w)<EOL>x = affine_transform_cv2(<EOL>x, transform_matrix, flags=flags, border_mode=border_mode<EOL>)  <EOL>return x<EOL>

Zooming/Scaling a single image that height and width are changed independently.

    Parameters
    -----------
    x : numpy.array
        An image with dimension of [row, col, channel] (default).
    h_range : float or tuple of 2 floats
        The zooming/scaling ratio of height, greater than 1 means larger.
            - float, a fixed ratio.
            - tuple of 2 floats, randomly sample a value as the ratio between 2 values.
    w_range : float or tuple of 2 floats
        The zooming/scaling ratio of width, greater than 1 means larger.
            - float, a fixed ratio.
            - tuple of 2 floats, randomly sample a value as the ratio between 2 values.
    border_mode : str
        - `constant`, pad the image with a constant value (i.e. black or 0)
        - `replicate`, the row or column at the very edge of the original is replicated to the extra border.

    Returns
    -------
    numpy.array
        A processed image.


def shear_multi2(<EOL>x, shear=(<NUM_LIT:0.1>, <NUM_LIT:0.1>), is_random=False, row_index=<NUM_LIT:0>, col_index=<NUM_LIT:1>, channel_index=<NUM_LIT:2>, fill_mode='<STR_LIT>', cval=<NUM_LIT:0.>,<EOL>order=<NUM_LIT:1><EOL>):

if len(shear) != <NUM_LIT:2>:<EOL><INDENT>raise AssertionError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>if isinstance(shear, tuple):<EOL><INDENT>shear = list(shear)<EOL><DEDENT>if is_random:<EOL><INDENT>shear[<NUM_LIT:0>] = np.random.uniform(-shear[<NUM_LIT:0>], shear[<NUM_LIT:0>])<EOL>shear[<NUM_LIT:1>] = np.random.uniform(-shear[<NUM_LIT:1>], shear[<NUM_LIT:1>])<EOL><DEDENT>shear_matrix = np.array([[<NUM_LIT:1>, shear[<NUM_LIT:0>], <NUM_LIT:0>], [shear[<NUM_LIT:1>], <NUM_LIT:1>, <NUM_LIT:0>], [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:1>]])<EOL>h, w = x[<NUM_LIT:0>].shape[row_index], x[<NUM_LIT:0>].shape[col_index]<EOL>transform_matrix = transform_matrix_offset_center(shear_matrix, h, w)<EOL>results = []<EOL>for data in x:<EOL><INDENT>results.append(affine_transform(data, transform_matrix, channel_index, fill_mode, cval, order))<EOL><DEDENT>return np.asarray(results)<EOL>

Shear images with the same arguments, randomly or non-randomly.
    Usually be used for image segmentation which x=[X, Y], X and Y should be matched.

    Parameters
    -----------
    x : list of numpy.array
        List of images with dimension of [n_images, row, col, channel] (default).
    others : args
        See ``tl.prepro.shear2``.

    Returns
    -------
    numpy.array
        A list of processed images.


def channel_shift_multi(x, intensity, is_random=False, channel_index=<NUM_LIT:2>):

if is_random:<EOL><INDENT>factor = np.random.uniform(-intensity, intensity)<EOL><DEDENT>else:<EOL><INDENT>factor = intensity<EOL><DEDENT>results = []<EOL>for data in x:<EOL><INDENT>data = np.rollaxis(data, channel_index, <NUM_LIT:0>)<EOL>min_x, max_x = np.min(data), np.max(data)<EOL>channel_images = [np.clip(x_channel + factor, min_x, max_x) for x_channel in x]<EOL>data = np.stack(channel_images, axis=<NUM_LIT:0>)<EOL>data = np.rollaxis(x, <NUM_LIT:0>, channel_index + <NUM_LIT:1>)<EOL>results.append(data)<EOL><DEDENT>return np.asarray(results)<EOL>

Shift the channels of images with the same arguments, randomly or non-randomly, see `numpy.rollaxis <https://docs.scipy.org/doc/numpy/reference/generated/numpy.rollaxis.html>`__.
    Usually be used for image segmentation which x=[X, Y], X and Y should be matched.

    Parameters
    -----------
    x : list of numpy.array
        List of images with dimension of [n_images, row, col, channel] (default).
    others : args
        See ``tl.prepro.channel_shift``.

    Returns
    -------
    numpy.array
        A list of processed images.


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]]


if len(coord) != <NUM_LIT:4>:<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>x1, y1, x2, y2 = coord<EOL>w = x2 - x1<EOL>h = y2 - y1<EOL>x_c = x1 + w / <NUM_LIT><EOL>y_c = y1 + h / <NUM_LIT><EOL>return [x_c, y_c, w, h]<EOL>

Convert one coordinate [x1, y1, x2, y2] to [x_center, y_center, w, h].
    It is the reverse process of ``obj_box_coord_centroid_to_upleft_butright``.

    Parameters
    ------------
    coord : list of 4 int/float
        One coordinate.

    Returns
    -------
    list of 4 numbers
        New bounding box.


def crop(x, wrg, hrg, is_random=False, row_index=<NUM_LIT:0>, col_index=<NUM_LIT:1>):

h, w = x.shape[row_index], x.shape[col_index]<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))<EOL>w_offset = int(np.random.uniform(<NUM_LIT:0>, w - wrg))<EOL>return x[h_offset:hrg + h_offset, w_offset:wrg + w_offset]<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>return x[h_offset:h_end, w_offset:w_end]<EOL><DEDENT>

Randomly or centrally crop an image.

    Parameters
    ----------
    x : numpy.array
        An image with dimension of [row, col, channel] (default).
    wrg : int
        Size of width.
    hrg : int
        Size of height.
    is_random : boolean,
        If True, randomly crop, else central crop. Default is False.
    row_index: int
        index of row.
    col_index: int
        index of column.

    Returns
    -------
    numpy.array
        A processed image.


def swirl_multi(<EOL>x, center=None, strength=<NUM_LIT:1>, radius=<NUM_LIT:100>, rotation=<NUM_LIT:0>, output_shape=None, order=<NUM_LIT:1>, mode='<STR_LIT>', cval=<NUM_LIT:0>,<EOL>clip=True, preserve_range=False, is_random=False<EOL>):

if radius == <NUM_LIT:0>:<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>rotation = np.pi / <NUM_LIT> * rotation<EOL>if is_random:<EOL><INDENT>center_h = int(np.random.uniform(<NUM_LIT:0>, x[<NUM_LIT:0>].shape[<NUM_LIT:0>]))<EOL>center_w = int(np.random.uniform(<NUM_LIT:0>, x[<NUM_LIT:0>].shape[<NUM_LIT:1>]))<EOL>center = (center_h, center_w)<EOL>strength = np.random.uniform(<NUM_LIT:0>, strength)<EOL>radius = np.random.uniform(<NUM_LIT>, radius)<EOL>rotation = np.random.uniform(-rotation, rotation)<EOL><DEDENT>results = []<EOL>for data in x:<EOL><INDENT>max_v = np.max(data)<EOL>if max_v > <NUM_LIT:1>:  <EOL><INDENT>data = data / max_v<EOL><DEDENT>swirled = skimage.transform.swirl(<EOL>data, center=center, strength=strength, radius=radius, rotation=rotation, output_shape=output_shape,<EOL>order=order, mode=mode, cval=cval, clip=clip, preserve_range=preserve_range<EOL>)<EOL>if max_v > <NUM_LIT:1>:<EOL><INDENT>swirled = swirled * max_v<EOL><DEDENT>results.append(swirled)<EOL><DEDENT>return np.asarray(results)<EOL>

Swirl multiple images with the same arguments, randomly or non-randomly.
    Usually be used for image segmentation which x=[X, Y], X and Y should be matched.

    Parameters
    -----------
    x : list of numpy.array
        List of images with dimension of [n_images, row, col, channel] (default).
    others : args
        See ``tl.prepro.swirl``.

    Returns
    -------
    numpy.array
        A list of processed images.


def affine_shift_matrix(wrg=(-<NUM_LIT:0.1>, <NUM_LIT:0.1>), hrg=(-<NUM_LIT:0.1>, <NUM_LIT:0.1>), w=<NUM_LIT:200>, h=<NUM_LIT:200>):

if isinstance(wrg, tuple):<EOL><INDENT>tx = np.random.uniform(wrg[<NUM_LIT:0>], wrg[<NUM_LIT:1>]) * w<EOL><DEDENT>else:<EOL><INDENT>tx = wrg * w<EOL><DEDENT>if isinstance(hrg, tuple):<EOL><INDENT>ty = np.random.uniform(hrg[<NUM_LIT:0>], hrg[<NUM_LIT:1>]) * h<EOL><DEDENT>else:<EOL><INDENT>ty = hrg * h<EOL><DEDENT>shift_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>return shift_matrix<EOL>

Create an affine transform matrix for image shifting.
    NOTE: In OpenCV, x is width and y is height.

    Parameters
    -----------
    wrg : float or tuple of floats
        Range to shift on width axis, -1 ~ 1.
            - float, a fixed distance.
            - tuple of 2 floats, randomly sample a value as the distance between these 2 values.
    hrg : float or tuple of floats
        Range to shift on height axis, -1 ~ 1.
            - float, a fixed distance.
            - tuple of 2 floats, randomly sample a value as the distance between these 2 values.
    w, h : int
        The width and height of the image.

    Returns
    -------
    numpy.array
        An affine transform matrix.


def obj_box_imresize(im, coords=None, size=None, interp='<STR_LIT>', mode=None, is_rescale=False):

if coords is None:<EOL><INDENT>coords = []<EOL><DEDENT>if size is None:<EOL><INDENT>size = [<NUM_LIT:100>, <NUM_LIT:100>]<EOL><DEDENT>imh, imw = im.shape[<NUM_LIT:0>:<NUM_LIT:2>]<EOL>imh = imh * <NUM_LIT:1.0>  <EOL>imw = imw * <NUM_LIT:1.0><EOL>im = imresize(im, size=size, interp=interp, mode=mode)<EOL>if is_rescale is False:<EOL><INDENT>coords_new = list()<EOL>for coord in coords:<EOL><INDENT>if len(coord) != <NUM_LIT:4>:<EOL><INDENT>raise AssertionError("<STR_LIT>")<EOL><DEDENT>x = int(coord[<NUM_LIT:0>] * (size[<NUM_LIT:1>] / imw))<EOL>y = int(coord[<NUM_LIT:1>] * (size[<NUM_LIT:0>] / imh))<EOL>w = int(coord[<NUM_LIT:2>] * (size[<NUM_LIT:1>] / imw))<EOL>h = int(coord[<NUM_LIT:3>] * (size[<NUM_LIT:0>] / imh))<EOL>coords_new.append([x, y, w, h])<EOL><DEDENT>return im, coords_new<EOL><DEDENT>else:<EOL><INDENT>return im, coords<EOL><DEDENT>

Resize an image, and compute the new bounding box coordinates.

    Parameters
    -------------
    im : numpy.array
        An image with dimension of [row, col, channel] (default).
    coords : list of list of 4 int/float or None
        Coordinates [[x, y, w, h], [x, y, w, h], ...]
    size interp and mode : args
        See ``tl.prepro.imresize``.
    is_rescale : boolean
        Set to True, if the input coordinates are rescaled to [0, 1], then return the original coordinates. Default is False.

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

    Examples
    --------
    >>> im = np.zeros([80, 100, 3])    # as an image with shape width=100, height=80
    >>> _, coords = obj_box_imresize(im, coords=[[20, 40, 30, 30], [10, 20, 20, 20]], size=[160, 200], is_rescale=False)
    >>> print(coords)
      [[40, 80, 60, 60], [20, 40, 40, 40]]
    >>> _, coords = obj_box_imresize(im, coords=[[20, 40, 30, 30]], size=[40, 100], is_rescale=False)
    >>> print(coords)
      [[20, 20, 30, 15]]
    >>> _, coords = obj_box_imresize(im, coords=[[20, 40, 30, 30]], size=[60, 150], is_rescale=False)
    >>> print(coords)
      [[30, 30, 45, 22]]
    >>> im2, coords = obj_box_imresize(im, coords=[[0.2, 0.4, 0.3, 0.3]], size=[160, 200], is_rescale=True)
    >>> print(coords, im2.shape)
      [[0.2, 0.4, 0.3, 0.3]] (160, 200, 3)


<EOL>rgb = rgb.astype('<STR_LIT:float>')<EOL>hsv = np.zeros_like(rgb)<EOL>hsv[..., <NUM_LIT:3>:] = rgb[..., <NUM_LIT:3>:]<EOL>r, g, b = rgb[..., <NUM_LIT:0>], rgb[..., <NUM_LIT:1>], rgb[..., <NUM_LIT:2>]<EOL>maxc = np.max(rgb[..., :<NUM_LIT:3>], axis=-<NUM_LIT:1>)<EOL>minc = np.min(rgb[..., :<NUM_LIT:3>], axis=-<NUM_LIT:1>)<EOL>hsv[..., <NUM_LIT:2>] = maxc<EOL>mask = maxc != minc<EOL>hsv[mask, <NUM_LIT:1>] = (maxc - minc)[mask] / maxc[mask]<EOL>rc = np.zeros_like(r)<EOL>gc = np.zeros_like(g)<EOL>bc = np.zeros_like(b)<EOL>rc[mask] = (maxc - r)[mask] / (maxc - minc)[mask]<EOL>gc[mask] = (maxc - g)[mask] / (maxc - minc)[mask]<EOL>bc[mask] = (maxc - b)[mask] / (maxc - minc)[mask]<EOL>hsv[..., <NUM_LIT:0>] = np.select([r == maxc, g == maxc], [bc - gc, <NUM_LIT> + rc - bc], default=<NUM_LIT> + gc - rc)<EOL>hsv[..., <NUM_LIT:0>] = (hsv[..., <NUM_LIT:0>] / <NUM_LIT>) % <NUM_LIT:1.0><EOL>return hsv<EOL>

Input RGB image [0~255] return HSV image [0~1].

    Parameters
    ------------
    rgb : numpy.array
        An image with values between 0 and 255.

    Returns
    -------
    numpy.array
        A processed image.


factor = np.random.uniform(<NUM_LIT:0>, <NUM_LIT:1>)<EOL>if prob >= factor:<EOL><INDENT>filp_matrix = np.array([[ -<NUM_LIT:1.> , <NUM_LIT:0.>, <NUM_LIT:0.> ],[ <NUM_LIT:0.>, <NUM_LIT:1.>, <NUM_LIT:0.> ],[ <NUM_LIT:0.>, <NUM_LIT:0.>, <NUM_LIT:1.> ]])<EOL>return filp_matrix<EOL><DEDENT>else:<EOL><INDENT>filp_matrix = np.array([[ <NUM_LIT:1.> , <NUM_LIT:0.>, <NUM_LIT:0.> ],[ <NUM_LIT:0.>, <NUM_LIT:1.>, <NUM_LIT:0.> ],[ <NUM_LIT:0.>, <NUM_LIT:0.>, <NUM_LIT:1.> ]])<EOL>return filp_matrix<EOL><DEDENT>

Create an affine transformation matrix for image horizontal flipping.
    NOTE: In OpenCV, x is width and y is height.

    Parameters
    ----------
    prob : float
        Probability to flip the image. 1.0 means always flip.

    Returns
    -------
    numpy.array
        An affine transform matrix.


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 flip_axis_multi(x, axis, is_random=False):

if is_random:<EOL><INDENT>factor = np.random.uniform(-<NUM_LIT:1>, <NUM_LIT:1>)<EOL>if factor > <NUM_LIT:0>:<EOL><INDENT>results = []<EOL>for data in x:<EOL><INDENT>data = np.asarray(data).swapaxes(axis, <NUM_LIT:0>)<EOL>data = data[::-<NUM_LIT:1>, ...]<EOL>data = data.swapaxes(<NUM_LIT:0>, axis)<EOL>results.append(data)<EOL><DEDENT>return np.asarray(results)<EOL><DEDENT>else:<EOL><INDENT>return np.asarray(x)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>results = []<EOL>for data in x:<EOL><INDENT>data = np.asarray(data).swapaxes(axis, <NUM_LIT:0>)<EOL>data = data[::-<NUM_LIT:1>, ...]<EOL>data = data.swapaxes(<NUM_LIT:0>, axis)<EOL>results.append(data)<EOL><DEDENT>return np.asarray(results)<EOL><DEDENT>

Flip the axises of multiple images together, such as flip left and right, up and down, randomly or non-randomly,

    Parameters
    -----------
    x : list of numpy.array
        List of images with dimension of [n_images, row, col, channel] (default).
    others : args
        See ``tl.prepro.flip_axis``.

    Returns
    -------
    numpy.array
        A list of processed images.


def shear_multi(<EOL>x, intensity=<NUM_LIT:0.1>, is_random=False, row_index=<NUM_LIT:0>, col_index=<NUM_LIT:1>, channel_index=<NUM_LIT:2>, fill_mode='<STR_LIT>', cval=<NUM_LIT:0.>,<EOL>order=<NUM_LIT:1><EOL>):

if is_random:<EOL><INDENT>shear = np.random.uniform(-intensity, intensity)<EOL><DEDENT>else:<EOL><INDENT>shear = intensity<EOL><DEDENT>shear_matrix = np.array([[<NUM_LIT:1>, -np.sin(shear), <NUM_LIT:0>], [<NUM_LIT:0>, np.cos(shear), <NUM_LIT:0>], [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:1>]])<EOL>h, w = x[<NUM_LIT:0>].shape[row_index], x[<NUM_LIT:0>].shape[col_index]<EOL>transform_matrix = transform_matrix_offset_center(shear_matrix, h, w)<EOL>results = []<EOL>for data in x:<EOL><INDENT>results.append(affine_transform(data, transform_matrix, channel_index, fill_mode, cval, order))<EOL><DEDENT>return np.asarray(results)<EOL>

Shear images with the same arguments, randomly or non-randomly.
    Usually be used for image segmentation which x=[X, Y], X and Y should be matched.

    Parameters
    -----------
    x : list of numpy.array
        List of images with dimension of [n_images, row, col, channel] (default).
    others : args
        See ``tl.prepro.shear``.

    Returns
    -------
    numpy.array
        A list of processed images.