Exemple #1
0
    def secure_anchor_iou_file(self):
        """
      integrated function to secure ious' supply
    """
        # check all the image files, if its .npy is not presented, gen it rightaway
        logging.info('check if all the iou files are ready...')
        for self.lineIdx in xrange(self.imgNum):
            self.lineStr = self.fs[self.lineIdx]
            if ';' not in self.lineStr:
                continue
            self.lg = self.lineStr.rsplit(';\t')
            self.imgName = self.lg.pop(0)

            ioufile = self.gen_ioufileDir(self.imgName, self.featName)
            if '.npz' in ioufile:  # already exists
                continue
            # if not, gen it...
            logging.info('.npz missed for %s, gen it...' % self.imgName)
            self.parseLine()
            if self.stride is None:
                self.stride = (self.im_info[0] / self.feat_shape[-2],
                               self.im_info[1] / self.feat_shape[-1])

            anchor = tool.genAnchor(self.im_info,self.angleD, self.HoW, self.sideLength,\
                    self.feat_shape,self.stride) # anchor : N x 5
            #      iou_matrix = it_IoU(anchor,     self.gdt[:,1:],   \
            #                self.x_num)    #  BE CAREFUL ! [:,0] IS FOR LABEL !

            iou_matrix = gpu_it_IoU(anchor,     self.gdt[:,1:],   \
                      self.x_num)    #  BE CAREFUL ! [:,0] IS FOR LABEL !

            np.savez(ioufile, anchor=anchor, iou=iou_matrix)
            logging.info('saved at %s' % ioufile)
        logging.info('ok.')
Exemple #2
0
	def next(self):  # one img per call
		self.checkAndSet()
		self.genIdx()
		self.parseLine()
		#   havnt ended yet! longway to go
		#   assign gdt for each anchor, but 1st, u need generate anchors
		
		#   an_gdtbox IS NOT gdtbox FOR NOW !
		anchor = tool.genAnchor(self.im_info,self.angleD, self.HoW, self.sideLength,\
										self.feat_shape,self.stride) # anchor : N x 5
#		logging.debug('anchor size:'+str(anchor.shape))
#		logging.debug('anchor data:'+str(anchor))

		if config.it.debug.debug:
			self.anchor=anchor

		#   IoU ?
		start=time.time() if config.debug.debug else None
		ioufile = os.path.join(self.ioutdir,self.imgName[:-4])+'_'+str(self.imgHWC[:2])+'.npy'   # encode the additional id

#		if self.hasiou_flag and not os.path.isfile(ioufile): # need to notify the owner
#			logging.info('ioufile does not exsists: '+ioufile)

		if not self.hasiou_flag or not os.path.isfile(ioufile): # calculate and save it
			iou_matrix = it_IoU(anchor,     self.gdt[:,1:],   \
								self.x_num)    #  BE CAREFUL ! [:,0] IS FOR LABEL !
			np.save(ioufile,iou_maxtrix)
		else:  # just load it
			iou_matrix = np.load(ioufile)



		if config.it.debug.debug:
			logging.debug('[IoU] time elapse:'+str(time.time()-start))
			logging.debug('[IoU] maximum:%f'%np.max(iou_matrix))
#			logging.debug('iou matrix:'+str(iou_matrix))
#			logging.debug('begin to calculate IoU...')
#			logging.debug('anchor:'+str(anchor))
#			logging.debug('gdt:'+str(self.gdt))

			#  display the img with anchors and gdt onit !
			img=self.img.asnumpy()
			img=tool.draw_angleBox(img,anchor,(0,250,0))
			img=tool.draw_angleBox(img,self.gdt[:,1:],(0,0,250))
			plt.imshow(img)
			plt.show()


		self.target_label,self.target_bbx, self.target_weight =post_iou(iou_matrix,anchor,self.gdt)




		self.img=nd.transpose(self.img,axes=(2,0,1))
		self.img=nd.expand_dims(self.img,axis=0)


		return mx.io.DataBatch( [self.img,self.gdt,self.im_info],[self.target_label,self.target_bbx, self.target_weight])
Exemple #3
0
s=raw_input('Press S to save raw featureMap\n')
if s=='S':
    v.save_rawfeat('feature-%s'%imgname)#v.save_rawfeature('feature-%s'%imgname)
