예제 #1
0
def test(opt, test_loader, net, split):
    start_time = time.time()
    eva = Evaluator(opt.n_classes, opt.bg_err)
    eva_crf = Evaluator(opt.n_classes, opt.bg_err)
    ims = []
    labels = []

    net = net.eval()

    for iteration, batch in enumerate(test_loader):
        im, label = batch
        im = im.cuda()
        label = label.cuda()
        out = net(im)
        prob = F.softmax(out, dim=1)
        for i in range(opt.batch_size):
            prob_np = prob[i].detach().cpu().numpy()
            label_np = label[i].cpu().numpy()
            im_np = im[i].cpu().numpy()
            ims.append(to_image(im[i, :3, :, :]))
            labels.append(label_np)
            eva.register(label_np, prob_np)
            prob_crf = crf(prob_np, im_np, opt.sdims, opt.schan, opt.compat,
                           opt.iters)
            eva_crf.register(label_np, prob_crf)
        print(
            str(iteration * opt.batch_size + i).zfill(2),
            time.time() - start_time, 'seconds')

    msa, preds_msa, miou, miiou, preds_miou = eva.evaluate()
    msa_crf, preds_msa_crf, miou_crf, miiou_crf, preds_miou_crf = eva_crf.evaluate(
    )
    print('Pre-CRF:  MSA: {}   mIoU: {}   miIoU: {}'.format(
        round(msa * 100, 1), round(miou * 100, 1), round(miiou * 100, 1)))
    print('Post-CRF: MSA: {}   mIoU: {}   miIoU: {}'.format(
        round(msa_crf * 100, 1), round(miou_crf * 100, 1),
        round(miiou_crf * 100, 1)))
    for i, label in enumerate(labels):
        pred_msa = preds_msa[i]
        pred_msa_crf = preds_msa_crf[i]
        pred_miou = preds_miou[i]
        pred_miou_crf = preds_miou_crf[i]
        vis_im = ims[i]
        vis_label = colormap(label)
        vis_pred_msa = colormap(pred_msa)
        vis_pred_msa_crf = colormap(pred_msa_crf)
        vis_pred_miou = colormap(pred_miou)
        vis_pred_miou_crf = colormap(pred_miou_crf)
        vis_all = np.concatenate(
            (np.concatenate((vis_im, vis_label), axis=2),
             np.concatenate((vis_pred_miou, vis_pred_miou_crf), axis=2)),
            axis=1)
        vis_all = vis_all.transpose((1, 2, 0))
        io.imsave(
            Path(opt.out_path) / split / (str(i).zfill(2) + '.png'), vis_all)
    return msa, miou, miiou, msa_crf, miou_crf, miiou_crf
예제 #2
0
def train(opt, vis, epoch, train_loader, net, optimizer, scheduler):
    net = net.train()
    train_len = len(train_loader)
    start_time = time.time()
    scheduler.step()
    for iteration, batch in enumerate(train_loader):
        # Load Data
        im, label = batch
        im = im.cuda(non_blocking=True)
        label = label.cuda(non_blocking=True)

        # Forward Pass
        out = net(im)
        loss = F.cross_entropy(out, label)

        # Backward Pass
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        scheduler.batch_step()

        # Logging
        cur_time = time.time()
        loss_scalar = float(loss.cpu().detach().numpy())
        if iteration < opt.threads:
            print('{} [{}]({}/{}) AvgTime:{:>4} Loss:{:>4}'.format(opt.env, epoch, iteration, train_len, \
                                                                   round((cur_time - start_time) / (iteration + 1), 2), \
                                                                   round(loss_scalar, 4)))
            if iteration == opt.threads - 1:
                start_time = cur_time
        else:
            print('{} [{}]({}/{}) AvgTime:{:>4} Loss:{:>4}'.format(opt.env, epoch, iteration, train_len, \
                                                                   round((cur_time - start_time) / (iteration + 1 - opt.threads), 2), \
                                                                   round(loss_scalar, 4)))

        # Visualization
        vis.iteration.append(epoch + iteration / train_len)
        vis.nlogloss.append(-np.log(np.maximum(1e-6, loss_scalar)))
        vis.plot_loss()
        if opt.vis_iter <= 0 or iteration % opt.vis_iter > 0:
            continue
        prob, pred = torch.max(out, dim=1)
        vis_rgb = to_image(im[0, 0:3, :, :] * 0.5)
        vis_nir = to_image(im[0, 3:4, :, :] * 0.5)
        vis_swir1 = to_image(im[0, 4:5, :, :] * 0.5)
        vis_swir2 = to_image(im[0, -2:-1, :, :] * 0.5)
        vis_label = colormap(label[0].cpu().numpy())
        vis_pred = colormap(pred[0].cpu().numpy())
        vis_im = np.concatenate((np.concatenate((vis_label, vis_pred), axis=1), \
                                 np.concatenate((vis_rgb, vis_nir), axis=1), \
                                 np.concatenate((vis_swir1, vis_swir2), axis=1)), axis=2)
        vis.plot_image(vis_im, 0)
