1 #ifndef MSHADOW_TENSOR_EXPR_EXT_H
2 #define MSHADOW_TENSOR_EXPR_EXT_H
20 template<
typename Device,
int dimdst,
int dimcast>
37 template<
typename SrcExp,
int srcdim>
55 utils::Assert( imshape[0] >= psize && imshape[1] >= psize,
"UnpackPatchToCol:image shape smaller than patch size");
63 this->
shape_[0] = o_height * o_width * num;
64 this->
shape_[1] = psize * psize * imshape[2];
74 template<
typename Device,
int dstdim>
86 const index_t o_height = ( imshape[1] - psize ) / pstride + 1;
87 const index_t o_width = ( imshape[0] - psize ) / pstride + 1;
89 utils::Assert( mat.
shape[1] == psize * psize * imshape[2],
"PackColToPatchExp: mat.shape[1] mismatch" );
101 template<
typename SrcExp,
int dimdst,
int dimsrc>
126 template<
typename SrcExp,
int dimsrc,
int a1,
int a2>
147 template<
typename EType,
typename Reducer,
int dimkeep>
163 template<
typename Reducer,
typename SrcExp,
int srcdim>
179 utils::Assert( sshape[0] >= ksize && sshape[1] >= ksize,
"pool: kernel must be smaller than image" );
190 utils::Assert( sshape[0] >= ksize && sshape[1] >= ksize,
"pool: kernel must be smaller than image" );
194 this->
shape_[1] = pshape[1];
195 this->
shape_[0] = pshape[0];
204 template<
typename Reducer,
typename Device>
233 template<
typename SrcExp,
int srcdim>
249 this->
shape_[1] += pad * 2;
250 this->
shape_[0] += pad * 2;
259 template<
typename SrcExp,
int srcdim>
277 this->
shape_[1] = cshape[1];
278 this->
shape_[0] = cshape[0];
287 this->
shape_[1] = cshape[1];
288 this->
shape_[0] = cshape[0];
299 template<
typename SrcExp,
int srcdim>
315 template<
typename Reducer,
typename SrcExp,
int srcdim>
323 utils::Assert( nsize % 2 == 1,
"ChannelPoolingExp: local size must be odd, to make it symmetric" );
334 template<
typename E,
typename R,
int d>
339 template<
typename E,
typename R,
int d>
353 template<
int dimcast,
typename Device,
int dimdst>
356 utils::Assert( src.shape[0] == shape[dimcast],
"broadcast, shape mismatch" );
376 template<
typename SrcExp,
int etype>
391 template<
typename Device,
int dstdim>
393 utils::Assert( imshape[0] >= psize && imshape[1] >= psize,
"PackColToPatch:image shape smaller than patch size");
405 template<
typename SrcExp,
int etype,
int dimdst>
419 template<
int a1,
int a2,
typename SrcExp,
int etype>
434 template<
int dimkeep,
typename SrcExp,
int etype>
449 template<
typename Reducer,
typename SrcExp,
int etype>
465 template<
typename Reducer,
typename SrcExp,
int etype>
481 template<
typename Reducer,
typename Device>
495 template<
typename SrcExp,
int etype>
509 template<
typename SrcExp,
int etype>
524 template<
typename SrcExp,
int etype>
537 template<
typename SrcExp,
int etype>
552 template<
typename Reducer,
typename SrcExp,
int etype>
565 template<
typename Device>
567 return broadcast<0>( src,
Shape2( nrow, src.shape[0] ) );
576 template<
typename SrcExp,
int etype>
578 return sumall_except_dim<0>( exp );
590 template<
typename SV,
typename Device,
typename EType,
typename Reducer,
int dimkeep>
594 MapReduceKeepHighDim<SV,Reducer,dimkeep>( dst, exp.
src_, exp.
scale_ );
598 template<
typename SV,
typename Device,
typename EType,
typename Reducer>
601 MapReduceKeepLowest<SV,Reducer>( dst, exp.
src_, exp.
scale_ );
608 template<
typename Device,
int dimdst,
int dimcast>
612 : dptr_( e.
src_.dptr ),
614 length_(e.
shape_[dimcast]){
618 return dptr_[ (y / ystride_) % length_ ];
622 const index_t ystride_, length_;
626 template<
typename Device,
int dimdst>
639 template<
typename SrcExp,
int srcdim>
645 o_height_(( i_height_ - psize_ ) / pstride_ + 1),
646 o_width_ (( i_width_ - psize_ ) / pstride_ + 1){
649 const index_t x_offset = i % psize_;
650 const index_t idivp = i / psize_;
651 const index_t y_offset = idivp % psize_;
652 const index_t c = idivp / psize_;
653 const index_t x = (j % o_width_) * pstride_ + x_offset;
654 const index_t jdivw = j / o_width_;
655 const index_t y = (jdivw % o_height_) * pstride_ + y_offset;
656 const index_t n = jdivw / o_height_;
658 if( x < i_width_ && y < i_height_ ){
659 return src_.Eval( ( n * i_channel_ + c ) * i_height_ + y, x );
666 const index_t psize_, pstride_, i_channel_, i_height_, i_width_, o_height_, o_width_;
669 template<
typename Device,
int dstdim>
675 o_width_(( e.
shape_[0] - psize_ ) / pstride_ + 1),
676 o_height_(( e.
shape_[1] - psize_ ) / pstride_ + 1){
681 const index_t y = i % i_height_;
682 const index_t idivh = i / i_height_;
683 const index_t c = idivh % i_channel_;
684 const index_t n = idivh / i_channel_;
686 const index_t py_min = y < psize_ ? 0 : (y-psize_+pstride_)/pstride_;
687 const index_t px_min = x < psize_ ? 0 : (x-psize_+pstride_)/pstride_;
688 const index_t py_max = min( (y+pstride_)/pstride_, o_height_);
689 const index_t px_max = min( (x+pstride_)/pstride_, o_width_ );
691 for(
index_t py = py_min; py < py_max; ++py ){
692 for(
index_t px = px_min; px < px_max; ++px ){
693 res += mat_[ (c * psize_ + y - py*pstride_) * psize_ + x - px*pstride_ ][ (n * o_height_ + py) * o_width_+px ];
700 const index_t psize_, pstride_, i_channel_, i_height_, o_width_, o_height_;
705 template<
typename SrcExp,
int dimdst,
int dimsrc>
712 const index_t idx = y * oshape0_ + x;
713 return src_.Eval( idx / ishape0_, idx % ishape0_ );
717 const index_t oshape0_, ishape0_;
720 template<
typename SrcExp,
int dimdst>
724 : src_(MakePlan(e.
src_)), oshape0_(e.
shape_[0]){
727 return src_.Eval( 0, y * oshape0_ + x );
736 template<
typename SrcExp,
int dimsrc,
int a1,
int a2>
740 : src_(MakePlan(e.
src_)),
755 return src_.Eval( ((((i/shape4_)*shape2_ + z) * shape3_+c) * shape4_ + n ) * shape1_ + y, j );
759 const index_t shape1_, shape2_, shape3_, shape4_;
762 template<
typename SrcExp,
int dimsrc,
int a2>
766 : src_(MakePlan(e.
src_)),
777 return src_.Eval( ( n*shape0_ + x ) * shape1_ + y , z );
781 const index_t shape0_, shape1_, shape2_;
786 template<
typename Reducer,
typename SrcExp,
int srcdim>
795 const index_t py = i % new_height_;
796 const index_t y_start = py * kstride_;
797 const index_t y_end = min( y_start + ksize_, src_height_ );
799 const index_t x_start = px * kstride_;
800 const index_t x_end = min( x_start + ksize_, src_width_ );
801 const index_t c = i / new_height_;
803 real_t res = Reducer::kInitV;
804 for (
index_t y = y_start; y < y_end; ++y) {
805 for (
index_t x = x_start; x < x_end; ++x) {
806 Reducer::Reduce( res, src_.Eval( c*src_height_+y, x ) );
813 const index_t ksize_, kstride_;
814 const index_t src_height_, src_width_;
818 template<
typename Reducer,
typename Device>
827 const index_t y = i % data_src_.shape[1];
828 const index_t c = i / data_src_.shape[1];
829 const real_t vsrc = data_src_[0][c][y][x];
831 const index_t py_min = y < ksize_ ? 0 : (y-ksize_+kstride_)/kstride_;
832 const index_t px_min = x < ksize_ ? 0 : (x-ksize_+kstride_)/kstride_;
833 const index_t py_max = min( (y+kstride_)/kstride_, data_pooled_.shape[1]);
834 const index_t px_max = min( (x+kstride_)/kstride_, data_pooled_.shape[0]);
837 for(
index_t py = py_min; py < py_max; ++py ){
838 for(
index_t px = px_min; px < px_max; ++px ){
839 val += Reducer::PartialGrad(vsrc, data_pooled_[0][c][py][px]) * grad_pooled_[0][c][py][px];
852 template<
typename SrcExp,
int srcdim>
860 const index_t y = i % new_height_;
861 const index_t c = i / new_height_;
862 if (y < pad_ || x < pad_)
return 0.0f;
865 if (h < src_height_ && w < src_width_) {
866 return src_.Eval(c * src_height_ + h, w);
879 template<
typename SrcExp,
int srcdim>
887 const index_t y = i % new_height_;
888 const index_t c = i / new_height_;
889 const index_t h = y + pad_height_;
890 const index_t w = x + pad_width_;
891 return src_.Eval(c * src_height_ + h, w);
895 const index_t pad_height_, pad_width_;
900 template<
typename SrcExp,
int srcdim>
904 : src_(MakePlan(e.
src_)), width_(e.
shape_[0]){}
906 return src_.Eval( i, width_ - j - 1 );
915 template<
typename Reducer,
typename SrcExp,
int srcdim>
919 : src_( MakePlan( e.
src_ ) ), channel_(e.
shape_[2]),
926 const index_t c = i % channel_;
927 const index_t n = i / channel_;
929 const index_t cstart = c < hnsize_ ? 0 : c - hnsize_;
930 const index_t cend = min( c + hnsize_ + 1, channel_ );
931 real_t res = Reducer::kInitV;
932 for(
index_t cc = cstart; cc < cend; ++ cc ){
933 Reducer::Reduce( res, src_.Eval( (n*channel_+cc)*height_ + y, x ) );
939 const index_t channel_, height_, width_, hnsize_;
951 const static bool kPass =
true;
963 :dptr_(t.
src_.dptr){}
const SrcExp & src_
source operand
Definition: tensor_expr_ext.h:302
ChannelPoolingExp< Reducer, SrcExp, ExpInfo< SrcExp >::kDim > chpool(const Exp< SrcExp, etype > &src, index_t nsize)
channel pooling, do reduction over (local nearby) channels, used to implement local response normaliz...
Definition: tensor_expr_ext.h:553
broadcast Tensor1D into a higher dimension Tensor input: Tensor<Device,1>: ishape[0] output: Tensor<D...
Definition: tensor_expr_ext.h:21
DotExp< TA, TB, ltrans, rtrans > operator*(const DotExp< TA, TB, ltrans, rtrans > &lhs, real_t rhs)
dot operator def
Definition: tensor_expr.h:206
unsigned index_t
type that will be used for index
Definition: tensor_base.h:123
Shape< dim > shape_
the shape of this expression
Definition: tensor_expr_engine-inl.hpp:22
PoolingExp(const SrcExp &src, Shape< 2 > pshape, index_t ksize, index_t kstride)
constructor, specify shape
Definition: tensor_expr_ext.h:187
index_t src_height_
source tensor height
Definition: tensor_expr_ext.h:240
UnPoolingExp< Reducer, Device > unpool(const Tensor< Device, 4 > &data_src, const Tensor< Device, 4 > &data_pooled, const Tensor< Device, 4 > &grad_pooled, index_t ksize, index_t kstride)
unpooling gradient for 4D, backprop gradient value back, revserse operation of pooling ...
Definition: tensor_expr_ext.h:482
This part of code gives plan that can be used to carry out execution.
Definition: tensor_expr_engine-inl.hpp:33
const SrcExp & src_
source operand
Definition: tensor_expr_ext.h:236
PoolingExp< Reducer, SrcExp, ExpInfo< SrcExp >::kDim > pool(const Exp< SrcExp, etype > &src, index_t ksize, index_t kstride)
pooling subregion results together
Definition: tensor_expr_ext.h:450
Definition: tensor_expr_engine-inl.hpp:201
bool CheckAlign(size_t pitch)
check if a pointer is aligned
Definition: tensor_sse-inl.hpp:52
index_t psize_
patch size
Definition: tensor_expr_ext.h:79
channel pooling expression, do reduction over (local nearby) channels, used to implement local respon...
Definition: tensor_expr_ext.h:316
shape of a tensor IMPORTANT NOTE: this shape is different from numpy.shape shape[0] gives the lowest ...
Definition: tensor.h:23
index_t ksize_
kernel size
Definition: tensor_expr_ext.h:213
ReshapeExp(const SrcExp &src, Shape< dimdst > shape)
constructor
Definition: tensor_expr_ext.h:108
index_t kstride_
kernel stride
Definition: tensor_expr_ext.h:215
const SrcExp & src_
source operand
Definition: tensor_expr_ext.h:166
real_t scale_
source operand, scale of the
Definition: tensor_expr_ext.h:152
const Tensor< Device, 4 > & grad_pooled_
gradient data of pooled part, to be propgate down
Definition: tensor_expr_ext.h:211
PaddingExp(const SrcExp &src, index_t pad)
constructor
Definition: tensor_expr_ext.h:244
index_t pstride_
patch stride
Definition: tensor_expr_ext.h:81
PackColToPatchXExp< Device, dstdim > pack_col2patch(const Tensor< Device, 2 > &mat, Shape< dstdim > imshape, index_t psize, index_t pstride)
reverse operation of pack_col2patch, can be used to implement deconvolution
Definition: tensor_expr_ext.h:392
void Assert(bool exp)
assert a expression is true
Definition: tensor_base.h:285
ReduceTo1DExp< SrcExp, red::sum, 0 > sum_rows(const Exp< SrcExp, etype > &exp)
a expression that sum over rows of a matrix
Definition: tensor_expr_ext.h:577
PackColToPatchXExp(const Tensor< Device, 2 > &mat, Shape< dstdim > imshape, index_t psize, index_t pstride)
constructor
Definition: tensor_expr_ext.h:83
unpack local (overlap) patches of image to column of mat, can be used to implement convolution...
Definition: tensor_expr_ext.h:38
MirroringExp< SrcExp, ExpInfo< SrcExp >::kDim > mirror(const Exp< SrcExp, etype > &src)
mirroring expression, mirror images in width
Definition: tensor_expr_ext.h:538
index_t src_width_
source tensor width
Definition: tensor_expr_ext.h:242
unpooling expr reverse operation of pooling, used to pass gradient back
Definition: tensor_expr_ext.h:205
support of sse2 optimization of some operations
CroppingExp(const SrcExp &src, Shape< 2 > cshape)
constructor
Definition: tensor_expr_ext.h:270
static check sse enable if a expression E can not be evaluated using sse, then kPass = false ...
Definition: tensor_sse-inl.hpp:357
float real_t
type that will be used for content
Definition: tensor_base.h:118
float vector real type, used for vectorization
Definition: tensor_sse-inl.hpp:88
reverse operation of UnpackPatchToCol, used to backprop gradient back this is a version supporting mu...
Definition: tensor_expr_ext.h:75
index_t src_height_
src height
Definition: tensor_expr_ext.h:268
const SubType & self(void) const
Definition: tensor_expr.h:52
padding expression, pad a image with zeros
Definition: tensor_expr_ext.h:234
const Tensor< Device, 1 > src_
source operand
Definition: tensor_expr_ext.h:23
index_t pad_width_
pad height
Definition: tensor_expr_ext.h:266
reduction to 1 dimension tensor input: Tensor<Device,k>: ishape output: Tensor<Device,1> shape[0] = ishape[dimkeep];
Definition: tensor_expr_ext.h:148
device name CPU
Definition: tensor.h:185
UnpackPatchToColXExp(const SrcExp &img, index_t psize, index_t pstride)
constructor
Definition: tensor_expr_ext.h:52
const Tensor< Device, 4 > & data_src_
source input, corresponds to src in pooling
Definition: tensor_expr_ext.h:207
PoolingExp(const SrcExp &src, index_t ksize, index_t kstride)
constructor
Definition: tensor_expr_ext.h:176
static type inference template, used to get the dimension of each expression, if ExpInfo<E>::kDim == ...
Definition: tensor_expr_engine-inl.hpp:153
const SrcExp & src_
source expression
Definition: tensor_expr_ext.h:129
UnpackPatchToColXExp< SrcExp, ExpInfo< SrcExp >::kDim > unpack_patch2col(const Exp< SrcExp, etype > &img, index_t psize, index_t pstride)
unpack local (overlap) patches of image to column of mat, can be used to implement convolution after ...
Definition: tensor_expr_ext.h:377
const Tensor< Device, 2 > & mat_
source operand
Definition: tensor_expr_ext.h:77
definitions of how expressions should be evaluated
UnPoolingExp(const Tensor< Device, 4 > &data_src, const Tensor< Device, 4 > &data_pooled, const Tensor< Device, 4 > &grad_pooled, index_t ksize, index_t kstride)
constructor
Definition: tensor_expr_ext.h:217
Definition: tensor_sse-inl.hpp:250
MSHADOW_XINLINE Shape< 2 > Shape2(index_t s1, index_t s0)
construct a two dimension shape, stride will equal s0
Definition: tensor.h:152
SwapAxisExp(const SrcExp &src)
constructor
Definition: tensor_expr_ext.h:131
index_t pstride_
patch stride
Definition: tensor_expr_ext.h:44
index_t i_height_
height of img
Definition: tensor_expr_ext.h:48
Broadcast1DExp(const Tensor< Device, 1 > &src, Shape< dimdst > shape)
constructor
Definition: tensor_expr_ext.h:25
MSHADOW_XINLINE index_t ProdShape(int dimstart, int dimend) const
Definition: tensor.h:104
Broadcast1DExp< Device, 2, 0 > repmat(const Tensor< Device, 1 > &src, index_t nrow)
a expression that replicate a 1 dimension tensor for nrow times
Definition: tensor_expr_ext.h:566
index_t ksize_
kernel size
Definition: tensor_expr_ext.h:168
index_t src_height_
source height shape[1]
Definition: tensor_expr_ext.h:172
crop expression, cut off the boundary region, reverse operation of padding
Definition: tensor_expr_ext.h:260
index_t pad_height_
pad height
Definition: tensor_expr_ext.h:264
MSHADOW_XINLINE real_t Eval(index_t y, index_t x) const
evaluate the expression at index [y][x] to be implemented by SubType
index_t pad_
pad size
Definition: tensor_expr_ext.h:238
Definition: tensor_expr_engine-inl.hpp:215
mirror expression, mirror a image in width
Definition: tensor_expr_ext.h:300
some engine that evaluate complex expression
Definition: tensor_expr_engine-inl.hpp:390
SwapAxisExp< SrcExp, ExpInfo< SrcExp >::kDim, a1, a2 > swapaxis(const Exp< SrcExp, etype > &src)
a expression that reshapes a tensor to another shape
Definition: tensor_expr_ext.h:420
index_t i_width_
width of img
Definition: tensor_expr_ext.h:50
index_t ishape0_
smallest dimension of input
Definition: tensor_expr_ext.h:106
reshape the content to another shape input: Tensor<Device,dimsrc>: ishape output: Tensor<Device...
Definition: tensor_expr_ext.h:102
Shape< dimension > shape
shape of the tensor
Definition: tensor.h:217
#define MSHADOW_CINLINE
cpu force inline
Definition: tensor_base.h:101
CroppingExp< SrcExp, ExpInfo< SrcExp >::kDim > crop(const Exp< SrcExp, etype > &src, Shape< 2 > oshape)
revserse operationg of padding, cut off boundaries, crop output from center of input ...
Definition: tensor_expr_ext.h:510
ReduceTo1DExp< SrcExp, red::sum, dimkeep > sumall_except_dim(const Exp< SrcExp, etype > &exp)
a sum over all dimensions, except dimkeep
Definition: tensor_expr_ext.h:435
base class for expression
Definition: tensor_expr.h:49
index_t psize_
patch size
Definition: tensor_expr_ext.h:42
const SrcExp & src_
source operand
Definition: tensor_expr_ext.h:318
index_t nsize_
neighbor size
Definition: tensor_expr_ext.h:320
MSHADOW_XINLINE size_t Size(void) const
Definition: tensor.h:82
const Tensor< Device, 4 > & data_pooled_
result of pooled data, corresponds to result of pooling
Definition: tensor_expr_ext.h:209
ReduceTo1DExp(const EType &src, real_t scale)
construct a repmat expression from src and nrow
Definition: tensor_expr_ext.h:154
const SrcExp & src_
source expression
Definition: tensor_expr_ext.h:104
a general class that allows extension that makes tensors of some shape
Definition: tensor_expr_engine-inl.hpp:20
const SrcExp & src_
source operand
Definition: tensor_expr_ext.h:262
const SrcExp & img_
source operand
Definition: tensor_expr_ext.h:40
const EType & src_
source operand
Definition: tensor_expr_ext.h:150
Definition: tensor_sse-inl.hpp:381
swap two axis of a tensor input: Tensor<Device,dim>: ishape output: Tensor<Device,dimdst> oshape[a1],oshape[a2] = ishape[a2],oshape[a1]
Definition: tensor_expr_ext.h:127
PaddingExp< SrcExp, ExpInfo< SrcExp >::kDim > pad(const Exp< SrcExp, etype > &src, index_t pad)
padding expression, pad a image with zeros on boundaries, padding affects shape[0], and shape[1]
Definition: tensor_expr_ext.h:496
index_t kstride_
kernel stride
Definition: tensor_expr_ext.h:170
Broadcast1DExp< Device, dimdst, dimcast > broadcast(const Tensor< Device, 1 > &src, Shape< dimdst > shape)
a expression that replicate a 1 dimension tensor in dimension dimcast
Definition: tensor_expr_ext.h:354
ReshapeExp< SrcExp, dimdst, ExpInfo< SrcExp >::kDim > reshape(const Exp< SrcExp, etype > &src, Shape< dimdst > oshape)
a expression that reshapes a tensor to another shape
Definition: tensor_expr_ext.h:406
CroppingExp(const SrcExp &src, Shape< 2 > cshape, index_t start_height, index_t start_width)
constructor
Definition: tensor_expr_ext.h:281
index_t i_channel_
number of input channel
Definition: tensor_expr_ext.h:46
MirroringExp(const SrcExp &src)
constructor
Definition: tensor_expr_ext.h:304
index_t src_width_
source width shape[0]
Definition: tensor_expr_ext.h:174
pooling expression, do reduction over local patches of a image
Definition: tensor_expr_ext.h:164
ChannelPoolingExp(const SrcExp &src, index_t nsize)
constructor
Definition: tensor_expr_ext.h:322