Ejemplo n.º 1
0
    h = F.dropout(F.relu(model.fl4(h)), train=False)
    y = model.fl5(h)

    return y


cnn = Classifier(model, gpu=-1)
cnn.set_forward(forward)

mnist = fetch_mldata('MNIST original', data_home='.')
perm = numpy.random.permutation(len(mnist.data))
mnist.data = mnist.data.astype(numpy.float32).reshape(70000, 1, 28, 28) / 255
mnist.target = mnist.target.astype(numpy.int32)
train_data = mnist.data[perm][:60000]
train_label = mnist.target[perm][:60000]
test_data = mnist.data[perm][60000:]
test_label = mnist.target[perm][60000:]

for epoch in range(15):
    print('epoch : %d' % (epoch + 1))
    err, acc = cnn.train(train_data, train_label, batchsize=200)
    print(acc, err)
    perm = numpy.random.permutation(len(test_data))
    terr, tacc = cnn.test(test_data[perm][:100], test_label[perm][:100])
    print(tacc, terr)

    with open('cnn.log', mode='a') as f:
        f.write("%d %f %f\n" % (epoch + 1, err, terr))

cnn.save_param('cnn.param.npy')
Ejemplo n.º 2
0
    h = F.max_pooling_2d(F.relu(h), 2)
    h = model.bn2(model.conv2(h))
    if layer == 2:
        return h

    h = F.max_pooling_2d(F.relu(h), 2)
    h = model.conv3(h)
    if layer == 3:
        return h

    return None


cnn.set_output(output)
imager = V.Visualizer(cnn)
imager.plot_filters('conv1')
imager.write_filters('conv1',
                     path='./filter',
                     identifier='filter_',
                     type='bmp')

imager.plot_output(numpy.array(arr).astype(numpy.float32)[:3], layer=1)
imager.write_output(numpy.array(arr).astype(numpy.float32),
                    layer=2,
                    path='./outputs',
                    identifier='l1_',
                    type='bmp')
imager.write_activation(arr[0], 2)

cnn.save_param()
Ejemplo n.º 3
0
    h = F.dropout(F.relu(model.fl4(h)), train=False)
    y = model.fl5(h)

    return y


cnn = Classifier(model, gpu=-1)
cnn.set_forward(forward)

mnist = fetch_mldata('MNIST original', data_home='.')
perm = numpy.random.permutation(len(mnist.data))
mnist.data = mnist.data.astype(numpy.float32).reshape(70000, 1, 28, 28) / 255
mnist.target = mnist.target.astype(numpy.int32)
train_data = mnist.data[perm][:60000]
train_label = mnist.target[perm][:60000]
test_data = mnist.data[perm][60000:]
test_label = mnist.target[perm][60000:]

for epoch in range(15):
    print('epoch : %d' % (epoch + 1))
    err, acc = cnn.train(train_data, train_label, batchsize=200)
    print(acc, err)
    perm = numpy.random.permutation(len(test_data))
    terr, tacc = cnn.test(test_data[perm][:100], test_label[perm][:100])
    print(tacc, terr)

    with open('cnn.log', mode='a') as f:
        f.write("%d %f %f\n" % (epoch + 1, err, terr))

cnn.save_param('cnn.param.npy')
Ejemplo n.º 4
0

def output(self, x, layer):
    h = model.bn1(model.conv1(x))
    if layer == 1:
        return h

    h = F.max_pooling_2d(F.relu(h), 2)
    h = model.bn2(model.conv2(h))
    if layer == 2:
        return h

    h = F.max_pooling_2d(F.relu(h), 2)
    h = model.conv3(h)
    if layer == 3:
        return h

    return None


cnn.set_output(output)
imager = V.Visualizer(cnn)
imager.plot_filters('conv1')
imager.write_filters('conv1', path='./filter', identifier='filter_', type='bmp')

imager.plot_output(numpy.array(arr).astype(numpy.float32)[:3], layer=1)
imager.write_output(numpy.array(arr).astype(numpy.float32), layer=2, path='./outputs', identifier='l1_', type='bmp')
imager.write_activation(arr[0], 2)

cnn.save_param()