def plot_predicted_new(dataset, model, model2, cfg, cfg2, class_names, class_names2, n_images): for i in range(n_images): image = dataset.load_image(i) #clown model scaled_image = mold_image(image, cfg) sample = expand_dims(scaled_image, 0) yhat = model.detect(sample, verbose=1)[0] r = yhat #coco model scaled_image = mold_image(image, cfg2) sample = expand_dims(scaled_image, 0) yhat2 = model2.detect(sample, verbose=1)[0] r2 = yhat2 #condition for k in range(r['masks'].shape[-1]): if class_names[r['class_ids'][k]] == 'clown': clownBox = r['rois'][k] for coco in range(r2['masks'].shape[-1]): if class_names2[r2['class_ids'][coco]] == 'person': try: if check_for_overlap(r['rois'][k], r2['rois'][coco]) == 'y': mask = r2['masks'][:, :, coco] image[mask] = 200 else: pass except: pass else: pass elif class_names[r['class_ids'][k]] != 'clown': pass else: pass display_instances_cust(image, r2['rois'], r2['masks'], r2['class_ids'], class_names2, scores=False, imagecount=i, show_bbox=False, captions=False, show_mask=False)
def evaluate_masks(model): """Evaluate the segmentation masks produced by the model.""" # Prepare the validation dataset for evaluation of the model. dataset_val = WildfireDataset() dataset_val.load_wildfires(args.dataset, "val") dataset_val.prepare() all_APs = [] thresholds = np.linspace(0.5,0.95,10) # Iterates across all IoU from 0.5 to 0.95 in steps of 0.05. for iou in thresholds: APs = [] t_pred = 0 t_start = time.time() for image_id in dataset_val.image_ids: # Loads an image and its ground truth. image, image_meta, gt_class_id, gt_bbox, gt_mask = modellib.load_image_gt(dataset_val, config, image_id, use_mini_mask=False) molded_images = np.expand_dims(modellib.mold_image(image, config), 0) # Returns detections and time to predict. t = time.time() results = model.detect([image], verbose=0) r = results[0] t_pred += (time.time() - t) print("Image : {}".format(str(int(image_id)+1))) print("Time to predict : {} secs".format(time.time() - t)) # Compute Average Precision of the image for that IoU threshold. AP, precisions, recalls, overlaps = utils.compute_mask_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks'], iou_threshold = iou) APs.append(AP) print("AP @ {} IoU = {}\n".format((iou), np.mean(APs)))
def evaluate(model): dataset_test = FoodDataset() dataset_test.load_food(args.dataset, "val") dataset_test.prepare() all_APs = [] # print(dataset_test.image_ids) np.random.shuffle(dataset_test.image_ids) # iou_t = 0.5 ious = [0.50,0.55,0.60,0.65,0.70,0.75,0.80,0.85,0.90,0.95] for iou_t in ious: APs = [] for image_id in dataset_test.image_ids: # Load image and ground truth data image, image_meta, gt_class_id, gt_bbox, gt_mask =\ modellib.load_image_gt(dataset_test, config, image_id, use_mini_mask=False) molded_images = np.expand_dims(modellib.mold_image(image, config), 0) # Run object detection results = model.detect([image], verbose=0) r = results[0] if r["class_ids"].size != 0: # Compute AP AP, precisions, recalls, overlaps =\ utils.compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks'], iou_threshold=iou_t) APs.append(AP) all_APs.append(AP) # print(AP) else: APs.append(0.0) all_APs.append(0.0) print("mAP-{}: ".format(iou_t), np.mean(APs) * 100, "%") print("mAP: ", np.mean(all_APs) * 100, "%")
def evaluate_model(dataset, model, cfg): APs = list() for image_id in dataset.image_ids: # load image, bounding boxes and masks for the image id image, image_meta, gt_class_id, gt_bbox, gt_mask = load_image_gt( dataset, cfg, image_id, use_mini_mask=False) # convert pixel values (e.g. center) scaled_image = mold_image(image, cfg) # convert image into one sample sample = np.expand_dims(scaled_image, 0) # make prediction yhat = model.detect(sample, verbose=1) # extract results for first sample r = yhat[0] # calculate statistics, including AP AP, _, _, _ = compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks']) # store APs.append(AP) # calculate the mean AP across all images mAP = np.mean(APs) print('Mean Average Precision: {}'.format(mAP)) return mAP
def evaluate_raspberry(model, dataset, annotations, eval_type="segm", limit=100, image_ids=None): # Compute VOC-Style mAP @ IoU=0.5 # Running on 10 images. Increase for better accuracy. inference_config = RaspberryConfig() image_ids = np.random.choice(dataset_val.image_ids, limit) APs = [] for image_id in image_ids: # Load image and ground truth data image, image_meta, gt_class_id, gt_bbox, gt_mask =\ modellib.load_image_gt(dataset_val, inference_config, image_id, 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) r = results[0] # Compute AP """AP, precisions, recalls, overlaps =\ utils.compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks'])""" AP = utils.compute_ap_range(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks']) APs.append(AP) #print(str(APs)) print("mAP: ", np.mean(APs)) mAP=np.mean(APs) average.append(mAP) mAP=str(mAP) f.write("%s, " % mAP) print("evaluation completed and recorded")
def img_preprocess(self, img): """Pre-processes the input image. img: Input image of shape (-1,XX,YY,3) Returns: molded_image: Molded imimg_age to be used as model input image_meta: Input image metadata anchors: [N, (y1, x1, y2, x2)]. All generated anchors in one array. Sorted with the same order of the given scales. So, anchors of scale[0] come first, then anchors of scale[1], and so on. window: (y1, x1, y2, x2). If max_dim is provided, padding might be inserted in the returned image. If so, this window is the coordinates of the image part of the full image (excluding the padding). The x2, y2 pixels are not included. """ molded_image, window, scale, padding, crop = utils.resize_image( img, min_dim=self.cococonfig.IMAGE_MIN_DIM, min_scale=self.cococonfig.IMAGE_MIN_SCALE, max_dim=self.cococonfig.IMAGE_MAX_DIM, mode=self.cococonfig.IMAGE_RESIZE_MODE) molded_image = model.mold_image(molded_image, self.cococonfig) image_meta = model.compose_image_meta( 0, img.shape, molded_image.shape, window, scale, np.zeros([self.cococonfig.NUM_CLASSES], dtype=np.int32)) anchors = MaskRCNN('inference', self.cococonfig, None).get_anchors(molded_image.shape) return molded_image, image_meta, anchors, window
def plot_actual_vs_predicted(image, model, cfg): # convert pixel values (e.g. center) scaled_image = mold_image(image, cfg) # convert image into one sample sample = np.expand_dims(scaled_image, 0) # make prediction yhat = model.detect(sample, verbose=0)[0] pyplot.imshow(image, interpolation='nearest') pyplot.title('Predicted') ax = pyplot.gca() # plot each box color = ['', 'red', 'blue'] for idx in range(len(yhat['rois'])): box = yhat['rois'][idx] # get coordinates y1, x1, y2, x2 = box # calculate width and height of the box width, height = x2 - x1, y2 - y1 # create the shape rect = Rectangle((x1, y1), width, height, fill=False, color=color[yhat['class_ids'][idx]]) # draw the box ax.add_patch(rect) # # show the figure pyplot.show()
def plot_predicted_coco(dataset, model3, cfg3, class_names2, n_images, phobia): for i in range(n_images): image = dataset.load_image(i) #coco model scaled_image = mold_image(image, cfg3) sample = expand_dims(scaled_image, 0) yhat2 = model3.detect(sample, verbose=1)[0] r2 = yhat2 #condition for coco in range(r2['masks'].shape[-1]): if class_names2[r2['class_ids'][coco]] == phobia: mask = r2['masks'][:, :, coco] image[mask] = 200 else: pass display_instances_cust(image, r2['rois'], r2['masks'], r2['class_ids'], class_names2, scores=False, imagecount=i, show_bbox=False, captions=False, show_mask=False)
def compute_matches(model, inference_config, image_id, dataset_val): image, image_meta, gt_class_id, gt_bbox, gt_mask = \ modellib.load_image_gt(dataset_val, inference_config, image_id, 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) r = results[0] gt_match, pred_match, overlaps = utils.compute_matches(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks'], iou_threshold=0.5) true_positives = np.count_nonzero(gt_match > -1) false_negatives = np.count_nonzero(gt_match == -1) false_positives = np.count_nonzero(pred_match == -1) total_count = np.count_nonzero(overlaps > 0.0) total_iou = np.sum(overlaps) return true_positives, false_negatives, false_positives, total_count, total_iou
def plot_actual_vs_predicted(dataset, model, cfg, figname, n_images=5): for i in range(n_images): # load image and mask image = dataset.load_image(i) mask, _ = dataset.load_mask(i) scaled_image = mold_image(image, cfg) sample = np.expand_dims(scaled_image, 0) yhat = model.detect(sample, verbose=0)[0] plt.subplot(n_images, 2, i*2+1) plt.axis('off') plt.imshow(image) if i == 0: plt.title('Actual') # plot masks for j in range(mask.shape[2]): plt.imshow(mask[:,:,j], cmap='gray', alpha=0.3) # get drawing context plt.subplot(n_images, 2, i*2+2) plt.axis('off') plt.imshow(image) if i == 0: plt.title('Predicted') ax = plt.gca() for box in yhat['rois']: y1, x1, y2, x2 = box width, height = x2 - x1, y2 - y1 rect = Rectangle((x1, y1), width, height, fill=False, color='red') ax.add_patch(rect) plt.savefig(figname)
def plot_prediction(dataset, model, cfg, n_images=5): """ Plot a single image at a time with predictions""" # load image and mask for i in range(n_images): # load the image and mask image = dataset.load_image(i) mask, _ = dataset.load_mask(i) # convert pixel values (e.g. center) scaled_image = mold_image(image, cfg) # convert image into one sample sample = expand_dims(scaled_image, 0) # make prediction yhat = model.detect(sample, verbose=0)[0] # plot raw pixel data pyplot.imshow(image) pyplot.title('Prediction') ax = pyplot.gca() # plot each box for box in yhat['rois']: # get coordinates y1, x1, y2, x2 = box # calculate width and height of the box width, height = x2 - x1, y2 - y1 # create the shape rect = Rectangle((x1, y1), width, height, fill=False, color='red') # draw the box ax.add_patch(rect) pyplot.show()
def evaluate_model(dataset, model, cfg): APs = list() #print('dataset imageid', dataset.image_id) for image_id in dataset.image_ids: print('image_id', image_id) # load image, bounding boxes and masks for the image id image, image_meta, gt_class_id, gt_bbox, gt_mask = load_image_gt( dataset, cfg, image_id, use_mini_mask=False) print('image size', image.size) # convert pixel values (e.g. center) scaled_image = mold_image(image, cfg) # convert image into one sample sample = expand_dims(scaled_image, 0) # make prediction yhat = model.detect(sample, verbose=0) # extract results for first sample r = yhat[0] print('rois', r['rois']) print('class ids', r['class_ids']) print('scores', r['scores']) print('gt bbox', gt_bbox) print('gt class id', gt_class_id) # calculate statistics, including AP AP, _, _, _ = compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks']) print('AP', AP) if np.isnan(AP): AP = 0 print('new AP', AP) # store APs.append(AP) # calculate the mean AP across all images mAP = mean(APs) return mAP
def plot_actual_vs_predicted(dataset, model, cfg, n_images=5, train=True): for i in range(n_images): image = dataset.load_image(i) if train: mask, _ = dataset.load_mask(i) else: mask, _ = dataset.load_mask(i + 200) scaled_image = mold_image(image, cfg) sample = expand_dims(scaled_image, 0) yhat = model.detect(sample, verbose=0)[0] pyplot.subplot(n_images, 2, i * 2 + 1) pyplot.axis('off') pyplot.imshow(image) if i == 0: pyplot.title('Actual') for j in range(mask.shape[2]): pyplot.imshow(mask[:, :, j], cmap='gray', alpha=0.3) pyplot.subplot(n_images, 2, i * 2 + 2) pyplot.axis('off') pyplot.imshow(image) if i == 0: pyplot.title('Predicted') ax = pyplot.gca() for box in yhat['rois']: y1, x1, y2, x2 = box width, height = x2 - x1, y2 - y1 rect = Rectangle((x1, y1), width, height, fill=False, color='red') ax.add_patch(rect) pyplot.show()
def evaluate(model, dataset, config): # Compute VOC-Style mAP @ IoU=0.5 APs = [] cnt = 0 for image_id in dataset.image_ids: try: # Load image and ground truth data image, image_meta, gt_class_id, gt_bbox, gt_mask = modellib.load_image_gt( dataset, config, image_id, use_mini_mask=False) molded_images = np.expand_dims(modellib.mold_image(image, config), 0) # Run object detection results = model.detect([image], verbose=0) r = results[0] # Compute AP AP, precisions, recalls, overlaps = utils.compute_ap( gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks']) APs.append(AP) except: print("Error while doing inference on image with image_id=" + str(image_id)) # Printing the progress while evaluating the model: cnt = cnt + 1 print("Progress: {:2.1%}".format(cnt / len(dataset.image_ids)), end="\r") print("Evaluation completed.") print("mAP: ", np.mean(APs))
def evaluate(model, dataset, config): # Compute VOC-Style mAP @ IoU=0.5 num_test = 10 # len(dataset.image_ids) image_ids = np.random.choice(dataset.image_ids, num_test) APs = [] for image_id in image_ids: path = dataset.image_reference(image_id) # Load image and ground truth data image, image_meta, gt_class_id, gt_bbox, gt_mask =\ modellib.load_image_gt(dataset, config, image_id, use_mini_mask=False) print("path, image_id, gt_class_id, gt_bbox =", path, image_id, gt_class_id, gt_bbox) if gt_class_id.size != 0: # skip 'empty' images molded_images = np.expand_dims(modellib.mold_image(image, config), 0) # Run object detection results = model.detect([image], verbose=1) r = results[0] print(" model predictions = ", r) # Compute AP AP, precisions, recalls, overlaps =\ utils.compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks']) print(" AP, precisions, recalls, overlaps =", AP, precisions, recalls, overlaps, "\n") APs.append(AP) else: print(" skipping") print("mAP: ", np.mean(APs))
def calc_mean_average_precision(dataset_val, inference_config, model): # Compute VOC-Style mAP @ IoU=0.5 # Running on 10 images. Increase for better accuracy. image_ids = np.random.choice(dataset_val.image_ids, 1000) APs = [] F1s = [] for image_id in image_ids: # Load image and ground truth data image, image_meta, gt_class_id, gt_bbox, gt_mask = \ modellib.load_image_gt(dataset_val, inference_config, image_id, 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) r = results[0] # Compute AP AP, precisions, recalls, overlaps = \ utils.compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks']) precision = np.mean(precisions) recall = np.mean(recalls) F1_instance = 2 * (precision * recall) / (precision + recall) APs.append(AP) F1s.append(F1_instance) return np.mean(APs), np.mean(F1s)
def evaluate_mAP(self, model, config, nums): """ Randomly choose n images and calculate the overall accuracy based on given model and configuration Args: model: The model to calculate the overall accuracy config: The model configuration when doing inference nums: The number of images want to test Returns: mAP: the mean of the accuracy after test n images """ image_ids = np.random.choice(self.image_ids, nums) APs = [] for image_id in image_ids: # Load image and ground truth data image, image_meta, gt_class_id, gt_bbox, gt_mask = modellib.load_image_gt(self, config, image_id, use_mini_mask=False) molded_images = np.expand_dims(modellib.mold_image(image, config), 0) # Run object detection results = model.detect([image], verbose=0) r = results[0] # Compute AP AP, precisions, recalls, overlaps = \ utils.compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks']) APs.append(AP) mAP = np.mean(APs) return mAP
def evaluate(model, dataset_dir): f = open("rgbdi_mAP.txt", "a+") # Compute VOC-Style mAP @ IoU=0.5 # Running on 10 images. Increase for better accuracy. image_ids = val_examples APs = [] dataset_val = TrafficDataset() dataset_val.load_traffic(dataset_dir, "val") dataset_val.prepare() for i, image_id in enumerate(image_ids): # Load image and ground truth data image, image_meta, gt_class_id, gt_bbox, gt_mask = \ modellib.load_image_gt(dataset_val, inference_config, i, use_mini_mask=False) molded_images = np.expand_dims( modellib.mold_image(image, inference_config), 0) print(image) print(i) # Run object detection results = model.detect([image], verbose=0) r = results[0] print(r) f.write(str(r)) # Compute AP AP, precisions, recalls, overlaps = \ utils.compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks']) print('AP: %f' % AP) APs.append(AP) f.write(str(AP)) print("mAP: ", np.mean(APs)) f.write("mAP: %f" % np.mean(APs))
def predicted_unseen(dataset, model, cfg, n_images=25, output_path=output_path): # load image and mask for i in range(n_images): #range(0, n_images*4, 5): # load the image and mask image = dataset.load_image(i) # convert pixel values (e.g. center) scaled_image = mold_image(image, cfg) # convert image into one sample sample = np.expand_dims(scaled_image, 0) # make prediction yhat = model.detect(sample, verbose=0)[0] # define subplot plt.figure(i, figsize=(5,5)) # plot raw pixel data # plot each box for (x1, y1, x2, y2), state in zip(yhat['rois'], yhat['class_ids']): # calculate width and height of the box cv2.rectangle(image, (y1, x1), (y2, x2), color = (0, 255, 0) if state else (255, 0, 0), thickness=2) # cv2.putText(image, "On" if state else "Off", (y1-5, x1-5),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0) if state else (255, 0, 0), 1) plt.imshow(image) plt.title('Predicted') # plt.plot([0], [0], color=(0, 1, 0), label="On") # plt.plot([0], [0], color=(1, 0, 0), label="Off") # plt.legend() plt.savefig(output_path+f"real_results_{i}")
def evaluate(model, limit=0): """Runs construction site images dataset evaluation. Compute VOC-Style mAP @ IoU=0.5 limit: if not 0, it's the number of images to use for evaluation """ # Limit to a subset if limit: image_ids = np.random.choice(args.dataset.image_ids, limit) # With no limit, use all images from args.dataset else: image_ids = args.dataset.image_ids APs = [] for image_id in image_ids: # Load image and ground truth data image, image_meta, gt_class_id, gt_bbox, gt_mask =\ modellib.load_image_gt(args.dataset, inference_config, image_id, 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) r = results[0] # Compute AP AP, precisions, recalls, overlaps =\ utils.compute_ap(gt_bbox, gt_class_id, r["rois"], r["class_ids"], r["scores"]) APs.append(AP) print("mAP: ", np.mean(APs))
def evaluate_ap(model, dataset, inference_config, coco, limit=0, image_ids=None): # Pick COCO images from the dataset image_ids = image_ids or dataset.image_ids # Limit to a subset if limit: image_ids = image_ids[:limit] # Get corresponding COCO image IDs. coco_image_ids = [dataset.image_info[id]["id"] for id in image_ids] t_prediction = 0 t_start = time.time() results = [] APs = [] for image_id in image_ids: # Load image and ground truth data image, image_meta, gt_class_id, gt_bbox, gt_mask =\ modellib.load_image_gt(dataset_val, inference_config, image_id) molded_images = np.expand_dims(modellib.mold_image(image, inference_config), 0) # Run object detection results = model.detect([image], verbose=0) r = results[0] # Compute AP AP, precisions, recalls, overlaps =\ utils.compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks']) APs.append(AP) print("AP for {}: {}".format(image_id, AP)) print("mAP for {} images: {}".format(len(image_ids),np.mean(APs)))
def plot_maskrcnn(dataset, model, cfg, image_id): image = dataset.load_image(image_id) scaled_image = mold_image(image, cfg) sample = np.expand_dims(scaled_image, 0) yhat = model.detect(sample, verbose=1)[0] display_instances(image, yhat['rois'], yhat['masks'], yhat['class_ids'], ['background', 'human'], yhat['scores'])
def evalution_fun(dataset_val): # Compute VOC-Style mAP @ IoU=0.5 # Running on 10 images. Increase for better accuracy. inference_config = InferenceConfig() # Recreate the model in inference mode model = modellib.MaskRCNN(mode="inference", config=inference_config, model_dir=MODEL_DIR) # model_path = os.path.join(ROOT_DIR, ".h5 file name here") model_path = model.find_last() # Load trained weights print("[Evalution]: Loading weights from ", model_path) model.load_weights(model_path, by_name=True) image_ids = np.random.choice(dataset_val.image_ids, 10) APs = [] for image_id in image_ids: # Load image and ground truth data image, image_meta, gt_class_id, gt_bbox, gt_mask = \ modellib.load_image_gt(dataset_val, inference_config, image_id, 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) r = results[0] # Compute AP AP, precisions, recalls, overlaps = \ utils.compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks']) APs.append(AP) print("mAP: ", np.mean(APs))
def evaluate(dataset, model, cfg): ''' computes the mean average precision (mAP) of a provided dataset using the provided model and configuration :param - dataset <LicensePlateDataset>: dataset to evaluate mAP on - model <mrcnn.model.MaskRCNN>: model used to evalute mAP - cfg <mrcnn.config.Config>: configurations for the mrcnn model :return - <float>: value for mAP ''' APs = [] for idx, img_id in enumerate(dataset.image_ids): img, img_meta, gt_class_id, gt_bbox, gt_mask = load_image_gt( dataset, cfg, img_id, use_mini_mask=False) sample = np.expand_dims(mold_image(img, cfg), 0) pred = model.detect(sample) AP, _, _, _ = compute_ap(gt_bbox, gt_class_id, gt_mask, pred[0]['rois'], pred[0]['class_ids'], pred[0]['scores'], pred[0]['masks']) APs.append(AP) return np.mean(APs)
def visualize_segmentations(self, config, model): # Validation dataset dataset_val = CocoDataset() val_type = "train" coco = dataset_val.load_coco(val_type, return_coco=True) dataset_val.prepare() # Test on a random image image_id = random.choice(dataset_val.image_ids) # Test on a specific image image_to_test = '2010_000898.jpg' saved_index = -1 for index, value in enumerate(dataset_val.image_info): file = value['path'] info = file.rsplit('/', 1) file_name = info[1] if file_name == image_to_test: saved_index = index image_id = saved_index original_image, image_meta, gt_class_id, gt_bbox, gt_mask = modellib.load_image_gt(dataset_val, config, image_id, use_mini_mask=False) log("original_image", original_image) log("image_meta", image_meta) log("gt_class_id", gt_class_id) log("gt_bbox", gt_bbox) log("gt_mask", gt_mask) visualize.display_instances(original_image, gt_bbox, gt_mask, gt_class_id, dataset_val.class_names, figsize=(8, 8), name='ground_truth.png') results = model.detect([original_image], verbose=1) r = results[0] visualize.display_instances(original_image, r['rois'], r['masks'], r['class_ids'], dataset_val.class_names, r['scores'], ax=get_ax(), name='predicted.png') # Compute VOC-Style mAP @ IoU=0.5 # Running on 10 images. Increase for better accuracy. image_ids = np.random.choice(dataset_val.image_ids, 10) APs = [] for image_id in image_ids: # Load image and ground truth data image, image_meta, gt_class_id, gt_bbox, gt_mask = modellib.load_image_gt(dataset_val, config, image_id, use_mini_mask=False) molded_images = np.expand_dims(modellib.mold_image(image, config), 0) # Run object detection results = model.detect([image], verbose=0) r = results[0] # Compute AP AP, precisions, recalls, overlaps = utils.compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks']) APs.append(AP) print("mAP: ", np.mean(APs))
def evaluate_pr_curve(dataset_val, inference_config, model): # Running on 10 images. Increase for better accuracy. image_ids = dataset_val.image_ids print(len(dataset_val.image_ids)) APs = [] F1s = [] ARs = [] runtimes = [] i = 0 tps = 0 fps = 0 fns = 0 precisions = [] recalls = [] tp_fp_fns = pd.DataFrame(columns=["tp", "fp", "fn", "score"]) for image_id in image_ids: # Load image and ground truth data image, image_meta, gt_class_id, gt_bbox, gt_mask = \ modellib.load_image_gt(dataset_val, inference_config, image_id, use_mini_mask=False) molded_images = np.expand_dims( modellib.mold_image(image, inference_config), 0) # Run object detection start_time = timeit.timeit() results = model.detect([image], verbose=0) end_time = timeit.timeit() runtimes.append(end_time - start_time) r = results[0] # Compute AP _, _, _, tp_fp_fn = \ utils.compute_matches(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks']) tp_fp_fns = tp_fp_fns.append(tp_fp_fn, ignore_index=True) if i % 100 == 0: print(i) if i > 1000: break i += 1 tp_fp_fns = tp_fp_fns.sort_values(by="score", ascending=False) print(tp_fp_fns) tp_fp_fns["acc tp"] = tp_fp_fns["tp"].expanding(1).sum() tp_fp_fns["acc fp"] = tp_fp_fns["fp"].expanding(1).sum() tp_fp_fns["acc fn"] = tp_fp_fns["fn"].expanding(1).sum() print(tp_fp_fns) tp_fp_fns["precision"] = tp_fp_fns["acc tp"] / (tp_fp_fns["acc tp"] + tp_fp_fns["acc fp"]) tp_fp_fns["recall"] = tp_fp_fns["acc tp"] / (tp_fp_fns["acc tp"] + tp_fp_fns["acc fn"]) print(tp_fp_fns) tp_fp_fns.to_csv(f"precision_recalls_{args.data_set}_{args.strategy}.csv") print(len(APs), len(F1s)) np.save(f"runtimes_CPU_{args.data_set}_{args.strategy}.npy", np.array(runtimes)) return np.mean(APs), np.mean(F1s), np.mean(ARs)
def run_map(out_path, num2_1, num2_2): dataset_val = ExpressDataset() dataset_val.out_path(out_path) dataset_val.set_dataset_class('val') dataset_val.set_dataset_exit_flag(True) dataset_val.load_shapes(num2_1, num2_2, 0) dataset_val.prepare() class InferenceConfig(ExpressConfig): GPU_COUNT = 1 IMAGES_PER_GPU = 1 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 # Either set a specific path or find last trained weights # model_path = os.path.join(ROOT_DIR, ".h5 file name here") #model_path = '/home/kingqi/proj/Mask_RCNN/log/express20190424T1751/mask_rcnn_express_0006.h5' model_path = model.find_last() # Load trained weights print("Loading weights from ", model_path) model.load_weights(model_path, by_name=True) image_ids = np.random.choice(dataset_val.image_ids, num2_1 + num2_2 * 6) APs = [] for image_id in image_ids: # Load image and ground truth data image, image_meta, gt_class_id, gt_bbox, gt_mask = modellib.load_image_gt( dataset_val, inference_config, image_id, use_mini_mask=False) molded_images = np.expand_dims( modellib.mold_image(image, inference_config), 0) image1 = dataset_val.load_image(image_id) mask1, class_ids1 = dataset_val.load_mask(image_id) bbox = utils.extract_bboxes(mask1) # visualize.display_top_masks(image, mask, class_ids, dataset_train.class_names) #visualize.display_instances(image1, bbox, mask1, class_ids1, dataset_val.class_names) # Run object detection results = model.detect([image], verbose=0) r = results[0] # visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], # dataset_val.class_names, r['scores']) # Compute AP AP, precisions, recalls, overlaps = utils.compute_ap( gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks']) APs.append(AP) print("mAP: ", np.mean(APs))
def my_evaluation(dataset, model, cfg, n): # load image and mask on_coverages = [] num_ions = [] for i in range(n): # load the image and mask image = dataset.load_image(i) boxes, states = dataset.get_actuals(i) # convert pixel values (e.g. center) scaled_image = mold_image(image, cfg) # convert image into one sample sample = np.expand_dims(scaled_image, 0) # make prediction yhat = model.detect(sample, verbose=0)[0] # define subplot correct_bright_ions = 0 correct_dark_ions = 0 false_bright_ions = 0 false_dark_ions = 0 missed_bright_ions = 0 missed_dark_ions = 0 actuals = list(zip(boxes.copy(), states.copy())) for (x1, y1, x2, y2), pred_state in zip(yhat['rois'], yhat['class_ids']): centre = ((x2+x1)//2, (y2+y1)//2) for box, act_state in actuals: # print(centre, box) # if the predicted centre is inside an actual location and their classes match, then count it if pred_state == act_state and is_inside(centre, box): # print(centre, box) actuals.remove((box, act_state)) if act_state: correct_bright_ions += 1 else: correct_dark_ions += 1 break else: if pred_state: false_bright_ions += 1 else: false_dark_ions += 1 for box, act_state in actuals: if act_state: missed_bright_ions += 1 else: missed_dark_ions += 1 num_ions.append(len(states)) on_coverages.append(correct_bright_ions/sum(states)) return on_coverages, num_ions
def evaluate_model(dataset, model, cfg): APs = list() num_boxes_list = [] pred_num_boxes_list = [] box_errors = [] #print('dataset imageid', dataset.image_id) for image_id in dataset.image_ids: print('image_id', image_id) # load image, bounding boxes and masks for the image id image, image_meta, gt_class_id, gt_bbox, gt_mask = load_image_gt( dataset, cfg, image_id, use_mini_mask=False) print('image size', image.size) # convert pixel values (e.g. center) scaled_image = mold_image(image, cfg) # convert image into one sample sample = expand_dims(scaled_image, 0) # make prediction yhat = model.detect(sample, verbose=0) # extract results for first sample r = yhat[0] #print('rois', r['rois']) #print('class ids', r['class_ids']) #print('scores', r['scores']) #print('gt bbox', gt_bbox) #print('gt class id', gt_class_id) #calculate number of boxes as compared to predicted number of boxes #then calculate percent error num_boxes = len(r['rois']) pred_num_boxes = len(gt_bbox) num_boxes_list.append(num_boxes) pred_num_boxes_list.append(pred_num_boxes) if num_boxes == 0 and pred_num_boxes == 0: box_error = 0 elif num_boxes == 0 and pred_num_boxes != 0: box_error = 1 else: box_error = abs(pred_num_boxes - num_boxes) / num_boxes box_errors.append(box_error) #print('num_boxes', num_boxes) #print('pred_num_boxes', pred_num_boxes) #print('box_error', box_error) # calculate statistics, including AP AP, _, _, _ = compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks']) print('AP', AP) if np.isnan(AP): AP = 0 # store APs.append(AP) # calculate the mean AP across all images mAP = mean(APs) mboxerrors = mean(box_errors) return mAP, mboxerrors, num_boxes_list, pred_num_boxes_list, APs
def predict(): import numpy as np from mrcnn.model import MaskRCNN from mrcnn.model import mold_image import skimage.io model_config = config.PredictionConfig() model = MaskRCNN(mode='inference', model_dir='./', config=model_config) model.keras_model.metrics_tensors = [] # load model weights model.load_weights(config.keras_model_dir, by_name=True) dir_path = '../data/test' outfile = open('../data/submission.csv', 'w') for id in range(1, 1515): # 读取文件报错,暂不知道解决方案 if id == 50: outfile.write('{},{},{}\n'.format(id, 5, 0)) continue elif id == 227: outfile.write('{},{},{}\n'.format(id, 1, 1)) continue elif id == 1201: outfile.write('{},{},{}\n'.format(id, 2, 0)) continue try: file_path = '{}/{}.jpg'.format(dir_path, id) image = skimage.io.imread(file_path) scaled_image = mold_image(image, model_config) sample = np.expand_dims(scaled_image, 0) yhat = model.detect(sample, verbose=0)[0] except: print(file_path) continue # 按照得分进行排序 indices = np.argsort(yhat["scores"])[::-1] boxes = [] for i in range(len(indices)): boxes.append([ yhat["class_ids"][i] - 1, yhat['rois'][i][1], yhat['rois'][i][0], yhat['rois'][i][3], yhat['rois'][i][2] ]) boxes = np.array(boxes) boxes = boxes[indices] hat = 0 person = 0 for box in boxes: label = box[0] if label == 0: hat += 1 else: person += 1 outfile.write('{},{},{}\n'.format(id, hat, person)) outfile.close()