Пример #1
0
def crop_candidate():
    df_node = pd.read_csv(opt.candidate_center)
    file_list = glob(opt.data_train + "*.mhd")
    done = []
    for fcount, img_file in enumerate(tqdm(file_list)):
        file_name = img_file.split('/')[-1][:-4]
        if is_exist(done, file_name):
            continue
        print "doing on ", file_name
        mini_df = df_node[df_node["seriesuid"] ==
                          file_name]  #get all nodules associate with file
        if mini_df.shape[0] > 0:
            img_arr, origin, spacing = load_ct(img_file)
            patient_nodules = []
            for node_idx, cur_row in mini_df.iterrows():
                crop_img = np.zeros([64, 64, 64])
                is_nodule = cur_row["isnodule"]
                center = np.array(
                    [cur_row["coordZ"], cur_row["coordY"],
                     cur_row["coordX"]])  # nodule center
                v_center = check_center(64, center, img_arr.shape)
                v_center = v_center.astype(np.int32)
                crop_img = img_arr[v_center[0] - 32:v_center[0] + 32,
                                   v_center[1] - 32:v_center[1] + 32,
                                   v_center[2] - 32:v_center[2] + 32]
                #np.savez('/mnt/7/0701_train_nodule_candidate/'+file_name+\
                #         "_"+str(node_idx)+"_"+str(int(is_nodule))+".npz",data=crop_img,center=v_center)
                np.save(
                    '/mnt/7/0705_train_48_64_candidate/' + file_name + "_" +
                    str(node_idx) + "_" + str(int(is_nodule)) + ".npy",
                    crop_img)
Пример #2
0
def crop_nodule():
    '''
    crop roi from the train dataset. for each train img,crop the nodule area in a rectangle
    then reverse it in 3 ways to augment it as the positive samples.
    random choose 10 point as the area center,crop the area as the negative samples
    '''
    df_node = pd.read_csv(opt.annotatiion_csv)
    file_list = glob(opt.data_train + "*.mhd")
    for fcount, img_file in enumerate(tqdm(file_list)):
        file_name = img_file.split('/')[-1][:-4]
        print "doing on ", file_name
        mini_df = df_node[df_node["seriesuid"] ==
                          file_name]  #get all nodules associate with file
        if mini_df.shape[0] > 0:
            img_arr, origin, spacing = load_ct(img_file)
            patient_nodules = []
            for node_idx, cur_row in mini_df.iterrows():
                crop_img = np.zeros([64, 64, 64])
                diam = cur_row["diameter_mm"]
                center = np.array(
                    [cur_row["coordZ"], cur_row["coordY"],
                     cur_row["coordX"]])  # nodule center
                v_center = np.rint(
                    (center - origin) / spacing
                )  # nodule center in voxel space (still x,y,z ordering)
                v_center = check_center(64, v_center, img_arr.shape)
                v_center = v_center.astype(np.int32)
                crop_img = img_arr[v_center[0] - 32:v_center[0] + 32,
                                   v_center[1] - 32:v_center[1] + 32,
                                   v_center[2] - 32:v_center[2] + 32]

                np.save(
                    opt.nodule_cubic + file_name + "_" + str(node_idx) +
                    ".npy", crop_img)
Пример #3
0
def test_seg(save_dir, data_path, model_def, model_weight):
    is_save = True
    crop_size = [64, 64, 64]
    prob_threshould = 0.4  #二值化阈值
    data_list = indices = open(data_path, 'r').read().splitlines()
    #data_list=["/home/x/data/datasets/tianchi/train/LKDS-00004.mhd"]
    data_list.sort()
    start = time.time()
    net = caffe.Net(model_def, model_weight, caffe.TEST)
    print "time:", time.time() - start
    for file_name in tqdm(data_list):
        mhd_name = file_name.split('/')[-1][:-4]
        if os.path.exists(save_dir + mhd_name + "_nodule.npy"):
            print mhd_name, " have finished"
            continue
        probs, img_arr = seg(net, file_name)
        np.save(mhd_name + "_probs.npy", probs)
        probs = probs > prob_threshould
        probs = morphology.dilation(probs, np.ones([3, 3, 3]))
        probs = morphology.dilation(probs, np.ones([3, 3, 3]))
        probs = morphology.erosion(probs, np.ones([3, 3, 3]))
        labels = measure.label(probs, connectivity=2)
        #label_vals = np.unique(labels)
        regions = measure.regionprops(labels)
        centers = []
        crops = []
        bboxes = []
        spans = []
        for prop in regions:
            B = prop.bbox
            if B[3] - B[0] > 2 and B[4] - B[1] > 4 and B[5] - B[2] > 4:
                z = int((B[3] + B[0]) / 2.0)
                y = int((B[4] + B[1]) / 2.0)
                x = int((B[5] + B[2]) / 2.0)
                span = np.array(
                    [int(B[3] - B[0]),
                     int(B[4] - B[1]),
                     int(B[5] - B[2])])
                spans.append(span)
                centers.append(np.array([z, y, x]))
                bboxes.append(B)
        for idx, bbox in enumerate(bboxes):
            crop = np.zeros(crop_size, dtype=np.float32)
            crop_center = centers[idx]
            half = np.array(crop_size) / 2
            crop_center = check_center(crop_size, crop_center, img_arr.shape)
            crop=img_arr[int(crop_center[0]-half[0]):int(crop_center[0]+half[0]),\
                         int(crop_center[1]-half[1]):int(crop_center[1]+half[1]),\
                         int(crop_center[2]-half[1]):int(crop_center[2]+half[1])]
            crops.append(crop)
        if is_save:
            np.save(save_dir + mhd_name + "_nodule.npy", np.array(crops))
            np.save(save_dir + mhd_name + "_center.npy", np.array(centers))
            np.save(save_dir + mhd_name + "_size.npy", np.array(spans))
