def test_dtype_upcast(self): # Checks dtype upcast for Corr3dMM methods. if not theano.config.cxx: pytest.skip("Need cxx for this test") def rand(shape, dtype="float64"): r = np.asarray(np.random.rand(*shape), dtype=dtype) return r * 2 - 1 ops = [ corr3d.Corr3dMM, corr3d.Corr3dMM_gradWeights, corr3d.Corr3dMM_gradInputs ] a_shapes = [[4, 5, 6, 3, 3], [1, 5, 6, 3, 3], [1, 5, 6, 3, 3]] b_shapes = [[7, 5, 3, 2, 2], [1, 5, 3, 1, 1], [7, 1, 3, 1, 1]] dtypes = ["float32", "float64"] for op, a_shape, b_shape in zip(ops, a_shapes, b_shapes): for a_dtype in dtypes: for b_dtype in dtypes: c_dtype = theano.scalar.upcast(a_dtype, b_dtype) a_tens = T.tensor5(dtype=a_dtype) b_tens = T.tensor5(dtype=b_dtype) a_tens_val = rand(a_shape, dtype=a_dtype) b_tens_val = rand(b_shape, dtype=b_dtype) c_tens = op()(a_tens, b_tens) f = theano.function([a_tens, b_tens], c_tens, mode=self.mode) assert f(a_tens_val, b_tens_val).dtype == c_dtype
def test_dtype_upcast(self): """ Checks dtype upcast for Corr3dMM methods. """ def rand(shape, dtype="float64"): r = numpy.asarray(numpy.random.rand(*shape), dtype=dtype) return r * 2 - 1 ops = [corr3d.Corr3dMM, corr3d.Corr3dMM_gradWeights, corr3d.Corr3dMM_gradInputs] a_shapes = [[4, 5, 6, 3, 3], [1, 5, 6, 3, 3], [1, 5, 6, 3, 3]] b_shapes = [[7, 5, 3, 2, 2], [1, 5, 3, 1, 1], [7, 1, 3, 1, 1]] dtypes = ["float32", "float64"] for op, a_shape, b_shape in zip(ops, a_shapes, b_shapes): for a_dtype in dtypes: for b_dtype in dtypes: c_dtype = theano.scalar.upcast(a_dtype, b_dtype) a_tens = T.tensor5(dtype=a_dtype) b_tens = T.tensor5(dtype=b_dtype) a_tens_val = rand(a_shape, dtype=a_dtype) b_tens_val = rand(b_shape, dtype=b_dtype) c_tens = op()(a_tens, b_tens) f = theano.function([a_tens, b_tens], c_tens, mode=self.mode) assert_equals(f(a_tens_val, b_tens_val).dtype, c_dtype)
def test_dtype_upcast(self): """ Checks dtype upcast for Corr3dMM methods. """ def rand(shape, dtype='float64'): r = numpy.asarray(numpy.random.rand(*shape), dtype=dtype) return r * 2 - 1 ops = [corr3d.Corr3dMM, corr3d.Corr3dMM_gradWeights, corr3d.Corr3dMM_gradInputs] a_shapes = [[4, 5, 6, 3, 3], [1, 5, 6, 3, 3], [1, 5, 6, 3, 3]] b_shapes = [[7, 5, 3, 2, 2], [1, 5, 3, 1, 1], [7, 1, 3, 1, 1]] dtypes = ['float32', 'float64'] for op, a_shape, b_shape in zip(ops, a_shapes, b_shapes): for a_dtype in dtypes: for b_dtype in dtypes: c_dtype = theano.scalar.upcast(a_dtype, b_dtype) a_tens = T.tensor5(dtype=a_dtype) b_tens = T.tensor5(dtype=b_dtype) a_tens_val = rand(a_shape, dtype=a_dtype) b_tens_val = rand(b_shape, dtype=b_dtype) c_tens = op()(a_tens, b_tens) f = theano.function([a_tens, b_tens], c_tens, mode=self.mode) assert_equals(f(a_tens_val, b_tens_val).dtype, c_dtype)
def setup_method(self): self.input = tt.tensor5("input", dtype=self.dtype) self.input.name = "default_V" self.filters = tt.tensor5("filters", dtype=self.dtype) self.filters.name = "default_filters" # This tests can run even when theano.config.blas.ldflags is empty. super().setup_method()
def test_dtype_upcast(self): # Checks dtype upcast for Corr3dMM methods. if not theano.config.cxx: raise SkipTest("Need cxx for this test") def rand(shape, dtype='float64'): r = np.asarray(np.random.rand(*shape), dtype=dtype) return r * 2 - 1 ops = [corr3d.Corr3dMM, corr3d.Corr3dMM_gradWeights, corr3d.Corr3dMM_gradInputs] a_shapes = [[4, 5, 6, 3, 3], [1, 5, 6, 3, 3], [1, 5, 6, 3, 3]] b_shapes = [[7, 5, 3, 2, 2], [1, 5, 3, 1, 1], [7, 1, 3, 1, 1]] dtypes = ['float32', 'float64'] for op, a_shape, b_shape in zip(ops, a_shapes, b_shapes): for a_dtype in dtypes: for b_dtype in dtypes: c_dtype = theano.scalar.upcast(a_dtype, b_dtype) a_tens = T.tensor5(dtype=a_dtype) b_tens = T.tensor5(dtype=b_dtype) a_tens_val = rand(a_shape, dtype=a_dtype) b_tens_val = rand(b_shape, dtype=b_dtype) c_tens = op()(a_tens, b_tens) f = theano.function([a_tens, b_tens], c_tens, mode=self.mode) assert_equals(f(a_tens_val, b_tens_val).dtype, c_dtype)
def setUp(self): super(TestCorr3D, self).setUp() self.input = T.tensor5("input", dtype=self.dtype) self.input.name = "default_V" self.filters = T.tensor5("filters", dtype=self.dtype) self.filters.name = "default_filters" if not conv.imported_scipy_signal and theano.config.cxx == "": raise SkipTest("Corr3dMM tests need SciPy or a c++ compiler")
def setUp(self): super(TestCorr3D, self).setUp() self.input = T.tensor5('input', dtype=self.dtype) self.input.name = 'default_V' self.filters = T.tensor5('filters', dtype=self.dtype) self.filters.name = 'default_filters' if not conv.imported_scipy_signal and theano.config.cxx == "": raise SkipTest("Corr3dMM tests need SciPy or a c++ compiler")
def setup_method(self): self.input = T.tensor5("input", dtype=self.dtype) self.input.name = "default_V" self.filters = T.tensor5("filters", dtype=self.dtype) self.filters.name = "default_filters" if not conv.imported_scipy_signal and theano.config.cxx == "": pytest.skip("Corr3dMM tests need SciPy or a c++ compiler") # This tests can run even when theano.config.blas.ldflags is empty. super().setup_method()
def setUp(self): super(TestCorr3D, self).setUp() self.input = T.tensor5('input', dtype=self.dtype) self.input.name = 'default_V' self.filters = T.tensor5('filters', dtype=self.dtype) self.filters.name = 'default_filters' if not conv.imported_scipy_signal and theano.config.cxx == "": raise SkipTest("Corr3dMM tests need SciPy or a c++ compiler") if not theano.config.blas.ldflags: raise SkipTest("Corr3dMM tests need a BLAS")
def build_model_3d(self): self.input = T.tensor5('input') self.params = () c_input = self.input feat_maps = 1 for ker, actfunc in self.conv_layers: conv = Conv_2d_to_3d(input=c_input, ker_size=ker[-3:], bank_size=1, input_maps=ker[1], output_maps=ker[0], activation=actfunc) c_input = conv.output self.params += conv.params feat_maps = ker[0] c_h_input = T.transpose(c_input, axes=(0, 2, 3, 4, 1)) c_n_in = feat_maps self.classifier = LogisticMultiLabelClassifier( T.reshape(c_h_input, (T.prod(c_h_input.shape[:4]), c_h_input.shape[4])), c_n_in, self.n_out) self.params += self.classifier.params
def __setstate__(self, params): """Pickle load config.""" self.__dict__.update(params[0]) if params[2] is not None and params[3] is not None: input_network = Network.load_model(params[2]) self.input_var = input_network.input_var self.input_network = { 'network': input_network, 'layer': params[3], 'get_params': params[4] } else: tensor_size = len(self.input_dimensions) if tensor_size == 2: self.input_var = T.matrix('input') elif tensor_size == 3: self.input_var = T.tensor3('input') elif tensor_size == 4: self.input_var = T.tensor4('input') elif tensor_size == 5: self.input_var = T.tensor5('input') self.y = T.matrix('truth') self.__init__(self.__dict__['name'], self.__dict__['input_dimensions'], self.__dict__['input_var'], self.__dict__['y'], self.__dict__['config'], self.__dict__['input_network'], self.__dict__['num_classes'], self.__dict__['activation'], self.__dict__['pred_activation'], self.__dict__['optimizer'], self.__dict__['learning_rate']) lasagne.layers.set_all_param_values(self.layers, params[1], **{self.name: True})
def __init__(self, generator, obs, num_samples, hdim, L, epsilon, box=(60, 100, 60, 100), init_state=None): self.generator = generator self.batch_size = obs.shape[0] print obs.shape, init_state.shape self.img_size = obs.shape[-1] self.obs_val = sharedX( np.reshape(obs, [1, self.batch_size, 3, self.img_size, self.img_size])) self.obs = T.tensor5() self.t = T.scalar() self.n_sam = num_samples self.hdim = hdim self.L = L self.eps = epsilon # mask mask = numpy.ones((1, 3, self.img_size, self.img_size)) mask[:, :, box[0]:box[1], box[2]:box[3]] = 0 self.mask = sharedX(mask) self.build(self.eps, self.L, init_state=init_state) print self.img_size, self.n_sam
def __init__(self, generator, obs, num_samples, sigma, hdim, L, epsilon, prior, init_state=None): self.generator = generator self.batch_size = obs.shape[0] self.obs_val = sharedX(np.reshape(obs, [1, self.batch_size, 3, 32, 32])) #ftensor5 = TensorType('float32', (False,)*5) #self.obs = ftensor5() self.obs = T.tensor5() #pdb.set_trace() self.t = T.scalar() self.sigma = sigma self.n_sam = num_samples self.hdim = hdim self.L = L self.eps = epsilon self.prior = prior if init_state is None: self.build(self.eps, self.L) else: self.build(self.eps, self.L, init_state=init_state)
def run(fold=0): print fold I_AM_FOLD = fold all_data = load_dataset(folder=paths.preprocessed_validation_data_folder) use_patients = all_data experiment_name = "final" results_folder = os.path.join(paths.results_folder, experiment_name, "fold%d" % I_AM_FOLD) write_images = False save_npy = True INPUT_PATCH_SIZE = (None, None, None) BATCH_SIZE = 2 n_repeats = 2 num_classes = 4 x_sym = T.tensor5() net, seg_layer = build_net(x_sym, INPUT_PATCH_SIZE, num_classes, 4, 16, batch_size=BATCH_SIZE, do_instance_norm=True) output_layer = seg_layer results_out_folder = os.path.join(results_folder, "pred_val_set") if not os.path.isdir(results_out_folder): os.mkdir(results_out_folder) with open( os.path.join(results_folder, "%s_Params.pkl" % (experiment_name)), 'r') as f: params = cPickle.load(f) lasagne.layers.set_all_param_values(output_layer, params) print "compiling theano functions" output = softmax_helper( lasagne.layers.get_output(output_layer, x_sym, deterministic=False, batch_norm_update_averages=False, batch_norm_use_averages=False)) pred_fn = theano.function([x_sym], output) _ = pred_fn( np.random.random((BATCH_SIZE, 4, 176, 192, 176)).astype(np.float32)) run_validation_mirroring(pred_fn, results_out_folder, use_patients, write_images=write_images, hasBrainMask=False, BATCH_SIZE=BATCH_SIZE, num_repeats=n_repeats, preprocess_fn=preprocess, save_npy=save_npy, save_proba=False)
def build_network(kwargs): '''Takes a pretrained classification net and adds a few convolutional layers on top of it and defines a detection loss function''' '''Args: num_convpool: number of conv layer-pool layer pairs delta: smoothing constant to loss function (ie sqrt(x + delta)) -> if x is 0 gradient is undefined num_filters num_fc_units num_extra_conv: conv layers to add on to each conv layer before max pooling nonlinearity: which nonlinearity to use throughout n_boxes: how many boxes should be predicted at each grid point, nclass: how many classes are we predicting, grid_size: size of the grid that encodes various locations of image (ie in the YOLO paper they use 7x7 grid) w_init: weight intitialization dropout_p: prob of dropping unit coord_penalty : penalty in YOLO loss function for getting coordinates wrong nonobj_penalty: penalty in YOLO loss for guessing object when there isn't one learning_rate weight_decay momentum load: whether to load weights or not load_path: path for loading weights''' if kwargs["im_dim"] == 3: input_var = T.tensor5('input_var') else: input_var = T.tensor4('input_var') target_var = T.tensor4('target_var') #is of shape (grid_size, grid_size,(n_boxes* 5 + nclass) print "Building model and compiling functions..." #make layers networks = build_layers(input_var,kwargs) #load in any pretrained weights if kwargs['ae_load_path'] != "None": networks['ae'] = load_weights(kwargs['ae_load_path'], networks['ae']) if kwargs['yolo_load_path'] != "None": networks['yolo'] = load_weights(kwargs['yolo_load_path'], networks['yolo']) #compile theano functions fns = make_fns(networks, input_var, target_var, kwargs) return fns, networks
def _to_symbolic_var(x): if x.ndim == 0: return T.scalar() elif x.ndim == 1: return T.vector() elif x.ndim == 2: return T.matrix() elif x.ndim == 3: return T.tensor3() elif x.ndim == 4: return T.tensor4() elif x.ndim == 5: return T.tensor5() else: raise Exception()
def main(): hf = h5py.File("/hdd/Documents/Data/3D-MNIST/full_dataset_vectors.h5", "r") X_train = hf["X_train"][0].reshape(1, 1, 16, 16, 16) X_train = np.rollaxis(X_train, 2, 5) X_train = np.array(X_train, dtype = np.float32) input_var = T.tensor5('inputs') network = build_cnn(input_var, 1) network_output = lasagne.layers.get_output(network) get_rotated = theano.function([input_var], network_output) image_result = get_rotated(X_train) import amitgroup as ag import amitgroup.plot as gr gr.images(np.mean(image_result, axis = 4)[0,0]) return
def __init__(self, input_shape=(None, 1, 33, 33, 33)): self.cubeSize = input_shape[-1] # Theano variables self.input_var = T.tensor5('input_var') # input image self.target_var = T.ivector('target_var') # target self.logger = logging.getLogger(__name__) input_layer = InputLayer(input_shape, self.input_var) self.logger.info('The shape of input layer is {}'.format( get_output_shape(input_layer))) hidden_layer1 = Conv3DLayer(incoming=input_layer, num_filters=16, filter_size=(3, 3, 3), W=HeUniform(gain='relu'), nonlinearity=rectify) self.logger.info('The shape of first hidden layer is {}'.format( get_output_shape(hidden_layer1))) hidden_layer2 = Conv3DLayer(incoming=hidden_layer1, num_filters=32, filter_size=(3, 3, 3), W=HeUniform(gain='relu'), nonlinearity=rectify) self.logger.info('The shape of second hidden layer is {}'.format( get_output_shape(hidden_layer2))) hidden_layer3 = Conv3DLayer(incoming=hidden_layer2, num_filters=2, filter_size=(1, 1, 1), W=HeUniform(gain='relu'), nonlinearity=rectify) self.logger.info('The shape of third hidden layer is {}'.format( get_output_shape(hidden_layer3))) shuffledLayer = DimshuffleLayer(hidden_layer3, (0, 2, 3, 4, 1)) self.logger.info('The shape of shuffled layer is {}'.format( get_output_shape(shuffledLayer))) reshapedLayer = ReshapeLayer(shuffledLayer, ([0], -1)) self.logger.info('The shape of reshaped layer is {}'.format( get_output_shape(reshapedLayer))) self.output_layer = NonlinearityLayer(reshapedLayer, softmax) self.logger.info('The shape of output layer is {}'.format( get_output_shape(self.output_layer)))
def get_net(net_cfg, args): l_out, ladder = net_cfg(args) if args["mode"] == "2d": X = T.tensor4('X') elif args["mode"] == "3d": X = T.tensor5('X') ladder_output = get_output([l_out] + ladder, X) net_out = ladder_output[0] # squared error loss is between the first two terms loss = squared_error(net_out, X).mean() ladder_output = ladder_output[1::] if "ladder" in args: sys.stderr.write("using ladder connections for conv\n") for i in range(0, len(ladder_output), 2): sys.stderr.write("ladder connection between %s and %s\n" % (str(ladder[i].output_shape), str(ladder[i+1].output_shape)) ) assert ladder[i].output_shape == ladder[i+1].output_shape loss += args["ladder"]*squared_error(ladder_output[i], ladder_output[i+1]).mean() net_out_det = get_output(l_out, X, deterministic=True) # this is deterministic + doesn't have the reg params added to the end loss_det = squared_error(net_out_det, X).mean() params = get_all_params(l_out, trainable=True) lr = theano.shared(floatX(args["learning_rate"])) if "optim" not in args: updates = nesterov_momentum(loss, params, learning_rate=lr, momentum=0.9) else: if args["optim"] == "rmsprop": updates = rmsprop(loss, params, learning_rate=lr) elif args["optim"] == "adam": updates = adam(loss, params, learning_rate=lr) train_fn = theano.function([X], [loss, loss_det], updates=updates) loss_fn = theano.function([X], loss) out_fn = theano.function([X], net_out_det) return { "train_fn": train_fn, "loss_fn": loss_fn, "out_fn": out_fn, "lr": lr, "l_out": l_out }
def fSPP(inp, level=3): inshape = inp._keras_shape[2:] Kernel = [[0] * 3 for i in range(level)] Stride = [[0] * 3 for i in range(level)] SPPout = T.tensor5() for iLevel in range(level): Kernel[iLevel] = np.ceil(np.divide(inshape, iLevel + 1, dtype=float)).astype(int) Stride[iLevel] = np.floor(np.divide(inshape, iLevel + 1, dtype=float)).astype(int) if inshape[2] % 3 == 2: Kernel[2][2] = Kernel[2][2] + 1 poolLevel = MaxPooling3D(pool_size=Kernel[iLevel], strides=Stride[iLevel])(inp) if iLevel == 0: SPPout = Flatten()(poolLevel) else: poolFlat = Flatten()(poolLevel) SPPout = concatenate([SPPout, poolFlat], axis=1) return SPPout
def compile_fn(network, net_config, args): """ Create Training function and validation function """ # Hyper-parameters base_lr = float(args.lr[0]) l2 = float(args.l2[0]) # Theano variables input_var = net_config['input'].input_var mask_var = net_config['mask'].input_var kspace_var = net_config['kspace_input'].input_var target_var = T.tensor5('targets') # Objective pred = lasagne.layers.get_output(network) # complex valued signal has 2 channels, which counts as 1. loss_sq = lasagne.objectives.squared_error(target_var, pred).mean() * 2 if l2: l2_penalty = lasagne.regularization.regularize_network_params(network, lasagne.regularization.l2) loss = loss_sq + l2_penalty * l2 update_rule = lasagne.updates.adam params = lasagne.layers.get_all_params(network, trainable=True) updates = update_rule(loss, params, learning_rate=base_lr) print(' Compiling ... ') t_start = time.time() train_fn = theano.function([input_var, mask_var, kspace_var, target_var], [loss], updates=updates, on_unused_input='ignore') val_fn = theano.function([input_var, mask_var, kspace_var, target_var], [loss, pred], on_unused_input='ignore') t_end = time.time() print(' ... Done, took %.4f s' % (t_end - t_start)) return train_fn, val_fn
def __init__(self, modelConfigFile): self.logger = logging.getLogger(__name__) self.modelConfig = {} execfile(modelConfigFile, self.modelConfig) self.numOfFMs = self.modelConfig['numOfFMs'] self.layerCategory = self.modelConfig['layerCategory'] self.numOfOutputClass = self.modelConfig['numOfOutputClass'] self.numOfInputChannels = self.modelConfig['numOfInputChannels'] self.receptiveField = reduce( lambda x, y: x + y, [int(layer[-1]) - 1 for layer in self.layerCategory], 1) self.inputShape = (None, self.numOfInputChannels, self.receptiveField, self.receptiveField, self.receptiveField) self.inputVar = T.tensor5('inputVar', dtype=theano.config.floatX) self.targetVar = T.ivector('targetVar') self.sectorNet = self.buildSectorNet()
import random import math import numpy as np import random from library import read_text_as_list, read_list_into_array, read_lists_into_arrays from network_definitions import fc from training_macros import train_network # Define parameters, prepare variables l1_penalty = 0 l2_penalty = 1e-4 list = read_text_as_list(args.image_text_list) num_modalities = np.size(list[0].split(',')) # Prepare Theano variables for inputs and targets patch_var = T.tensor5('patch') loc_var = T.tensor5('loc') target_var = T.fmatrix('targets') learning_rate = T.scalar(name='learning_rate') iter = T.scalar(name='iter') num_labels = args.num_labels # Get network [network, shaped] = fc(args.input_patch_radius, args.output_patch_radius, patch_var, loc_var, target_var, args.location, num_modalities, num_labels) # Create a loss expression for training prediction = lasagne.layers.get_output(network, deterministic=False) loss = lasagne.objectives.categorical_crossentropy(prediction, target_var)
def run(fold=0): print fold # ================================================================================================================= I_AM_FOLD = fold np.random.seed(65432) lasagne.random.set_rng(np.random.RandomState(98765)) sys.setrecursionlimit(2000) BATCH_SIZE = 2 INPUT_PATCH_SIZE =(128, 128, 128) num_classes=4 EXPERIMENT_NAME = "final" results_dir = os.path.join(paths.results_folder) if not os.path.isdir(results_dir): os.mkdir(results_dir) results_dir = os.path.join(results_dir, EXPERIMENT_NAME) if not os.path.isdir(results_dir): os.mkdir(results_dir) results_dir = os.path.join(results_dir, "fold%d"%I_AM_FOLD) if not os.path.isdir(results_dir): os.mkdir(results_dir) n_epochs = 300 lr_decay = np.float32(0.985) base_lr = np.float32(0.0005) n_batches_per_epoch = 100 n_test_batches = 10 n_feedbacks_per_epoch = 10. num_workers = 6 workers_seeds = [123, 1234, 12345, 123456, 1234567, 12345678] # ================================================================================================================= all_data = load_dataset() keys_sorted = np.sort(all_data.keys()) crossval_folds = KFold(len(all_data.keys()), n_folds=5, shuffle=True, random_state=123456) ctr = 0 for train_idx, test_idx in crossval_folds: print len(train_idx), len(test_idx) if ctr == I_AM_FOLD: train_keys = [keys_sorted[i] for i in train_idx] test_keys = [keys_sorted[i] for i in test_idx] break ctr += 1 train_data = {i:all_data[i] for i in train_keys} test_data = {i:all_data[i] for i in test_keys} data_gen_train = create_data_gen_train(train_data, INPUT_PATCH_SIZE, num_classes, BATCH_SIZE, contrast_range=(0.75, 1.5), gamma_range = (0.8, 1.5), num_workers=num_workers, num_cached_per_worker=2, do_elastic_transform=True, alpha=(0., 1300.), sigma=(10., 13.), do_rotation=True, a_x=(0., 2*np.pi), a_y=(0., 2*np.pi), a_z=(0., 2*np.pi), do_scale=True, scale_range=(0.75, 1.25), seeds=workers_seeds) data_gen_validation = BatchGenerator3D_random_sampling(test_data, BATCH_SIZE, num_batches=None, seed=False, patch_size=INPUT_PATCH_SIZE, convert_labels=True) val_transforms = [] val_transforms.append(GenerateBrainMaskTransform()) val_transforms.append(BrainMaskAwareStretchZeroOneTransform(clip_range=(-5, 5), per_channel=True)) val_transforms.append(SegChannelSelectionTransform([0])) val_transforms.append(ConvertSegToOnehotTransform(range(4), 0, "seg_onehot")) val_transforms.append(DataChannelSelectionTransform([0, 1, 2, 3])) data_gen_validation = MultiThreadedAugmenter(data_gen_validation, Compose(val_transforms), 2, 2) x_sym = T.tensor5() seg_sym = T.matrix() net, seg_layer = build_net(x_sym, INPUT_PATCH_SIZE, num_classes, 4, 16, batch_size=BATCH_SIZE, do_instance_norm=True) output_layer_for_loss = net # add some weight decay l2_loss = lasagne.regularization.regularize_network_params(output_layer_for_loss, lasagne.regularization.l2) * 1e-5 # the distinction between prediction_train and test is important only if we enable dropout (batch norm/inst norm # does not use or save moving averages) prediction_train = lasagne.layers.get_output(output_layer_for_loss, x_sym, deterministic=False, batch_norm_update_averages=False, batch_norm_use_averages=False) loss_vec = - soft_dice_per_img_in_batch(prediction_train, seg_sym, BATCH_SIZE)[:, 1:] loss = loss_vec.mean() loss += l2_loss acc_train = T.mean(T.eq(T.argmax(prediction_train, axis=1), seg_sym.argmax(-1)), dtype=theano.config.floatX) prediction_test = lasagne.layers.get_output(output_layer_for_loss, x_sym, deterministic=True, batch_norm_update_averages=False, batch_norm_use_averages=False) loss_val = - soft_dice_per_img_in_batch(prediction_test, seg_sym, BATCH_SIZE)[:, 1:] loss_val = loss_val.mean() loss_val += l2_loss acc = T.mean(T.eq(T.argmax(prediction_test, axis=1), seg_sym.argmax(-1)), dtype=theano.config.floatX) # learning rate has to be a shared variable because we decrease it with every epoch params = lasagne.layers.get_all_params(output_layer_for_loss, trainable=True) learning_rate = theano.shared(base_lr) updates = lasagne.updates.adam(T.grad(loss, params), params, learning_rate=learning_rate, beta1=0.9, beta2=0.999) dc = hard_dice_per_img_in_batch(prediction_test, seg_sym.argmax(1), num_classes, BATCH_SIZE).mean(0) train_fn = theano.function([x_sym, seg_sym], [loss, acc_train, loss_vec], updates=updates) val_fn = theano.function([x_sym, seg_sym], [loss_val, acc, dc]) all_val_dice_scores=None all_training_losses = [] all_validation_losses = [] all_validation_accuracies = [] all_training_accuracies = [] val_dice_scores = [] epoch = 0 while epoch < n_epochs: if epoch == 100: data_gen_train = create_data_gen_train(train_data, INPUT_PATCH_SIZE, num_classes, BATCH_SIZE, contrast_range=(0.85, 1.25), gamma_range = (0.8, 1.5), num_workers=6, num_cached_per_worker=2, do_elastic_transform=True, alpha=(0., 1000.), sigma=(10., 13.), do_rotation=True, a_x=(0., 2*np.pi), a_y=(-np.pi/8., np.pi/8.), a_z=(-np.pi/8., np.pi/8.), do_scale=True, scale_range=(0.85, 1.15), seeds=workers_seeds) if epoch == 175: data_gen_train = create_data_gen_train(train_data, INPUT_PATCH_SIZE, num_classes, BATCH_SIZE, contrast_range=(0.9, 1.1), gamma_range = (0.85, 1.3), num_workers=6, num_cached_per_worker=2, do_elastic_transform=True, alpha=(0., 750.), sigma=(10., 13.), do_rotation=True, a_x=(0., 2*np.pi), a_y=(-0.00001, 0.00001), a_z=(-0.00001, 0.00001), do_scale=True, scale_range=(0.85, 1.15), seeds=workers_seeds) epoch_start_time = time.time() learning_rate.set_value(np.float32(base_lr* lr_decay**(epoch))) print "epoch: ", epoch, " learning rate: ", learning_rate.get_value() train_loss = 0 train_acc_tmp = 0 train_loss_tmp = 0 batch_ctr = 0 for data_dict in data_gen_train: data = data_dict["data"].astype(np.float32) seg = data_dict["seg_onehot"].astype(np.float32).transpose(0, 2, 3, 4, 1).reshape((-1, num_classes)) if batch_ctr != 0 and batch_ctr % int(np.floor(n_batches_per_epoch/n_feedbacks_per_epoch)) == 0: print "number of batches: ", batch_ctr, "/", n_batches_per_epoch print "training_loss since last update: ", \ train_loss_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch), " train accuracy: ", \ train_acc_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch) all_training_losses.append(train_loss_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch)) all_training_accuracies.append(train_acc_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch)) train_loss_tmp = 0 train_acc_tmp = 0 if len(val_dice_scores) > 0: all_val_dice_scores = np.concatenate(val_dice_scores, axis=0).reshape((-1, num_classes)) try: printLosses(all_training_losses, all_training_accuracies, all_validation_losses, all_validation_accuracies, os.path.join(results_dir, "%s.png" % EXPERIMENT_NAME), n_feedbacks_per_epoch, val_dice_scores=all_val_dice_scores, val_dice_scores_labels=["brain", "1", "2", "3", "4", "5"]) except: pass loss_vec, acc, l = train_fn(data, seg) loss = loss_vec.mean() train_loss += loss train_loss_tmp += loss train_acc_tmp += acc batch_ctr += 1 if batch_ctr >= n_batches_per_epoch: break all_training_losses.append(train_loss_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch)) all_training_accuracies.append(train_acc_tmp/np.floor(n_batches_per_epoch/n_feedbacks_per_epoch)) train_loss /= n_batches_per_epoch print "training loss average on epoch: ", train_loss val_loss = 0 accuracies = [] valid_batch_ctr = 0 all_dice = [] for data_dict in data_gen_validation: data = data_dict["data"].astype(np.float32) seg = data_dict["seg_onehot"].astype(np.float32).transpose(0, 2, 3, 4, 1).reshape((-1, num_classes)) w = np.zeros(num_classes, dtype=np.float32) w[np.unique(seg.argmax(-1))] = 1 loss, acc, dice = val_fn(data, seg) dice[w==0] = 2 all_dice.append(dice) val_loss += loss accuracies.append(acc) valid_batch_ctr += 1 if valid_batch_ctr >= n_test_batches: break all_dice = np.vstack(all_dice) dice_means = np.zeros(num_classes) for i in range(num_classes): dice_means[i] = all_dice[all_dice[:, i]!=2, i].mean() val_loss /= n_test_batches print "val loss: ", val_loss print "val acc: ", np.mean(accuracies), "\n" print "val dice: ", dice_means print "This epoch took %f sec" % (time.time()-epoch_start_time) val_dice_scores.append(dice_means) all_validation_losses.append(val_loss) all_validation_accuracies.append(np.mean(accuracies)) all_val_dice_scores = np.concatenate(val_dice_scores, axis=0).reshape((-1, num_classes)) try: printLosses(all_training_losses, all_training_accuracies, all_validation_losses, all_validation_accuracies, os.path.join(results_dir, "%s.png" % EXPERIMENT_NAME), n_feedbacks_per_epoch, val_dice_scores=all_val_dice_scores, val_dice_scores_labels=["brain", "1", "2", "3", "4", "5"]) except: pass with open(os.path.join(results_dir, "%s_Params.pkl" % (EXPERIMENT_NAME)), 'w') as f: cPickle.dump(lasagne.layers.get_all_param_values(output_layer_for_loss), f) with open(os.path.join(results_dir, "%s_allLossesNAccur.pkl"% (EXPERIMENT_NAME)), 'w') as f: cPickle.dump([all_training_losses, all_training_accuracies, all_validation_losses, all_validation_accuracies, val_dice_scores], f) epoch += 1
net_input[0,3] = patient_data["adc"] net_input[0,4] = patient_data["cbv"] print "compiling theano functions" # uild_UNet(n_input_channels=1, BATCH_SIZE=None, num_output_classes=2, pad='same', nonlinearity=lasagne.nonlinearities.elu, input_dim=(128, 128), base_n_filters=64, do_dropout=False): net = build_UNet3D(5, 1, num_output_classes=5, base_n_filters=16, input_dim=new_shape, pad=0) output_layer = net["segmentation"] with open(os.path.join(results_folder, "%s_allLossesNAccur_ep%d.pkl" % (experiment_name, epoch)), 'r') as f: tmp = cPickle.load(f) with open(os.path.join(results_folder, "%s_Params_ep%d.pkl" % (experiment_name, epoch)), 'r') as f: params = cPickle.load(f) lasagne.layers.set_all_param_values(output_layer, params) import theano.tensor as T data_sym = T.tensor5() output = softmax_helper(lasagne.layers.get_output(output_layer, data_sym, deterministic=False)) pred_fn = theano.function([data_sym], output) print "predicting image" cmap = ListedColormap([(0,0,0), (0,0,1), (0,1,0), (1,0,0), (1,1,0), (0.3, 0.5, 1)]) res = pred_fn(net_input) res_img = res.argmax(1)[0] seg = patient_data["seg"] seg = center_crop_3d_image(seg, output_layer.output_shape[2:]) patient_data["t1"] = center_crop_3d_image(patient_data["t1"], output_layer.output_shape[2:]) patient_data["t1km"] = center_crop_3d_image(patient_data["t1km"], output_layer.output_shape[2:]) patient_data["flair"] = center_crop_3d_image(patient_data["flair"], output_layer.output_shape[2:])
def main(model='mlp', num_epochs=100): # Load the dataset print("Loading data...") # num_per_class = 100 # print("Using %d per class" % num_per_class) print("Using all the training data") # Load Data## X_train, y_train, X_test, y_test = load_data_3D("/X_train.npy", "/Y_train.npy", "/X_test.npy", "/Y_test.npy", "MNIST_3D") X_test_all = X_test y_test_all = y_test X_test = X_test_all[:200] y_test = y_test[:200] X_train_final = [] y_train_final = [] for i in range(10): X_train_class = X_train[y_train == i] permutate_index = np.arange(X_train_class.shape[0]) X_train_final.append(X_train_class[permutate_index[:100]]) y_train_final += [i] * 100 X_train = np.vstack(X_train_final) y_train = np.array(y_train_final, dtype=np.int32) # Define Batch Size ## batch_size = 100 # Define nRotation for exhaustive search ## nRotation = 8 # The dimension would be (nRotation * n, w, h) input_var = T.tensor5('inputs') vanilla_target_var = T.ivector('vanilla_targets') # Create neural network model (depending on first command line parameter) network, network_for_rotation, weight_decay, network_transformed = \ build_cnn(input_var, batch_size) # saved_weights = np.load("../data/mnist_Chi_dec_100.npy") saved_weights = \ np.load("../data/mnist_CNN_params_drop_out_Chi_2017_hinge.npy") affine_matrix_matrix = [np.zeros((batch_size * 10,), dtype=np.float32), np.zeros((batch_size * 10,), dtype=np.float32), np.zeros((batch_size * 10,), dtype=np.float32)] network_saved_weights = affine_matrix_matrix + \ [saved_weights[i] for i in range(saved_weights.shape[0])] lasagne.layers.set_all_param_values(network, network_saved_weights) # Create a loss expression for training, i.e., a scalar objective we want # to minimize (for our multi-class problem, it is the cross-entropy loss): # The dimension would be (nRotation * n, 10) predictions = lasagne.layers.get_output(network) predictions = T.reshape(predictions, (-1, 10, 10)) predictions_rotation = lasagne.layers.get_output(network_for_rotation, deterministic=True) predictions_rotation_for_loss = \ lasagne.layers.get_output(network_for_rotation) transformed_images = lasagne.layers.get_output(network_transformed, deterministic=True) # loss_affine_before = \ # lasagne.objectives.squared_error(predictions_rotation.clip(-20, 3), 3) + lasagne.objectives.squared_error(predictions_rotation.clip(3, 20), 20) loss_affine_before = lasagne.objectives.squared_error(predictions_rotation.clip(-20, 20), 20) # loss_affine_before = -predictions_rotation_for_loss loss_affine = loss_affine_before.mean() loss = lasagne.objectives.multiclass_hinge_loss( predictions_rotation_for_loss, vanilla_target_var) loss = loss.mean() + weight_decay # This is to use the rotation that the "gradient descent" think will # give the highest score for each of the classes train_acc_1 = T.mean(T.eq(T.argmax(predictions_rotation_for_loss, axis=1), vanilla_target_var), dtype=theano.config.floatX) # This is to use all the scores produces by all the rotations, # compare them and get the highest one for each digit train_acc_2 = T.mean(T.eq(T.argmax(T.max(predictions, axis=1), axis=1), vanilla_target_var), dtype=theano.config.floatX) params = lasagne.layers.get_all_params(network, trainable=True) affine_params = params[:3] model_params = params[3:] updates_affine = lasagne.updates.sgd(loss_affine, affine_params, learning_rate=1000) """ d_loss_wrt_params = T.grad(loss_affine, affine_params) updates_affine = OrderedDict() for param, grad in zip(affine_params, d_loss_wrt_params): updates_affine[param] = param - 1000 * grad """ updates_model = lasagne.updates.adagrad(loss, model_params, learning_rate=0.01) test_prediction = lasagne.layers.get_output(network, deterministic=True) test_prediction = T.reshape(test_prediction, (-1, 10, 10)) test_prediction_rotation = lasagne.layers.get_output(network_for_rotation, deterministic=True) test_acc = T.mean(T.eq(T.argmax(T.max(test_prediction, axis=1), axis=1), vanilla_target_var), dtype=theano.config.floatX) # Compile a function performing a training step on a mini-batch (by giving # the updates dictionary) and returning the corresponding training loss: train_model_fn = theano.function([input_var, vanilla_target_var], [loss, train_acc_1, train_acc_2], updates=updates_model) train_affine_fn = theano.function([input_var], [loss_affine, loss_affine_before], updates=updates_affine) val_fn = theano.function([input_var, vanilla_target_var], [test_acc, transformed_images]) Parallel_Search = True TRAIN = True TEST = True # Finally, launch the training loop. # We iterate over epochs: cached_affine_matrix = np.array(np.zeros((3, X_train.shape[0], 10)), dtype=np.float32) for epoch in range(num_epochs): start_time = time.time() if (epoch % 20 == 0 or epoch + 1 == num_epochs) and TEST: print ("Start Evaluating...") test_acc = 0 test_batches = 0 affine_test_batches = 0 # Find best rotation if epoch + 1 == num_epochs: X_test = X_test_all y_test = y_test_all cached_affine_matrix_test = \ np.array(np.zeros((3, X_test.shape[0], 10)), dtype=np.float32) for batch in iterate_minibatches(X_test, y_test, batch_size, shuffle=False): inputs, targets, index = batch inputs = inputs.reshape(batch_size, 1, 40, 40, 40) train_loss_before_all = [] affine_params_all = [] if Parallel_Search: searchCandidate = 27 eachSearchCandidate = 3 eachDegree = 90.0 / eachSearchCandidate for x in range(eachSearchCandidate): x_degree = \ np.ones((batch_size * 10,), dtype=np.float32) * eachDegree * x - 45.0 for y in range(eachSearchCandidate): y_degree = \ np.ones((batch_size * 10,), dtype=np.float32) * eachDegree * y - 45.0 for z in range(eachSearchCandidate): z_degree = \ np.ones((batch_size * 10,), dtype=np.float32) * eachDegree * z - 45.0 affine_params[0].set_value(x_degree) affine_params[1].set_value(y_degree) affine_params[2].set_value(z_degree) for i in range(10): weightsOfParams = \ lasagne.layers.\ get_all_param_values(network) train_loss, train_loss_before = \ train_affine_fn(inputs) # print("degree x:", weightsOfParams[0][0]) # print("degree y:", weightsOfParams[1][0]) # print("degree z:", weightsOfParams[2][0]) # print("train_loss: ", train_loss_before[0]) # print("total_loss: ", train_loss) affine_params_all.append( np.array(weightsOfParams[:3]).reshape( 1, 3, batch_size, 10)) train_loss_before_all.append( train_loss_before.reshape( 1, batch_size, 10)) else: searchCandidate = 200 affine_params[0].set_value(np.zeros((batch_size * 10,), dtype=np.float32)) affine_params[1].set_value(np.zeros((batch_size * 10,), dtype=np.float32)) affine_params[2].set_value(np.zeros((batch_size * 10,), dtype=np.float32)) for i in range(searchCandidate): weightsOfParams = lasagne.layers.get_all_param_values(network) train_loss, train_loss_before = train_affine_fn(inputs) affine_params_all.append(np.array(weightsOfParams[:3].reshape(1, 3, batch_size, 10))) train_loss_before_all.append(train_loss_before.reshape(1, batch_size, 10)) train_loss_before_all = np.vstack(train_loss_before_all) affine_params_all = np.vstack(affine_params_all) train_loss_before_all_reshape = train_loss_before_all.reshape(searchCandidate, -1) affine_params_all_reshape_x = affine_params_all[:, 0].reshape(searchCandidate, -1) affine_params_all_reshape_y = affine_params_all[:, 1].reshape(searchCandidate, -1) affine_params_all_reshape_z = affine_params_all[:, 2].reshape(searchCandidate, -1) # Find the search candidate that gives the lowest loss train_arg_min = np.argmin(train_loss_before_all_reshape, axis=0) # According to the best search candidate, get the rotations. affine_params_all_reshape_x = affine_params_all_reshape_x[train_arg_min, np.arange(train_arg_min.shape[0])] affine_params_all_reshape_y = affine_params_all_reshape_y[train_arg_min, np.arange(train_arg_min.shape[0])] affine_params_all_reshape_z = affine_params_all_reshape_z[train_arg_min, np.arange(train_arg_min.shape[0])] cached_affine_matrix_test[0, index] = affine_params_all_reshape_x.reshape(-1, 10) cached_affine_matrix_test[1, index] = affine_params_all_reshape_y.reshape(-1, 10) cached_affine_matrix_test[2, index] = affine_params_all_reshape_z.reshape(-1, 10) affine_test_batches += 1 print(affine_test_batches) for batch in iterate_minibatches(X_test, y_test, batch_size, shuffle=False): inputs, targets, index = batch affine_params[0].set_value(cached_affine_matrix_test[0, index].reshape(-1,)) affine_params[1].set_value(cached_affine_matrix_test[1, index].reshape(-1,)) affine_params[2].set_value(cached_affine_matrix_test[2, index].reshape(-1,)) inputs = inputs.reshape(batch_size, 1, 40, 40, 40) acc, transformed_image_result = val_fn(inputs, targets) test_acc += acc test_batches += 1 if test_batches == 1: np.save("/hdd/Documents/tmp/3d_transformed_image_%d.npy" % epoch, transformed_image_result) print("Final results:") print(" test accuracy:\t\t{:.2f} %".format( test_acc / test_batches * 100)) if not TRAIN: break print("Starting training...") train_err = 0 train_acc_sum_1 = 0 train_acc_sum_2 = 0 train_batches = 0 affine_train_batches = 0 if epoch % 20 == 0: weightsOfParams = lasagne.layers.get_all_param_values(network) batch_loss = 0 for batch in iterate_minibatches(X_train, y_train, batch_size, shuffle=False): inputs, targets, index = batch inputs = inputs.reshape(batch_size, 1, 40, 40, 40) train_loss_before_all = [] affine_params_all = [] if Parallel_Search: searchCandidate = 27 eachSearchCandidate = 3 eachDegree = 90.0 / eachSearchCandidate for x in range(eachSearchCandidate): x_degree = np.ones((batch_size * 10,), dtype=np.float32) * eachDegree * x - 45.0 for y in range(eachSearchCandidate): y_degree = np.ones((batch_size * 10,), dtype=np.float32) * eachDegree * y - 45.0 for z in range(eachSearchCandidate): z_degree = np.ones((batch_size * 10,), dtype=np.float32) * eachDegree * z - 45.0 affine_params[0].set_value(x_degree) affine_params[1].set_value(y_degree) affine_params[2].set_value(z_degree) for i in range(10): weightsOfParams = lasagne.layers.get_all_param_values(network) train_loss, train_loss_before = train_affine_fn(inputs) affine_params_all.append(np.array(weightsOfParams[:3]).reshape(1, 3, batch_size, 10)) train_loss_before_all.append(train_loss_before.reshape(1, batch_size, 10)) else: searchCandidate = 200 affine_params[0].set_value(np.zeros((batch_size * 10,), dtype=np.float32)) affine_params[1].set_value(np.zeros((batch_size * 10,), dtype=np.float32)) affine_params[2].set_value(np.zeros((batch_size * 10,), dtype=np.float32)) for i in range(searchCandidate): weightsOfParams = lasagne.layers.get_all_param_values(network) train_loss, train_loss_before = train_affine_fn(inputs) affine_params_all.append(np.array(weightsOfParams[:3].reshape(1, 3, batch_size, 10))) train_loss_before_all.append(train_loss_before.reshape(1, batch_size, 10)) train_loss_before_all = np.vstack(train_loss_before_all) affine_params_all = np.vstack(affine_params_all) train_loss_before_all_reshape = train_loss_before_all.reshape(searchCandidate, -1) affine_params_all_reshape_x = affine_params_all[:, 0].reshape(searchCandidate, -1) affine_params_all_reshape_y = affine_params_all[:, 1].reshape(searchCandidate, -1) affine_params_all_reshape_z = affine_params_all[:, 2].reshape(searchCandidate, -1) # Find the search candidate that gives the lowest loss train_arg_min = np.argmin(train_loss_before_all_reshape, axis=0) # According to the best search candidate, get the rotations. affine_params_all_reshape_x = affine_params_all_reshape_x[train_arg_min, np.arange(train_arg_min.shape[0])] affine_params_all_reshape_y = affine_params_all_reshape_y[train_arg_min, np.arange(train_arg_min.shape[0])] affine_params_all_reshape_z = affine_params_all_reshape_z[train_arg_min, np.arange(train_arg_min.shape[0])] cached_affine_matrix[0, index] = affine_params_all_reshape_x.reshape(-1, 10) cached_affine_matrix[1, index] = affine_params_all_reshape_y.reshape(-1, 10) cached_affine_matrix[2, index] = affine_params_all_reshape_z.reshape(-1, 10) affine_train_batches += 1 batch_loss += np.mean(train_loss_before_all) print(affine_train_batches) print(batch_loss / affine_train_batches) print (time.time() - start_time) if 1: for batch in iterate_minibatches(X_train, y_train, batch_size, shuffle=True): inputs, targets, index = batch affine_params[0].set_value(cached_affine_matrix[0, index].reshape(-1,)) affine_params[1].set_value(cached_affine_matrix[1, index].reshape(-1,)) affine_params[2].set_value(cached_affine_matrix[2, index].reshape(-1,)) inputs = inputs.reshape(batch_size, 1, 40, 40, 40) train_loss_value, train_acc_value_1, train_acc_value_2 = train_model_fn(inputs, targets) train_err += train_loss_value train_acc_sum_1 += train_acc_value_1 train_acc_sum_2 += train_acc_value_2 train_batches += 1 # Then we print the results for this epoch: print("Epoch {} of {} took {:.3f}s".format( epoch + 1, num_epochs, time.time() - start_time)) print(" training loss:\t\t{:.6f}".format(train_err / train_batches)) print(" training acc 1:\t\t{:.6f}".format(train_acc_sum_1 / train_batches)) print(" training acc 2:\t\t{:.6f}".format(train_acc_sum_2 / train_batches)) if epoch % 21 == 0: weightsOfParams = lasagne.layers.get_all_param_values(network) np.save("../data/mnist_CNN_params_drop_out_Chi_2017_ROT_hinge_2000_em_new_batch_run_epoch_%d_3d_find_rotation.npy" % epoch, weightsOfParams)
def __init__(self, configFile): self.logger = logging.getLogger(__name__) self.configInfo = {} execfile(configFile, self.configInfo) assert self.configInfo['networkType'] == 'dilated3DNet' self.networkType = 'dilated3DNet' self.outputFolder = self.configInfo['outputFolder'] # ---------------------------------------------------------------------------------------- # For build Dilated3DNet. self.preTrainedWeights = self.configInfo['preTrainedWeights'] # For displaying the output shape change process through the network. self.modals = self.configInfo['modals'] self.inputChannels = len(self.modals) self.trainSampleSize = self.configInfo['trainSampleSize'] self.kernelShapeList = self.configInfo['kernelShapeList'] self.kernelNumList = self.configInfo['kernelNumList'] self.dilatedFactorList = self.configInfo['dilatedFactorList'] self.numOfClasses = self.configInfo['numOfClasses'] self.dropoutRates = self.configInfo['dropoutRates'] assert len(self.kernelShapeList) == len(self.kernelNumList) assert len(self.kernelShapeList) == len(self.dilatedFactorList) assert self.numOfClasses == self.kernelNumList[-1] self.inputShape = (None, self.inputChannels, None, None, None) self.inputVar = T.tensor5('inputVar', dtype = theano.config.floatX) self.targetVar = T.tensor4('targetVar', dtype = 'int32') self.receptiveField = 'Only after building the dilated3DNet, can we get the receptive filed' self.outputLayer = self.buildDilated3DNet() self.restoreWeights() # ---------------------------------------------------------------------------------------- # For comlile train function. # We let the learning can be learnt self.weightsFolder = self.configInfo['weightsFolder'] self.learningRate = theano.shared(np.array(self.configInfo['learningRate'], dtype = theano.config.floatX)) self.learningRateDecay = np.array(self.configInfo['learningRateDecay'], dtype = theano.config.floatX) self.weightDecay = self.configInfo['weightDecay'] self.optimizerDict = {'rmsprop':rmsprop, 'momentum':momentum} self.optimizer = self.optimizerDict[self.configInfo['optimizer']] self.trainFunction = self.complieTrainFunction() # ---------------------------------------------------------------------------------------- # For compile val function self.valSampleSize = self.configInfo['valSampleSize'] self.valFunction = self.compileValFunction() # ---------------------------------------------------------------------------------------- # For compile test function self.testSampleSize = self.configInfo['testSampleSize'] self.testFunction = self.compileTestFunction()
def get_net(net_cfg, args): # encoder, classification, decoder = net_cfg(...) l_out_for_classifier, l_out_for_decimator, l_out_for_decoder = net_cfg( args) if args["dim"] == "2d": X = T.tensor4('X') Y = T.tensor4('Y') elif args["dim"] == "3d": X = T.tensor5('X') Y = T.tensor5('Y') net_out_for_classifier = get_output(l_out_for_classifier, X) # returns the sigmoid masks net_out_for_decimator = get_output(l_out_for_decimator, Y) net_out_for_decoder = get_output(l_out_for_decoder, X) loss_reconstruction = squared_error(net_out_for_decoder, X).mean() loss_segmentation = binary_crossentropy(net_out_for_classifier, net_out_for_decimator).mean() if args["mode"] == "segmentation": sys.stderr.write( "mode = segmentation (loss_combined = loss_segmentation)\n") loss_combined = loss_segmentation elif args["mode"] == "mixed": sys.stderr.write( "mode = mixed (loss_combined = loss_segmentation + loss_reconstruction)\n" ) loss_combined = loss_segmentation + loss_reconstruction params = get_all_params( l_out_for_decoder, trainable=True ) # this needs to be fixed for when we do future prediction lr = theano.shared(floatX(args["learning_rate"])) if "optim" not in args: updates = nesterov_momentum(loss_combined, params, learning_rate=lr, momentum=0.9) else: if args["optim"] == "rmsprop": sys.stderr.write("using rmsprop optimisation\n") updates = rmsprop(loss_combined, params, learning_rate=lr) elif args["optim"] == "adam": sys.stderr.write("using adam optimisation\n") updates = adam(loss_combined, params, learning_rate=lr) # todo: fix [loss,loss] train_fn = theano.function( [X, Y], [loss_reconstruction, loss_segmentation, loss_combined], updates=updates, on_unused_input='warn') loss_fn = theano.function( [X, Y], [loss_reconstruction, loss_segmentation, loss_combined], on_unused_input='warn') classifier_out_fn = theano.function([X], net_out_for_classifier, on_unused_input='warn') decoder_out_fn = theano.function([X], net_out_for_decoder, on_unused_input='warn') return { "train_fn": train_fn, "loss_fn": loss_fn, "classifier_out_fn": classifier_out_fn, "decoder_out_fn": decoder_out_fn, "lr": lr, "l_out_for_classifier": l_out_for_classifier, "l_out_for_decimator": l_out_for_decimator, "l_out_for_decoder": l_out_for_decoder }
def compile_theano_functions(self, data_type='2D', loss='cross_entropy'): assert self.net != None ### symbolic theano input theano_args = OrderedDict() dim = len(self.cf.dim) if data_type == '2D': assert dim == 2 theano_args['X'] = T.tensor4() theano_args['y'] = T.tensor4() theano_args['X'].tag.test_value = np.random.rand( 20, 1, 256, 256).astype('float32') self.logger.info('Net: Working with 2D data.') val_args = deepcopy(theano_args) train_args = deepcopy(theano_args) train_args['lr'] = T.scalar(name='lr') ### prediction functions # get flattened softmax prediction of shape (pixels, classes), where pixels = b*0*1 prediction_train_smax_flat = get_output( self.net[self.cf.out_layer_flat], train_args['X'], deterministic=False) prediction_val_smax_flat = get_output( self.net[self.cf.out_layer_flat], val_args['X'], deterministic=True) # reshape softmax prediction: shapes (pixels,c) -> (b,c,0,1) prediction_train_smax = prediction_train_smax_flat.reshape( (train_args['X'].shape[0], self.cf.out_dim[0], self.cf.out_dim[1], self.cf.num_classes)).transpose( (0, 3, 1, 2)) prediction_val_smax = prediction_val_smax_flat.reshape( (val_args['X'].shape[0], self.cf.out_dim[0], self.cf.out_dim[1], self.cf.num_classes)).transpose( (0, 3, 1, 2)) self.predict_smax['train'] = theano.function([train_args['X']], prediction_train_smax) self.predict_smax['val'] = theano.function([val_args['X']], prediction_val_smax) # reshape target vector: shapes (b,c,0,1) -> (b*0*1,c) flat_target_train = train_args['y'].transpose( (0, 2, 3, 1)).reshape((-1, self.cf.num_classes)) flat_target_val = val_args['y'].transpose((0, 2, 3, 1)).reshape( (-1, self.cf.num_classes)) elif data_type == '3D': assert dim == 3 theano_args['X'] = T.tensor5() theano_args['y'] = T.tensor5() self.logger.info('Net: Working with 3D data.') val_args = deepcopy(theano_args) train_args = deepcopy(theano_args) train_args['lr'] = T.scalar(name='lr') ### prediction functions # get flattened softmax prediction of shape (pixels, classes), where pixels = b*0*1*2 prediction_train_smax_flat = get_output( self.net[self.cf.out_layer_flat], train_args['X'], deterministic=False) prediction_val_smax_flat = get_output( self.net[self.cf.out_layer_flat], val_args['X'], deterministic=True) # reshape softmax prediction: shapes (pixels,c) -> (b,c,0,1,2) prediction_train_smax = prediction_train_smax_flat.reshape( (train_args['X'].shape[0], self.cf.dim[0], self.cf.dim[1], self.cf.dim[2], self.cf.num_classes)).transpose( (0, 4, 1, 2, 3)) prediction_val_smax = prediction_val_smax_flat.reshape( (val_args['X'].shape[0], self.cf.dim[0], self.cf.dim[1], self.cf.dim[2], self.cf.num_classes)).transpose( (0, 4, 1, 2, 3)) self.predict_smax['train'] = theano.function([train_args['X']], prediction_train_smax) self.predict_smax['val'] = theano.function([val_args['X']], prediction_val_smax) # reshape target vector: shapes (b,c,0,1,2) -> (b*0*1*2,c) flat_target_train = train_args['y'].transpose( (0, 2, 3, 4, 1)).reshape((-1, self.cf.num_classes)) flat_target_val = val_args['y'].transpose((0, 2, 3, 4, 1)).reshape( (-1, self.cf.num_classes)) pred_train_one_hot = get_one_hot_prediction(prediction_train_smax, self.cf.num_classes) pred_val_one_hot = get_one_hot_prediction(prediction_val_smax, self.cf.num_classes) self.predict_one_hot['val'] = theano.function([val_args['X']], pred_val_one_hot) self.predict_one_hot['train'] = theano.function([train_args['X']], pred_train_one_hot) prediction_val = T.argmax(prediction_val_smax, axis=1) prediction_train = T.argmax(prediction_train_smax, axis=1) self.predict['val'] = theano.function([val_args['X']], prediction_val) self.predict['train'] = theano.function([train_args['X']], prediction_train) ### evaluation metrics train_dices_hard = binary_dice_per_instance_and_class( pred_train_one_hot, train_args['y'], dim) val_dices_hard = binary_dice_per_instance_and_class( pred_val_one_hot, val_args['y'], dim) train_dices_soft = binary_dice_per_instance_and_class( prediction_train_smax, train_args['y'], dim) val_dices_soft = binary_dice_per_instance_and_class( prediction_val_smax, val_args['y'], dim) ### loss types if loss == 'cross_entropy': self.loss['train'] = categorical_crossentropy( prediction_train_smax_flat, flat_target_train).mean() self.loss['val'] = categorical_crossentropy( prediction_val_smax_flat, flat_target_val).mean() if loss == 'weighted_cross_entropy': theano_args['w'] = T.fvector() train_args['w'] = T.fvector() train_loss = categorical_crossentropy(prediction_train_smax_flat, flat_target_train) train_loss *= train_args['w'] self.loss['train'] = train_loss.mean() val_args['w'] = T.fvector() val_loss = categorical_crossentropy(prediction_val_smax_flat, flat_target_val) val_loss *= val_args['w'] self.loss['val'] = val_loss.mean() if loss == 'dice': self.loss['train'] = 1 - train_dices_soft.mean() self.loss['val'] = 1 - val_dices_soft.mean() self.logger.info('Net: Using {} loss.'.format(loss)) if self.cf.use_weight_decay: training_loss = self.loss['train'] +\ self.cf.weight_decay * lasagne.regularization.regularize_network_params(self.net[self.cf.out_layer_flat], lasagne.regularization.l2) self.logger.info('Net: Using weight decay of {}.'.format( self.cf.weight_decay)) else: training_loss = self.loss['train'] ### training functions params = get_all_params(self.net[self.cf.out_layer_flat], trainable=True) grads = theano.grad(training_loss, params) updates = adam(grads, params, learning_rate=train_args['lr']) self.train_fn = theano.function(train_args.values(), [self.loss['train'], train_dices_hard], updates=updates) self.val_fn = theano.function(val_args.values(), [self.loss['val'], val_dices_hard]) self.logger.info('Net: Compiled theano functions.')
def __init__( self, input_shape, output_dim, hidden_sizes, conv_filters, conv_filter_sizes, conv_strides, conv_pads, hidden_nonlinearity=LN.tanh, output_pi_nonlinearity=LN.softmax, conv_W_init=LI.GlorotUniform(), hidden_W_init=NormCInit(1.0), output_pi_W_init=NormCInit(0.01), # (like in OpenAI baselines) output_v_W_init=NormCInit(1.0), all_b_init=LI.Constant(0.), hidden_h_init=LI.Constant(0.), hidden_init_trainable=False, pixel_scale=255., input_var=None, name=None, ): if name is None: prefix = "" else: prefix = name + "_" if input_var is None: # NOTE: decide whether to provide frame history in each obs input_var = T.tensor5('input', dtype='uint8') step_input_var = T.tensor4('step_input', dtype='uint8') assert input_var.ndim == 5 assert len(input_shape) == 3 # First, build the TRAINING layers. ################################### l_in = L.InputLayer(shape=(None, None) + input_shape, input_var=input_var) x = ScalarFixedScaleLayer(l_in, scale=pixel_scale, name='pixel_scale') # Reshape leading dimensions for convolution (n_batch x n_step) n_batch, n_step = x.shape[0], x.shape[1] x = L.reshape(x, (n_batch * n_step, ) + input_shape) ls_conv = list() for i, (n_filters, size, stride, pad) in enumerate( zip(conv_filters, conv_filter_sizes, conv_strides, conv_pads)): x = L.Conv2DLayer( x, num_filters=n_filters, filter_size=size, stride=(stride, stride), pad=pad, nonlinearity=hidden_nonlinearity, W=conv_W_init, b=all_b_init, name="%sconv_hidden_%d" % (prefix, i), ) ls_conv.append(x) l_conv_out = x # Reshape leading dimensions for recurrence, might as well flatten trailing x = L.reshape(x, [n_batch, n_step, -1]) ls_recurrent = list() ls_prev_hidden = list() for i, hidden_size in enumerate(hidden_sizes): l_prev_hidden = L.InputLayer(shape=(None, ) + hidden_size) x = RecurrentLayer( incomings=(x, l_prev_hidden), num_units=hidden_size, nonlinearity=hidden_nonlinearity, name="%shidden_recurrent_%d" % (prefix, i), W_xh=hidden_W_init, W_hh=hidden_W_init, b=all_b_init, h0=hidden_h_init, h0_trainable=hidden_init_trainable, ) ls_prev_hidden.append(l_prev_hidden) ls_recurrent.append(x) l_out_pi = L.DenseLayer( x, num_units=output_dim, nonlinearity=output_pi_nonlinearity, name="%soutput_pi" % (prefix, ), W=output_pi_W_init, b=all_b_init, ) l_out_pi = L.reshape(l_out_pi, [n_batch, n_step, output_dim]) l_out_v = L.DenseLayer( x, num_units=1, nonlinearity=LN.linear, name="%soutput_v" % (prefix, ), W=output_v_W_init, b=all_b_init, ) l_out_v = L.reshape(l_out_v, [n_batch, n_step]) # Second, repeat for one-step INFERENCE layers (no time dimension) #### # (yes this is annoying) # Needed because input has different n_dim, and different reshaping # throughout. l_step_in = L.InputLayer(shape=(None, ) + input_shape, input_var=step_input_var) x_step = ScalarFixedScaleLayer(l_step_in, scale=pixel_scale, name='step_pixel_scale') for i, (n_filters, size, stride, pad) in enumerate( zip(conv_filters, conv_filter_sizes, conv_strides, conv_pads)): x_step = L.Conv2DLayer( x_step, num_filters=n_filters, filter_size=size, stride=(stride, stride), pad=pad, nonlinearity=hidden_nonlinearity, W=ls_conv[i].W, # must use the same parameter variable b=ls_conv[i].b, name="%sconv_hidden_step_%d" % (prefix, i), ) l_step_conv_out = x_step ls_step_hidden = list() for i, hidden_size in enumerate(hidden_sizes): x_step = RecurrentLayer( incomings=(x_step, ls_prev_hidden[i]), num_units=hidden_size, nonlinearity=hidden_nonlinearity, name="%shidden_step_%d" % (prefix, i), W_xh=ls_recurrent[i].W_xh, W_hh=ls_recurrent[i].W_hh, b=ls_recurrent[i].b, h0=ls_recurrent[i].h0, h0_trainable=hidden_init_trainable, ) ls_step_hidden.append(x_step) l_step_pi = L.DenseLayer( x_step, num_units=output_dim, nonlinearity=output_pi_nonlinearity, W=l_out_pi.W, b=l_out_pi.b, ) l_step_v = L.DenseLayer( x_step, num_units=1, nonlinearity=LN.linear, W=l_out_v.W, b=l_out_v.b, ) l_step_v = L.reshape(l_step_v, [-1]) self._l_out_pi = l_out_pi self._l_out_v = l_out_v self._l_in = l_in self._l_conv_out = l_conv_out self._ls_recurrent = ls_recurrent self._ls_prev_hidden = ls_prev_hidden self._l_step_in = l_step_in self._ls_step_hidden = ls_step_hidden self._l_step_pi = l_step_pi self._l_step_v = l_step_v self._l_step_conv_out = l_step_conv_out
net = build_model(weights_path, options) # iterate through all test scans for t1, current_scan in zip(t1_test_paths, folder_names): t = test_scan(net, t1, options) print " --> tested subject :", current_scan, "(elapsed time:", t, "min.)" #### Batch iteration functions # In[ ]: from utils import iterate_minibatches, iterate_minibatches_train # In[ ]: input_var = T.tensor5(name='input', dtype='float32') target_var = T.ivector() input_var2 = T.matrix(name='input2', dtype='float32') #input_var2 = T.vector2(name='input2', dtype='float32') #### Network definition # In[ ]: def build_net(): """Method for VoxResNet Building. Returns ------- dictionary
def compile_theano_functions(self, data_type='2D', loss='cross_entropy'): assert self.net != None ### symbolic theano input theano_args = OrderedDict() dim = len(self.cf.dim) if data_type == '2D': assert dim == 2 theano_args['X'] = T.tensor4() theano_args['y'] = T.ivector() self.logger.info('Net: Working with 2D data.') elif data_type == '3D': assert dim == 3 theano_args['X'] = T.tensor5() theano_args['y'] = T.ivector() self.logger.info('Net: Working with 3D data.') val_args = deepcopy(theano_args) train_args = deepcopy(theano_args) train_args['lr'] = T.scalar(name='lr') ### prediction functions # get softmax prediction of shape (b, classes) prediction_train_smax = get_output(self.net[self.cf.out_layer], train_args['X'], deterministic=False) prediction_val_smax = get_output(self.net[self.cf.out_layer], val_args['X'], deterministic=True) self.predict_smax['train'] = theano.function([train_args['X']], prediction_train_smax) self.predict_smax['val'] = theano.function([val_args['X']], prediction_val_smax) # get one hot encoded target vector one_hot_target_train = get_one_hot_class_target( train_args['y'], self.cf.num_classes) one_hot_target_val = get_one_hot_class_target(val_args['y'], self.cf.num_classes) ### cross entropy loss if loss == 'cross_entropy': self.loss['train'] = categorical_crossentropy( prediction_train_smax, one_hot_target_train).mean() self.loss['val'] = categorical_crossentropy( prediction_val_smax, one_hot_target_val).mean() if self.cf.use_weight_decay: training_loss = self.loss['train'] +\ self.cf.weight_decay * lasagne.regularization.regularize_network_params(self.net[self.cf.out_layer], lasagne.regularization.l2) self.logger.info('Net: Using weight decay of {}.'.format( self.cf.weight_decay)) else: training_loss = self.loss['train'] ### accuracy train_acc = T.mean( T.eq(T.argmax(prediction_train_smax, axis=1), train_args['y'])) val_acc = T.mean( T.eq(T.argmax(prediction_val_smax, axis=1), val_args['y'])) ### training functions params = get_all_params(self.net[self.cf.out_layer], trainable=True) grads = theano.grad(training_loss, params) updates = adam(grads, params, learning_rate=train_args['lr']) self.train_fn = theano.function(train_args.values(), [self.loss['train'], train_acc], updates=updates) self.val_fn = theano.function( val_args.values(), [self.loss['val'], val_acc, prediction_val_smax]) self.logger.info('Net: Compiled theano functions.')
def build_network(self): self.input_var = tensor5() self.output_var = matrix() net = OrderedDict() if self.instance_norm: norm_fct = batch_norm norm_kwargs = {'axes': (2, 3, 4)} else: norm_fct = batch_norm norm_kwargs = {'axes': 'auto'} self.input_layer = net['input'] = InputLayer( (self.batch_size, self.n_input_channels, self.input_dim[0], self.input_dim[1], self.input_dim[2]), self.input_var) net['contr_1_1'] = norm_fct( Conv3DLayer(net['input'], self.base_n_filters, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) net['contr_1_2'] = norm_fct( Conv3DLayer(net['contr_1_1'], self.base_n_filters, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) net['pool1'] = Pool3DLayer(net['contr_1_2'], (1, 2, 2)) net['contr_2_1'] = norm_fct( Conv3DLayer(net['pool1'], self.base_n_filters * 2, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) net['contr_2_2'] = norm_fct( Conv3DLayer(net['contr_2_1'], self.base_n_filters * 2, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) l = net['pool2'] = Pool3DLayer(net['contr_2_2'], (1, 2, 2)) if self.dropout is not None: l = DropoutLayer(l, p=self.dropout) net['contr_3_1'] = norm_fct( Conv3DLayer(l, self.base_n_filters * 4, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) net['contr_3_2'] = norm_fct( Conv3DLayer(net['contr_3_1'], self.base_n_filters * 4, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) l = net['pool3'] = Pool3DLayer(net['contr_3_2'], (1, 2, 2)) if self.dropout is not None: l = DropoutLayer(l, p=self.dropout) net['contr_4_1'] = norm_fct( Conv3DLayer(l, self.base_n_filters * 8, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) net['contr_4_2'] = norm_fct( Conv3DLayer(net['contr_4_1'], self.base_n_filters * 8, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) l = net['pool4'] = Pool3DLayer(net['contr_4_2'], (1, 2, 2)) if self.dropout is not None: l = DropoutLayer(l, p=self.dropout) net['encode_1'] = norm_fct( Conv3DLayer(l, self.base_n_filters * 16, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) l = net['encode_2'] = norm_fct( Conv3DLayer(net['encode_1'], self.base_n_filters * 16, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) net['upscale1'] = Upscale3DLayer(l, (1, 2, 2)) l = net['concat1'] = ConcatLayer([net['upscale1'], net['contr_4_2']], cropping=(None, None, "center", "center", "center")) if self.dropout is not None: l = DropoutLayer(l, p=self.dropout) net['expand_1_1'] = norm_fct( Conv3DLayer(l, self.base_n_filters * 8, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) l = net['expand_1_2'] = norm_fct( Conv3DLayer(net['expand_1_1'], self.base_n_filters * 8, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) net['upscale2'] = Upscale3DLayer(l, (1, 2, 2)) l = net['concat2'] = ConcatLayer([net['upscale2'], net['contr_3_2']], cropping=(None, None, "center", "center", "center")) if self.dropout is not None: l = DropoutLayer(l, p=self.dropout) net['expand_2_1'] = norm_fct( Conv3DLayer(l, self.base_n_filters * 4, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) ds2 = l = net['expand_2_2'] = norm_fct( Conv3DLayer(net['expand_2_1'], self.base_n_filters * 4, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) net['upscale3'] = Upscale3DLayer(l, (1, 2, 2)) l = net['concat3'] = ConcatLayer([net['upscale3'], net['contr_2_2']], cropping=(None, None, "center", "center", "center")) if self.dropout is not None: l = DropoutLayer(l, p=self.dropout) net['expand_3_1'] = norm_fct( Conv3DLayer(l, self.base_n_filters * 2, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) l = net['expand_3_2'] = norm_fct( Conv3DLayer(net['expand_3_1'], self.base_n_filters * 2, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) net['upscale4'] = Upscale3DLayer(l, (1, 2, 2)) net['concat4'] = ConcatLayer([net['upscale4'], net['contr_1_2']], cropping=(None, None, "center", "center", "center")) net['expand_4_1'] = norm_fct( Conv3DLayer(net['concat4'], self.base_n_filters, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) net['expand_4_2'] = norm_fct( Conv3DLayer(net['expand_4_1'], self.base_n_filters, 3, nonlinearity=self.nonlinearity, pad=self.pad, W=lasagne.init.HeNormal(gain="relu")), **norm_kwargs) net['output_segmentation'] = Conv3DLayer(net['expand_4_2'], self.num_classes, 1, nonlinearity=None) ds2_1x1_conv = Conv3DLayer(ds2, self.num_classes, 1, 1, 'same', nonlinearity=lasagne.nonlinearities.linear, W=lasagne.init.HeNormal(gain='relu')) ds1_ds2_sum_upscale = Upscale3DLayer(ds2_1x1_conv, (1, 2, 2)) ds3_1x1_conv = Conv3DLayer(net['expand_3_2'], self.num_classes, 1, 1, 'same', nonlinearity=lasagne.nonlinearities.linear, W=lasagne.init.HeNormal(gain='relu')) ds1_ds2_sum_upscale_ds3_sum = ElemwiseSumLayer( (ds1_ds2_sum_upscale, ds3_1x1_conv)) ds1_ds2_sum_upscale_ds3_sum_upscale = Upscale3DLayer( ds1_ds2_sum_upscale_ds3_sum, (1, 2, 2)) self.seg_layer = l = ElemwiseSumLayer( (net['output_segmentation'], ds1_ds2_sum_upscale_ds3_sum_upscale)) net['dimshuffle'] = DimshuffleLayer(l, (0, 2, 3, 4, 1)) batch_size, n_z, n_rows, n_cols, _ = lasagne.layers.get_output( net['dimshuffle']).shape net['reshapeSeg'] = ReshapeLayer( net['dimshuffle'], (batch_size * n_rows * n_cols * n_z, self.num_classes)) self.output_layer = net['output_flattened'] = NonlinearityLayer( net['reshapeSeg'], nonlinearity=lasagne.nonlinearities.softmax)
data_gen_train = segDataAugm.elastric_transform_generator(data_gen_train, 900, 12) data_gen_train = segDataAugm.mirror_axis_generator(data_gen_train) data_gen_train = segDataAugm.center_crop_generator(data_gen_train, CROP_PATCHES_TO_THIS) data_gen_train = segDataAugm.center_crop_seg_generator(data_gen_train, OUTPUT_PATCH_SIZE) data_gen_train = MultiThreadedGenerator(data_gen_train, 8, 8) data_gen_train._start() net = build_UNet3D(5, BATCH_SIZE, num_output_classes=num_classes, base_n_filters=16, input_dim=CROP_PATCHES_TO_THIS, pad=0) output_layer_for_loss = net["output_flattened"] n_batches_per_epoch = 300 # n_batches_per_epoch = np.floor(n_training_samples/float(BATCH_SIZE)) n_test_batches = 30 # n_test_batches = np.floor(n_val_samples/float(BATCH_SIZE)) x_sym = T.tensor5() seg_sym = T.ivector() w_sym = T.vector() # add some weight decay l2_loss = lasagne.regularization.regularize_network_params(output_layer_for_loss, lasagne.regularization.l2) * 1e-5 # the distinction between prediction_train and test is important only if we enable dropout prediction_train = lasagne.layers.get_output(output_layer_for_loss, x_sym, deterministic=False) # we could use a binary loss but I stuck with categorical crossentropy so that less code has to be changed if your # application has more than two classes loss_vec = lasagne.objectives.categorical_crossentropy(prediction_train, seg_sym) loss_vec *= w_sym loss = loss_vec.mean() loss += l2_loss acc_train = T.mean(T.eq(T.argmax(prediction_train, axis=1), seg_sym), dtype=theano.config.floatX)
X_train, y_train, X_test, y_test = load_data("/X_train.npy", "/Y_train.npy", "/X_test.npy", "/Y_test.npy") X_train_2d = X_train.reshape(X_train.shape[0], 1, 28, 28, 1) X_train_3d = np.zeros((X_train.shape[0], 1, 40, 40, 40)) X_train_3d[:, :, 6:34, 6:34, 17:23] = X_train_2d X_train_3d = np.array(X_train_3d, dtype = np.float32) X_test_2d = X_test.reshape(X_test.shape[0], 1, 28, 28, 1) X_test_3d = np.zeros((X_test.shape[0], 1, 40, 40, 40)) X_test_3d[:, :, 6:34, 6:34, 17:23] = X_test_2d X_test_3d = np.array(X_test_3d, dtype = np.float32) input_var = T.tensor5('inputs') network = build_cnn(input_var, 100) network_output = lasagne.layers.get_output(network) get_rotated = theano.function([input_var], network_output) # Start Creating training 3d MNIST Data image_result_list = [] for i in range(X_train_3d.shape[0] // 100): x_degree = np.random.randint(-45, 45, 100).reshape(100,) y_degree = np.random.randint(-45, 45, 100).reshape(100,) z_degree = np.random.randint(-45, 45, 100).reshape(100,) lasagne.layers.set_all_param_values(network, [np.array(x_degree,dtype=np.float32), np.array(y_degree,dtype=np.float32), np.array(z_degree,dtype=np.float32)]) image_result = get_rotated(X_train_3d[100 * i : 100 * (i+1)])
input_shape = (None, None, Convlayersize[-4], Convlayersize[-4], Convlayersize[-4]) filter_shape = (Channel[-4], Channel[-5], kernal[-4], kernal[-4], kernal[-4]) GlX = sigmoid( conv(Gl4, wx, filter_shape=filter_shape, input_shape=input_shape, conv_mode='deconv')) return GlX X = T.tensor5() Z = T.matrix() gX = gen(Z, *gen_params) print 'COMPILING' t = time() # _train_g = theano.function([X, Z, Y], cost, updates=g_updates) # _train_d = theano.function([X, Z, Y], cost, updates=d_updates) _gen = theano.function([Z], gX) print '%.2f seconds to compile theano functions' % (time() - t) # trX, trY, ntrain = load_shapenet_train() n = 10 nbatch = 10 rng = np.random.RandomState(int(time()))
def interpolate(config_file, *args): net = DeepLTE(config_file) mon = nn.monitor.Monitor(config_file) mon.dump_model(net) X__ = T.tensor5('input', theano.config.floatX) X = X__[0] X_nodes = X[net.nodes] X_targets = X[net.targets] X_recon = T.concatenate((X_nodes, X_targets)) X_ = theano.shared( np.zeros(( 1, net.num_frames, ) + net.input_tensor_shape[1:], 'float32'), 'input_placeholder') dm = VideoManager(config_file, X_) # training costs net.set_training_status(True) lr_ = theano.shared(net.learning_rate, 'learning rate') updates, cost, psnr = net.learn(X_nodes, X_recon, lambda1=1., lambda2=1e-3, lambda3=1e-4, learning_rate=lr_) optimize = nn.compile([], [cost, psnr], updates=updates, givens={X__: X_}, name='optimize deepLTE') # testing cost net.set_training_status(False) W = T.scalar('pos', 'float32') new_frame = net(X_nodes, W) test = nn.compile([W], new_frame, givens={X__: X_}, name='test deepLTE') # training scheme last_seq_cost = -np.inf for seq, _ in enumerate(dm.get_batches()): print('Interpolating sequence %d' % (seq + 1)) iteration = 0 cost = 1e10 start_time = time.time() while iteration < net.n_epochs: iteration += 1 _recon_cost, _psnr = optimize() if np.isnan(_recon_cost) or np.isinf(_recon_cost): raise ValueError('Training failed.') cost = _recon_cost if iteration % net.validation_frequency == 0: mon.plot('reconstruction train cost %d' % (seq + 1), _recon_cost) mon.plot('train psnr %d' % (seq + 1), _psnr) mon.plot('time elapsed %d' % (seq + 1), (time.time() - start_time) / 60.) mon.flush() mon.tick() if cost < last_seq_cost: break for idx, pos in enumerate(np.array(net.interps, dtype='float32')): img = test(pos) mon.save_image('%d %d' % (seq + 1, idx), img, dm.unnormalize) mon.flush() if seq == 0: last_seq_cost = cost
def get_rcnn_kwargs(input_width, input_height, num_channels, conv_num_filters, conv_filter_size, pool_size, rec_num_units, p_dropout=0.5, learning_rate=1e-3, recurrent_layer_type=MILSTMLayer, **kwargs): # Select number of gradient steps to be used for BPTT if 'gradient_steps' not in kwargs: gradient_steps = -1 else: gradient_steps = kwargs['gradient_steps'] # Input shape is (batch_size, sequence_length, num_channels, # input_height, input_width) input_shape = (None, None, num_channels, input_height, input_width,) # Define input layer l_in = lasagne.layers.InputLayer(input_shape) # symbolic variable for the input of the layer input_var = l_in.input_var # symbolic variables representing the size of the layer bsize, seqlen, _, _, _ = input_var.shape # Reshape layer for convolutional stage l_reshape_1 = lasagne.layers.ReshapeLayer( l_in, (-1, num_channels, input_height, input_width)) # Convolutional stage l_conv = lasagne.layers.Conv2DLayer(l_reshape_1, conv_num_filters, conv_filter_size, nonlinearity=None, b=None) # Add bias and nonlinearities (so that they don't get included # in the computation of the gradient in the inverse layer # np.random.seed(84) # bias = np.random.rand(conv_num_filters).astype(floatX) % 0.1 l_conv_b = lasagne.layers.BiasLayer(l_conv) # , b=bias) l_conv_nl = lasagne.layers.NonlinearityLayer(l_conv_b, nonlinearity=nonlinearities.sigmoid) # Max pooling layer l_pool = lasagne.layers.MaxPool2DLayer(l_conv_nl, pool_size=pool_size) # Dropout layer l_dropout = lasagne.layers.DropoutLayer(l_pool, p_dropout) # output shape for the convolutional stage out_shape = l_pool.output_shape # Reshape layer for recurrent stage # the shape is now (batch_size, sequence_length, num_channels * input_width * # input_height) l_reshape_2 = lasagne.layers.ReshapeLayer( l_dropout, (bsize, seqlen, np.prod(out_shape[1:]))) # Recurrent stage l_rec = recurrent_layer_type( l_reshape_2, rec_num_units) # Decoding stage l_rec_d = recurrent_layer_type( l_rec, num_units=l_rec.input_shapes[0][-1]) # Reshape output for decoding convolutional layers l_reshape_3 = lasagne.layers.ReshapeLayer( l_rec_d, (bsize * seqlen, ) + out_shape[1:]) # Invert pool layer l_inv_pool = lasagne.layers.InverseLayer(l_reshape_3, l_pool) # Invert convolutional layers l_inv_conv = lasagne.layers.InverseLayer(l_inv_pool, l_conv) # Add nonlinearity # l_inv_conv_nl = l_inv_conv l_inv_conv_nl = lasagne.layers.NonlinearityLayer(l_inv_conv, nonlinearity=nonlinearities.sigmoid) # Output layer (with same shape as the input) l_out = lasagne.layers.ReshapeLayer( l_inv_conv_nl, (bsize, seqlen, num_channels, input_height, input_width)) target = T.tensor5() # Mask output # Are binary masks necessary? deterministic_out = myMask( lasagne.layers.get_output(l_out, deterministic=True), input_var) stochastic_out = myMask(lasagne.layers.get_output(l_out), input_var) # Get parameters params = lasagne.layers.get_all_params(l_out, trainable=True) # Compute training and validation losses stochastic_loss = lasagne.objectives.binary_crossentropy( stochastic_out, target).mean() stochastic_loss.name = 'stochastic loss' deterministic_loss = lasagne.objectives.binary_crossentropy( deterministic_out, target).mean() deterministic_loss.name = 'deterministic loss' # Compute updates updates = lasagne.updates.rmsprop( stochastic_loss, params, learning_rate=learning_rate) train_loss = [stochastic_loss] valid_loss = [deterministic_loss] return dict(l_in=l_in, l_out=l_out, train_loss=train_loss, valid_loss=valid_loss, # input_var=input_var, target=target, updates=updates, predictions=deterministic_out, gradient_steps=gradient_steps, model_type='RNN')
def __init__(self, image_shp, layer_params): """ Для инициализации требуется указать размер входных изображений и параметры для каждого сверточного слоя. Для каждого слоя свертки автоматически генерируется слой транспонированной свертки как обратный. Пример: Пусть cl_1, cl_2 - сверточные слои. Тогда будет построена сеть X->cl_1->cl_2->dcl_2->dcl_1->Y, где dcl_i = (cl_i)^{-1}. В сети используются skip-connections: если X проходит через cl_i, то на выходе получается X'. Тогда на вход dcl_i подается результат, полученный на предыдущем слое + X'. Такая архитектура помогает решить проблему исчезающего градиента. :param image_shp: размер входных изображений (None, 1, 3, h, w) Если 0-ой элемент != None, то он все равно заменится :param layer_params: список параметров для conv слоев - число и размер фильтра (count, size) - border 'valid' | 'full' | (d,h,w) - subsample (d,h,w) - dilation пока не используется """ # размер входных изображений хранится отдельно: # это нужно для валидации загруженного объекта Nnet из .save файла self.input_shape = image_shp # заполним значениями по умолчанию None-ы # layer_params = self.complete_layer_params(layer_params) # параметры, которые уже известны # self.filter_shps, self.border_shps, \ # self.subsamples, self.dilations = list(zip(*layer_params)) # эти параметры будут вычисляться self.image_shps = [] self.c_layers = [] self.dc_layers = [] # символьные переменные input = T.tensor5('input', dtype=th.config.floatX) expected = T.tensor5('expected', dtype=th.config.floatX) self.x = input self.y = expected # инициализация слоев свертки x = input i_shp = (None, *image_shp[1:]) # for (count, size), border, subsample, dilation in layer_params: for param in layer_params: print(str(param)) self.image_shps.append(i_shp) count, size = param['filter_shape'] f_shp = (count, i_shp[1], i_shp[2], size, size) param['filter_shape'] = f_shp param['image_shape'] = i_shp # cl = ConvLayer3D(np.random, x, f_shp, i_shp, border, subsample) cl = ConvLayer3D(np.random, x, **param) self.c_layers.append(cl) x = cl.output i_shp = cl.output_shp() # инициализация слоев транспонированной свертки # for i, (((count, size), border, subsample, dilation), i_shp, c_l) \ # in enumerate(zip(reversed(layer_params), # reversed(self.image_shps), # reversed(self.c_layers))): for i, (param, c_l) in enumerate( zip(reversed(layer_params), reversed(self.c_layers))): # f_shp = (count, i_shp[1], i_shp[2], size, size) # dcl = DeconvLayer3D(np.random, x, f_shp, i_shp, border, subsample) dcl = DeconvLayer3D(np.random, x, **param) self.dc_layers.append(dcl) x = dcl.output + c_l.input \ if i < len(layer_params) - 1 else dcl.output # результат применения сети к случайному изображению result = T.nnet.relu(x) self.func = th.function([input], result) cost = T.square(result - expected).mean() / 2 self.cost = cost # результат применения сети к изображению, # для которого известен желаемый результат, и оценка self.check = th.function( [input, expected], [result, cost], ) params = [l.W for l in self.c_layers + self.dc_layers] \ + [l.b for l in self.c_layers + self.dc_layers] self.params = params grads = T.grad(cost, params) self.grads = grads self.train_stop = False print('inited')