Ejemplo n.º 1
0
def generate_mean_pixel_file():
    C = Config()
    all_imgs, _, _ = get_data(ROI_BBOX_FILE)

    avg = [0, 0, 0]
    for img_data in all_imgs:
        print(img_data['filepath'])
        img_data_aug, x_img = augment(img_data, C, augment=False)

        (width, height) = (img_data_aug['width'], img_data_aug['height'])
        (rows, cols, _) = x_img.shape

        # get image dimensions for resizing
        (resized_width,
         resized_height) = get_new_img_size(width, height, C.im_size)

        # resize the image so that smalles side is length = 600px
        x_img = cv2.resize(x_img, (resized_width, resized_height),
                           interpolation=cv2.INTER_CUBIC)
        pixels = (resized_width * resized_height)
        avg[0] += np.sum(x_img[:, :, 0]) / pixels
        avg[1] += np.sum(x_img[:, :, 1]) / pixels
        avg[2] += np.sum(x_img[:, :, 2]) / pixels
    avg = [a / len(all_imgs) for a in list(avg)]
    np.savetxt(MEAN_PIXEL_FILE, avg, delimiter=',')
Ejemplo n.º 2
0
def get_anchor_gt(all_img_data, class_count, C, backend, mode='train'):

	all_img_data = sorted(all_img_data)

	sample_selector = SampleSelector(class_count)

	while True:
		if mode == 'train':
			random.shuffle(all_img_data)

		for img_data in all_img_data:
			try:

				if C.balanced_classes and sample_selector.skip_sample_for_balanced_class(img_data):
					continue

				# read in image, and optionally add augmentation

				if mode == 'train':
					img_data_aug, x_img = data_augment.augment(img_data, C, augment=True)
				else:
					img_data_aug, x_img = data_augment.augment(img_data, C, augment=False)

				(width, height) = (img_data_aug['width'], img_data_aug['height'])
				(rows, cols, _) = x_img.shape

				assert cols == width
				assert rows == height

				# get image dimensions for resizing
				(resized_width, resized_height) = get_new_img_size(width, height, C.im_size)

				# resize the image so that smalles side is length = 600px
				x_img = cv2.resize(x_img, (resized_width, resized_height), interpolation=cv2.INTER_CUBIC)

				try:
					y_rpn_cls, y_rpn_regr = calc_rpn(C, img_data_aug, width, height, resized_width, resized_height)
				except:
					continue

				# Zero-center by mean pixel, and preprocess image

				x_img = x_img[:,:, (2, 1, 0)]  # BGR -> RGB
				x_img = x_img.astype(np.float32)
				x_img[:, :, 0] -= C.img_channel_mean[0]
				x_img[:, :, 1] -= C.img_channel_mean[1]
				x_img[:, :, 2] -= C.img_channel_mean[2]
				x_img /= C.img_scaling_factor

				x_img = np.transpose(x_img, (2, 0, 1))
				x_img = np.expand_dims(x_img, axis=0)

				y_rpn_regr[:, y_rpn_regr.shape[1]/2:, :, :] *= C.std_scaling

				if backend == 'tf':
					x_img = np.transpose(x_img, (0, 2, 3, 1))
					y_rpn_cls = np.transpose(y_rpn_cls, (0, 2, 3, 1))
					y_rpn_regr = np.transpose(y_rpn_regr, (0, 2, 3, 1))

				yield np.copy(x_img), [np.copy(y_rpn_cls), np.copy(y_rpn_regr)], img_data_aug

			except Exception as e:
				print(e)
				continue
Ejemplo n.º 3
0
def get_anchor_gt(all_img_data,
                  class_mapping,
                  class_count,
                  C,
                  backend,
                  mode='train'):
    downscale = float(C.rpn_stride)

    all_img_data = sorted(all_img_data, key=sorted)

    anchor_sizes = C.anchor_box_scales
    anchor_ratios = C.anchor_box_ratios

    num_anchors = len(anchor_sizes) * len(anchor_ratios)

    sample_selector = SampleSelector(class_count)

    while True:
        if mode == 'train':
            random.shuffle(all_img_data)

        for img_data in all_img_data:
            if C.balanced_classes and sample_selector.skip_sample_for_balanced_class(
                    img_data):
                continue

            # read in image, and optionally add augmentation
            # img_data_aug, x_img = data_augment.augment(img_data, C, augment=True)
            if mode == 'train':
                img_data_aug, x_img = data_augment.augment(img_data,
                                                           C,
                                                           augment=True)
            else:
                img_data_aug, x_img = data_augment.augment(img_data,
                                                           C,
                                                           augment=False)

            (width, height) = (img_data_aug['width'], img_data_aug['height'])
            (rows, cols, _) = x_img.shape

            assert cols == width
            assert rows == height

            # get image dimensions for resizing
            (resized_width,
             resized_height) = get_new_img_size(width, height, C.im_size)

            # resize the image so that smalles side is length = 600px
            x_img = cv2.resize(x_img, (resized_width, resized_height),
                               interpolation=cv2.INTER_CUBIC)

            # calculate the output map size based on the network architecture
            (output_width,
             output_height) = get_img_output_length(resized_width,
                                                    resized_height)
            try:
                x_rois, y_rpn_cls, y_rpn_regr, y_class_num, y_class_regr = calcY(
                    C, class_mapping, img_data_aug, width, height,
                    resized_width, resized_height)
            except:
                continue
            # Zero-center by mean pixel
            x_img = x_img.astype(np.float32)
            x_img[:, :, 0] -= 103.939
            x_img[:, :, 1] -= 116.779
            x_img[:, :, 2] -= 123.68

            x_img = np.transpose(x_img, (2, 0, 1))
            x_img = np.expand_dims(x_img, axis=0)

            y_rpn_regr[:, int(y_rpn_regr.shape[1] / 2):, :, :] *= C.std_scaling
            y_class_regr[:,
                         int(y_class_regr.shape[1] / 2):, :] *= C.std_scaling

            if backend == 'tf':
                x_img = np.transpose(x_img, (0, 2, 3, 1))
                y_rpn_cls = np.transpose(y_rpn_cls, (0, 2, 3, 1))
                y_rpn_regr = np.transpose(y_rpn_regr, (0, 2, 3, 1))

            yield [np.copy(x_img), np.copy(x_rois)], [
                np.copy(y_rpn_cls),
                np.copy(y_rpn_regr),
                np.copy(y_class_num),
                np.copy(y_class_regr)
            ]
