Exemple #1
0
def convertcsv(bboxfname, bboxpath, detp):
    resolution = np.array([1, 1, 1])
    
    origin = np.load(sideinfopath+bboxfname[:-8]+'_origin.npy', mmap_mode='r')
    spacing = np.load(sideinfopath+bboxfname[:-8]+'_spacing.npy', mmap_mode='r')
    extendbox = np.load(sideinfopath+bboxfname[:-8]+'_extendbox.npy', mmap_mode='r')
    
    pbb = np.load(bboxpath+bboxfname, mmap_mode='r')
    diam = pbb[:,-1]
    
    check = sigmoid(pbb[:,0]) > detp
    pbbold = np.array(pbb[check])
#    pbbold = np.array(pbb[pbb[:,0] > detp])
    pbbold = np.array(pbbold[pbbold[:,-1] > 3])  # add new 9 15
    
    pbb = nms(pbbold, nmsthresh)
    pbb = np.array(pbb[:, :-1])
    pbb[:, 1:] = np.array(pbb[:, 1:] + np.expand_dims(extendbox[:,0], 1).T)
    pbb[:, 1:] = np.array(pbb[:, 1:] * np.expand_dims(resolution, 1).T / np.expand_dims(spacing, 1).T)
    
    pos = VoxelToWorldCoord(pbb[:, 1:], origin, spacing)
    rowlist = []
    
    for nk in range(pos.shape[0]): # pos[nk, 2], pos[nk, 1], pos[nk, 0]
        rowlist.append([bboxfname[:-8], pos[nk, 2], pos[nk, 1], pos[nk, 0], diam[nk], 1/(1+np.exp(-pbb[nk,0]))])
        
    return rowlist
Exemple #2
0
    def __init__(self, split, config, phase='train'):
        assert (phase == 'train' or phase == 'val' or phase == 'test')

        self.random_sample = config['random_sample']  #True
        self.T = config['T']  #1
        self.topk = config['topk']  #5
        self.crop_size = config['crop_size']  #[96,96,96]
        self.stride = config['stride']  #4
        self.augtype = config[
            'augtype']  #{'flip':True,'swap':False,'rotate':False,'scale':False}
        #self.labels = np.array(pandas.read_csv(config['labelfile']))

        datadir = config['datadir']  #preprocess_result_path
        bboxpath = config['bboxpath']  #'../detector/results/res18/bbox/',
        self.phase = phase
        self.candidate_box = []
        self.pbb_label = []

        split = [str(f, 'utf-8') for f in split]
        idcs = split
        self.filenames = [
            os.path.join(datadir, '%s_clean.npy' % idx) for idx in idcs
        ]
        labels = np.array(pandas.read_csv(
            config['labelfile']))  #'./full_label.csv'
        if phase != 'test':
            # print('0',labels[:,0])
            # print('0=', [str(f,'utf-8') for f in split])
            # self.yset = np.array([labels[labels[:,0]==str(f,'utf-8'),1] for f in split]).astype('int')
            self.yset = np.array([
                labels[labels[:, 0] == f.split('-')[0].split('_')[0], 1]
                for f in split
            ]).astype('int')
        # print([str(f,'utf-8') for f in idcs])
        # idcs = [str(f,'utf-8') for f in idcs]

        for idx in idcs:
            pbb = np.load(os.path.join(bboxpath, idx + '_pbb.npy'))
            # print('pbb.shape',pbb.shape)#(~,5)
            pbb = pbb[pbb[:, 0] > config['conf_th']]  #-1
            pbb = nms(pbb, config['nms_th'])  #0.05

            lbb = np.load(os.path.join(bboxpath, idx + '_lbb.npy'))
            pbb_label = []

            for p in pbb:
                isnod = False
                for l in lbb:
                    score = iou(p[1:5], l)
                    if score > config['detect_th']:  #0.05
                        isnod = True
                        break
                pbb_label.append(isnod)


