def test_convolution_layer(input_data):
    W = np.random.randn(1, 3, 2, 2)
    b = np.random.randn(1)

    myconv = Convolution(W, b)
    myresult = myconv.forward(input_data)

    bookconv = book_layers.Convolution(W, b)
    bookconvresult = bookconv.forward(input_data)

    print(f"test result of test_convolution_layer: {(myresult==bookconvresult).all()}")
def test_convolution_backward(input_data):
    W = np.random.randn(1, 3, 2, 2)
    b = np.random.randn(1)

    myconv = Convolution(W, b)
    myresult = myconv.forward(input_data)

    bookconv = book_layers.Convolution(W, b)
    bookconvresult = bookconv.forward(input_data)

    dx = np.random.randn(*myresult.shape)
    dx2 = dx.copy()
    my_b_r = myconv.backward(dx)
    book_b_r = bookconv.backward(dx2)
    #print(my_b_r)
    #print(book_b_r)
    different = (my_b_r-book_b_r)
    print(f"test result of test_pooling_layer: {different.max()<1e-10}")
Exemple #3
0
network = SimpleConvNet(input_dim=(1,28,28), 
                        conv_param = {'filter_num':30, 'filter_size':5, 'pad':0, 'stride':1},
                        hidden_size=100, output_size=10, weight_init_std=0.01)

# 学習後の重み
network.load_params("params.pkl")

filter_show(network.params['W1'], 16)

img = imread('../data/lena_gray.png')
img = img.reshape(1, 1, *img.shape)

fig = plt.figure()

w_idx = 1

for i in range(16):
    w = network.params['W1'][i]
    b = 0  # network.params['b1'][i]

    w = w.reshape(1, *w.shape)
    #b = b.reshape(1, *b.shape)
    conv_layer = Convolution(w, b) 
    out = conv_layer.forward(img)
    out = out.reshape(out.shape[2], out.shape[3])
    
    ax = fig.add_subplot(4, 4, i+1, xticks=[], yticks=[])
    ax.imshow(out, cmap=plt.cm.gray_r, interpolation='nearest')

plt.show()
Exemple #4
0
from keras import backend as K

import warnings
warnings.filterwarnings('ignore')

inputs = np.random.uniform(size=(10, 3, 30, 30))
params = {
    'kernel_h': 5,
    'kernel_w': 5,
    'pad': 0,
    'stride': 2,
    'in_channel': inputs.shape[1],
    'out_channel': 64,
}
layer = Convolution(params)
out = layer.forward(inputs)

keras_model = keras.Sequential()
keras_layer = layers.Conv2D(filters=params['out_channel'],
                            kernel_size=(params['kernel_h'],
                                         params['kernel_w']),
                            strides=(params['stride'], params['stride']),
                            padding='valid',
                            data_format='channels_first',
                            input_shape=inputs.shape[1:])
keras_model.add(keras_layer)
sgd = optimizers.SGD(lr=0.01)
keras_model.compile(loss='mean_squared_error', optimizer='sgd')
weights = np.transpose(layer.weights, (2, 3, 1, 0))
keras_layer.set_weights([weights, layer.bias])
keras_out = keras_model.predict(inputs, batch_size=inputs.shape[0])