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])
def view(self,filter_th,iou_th,block=False, onlyneed_box=False,\ quiet_model=False): #assert 0 < filter_th < 1 #print self.raw_predict_bbox.shape, self.anchor.shape # print self.mod.get_outputs()[0].context # assert 0 self.indx = mx_nms(self.predict_bbox, None, self.predict_score,\ self.predict_transfered_bbox, iou_th,score_thresh=filter_th,ctx=cfg.predict.nms_ctx)#, min_area=0, max_area=np.inf) # predict_score.shape : 1 x num x 2 #self.indx = self.predict_score[:,1] > filter_th print('num of indexed bbox: %d'%len(self.indx)) self.filter_transfered_bbox = self.predict_transfered_bbox[self.indx,:] if onlyneed_box: score = self.predict_score score = score.asnumpy()[0] if not isinstance(score,np.ndarray) else score score = score[self.indx,1:2] return np.concatenate( [self.filter_transfered_bbox, score], axis=1) img = self.img # nms... self.view_img = tool.draw_angleBox(img, self.filter_transfered_bbox.astype(np.float),(253,0, 253)) if quiet_model: return # plt.figure() plt.imshow(self.view_img) plt.title(self.imgname) plt.show(block=block)
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()
HOME='/disk1/c20160802009T/' boxtxt_dir = HOME+'angleNet/metric/R2CNN_FPN_Tensorflow/tools/angleNet_res/896ckpt80000/boxes' img_dir = HOME+'angleNet/local/data/test/896' img_name = '34.png' boxtxt_name = 'result-boxes%s.txt'%(img_name.split('.')[0]) img_path = os.path.join(img_dir, img_name) assert os.path.isfile(img_path), img_path img = cv2.imread( img_path )[:,:,[2,1,0]] txt_path = os.path.join( boxtxt_dir, boxtxt_name ) assert os.path.join(txt_path), txt_path boxes = np.loadtxt(txt_path) img = draw_angleBox(img, boxes[:,:-1], (255,0,0)) plt.imshow(img) plt.show() """ !TEST OK """
# go for 'label', 'gdt' for i, obj in enumerate(lg): """ now obj: label x,y alpha(deg) rh rw """ label, xy, alphaD, rh, rw = obj.split('\t') label, alphaD, rh, rw = [np.float(_) for _ in (label, alphaD, rh, rw)] x, y = [np.float(_) for _ in xy.rsplit(',')] alpha = np.deg2rad(alphaD) # deg 2 rad gdt[i][:] = np.array([label, x, y, alpha, rh, rw]) return gdt d = gen_label_dict(labeltxt) imglist = os.listdir(imgdir) for imgname in imglist: s = d[imgname] if ';' not in s: print('invalid image[%s], skip...' % imgname) continue img = cv2.imread(os.path.join(imgdir, imgname)) gdts = parseLine(s) # print (gdts) # assert 0 img = draw_angleBox(img, gdts[:, 1:].astype(np.float), [255, 0, 0], line_width=2) cv2.imwrite(os.path.join(outputdir, imgname), img) print('Save Ground Truth into %s' % outputdir)
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 ! self.stride = (self.im_info[0] / self.feat_shape[-2], self.im_info[1] / self.feat_shape[-1]) self.anchor, self.iou_matrix = self.load_anchor_iou( self.imgName, self.featName) # checksum... # if config.it.debug.debug: # self.anchor=anchor # if self.hasiou_flag and not os.path.isfile(ioufile): # need to notify the owner # logging.info('ioufile does not exsists: '+ioufile) img = self.img.asnumpy() + self.mean self.raw_img = img.astype(np.uint8) if config.it.debug.debug: # logging.debug('[IoU] time elapsed:'+str(time.time()-start)) logging.debug('[IoU] maximum:%f' % np.max(self.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 ! show_anchor_indx = np.random.randint(0, self.anchor.shape[0], cfg.debug.it.anchor_num) img = tool.draw_angleBox(img, self.anchor[show_anchor_indx][:], (0, 250, 0)) img = tool.draw_angleBox(img, self.gdt[:, 1:], (0, 0, 250)) plt.imshow(img) ax = plt.gca() # ax.text(x,y,text) iou_list = self.iou_matrix[show_anchor_indx][:].max(axis=1) xy_list = self.anchor[show_anchor_indx, :2] # logging.debug('iou list shape: '+str(iou_list.shape)) # logging.debug('xy list shape: '+str(xy_list.shape)) # logging.debug('anchor shape: '+str(self.anchor.shape)) # assert 0 tool.text_fig(ax, xy_list, iou_list) plt.show() self.target_label,self.target_bbx, self.rpn_inside_weight, self.rpn_outside_weight =\ post_iou(self.iou_matrix,self.anchor,self.gdt) # print(self.target_label.shape) # assert 0 self.img = nd.transpose(self.img, axes=(2, 0, 1)) self.img = nd.expand_dims(mx.nd.array(self.img, cfg.train.ctx), axis=0) self.rpn_inside_weight = nd.expand_dims(mx.nd.array( self.rpn_inside_weight, cfg.train.ctx), axis=0) self.rpn_outside_weight = nd.expand_dims(mx.nd.array( self.rpn_outside_weight, cfg.train.ctx), axis=0) self.target_label = nd.expand_dims(mx.nd.array(self.target_label, cfg.train.ctx), axis=0) self.target_bbx = nd.expand_dims(mx.nd.array(self.target_bbx, cfg.train.ctx), axis=0) # self.provide_label = [('img', self.img.shape ), ('gdt', self.gdt.shape), ('im_info',self.im_info.shape)] # self.provide_data = [('label',self.target_label.shape), ('bbx',self.target_bbx),('weight', self.target)weight.shape)] # self.provide_data =[(self.dataNames[0], tuple(self.img.shape)),\ # (self.dataNames[1],tuple(self.gdt.shape) ),\ # (self.dataNames[2],tuple(self.im_info)),\ # (self.dataNames[3],tuple(self.rpn_inside_weight.shape)),\ # (self.dataNames[4],tuple(self.rpn_outside_weight.shape))] # self.provide_label =[(self.labelNames[0],tuple(self.target_label.shape)),\ # (self.labelNames[1],tuple(self.target_bbx.shape))] self.provide_data =[(self.dataNames[0], tuple(self.img.shape)),\ (self.dataNames[1],tuple(self.rpn_inside_weight.shape)),\ (self.dataNames[2],tuple(self.rpn_outside_weight.shape))] self.provide_label =[(self.labelNames[0],tuple(self.target_label.shape)),\ (self.labelNames[1],tuple(self.target_bbx.shape))] # return mx.io.DataBatch( [self.img,self.gdt,self.im_info, self.rpn_inside_weight, self.rpn_outside_weight],[self.target_label,self.target_bbx ],provide_data=self.provide_data, provide_label=self.provide_label) return mx.io.DataBatch( [self.img, self.rpn_inside_weight, self.rpn_outside_weight], [self.target_label, self.target_bbx], provide_data=self.provide_data, provide_label=self.provide_label)