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)
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)
def seg(file_name, model): ''' 用CPU跑特别慢 确实GPU很有必要 ''' seg_size = opt.seg_size print " data prepared................................." img_arr, origin, spacing = load_ct(file_name + '.mhd') img_new = normalize(img_arr) depth, height, width = img_new.shape blocks, indexs = cropBlocks(img_new, seg_size) probs = np.zeros(img_new.shape, dtype=np.float32) num = np.array(img_new.shape) / seg_size off = np.array(img_new.shape) - seg_size * num off_min = off / 2 batch_num = opt.batch_size print "doing on patient:", file_name for i in range(blocks.shape[0]): if (i % batch_num == batch_num - 1): batch_inputs_numpy = [ torch.from_numpy(blocks[j][np.newaxis, np.newaxis, :, :, :]) for j in range(i - batch_num + 1, i + 1) ] batch_inputs = torch.autograd.Variable(torch.cat( batch_inputs_numpy, 0), volatile=True).cuda() batch_outputs = model(batch_inputs) for j in range(i - batch_num + 1, i + 1): probs[off_min[0] + indexs[j, 0] * seg_size[0]:off_min[0] + indexs[j, 0] * seg_size[0] + seg_size[0], off_min[1] + indexs[j, 1] * seg_size[1]:off_min[1] + indexs[j, 1] * seg_size[1] + seg_size[1], off_min[2] + indexs[j, 2] * seg_size[2]:off_min[2] + indexs[j, 2] * seg_size[2] + seg_size[2], ] = batch_outputs.data.cpu()[ j - (i - batch_num + 1)].numpy() if i % 50 == 0: print i, " have finished" return probs, img_arr return output
def load_image(self, index): image, origin, spacing = load_ct(self.img_list[index]) label, nodule_centers, _, _ = make_mask(self.img_list[index]) types = np.random.random_sample() cubic_list = [] masks_list = [] for ii in range(len(nodule_centers)): rand = self.randn offset = np.random.randint(-1 * rand, rand, 3) #center_index=np.random.randint(len(nodule_centers)) cubic, mask = crop(image, label, v_center=offset + nodule_centers[ii], crop_size=self.crop_size) cubic_list.append(normalize(cubic)) masks_list.append(mask) cubic_, mask_ = crop(image, label, v_center=None, crop_size=self.crop_size) cubic_list.append(normalize(cubic_)) masks_list.append(mask_) return cubic_list, masks_list
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))
def seg(net, file_name): ''' 用CPU跑特别慢 确实GPU很有必要 ''' print " data prepared................................." img_arr, origin, spacing = load_ct(file_name) seg_size = [80, 80, 80] #if img_arr.shape[0]<seg_size[0]: # 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]) img_new = normalize(img_arr) depth, height, width = img_new.shape off_min = np.array([0, 0, 0]) #num = np.array(img_new.shape) / seg_size #off = np.array(img_new.shape) - seg_size * num #off_min = off / 2 blocks, indexs = cropBlocks(img_new, seg_size, off_min) probs1 = np.zeros(img_new.shape, dtype=np.float32) batch_num = 2 print "doing on patient:", file_name for i in range(blocks.shape[0]): if (i % batch_num == batch_num - 1): batch_inputs_numpy = [ blocks[j][np.newaxis, np.newaxis, :, :, :] for j in range(i - batch_num + 1, i + 1) ] #import ipdb;ipdb.set_trace() net.blobs['data'].data[...] = batch_inputs_numpy #print net.blobs['data'].data.shape batch_outputs = net.forward() for j in range(i - batch_num + 1, i + 1): probs1[off_min[0] + indexs[j, 0] * seg_size[0]:off_min[0] + indexs[j, 0] * seg_size[0] + seg_size[0], off_min[1] + indexs[j, 1] * seg_size[1]:off_min[1] + indexs[j, 1] * seg_size[1] + seg_size[1], off_min[2] + indexs[j, 2] * seg_size[2]:off_min[2] + indexs[j, 2] * seg_size[2] + seg_size[2], ] = batch_outputs['probs'][j - (i - batch_num + 1), 0] if i % 50 == 0: print i, " have finished" print "probs1.max()", probs1.max() #seg_size = [64,64,64] #if img_arr.shape[0]<seg_size[0]: # seg_size=[img_arr.shape[0],64,64] #net.blobs['data'].reshape(opt.batch_size,1,seg_size[0],seg_size[1],seg_size[2]) off_min = np.array([40, 40, 40]) #num = np.array(img_new.shape) / seg_size #off = np.array(img_new.shape) - seg_size * num #off_min = off / 2 blocks, indexs = cropBlocks(img_new, seg_size, off_min) probs2 = np.zeros(img_new.shape, dtype=np.float32) batch_num = 2 print "doing on patient:", file_name for i in range(blocks.shape[0]): if (i % batch_num == batch_num - 1): batch_inputs_numpy = [ blocks[j][np.newaxis, np.newaxis, :, :, :] for j in range(i - batch_num + 1, i + 1) ] net.blobs['data'].data[...] = batch_inputs_numpy batch_outputs = net.forward() for j in range(i - batch_num + 1, i + 1): probs2[off_min[0] + indexs[j, 0] * seg_size[0]:off_min[0] + indexs[j, 0] * seg_size[0] + seg_size[0], off_min[1] + indexs[j, 1] * seg_size[1]:off_min[1] + indexs[j, 1] * seg_size[1] + seg_size[1], off_min[2] + indexs[j, 2] * seg_size[2]:off_min[2] + indexs[j, 2] * seg_size[2] + seg_size[2], ] = batch_outputs['probs'][j - (i - batch_num + 1), 0] if i % 50 == 0: print i, " have finished" print "probs2.max()", probs2.max() return (probs1 + probs2) / 2.0, img_arr