Mmdetection: error while using coco tools to show results

Created on 2 Nov 2018  ·  5Comments  ·  Source: open-mmlab/mmdetection

coco=COCO(annFile)

File "/home/wit/anaconda2/envs/py36/lib/python3.6/site-packages/pycocotools-2.0.0-py3.6-linux-x86_64.egg/pycocotools/coco.py", line 85, in __init__
assert type(dataset)==dict, 'annotation file format {} not supported'.format(type(dataset))
AssertionError: annotation file format not supported

It seems that the method of writing the results in test is not the same as the standard coco format, because it can still run normally when reading the training dataSet directly.

I think the problem lies in the outermost brackets and should be in braces. There is no line break for the entire document, which makes it very complicated to fix.

bug

Most helpful comment

here's my code to show compacted RLE results and polygon label:

import time
import mmcv
import cv2 as cv
import json
import numpy as np
import pycocotools.mask as maskutil
from itertools import groupby
from skimage import measure,draw

def mainShow():
    testimagepath   = "XX.json"
    compressedRLECOCOlabelpath = "XX.pkl.json"
    imageprefix     = "XX/test-images/"

    startTime = time.time()
    trthset = json.load(open(testimagepath, 'r'))
    assert type(trthset) == dict, 'annotation file format {} not supported'.format(type(trthset))
    prdcset = json.load(open(compressedRLECOCOlabelpath, 'r'))
    assert type(prdcset) == dict, 'annotation file format {} not supported'.format(type(prdcset))
    print('Done (t={:0.2f}s)'.format(time.time() - startTime))

    ann_Y0 = trthset['annotations']
    ann_Y1 = prdcset['annotations']

    for image in trthset['images']:
        imagepath = imageprefix+image['file_name']
        img = cv.imread(imagepath)

        src = np.zeros((768,768,3), np.uint8)
        src[:,:,:]=img[:,:,:]
        dst = np.zeros((768,768,3), np.uint8)
        dst[:,:,:]=img[:,:,:]

        masks = np.zeros((768, 768, 1), np.uint8)
        masks.fill(0)
        id0 = image['id']

        counts = 0

        for target in ann_Y0:
            if target['image_id']==id0:
                counts += 1
                j=0
                X=[]
                Y=[]
                for seg in target['segmentation'][0]:
                    if j == 0:
                        x = float(seg)
                        X.append(x)
                    else:
                        y = float(seg)
                        Y.append(y)
                    j = 1-j

                rr, cc = draw.polygon(Y, X)
                draw.set_color(src, [rr, cc], [0, 0, 255], 0.4)

                Point = np.zeros((len(Y), 2), dtype='int32')
                Point [:, 0] = X[:]
                Point [:, 1] = Y[:]
                #print(Point)
                cv.fillPoly(masks, np.array([Point],'int32'), 1)
        src[:, :, 0] = img[:, :, 0] #* 0.9 + masks[:, :, 0] * 0.1 * 255.0 / counts
        src[:, :, 1] = img[:, :, 1] #* 0.9 + masks[:, :, 0] * 0.1 * 255.0 / counts
        src[:, :, 2] = img[:, :, 2] * 0.2 + masks[:, :, 0] * 0.8 * 255.0 / counts

        mmcv.imshow(src,"Y",1)

        masks.fill(0)
        counts = 0
        for target in ann_Y1:
            if target['image_id']==id0:
                counts += 1
                CRLE    = target['segmentation']
                #print(CRLE)
                mask    = maskutil.decode(CRLE)
                mask    = np.reshape(mask, (img.shape[1], img.shape[0], 1))
                masks[:, :] = masks[:, :] + mask[:, :]

        dst[:, :, 0] = img[:, :, 0] * 0.2 + masks[:, :, 0] * 0.8 * 255.0/counts
        dst[:, :, 1] = img[:, :, 1] #* 0.5 + masks[:, :, 0] * 0.5 * 255.0/counts
        dst[:, :, 2] = src[:, :, 2] * 0.9 + masks[:, :, 0] * 0.1 * 255.0/counts
        mmcv.imshow(dst,"Y'")

    return 0

All 5 comments

and whats the meaning of

"segmentation": {
"size": [
768,
768
],
"counts": "`eQ66ig02O1O000000000000000000000000001O00000000000000000000000000000000000000000000000000000000O2O0NbZj:"
},

how could I translate it back to some thing like

