def load_train_data(count=2000, start=10000): """Loads training and/or evaluation data""" print("Loading data...") biffolder = '..\\..\\data\\bifs' nobiffolder = '..\\..\\data\\nobifs' # 4 dimensional data data = np.zeros([2 * count, 17, 17, 17], dtype=np.float32) # print(np.shape(data)) # First load the bif data. for i in utility.my_range(start, start + count, 1): currentfile = biffolder + '\\cropped' + str(i) + ".nii.gz" data[i - start, :, :, :] = np.array(get_itk_array(currentfile)) print("Loaded bif data.") # Now load the no bif data. for i in utility.my_range(start, start + count, 1): currentfile = nobiffolder + '\\cropped' + str(i) + ".nii.gz" data[i + count - start, :, :, :] = np.array(get_itk_array(currentfile)) print("Loaded no bif data.") print("All data loaded.") return data
def load_test_data_folder(folder_name, number_files=0): # Read info file. with open(folder_name + "info.txt") as f: content = f.readlines() content = [x.strip() for x in content] num_files = int(content[4]) print("Total Number of Files: ", num_files) print("Loading testing data...") if number_files != 0: num_files = number_files print("Loading only the first ", num_files, " of data.") data = np.zeros([num_files, 64, 64, 64], dtype='float32') for i in utility.my_range(0, num_files, 1): data_file_name = folder_name + "cropped" + str(i) + ".nii.gz" data[i] = np.asarray(get_itk_array(data_file_name), dtype='float32') print("All data loaded.") return data
def main(unused_args): test_data = load_test_data_folder('../input/norm2/', ) test_data = np.reshape(test_data, [-1, 64, 64]) print("Input Data...") print("Shape: ", np.shape(test_data)) print("Max: ", np.max(test_data)) print("Min: ", np.min(test_data)) print("Mean: ", np.mean(test_data)) # Create the Estimator mnist_classifier = tf.estimator.Estimator( model_fn=cnn_model_fn, model_dir="/home/joseph/Projects/vascularnetworks/models/giles2d_deep") predict_input_fn = tf.estimator.inputs.numpy_input_fn( x={"x": test_data}, batch_size=1, num_epochs=1, shuffle=False) # Predict data predictions = mnist_classifier.predict(input_fn=predict_input_fn) predictions = np.array(list(predictions)) print(np.shape(predictions)) print(predictions[0]['classes']) print(np.shape(predictions[0]['classes'])) result = np.zeros([64, 64, 64, 64], dtype='uint8') for i in utility.my_range(0, 64, 1): for j in utility.my_range(0, 64, 1): result[i, j, :, :] = predictions[i * 64 + j]['classes'] # print(result) # print(np.shape(result)) for i in utility.my_range(0, 64, 1): write_itk_imageArray(result[i], '../input/myseg2/cropped' + str(i) + '.nii.gz')
def load_test_data(filename, blocksize=8): """Loads testing data""" print("Started loading.") inputimage = np.array(get_itk_array(filename)) inputsize = np.shape(inputimage) xsize = inputsize[0] ysize = inputsize[1] zsize = inputsize[2] count = 128 * 128 * 128 data = np.zeros([count, 17, 17, 17], dtype=np.float32) index = 0 for i in utility.my_range(blocksize, blocksize + 128, 1): # print("Step", i) for j in utility.my_range(blocksize, blocksize + 128, 1): for k in utility.my_range(blocksize, blocksize + 128, 1): data[index, :, :, :] = inputimage[(i - blocksize):(i + blocksize + 1), (j - blocksize):(j + blocksize + 1), (k - blocksize):(k + blocksize + 1)] index = index + 1 print("Loaded data.") return data
def main(unused_args): # Load training and eval data # mnist = tf.contrib.learn.datasets.load_dataset("mnist") # train_data = mnist.train.images # Returns np.array (55000, 784) # train_labels = np.asarray(mnist.train.labels, dtype=np.int32) # (55000, 1) test_data = load_test_data('..\\input\\test.mhd') # print("Input Data...") # print("Shape: ", np.shape(test_data)) # print("Max: ", np.max(test_data)) # print("Min: ", np.min(test_data)) # print("Mean: ", np.mean(test_data)) # Create the Estimator mnist_classifier = tf.estimator.Estimator( model_fn=cnn_model_fn, model_dir="/tmp/mnist_convnet_model") predict_input_fn = tf.estimator.inputs.numpy_input_fn( x={"x": test_data}, batch_size=100, num_epochs=1, shuffle=False) # Predict data predictions = mnist_classifier.predict(input_fn=predict_input_fn) predictions = np.array(list(predictions)) result = np.zeros(np.shape(predictions)) for index in utility.my_range(0, np.shape(predictions)[0], 1): result[index] = predictions[index]['classes'] # print(result) write_itk_imageArray(np.reshape(result, [128, 128, 128]), '..\\output\\detected.nii.gz')
ptindex = 0 Points = vtk.vtkPoints() line = vtk.vtkCellArray() radius = vtk.vtkIntArray() radius.SetNumberOfComponents(1) radius.SetName("radius") # line.Allocate(8) print("started") data = dict() edges = dict() for i in utility.my_range(1, dims[0] - 1, 1): print("Step: ", i) for j in utility.my_range(1, dims[1] - 1, 1): for k in utility.my_range(1, dims[2] - 1, 1): # If pixel is 1 if inputimg[i, j, k] == 1: # Get index at image ind = i * dims[0] * dims[1] + j * dims[1] + k # Check if already added to list. If not, add. if ind not in data: Points.InsertNextPoint(i, j, k)
content = [x.strip() for x in content] stepsize = content[0] xSize = content[1] ySize = content[2] zSize = content[3] numfiles = content[4] print("Step Size is: ", stepsize) print("Total Image Size is: ", xSize, ySize, zSize) print("Total Number of Files: ", numfiles) # Segment every file. index = 0 for i in utility.my_range(0, int(numfiles), 1): print("Segmenting file: ", "cropped" + str(index) + ".nii.gz") datafilename = workfolder + "/cropped" + str(index) + ".nii.gz" writefilename = segfolder + "/cropped" + str(index) + ".nii.gz" # Apply thresholding to it. os.system("python " + executable + " " + datafilename + " " + writefilename + " " + thresholdvalue) # Update indices index = index + 1 print("Done.")
xSize = inputSize[0] ySize = inputSize[1] zSize = inputSize[2] file = open(workfolder + "info.txt", "w") file.write(str(blocksize) + "\n") file.write(str(xSize) + "\n") file.write(str(ySize) + "\n") file.write(str(zSize) + "\n") print(blocksize, xSize, ySize, zSize) # Index for bifurcations index = 0 for i in utility.my_range(blocksize + stepsize, xSize - blocksize, 1): print("Step", i) for j in utility.my_range(blocksize + stepsize, ySize - blocksize, 1): for k in utility.my_range(blocksize + stepsize, zSize - blocksize, 1): # print("Step at: (", index, ")", i, j, k) # If bifurcation exists. if bifimg[i, j, k] != 0: # Crop image and write it. # Checks surrounding. If no bifurc. Add it. surrounding = bifimg[(i - 2 * stepsize - 1):i, (j - 2 * stepsize):j,
xSize = int(content[1]) ySize = int(content[2]) zSize = int(content[3]) numfiles = int(content[4]) startX = 0 startY = 0 startZ = 0 # Create a file with that size. joinedfile = np.zeros([xSize, ySize, zSize], dtype="uint8") # Join the small files into one big file. index = 0 for i in utility.my_range(0, xSize, stepsize): for j in utility.my_range(0, ySize, stepsize): for k in utility.my_range(0, zSize, stepsize): print("Step at: (", index, ")", startX, startY, startZ) endX = startX + stepsize endY = startY + stepsize endZ = startZ + stepsize currentfilename = splitfolder + "/cropped" + str(index) + ".nii.gz" currentfile = np.array(get_itk_array(currentfilename), dtype='uint8') joinedfile[startX:endX, startY:endY, startZ:endZ] = currentfile # Update indices