Пример #1
0
 def write_coco_stuff_results(self, stuff_results, res_file):
     with io.open(res_file, 'w', encoding='utf8') as output:
         print('Writing results json to %s' % res_file)
         # Annotation start
         output.write(unicode('[\n'))
         for i, img_id in enumerate(self.imageset_index):
             stuff_result = stuff_results[i]
             assert stuff_result.shape[0] == self.coco.loadImgs(img_id)[0]['height']
             assert stuff_result.shape[1] == self.coco.loadImgs(img_id)[0]['width']
             if not self.stuff_order:
                 for j in range(self.num_classes):
                     stuff_result[stuff_result == j] = self._class_ind_to_coco_ind[j]
                 stuff_result[stuff_result == -1] = 183
             else:
                 stuff_result[stuff_result == -1] = 0
             anns = segmentationToCocoResult(stuff_result.astype(np.uint8), img_id, stuffStartId=self.stuffStartId)
             # Write JSON
             str_ = json.dumps(anns)
             str_ = str_[1:-1]
             if len(str_) > 0:
                 output.write(unicode(str_))
             # Add comma separator
             if i < len(self.imageset_index) - 1 and len(str_) > 0:
                 output.write(unicode(','))
             # Add line break
             output.write(unicode('\n'))
         # Annotation end
         output.write(unicode(']'))
def makePrediction(config_obj, image_path, model, cuda=True, crf=False):
    torch.set_grad_enabled(False)

    image_id = extractIdFromPath(image_path)

    # Image preprocessing
    image = cv2.imread(image_path, cv2.IMREAD_COLOR).astype(float)
    image = preProcessImage(image, config_obj, cuda)

    # Inference
    output = model(image)
    output = F.interpolate(output,
                           size=image.shape[2:],
                           mode="bilinear",
                           align_corners=True)
    output = F.softmax(output, dim=1)
    output = output.data.cpu().numpy()[0]

    if crf:
        output = dense_crf(image_original, output)
    labelmap = np.argmax(output, axis=0)

    cocoResFormat = cocostuff.segmentationToCocoResult(labelmap, image_id)

    return cocoResFormat
Пример #3
0
def showSegmentationModelGenerated(model, data, dataLoader, numToShow=10):
  cocoData = coco.COCO(annotation_file='cocostuffapimaster/annotations/stuff_val2017.json')
  categories = cocoData.loadCats(cocoData.getCatIds())

  n = 0

  for (idx, batch) in enumerate(dataLoader):
    plt.figure()
    batch_x = batch[0]
    batch_y = batch[1].to(torch.int64)
    pad = batch[2]
    imgId = batch[3][0]

    with torch.no_grad():
      outputs = model.forward(batch_x.cuda()).cpu()
    del batch_x
    del batch_y

    img = cocoData.loadImgs(imgId)[0]

    I = skimage.io.imread(img['coco_url'])
    plt.subplot(121)
    plt.imshow(I)
    plt.axis('off')
    plt.title('original image')
    # Load and display stuff annotations
    mask = torch.argmax(outputs[0],0)


    idCount = torch.bincount(mask.reshape(-1), weights=None)
    cats = {}
    for category in categories:
      if idCount[category['id']] != 0:
        cats[category['name']] = idCount[category['id']].item()
    print(cats)
    anns = segmentationToCocoResult(mask, imgId)
    plt.subplot(122)
    plt.imshow(I)
    cocoData.showAnns(anns)
    plt.axis('off')
    plt.title('annotated image')    
    plt.show()
    n += 1
    if n == numToShow:
      break
