Giter Club home page Giter Club logo

Comments (4)

wuhuikai avatar wuhuikai commented on June 12, 2024 1

Please run this script to get the predicted label maps.

from fastfcn.

qiulesun avatar qiulesun commented on June 12, 2024

@wuhuikai , Thanks for your replying. How do I get segmentation ground truth of both training and val sets of PContext?

from fastfcn.

wuhuikai avatar wuhuikai commented on June 12, 2024
import os
import numpy as np

import torch

from PIL import Image
from tqdm import trange
from detail import Detail

def get_voc_pallete(num_cls):
    n = num_cls
    pallete = [0]*(n*3)
    for j in range(0,n):
            lab = j
            pallete[j*3+0] = 0
            pallete[j*3+1] = 0
            pallete[j*3+2] = 0
            i = 0
            while (lab > 0):
                    pallete[j*3+0] |= (((lab >> 0) & 1) << (7-i))
                    pallete[j*3+1] |= (((lab >> 1) & 1) << (7-i))
                    pallete[j*3+2] |= (((lab >> 2) & 1) << (7-i))
                    i = i + 1
                    lab >>= 3
    return pallete

if not os.path.exists('gts'):
    os.makedirs('gts')

root=os.path.expanduser('~/.encoding/data')
BASE_DIR = 'VOCdevkit/VOC2010'
NUM_CLASS = 59

#from detail import mask
root = os.path.join(root, BASE_DIR)
annFile = os.path.join(root, 'trainval_merged.json')
imgDir = os.path.join(root, 'JPEGImages')
detail = Detail(annFile, imgDir, 'val')
ids = detail.getImgs()
# generate masks
_mapping = np.sort(np.array([
    0, 2, 259, 260, 415, 324, 9, 258, 144, 18, 19, 22,
    23, 397, 25, 284, 158, 159, 416, 33, 162, 420, 454, 295, 296,
    427, 44, 45, 46, 308, 59, 440, 445, 31, 232, 65, 354, 424,
    68, 326, 72, 458, 34, 207, 80, 355, 85, 347, 220, 349, 360,
    98, 187, 104, 105, 366, 189, 368, 113, 115]))
_key = np.array(range(len(_mapping))).astype('uint8')
mask_file = os.path.join(root, 'val.pth')
print('mask_file:', mask_file)

def _class_to_index(mask):
    # assert the values
    values = np.unique(mask)
    for i in range(len(values)):
        assert(values[i] in _mapping)
    index = np.digitize(mask.ravel(), _mapping, right=True)
    return _key[index].reshape(mask.shape)

def _preprocess(mask_file):
    masks = {}
    tbar = trange(len(ids))
    print("Preprocessing mask, this will take a while." + \
        "But don't worry, it only run once for each split.")
    for i in tbar:
        img_id = ids[i]
        mask = Image.fromarray(_class_to_index(detail.getMask(img_id)))
        masks[img_id['image_id']] = mask
        tbar.set_description("Preprocessing masks {}".format(img_id['image_id']))
    torch.save(masks, mask_file)
    return masks

if os.path.exists(mask_file):
    masks = torch.load(mask_file)
else:
    masks = _preprocess(mask_file)

for i in trange(len(ids)):
    img_id = ids[i]

    path = os.path.basename(img_id['file_name'])

    mask = masks[img_id['image_id']]
    mask.putpalette(get_voc_pallete(256))

    outname = os.path.splitext(path)[0] + '.png'
    mask.save(os.path.join('gts', outname))

from fastfcn.

wuhuikai avatar wuhuikai commented on June 12, 2024

You can use above script to generate val labels. With minor modification, it can be used to generate train labels. Sorry for the bad code style 😭

from fastfcn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.