#             if idx.startswith()
            self.candidate_box.append(pbb)
            self.pbb_label.append(np.array(pbb_label))
        self.crop = simpleCrop(config, phase)
    def __init__(self, split, config, phase='train'):
        assert (phase == 'train' or phase == 'val' or phase == 'test')

        self.random_sample = config['random_sample']
        self.T = config['T']
        self.topk = config['topk']
        self.crop_size = config['crop_size']
        self.stride = config['stride']
        self.augtype = config['augtype']
        # self.labels = np.array(pandas.read_csv(config['labelfile']))

        datadir = config['datadir']
        bboxpath = config['bboxpath']
        self.phase = phase
        self.candidate_box = []
        self.pbb_label = []

        idcs = split
        self.filenames = [
            os.path.join(datadir, '%s_clean.npy' % idx) for idx in idcs
        ]
        labels = np.array(pandas.read_csv(config['labelfile']))
        if phase != 'test':
            self.yset = np.array([
                labels[labels[:, 0] == f.split('-')[0].split('_')[0], 1]
                for f in split
            ]).astype('int')
        idcs = [f.split('-')[0] for f in idcs]
        '''
        1. 对目标检测过的预测结果做nms
        2. 提取使用到的label    
        3. self.pbb_label 预测结果中是否含有目标的label
        4. self.candidate_box 候选框pbb 为预测结果的加载,疑似存在目标
        5. yset 为分类的annotations
        '''

        for idx in idcs:
            # output shape = n*[conf,z,h,w,d]
            pbb = np.load(os.path.join(bboxpath, idx + '_pbb.npy'))
            pbb = pbb[pbb[:, 0] > config['conf_th']]
            pbb = nms(pbb, config['nms_th'])

            lbb = np.load(os.path.join(bboxpath, idx + '_lbb.npy'))
            pbb_label = []

            for p in pbb:
                isnod = False
                for l in lbb:
                    score = iou(p[1:5], l)
                    if score > config['detect_th']:
                        isnod = True
                        break
                pbb_label.append(isnod)
            #             if idx.startswith()
            self.candidate_box.append(pbb)
            self.pbb_label.append(np.array(pbb_label))
        self.crop = simpleCrop(config, phase)
Exemple #4
0
    def __init__(self, dirlist, config, phase='train'):
        assert (phase in {'train', 'val', 'test'})

        self.random_sample = config['random_sample']
        self.T = config['T']
        self.topk = config['topk']
        self.crop_size = config['crop_size']
        self.stride = config['stride']
        self.augtype = config['augtype']
        self.filling_value = config['filling_value']

        # self.labels = np.array(pandas.read_csv(config['labelfile']))

        datadir = config['datadir']
        bboxpath = config['bboxpath']
        self.phase = phase
        self.candidate_box = []
        self.pbb_label = []

        idcs = dirlist

        self.filenames = [
            p.join(datadir, '%s_clean.npy' % idx.split('-')[0]) for idx in idcs
        ]

        if self.phase != 'test':
            self.yset = 1 - np.array([f.split('-')[1][2]
                                      for f in idcs]).astype('int')

        for idx in idcs:
            pbb = np.load(p.join(bboxpath, idx + '_pbb.npy'))
            pbb = pbb[pbb[:, 0] > config['conf_th']]
            pbb = nms(pbb, config['nms_th'])

            lbb = np.load(p.join(bboxpath, idx + '_lbb.npy'))
            pbb_label = []

            for pb in pbb:
                isnod = False

                for lb in lbb:
                    score = iou(pb[1:5], lb)

                    if score > config['detect_th']:
                        isnod = True
                        break

                pbb_label.append(isnod)

            # if idx.startswith()
            self.candidate_box.append(pbb)
            self.pbb_label.append(np.array(pbb_label))

        self.crop = simpleCrop(config, phase)
