class ConfigTest(unittest.TestCase): def setUp(self): self.config = Config() def test_config_values(self): self.assertEqual(self.config.GPU_COUNT, 1) self.assertEqual(self.config.IMAGES_PER_GPU, 2) self.assertEqual(self.config.STEPS_PER_EPOCH, 1000) self.assertEqual(self.config.BACKBONE, "resnet101") def test_config_value_types(self): self.assertIsInstance(self.config.BACKBONE, str) self.assertIsInstance(self.config.BACKBONE_STRIDES, list) self.assertIsInstance(self.config.MEAN_PIXEL, np.ndarray) self.assertIsInstance(self.config.LOSS_WEIGHTS, dict) self.assertIsInstance(self.config.USE_RPN_ROIS, bool) self.assertIsInstance(self.config.TRAIN_BN, bool) self.assertIsInstance(self.config.IMAGE_META_SIZE, int) def test_config_value_shapes(self): self.assertEqual(len(self.config.BACKBONE_STRIDES), 5) self.assertEqual(len(self.config.MINI_MASK_SHAPE), 2) self.assertEqual(self.config.MEAN_PIXEL.shape, (3, )) self.assertEqual(self.config.IMAGE_SHAPE.shape, (3, )) def test_config_value_calc(self): self.assertEqual(self.config.BATCH_SIZE, self.config.IMAGES_PER_GPU * self.config.GPU_COUNT) def test_display_func(self): capturedOutput = io.StringIO() sys.stdout = capturedOutput self.config.display() self.assertTrue('Configurations:' in capturedOutput.getvalue())
def from_config_file(config_file): c = Config() config = configparser.ConfigParser() config.read(config_file) for k, v in config['mask_rcnn'].items(): setattr(c, k.upper(), literal_eval(v)) c.__init__() return c
def __init__(self, channel = 3): assert channel == 1 or channel == 3 or channel == 4, "The channel must be 1, 3 or 4! Given: {}".format(channel) self.IMAGE_CHANNEL_COUNT = channel if channel == 1 or channel == 3: return elif channel == 4: self.MEAN_PIXEL = np.append(self.MEAN_PIXEL, 10) #elif channel == 1: #self.MEAN_PIXEL = [np.sum(self.MEAN_PIXEL) / 3] Config.__init__(self)
def __init__(self, dataset_name, classnames): # Give the configuration a recognizable name self.dataset_name = dataset_name self.NAME = dataset_name self.CLASS_NAMES = classnames self.ALL_CLASS_NAMES = ['BG'] + self.CLASS_NAMES # Number of classes (including background) self.NUM_CLASSES = len(self.ALL_CLASS_NAMES) self.map_name_to_id = {} Config.__init__(self)
def load_config_obj(path): with open(path, 'rb') as fid: dic = pickle.load(fid) # convert dictionary to a config class obj = Config() for var, val in dic.items(): setattr(obj, var, val) return obj
def __init__(self, dataset_path, dataset_name): super(RCNNDetector, self).__init__(dataset_path, dataset_name) self.train_set = ClassDataset() self.test_set = ClassDataset() # self.train_set = KangarooDataset() # self.test_set = KangarooDataset() self.model = "rcnn" self.modelWeights = None self.config = Config()
def test_config_to_dict(): config_dict = Config().to_dict() assert config_dict["BATCH_SIZE"] == 2 assert config_dict["LOSS_WEIGHTS"] == \ {'rpn_class_loss': 1.0, 'rpn_bbox_loss': 1.0, 'mrcnn_class_loss': 1.0, 'mrcnn_bbox_loss': 1.0, 'mrcnn_mask_loss': 1.0} assert config_dict["MEAN_PIXEL"].all() == np.array([123.7, 116.8, 103.9]).all()
def load_network(self): config = Config() config.NAME = 'predict' config.NUM_CLASSES = 1 + 1 config.IMAGES_PER_GPU = 1 config.GPU_COUNT = 1 additional_info = json.load(open(self.config_path)) for i,j in additional_info.items(): try: setattr(config,i,eval(j)) except: setattr(config,i,j) config.__init__() from mrcnn import model as modellib self.model = modellib.MaskRCNN(mode="inference", model_dir='./',config=config)
def __init__(self, batch_size): Config.__init__(self) self.BATCH_SIZE = batch_size
def __init__(self, taxonomy): # Number of classes (including background) self.NUM_CLASSES = 1 + len(taxonomy) # Background + objects Config.__init__(self) # run __init__ from Config
required=False, default=5e-4, help="How much to decay the learning rate.") parser.add_argument("--vgg", required=False, default=None, help="Pretrained vgg16 weight path.") args = parser.parse_args() model_name = args.model TRAIN_BATCH = args.train_batch VAL_BATCH = args.val_batch lr_init = args.lr_init lr_decay = args.lr_decay vgg_path = args.vgg config = Config() labels = 2 if model_name == "fcn": model = fcn_8s(input_shape=(256, 256, 7), num_classes=labels, lr_init=lr_init, lr_decay=lr_decay, vgg_weight_path=vgg_path) elif model_name == "unet": model = unet(input_shape=(256, 256, 7), num_classes=labels, lr_init=lr_init, lr_decay=lr_decay, vgg_weight_path=vgg_path) elif model_name == "pspnet": model = pspnet50(input_shape=(256, 256, 7),
def extractBboxFromTrainingData(bbox_conf_threshold = 0.9, tp_dir='bbox_patch_data/TP_bbox', fp_dir='../Input/bbox_patch_data/FP_bbox'): # To extract ground truth bbox patches and false positive patches bbox # to train a post-classifier on bbox detected by mask-rcnn # save image im original patched size (to be resized when training) # retreive path to model and config class model_path = 'pneumonia20181020T2012_0165/mask_rcnn_pneumonia_0032.h5' # find_lastest_model(ROOT_DIR) # retrieve DetectorConfig from pickle file bbox_conf_threshold = 0.95 class inference_config(Config): NAME = 'pneumonia' GPU_COUNT = 1 IMAGES_PER_GPU = 1 BACKBONE = "resnet50" # 'resnet50' NUM_CLASSES = 2 IMAGE_MAX_DIM = 256 IMAGE_MIN_DIM = 256 RPN_ANCHOR_SCALES = (32, 64, 128, 256) TRAIN_ROIS_PER_IMAGE = 32 MAX_GT_INSTANCES = 4 DETECTION_MAX_INSTANCES = 3 DETECTION_MIN_CONFIDENCE = 0.78 DETECTION_NMS_THRESHOLD = 0.01 inference_config = inference_config() config = Config() config.IMAGE_MIN_DIM = ORIG_SIZE config.IMAGE_MAX_DIM = ORIG_SIZE # Recreate the model in inference mode model = modellib.MaskRCNN(mode='inference', config=inference_config, model_dir=ROOT_DIR) resize_factor = ORIG_SIZE / inference_config.IMAGE_SHAPE[0] # Load trained weights (fill in path to trained weights here) print('Retrieving mask RCNN...') assert model_path != "", "Provide path to trained weights for mask RCNN" print("Loading weights from ", model_path) model.load_weights(model_path, by_name=True) #read data from pkl file dataset = read_training_data_from_pkl() from scipy import misc for image_id in dataset.image_ids: print(image_id) original_image, image_meta, gt_class_id, gt_bbox, gt_mask = \ modellib.load_image_gt(dataset, config, image_id, use_mini_mask=False) resize_image, resize_image_meta, resize_gt_class_id, resize_gt_bbox, resize_gt_mask = \ modellib.load_image_gt(dataset, inference_config, image_id, use_mini_mask=False) # extract patch from ground truth if len(gt_bbox): continue # num_instances = len(gt_bbox) # for i in range(num_instances): # x1 = gt_bbox[i][1] #top left # y1 = gt_bbox[i][0] # width = gt_bbox[i][3] - x1 #bottom righ # height = gt_bbox[i][2] - y1 # masked = np.zeros((height, width)) # #print(x1, y1, width, height) # masked = np.array(original_image[y1:y1+height, x1:x1+width, 0], dtype=np.int32) # file_name = os.path.join(tp_dir, str(image_id) + '_' + str(i) + '_GT.png') # misc.imsave(file_name, masked) else: # extract patch from false postive result results = model.detect([resize_image]) # , verbose=1) r = results[0] index2remove = r['scores'] <= bbox_conf_threshold if len(index2remove[index2remove == True]) == len(r['scores']): continue # no bbox in prediction num_instances = len(r['rois']) for i in range(num_instances): if r['scores'][i] > bbox_conf_threshold: x1 = r['rois'][i][1] y1 = r['rois'][i][0] width = int((r['rois'][i][3] - x1) * resize_factor) height = int((r['rois'][i][2] - y1) * resize_factor) x1 = int(x1 * resize_factor) y1 = int(y1 * resize_factor) masked = np.array(original_image[y1:y1+height, x1:x1+width, 0], dtype=np.int32) file_name = os.path.join(fp_dir, str(image_id) + '_' + str(i) + '_FP.png') misc.imsave(file_name, masked)
def setUp(self): self.config = Config()
router_train = DatasetXML() router_train.load_dataset(dataset_dir, label, train_imgs) router_train.prepare() print('Train: %d' % len(router_train.image_ids)) router_test = DatasetXML() router_test.load_dataset(dataset_dir, label, test_imgs) router_test.prepare() print('Test: %d' % len(router_test.image_ids)) else: # pkl format file_name = 'data_val2017-laptop' pkldata = pkl.load(open(os.path.join(dataset_dir, file_name+'.pkl'), 'rb')) # =============== train models =============== # prepare config config = Config() config.NAME = label + "_cfg" # Give the configuration a recognizable name config.STEPS_PER_EPOCH = n_steps # Number of training steps per epoch config.NUM_CLASSES = 1 + 1 # Number of classes (background + router) model = MaskRCNN(mode='training', model_dir=model_root_dir, config=config) model.load_weights(model_dir, by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"]) model.train(router_train, router_test, learning_rate=config.LEARNING_RATE, epochs=n_epochs, layers='heads')
def __init__(self,num_class): Config.__init__(self,num_class)
def test_config_display(capsys): config = Config() config.display() captured = capsys.readouterr() assert "LEARNING_MOMENTUM 0.9" in captured.out
default=DEFAULT_LOGS_DIR, metavar="/path/to/logs/", help='Logs and checkpoints directory (default=logs/)') args = parser.parse_args() # Validate arguments if args.command == "train": assert args.dataset, "Argument --dataset is required for training" print("Weights: ", args.weights) print("Dataset: ", args.dataset) print("Logs: ", args.logs) # Configurations if args.command == "train": config = Config() else: class InferenceConfig(Config): # Set batch size to 1 since we'll be running inference on # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU GPU_COUNT = 1 IMAGES_PER_GPU = 1 config = InferenceConfig() config.display() # Create model if args.command == "train": model = modellib.MaskRCNN(mode="training", config=config, model_dir=args.logs) else: model = modellib.MaskRCNN(mode="inference", config=config,
def __init__(self, class_names): self.NUM_CLASSES = len(class_names) Config.__init__(self)