示例#1
0
def InferImage(net, image, labels):
    import numpy as np
    import xdnn_io
    global board_avail
    transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
    transformer.set_transpose('data', (2, 0, 1))
    transformer.set_mean('data', np.array([104, 117, 123]))
    transformer.set_raw_scale('data', 255)
    transformer.set_channel_swap('data',
                                 (2, 1, 0))  # if using RGB instead if BGR
    img = caffe.io.load_image(image)
    net.blobs['data'].data[...] = transformer.preprocess('data', img)
    ptxtShape = net.blobs["data"].data.shape
    print("Running with shape of: ", ptxtShape)
    with board_avail:
        out = net.forward()
        for key in out:
            try:
                if out[key].shape[1] == 1000:
                    softmax = out[key]
            except:
                pass
        Labels = xdnn_io.get_labels(labels)
        xdnn_io.printClassification(softmax, [image], Labels)
        result = xdnn_io.getClassification(softmax, [image], Labels)
    return result
def post_process(qFrom, args, img_paths, streamQ, fpgaOutputs):
    numProcessed = 0
    labels = xdnn_io.get_labels(args['labels'])
    zmqPub = None
    if args['zmqpub']:
        zmqPub = ZmqResultPublisher(args['deviceID'])
    goldenMap = None
    if args['golden']:
        goldenMap = xdnn_io.getGoldenMap(args['golden'])
        top5Count = 0
        top1Count = 0

    (fcWeight, fcBias) = xdnn_io.loadFCWeightsBias(args)
    bsz = args['batch_sz']
    fcOutput = np.empty((
        bsz,
        args['outsz'],
    ), dtype=np.float32, order='C')
    start = 0
    while True:
        (sId, img_idx) = qFrom.get()
        if numProcessed == 0:
            start = timeit.default_timer()
        if sId is None or img_idx is None:
            break

        imgList = []
        for x in np.nditer(img_idx):
            if x >= 0:
                imgList.append(img_paths[x])
                numProcessed += 1

        npout_view = np.frombuffer(fpgaOutputs[sId].get_obj(),
                                   dtype=np.float32)
        xdnn.computeFC(fcWeight, fcBias, npout_view, bsz, args['outsz'],
                       args['fpgaoutsz'], fcOutput)
        streamQ.put(sId)

        smaxOutput = xdnn.computeSoftmax(fcOutput)
        if args['golden']:
            for i, p in enumerate(imgList):
                top1Count += xdnn_io.isTopK(smaxOutput[i], goldenMap, p,
                                            labels, 1)
                top5Count += xdnn_io.isTopK(smaxOutput[i], goldenMap, p,
                                            labels, 5)

        if zmqPub is not None:
            predictMsg = xdnn_io.getClassification(smaxOutput,
                                                   imgList,
                                                   labels,
                                                   zmqPub=True)
            zmqPub.send(predictMsg)

    print("%g images/s" % (float(numProcessed) / (time.time() - start)))

    if args['golden']:
        print ("\nAverage accuracy (n=%d) Top-1: %.1f%%, Top-5: %.1f%%\n") \
          % (numProcessed,
             float(top1Count)/float(numProcessed)*100.,
             float(top5Count)/float(numProcessed)*100.)