Exemple #5
0
    def __init__(self, split, config, phase = 'train'):
        assert(phase == 'train' or phase == 'val' or phase == 'test')
        
        self.random_sample = config['random_sample']
        self.T = config['T']
        self.topk = config['topk']
        self.crop_size = config['crop_size']
        self.stride = config['stride']
        #print ('------------stride: ', self.stride)
        self.augtype  = config['augtype']
        self.filling_value = config['filling_value']
        
        #self.labels = np.array(pandas.read_csv(config['labelfile']))
        
        datadir = config['datadir']
        bboxpath  = config['bboxpath']
        self.phase = phase
        self.candidate_box = []
        self.pbb_label = []
        self.subj_name = []
        idcs = split
        self.filenames = [os.path.join(datadir, '%s_clean.npy' % idx.split('-')[0]) for idx in idcs]
        if self.phase!='test':
            self.yset = 1-np.array([f.split('-')[1][2] for f in idcs]).astype('int')
 
        
        for idx in idcs:
            #print ('This is ', idx)
            try:
                pbb = np.load(os.path.join(bboxpath,idx+'_pbb.npy'))
            except:
                print (idx+'_pbb.npy not existed' )
                continue
            pbb = pbb[pbb[:,0]>config['conf_th']]
            pbb = nms(pbb, config['nms_th'])
            
            lbb = np.load(os.path.join(bboxpath,idx+'_lbb.npy'))
            pbb_label = []
            
            for p in pbb:
                isnod = False
                for l in lbb:
                    score = iou(p[1:5], l)
                    if score > config['detect_th']:
                        isnod = True
                        break
                pbb_label.append(isnod)
#             if idx.startswith()
            self.subj_name.append(idx)
            self.candidate_box.append(pbb)
            self.pbb_label.append(np.array(pbb_label))
        self.crop = simpleCrop(config,phase)
Exemple #6
0
    def __init__(self, split, config, phase = 'train'):
        assert(phase == 'train' or phase == 'val' or phase == 'test')
        
        self.random_sample = config['random_sample']
        self.T = config['T']
        self.topk = config['topk']
        self.crop_size = config['crop_size']
        self.stride = config['stride']
        self.augtype  = config['augtype']
        #self.labels = np.array(pandas.read_csv(config['labelfile']))
        
        datadir = config['datadir']
        bboxpath  = config['bboxpath']
        self.phase = phase
        self.candidate_box = []
        self.pbb_label = []
        
        idcs = split
        self.filenames = [os.path.join(datadir, '%s_clean.npy' % idx) for idx in idcs]
        labels = np.array(pandas.read_csv(config['labelfile']))
        if phase !='test':
            for tmp in [labels[labels[:, 0] == f.split('-')[0].split('_')[0], 1] for f in split]:
                print(tmp.shape)
	    print([labels[labels[:,0]==f.split('-')[0].split('_')[0],1] for f in split])
            self.yset = np.array([labels[labels[:,0]==f.split('-')[0].split('_')[0],1] for f in split]).astype('int')
        idcs = [f.split('-')[0] for f in idcs]
        
        
        for idx in idcs:
            pbb = np.load(os.path.join(bboxpath,idx+'_pbb.npy'))
            pbb = pbb[pbb[:,0]>config['conf_th']]
            pbb = nms(pbb, config['nms_th'])
            
            lbb = np.load(os.path.join(bboxpath,idx+'_lbb.npy'))
            pbb_label = []
            
            for p in pbb:
                isnod = False
                for l in lbb:
                    score = iou(p[1:5], l)
                    if score > config['detect_th']:
                        isnod = True
                        break
                pbb_label.append(isnod)
#             if idx.startswith()
            self.candidate_box.append(pbb)
            self.pbb_label.append(np.array(pbb_label))
        self.crop = simpleCrop(config,phase)
Exemple #7
0
    def __init__(self, split, config, phase = 'train'):
        assert(phase == 'train' or phase == 'val' or phase == 'test')
        
        self.random_sample = config['random_sample']
        self.T = config['T']
        self.topk = config['topk']
        self.crop_size = config['crop_size']
        self.stride = config['stride']
        self.augtype  = config['augtype']
        self.filling_value = config['filling_value']
        
        #self.labels = np.array(pandas.read_csv(config['labelfile']))
        
        datadir = config['datadir']
        bboxpath  = config['bboxpath']
        self.phase = phase
        self.candidate_box = []
        self.pbb_label = []
        
        idcs = split
        self.filenames = [os.path.join(datadir, '%s_clean.npy' % idx.split('-')[0]) for idx in idcs]
        if self.phase!='test':
            self.yset = 1-np.array([f.split('-')[1][2] for f in idcs]).astype('int')
 
        
        for idx in idcs:
            pbb = np.load(os.path.join(bboxpath,idx+'_pbb.npy'))
            pbb = pbb[pbb[:,0]>config['conf_th']]
            pbb = nms(pbb, config['nms_th'])
            
            lbb = np.load(os.path.join(bboxpath,idx+'_lbb.npy'))
            pbb_label = []
            
            for p in pbb:
                isnod = False
                for l in lbb:
                    score = iou(p[1:5], l)
                    if score > config['detect_th']:
                        isnod = True
                        break
                pbb_label.append(isnod)