"segmentation": [
[
388.0,
82.5,
382.5,
82.0,
383.0,
75.5,
413.5,
73.0,
414.0,
79.5,
388.0,
82.5
]

here's my codes to change lines

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
    ifstream src("xxx/maskrcnn.pkl.json");
    ofstream dst("xxx/maskrcnn.reorg.pkl.json");

    string getall;
    getline(src, getall);

    string space = "    ";
    size_t count_of_space = 0;
    size_t n = getall.size();
    bool inmark = false;
    for (size_t i = 0; i < n; ++i){
        char c = getall[i];
        if (c < 1)break;
        //printf("%c[%d/%d]\n", c, i, n);
        if (c == '\"'){
            inmark = !inmark;
        }
        if (inmark){
            dst << c;
            continue;
        }
        switch (c){
        case '[':
        case '{':
            count_of_space += 1;
            dst << c << '\n';
            for (size_t j = 0; j < count_of_space; ++j)dst << space;
            break;
        case ']':
            count_of_space -= 1;
            if (count_of_space < 0)count_of_space = 0;
            dst << '\n';
            for (size_t j = 0; j < count_of_space; ++j)dst << space;
            dst << ']';
            if (i + 3 < n){
                if (getall[i + 1] != ','&&getall[i + 2] != ',')dst << '\n';
            }
            break;
        case '}':
            count_of_space -= 1;
            if (count_of_space < 0)count_of_space = 0;
            dst << '\n';
            for (size_t j = 0; j < count_of_space; ++j)dst << space;
            dst << '}';
            if (i + 3 < n){
                if(getall[i + 1] != ','&&getall[i + 2] != ',')dst << '\n';
            }
            break;
        case ',':
            dst << ',';
        case '\n':
            dst << '\n';
            if (count_of_space < 0)count_of_space = 0;
            for (size_t j = 0; j < count_of_space; ++j)dst << space;
            break;
        default:
            dst << c;
            break;
        }
    }

    return 0;
}

here's my code to show compacted RLE results and polygon label:

import time
import mmcv
import cv2 as cv
import json
import numpy as np
import pycocotools.mask as maskutil
from itertools import groupby
from skimage import measure,draw

def mainShow():
    testimagepath   = "XX.json"
    compressedRLECOCOlabelpath = "XX.pkl.json"
    imageprefix     = "XX/test-images/"

    startTime = time.time()
    trthset = json.load(open(testimagepath, 'r'))
    assert type(trthset) == dict, 'annotation file format {} not supported'.format(type(trthset))
    prdcset = json.load(open(compressedRLECOCOlabelpath, 'r'))
    assert type(prdcset) == dict, 'annotation file format {} not supported'.format(type(prdcset))
    print('Done (t={:0.2f}s)'.format(time.time() - startTime))

    ann_Y0 = trthset['annotations']
    ann_Y1 = prdcset['annotations']

    for image in trthset['images']:
        imagepath = imageprefix+image['file_name']
        img = cv.imread(imagepath)

        src = np.zeros((768,768,3), np.uint8)
        src[:,:,:]=img[:,:,:]
        dst = np.zeros((768,768,3), np.uint8)
        dst[:,:,:]=img[:,:,:]

        masks = np.zeros((768, 768, 1), np.uint8)
        masks.fill(0)
        id0 = image['id']

        counts = 0

        for target in ann_Y0:
            if target['image_id']==id0:
                counts += 1
                j=0
                X=[]
                Y=[]
                for seg in target['segmentation'][0]:
                    if j == 0:
                        x = float(seg)
                        X.append(x)
                    else:
                        y = float(seg)
                        Y.append(y)
                    j = 1-j

                rr, cc = draw.polygon(Y, X)
                draw.set_color(src, [rr, cc], [0, 0, 255], 0.4)

                Point = np.zeros((len(Y), 2), dtype='int32')
                Point [:, 0] = X[:]
                Point [:, 1] = Y[:]
                #print(Point)
                cv.fillPoly(masks, np.array([Point],'int32'), 1)
        src[:, :, 0] = img[:, :, 0] #* 0.9 + masks[:, :, 0] * 0.1 * 255.0 / counts
        src[:, :, 1] = img[:, :, 1] #* 0.9 + masks[:, :, 0] * 0.1 * 255.0 / counts
        src[:, :, 2] = img[:, :, 2] * 0.2 + masks[:, :, 0] * 0.8 * 255.0 / counts

        mmcv.imshow(src,"Y",1)

        masks.fill(0)
        counts = 0
        for target in ann_Y1:
            if target['image_id']==id0:
                counts += 1
                CRLE    = target['segmentation']
                #print(CRLE)
                mask    = maskutil.decode(CRLE)
                mask    = np.reshape(mask, (img.shape[1], img.shape[0], 1))
                masks[:, :] = masks[:, :] + mask[:, :]

        dst[:, :, 0] = img[:, :, 0] * 0.2 + masks[:, :, 0] * 0.8 * 255.0/counts
        dst[:, :, 1] = img[:, :, 1] #* 0.5 + masks[:, :, 0] * 0.5 * 255.0/counts
        dst[:, :, 2] = src[:, :, 2] * 0.9 + masks[:, :, 0] * 0.1 * 255.0/counts
        mmcv.imshow(dst,"Y'")

    return 0

Have you solved it?

@Fengweinlpr Yes, there're two keys:

  1. Test json should only contains the testing result. It starts with '[' and ends with ']'. Its overview is more simple than the training json.
  1. Label style of segmentation. In old(2018) mmdet version, it generated compressed(compacted) RLE, but what I need is uncompressed RLE (or poly, I forgot).
    The code can be found in my blog: testing of mask translating and draw segmentation result

I haven't used segmentation in mmdet v2.0+. So I don't know whether it's your situation.

Was this page helpful?
0 / 5 - 0 ratings