def _prepare_blobs(im, pixel_means, target_size, max_size): """ Reference: blob.prep_im_for_blob() """ im = im.astype(np.float32, copy=False) im -= pixel_means if cfg.SOBEL: ###### add by Xiaohang ######## im /= np.array([[[57.375, 57.12, 58.395]]]) im = sobel(im) ############################### im_shape = im.shape im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) im_scale = float(target_size) / float(im_size_min) if np.round(im_scale * im_size_max) > max_size: im_scale = float(max_size) / float(im_size_max) im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) # Reuse code in blob_utils and fit FPN blob = blob_utils.im_list_to_blob([im]) blobs = {} blobs["data"] = blob blobs["im_info"] = np.array([[blob.shape[2], blob.shape[3], im_scale]], dtype=np.float32) return blobs
def _prepare_blobs( im, pixel_means, target_size, max_size, ): ''' Reference: blob.prep_im_for_blob() ''' im = im.astype(np.float32, copy=False) im -= pixel_means im_shape = im.shape im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) im_scale = float(target_size) / float(im_size_min) if np.round(im_scale * im_size_max) > max_size: im_scale = float(max_size) / float(im_size_max) im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) # Reuse code in blob_utils and fit FPN blob = blob_utils.im_list_to_blob([im]) blobs = {} blobs['data'] = blob blobs['im_info'] = np.array( [[blob.shape[2], blob.shape[3], im_scale]], dtype=np.float32 ) return blobs
def _get_image_blob(roidb): """Builds an input blob from the images in the roidb at the specified scales. """ num_images = len(roidb) # Sample random scales to use for each image in this batch scale_inds = np.random.randint( 0, high=len(cfg.TRAIN.SCALES), size=num_images ) processed_ims = [] im_scales = [] for i in range(num_images): im = cv2.imread(roidb[i]['image']) assert im is not None, \ 'Failed to read image \'{}\''.format(roidb[i]['image']) if roidb[i]['flipped']: im = im[:, ::-1, :] target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, im_scale = blob_utils.prep_im_for_blob( im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE ) im_scales.append(im_scale) processed_ims.append(im) # Create a blob to hold the input images blob = blob_utils.im_list_to_blob(processed_ims) return blob, im_scales
def _get_image_blob(roidb): """Builds an input blob from the images in the roidb at the specified scales. """ num_images = len(roidb) # Sample random scales to use for each image in this batch scale_inds = np.random.randint( 0, high=len(cfg.TRAIN.SCALES), size=num_images ) processed_ims = [] im_scales = [] for i in range(num_images): im = cv2.imread(roidb[i]['image']) logger.info(roidb[i]['image']) assert im is not None, \ 'Failed to read image \'{}\''.format(roidb[i]['image']) if roidb[i]['flipped']: im = im[:, ::-1, :] target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, im_scale = blob_utils.prep_im_for_blob( im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE ) im_scales.append(im_scale) processed_ims.append(im) # Create a blob to hold the input images blob = blob_utils.im_list_to_blob(processed_ims) return blob, im_scales
def _get_image_blob(roidb): """Builds an input blob from the images in the roidb at the specified scales. """ num_images = len(roidb) # roidb是一个list数据,每一个元素是dict,获取roidb的数据个数,即样本数 # Sample random scales to use for each image in this batch scale_inds = np.random.randint(0, high=len(cfg.TRAIN.SCALES), size=num_images) # 生成每一张对应的scale尺度的list processed_ims = [] im_scales = [] Gauss_kernel = 0 SigmaX = 0 alpha = 1 beta = 0 for i in range(num_images): im = cv2.imread(roidb[i]['image']) # 读取对应的数据图片 if cfg.TRAIN.AUGUMENT: im_tmp = cv2.imread(roidb[i]['image']) random_flag = random.randint(0, 1) # 随机进行高斯模糊 #print(random_flag) if random_flag: Gauss_kernel = 7 #np.random.randint(5, 7) SigmaX = 5.5 #np.random.randint(0, 70) /10.0 im = randomGaussian(im_tmp, Gauss_kernel, SigmaX) alpha = np.random.randint(0, 15) / 10.0 beta = np.random.randint(0, 15) im = Contrast_and_Brightness(alpha, beta, im) #cv2.imwrite('test.jpg',im) else: im = im_tmp.copy() #print(random_flag,alpha,beta) assert im is not None, \ 'Failed to read image \'{}\''.format(roidb[i]['image']) if roidb[i]['flipped']: # 如果flipped为真 则翻转数据 im = im[:, ::-1, :] # 第二个,前的-1表示对x轴的数据反向读取,即对图进行x轴的翻转 target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, im_scale = blob_utils.prep_im_for_blob( im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) # 会做一波预处理,resize为宽为600的图 im_scales.append(im_scale) processed_ims.append(im) # Create a blob to hold the input images blob = blob_utils.im_list_to_blob(processed_ims) return blob, im_scales
def _get_image_blob(roidb): """Builds an input blob from the images in the roidb at the specified scales. """ num_images = len(roidb) # Sample random scales to use for each image in this batch scale_inds = np.random.randint(0, high=len(cfg.TRAIN.SCALES), size=num_images) angle = np.random.uniform(cfg.TRAIN.ROTATION_ANGLES[0], cfg.TRAIN.ROTATION_ANGLES[1]) shear = np.random.uniform(cfg.TRAIN.SHEAR_INT[0], cfg.TRAIN.SHEAR_INT[1]) # zoom = np.random.uniform(cfg.TRAIN.ZOOM[0], cfg.TRAIN.ZOOM[1]) channel_shift = np.random.uniform(cfg.TRAIN.CHANNEL_SHIFT[0], cfg.TRAIN.CHANNEL_SHIFT[1]) processed_ims = [] im_transformation_matrices = [] im_shapes = [] zooms = [] im_scales = [] for i in range(num_images): im = cv2.imread(roidb[i]['image']) assert im is not None, \ 'Failed to read image \'{}\''.format(roidb[i]['image']) if roidb[i]['h_flipped'] and roidb[i]['v_flipped']: im = im[::-1, ::-1, :] elif roidb[i]['v_flipped']: im = im[::-1, :, :] elif roidb[i]['h_flipped']: im = im[:, ::-1, :] target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, transformation_matrix, im_shape, im_scale = blob_utils.prep_im_for_blob( im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE, angle, shear, channel_shift) im_transformation_matrices.append(transformation_matrix) im_shapes.append(im_shape) # zooms.append(zoom) processed_ims.append(im) im_scales.append(im_scale) # Create a blob to hold the input images blob = blob_utils.im_list_to_blob(processed_ims) return blob, im_transformation_matrices, im_shapes, im_scales
def _get_image_blob(roidb): """Builds an input blob from the images in the roidb at the specified scales. """ num_images = len(roidb) # Sample random scales to use for each image in this batch scale_inds = np.random.randint(0, high=len(cfg.TRAIN.SCALES), size=num_images) processed_ims = [] im_scales = [] for i in range(num_images): roi = roidb[i] im = cv2.imread(roi['image']) assert im is not None, \ 'Failed to read image \'{}\''.format(roi['image']) if roi['flipped']: im = im[:, ::-1, :] # added by Jerome h, w, _ = im.shape roi['width'] = w roi['height'] = h boxes = roi['boxes'] # will be only true for live_targets: if cfg.TRAIN.DOMAIN_ADAPTATION: if len(boxes) == 1 and (boxes[0] == np.array( [0., 0., 0., 0.], dtype=np.float32)).all(): roi['boxes'] = np.array( [[w * .2, h * .2, w * .8, h * .8]], dtype=np.float32 ) # to make sure enough (one or more) anchors are retained target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, im_scale = blob_utils.prep_im_for_blob(im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im_scales.append(im_scale) processed_ims.append(im) # Create a blob to hold the input images blob = blob_utils.im_list_to_blob(processed_ims) return blob, im_scales
def _get_image_blob(roidb): """Builds an input blob from the images in the roidb at the specified scales. """ num_images = len(roidb) # Sample random scales to use for each image in this batch scale_inds = np.random.randint(0, high=len(cfg.TRAIN.SCALES), size=num_images) processed_ims = [] im_scales = [] im_crops = [] for i in range(num_images): im = cv2.imread(roidb[i]['image']) assert im is not None, \ 'Failed to read image \'{}\''.format(roidb[i]['image']) if roidb[i]['flipped']: im = im[:, ::-1, :] # data augmentation # prefix = uuid.uuid1() # reid_roi_data.save_image(im, '{}_b'.format(prefix)) im, im_crop = reid_roi_data.random_crop(im) im, im_crop2 = reid_roi_data.horizontal_crop(im) im_crop[2] = im_crop[2] - (im_crop[2] - im_crop[0] - im_crop2[2]) im = reid_roi_data.hsv_jitter(im) im = reid_roi_data.gaussian_blur(im) im = reid_roi_data.random_erasing(im) # reid_roi_data.save_image(im, '{}_a'.format(prefix)) target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, im_scale = blob_utils.prep_im_for_blob(im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im_scales.append(im_scale) im_crops.append(im_crop) processed_ims.append(im) # Create a blob to hold the input images blob = blob_utils.im_list_to_blob(processed_ims) return blob, im_scales, im_crops
def _get_image_blob(roidb): """Builds an input blob from the images in the roidb at the specified scales. """ #print("qwryyudfggjhgkjgk{}sdfgfdsg".format(roidb)) # print("minibatch.py 118 此轮roidb的长度为:",len(roidb)) # 获得数据的数量 num_images = len(roidb) # 在这批数据中最XXXX的采样随机比例 (600,)这个的len()只是1 np.random.randint即在第一个参数和'high'值-1,的整数间生成list,list元素数量是roidb的数量 scale_inds = np.random.randint( 0, high=len(cfg.TRAIN.SCALES), size=num_images) # 因为cfg.TRAIN.SCALES为(600,), 所以生成的是一个全为0的list processed_ims = [] # 图片数据的list im_scales = [] # 尺度比例list for i in range(num_images): #print("minibatch.py 109 @@@@@@@ roidb的boxes{}".format(roidb[i]['boxes'])) try: im = cv2.imread(roidb[i]['image']) # 读取roidb的图像数据 #print("---这是此次的for循环的第{}/{}轮,图像为{}".format(i,num_images,roidb[i]['image'])) except Exception as e: print("Exception:", e) assert im is not None, \ 'Failed to read image \'{}\''.format(roidb[i]['image']) # 如果图像不存在则assert if roidb[i]['flipped']: # 如果图的flipped为翻转,则翻转 im = im[:, ::-1, :] # 对图像的宽做倒序 即x轴flipped # s_t1 = time.time() target_size = cfg.TRAIN.SCALES[scale_inds[i]] # target_size是600 im, im_scale = blob_utils.prep_im_for_blob( # im是 resize后的数据, im_scale 是resize的比例 参数中cfg.PIXEL_MEANS/cfg.TRAIN.MAX_SIZE在配置文件中有配置 im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im_scales.append(im_scale) # 加入到im_scales的list中 processed_ims.append(im) # 加入到图像数据list中 # Create a blob to hold the input images blob = blob_utils.im_list_to_blob(processed_ims) #e_t1 = time.time() #t_t1 = e_t1 - s_t1 #print("时间_最终:{}".format(t_t1)) return blob, im_scales
def _get_image_blob(roidb): """Builds an input blob from the images in the roidb at the specified scales. """ num_images = len(roidb) # Sample random scales to use for each image in this batch scale_inds = np.random.randint(0, high=len(cfg.TRAIN.SCALES), size=num_images) processed_ims = [] im_scales = [] for i in range(num_images): im = cv2.imread(roidb[i]['image']) assert im is not None, \ 'Failed to read image \'{}\''.format(roidb[i]['image']) if roidb[i]['flipped']: im = im[:, ::-1, :] if roidb[i]['cropped'] != []: width = im.shape[1] height = im.shape[0] crop_size = cfg.TRAIN.CROPPED_SIZE crop_index = roidb[i]['cropped'].index(1) xmin = int(height / crop_size * int(round(crop_index / 2))) xmax = int(xmin + (height / crop_size)) ymin = int(width / crop_size * int(round(crop_index / 2))) ymax = int(ymin + (width / crop_size)) im = im[xmin:xmax, ymin:ymax, :] target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, im_scale = blob_utils.prep_im_for_blob(im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im_scales.append(im_scale) processed_ims.append(im) # Create a blob to hold the input images blob = blob_utils.im_list_to_blob(processed_ims) return blob, im_scales
def _get_img_blob_mul(roidb): num_images = len(roidb) processed_img = [] imgs_scale = [] scale_inds = np.random.randint(0, len(cfg.TRAIN.SCALES), size=num_images) all_roidb = [] for i in range(num_images): img_path = roidb[i]['image'] img = cv2.imread(img_path) h, w = img.shape[:2] # 如果图的flipped为翻转,则翻转 if roidb[i]['flipped']: img = img[:, ::-1, :] target_size = cfg.TRAIN.SCALES[scale_inds[i]] # im是 resize后的数据, im_scale 是resize的比例 参数中cfg.PIXEL_MEANS/cfg.TRAIN.MAX_SIZE在配置文件中有配置 im, imscale = blob_utils.prep_im_for_blob(img, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(roidb[i]) img_ = img.copy() # 其实在原图做完计算之后可以不用考虑值会改变的事了 # print("copy图像时间:{}".format(t_t_)) coor, new_boxes_crop, img_crop_w, img_crop_h, index_list = crop( h, w, roidb[i], WIDTH_REGION, HEIGHT_REGION) # 深拷贝以避免改变原始图像数据 img_ = copy.deepcopy(img_[coor[0]:coor[1], coor[2]:coor[3]]) cv2.imwrite("crop_image.jpg", img_) if not new_boxes_crop == []: new_roidb = creat_new_roidb(roidb[i], img_crop_w, img_crop_h, new_boxes_crop, index_list) target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, imscale = blob_utils.prep_im_for_blob(img_, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) time_start = time.time() if MASKRCNN_SWITCH: segms = [] seg_areas = [] #print("roidb:---------------------",new_roidb ) if len(new_roidb['segms']) > 0: for m_s, roi_s in enumerate(new_roidb['segms']): roi_s = np.array(roi_s[0][:-4]).reshape(1, -1, 2) img_se = np.zeros( (roidb[i]['height'], roidb[i]['width'], 3)) img_se_ = img_se.copy()[coor[0]:coor[1], coor[2]:coor[3]] imag = cv2.drawContours(img_se, [roi_s], -1, (255, 255, 255), -1) #cv2.imwrite("mask_raw.jpg", imag) imag = imag[coor[0]:coor[1], coor[2]:coor[3]] cv2.imwrite("mask_single.jpg", imag) imag = cv2.imread("mask_single.jpg") gray = cv2.cvtColor(imag, cv2.COLOR_BGR2GRAY) # 一定要做二值化,否则出来的是散点 ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) _, contours, hierarchy = cv2.findContours( binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #img_fill = cv2.drawContours(imag, contours,-1,(0,255,0),-1) #cv2.imwrite("mask_single_fill.jpg", img_fill) if len(contours) > 0: #print("------------------contours:-----------------",contours) for kk, con_point in enumerate(contours): if con_point[0][0][0] == 0: con_point[0][0][0] = 1 elif con_point[0][0][0] == img_crop_w - 1: con_point[0][0][0] = img_crop_w - 2 else: pass if con_point[0][0][1] == 0: con_point[0][0][1] = 1 elif con_point[0][0][1] == img_crop_h - 1: con_point[0][0][1] = img_crop_h - 2 else: pass roi_s = list(np.array(contours).reshape(1, -1)) area = float(int(cv2.contourArea(contours[0]))) segms.append(list(roi_s)) seg_areas.append(area) segms_ = [] # 验证mask框----- # for rk, roi_k in enumerate(segms): # roi_k = np.array(roi_k).reshape(-1,1,2) # segms_.append(roi_k) # #print("!!!!!points!!!!!!!",segms_) # imag_ = cv2.drawContours(img_se_, segms_ ,-1,(255,255,255),-1) # cv2.imwrite("maskrcnn_crop.jpg", imag_) # --------------------------------------------------------------------------------- new_roidb['segms'] = list(segms) new_roidb['seg_areas'] = seg_areas time_end = time.time() time_used = time_end - time_start print("Maskrcnn 裁剪耗时{}".format(time_used)) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(new_roidb) else: roidb_copy = creat_new_roidb_empty(roidb[i], img_crop_w, img_crop_h) target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, imscale = blob_utils.prep_im_for_blob(img_, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(roidb_copy) blob = blob_utils.im_list_to_blob(processed_img) return blob, imgs_scale, all_roidb
def get_img_blob(roidb): num_images = len(roidb) processed_img = [] imgs_scale = [] roidb_base = {} all_roidb = [] scale_inds = np.random.randint(0, len(cfg.TRAIN.SCALES), size=num_images) for i in range(num_images): # print(i) roidb_copy = {} # print("minibatch.py 220 @@@@@@@roidb[boxes]{}".format(roidb[i]['boxes'])) img_path = roidb[i][u'image'] # print("minibatch.py 222 image:------------------",img_path) img = cv2.imread(img_path) h, w = img.shape[:2] if roidb[i]['flipped']: # 如果图的flipped为翻转,则翻转 img = img[:, ::-1, :] # img=np.array(img) target_size = cfg.TRAIN.SCALES[scale_inds[i]] # target_size是600 im, imscale = blob_utils.prep_im_for_blob( # im是 resize后的数据, im_scale 是resize的比例 参数中cfg.PIXEL_MEANS/cfg.TRAIN.MAX_SIZE在配置文件中有配置 img, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(roidb[i]) z = crop(h, w, roidb[i]) coor, new_boxes_crop, new_classes_crop, img_crop_w, img_crop_h, index_list = z if not new_boxes_crop == []: # print("minibatch 237 ",index_list) gt_class = roidb[i]['gt_classes'][index_list] # print("minibatch 239 ",gt_class) gt_overlaps = roidb[i]['gt_overlaps'][index_list] max_overlaps = roidb[i]['max_overlaps'][ index_list] #np.max(gt_overlaps,axis=1) max_classes = roidb[i]['max_classes'][ index_list] #np.argmax(gt_overlaps,axis=1) roidb_copy = roidb[i] roidb_copy['boxes'] = np.array(new_boxes_crop, dtype=np.float32) roidb_copy[u'width'] = img_crop_w roidb_copy[u'height'] = img_crop_h roidb_copy['gt_classes'] = gt_class roidb_copy['gt_overlaps'] = gt_overlaps roidb_copy['is_crowd'] = roidb[i]['is_crowd'][index_list] roidb_copy['box_to_gt_ind_map'] = roidb[i]['box_to_gt_ind_map'][ index_list] roidb_copy[u'max_classes'] = max_classes # print(roidb_copy[-1]['max_overlaps']) roidb_copy['max_overlaps'] = max_overlaps # m=roidb_copy[-1]['max_overlaps'] box_target = compute_bbox_regression_targets(roidb_copy) roidb_copy['bbox_target'] = box_target target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, imscale = blob_utils.prep_im_for_blob( img[coor[0]:coor[1], coor[2]:coor[3]], cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(roidb_copy) else: roidb_copy = roidb[i] roidb_copy[u'width'] = img_crop_w roidb_copy[u'height'] = img_crop_h target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, imscale = blob_utils.prep_im_for_blob( img[coor[0]:coor[1], coor[2]:coor[3]], cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(roidb_copy) # if not processed_img==[]: blob = blob_utils.im_list_to_blob(processed_img) # print(" minibatch 266 @@@@@@@imgs_scale{}, roidb[boxes]{}]".format(imgs_scale,all_roidb)) return blob, imgs_scale, all_roidb
def _get_image_blob_s6(roidb, roidb_noclass1): """Builds an input blob from the images in the roidb at the specified scales. """ num_images = len(roidb) # Sample random scales to use for each image in this batch scale_inds = np.random.randint(0, high=len(cfg.TRAIN.SCALES), size=num_images) processed_ims = [] im_scales = [] error_flag = [0, 0] for i in range(num_images): roidb_noclass = roidb_noclass1.copy() if roidb[i][u'image'].split('/')[-1] == u'test.jpg': #test.jpg random_bbox = dict() random_bbox['kernel_size_x'] = int(WIDTH / 5) random_bbox['kernel_size_y'] = int(HEIGHT / 5) random_bbox['tl_x'] = 0 random_bbox['tl_y'] = 0 x0 = random_bbox['tl_x'] x1 = random_bbox['tl_x'] + random_bbox['kernel_size_x'] y0 = random_bbox['tl_y'] y1 = random_bbox['tl_y'] + random_bbox['kernel_size_y'] im = cv2.imread(roidb[i]['image'])[y0:y1, x0:x1] im = cv2.resize(im, (WIDTH, HEIGHT)) # cv2.imwrite('/home/icubic/aa.png',im) error_flag[i] = 0 roidb[i] = roidb_noclass.copy() roidb[i][u'height'] = HEIGHT roidb[i][u'width'] = WIDTH target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, im_scale = blob_utils.prep_im_for_blob(im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im_scales.append(im_scale) processed_ims.append(im) else: if 1: if len(roidb[i][u'boxes']) == 0: #print('no bbox') random_bbox = dict() random_flag = random.randint(0, 1) real_yuanlai_width = roidb[i][u'width'] * 1 real_yuanlai_height = roidb[i][u'height'] * 1 width_ratio = float(real_yuanlai_width) / WIDTH_FIX #1024 height_after_ratio = int( float(real_yuanlai_height) / width_ratio) width_after_ratio = WIDTH_FIX #1024 if 1: if random_flag == 0: #print(random_flag) random_bbox['kernel_size_x'] = int( float(width_after_ratio) / 5.0) #int(WIDTH / 5.0) random_bbox['kernel_size_y'] = int( float(height_after_ratio) / 5.0) #int(HEIGHT / 5.0) random_X = width_after_ratio - random_bbox[ 'kernel_size_x'] random_Y = height_after_ratio - random_bbox[ 'kernel_size_y'] try: random_bbox['tl_x'] = random.randint( 0, random_X) random_bbox['tl_y'] = random.randint( 0, random_Y) except: print('aa') x0 = random_bbox['tl_x'] x1 = random_bbox['tl_x'] + random_bbox[ 'kernel_size_x'] y0 = random_bbox['tl_y'] y1 = random_bbox['tl_y'] + random_bbox[ 'kernel_size_y'] im = cv2.imread(roidb[i][u'image']) im = cv2.resize( im, (width_after_ratio, height_after_ratio))[y0:y1, x0:x1] im = cv2.resize(im, (WIDTH, HEIGHT)) roidb[i] = roidb_noclass.copy() roidb[i][u'height'] = HEIGHT roidb[i][u'width'] = WIDTH else: #print(random_flag) random_bbox['kernel_size_x'] = int( float(width_after_ratio) / 1.0) random_bbox['kernel_size_y'] = int( float(height_after_ratio) / 1.0) random_X = width_after_ratio - random_bbox[ 'kernel_size_x'] random_Y = height_after_ratio - random_bbox[ 'kernel_size_y'] random_bbox['tl_x'] = random.randint(0, random_X) random_bbox['tl_y'] = random.randint(0, random_Y) x0 = random_bbox['tl_x'] x1 = random_bbox['tl_x'] + random_bbox[ 'kernel_size_x'] y0 = random_bbox['tl_y'] y1 = random_bbox['tl_y'] + random_bbox[ 'kernel_size_y'] im = cv2.imread(roidb[i][u'image']) im = cv2.resize( im, (width_after_ratio, height_after_ratio))[y0:y1, x0:x1] im = cv2.resize(im, (WIDTH, HEIGHT)) roidb[i] = roidb_noclass.copy() roidb[i][u'height'] = HEIGHT roidb[i][u'width'] = WIDTH else: im = cv2.imread(roidb[i][u'image']) im = cv2.resize(im, (WIDTH, HEIGHT)) roidb[i] = roidb_noclass.copy() roidb[i][u'height'] = HEIGHT roidb[i][u'width'] = WIDTH # cv2.imwrite('/home/icubic/daily_work/circruit_model/tmp_images/aa.png',im) assert im is not None, \ 'Failed to read image \'{}\''.format(roidb[i]['image']) if roidb[i][ 'flipped']: #for image flip background training im = im[:, ::-1, :] target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, im_scale = blob_utils.prep_im_for_blob( im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im_scales.append(im_scale) processed_ims.append(im) continue #if len(roidb[i][u'boxes']==1): # print('roidb=1') #if len(roidb[i][u'boxes']>1): # print('roidb>1') real_yuanlai_width = roidb[i][u'width'] * 1 real_yuanlai_height = roidb[i][u'height'] * 1 width_ratio = float(real_yuanlai_width) / WIDTH_FIX height_after_ratio = int( float(real_yuanlai_height) / width_ratio) width_after_ratio = WIDTH_FIX real_class = [] #roidb[i]['gt_classes'][0] num_real_class = len(roidb[i]['gt_classes']) random_bbox = dict() random_bbox['kernel_size_x'] = int( float(width_after_ratio) / 5.0) #int(WIDTH_FIX / 5) random_bbox['kernel_size_y'] = int( float(height_after_ratio) / 5.0) #int(WIDTH_FIX / 5) if 1: w_tongji = 0 h_tongji = 0 for i_tongji, sub_boxes_tongji in enumerate( roidb[i][u'boxes']): crop_x0_tongji = int(sub_boxes_tongji[0] / real_yuanlai_width * width_after_ratio) crop_y0_tongji = int(sub_boxes_tongji[1] / real_yuanlai_height * height_after_ratio) crop_x1_tongji = int(sub_boxes_tongji[2] / real_yuanlai_width * width_after_ratio) crop_y1_tongji = int(sub_boxes_tongji[3] / real_yuanlai_height * height_after_ratio) w_tongji = crop_x1_tongji - crop_x0_tongji h_tongji = crop_y1_tongji - crop_y0_tongji if w_tongji > int(WIDTH_FIX / 5.0) or h_tongji > int( WIDTH_FIX / 5.0): random_bbox['kernel_size_x'] = int( float(width_after_ratio) / 1.0) random_bbox['kernel_size_y'] = int( float(height_after_ratio) / 1.0) break if random_bbox['kernel_size_x'] == int(WIDTH_FIX / 5.0): random_X = width_after_ratio - random_bbox['kernel_size_x'] random_Y = height_after_ratio - random_bbox['kernel_size_y'] random_bbox['tl_x'] = random.randint(0, random_X) random_bbox['tl_y'] = random.randint(0, random_Y) x0 = random_bbox['tl_x'] x1 = random_bbox['tl_x'] + random_bbox['kernel_size_x'] y0 = random_bbox['tl_y'] y1 = random_bbox['tl_y'] + random_bbox['kernel_size_y'] try: im = cv2.imread(roidb[i][u'image']) except: im = cv2.imread(roidb[i][u'image']) im = cv2.resize( im, (width_after_ratio, height_after_ratio))[y0:y1, x0:x1] im = cv2.resize(im, (WIDTH, HEIGHT)) sum_inside_overlaps = 0 boxes_inside_overlaps = [] for i_roidb, sub_boxes in enumerate(roidb[i][u'boxes']): crop_x0 = int(sub_boxes[0] / real_yuanlai_width * width_after_ratio) crop_y0 = int(sub_boxes[1] / real_yuanlai_height * height_after_ratio) crop_x1 = int(sub_boxes[2] / real_yuanlai_width * width_after_ratio) crop_y1 = int(sub_boxes[3] / real_yuanlai_height * height_after_ratio) #real_x0 = float(crop_x0 - x0)*1024/224 # float(crop_x0) / 1024 * 224 #real_y0 = float(crop_y0 - y0)*1024/224 # float(crop_y0) / 1024 * 224 #real_x1 = float(crop_x1 - x0)*1024/224 # float(crop_x1) / 1024 * 224 #real_y1 = float(crop_y1 - y0)*1024/224 overlaps_rate = solve_coincide( (x0, y0, x1, y1), (crop_x0, crop_y0, crop_x1, crop_y1)) if overlaps_rate > 0.9: sum_inside_overlaps = sum_inside_overlaps + 1 #real_x0 = crop_x0 - x0 # float(crop_x0) / 1024 * 224 #real_y0 = crop_y0 - y0 # float(crop_y0) / 1024 * 224 #real_x1 = crop_x1 - x0 # float(crop_x1) / 1024 * 224 #real_y1 = crop_y1 - y0 real_x0 = float(crop_x0 - x0) * WIDTH / ( random_bbox['kernel_size_x'] ) # float(crop_x0) / 1024 * 224 real_y0 = float(crop_y0 - y0) * HEIGHT / ( random_bbox['kernel_size_y'] ) # float(crop_y0) / 1024 * 224 real_x1 = float(crop_x1 - x0) * WIDTH / ( random_bbox['kernel_size_x'] ) # float(crop_x1) / 1024 * 224 real_y1 = float(crop_y1 - y0) * HEIGHT / ( random_bbox['kernel_size_y']) if real_x0 < 0: real_x0 = 0 if real_x0 > WIDTH: real_x0 = WIDTH if real_x1 < 0: real_x1 = 0 if real_x1 > WIDTH: real_x1 = WIDTH if real_y0 < 0: real_y0 = 0 if real_y0 > HEIGHT: real_y0 = HEIGHT if real_y1 < 0: real_y1 = 0 if real_y1 > HEIGHT: real_y1 = HEIGHT #cv2.rectangle(im, (int(real_x0), int(real_y0)), (int(real_x1), int(real_y1)), (0, 255, 255), 3) #cv2.imwrite('/home/icubic/daily_work/code/Detectron/detectron/datasets/data/shanghai/aa.png',im) boxes_inside_overlaps.append( [real_x0, real_y0, real_x1, real_y1]) real_class.append(roidb[i]['gt_classes'][i_roidb]) #cv2.rectangle(im, (int(real_x0), int(real_y0)), #(int(real_x1), int(real_y1)), (255, 0, 255)) #cv2.imwrite('/home/icubic/daily_work/code/circruit/new/result/uu.png', im) #a = roidb[i]['gt_overlaps'].toarray() if sum_inside_overlaps > 0: num_valid_objs = sum_inside_overlaps * 1 boxes = np.zeros((num_valid_objs, 4), dtype=np.float32) gt_classes = np.zeros((num_valid_objs), dtype=np.int32) gt_overlaps = np.zeros((num_valid_objs, REAL_CLASS), dtype=np.float32) box_to_gt_ind_map = np.zeros((num_valid_objs), dtype=np.int32) is_crowd = np.zeros((num_valid_objs), dtype=np.bool) for ix in range(num_valid_objs): gt_classes[ix] = real_class[ix] #real_class*1 try: gt_overlaps[ix, real_class] = 1.0 except: print('error') is_crowd[ix] = False box_to_gt_ind_map[ix] = ix for i_index in range(4): boxes[ix, i_index] = boxes_inside_overlaps[ix][ i_index] #for ix in range(num_valid_objs): #box_to_gt_ind_map[ix] = ix #cls = real_class*1 roidb_noclass['boxes'] = np.append( roidb_noclass['boxes'], boxes, axis=0) roidb_noclass['gt_classes'] = np.append( roidb_noclass['gt_classes'], gt_classes) #mm = np.append( # roidb_noclass['gt_overlaps'].toarray(), gt_overlaps,axis=0) roidb_noclass['gt_overlaps'] = np.append( roidb_noclass['gt_overlaps'].toarray(), gt_overlaps) roidb_noclass['gt_overlaps'] = scipy.sparse.csr_matrix( roidb_noclass['gt_overlaps']) #mm = np.append(mm, gt_overlaps, axis=0) #roidb_noclass['gt_overlaps'] = scipy.sparse.csr_matrix(mm) roidb_noclass['is_crowd'] = np.append( roidb_noclass['is_crowd'], is_crowd) roidb_noclass['box_to_gt_ind_map'] = np.append( roidb_noclass['box_to_gt_ind_map'], box_to_gt_ind_map) gt_overlaps = roidb_noclass['gt_overlaps'].toarray() # max overlap with gt over classes (columns) max_overlaps = gt_overlaps.max(axis=1) # gt class that had the max overlap max_classes = gt_overlaps.argmax(axis=1) roidb_noclass['max_classes'] = max_classes roidb_noclass['max_overlaps'] = max_overlaps # sanity checks # if max overlap is 0, the class must be background (class 0) zero_inds = np.where(max_overlaps == 0)[0] assert all(max_classes[zero_inds] == 0) # if max overlap > 0, the class must be a fg class (not class 0) nonzero_inds = np.where(max_overlaps > 0)[0] assert all(max_classes[nonzero_inds] != 0) roidb_noclass[ 'bbox_targets'] = compute_bbox_regression_targets( roidb_noclass) roidb[i] = roidb_noclass.copy() roidb[i][u'height'] = HEIGHT roidb[i][u'width'] = WIDTH else: roidb[i] = roidb_noclass.copy() roidb[i][u'height'] = HEIGHT roidb[i][u'width'] = WIDTH #print('aa') target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, im_scale = blob_utils.prep_im_for_blob( im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im_scales.append(im_scale) processed_ims.append(im) else: im = cv2.imread(roidb[i]['image']) assert im is not None, \ 'Failed to read image \'{}\''.format(roidb[i]['image']) if roidb[i]['flipped']: im = im[:, ::-1, :] target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, im_scale = blob_utils.prep_im_for_blob( im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im_scales.append(im_scale) processed_ims.append(im) # Create a blob to hold the input images blob = blob_utils.im_list_to_blob(processed_ims) return blob, im_scales, error_flag
def _get_image_blob(roidb): """Builds an input blob from the images in the roidb at the specified scales. """ #print("qwryyudfggjhgkjgk{}sdfgfdsg".format(roidb)) # print("minibatch.py 118 此轮roidb的长度为:",len(roidb)) # 获得数据的数量 num_images = len(roidb) # 在这批数据中最XXXX的采样随机比例 (600,)这个的len()只是1 np.random.randint即在第一个参数和'high'值-1,的整数间生成list,list元素数量是roidb的数量 scale_inds = np.random.randint( 0, high=len(cfg.TRAIN.SCALES), size=num_images) # 因为cfg.TRAIN.SCALES为(600,), 所以生成的是一个全为0的list processed_ims = [] # 图片数据的list im_scales = [] # 尺度比例list for i in range(num_images): try: im = cv2.imread(roidb[i]['image']) # 读取roidb的图像数据 except Exception as e: print("Exception:", e) assert im is not None, \ 'Failed to read image \'{}\''.format(roidb[i]['image']) # 如果图像不存在则assert if roidb[i]['flipped']: # 如果图的flipped为翻转,则翻转 im = im[:, ::-1, :] # 对图像的宽做倒序 即x轴flipped img_ = im #-------------在无多尺度版本中增加色彩模块---------------------------------- if MULTI_COLOR: color_random = random.uniform(0, 1) #print("color_random--------------",color_random) if color_random <= GAUSSIAN: #print("高斯") img_ = randomGaussian(img_, GAUSSIAN_KERNEL, GAUSSIAN_SIGMAX) elif color_random > GAUSSIAN and color_random <= CAB: #print("融合暗") img_ = Contrast_and_Brightness(ALPHA, GAMMA, img_) elif color_random > CAB and color_random <= GAMMA: #print("GAMMA") img_ = gamma_trans(img_, GAMMA_THRESHOLD) elif color_random > GAMMA and color_random <= HSV: #print("HSV") img_ = img2darker(img_, HSV_THRESHOLD) elif color_random > HSV and color_random <= BRIGHT: #print("BRIGHT") img_ = bright_trans(img_, BRIGHT_THRESHOLD) else: pass #----------------------------------------------------------------------- # s_t1 = time.time() target_size = cfg.TRAIN.SCALES[scale_inds[i]] # target_size是600 im, im_scale = blob_utils.prep_im_for_blob( # im是 resize后的数据, im_scale 是resize的比例 参数中cfg.PIXEL_MEANS/cfg.TRAIN.MAX_SIZE在配置文件中有配置 im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im_scales.append(im_scale) # 加入到im_scales的list中 processed_ims.append(im) # 加入到图像数据list中 # Create a blob to hold the input images blob = blob_utils.im_list_to_blob(processed_ims) #e_t1 = time.time() #t_t1 = e_t1 - s_t1 #print("时间_最终:{}".format(t_t1)) return blob, im_scales
def _get_img_blob_mul(roidb): num_images = len(roidb) processed_img = [] imgs_scale = [] scale_inds = np.random.randint(0, len(cfg.TRAIN.SCALES), size=num_images) all_roidb = [] for i in range(num_images): # print(i) #print("minibatch.py 187 @@@@@@@ roidb[boxes]{}".format(roidb[i]['boxes'])) img_path = roidb[i]['image'] #print("minibatch.py 189 image:------",img_path) img = cv2.imread(img_path) h, w = img.shape[:2] # 如果图的flipped为翻转,则翻转 if roidb[i]['flipped']: img = img[:, ::-1, :] # target_size是600,或者yaml中设置的尺度 target_size = cfg.TRAIN.SCALES[scale_inds[i]] # im是 resize后的数据, im_scale 是resize的比例 参数中cfg.PIXEL_MEANS/cfg.TRAIN.MAX_SIZE在配置文件中有配置 im, imscale = blob_utils.prep_im_for_blob(img, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(roidb[i]) img_ = img.copy() # 其实在原图做完计算之后可以不用考虑值会改变的事了 # s_t = time.time() # ---------------------尽量不改变原roidb的值 roidb_n = roidb[i] # if ROTATION: # 是否旋转 rot_random = random.uniform(0, 1) if rot_random <= ROT: roidb_n = {} roidb_n['boxes'] = copy.deepcopy(roidb[i]['boxes']) roidb_n['width'] = roidb[i]['width'] roidb_n['height'] = roidb[i]['height'] roidb_n['has_visible_keypoints'] = roidb[i][ 'has_visible_keypoints'] roidb_n['flipped'] = roidb[i]['flipped'] roidb_n['seg_areas'] = copy.deepcopy(roidb[i]['seg_areas']) roidb_n['dataset'] = roidb[i]['dataset'] roidb_n['segms'] = copy.deepcopy(roidb[i]['segms']) roidb_n['id'] = roidb[i]['id'] roidb_n['image'] = roidb[i]['image'] roidb_n['gt_classes'] = roidb[i]['gt_classes'] roidb_n['gt_overlaps'] = roidb[i]['gt_overlaps'] roidb_n['is_crowd'] = roidb[i]['is_crowd'] roidb_n['box_to_gt_ind_map'] = roidb[i]['box_to_gt_ind_map'] roidb_n['max_classes'] = roidb[i]['max_classes'] roidb_n['max_overlaps'] = roidb[i]['max_overlaps'] roidb_n['bbox_targets'] = roidb[i]['bbox_targets'] if ROT_RANDOM: angle = random.randint(-ROT_RANDOM_ANGLE, ROT_RANDOM_ANGLE) else: angle = random.randint(1, 4) angle = angle * 90.0 img_, h_, w_, l, n_w, n_h = rotate_image(img_, angle, True) # 裁剪完后的图 angle = (angle / 180.0 * math.pi) # 坐标调整,先计算pad后坐标,再计算旋转后,在计算裁剪后 for m, roi_boxes in enumerate(roidb_n['boxes']): roi_boxes[0] = roi_boxes[0] + int((l - w_) / 2.0) roi_boxes[1] = roi_boxes[1] + int((l - h_) / 2.0) roi_boxes[2] = roi_boxes[2] + int((l - w_) / 2.0) roi_boxes[3] = roi_boxes[3] + int((l - h_) / 2.0) x0, y0 = rot_compute(l / 2, l / 2, roi_boxes[0], roi_boxes[1], angle) x1, y1 = rot_compute(l / 2, l / 2, roi_boxes[2], roi_boxes[1], angle) x2, y2 = rot_compute(l / 2, l / 2, roi_boxes[0], roi_boxes[3], angle) x3, y3 = rot_compute(l / 2, l / 2, roi_boxes[2], roi_boxes[3], angle) min_x, min_y, max_x, max_y = box_coordinate( x0, y0, x1, y1, x2, y2, x3, y3) min_x = min_x - int((l - n_w) / 2.0) min_y = min_y - int((l - n_h) / 2.0) max_x = max_x - int((l - n_w) / 2.0) max_y = max_y - int((l - n_h) / 2.0) roidb_n['boxes'][m][0] = int(min_x) roidb_n['boxes'][m][1] = int(min_y) roidb_n['boxes'][m][2] = int(max_x) roidb_n['boxes'][m][3] = int(max_y) # Maskrcnn的点的扩增模块---------------------------------------------------------------- if MASKRCNN_SWITCH: for m_segme, roi_segms in enumerate(roidb_n['segms']): for m_segme_index in range(len(roi_segms) / 2): roi_segms[m_segme][ m_segme_index] = roi_segms_point + int( (l - w_) / 2.0) roi_segms[m_segme][m_segme_index + 1] = roi_segms_point + int( (l - h_) / 2.0) x_segme, y_segme = rot_compute( l / 2, l / 2, roi_segms[m_segme][m_segme_index], roi_segms[m_segme][m_segme_index + 1], angle) roidb_n['segms'][m_segme][m_segme_index] = int( x_segme) roidb_n['segms'][m_segme][m_segme_index + 1] = int(y_segme) # ----------------------------------------------------------------------------------- # roidb[i] = roidb_n else: pass # e_t_ = time.time() # t_t_ = e_t_ - s_t # print("旋转时间_:{}".format(t_t_)) coor, new_boxes_crop, img_crop_w, img_crop_h, index_list = crop( h, w, roidb_n, WIDTH_REGION, HEIGHT_REGION) # 裁剪后实例分割的相对应的segms -------------Maskrcnn的点的扩增模块----------------------------------- if MASKRCNN_SWITCH: roidb_n['segms'] = roidb_n['segms'][index_list] segms = [] seg_areas = [] for m_s, roi_s in enumerate(roidb_n['segms']): roi_s = np.array(roi_s).reshape(1, -1, 2) img_se = np.zeros((roidb_n['height'], roidb_n['width'], 3)) imag = cv2.drawContours(img_se, roi_s, -1, (255, 255, 255), -1) imag = imag[coor[0]:coor[1], coor[2]:coor[3]] imag = cv2.cvtColor(imag, cv2.COLOR_BGR2GRAY) _, contours, hierarchy = cv2.findContours( imag, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for kk, con_point in enumerate(contours): if con_point[0][0] == 0: con_point[0][0] = 1 elif con_point[0][0] == img_crop_w - 1: con_point[0][0] = con_point[0][0] - 1 else: pass if con_point[0][1] == 0: con_point[0][1] = 1 elif con_point[0][1] == img_crop_w - 1: con_point[0][1] = con_point[0][0] - 1 else: pass roi_s = contours.reshape(1, -1) eara = float(int(cv2.contourArea(contours[0]))) segms.append(roi_s) seg_areas.append(eara) roidb_n['segms'] = segms roidb_n['seg_areas'] = np.array(eara, dtype=np.float) # ------------------------------------------------------------------------------------------------------- # 深拷贝以避免改变原始图像数据 img_ = copy.deepcopy(img_[coor[0]:coor[1], coor[2]:coor[3]]) # e_t = time.time() # t_t = e_t - s_t # print("时间:{}".format(t_t)) # 随机决定要不要做色度上的数据扩增 if MULTI_COLOR: color_random = random.uniform(0, 1) #print("ccccccc",color_random) if color_random <= GAUSSIAN: #print("高斯") img_ = randomGaussian(img_, GAUSSIAN_KERNEL, GAUSSIAN_SIGMAX) elif color_random <= CAB: pass else: #print("融合暗") img_ = Contrast_and_Brightness(ALPHA, GAMMA, img_) if not new_boxes_crop == []: #----如果裁剪完后有缺陷 #print("执行了minibatch.py 230") new_roidb = creat_new_roidb(roidb_n, img_crop_w, img_crop_h, new_boxes_crop, index_list) # e_t2 = time.time() # t_t2= e_t2 - s_t #print("时间_创建新roidb:{}".format(t_t2)) target_size = cfg.TRAIN.SCALES[scale_inds[i]] #im, imscale = blob_utils.prep_im_for_blob(img[coor[0]:coor[1],coor[2]:coor[3]], cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im, imscale = blob_utils.prep_im_for_blob(img_, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) # e_t3 = time.time() # t_t3= e_t3 - s_t # print("时间_创建新roidb:{}".format(t_t3)) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(new_roidb) else: #----如果裁剪完后是一张好的图 #print("执行了minibatch.py 239") roidb_copy = creat_new_roidb_empty(roidb_n, img_crop_w, img_crop_h) target_size = cfg.TRAIN.SCALES[scale_inds[i]] # im, imscale = blob_utils.prep_im_for_blob(img[coor[0]:coor[1],coor[2]:coor[3]], cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im, imscale = blob_utils.prep_im_for_blob(img_, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(roidb_copy) blob = blob_utils.im_list_to_blob(processed_img) # print(" minibatch 266 @@@@@@@imgs_scale{}, roidb[boxes]{}]".format(imgs_scale,all_roidb)) #print(len(all_roidb)) #print(all_roidb) # e_t__ = time.time() # t_t__ = e_t__ - s_t # print("时间_最终:{}".format(t_t__)) return blob, imgs_scale, all_roidb
def _get_image_blob(roidb): """Builds an input blob from the images in the roidb at the specified scales. """ #print("qwryyudfggjhgkjgk{}sdfgfdsg".format(roidb)) # print("minibatch.py 118 此轮roidb的长度为:",len(roidb)) # 获得数据的数量 num_images = len(roidb) # 在这批数据中最XXXX的采样随机比例 (600,)这个的len()只是1 np.random.randint即在第一个参数和'high'值-1,的整数间生成list,list元素数量是roidb的数量 scale_inds = np.random.randint( 0, high=len(cfg.TRAIN.SCALES), size=num_images) # 因为cfg.TRAIN.SCALES为(600,), 所以生成的是一个全为0的list processed_ims = [] # 图片数据的list im_scales = [] # 尺度比例list for i in range(num_images): #print("minibatch.py 109 @@@@@@@ roidb的boxes{}".format(roidb[i]['boxes'])) try: im = cv2.imread(roidb[i]['image']) # 读取roidb的图像数据 #print("---这是此次的for循环的第{}/{}轮,图像为{}".format(i,num_images,roidb[i]['image'])) except Exception as e: print("Exception:", e) assert im is not None, \ 'Failed to read image \'{}\''.format(roidb[i]['image']) # 如果图像不存在则assert if roidb[i]['flipped']: # 如果图的flipped为翻转,则翻转 im = im[:, ::-1, :] # 对图像的宽做倒序 即x轴flipped # s_t1 = time.time() target_size = cfg.TRAIN.SCALES[scale_inds[i]] # target_size是600 im, im_scale = blob_utils.prep_im_for_blob( # im是 resize后的数据, im_scale 是resize的比例 参数中cfg.PIXEL_MEANS/cfg.TRAIN.MAX_SIZE在配置文件中有配置 im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im_scales.append(im_scale) # 加入到im_scales的list中 processed_ims.append(im) # 加入到图像数据list中 #--------------------- 无多尺度下开启旋转模块 -------------------------------------- img_ = img.copy() # 其实在原图做完计算之后可以不用考虑值会改变的事了 if ROTATION: # 是否旋转 rot_random = random.uniform(0, 1) if rot_random <= ROT: roidb_n = {} roidb_n['boxes'] = copy.deepcopy(roidb[i]['boxes']) roidb_n['width'] = roidb[i]['width'] roidb_n['height'] = roidb[i]['height'] roidb_n['has_visible_keypoints'] = roidb[i][ 'has_visible_keypoints'] roidb_n['flipped'] = roidb[i]['flipped'] roidb_n['seg_areas'] = roidb[i]['seg_areas'] roidb_n['dataset'] = roidb[i]['dataset'] roidb_n['segms'] = roidb[i]['segms'] roidb_n['id'] = roidb[i]['id'] roidb_n['image'] = roidb[i]['image'] roidb_n['gt_classes'] = roidb[i]['gt_classes'] roidb_n['gt_overlaps'] = roidb[i]['gt_overlaps'] roidb_n['is_crowd'] = roidb[i]['is_crowd'] roidb_n['box_to_gt_ind_map'] = roidb[i]['box_to_gt_ind_map'] roidb_n['max_classes'] = roidb[i]['max_classes'] roidb_n['max_overlaps'] = roidb[i]['max_overlaps'] roidb_n['bbox_targets'] = roidb[i]['bbox_targets'] if ROT_RANDOM: angle = random.randint(-ROT_RANDOM_ANGLE, ROT_RANDOM_ANGLE) else: angle = random.randint(1, 4) angle = angle * 90.0 img_, h_, w_, l, n_w, n_h = rotate_image(img_, angle, True) # 裁剪完后的图 angle = (angle / 180.0 * math.pi) # 坐标调整,先计算pad后坐标,再计算旋转后,在计算裁剪后 for m, roi_boxes in enumerate(roidb_n['boxes']): roi_boxes[0] = roi_boxes[0] + int((l - w_) / 2.0) roi_boxes[1] = roi_boxes[1] + int((l - h_) / 2.0) roi_boxes[2] = roi_boxes[2] + int((l - w_) / 2.0) roi_boxes[3] = roi_boxes[3] + int((l - h_) / 2.0) x0, y0 = rot_compute(l / 2, l / 2, roi_boxes[0], roi_boxes[1], angle) x1, y1 = rot_compute(l / 2, l / 2, roi_boxes[2], roi_boxes[1], angle) x2, y2 = rot_compute(l / 2, l / 2, roi_boxes[0], roi_boxes[3], angle) x3, y3 = rot_compute(l / 2, l / 2, roi_boxes[2], roi_boxes[3], angle) min_x, min_y, max_x, max_y = box_coordinate( x0, y0, x1, y1, x2, y2, x3, y3) min_x = min_x - int((l - n_w) / 2.0) min_y = min_y - int((l - n_h) / 2.0) max_x = max_x - int((l - n_w) / 2.0) max_y = max_y - int((l - n_h) / 2.0) roidb_n['boxes'][m][0] = int(min_x) roidb_n['boxes'][m][1] = int(min_y) roidb_n['boxes'][m][2] = int(max_x) roidb_n['boxes'][m][3] = int(max_y) # Maskrcnn的点的扩增模块---------------------------------------------------------------- if MASKRCNN_SWITCH: for m_segme, roi_segms in enumerate(roidb_n['segms']): for m_segme_index in range(len(roi_segms) / 2): roi_segms[m_segme][ m_segme_index] = roi_segms_point + int( (l - w_) / 2.0) roi_segms[m_segme][m_segme_index + 1] = roi_segms_point + int( (l - h_) / 2.0) x_segme, y_segme = rot_compute( l / 2, l / 2, roi_segms[m_segme][m_segme_index], roi_segms[m_segme][m_segme_index + 1], angle) roidb_n['segms'][m_segme][m_segme_index] = int( x_segme) roidb_n['segms'][m_segme][m_segme_index + 1] = int(y_segme) # ----------------------------------------------------------------------------------- roidb[i] = roidb_n else: pass target_size = cfg.TRAIN.SCALES[scale_inds[i]] #im, imscale = blob_utils.prep_im_for_blob(img[coor[0]:coor[1],coor[2]:coor[3]], cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im, imscale = blob_utils.prep_im_for_blob(img_, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) # e_t3 = time.time() # t_t3= e_t3 - s_t # print("时间_创建新roidb:{}".format(t_t3)) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(new_roidb) #--------------------- 无多尺度下开启旋转模块 -------------------------------------- # Create a blob to hold the input images blob = blob_utils.im_list_to_blob(processed_ims) #e_t1 = time.time() #t_t1 = e_t1 - s_t1 #print("时间_最终:{}".format(t_t1)) return blob, im_scales
def _get_img_blob_mul(roidb): num_images = len(roidb) processed_img = [] imgs_scale = [] scale_inds = np.random.randint(0, len(cfg.TRAIN.SCALES), size=num_images) all_roidb = [] for i in range(num_images): img_path = roidb[i]['image'] img = cv2.imread(img_path) h, w = img.shape[:2] # 如果图的flipped为翻转,则翻转 if roidb[i]['flipped']: img = img[:, ::-1, :] target_size = cfg.TRAIN.SCALES[scale_inds[i]] # im是 resize后的数据, im_scale 是resize的比例 参数中cfg.PIXEL_MEANS/cfg.TRAIN.MAX_SIZE在配置文件中有配置 im, imscale = blob_utils.prep_im_for_blob(img, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(roidb[i]) #print("roidb:---------------------",roidb[i]) img_ = img.copy() # 其实在原图做完计算之后可以不用考虑值会改变的事了 #----------------------旋转模块------------------------------- # ---------------------尽量不改变原roidb的值 roidb_n = roidb[i] if ROTATION: # 是否旋转 rot_random = random.uniform(0, 1) if rot_random <= ROT: if ROT_RANDOM: angle = random.randint(-ROT_RANDOM_ANGLE, ROT_RANDOM_ANGLE) else: angle = random.randint(1, 4) angle = angle * 90.0 img_, h_, w_, l, n_w, n_h = rotate_image(img_, angle, True) # 裁剪完后的图 angle = (angle / 180.0 * math.pi) roidb_n = creat_rotation_roidb(roidb_n, angle, l, w_, h_, n_w, n_h, MASKRCNN_SWITCH) else: pass # e_t_ = time.time() # t_t_ = e_t_ - s_t # print("旋转时间_:{}".format(t_t_)) # print("copy图像时间:{}".format(t_t_)) coor, new_boxes_crop, img_crop_w, img_crop_h, index_list = crop( h, w, roidb[i], WIDTH_REGION, HEIGHT_REGION) # 深拷贝以避免改变原始图像数据 img_ = copy.deepcopy(img_[coor[0]:coor[1], coor[2]:coor[3]]) # cv2.imwrite("crop_image.jpg", img_) # ------------------------ 色彩模块 -------------------------- if MULTI_COLOR: color_random = random.uniform(0, 1) #print("ccccccc",color_random) if color_random <= GAUSSIAN: #print("高斯") img_ = randomGaussian(img_, GAUSSIAN_KERNEL, GAUSSIAN_SIGMAX) elif color_random > GAUSSIAN and color_random <= CAB: #print("融合暗") img_ = Contrast_and_Brightness(ALPHA, GAMMA, img_) elif color_random > CAB and color_random <= GAMMA: #print("GAMMA") img_ = gamma_trans(img_, GAMMA_THRESHOLD) elif color_random > GAMMA and color_random <= HSV: #print("GAMMA") img_ = img2darker(img_, HSV_THRESHOLD) elif color_random > HSV and color_random <= BRIGHT: #print("GAMMA") img_ = bright_trans(img_, BRIGHT_THRESHOLD) else: pass #---------------------------------------------------------- if not new_boxes_crop == []: time_start = time.time() new_roidb = creat_new_roidb(roidb[i], img_crop_w, img_crop_h, new_boxes_crop, index_list, coor, MASKRCNN_SWITCH) #MASKRCNN_SWITCH) target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, imscale = blob_utils.prep_im_for_blob(img_, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) # -----maskrcnn相关的的实例分割的相关代码已集成到creat_new_roidb模块中 # time_start = time.time() # if MASKRCNN_SWITCH : # segms = [] # seg_areas = [] # #print("roidb:---------------------",new_roidb ) # if len(new_roidb['segms'])>0: # for m_s, roi_s in enumerate(new_roidb['segms']): # roi_s = np.array(roi_s[0][:-4]).reshape(1,-1,2) # img_se = np.zeros((roidb[i]['height'], roidb[i]['width'], 3)) # #img_se = np.zeros((roidb[i]['height'], roidb[i]['width'])) # img_se_ = img_se.copy()[coor[0]:coor[1], coor[2]:coor[3]] # imag = cv2.drawContours(img_se, [roi_s],-1,(255,255,255),-1) # #cv2.imwrite("mask_raw.jpg", imag) # imag = imag[coor[0]:coor[1], coor[2]:coor[3]] # # 必须转化为uint8格式,才可以进行二值化 # imag = np.array(imag, dtype = np.uint8) # #cv2.imwrite("mask_single.jpg", imag) # #imag = cv2.imread("mask_single.jpg", 3) # print("!@#^%^&^*^*",imag.dtype) # gray = cv2.cvtColor(imag, cv2.COLOR_BGR2GRAY) # # 一定要做二值化,否则出来的是散点 # ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) # _, contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) # img_fill = cv2.drawContours(imag, contours,-1,(0,255,0),-1) # #cv2.imwrite("mask_single_fill.jpg", img_fill) # if len(contours) > 0: # #print("------------------contours:-----------------",contours) # for kk, con_point in enumerate(contours): # if con_point[0][0][0] == 0: # con_point[0][0][0] = 1 # elif con_point[0][0][0] == img_crop_w -1: # con_point[0][0][0] = img_crop_w - 2 # else: pass # if con_point[0][0][1] == 0: # con_point[0][0][1] = 1 # elif con_point[0][0][1] == img_crop_h -1: # con_point[0][0][1] = img_crop_h - 2 # else: pass # roi_s = np.array(contours).reshape(1,-1) # area = float(int(cv2.contourArea(contours[0]))) # segms.append(roi_s.tolist()) # seg_areas.append(area) # # segms_ = [] # # # 验证mask框----- # # for rk, roi_k in enumerate(segms): # # roi_k = np.array(roi_k).reshape(-1,1,2) # # segms_.append(roi_k) # # #print("!!!!!points!!!!!!!",segms_) # # imag_ = cv2.drawContours(img_se_, segms_ ,-1,(255,255,255),-1) # # cv2.imwrite("maskrcnn_crop.jpg", imag_) # # --------------------------------------------------------------------------------- # new_roidb['segms'] = segms # new_roidb['seg_areas'] = np.array(seg_areas, dtype = np.float32) time_end = time.time() time_used = time_end - time_start print("Maskrcnn 裁剪耗时{}".format(time_used)) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(new_roidb) else: roidb_copy = creat_new_roidb_empty(roidb[i], img_crop_w, img_crop_h) target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, imscale = blob_utils.prep_im_for_blob(img_, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(roidb_copy) blob = blob_utils.im_list_to_blob(processed_img) return blob, imgs_scale, all_roidb
def _get_image_blob(roidb): """Builds an input blob from the images in the roidb at the specified scales. """ #print("qwryyudfggjhgkjgk{}sdfgfdsg".format(roidb)) # print("minibatch.py 118 此轮roidb的长度为:",len(roidb)) # 获得数据的数量 num_images = len(roidb) # 在这批数据中最XXXX的采样随机比例 (600,)这个的len()只是1 np.random.randint即在第一个参数和'high'值-1,的整数间生成list,list元素数量是roidb的数量 scale_inds = np.random.randint( 0, high=len(cfg.TRAIN.SCALES), size=num_images) # 因为cfg.TRAIN.SCALES为(600,), 所以生成的是一个全为0的list processed_img = [] # 图片数据的list im_scales = [] all_roidb = [] # 尺度比例list for i in range(num_images): #print("minibatch.py 109 @@@@@@@ roidb的boxes{}".format(roidb[i]['boxes'])) try: im = cv2.imread(roidb[i]['image']) # 读取roidb的图像数据 img = im.copy() #print("---这是此次的for循环的第{}/{}轮,图像为{}".format(i,num_images,roidb[i]['image'])) except Exception as e: print("Exception:", e) assert im is not None, \ 'Failed to read image \'{}\''.format(roidb[i]['image']) # 如果图像不存在则assert if roidb[i]['flipped']: # 如果图的flipped为翻转,则翻转 im = im[:, ::-1, :] # 对图像的宽做倒序 即x轴flipped # s_t1 = time.time() target_size = cfg.TRAIN.SCALES[scale_inds[i]] # target_size是600 im, im_scale = blob_utils.prep_im_for_blob( # im是 resize后的数据, im_scale 是resize的比例 参数中cfg.PIXEL_MEANS/cfg.TRAIN.MAX_SIZE在配置文件中有配置 im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im_scales.append(im_scale) # 加入到im_scales的list中 processed_img.append(im) # 加入到图像数据list中 all_roidb.append(roidb[i]) #--------------------- 无多尺度下开启旋转模块 -------------------------------------- img_ = img.copy() # 其实在原图做完计算之后可以不用考虑值会改变的事了 roidb_n = roidb[i] if ROTATION: # 是否旋转 rot_random = random.uniform(0, 1) if rot_random <= ROT: if ROT_RANDOM: angle = random.randint(-ROT_RANDOM_ANGLE, ROT_RANDOM_ANGLE) else: angle = random.randint(1, 4) angle = angle * 90.0 img_, h_, w_, l, n_w, n_h = rotate_image(img_, angle, True) # 裁剪完后的图 angle = (angle / 180.0 * math.pi) roidb_n = creat_rotation_roidb(roidb_n, angle, l, w_, h_, n_w, n_h, MASKRCNN_SWITCH) else: pass target_size = cfg.TRAIN.SCALES[scale_inds[i]] #im, imscale = blob_utils.prep_im_for_blob(img[coor[0]:coor[1],coor[2]:coor[3]], cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im, imscale = blob_utils.prep_im_for_blob(img_, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) # e_t3 = time.time() # t_t3= e_t3 - s_t # print("时间_创建新roidb:{}".format(t_t3)) processed_img.append(im) im_scales.append(imscale) all_roidb.append(roidb_n) #--------------------- 无多尺度下开启旋转模块 -------------------------------------- # Create a blob to hold the input images blob = blob_utils.im_list_to_blob(processed_img) #e_t1 = time.time() #t_t1 = e_t1 - s_t1 #print("时间_最终:{}".format(t_t1)) return blob, im_scales
def _get_image_blob(roidb): """Builds an input blob from the images in the roidb at the specified scales. """ num_images = len(roidb) # Sample random scales to use for each image in this batch scale_inds = np.random.randint(0, high=len(cfg.TRAIN.SCALES), size=num_images) processed_ims = [] im_scales = [] im_crops = [] for i in range(num_images): im = cv2.imread(roidb[i]['image']) assert im is not None, \ 'Failed to read image \'{}\''.format(roidb[i]['image']) if roidb[i]['flipped']: im = im[:, ::-1, :] if cfg.WSL.USE_DISTORTION: hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV) s0 = npr.random() * (cfg.WSL.SATURATION - 1) + 1 s1 = npr.random() * (cfg.WSL.EXPOSURE - 1) + 1 s0 = s0 if npr.random() > 0.5 else 1.0 / s0 s1 = s1 if npr.random() > 0.5 else 1.0 / s1 hsv = np.array(hsv, dtype=np.float32) hsv[:, :, 1] = np.minimum(s0 * hsv[:, :, 1], 255) hsv[:, :, 2] = np.minimum(s1 * hsv[:, :, 2], 255) hsv = np.array(hsv, dtype=np.uint8) im = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) if cfg.WSL.USE_CROP: im_shape = np.array(im.shape) crop_dims = im_shape[:2] * cfg.WSL.CROP r0 = npr.random() r1 = npr.random() s = im_shape[:2] - crop_dims s[0] *= r0 s[1] *= r1 im_crop = np.array( [s[0], s[1], s[0] + crop_dims[0] - 1, s[1] + crop_dims[1] - 1], dtype=np.int32) im = im[im_crop[0]:im_crop[2] + 1, im_crop[1]:im_crop[3] + 1, :] else: im_crop = np.array([0, 0, im.shape[0] - 1, im.shape[1] - 1], dtype=np.int32) target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, im_scale = blob_utils.prep_im_for_blob(im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im_scales.append(im_scale) im_crops.append(im_crop) processed_ims.append(im) # Create a blob to hold the input images blob = blob_utils.im_list_to_blob(processed_ims) return blob, im_scales, im_crops
def _get_img_blob_mul(roidb): num_images = len(roidb) processed_img = [] imgs_scale = [] scale_inds = np.random.randint(0, len(cfg.TRAIN.SCALES), size=num_images) all_roidb = [] for i in range(num_images): # print(i) #print("minibatch.py 187 @@@@@@@ roidb[boxes]{}".format(roidb[i]['boxes'])) img_path = roidb[i]['image'] #print("minibatch.py 189 image:------",img_path) img = cv2.imread(img_path) h, w = img.shape[:2] # 如果图的flipped为翻转,则翻转 if roidb[i]['flipped']: img = img[:, ::-1, :] # target_size是600,或者yaml中设置的尺度 target_size = cfg.TRAIN.SCALES[scale_inds[i]] # im是 resize后的数据, im_scale 是resize的比例 参数中cfg.PIXEL_MEANS/cfg.TRAIN.MAX_SIZE在配置文件中有配置 im, imscale = blob_utils.prep_im_for_blob(img, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(roidb[i]) img_ = img.copy() # 其实在原图做完计算之后可以不用考虑值会改变的事了 # s_t = time.time() if ROTATION: # 是否旋转 rot_random = random.uniform(0, 1) if rot_random <= ROT: roidb_n = {} roidb_n['boxes'] = copy.deepcopy(roidb[i]['boxes']) roidb_n['width'] = roidb[i]['width'] roidb_n['height'] = roidb[i]['height'] roidb_n['has_visible_keypoints'] = roidb[i][ 'has_visible_keypoints'] roidb_n['flipped'] = roidb[i]['flipped'] roidb_n['seg_areas'] = roidb[i]['seg_areas'] roidb_n['dataset'] = roidb[i]['dataset'] roidb_n['segms'] = roidb[i]['segms'] roidb_n['id'] = roidb[i]['id'] roidb_n['image'] = roidb[i]['image'] roidb_n['gt_classes'] = roidb[i]['gt_classes'] roidb_n['gt_overlaps'] = roidb[i]['gt_overlaps'] roidb_n['is_crowd'] = roidb[i]['is_crowd'] roidb_n['box_to_gt_ind_map'] = roidb[i]['box_to_gt_ind_map'] roidb_n['max_classes'] = roidb[i]['max_classes'] roidb_n['max_overlaps'] = roidb[i]['max_overlaps'] roidb_n['bbox_targets'] = roidb[i]['bbox_targets'] if ROT_RANDOM: angle = random.randint(-ROT_RANDOM_ANGLE, ROT_RANDOM_ANGLE) else: angle = random.randint(1, 4) angle = angle * 90.0 img_, h_, w_, l, n_w, n_h = rotate_image(img_, angle, True) # 裁剪完后的图 angle = (angle / 180.0 * math.pi) # 坐标调整,先计算pad后坐标,再计算旋转后,在计算裁剪后 for m, roi_boxes in enumerate(roidb_n['boxes']): roi_boxes[0] = roi_boxes[0] + int((l - w_) / 2.0) roi_boxes[1] = roi_boxes[1] + int((l - h_) / 2.0) roi_boxes[2] = roi_boxes[2] + int((l - w_) / 2.0) roi_boxes[3] = roi_boxes[3] + int((l - h_) / 2.0) x0, y0 = rot_compute(l / 2, l / 2, roi_boxes[0], roi_boxes[1], angle) x1, y1 = rot_compute(l / 2, l / 2, roi_boxes[2], roi_boxes[1], angle) x2, y2 = rot_compute(l / 2, l / 2, roi_boxes[0], roi_boxes[3], angle) x3, y3 = rot_compute(l / 2, l / 2, roi_boxes[2], roi_boxes[3], angle) min_x, min_y, max_x, max_y = box_coordinate( x0, y0, x1, y1, x2, y2, x3, y3) min_x = min_x - int((l - n_w) / 2.0) min_y = min_y - int((l - n_h) / 2.0) max_x = max_x - int((l - n_w) / 2.0) max_y = max_y - int((l - n_h) / 2.0) roidb_n['boxes'][m][0] = int(min_x) roidb_n['boxes'][m][1] = int(min_y) roidb_n['boxes'][m][2] = int(max_x) roidb_n['boxes'][m][3] = int(max_y) roidb[i] = roidb_n else: pass # e_t_ = time.time() # t_t_ = e_t_ - s_t # print("旋转时间_:{}".format(t_t_)) coor, new_boxes_crop, img_crop_w, img_crop_h, index_list = crop( h, w, roidb[i], [0.8, 0.9], [0.8, 0.9]) # 深拷贝以避免改变原始图像数据 img_ = copy.deepcopy(img_[coor[0]:coor[1], coor[2]:coor[3]]) # e_t = time.time() # t_t = e_t - s_t # print("时间:{}".format(t_t)) # 随机决定要不要做色度上的数据扩增 if MULTI_COLOR: color_random = random.uniform(0, 1) #print("ccccccc",color_random) if color_random <= GAUSSIAN: #print("高斯") img_ = randomGaussian(img_, GAUSSIAN_KERNEL, GAUSSIAN_SIGMAX) elif color_random <= CAB: pass else: #print("融合暗") img_ = Contrast_and_Brightness(ALPHA, GAMMA, img_) if not new_boxes_crop == []: #print("执行了minibatch.py 230") new_roidb = creat_new_roidb(roidb[i], img_crop_w, img_crop_h, new_boxes_crop, index_list) # e_t2 = time.time() # t_t2= e_t2 - s_t #print("时间_创建新roidb:{}".format(t_t2)) target_size = cfg.TRAIN.SCALES[scale_inds[i]] #im, imscale = blob_utils.prep_im_for_blob(img[coor[0]:coor[1],coor[2]:coor[3]], cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im, imscale = blob_utils.prep_im_for_blob(img_, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) # e_t3 = time.time() # t_t3= e_t3 - s_t # print("时间_创建新roidb:{}".format(t_t3)) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(new_roidb) # img__ = img_ # img_name = str(new_roidb['image']).split("/",-1)[-1] # for mi, roi_ng in enumerate(new_roidb['boxes']): # cv2.rectangle(img__, (roi_ng[0], roi_ng[1]),(roi_ng[2], roi_ng[3]), (255,0,0),2) else: #print("执行了minibatch.py 239") roidb_copy = creat_new_roidb_empty(roidb[i], img_crop_w, img_crop_h) target_size = cfg.TRAIN.SCALES[scale_inds[i]] # im, imscale = blob_utils.prep_im_for_blob(img[coor[0]:coor[1],coor[2]:coor[3]], cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im, imscale = blob_utils.prep_im_for_blob(img_, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(roidb_copy) # img__ = img_ # img_name = str(roidb_copy['image']).split("/",-1)[-1] # time_n = time.time() # cv2.imwrite("/home/icubic/detectron_img/" + img_name[:-4] + "_" +str(time_n) + ".jpg" , img__) blob = blob_utils.im_list_to_blob(processed_img) # print(" minibatch 266 @@@@@@@imgs_scale{}, roidb[boxes]{}]".format(imgs_scale,all_roidb)) #print(len(all_roidb)) #print(all_roidb) # e_t__ = time.time() # t_t__ = e_t__ - s_t # print("时间_最终:{}".format(t_t__)) return blob, imgs_scale, all_roidb
def get_img_blob(roidb): num_images = len(roidb) processed_img = [] imgs_scale = [] roidb_base = {} all_roidb = [] scale_inds = np.random.randint(0, len(cfg.TRAIN.SCALES), size=num_images) for i in range(num_images): # print(i) roidb_copy = {} # print("minibatch.py 220 @@@@@@@roidb[boxes]{}".format(roidb[i]['boxes'])) img_path = roidb[i][u'image'] # print("minibatch.py 222 image:------------------",img_path) img = cv2.imread(img_path) # img=np.array(img) z = crop(img, roidb[i]) image, new_boxes_crop, new_classes_crop, img_crop_w, img_crop_h, index_list = z # for i in range(len(new_boxes_crop)): # bbox = new_boxes_crop[i] # x_min = bbox[0] # y_min = bbox[1] # x_max = bbox[2] # y_max = bbox[3] # cv2.rectangle(image, (int(x_min), int(y_min)), (int(x_max), int(y_max)), (0, 255, 0), 6) # cv2.imwrite('./new_img.jpg',image) if not new_boxes_crop == []: # print("minibatch 237 ",index_list) gt_class = roidb[i]['gt_classes'][index_list] # print("minibatch 239 ",gt_class) gt_overlaps = roidb[i]['gt_overlaps'][index_list] max_overlaps = roidb[i]['max_overlaps'][ index_list] #np.max(gt_overlaps,axis=1) max_classes = roidb[i]['max_classes'][ index_list] #np.argmax(gt_overlaps,axis=1) roidb_copy = roidb[i] roidb_copy['boxes'] = np.array(new_boxes_crop, dtype=np.float32) roidb_copy[u'width'] = img_crop_w roidb_copy[u'height'] = img_crop_h roidb_copy['gt_classes'] = gt_class roidb_copy['gt_overlaps'] = gt_overlaps roidb_copy['is_crowd'] = roidb[i]['is_crowd'][index_list] roidb_copy['box_to_gt_ind_map'] = roidb[i]['box_to_gt_ind_map'][ index_list] roidb_copy[u'max_classes'] = max_classes # print(roidb_copy[-1]['max_overlaps']) roidb_copy['max_overlaps'] = max_overlaps # m=roidb_copy[-1]['max_overlaps'] box_target = compute_bbox_regression_targets(roidb_copy) roidb_copy['bbox_target'] = box_target target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, imscale = blob_utils.prep_im_for_blob(img, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(roidb_copy) else: roidb_copy = roidb[i] roidb_copy[u'width'] = img_crop_w roidb_copy[u'height'] = img_crop_h target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, imscale = blob_utils.prep_im_for_blob(img, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(roidb_copy) if not processed_img == []: blob = blob_utils.im_list_to_blob(processed_img) # print(" minibatch 266 @@@@@@@imgs_scale{}, roidb[boxes]{}]".format(imgs_scale,all_roidb)) return blob, imgs_scale, all_roidb
def _get_img_blob_mul(roidb): num_images = len(roidb) processed_img = [] imgs_scale = [] scale_inds = np.random.randint(0, len(cfg.TRAIN.SCALES), size=num_images) all_roidb = [] for i in range(num_images): img_path = roidb[i]['image'] img = cv2.imread(img_path) h, w = img.shape[:2] # 如果图的flipped为翻转,则翻转 if roidb[i]['flipped']: img = img[:, ::-1, :] target_size = cfg.TRAIN.SCALES[scale_inds[i]] # im是 resize后的数据, im_scale 是resize的比例 参数中cfg.PIXEL_MEANS/cfg.TRAIN.MAX_SIZE在配置文件中有配置 im, imscale = blob_utils.prep_im_for_blob(img, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(roidb[i]) img_ = img.copy() # 其实在原图做完计算之后可以不用考虑值会改变的事了 # print("copy图像时间:{}".format(t_t_)) coor, new_boxes_crop, img_crop_w, img_crop_h, index_list = crop( h, w, roidb[i], WIDTH_REGION, HEIGHT_REGION) # 深拷贝以避免改变原始图像数据 img_ = copy.deepcopy(img_[coor[0]:coor[1], coor[2]:coor[3]]) if not new_boxes_crop == []: new_roidb = creat_new_roidb(roidb[i], img_crop_w, img_crop_h, new_boxes_crop, index_list) target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, imscale = blob_utils.prep_im_for_blob(img_, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) time_start = time.time() if MASKRCNN_SWITCH: segms = [] seg_areas = [] for m_s, roi_s in enumerate(new_roidb['segms']): roi_s = np.array(roi_s).reshape(1, -1, 2) img_se = np.zeros( (new_roidb['height'], new_roidb['width'], 3)) imag = cv2.drawContours(img_se, roi_s, -1, (255, 255, 255), -1) imag = imag[coor[0]:coor[1], coor[2]:coor[3]] imag = cv2.cvtColor(imag, cv2.COLOR_BGR2GRAY) _, contours, hierarchy = cv2.findContours( imag, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for kk, con_point in enumerate(contours): if con_point[0][0] == 0: con_point[0][0] = 1 elif con_point[0][0] == img_crop_w - 1: con_point[0][0] = con_point[0][0] - 1 else: pass if con_point[0][1] == 0: con_point[0][1] = 1 elif con_point[0][1] == img_crop_w - 1: con_point[0][1] = con_point[0][0] - 1 else: pass roi_s = contours.reshape(1, -1) eara = float(int(cv2.contourArea(contours[0]))) segms.append(roi_s) seg_areas.append(eara) new_roidb['segms'] = segms new_roidb['seg_areas'] = np.array(eara, dtype=np.float) time_end = time.time() time_used = time_end - time_start print("Maskrcnn 裁剪耗时{}".format(time_used)) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(new_roidb) else: roidb_copy = creat_new_roidb_empty(roidb[i], img_crop_w, img_crop_h) target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, imscale = blob_utils.prep_im_for_blob(img_, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) processed_img.append(im) imgs_scale.append(imscale) all_roidb.append(roidb_copy) blob = blob_utils.im_list_to_blob(processed_img) return blob, imgs_scale, all_roidb
def _get_image_blob_s6_0(roidb, roidb_noclass1): """Builds an input blob from the images in the roidb at the specified scales. """ num_images = len(roidb) # Sample random scales to use for each image in this batch scale_inds = np.random.randint(0, high=len(cfg.TRAIN.SCALES), size=num_images) processed_ims = [] im_scales = [] error_flag = [0, 0] for i in range(num_images): roidb_noclass = roidb_noclass1.copy() if roidb[i][u'image'].split('/')[-1] == u'test.jpg': random_bbox = dict() random_bbox['kernel_size'] = 224 random_bbox['tl_x'] = 0 random_bbox['tl_y'] = 0 x0 = random_bbox['tl_x'] x1 = random_bbox['tl_x'] + random_bbox['kernel_size'] y0 = random_bbox['tl_y'] y1 = random_bbox['tl_y'] + random_bbox['kernel_size'] im = cv2.imread(roidb[i]['image'])[y0:y1, x0:x1] im = cv2.resize(im, (WIDTH, HEIGHT)) #cv2.imwrite('/home/icubic/aa.png',im) error_flag[i] = 0 roidb[i] = roidb_noclass.copy() roidb[i][u'height'] = HEIGHT roidb[i][u'width'] = WIDTH else: if 1: real_class = [] #roidb[i]['gt_classes'][0] num_real_class = len(roidb[i]['gt_classes']) random_bbox = dict() random_bbox['kernel_size'] = 224 random_bbox['tl_x'] = random.randint(0, 800) random_bbox['tl_y'] = random.randint(0, 800) x0 = random_bbox['tl_x'] x1 = random_bbox['tl_x'] + random_bbox['kernel_size'] y0 = random_bbox['tl_y'] y1 = random_bbox['tl_y'] + random_bbox['kernel_size'] im = cv2.imread(roidb[i]['image'])[y0:y1, x0:x1] im = cv2.resize(im, (WIDTH, HEIGHT)) sum_inside_overlaps = 0 boxes_inside_overlaps = [] for i_roidb, sub_boxes in enumerate(roidb[i][u'boxes']): crop_x0 = int(sub_boxes[0]) crop_y0 = int(sub_boxes[1]) crop_x1 = int(sub_boxes[2]) crop_y1 = int(sub_boxes[3]) #real_x0 = float(crop_x0 - x0)*1024/224 # float(crop_x0) / 1024 * 224 #real_y0 = float(crop_y0 - y0)*1024/224 # float(crop_y0) / 1024 * 224 #real_x1 = float(crop_x1 - x0)*1024/224 # float(crop_x1) / 1024 * 224 #real_y1 = float(crop_y1 - y0)*1024/224 overlaps_rate = solve_coincide( (x0, y0, x1, y1), (crop_x0, crop_y0, crop_x1, crop_y1)) if overlaps_rate > 0.9: sum_inside_overlaps = sum_inside_overlaps + 1 #real_x0 = crop_x0 - x0 # float(crop_x0) / 1024 * 224 #real_y0 = crop_y0 - y0 # float(crop_y0) / 1024 * 224 #real_x1 = crop_x1 - x0 # float(crop_x1) / 1024 * 224 #real_y1 = crop_y1 - y0 real_x0 = float( crop_x0 - x0) * WIDTH / 224 # float(crop_x0) / 1024 * 224 real_y0 = float( crop_y0 - y0) * HEIGHT / 224 # float(crop_y0) / 1024 * 224 real_x1 = float( crop_x1 - x0) * WIDTH / 224 # float(crop_x1) / 1024 * 224 real_y1 = float(crop_y1 - y0) * HEIGHT / 224 if real_x0 < 0: real_x0 = 0 if real_x0 > WIDTH: real_x0 = WIDTH if real_x1 < 0: real_x1 = 0 if real_x1 > WIDTH: real_x1 = WIDTH if real_y0 < 0: real_y0 = 0 if real_y0 > HEIGHT: real_y0 = HEIGHT if real_y1 < 0: real_y1 = 0 if real_y1 > HEIGHT: real_y1 = HEIGHT boxes_inside_overlaps.append( [real_x0, real_y0, real_x1, real_y1]) real_class.append(roidb[i]['gt_classes'][i_roidb]) #cv2.rectangle(im, (int(real_x0), int(real_y0)), #(int(real_x1), int(real_y1)), (255, 0, 255)) #cv2.imwrite('/home/icubic/daily_work/code/circruit/new/result/uu.png', im) #a = roidb[i]['gt_overlaps'].toarray() if sum_inside_overlaps > 0: num_valid_objs = sum_inside_overlaps * 1 boxes = np.zeros((num_valid_objs, 4), dtype=np.float32) gt_classes = np.zeros((num_valid_objs), dtype=np.int32) gt_overlaps = np.zeros((num_valid_objs, 3), dtype=np.float32) box_to_gt_ind_map = np.zeros((num_valid_objs), dtype=np.int32) is_crowd = np.zeros((num_valid_objs), dtype=np.bool) for ix in range(num_valid_objs): gt_classes[ix] = real_class[ix] #real_class*1 try: gt_overlaps[ix, real_class] = 1.0 except: print('error') is_crowd[ix] = False box_to_gt_ind_map[ix] = ix for i_index in range(4): boxes[ix, i_index] = boxes_inside_overlaps[ix][i_index] #for ix in range(num_valid_objs): #box_to_gt_ind_map[ix] = ix #cls = real_class*1 roidb_noclass['boxes'] = np.append(roidb_noclass['boxes'], boxes, axis=0) roidb_noclass['gt_classes'] = np.append( roidb_noclass['gt_classes'], gt_classes) #mm = np.append( # roidb_noclass['gt_overlaps'].toarray(), gt_overlaps,axis=0) roidb_noclass['gt_overlaps'] = np.append( roidb_noclass['gt_overlaps'].toarray(), gt_overlaps) roidb_noclass['gt_overlaps'] = scipy.sparse.csr_matrix( roidb_noclass['gt_overlaps']) #mm = np.append(mm, gt_overlaps, axis=0) #roidb_noclass['gt_overlaps'] = scipy.sparse.csr_matrix(mm) roidb_noclass['is_crowd'] = np.append( roidb_noclass['is_crowd'], is_crowd) roidb_noclass['box_to_gt_ind_map'] = np.append( roidb_noclass['box_to_gt_ind_map'], box_to_gt_ind_map) gt_overlaps = roidb_noclass['gt_overlaps'].toarray() # max overlap with gt over classes (columns) max_overlaps = gt_overlaps.max(axis=1) # gt class that had the max overlap max_classes = gt_overlaps.argmax(axis=1) roidb_noclass['max_classes'] = max_classes roidb_noclass['max_overlaps'] = max_overlaps # sanity checks # if max overlap is 0, the class must be background (class 0) zero_inds = np.where(max_overlaps == 0)[0] assert all(max_classes[zero_inds] == 0) # if max overlap > 0, the class must be a fg class (not class 0) nonzero_inds = np.where(max_overlaps > 0)[0] assert all(max_classes[nonzero_inds] != 0) roidb_noclass[ 'bbox_targets'] = compute_bbox_regression_targets( roidb_noclass) roidb[i] = roidb_noclass.copy() roidb[i][u'height'] = HEIGHT roidb[i][u'width'] = WIDTH else: roidb[i] = roidb_noclass.copy() roidb[i][u'height'] = HEIGHT roidb[i][u'width'] = WIDTH if 0: if sum_inside_overlaps == 0: roidb[i] = roidb_noclass['0'].copy() roidb[i][u'height'] = 1024 roidb[i][u'width'] = 1024 if sum_inside_overlaps == 1: num_valid_objs = 1 roidb[i] = roidb_noclass['1'].copy() a = roidb[i]['gt_overlaps'].toarray() #for i_inside in enumerate(sum_inside_overlaps) if sum_inside_overlaps == 2: num_valid_objs = 2 roidb[i] = roidb_noclass['2'].copy() a = roidb[i]['gt_overlaps'].toarray() if sum_inside_overlaps == 3: num_valid_objs = 3 roidb[i] = roidb_noclass['3'].copy() a = roidb[i]['gt_overlaps'].toarray() if 0: crop_x0 = int(roidb[i][u'boxes'][0][0]) crop_y0 = int(roidb[i][u'boxes'][0][1]) crop_x1 = int(roidb[i][u'boxes'][0][2]) crop_y1 = int(roidb[i][u'boxes'][0][3]) crop_w = crop_x1 - crop_x0 crop_h = crop_y1 - crop_y0 random_bbox = dict() random_bbox['kernel_size'] = 224 random_bbox['tl_x'] = random.randint(0, 800) random_bbox['tl_y'] = random.randint(0, 800) x0 = random_bbox['tl_x'] x1 = random_bbox['tl_x'] + random_bbox['kernel_size'] y0 = random_bbox['tl_y'] y1 = random_bbox['tl_y'] + random_bbox['kernel_size'] #real_x0 = crop_x0-x0#float(crop_x0) / 1024 * 224 #real_y0 = crop_y0-y0#float(crop_y0) / 1024 * 224 #real_x1 = 1024#float(crop_x1) / 1024 * 224 #real_y1 = 1024#float(crop_y1) / 1024 * 224 overlaps_rate = solve_coincide( (x0, y0, x1, y1), (crop_x0, crop_y0, crop_x1, crop_y1)) im = cv2.imread(roidb[i]['image'])[y0:y1, x0:x1] #im = cv2.resize(im, (1024, 1024)) if overlaps_rate > 0.9: real_x0 = crop_x0 - x0 # float(crop_x0) / 1024 * 224 real_y0 = crop_y0 - y0 # float(crop_y0) / 1024 * 224 real_x1 = crop_x1 - x0 # float(crop_x1) / 1024 * 224 real_y1 = crop_y1 - y0 roidb[i][u'boxes'][0][0] = real_x0 roidb[i][u'boxes'][0][1] = real_y0 roidb[i][u'boxes'][0][2] = real_x1 roidb[i][u'boxes'][0][3] = real_y1 roidb[i][u'height'] = 224 roidb[i][u'width'] = 224 error_flag[i] = 1 #cv2.imwrite('/home/icubic/daily_work/code/Detectron/detectron/datasets/data/s6_test/aa.png',im) else: roidb[i] = roidb_noclass.copy() roidb[i][u'height'] = 224 roidb[i][u'width'] = 224 error_flag[i] = 0 #print('aa') assert im is not None, \ 'Failed to read image \'{}\''.format(roidb[i]['image']) if roidb[i]['flipped']: im = im[:, ::-1, :] target_size = cfg.TRAIN.SCALES[scale_inds[i]] im, im_scale = blob_utils.prep_im_for_blob(im, cfg.PIXEL_MEANS, target_size, cfg.TRAIN.MAX_SIZE) im_scales.append(im_scale) processed_ims.append(im) # Create a blob to hold the input images blob = blob_utils.im_list_to_blob(processed_ims) return blob, im_scales, error_flag