#             if idx.startswith()
            self.candidate_box.append(pbb)
            self.pbb_label.append(np.array(pbb_label))
        self.crop = simpleCrop(config,phase)
Exemple #8
0
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import sys

sys.path.append('../training/')
sys.path.append('../')
sys.path.append('../preprocessing/')
from layers import nms, iou

img = np.load('./1_clean.npy')
pbb = np.load('./1_pbb.npy')

pbb = pbb[pbb[:, 0] > -1]

pbb = nms(pbb, 0.05)
box = pbb[0].astype('int')[1:]

ax = plt.subplot(1, 1, 1)
plt.imshow(img[0, box[0]], 'gray')
plt.axis('off')
rect = patches.Rectangle((box[2] - box[3], box[1] - box[3]),
                         box[3] * 2,
                         box[3] * 2,
                         linewidth=2,
                         edgecolor='red',
                         facecolor='none')
ax.add_patch(rect)
Exemple #9
0
    # print('NoduleBox',boxS)#
    # ax = plt.subplot(1,1,1)
    # plt.imshow(img[0,boxS[0]],'gray')
    # plt.axis('off')
    # rect = patches.Rectangle((boxS[2]-boxS[3],boxS[1]-boxS[3]),boxS[3]*2,boxS[3]*2,linewidth=2,edgecolor='blue',facecolor='none')
    # ax.add_patch(rect)
    # plt.savefig(os.path.join(save_dir,imglist[i].split('_')[0] + '---' + str(boxS[0]) + '.png'))
    # plt.close()
    if pbb.shape[0] != 0:
        max = np.max(pbb[:, 0])
        thes = -10
        # thes = -1
        if max < -1:
            thes = max
        pbb = pbb[pbb[:, 0] >= thes]
        pbb = nms(pbb, 0.1)
        print('pbb', imglist[i].split('_')[0], pbb.shape, img.shape)

        for box_num in range(pbb.shape[0]):
            box = pbb[box_num].astype('int')[1:]  #取第一个box的xyzd
            # if imgLungmask[0,box[0],box[1],box[2]] != 0:
            if box[0] < img.shape[1]:
                print('box', box)  #array([169, 153,  69,  13])

                ax = plt.subplot(1, 1, 1)
                plt.imshow(img[0, box[0]], 'gray')
                plt.axis('off')
                rect = patches.Rectangle((box[2] - box[3], box[1] - box[3]),
                                         box[3] * 2,
                                         box[3] * 2,
                                         linewidth=2,
    # print('boxXYZ,pos',boxXYZ,pos)
    # ax = plt.subplot(1,1,1)
    # plt.imshow(img[0,boxS[0]],'gray')
    # plt.axis('off')
    # rect = patches.Rectangle((boxS[2]-boxS[3],boxS[1]-boxS[3]),boxS[3]*2,boxS[3]*2,linewidth=2,edgecolor='blue',facecolor='none')
    # ax.add_patch(rect)
    # plt.savefig(os.path.join(save_dir,imglist[i].split('_')[0] + '---' + str(boxS[0]) + '.png'))
    # plt.close()
    if pbb5.shape[0] != 0:
        max = np.max(pbb5[:, 0])
        thes = -10
        # thes = -1
        if max < -1:
            thes = max
        pbb5 = pbb5[pbb5[:, 0] >= thes]
        pbb5 = nms(pbb5, 0.1)
        # print('pbb5',imglist[i].split('_')[0],pbb5.shape, Image.shape, extendbox.shape)
        pbb = np.array(pbb5[:, :-1])  #去掉直径
        print('pbb5', pbb5, Spacing)
        pbb[:, 1:] = np.array(pbb[:, 1:] + np.expand_dims(
            extendbox[:, 0], 1).T)  #对输出加上拓展box的坐标,其实就是恢复为原来的坐标,我对这个拓展box深恶痛绝

        pbb[:, 1:] = np.array(
            pbb[:, 1:] * np.expand_dims(resolution, 1).T /
            np.expand_dims(Spacing, 1).T)  #将输出恢复为原来的分辨率,这样就对应了原始数据中的体素坐标
        pbb5[:, 2:] = np.array(
            pbb5[:, 2:] * np.expand_dims(resolution, 1).T /
            np.expand_dims(Spacing, 1).T)  #将输出恢复为原来的分辨率,这样就对应了原始数据中的体素坐标
        pos = VoxelToWorldCoord(pbb[:, 1:], Origin, Spacing)

        # print('pos',pos)
Exemple #11
0
def test(data_loader, net, get_pbb, save_dir, config, test_set):
    start_time = time.time()
    # eg test2_100
    save_dir = os.path.join(
        save_dir, 'bbox/',
        test_set + '_' + os.path.splitext(os.path.basename(args.resume))[0])

    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    print(save_dir)
    net.eval()
    namelist = []
    split_comber = data_loader.dataset.split_comber
    for i_name, (data, target, coord, nzhw) in enumerate(data_loader):
        s = time.time()
        target = [np.asarray(t, np.float32) for t in target]
        lbb = target[0]
        nzhw = nzhw[0]
        name = os.path.basename(
            data_loader.dataset.filenames[i_name]).split('_clean')[0]
        data = data[0][0]
        coord = coord[0][0]
        isfeat = False
        if 'output_feature' in config:
            if config['output_feature']:
                isfeat = True
        n_per_run = args.n_test
        print(data.size())
        splitlist = range(0, len(data) + 1, n_per_run)
        if splitlist[-1] != len(data):
            splitlist.append(len(data))
        outputlist = []
        featurelist = []

        for i in range(len(splitlist) - 1):
            input = Variable(data[splitlist[i]:splitlist[i + 1]],
                             volatile=True).cuda()
            inputcoord = Variable(coord[splitlist[i]:splitlist[i + 1]],
                                  volatile=True).cuda()
            if isfeat:
                output, feature = net(input, inputcoord)
                featurelist.append(feature.data.cpu().numpy())
            else:
                output = net(input, inputcoord)
            outputlist.append(output.data.cpu().numpy())
        output = np.concatenate(outputlist, 0)
        output = split_comber.combine(output, nzhw=nzhw)
        if isfeat:
            feature = np.concatenate(featurelist,
                                     0).transpose([0, 2, 3, 4,
                                                   1])[:, :, :, :, :,
                                                       np.newaxis]
            feature = split_comber.combine(feature, sidelen)[..., 0]

        # Initially filter by sig(-3) = 0.047
        thresh = -3
        pbb, mask = get_pbb(output, thresh, ismask=True)
        if isfeat:
            feature_selected = feature[mask[0], mask[1], mask[2]]
            np.save(os.path.join(save_dir, name + '_feature.npy'),
                    feature_selected)
        #tp,fp,fn,_ = acc(pbb,lbb,0,0.1,0.1)
        #print([len(tp),len(fp),len(fn)])
        print([i_name, name])
        e = time.time()

        #pbb = nms(pbb[pbb[:,0] > 0], 0.1) # Save only unique predicted bboxes with threshold sig(0) = 0.5
        pbb = nms(pbb, 0.1)
        np.save(os.path.join(save_dir, name + '_pbb.npy'), pbb)
        np.save(os.path.join(save_dir, name + '_lbb.npy'), lbb)
    np.save(os.path.join(save_dir, 'namelist.npy'), namelist)
    end_time = time.time()

    print('elapsed time is %3.2f seconds' % (end_time - start_time))
    print
    print