Ejemplo n.º 4
0
all_imgs = []

classes = {}

bbox_threshold = 0.8

visualise = True

#for idx, img_name in enumerate(sorted(os.listdir(img_path))):
#	if not img_name.lower().endswith(('.bmp', '.jpeg', '.jpg', '.png', '.tif', '.tiff')):
#		continue

for idx in range(64):
    st = time.time()
    _, img = data_augment.augment(n=np.random.randint(low=1, high=5))
    X, ratio = format_img(img, C)
    X = np.transpose(X, (0, 2, 3, 1))

    img = (img * 255).astype(np.uint8)
    img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)

    [Y1, Y2, F] = model_rpn.predict(X)
    R = roi_helpers.rpn_to_roi(Y1,
                               Y2,
                               C,
                               K.image_dim_ordering(),
                               overlap_thresh=0.7)

    R[:, 2] -= R[:, 0]
    R[:, 3] -= R[:, 1]
Ejemplo n.º 5
0
def get_anchor_gt(all_img_data, class_count, C, img_length_calc_function, mode='train'):
    #original code doen not compatible with py3.5
    #all_img_data = sorted(all_img_data)
    sample_selector = SampleSelector(class_count)


    if mode == 'train':
        #randomlized the data
        np.random.shuffle(all_img_data)
    #print("start")

    for img_data in all_img_data:
        try:
            ## C.balanced_classes = False for dafault
            ## bool for skip_
            ## some kinds of threshold
            if C.balanced_classes and sample_selector.skip_sample_for_balanced_class(img_data):
                continue

            #read img and augment
            ## if train: hor/ver flip or rotate according to setting
            ## else: just read img and return img and its size
            ## img_data_aug 是哈希表,存放各种和当前图象有关的信息
            ## img 是cv2读进来的图片
            #print("read")
            if mode == 'train' :
                img_data_aug, x_img = data_augment.augment(img_data, C, augment = True)
            else:
                img_data_aug, x_img = data_augment.augment(img_data, C, augment = False)
            ## check the information on side
            (width, height) = (img_data_aug['width'], img_data_aug['height'])
            (rows,cols, _ ) = x_img.shape
            assert cols == width
            assert rows == height

            # get image dim
            # C.im_size = 600
            # resize and make the shorter side fixing at 600
            (resized_w, resized_h) = get_new_img_size(width, height,C.im_size)
            x_img = cv2.resize(x_img, (resized_w, resized_h), interpolation = cv2.INTER_CUBIC)

            try:
                y_rpn_cls, y_rpn_reg = calc_rpn(C, img_data_aug, width, height, resized_w, resized_h, img_length_calc_function)
            except:
                continue

            ############################################### 4.1 ################################################################

            ## Zero-mean preprocess
            #BGR -> RGB due to cv2
            x_img = x_img[:,:,(2,1,0)]
            x_img = x_img.astype(np.float32)

            #C.img_channel_mean should calculated by ourself
            ## 减去均值达成均值归一
#             x_img[:,:,0] -= C.img_chanel_mean[0]
#             x_img[:,:,1] -= C.img_chanel_mean[1]
#             x_img[:,:,2] -= C.img_chanel_mean[2]
            x_img[:,:,0] -= np.mean(x_img[:,:,0])
            x_img[:,:,1] -= np.mean(x_img[:,:,1])
            x_img[:,:,2] -= np.mean(x_img[:,:,2])
            # C.img_scaling_factor = 1
            x_img /=  C.img_scaleing_factor


            ## 1, channel, height , weight
            x_img = np.transpose(x_img, (2,0,1))
            x_img = np.expand_dims(x_img, axis = 0)

            C.std_scaling = 4
            ## 1, num_anchor*4*2, height, weight
            ## y_rpn_reg.shape[1] // 2 = 9
            ## y_rpn_overlap 前面9个
            ## y_rpn_reg     后面9个
            y_rpn_reg[:, y_rpn_reg.shape[1] // 2:, :, :] *= C.std_scaling

            ## 1,height width channel for tensorflow form
            x_img = np.transpose(x_img, (0,2,3,1))
            ## 1,height, width, num_anchor*x for tensorflow form
            y_rpn_cls = np.transpose(y_rpn_cls, (0,2,3,1))
            y_rpn_reg = np.transpose(y_rpn_reg, (0,2,3,1))

            #build a generator
            yield np.copy(x_img), [np.copy(y_rpn_cls), np.copy(y_rpn_reg)], img_data_aug

        #keep moving
        except Exception as e:
            print(e)
            continue