def load_model(ROOT_DIR, device = 'GPU'): # return 'abc', [] device_scope = '/{}:0'.format(device.lower()) with tf.device(device_scope): MODEL_DIR = os.path.join(ROOT_DIR, 'demo', 'model', 'logs') class InferenceConfig(mapillary.MapillaryConfig): GPU_COUNT = 1 IMAGES_PER_GPU = 1 inference_config = InferenceConfig() print (' - Batch Size : ', inference_config.BATCH_SIZE) model = modellib.MaskRCNN(mode="inference", config=inference_config, model_dir=MODEL_DIR) # model_path = model.find_last()[1] model_path_base = '/home/play/playment/production/Mask_RCNN/demo/model/logs/mapillary20180315T0317/' models = ['mask_rcnn_mapillary_0196.h5','mask_rcnn_mapillary_0158.h5', 'mask_rcnn_mapillary_0083.h5'] model_path = os.path.join(model_path_base, models[0]) print (' - Model Path : ', model_path) if model_path != None: model.load_weights(model_path, by_name=True) return model, inference_config else: sys.exit(1) return []
config = HarzConfig() else: class InferenceConfig(HarzConfig): # 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, model_dir=args.logs) if args.weights and args.weights.lower() == "last": # Find last trained weights print("Loading model from previous weight file!") weights_path = model.find_last() model.load_weights(weights_path, by_name=True) # Select weights file to load # if args.weights.lower() == "coco": # weights_path = COCO_WEIGHTS_PATH
class InferenceConfig(MyConfig): GPU_COUNT = 1 IMAGES_PER_GPU = 1 DETECTION_MIN_CONFIDENCE = 0.5 config = InferenceConfig() config.display() for k, v in vars(args).items(): setattr(config, k, getattr(args, k)) config.display() # Create model if args.command == "train": model = modellib.MaskRCNN(mode="training", config=config) else: model = modellib.MaskRCNN(mode="inference", config=config) if config.weights: # Find last trained weights print("Loading model from previous weight file!") if config.weights == "last": weights_path = model.find_last() model.load_weights(weights_path, by_name=True) else: model.load_weights(config.weights, by_name=True) # Train or evaluate if args.command == "train": train(model, config)
if 'src.model' in sys.modules : del sys.modules['src.model'] if 'src.utils' in sys.modules : del sys.modules['src.utils'] if 'src.parallel_model' in sys.modules : del sys.modules['src.parallel_model'] import src.utils as utils import src.model as modellib import src.parallel_model as parallel_model if 'keras.' in sys.modules : del sys.modules['keras'] import keras MODEL_DIR = os.path.join(ROOT_DIR, 'demo', 'model', 'logs') COCO_MODEL_PATH = os.path.join(ROOT_DIR, 'demo', 'model', "mask_rcnn_coco.h5") model = modellib.MaskRCNN(mode="training", config=mapillary_config, model_dir=MODEL_DIR) # model.keras_model = parallel_model.ParallelModel(model.keras_model, mapillary_config.GPU_COUNT, verbose = 0) # model.keras_model.summary() init_with = "coco" # imagenet, coco, or last # init_with = "last" # imagenet, coco, or last if init_with == "imagenet": model.load_weights(model.get_imagenet_weights(), by_name=True) elif init_with == "coco": model.load_weights(COCO_MODEL_PATH, by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"]) elif init_with == "last": model_path = model.find_last()[1] print (' - Path : ', model_path)
all visualizations in the notebook. Provide a central point to control graph sizes. Change the default size attribute to control the size of rendered images """ _, ax = plt.subplots(rows, cols, figsize=(size * cols, size * rows)) return ax inference_config = InferenceConfig() inference_config.display() # Recreate the model in inference mode model = modellib.MaskRCNN(mode="inference", config=inference_config, model_dir=MODEL_DIR) # Get path to saved weights # Either set a specific path or find last trained weights # model_path = os.path.join(ROOT_DIR, ".h5 file name here") model_path = model.find_last()[1] # 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) # Validation dataset dataset_val = CellsDataset() # dataset_val.load_cells(IMAGES_DIR, config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1])
# dataset_val.load_cells(IMAGES_DIR, config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1]) dataset_val.load_cells(IMAGES_DIR) dataset_val.prepare() # Load and display random samples image_ids = np.random.choice(dataset_train.image_ids, 1) print(image_ids) for image_id in image_ids: image = dataset_train.load_image(image_id) mask, class_ids = dataset_train.load_mask(image_id) visualize.display_top_masks(image, mask, class_ids, dataset_train.class_names) # Create model in training mode model = modellib.MaskRCNN(mode="training", config=config, model_dir=MODEL_DIR) # from keras.utils import plot_model # plot_model(model, to_file='model_training_mode.png', show_shapes=True) # Which weights to start with? init_with = "imagenet" # imagenet, coco, or last 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,