def generate_data_trip(img_data, C, backend,draw_flag = False): X, Y, img_data= data_generators.get_anchor_gt_trip(img_data, C, backend) P_rpn = model_rpn.predict_on_batch(X) R = roi_helpers.rpn_to_roi(P_rpn[0], P_rpn[1], C, K.image_dim_ordering(), use_regr=True, overlap_thresh=0.7, max_boxes=300) # note: calc_iou converts from (x1,y1,x2,y2) to (x,y,w,h) format X2, Y1, Y2, Y_view = roi_helpers.calc_iou_new(R, img_data, C, class_mapping) pos_samples = np.where(Y_view[0, :, -1] != 20) if C.num_rois > 1: if len(pos_samples) < C.num_rois: selected_pos_samples = pos_samples[0].tolist() else: selected_pos_samples = np.random.choice(pos_samples[0], C.num_rois, replace=False).tolist() R,Y_view = roi_helpers.prep_flip(X2[:,selected_pos_samples,:],Y_view[:,selected_pos_samples,:],C) if draw_flag: Im = cv2.imread(img_data['filepath']) key = img_data['bboxes'][0]['class'] azimuth = img_data['bboxes'][0]['azimuth'] bbox = np.array([[img_data['bboxes'][0]['x1'],img_data['bboxes'][0]['y1'],img_data['bboxes'][0]['x2'],img_data['bboxes'][0]['y2']]]) img = img_helpers.draw_bbox(Im, bbox, 0, [azimuth], 1, class_mapping_inv, key) img_helpers.display_image(img,0) return X,R,Y_view
def calc_roi_siam(Im, R, X, title_id): bboxes = {} probs = {} azimuths = {} idx = [] bbox_threshold = 0.7 for jk in range(R.shape[0] // C.num_rois + 1): ROIs = np.expand_dims(R[C.num_rois * jk:C.num_rois * (jk + 1), :], axis=0) if ROIs.shape[1] == 0: break if jk == R.shape[0] // C.num_rois: # pad R curr_shape = ROIs.shape target_shape = (curr_shape[0], C.num_rois, curr_shape[2]) ROIs_padded = np.zeros(target_shape).astype(ROIs.dtype) ROIs_padded[:, :curr_shape[1], :] = ROIs ROIs_padded[0, curr_shape[1]:, :] = ROIs[0, 0, :] ROIs = ROIs_padded [P_cls, P_regr, P_view] = model_classifier.predict([X, ROIs]) for ii in range(P_cls.shape[1]): if np.max(P_cls[0, ii, :]) < bbox_threshold or np.argmax( P_cls[0, ii, :]) == (P_cls.shape[2] - 1): continue cls_num = np.argmax(P_cls[0, ii, :]) cls_name = class_mapping_inv[cls_num] cls_view = P_view[0, ii, 360 * cls_num:360 * (cls_num + 1)] if cls_name not in bboxes: bboxes[cls_name] = [] probs[cls_name] = [] azimuths[cls_name] = [] (x, y, w, h) = ROIs[0, ii, :] try: (tx, ty, tw, th) = P_regr[0, ii, 4 * cls_num:4 * (cls_num + 1)] tx /= C.classifier_regr_std[0] ty /= C.classifier_regr_std[1] tw /= C.classifier_regr_std[2] th /= C.classifier_regr_std[3] x, y, w, h = roi_helpers.apply_regr(x, y, w, h, tx, ty, tw, th) except: pass bboxes[cls_name].append([ C.rpn_stride * x, C.rpn_stride * y, C.rpn_stride * (x + w), C.rpn_stride * (y + h) ]) probs[cls_name].append(np.max(P_cls[0, ii, :])) azimuths[cls_name].append(np.argmax(cls_view, axis=0)) idx.append(jk * C.num_rois + ii) key = cls_name bbox = np.array(bboxes[key]) prob = np.array(probs[key]) azimuth = np.array(azimuths[key]) # bbox, prob, azimuth = roi_helpers.non_max_suppression_fast(bbox, prob, azimuth, overlap_thresh=0.3,use_az=True) if draw_flag: img = img_helpers.draw_bbox(Im, bbox, prob, azimuth, ratio, class_mapping_inv, key) img_helpers.display_image(img, title_id) return bbox, prob, azimuth, idx