def create_model(gpu_num=1, images_per_gpu=1, detection_confidence=0.5, detection_nms_threshold=0.3): ''' Creates the Mask-RCNN model trained on COCO. :param gpu_num: The number of GPUs to use :param images_per_gpu: The number of images to process per GPU. :return: the loaded model. ''' class InferenceConfig(coco.CocoConfig): # 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 = gpu_num IMAGES_PER_GPU = images_per_gpu DETECTION_MIN_CONFIDENCE = detection_confidence DETECTION_NMS_THRESHOLD = detection_nms_threshold config = InferenceConfig() # Create model object in inference mode. model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) # Load weights trained on MS-COCO model.load_weights(COCO_MODEL_PATH, by_name=True) return model
def load_maskrcnn_model(): """ load maskrcnn nucleus model, and config the settings :return: """ print("define the mask RCNN configuration!") config = nucleus_train.NucleusInferenceConfig() config.BACKBONE = "resnet101" config.DETECTION_MAX_INSTANCES = 2000 config.POST_NMS_ROIS_INFERENCE = 6000 config.RPN_NMS_THRESHOLD = 0.7 config.DETECTION_NMS_THRESHOLD = 0.3 config.BATCH_SIZE = 1 * GPU_COUNT #2 # the para indicate that you want to prediction image numbers everytime config.IMAGES_PER_GPU = 1 # 2 #every GPU can process how many image config.GPU_COUNT = GPU_COUNT # The batch_size was calculated by GPU_COUNT * IMAGES_PER_GPU config.display() # DEVICE = "/gpu:0" # /cpu:0 or /gpu:0 # TEST_MODE = "inference" print("load mask RCNN model and network weight!") print("Loading weights from ", MASK_RCNN_SEG_MODEL) # with tf.device(DEVICE): model = modellib.MaskRCNN(mode="inference", model_dir=LOGS_DIR, config=config) model.load_weights(MASK_RCNN_SEG_MODEL, by_name=True) print("mask RCNN load complete!") return model
def __init__(self, img_size=None, threshold=None, gpu_count=None, images_per_gpu=None): ignore_warnings() # Configuration self.MODEL_DIR = "Source/mask_rcnn_fashion_0006.h5" self.LABEL_DIR = "Source/label_descriptions.json" self.MASK_DIR = "Mask_RCNN" self.NUM_CATS = 46 if img_size is None: self.IMAGE_SIZE = 512 else: self.IMAGE_SIZE = img_size with open(self.LABEL_DIR) as f: self.label_descriptions = json.load(f) self.label_names = [ x['name'] for x in self.label_descriptions['categories'] ] # Setup Configuration class InferenceConfig(Config): NAME = "fashion" NUM_CLASSES = self.NUM_CATS + 1 # +1 for the background class BACKBONE = 'resnet101' IMAGE_MIN_DIM = self.IMAGE_SIZE IMAGE_MAX_DIM = self.IMAGE_SIZE IMAGE_RESIZE_MODE = 'none' RPN_ANCHOR_SCALES = (16, 32, 64, 128, 256) if threshold is None: DETECTION_MIN_CONFIDENCE = 0.7 else: DETECTION_MIN_CONFIDENCE = threshold if gpu_count is None: GPU_COUNT = 1 else: GPU_COUNT = gpu_count if images_per_gpu is None: IMAGES_PER_GPU = 1 else: IMAGES_PER_GPU = images_per_gpu # Execute Inference Configuration self.inference_config = InferenceConfig() self.model = modellib.MaskRCNN(mode='inference', config=self.inference_config, model_dir=self.MASK_DIR) self.model.load_weights(self.MODEL_DIR, by_name=True)
def __init__(self, dict_para): # Checking that weights_path is valid assert pathlib.Path(dict_para["weights_path"]).is_file() is True, "Mask-RCNN - Invalid Weights Path" # If valid parameters, store and continue self.dict_para = dict_para # Mask-RCNN Setup config = InferenceConfig() config.display() # Loading Mask-RCNN Model self.model = modellib.MaskRCNN(mode="inference", config=config, model_dir=gv.DEFAULT_LOGS_DIR) self.model.load_weights(dict_para["weights_path"], by_name=True) return None
def __init__(self) -> None: # Root directory of the project root_dir = os.path.abspath("./Mask_RCNN") # Import Mask RCNN sys.path.append(root_dir) # To find local version of the library # Import COCO config sys.path.append(os.path.join(root_dir, "samples/coco/")) # To find local version # Directory to save logs and trained model model_dir = os.path.join(root_dir, "logs") # Local path to trained weights file coco_model_path = os.path.join(root_dir, "mask_rcnn_coco.h5") # Download COCO trained weights from Releases if needed if not os.path.exists(coco_model_path): utils.download_trained_weights(coco_model_path) # Create model object in inference mode. self.model = modellib.MaskRCNN(mode="inference", model_dir=model_dir, config=config) # Load weights trained on MS-COCO self.model.load_weights(coco_model_path, by_name=True) self.frames = []
def fit(self): with tf.device(self.DEVICE): model = modelib.MaskRCNN(mode="inference", model_dir=self.MODEL_DIR, config=self.config) # Load weights self.weights_path = self.CARPLATE_WEIGHTS_PATH print("Loading weights", self.weights_path) model.load_weights(self.weights_path, by_name=True) # Run Detection 这里为模型标注数据 # print(self.image.shape[:2]) self.image = modelib.load_image_gt(self.image, self.config) # print(self.image.shape[:2]) # 模型预测功能 self.results = model.detect([self.image], verbose=0) self.r = self.results[0] # 1.提取车牌区域,只取第一个车牌 mask = self.r['masks'][:, :, 0].astype(np.uint8) _, contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) try: cnt = contours[0] except Exception: self.flag = False print("Model Location Error") return None epsilon = 0.1 * cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, epsilon, True) approx = approx.squeeze() if approx.shape == (4, 2): self.box = np.zeros_like(approx) self.box[:, 0] = approx[:, 1] self.box[:, 1] = approx[:, 0] else: rect = cv2.minAreaRect(np.array(np.nonzero(mask)).T) self.box = cv2.boxPoints(rect).astype(np.int)
assert args.dataset, "Argument --dataset is required for training" elif args.command == "inference": assert True print("Weights: ", args.weights) print("Dataset: ", args.dataset) print("Logs: ", args.logs) # Configurations config = MalariaConfig( ) if args.command == "train" else MalariaInferenceConfig() config.display() # Create model model = modellib.MaskRCNN( mode="training" if args.command == "train" else "inference", config=config, model_dir=args.logs) # Select weights file to load if args.weights.lower() == "coco": weights_path = COCO_WEIGHTS_PATH # Download weights file if not os.path.exists(weights_path): utils.download_trained_weights(weights_path) elif args.weights.lower() == "imagenet": # Start from ImageNet trained weights weights_path = model.get_imagenet_weights() elif args.weights.lower() == "last": # Find last trained weights weights_path = model.find_last() else:
# Training dataset dataset_train = mrcnn_configs.NucleusDataset(config=config_train) dataset_train.load_nucleus(subset="train") dataset_train.prepare() # Validation dataset dataset_val = mrcnn_configs.NucleusDataset(config=config_train) dataset_val.load_nucleus(subset="val") dataset_val.prepare() # ================================================================= # Create model # Create model in training mode model = modellib.MaskRCNN(mode="training", config=config_train, model_dir=MODEL_DIR) if init_with == "imagenet": model.load_weights(model.get_imagenet_weights(), by_name=True) elif init_with == "coco": # Load weights trained on MS COCO, but skip layers that # are different due to the different number of classes # See README for instructions to download the COCO weights model.load_weights(COCO_MODEL_PATH, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask" ])
# DEFAULT_LOG_FILE = '/home/amank/Documents/logs_file/logs_lochan' # DEFAULT_WEIGHT_FILE_PATH = '/home/amank/Documents/weight_files/instance_segmentation/aadhaar_model_v1.h5' # DEFAULT_API_NAME = '/aadhaar_extraction' # ITEM_OF_INTEREST will be prediction item returned by the API # Except for 'masks', everthing else returns a numpy array converted to list # In case of the mask, all the images extracted based on the masks by the algorithm are returned in base64 format ITEM_OF_INTEREST = 'masks' DEVICE_NAME = '/cpu:0' config_file = InferenceConfig() transformer = perspective_transform.transform() with tf.device(config_file.DEVICE_NAME): model = modellib.MaskRCNN(mode="inference", config=config_file, model_dir=config_file.DEFAULT_LOG_FILE) model.load_weights(config_file.DEFAULT_WEIGHT_FILE_PATH, by_name=True) model.keras_model._make_predict_function() def base64_to_skimage(base64_encoded_image): # TODO: Ensure that the b and quotes have been removed from the string-fied image encoding # if isinstance(base64_encoded_image, bytes): # base64_encoded_image = base64_encoded_image.decode("utf-8") # Note: Image string might contain the extra b and single_quotes which have to be # removed before proceding with decoding otherwise decoding will fail imgdata = base64.b64decode(base64_encoded_image) img = skimage.io.imread(imgdata, plugin='imageio') return img
metavar="/path/to/logs/", help='Logs and checkpoints directory (default=/projects/lungbox/models)' ) args = parser.parse_args() # Set up training data elapsed_start = time.perf_counter() data = TrainingData(subset_size=int(args.subset_size), validation_split=float(args.validation_split)) elapsed_end = time.perf_counter() logger.info('Data Setup Time: %ss' % round(elapsed_end - elapsed_start, 4)) # Set up Mask R-CNN model elapsed_start = time.perf_counter() model = modellib.MaskRCNN(mode='training', config=DetectorConfig(), model_dir=args.model_dir) elapsed_end = time.perf_counter() logger.info('Model Setup Time: %ss' % round(elapsed_end - elapsed_start, 4)) # Add image augmentation params if args.use_augmentation: augmentation = iaa.SomeOf( (0, 5), [ # iaa.Fliplr(0.5), # crop some of the images by 0-10% of their height/width iaa.Crop(percent=(0, 0.1)),
# ================================================================== # Prep for maskRCNN inference # Inference Configuration config_inference = mrcnn_configs.NucleusConfig( is_training= False, verbose=verbose) if verbose: config_inference.display() # load dataset dataset = mrcnn_configs.NucleusDataset(config= config_inference) dataset.load_nucleus(specific_ids= image_ids) dataset.prepare() # Create model in inference mode model = modellib.MaskRCNN( mode= "inference", model_dir= logs_dir, config= config_inference) # Load weights if verbose: print("Loading weights ", model_weights_path) model.load_weights(model_weights_path, by_name=True) # Get bounds for which images go to what batch (just the bounds) n_images_tot = len(dataset.image_ids) idx_bounds = list(np.arange(0, n_images_tot, config_inference.BATCH_SIZE)) if idx_bounds[-1] != n_images_tot: idx_bounds.append(n_images_tot) # ================================================================== # Now go through batches and do inference
config.display() if args.command == "train": command_mode = "training" else: command_mode = "inference" # Create model if args.use_pa: model = panetmodellib.PAMaskRCNN(mode=command_mode, config=config, model_dir=MODEL_DIR) else: model = modellib.MaskRCNN(mode=command_mode, config=config, model_dir=MODEL_DIR) # Select weights file to load if args.model.lower() == "voc": model_path = VOC_MODEL_PATH elif args.model.lower() == "last": # Find last trained weights model_path = model.find_last() elif args.model.lower() == "imagenet": # Start from ImageNet trained weights model_path = model.get_imagenet_weights() else: model_path = args.model # Load weights print("Loading weights ", model_path)
import tensorflow as tf print(tf.__version__) # docker: nvcr.io/nvidia/tensorflow:19.05-py3 -->> 1.13.1 # Nvidia DGX workstation # time tracking [start, head training, fine-tune] time_tracker = [time.time()] # Load configuration config = config.CustomConfigCOCO() # datasets # TODO: define split ratio in config train_dataset, valid_dataset = dataHandling.MakeDatasets(config) model = modellib.MaskRCNN(mode='training', config=config, model_dir=str(config.ROOT_DIR)) model.load_weights(config.WEIGHTS_PATH, by_name=True, exclude=[ 'mrcnn_class_logits', 'mrcnn_bbox_fc', 'mrcnn_bbox', 'mrcnn_mask']) # Step 1 train heads augmentation = iaa.Sequential([ iaa.Fliplr(0.5), # only horizontal flip here iaa.Crop(percent=(0, 0.1)), iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.25)) ), iaa.Affine( scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, rotate=(-25, 25),
default=GlobalConfig.get('MODEL_DIR') + GlobalConfig.get('LATEST_MODEL'), metavar="/path/to/logs/", help='Path to the Keras model') parser.add_argument('--model_dir', required=False, default=GlobalConfig.get('MODEL_DIR'), metavar="/path/to/logs/", help='Logs and checkpoints directory (default=/projects/lungbox/models)') args = parser.parse_args() # Read the image elapsed_start = time.perf_counter() image = pydicom.read_file(args.image_path) elapsed_end = time.perf_counter() logger.info('Data Setup Time: %ss' % round(elapsed_end - elapsed_start, 4)) # Set up Mask R-CNN model elapsed_start = time.perf_counter() infer_config = InferenceConfig() model = modellib.MaskRCNN(mode='training', config=infer_config, model_dir=args.model_dir) model.load_weights(args.h5_path, by_name=True) elapsed_end = time.perf_counter() logger.info('Model Setup Time: %ss' % round(elapsed_end - elapsed_start, 4)) # Run inference! elapsed_start = time.perf_counter() r = model.detect([image], verbose=False)[0] elapsed_end = time.perf_counter() logger.info('Training Time: %ss' % round(elapsed_end - elapsed_start, 4))
def detectModel(): model = modellib.MaskRCNN(mode='training', config=config)
else: class InferenceConfig(CocoConfig): # 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 DETECTION_MIN_CONFIDENCE = 0 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, model_dir=args.logs) # Select weights file to load if args.model.lower() == "coco": model_path = COCO_MODEL_PATH elif args.model.lower() == "last": # Find last trained weights model_path = model.find_last()[1] elif args.model.lower() == "imagenet": # Start from ImageNet trained weights model_path = model.get_imagenet_weights()
if not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH) # Directory of images to run detection on IMAGE_DIR = os.path.join(ROOT_DIR, "images") class InferenceConfig(coco.CocoConfig): # 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() model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) model.load_weights(COCO_MODEL_PATH, by_name=True) # read the input RGB image images = sio.loadmat('./dataset/birds/birds_2_views.mat') count = 0 for image_name in images['view_1']: image_name = image_name.strip() image = skimage.io.imread(image_name) results = model.detect([image], verbose=0) load_masked_image(image, results[0]['masks'], image_name) count += 1 print(count) # check if there are three segments for all images for i in range(3): if not os.path.exists('{}_{seg}.jpg'.format(image_name[:-4], seg=i)):