Пример #4
0
def main():
    """main function

    Main function... (what do you expect me to say...)

    Args:
        - none

    Returns:
        - none
    """

    # Main function for evaluate
    parser = argparse.ArgumentParser(description="Evaluate the model")
    parser.add_argument(
        "--net",
        help=
        "The type of net work which is either mrcnn, unet, deeplab or custom.",
        required=True,
        default="unet")
    parser.add_argument("--img_size",
                        required=True,
                        type=int,
                        help="The size of input image")
    parser.add_argument("--gpu_id",
                        required=False,
                        default="0",
                        type=str,
                        help="The id of the gpu used when training.")
    parser.add_argument(
        "--weight_path",
        required - False,
        defalut=None,
        type=str,
        help=
        "The name of trained weights. If not specified, the model will use 'net_imgSize.h5'. "
    )

    # Parse argument
    args = parser.parse_args()
    net_type = args.net
    gpu_number = args.gpu_id
    img_size = args.img_size
    weight_path = args.weight_path

    import os
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    os.environ["CUDA_VISIBLE_DEVICES"] = gpu_number
    # Argument check
    if not (net_type in {"unet", "deeplab", "custom", "mrcnn"}):
        raise ValueError(
            "netType should be either unet, deeplab, mrcnn and custom.")

    if net_type in {"unet", "deeplab", "custom"}:

        # Get config
        Config = cfg.Config()

        # COCO instance
        print("Reading COCO ground truth...")
        cocoGt = COCO(Config.COCO_training_ann_path)
        cocoValGt = COCO(Config.COCO_validation_ann_path)
        print("Finished")

        # Get all classes
        classes = len(cocoGt.getCatIds())

        id_to_index = dict()
        # There is a wired class of 0 in the feature map of type zero
        index_to_id = dict()

        # Because the id of COCO dataset starts from 92, we should project those id to index so that keras
        # utils can convert the segmentation map into one hot categorical encoding.
        for index, id in enumerate(cocoGt.getCatIds()):
            id_to_index[id] = index
            index_to_id[index] = id

        if net_type == "unet":
            model = basic_model.unet(input_size=(img_size, img_size, 3),
                                     classes=len(id_to_index))
        elif net_type == "deeplab":
            deeplab_model = basic_model.Deeplabv3(input_shape=(img_size,
                                                               img_size, 3),
                                                  classes=len(id_to_index),
                                                  backbone="xception")
            output = KL.Activation("softmax")(deeplab_model.output)
            model = KM.Model(deeplab_model.input, output)
        elif net_type == "custom":
            model = model.custom_model(input_shape=(img_size, img_size, 3),
                                       classes=len(id_to_index))

    if net_type in {"unet", "deeplab", "custom"}:

        file_list = glob(Config.COCO_training_path + '*')
        val_list = glob(Config.COCO_validation_path + '*')

        if weight_path in None:
            model.load_weights(net_type + "_" + str(img_size) + ".h5")
            print("weights loaded!")
        else:
            model.load_weights(weight_path)
            print("weights loaded!")

        #model.compile(optimizer = KO.Adam(clipvalue=2.), loss="categorical_crossentropy", metrics=["accuracy"])
        model.compile(optimizer=KO.Adam(),
                      loss="categorical_crossentropy",
                      metrics=["accuracy"])
        print("Prediction start...")

        vfunc = np.vectorize(lambda index: index_to_id[index])

        anns = []

        # Convert into COCO annotation
        for i in trange(len(val_list)):
            image = val_list[i]
            image_id = int(image.replace(".jpg", '')[-12:])

            cropping_image, padding_dims, original_size = utils.padding_and_cropping(
                image, (img_size, img_size))
            cropping_image = preprocess_input(cropping_image, mode="torch")

            result = model.predict(cropping_image)
            result = np.argmax(result, axis=3)

            seg_result = utils.reverse_padding_and_cropping(
                result, padding_dims, original_size)
            seg_result = vfunc(seg_result)
            COCO_ann = cocostuffhelper.segmentationToCocoResult(seg_result,
                                                                imgId=image_id)
            for ann in COCO_ann:
                ann["segmentation"]["counts"] = ann["segmentation"][
                    "counts"].decode("ascii")  # json can't dump byte string
            anns += COCO_ann

        with open("result.json", "w") as file:
            json.dump(anns, file)

        # Evaluate result
        resFile = "result.json"
        cocoDt = cocoValGt.loadRes(resFile)
        cocoEval = COCOStuffeval(cocoValGt, cocoDt)
        cocoEval.evaluate()
        cocoEval.summarize()