#s=raw_input('press any key to exit')
"""

##################################
#   draw anchor
##################################
current_dir = os.getcwd()
os.chdir('..')
from tool import genAnchor, draw_angleBox
os.chdir(current_dir)

imgsize = 224
feat_size = 14 * 4
stride = imgsize / feat_size
stride = [imgsize, imgsize]
feat_shape = [feat_size, feat_size]
angleD = [0, 60, -60]
HoW = [2, 0.5, 1]
sideLength = [90, 60, 30]

img = cv2.imread(os.path.join(imgdir, imgname))[:, :, [2, 1, 0]]
anchor = genAnchor(None, angleD, HoW, sideLength, feat_shape, stride)

img_anchor = draw_angleBox(img, anchor, [0, 0, 255])
plt.imshow(img_anchor)
plt.show()
Exemple #4
0
  def predict(self,img_path):
    os.path.isfile(img_path),'%s does not exist!'%img_path

    self.imgname = os.path.basename(img_path)
    self.img = mx.img.imdecode(open(img_path,'rb').read()).asnumpy()

    img = mx.nd.array(self.img - self.m, cfg.predict.ctx)

    img = mx.nd.transpose(img, axes=(2,0,1))
    img = mx.nd.expand_dims( img, axis=0)
    self.im_info = np.array(self.img.shape[:2])
    _,feat_shape, _ = self.feature_symbol.infer_shape(data=img.shape)
    feat_shape = feat_shape[0]
    self.feat_shape = feat_shape
    self.stride=(self.im_info[0]/self.feat_shape[-2],self.im_info[1]/self.feat_shape[-1])
    self.anchor  = tool.genAnchor(self.im_info,self.angleD, self.HoW, self.sideLength, self.feat_shape, self.stride)
    logging.debug(img.shape)
#    assert 0
    d = mx.io.DataBatch([img],provide_data=[ ('data',img.shape),], provide_label=None)
    self.mod.forward(d)
   
    #self.raw_predict_bbox, self.raw_predict_score = [mx.nd.reshape(x,(-3,0)) for x in  self.mod.get_outputs()[:2] ]
    self.predict_bbox, self.predict_score = [mx.nd.reshape(x,(-3,0)).asnumpy() for x in  self.mod.get_outputs()[:2] ]
    
    self.predict_transfered_bbox  = tool.bbox_inv_transfer(self.anchor, self.predict_bbox)
    #  filter who obviously out of boundary...
    pick_idx =  self.predict_transfered_bbox[:,0] < self.im_info[1]
    self.predict_transfered_bbox = self.predict_transfered_bbox[pick_idx,:]
    self.predict_score           = self.predict_score[pick_idx,:]
    pick_idx =  self.predict_transfered_bbox[:,0] >0
    self.predict_transfered_bbox = self.predict_transfered_bbox[pick_idx,:]
    self.predict_score           = self.predict_score[pick_idx,:]
#    logging.debug('x:'+str(pick_idx.sum()))

    pick_idx = self.predict_transfered_bbox[:,1] < self.im_info[0]
    self.predict_transfered_bbox = self.predict_transfered_bbox[pick_idx,:]
    self.predict_score           = self.predict_score[pick_idx,:]
    pick_idx = self.predict_transfered_bbox[:,1] > 0
    self.predict_transfered_bbox = self.predict_transfered_bbox[pick_idx,:]
    self.predict_score           = self.predict_score[pick_idx,:]
#    logging.debug('y:'+str(pick_idx.sum()))

    pick_idx =self.predict_transfered_bbox[:,3] < max(self.im_info)
    self.predict_transfered_bbox = self.predict_transfered_bbox[pick_idx,:]
    self.predict_score           = self.predict_score[pick_idx,:]
    pick_idx =self.predict_transfered_bbox[:,3]> 0 
    self.predict_transfered_bbox = self.predict_transfered_bbox[pick_idx,:]
    self.predict_score           = self.predict_score[pick_idx,:]
#    logging.debug('rh:'+str(pick_idx.sum()))
    

    pick_idx =self.predict_transfered_bbox[:,4] < max(self.im_info)
    self.predict_transfered_bbox = self.predict_transfered_bbox[pick_idx,:]
    self.predict_score           = self.predict_score[pick_idx,:]
    pick_idx =self.predict_transfered_bbox[:,4] > 0
    self.predict_transfered_bbox = self.predict_transfered_bbox[pick_idx,:]
    self.predict_score           = self.predict_score[pick_idx,:]
#    logging.debug('rw:'+str(pick_idx.sum()))
    pick_idx =self.predict_transfered_bbox[:,2] < np.pi
    self.predict_transfered_bbox = self.predict_transfered_bbox[pick_idx,:]
    self.predict_score           = self.predict_score[pick_idx,:]
    pick_idx =self.predict_transfered_bbox[:,2] > -np.pi
    self.predict_transfered_bbox = self.predict_transfered_bbox[pick_idx,:]
    self.predict_score           = self.predict_score[pick_idx,:]
Exemple #5
0
                    label_names=('target_label', ),
                    context=ctx)

# need an anchor generator...
#   for raw image prediction use.
if os.path.isfile(imgPath):
    feat_symbol = symbol.get_internals()[cfg.net.rpn_conv_name + '_output']
    with open(imgPath, 'rb') as f:
        img_mx_raw = mx.img.imdecode(f.read())
        img = mx.nd.array(img_mx_raw.asnumpy() - np.array(cfg.it.mean))
    H, W, C = img.shape
    _, feat_shape, _ = feat_symbol.infer_shape(data=(1, C, H, W))
    feat_shape = feat_shape[0]
    im_info = np.array([H, W])
    stride = (im_info[0] / feat_shape[-2], im_info[1] / feat_shape[-1])
    anchor = tool.genAnchor( im_info,cfg.ANCHOR_angleD, cfg.ANCHOR_HoW, cfg.ANCHOR_sideLength,\
              feat_shape, stride) # anchor : N x 5
    img = mx.nd.transpose(img, axes=(2, 0, 1))
    img_data = mx.nd.expand_dims(img, axis=0)
    dataBatch = mx.io.DataBatch([
        img_data,
    ])
    mod.bind(data_shapes=[
        ('data', img_data.shape),
    ], for_training=False)
    mod.init_params(None, arg_params=arg_params, aux_params= aux_params, allow_missing=False,\
                  allow_extra=True)

    mod.forward(dataBatch)
    viz_score_predict(img_mx_raw.asnumpy().astype(np.uint8), anchor, None,mod.get_outputs()[1].asnumpy(),\
              mod.get_outputs()[0].asnumpy(), score_th=.1,show_num=150 )
    mx.nd.waitall()