예제 #1
0
파일: cnn_basic.py 프로젝트: chamlhy/python
'''卷积层 & 池化层
	每次我们采样一个跟权重一样大小的窗口,让它跟权重做按元素的乘法然后相加。通常我们也是用卷积的术语把这个窗口叫kernel或者filter。
	stride移动窗口步伐大小
	pad在边缘填充窗口大小
	当输入数据有多个通道的时候,每个通道会有对应的权重,然后会对每个通道做卷积之后在通道之间求和
	当输入需要多通道时,每个输出通道有对应权重,然后每个通道上做卷积。
	池化层能够很好的缓解卷积层位置敏感问题。它跟卷积类似每次看一个小窗口,然后选出窗口里面最大的元素,或者平均元素作为输出
	'''

#卷积层
# 输入输出数据格式是 batch x channel x height x width,这里batch和channel都是1
# 权重格式是 output_channels x in_channels x height x width,这里input_filter和output_filter都是1。
w = nd.arange(4).reshape((1,1,2,2))
b = nd.array([1])
data = nd.arange(9).reshape((1,1,3,3))
out = nd.Convolution(data, w, b, kernel=w.shape[2:], num_filter=w.shape[0])

#移动窗口
out = nd.Convolution(data, w, b, kernel=w.shape[2:], num_filter=w.shape[0],
                     stride=(2,2), pad=(1,1))

#输入多通道(注意weight,data大小变了)
w = nd.arange(8).reshape((1,2,2,2))
data = nd.arange(18).reshape((1,2,3,3))

out = nd.Convolution(data, w, b, kernel=w.shape[2:], num_filter=w.shape[0])


#输出多通道(注意weight,bias,data,out大小都变了)
w = nd.arange(16).reshape((2,2,2,2))
data = nd.arange(18).reshape((1,2,3,3))
def net(X, is_training=True, debug=False):
    ########################
    #  Define the computation of the first convolutional layer
    ########################
    h1_conv = nd.Convolution(data=X,
                             weight=W1,
                             bias=b1,
                             kernel=(3, 3),
                             num_filter=20)
    h1_normed = batch_norm(h1_conv,
                           gamma1,
                           beta1,
                           scope_name='bn1',
                           is_training=is_training)
    h1_activation = relu(h1_normed)
    h1 = nd.Pooling(data=h1_activation,
                    pool_type="avg",
                    kernel=(2, 2),
                    stride=(2, 2))
    if debug:
        print("h1 shape: %s" % (np.array(h1.shape)))

    ########################
    #  Define the computation of the second convolutional layer
    ########################
    h2_conv = nd.Convolution(data=h1,
                             weight=W2,
                             bias=b2,
                             kernel=(5, 5),
                             num_filter=50)
    h2_normed = batch_norm(h2_conv,
                           gamma2,
                           beta2,
                           scope_name='bn2',
                           is_training=is_training)
    h2_activation = relu(h2_normed)
    h2 = nd.Pooling(data=h2_activation,
                    pool_type="avg",
                    kernel=(2, 2),
                    stride=(2, 2))
    if debug:
        print("h2 shape: %s" % (np.array(h2.shape)))

    ########################
    #  Flattening h2 so that we can feed it into a fully-connected layer
    ########################
    h2 = nd.flatten(h2)
    if debug:
        print("Flat h2 shape: %s" % (np.array(h2.shape)))

    ########################
    #  Define the computation of the third (fully-connected) layer
    ########################
    h3_linear = nd.dot(h2, W3) + b3
    h3_normed = batch_norm(h3_linear,
                           gamma3,
                           beta3,
                           scope_name='bn3',
                           is_training=is_training)
    h3 = relu(h3_normed)
    if debug:
        print("h3 shape: %s" % (np.array(h3.shape)))

    ########################
    #  Define the computation of the output layer
    ########################
    yhat_linear = nd.dot(h3, W4) + b4
    if debug:
        print("yhat_linear shape: %s" % (np.array(yhat_linear.shape)))

    return yhat_linear
예제 #3
0
#!/usr/bin/env python
#coding:utf-8
#author: chengfu.wcy

from mxnet import nd

w = nd.arange(4).reshape((1, 1, 2, 2))

b = nd.array([1])

data = nd.arange(9).reshape((1, 1, 3, 3))
out = nd.Convolution(data, w, b, kernel=w.shape[2:], num_filter=w.shape[1])

