Пример #1
0
def main():
    args = parse_args()
    config = InferenceConfig.from_config_dir(args.exp_dir)
    config.test_fn = args.test_file
    dataset = InferenceDataset(config)
    model = Seq2seqInferenceModel(config, dataset)
    model.run_inference(outfile=args.output_file)
Пример #2
0
 def __init__(self,config=None):
     if config == None:
        config = InferenceConfig() 
     dir_path = os.path.dirname(os.path.realpath(__file__))
     file = open(os.path.join(dir_path, config.MODEL_DIR, config.MODEL_FILE), 'rb')
     self._svm_model = pickle.load(file)
     file.close()
Пример #3
0
def run(filepath, is_gt, visualize):
	# FIlepath: Path to write file csv for test, else path to gt csv
	print(visualize)
	# select trained model
	dir_names = next(os.walk(MODEL_DIR))[1]
	print(MODEL_DIR)
	key = config.NAME.lower()
	dir_names = filter(lambda f: f.startswith(key), dir_names)
	dir_names = sorted(dir_names)
	print(dir_names)
	if not dir_names:
		import errno
		raise FileNotFoundError(
			errno.ENOENT,
			"Could not find model directory under {}".format(MODEL_DIR))

	fps = []
	# Pick last directory
	for d in dir_names:
		dir_name = os.path.join(MODEL_DIR, d)
		# Find the last checkpoint
		checkpoints = next(os.walk(dir_name))[2]
		checkpoints = filter(lambda f: f.startswith("mask_rcnn"), checkpoints)
		checkpoints = sorted(checkpoints)
		if not checkpoints:
			print('No weight files in {}'.format(dir_name))
		else:
			checkpoint = os.path.join(dir_name, checkpoints[-1])
			fps.append(checkpoint)

	model_path = sorted(fps)[-1]
	print('Found model {}'.format(model_path))

	inference_config = InferenceConfig()

	# Recreate the model in inference mode
	model = modellib.MaskRCNN(mode='inference',
							config=inference_config,
							model_dir=MODEL_DIR)

	# Load trained weights (fill in path to trained weights here)
	assert model_path != "", "Provide path to trained weights"
	print("Loading weights from ", model_path)
	model.load_weights(model_path, by_name=True)

	# Get filenames of test dataset DICOM images
	test_image_fps = get_dicom_fps(test_dicom_dir)
	# Write predictions to file
	predict(model, test_image_fps, filepath=filepath, is_gt=is_gt, visualize=visualize)
