ó —Àv]c @sIddlmZddlZddlZejjejjeƒƒZejj ejj edƒƒddl Z ddl Z ddlZddlZddlZddlZyddlZWnek rÖdZnXd„Zd„Zd„Zd„Zd„Zd „Zd „Zd „Zed krEeƒZejrJeeƒqEejj ej!ƒrkej!Z"nejjej!ƒZ"gej#e"ƒD]<Z$ejj%ejj e"e$ƒƒrejj e"e$ƒ^qZ&d Z'xMe&D]EZ$e$j(ej!ƒrße$j)dƒrße*de$de"ƒe'd7Z'ee$ƒZ+ej,dkrœedk rœge-ej,ƒD]Z.ej/dƒ^qZZ0ej/dƒZ1ge-ej,ƒD]+Z.ej2dedee0e.e1fƒ^q”Z3xe3D]Z4e4j5ƒqÌWej2dede1e$e"fƒZ6e6j5ƒx;e7e+ƒD]-\Z.Z8e0e.e9e0ƒj:e.e8fƒqWxe0D]Z;e;j:dƒqPWxe3D]Z4e4j ƒqnWe1j:dƒe6j ƒq$e*dƒyddl/Z<Wnek rÕddl<Z<nXe<j/ƒZ1ejj=e$ƒZ$ejj>e$ƒd dZ?ejj>e$ƒd dZ@e jAjBejj e"e@ƒejj e"e?ƒdƒZCd ZDejƒZEx±e7e+ƒD] \Z.Z8eee.e8e1ƒe1jFƒr®q}ne1jGƒ\ZHZIZHeCjJe8d eIƒeDdd krejƒZKe*deKeEdeDƒeKZEneDd7ZDq}WqßqßWe'sEe*dej!ƒqEndS(iÿÿÿÿ(tprint_functionNs ../pythonc csäd}|rGi}xçtj|dtƒD]Ð\}}}|jƒ|jƒxª|D]¢}tjj||ƒ} tjj|ƒdjƒ} tjj| ƒrR| |krR||krÆt |ƒ||AtN(tostwalktTruetsorttpathtjointsplitexttlowertisfiletlentrelpathtsortedtitemstprinttlistdir( troott recursivetextstitcatR tdirstfilestfnametfpathtsuffixtktv((s3/tmp/pip-install-Qvdv_2/mxnet/mxnet/tools/im2rec.pyt list_image&s, %    !(#c Cs‰t|dƒt}xjt|ƒD]\\}}d|d}x |dD]}|d|7}qDW|d|d7}|j|ƒqWWdQXdS( s$Hepler function to write image list into the file. The format is as below, integer_image_index float_label_index path_to_image Note that the blank between number and tab is only used for readability. Parameters ---------- path_out: string image_list: list tws%d iis%f s%s iN(topent enumeratetwrite(tpath_outt image_listtfoutRtitemtlinetj((s3/tmp/pip-install-Qvdv_2/mxnet/mxnet/tools/im2rec.pyt write_listKs c Cs¤t|j|j|jƒ}t|ƒ}|jtkrStjdƒtj|ƒnt |ƒ}||j d|j }x&t |j ƒD]}||||d|!}|j dkrÂd|}nd}t ||j ƒ}t ||jƒ}|j dkrt|j|d|ƒq‡|jr@t|j|d|| ƒn|j |jdkryt|j|d|||ƒnt|j|d ||||!ƒq‡Wd S( shGenerates .lst file. Parameters ---------- args: object that contains all the arguments idis_%dRgð?s.lsts _test.lsts_val.lsts _train.lstN(R!RRRtlisttshuffleRtrandomtseedRtchunkstrangetintt train_ratiot test_ratioR,tprefix( targsR'tNt chunk_sizeRtchunkt str_chunktseptsep_test((s3/tmp/pip-install-Qvdv_2/mxnet/mxnet/tools/im2rec.pyt make_list]s*     #c cst|ƒ}xøtr |jƒ}|s.Png|jƒjdƒD]}|jƒ^qD}t|ƒ}|dkrŽtd||fƒqnyFt|dƒg|dgg|dd!D]}t|ƒ^qº}Wn*t k r}td||fƒqnX|VqWWdQXdS( sÁReads the .lst file and generates corresponding iterator. Parameters ---------- path_in: string Returns ------- item iterator that contains information in .lst file s isFlst should have at least has three parts, but only has %s parts for %siiÿÿÿÿis(Parsing lst met error for %s, detail: %sN( R#RtreadlinetstriptsplitRRR3tfloatt Exception(tpath_intfinR*Rtline_lenR)te((s3/tmp/pip-install-Qvdv_2/mxnet/mxnet/tools/im2rec.pyt read_list{s   .  Fc Cswtjj|j|dƒ}t|ƒdkr]|jr]tjjd|d|ddƒ}n#tjjd|d|ddƒ}|j r*ySt |dƒ}|j ƒ}WdQXtjj ||ƒ}|j |||fƒWnGtk r%} tjƒtd|d| ƒ|j |d|fƒnXdSytj||jƒ}Wn6tjƒtd|ƒ|j |d|fƒdSX|dkr°td |ƒ|j |d|fƒdS|jr_|jd|jdkr|jd|jdd} || | |jd…dd…f}q_|jd|jdd} |dd…| | |jd…f}n|jrê|jd|jdkr­|j|jd|j|jdf} n(|jd|j|jd|jf} tj|| ƒ}nyAtjj||d |jd |jƒ}|j |||fƒWnEtk rr} tjƒtd || ƒ|j |d|fƒdSXdS( s¤Reads, preprocesses, packs the image and put it back in output queue. Parameters ---------- args: object i: int item: list q_out: queue iiiitrbNspack_img error:s%imread error trying to load file: %s s+imread read blank (None) image for file: %stqualitytimg_fmtspack_img error on file: %s(RR R RRt pack_labeltmxtrecordiotIRHeadert pass_throughR#treadtpacktputRCt tracebackt print_excRtNonetcv2timreadtcolort center_croptshapetresizetpack_imgRJtencoding( R7RR)tq_outtfullpaththeaderREtimgtsRGtmargintnewsize((s3/tmp/pip-install-Qvdv_2/mxnet/mxnet/tools/im2rec.pyt image_encode–sZ &#     ** +(' cCsLxEtrG|jƒ}|dkr%Pn|\}}t||||ƒqWdS(s¿Function that will be spawned to fetch the image from the input queue and put it back to output queue. Parameters ---------- args: object q_in: queue q_out: queue N(RtgetRVRf(R7tq_inR_tdeqRR)((s3/tmp/pip-install-Qvdv_2/mxnet/mxnet/tools/im2rec.pyt read_workerÔs    cCs~tjƒ}d}tjj|ƒ}tjj|ƒdd}tjj|ƒdd}tjjtjj||ƒtjj||ƒdƒ}i}t } xÝ| ry|j ƒ} | d k rÝ| \} } } | | f|| .lst. Otherwise im2rec will read .lst and create a database at .recs--extstnargst+tdefaults.jpegs.jpgs.pngs$list of acceptable image extensions.s--chunksttypeisnumber of chunks.s --train-ratiogð?s$Ratio of images to use for training.s --test-ratiois#Ratio of images to use for testing.s --recursives³If true recursively walk through subdirs and assign an unique label to images in each folder. Otherwise only include images in the root folder and give them label 0.s --no-shuffletdestR.t store_falsesTIf this is passed, im2rec will not randomize the image order in .lstsOptions for creating databases--pass-throughs3whether to skip transformation and save image as iss--resizesbresize the shorter edge of image to the newsize, original images will be packed by default.s --center-crops@specify whether to crop the center image to make it rectangular.s --qualityi_sFJPEG quality for encoding, 1-100; or PNG compression for encoding, 1-9s --num-threads®number of thread to use for encoding. order of images will be different from the input list if >1. the input list will be modified to match the resulting order.s--colortchoicesiÿÿÿÿsïspecify the color mode of the loaded image. 1: Loads a color image. Any transparency of image will be neglected. It is the default flag. 0: Loads image in grayscale mode. -1:Loads image as such including alpha channel.s --encodings#specify the encoding of the images.s --pack-labels?Whether to also pack multi dimensional label in the record file(targparsetArgumentParsertArgumentDefaultsHelpFormattert add_argumenttadd_argument_groupR3RBtstrt parse_argsRR tabspathR6R(tparsertcgrouptrgroupR7((s3/tmp/pip-install-Qvdv_2/mxnet/mxnet/tools/im2rec.pyRŒ sP   !'$ t__main__is.lstsCreating .rec file fromtiniittargetR7sDmultiprocessing not available, fall back to single threaded encodings.recs.idxR"ièstime:s count:s)Did not find and list file with prefix %s(Lt __future__RRtsysR Rtdirnamet__file__t curr_pathtappendR tmxnetRMR/R†RWRkRTtmultiprocessingt ImportErrorRVR!R,R>RHRfRjRyRŒt__name__R7R-tisdirR6RpRRRRRrt startswithtendswithRR't num_threadR2RtQueueRhR_tProcesst read_processtptstartt write_processR$R)RRStqtqueueRlR RsRtRNRmRutcntRqtemptyRgt_RcRoRx(((s3/tmp/pip-install-Qvdv_2/mxnet/mxnet/tools/im2rec.pyts            %    >  % <     < !  +> ! %