Ejemplo n.º 1
0
def validation_phase_mIoU(history, main_dict, model, val_set, 
                          predict_name, epoch):
    category2name = lambda x: "person" if x == 1 else "car"
    iou_mean = {"person":0, "car":0}
    iou_sum = {"person":0, "car":0}
    n_objects = {"person":0, "car":0}
    n_images = len(val_set)
    for i in range(n_images):
        
        batch = ms.get_batch(val_set, [i])
        maskObjects = batch["maskObjects"].squeeze()
        pred_dict = model.predict(batch, predict_method="BestDice")
    
        for ann in pred_dict["annList"]:
            point = ann["point"]
            category_id = point["category_id"]

            label = maskObjects[point["y"], point["x"]].item()
            assert label != 0
            A = maskObjects == label
            B = au.ann2mask(ann)["mask"]

            iou_sum[category2name(category_id)] += au.compute_iou(ms.t2n(A), ms.t2n(B))
            n_objects[category2name(category_id)] += 1

            # ms.images(batch["images"], A, denorm=1, win="GT")
            # ms.images(batch["images"], B, denorm=1,  win="Pred")

        for name in ["person", "car"]:
          iou_mean[name] = iou_sum[name] / max(1, n_objects[name])

        print("{}/{} - {:.3f} {:.3f}".format(i, n_images, iou_mean["person"], 
                  iou_mean["car"]))


    # iou_mean = iou_sum / n_objects
    val_dict = {}
    val_dict["iou_mean"] = iou_mean 
    val_dict["predict_name"] = predict_name
    val_dict["epoch"] = epoch
    val_dict["time"] = datetime.datetime.now().strftime("%b %d, 20%y")

    # Update history
    history["val"] += [val_dict]

    # Higher is better
    if (history["best_model"] == {} or 
        history["best_model"]["iou_mean"]["person"] <= val_dict["iou_mean"]["person"]):

      history["best_model"] = val_dict
      ms.save_best_model(main_dict, model)
      

    return history
Ejemplo n.º 2
0
def validation_phase_mAP(history, main_dict, model, val_set, predict_name, epoch):
    val_dict, pred_annList = au.validate(model, val_set, 
                predict_method=predict_name, 
                n_val=len(val_set), return_annList=True)
  
    val_dict["predict_name"] = predict_name
    val_dict["epoch"] = epoch
    val_dict["time"] = datetime.datetime.now().strftime("%b %d, 20%y")

    # Update history
    history["val"] += [val_dict]

    # Higher is better
    if (history["best_model"] == {} or 
        history["best_model"]["0.5"] <= val_dict["0.5"]):

      history["best_model"] = val_dict
      ms.save_best_model(main_dict, model)
      
      ms.save_pkl(main_dict["path_best_annList"], pred_annList)
      ms.copy_code_best(main_dict)

    return history
Ejemplo n.º 3
0
def validation_phase_MAE(history, main_dict, model, val_set, metric_class, verbose, metric_name, 
                         predict_name, epoch):
    val_dict = ms.validate(dataset=val_set, 
                    model=model, 
                    verbose=verbose, 
                    metric_class=metric_class, 
                    batch_size=1,
                    epoch=epoch)
    val_dict["predict_name"] = predict_name
    val_dict["epoch"] = epoch

    # Update history
    history["val"] += [val_dict]

    # Higher is better

    if (history["best_model"] == {} or 
        history["best_model"][metric_name] >= val_dict[metric_name]):

      history["best_model"] = val_dict
      ms.save_best_model(main_dict, model)

    return history