예제 #3
0
def get_seg_label_by_name(ImgName,
                          mc,
                          max_size=(640, 640),
                          default_ImgLoc='./VOCdevkit/VOC2012/JPEGImages/'):
    #should read original images here, and do not resize it here
    MEAN_PIXEL = np.array([103.939, 116.779, 123.68])
    ImgName_ = ImgName.split('.')[0] + '.jpg'
    FileName = default_ImgLoc + ImgName_
    orign_img = cv2.imread(FileName)
    im = orign_img - MEAN_PIXEL

    seg = cv2.imread('./VOCdevkit/VOC2012/SegmentationClass/' +
                     ImgName)[:, :, ::-1]  #utils.get_seg_by_name(ImgName)
    #print(seg)
    #print(seg.shape)
    #seg = seg[:,:,::-1]
    #print('segment shape',seg.shape)
    gray_to_rgb, rgb_to_gray = utils.colormap()
    #rgb_to_gray = utils.label_colormap()
    #print(rgb_to_gray.shape)
    #print(rgb_to_gray)
    #rgb_to_gray = [rgb_to_gray,]# * mc.BATCH_SIZE#rgb_to_gray * mc.BATCH_SIZE
    #print(len(rgb_to_gray))
    row, col, _ = im.shape
    im_blob = np.zeros((max_size[0], max_size[1], 3))
    im_blob[0:row, 0:col, :] = im
    seg_blob = np.zeros((max_size[0], max_size[1], 1))
    mask = np.zeros_like(seg_blob)
    for i in xrange(row):
        for j in xrange(col):
            seg_blob[i, j] = rgb_to_gray[tuple(seg[i, j, :])]
            # Discard 255 edge class
            if seg_blob[i, j] != 255:
                mask[i, j] = 1
            else:
                seg_blob[i, j] = 0
    #print(seg_blob)
    cv2.imwrite('res_img_seg.png', seg_blob)
    """
    seg_blob = np.zeros((max_size[0], max_size[1],1))
    mask = np.zeros_like(seg_blob)
    row, col, _ = orign_img.shape
    seg_gray = np.zeros((row, col))
    #print(rgb_to_gray[tuple(seg[0,0,:])])
    for i in xrange(row):
        for j in xrange(col):
            #print(np.asarray(seg[i,j]))
            seg_gray[i,j] = rgb_to_gray[tuple(seg[i,j])]
    seg_blob = cv2.resize(seg_gray, max_size, interpolation=cv2.INTER_NEAREST)
    for i in xrange(max_size[0]):
        for j in xrange(max_size[1]):
            if seg_blob[i,j] != 255:
                mask[i,j] = 1
            else:
                seg_blob[i,j] = 0
    #print(seg_blob.shape, mask.shape)
    seg_blob = np.array([seg_blob]).transpose((1,2,0))
    mask = np.array([mask]).transpose((1,2,0))
    """
    return im_blob, seg_blob, mask
예제 #4
0
파일: image.py 프로젝트: zhukequan/IRTK
def rview(img, seg=None, overlay=None, colors=None, binary="rview"):
    """
    Launches rview.
    """
    if isinstance(img, Image):
        if overlay is not None and seg is not None:
            print "You cannot specify both seg and overlay"
            return
        if overlay is not None and colors is None:
            colors = 'jet'  # default colormap
        if colors is not None and overlay is None:
            overlay = img.rescale()  # we want to see img in colors
            img = zeros(img.get_header(), dtype='int16')
        if isinstance(colors, str):
            colors = utils.get_colormap(colors)
        if seg is not None and colors is None:
            colors = utils.random_colormap(seg.max())

        # now, seg == overlay
        if overlay is not None:
            seg = overlay

        if seg is not None and not isinstance(seg, Image):
            seg = Image(seg, img.get_header())

        handle, filename = tempfile.mkstemp(suffix=".nii")
        garbage.append(filename)
        imwrite(filename, img.astype('int16'))  # rview reads in "short" anyway
        args = [binary, filename]
        if seg is not None:
            handle, seg_filename = tempfile.mkstemp(suffix=".nii")
            garbage.append(seg_filename)
            handle, colors_filename = tempfile.mkstemp(suffix=".txt")
            garbage.append(colors_filename)
            imwrite(seg_filename, seg.astype('int16'))
            args.extend(["-seg", seg_filename])
            utils.colormap(colors, colors_filename)
            args.extend(["-lut", colors_filename])
        if overlay is not None:
            args.append("-labels")

    # launch rview as an independant process
    subprocess.Popen(args)