示例#3
0
def main(argv):
    args = xdnn_io.processCommandLine(argv)
    ret, handles = xdnn.createHandle(args['xclbin'], "kernelSxdnn_0")
    # ret = xdnn.createHandle(g_xclbin, "kernelSxdnn_0", g_xdnnLib)
    if ret != 0:
      sys.exit(1)
    labels = xdnn_io.get_labels(args['labels'])

    # TODO dict of tuples instead?
    fpgaRT          = {}
    fpgaOutputs     = {}
    fcWeights       = {}
    fcBiases        = {}
    netFiles        = {}
    confNames       = []

    args = args['jsoncfg']      # we do not use other args' keys
    for netconf_args in args:
      
      confName   = str(netconf_args['name'])
      confNames += [confName]
      # netconf_args['netcfg'] = './data/{}_{}.json'.format(netconf_args['net'], netconf_args['dsp'])
      fpgaRT[confName] = xdnn.XDNNFPGAOp(handles, netconf_args)
      netconf_args['in_shape'] = tuple((netconf_args['batch_sz'],) + tuple(fpgaRT[confName].getInputDescriptors().itervalues().next()[1:] )) 
      (fcWeights[confName],
        fcBiases[confName]) = xdnn_io.loadFCWeightsBias(netconf_args)
      fpgaOutputs[confName]             = np.empty ((netconf_args['batch_sz'], int(netconf_args['fpgaoutsz']),), dtype=np.float32, order='C')
      netFiles[confName]                = str(netconf_args['netcfg'])

    batchArrays = []
    for streamId, netconf_args in enumerate(args):
      batchArrays.append(np.empty(netconf_args['in_shape'], dtype=np.float32, order='C'))
      pl = []
      img_paths = xdnn_io.getFilePaths(netconf_args['images'])
      for j, p in enumerate(img_paths[:netconf_args['batch_sz']]):
        batchArrays[-1][j, ...], _ = xdnn_io.loadImageBlobFromFile(p, netconf_args['img_raw_scale'],
                                                                  netconf_args['img_mean'],
                                                                  netconf_args['img_input_scale'],
                                                                  netconf_args['in_shape'][2],
                                                                  netconf_args['in_shape'][3])
        pl.append(p)

      confName = str(netconf_args['name'])
      firstInputName = fpgaRT[confName].getInputs().iterkeys().next()
      firstOutputName = fpgaRT[confName].getOutputs().iterkeys().next()
      fpgaRT[confName].exec_async({ firstInputName : batchArrays[-1] }, { firstOutputName : fpgaOutputs[confName] }, streamId)

    for streamId, confName in enumerate(confNames):
      fpgaRT[confName].get_result (streamId)

    for netconf_args in args:
      confName = str(netconf_args['name'])
      fcOut = np.empty( (netconf_args['batch_sz'], netconf_args['outsz']), dtype=np.float32, order = 'C')
      xdnn.computeFC (fcWeights[confName], fcBiases[confName], fpgaOutputs[confName], fcOut)

      softmaxOut = xdnn.computeSoftmax(fcOut)
      xdnn_io.printClassification(softmaxOut, netconf_args['images'], labels);

    xdnn.closeHandle()
示例#4
0
def init_fpga():
    # Instead of using command line, we hard code it here.
    # Typing correct args is almost impossible so either do it in .sh or .py
    #
    global g_args
    global g_ctxt
    print(" --- INIT FPGA --- \n")
    xdnnArgs = build_xdnn_args()
    print(xdnnArgs)
    g_args = xdnn_io.processCommandLine(xdnnArgs)
    print(" --- After parsing --- \n")
    print(g_args)

    print(" --- Create handle --- \n")
    ret, handles = xdnn.createHandle(g_args['xclbin'], "kernelSxdnn_0")
    if ret != 0:
        print(" --- !!! FAILED: Cannot create handle. --- \n")
        sys.exit(1)

    print(" --- Create fpgaRT --- \n")
    fpgaRT = xdnn.XDNNFPGAOp(handles, g_args)
    g_ctxt["fpgaRT"] = fpgaRT

    print(" --- Weight and Bias --- \n")
    fcWeight, fcBias = xdnn_io.loadFCWeightsBias(g_args)
    g_ctxt["fcWeight"] = fcWeight
    g_ctxt["fcBias"] = fcBias

    print(" --- Init input input/output area --- \n")
    if is_deploymode():
        g_ctxt['fpgaOutput'] = fpgaRT.getOutputs()
        g_ctxt['fpgaInput'] = fpgaRT.getInputs()
        g_ctxt['inShape'] = (g_args['batch_sz'], ) + tuple(
            fpgaRT.getInputDescriptors().itervalues().next()[1:])
    else:
        g_ctxt['fpgaOutput'] = np.empty((
            g_args['batch_sz'],
            g_args['fpgaoutsz'],
        ),
                                        dtype=np.float32,
                                        order='C')
        g_ctxt['batch_array'] = np.empty(
            ((g_args['batch_sz'], ) + g_args['in_shape']),
            dtype=np.float32,
            order='C')

    g_ctxt['fcOutput'] = np.empty((
        g_args['batch_sz'],
        g_args['outsz'],
    ),
                                  dtype=np.float32,
                                  order='C')

    print(" --- Get lables --- \n")
    g_ctxt['labels'] = xdnn_io.get_labels(g_args['labels'])
    # golden?   What is that?
    # Seems we are done.

    print(" --- FPGA INITIALIZED! ---\n")
