def __init__(self,pat_idx=None): fnames = [22,24,25,26,27] if(pat_idx > 15): pat_idx = fnames[pat_idx-16] flair = mha.new(IMAGES_FOLDER_PATH + '/BRATS_HG00' + str(pat_idx).zfill(2) + '/BRATS_HG00' + str(pat_idx).zfill(2) + '_FLAIR.mha') t1 = mha.new(IMAGES_FOLDER_PATH + '/BRATS_HG00' + str(pat_idx).zfill(2) + '/BRATS_HG00' + str(pat_idx).zfill(2) + '_T1.mha') t2 = mha.new(IMAGES_FOLDER_PATH + '/BRATS_HG00' + str(pat_idx).zfill(2) + '/BRATS_HG00' + str(pat_idx).zfill(2) + '_T2.mha') t1c = mha.new(IMAGES_FOLDER_PATH + '/BRATS_HG00' + str(pat_idx).zfill(2) + '/BRATS_HG00' + str(pat_idx).zfill(2) + '_T1C.mha') self.truth = mha.new(TRUTH_FOLDER_PATH + '/BRATS_HG00' + str(pat_idx).zfill(2) + '_truth.mha').data self.data = numpy.array([flair.data,t1.data,t2.data,t1c.data])
def _scan_input(input_par): """ This private function scans the input data parameter. If it is a file name (string) it returns the mha object, else if it is already a mha object it returns the unchanged input parameter """ if type(input_par)==str: return mha.new(input_file=input_par) elif type(input_par)==types.InstanceType: return input_par
def classify_test_data_3d(activate2, W_list, b_list, path, patch_size_x, patch_size_y, patch_size_z, prefix, recon_flag): Flair = [] T1 = [] T2 = [] T_1c = [] Truth = [] Folder = [] Recon = [] Subdir_array = [] # patch_size = 11 for subdir, dirs, files in os.walk(path): # if len(Flair) < 21: # continue for file1 in files: if file1[-3:] == 'mha' and ('Flair' in file1): Flair.append(file1) Folder.append(subdir + '/') Subdir_array.append(subdir[-5:]) elif file1[-3:] == 'mha' and ('T1' in file1 and 'T1c' not in file1): T1.append(file1) elif file1[-3:] == 'mha' and ('T2' in file1): T2.append(file1) elif file1[-3:] == 'mha' and ('T1c' in file1 or 'T_1c' in file1): T_1c.append(file1) elif file1[-3:] == 'mha' and 'OT' in file1: Truth.append(file1) elif file1[-3:] == 'mha' and 'Recon' in file1: Recon.append(file1) number_of_images = len(Flair) for image_iterator in range(number_of_images): print 'Iteration : ', image_iterator + 1 print 'Folder : ', Folder[image_iterator] print '... predicting' Flair_image = mha.new(Folder[image_iterator] + Flair[image_iterator]) T1_image = mha.new(Folder[image_iterator] + T1[image_iterator]) T2_image = mha.new(Folder[image_iterator] + T2[image_iterator]) T_1c_image = mha.new(Folder[image_iterator] + T_1c[image_iterator]) # Flair_image = nib.load(Folder[image_iterator]+Flair[image_iterator]) # T1_image = nib.load(Folder[image_iterator]+T1[image_iterator]) # T2_image = nib.load(Folder[image_iterator]+T2[image_iterator]) # T_1c_image = nib.load(Folder[image_iterator]+T_1c[image_iterator]) if recon_flag is True: Recon_image = mha.new(Folder[image_iterator] + Recon[image_iterator]) Flair_image = Flair_image.data T1_image = T1_image.data T2_image = T2_image.data T_1c_image = T_1c_image.data if recon_flag is True: Recon_image = Recon_image.data xdim, ydim, zdim = Flair_image.shape prediction_image = [] Flair_patch = image.extract_patches( Flair_image, [patch_size_x, patch_size_y, patch_size_z]) T1_patch = image.extract_patches( T1_image, [patch_size_x, patch_size_y, patch_size_z]) T2_patch = image.extract_patches( T2_image, [patch_size_x, patch_size_y, patch_size_z]) T_1c_patch = image.extract_patches( T_1c_image, [patch_size_x, patch_size_y, patch_size_z]) if recon_flag is True: Recon_patch = image.extract_patches( Recon_image, [patch_size_x, patch_size_y, patch_size_z]) print 'Raw patches extracted' #print Flair_patch.shape #print T1_patch.shape #print T2_patch.shape #print T_1c_patch.shape for j in range(Flair_patch.shape[2]): #print 'Slice : ',j+1 F_slice = Flair_patch[:, :, j, :, :, :] T1_slice = T1_patch[:, :, j, :, :, :] T2_slice = T2_patch[:, :, j, :, :, :] T_1c_slice = T_1c_patch[:, :, j, :, :, :] if recon_flag is True: Recon_slice = Recon_patch[:, :, j, :, :, :] F_slice = F_slice.reshape( F_slice.shape[0] * F_slice.shape[1], patch_size_x * patch_size_y * patch_size_z) T1_slice = T1_slice.reshape( T1_slice.shape[0] * T1_slice.shape[1], patch_size_x * patch_size_y * patch_size_z) T2_slice = T2_slice.reshape( T2_slice.shape[0] * T2_slice.shape[1], patch_size_x * patch_size_y * patch_size_z) T_1c_slice = T_1c_slice.reshape( T_1c_slice.shape[0] * T_1c_slice.shape[1], patch_size_x * patch_size_y * patch_size_z) if recon_flag is True: Recon_slice = Recon_slice.reshape( Recon_slice.shape[0] * Recon_slice.shape[1], patch_size_x * patch_size_y * patch_size_z) if recon_flag == True: temp_patch = np.concatenate( [F_slice, T1_slice, T2_slice, T_1c_slice, Recon_slice], axis=1) else: temp_patch = np.concatenate( [F_slice, T1_slice, T2_slice, T_1c_slice], axis=1) #print 'Size of temp_patch : ',temp_patch.shape prediction_slice = predictOutput(temp_patch, activate2, W_list, b_list) prediction_image.append(prediction_slice) prediction_image = np.array(prediction_image) prediction_image = np.transpose(prediction_image) prediction_image = prediction_image.reshape([ xdim - patch_size_x + 1, ydim - patch_size_y + 1, zdim - patch_size_z + 1 ]) output_image = np.zeros([xdim, ydim, zdim]) output_image[1 + ((patch_size_x - 1) / 2):xdim - ((patch_size_x - 1) / 2) + 1, 1 + ((patch_size_y - 1) / 2):ydim - ((patch_size_y - 1) / 2) + 1, 1 + ((patch_size_z - 1) / 2):zdim - ((patch_size_z - 1) / 2) + 1] = prediction_image # np.save(Folder[image_iterator]+Subdir_array[image_iterator]+'_'+prefix+'_output_image.npy',output_image)#TODO: save it in meaningful name in corresponding folder #a=np.transpose(output_image) # for j in xrange(a.shape[0]): # a[j,:,:] = np.transpose(a[j,:,:]) #a=a.reshape(155,240,240) print 'writing mha...' affine = [[-1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] img = nib.Nifti1Image(output_image, affine) img.set_data_dtype(np.int32) nib.save( img, Folder[image_iterator] + Subdir_array[image_iterator] + '_' + prefix + '.nii') #output_image = itk_py_converter.GetImageFromArray(a.tolist()) #writer.SetFileName(Folder[image_iterator]+Subdir_array[image_iterator]+'_'+prefix+'_.mha') #writer.SetInput(output_image) #writer.Update() print '########success########'
def U_Patch_Preprocess_recon_2D(patch_size_x=5,patch_size_y=5,prefix='SdA',in_root='',out_root='',recon_flag=True): #Initialize user variables patch_size = patch_size_x patch_pixels = patch_size*patch_size pixel_offset = int(patch_size*0.7) padding = patch_size/2 threshold = patch_pixels*0.3 if recon_flag == False: recon_num = 4 if recon_flag == True: recon_num = 5 patches = np.zeros(patch_pixels*recon_num) ground_truth = np.zeros(1) #paths to images path = in_root Flair = [] T1 = [] T2 = [] T_1c = [] Truth = [] Recon=[] Folder = [] for subdir, dirs, files in os.walk(path): # if len(Flair) is 4: # break for file1 in files: #print file1 if file1[-3:]=='mha' and 'Flair' in file1: Flair.append(file1) Folder.append(subdir+'/') elif file1[-3:]=='mha' and 'T1' in file1: T1.append(file1) elif file1[-3:]=='mha' and 'T2' in file1: T2.append(file1) elif file1[-3:]=='mha' and 'T_1c' in file1: T_1c.append(file1) elif file1[-3:]=='mha' and 'OT' in file1: Truth.append(file1) #elif file1[-3:]=='mha' and 'Recon' in file1: # Recon.append(file1) number_of_images = len(Flair) print 'Number of images : ', number_of_images for image_iterator in range(number_of_images): print 'Iteration : ',image_iterator+1 print 'Folder : ', Folder[image_iterator] Flair_image = mha.new(Folder[image_iterator]+Flair[image_iterator]) T1_image = mha.new(Folder[image_iterator]+T1[image_iterator]) T2_image = mha.new(Folder[image_iterator]+T2[image_iterator]) T_1c_image = mha.new(Folder[image_iterator]+T_1c[image_iterator]) if recon_flag == True: Recon_image = mha.new(Folder[image_iterator]+Recon[image_iterator]) Truth_image = mha.new(Folder[image_iterator]+Truth[image_iterator]) Flair_image = Flair_image.data T1_image = T1_image.data T2_image = T2_image.data T_1c_image = T_1c_image.data if recon_flag == True: Recon_image=Recon_image.data Truth_image = Truth_image.data x_span,y_span,z_span = np.where(Truth_image!=0) start_slice = min(z_span) stop_slice = max(z_span) image_patch = np.zeros(patch_size*patch_size*recon_num) image_label = np.zeros(1) for i in range(start_slice, stop_slice+1): Flair_slice = np.transpose(Flair_image[:,:,i]) T1_slice = np.transpose(T1_image[:,:,i]) T2_slice = np.transpose(T2_image[:,:,i]) T_1c_slice = np.transpose(T_1c_image[:,:,i]) if recon_flag==True: Recon_slice = np.transpose(Recon_image[:,:,i]) Truth_slice = np.transpose(Truth_image[:,:,i]) x_dim,y_dim = np.size(Flair_slice,axis=0), np.size(Flair_slice, axis=1) x_span,y_span = np.where(Truth_slice!=0) if len(x_span)==0 or len(y_span)==0: continue x_start = np.min(x_span) - padding x_stop = np.max(x_span) + padding+1 y_start = np.min(y_span) - padding y_stop = np.max(y_span) + padding+1 Flair_patch = image.extract_patches(Flair_slice[x_start:x_stop, y_start:y_stop], patch_size, extraction_step = pixel_offset) T1_patch = image.extract_patches(T1_slice[x_start:x_stop, y_start:y_stop], patch_size, extraction_step = pixel_offset) T2_patch = image.extract_patches(T2_slice[x_start:x_stop, y_start:y_stop], patch_size, extraction_step = pixel_offset) T_1c_patch = image.extract_patches(T_1c_slice[x_start:x_stop, y_start:y_stop], patch_size, extraction_step = pixel_offset) if recon_flag==True: Recon_patch = image.extract_patches(Recon_slice[x_start:x_stop, y_start:y_stop], patch_size, extraction_step = pixel_offset) Truth_patch = image.extract_patches(Truth_slice[x_start:x_stop, y_start:y_stop], patch_size, extraction_step = pixel_offset) #print '1. truth dimension :', Truth_patch.shape Flair_patch = Flair_patch.reshape(Flair_patch.shape[0]*Flair_patch.shape[1], patch_size*patch_size) T1_patch = T1_patch.reshape(T1_patch.shape[0]*T1_patch.shape[1], patch_size*patch_size) T2_patch = T2_patch.reshape(T2_patch.shape[0]*T2_patch.shape[1], patch_size*patch_size) T_1c_patch = T_1c_patch.reshape(T_1c_patch.shape[0]*T_1c_patch.shape[1], patch_size*patch_size) if recon_flag==True: Recon_patch = Recon_patch.reshape(Recon_patch.shape[0]*Recon_patch.shape[1], patch_size*patch_size) Truth_patch = Truth_patch.reshape(Truth_patch.shape[0]*Truth_patch.shape[1], patch_size, patch_size) #print '2. truth dimension :', Truth_patch.shape if recon_flag == True: slice_patch = np.concatenate([Flair_patch, T1_patch, T2_patch, T_1c_patch,Recon_patch], axis=1) else: slice_patch = np.concatenate([Flair_patch, T1_patch, T2_patch, T_1c_patch], axis=1) Truth_patch = Truth_patch[:,(patch_size-1)/2,(patch_size-1)/2] Truth_patch = np.array(Truth_patch) Truth_patch = Truth_patch.reshape(len(Truth_patch),1) #print '3. truth dimension :', Truth_patch.shape # image_patch = np.vstack([image_patch,slice_patch]) # image_label = np.vstack([image_label, Truth_patch]) # num_of_class = [] # for i in xrange(5): # num_of_class.append(np.sum((image_label==i).astype(int))) # min_num = min(num_of_class) # min_num_2 = min(x for x in num_of_class if x!=min_num) # for i in xrange(5): # #print 'image patch : ', image_patch.shape # #print 'image_label : ', image_label.shape # index_x,index_y = np.where(image_label==i) # temp_patch = image_patch[index_x,:] # temp_label = image_label[index_x,:] # index = np.arange(len(temp_patch)) # shuffle(index) # #print 'Temp patch : ', temp_patch.shape # #print 'Temp_label : ', temp_label.shape # if len(index)>min_num_2: # temp_patch = temp_patch[index[0:min_num_2],:] # temp_label = temp_label[index[0:min_num_2],:] # patches = np.vstack([patches,temp_patch]) # ground_truth = np.vstack([ground_truth, temp_label]) #------check indentation-----# patches = np.vstack([patches,slice_patch]) ground_truth = np.vstack([ground_truth, Truth_patch]) for k, item in enumerate(ground_truth): if item != 0: ground_truth[k] = 1 print 'Number of non-zeros in ground truth : ', np.sum((ground_truth!=0).astype(int)) print 'Number of zeros in ground truth : ', np.sum((ground_truth==0).astype(int)) ground_truth = ground_truth.reshape(len(ground_truth)) if recon_flag==False: patches = patches[:,0:patch_size*patch_size*4] if 'training' in out_root and recon_flag == True: print'... Saving the 2D training patches' np.save(out_root+'u_10_trainpatch_2D_'+prefix+'_.npy',patches) np.save(out_root+'u_10_trainlabel_2D_'+prefix+'_.npy',ground_truth) elif recon_flag == True: print '... Saving the 2D validation patches' np.save(out_root+'u_10_validpatch_2D_'+prefix+'_.npy',patches) np.save(out_root+'u_10_validlabel_2D_'+prefix+'_.npy',ground_truth) if 'training' in out_root and recon_flag == False: print'... Saving the 2D training patches' np.save(out_root+'u_10_trainpatch_2D_'+prefix+'_.npy',patches) np.save(out_root+'u_10_trainlabel_2D_'+prefix+'_.npy',ground_truth) elif recon_flag == False: print '... Saving the 2D validation patches' np.save(out_root+'u_10_validpatch_2D_'+prefix+'_.npy',patches) np.save(out_root+'u_10_validlabel_2D_'+prefix+'_.npy',ground_truth)
def generate_cubes(): dirName = 'p' + str(plen) + 'o' + str(offset) + 'm' + str(mask) fh = open("output.txt", "a")#Output file for debugging if not os.path.exists(DATA_PATH + '/' + dirName): os.makedirs(DATA_PATH + '/' + dirName) #Read patients list patients = os.listdir(IM_PATH) #for patIdx in xrange(bratsPatients): for p in sorted(patients): #Patstr = str(patIdx+1).zfill(3) #Padding the string with zeros on left #SeqStr = str(seqIdx+1).zfill(3) #truth = nib.load(TRUTH_PATH + '/' + p + '/masks/training' + Patstr + '_' + TimStr + '_mask' + str(mask) + '.nii').get_data() truthf = os.listdir(TRUTH_PATH+'/'+p) truth = mha.new(truthf) truth = truth.data shape = numpy.array(truth.size) numCubes = (shape - 2*(offset/2))/numPred #numpred=plen-offset+1 newShape = 2*(offset/2) + numCubes*numPred cutOff = shape - newShape truthCrop = truth[cutOff[0]/2:cutOff[0]/2 + newShape[0], cutOff[1]/2:cutOff[1]/2 + newShape[1], cutOff[2]/2:cutOff[2]/2 + newShape[2]] SEQ_PATH = IM_PATH + '/' + p #Read sequences list files = os.listdir(SEQ_PATH) #for seqIdx in xrange(bratsSeqs): for i in range(bratsSeqs): f = sorted(files)[i] path = os.path.join(SEQ_PATH,f) if os.path.isfile(path): data = numpy.zeros([bratsChannels,newShape[0],newShape[1],newShape[2]],dtype = 'float32') #data[0,:,:,:] = nib.load(IM_PATH + '/training' + Patstr + '/' + TimStr + '/N_training' + Patstr + '_' + TimStr + '_flair_pp.nii').get_data()[cutOff[0]/2:cutOff[0]/2 + newShape[0],cutOff[1]/2:cutOff[1]/2 + newShape[1],cutOff[2]/2:cutOff[2]/2 + newShape[2]] #data[1,:,:,:] = nib.load(IM_PATH + '/training' + Patstr + '/' + TimStr + '/N_training' + Patstr + '_' + TimStr + '_mprage_pp.nii').get_data()[cutOff[0]/2:cutOff[0]/2 + newShape[0],cutOff[1]/2:cutOff[1]/2 + newShape[1],cutOff[2]/2:cutOff[2]/2 + newShape[2]] #data[2,:,:,:] = nib.load(IM_PATH + '/training' + Patstr + '/' + TimStr + '/N_training' + Patstr + '_' + TimStr + '_pd_pp.nii').get_data()[cutOff[0]/2:cutOff[0]/2 + newShape[0],cutOff[1]/2:cutOff[1]/2 + newShape[1],cutOff[2]/2:cutOff[2]/2 + newShape[2]] #data[3,:,:,:] = nib.load(IM_PATH + '/training' + Patstr + '/' + TimStr + '/N_training' + Patstr + '_' + TimStr + '_t2_pp.nii').get_data()[cutOff[0]/2:cutOff[0]/2 + newShape[0],cutOff[1]/2:cutOff[1]/2 + newShape[1],cutOff[2]/2:cutOff[2]/2 + newShape[2]] data[i,:,:,:] = f.get_data()[cutOff[0]/2:cutOff[0]/2 + newShape[0],cutOff[1]/2:cutOff[1]/2 + newShape[1],cutOff[2]/2:cutOff[2]/2 + newShape[2]] data = data.astype('float32') #count = 0 dataList = [] truthList = [] for ix in xrange(numCubes[0]): startx = ix*numPred endx = startx + numPred + 2*(offset/2) for iy in xrange(numCubes[1]): starty = iy*numPred endy = starty + numPred + 2*(offset/2) for iz in xrange(numCubes[2]): startz = iz*numPred endz = startz + numPred + 2*(offset/2) tumourVoxels = numpy.array(truthCrop[startx+offset/2:endx-offset/2,starty+offset/2:endy-offset/2,startz+offset/2:endz-offset/2]) #NtumourVoxels = numpy.sum(tumourVoxels) unique, counts = np.unique(tumourVoxels, return_counts=True) #y=numpy.asarray((unique)).T z=numpy.asarray((counts)).T sum=numpy.sum(z) if(sum > Threshold): #count = count+1 dataList.append(data[:,startx:endx,starty:endy,startz:endz]) truthList.append(tumourVoxels) #data_file = file(DATA_PATH + '/' + dirName + '/pat_'+ Patstr + '_time_' + TimStr + '.pkl','wb') data_file = file(DATA_PATH + '/' + dirName + '/'+ p + '.pkl','wb') truth_file = file(DATA_PATH + '/' + dirName + '/' + p + '_truth.pkl','wb') cPickle.dump(numpy.array(dataList),data_file,cPickle.HIGHEST_PROTOCOL) cPickle.dump(numpy.array(truthList),truth_file,cPickle.HIGHEST_PROTOCOL) data_file.close() truth_file.close() u= 'Pat ' + p + ' had ' + str(sum) + ' tumour cubes\n' fh.write(u) fh.close
elif file1[-3:]=='mha' and 'T2' in file1: T2.append(file1) elif file1[-3:]=='mha' and 'T_1c' in file1: T_1c.append(file1) elif file1[-3:]=='mha' and 'OT' in file1: Truth.append(file1) number_of_images = len(Flair) print 'Number of images : ', number_of_images count1, count2, count3, count4, count5 = 0,0,0,0,0 for image_iterator in range(number_of_images): print 'Image number : ',image_iterator+1 print 'Folder : ', Folder[image_iterator] Flair_image = mha.new(Folder[image_iterator]+Flair[image_iterator]) T1_image = mha.new(Folder[image_iterator]+T1[image_iterator]) T2_image = mha.new(Folder[image_iterator]+T2[image_iterator]) T_1c_image = mha.new(Folder[image_iterator]+T_1c[image_iterator]) Truth_image = mha.new(Folder[image_iterator]+Truth[image_iterator]) Flair_image = Flair_image.data T1_image = T1_image.data T2_image = T2_image.data T_1c_image = T_1c_image.data Truth_image = Truth_image.data x_span,y_span,z_span = np.where(Truth_image!=0) start_slice = min(z_span) stop_slice = max(z_span)
def classify_test_data(activate2, W_list, b_list): path = '../BRATS/Normalised_Testing/' Flair = [] T1 = [] T2 = [] T_1c = [] Folder = [] Subdir_array = [] patch_size = 11 for subdir, dirs, files in os.walk(path): if len(Flair) is 5: break for file1 in files: #print file1 if file1[-3:]=='mha' and 'Flair' in file1: Flair.append(file1) Folder.append(subdir+'/') Subdir_array.append(subdir[-5:]) elif file1[-3:]=='mha' and 'T1' in file1: T1.append(file1) elif file1[-3:]=='mha' and 'T2' in file1: T2.append(file1) elif file1[-3:]=='mha' and 'T_1c' in file1: T_1c.append(file1) number_of_images = len(Flair) for image_iterator in range(number_of_images): print 'Iteration : ',image_iterator+1 print 'Folder : ', Folder[image_iterator] print '... predicting' Flair_image = mha.new(Folder[image_iterator]+Flair[image_iterator]) T1_image = mha.new(Folder[image_iterator]+T1[image_iterator]) T2_image = mha.new(Folder[image_iterator]+T2[image_iterator]) T_1c_image = mha.new(Folder[image_iterator]+T_1c[image_iterator]) Flair_image = Flair_image.data T1_image = T1_image.data T2_image = T2_image.data T_1c_image = T_1c_image.data xdim, ydim, zdim = Flair_image.shape prediction_image = [] for i in range(zdim): Flair_slice = np.transpose(Flair_image[:,:,i]) T1_slice = np.transpose(T1_image[:,:,i]) T2_slice = np.transpose(T2_image[:,:,i]) T_1c_slice = np.transpose(T_1c_image[:,:,i]) Flair_patch = image.extract_patches_2d(Flair_slice, (patch_size, patch_size)) F_P=Flair_patch.reshape(len(Flair_patch),patch_size*patch_size) T1_patch = image.extract_patches_2d(T1_slice, (patch_size, patch_size)) T1_P=T1_patch.reshape(len(Flair_patch),patch_size*patch_size) T2_patch = image.extract_patches_2d(T2_slice, (patch_size, patch_size)) T2_P=T2_patch.reshape(len(Flair_patch),patch_size*patch_size) T_1c_patch = image.extract_patches_2d(T_1c_slice, (patch_size, patch_size)) T1c_P=T_1c_patch.reshape(len(Flair_patch),patch_size*patch_size) temp_patch = np.concatenate([F_P,T1_P,T2_P,T1c_P],axis=1) prediction_slice = predictOutput(temp_patch, activate2, W_list, b_list) prediction_image.append(prediction_slice) prediction_image = np.array(prediction_image) prediction_image = np.transpose(prediction_image) prediction_image = prediction_image.reshape([xdim-patch_size+1, ydim-patch_size+1, zdim]) output_image = np.zeros([xdim,ydim,zdim]) output_image[1+((patch_size-1)/2):xdim-((patch_size-1)/2)+1,1+((patch_size-1)/2):ydim-((patch_size-1)/2)+1,:] = prediction_image np.save(Folder[image_iterator]+Subdir_array[image_iterator]+'_output_image.npy',output_image)#TODO: save it in meaningful name in corresponding folder
def classify_test_data_3d(activate2, W_list, b_list): path = '../BRATS/10_1/Normalised_Test/' Flair = [] T1 = [] T2 = [] T_1c = [] Folder = [] Subdir_array = [] # patch_size = 11 patch_size_x = 5 patch_size_y = 5 patch_size_z = 5 for subdir, dirs, files in os.walk(path): if len(Flair) is 20: break for file1 in files: #print file1 if file1[-3:]=='mha' and 'Flair' in file1: Flair.append(file1) Folder.append(subdir+'/') Subdir_array.append(subdir[-5:]) elif file1[-3:]=='mha' and 'T1' in file1: T1.append(file1) elif file1[-3:]=='mha' and 'T2' in file1: T2.append(file1) elif file1[-3:]=='mha' and 'T_1c' in file1: T_1c.append(file1) number_of_images = len(Flair) for image_iterator in range(number_of_images): print 'Iteration : ',image_iterator+1 print 'Folder : ', Folder[image_iterator] print '... predicting' Flair_image = mha.new(Folder[image_iterator]+Flair[image_iterator]) T1_image = mha.new(Folder[image_iterator]+T1[image_iterator]) T2_image = mha.new(Folder[image_iterator]+T2[image_iterator]) T_1c_image = mha.new(Folder[image_iterator]+T_1c[image_iterator]) Flair_image = Flair_image.data T1_image = T1_image.data T2_image = T2_image.data T_1c_image = T_1c_image.data xdim, ydim, zdim = Flair_image.shape prediction_image = [] Flair_patch = image.extract_patches(Flair_image, [patch_size_x,patch_size_y,patch_size_z]) T1_patch = image.extract_patches(T1_image, [patch_size_x,patch_size_y,patch_size_z]) T2_patch = image.extract_patches(T2_image, [patch_size_x,patch_size_y,patch_size_z]) T_1c_patch = image.extract_patches(T_1c_image, [patch_size_x,patch_size_y,patch_size_z]) print 'Raw patches extracted' print Flair_patch.shape print T1_patch.shape print T2_patch.shape print T_1c_patch.shape for j in range(Flair_patch.shape[2]): print 'Slice : ',j+1 F_slice = Flair_patch[:,:,j,:,:,:] T1_slice = T1_patch[:,:,j,:,:,:] T2_slice = T2_patch[:,:,j,:,:,:] T_1c_slice = T_1c_patch[:,:,j,:,:,:] F_slice = F_slice.reshape(F_slice.shape[0]*F_slice.shape[1], patch_size_x*patch_size_y*patch_size_z) T1_slice = T1_slice.reshape(T1_slice.shape[0]*T1_slice.shape[1], patch_size_x*patch_size_y*patch_size_z) T2_slice = T2_slice.reshape(T2_slice.shape[0]*T2_slice.shape[1], patch_size_x*patch_size_y*patch_size_z) T_1c_slice = T_1c_slice.reshape(T_1c_slice.shape[0]*T_1c_slice.shape[1], patch_size_x*patch_size_y*patch_size_z) temp_patch = np.concatenate([F_slice,T1_slice,T2_slice,T_1c_slice],axis=1) print 'Size of temp_patch : ',temp_patch.shape prediction_slice = predictOutput(temp_patch, activate2, W_list, b_list) prediction_image.append(prediction_slice) prediction_image = np.array(prediction_image) prediction_image = np.transpose(prediction_image) prediction_image = prediction_image.reshape([xdim-patch_size_x+1, ydim-patch_size_y+1, zdim-patch_size_z+1]) output_image = np.zeros([xdim,ydim,zdim]) output_image[1+((patch_size_x-1)/2):xdim-((patch_size_x-1)/2)+1,1+((patch_size_y-1)/2):ydim-((patch_size_y-1)/2)+1,1+((patch_size_z-1)/2):zdim-((patch_size_z-1)/2)+1] = prediction_image np.save(Folder[image_iterator]+Subdir_array[image_iterator]+'_output_image.npy',output_image)#TODO: save it in meaningful name in corresponding folder
import os import sys import mha file1 = '/home/dueo/data/BRATS/BRATS-2/Image_Data/HG/0001/VSD.Brain.XX.O.MR_T1/VSD.Brain.XX.O.MR_T1.685.mha' file1 = '/Users/oli/Proj_Large_Data/Deep_Learning_MRI/BRATS-2/Image_Data/HG/0001/VSD.Brain.XX.O.MR_T1/VSD.Brain.XX.O.MR_T1.685.mha' img = mha.new(input_file=file1) img.read_mha(file1) #from medpy.io import load #image_data, image_header = load(file1)
def classify_test_data_3d(activate2, W_list, b_list, path, patch_size_x, patch_size_y, patch_size_z, prefix, recon_flag, writer, itk_py_converter): Flair = [] T1 = [] T2 = [] T_1c = [] Truth = [] Folder = [] Recon = [] Subdir_array = [] # patch_size = 11 for subdir, dirs, files in os.walk(path): # if(len(Flair) is 1): # break for file1 in files: if file1[-3:]=='mha' and ('Flair' in file1): Flair.append(file1) Folder.append(subdir+'/') Subdir_array.append(subdir[-5:]) elif file1[-3:]=='mha' and ('t1_z' in file1 or 'T1' in file1): T1.append(file1) elif file1[-3:]=='mha' and ('t2' in file1 or 'T2' in file1): T2.append(file1) elif file1[-3:]=='mha' and ('t1c_z' in file1 or 'T_1c' in file1): T_1c.append(file1) elif file1[-3:]=='mha' and 'OT' in file1: Truth.append(file1) elif file1[-3:]=='mha' and 'Recon' in file1: Recon.append(file1) number_of_images = len(Flair) for image_iterator in range(number_of_images): print 'Iteration : ',image_iterator+1 print 'Folder : ', Folder[image_iterator] print '... predicting' Flair_image = mha.new(Folder[image_iterator]+Flair[image_iterator]) T1_image = mha.new(Folder[image_iterator]+T1[image_iterator]) T2_image = mha.new(Folder[image_iterator]+T2[image_iterator]) T_1c_image = mha.new(Folder[image_iterator]+T_1c[image_iterator]) if recon_flag is True: Recon_image = mha.new(Folder[image_iterator]+Recon[image_iterator]) Flair_image = Flair_image.data T1_image = T1_image.data T2_image = T2_image.data T_1c_image = T_1c_image.data if recon_flag is True: Recon_image = Recon_image.data xdim, ydim, zdim = Flair_image.shape prediction_image = [] Flair_patch = image.extract_patches(Flair_image, [patch_size_x,patch_size_y,patch_size_z]) T1_patch = image.extract_patches(T1_image, [patch_size_x,patch_size_y,patch_size_z]) T2_patch = image.extract_patches(T2_image, [patch_size_x,patch_size_y,patch_size_z]) T_1c_patch = image.extract_patches(T_1c_image, [patch_size_x,patch_size_y,patch_size_z]) if recon_flag is True: Recon_patch = image.extract_patches(Recon_image, [patch_size_x,patch_size_y,patch_size_z]) print 'Raw patches extracted' #print Flair_patch.shape #print T1_patch.shape #print T2_patch.shape #print T_1c_patch.shape for j in range(Flair_patch.shape[2]): #print 'Slice : ',j+1 F_slice = Flair_patch[:,:,j,:,:,:] T1_slice = T1_patch[:,:,j,:,:,:] T2_slice = T2_patch[:,:,j,:,:,:] T_1c_slice = T_1c_patch[:,:,j,:,:,:] if recon_flag is True: Recon_slice = Recon_patch[:,:,j,:,:,:] F_slice = F_slice.reshape(F_slice.shape[0]*F_slice.shape[1], patch_size_x*patch_size_y*patch_size_z) T1_slice = T1_slice.reshape(T1_slice.shape[0]*T1_slice.shape[1], patch_size_x*patch_size_y*patch_size_z) T2_slice = T2_slice.reshape(T2_slice.shape[0]*T2_slice.shape[1], patch_size_x*patch_size_y*patch_size_z) T_1c_slice = T_1c_slice.reshape(T_1c_slice.shape[0]*T_1c_slice.shape[1], patch_size_x*patch_size_y*patch_size_z) if recon_flag is True: Recon_slice = Recon_slice.reshape(Recon_slice.shape[0]*Recon_slice.shape[1], patch_size_x*patch_size_y*patch_size_z) if recon_flag == True: temp_patch = np.concatenate([F_slice,T1_slice,T2_slice,T_1c_slice,Recon_slice],axis=1) else: temp_patch = np.concatenate([F_slice,T1_slice,T2_slice,T_1c_slice],axis=1) #print 'Size of temp_patch : ',temp_patch.shape prediction_slice = predictOutput(temp_patch, activate2, W_list, b_list) prediction_image.append(prediction_slice) prediction_image = np.array(prediction_image) prediction_image = np.transpose(prediction_image) prediction_image = prediction_image.reshape([xdim-patch_size_x+1, ydim-patch_size_y+1, zdim-patch_size_z+1]) output_image = np.zeros([xdim,ydim,zdim]) output_image[1+((patch_size_x-1)/2):xdim-((patch_size_x-1)/2)+1,1+((patch_size_y-1)/2):ydim-((patch_size_y-1)/2)+1,1+((patch_size_z-1)/2):zdim-((patch_size_z-1)/2)+1] = prediction_image # np.save(Folder[image_iterator]+Subdir_array[image_iterator]+'_'+prefix+'_output_image.npy',output_image)#TODO: save it in meaningful name in corresponding folder a=np.transpose(output_image) # for j in xrange(a.shape[0]): # a[j,:,:] = np.transpose(a[j,:,:]) #a=a.reshape(155,240,240) print 'writing mha...' output_image = itk_py_converter.GetImageFromArray(a.tolist()) writer.SetFileName(Folder[image_iterator]+Subdir_array[image_iterator]+'_'+prefix+'_.mha') writer.SetInput(output_image) writer.Update() print '########success########'
def classify_test_data(activate2, W_list, b_list, path, patch_size, prefix, recon_flag, slice_num=1): Flair = [] T1 = [] T2 = [] T_1c = [] Recon = [] Folder = [] Truth=[] Subdir_array = [] label_num = 5 for subdir, dirs, files in os.walk(path): # if len(Flair) is 1: # break for file1 in files: #print file1 if file1[-3:]=='nii' and ( 'Flair' in file1): Flair.append(file1) Folder.append(subdir+'/') Subdir_array.append(subdir[-5:]) elif file1[-3:]=='nii' and ('T1' in file1 and 'T1c' not in file1): T1.append(file1) elif file1[-3:]=='nii' and ('T2' in file1): T2.append(file1) elif file1[-3:]=='nii' and ('T1c' in file1 or 'T_1c' in file1): T_1c.append(file1) elif file1[-3:]=='mha' and 'OT' in file1: Truth.append(file1) elif file1[-3:]=='mha' and 'Recon' in file1: Recon.append(file1) number_of_images = len(Flair) for image_iterator in range(number_of_images): print 'Iteration : ',image_iterator+1 print 'Folder : ', Folder[image_iterator] print 'T2: ', T2[image_iterator] print '... predicting' Flair_image = nib.load(Folder[image_iterator]+Flair[image_iterator]) T1_image = nib.load(Folder[image_iterator]+T1[image_iterator]) T2_image = nib.load(Folder[image_iterator]+T2[image_iterator]) T_1c_image = nib.load(Folder[image_iterator]+T_1c[image_iterator]) if recon_flag is True: Recon_image = mha.new(Folder[image_iterator]+Recon[image_iterator]) Flair_image = Flair_image.get_data() T1_image = T1_image.get_data() T2_image = T2_image.get_data() T_1c_image = T_1c_image.get_data() if recon_flag is True: Recon_image = Recon_image.data print 'Input shape: ', Flair_image.shape if slice_num ==2: Flair_image = np.swapaxes(Flair_image,0,1) Flair_image = np.swapaxes(Flair_image,1,2) T1_image = np.swapaxes(T1_image, 0,1) T1_image = np.swapaxes(T1_image, 1, 2) T2_image = np.swapaxes(T2_image, 0,1) T2_image = np.swapaxes(T2_image, 1, 2) T_1c_image = np.swapaxes(T_1c_image, 0,1) T_1c_image = np.swapaxes(T_1c_image, 1, 2) elif slice_num == 3: Flair_image = np.swapaxes(Flair_image,0,1) Flair_image = np.swapaxes(Flair_image,0,2) T1_image = np.swapaxes(T1_image, 0,1) T1_image = np.swapaxes(T1_image, 0, 2) T2_image = np.swapaxes(T2_image, 0,1) T2_image = np.swapaxes(T2_image, 0, 2) T_1c_image = np.swapaxes(T_1c_image, 0,1) T_1c_image = np.swapaxes(T_1c_image, 0, 2) xdim, ydim, zdim = Flair_image.shape prediction_image = [] # if slice_num ==1: # prediction_image = np.zeros([(xdim-patch_size+1)*(ydim-patch_size+1), zdim]) # elif slice_num==2 or slice_num==3: # prediction_image = np.zeros([zdim, (ydim-patch_size+1)*(xdim-patch_size+1)]) print print 'shape:',Flair_image.shape for i in range(zdim): Flair_slice = np.transpose(Flair_image[:,:,i]) T1_slice = np.transpose(T1_image[:,:,i]) T2_slice = np.transpose(T2_image[:,:,i]) T_1c_slice = np.transpose(T_1c_image[:,:,i]) if recon_flag is True: Recon_slice = np.transpose(Recon_image[:,:,i]) Flair_patch = image.extract_patches_2d(Flair_slice, (patch_size, patch_size)) F_P=Flair_patch.reshape(len(Flair_patch),patch_size*patch_size) T1_patch = image.extract_patches_2d(T1_slice, (patch_size, patch_size)) T1_P=T1_patch.reshape(len(Flair_patch),patch_size*patch_size) T2_patch = image.extract_patches_2d(T2_slice, (patch_size, patch_size)) T2_P=T2_patch.reshape(len(Flair_patch),patch_size*patch_size) T_1c_patch = image.extract_patches_2d(T_1c_slice, (patch_size, patch_size)) T1c_P=T_1c_patch.reshape(len(Flair_patch),patch_size*patch_size) temp_patch = np.concatenate([F_P,T1_P,T2_P,T1c_P],axis=1) prediction_slice = predictOutput(temp_patch, activate2, W_list, b_list) # print 'Shape of slice : ', prediction_slice.shape prediction_image.append(prediction_slice) prediction_image = np.array(prediction_image) print 'Prediction shape : ', prediction_image.shape # print 'Slice num : ',slice_num if slice_num==1: prediction_image = np.transpose(prediction_image,(1,0,2)) print 'Look here : ', prediction_image.shape prediction_image = prediction_image.reshape([xdim-patch_size+1, ydim-patch_size+1, zdim, label_num]) print 'Look after : ', prediction_image.shape output_image = np.zeros([xdim,ydim,zdim,label_num]) output_image[1+((patch_size-1)/2):xdim-((patch_size-1)/2)+1,1+((patch_size-1)/2):ydim-((patch_size-1)/2)+1,:] = prediction_image print 'output shape', output_image.shape elif slice_num==2: prediction_image = prediction_image.reshape([zdim, ydim-patch_size+1, xdim-patch_size+1, label_num]) output_image = np.zeros([xdim,ydim,zdim,label_num]) output_image[:,1+((patch_size-1)/2):ydim-((patch_size-1)/2)+1,1+((patch_size-1)/2):xdim-((patch_size-1)/2)+1] = prediction_image output_image = np.swapaxes(output_image,2,1) output_image = np.swapaxes(output_image,1,0) elif slice_num == 3: prediction_image = prediction_image.reshape([zdim, ydim-patch_size+1, xdim-patch_size+1, label_num]) output_image = np.zeros([zdim,ydim,xdim,label_num]) output_image[:,1+((patch_size-1)/2):ydim-((patch_size-1)/2)+1,1+((patch_size-1)/2):xdim-((patch_size-1)/2)+1,:] = prediction_image # print # np.save(,output_image)#TODO: save it in meaningful name in corresponding folder # break print 'Output shape : ', output_image.shape np.save(Folder[image_iterator]+Subdir_array[image_iterator]+prefix+'.npy', output_image) output_image = np.argmax(output_image,axis = 3) a = output_image for j in xrange(a.shape[2]): a[:,:,j] = np.transpose(a[:,:,j]) print 'a shape here: ', a.shape output_image = a # save_image = np.zeros() # output_image = itk_py_converter.GetImageFromArray(a.tolist()) # writer.SetFileName(Folder[image_iterator]+Subdir_array[image_iterator]+'_'+prefix+'_.mha') # writer.SetInput(output_image) # writer.Update() affine=[[-1,0,0,0],[0,-1,0,0],[0,0,1,0],[0,0,0,1]] img = nib.Nifti1Image(output_image, affine) img.set_data_dtype(np.int32) nib.save(img,Folder[image_iterator]+Subdir_array[image_iterator]+prefix+'xyz.nii') print '########success########'
import os import sys import time import theano import theano.tensor as T import theano.tensor.nlinalg as LA import numpy from theano.printing import pp import mha sys.path.insert(1,'../headers/') flair = mha.new('../patient1/BRATS_HG0001/BRATS_HG0001_FLAIR.mha') t1 = mha.new('../patient1/BRATS_HG0001/BRATS_HG0001_T1.mha') t2 = mha.new('../patient1/BRATS_HG0001/BRATS_HG0001_T2.mha') t1c = mha.new('../patient1/BRATS_HG0001/BRATS_HG0001_T1C.mha') truth = mha.new('../patient1/BRATS_HG0001_truth.mha')
import os import sys import mha file1 = '/home/dueo/data/BRATS/BRATS-2/Image_Data/HG/0001/VSD.Brain.XX.O.MR_T1/VSD.Brain.XX.O.MR_T1.685.mha' file1 = '/Users/oli/Proj_Large_Data/Deep_Learning_MRI/BRATS-2/Image_Data/HG/0001/VSD.Brain.XX.O.MR_T1/VSD.Brain.XX.O.MR_T1.685.mha' img=mha.new(input_file=file1) img.read_mha(file1) #from medpy.io import load #image_data, image_header = load(file1)
elif file1[-3:]=='mha' and 'T2' in file1: T2.append(file1) elif file1[-3:]=='mha' and 'T_1c' in file1: T_1c.append(file1) elif file1[-3:]=='mha' and 'OT' in file1: Truth.append(file1) number_of_images = len(Flair) print 'Number of images : ', number_of_images count1, count2, count3, count4, count5 = 0,0,0,0,0 for image_iterator in range(number_of_images): print 'Iteration : ',image_iterator+1 print 'Folder : ', Folder[image_iterator] Flair_image = mha.new(Folder[image_iterator]+Flair[image_iterator]) T1_image = mha.new(Folder[image_iterator]+T1[image_iterator]) T2_image = mha.new(Folder[image_iterator]+T2[image_iterator]) T_1c_image = mha.new(Folder[image_iterator]+T_1c[image_iterator]) Truth_image = mha.new(Folder[image_iterator]+Truth[image_iterator]) Flair_image = Flair_image.data T1_image = T1_image.data T2_image = T2_image.data T_1c_image = T_1c_image.data Truth_image = Truth_image.data x_span,y_span,z_span = np.where(Truth_image!=0) start_slice = min(z_span) stop_slice = max(z_span)
w_shp = (1,1,9,9) w_bound = numpy.sqrt(9*9) W = theano.shared(numpy.asarray(rng.uniform(low = -1.0/w_bound, high = 1.0/w_bound, size = w_shp), dtype = input.dtype), name = 'W') b_shp = (1,) b= theano.shared(numpy.asarray( rng.uniform(low = -.5, high = .5, size = b_shp), dtype = input.dtype), name = 'b') conv_out = conv.conv2d(input,W) output = T.nnet.sigmoid(conv_out + b.dimshuffle('x', 0, 'x', 'x')) f = theano.function([input],output) T1C = mha.new() T1C.read_mha('../patient1/BRATS_HG0001_T1C.mha') img = T1C.data[:,:,100] img = numpy.float32(img)/numpy.max(img) img_ = img.reshape(1,1,160,216) f_img = f(img_) pylab.subplot(2, 1, 1); pylab.axis('off'); pylab.imshow(img) pylab.gray(); # recall that the convOp output (filtered image) is actually a "minibatch", # of size 1 here, so we take index 0 in the first dimension: pylab.subplot(2, 1, 2); pylab.axis('off'); pylab.imshow(f_img[0, 0, :, :]) #pylab.subplot(1, 3, 3); pylab.axis('off'); pylab.imshow(f_img[0, 1, :, :]) pylab.show()
def classify_test_data(activate2, W_list, b_list, path, patch_size, prefix, recon_flag, slice_num=1): Flair = [] T1 = [] T2 = [] T_1c = [] Recon = [] Folder = [] Truth = [] Subdir_array = [] for subdir, dirs, files in os.walk(path): # if len(Flair) is 1: # break for file1 in files: #print file1 if file1[-3:] == 'nii' and ('Flair' in file1): Flair.append(file1) Folder.append(subdir + '/') Subdir_array.append(subdir[-5:]) elif file1[-3:] == 'nii' and ('T1' in file1 and 'T1c' not in file1): T1.append(file1) elif file1[-3:] == 'nii' and ('T2' in file1): T2.append(file1) elif file1[-3:] == 'nii' and ('T1c' in file1 or 'T_1c' in file1): T_1c.append(file1) elif file1[-3:] == 'mha' and 'OT' in file1: Truth.append(file1) elif file1[-3:] == 'mha' and 'Recon' in file1: Recon.append(file1) number_of_images = len(Flair) for image_iterator in range(number_of_images): print 'Iteration : ', image_iterator + 1 print 'Folder : ', Folder[image_iterator] print 'T2: ', T2[image_iterator] print '... predicting' Flair_image = nib.load(Folder[image_iterator] + Flair[image_iterator]) T1_image = nib.load(Folder[image_iterator] + T1[image_iterator]) T2_image = nib.load(Folder[image_iterator] + T2[image_iterator]) T_1c_image = nib.load(Folder[image_iterator] + T_1c[image_iterator]) if recon_flag is True: Recon_image = mha.new(Folder[image_iterator] + Recon[image_iterator]) Flair_image = Flair_image.get_data() T1_image = T1_image.get_data() T2_image = T2_image.get_data() T_1c_image = T_1c_image.get_data() if recon_flag is True: Recon_image = Recon_image.data print 'Input shape: ', Flair_image.shape if slice_num == 2: Flair_image = np.swapaxes(Flair_image, 0, 1) Flair_image = np.swapaxes(Flair_image, 1, 2) T1_image = np.swapaxes(T1_image, 0, 1) T1_image = np.swapaxes(T1_image, 1, 2) T2_image = np.swapaxes(T2_image, 0, 1) T2_image = np.swapaxes(T2_image, 1, 2) T_1c_image = np.swapaxes(T_1c_image, 0, 1) T_1c_image = np.swapaxes(T_1c_image, 1, 2) elif slice_num == 3: Flair_image = np.swapaxes(Flair_image, 0, 1) Flair_image = np.swapaxes(Flair_image, 0, 2) T1_image = np.swapaxes(T1_image, 0, 1) T1_image = np.swapaxes(T1_image, 0, 2) T2_image = np.swapaxes(T2_image, 0, 1) T2_image = np.swapaxes(T2_image, 0, 2) T_1c_image = np.swapaxes(T_1c_image, 0, 1) T_1c_image = np.swapaxes(T_1c_image, 0, 2) xdim, ydim, zdim = Flair_image.shape prediction_image = [] print print 'shape:', Flair_image.shape for i in range(zdim): Flair_slice = np.transpose(Flair_image[:, :, i]) T1_slice = np.transpose(T1_image[:, :, i]) T2_slice = np.transpose(T2_image[:, :, i]) T_1c_slice = np.transpose(T_1c_image[:, :, i]) if recon_flag is True: Recon_slice = np.transpose(Recon_image[:, :, i]) Flair_patch = image.extract_patches_2d(Flair_slice, (patch_size, patch_size)) F_P = Flair_patch.reshape(len(Flair_patch), patch_size * patch_size) T1_patch = image.extract_patches_2d(T1_slice, (patch_size, patch_size)) T1_P = T1_patch.reshape(len(Flair_patch), patch_size * patch_size) T2_patch = image.extract_patches_2d(T2_slice, (patch_size, patch_size)) T2_P = T2_patch.reshape(len(Flair_patch), patch_size * patch_size) T_1c_patch = image.extract_patches_2d(T_1c_slice, (patch_size, patch_size)) T1c_P = T_1c_patch.reshape(len(Flair_patch), patch_size * patch_size) if recon_flag is True: Recon_patch = image.extract_patches_2d( Recon_slice, (patch_size, patch_size)) R_P = Recon_patch.reshape(len(Flair_patch), patch_size * patch_size) if recon_flag == True: temp_patch = np.concatenate([F_P, T1_P, T2_P, T1c_P, R_P], axis=1) else: temp_patch = np.concatenate([F_P, T1_P, T2_P, T1c_P], axis=1) prediction_slice = predictOutput(temp_patch, activate2, W_list, b_list) prediction_image.append(prediction_slice) np.save('/media/bmi/varkey/new_n4/prediction_image.npy', prediction_image) prediction_image = np.array(prediction_image) print 'Prediction shape : ', prediction_image.shape #prediction_image = np.transpose(prediction_image) print 'Slice num : ', slice_num if slice_num == 1: print 'Look here : ', prediction_image.shape prediction_image = prediction_image.reshape( [xdim - patch_size + 1, ydim - patch_size + 1, zdim]) print 'Look after : ', prediction_image.shape output_image = np.zeros([xdim, ydim, zdim]) output_image[1 + ((patch_size - 1) / 2):xdim - ((patch_size - 1) / 2) + 1, 1 + ((patch_size - 1) / 2):ydim - ((patch_size - 1) / 2) + 1, :] = prediction_image print 'output shape', output_image.shape elif slice_num == 2: prediction_image = prediction_image.reshape( [zdim, ydim - patch_size + 1, xdim - patch_size + 1]) output_image = np.zeros([xdim, ydim, zdim]) output_image[:, 1 + ((patch_size - 1) / 2):ydim - ((patch_size - 1) / 2) + 1, 1 + ((patch_size - 1) / 2):xdim - ((patch_size - 1) / 2) + 1] = prediction_image output_image = np.swapaxes(output_image, 2, 1) output_image = np.swapaxes(output_image, 1, 0) elif slice_num == 3: prediction_image = prediction_image.reshape( [zdim, ydim - patch_size + 1, xdim - patch_size + 1]) output_image = np.zeros([zdim, ydim, xdim]) output_image[:, 1 + ((patch_size - 1) / 2):ydim - ((patch_size - 1) / 2) + 1, 1 + ((patch_size - 1) / 2):xdim - ((patch_size - 1) / 2) + 1] = prediction_image # print # np.save(,output_image)#TODO: save it in meaningful name in corresponding folder # break print 'Output shape : ', output_image.shape a = output_image for j in xrange(a.shape[2]): a[:, :, j] = np.transpose(a[:, :, j]) print 'a shape here: ', a.shape output_image = a print 'writing mha...' # output_image = itk_py_converter.GetImageFromArray(a.tolist()) # writer.SetFileName(Folder[image_iterator]+Subdir_array[image_iterator]+'_'+prefix+'_.mha') # writer.SetInput(output_image) # writer.Update() affine = [[-1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] img = nib.Nifti1Image(output_image, affine) img.set_data_dtype(np.int32) nib.save( img, Folder[image_iterator] + Subdir_array[image_iterator] + prefix + '.nii') print '########success########'
layer2_input = layer1.output.flatten(2) layer2 = HiddenLayer(rng, input = layer2_input, n_in= 50*64, n_out = 500, activation = T.tanh ) layer3 = LogisticRegression(input = layer2.output,n_in = 500,n_out = 3) cost = layer3.negative_log_likelihood(y) test_model = theano.function([x,y],layer3.errors(y)) valid_model = theano.function([x,y],layer3.errors(y)) params = layer3.params + layer2.params + layer1.params + layer0.params grads = T.grad(cost,params) updates = [(param_i,param_i-learning_rate*grad_i) for param_i,grad_i in zip(params,grads)] train_model = theano.function([x,y],cost,updates = updates) flair = mha.new() t1 = mha.new() t2 = mha.new() t1c = mha.new() truth = mha.new() flair.read_mha('patient1/BRATS_HG0001_FLAIR.mha') t1.read_mha('patient1/BRATS_HG0001_T1.mha') t2.read_mha('patient1/BRATS_HG0001_T2.mha') t1c.read_mha('patient1/BRATS_HG0001_T1C.mha') truth.read_mha('patient1/BRATS_HG0001_truth.mha') """ 176,160,216 16,16 20,16 24,16 .... 160,16
def classify_test_data(activate2, W_list, b_list, path, patch_size, prefix, recon_flag, writer, itk_py_converter): Flair = [] T1 = [] T2 = [] T_1c = [] Recon = [] Folder = [] Truth=[] Subdir_array = [] for subdir, dirs, files in os.walk(path): # if len(Flair) is 1: # break for file1 in files: #print file1 if file1[-3:]=='mha' and ( 'Flair' in file1): Flair.append(file1) Folder.append(subdir+'/') Subdir_array.append(subdir[-5:]) elif file1[-3:]=='mha' and ('t1_z' in file1 or 'T1' in file1): T1.append(file1) elif file1[-3:]=='mha' and ('t2' in file1 or 'T2' in file1): T2.append(file1) elif file1[-3:]=='mha' and ('t1c_z' in file1 or 'T_1c' in file1): T_1c.append(file1) elif file1[-3:]=='mha' and 'OT' in file1: Truth.append(file1) elif file1[-3:]=='mha' and 'Recon' in file1: Recon.append(file1) number_of_images = len(Flair) for image_iterator in range(number_of_images): print 'Iteration : ',image_iterator+1 print 'Folder : ', Folder[image_iterator] print 'T2: ', T2[image_iterator] print '... predicting' Flair_image = mha.new(Folder[image_iterator]+Flair[image_iterator]) T1_image = mha.new(Folder[image_iterator]+T1[image_iterator]) T2_image = mha.new(Folder[image_iterator]+T2[image_iterator]) T_1c_image = mha.new(Folder[image_iterator]+T_1c[image_iterator]) if recon_flag is True: Recon_image = mha.new(Folder[image_iterator]+Recon[image_iterator]) Flair_image = Flair_image.data T1_image = T1_image.data T2_image = T2_image.data T_1c_image = T_1c_image.data if recon_flag is True: Recon_image = Recon_image.data xdim, ydim, zdim = Flair_image.shape prediction_image = [] for i in range(zdim): Flair_slice = np.transpose(Flair_image[:,:,i]) T1_slice = np.transpose(T1_image[:,:,i]) T2_slice = np.transpose(T2_image[:,:,i]) T_1c_slice = np.transpose(T_1c_image[:,:,i]) if recon_flag is True: Recon_slice = np.transpose(Recon_image[:,:,i]) Flair_patch = image.extract_patches_2d(Flair_slice, (patch_size, patch_size)) F_P=Flair_patch.reshape(len(Flair_patch),patch_size*patch_size) T1_patch = image.extract_patches_2d(T1_slice, (patch_size, patch_size)) T1_P=T1_patch.reshape(len(Flair_patch),patch_size*patch_size) T2_patch = image.extract_patches_2d(T2_slice, (patch_size, patch_size)) T2_P=T2_patch.reshape(len(Flair_patch),patch_size*patch_size) T_1c_patch = image.extract_patches_2d(T_1c_slice, (patch_size, patch_size)) T1c_P=T_1c_patch.reshape(len(Flair_patch),patch_size*patch_size) if recon_flag is True: Recon_patch = image.extract_patches_2d(Recon_slice, (patch_size, patch_size)) R_P=Recon_patch.reshape(len(Flair_patch),patch_size*patch_size) if recon_flag == True: temp_patch = np.concatenate([F_P,T1_P,T2_P,T1c_P,R_P],axis=1) else: temp_patch = np.concatenate([F_P,T1_P,T2_P,T1c_P],axis=1) prediction_slice = predictOutput(temp_patch, activate2, W_list, b_list) prediction_image.append(prediction_slice) prediction_image = np.array(prediction_image) prediction_image = np.transpose(prediction_image) prediction_image = prediction_image.reshape([xdim-patch_size+1, ydim-patch_size+1, zdim]) output_image = np.zeros([xdim,ydim,zdim]) output_image[1+((patch_size-1)/2):xdim-((patch_size-1)/2)+1,1+((patch_size-1)/2):ydim-((patch_size-1)/2)+1,:] = prediction_image # np.save(,output_image)#TODO: save it in meaningful name in corresponding folder # break a=np.transpose(output_image) for j in xrange(a.shape[0]): a[j,:,:] = np.transpose(a[j,:,:]) #a=a.reshape(155,240,240) print 'writing mha...' output_image = itk_py_converter.GetImageFromArray(a.tolist()) writer.SetFileName(Folder[image_iterator]+Subdir_array[image_iterator]+'_'+prefix+'_.mha') writer.SetInput(output_image) writer.Update() print '########success########'