Esempio n. 1
0
    "--config_filename",
    type=str,
    default="config.pickle",
    help=
    "Location to read the metadata related to the training (generated when training)."
)

# parse and validate parameters
args = parser.parse_args()

for k, v in args._get_kwargs():
    if isinstance(v, str):
        setattr(args, k, v.strip().lower())

# print arguments
rootLogger.info("Running with the following parameters:")
rootLogger.info(vars(args))

# which architecture to use {VGG16, Resnet50}
if args.architecture == 'vgg':
    from keras_frcnn.networks import vgg as nn
elif args.architecture == 'resnet':
    from keras_frcnn.networks import resnet as nn
else:
    rootLogger.info('Not a valid model')
    raise ValueError

if not args.model_weight_path:  # if filename is not given
    rootLogger.info(
        'Error: path to test data must be specified. Pass --model_weight_path to command line'
    )
def get_anchor_gt(img, C, img_length_calc_function, mode='train'):
    """
    This function returns Ground Truth Anchor
    :param img:
    :param C:
    :param img_length_calc_function:
    :param mode:
    :return:
    """

    # Randomly shuffle training data
    # if mode == 'train':
    #     np.random.shuffle(img_data)

    # for img in img_data:
    try:
        # read in image, and optionally add augmentation

        # Augment the data in case of training
        # if mode == 'train':
        #     # Returns augmented image and corresponding bboxes
        #     img_data_aug, x_img = data_augment.augment(img, C, augment=True)
        # else:
        img_data_aug, x_img = data_augment.augment(img, C, augment=False)

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

        # Cross validate if the augmented data is correct
        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)

        # TODO :  Calculates RPN
        try:
            y_rpn_cls, y_rpn_regr = calc_rpn(C, img_data_aug, width, height,
                                             resized_width, resized_height,
                                             img_length_calc_function)
        except:
            rootLogger.info("Exception occured in RPN")
            # 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

        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))

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

    except Exception as e:
        rootLogger.info("Exception occured in `anchor_gt`")
def get_data(input_path):
    classes_count = {}
    class_mapping = {}
    visualise = False

    # Get csv file for data
    train_annot_path = os.path.join(input_path, 'train.csv')
    val_annot_path = os.path.join(input_path, 'train.csv')

    # Get Image Path
    imgsets_path_train = os.path.join(input_path, 'train')
    imgsets_path_val = os.path.join(input_path, 'train')

    rootLogger.info('Parsing annotation files')

    train_files = []
    val_files = []

    train_annotation_data = []
    val_annotation_data = []

    try:
        # Training Data

        with open(train_annot_path) as f:
            csv_reader = csv.reader(f, delimiter=',')
            line_count = 0
            for row in csv_reader:
                if line_count == 0:
                    line_count += 1
                elif np.abs(int(row[4]) - int(row[6])) > 50 and np.abs(
                        int(row[5]) - int(row[7])) > 50:
                    train_files.append(row[0])
                    print(row[0])
                    annotation_data = {
                        'filepath':
                        os.path.join(imgsets_path_train, row[0]),
                        'width':
                        row[1],
                        'height':
                        row[2],
                        'bboxes': [{
                            'class': row[3],
                            'x1': int(row[4]),
                            'x2': int(row[6]),
                            'y1': int(row[5]),
                            'y2': int(row[7])
                        }]
                    }
                    train_annotation_data.append(annotation_data)

                    class_name = annotation_data['bboxes'][0]['class']
                    if class_name not in classes_count:
                        classes_count[class_name] = 1
                        break
                    else:
                        classes_count[class_name] += 1

                    if class_name not in class_mapping:
                        class_mapping[class_name] = len(class_mapping)
    except Exception as e:
        print(e)
        rootLogger.info("Exception in loading training data")

    try:

        # Validation Data
        with open(val_annot_path) as f:
            csv_reader = csv.reader(f, delimiter=',')
            line_count = 0
            for row in csv_reader:
                if line_count == 0:
                    line_count += 1
                else:
                    val_files.append(row[0])
                    print(row[0])
                    annotation_data = {
                        'filepath':
                        os.path.join(imgsets_path_val, row[0]),
                        'width':
                        row[1],
                        'height':
                        row[2],
                        'bboxes': [{
                            'class': row[3],
                            'x1': int(row[4]),
                            'x2': int(row[6]),
                            'y1': int(row[5]),
                            'y2': int(row[7])
                        }]
                    }
                    val_annotation_data.append(annotation_data)

                    class_name = annotation_data['bboxes'][0]['class']
                    if class_name not in classes_count:
                        classes_count[class_name] = 1
                    else:
                        classes_count[class_name] += 1
                        break

                    if class_name not in class_mapping:
                        class_mapping[class_name] = len(class_mapping)
    except Exception as e:
        print(e)
        rootLogger.info("Exception in loading Validation data")

    # all_imgs = train_annotation_data + val_annotation_data
    return train_annotation_data, val_annotation_data, classes_count, class_mapping
Esempio n. 4
0
    default="config.pickle",
    help=
    "Location to store all the metadata related to the training (to be used when testing)."
)

# parse and validate parameters
args = parser.parse_args()

for k, v in args._get_kwargs():
    if isinstance(v, str):
        setattr(args, k, v.strip().lower())

os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu) if args.gpu > -1 else '-1'

# print arguments
rootLogger.info("Running with the following parameters:")
rootLogger.info(vars(args))

# pass the settings from the command line, and persist them in the config object
C = config.Config()

# Use this augmentation only in case of training data, not the validation data
C.use_horizontal_flips = bool(args.horizontal_flips)
C.use_vertical_flips = bool(args.vertical_flips)
C.rot_90 = bool(args.rot_90)

C.model_path = args.output_weight_path
C.num_rois = int(args.num_rois)

# which architecture to use {VGG16, Resnet50}
if args.architecture == 'vgg':