mask, class_ids, Train.class_names, limit=len(class_ids)) class InferenceConfig(RSNAConfig): GPU_COUNT = 1 IMAGES_PER_GPU = 1 inference_config = InferenceConfig() if 0: # Load trained weights # Recreate the model in inference mode model = modellib.MaskRCNN(mode="inference", config=inference_config, model_dir="./model") # 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() model.load_weights(model_path, by_name=True) iid = np.random.choice(Validate.image_ids) original_image, image_meta, gt_class_id, gt_bbox, gt_mask =\ modellib.load_image_gt(Validate, inference_config, iid, use_mini_mask=False) results = model.detect([original_image], verbose=1) r = results[0] log("original_image", original_image)
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()
def maskRCNNInferenceMRI(preTrainednet, maskBranchNet, testSetNdx): # preTrainednet='imagenet';testSetNdx=1;maskBranchNet='def'; crfEnabled = 1 morfFilteringEnabled = 0 TestSetNum = testSetNdx init_with = preTrainednet fileName = 'Mask_RCNN_testSet' + str( TestSetNum) + '_pretrained_' + init_with + '_maskNet_' + maskBranchNet address = "/fileserver/abd/marzieh/deepLearningModels/MaskRCNN/" + fileName + "/" MODEL_DIR = os.path.join(address) #MODEL_DIR = os.path.join(ROOT_DIR, "logs") class InferenceConfig(kidney.KidneysConfig): GPU_COUNT = 1 IMAGES_PER_GPU = 1 inference_config = InferenceConfig() inference_config.head = maskBranchNet model = modellib.MaskRCNN(mode="inference", config=inference_config, model_dir=MODEL_DIR) subjectNamesNormalTrain, subjectNamesNormalTest, trainKidCond, testKidCond = selectTrainAndTestSubjects( TestSetNum) txt_file = open(address + 'selectedEpoc.txt', 'r') selectedEpoch = str(int(txt_file.read()))[1:] model_path = [x[0] for x in os.walk(address) ][1] + '/mask_rcnn_kidneys_' + selectedEpoch + '.h5' model.load_weights(model_path, by_name=True) scores = [] columns = [ 'Name', 'kidney Condition', 'F1-Score', 'Prec', 'Rec', 'VEE', 'testSet', 'Model' ] index = np.arange(len(subjectNamesNormalTest)) df = pd.DataFrame(index=index, columns=columns) df = df.fillna(0) dfNd = 0 for s in subjectNamesNormalTest: # s=subjectNamesNormalTest[0]; dataset_val = kidney.KidneysDataset() isValidationData = 1 dataset_val.load_Kidneys([s], isValidationData) dataset_val.prepare() sliceIds = dataset_val.image_ids rightMask3Dorig = np.zeros((256, 256, len(sliceIds))) rightMask3Dpred = np.zeros((256, 256, len(sliceIds))) leftMask3Dorig = np.zeros((256, 256, len(sliceIds))) leftMask3Dpred = np.zeros((256, 256, len(sliceIds))) print(s) for sl in range(len(sliceIds)): image, image_meta, gt_class_id, gt_bbox, gt_mask =\ modellib.load_image_gt(dataset_val, inference_config, sliceIds[sl], use_mini_mask=False) # molded_images = np.expand_dims(modellib.mold_image(image, inference_config), 0) # Run object detection results = model.detect([image], verbose=0) # print(sl,results[0]['class_ids'],gt_class_id) if 1 in results[0]['class_ids'] and 2 in results[0]['class_ids']: rightNdx = np.where(results[0]['class_ids'] == 1)[0][0] leftNdx = np.where(results[0]['class_ids'] == 2)[0][0] rightMask3Dpred[:, :, sl] = results[0]['masks'][:, :, rightNdx] leftMask3Dpred[:, :, sl] = results[0]['masks'][:, :, leftNdx] elif 1 in results[0]['class_ids'] and 2 not in results[0][ 'class_ids']: rightMask3Dpred[:, :, sl] = results[0]['masks'][:, :, 0] elif 1 not in results[0]['class_ids'] and 2 in results[0][ 'class_ids']: leftMask3Dpred[:, :, sl] = results[0]['masks'][:, :, 0] if 1 in gt_class_id and 2 in gt_class_id: rightMask3Dorig[:, :, sl] = gt_mask[:, :, 0] leftMask3Dorig[:, :, sl] = gt_mask[:, :, 1] elif 1 in gt_class_id and 2 not in gt_class_id: rightMask3Dorig[:, :, sl] = gt_mask[:, :, 0] elif 1 not in gt_class_id and 2 in gt_class_id: leftMask3Dorig[:, :, sl] = gt_mask[:, :, 0] if crfEnabled: rightMask3DpredCRF = np.zeros((256, 256, len(sliceIds))) leftMask3DpredCRF = np.zeros((256, 256, len(sliceIds))) for ndx in range(leftMask3Dpred.shape[1]): rightMask3DpredCRF[ ndx, :, :] = DCRF_postprocess_2D.DCRF_postprocess_2D( rightMask3Dpred[ndx, :, :], rightMask3Dpred[ndx, :, :]) leftMask3DpredCRF[ ndx, :, :] = DCRF_postprocess_2D.DCRF_postprocess_2D( leftMask3Dpred[ndx, :, :], leftMask3Dpred[ndx, :, :]) avgPerfOverKidneys = np.mean([ calcPerf(rightMask3DpredCRF, rightMask3Dorig), calcPerf(leftMask3DpredCRF, leftMask3Dorig) ], axis=0) # import matplotlib.pyplot as plt # plt.figure();ndx=100; # plt.subplot(131);plt.imshow(rightMask3Dorig[ndx,:,:]) # plt.subplot(132);plt.imshow(rightMask3Dpred[ndx,:,:]) # plt.subplot(133);plt.imshow(rightMask3DpredCRF[ndx,:,:]) elif morfFilteringEnabled: # rightMask3Dpred2=morphology.remove_small_objects(rightMask3Dpred.astype(int), 10000) # leftMask3Dpred2=morphology.remove_small_objects(leftMask3Dpred.astype(int), 10000) rightMask3DpredCRF = np.zeros((256, 256, len(sliceIds))) leftMask3DpredCRF = np.zeros((256, 256, len(sliceIds))) selem = morphology.disk(3) for ndx in range(leftMask3Dpred.shape[1]): # rightMask3DpredCRF[ndx,:,:]=morphology.binary_closing(rightMask3Dpred[ndx,:,:],selem) # leftMask3DpredCRF[ndx,:,:]=morphology.binary_closing(leftMask3Dpred[ndx,:,:],selem) rightMask3DpredCRF = morphology.remove_small_objects( rightMask3Dpred.astype(int), 1000) leftMask3DpredCRF = morphology.remove_small_objects( leftMask3Dpred.astype(int), 1000) avgPerfOverKidneys = np.mean([ calcPerf(rightMask3DpredCRF, rightMask3Dorig), calcPerf(leftMask3DpredCRF, leftMask3Dorig) ], axis=0) else: avgPerfOverKidneys = np.mean([ calcPerf(rightMask3Dpred, rightMask3Dorig), calcPerf(leftMask3Dpred, leftMask3Dorig) ], axis=0) # isValidationData=0;array([ 0.91786584, 0.93257748, 0.90517073, 848.5,3.97734375]) # iisValidationData=1; 0.86431, 0.932577, 0.807285, 3929.5, 18.4195] #iisValidationData=1;+CRF 0.916862,0.893233,0.942325,-1215,-5.69531 scores.append(avgPerfOverKidneys) df.ix[dfNd] = pd.Series({ 'Name': s, 'kidney Condition': testKidCond[dfNd], 'F1-Score': avgPerfOverKidneys[0] * 100, 'Prec': avgPerfOverKidneys[1] * 100, 'Rec': avgPerfOverKidneys[2] * 100, 'VEE': avgPerfOverKidneys[4], 'testSet': TestSetNum, 'Model': preTrainednet + '-' + maskBranchNet }) dfNd += 1 # print(dfNd) averageOverSubjectPrefsNormal = np.mean(scores[0:4], axis=0) averageOverSubjectPrefsAbnormal = np.mean(scores[4:], axis=0) f1Normal = np.round(averageOverSubjectPrefsNormal[0] * 100, 2) precNormal = np.round(averageOverSubjectPrefsNormal[1] * 100, 2) recNormal = np.round(averageOverSubjectPrefsNormal[2] * 100, 2) veeNormal = np.round(abs(averageOverSubjectPrefsNormal[4]), 2) f1Abnormal = np.round(averageOverSubjectPrefsAbnormal[0] * 100, 2) precAbnormal = np.round(averageOverSubjectPrefsAbnormal[1] * 100, 2) recAbnormal = np.round(averageOverSubjectPrefsAbnormal[2] * 100, 2) veeAbnormal = np.round(abs(averageOverSubjectPrefsAbnormal[4]), 2) return [ f1Normal, precNormal, recNormal, veeNormal, f1Abnormal, precAbnormal, recAbnormal, veeAbnormal ], df
ROOT_DIR = os.getcwd() HOME_DIR = expanduser('~') DSB_DATA_DIR = join(HOME_DIR, '.kaggle/competitions/data-science-bowl-2018/') # COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.pth") COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_dsb_0020.pth") test_dir = join(DSB_DATA_DIR, 'stage1_test') test_ids = os.listdir(test_dir) dataset_test = DsbDataset() dataset_test.load_dataset(test_ids, test_dir) dataset_test.prepare() dsb_config = DsbConfig() model = modellib.MaskRCNN(config=dsb_config, model_dir='./logs') model = model.cuda() model.load_state_dict(torch.load(COCO_MODEL_PATH), strict=False) raw_predictions = [] for test_id in dataset_test.image_ids: test_image1 = dataset_test.load_image(test_id, 0) pred = model.detect([test_image1]) pred = pred[0] sc = pred['scores'] pred = pred['masks'] raw_predictions.append((pred, sc)) def rle_encoding(x): ''' x: numpy array of shape (height, width), 1 - mask, 0 - background
def instance_segment_train(**kwargs): data_base_dir = kwargs['data_base_dir'] init_with = kwargs['init_with'] outputs_base_dir = 'outputs' pretrained_model_base_dir = 'pretrained_model' save_model_dir = os.path.join(outputs_base_dir, 'snapshot') log_dir = os.path.join(outputs_base_dir, 'log') coco_model_path = os.path.join(pretrained_model_base_dir, 'mask_rcnn_coco.h5') imagenet_model_path = os.path.join( pretrained_model_base_dir, 'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5') os.makedirs(save_model_dir, exist_ok=True) os.makedirs(log_dir, exist_ok=True) config = SketchTrainConfig() config.display() # Training dataset dataset_train = SketchDataset(data_base_dir) dataset_train.load_sketches("train") dataset_train.prepare() # Create model in training mode model = modellib.MaskRCNN(mode="training", config=config, model_dir=save_model_dir, log_dir=log_dir) if init_with == "imagenet": print("Loading weights from ", imagenet_model_path) model.load_weights(imagenet_model_path, 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 print("Loading weights from ", coco_model_path) model.load_weights(coco_model_path, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask" ]) elif init_with == "last": # Load the last model you trained and continue training last_model_path = model.find_last()[1] print("Loading weights from ", last_model_path) model.load_weights(last_model_path, by_name=True) else: print("Training from fresh start.") # Fine tune all layers model.train(dataset_train, learning_rate=config.LEARNING_RATE, epochs=config.TOTAL_EPOCH, layers="all") # Save final weights save_model_path = os.path.join( save_model_dir, "mask_rcnn_" + config.NAME + "_" + str(config.TOTAL_EPOCH) + ".h5") model.keras_model.save_weights(save_model_path)
#================================================================== 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 NAME = "coco" NUM_CLASSES = 1 + 80 # COCO has 80 classes config = InferenceConfig() config.display() #================================================================== # Create model object in inference mode. model = modellib.MaskRCNN(mode="inference", model_dir="./logs", config=config) # Load weights trained on MS-COCO model.load_weights("./mask_rcnn_coco.h5", 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',
def run_model(checkpoint_path, video_in_path, video_out_path, detections_path, max_frames, stride): config = InferenceConfig() config.display() model = modellib.MaskRCNN(mode="inference", model_dir='./logs', config=config) model.load_weights(checkpoint_path, by_name=True) cap = cv2.VideoCapture(video_in_path) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) rate = int(cap.get(cv2.CAP_PROP_FPS)) vid_out = None if video_out_path: vid_out = cv2.VideoWriter(video_out_path, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), rate, (width, height)) count = 0 frame_detections = {} while cap.isOpened(): start = time.time() ret, frame = cap.read() if not ret or count > max_frames: break if count % stride != 0: count = count + 1 continue molded_images, image_metas, windows = model.mold_inputs([frame]) # Run object detection detections, mrcnn_class, mrcnn_bbox, mrcnn_mask, \ rois, rpn_class, rpn_bbox = \ model.keras_model.predict([molded_images, image_metas], verbose=0) zero_ix = np.where(detections[0][:, 4] == 0)[0] N = zero_ix[0] if zero_ix.shape[0] > 0 else detections[0].shape[0] # Extract boxes, class_ids, scores, and class-specific masks boxes, class_ids, scores, masks = process_detections( detections[0], mrcnn_mask[0], frame.shape, windows[0]) print(boxes.shape, class_ids.shape, scores.shape, masks.shape) boxes = boxes.astype(np.float32) boxes[:, 0] = boxes[:, 0] / frame.shape[0] boxes[:, 2] = boxes[:, 2] / frame.shape[0] boxes[:, 1] = boxes[:, 1] / frame.shape[1] boxes[:, 3] = boxes[:, 3] / frame.shape[1] frame_detections[count] = [boxes, class_ids, scores, masks] print(class_ids) if vid_out: final_rois, final_class_ids, final_scores, final_masks = \ model.unmold_detections(detections[0], mrcnn_mask[0], frame.shape, windows[0]) mask_img = display_instances(frame, final_rois, final_masks, final_class_ids, class_names, final_scores) vid_out.write(mask_img) end = time.time() print('time', count, end - start) count = count + 1 if vid_out: vid_out.release() if detections_path: np.save(detections_path, frame_detections)
def train(dataset, modelPath, classes, logs, modelName, epochs=200, steps_per_epoch=3000, ROIsPerImage=64, flags=''): print("Logs: ", logs) # Configurations # TODO: Make as user parameters config = ModelConfig(name=modelName, imagesPerGPU=1, GPUcount=1, numClasses=len(classes) + 1, trainROIsPerImage=ROIsPerImage, stepsPerEpoch=steps_per_epoch, miniMaskShape=(128, 128), validationSteps=100, imageMaxDim=256 * 3, imageMinDim=256 * 3) config.display() # raise SystemExit(0) # Create model model = modellib.MaskRCNN(mode="training", config=config, model_dir=logs) # Load weights print("Loading weights ", modelPath) # TODO: Make as a parameter if "coco.h5" in modelPath: model.load_weights(modelPath, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask" ]) else: model.load_weights(modelPath, by_name=True) print('Reading images from dataset ', dataset) images = list() for root, subdirs, _ in os.walk(dataset): if not subdirs: # TODO: More structures images.append(root) shuffle(images) if 's' in flags: # Write list of unused images to logs testImagesThreshold = int(len(images) * .9) print('List of unused images saved in the logs directory ' 'as "unused.txt"') with open(os.path.join(logs, 'unused.txt'), 'w') as unused: for filename in images[testImagesThreshold:]: unused.write('{}\n'.format(filename)) else: testImagesThreshold = len(images) evalImagesThreshold = int(testImagesThreshold * .75) # Training dataset trainImages = images[:evalImagesThreshold] dataset_train = utils.Dataset() dataset_train.import_contains(classes, trainImages, modelName) dataset_train.prepare() # Validation dataset evalImages = images[evalImagesThreshold:testImagesThreshold] dataset_val = utils.Dataset() dataset_val.import_contains(classes, evalImages, modelName) dataset_val.prepare() # Training - Stage 1 # Adjust epochs and layers as needed print("Training network heads") model.train(dataset_train, dataset_val, learning_rate=config.LEARNING_RATE, epochs=int(epochs / 7), layers='heads') # Training - Stage 2 # Finetune layers from ResNet stage 4 and up print("Fine tune Resnet stage 4 and up") model.train( dataset_train, dataset_val, learning_rate=config.LEARNING_RATE / 10, # no dividing orig epochs=int(epochs / 7) * 3, layers='4+') # Training - Stage 3 # Fine tune all layers print("Fine tune all layers") model.train( dataset_train, dataset_val, learning_rate=config.LEARNING_RATE / 100, # just 10 original epochs=epochs, layers='all')
def main(): global config parser = argparse.ArgumentParser() parser.add_argument("command", metavar="<command>", help="'train' or 'evaluate'") parser.add_argument("--logs", required=False, metavar="/path/to/logs/", help="Path to store logs and checkpoints.", default=os.path.join(ROOT_DIR, "../logs", EXP_NAME)) parser.add_argument('--weights', required=False, metavar="/path/to/weights.h5", help="Path to weights .h5 file or 'coco' or 'last'", default='coco') parser.add_argument('--limit', required=False, metavar="Testset size", help="Testset Size, availible when evaluation.", default=None) args = parser.parse_args() assert args.command in ['train', 'evaluate'] if not os.path.exists(args.logs): os.mkdir(args.logs) config = S3DConfig() if args.command == 'evaluate': config.IMAGES_PER_GPU = 1 config.BATCH_SIZE = 1 config.display() 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.lower() == 'last': weight_path = model.find_last() elif args.weights.lower() == 'coco': weight_path = COCO_PATH else: weight_path = args.weights if args.weights.lower() == 'coco': model.load_weights(weight_path, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask" ]) else: model.load_weights(weight_path, by_name=True) if args.command == 'train': train(model) else: test(model, config, args.limit, args.logs)
def masks_generation(train, source, destination, all=True): print('START PROGRAM !!!') ROOT_DIR = os.getcwd() MODEL_DIR = os.path.join(ROOT_DIR, "logs") COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.pth") 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 = 0 for CPU GPU_COUNT = 1 IMAGES_PER_GPU = 1 config = InferenceConfig() config.display() # Create model object. model = modellib.MaskRCNN(model_dir=MODEL_DIR, config=config) if config.GPU_COUNT: model = model.cuda() # Load weights trained on MS-COCO model.load_state_dict(torch.load(COCO_MODEL_PATH)) # 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' ] counter = 0 invalid_file = [] if not os.path.isdir(destination): os.mkdir(destination, 0o777) for fname in os.listdir(source): counter = counter + 1 if not train and (fname.split('_')[0] == '-1' or fname.split('_')[0] == '0000'): continue #image = skimage.io.imread(os.path.join(source, fname)) image = cv2.imread(os.path.join(source, fname), cv2.IMREAD_COLOR) if image is None: invalid_file.append(fname) continue if len(image.shape) != 3: invalid_file.append(fname) continue if image.shape[2] != 3: invalid_file.append(fname) continue image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) results = model.detect([image]) if results is None: invalid_file.append(fname) print('None File') continue r = results[0] person = [] for k, v in enumerate(r['class_ids']): if v == 1: person.append(k) if len(person) != 0: if all: a = np.zeros([128, 64], np.uint8) else: M_SCORE = 0 K_MAX = 0 for k, v in enumerate(r['scores']): if k in person and v > M_SCORE: M_SCORE = v K_MAX = k final_image = (r['masks'][:, :, K_MAX] * 255).astype(np.uint8) cv2.imwrite(os.path.join(destination, fname), final_image) else: invalid_file.append(fname) continue if counter != 0 and counter % 500 == 0: print(counter) if train: with open('./no_mask_train.pck', 'wb') as fn: pickle.dump(invalid_file, fn) else: with open('./no_mask_test.pck', 'wb') as fn: pickle.dump(invalid_file, fn)
def __init__(self, fig, ax, img_dir, classes, model_path, json_file): self.RS = RectangleSelector( ax, self.line_select_callback, drawtype='box', useblit=True, button=[1, 3], # don't use middle button minspanx=5, minspany=5, spancoords='pixels', interactive=True) ax.set_yticklabels([]) ax.set_xticklabels([]) #self.classes, self.img_paths, _ = read_JSON_file(json_file) with open(classes, 'r') as f: self.classes, img_paths = sorted([ x.strip().split(',')[0] for x in f.readlines() ]), glob.glob(os.path.abspath(os.path.join(img_dir, '*.jpg'))) plt.tight_layout() self.ax = ax self.fig = fig self.axradio = plt.axes([0.0, 0.0, 0.1, 1]) self.radio = RadioButtons(self.axradio, self.classes) self.zoom_scale = 1.2 self.zoom_id = self.fig.canvas.mpl_connect('scroll_event', self.zoom) self.keyboard_id = self.fig.canvas.mpl_connect('key_press_event', self.onkeyboard) self.selected_poly = False self.axsave = plt.axes([0.81, 0.05, 0.1, 0.05]) self.b_save = Button(self.axsave, 'Save') self.b_save.on_clicked(self.save) self.objects, self.existing_patches, self.existing_rects = [], [], [] self.num_pred = 0 if json_file is None: self.images, self.annotations = [], [] self.index = 0 self.ann_id = 0 else: with open(json_file, 'r') as g: d = json.loads(g.read()) self.images, self.annotations = d['images'], d['annotations'] self.index = len(self.images) self.ann_id = len(self.annotations) print(self.index) prev_files = [x['file_name'] for x in self.images] for i, f in enumerate(img_paths): im = Image.open(f) width, height = im.size dic = { 'file_name': f, 'id': self.index + i, 'height': height, 'width': width } if f not in prev_files: self.images.append(dic) else: self.index += 1 image = plt.imread(self.images[self.index]['file_name']) self.ax.imshow(image, aspect='auto') sys.path.append(args['maskrcnn_dir']) from config import Config import model as modellib from skimage.measure import find_contours from visualize_cv2 import random_colors class InstanceConfig(Config): NAME = os.path.basename(model_path) GPU_COUNT = 1 IMAGES_PER_GPU = 1 NUM_CLASSES = 22 + 1 IMAGE_SHAPE = np.array( [Config.IMAGE_MIN_DIM, Config.IMAGE_MIN_DIM, 3]) self.config = InstanceConfig() plt.connect('draw_event', self.persist) # Create model object in inference mode. self.model = modellib.MaskRCNN( mode="inference", model_dir='/'.join(args['weights_path'].split('/')[:-2]), config=self.config) # Load weights trained on MS-COCO self.model.load_weights(args['weights_path'], by_name=True) r = self.model.detect([image], verbose=0)[0] # Number of instances N = r['rois'].shape[0] masks = r['masks'] # Show area outside image boundaries. height, width = image.shape[:2] class_ids, scores, rois = r['class_ids'], r['scores'], r['rois'], for i in range(N): # Label class_id = class_ids[i] score = scores[i] if scores is not None else None label = self.classes[class_id - 1] pat = patches.Rectangle((rois[i][1], rois[i][0]), rois[i][3] - rois[i][1], rois[i][2] - rois[i][0], linewidth=1, edgecolor='r', facecolor='r', alpha=0.4) rect = self.ax.add_patch(pat) self.objects.append(label) self.existing_patches.append(pat.get_bbox().get_points()) self.existing_rects.append(pat) self.num_pred = len(self.objects)
def main(): parse = argparse.ArgumentParser() parse.add_argument("--image", type=str) parse.add_argument('--video', type=str) args = parse.parse_args() ROOT_DIR = os.getcwd() MODEL_DIR = os.path.join(ROOT_DIR, "logs") COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5") if not os.path.exists(COCO_MODEL_PATH): raise AssertionError('please download the pre-trained model') colorsFile = "colors.txt" with open(colorsFile, 'rt') as f: colorsStr = f.read().rstrip('\n').split('\n') colors = [] for i in range(len(colorsStr)): rgb = colorsStr[i].split(' ') color = np.array([float(rgb[0]), float(rgb[1]), float(rgb[2])]) colors.append(color) inference_config = InferenceConfig() model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=inference_config) model.load_weights(COCO_MODEL_PATH, by_name=True) if (args.image): if not os.path.isfile(args.image): print("Input image file ", args.image, " doesn't exist") sys.exit(1) cap = cv.VideoCapture(args.image) outputFile = args.image[:-4]+'_mask_rcnn_out_py.jpg' elif (args.video): if not os.path.isfile(args.video): print("Input video file ", args.video, " doesn't exist") sys.exit(1) cap = cv.VideoCapture(args.video) outputFile = args.video[:-4]+'_mask_rcnn_out_py.avi' else: cap = cv.VideoCapture(0) if (not args.image): vid_writer = cv.VideoWriter(outputFile, cv.VideoWriter_fourcc('M', 'J', 'P', 'G'), 30, (round(cap.get(cv.CAP_PROP_FRAME_WIDTH)), round(cap.get(cv.CAP_PROP_FRAME_HEIGHT)))) maskThreshold = 0.3 while cv.waitKey(1) < 0: hasFrame, frame = cap.read() if not hasFrame: print("Done processing !!!") print("Output file is stored as ", outputFile) cv.waitKey(3000) break print("frame shape:", frame.shape) # class_names = ['BG', 'person'] results = model.detect_keypoint([frame], verbose=1) r = results[0] if r['masks'].shape[0]: for i in range(r['masks'].shape[2]): mask = r['masks'][:, :, i] mask = (mask > maskThreshold) roi = frame[mask] colorIndex = random.randint(0, len(colors)-1) color = colors[colorIndex] frame[mask] = ([0.3 * color[0], 0.3 * color[1], 0.3 * color[2]] + 0.7 * roi).astype(np.uint8) mask = mask.astype(np.uint8) _, contours, hierarchy = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) cv.drawContours(frame, contours, -1, color, 3, cv.LINE_8, hierarchy, 100) keypoints = np.array(r['keypoints']).astype(int) skeleton = [0, -1, -1, 5, -1, 6, 5, 7, 6, 8, 7, 9, 8, 10, 11, 13, 12, 14, 13, 15, 14, 16] for i in range(len(keypoints)): # Skeleton: 11*2 limb_colors = [[0, 0, 255], [0, 170, 255], [0, 255, 170], [0, 255, 0], [170, 255, 0], [255, 170, 0], [255, 0, 0], [255, 0, 170], [170, 0, 255], [170, 170, 0], [170, 0, 170]] if(len(skeleton)): skeleton = np.reshape(skeleton, (-1, 2)) neck = np.array((keypoints[i, 5, :] + keypoints[i, 6, :]) / 2).astype(int) if(keypoints[i, 5, 2] == 0 or keypoints[i, 6, 2] == 0): neck = [0, 0, 0] limb_index = -1 for limb in skeleton: limb_index += 1 start_index, end_index = limb if(start_index == -1): Joint_start = neck else: Joint_start = keypoints[i][start_index] if(end_index == -1): Joint_end = neck else: Joint_end = keypoints[i][end_index] if ((Joint_start[2] != 0) & (Joint_end[2] != 0)): cv.line(frame, tuple(Joint_start[:2]), tuple(Joint_end[:2]), limb_colors[limb_index], 5) if (args.image): cv.imwrite(outputFile, frame.astype(np.uint8)) else: vid_writer.write(frame.astype(np.uint8))
# visualize.display_keypoints(original_image,gt_bbox,gt_keypoint,gt_class_id,dataset.class_names) data_tra = FIDataset() data_tra.load_FI() data_tra.prepare() data_val = FIDataset() data_val.load_FI(training=False) data_val.prepare() model_dir = './logs_{}'.format(IMAGE_CATEGORY) if not os.path.exists(model_dir): os.makedirs(model_dir) model = modellib.MaskRCNN(mode='training', config=config, model_dir=model_dir) try: model.load_weights(model.find_last()[1], by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask" ]) except TypeError as e: model.load_weights('./mask_rcnn_coco.h5', by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"
train_dataset_keypoints = MosquitoesDataset() train_dataset_keypoints.load_dataset(dataset_dir, "train") train_dataset_keypoints.prepare() val_dataset_keypoints = MosquitoesDataset() val_dataset_keypoints.load_dataset(dataset_dir, "val") val_dataset_keypoints.prepare() # In[ ]: ROOT_DIR = os.getcwd() MODEL_DIR = os.path.join(ROOT_DIR, "logs") config = MosquitoesConfig() # Local path to trained weights file COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5") # Create model object in inference mode. model = modellib.MaskRCNN(mode="training", model_dir=MODEL_DIR, config=config) # Load weights trained on MS-COCO #model.load_weights(COCO_MODEL_PATH, by_name=True,exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"]) #print("Loading weights from ", COCO_MODEL_PATH) # Training - Stage 1 print("Train heads") model.train(train_dataset_keypoints, val_dataset_keypoints,\ learning_rate=config.LEARNING_RATE,\ epochs=15,\ layers='heads')
config = CocoConfig() 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 config = InferenceConfig() config.print() # Create model if args.command == "train": model = modellib.MaskRCNN(mode="training", config=config, model_dir=MODEL_DIR) else: model = modellib.MaskRCNN(mode="inference", config=config, model_dir=MODEL_DIR) # 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()
# In[21]: # Note that any hyperparameters here, such as LR, may still not be optimal LR = 1e-4 EPOCHS = [2, 6, 8] import warnings warnings.filterwarnings("ignore") # In[22]: #This section creates a Mask R-CNN model and specifies augmentations to be used. model = modellib.MaskRCNN(mode='training', config=config, model_dir=ROOT_DIR) # In[24]: #h5 파일을 python file과 같은 곳에 넣어줘야함 COCO_WEIGHTS_PATH = os.path.join(r'mask_rcnn_coco.h5') print(os.path) # In[25]: model.load_weights(COCO_WEIGHTS_PATH, by_name=True, exclude=['mrcnn_class_logits', 'mrcnn_bbox_fc', 'mrcnn_bbox', 'mrcnn_mask'])
def continueTrainingCoCo_kfold(epochs): COCO_MODEL_PATH = os.path.join(ROOT_DIR, "model", "mask_rcnn_coco.h5") if not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH) train_fold = [0, 1, 2] for i in range(3): if i not in train_fold: print('skip fold', i) continue config = train_config() if FULLDATA: config.STEPS_PER_EPOCH = 10000 config.LEARNING_RATE = config.LEARNING_RATE / 4 config.NAME = '{}{}'.format(config.NAME, i) config.display() dataset = ShapesDataset(i, 0, -1) dataset.load_imgs(TRAIN_PATH) dataset.prepare() #print("Image Count: {}".format(len(dataset.image_ids))) #print("Class Count: {}".format(dataset.num_classes)) dataset_valid = ShapesDataset(i, 1, 1) dataset_valid.load_imgs(VALID_PATH) dataset_valid.prepare() model = modellib.MaskRCNN(mode="training", config=config, model_dir=MODEL_DIR) search_dir = os.path.join(MODEL_DIR, config.NAME) if not os.path.exists(search_dir): os.mkdir(search_dir) print('search_dir', search_dir) model_path = 'logs/best_round0.h5' #model.find_last(search_dir)[1] if model_path: print("Loading weights from " + model_path) model.load_weights(model_path, by_name=True) elif config.TRAIN_FROM_COCO == 1: print('train from CoCo') model.load_weights(COCO_MODEL_PATH, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask" ]) else: print('train from scrash') # print('start training', i); # model.train(dataset, dataset_valid, # learning_rate=config.LEARNING_RATE * 2, # epochs=5, # layers="heads", # argument = config.ARGUMENT) # model_path = model.find_last(search_dir)[1] # print("Loading weights from " + model_path) # model.load_weights(model_path, by_name=True) # stage1_path = os.path.join( # model.log_dir,'{}_{}.h5'.format(config.NAME.lower(), 'heads')) # model_path = model.find_last(search_dir)[1] # if model_path: # print("Loading weights from " + model_path) # model.load_weights(model_path, by_name=True) # model.train(dataset, dataset_valid, # learning_rate=config.LEARNING_RATE, # epochs=15, # layers="all", # argument = config.ARGUMENT) # model_path = model.find_last(search_dir)[1] # if model_path: # print("Loading weights from " + model_path) # model.load_weights(model_path, by_name=True) model.train(dataset, dataset_valid, learning_rate=config.LEARNING_RATE / 20, epochs=50, layers="all", argument=config.ARGUMENT) from keras import backend as K K.clear_session()
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) # model_dir:保存训练日志和训练权重的目录 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()
AMODAL_MODEL_PATH = './checkpoints/COCOA.pth' class InferenceConfig(Amodalfig): # 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 object. config.NUM_CLASSES = 1+1 model = modellib.MaskRCNN(model_dir=MODEL_DIR, config=config) model.mask.conv1 = nn.Conv2d(439, 256, kernel_size=3, stride=1) model.mask.conv5 = nn.Conv2d(256, config.NUM_CLASSES, kernel_size=1, stride=1) model.classifier.linear_class = nn.Linear(1024, config.NUM_CLASSES) model.classifier.linear_bbox = nn.Linear(1024, config.NUM_CLASSES * 4) model.GLM_modual = DeepLabV2_ResNet101_MSC(182) model.current_epoch = 0 # Load weights trained on MS-COCO model.load_weights(AMODAL_MODEL_PATH) if config.GPU_COUNT: model = model.cuda() class_names = ['BG', 'objects'] image_list = os.listdir(IMAGE_DIR)
def test_simple(params): """Test function.""" import os import sys import coco import utils from mrcnn.config import Config ROOT_DIR = os.getcwd() MODEL_DIR = os.path.join(ROOT_DIR, "logs") COCO = os.path.normpath(os.getcwd() + os.sep + os.pardir) COCO = os.path.join(COCO + os.sep, "Mask RCNN") COCO_MODEL_PATH = os.path.join(COCO, "mask_rcnn_coco.h5") #print(COCO_MODEL_PATH) if os.path.isdir('Detection') is False: os.mkdir('Detection') DET_DIR = os.path.join(ROOT_DIR, "Detection") DET_PATH = DET_DIR + os.sep if not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH) class CocoConfig(Config): """Configuration for training on MS COCO. Derives from the base Config class and overrides values specific to the COCO dataset. """ # Give the configuration a recognizable name NAME = "coco" # We use a GPU with 12GB memory, which can fit two images. # Adjust down if you use a smaller GPU. IMAGES_PER_GPU = 2 # Uncomment to train on 8 GPUs (default is 1) # GPU_COUNT = 8 # Number of classes (including background) # NUM_CLASSES = 37 # COCO has 80 classes NUM_CLASSES = 81 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 # MAX_GT_INSTANCES = 100 # TRAIN_ROIS_PER_IMAGE = 50 # BACKBONE = "resnet50" #not working at all! # RPN_ANCHOR_STRIDE = 2 # POST_NMS_ROIS_TRAINING = 1000 # POST_NMS_ROIS_INFERENCE = 500 # IMAGE_MIN_DIM = 400 # really much faster but bad results # IMAGE_MAX_DIM = 640 # DETECTION_MAX_INSTANCES = 50 #a little faster but some instances not recognized config = InferenceConfig() config.display() left = tf.placeholder(tf.float32, [2, args.input_height, args.input_width, 3]) modeld = MonodepthModel(params, "test", left, None) modelrcnn = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) modelrcnn.load_weights(COCO_MODEL_PATH, by_name=True) 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' ] location1 = [] location = '' for i in range(0, 5): lat = str((50.0753397 - i * 0.0001)) lon = str(14.4189888) location1 = lat + ',' + lon + '; ' location += location1 apiargs = { 'location': location, #'50.0753397,14.4189888 ; 50.0795436,14.3907308 ;50.10291748018805, 14.39132777985096', 'size': '640x640', 'heading': '180', 'fov': '90', 'key': 'AIzaSyCciJlgQzOFXZXhvM1ORscu0Cj5dj-lTRo', 'pitch': '0' } # Get a list of all possible queries from multiple parameters api_list = google_streetview.helpers.api_list(apiargs) # Create a results object for all possible queries resultsg = google_streetview.api.results(api_list) # Preview results # resultsg.preview() # Download images to directory 'downloads' resultsg.download_links('Downloads') # Save metadata # resultsg.save_metadata('metadata.json') onlyfiles = [] while True: # ret , frame = capture.read() # input_image = frame images = os.path.join(ROOT_DIR, 'Downloads') for f in listdir(images): if f is not 'metadat.json': onlyfiles.append(f) onlyfiles = [f for f in listdir(images) if isfile(join(images, f))] frame = np.empty(len(onlyfiles), dtype=object) for n in range(0, len(onlyfiles) - 1): frame = cv2.imread(join(images, onlyfiles[n])) input_image = frame original_height, original_width, num_channels = input_image.shape input_image = scipy.misc.imresize( input_image, [args.input_height, args.input_width], interp='lanczos') input_image = input_image.astype(np.float32) / 255 input_images = np.stack((input_image, np.fliplr(input_image)), 0) # SESSION config = tf.ConfigProto(allow_soft_placement=True) sess = tf.Session(config=config) # INIT sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) coordinator = tf.train.Coordinator() disp = sess.run(modeld.disp_left_est[0], feed_dict={left: input_images}) disp_pp = post_process_disparity(disp.squeeze()).astype(np.float32) results = modelrcnn.detect([frame], verbose=0) r = results[0] disp_to_img = scipy.misc.imresize( disp_pp.squeeze(), [original_height, original_width]) frame = display_instances(frame, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'], resultsg) cv2.imwrite(DET_PATH + str(n) + '.jpeg', frame) cv2.destroyAllWindows() break map.save(outfile='map.html')
create_dir(args.output_image_path, class_name) model_dir = os.getcwd() class_names = read_classes(args.rcnn_classes) config = InferenceConfig() config.BATCH_SIZE = args.batch_size config.IMAGE_PER_GPU = args.gpu_count config.display() model = modelib.MaskRCNN(mode="inference", model_dir=model_dir, config=config) model.load_weights(args.rcnn_model_path, by_name=True) image_data_gen = ImageDataGenerator(horizontal_flip=False) images = BatchProcessing(image_data_gen) data_set_len = len([ name for name in os.listdir(os.path.join(args.base_image_path, class_name)) if os.path.isfile( os.path.join(os.path.join(args.base_image_path, class_name), name)) ])
print("Dataset: ", args.dataset) if args.subset: print("Subset: ", args.subset) #print("Logs: ", args.logs) # Configurations if args.command == "train": config = NucleusConfig() else: config = NucleusInferenceConfig() config.display() # Create model if args.command == "train": model = modellib.MaskRCNN(mode="training", config=config, model_dir=DEFAULT_LOGS_DIR) else: model = modellib.MaskRCNN(mode="inference", config=config, model_dir=DEFAULT_LOGS_DIR) weights_path = '' # Select weights file to load #TODO: check if checkpoint exists print(len(os.listdir(DEFAULT_LOGS_DIR))) print('root dir', ROOT_DIR) if len(os.listdir(DEFAULT_LOGS_DIR)) == 0: weights_path = COCO_WEIGHTS_PATH
if args.command == "train": config = CocoConfig() 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 # GPU_COUNT = 0 IMAGES_PER_GPU = 2 DETECTION_MIN_CONFIDENCE = 0 config = InferenceConfig() config.display() # Create model if args.command == "train": model = modellib.MaskRCNN(config=config, model_dir=args.logs) else: model = modellib.MaskRCNN(config=config, model_dir=args.logs) if config.GPU_COUNT: os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2,3" model = model.cuda() #model = torch.nn.DataParallel(model).cuda() # Select weights file to load if args.model: 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":
layers='4+') # -------------------------------------------------------------------------- ############################################################ # Training ############################################################ if __name__ == '__main__': config = AdrivingConfig() config.display() DEVICE = "/gpu:0" # /cpu:0 or /gpu:0 # with tf.device(DEVICE): model = modellib.MaskRCNN(config=config, model_dir=os.path.join(DEFAULT_LOGS_DIR, 'pytorch_resnet')) INIT_WEIGHTS_PATH = os.path.join( '../../', setting['MODEL_CHECKPOINT_DIR'], 'mask_rcnn_adriving_aug_1024_1024_1e-5_4p_0428.pth') weights_path = INIT_WEIGHTS_PATH print(weights_path) model.load_weights(weights_path, strict=True) model = model.cuda() data_dir = Path( os.path.join('../../', setting['TEST_DATA_CLEAN_PATH'], 'train_val')) train(model)
MODEL_DIR = os.path.join(ROOT_DIR, "mylogs") # Local path to trained weights file COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco_humanpose.h5") class InferenceConfig(coco.CocoConfig): GPU_COUNT = 1 IMAGES_PER_GPU = 1 KEYPOINT_MASK_POOL_SIZE = 7 inference_config = InferenceConfig() # Recreate the model in inference mode model = modellib.MaskRCNN(mode="inference", config=inference_config, model_dir=MODEL_DIR) # Get path to saved weights model_path = os.path.join(ROOT_DIR, "mask_rcnn_coco_humanpose.h5") assert model_path != "", "Provide path to trained weights" print("Loading weights from ", model_path) model.load_weights(model_path, by_name=True) class_names = ['BG', 'person'] def cv2_display_keypoint(image, boxes, keypoints,
def main(): parser = argparse.ArgumentParser() parser.add_argument("--input_name", type=str, required=True, help="Input image name") parser.add_argument("--output_name", type=str, required=True, help="Output image name") args = parser.parse_args() # Root directory of the project ROOT_DIR = os.getcwd() # 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) 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() # 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) 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', 'bottle', 'microphone' ] # Load image image = skimage.io.imread(args.input_name) # Run detection results = model.detect([image], verbose=1) # Visualize results r = results[0] # Blur backround of the image and save it with output_name visualize.display_blurred(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'], name=args.output_name)
# Training dataset dataset_train = BowlDataset() dataset_train.load_bowl(train_ids) dataset_train.prepare() # # Validation dataset, same as training.. will use pad64 on this one dataset_val = BowlDataset() dataset_val.load_bowl(train_ids) dataset_val.prepare() # Directory to save logs and trained model MODEL_DIR = os.path.join(ROOT_DIR, "logs") model = modellib.MaskRCNN(mode="training", config=bowl_config, model_dir=MODEL_DIR) ## Change this with the path to the last epoch of train step 2 model_path = os.path.join(MODEL_DIR, '00220190520T1522', 'final.h5') model.load_weights(model_path, by_name=True) import time start_time = time.time() ## Augment True will perform flipud fliplr and 90 degree rotations on the 512x512 images # Image augmentation # http://imgaug.readthedocs.io/en/latest/source/augmenters.html # ## This should be the equivalente version of my augmentations using the imgaug library
'bottle': 'bottle', 'bowl': 'bowl', 'cup': 'mug', 'laptop': 'laptop' } coco_cls_ids = [] for coco_cls in class_map: ind = coco_names.index(coco_cls) coco_cls_ids.append(ind) assert mode in ['detect', 'eval'] if mode == 'detect': # Recreate the model in inference mode model = Model.MaskRCNN(mode='inference', config=config, model_dir=MODEL_DIR) gt_dir = os.path.join('data', 'gts', data) if data == 'val': dataset_val = NOCSDataset(synset_names, 'val', config) dataset_val.load_camera_scenes(camera_dir) dataset_val.prepare(class_map) dataset = dataset_val elif data == 'real_test': dataset_real_test = NOCSDataset(synset_names, 'test', config) dataset_real_test.load_real_scenes(real_dir) dataset_real_test.prepare(class_map) dataset = dataset_real_test else: assert False, 'Unknown data resource.'
import model as modellib ROOT_DIR = os.getcwd() MODEL_DIR = os.path.join(ROOT_DIR, "logs") COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5") if not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH) # class InferenceConfig(coco.CocoConfig): # 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) 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'
def run(self): # --------------------------- Preseting --------------------------- # import regular module import os import sys import time import numpy as np import tensorflow as tf import matplotlib import matplotlib.pyplot import shapefile # Root directory of the project ROOT_DIR = r"/pylon5/ps5fp1p/wez13005/local_dir" # Import Mask RCNN sys.path.append(ROOT_DIR) # Directory to save logs and trained model MODEL_DIR = os.path.join(ROOT_DIR, "logs") # import the Mask R-CNN module import utils import model as modellib from model import log # import the configuration import iwp_InferenceConfidenceLevel as polygon # --------------------------- Configurations --------------------------- # Set config config = polygon.PolygonConfig() POLYGON_DIR = self.POLYGON_DIR weights_path = self.weights_path output_shp_root = self.output_shp_root # Override the training configurations with a few # changes for inferencing. class InferenceConfig(config.__class__): # Run detection on one image at a time GPU_COUNT = 1 IMAGES_PER_GPU = 1 config = InferenceConfig() # config.display() # --------------------------- Preferences --------------------------- # Device to load the neural network on. # Useful if you're training a model on the same # machine, in which case use CPU and leave the # GPU for training. DEVICE = "/gpu:%s" % (self.gpu_id) # /cpu:0 or /gpu:0 os.environ['CUDA_VISIBLE_DEVICES'] = "{}".format(self.gpu_id) # Inspect the model in training or inference modes # values: 'inference' or 'training' # TODO: code for 'training' test mode not ready yet TEST_MODE = "inference" """ # --------------------------- Limit the GPU usable memomry --------------------------- from keras.backend.tensorflow_backend import set_session tf_config = tf.ConfigProto() tf_config.gpu_options.per_process_gpu_memory_fraction = 0.2 set_session(tf.Session(config=tf_config)) """ # --------------------------- Load validation dataset and Model --------------------------- # Load validation dataset dataset = polygon.PolygonDataset() dataset.load_polygon(POLYGON_DIR, "val") # Must call before using the dataset dataset.prepare() print("Images: {}\nClasses: {}".format(len(dataset.image_ids), dataset.class_names)) # Create model in inference mode with tf.device(DEVICE): model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) # Load weights print("Loading weights ", weights_path) model.load_weights(weights_path, by_name=True) # --------------------------- Workers --------------------------- while True: input_img = self.input_queue.get() # print (input_img) if input_img is None: self.input_queue.task_done() print("Exiting Process %d" % self.gpu_id) break else: # get the upper left x y of the image divided_img_name = input_img.split('/')[-1] i, j, ul_row_divided_img, ul_col_divided_img = divided_img_name.split( '.jpg')[0].split('_') output_shp_path = os.path.join( output_shp_root, divided_img_name.split('..jpg')[0] + '.shp') # read image as array image = matplotlib.pyplot.imread(input_img) # create shp file w = shapefile.Writer(shapeType=shapefile.POLYGON) w.field('Class', 'C', size=5) # ------------------ detection --------------------- # Run object detection results = model.detect([image], verbose=False) # Display results # ax = get_ax(1) r = results[0] if len(r['class_ids']): # output each mask for id_masks in range(r['masks'].shape[2]): # read the mask mask = r['masks'][:, :, id_masks] padded_mask = np.zeros( (mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8) padded_mask[1:-1, 1:-1] = mask # the first element is the only polygon contours = find_contours( padded_mask, 0.5)[0] * np.array( [[self.y_resolution, self.x_resolution]]) # print (contours[0:20]) class_id = r['class_ids'][id_masks] # adjust the contours to RS imagery (row,col) contours = contours + np.array([[ float(ul_row_divided_img), float(ul_col_divided_img) ]]) # print (contours[0:20]) # swap two cols contours.T[[0, 1]] = contours.T[[1, 0]] # write shp file w.poly(parts=[contours.tolist()]) w.record(class_id) # save shp file w.save(output_shp_path)