print data
print w
print b
print out
예제 #4
0
def flowdr(dem_fill,NoData,rows,cols,ctx,switch):
    ingrid = np.indices((rows, cols))
    ingrid[0]        # row indices
    ingrid[1]        # column indices
    ingridxmx=nd.array(ingrid[1],ctx[0]).reshape((1,1,rows, cols))
    ingridymx=nd.array(ingrid[0],ctx[0]).reshape((1,1,rows, cols))
    dem_fillmx=nd.array(dem_fill,ctx[0])
    demmx=dem_fillmx.reshape((1,1,rows, cols))
    res=1
    l=[0,1,2,3,4,5,6,7,0]
    direct=[1,2,4,8,16,32,64,128]
    direct_d=[[1,3],[2,6],[4,12],[8,24],[16,48],[32,96],[64,192],[128,129]]
    weight=[None]*8
    weight1=[None]*8
    convx=[None]*8
    convy=[None]*8
    convz=[None]*8
    runlen=[1,ma.pow(2,0.5),1,ma.pow(2,0.5),1,ma.pow(2,0.5),1,ma.pow(2,0.5)]*res
    n = [[[] for x in range(3)] for x in range(8)]#create list to store normal vectors for each facet
    s = [None]*8
    d = [None]*8

    weight[0] = nd.array([[0, 0, 0], [0, 1, -1], [0, 0, 0]], gpu(0))
    weight[1] = nd.array([[0, 0, -1], [0, 1, 0], [0, 0, 0]], gpu(0))
    weight[2] = nd.array([[0, -1, 0], [0, 1, 0], [0, 0, 0]], gpu(0))
    weight[3] = nd.array([[-1, 0, 0], [0, 1, 0], [0, 0, 0]], gpu(0))
    weight[4] = nd.array([[0, 0, 0], [-1, 1, 0], [0, 0, 0]], gpu(0))
    weight[5] = nd.array([[0, 0, 0], [0, 1, 0], [-1, 0, 0]], gpu(0))
    weight[6] = nd.array([[0, 0, 0], [0, 1, 0], [0, -1, 0]], gpu(0))
    weight[7] = nd.array([[0, 0, 0], [0, 1, 0], [0, 0, -1]], gpu(0))
    
    weight1[0] = nd.array([[0, 0, 0], [0, 1, -10], [0, 0, 0]], gpu(0))
    weight1[1] = nd.array([[0, 0, -10], [0, 1, 0], [0, 0, 0]], gpu(0))
    weight1[2] = nd.array([[0, -10, 0], [0, 1, 0], [0, 0, 0]], gpu(0))
    weight1[3] = nd.array([[-10, 0, 0], [0, 1, 0], [0, 0, 0]], gpu(0))
    weight1[4] = nd.array([[0, 0, 0], [-10, 1, 0], [0, 0, 0]], gpu(0))
    weight1[5] = nd.array([[0, 0, 0], [0, 1, 0], [-10, 0, 0]], gpu(0))
    weight1[6] = nd.array([[0, 0, 0], [0, 1, 0], [0, -10, 0]], gpu(0))
    weight1[7] = nd.array([[0, 0, 0], [0, 1, 0], [0, 0, -10]], gpu(0))

    d0=nd.zeros((rows, cols),ctx[0],dtype='float32')
    dd=nd.zeros((rows, cols),ctx[0],dtype='float32')
    d_flat=nd.zeros((rows, cols),ctx[0],dtype='float32')
    flat=nd.zeros((rows, cols),ctx[0],dtype='float32')
    dep=nd.zeros((rows, cols),ctx[0],dtype='float32')
    high=nd.zeros((rows, cols),ctx[0],dtype='float32')
    fd=nd.zeros((rows, cols),ctx[0],dtype='float32')-999
    d_compact=nd.zeros((rows, cols),ctx[0],dtype='float32')-1

    for i in range(0,8):
        w=weight[i].reshape((1, 1, 3, 3))
        convz[i] = nd.Convolution(data=demmx, weight=w, kernel=(3,3), no_bias=True, num_filter=1,pad=(1,1),cudnn_tune='off')
        convz[i]=convz[i][0,0,:,:]
        if switch==1 or 3:
            convx[i] = nd.Convolution(data=ingridxmx, weight=w, kernel=(3,3), no_bias=True, num_filter=1,pad=(1,1),cudnn_tune='off')
            convy[i] = nd.Convolution(data=ingridymx, weight=w, kernel=(3,3), no_bias=True, num_filter=1,pad=(1,1),cudnn_tune='off')        
            convx[i]=convx[i][0,0,:,:]
            convy[i]=convy[i][0,0,:,:]
        
    if switch==1 or 3:
        for p in range(0,8):#8 facets from N-NE clockwise
            l0=l[p]
            l1=l[p+1]
            d[l0]=d0-999#Nodata value
            dmax=d0-999
            smax=d0-999
            n[l0][0]= convz[l0]*convy[l1]-convz[l1]*convy[l0]#nx
            n[l0][1]= convz[l0]*convx[l1]-convz[l1]*convx[l0]#ny
            n[l0][2]= convy[l0]*convx[l1]-convy[l1]*convx[l0]#nz
            #make boolean mask to determine direction d and slope s
            d[l0]=nd.where(condition=((n[l0][0]==0)*(n[l0][1]>=0)),x=d0,y=d[l0])

            d[l0]=nd.where(condition=((n[l0][0]==0)*(n[l0][1])<0),x=d0+ma.pi,y=d[l0])

            d[l0]=nd.where(condition=(n[l0][0]>0),x=ma.pi/2-nd.arctan(n[l0][1]/n[l0][0]),y=d[l0])

            d[l0]=nd.where(condition=(n[l0][0]<0),x=3*ma.pi/2-nd.arctan(n[l0][1]/n[l0][0]),y=d[l0])


            d[l0]=nd.where(condition=((convz[l0]<=0)*(convz[l1]<=0)),x=dmax,y=d[l0])

            s[l0]=-nd.tan(nd.arccos(n[l0][2]/(nd.sqrt(nd.square(n[l0][0])+nd.square(n[l0][1])+nd.square(n[l0][2])))))#slope of the triangular facet
            s[l0]=nd.where(condition=((convz[l0]<=0)*(convz[l1]<=0)),x=smax,y=s[l0])
            #Modify the scenario when the steepest slope is outside the 45 range of each facet
            dmax=nd.where(condition=((convz[l0]/runlen[l0]>=convz[l1]/runlen[l0])*(convz[l0]>0)),x=d0+ma.pi*l0/4,y=dmax)
            dmax=nd.where(condition=((convz[l0]/runlen[l0]<convz[l1]/runlen[l0])*(convz[l1]>0)),x=d0+ma.pi*(l0+1)/4,y=dmax)

            smax=nd.where(condition=((convz[l0]>=convz[l1])*(convz[l0]>0)),x=convz[l0]/runlen[l0],y=smax)
            smax=nd.where(condition=((convz[l0]<convz[l1])*(convz[l1]>0)),x=convz[l1]/runlen[l1],y=smax)
            d[l0]=nd.where(condition=((d[l0]<ma.pi*l0/4)+(d[l0]>ma.pi*l1/4)),x=dmax,y=d[l0])

            s[l0]=nd.where(condition=((d[l0]<ma.pi*l0/4)+(d[l0]>ma.pi*l1/4)),x=smax,y=s[l0])

            if switch==1:

                #flat and depressions indicator grid    

                flat=(convz[l0]==0)+flat
                dep=(convz[l0]<0)+dep
                high=(convz[l0]>0)+high

        for q in range(0,8):#check if the 45 degree range angles need to be maintaied, otherwise delete (set to NoData)
            l0=l[q]
            l1=l[q+1]
            l2=l[q-1]
            dmax=d0-999
            if q==0:
                dmax=nd.where(condition=(d[0]==d[1]),x=d[0],y=dmax)
                dmax=nd.where(condition=(d[0]==d[7]),x=d[0],y=dmax)
                d[0]=nd.where(condition=((d[0]==ma.pi*l0/4)+(d[0]==ma.pi*l1/4)),x=dmax,y=d[0])
            else:
                dmax=nd.where(condition=(d[l0]==d[l1]),x=d[l0],y=dmax)
                dmax=nd.where(condition=(d[l0]==d[l2]),x=d[l0],y=dmax)
                d[l0]=nd.where(condition=((d[l0]==ma.pi*l0/4)+(d[l0]==ma.pi*l1/4)),x=dmax,y=d[l0])
    #Check if flat or surface depression area. then lable with -1 or -10 respectively

    if switch==1:

        fd=nd.where(condition=(flat==8),x=d0-2,y=fd)#flats

        fd=nd.where(condition=(dep>=1)*(high==0),x=d0-3,y=fd)#high edge

        high_zero=nd.where(condition=(high==0),x=d0+1,y=d0)
    
    
    for j in range (0,8):
        if switch==1 or switch==2:
            d_flat=nd.where(condition=(convz[j]==0),x=d0+direct[j],y=d0)+d_flat
        
        if switch==1:
            flat_near=nd.where(condition=(convz[j]==0),x=d0+5,y=d0)
            dd1=high_zero+flat_near
            w=weight1[j].reshape((1, 1, 3, 3))
            dd1=dd1.reshape((1,1,rows, cols))
            conv_near= nd.Convolution(data=dd1, weight=w, kernel=(3,3), no_bias=True, num_filter=1,pad=(1,1),cudnn_tune='off')
            conv_near= conv_near[0,0,:,:]
            dd=nd.where(condition=(conv_near==-5)+(conv_near==-59)+(conv_near==-54)+(conv_near==-4),x=d0+1,y=d0)+dd

        if switch==1 or switch==3:
            d_compact=nd.where(condition=(d[j]==ma.pi*j/4),x=d0+direct_d[j][0],y=d_compact)
            d_compact=nd.where(condition=(d[j]>j*ma.pi/4)*(d[j]<(j+1)*ma.pi/4),x=d0+direct_d[j][1],y=d_compact)

    if switch==1 or switch==3:
        d_compact=nd.where(condition=(dem_fillmx==d0+NoData),x=d0-999,y=d_compact)#NoData        
    
    if switch==1:
        fd=nd.where(condition=(dd>=1)*(high>=1),x=d0-1,y=fd)#low edge
        fd=nd.where(condition=(dep==8),x=d0-10,y=fd)#lowest points in depressions
        return (fd.asnumpy(),d_compact.asnumpy(),d_flat.asnumpy())

    if switch==2:
        return (d_flat.asnumpy())
    if switch==3:
        return (d_compact.asnumpy())