Пример #4
0
def evaluate(options):
    config = InferenceConfig(options)
    config.FITTING_TYPE = options.numAnchorPlanes

    if options.dataset == '':
        dataset = PlaneDataset(options,
                               config,
                               split='test',
                               random=False,
                               load_semantics=False)
    elif options.dataset == 'occlusion':
        config_dataset = copy.deepcopy(config)
        config_dataset.OCCLUSION = False
        dataset = PlaneDataset(options,
                               config_dataset,
                               split='test',
                               random=False,
                               load_semantics=True)
    elif 'nyu' in options.dataset:
        dataset = NYUDataset(options, config, split='val', random=False)
    elif options.dataset == 'synthia':
        dataset = SynthiaDataset(options, config, split='val', random=False)
    elif options.dataset == 'kitti':
        camera = np.zeros(6)
        camera[0] = 9.842439e+02
        camera[1] = 9.808141e+02
        camera[2] = 6.900000e+02
        camera[3] = 2.331966e+02
        camera[4] = 1242
        camera[5] = 375
        dataset = InferenceDataset(
            options,
            config,
            image_list=glob.glob('../../Data/KITTI/scene_3/*.png'),
            camera=camera)
    elif options.dataset == '7scene':
        camera = np.zeros(6)
        camera[0] = 519
        camera[1] = 519
        camera[2] = 320
        camera[3] = 240
        camera[4] = 640
        camera[5] = 480
        dataset = InferenceDataset(
            options,
            config,
            image_list=glob.glob('../../Data/SevenScene/scene_3/*.png'),
            camera=camera)
    elif options.dataset == 'tanktemple':
        camera = np.zeros(6)
        camera[0] = 0.7
        camera[1] = 0.7
        camera[2] = 0.5
        camera[3] = 0.5
        camera[4] = 1
        camera[5] = 1
        dataset = InferenceDataset(
            options,
            config,
            image_list=glob.glob('../../Data/TankAndTemple/scene_4/*.jpg'),
            camera=camera)
    elif options.dataset == 'make3d':
        camera = np.zeros(6)
        camera[0] = 0.7
        camera[1] = 0.7
        camera[2] = 0.5
        camera[3] = 0.5
        camera[4] = 1
        camera[5] = 1
        dataset = InferenceDataset(
            options,
            config,
            image_list=glob.glob('../../Data/Make3D/*.jpg'),
            camera=camera)
    elif options.dataset == 'popup':
        camera = np.zeros(6)
        camera[0] = 0.7
        camera[1] = 0.7
        camera[2] = 0.5
        camera[3] = 0.5
        camera[4] = 1
        camera[5] = 1
        dataset = InferenceDataset(
            options,
            config,
            image_list=glob.glob('../../Data/PhotoPopup/*.jpg'),
            camera=camera)
    elif options.dataset == 'cross' or options.dataset == 'cross_2':
        image_list = [
            'test/cross_dataset/' + str(c) + '_image.png' for c in range(12)
        ]
        cameras = []
        camera = np.zeros(6)
        camera[0] = 587
        camera[1] = 587
        camera[2] = 320
        camera[3] = 240
        camera[4] = 640
        camera[5] = 480
        for c in range(4):
            cameras.append(camera)
            continue
        camera_kitti = np.zeros(6)
        camera_kitti[0] = 9.842439e+02
        camera_kitti[1] = 9.808141e+02
        camera_kitti[2] = 6.900000e+02
        camera_kitti[3] = 2.331966e+02
        camera_kitti[4] = 1242.0
        camera_kitti[5] = 375.0
        for c in range(2):
            cameras.append(camera_kitti)
            continue
        camera_synthia = np.zeros(6)
        camera_synthia[0] = 133.185088
        camera_synthia[1] = 134.587036
        camera_synthia[2] = 160.000000
        camera_synthia[3] = 96.000000
        camera_synthia[4] = 320
        camera_synthia[5] = 192
        for c in range(2):
            cameras.append(camera_synthia)
            continue
        camera_tanktemple = np.zeros(6)
        camera_tanktemple[0] = 0.7
        camera_tanktemple[1] = 0.7
        camera_tanktemple[2] = 0.5
        camera_tanktemple[3] = 0.5
        camera_tanktemple[4] = 1
        camera_tanktemple[5] = 1
        for c in range(2):
            cameras.append(camera_tanktemple)
            continue
        for c in range(2):
            cameras.append(camera)
            continue
        dataset = InferenceDataset(options,
                                   config,
                                   image_list=image_list,
                                   camera=cameras)
    elif options.dataset == 'selected':
        image_list = glob.glob('test/selected_images/*_image_0.png')
        image_list = [
            filename for filename in image_list
            if '63_image' not in filename and '77_image' not in filename
        ] + [
            filename for filename in image_list
            if '63_image' in filename or '77_image' in filename
        ]
        camera = np.zeros(6)
        camera[0] = 587
        camera[1] = 587
        camera[2] = 320
        camera[3] = 240
        camera[4] = 640
        camera[5] = 480
        dataset = InferenceDataset(options,
                                   config,
                                   image_list=image_list,
                                   camera=camera)
    elif options.dataset == 'comparison':
        image_list = [
            'test/comparison/' + str(index) + '_image_0.png'
            for index in [65, 11, 24]
        ]
        camera = np.zeros(6)
        camera[0] = 587
        camera[1] = 587
        camera[2] = 320
        camera[3] = 240
        camera[4] = 640
        camera[5] = 480
        dataset = InferenceDataset(options,
                                   config,
                                   image_list=image_list,
                                   camera=camera)
    elif 'inference' in options.dataset:
        image_list = glob.glob(options.customDataFolder +
                               '/*.png') + glob.glob(options.customDataFolder +
                                                     '/*.jpg')
        if os.path.exists(options.customDataFolder + '/camera.txt'):
            camera = np.zeros(6)
            with open(options.customDataFolder + '/camera.txt', 'r') as f:
                for line in f:
                    values = [
                        float(token.strip()) for token in line.split(' ')
                        if token.strip() != ''
                    ]
                    for c in range(6):
                        camera[c] = values[c]
                        continue
                    break
                pass
        else:
            camera = [
                filename.replace('.png', '.txt').replace('.jpg', '.txt')
                for filename in image_list
            ]
            pass
        dataset = InferenceDataset(options,
                                   config,
                                   image_list=image_list,
                                   camera=camera)
        pass

    print('the number of images', len(dataset))

    dataloader = DataLoader(dataset,
                            batch_size=1,
                            shuffle=False,
                            num_workers=1)

    epoch_losses = []
    data_iterator = tqdm(dataloader, total=len(dataset))

    specified_suffix = options.suffix
    with torch.no_grad():
        detectors = []
        for method in options.methods:
            if method == 'w':
                options.suffix = 'pair_' + specified_suffix if specified_suffix != '' else 'pair'
                detectors.append(('warping',
                                  PlaneRCNNDetector(options,
                                                    config,
                                                    modelType='pair')))
            elif method == 'b':
                options.suffix = specified_suffix if specified_suffix != '' else ''
                detectors.append(('basic',
                                  PlaneRCNNDetector(options,
                                                    config,
                                                    modelType='pair')))
            elif method == 'o':
                options.suffix = 'occlusion_' + specified_suffix if specified_suffix != '' else 'occlusion'
                detectors.append(('occlusion',
                                  PlaneRCNNDetector(options,
                                                    config,
                                                    modelType='occlusion')))
            elif method == 'p':
                detectors.append(
                    ('planenet', PlaneNetDetector(options, config)))
            elif method == 'e':
                detectors.append(
                    ('planerecover', PlaneRecoverDetector(options, config)))
            elif method == 't':
                if 'gt' in options.suffix:
                    detectors.append(
                        ('manhattan_gt',
                         TraditionalDetector(options, config, 'manhattan_gt')))
                else:
                    detectors.append(
                        ('manhattan_pred',
                         TraditionalDetector(options, config,
                                             'manhattan_pred')))
                    pass
            elif method == 'n':
                options.suffix = specified_suffix if specified_suffix != '' else ''
                detectors.append(('non_planar',
                                  DepthDetector(options,
                                                config,
                                                modelType='np')))
            elif method == 'r':
                options.suffix = specified_suffix if specified_suffix != '' else ''
                detectors.append(('refine',
                                  PlaneRCNNDetector(options,
                                                    config,
                                                    modelType='refine')))
            elif method == 's':
                options.suffix = specified_suffix if specified_suffix != '' else ''
                detectors.append(
                    ('refine_single',
                     PlaneRCNNDetector(options,
                                       config,
                                       modelType='refine_single')))
            elif method == 'f':
                options.suffix = specified_suffix if specified_suffix != '' else ''
                detectors.append(('final',
                                  PlaneRCNNDetector(options,
                                                    config,
                                                    modelType='final')))
                pass
            continue
        pass

    if not options.debug:
        for method_name in [detector[0] for detector in detectors]:
            os.system('rm ' + options.test_dir + '/*_' + method_name + '.png')
            continue
        pass

    all_statistics = []
    for name, detector in detectors:
        statistics = [[], [], [], []]
        for sampleIndex, sample in enumerate(data_iterator):
            if options.testingIndex >= 0 and sampleIndex != options.testingIndex:
                if sampleIndex > options.testingIndex:
                    break
                continue
            input_pair = []
            camera = sample[30][0].cuda()
            for indexOffset in [
                    0,
            ]:
                images, image_metas, rpn_match, rpn_bbox, gt_class_ids, gt_boxes, gt_masks, gt_parameters, gt_depth, extrinsics, planes, gt_segmentation = sample[
                    indexOffset +
                    0].cuda(), sample[indexOffset + 1].numpy(), sample[
                        indexOffset +
                        2].cuda(), sample[indexOffset + 3].cuda(), sample[
                            indexOffset +
                            4].cuda(), sample[indexOffset + 5].cuda(), sample[
                                indexOffset +
                                6].cuda(), sample[indexOffset + 7].cuda(
                                ), sample[indexOffset + 8].cuda(), sample[
                                    indexOffset + 9].cuda(), sample[
                                        indexOffset +
                                        10].cuda(), sample[indexOffset +
                                                           11].cuda()

                masks = (
                    gt_segmentation == torch.arange(gt_segmentation.max() +
                                                    1).cuda().view(-1, 1,
                                                                   1)).float()
                input_pair.append({
                    'image': images,
                    'depth': gt_depth,
                    'bbox': gt_boxes,
                    'extrinsics': extrinsics,
                    'segmentation': gt_segmentation,
                    'camera': camera,
                    'plane': planes[0],
                    'masks': masks,
                    'mask': gt_masks
                })
                continue

            if sampleIndex >= options.numTestingImages:
                break

            with torch.no_grad():
                detection_pair = detector.detect(sample)
                pass

            if options.dataset == 'rob':
                depth = detection_pair[0]['depth'].squeeze().detach().cpu(
                ).numpy()
                os.system('rm ' +
                          image_list[sampleIndex].replace('color', 'depth'))
                depth_rounded = np.round(depth * 256)
                depth_rounded[np.logical_or(depth_rounded < 0,
                                            depth_rounded >= 256 * 256)] = 0
                cv2.imwrite(
                    image_list[sampleIndex].replace('color', 'depth').replace(
                        'jpg', 'png'), depth_rounded.astype(np.uint16))
                continue

            if 'inference' not in options.dataset:
                for c in range(len(input_pair)):
                    evaluateBatchDetection(
                        options,
                        config,
                        input_pair[c],
                        detection_pair[c],
                        statistics=statistics,
                        printInfo=options.debug,
                        evaluate_plane=options.dataset == '')
                    continue
            else:
                for c in range(len(detection_pair)):
                    np.save(
                        options.test_dir + '/' + str(sampleIndex % 500) +
                        '_plane_parameters_' + str(c) + '.npy',
                        detection_pair[c]['detection'][:, 6:9])
                    np.save(
                        options.test_dir + '/' + str(sampleIndex % 500) +
                        '_plane_masks_' + str(c) + '.npy',
                        detection_pair[c]['masks'][:, 80:560])
                    continue
                pass

            if sampleIndex < 30 or options.debug or options.dataset != '':
                visualizeBatchPair(options,
                                   config,
                                   input_pair,
                                   detection_pair,
                                   indexOffset=sampleIndex % 500,
                                   suffix='_' + name + options.modelType,
                                   write_ply=options.testingIndex >= 0,
                                   write_new_view=options.testingIndex >= 0
                                   and 'occlusion' in options.suffix)
                pass
            if sampleIndex >= options.numTestingImages:
                break
            continue
        if 'inference' not in options.dataset:
            options.keyname = name
            printStatisticsDetection(options, statistics)
            all_statistics.append(statistics)
            pass
        continue
    if 'inference' not in options.dataset:
        if options.debug and len(detectors) > 1:
            all_statistics = np.concatenate([
                np.arange(len(all_statistics[0][0])).reshape((-1, 1)),
            ] + [np.array(statistics[3]) for statistics in all_statistics],
                                            axis=-1)
            print(all_statistics.astype(np.int32))
            pass
        if options.testingIndex == -1:
            np.save('logs/all_statistics.npy', all_statistics)
            pass
        pass
    return