예제 #5
0
파일: image.py 프로젝트: 151706061/IRTK
def rview( img, seg=None, overlay=None, colors=None, binary="rview" ):
    """
    Launches rview.
    """
    if isinstance( img, Image ):
        if overlay is not None and seg is not None:
            print "You cannot specify both seg and overlay"
            return
        if overlay is not None and colors is None:
            colors = 'jet' # default colormap
        if colors is not None and overlay is None:
            overlay = img.rescale() # we want to see img in colors
            img = zeros(img.get_header(), dtype='int16')
        if isinstance( colors, str ):
            colors = utils.get_colormap( colors )
        if seg is not None and colors is None:
            colors = utils.random_colormap(seg.max())

        # now, seg == overlay
        if overlay is not None:
            seg = overlay

        if seg is not None and not isinstance(seg, Image):
            seg = Image( seg, img.get_header() )
    
        handle,filename = tempfile.mkstemp(suffix=".nii")
        garbage.append(filename)
        imwrite( filename, img.astype('int16') ) # rview reads in "short" anyway
        args = [ binary, filename ]
        if seg is not None:
            handle,seg_filename = tempfile.mkstemp(suffix=".nii")
            garbage.append(seg_filename)
            handle,colors_filename = tempfile.mkstemp(suffix=".txt")
            garbage.append(colors_filename)
            imwrite( seg_filename, seg.astype('int16') )
            args.extend( ["-seg", seg_filename])
            utils.colormap( colors, colors_filename )
            args.extend( ["-lut", colors_filename])
        if overlay is not None:
            args.append("-labels")

    # launch rview as an independant process
    subprocess.Popen( args )
예제 #6
0
#!/usr/bin/env python3
import collections, functools, itertools, logging, multiprocessing, random
import matplotlib.cm
import matplotlib.patches
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy.optimize
import scipy.special
import plot_fit, utils
import plot_fit

CMAP_VIRIDIS = matplotlib.cm.get_cmap("viridis")

CMAP_FOLDED_VIRIDIS = utils.colormap(
    "folded_viridis",
    np.concatenate([CMAP_VIRIDIS(np.linspace(0.0, 1.0, 100)),
                    CMAP_VIRIDIS(np.linspace(1.0, 0.0, 100))]))

CMAP_FOLDED_PINK = utils.colormap(
    "folded_pink",
    ["#ffffff", "#ec7ca3", "#ffffff"])

# scaling transformations
TRANSFORM_LOG_ABS = lambda x: np.log(abs(x)), lambda x: np.exp(x)
TRANSFORM_ID = lambda x: x, lambda x: x

def gather_fit_data_inner(group, fit_count, maxfev):
    badness_threshold = plot_fit.LOGDERIV_BADNESS_THRESHOLD
    (label, interaction, num_filled, freq, method), gg = group
    logging.info(f"{(label, interaction, num_filled, freq, method)}")
    gg = gg.rename(columns={"num_shells": "x", "energy": "y"})
예제 #7
0
#!/usr/bin/env python3
import functools, random
import matplotlib.cm
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy.optimize
import scipy.special
import fits, utils

CMAP_VIRIDIS = matplotlib.cm.get_cmap("viridis")

CMAP_FOLDED_VIRIDIS = utils.colormap(
    "folded_viridis",
    np.concatenate([CMAP_VIRIDIS(np.linspace(0.0, 1.0, 100)),
                    CMAP_VIRIDIS(np.linspace(1.0, 0.0, 100))]))

CMAP_FOLDED_PINK = utils.colormap(
    "folded_pink",
    ["#ffffff", "#ec7ca3", "#ffffff"])

# scaling transformations
TRANSFORM_LOG_ABS = lambda x: np.log(abs(x)), lambda x: np.exp(x)
TRANSFORM_ID = lambda x: x, lambda x: x

def parse_bin(s):
    # why Pandas converts bins to strings, no-one knows
    return (tuple(map(float, s[1:-1].split(","))))

def parse_binned_groups(stream):
    return ((parse_bin(k), v) for k, v in stream)