예제 #5
0
    def forward(self, is_train, req, in_data, out_data, aux):
        if is_train:
            x = in_data[0]
            w = in_data[1]
            int_w = in_data[3]
            y = out_data[0]
            # print(self.no_bias)

            quan_x, x_shift_bit = self.int_quantize(x)
            quan_w, w_shift_bit = self.int_quantize(w)

            self.assign(in_data[3], req[0], quan_w)

            # print(quan_x.sum(), quan_w)
            if self.no_bias:
                y[:] = nd.Convolution(data=quan_x,
                                      weight=quan_w,
                                      kernel=self.kernel,
                                      num_filter=self.num_filter,
                                      stride=self.stride,
                                      pad=self.pad,
                                      no_bias=self.no_bias,
                                      workspace=self.workspace,
                                      name=self.name)

            else:
                b = in_data[2]
                int_b = in_data[4]
                quan_b, b_shift_bit = self.int_quantize(b)
                y = nd.Convolution(data=quan_x,
                                   weight=quan_w,
                                   bias=quan_b,
                                   kernel=self.kernel,
                                   num_filter=self.num_filter,
                                   stride=self.stride,
                                   pad=self.pad,
                                   no_bias=self.no_bias,
                                   workspace=self.workspace,
                                   name=self.name)
                self.assign(in_data[4], req[0], quan_b)

            self.assign(out_data[0], req[0], mx.nd.array(y))
        else:
            x = in_data[0]
            int_w = in_data[3]
            y = out_data[0]

            quan_x, x_shift_bit = self.int_quantize(x)

            if self.no_bias:
                y[:] = nd.Convolution(data=quan_x,
                                      weight=int_w,
                                      kernel=self.kernel,
                                      num_filter=self.num_filter,
                                      stride=self.stride,
                                      pad=self.pad,
                                      no_bias=self.no_bias,
                                      workspace=self.workspace,
                                      name=self.name)

            else:
                int_b = in_data[4]
                y = nd.Convolution(data=quan_x,
                                   weight=int_w,
                                   bias=int_b,
                                   kernel=self.kernel,
                                   num_filter=self.num_filter,
                                   stride=self.stride,
                                   pad=self.pad,
                                   no_bias=self.no_bias,
                                   workspace=self.workspace,
                                   name=self.name)

            self.assign(out_data[0], req[0], mx.nd.array(y))
