def make_node(self, rstate, size): # error checking slightly redundant here, since # this op should not be called directly. # # call through MRG_RandomStreams instead. broad = [] for i in range(self.output_type.ndim): broad.append(tensor.extract_constant(size[i]) == 1) output_type = self.output_type.clone(broadcastable=broad)() rstate = as_gpuarray_variable(rstate, infer_context_name(rstate)) return Apply(self, [rstate, size], [rstate.type(), output_type])
def make_node(self, rstate, size): # error checking slightly redundant here, since # this op should not be called directly. # # call through MRG_RandomStreams instead. broad = [] for i in range(self.output_type.ndim): broad.append(tensor.extract_constant(size[i]) == 1) output_type = self.output_type.clone(broadcastable=broad)() rstate = as_gpuarray_variable(rstate, infer_context_name(rstate)) return Apply(self, [rstate, size], [rstate.type(), output_type])
def out_shape(imgshape, ws=None, ignore_border=False, stride=None, pad=None, ndim=2, ds=None, st=None, padding=None): """ Return the shape of the output from this op, for input of given shape and flags. Parameters ---------- imgshape : tuple, list, or similar of integer or scalar Theano variable The shape of a tensor of images. The last N elements are interpreted as the number of rows, and the number of cols. ws : list or tuple of N ints Downsample factor over rows and column. ws indicates the pool region size. ignore_border : bool If ws doesn't divide imgshape, do we include an extra row/col/slice of partial downsampling (False) or ignore it (True). stride : list or tuple of N ints or None Stride size, which is the number of shifts over rows/cols/slices to get the next pool region. If stride is None, it is considered equal to ws (no overlap on pooling regions). pad : tuple of N ints or None For each downsampling dimension, this specifies the number of zeros to add as padding on both sides. For 2D and (pad_h, pad_w), pad_h specifies the size of the top and bottom margins, pad_w specifies the size of the left and right margins. No padding is added if pad is None. ndim : int The number of pooling dimensions N. The default is 2. ds *deprecated*, use parameter ws instead. st *deprecated*, use parameter st instead. padding *deprecated*, use parameter pad instead. Returns ------- list The shape of the output from this op, for input of given shape. This will have the same length as imgshape, but with last N elements reduced as per the downsampling & ignore_border flags. """ # check for deprecated parameter names if ds is not None: if ws is not None: raise ValueError( "You can't provide a tuple value to both 'ws' and 'ds'." " Please provide a value only to 'ws'.") else: warnings.warn( "DEPRECATION: the 'ds' parameter is not going to exist" " anymore as it is going to be replaced by the parameter" " 'ws'.", stacklevel=2) ws = ds elif ds is None and ws is None: raise ValueError( "You must provide a tuple value for the window size.") if st is not None: if stride is not None: raise ValueError( "You can't provide a tuple value to both 'st and 'stride'." " Please provide a value only to 'stride'.") else: warnings.warn( "DEPRECATION: the 'st' parameter is not going to exist" " anymore as it is going to be replaced by the parameter" " 'stride'.", stacklevel=2) stride = st if padding is not None: zero_pad = (0, ) * ndim if pad not in {None, zero_pad}: raise ValueError( "You can't provide a tuple value to both 'padding' and pad." " Please provide a value only to pad.") else: warnings.warn( "DEPRECATION: the 'padding' parameter is not going to" " exist anymore as it is going to be replaced by the" " parameter 'pad'.", stacklevel=2) pad = padding if ndim is None: ndim = 2 assert ndim > 0 if len(imgshape) < ndim: raise TypeError( 'imgshape must have at least {} dimensions'.format(ndim)) if stride is None: stride = ws if pad is None: pad = (0, ) * ndim patch_shape = tuple( tensor.extract_constant(imgshape[-ndim + i]) + pad[i] * 2 for i in xrange(ndim)) def compute_out(v, downsample, stride): if ignore_border: if downsample == stride: return v // stride else: out = (v - downsample) // stride + 1 if isinstance(out, theano.Variable): return tensor.maximum(out, 0) else: return numpy.maximum(out, 0) else: if isinstance(v, theano.Variable): return tensor.switch( tensor.ge(stride, downsample), (v - 1) // stride + 1, tensor.maximum(0, (v - 1 - downsample) // stride + 1) + 1) elif stride >= downsample: return (v - 1) // stride + 1 else: return max(0, (v - 1 - downsample + stride) // stride) + 1 out_shape = [ compute_out(patch_shape[i], ws[i], stride[i]) for i in xrange(ndim) ] rval = list(imgshape[:-ndim]) + out_shape return rval
def out_shape(imgshape, ds, ignore_border=False, st=None, padding=(0, 0)): """ Return the shape of the output from this op, for input of given shape and flags. Parameters ---------- imgshape : tuple, list, or similar of integer or scalar Theano variable The shape of a tensor of images. The last two elements are interpreted as the number of rows, and the number of cols. ds : list or tuple of two ints Downsample factor over rows and columns this parameter indicates the size of the pooling region. st : list or tuple of two ints The stride size. This is the distance between the pooling regions. If it's set to None, it equals ds. ignore_border : bool If ds doesn't divide imgshape, do we include an extra row/col of partial downsampling (False) or ignore it (True). padding : tuple of two ints (pad_h, pad_w), 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. Returns ------- list The shape of the output from this op, for input of given shape. This will have the same length as imgshape, but with last two elements reduced as per the downsampling & ignore_border flags. """ if len(imgshape) < 2: raise TypeError('imgshape must have at least two elements ' '(rows, cols)') if st is None: st = ds r, c = imgshape[-2:] r = tensor.extract_constant(r) c = tensor.extract_constant(c) if padding[0]: r += padding[0] * 2 if padding[1]: c += padding[1] * 2 if ignore_border: if ds[0] == st[0]: nr = r // st[0] else: out_r = (r - ds[0]) // st[0] + 1 if isinstance(r, theano.Variable): nr = tensor.maximum(out_r, 0) else: nr = numpy.maximum(out_r, 0) if ds[1] == st[1]: nc = c // st[1] else: out_c = (c - ds[1]) // st[1] + 1 if isinstance(c, theano.Variable): nc = tensor.maximum(out_c, 0) else: nc = numpy.maximum(out_c, 0) else: if isinstance(r, theano.Variable): nr = tensor.switch( tensor.ge(st[0], ds[0]), (r - 1) // st[0] + 1, tensor.maximum(0, (r - 1 - ds[0]) // st[0] + 1) + 1) elif st[0] >= ds[0]: nr = (r - 1) // st[0] + 1 else: nr = max(0, (r - 1 - ds[0] + st[0]) // st[0]) + 1 if isinstance(c, theano.Variable): nc = tensor.switch( tensor.ge(st[1], ds[1]), (c - 1) // st[1] + 1, tensor.maximum(0, (c - 1 - ds[1]) // st[1] + 1) + 1) elif st[1] >= ds[1]: nc = (c - 1) // st[1] + 1 else: nc = max(0, (c - 1 - ds[1] + st[1]) // st[1]) + 1 rval = list(imgshape[:-2]) + [nr, nc] return rval
def out_shape(imgshape, ds, ignore_border=False, st=None, padding=(0, 0)): """ Return the shape of the output from this op, for input of given shape and flags. Parameters ---------- imgshape : tuple, list, or similar of integer or scalar Theano variable The shape of a tensor of images. The last two elements are interpreted as the number of rows, and the number of cols. ds : list or tuple of two ints Downsample factor over rows and columns this parameter indicates the size of the pooling region. st : list or tuple of two ints The stride size. This is the distance between the pooling regions. If it's set to None, it equals ds. ignore_border : bool If ds doesn't divide imgshape, do we include an extra row/col of partial downsampling (False) or ignore it (True). padding : tuple of two ints (pad_h, pad_w), 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. Returns ------- list The shape of the output from this op, for input of given shape. This will have the same length as imgshape, but with last two elements reduced as per the downsampling & ignore_border flags. """ if len(imgshape) < 2: raise TypeError("imgshape must have at least two elements " "(rows, cols)") if st is None: st = ds r, c = imgshape[-2:] r = tensor.extract_constant(r) c = tensor.extract_constant(c) if padding[0]: r += padding[0] * 2 if padding[1]: c += padding[1] * 2 if ignore_border: if ds[0] == st[0]: nr = r // st[0] else: out_r = (r - ds[0]) // st[0] + 1 if isinstance(r, theano.Variable): nr = tensor.maximum(out_r, 0) else: nr = numpy.maximum(out_r, 0) if ds[1] == st[1]: nc = c // st[1] else: out_c = (c - ds[1]) // st[1] + 1 if isinstance(c, theano.Variable): nc = tensor.maximum(out_c, 0) else: nc = numpy.maximum(out_c, 0) else: if isinstance(r, theano.Variable): nr = tensor.switch( tensor.ge(st[0], ds[0]), (r - 1) // st[0] + 1, tensor.maximum(0, (r - 1 - ds[0]) // st[0] + 1) + 1 ) elif st[0] >= ds[0]: nr = (r - 1) // st[0] + 1 else: nr = max(0, (r - 1 - ds[0] + st[0]) // st[0]) + 1 if isinstance(c, theano.Variable): nc = tensor.switch( tensor.ge(st[1], ds[1]), (c - 1) // st[1] + 1, tensor.maximum(0, (c - 1 - ds[1]) // st[1] + 1) + 1 ) elif st[1] >= ds[1]: nc = (c - 1) // st[1] + 1 else: nc = max(0, (c - 1 - ds[1] + st[1]) // st[1]) + 1 rval = list(imgshape[:-2]) + [nr, nc] return rval
def out_shape(imgshape, ws=None, ignore_border=False, stride=None, pad=None, ndim=2, ds=None, st=None, padding=None): """ Return the shape of the output from this op, for input of given shape and flags. Parameters ---------- imgshape : tuple, list, or similar of integer or scalar Theano variable The shape of a tensor of images. The last N elements are interpreted as the number of rows, and the number of cols. ws : list or tuple of N ints Downsample factor over rows and column. ws indicates the pool region size. ignore_border : bool If ws doesn't divide imgshape, do we include an extra row/col/slice of partial downsampling (False) or ignore it (True). stride : list or tuple of N ints or None Stride size, which is the number of shifts over rows/cols/slices to get the next pool region. If stride is None, it is considered equal to ws (no overlap on pooling regions). pad : tuple of N ints or None For each downsampling dimension, this specifies the number of zeros to add as padding on both sides. For 2D and (pad_h, pad_w), pad_h specifies the size of the top and bottom margins, pad_w specifies the size of the left and right margins. No padding is added if pad is None. ndim : int The number of pooling dimensions N. The default is 2. ds *deprecated*, use parameter ws instead. st *deprecated*, use parameter st instead. padding *deprecated*, use parameter pad instead. Returns ------- list The shape of the output from this op, for input of given shape. This will have the same length as imgshape, but with last N elements reduced as per the downsampling & ignore_border flags. """ # check for deprecated parameter names if ds is not None: if ws is not None: raise ValueError( "You can't provide a tuple value to both 'ws' and 'ds'." " Please provide a value only to 'ws'." ) else: warnings.warn( "DEPRECATION: the 'ds' parameter is not going to exist" " anymore as it is going to be replaced by the parameter" " 'ws'.", stacklevel=2 ) ws = ds elif ds is None and ws is None: raise ValueError( "You must provide a tuple value for the window size." ) if st is not None: if stride is not None: raise ValueError( "You can't provide a tuple value to both 'st and 'stride'." " Please provide a value only to 'stride'." ) else: warnings.warn( "DEPRECATION: the 'st' parameter is not going to exist" " anymore as it is going to be replaced by the parameter" " 'stride'.", stacklevel=2 ) stride = st if padding is not None: zero_pad = (0,) * ndim if pad not in {None, zero_pad}: raise ValueError( "You can't provide a tuple value to both 'padding' and pad." " Please provide a value only to pad." ) else: warnings.warn( "DEPRECATION: the 'padding' parameter is not going to" " exist anymore as it is going to be replaced by the" " parameter 'pad'.", stacklevel=2 ) pad = padding if ndim is None: ndim = 2 assert ndim > 0 if len(imgshape) < ndim: raise TypeError('imgshape must have at least {} dimensions'.format(ndim)) if stride is None: stride = ws if pad is None: pad = (0,) * ndim patch_shape = tuple(tensor.extract_constant(imgshape[-ndim + i]) + pad[i] * 2 for i in xrange(ndim)) def compute_out(v, downsample, stride): if ignore_border: if downsample == stride: return v // stride else: out = (v - downsample) // stride + 1 if isinstance(out, theano.Variable): return tensor.maximum(out, 0) else: return numpy.maximum(out, 0) else: if isinstance(v, theano.Variable): return tensor.switch(tensor.ge(stride, downsample), (v - 1) // stride + 1, tensor.maximum(0, (v - 1 - downsample) // stride + 1) + 1) elif stride >= downsample: return (v - 1) // stride + 1 else: return max(0, (v - 1 - downsample + stride) // stride) + 1 out_shape = [compute_out(patch_shape[i], ws[i], stride[i]) for i in xrange(ndim)] rval = list(imgshape[:-ndim]) + out_shape return rval
def out_shape(tensor_shape, ds, n_pool_dim=3, ignore_border=False, st=None, padding=(0, 0, 0)): """ Return the shape of the output from this op, for input of given shape and flags. Parameters ---------- tensor_shape : tuple, list, or similar of integer or scalar Theano variable The shape of a tensor of images. The last two elements are interpreted as the number of rows, and the number of cols. ds : list or tuple of two ints Downsample factor over rows and columns this parameter indicates the size of the pooling region. n_pool_dim : int The number of desired pooling dimension. st : list or tuple of two ints The stride size. This is the distance between the pooling regions. If it's set to None, it equals ds. ignore_border : bool If ds doesn't divide imgshape, do we include an extra row/col of partial downsampling (False) or ignore it (True). padding : tuple of two ints (pad_h, pad_w), 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. Returns ------- list The shape of the output from this op, for input of given shape. This will have the same length as imgshape, but with last two elements reduced as per the downsampling & ignore_border flags. """ if len(tensor_shape) < n_pool_dim: raise TypeError('tensor_shape must have at least %i elements ' % int(n_pool_dim)) if st is None: st = ds if any([n_pool_dim != n_dim for n_dim in [len(padding), len(ds), len(st)]]): raise ValueError('the length of padding, tensor_shape and' ' downsample factor must be the same.') tensor_shape_const = [tensor.extract_constant(dim) for dim in tensor_shape[-n_pool_dim:]] # add padding for i, dim_size in enumerate(tensor_shape_const): dim_size += padding[i] * n_pool_dim out_dims = [] for i in range(n_pool_dim): if ignore_border: if ds[i] == st[i]: out_dims.append(tensor_shape_const[i] // st[i]) else: out_dims.append((tensor_shape_const[i] - ds[i]) // st[i] + 1) if isinstance(out_dims[i], theano.Variable): out_dims[i] = tensor.maximum(out_dims[i], 0) else: out_dims[i] = numpy.maximum(out_dims[i], 0) else: if isinstance(tensor_shape_const[0], theano.Variable): out_dims.append(tensor.switch(tensor.ge(st[i], ds[i]), (tensor_shape_const[i] - 1) // st[i] + 1, tensor.maximum(0, (r - 1 - ds[i]) // st[i] + 1) + 1)) elif st[i] >= ds[i]: out_dims.append((tensor_shape_const[i] - 1) // st[i] + 1) else: out_dims.append( max(0, (tensor_shape_const[i] - 1 - ds[i] + st[i]) // st[i]) + 1) rval = list(tensor_shape[:-n_pool_dim]) + out_dims return rval