def test_max_pool_3d_3D(self): rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1, 1), (3, 2, 1)) imval = rng.rand(4, 5, 6) images = tensor.dtensor3() for maxpoolshp, ignore_border, mode in product(maxpoolshps, [True, False], ['max', 'sum', 'average_inc_pad', 'average_exc_pad']): # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border numpy_output_val = self.numpy_max_pool_nd(imval, maxpoolshp, ignore_border, mode=mode) output = pool_3d(images, maxpoolshp, ignore_border, mode=mode) output_val = function([images], output)(imval) utt.assert_allclose(output_val, numpy_output_val) def mp(input): return pool_3d(input, maxpoolshp, ignore_border, mode=mode) utt.verify_grad(mp, [imval], rng=rng)
def test_max_pool_3d_3D_deprecated_interface(self): rng = np.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1, 1), (3, 2, 1)) imval = rng.rand(4, 5, 6) images = tensor.dtensor3() for maxpoolshp, ignore_border, mode in product( maxpoolshps, [True, False], ["max", "sum", "average_inc_pad", "average_exc_pad"], ): # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border numpy_output_val = self.numpy_max_pool_nd(imval, maxpoolshp, ignore_border, mode=mode) output = pool_3d( input=images, ds=maxpoolshp, ignore_border=ignore_border, st=maxpoolshp, padding=(0, 0, 0), mode=mode, ) output_val = function([images], output)(imval) utt.assert_allclose(output_val, numpy_output_val) def mp(input): return pool_3d(input, maxpoolshp, ignore_border, mode=mode)
def pool(self, input, window, mode, stride, pad, autopad): if mode == 'max': mode = 'max' elif mode == 'sum': mode = 'sum' elif mode == 'avg': mode = 'average_exc_pad' elif mode == 'avgpad': mode = 'average_inc_pad' else: mode = 'sum' if input.ndim == 4: return P.pool_2d(input=input, ws=window, ignore_border=not autopad, stride=stride, pad=pad, mode=mode) elif input.ndim == 5: return P.pool_3d(input=input, ws=window, ignore_border=not autopad, stride=stride, pad=pad, mode=mode) else: basic.defaultreturn()
def test_DownsampleFactorMax(self): rng = numpy.random.RandomState(utt.fetch_seed()) # maxpool, input size examples = ( ((2,), (16,)), ((2,), (4, 16,)), ((2,), (4, 2, 16,)), ((1, 1), (4, 2, 16, 16)), ((2, 2), (4, 2, 16, 16)), ((3, 3), (4, 2, 16, 16)), ((3, 2), (4, 2, 16, 16)), ((3, 2, 2), (3, 2, 16, 16, 16)), ((2, 3, 2), (3, 2, 16, 16, 16)), ((2, 2, 3), (3, 2, 16, 16, 16)), ((2, 2, 3, 2), (3, 2, 6, 6, 6, 5)), ) for example, ignore_border, mode in product(examples, [True, False], ['max', 'sum', 'average_inc_pad', 'average_exc_pad']): (maxpoolshp, inputsize) = example imval = rng.rand(*inputsize) images = theano.shared(imval) # Pure Numpy computation numpy_output_val = self.numpy_max_pool_nd(imval, maxpoolshp, ignore_border, mode=mode) # The pool_2d or pool_3d helper methods if len(maxpoolshp) == 2: output = pool_2d(images, maxpoolshp, ignore_border, mode=mode) f = function([], [output, ]) output_val = f() utt.assert_allclose(output_val, numpy_output_val) elif len(maxpoolshp) == 3: output = pool_3d(images, maxpoolshp, ignore_border, mode=mode) f = function([], [output, ]) output_val = f() utt.assert_allclose(output_val, numpy_output_val) # Pool op maxpool_op = Pool(ndim=len(maxpoolshp), ignore_border=ignore_border, mode=mode)(images, maxpoolshp) output_shape = Pool.out_shape(imval.shape, maxpoolshp, ndim=len(maxpoolshp), ignore_border=ignore_border) utt.assert_allclose(numpy.asarray(output_shape), numpy_output_val.shape) f = function([], maxpool_op) output_val = f() utt.assert_allclose(output_val, numpy_output_val)
def pool_3d(x, ws=(2,2,2), ignore_border=True, stride=None, pad=(0, 0, 0), mode='max'): """ Pooling 3 dimension along the last 3 dimensions of input, support for any dimensional input with ndim>=3. :param x: :param ws: :param ignore_border: :param stride: :param pad: :param mode: :return: """ return pool.pool_3d(x, ws=ws, ignore_border=ignore_border, stride=stride, pad=pad, mode=mode)
def pool_3d(x, ws=(2, 2, 2), ignore_border=True, stride=None, pad=(0, 0, 0), mode='max'): return pool.pool_3d(x, ws=ws, ignore_border=ignore_border, stride=stride, pad=pad, mode=mode)
def __init__(self, _net, _input): # Save config information to its layer self.Ws = _net.layer_opts['pool3D_filter_size'] self.ignore_border = _net.layer_opts['pool3D_ignore_border'] self.stride = _net.layer_opts['pool3D_stride'] self.padding = _net.layer_opts['pool3D_padding'] self.mode = _net.layer_opts['pool3D_mode'] self.output = pool_3d(input=_input, ws=self.Ws, ignore_border=self.ignore_border, stride=self.stride, pad=self.padding, mode=self.mode)
def pool3d(x, pool_size=(2, 2), strides=None, border_mode=(0, 0, 0), ignore_border=True, mode='max'): """ Parameters ---------- x : N-D theano tensor of input images Input images. Max pooling will be done over the 2 last dimensions. pool_size : tuple of length 3 Factor by which to downscale (vertical ds, horizontal ds). (2,2,2) will halve the image in each dimension. strides : tuple of 3 ints Stride size, which is the number of shifts over rows/cols to get the next pool region. If st is None, it is considered equal to ds (no overlap on pooling regions). ignore_border : bool (default None, will print a warning and set to False) When True, (5,5,5) input with ds=(2,2,2) will generate a (2,2,2) output. (3,3,3) otherwise. padding : tuple of 3 ints (pad_h, pad_w, pad_l), pad zeros to extend beyond four borders of the images, pad_h is the size of the top and bottom margins, and pad_w is the size of the left and right margins. mode : {'max', 'avg'} Operation executed on each window. `max` or `average` Note ---- This pooling algorithm has non-deterministic behaviour on cuDNN """ # ====== convert to theano formated shapes ====== # input_shape = get_shape(x) # pool_size x, pool_size, strides, border_mode, mode = __validate_pool_stride_border( x, pool_size, strides, border_mode, mode, ndim=3) x = __img_theano_format(x) # ====== On GPU: use CuDNN ====== # pool_out = pool.pool_3d(x, ws=pool_size, stride=strides, ignore_border=ignore_border, pad=(0, 0, 0) if isinstance(border_mode, str) else border_mode, mode=mode) # ====== Estimate output shape ====== # pool_out = __img_tensorflow_format(pool_out) output_shape = get_pool_output_shape(input_shape, pool_size, ignore_border=ignore_border, strides=strides, pad=border_mode) add_shape(pool_out, tuple(output_shape)) return pool_out
def set_inpt(self, inpt, inpt_dropout, mini_batch_size): self.inpt = inpt.reshape(self.image_shape) ## conv3d takes as input (Batch, Z, n_feature maps, Y, X) ## we feed it (Batch, n_feature_maps, X, Y, Z) so we need to shuffle it #OUTPUTS: (N, Z- z_filter + 1, n_features, Y - y_filter + 1, X - x_filter + 1) conv_out = conv3d( signals=self.inpt.dimshuffle(0, 4, 1, 3, 2), filters=self.w.dimshuffle(0, 4, 1, 3, 2), filters_shape=[self.filter_shape[idx] for idx in [0, 4, 1, 3, 2]], signals_shape=[self.image_shape[idx] for idx in [0, 4, 1, 3, 2]]) conv_out = conv_out.dimshuffle(0, 2, 4, 3, 1) self.pooled_out = pool.pool_3d(input=conv_out, ws=self.poolsize, ignore_border=True) self.activation = self.pooled_out + self.b.dimshuffle( 'x', 0, 'x', 'x', 'x') ##dimshuffle broadcasts the bias vector ## across the 3D tensor dimvs self.output = self.activation_fn(self.activation) self.output_dropout = self.output # no dropout in the convolutional layers
def set_output(self): shuffled_output = self._prev_layer.output.dimshuffle(0, 2, 1, 3, 4) pooled_output = pool.pool_3d(shuffled_output, ds=self._pool_size, ignore_border=True, padding=self._padding, mode='average_inc_pad') reshape_pooled = tensor.reshape( pooled_output, (self._input_shape[0], self._input_shape[2])) output1 = tensor.dot(reshape_pooled, self.W1.val) if self.bias: output1 += self.b1.val output1 = relu(output1) output2 = tensor.dot(output1, self.W2.val) if self.bias: output2 += self.b2.val output2 = sigmoid(output2) se_out = tensor.reshape( output2, (self._input_shape[0], 1, self._input_shape[2], 1, 1)) self._output = self._prev_layer.output * se_out
def test_max_pool_3d_3D(self): rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1, 1), (3, 2, 1)) imval = rng.rand(4, 5, 6) images = tensor.dtensor3() for maxpoolshp, ignore_border, mode in product( maxpoolshps, [True, False], ['max', 'sum', 'average_inc_pad', 'average_exc_pad']): # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border numpy_output_val = self.numpy_max_pool_nd(imval, maxpoolshp, ignore_border, mode=mode) output = pool_3d(images, maxpoolshp, ignore_border, mode=mode) output_val = function([images], output)(imval) utt.assert_allclose(output_val, numpy_output_val) def mp(input): return pool_3d(input, maxpoolshp, ignore_border, mode=mode) utt.verify_grad(mp, [imval], rng=rng)
def mp(input): return pool_3d(input, maxpoolshp, ignore_border, mode=mode)
def test_DownsampleFactorMax(self): rng = numpy.random.RandomState(utt.fetch_seed()) # maxpool, input size examples = ( ((2, ), (16, )), ((2, ), ( 4, 16, )), ((2, ), ( 4, 2, 16, )), ((1, 1), (4, 2, 16, 16)), ((2, 2), (4, 2, 16, 16)), ((3, 3), (4, 2, 16, 16)), ((3, 2), (4, 2, 16, 16)), ((3, 2, 2), (3, 2, 16, 16, 16)), ((2, 3, 2), (3, 2, 16, 16, 16)), ((2, 2, 3), (3, 2, 16, 16, 16)), ((2, 2, 3, 2), (3, 2, 6, 6, 6, 5)), ) for example, ignore_border, mode in product( examples, [True, False], ['max', 'sum', 'average_inc_pad', 'average_exc_pad']): (maxpoolshp, inputsize) = example imval = rng.rand(*inputsize) images = theano.shared(imval) # Pure Numpy computation numpy_output_val = self.numpy_max_pool_nd(imval, maxpoolshp, ignore_border, mode=mode) # The pool_2d or pool_3d helper methods if len(maxpoolshp) == 2: output = pool_2d(images, maxpoolshp, ignore_border, mode=mode) f = function([], [ output, ]) output_val = f() utt.assert_allclose(output_val, numpy_output_val) elif len(maxpoolshp) == 3: output = pool_3d(images, maxpoolshp, ignore_border, mode=mode) f = function([], [ output, ]) output_val = f() utt.assert_allclose(output_val, numpy_output_val) # Pool op maxpool_op = Pool(ndim=len(maxpoolshp), ignore_border=ignore_border, mode=mode)(images, maxpoolshp) output_shape = Pool.out_shape(imval.shape, maxpoolshp, ndim=len(maxpoolshp), ignore_border=ignore_border) utt.assert_allclose(numpy.asarray(output_shape), numpy_output_val.shape) f = function([], maxpool_op) output_val = f() utt.assert_allclose(output_val, numpy_output_val)
def __init__(self, rng, input, signal_shape, filter_shape, poolsize=(2, 2, 2), stride=None, if_pool=False, if_hidden_pool=False, act=None, share_with=None, tied=None, border_mode='valid'): self.input = input if share_with: self.W = share_with.W self.b = share_with.b self.W_delta = share_with.W_delta self.b_delta = share_with.b_delta elif tied: self.W = tied.W.dimshuffle(1,0,2,3) self.b = tied.b self.W_delta = tied.W_delta.dimshuffle(1,0,2,3) self.b_delta = tied.b_delta else: fan_in = np.prod(filter_shape[1:]) poolsize_size = np.prod(poolsize) if poolsize else 1 fan_out = (filter_shape[0] * np.prod(filter_shape[2:]) / poolsize_size) W_bound = np.sqrt(6. / (fan_in + fan_out)) self.W = theano.shared( np.asarray( rng.uniform(low=-W_bound, high=W_bound, size=filter_shape), dtype=theano.config.floatX ), borrow=True ) b_values = np.zeros((filter_shape[0],), dtype=theano.config.floatX) self.b = theano.shared(value=b_values, borrow=True) self.W_delta = theano.shared( np.zeros(filter_shape, dtype=theano.config.floatX), borrow=True ) self.b_delta = theano.shared(value=b_values, borrow=True) # convolution conv_out = nnet.conv3d( input, filters=self.W, input_shape=signal_shape, filter_shape=filter_shape, border_mode=border_mode) #if poolsize: if if_pool: conv_out = conv_out.dimshuffle(0,2,1,3,4) #maxpool3d works on last 3 dimesnions pooled_out = pools.pool_3d( input=conv_out, ds=poolsize, ignore_border=True) tmp_out = pooled_out.dimshuffle(0,2,1,3,4) tmp = tmp_out + self.b.dimshuffle('x', 'x', 0, 'x', 'x') elif if_hidden_pool: pooled_out = pools.pool_2d( input=conv_out, ds=poolsize[:2], st=stride, ignore_border=True) tmp = pooled_out + self.b.dimshuffle('x', 'x', 0, 'x', 'x') else: tmp = conv_out + self.b.dimshuffle('x', 'x', 0, 'x', 'x') if act == 'tanh': self.output = T.tanh(tmp) elif act == 'sigmoid': self.output = nnet.sigmoid(tmp) elif act == 'relu': # self.output = tmp * (tmp>0) self.output = 0.5 * (tmp + abs(tmp)) + 1e-9 elif act == 'softplus': # self.output = T.log2(1+T.exp(tmp)) self.output = nnet.softplus(tmp) else: self.output = tmp self.get_activation = theano.function( [self.input], self.output, updates=None, name='get hidden activation') # store parameters of this layer self.params = [self.W, self.b] self.deltas = [self.W_delta, self.b_delta]
def __init__(self, signal_shape, filter_shape, poolsize, activation=None): rng = np.random.RandomState(None) dtensor5 = T.TensorType('float32', (False,)*5) self.inputs = dtensor5(name='inputs') self.image_shape = signal_shape self.batchsize = signal_shape[0] self.in_channels = signal_shape[2] self.in_depth = signal_shape[1] self.in_width = signal_shape[4] self.in_height = signal_shape[3] self.flt_channels = filter_shape[0] self.flt_time = filter_shape[1] self.flt_width = filter_shape[4] self.flt_height = filter_shape[3] self.activation = activation self.hidden_layer=ConvolutionLayer3D(rng, input=self.inputs, signal_shape=signal_shape, filter_shape=filter_shape, act=activation, border_mode='full', if_hidden_pool=False) self.hidden_image_shape = (self.batchsize, self.in_depth, self.flt_channels, self.in_height+self.flt_height-1, self.in_width+self.flt_width-1) self.hidden_pooled_image_shape = (self.batchsize, self.in_depth/2, self.flt_channels, (self.in_height+self.flt_height-1)/2, (self.in_width+self.flt_width-1)/2) self.hidden_filter_shape = (self.in_channels, self.flt_time, self.flt_channels, self.flt_height, self.flt_width) self.recon_layer=ConvolutionLayer3D(rng, input=self.hidden_layer.output, signal_shape=self.hidden_image_shape, filter_shape=self.hidden_filter_shape, act=activation, border_mode='valid') self.layers = [self.hidden_layer, self.recon_layer] self.params = sum([layer.params for layer in self.layers], []) L=T.sum(T.pow(T.sub(self.recon_layer.output, self.inputs), 2), axis=(1,2,3,4)) self.cost = 0.5*T.mean(L) self.grads = T.grad(self.cost, self.params) self.updates = adadelta_updates(self.params, self.grads, rho=0.95, eps=1e-6) self.train = theano.function( [self.inputs], self.cost, updates=self.updates, name = "train cae model" ) self.activation = pools.pool_3d( input=self.hidden_layer.output.dimshuffle(0,2,1,3,4), ds=poolsize, ignore_border=True) self.activation = self.activation.dimshuffle(0,2,1,3,4) self.get_activation = theano.function( [self.inputs], self.activation, updates=None, name='get hidden activation')
def set_output(self): pooled_out = pool.pool_3d(input=self._prev_layer.output, ds=self._pool_size, ignore_border=True, padding=self._padding) self._output = pooled_out