예제 #6
0
def nms_attention_nd(roi_feat,
                     position_mat,
                     nms_pair_pos_fc1_1_weight,
                     nms_pair_pos_fc1_1_bias,
                     nms_query_1_weight,
                     nms_query_1_bias,
                     nms_key_1_weight,
                     nms_key_1_bias,
                     nms_linear_out_1_weight,
                     nms_linear_out_1_bias,
                     num_rois,
                     dim=(1024, 1024, 1024),
                     fc_dim=(64, 16),
                     feat_dim=1024,
                     group=16,
                     index=1):
    """ Attetion module with vectorized version

    Args:
        roi_feat: [num_rois, num_fg_classes, feat_dim]
        position_mat: [num_fg_classes, num_rois, num_rois, 4]
        num_rois: number of rois
        dim: key, query and linear_out dim
        fc_dim:
        feat_dim:
        group:
        index:

    Returns:
        output: [num_rois, num_fg_classes, fc_dim]
    """
    dim_group = (dim[0] / group, dim[1] / group, dim[2] / group)
    roi_feat = nd.transpose(roi_feat, axes=(1, 0, 2))
    # roi_feat_reshape, [num_fg_classes*num_rois, feat_dim]
    roi_feat_reshape = nd.Reshape(roi_feat, shape=(-3, -2))
    # position_embedding, [num_fg_classes, num_rois, num_rois, fc_dim[0]]
    position_embedding = extract_pairwise_multi_position_embedding_nd(
        position_mat, fc_dim[0])
    # [num_fg_classes * num_rois * num_rois, fc_dim[0]]
    position_embedding_reshape = nd.Reshape(position_embedding,
                                            shape=(-1, fc_dim[0]))
    # position_feat_1, [num_fg_classes * num_rois * num_rois, fc_dim[1]]
    position_feat_1 = nd.FullyConnected(name='nms_pair_pos_fc1_' + str(index),
                                        data=position_embedding_reshape,
                                        weight=nms_pair_pos_fc1_1_weight,
                                        bias=nms_pair_pos_fc1_1_bias,
                                        num_hidden=fc_dim[1])
    # position_feat_1, [num_fg_classes, num_rois, num_rois, fc_dim[1]]
    position_feat_1 = nd.Reshape(position_feat_1,
                                 shape=(-1, num_rois, num_rois, fc_dim[1]))
    aff_weight = nd.Activation(data=position_feat_1, act_type='relu')
    # aff_weight, [num_fg_classes, fc_dim[1], num_rois, num_rois]
    aff_weight = nd.transpose(aff_weight, axes=(0, 3, 1, 2))

    ####################### multi head in batch###########################
    assert dim[0] == dim[1], 'Matrix multi requires the same dims!'
    # q_data, [num_fg_classes * num_rois, dim[0]]
    q_data = nd.FullyConnected(name='nms_query_' + str(index),
                               data=roi_feat_reshape,
                               weight=nms_query_1_weight,
                               bias=nms_query_1_bias,
                               num_hidden=dim[0])
    # q_data, [num_fg_classes, num_rois, group, dim_group[0]]
    q_data_batch = nd.Reshape(q_data,
                              shape=(-1, num_rois, group, dim_group[0]))
    q_data_batch = nd.transpose(q_data_batch, axes=(0, 2, 1, 3))
    # q_data_batch, [num_fg_classes * group, num_rois, dim_group[0]]
    q_data_batch = nd.Reshape(q_data_batch, shape=(-3, -2))
    k_data = nd.FullyConnected(name='nms_key_' + str(index),
                               data=roi_feat_reshape,
                               weight=nms_key_1_weight,
                               bias=nms_key_1_bias,
                               num_hidden=dim[1])
    # k_data, [num_fg_classes, num_rois, group, dim_group[1]]
    k_data_batch = nd.Reshape(k_data,
                              shape=(-1, num_rois, group, dim_group[1]))
    k_data_batch = nd.transpose(k_data_batch, axes=(0, 2, 1, 3))
    # k_data_batch, [num_fg_classes * group, num_rois, dim_group[1]]
    k_data_batch = nd.Reshape(k_data_batch, shape=(-3, -2))
    v_data = roi_feat
    aff = nd.batch_dot(lhs=q_data_batch,
                       rhs=k_data_batch,
                       transpose_a=False,
                       transpose_b=True)
    # aff_scale, [num_fg_classes * group, num_rois, num_rois]
    aff_scale = (1.0 / math.sqrt(float(dim_group[1]))) * aff

    assert fc_dim[1] == group, 'Check the dimensions in attention!'
    # [num_fg_classes * fc_dim[1], num_rois, num_rois]
    aff_weight_reshape = nd.Reshape(aff_weight, shape=(-3, -2))
    # weighted_aff, [num_fg_classes * fc_dim[1], num_rois, num_rois]
    weighted_aff = nd.log(nd.maximum(aff_weight_reshape, 1e-6)) + aff_scale
    # aff_softmax, [num_fg_classes * fc_dim[1], num_rois, num_rois]
    aff_softmax = nd.softmax(data=weighted_aff,
                             axis=2,
                             name='nms_softmax_' + str(index))
    aff_softmax_reshape = nd.Reshape(aff_softmax,
                                     shape=(-1, fc_dim[1] * num_rois, 0))
    # output_t, [num_fg_classes, fc_dim[1] * num_rois, feat_dim]
    output_t = nd.batch_dot(lhs=aff_softmax_reshape, rhs=v_data)
    # output_t_reshape, [num_fg_classes, fc_dim[1], num_rois, feat_dim]
    output_t_reshape = nd.Reshape(output_t,
                                  shape=(-1, fc_dim[1], num_rois, feat_dim))
    # output_t_reshape, [fc_dim[1], feat_dim, num_rois, num_fg_classes]
    output_t_reshape = nd.transpose(output_t_reshape, axes=(1, 3, 2, 0))
    # output_t_reshape, [1, fc_dim[1] * feat_dim, num_rois, num_fg_classes]
    output_t_reshape = nd.Reshape(output_t_reshape,
                                  shape=(1, fc_dim[1] * feat_dim, num_rois,
                                         -1))
    linear_out = nd.Convolution(name='nms_linear_out_' + str(index),
                                data=output_t_reshape,
                                weight=nms_linear_out_1_weight,
                                bias=nms_linear_out_1_bias,
                                kernel=(1, 1),
                                num_filter=dim[2],
                                num_group=fc_dim[1])
    # [dim[2], num_rois, num_fg_classes]
    linear_out_reshape = nd.Reshape(linear_out, shape=(dim[2], num_rois, -1))
    # [num_rois, num_fg_classes, dim[2]]
    output = nd.transpose(linear_out_reshape, axes=(1, 2, 0))
    return output
