def load_fcn_data(window_size=16): from utils import get_patches,onehot_images img = plt.imread("data/image_test10.jpg") label_img = plt.imread("data/10gt.jpg") label_img = onehot_images([label_img]) data,labels = get_patches(img,label_img[0],inner_patch_size=window_size,outer_patch_size=window_size) return data,labels
def train_net(): """ Loads train and validation data, gets patches and fits a model. Returns: - model: trained model """ x_val, y_val = np.load('data/x_tmp_%d.npy' % N_Cls), np.load('data/y_tmp_%d.npy' % N_Cls) img = np.load('data/x_trn_%d.npy' % N_Cls) msk = np.load('data/y_trn_%d.npy' % N_Cls) x_trn, y_trn = get_patches(img, msk, amt=train_patches) model = get_unet() if len(sys.argv) > 1: model.load_weights(sys.argv[1]) print "[train_net] Training started with: batch size:", batch_size, "optimizer lr:", learning_rate model_checkpoint = ModelCheckpoint('weights/unet_tmp.hdf5', monitor='loss', save_best_only=True) for i in range(1): model.fit(x_trn, y_trn, batch_size=batch_size, nb_epoch=num_epoch, verbose=1, shuffle=True, callbacks=[model_checkpoint], validation_data=(x_val, y_val)) del x_trn del y_trn score, trs = calc_jacc(model) model.save_weights('weights/unet_10_%d_%d_jk%.4f' % (batch_size, num_epoch, score)) # x_trn, y_trn = get_patches(img, msk) return model
def make_val(): """ Makes a validation dataset using patches from main image """ print "let's pick some samples for validation" img = np.load('data/x_trn_%d.npy' % N_Cls) msk = np.load('data/y_trn_%d.npy' % N_Cls) x, y = get_patches(img, msk, amt=validation_patches) np.save('data/x_tmp_%d' % N_Cls, x) np.save('data/y_tmp_%d' % N_Cls, y)
def load_patched_data(inner_window_size=16,outer_window_size=64): print "patching..." from utils import get_patches data_windows_outer = [] label_windows_inner = [] images, labels = load_data_images(inner_window_size=inner_window_size) print "orig[0].shape:",images[0].shape, labels[0].shape for image,label in zip(images,labels): # outer, inner = get_windows(image,label,inner_window_size=inner_window_size,outer_window_size=outer_window_size) inner, outer = get_patches(image,label,inner_window_size,outer_window_size) # for outer_window,inner_window in zip(outer,inner): data_windows_outer.extend(outer) label_windows_inner.extend(inner) print "patching completed" return np.array(data_windows_outer),np.array(label_windows_inner)
def make_concurrent_tfserving_prediction(image, model, offset): patch_slices = get_patches(np.array(image.squeeze().shape), offset) result_futures = [ (j, predict_tfserving_api(serialize_data_to_tfrecord(image[..., slices[0], slices[1]]), shape=[1], model_name=model, model_input='serialized_example')) for j, slices in enumerate(patch_slices) ] predictions = collect_async_predictions(result_futures) shape = np.array(image.squeeze().shape) return assemble_prediction(predictions, shape, offset)
def generate_patches(): data_dir = config['Lung']['DataDirectory'] scans = pl.query(pl.Scan).filter() train, valid, test = scan_index_split(scans.count()) if os.path.isfile(os.path.join(data_dir, 'infos.pl')): infos = pickle.load(open(os.path.join(data_dir, 'infos.pl'), 'rb')) else: infos = dict() infos['start_scan_index'] = 0 infos['train_size'] = 0 infos['valid_size'] = 0 infos['test_size'] = 0 for index_scan, scan in enumerate( tqdm(scans, desc='Patch Gen', dynamic_ncols=True, total=scans.count())): if index_scan < infos['start_scan_index']: continue if index_scan in test: which_set = 'test' elif index_scan in valid: which_set = 'valid' else: which_set = 'train' try: X, y = get_patches(scan) except KeyboardInterrupt: # ctrl-c break except: infos[index_scan] = {'start': -1, 'size': -1} else: infos[index_scan] = { 'start': infos[which_set + '_size'], 'size': X.shape[0] } # save patch for xi, yi in zip(X, y): save_patch(xi, yi, infos[which_set + '_size'], which_set) infos[which_set + '_size'] += 1 infos['start_scan_index'] = index_scan + 1 pickle.dump(infos, open(os.path.join(data_dir, 'infos.pl'), 'wb'))
def __init__(self, set_name, index, **kwargs): def preprocess_image(image): """ Preprocess image and its annotations. """ MEAN, STD = 174., 825. # image = (image - image.mean()) / image.std() image = (image - MEAN) / STD return image self.set_name = set_name self.scan_index = scan_index_split(1018)[{ 'train': 0, 'valid': 1, 'test': 2 }[set_name]][index] self.scan = pl.query(pl.Scan).filter()[self.scan_index] self.X, self.y = get_patches(self.scan, negative_ratio=2.) super(LungScanGenerator, self).__init__(**dict( kwargs, group_method='none', preprocess_image=preprocess_image))
def fit(self, img): patches = utils.get_patches(img, self.patch_size) patches = patches.reshape(patches.shape[0], -1) main = decomposition.PCA(n_components=self.n_components) main.fit(patches) eigv = main.components_ mean = main.mean_ bias = -eigv.dot(mean) weight = eigv.reshape(self.n_components, img.shape[0], self.patch_size, self.patch_size) self.main.weight = nn.Parameter( torch.from_numpy(weight.astype(np.float32))) self.main.bias = nn.Parameter(torch.from_numpy(bias.astype( np.float32))) for p in self.parameters(): p.requires_grad_(False) print '[PCA] explained variance: {}'.format( sum(main.explained_variance_ratio_))
def main(): WEIGHTS_PATH = 'weights' logger.info("Reading images") for file in os.listdir(PATH_TO_IMAGES): if file.endswith(".tiff"): ##### /read an image in the dataset ##### img_path = os.path.join(PATH_TO_IMAGES, file) img = PIL.Image.open(img_path) img = img.convert('RGB') img = np.asarray(img) #[H, W, C] img = utils.normalize(img) ##### /read an image in the dataset ##### ##### read a label in the dataset ##### f_name, f_ext = os.path.splitext(file) label_path = os.path.join(PATH_TO_LABELS, f_name + ".tif") assert os.path.isfile(label_path) label = PIL.Image.open(label_path) #label = label.convert('RGB') label = np.asarray(label) #[H, W, num_classes] ##### /read a label in the dataset ##### train_xsz = int(3/4 * img.shape[0]) # use 75% of image as train and 25% for validation X_DICT_TRAIN[file] = img[:train_xsz, :, :] #print (X_DICT_TRAIN[file].shape) Y_DICT_TRAIN[file] = label[:train_xsz, :, :] #print (Y_DICT_TRAIN[file].shape) X_DICT_VALIDATION[file] = img[train_xsz:, :, :] Y_DICT_VALIDATION[file] = label[train_xsz:, :, :] logger.info("Training set: {} images".format(len(X_DICT_TRAIN))) logger.info("Training set: {} labels".format(len(Y_DICT_TRAIN))) logger.info("Validation set: {} images".format(len(X_DICT_VALIDATION))) logger.info("Validation set: {} labels".format(len(Y_DICT_VALIDATION))) x_train, y_train = utils.get_patches(X_DICT_TRAIN, Y_DICT_TRAIN, n_patches=TRAIN_SZ, sz=PATCH_SZ) assert len(x_train) == len(y_train) logger.info("Generated {} patches for training".format(len(x_train))) x_val, y_val = utils.get_patches(X_DICT_VALIDATION, Y_DICT_VALIDATION, n_patches=VAL_SZ, sz=PATCH_SZ) assert len(x_val) == len(y_val) logger.info("Generated {} patches for validation".format(len(x_val))) logger.info("########## Training ##########") # define model model = Unet() model = Unet(backbone_name='resnet34', input_shape=(PATCH_SZ, PATCH_SZ, 3), classes=N_CLASSES, encoder_weights=None) model.compile('Adam', loss=bce_jaccard_loss, metrics=[iou_score]) # load weights (if specified) if not os.path.exists(WEIGHTS_PATH): os.makedirs(WEIGHTS_PATH) WEIGHTS_PATH += "/seg_weights.{epoch:02d}-{val_iou_score:.2f}.hdf5" ########## define callbacks ########## model_checkpoint = keras.callbacks.ModelCheckpoint( WEIGHTS_PATH, monitor='val_loss', verbose=1, save_best_only=True, period=5 ) csv_logger = keras.callbacks.CSVLogger( 'log_unet.csv', append=True, separator=';' ) tensorboard = keras.callbacks.TensorBoard( log_dir='./tensorboard_unet/', write_graph=True, write_images=True ) ########## /define callbacks ########## # fit model model.fit( x=x_train, y=y_train, batch_size=BATCH_SIZE, epochs=N_EPOCHS, verbose=1, #show an animated progress bar shuffle=True, callbacks=[ model_checkpoint, csv_logger, tensorboard ], validation_data=(x_val, y_val), )
from torchvision import transforms from utils import get_patches, recon_image from model import Modified3DUNet import os from utils import normalize image_file = '/media/bubbles/fecf5b15-5a64-477b-8192-f8508a986ffe/ai/ryan/miccai/kits19/brats/data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/tel_1/t1.nii.gz' outpath = '../data/verse/test_images/images/' image_name = image_file.split('/')[-1].split('.')[0] if len(os.listdir(outpath)) > 1: print('Patches found at ', outpath) pass else: print('Extracting patches at ', outpath) get_patches(image_file, outpath, 'train') recon_image('../data/verse/test_images/images/', image_file, '../data/verse/test_images/image_recon') print('Done Extracting pacthes and reconstructing images') # get all the image and mask path and number of images test_image_paths = glob.glob('../data/verse/test_images/images/*.npy') test_image_paths = sorted( test_image_paths, key=lambda files: files.split('/')[-1].split('.')[0][-3:]) class CustomDataset(Dataset): def __init__(self, image_paths): self.image_paths = image_paths
plt.draw() if visualize: plt.ioff() def whiten(X, fudge=1E-18): Xcov = np.dot(X.T, X) d, V = np.linalg.eigh(Xcov) D = np.diag(1. / np.sqrt(d + fudge)) W = np.dot(np.dot(V, D), V.T) X_white = np.dot(X, W) return X_white if __name__ == '__main__': patches = get_patches(8, 1000) patches = patches.reshape(patches.shape[0], -1) np.random.shuffle(patches) patches = whiten(patches) patches = preprocessing.scale(patches) # for i in xrange(100): # plt.subplot(10, 10, i) # plt.xticks(()) # plt.yticks(()) # plt.imshow(patches[i].reshape(8, 8), cmap='gray', interpolation='none') # plt.show() coder = SparseCoder( patches, 100, learning_rate=0.1,
def main(): args = parse_args() if args.input.lower().endswith(".npy"): image = np.load(args.input) elif args.input.lower().endswith((".png", ".jpg", ".jpeg", ".tif")): image = imread(args.input) else: raise ValueError print("Read image {} with shape {}.".format(args.input, image.shape)) if image.dtype == np.uint8: bit_depth = 8 elif image.dtype == np.uint16: bit_depth = 16 elif np.issubdtype(image.dtype, np.floating): bit_depth = 1 else: raise TypeError psnr = get_psnr(bit_depth) model = keras.models.load_model(args.model, custom_objects={"psnr": psnr}) # Transform into 4D tensor if image.ndim == 2: image = image[np.newaxis, :, :, np.newaxis] elif image.ndim == 3: image = image[np.newaxis, :, :] elif image.ndim != 4: raise Exception("Error with image dimensions: ndim should be 2 or " \ "3, but received {}".format(image.ndim)) patch_size = args.patch print("Cutting images into {}x{} patches.".format(patch_size, patch_size)) image = get_patches(image, patch_size, 0) print("Adding noise with standard deviation {}.".format(args.sigma)) noisy = NoiseGenerator.add_gaussian_noise(image, args.sigma) print("Making prediction on input tensor with shape {}".format( noisy.shape)) pred = model.predict(noisy, batch_size=1) pred_psnr = psnr(image, pred) print("PSNR of prediction: {}".format(K.eval(pred_psnr))) if args.output: if args.output.lower().endswith(".npz"): print("Saving image, noisy image and prediction to {}".format( args.output)) np.savez(args.output, image=image, noisy=noisy, pred=pred) elif args.output.lower().endswith(".npy"): print("Saving prediction to {}".format(args.output)) np.save(args.output, pred) elif args.output.lower().endswith((".png", ".jpg", ".jpeg", ".tif")): print("Saving prediction to {}".format(args.output)) for i, p in enumerate(pred): extension_dot_idx = args.output.rfind(".") output = args.output[:extension_dot_idx] + "_" + str(i + 1) \ + args.output[extension_dot_idx:] imsave(output, np.clip(np.squeeze(p), 0, bit_depth). \ astype(image.dtype)) else: raise ValueError if args.visualize: while True: try: i = input("Patch number (0-{}): ".format(pred.shape[0] - 1)) if i == "exit": exit(0) else: i = int(i) fig, axes = plt.subplots(1, 3) axes[0].imshow(image[i, :, :, 0], cmap="Greys_r") axes[0].set_title("Original") axes[0].set_aspect('equal', adjustable='box') axes[1].imshow(noisy[i, :, :, 0], cmap="Greys_r") axes[1].set_title("Noisy (sigma = {})".format(args.sigma)) axes[1].set_aspect('equal', adjustable='box') axes[2].imshow(pred[i, :, :, 0], cmap="Greys_r") axes[2].set_title("Restored") axes[2].set_aspect('equal', adjustable='box') for ax in axes: ax.set_xticks([]) ax.set_yticks([]) plt.show() except Exception: continue