Yc@@s'dZddlmZmZddlZddlZddlZddlZddlZ yddl Z Wne k rdZ nXddl mZddlmZddlmZddlmZdd lmZdd lmZdd lmZd Zd ZdZd;dZddZdddZddZ 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)YZ.d*e%fd+YZ/d,e+fd-YZ0d.e%fd/YZ1d0e%fd1YZ2d2e%fd3YZ3d4e%fd5YZ4d6e%fd7YZ5de6e6e6dddddddddd8 Z7d9ej8fd:YZ9dS(<s6Read individual image files and perform augmentations.i(tabsolute_importtprint_functionNi(t numeric_types(tndarray(t_ndarray_internal(t _cvimresize(t_cvcopyMakeBorder(tio(trecordiocO@stj|||S(s Read 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.imdecode("flower.jpg", flag=0) Set `to_rgb` parameter to 0 to get output in OpenCV format (BGR) >>> mx.img.imdecode(str_image, to_rgb=0) (t _internalt _cvimread(tfilenametargstkwargs((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pytimread,s&cO@sRt|tjs?tjtj|dtjdtj}ntj|||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 tdtype( t isinstancetndtNDArraytarraytnpt frombuffertuint8R t _cvimdecode(tbufR R ((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pytimdecodeUs1-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((s1build/bdist.linux-armv7l/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(tlentAssertionErrortrandomtrandintt ValueError(tinterptsizestohtowtnhtnw((s1build/bdist.linux-armv7l/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((tshapetimresizeR.(tsrcRR(RRt_tnew_htnew_w((s1build/bdist.linux-armv7l/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(RtcropRR/tNoneR0R.( R1tx0ty0RRRR(toutR)((s1build/bdist.linux-armv7l/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(R/R"R%R&R=( R1RR(RRR2R4R3R:R;R<((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyt random_cropCs 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(R/R"RR=( R1RR(RRR2R4R3R:R;R<((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyt center_cropjs '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(R9(R1tmeantstd((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pytcolor_normalizes     c C@s@|j\}}}||}xtdD]}tj|d|} tj|} tttj| | } tttj| | } tjdkr| | } } n| |kr)| |kr)tjd|| } tjd|| }t || || | ||}|| || | ffSq)Wt |||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). min_area : int Minimum area to be maintained after cropping 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. i g?g?i( R/trangeR%tuniformRtroundRtsqrtR&R=R?(R1Rtmin_areatratioR(RRR2tareat target_areat new_ratioR4R3R:R;R<((s1build/bdist.linux-armv7l/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|RR((RRR1((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyR\4s(RYR]R^RUR\(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRb%s  tRandomSizedCropAugcB@s#eZdZddZdZRS(sMake random crop with random resizing and random aspect ratio jitter augmenter. Parameters ---------- size : tuple of (int, int) Size of the crop formatted as (width, height). min_area : int Minimum area to be maintained after cropping 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. ic C@sStt|jd|d|d|d|||_||_||_||_dS(NRRGRHR((R`RcRURRGRHR((RRRRGRHR(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRUGs     cC@s&t||j|j|j|jdS(sAugmenter bodyi(RLRRGRHR((RRR1((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyR\Os(RYR]R^RUR\(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRc9s  t CenterCropAugcB@s#eZdZddZdZRS(sMake center crop augmenter. Parameters ---------- size : list or tuple of int The desired output image size. interp : int, optional, default=2 Interpolation method. See resize_short for details. icC@s5tt|jd|d|||_||_dS(NRR((R`RdRURR((RRRR(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRU^s cC@st||j|jdS(sAugmenter bodyi(R?RR((RRR1((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyR\cs(RYR]R^RUR\(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRdTs  tRandomOrderAugcB@s)eZdZdZdZdZRS(sApply list of augmenters in random order Parameters ---------- ts : list of augmenters A series of augmenters to be applied in random order cC@s tt|j||_dS(N(R`ReRUtts(RRRf((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRUpscC@s2|jjjg|jD]}|j^qgS(s-Override the default to avoid duplicate dump.(RXRYRZRfRW(RRtx((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRWtscC@s4tj|jx|jD]}||}qW|S(sAugmenter body(R%tshuffleRf(RRR1tt((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyR\xs(RYR]R^RURWR\(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRehs  tBrightnessJitterAugcB@s eZdZdZdZRS(sRandom brightness jitter augmentation. Parameters ---------- brightness : float The brightness jitter ratio range, [0, 1] cC@s&tt|jd|||_dS(Nt brightness(R`RjRURk(RRRk((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRUscC@s+dtj|j |j}||9}|S(sAugmenter bodyg?(R%RDRk(RRR1talpha((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyR\s (RYR]R^RUR\(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRjs tContrastJitterAugcB@s eZdZdZdZRS(sRandom contrast jitter augmentation. Parameters ---------- contrast : float The contrast jitter ratio range, [0, 1] cC@sGtt|jd|||_tjdddggg|_dS(NtcontrastgA`"?gbX9?gv/?(R`RmRURnRRtcoef(RRRn((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRUs cC@sddtj|j |j}||j}dd||jtj|}||9}||7}|S(sAugmenter bodyg?g@(R%RDRnRoRRtsum(RRR1Rltgray((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyR\s  "  (RYR]R^RUR\(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRms tSaturationJitterAugcB@s eZdZdZdZRS(sRandom saturation jitter augmentation. Parameters ---------- saturation : float The saturation jitter ratio range, [0, 1] cC@sGtt|jd|||_tjdddggg|_dS(Nt saturationgA`"?gbX9?gv/?(R`RrRURsRRRo(RRRs((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRUs cC@skdtj|j |j}||j}tj|dddt}|d|9}||9}||7}|S(sAugmenter bodyg?taxisitkeepdims(R%RDRsRoRRptTrue(RRR1RlRq((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyR\s   (RYR]R^RUR\(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRrs t HueJitterAugcB@s eZdZdZdZRS(s~Random hue jitter augmentation. Parameters ---------- hue : float The hue jitter ratio range, [0, 1] cC@stt|jd|||_tjdddgdddgdd d gg|_tjd d d gd ddgd ddgg|_dS(NthuegA`"?gbX9?gv/?gOn?gK7ѿg%CԿg5^I ?g#~jgCl?g?gd;O?gZd;?g rhѿgNbX9gʡEgHzG?(R`RwRURxRRttyiqtityiq(RRRx((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRUs   cC@stj|j |j}tj|tj}tj|tj}tjdddgd|| gd||gg}tjtj|j ||j j }t j|t j|}|S(sAugmenter body. Using approximate linear transfomation described in: https://beesbuzz.biz/code/hsv_color_transforms.php g?g( R%RDRxRtcostpitsinRtdotRyRztTR(RRR1RltvsutvswtbtRi((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyR\s '(RYR]R^RUR\(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRws tColorJitterAugcB@seZdZdZRS(sCApply random brightness, contrast and saturation jitter in random order. Parameters ---------- brightness : float The brightness jitter ratio range, [0, 1] contrast : float The contrast jitter ratio range, [0, 1] saturation : float The saturation jitter ratio range, [0, 1] cC@sg}|dkr(|jt|n|dkrJ|jt|n|dkrl|jt|ntt|j|dS(Ni(tappendRjRmRrR`RRU(RRRkRnRsRf((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRUs   (RYR]R^RU(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRs t LightingAugcB@s eZdZdZdZRS(sAdd PCA based noise. Parameters ---------- alphastd : float Noise level eigval : 3x1 np.array Eigen values eigvec : 3x3 np.array Eigen vectors cC@sDtt|jd|d|d|||_||_||_dS(Ntalphastdteigvalteigvec(R`RRURRR(RRRRR((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRUs%  cC@sQtjjd|jdd}tj|j||j}|tj|7}|S(sAugmenter bodyiRi(i( RR%tnormalRR~RRRR(RRR1Rltrgb((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyR\ s(RYR]R^RUR\(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRs  tColorNormalizeAugcB@s eZdZdZdZRS(sMean and std normalization. Parameters ---------- mean : NDArray RGB mean to be subtracted std : NDArray RGB standard deviation to be divided cC@sktt|jd|d||dk r:tj|nd|_|dk r^tj|nd|_dS(NR@RA(R`RRUR9RRR@RA(RRR@RA((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRUs$cC@st||j|jS(sAugmenter body(RBR@RA(RRR1((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyR\$s(RYR]R^RUR\(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRs  t RandomGrayAugcB@s eZdZdZdZRS(s}Randomly convert to gray image. Parameters ---------- p : float Probability to convert to grayscale cC@s\tt|jd|||_tjdddgdddgdddgg|_dS(NtpgzG?g ףp= ?gQ?(R`RRURRRtmat(RRR((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRU1s   cC@s1tj|jkr-tj||j}n|S(sAugmenter body(R%RRR~R(RRR1((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyR\8s(RYR]R^RUR\(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyR)s tHorizontalFlipAugcB@s eZdZdZdZRS(sxRandom horizontal flip. Parameters ---------- p : float Probability to flip image horizontally cC@s&tt|jd|||_dS(NR(R`RRUR(RRR((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRUGscC@s1tj|jkr-tj|dd}n|S(sAugmenter bodyRti(R%RRtflip(RRR1((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyR\Ks(RYR]R^RUR\(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyR?s tCastAugcB@s eZdZdZdZRS(sCast to float32cC@stt|jdddS(Nttypetfloat32(R`RRU(RR((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRUTscC@s|jtj}|S(sAugmenter body(tastypeRR(RRR1((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyR\Ws(RYR]R^RUR\(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRRs cC@sg}|dkr+|jt|| n|d|df}|r~|sQt|jt|dddddf| n5|r|jt|| n|jt|| |r|jtdn|jt|s|s| r |jt||| n| r)|jt | n| dkrt j dd d g}t j d d d gdddgdddgg}|jt | ||n| dkr|jt | n|tkrt j dddg}n:|dk rt|t jr|jddkstn|tkrFt j dddg}n:|dk rt|t jrw|jddkstn|dk s|dk r|jt||n|S(s Creates an augmenter list. Parameters ---------- data_shape : tuple of int Shape for output data resize : int Resize shorter edge if larger than 0 at the begining rand_crop : bool Whether to enable random cropping other than center crop rand_resize : bool Whether to enable random sized cropping, require rand_crop to be enabled rand_gray : float [0, 1], probability to convert to grayscale for all channels, the number of channels will not be reduced to 1 rand_mirror : bool Whether to apply horizontal flip to image with probability 0.5 mean : np.ndarray or None Mean pixel values for [r, g, b] std : np.ndarray or None Standard deviations for [r, g, b] brightness : float Brightness jittering range (percent) contrast : float Contrast jittering range (percent) saturation : float Saturation jittering range (percent) hue : float Hue jittering range (percent) pca_noise : float Pca noise level (percent) inter_method : int, default=2(Area-based) 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). Examples -------- >>> # 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(RR_R$RcRbRdRRRRwRRRRRvR9RRR/R(t data_shapetresizet rand_cropt rand_resizet rand_mirrorR@RARkRnRsRxt pca_noiset rand_grayt inter_methodtauglistt crop_sizeRR((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pytCreateAugmenter]sH?  -     .  .t ImageIterc B@seZdZdddddeddddddd ZdZdZdZd Z d Z d Z d Z d Z dZRS(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. kwargs : ... More arguments for creating augmenter. See mx.image.CreateAugmenter. iitdatat softmax_labelc K@sGtt|j|s4|s4t| ts4ttjjdd}t j dt |t j d|j j }|rt j d|||rtj||d|_t|jj|_qtj|d|_d|_n d|_|rt j d||t|}i} g}xt|jdD]{}|jjd }tjg|dd !D]}t|^qg}t|d }||d f| |<|j|q8W| |_WdQXnt| trt j d |i}g}d}x| D]}t |}|d7}t |d krEtj|d }n?t|d t!rqtj|d g}ntj|d }||d f||<|jt |qW||_n d|_||_"|j#|| |f|fg|_$|dkr|||ffg|_%n||ffg|_%||_&||_'||_(||_)|jdkrn||_*n?|s| dkr|jdk st|j|_*n d|_*| dkr| | kstt |j*}|| }|j*| || d|!|_*n| dkr't+|||_,n | |_,d |_-|j.dS(NtMXNET_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 iis%s: loading image list...i(/R`RRURtlistR$tostenvirontgettloggingtinfotstrRXRYRtMXIndexedRecordIOtimgrectkeystimgidxt MXRecordIOR9topentitertreadlinetstriptsplitRRRRRtimglistR#Rt path_roottcheck_data_shapet provide_datat provide_labelt batch_sizeRt label_widthRhtseqRRtcurtreset(RRRRRt path_imgrect path_imglistRt path_imgidxRht part_indext num_partstaug_listRt data_namet label_nameR t num_threadst class_nametfintimgkeystlinetitlabeltkeytresulttindextimgtNtC((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRUs!      /                "   cC@sH|jrtj|jn|jdk r;|jjnd|_dS(s1Resets the iterator to the beginning of the data.iN(RhR%RRR9RR(RR((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRUs  cC@s,|jdk r|jt|jkr0tn|j|j}|jd7_|jdk r|jj|}tj|\}}|j dkr|j |fS|j |d|fSq(|j |\}}||j |fSnF|jj }|dkrtntj|\}}|j |fSdS(s+Helper function for reading in next sample.iiN( RR9RR#t StopIterationRtread_idxRtunpackRRt read_imagetread(RRtidxtstheaderRRtfname((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyt next_sample]s$    c C@s\|j}|j\}}}tj||||f}tj|jdd}d}yx||kr|j\}} |j| } y|j| Wn,tk r} t j dt | q\nX|j | } ||kst d|j| ||<|||<|d7}q\WWn tk r>|s?tq?nXtj|g|g||S(sReturns the next batch of data.iisInvalid image, skipping: %ss7Batch size must be multiples of augmenter output length(RRRtemptyRRRtcheck_valid_imaget RuntimeErrorRtdebugRtaugmentation_transformR$tpostprocess_dataRRt DataBatch( RRRtcRRt batch_datat batch_labelRRRRte((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pytnextus.    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'(RRR((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRscC@s,t|djdkr(tdndS(s!Checks if the input data is validisData shape is wrongN(R#R/R(RRR((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRscC@s t|S(s\Decodes a string or byte string to an NDArray. See mx.img.imdecode for more details.(R(RRR((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRscC@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(RRRRR((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRs$cC@s$x|jD]}||}q W|S(s2Transforms input data with specified augmentation.(R(RRRtaug((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRscC@stj|ddS(s@Final postprocessing step before image is loaded into the batch.taxesiii(iii(Rt transpose(RRtdatum((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRsN(RYR]R^R9tFalseRURRRRRRRRR(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyRs- T       ((:R^t __future__RRRR%RRVtnumpyRtcv2t ImportErrorR9tbaseRRRRRR RR0RtcopyMakeBorderRRRRR"R.R5R=R>R?RBRLtobjectRMR_RaRbRcRdReRjRmRrRwRRRRRRRRtDataIterR(((s1build/bdist.linux-armv7l/egg/mxnet/image/image.pyts`        ) 6 # 7 > ' 1  /" p