Ejemplo n.º 1
0
    def test_maxpool_layer_1_forward(self):
        print('\n==================================')
        print('       Test maxpool layer forward      ')
        print('==================================')
        np.random.seed(123)
        in_dim, out_dim = 3, 3
        kernel_size, stride = 3, 1

        x = np.random.randn(1, in_dim, 5, 5)
        maxpool_layer = MaxPoolingLayer(kernel_size, stride)
        
        maxpool_out = maxpool_layer.forward(x)

        correct_out = [[[[1.65143654, 1.49138963, 1.49138963],
                        [2.20593008, 2.20593008, 2.18678609],
                        [2.20593008, 2.20593008, 2.18678609]],

                        [[0.9071052 , 0.9071052 , 0.92746243],
                        [0.68822271, 0.68822271, 0.92746243],
                        [2.39236527, 2.39236527, 2.23814334]],

                        [[1.75488618, 1.75488618, 1.75488618],
                        [1.75488618, 1.75488618, 1.75488618],
                        [1.16220405, 1.16220405, 1.41729905]]]]

        e = rel_error(correct_out, maxpool_out)
        print('Relative difference:', e)
        self.assertTrue(e <= 5e-8)
Ejemplo n.º 2
0
    def test_maxpool_layer_2_backward(self):
        print('\n==================================')
        print('       Test maxpool layer backward     ')
        print('==================================')
        np.random.seed(123)
        in_dim, out_dim = 3, 3
        kernel_size, stride = 3, 1

        x = np.random.randn(1, in_dim, 5, 5)
        maxpool_layer = MaxPoolingLayer(kernel_size, stride)

        maxpool_out = maxpool_layer.forward(x)
        d_prev = np.random.randn(*maxpool_out.shape)
        dx = maxpool_layer.backward(d_prev, 0.01)

        correct_dx = [[[[0., 0., 0., 0., 0.], [1.03972709, 0., 0., 0., 0.],
                        [0., 0., -0.52939562, 0., 0.],
                        [0., -1.47139598, 2.06254556, 0., 0.],
                        [0., 0., 0., 0., 0.]],
                       [[0., -1.40066055, 0., 0., 0.],
                        [0., 0., 0., 0., -0.56802076],
                        [0., 0., 0.87459023, 0., 0.], [0., 0., 0., 0., 0.],
                        [0., -0.11714651, 0., 0., 0.18103513]],
                       [[0., 0., 0., 0., 0.], [0., 0., -0.19467321, 0., 0.],
                        [0., 0., 0., 0., -1.97788793], [0., 0., 0., 0., 0.],
                        [0., 0., 0.26287825, 0., 0.]]]]

        dx_e = rel_error(correct_dx, dx)

        print('Relative difference dx:', dx_e)

        self.assertTrue(dx_e <= 5e-8)
Ejemplo n.º 3
0
def test_model(num_feat, num_classes):
    classifier = CNN_Classifier()
    classifier.add_layer(
        'Conv-1', ConvolutionLayer(num_feat, 2, kernel_size=3, stride=1,
                                   pad=1))
    classifier.add_layer('ReLU', ReLU())
    classifier.add_layer(
        'Conv-2', ConvolutionLayer(2, 3, kernel_size=3, stride=1, pad=1))
    classifier.add_layer('tanh', Tanh())
    classifier.add_layer(
        'Conv-3', ConvolutionLayer(3, 3, kernel_size=3, stride=1, pad=0))
    classifier.add_layer('Sigmoid', Sigmoid())
    classifier.add_layer('Max-pool - 1',
                         MaxPoolingLayer(kernel_size=2, stride=1))
    classifier.add_layer('FC-4', FCLayer(12, num_classes))
    classifier.add_layer('Softmax', SoftmaxLayer())
    return classifier
Ejemplo n.º 4
0
# 아래 예시를 참고하여 과제에 주어진 대로 (혹은 과제와 별개로 원하는 대로) Layer를 추가하세요.

# Convolution Layer
CNN.add_layer('Conv Layer - 1', ConvolutionLayer(in_channels=in_channnel, out_channels=8, kernel_size=3, pad=1))

# ReLU Layer
CNN.add_layer('ReLU Layer - 1', ReLU())