示例#5
0
def main():
    args = xdnn_io.processCommandLine()

    ret, handles = xdnn.createHandle(args['xclbin'], "kernelSxdnn_0")
    if ret != 0:
        sys.exit(1)
    fpgaRT = xdnn.XDNNFPGAOp(handles, args)
    fcWeight, fcBias = xdnn_io.loadFCWeightsBias(args)
    img_paths = xdnn_io.getFilePaths(args['images'])
    fpgaOutput = np.empty((
        args['batch_sz'],
        args['fpgaoutsz'],
    ),
                          dtype=np.float32,
                          order='C')
    fcOutput = np.empty((
        args['batch_sz'],
        args['outsz'],
    ),
                        dtype=np.float32,
                        order='C')
    batch_array = np.empty(((args['batch_sz'], ) + args['in_shape']),
                           dtype=np.float32,
                           order='C')
    labels = xdnn_io.get_labels(args['labels'])
    if args['golden']:
        goldenMap = xdnn_io.getGoldenMap(args['golden'])
        top5Count = 0
        top1Count = 0

    for i in xrange(0, len(img_paths), args['batch_sz']):
        pl = []
        for j, p in enumerate(img_paths[i:i + args['batch_sz']]):
            batch_array[j, ...], _ = xdnn_io.loadImageBlobFromFile(
                p, args['img_raw_scale'], args['img_mean'],
                args['img_input_scale'], args['in_shape'][2],
                args['in_shape'][1])
            pl.append(p)

        fpgaRT.execute(batch_array, fpgaOutput)
        xdnn.computeFC(fcWeight, fcBias, fpgaOutput, args['batch_sz'],
                       args['outsz'], args['fpgaoutsz'], fcOutput)
        softmaxOut = xdnn.computeSoftmax(fcOutput)
        xdnn_io.printClassification(softmaxOut, pl, labels)
        if args['golden']:
            for j, p in enumerate(img_paths[i:i + args['batch_sz']]):
                top1Count += xdnn_io.isTopK(softmaxOut[j], goldenMap, p,
                                            labels, 1)
                top5Count += xdnn_io.isTopK(softmaxOut[j], goldenMap, p,
                                            labels, 5)

    xdnn.closeHandle()
    if args['golden']:
        print("\nAverage accuracy (n=%d) Top-1: %.1f%%, Top-5: %.1f%%\n") % (
            len(img_paths), float(top1Count) / float(len(img_paths)) * 100.,
            float(top5Count) / float(len(img_paths)) * 100.)
示例#6
0
    def __init__(self, maxNumStreams):
        self._maxNumStreams = maxNumStreams
        self._streamsAvailable = []
        self._streamInputs = []
        self._streamOutputs = []

        self._config = xdnn_io.processCommandLine()
        ret, handles = xdnn.createHandle(self._config['xclbin'])
        if ret != 0:
            sys.exit(1)

        self._fpgaRT = xdnn.XDNNFPGAOp(handles, self._config)
        self._fcWeight, self._fcBias = xdnn_io.loadFCWeightsBias(self._config)
        self._labels = xdnn_io.get_labels(self._config['labels'])

        for i in range(maxNumStreams):
            self._streamsAvailable.append(i)
            self._streamInputs.append(None)
            self._streamOutputs.append(None)