Beispiel #1
0
    def _backward(din, w_value, x_value, col, col_w, pad, stride):
        FN, C, FH, FW = w_value.shape
        din = din.transpose(0, 2, 3, 1).reshape(-1, FN)

        db = np.sum(din, axis=0)
        dw = np.dot(col.T, din)
        dw = dw.transpose(1, 0).reshape(FN, C, FH, FW)

        dcol = np.dot(din, col_w.T)
        dx = tff.col2im(dcol, x_value.shape, FH, FW, stride, pad)

        return dw, dx, db
Beispiel #2
0
    def _backward(din, x_value, filter_h, filter_w, pad, stride, arg_max):
        din = din.transpose(0, 2, 3, 1)

        pool_size = filter_h * filter_w
        dmax = np.zeros((din.size, pool_size))
        dmax[np.arange(arg_max.size), arg_max.flatten()] = din.flatten()
        dmax = dmax.reshape(din.shape + (pool_size, ))

        dcol = dmax.reshape(dmax.shape[0] * dmax.shape[1] * dmax.shape[2], -1)
        dx = tff.col2im(dcol, x_value.shape, filter_h, filter_w, stride, pad)

        return dx
Beispiel #3
0
    def _backward(din, w_value, x_value, pad, stride, arg_max):
        FN, C, FH, FW = w_value.shape
        din = din.transpose(0, 2, 3, 1)

        pool_size = FH * FW
        dmax = np.zeros((din.size, pool_size))
        dmax[np.arange(arg_max.size), arg_max.flatten()] = din.flatten()
        dmax = dmax.reshape(din.shape + (pool_size, ))

        dcol = dmax.reshape(dmax.shape[0] * dmax.shape[1] * dmax.shape[2], -1)
        dx = tff.col2im(dcol, x_value.shape, FH, FW, stride, pad)

        return dx
Beispiel #4
0
    def backward(self, din, is_numba):
        if is_numba:
            self._backward(din, self.x_value, self.filter_h, self.filter_w,
                           self.pad, self.stride, self.arg_max)
        else:
            din = din.transpose(0, 2, 3, 1)

            pool_size = self.filter_h * self.filter_w
            dmax = np.zeros((din.size, pool_size))
            dmax[np.arange(self.arg_max.size),
                 self.arg_max.flatten()] = din.flatten()
            dmax = dmax.reshape(din.shape + (pool_size, ))

            dcol = dmax.reshape(dmax.shape[0] * dmax.shape[1] * dmax.shape[2],
                                -1)
            dx = tff.col2im(dcol, self.x_value.shape, self.filter_h,
                            self.filter_w, self.stride, self.pad)

            return dx
Beispiel #5
0
    def backward(self, din, is_numba):
        if is_numba:
            dw, dx, db = self._backward(din, self.w_value, self.x_value,
                                        self.col, self.col_w, self.pad,
                                        self.stride)
            self.dw = dw
            self.db = db
            return dx
        else:
            FN, C, FH, FW = self.w_value.shape
            din = din.transpose(0, 2, 3, 1).reshape(-1, FN)

            self.db = np.sum(din, axis=0)
            self.dw = np.dot(self.col.T, din)
            self.dw = self.dw.transpose(1, 0).reshape(FN, C, FH, FW)

            dcol = np.dot(din, self.col_w.T)
            dx = tff.col2im(dcol, self.x_value.shape, FH, FW, self.stride,
                            self.pad)
            return dx