예제 #7
0
    def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
        dx = in_grad[0]
        dw = in_grad[1]
        x = in_data[0]
        w = in_data[1]
        # b = in_data[2]
        # w = in_data[3]
        # quan_b = in_data[4]
        x_shift_bit = in_data[5]
        dy = out_grad[0]

        # x, x_shift_bit = self.int_quantize(x)
        quan_w, w_shift_bit = self.int_quantize(w)
        y = out_data[0]
        if self.no_bias:
            x.attach_grad(), quan_w.attach_grad()
            with autograd.record():
                y[:] = nd.Convolution(data=x,
                                      weight=quan_w,
                                      kernel=self.kernel,
                                      num_filter=self.num_filter,
                                      stride=self.stride,
                                      pad=self.pad,
                                      no_bias=self.no_bias,
                                      workspace=self.workspace,
                                      name=self.name)
            dx, dw = autograd.grad(y, [x, quan_w], dy, retain_graph=True)
            y, y_shift_bit = self.int_quantize(y)
            # print(y_shift_bit)
            # y_shift_bit = (x_shift_bit * 0.3 + y_shift_bit * 0.7).floor()
            self.assign(in_grad[0], req[0], dx / (2**y_shift_bit))
            self.assign(in_grad[1], req[0], dw / (2**w_shift_bit))

            self.assign(in_data[3], req[0], quan_w)
            # self.assign(in_data[3], req[0], quan_b)
            self.assign(in_data[5], req[0], y_shift_bit)

        else:
            b = in_data[2]
            b, b_shift_bit = self.int_quantize(b)
            x.attach_grad(), w.attach_grad(), b.attach_grad()
            with autograd.record():
                y[:] = nd.Convolution(data=x,
                                      weight=w,
                                      bias=b,
                                      kernel=self.kernel,
                                      num_filter=self.num_filter,
                                      stride=self.stride,
                                      pad=self.pad,
                                      no_bias=self.no_bias,
                                      workspace=self.workspace,
                                      name=self.name)
            dx, dw, db = autograd.grad(y, [x, w, b], dy, retain_graph=True)
            self.assign(in_grad[0], req[0], dx / (2**x_shift_bit))
            self.assign(in_grad[1], req[0], dw / (2**w_shift_bit))
            self.assign(in_grad[2], req[0], db / (2**b_shift_bit))

            self.assign(in_data[2], req[0], quan_w)
            self.assign(in_data[3], req[0], quan_b)
            self.assign(in_data[4], req[0], x_shift_bit)
            self.assign(in_data[5], req[0], y_shift_bit)