Пример #5
0
def main():
    """main function

    Main function... (what do you expect me to say...)

    Args:
        - none

    Returns:
        - none
    """

    # Main function for evaluate
    parser = argparse.ArgumentParser(
        description="A testing framework for semantic segmentation.")
    parser.add_argument(
        "--net",
        required=True,
        default="unet",
        type=str,
        help=
        "(str) The type of net work which is either unet, deeplab or custom.")
    parser.add_argument("--epochs", required=False, default=500, type=int)
    parser.add_argument("--batch_size", required=False, default=16, type=int)
    parser.add_argument("--gpu_id",
                        required=False,
                        default="0",
                        type=str,
                        help="(str) The id of the gpu used when training.")
    parser.add_argument("--img_size",
                        required=False,
                        default=192,
                        type=int,
                        help="(int) The size of input image")
    parser.add_argument(
        "--load_weights",
        required=False,
        default=False,
        type=bool,
        help="(bool) Use old weights or not (named net_imgSize.h5)")

    # Parse argument
    args = parser.parse_args()
    net_type = args.net
    epochs = args.epochs
    batch_size = args.batch_size
    gpu_number = args.gpu_id
    img_size = args.img_size

    import os
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    os.environ["CUDA_VISIBLE_DEVICES"] = gpu_number
    # Argument check
    if not (net_type in {"unet", "deeplab", "custom"}):
        raise ValueError("netType should be either unet, deeplab and custom.")

    # Get config
    Config = cfg.Config()

    # COCO instance
    print("Reading COCO ground truth...")
    cocoGt = COCO(Config.COCO_training_ann_path)
    cocoValGt = COCO(Config.COCO_validation_ann_path)
    print("Finished")

    # Get all classes
    classes = len(cocoGt.getCatIds())

    id_to_index = dict()
    # There is a wired class of 0 in the feature map of type zero
    index_to_id = dict()

    # Because the id of COCO dataset starts from 92, we should project those id to index so that keras
    # utils can convert the segmentation map into one hot categorical encoding.
    for index, id in enumerate(cocoGt.getCatIds()):
        id_to_index[id] = index
        index_to_id[index] = id

    if net_type == "unet":
        model = basic_model.unet(input_size=(img_size, img_size, 3),
                                 classes=len(id_to_index))
    elif net_type == "deeplab":
        deeplab_model = basic_model.Deeplabv3(input_shape=(img_size, img_size,
                                                           3),
                                              classes=len(id_to_index),
                                              backbone="xception")
        output = KL.Activation("softmax")(deeplab_model.output)
        model = KM.Model(deeplab_model.input, output)
    elif net_type == "custom":
        model = model.custom_model(input_shape=(img_size, img_size, 3),
                                   classes=len(id_to_index))

    file_list = glob(Config.COCO_training_path + '*')
    val_list = glob(Config.COCO_validation_path + '*')

    if args.load_weights:
        try:
            model.load_weights(net_type + "_" + str(img_size) + ".h5")
            print("weights loaded!")
        except:
            print("weights not found!")

    checkpointer = KC.ModelCheckpoint(filepath=net_type + "_" + str(img_size) +
                                      ".h5",
                                      verbose=1,
                                      save_best_only=True)

    model.compile(optimizer=KO.Adam(),
                  loss="categorical_crossentropy",
                  metrics=["accuracy"])
    model.fit_generator(data.generator(batch_size, file_list,
                                       (img_size, img_size), cocoGt,
                                       id_to_index, True),
                        validation_data=data.generator(batch_size, val_list,
                                                       (img_size, img_size),
                                                       cocoValGt, id_to_index,
                                                       False),
                        validation_steps=10,
                        steps_per_epoch=100,
                        epochs=epochs,
                        use_multiprocessing=True,
                        workers=8,
                        callbacks=[checkpointer])
    print("Prediction start...")

    vfunc = np.vectorize(lambda index: index_to_id[index])

    anns = []

    # Convert into COCO annotation
    for i in trange(len(val_list)):
        image = val_list[i]
        image_id = int(image.replace(".jpg", '')[-12:])

        cropping_image, padding_dims, original_size = utils.padding_and_cropping(
            image, (img_size, img_size))
        cropping_image = preprocess_input(cropping_image, mode="torch")

        result = model.predict(cropping_image)
        result = np.argmax(result, axis=3)

        seg_result = utils.reverse_padding_and_cropping(
            result, padding_dims, original_size)
        seg_result = vfunc(seg_result)
        COCO_ann = cocostuffhelper.segmentationToCocoResult(seg_result,
                                                            imgId=image_id)
        for ann in COCO_ann:
            ann["segmentation"]["counts"] = ann["segmentation"][
                "counts"].decode("ascii")  # json can't dump byte string
        anns += COCO_ann

    with open("result.json", "w") as file:
        json.dump(anns, file)

    # Read result file
    # Test for fake result
    #resFile = Config.fake_result

    # Evaluate result
    resFile = "result.json"
    cocoDt = cocoValGt.loadRes(resFile)
    cocoEval = COCOStuffeval(cocoValGt, cocoDt)
    cocoEval.evaluate()
    cocoEval.summarize()
Пример #6
0
    len_val = len(rgb_vallist)
    len_test = len(rgb_testlist)
    train_anns = {}
    val_anns = {}

    generate annotations data for train images
    train_anns = []
    data = {}
    t_ann_count = 0
    for i in range(0,1000):
        im_name = rgb_trainlist[i]['name']
        im_id = rgb_trainlist[i]['id']
        iend = im_name.find('_domain')
        im_name = im_name[0:iend]
        labmap = gen_labelmap(im_name) #generate label map
        t_ann = cocohelp.segmentationToCocoResult(labmap,int(im_id),stuffStartId=0)
        train_anns.append(t_ann)
        t_ann_count+=1
        print("train ann count:", t_ann_count)
    
    data["annotations"] = train_anns
    
    #create train_anns2.json
    with open('train_anns2.json','w') as outfile:
        json.dump(train_anns,outfile,sort_keys=True,indent=4)
    print("finished generating train_anns2.json")
    
    generate annotations data for val images
    val_anns = []
    data = {}
    v_ann_count = 0