コード例 #1
0
def main(args):
    print('[*] Arguments: %s' % args)
    print('[*] Read MNIST...')
    num_test_images = args.num_test_images
    images, labels = load_mnist('test', path='./data', max_ind=num_test_images)
    images, labels = images[:num_test_images, :, :], labels[:num_test_images]
    images = images.astype(np.float32)
    images = images / 255.
    print('[*] The shape of image: %s' % str(images.shape))

    print('[*] Load the network...')
    if args.network == 'mlp':  # Lab 2
        if args.quantized:
            print('[!] MLP does not support quantization')
            return
        model_path = os.path.join('./pretrained_weights',
                                  'mlp_iter_10000.caffemodel')
        net = MLP(model_path, args)
    elif args.network == 'cnn':
        if args.quantized:  # Lab 14
            model_path = os.path.join('./pretrained_weights',
                                      'quantized_cnn_weights.txt')
        else:  # Lab 11
            model_path = os.path.join('./pretrained_weights',
                                      'cnn_weights.txt')
        net = CNN(model_path, args)
    else:
        raise

    print('[*] Run tests...')
    test_images = [images[i, :, :].copy() for i in xrange(num_test_images)]
    n_correct = 0
    start_time = time.time()

    for i in xrange(num_test_images):
        X = test_images[i]
        X = X.reshape((28 * 28))  # 28x28->784

        logit = net.inference(X)
        prediction = np.argmax(logit)
        label = labels[i, ]

        n_correct += (label == prediction)

    print('[*] Statistics...')
    model_stats = {
        'total_time': time.time() - start_time,
        'total_image': num_test_images,
        'accuracy': float(n_correct) / num_test_images,
        'avg_num_call': net.total_num_call[0] / num_test_images,
        'm_size': net.m_size,
        'v_size': net.v_size,
    }
    pp.pprint(model_stats)
コード例 #2
0
        self.total_num_call = np.zeros(1, dtype=np.int)
        self.output = np.zeros(10, dtype=np.float32)

    def __del__(self):
        self.lib.delNet(self.net)

    def inference(self, input):
        self.lib.inference(self.net, input.ctypes.data,
                           self.output.ctypes.data,
                           self.total_num_call.ctypes.data)
        return self.output


if __name__ == "__main__":
    print("read dataset...")
    images, labels = load_mnist("test", path="./data")
    images, labels = images[:NUM_TEST_IMAGES, :, :], labels[:NUM_TEST_IMAGES]
    images = images.astype(np.float32)
    images = images / 255.
    print("images", images.shape)

    print("create network...")
    model_path = os.path.join("./pretrained_weights",
                              "mlp_iter_10000.caffemodel")
    net = MLP(model_path, m_size=M_SIZE, v_size=V_SIZE)
    test_images = [images[i, :, :].copy() for i in xrange(NUM_TEST_IMAGES)]

    print("run test...")
    n_correct = 0
    start_time = time.time()
    for i in xrange(NUM_TEST_IMAGES):