Exemple #1
0
def try_filters(file_name):
    img = cv2.imread(file_name)
    # cv2 format is:G B R, change it to R G B
    img1 = img[:, :, [2, 1, 0]]
    #plt.imshow(img2)
    #plt.show()
    img2 = cv2.cvtColor(img1, cv2.COLOR_RGB2GRAY)
    batch_size = 1
    input_channel = 1
    (height, width) = img2.shape
    FH = 3
    FW = 3
    print(img2.shape)
    data = img2.reshape((1, 1, height, width))
    hp = HyperParameters_4_2(0.1,
                             10,
                             batch_size,
                             net_type=NetType.MultipleClassifier,
                             init_method=InitialMethod.Xavier,
                             optimizer_name=OptimizerName.Momentum)
    conv = ConvLayer((1, height, width), (1, FH, FW), (1, 1), hp)
    conv.initialize("know_cnn", "name")

    filters = [
        np.array([0, -1, 0, -1, 5, -1, 0, -1, 0]),  # sharpness filter
        np.array([0, 0, 0, -1, 2, -1, 0, 0, 0]),  # vertical edge
        np.array([1, 1, 1, 1, -9, 1, 1, 1, 1]),  # surround
        np.array([-1, -2, -1, 0, 0, 0, 1, 2, 1]),  # sobel y
        np.array([0, 0, 0, 0, 1, 0, 0, 0, 0]),  # nothing
        np.array([0, -1, 0, 0, 2, 0, 0, -1, 0]),  # horizontal edge
        np.array([0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11,
                  0.11]),  # blur
        np.array([-1, 0, 1, -2, 0, 2, -1, 0, 1]),  # sobel x
        np.array([2, 0, 0, 0, -1, 0, 0, 0, -1])
    ]  # embossing

    filters_name = [
        "sharpness", "vertical edge", "surround", "sobel y", "nothing",
        "horizontal edge", "blur", "sobel x", "embossing"
    ]

    fig, ax = plt.subplots(nrows=3, ncols=3, figsize=(9, 9))
    for i in range(len(filters)):
        filter = np.repeat(filters[i],
                           input_channel).reshape(batch_size, input_channel,
                                                  FH, FW)
        conv.set_filter(filter, None)
        z = conv.forward(data)
        #z = normalize(z, 255)
        ax[i // 3, i % 3].imshow(z[0, 0])
        ax[i // 3, i % 3].set_title(filters_name[i])
        ax[i // 3, i % 3].axis("off")
    plt.suptitle("filters")
    plt.show()
    return z
def test_performance():
    batch_size = 64
    params = HyperParameters_4_2(0.1,
                                 1,
                                 batch_size,
                                 net_type=NetType.MultipleClassifier,
                                 init_method=InitialMethod.Xavier)
    stride = 1
    padding = 1
    fh = 3
    fw = 3
    input_channel = 3
    output_channel = 4
    iw = 28
    ih = 28
    # 64 个 3 x 28 x 28 的图像输入(模拟 mnist)
    x = np.random.randn(batch_size, input_channel, iw, ih)

    c1 = ConvLayer((input_channel, iw, ih), (output_channel, fh, fw),
                   (stride, padding), params)
    c1.initialize("test", "test", False)

    # dry run
    for i in range(5):
        f1 = c1.forward_numba(x)
        delta_in = np.ones((f1.shape))
        b1, dw1, db1 = c1.backward_numba(delta_in, 1)
    # run
    s1 = time.time()
    for i in range(100):
        f1 = c1.forward_numba(x)
        b1, dw1, db1 = c1.backward_numba(delta_in, 1)
    e1 = time.time()
    print("method numba:", e1 - s1)

    # dry run
    for i in range(5):
        f2 = c1.forward_img2col(x)
        b2, dw2, db2 = c1.backward_col2img(delta_in, 1)
    # run
    s2 = time.time()
    for i in range(100):
        f2 = c1.forward_img2col(x)
        b2, dw2, db2 = c1.backward_col2img(delta_in, 1)
    e2 = time.time()
    print("method img2col:", e2 - s2)

    print("compare correctness of method 1 and method 2:")
    print("forward:", np.allclose(f1, f2, atol=1e-7))
    print("backward:", np.allclose(b1, b2, atol=1e-7))
    print("dW:", np.allclose(dw1, dw2, atol=1e-7))
    print("dB:", np.allclose(db1, db2, atol=1e-7))
Exemple #3
0
def conv_relu_pool():
    img = cv2.imread(circle_pic)
    #img2 = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    batch_size = 1
    (height, width, input_channel) = img.shape
    FH = 3
    FW = 3
    data = np.transpose(img, axes=(2, 1, 0)).reshape(
        (batch_size, input_channel, width, height))
    hp = HyperParameters_4_2(0.1,
                             10,
                             batch_size,
                             net_type=NetType.MultipleClassifier,
                             init_method=InitialMethod.Xavier,
                             optimizer_name=OptimizerName.Momentum)
    conv = ConvLayer((input_channel, width, height), (1, FH, FW), (1, 0), hp)
    conv.initialize("know_cnn", "conv")
    kernal = np.array([-1, 0, 1, -2, 0, 2, -1, 0, 1])
    filter = np.repeat(kernal, input_channel).reshape(batch_size,
                                                      input_channel, FH, FW)
    conv.set_filter(filter, None)
    z1 = conv.forward(data)
    z2 = Relu().forward(z1)
    pool = PoolingLayer(z2[0].shape, (2, 2), 2, PoolingTypes.MAX)
    pool.initialize("know_cnn", "pool")
    z3 = pool.forward(z2)

    fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(8, 6))
    ax[0, 0].imshow(img[:, :, [2, 1, 0]])
    ax[0, 0].axis("off")
    ax[0, 0].set_title("source:" + str(img.shape))
    ax[0, 1].imshow(z1[0, 0].T)
    ax[0, 1].axis("off")
    ax[0, 1].set_title("conv:" + str(z1.shape))
    ax[1, 0].imshow(z2[0, 0].T)
    ax[1, 0].axis("off")
    ax[1, 0].set_title("relu:" + str(z2.shape))
    ax[1, 1].imshow(z3[0, 0].T)
    ax[1, 1].axis("off")
    ax[1, 1].set_title("pooling:" + str(z3.shape))

    plt.suptitle("conv-relu-pool")
    plt.show()
Exemple #4
0
def test_4d_im2col():
    batch_size = 2
    stride = 1
    padding = 0
    fh = 2
    fw = 2
    input_channel = 3
    output_channel = 2
    iw = 3
    ih = 3

    x = np.random.randn(batch_size, input_channel, iw, ih)
    params = HyperParameters_4_2(
        0.1, 1, batch_size,
        net_type=NetType.MultipleClassifier,
        init_method=InitialMethod.Xavier)
    c1 = ConvLayer((input_channel,iw,ih), (output_channel,fh,fw), (stride, padding), params)
    c1.initialize("test", "test", False)
    f1 = c1.forward_numba(x)
    f2 = c1.forward_img2col(x)
    print("correctness:", np.allclose(f1, f2, atol=1e-7))