[c@@sdZddlmZmZddlZddlZddlZddlZddlZ ddl m Z ddl m ZddlmZddl mZd d lmZmZmZmZd d lmZmZmZmZmZd d lmZmZmZd e fdYZ!de!fdYZ"de!fdYZ#de!fdYZ$de!fdYZ%de!fdYZ&dd&d'dddd Z'dddde(dddddddddd(d)ddd*d#Z*d$efd%YZ+dS(+s;Read images and perform augmentations for object detection.i(tabsolute_importtprint_functionNi(t numeric_types(tndarray(t_cvcopyMakeBorder(tioi(tRandomOrderAugtColorJitterAugt LightingAugtColorNormalizeAug(t ResizeAugtForceResizeAugtCastAugt HueJitterAugt RandomGrayAug(t fixed_cropt ImageItert Augmentert DetAugmentercB@s)eZdZdZdZdZRS(sDetection base augmentercK@s|||_xl|jjD][\}}t|tjrF|j}nt|tjr|j}||j|>P^c C@sddlm}|j s2|dks2|dkr6dS|jd||}|jd||}xt|jD]}tj|j}|dkrqpnt t |||} t t |||} t | ||krt |d|} n| |kr|} n| | kr)| } n| | krJtj | | } nt t | |} | |ksrt | | } | |kr| d7} t t | |} | | } n| |kr| d8} t t | |} | | } n|| ko|kno?d| ko!|kno?d| ko=|knsHqpntj dt d|| } tj dt d|| }|j||| || | | ||rp|j||| | | f||}|dk r|| | | |fSqpqpWdS(sPropose cropping areasi(tsqrtig5?(N((tmathR{RJRBtrangeRDR7tuniformRAtinttroundtrandintREtmaxRuRzRP(RR'RlRkR{tmin_areatmax_areat_tratioRTtmax_hRStareaRRR6t new_label((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyRNsN"             T)$ (g?gHzG?(g?g?( R#R)R*RR(RXRgRuRzRN(((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyR?s     tDetRandomPadAugcB@s>eZdZd d dd dZdZd Zd ZRS(sRandom padding augmenter. Parameters ---------- aspect_ratio_range : tuple of floats, default=(0.75, 1.33) The padded area of the image must have an aspect ratio = width / height within this range. area_range : tuple of floats, default=(1.0, 3.0) The padded area of the image must be larger than the original area max_attempts : int, default=50 Number of attempts at generating a padded region of the image of the specified constraints. After max_attempts failures, return the original image. pad_val: float or tuple of float, default=(128, 128, 128) pixel value to be filled when padding is enabled. g?gHzG?g?g@i2ic C@st|ttfs3t|ts*t|}nt|ttfst|ts]ttjdt|||f}nt|ttfst|tsttjd|||f}ntt |j d|d|d|d|||_ ||_ ||_ ||_t|_|ddksG|d |dkrZtjd |n@|d d ks~|d |dkrtjd |n t|_dS( Ns/Using fixed aspect ratio: %s in DetRandomPadAugs-Using fixed area range: %s in DetRandomPadAugRARBRDtpad_valig?is2Skip DetRandomPadAug due to invalid parameters: %ss:Skip DetRandomPadAug due to invalid aspect_ratio_range: %s(RR1R2RRERFRGRHR.RRRRARBRDRIRJRKRLRM(RRARBRDR((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyRSs4         $$  c C@s|j\}}}|j|||}|ry|\}}} } }t||| |||| ||dd|j}n||fS(sAugmenter bodyitvalues(ROt_random_pad_proposaltcopyMakeBorderR( RR&R'RlRkRtpadR6RRRSRT((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyR(qs 7cC@s|j}|dddf||d|d|dddf<|ddd f||d|d|ddd f<|S( s(Update label according to padding regionNiiiii(ii(ii(ii(ii(R\(RR'tpad_boxRlRkRf((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyRzzs ::cC@sddlm}|j s2|dks2|dkr6dS|jd||}|jd||}xt|jD]}tj|j}|dkrqpnt t |||} t t |||} t | ||krt |d|} n| |kr|} n| | kr)| } n| | krJtj | | } nt t | |} | |dksp| |dkrqpntj dt d| |} tj dt d| |} |j || | | | f||}| | | | |fSWdS(sGenerate random padding regioni(R{igB?i(((R|R{RJRBR}RDR7R~RARRRRRz(RR'RlRkR{RRRRRTRRSRRR6R((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyRs6"       $(g?gHzG?(g?g@(iii(R#R)R*RR(RzR(((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyRCs  g?g?gHzG?g?g?g333333?i2c C@sd}||||||g}g}xQt|D]C\} } } } } |jtd| d| d| d| d| q7Wt|d|S(sHelper function to create multiple random crop augmenters. Parameters ---------- min_object_covered : float or list of float, default=0.1 The cropped area of the image must contain at least this fraction of any bounding box supplied. The value of this parameter should be non-negative. In the case of 0, the cropped area does not need to overlap any of the bounding boxes supplied. min_eject_coverage : float or list of float, default=0.3 The minimum coverage of cropped sample w.r.t its original size. With this constraint, objects that have marginal area after crop will be discarded. aspect_ratio_range : tuple of floats or list of tuple of floats, default=(0.75, 1.33) The cropped area of the image must have an aspect ratio = width / height within this range. area_range : tuple of floats or list of tuple of floats, default=(0.05, 1.0) The cropped area of the image must contain a fraction of the supplied image within in this range. max_attempts : int or list of int, default=50 Number of attempts at generating a cropped/padded region of the image of the specified constraints. After max_attempts failures, return the original image. Examples -------- >>> # An example of creating multiple random crop augmenters >>> min_object_covered = [0.1, 0.3, 0.5, 0.7, 0.9] # use 5 augmenters >>> aspect_ratio_range = (0.75, 1.33) # use same range for all augmenters >>> area_range = [(0.1, 1.0), (0.2, 1.0), (0.2, 1.0), (0.3, 0.9), (0.5, 1.0)] >>> min_eject_coverage = 0.3 >>> max_attempts = 50 >>> aug = mx.image.det.CreateMultiRandCropAugmenter(min_object_covered=min_object_covered, aspect_ratio_range=aspect_ratio_range, area_range=area_range, min_eject_coverage=min_eject_coverage, max_attempts=max_attempts, skip_prob=0) >>> aug.dumps() # show some details cS@sg}d}xK|D]C}t|ts4|g}n|j|t|t|}qWxUt|D]G\}}t||krgt|dkst||||>> # An example of creating multiple augmenters >>> augs = mx.image.CreateDetAugmenter(data_shape=(3, 300, 300), rand_crop=0.5, ... rand_pad=0.5, rand_mirror=True, mean=True, brightness=0.125, contrast=0.125, ... saturation=0.125, pca_noise=0.05, inter_method=10, min_object_covered=[0.3, 0.5, 0.9], ... area_range=(0.3, 3.0)) >>> # dump the details >>> for aug in augs: ... aug.dumps() iR0ig?g?ig{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 RR9RR/R R RR RtarrayRRRMRPRRRORER (t data_shapetresizet rand_croptrand_padt rand_grayt rand_mirrortmeantstdt brightnesstcontrastt saturationt pca_noisethuet inter_methodR@RARBRCRDRtauglistt crop_augstpad_augteigvalteigvec((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pytCreateDetAugmentersNU      *"  "   .  .t ImageDetIterc B@seZdZddddeddddddd ZdZdZdZddd Z d Z d Z d Z dd dde ddddZedZRS(sImage iterator with a large number of augmentation choices for detection. Parameters ---------- aug_list : list or None Augmenter list for generating distorted images 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. 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 Name for detection labels kwargs : ... More arguments for creating augmenter. See mx.image.CreateDetAugmenter. iitdataR'cK@stt|jd|d|d|d|d|d|d|d|d | d gd | d | d | | dkrt|||_n | |_|j}| |j|d|dffg|_||_ dS(Nt batch_sizeRt path_imgrect path_imglistt path_roott path_imgidxR8t part_indext num_partsR4timglistt data_namet label_nameii( R.RRRPRRt_estimate_label_shapeRt provide_labelt label_shape(RRRRRRRR8RRR4RRRRR((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyRs       &cC@st|jdks(|jddkrGdt|}t|ntjtj|dddfdk|dddf|dddfk|dddf|dddfkd}|jdkrtd ndS( sValidate label and its shape.iiis0Label with shape (1+, 5+) required, %s received.NiiisInvalid label occurs.(RRORHt RuntimeErrorRRZRvRi(RR'tmsgt valid_label((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyt_check_valid_labels(N6cC@sd}|jyHxAtrV|j\}}|j|}t||jd}qWWntk rknX|j||jdfS(s'Helper function to estimate label shapeii(tresetRMt next_samplet _parse_labelRROt StopIteration(Rt max_countR'R((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyRs    cC@spt|tjr!|j}n|j}|jdkrXtdt|jnt |d}t |d}|j||dkrdt|j|f}t|nt j ||d|f}t j t j |dddf|dddfk|ddd f|ddd fkd}|jdkr\td n||ddfS( s"Helper function to parse object detection label. Format for raw label: n k ... [id xmin ymin xmax ymax ...] [repeat] where n is the width of header, 2 or larger k is the width of each object annotation, can be arbitrary, at least 5 isLabel shape is invalid: iis5Label shape %s inconsistent with annotation width %d.iNiiis%Encounter sample with no valid label.(RRRRtravelRiRRHRORRtreshapeRZRv(RR'trawt header_widtht obj_widthRRfRy((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyRs  kcC@s|dk rC|j||jdd|jf|fg|_n|dk r|j||jdd|jf|fg|_ndS(sReshape iterator for data_shape or label_shape. Parameters ---------- data_shape : tuple or None Reshape the data_shape to the new shape if not None label_shape : tuple or None Reshape label shape to new shape if not None iN(RPtcheck_data_shapet provide_dataRtcheck_label_shapeR(RRR((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyRs  *  cC@s|j}|j\}}}tj||||f}tj|jdd}d|(d}yGx@||kr|j\}} |j| } yH|j| g|j |}|j | |\} }|j |Wn,t k r} t jdt| qcnXx| gD]} ||ks/td|j| ||<|jd} tj|||d| +| ||jdkrd||| )n|d7}qWqcWWn tk r|stqnXtj|g|g||S(s/Override the function for returning next batch.iiisInvalid image, skipping: %ss7Batch size must be multiples of augmenter output length(RRRtzerostemptyRRtimdecodetcheck_valid_imageRtaugmentation_transformRRRFtdebugRHREtpostprocess_dataRORRRt DataBatch(RRtcRTRSt batch_datat batch_labeltiR'tsRtetdatumt num_object((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pytnexts<    cC@s3x&|jD]}|||\}}q W||fS(s<Override Transforms input data with specified augmentations.(R(RRR'R5((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyRscC@st|dks!tdn|d|jdkrbd|jd|df}t|n|d|jdddkrd|jddd|df}t|ndS(s&Checks if the new label shape is validis label_shape should have length 2is:Attempts to reduce label count from %d to %d, not allowed.is0label_shape object width inconsistent: %d vs %d.N(RR3RR(RRR((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyRs it draw_nextc ! c@ssyddl} Wn*tk r<} tjdt| dSXd} yxtrO|j\} } |j| }y#|j|g|j | } Wn,t k r} t j dt| qInX| d7} |j || \}} |j}|tkrtjdddg}n:|dk rQt|tjrH|jddksQtn|dk rj||9}n|tkrtjd d d g}n:|dk rt|tjr|jddkstn|dk r||7}n|dddddf|dddddf<|rCtjdtjd|}n|r_|ddd}n|jtj}|j\}}}xt| jdD]}t| |df|}|dkrqnt| |d f|}t| |d f|}t| |df|}|s3tjjd dn|}| j|||f||f|||dk rt| |df}||kr||}dj|}| j }d}| j!|||d dd}d}|d||df} | j"||| |||d qqqW|dk rG| j#||| j$|n|VqIWWnt%k rn| sodSnXdS(sDisplay next image with bounding boxes drawn. Parameters ---------- color : tuple Bounding box color in RGB, use None for random color thickness : int Bounding box border thickness mean : True or numpy.ndarray Compensate for the mean to have better visual effect std : True or numpy.ndarray Revert standard deviations clip : bool If true, clip to [0, 255] for better visual effect waitKey : None or int Hold the window for waitKey milliseconds if set, skip ploting if None window_name : str Plot window name if waitKey is set. id2labels : dict Mapping of labels id to labels name. Returns ------- numpy.ndarray Examples -------- >>> # use draw_next to get images with bounding boxes drawn >>> iterator = mx.image.ImageDetIter(1, (3, 600, 600), path_imgrec='train.rec') >>> for image in iterator.draw_next(waitKey=None): ... # display image >>> # or let draw_next display using cv2 module >>> for image in iterator.draw_next(waitKey=0, window_name='disp'): ... pass iNs&Unable to import cv2, skip drawing: %ssInvalid image, skipping: %sig(\2M@g(\L@gL@igQ^@gR]@gRY@iiiis{:s}g?i(ii(ii(iii(iii(iii(&tcv2t ImportErrorRKRLRHRMRRRRRRFRRRRRRPRRRORERURYtastypetuint8R}RR7trandt rectangletformattFONT_HERSHEY_SIMPLEXt getTextSizetputTexttimshowtwaitKeyR(!Rtcolort thicknessRRtclipRt window_namet id2labelsRRtcountR'RRtimageRlRkRRRmRnRoRptbctcls_idtcls_namettexttfontt font_scalet text_heightttcttpos((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyR%s%     .    .  8! "%     )   cC@st|tstd|j}|j}|d|dksMtdt|d|d}||dkr|jd||dfn||dkr|jd||dfn|r|t|d|dkrtj d||dn|S(sSynchronize label shape with the input iterator. This is useful when train/validation iterators have different label padding. Parameters ---------- it : ImageDetIter The other iterator to synchronize verbose : bool Print verbose log if true Returns ------- ImageDetIter The synchronized other iterator, the internal label shape is updated as well. Examples -------- >>> train_iter = mx.image.ImageDetIter(32, (3, 300, 300), path_imgrec='train.rec') >>> val_iter = mx.image.ImageDetIter(32, (3, 300, 300), path.imgrec='val.rec') >>> train_iter.label_shape (30, 6) >>> val_iter.label_shape (25, 6) >>> val_iter = train_iter.sync_label_shape(val_iter, verbose=False) >>> train_iter.label_shape (30, 6) >>> val_iter.label_shape (30, 6) s"Synchronize with invalid iterator.isobject width mismatch.is Resized label_shape to (%d, %d).N( RRRERRRRPtminRFRG(Rtittverbosettrain_label_shapetval_label_shapeR((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pytsync_label_shapes   #N(R#R)R*RPRIRRRRRRRRRMRR (((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyRps'    "  k(g?gHzG?(g?g?(g?gHzG?(g?g@(iii(,R*t __future__RRR RFR7RKtnumpyRtbaseRtRRtndarray._internalRRRRRRRR R R R R RRRRtobjectRR+R/R9R?RRRIRPRR(((sU/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/image/detection.pyts8     "($^?