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 = []
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: weights_path = args.weights # Load weights print("Loading weights ", weights_path) if args.weights.lower() == "coco": # Exclude the last layers because they require a matching # number of classes model.load_weights(weights_path,
def func(filepath,filename): # Root directory of the project ROOT_DIR = os.path.abspath("../") # Import Mask RCNN sys.path.append(ROOT_DIR) # To find local version of the library from Mask_RCNN.mrcnn import utils import Mask_RCNN.mrcnn.model as modellib from Mask_RCNN.mrcnn import visualize # Import COCO config sys.path.append(os.path.join(ROOT_DIR, "samples/coco/")) # To find local version from Mask_RCNN.samples.coco.coco import CocoConfig # 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) # Directory of images to run detection on IMAGE_DIR = os.path.join(ROOT_DIR, "images") 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 config = InferenceConfig() config.display() # Create model object in inference mode. model = modellib.Mask_RCNN(mode="inference", model_dir=MODEL_DIR, config=config) # Load weights trained on MS-COCO model.load_weights(COCO_MODEL_PATH, by_name=True) # COCO Class names # Index of the class in the list is its ID. For example, to get ID of # the teddy bear class, use: class_names.index('teddy bear') class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'] # Load a random image from the images folder # file_names = next(os.walk(IMAGE_DIR))[2] image = skimage.io.imread(filepath)#os.path.join(IMAGE_DIR, random.choice(file_names))) # Run detection results = model.detect([image], verbose=1) # Visualize results r = results[0] visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'],filename=filename) # func()
import os import Mask_RCNN.mrcnn.model as modellib from Mask_RCNN.mrcnn import utils # Download COCO trained weights from Releases if needed COCO_MODEL_PATH = os.path.join('..', '..', 'Mask_RCNN', "mask_rcnn_coco.h5") if not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH) import algorithmic_suggestions.configs_for_AlgorithmicSuggestions_MaskRCNN as mrcnn_configs # ================================================================= # Params # Directory to save logs and trained model MODEL_DIR = "/home/mohamedt/Desktop/WSI_Segmentation/Models/TCGA_maskrcnn/19July2018/" # Which weights to start with? init_with = "weight_file" # imagenet, coco, last, or weight_file if init_with == "weight_file": model_to_use = "nucleus20180720T1413" model_epoch = "mask_rcnn_nucleus_0022.h5" model_weights_path = "/home/mohamedt/Desktop/WSI_Segmentation/Models/TCGA_maskrcnn/19July2018/%s/%s" % ( model_to_use, model_epoch) augmentation = None n_epochs = 50 # Configurations config_train = mrcnn_configs.NucleusConfig(is_training=True)