Beispiel #1
0
def make_data(n, c, in_h, in_w, out_h, out_w, scale):
    mean = random_float32((1, c, in_h, in_w))
    bottom = np.zeros((n, c, in_h, in_w), dtype=np.float32)
    for in_n, in_c, y, x in itertools.product(range(n), range(c), range(in_h),
                                              range(in_w)):
        bottom[in_n, in_c, y, x] = in_n * 10 + in_c * 3 + y * 2 + x * 1
    bottom += mean
    crop_y = (in_h - out_h) // 2
    crop_x = (in_w - out_w) // 2
    top = ((bottom - mean)[:, :, crop_y:crop_y + out_h,
                           crop_x:crop_x + out_w]) * scale
    mean, bottom, top = reorder_nchw_hwcn(mean, bottom, top)
    return mean, bottom, top
Beispiel #2
0
def batch_normalization(in_shape, eps=1e-5):
    from chainer.functions.normalization.batch_normalization import BatchNormalizationFunction
    f = BatchNormalizationFunction(eps=eps)
    # assumes (n, c) or (n, c, h, w); c is dimension to be normalized
    # in sukiyaki, shape is (c, n) and (h, w, c, n)
    c = in_shape[1]
    gamma = random_float32((c, ))
    beta = random_float32((c, ))
    x = random_float32(in_shape)
    y, = f.forward((x, gamma, beta))

    gy = random_float32(in_shape)
    gx, ggamma, gbeta = f.backward((x, gamma, beta), (gy, ))

    shape_4d = len(in_shape) == 4
    if shape_4d:
        target_dim = 3
        x, y, gy, gx = reorder_nchw_hwcn(x, y, gy, gx)
    else:
        target_dim = 1
        x, y, gy, gx = reverse_order(x, y, gy, gx)

    layer_params = {
        "type": "batch_normalization",
        "params": {
            "target_dim": target_dim,
            "in_size": c,
            "eps": eps
        }
    }
    return {
        "layer_params": layer_params,
        "train_params": {
            "gamma": gamma,
            "beta": beta
        },
        "delta_params": {
            "delta_gamma": ggamma,
            "delta_beta": gbeta
        },
        "forward": {
            "bottoms": [x],
            "tops": [y]
        },
        "backward": {
            "bottoms": [x],
            "top_deltas": [gy],
            "bottom_deltas": [gx]
        }
    }
Beispiel #3
0
def convolution_2d(n, in_size, out_size, in_shape, ksize, stride, pad):
    from chainer.functions.connection.convolution_2d import Convolution2DFunction
    f = Convolution2DFunction(stride=stride, pad=pad)
    x = random_float32((n, in_size) + in_shape)
    W = random_float32((out_size, in_size) + ksize)
    b = random_float32((out_size, ))
    y, = f.forward((x, W, b))
    gy = random_float32(y.shape)
    gx, gW, gb = f.backward((x, W, b), (gy, ))

    # change order from (n, c, h, w) to (h, w, c, n), (out,in,h,w) to (h, w, in, out)
    x, W, y, gy, gx, gW = reorder_nchw_hwcn(x, W, y, gy, gx, gW)

    layer_params = {
        "type": "convolution_2d",
        "params": {
            "in_size": in_size,
            "out_size": out_size,
            "ksize": ksize,
            "stride": stride,
            "pad": pad
        }
    }

    return {
        "layer_params": layer_params,
        "train_params": {
            "weight": W,
            "bias": b
        },
        "delta_params": {
            "delta_weight": gW,
            "delta_bias": gb
        },
        "forward": {
            "bottoms": [x],
            "tops": [y]
        },
        "backward": {
            "bottoms": [x],
            "top_deltas": [gy],
            "bottom_deltas": [gx]
        }
    }
Beispiel #4
0
def pooling_2d(type, n, in_size, in_shape, ksize, stride, pad):
    from chainer.functions.pooling.max_pooling_2d import MaxPooling2D
    from chainer.functions.pooling.average_pooling_2d import AveragePooling2D
    if type == 'max':
        f = MaxPooling2D(stride=stride, pad=pad, ksize=ksize)
    elif type == 'average':
        f = AveragePooling2D(stride=stride, pad=pad, ksize=ksize)

    x = random_float32((n, in_size) + in_shape)
    y, = f.forward((x, ))
    gy = random_float32(y.shape)
    gx, = f.backward((x, ), (gy, ))

    x, y, gy, gx = reorder_nchw_hwcn(x, y, gy, gx)

    layer_params = {
        "type": "pooling_2d",
        "params": {
            "ksize": ksize,
            "stride": stride,
            "pad": pad,
            "type": type
        }
    }

    return {
        "layer_params": layer_params,
        "forward": {
            "bottoms": [x],
            "tops": [y]
        },
        "backward": {
            "bottoms": [x],
            "top_deltas": [gy],
            "bottom_deltas": [gx]
        }
    }
Beispiel #5
0
def convolution_2d_group(n, in_size, out_size, in_shape, ksize, stride, pad,
                         groups):
    from chainer.functions.connection.convolution_2d import Convolution2DFunction
    f = Convolution2DFunction(stride=stride, pad=pad)
    x = random_float32((n, in_size) + in_shape)
    in_group_size = in_size // groups
    out_group_size = out_size // groups
    W = random_float32((out_size, in_group_size) + ksize)
    b = random_float32((out_size, ))
    ys = []
    gys = []
    gxs = []
    gWs = []
    gbs = []
    for g in range(groups):
        xg = x[:, g * in_group_size:(g + 1) * in_group_size, :, :]
        Wg = W[g * out_group_size:(g + 1) * out_group_size, :, :, :]
        bg = b[g * out_group_size:(g + 1) * out_group_size]
        yg, = f.forward((xg, Wg, bg))
        gyg = random_float32(yg.shape)
        gxg, gWg, gbg = f.backward((xg, Wg, bg), (gyg, ))
        ys.append(yg)
        gys.append(gyg)
        gxs.append(gxg)
        gWs.append(gWg)
        gbs.append(gbg)

    y = np.concatenate(ys, axis=1)
    gy = np.concatenate(gys, axis=1)
    gx = np.concatenate(gxs, axis=1)
    gW = np.concatenate(gWs, axis=0)
    gb = np.concatenate(gbs, axis=0)
    # change order from (n, c, h, w) to (h, w, c, n), (out,in,h,w) to (h, w, in, out)
    x, W, y, gy, gx, gW = reorder_nchw_hwcn(x, W, y, gy, gx, gW)

    layer_params = {
        "type": "convolution_2d",
        "params": {
            "in_size": in_size,
            "out_size": out_size,
            "ksize": ksize,
            "stride": stride,
            "pad": pad,
            "group": groups
        }
    }

    return {
        "layer_params": layer_params,
        "train_params": {
            "weight": W,
            "bias": b
        },
        "delta_params": {
            "delta_weight": gW,
            "delta_bias": gb
        },
        "forward": {
            "bottoms": [x],
            "tops": [y]
        },
        "backward": {
            "bottoms": [x],
            "top_deltas": [gy],
            "bottom_deltas": [gx]
        }
    }