Пример #4
0
def crop_nodule(img_file):
    '''
    crop roi from the train dataset. for each train img,crop the nodule area in a rectangle
    then reverse it in 3 ways to augment it as the positive samples.
    random choose 10 point as the area center,crop the area as the negative samples
    '''

    file_name = img_file.split('/')[-1][:-4]
    print file_name
    mini_df = df_node[df_node["seriesuid"] == file_name]
    print mini_df
    if mini_df.shape[0] > 0:
        size, origin, spacing = load_ct_info(img_file)
        if size[0] < 108:
            crop_size = [size[0], 108, 108]
        else:
            crop_size = [108] * 3
        patient_nodules = []
        #import ipdb;ipdb.set_trace()
        for node_idx, cur_row in mini_df.iterrows():
            #crop_img=np.zeros(crop_size)
            crop_mask = np.zeros(crop_size)
            diam = cur_row["diameter_mm"]
            center = np.array(
                [cur_row["coordZ"], cur_row["coordY"],
                 cur_row["coordX"]])  # nodule center
            v_center = np.rint(
                (center - origin) /
                spacing)  # nodule center in voxel space (still x,y,z ordering)
            v_center = check_center(crop_size, v_center, size)
            v_center = np.array(crop_size) / 2
            #half=np.array(crop_size)/2
            #crop_img=img_arr[v_center[0]-half[0]:v_center[0]+crop_size[0]-half[0],\
            #                 v_center[1]-half[1]:v_center[1]+half[1],v_center[2]-half[2]:v_center[2]+half[2]]
            radius = diam / 2
            span = np.round(radius / spacing).astype(np.int32)
            #print v_center
            #print span
            crop_mask[v_center[0] - span[0]:v_center[0] + span[0],
                      v_center[1] - span[1]:v_center[1] + span[1],
                      v_center[2] - span[2]:v_center[2] + span[2]] = 1.0

            print crop_mask.sum()
            np.save(
                '/mnt/7/train_nodule_mask/' + file_name + "_" + str(node_idx) +
                ".npy", crop_mask)
Пример #5
0
def doTest(file_name, model):
    probs, img_arr = seg(opt.img_dir + file_name, model)
    probs = probs > opt.prob_threshould
    probs = morphology.dilation(probs, np.ones([3, 3, 3]))
    probs = morphology.dilation(probs, np.ones([3, 3, 3]))
    probs = morphology.erosion(probs, np.ones([3, 3, 3]))
    #np.save("probs.npy",probs)
    labels = measure.label(probs, connectivity=2)
    #label_vals = np.unique(labels)
    regions = measure.regionprops(labels)
    centers = []
    crops = []
    bboxes = []
    spans = []
    for prop in regions:
        B = prop.bbox
        if B[3] - B[0] > 2 and B[4] - B[1] > 4 and B[5] - B[2] > 4:
            z = int((B[3] + B[0]) / 2.0)
            y = int((B[4] + B[1]) / 2.0)
            x = int((B[5] + B[2]) / 2.0)
            span = np.array(
                [int(B[3] - B[0]),
                 int(B[4] - B[1]),
                 int(B[5] - B[2])])
            spans.append(span)
            centers.append(np.array([z, y, x]))
            bboxes.append(B)
    for idx, bbox in enumerate(bboxes):
        crop = np.zeros([opt.crop_size, opt.crop_size, opt.crop_size],
                        dtype=np.float32)
        crop_center = centers[idx]
        half = opt.crop_size / 2
        crop_center = check_center(opt.crop_size, crop_center, img_arr.shape)
        crop=img_arr[int(crop_center[0]-half):int(crop_center[0]+half),\
                     int(crop_center[1]-half):int(crop_center[1]+half),\
                     int(crop_center[2]-half):int(crop_center[2]+half)]
        crops.append(crop)
    if opt.is_save:
        np.save(opt.save_dir + file_name + "_nodule.npy", np.array(crops))
        np.save(opt.save_dir + file_name + "_center.npy", np.array(centers))
        np.save(opt.save_dir + file_name + "_size.npy", np.array(spans))
