()
Test the unified segmenter.
Test the unified segmenter.
Test the unified segmenter .
def test_main():
    '''
    Test the unified segmenter.
    '''
    from PIL import Image
    testim = Image.open('script/testdata/test_church_242.jpg')
    tensor_im = (torch.from_numpy(numpy.asarray(testim)).permute(2, 0, 1)
            .float() / 255 * 2 - 1)[None, :, :, :].cuda()
    segmenter = UnifiedParsingSegmenter()
    seg = segmenter.segment_batch(tensor_im)
    bc = torch.bincount(seg.view(-1))
    labels, cats = segmenter.get_label_and_category_names()
    for label in bc.nonzero()[:,0]:
        if label.item():
            # What is the prediction for this class?
            pred, mask = segmenter.predict_single_class(tensor_im, label.item())
            assert mask.sum().item() == bc[label].item()
            assert len(((seg == label).max(1)[0] - mask).nonzero()) == 0
            inside_pred = pred[mask].mean().item()
            outside_pred = pred[~mask].mean().item()
            print('%s (%s, #%d): %d pixels, pred %.2g inside %.2g outside' %
                (labels[label.item()] + (label.item(), bc[label].item(),
                    inside_pred, outside_pred)))
def test_main ( ) : from PIL import Image testim = Image . open ( 'script/testdata/test_church_242.jpg' ) tensor_im = ( torch . from_numpy ( numpy . asarray ( testim ) ) . permute ( 2 , 0 , 1 ) . float ( ) / 255 * 2 - 1 ) [ None , : , : , : ] . cuda ( ) segmenter = UnifiedParsingSegmenter ( ) seg = segmenter . segment_batch ( tensor_im ) bc = torch . bincount ( seg . view ( - 1 ) ) labels , cats = segmenter . get_label_and_category_names ( ) for label in bc . nonzero ( ) [ : , 0 ] : if label . item ( ) : # What is the prediction for this class? pred , mask = segmenter . predict_single_class ( tensor_im , label . item ( ) ) assert mask . sum ( ) . item ( ) == bc [ label ] . item ( ) assert len ( ( ( seg == label ) . max ( 1 ) [ 0 ] - mask ) . nonzero ( ) ) == 0 inside_pred = pred [ mask ] . mean ( ) . item ( ) outside_pred = pred [ ~ mask ] . mean ( ) . item ( ) print ( '%s (%s, #%d): %d pixels, pred %.2g inside %.2g outside' % ( labels [ label . item ( ) ] + ( label . item ( ) , bc [ label ] . item ( ) , inside_pred , outside_pred ) ) )