# Convolution Layer 
CNN.add_layer('Conv Layer - 2', ConvolutionLayer(in_channels=8, out_channels=8, kernel_size=3, pad=1))

# ReLU Layer
CNN.add_layer('ReLU Layer - 2', ReLU())

# Max-pool Layer
CNN.add_layer('Max-Pool Layer', MaxPoolingLayer(kernel_size=2, stride=2))

# FC Layer
CNN.add_layer('FC Example Layer - 1', FCLayer(input_dim=1568, output_dim=500))

# FC Layer
CNN.add_layer('FC Example Layer - 2', FCLayer(input_dim=500, output_dim=5))

# Softmax Layer
# 이 layer는 항상 마지막에 추가
CNN.add_layer('Softmax Layer', SoftmaxLayer())

# Model Architecture 출력
CNN.summary()

# Hyper-parameters
Ejemplo n.º 5
0
conv_dx = conv_layer.backward(conv_out)
conv_dW = conv_layer.dW
conv_db = conv_layer.db

print('Convolution Layer Backward Check')
print('dx Difference : ', (conv_dx - correct_conv_dx).sum())
print('dW Difference : ', (conv_dW - correct_conv_dW).sum())
print('db Difference : ', (conv_db - correct_conv_db).sum())
print()
# ===========================================================================

print(
    '============================ 4. Pooling Layer ============================='
)
kernel_size = 2
max_pool = MaxPoolingLayer(kernel_size, stride=kernel_size)
x = np.array([[5, 1, 3, 10], [-1, 0, 4, -1], [-10, -1, 11, 1], [0, 1, -5, 3]],
             dtype=np.float32).reshape(1, 1, 4, 4)
pooled = max_pool.forward(x)
d_pool = max_pool.backward(np.ones_like(pooled))

print('Difference pool out: ', (correct_pooled - pooled).sum())
print('Difference d_pool: ', (correct_d_pool - d_pool).sum())
print()

# ===========================================================================

print(
    '=============================== 5. FC Layer ==============================='
)
fc_layer = FCLayer(2, 2)
Ejemplo n.º 6
0
# Add Layers, Layer 추가 예제
# 아래 예시를 참고하여 과제에 주어진 대로 (혹은 과제와 별개로 원하는 대로) Layer를 추가하세요.

# Convolution Layer
CNN.add_layer(
    'Conv Example Layer',
    ConvolutionLayer(in_channels=in_channnel,
                     out_channels=16,
                     kernel_size=5,
                     pad=1))

# ReLU Layer
CNN.add_layer('ReLU Example Layer', ReLU())

# Max-pool Layer
CNN.add_layer('Max-Pool Example Layer', MaxPoolingLayer(kernel_size=3,
                                                        stride=3))

# FC Layer
CNN.add_layer('FC Example Layer', FCLayer(input_dim=1234, output_dim=123))

# Softmax Layer
# 이 layer는 항상 마지막에 추가
CNN.add_layer('Softmax Layer', SoftmaxLayer())

# Model Architecture 출력
CNN.summary()

# Hyper-parameters
num_epochs = None
learning_rate = None
print_every = None
Ejemplo n.º 7
0
train_accuracy = []
valid_accuracy = []

best_epoch = -1
best_acc = -1
best_model = None

# =============================== EDIT HERE ===============================

# Add layers
CNN.add_layer('Conv-1', ConvolutionLayer(in_channels=in_channels, out_channels=4, kernel_size=3, pad=1))
CNN.add_layer('ReLU-1',ReLU())
CNN.add_layer('Conv-2', ConvolutionLayer(in_channels=4, out_channels=4, kernel_size=3, pad=1))
CNN.add_layer('ReLU-2',ReLU())
CNN.add_layer('Max-pool-1',MaxPoolingLayer(2,2))
CNN.add_layer('FC-1',FCLayer(784,500))
CNN.add_layer('ReLU-3',ReLU())
CNN.add_layer('FC-2',FCLayer(500,10))
CNN.add_layer('Softmax Layer',SoftmaxLayer())

# =========================================================================

CNN.summary()

print('Training Starts...')
num_batch = int(np.ceil(num_train / batch_size))

for epoch in range(1, num_epochs + 1):
    start = time.time()
    epoch_loss = 0.0