示例#1
0
def main():
    args = get_args()

    # Defining network first
    x = nn.Variable((1, 3, 224, 224))
    y = darknet19.darknet19_classification(x / 255, test=True)

    # Get NNabla parameters
    params = nn.get_parameters(grad_only=False)

    # Parse Darknet weights and store them into NNabla params
    dn_weights = parser.load_weights_raw(args.input)
    cursor = 0
    for i in range(1, 19):  # 1 to 18
        cursor = parser.load_convolutional_and_get_next_cursor(
            dn_weights, cursor, params, 'c{}'.format(i))
    cursor = parser.load_convolutional_and_get_next_cursor(
        dn_weights, cursor, params, 'c19', no_bn=True)

    nn.save_parameters(args.output)
示例#2
0
def main():
    args = get_args()
    from nnabla.ext_utils import get_extension_context
    ctx = get_extension_context(args.context)
    nn.set_default_context(ctx)

    nn.load_parameters(args.weights)
    x = nn.Variable((1, 3, args.size, args.size))
    y = darknet19.darknet19_classification(x / 255, test=True)

    label_names = np.loadtxt('imagenet.shortnames.list',
                             dtype=str,
                             delimiter=',')[:1000]

    img = imread(args.input)
    img = imresize(img, (args.size, args.size))

    x.d = img.transpose(2, 0, 1).reshape(1, 3, args.size, args.size)
    y.forward(clear_buffer=True)

    # softmax
    p = F.reshape(F.mul_scalar(F.softmax(y.data), 100), (y.size, ))

    # Show top-5 prediction
    inds = np.argsort(y.d.flatten())[::-1][:5]
    for i in inds:
        print('{}: {:.1f}%'.format(label_names[i], p.data[i]))

    s = time.time()
    n_time = 10
    for i in range(n_time):
        y.forward(clear_buffer=True)
    # Invoking device-to-host copy to synchronize the device (if CUDA).
    _ = y.d
    print("Processing time: {:.1f} [ms/image]".format(
        (time.time() - s) / n_time * 1000))