[c@@sUdZddlmZmZddlZddlZddlZddlZddlZddl Z ddl Z yddl Z Wne k rdZ nXddlmZddlmZddlmZddlmZdd lmZdd lmZdd lmZd Zd ZdZd=dZddZ dddZ!ddZ"ddZ#ddZ$ddZ%de&fdYZ'de'fdYZ(de'fdYZ)de'fdYZ*de'fdYZ+d e'fd!YZ,d"e'fd#YZ-d$e'fd%YZ.d&e'fd'YZ/d(e'fd)YZ0d*e'fd+YZ1d,e'fd-YZ2d.e.fd/YZ3d0e'fd1YZ4d2e'fd3YZ5d4e'fd5YZ6d6e'fd7YZ7d8e'fd9YZ8de9e9e9dddddddddd: Z:d;ej;fd<YZ<dS(>s6Read individual image files and perform augmentations.i(tabsolute_importtprint_functionNi(t numeric_types(tndarray(t _internal(t _cvimresize(t_cvcopyMakeBorder(tio(trecordiocO@stj|||S(sRead and decode an image to an NDArray. Note: `imread` uses OpenCV (not the CV2 Python library). MXNet must have been built with USE_OPENCV=1 for `imdecode` to work. Parameters ---------- filename : str Name of the image file to be loaded. flag : {0, 1}, default 1 1 for three channel color output. 0 for grayscale output. to_rgb : bool, default True True for RGB formatted output (MXNet default). False for BGR formatted output (OpenCV default). out : NDArray, optional Output buffer. Use `None` for automatic allocation. Returns ------- NDArray An `NDArray` containing the image. Example ------- >>> mx.img.imread("flower.jpg") Set `flag` parameter to 0 to get grayscale output >>> mx.img.imread("flower.jpg", flag=0) Set `to_rgb` parameter to 0 to get output in OpenCV format (BGR) >>> mx.img.imread("flower.jpg", to_rgb=0) (Rt _cvimread(tfilenametargstkwargs((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pytimread/s&cO@st|tjsztjddkrMt|ttjf rMtdntj tj |dtj dtj }nt j |||S(suDecode an image to an NDArray. Note: `imdecode` uses OpenCV (not the CV2 Python library). MXNet must have been built with USE_OPENCV=1 for `imdecode` to work. Parameters ---------- buf : str/bytes or numpy.ndarray Binary image data as string or numpy ndarray. flag : int, optional, default=1 1 for three channel color output. 0 for grayscale output. to_rgb : int, optional, default=1 1 for RGB formatted output (MXNet default). 0 for BGR formatted output (OpenCV default). out : NDArray, optional Output buffer. Use `None` for automatic allocation. Returns ------- NDArray An `NDArray` containing the image. Example ------- >>> with open("flower.jpg", 'rb') as fp: ... str_image = fp.read() ... >>> image = mx.img.imdecode(str_image) >>> image Set `flag` parameter to 0 to get grayscale output >>> with open("flower.jpg", 'rb') as fp: ... str_image = fp.read() ... >>> image = mx.img.imdecode(str_image, flag=0) >>> image Set `to_rgb` parameter to 0 to get output in OpenCV format (BGR) >>> with open("flower.jpg", 'rb') as fp: ... str_image = fp.read() ... >>> image = mx.img.imdecode(str_image, to_rgb=0) >>> image iisgbuf must be of type bytes or numpy.ndarray,if you would like to input type str, please convert to bytestdtype(t isinstancetndtNDArraytsyst version_infotbytestnpRt ValueErrortarrayt frombuffertuint8Rt _cvimdecode(tbufR R ((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pytimdecodeXs 1,-cC@s|\}}|\}}||krBt||||}}n||krl|t|||}}nt|t|fS(sScales down crop size if it's larger than image size. If width/height of the crop is larger than the width/height of the image, sets the width/height to the width/height of the image. Parameters ---------- src_size : tuple of int Size of the image in (width, height) format. size : tuple of int Size of the crop in (width, height) format. Returns ------- tuple of int A tuple containing the scaled crop size in (width, height) format. Example -------- >>> src_size = (640,480) >>> size = (720,120) >>> new_size = mx.img.scale_down(src_size, size) >>> new_size (640,106) (tfloattint(tsrc_sizetsizetwthtswtsh((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyt scale_downs    cC@s|dkr|r{t|dks*t|\}}}}||krX||krXdS||krt||krtdSdSqdSn|dkrtjddS|d krtd|n|S( sGet the interpolation method for resize functions. The major purpose of this function is to wrap a random interp method selection and a auto-estimation method. Parameters ---------- interp : int interpolation method for all resizing operations Possible values: 0: Nearest Neighbors Interpolation. 1: Bilinear interpolation. 2: Area-based (resampling using pixel area relation). It may be a preferred method for image decimation, as it gives moire-free results. But when the image is zoomed, it is similar to the Nearest Neighbors method. (used by default). 3: Bicubic interpolation over 4x4 pixel neighborhood. 4: Lanczos interpolation over 8x8 pixel neighborhood. 9: Cubic for enlarge, area for shrink, bilinear for others 10: Random select from interpolation method metioned above. Note: When shrinking an image, it will generally look best with AREA-based interpolation, whereas, when enlarging an image, it will generally look best with Bicubic (slow) or Bilinear (faster but still looks OK). More details can be found in the documentation of OpenCV, please refer to http://docs.opencv.org/master/da/d54/group__imgproc__transform.html. sizes : tuple of int (old_height, old_width, new_height, new_width), if None provided, auto(9) will return Area(2) anyway. Returns ------- int interp method from 0 to 4 i iiiii isUnknown interp method %d(iiiii(tlentAssertionErrortrandomtrandintR(tinterptsizestohtowtnhtnw((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyt_get_interp_methods$   c C@sv|j\}}}||kr6||||}}n||||}}t|||dt|||||fS(sResizes shorter edge to size. Note: `resize_short` uses OpenCV (not the CV2 Python library). MXNet must have been built with OpenCV for `resize_short` to work. Resizes the original image by setting the shorter edge to size and setting the longer edge accordingly. Resizing function is called from OpenCV. Parameters ---------- src : NDArray The original image. size : int The length to be set for the shorter edge. interp : int, optional, default=2 Interpolation method used for resizing the image. Possible values: 0: Nearest Neighbors Interpolation. 1: Bilinear interpolation. 2: Area-based (resampling using pixel area relation). It may be a preferred method for image decimation, as it gives moire-free results. But when the image is zoomed, it is similar to the Nearest Neighbors method. (used by default). 3: Bicubic interpolation over 4x4 pixel neighborhood. 4: Lanczos interpolation over 8x8 pixel neighborhood. 9: Cubic for enlarge, area for shrink, bilinear for others 10: Random select from interpolation method metioned above. Note: When shrinking an image, it will generally look best with AREA-based interpolation, whereas, when enlarging an image, it will generally look best with Bicubic (slow) or Bilinear (faster but still looks OK). More details can be found in the documentation of OpenCV, please refer to http://docs.opencv.org/master/da/d54/group__imgproc__transform.html. Returns ------- NDArray An 'NDArray' containing the resized image. Example ------- >>> with open("flower.jpeg", 'rb') as fp: ... str_image = fp.read() ... >>> image = mx.img.imdecode(str_image) >>> image >>> size = 640 >>> new_image = mx.img.resize_short(image, size) >>> new_image R*(tshapetimresizeR0(tsrcR R*R"R!t_tnew_htnew_w((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyt resize_shorts 6 c C@stj|d||dfd||||t|jdf}|dk r||f|kr|||d|df}t|dt|||}n|S(s`Crop src at fixed location, and (optionally) resize it to size. Parameters ---------- src : NDArray Input image x0 : int Left boundary of the cropping area y0 : int Top boundary of the cropping area w : int Width of the cropping area h : int Height of the cropping area size : tuple of (w, h) Optional, resize to new size after cropping interp : int, optional, default=2 Interpolation method. See resize_short for details. Returns ------- NDArray An `NDArray` containing the cropped image. tbeginitendiiR*N(RtcropRR1tNoneR2R0( R3tx0ty0R!R"R R*toutR+((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyt fixed_crop)s B!c C@s|j\}}}t||f|\}}tjd||}tjd||} t||| ||||} | || ||ffS(sRandomly crop `src` with `size` (width, height). Upsample result if `src` is smaller than `size`. Parameters ---------- src: Source image `NDArray` size: Size of the crop formatted as (width, height). If the `size` is larger than the image, then the source image is upsampled to `size` and returned. interp: int, optional, default=2 Interpolation method. See resize_short for details. Returns ------- NDArray An `NDArray` containing the cropped image. Tuple A tuple (x, y, width, height) where (x, y) is top-left position of the crop in the original image and (width, height) are the dimensions of the cropped image. Example ------- >>> im = mx.nd.array(cv2.imread("flower.jpg")) >>> cropped_im, rect = mx.image.random_crop(im, (100, 100)) >>> print cropped_im >>> print rect (20, 21, 100, 100) i(R1R%R(R)R?( R3R R*R"R!R4R6R5R<R=R>((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyt random_cropIs c C@s|j\}}}t||f|\}}t||d}t||d} t||| ||||} | || ||ffS(sVCrops the image `src` to the given `size` by trimming on all four sides and preserving the center of the image. Upsamples if `src` is smaller than `size`. .. note:: This requires MXNet to be compiled with USE_OPENCV. Parameters ---------- src : NDArray Binary source image data. size : list or tuple of int The desired output image size. interp : int, optional, default=2 Interpolation method. See resize_short for details. Returns ------- NDArray The cropped image. Tuple (x, y, width, height) where x, y are the positions of the crop in the original image and width, height the dimensions of the crop. Example ------- >>> with open("flower.jpg", 'rb') as fp: ... str_image = fp.read() ... >>> image = mx.image.imdecode(str_image) >>> image >>> cropped_image, (x, y, width, height) = mx.image.center_crop(image, (1000, 500)) >>> cropped_image >>> x, y, width, height (1241, 910, 1000, 500) i(R1R%RR?( R3R R*R"R!R4R6R5R<R=R>((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyt center_cropps 'cC@s6|dk r||8}n|dk r2||:}n|S(s9Normalize src with mean and std. Parameters ---------- src : NDArray Input image mean : NDArray RGB mean to be subtracted std : NDArray RGB standard deviation to be divided Returns ------- NDArray An `NDArray` containing the normalized image. N(R;(R3tmeantstd((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pytcolor_normalizes     c K@s|j\}}}||} d|krJtjdt|jd}n| s]tdt|tr{|df}nxtdD] }t j |d|d| } t j |} t t t j| | } t t t j| | } t j dkr| | } } n| |kr| |krt jd|| }t jd|| }t|||| | ||}|||| | ffSqWt|||S( sRandomly crop src with size. Randomize area and aspect ratio. Parameters ---------- src : NDArray Input image size : tuple of (int, int) Size of the crop formatted as (width, height). area : float in (0, 1] or tuple of (float, float) If tuple, minimum area and maximum area to be maintained after cropping If float, minimum area to be maintained after cropping, maximum area is set to 1.0 ratio : tuple of (float, float) Aspect ratio range as (min_aspect_ratio, max_aspect_ratio) interp: int, optional, default=2 Interpolation method. See resize_short for details. Returns ------- NDArray An `NDArray` containing the cropped image. Tuple A tuple (x, y, width, height) where (x, y) is top-left position of the crop in the original image and (width, height) are the dimensions of the cropped image. tmin_areas4`min_area` is deprecated. Please use `area` instead.s4unexpected keyword arguments for `random_size_crop`.g?i iig?(R1twarningstwarntDeprecationWarningtpopR'RRtrangeR(tuniformRtroundRtsqrtR)R?RA(R3R tareatratioR*R R"R!R4tsrc_areat target_areat new_ratioR6R5R<R=R>((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pytrandom_size_crops,   t AugmentercB@s)eZdZdZdZdZRS(sImage Augmenter base classcK@s|||_xl|jjD][\}}t|tjrF|j}nt|tjr|j}||j|>> # An example of creating multiple augmenters >>> augs = mx.image.CreateAugmenter(data_shape=(3, 300, 300), rand_mirror=True, ... mean=True, brightness=0.125, contrast=0.125, rand_gray=0.05, ... saturation=0.125, pca_noise=0.05, inter_method=10) >>> # dump the details >>> for aug in augs: ... aug.dumps() iiig{Gz?g@g@g?g{GK@gV-@g|?5^?g)\(g鷯?g=yX?gg;Onrg?5^I g[B>٬gyX5;g.1?gQ^@gR]@gRY@ig(\2M@g(\L@gL@N(ii(ii(RRkR'RnRmRoRRRRRRRRRRR;RRRR1R(t data_shapetresizet rand_cropt rand_resizet rand_mirrorRBRCRtRwR|Rt pca_noiset rand_grayt inter_methodtauglistt crop_sizeRR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pytCreateAugmentersH?  -     7  7t ImageItercB@seZdZdddddedddddddddZdZd Zd Zdd Z d Z d Z dZ dZ dZdZdZRS(sImage data iterator with a large number of augmentation choices. This iterator supports reading from both .rec files and raw image files. To load input images from .rec files, use `path_imgrec` parameter and to load from raw image files, use `path_imglist` and `path_root` parameters. To use data partition (for distributed training) or shuffling, specify `path_imgidx` parameter. Parameters ---------- batch_size : int Number of examples per batch. data_shape : tuple Data shape in (channels, height, width) format. For now, only RGB image with 3 channels is supported. label_width : int, optional Number of labels per example. The default label width is 1. path_imgrec : str Path to image record file (.rec). Created with tools/im2rec.py or bin/im2rec. path_imglist : str Path to image list (.lst). Created with tools/im2rec.py or with custom script. Format: Tab separated record of index, one or more labels and relative_path_from_root. imglist: list A list of images with the label(s). Each item is a list [imagelabel: float or list of float, imgpath]. path_root : str Root folder of image files. path_imgidx : str Path to image index file. Needed for partition and shuffling when using .rec source. shuffle : bool Whether to shuffle all images at the start of each iteration or not. Can be slow for HDD. part_index : int Partition index. num_parts : int Total number of partitions. data_name : str Data name for provided symbols. label_name : str Label name for provided symbols. dtype : str Label data type. Default: float32. Other options: int32, int64, float64 last_batch_handle : str, optional How to handle the last batch. This parameter can be 'pad'(default), 'discard' or 'roll_over'. If 'pad', the last batch will be padded with data starting from the begining If 'discard', the last batch will be discarded If 'roll_over', the remaining elements will be rolled over to the next iteration kwargs : ... More arguments for creating augmenter. See mx.image.CreateAugmenter. iitdatat softmax_labelRtpadc K@stt|j|s4|s4t| ts4t|dksPt|dtjjdd}t j dt |t j d |j j }|rt j d |||rtj||d |_t|jj|_q tj|d |_d|_n d|_|rt j d ||t|}i} g}x|t|jd D]h}|jjd}tj|dd!d|}t|d}||df| |<|j|qTW| |_WdQXn t| trt j d|i}g}d}x| D]}t |}|d7}t|dkrTtj|d d|}nKt|dt rtj|dgd|}ntj|dd|}||df||<|jt |q W||_n d|_||_!|j"|| |f|fg|_#|dkr5|||ffg|_$n||ffg|_$||_%||_&||_'||_(|jdkr||_)n?|s| dkr|jdk st|j|_)n d|_)| dkr!| | kstt|j)}|| }|j)| || d|!|_)n| dkrBt*|||_+n | |_+d|_,t-|_.||_/|j)dk rt|j)nd|_0d|_1d|_2d|_3|j4dS(Ntint32Rtint64tfloat64s label not supportedtMXNET_CPU_WORKER_NTHREADSis Using %s threads for decoding...sYSet enviroment variable MXNET_CPU_WORKER_NTHREADS to a larger number to use more threads.s%s: loading recordio %s...trs%s: loading image list %s...ts iRis%s: loading image list...i(sint32sfloat32sint64sfloat64(5RgRR\RtlistR'tostenvirontgettloggingtinfotstrR_R`RtMXIndexedRecordIOtimgrectkeystimgidxt MXRecordIOR;topentitertreadlinetstriptsplitRRRRtimglistR&Rt path_roottcheck_data_shapet provide_datat provide_labelt batch_sizeRt label_widthRqtseqRRtcurRt _allow_readtlast_batch_handlet num_imaget _cache_datat _cache_labelt _cache_idxtreset(RYRRRt path_imgrect path_imglistRt path_imgidxRqt part_indext num_partstaug_listRt data_namet label_nameRRR t num_threadst class_nametfintimgkeystlinetlabeltkeytresulttindextimgtNtC((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyR\5s!                      "     '   cC@s|jdk r+|jr+tj|jn|jdksI|jdkr|jdk rh|jjnd|_|j t krt |_ qndS(s1Resets the iterator to the beginning of the data.t roll_overiN( RR;RqR(RRRRRRtFalseR(RY((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyRs cC@s{|jdk r+|jr+tj|jn|jdk rJ|jjnd|_t|_d|_ d|_ d|_ dS(s-Resets the iterator and ignore roll over dataiN( RR;RqR(RRRRRRRR(RY((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyt hard_resets    cC@sx|jtkrtn|jdk r|j|jkrL|j|j}n!|jdkrgd|_nt|jd7_|jdk r|jj |}t j |\}}|j dkr|j |fS|j |d|fSqt|j |\}}||j|fSne|jj}|dkrR|jdkrI|jjntnt j |\}}|j |fSdS(s+Helper function for reading in next sample.tdiscardiiN(RRt StopIterationRR;RRRRtread_idxRtunpackRRt read_imagetreadR(RYtidxtstheaderRRtfname((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyt next_samples0     c C@s|}|j}yx||kr|j\}}|j|}y|j|Wn,tk r} tjdt| qnX|j|}||kst d|j |||<|||<|d7}qWWn t k r|st qnX|S(s$Helper function for batchifying datasInvalid image, skipping: %ss7Batch size must be multiples of augmenter output lengthi( RRRtcheck_valid_imaget RuntimeErrorRtdebugRtaugmentation_transformR'tpostprocess_dataR( RYt batch_datat batch_labeltstarttiRRRRte((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyt _batchifys(    c C@s|j}|j\}}}|jdk r~|jdk sEtd|jdk s`td|j}|j}|j}nGtj||||f}tj|j dd}|j ||}||}|dkr|j dkrt q|j dkr5|jdkr5||_||_||_t q|j |||} |j dkret |_qd|_d|_d|_ntj|g|gd|S( sReturns the next batch of data.s_cache_label didn't have valuess_cache_idx didn't have valuesiiRRRN(RRRR;RR'RRtemptyRRRRRRRt DataBatch( RYRtcR"R!RRRRR4((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pytnexts8               cC@sDt|dks!tdn|ddks@tdndS(s'Checks if the input data shape is validis6data_shape should have length 3, with dimensions CxHxWis0This iterator expects inputs to have 3 channels.N(R&R(RYR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyR scC@s,t|djdkr(tdndS(s!Checks if the input data is validisData shape is wrongN(R&R1R(RYR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyRsc@sTfd}yt|}Wn.tk rO}tdj||nX|S(s\Decodes a string or byte string to an NDArray. See mx.img.imdecode for more details.c@sjdk r-jjjd}njjd}jdk ruj|\}}dj|}ndj|}d|S(s,Locate the image file/index if decode fails.is filename: {}s index: {}s Broken image N(RR;RRRtformat(RR4Rtmsg(RY(sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pytlocatess{}, {}(Rt ExceptionRR(RYRRRR((RYsQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyRs  cC@s:ttjj|j|d}|j}WdQX|S(sReads an input image `fname` and returns the decoded raw bytes. Example usage: ---------- >>> dataIter.read_image('Face.jpg') # returns decoded raw bytes. trbN(RRtpathtjoinRR(RYRRR((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyR*s$cC@s$x|jD]}||}q W|S(s2Transforms input data with specified augmentation.(R(RYRRj((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyR4scC@stj|ddS(s@Final postprocessing step before image is loaded into the batch.taxesiii(iii(Rt transpose(RYtdatum((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyR:sN(R`RdReR;RR\RRRRRRRRRRR(((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pyRs"5   Z   (    ((=Ret __future__RRRRR(RR]RFtnumpyRtcv2t ImportErrorR;tbaseRRRRRtndarray._internalRR2RtcopyMakeBorderRRR RR%R0R7R?R@RARDRStobjectRTRfRkRlRmRnRoRpRsRvR{RRRRRRRRRtDataIterR(((sQ/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/image.pytsf          ) 9 # 7 > ' 1  9"" p