예제 #8
0
    def forward(self, is_train, req, in_data, out_data, aux):
        if is_train:
            x = in_data[0]
            w = in_data[1]
            quan_w = in_data[3]
            quan_b = in_data[4]
            x_shift_bit = in_data[5]
            #if x.max() > 127 or x.min() < -128:
            #   print('False')
            y = out_data[0]
            last_shift_bit = out_data[1]
            if self.no_bias:
                quan_w, w_shift_bit = self.int_quantize(w)
                y[:] = nd.Convolution(data=x,
                                      weight=quan_w,
                                      kernel=self.kernel,
                                      num_filter=self.num_filter,
                                      stride=self.stride,
                                      pad=self.pad,
                                      no_bias=self.no_bias,
                                      workspace=self.workspace,
                                      name=self.name)

            else:

                b = in_data[2]

                quan_w, quan_b, w_shift_bit = self.int_quantize_double(w, b)
                y = nd.Convolution(data=quan_x,
                                   weight=quan_w,
                                   bias=quan_b,
                                   kernel=self.kernel,
                                   num_filter=self.num_filter,
                                   stride=self.stride,
                                   pad=self.pad,
                                   no_bias=self.no_bias,
                                   workspace=self.workspace,
                                   name=self.name)

            y, y_shift_bit = self.int_quantize(y)
            self.assign(out_data[0], req[0], mx.nd.array(y))
            self.assign(out_data[1], req[0], mx.nd.array(y_shift_bit))

        else:
            quan_x = in_data[0]
            w = in_data[1]
            quan_w = in_data[3]
            y_shift_bit = in_data[5]
            y = out_data[0]

            # quan_x, x_shift_bit = self.int_quantize(x)

            if self.no_bias:
                quan_w_2, w_shift_bit = self.int_quantize(w)
                # print('sum', (quan_w_2 != quan_w).sum())
                # self.assign(in_data[1], req[0], quan_w)
                y[:] = nd.Convolution(data=quan_x,
                                      weight=quan_w_2,
                                      kernel=self.kernel,
                                      num_filter=self.num_filter,
                                      stride=self.stride,
                                      pad=self.pad,
                                      no_bias=self.no_bias,
                                      workspace=self.workspace,
                                      name=self.name)
            # y, y_shift_bit = self.int_quantize(y)

            # print('----------------int conv------------------')
            # print(y)
            else:
                b = in_data[2]
                quan_w, quan_b, w_shift_bit = self.int_quantize_double(w, b)
                y = nd.Convolution(data=quan_x,
                                   weight=quan_w,
                                   bias=quan_b,
                                   kernel=self.kernel,
                                   num_filter=self.num_filter,
                                   stride=self.stride,
                                   pad=self.pad,
                                   no_bias=self.no_bias,
                                   workspace=self.workspace,
                                   name=self.name)

            y = (y * (2**y_shift_bit)).floor()

            self.assign(out_data[0], req[0], mx.nd.array(y))
            self.assign(out_data[1], req[0], y_shift_bit)
예제 #9
0
파일: base.py 프로젝트: chr5tphr/ecGAN
 def _forward(self, data, weight, bias=None):
     kwargs = self._kwargs.copy()
     kwargs['no_bias'] = bias is None
     return nd.Convolution(data, weight, bias, name='fwd', **kwargs)