Пример #6
0
def test_seg(save_dir,data_path,model_def,model_weight):
    is_save=True
    prob_threshould=0.4#二值化阈值
    save_dir=opt.save_dir#切块保存路径
    data_list=indices = open(data_path, 'r').read().splitlines()
    start=time.time()
    net=caffe.Net(model_def,model_weight,caffe.TEST)
    for file_name in tqdm(data_list[:2]):
        img_arr,origin,spacing=load_ct(file_name)
        img_new=normalize(img_arr)
        seg_size = [64,64,64] 
        crop_z=64
        dim_z=img_arr.shape[0]
        if dim_z<seg_size[0]:
            crop_z=img_arr.shape[0]
            img_pad=np.zeros(img_arr.shape,dtype=np.float32)
            img_pad[32-dim_z/2:32+dim_z-dim_z/2,:,:]=img_new
            probs1=seg(net2,file_name,seg_size,img_pad)
            probs1=probs1[32-dim_z/2:32+dim_z-dim_z/2]
            del img_pad
        else:
            probs1=seg(net2,file_name,seg_size,img_new)
        
        
        seg_size = [80,80,80] 
        dim_z=img_arr.shape[0]
        if dim_z<seg_size[0]:
            img_pad=np.zeros(img_arr.shape,dtype=np.float32)
            img_pad[40-dim_z/2:40+dim_z-dim_z/2,:,:]=img_new
            probs2=seg(net1,file_name,seg_size,img_pad)
            probs2=probs2[40-dim_z/2:40+dim_z-dim_z/2]
            del img_pad
        else:
            probs2=seg(net1,file_name,seg_size,img_new)
            #seg_size=[img_arr.shape[0],80,80]
        #net.blobs['data'].reshape(opt.batch_size,1,seg_size[0],seg_size[1],seg_size[2])
        #import ipdb; ipdb.set_trace()
        probs=(probs1+probs2)/2.0
        np.save(mhd_name+"_probs.npy",probs)
        crop_size=[crop_z,64,64]
        mhd_name=file_name.split('/')[-1][:-4]
        probs=probs>prob_threshould 
        probs=morphology.dilation(probs,np.ones([3,3,3]))
        probs=morphology.dilation(probs,np.ones([3,3,3]))
        probs=morphology.erosion(probs,np.ones([3,3,3]))
        np.save(file_name+"_probs.npy",probs)
        labels = measure.label(probs,connectivity=2)
        #label_vals = np.unique(labels)
        regions = measure.regionprops(labels)
        centers = []
        crops = []
        bboxes = []
        spans=[]
        for prop in regions:
            B = prop.bbox
            if B[3]-B[0]>2 and B[4]-B[1]>4 and B[5]-B[2]>4:
                z=int((B[3]+B[0])/2.0)
                y=int((B[4]+B[1])/2.0)
                x=int((B[5]+B[2])/2.0)
                span=np.array([int(B[3]-B[0]),int(B[4]-B[1]),int(B[5]-B[2])])
                spans.append(span)
                centers.append(np.array([z,y,x]))
                bboxes.append(B)
        for idx,bbox in enumerate(bboxes):
            crop=np.zeros(crop_size,dtype=np.float32)
            crop_center=centers[idx]
            half=np.array(crop_size)/2
            crop_center=check_center(crop_size,crop_center,img_arr.shape)
            crop=img_arr[int(crop_center[0]-half[0]):int(crop_center[0]+half[0]),\
                         int(crop_center[1]-half[1]):int(crop_center[1]+half[1]),\
                         int(crop_center[2]-half[1]):int(crop_center[2]+half[1])]
            crops.append(crop)
        if is_save:
            np.save(save_dir+mhd_name+"_nodule.npy",np.array(crops))
            np.save(save_dir+mhd_name+"_center.npy",np.array(centers))
            np.save(save_dir+mhd_name+"_size.npy",np.array(spans))