# Root directory of the project
ROOT_DIR = os.path.abspath("../")
sys.path.append(ROOT_DIR)  # To find local version of the library

# Logging confg
logging.basicConfig(level=logging.DEBUG,handlers=[
        logging.FileHandler("{0}/{1}.log".format("/logs", "classifierservice-flask")),
        logging.StreamHandler()])

############################################################
#  Configurations
#  Inherits from config.py
############################################################
from config import InferenceConfig
config = InferenceConfig()

# Create model object in inference mode.
module = __import__(config.MODEL_MODULE, fromlist=[config.MODEL_CLASS])
my_class = getattr(module,config.MODEL_CLASS)
model = my_class(config)

#Make a prediction before starting the server (First prediction takes longer)
data=config.MODEL_SAMPLE_INPUT 
classification=model.predict(data)
logging.info('Model and weight have been loaded.')


def run_infer_content(data):
    #logging.info('Load data: %s',data)
    if isinstance(data,str):
Пример #6
0
###############################################################################
# Load image
###############################################################################

im_path = str(TRAIN_IMAGES_DIR / args.image)
print(f"Running on image '{args.image}'.")
original_im = cv2.imread(im_path)
original_im = cv2.cvtColor(original_im, cv2.COLOR_BGR2RGB)
im = original_im.copy()

###############################################################################
# Detection
###############################################################################

steel_config = InferenceConfig()

model = MaskRCNN(mode="inference",
                 config=steel_config,
                 model_dir=str(MODEL_DIR))

# Run the detection pipeline
# images: List of images, potentially of different sizes.
# Returns a list of dicts, one dict per image. The dict contains:
#     rois      : [N, (y1, x1, y2, x2)] detection bounding boxes
#     class_ids : [N] int class IDs
#     scores    : [N] float probability scores for the class IDs
#     masks     : [H, W, N] instance binary masks
results = model.detect(images=[im], verbose=1)
r = results[0]
Пример #7
0
 def __init__(self):
     super().__init__()
     model_config = InferenceConfig()
     self.preprocess_obj = ForwardModel(model_config)
Пример #8
0
 def __init__(self, config=None):
     if config == None:
         config = InferenceConfig()