Exemplo n.º 1
0
def loadFCWeightsBias(data_dir):
    weight = []
    fname = "%s/fc" % data_dir
    if not os.path.exists(fname):
        nearMatch = getNearFileMatchWithPrefix(data_dir, "fc")
        if nearMatch:
            fname = nearMatch
    if os.path.exists(fname):
        with open(fname, 'r') as f:
            line = f.read()
            vals = line.strip().split(' ')
            weight = [float(v) for v in vals]
    else:
        print(("No FC layers found in %s" % data_dir))
        return (None, None)

    bias = []
    fname = "%s/fc_bias" % data_dir
    if not os.path.exists(fname):
        nearMatch = getNearFileMatchWithPrefix(data_dir, "fc_bias")
        if nearMatch:
            fname = nearMatch
    with open(fname, 'r') as f:
        line = f.read()
        vals = line.strip().split(' ')
        bias = [float(v) for v in vals]

    (weightFpga, fpgaHandle) = xdnn.makeFPGAFloatArray(len(weight))
    (biasFpga, fpgaHandle) = xdnn.makeFPGAFloatArray(len(bias))

    weightFpga[:] = weight
    biasFpga[:] = bias

    return (weightFpga, biasFpga)
Exemplo n.º 2
0
def prepareOutput(output_sz, batch_sz):
    """
    Allocate memory for outputs.

    :param output_sz: Number of elements in the output volume returned by FPGA for a single inference. This is specific to the network. 
    :type output_sz: int.
    :param batch_sz: Number of images to be processed simultaneously.
    :type batch_sz: int.
    :returns: numpy.ndarray -- 1D Array large enough to hold output_sz*batch_sz elements.
    """
    (fpgaOutput, fpgaHandle) = xdnn.makeFPGAFloatArray(output_sz * batch_sz)

    return fpgaOutput
Exemplo n.º 3
0
def init_fpga():
    global g_inputs
    global g_inputbuf
    global g_fpgaOutput
    global g_weightsBlob
    global g_fcWeight
    global g_fcBias
    print(" --- INIT FPGA --- \n")
    print("xclbin: {0}.\n".format(g_xclbin))
    print("xdnnLib: {0}.\n".format(g_xdnnLib))
    ret = xdnn.createManager(g_xdnnLib)
    if ret != True:
        raise SystemExit("Error: xdnn createManager failed.")
    (g_fcWeight, g_fcBias) = xdnn_io.loadFCWeightsBias(g_xdnnTestDataDir)

    ret = xdnn.createHandle(g_xclbin, "kernelSxdnn_0", g_xdnnLib, g_numDevices)
    if ret:
        raise SystemExit("ERROR: Unable to create handle to FPGA")
    else:
        print("INFO: Sucessfully create handle to FPGA.")

    # magics.   See ml-suite/notebooks tutorial.   Should we overwrite PE?
    args = {
        'datadir': g_xdnnTestDataDir,
        'quantizecfg': g_fpgaCfgFile,
        'scaleA': g_scaleA,
        'scaleB': g_scaleB,
        'PE': -1,
        'netcfg': g_netFile
    }

    print(" --- load weights --- \n")
    g_weightsBlob = xdnn_io.loadWeightsBiasQuant(args)

    print(" --- read lable file --- \n")
    with open(g_lableFile, 'r') as f:
        for line in f:
            g_labelarray.append(line.strip())

    print(" --- prepare inputs --- \n")
    g_inputs = np.zeros((g_batchSize, g_img_c * g_img_h * g_img_w),
                        dtype=np.float32)
    g_inputbuf = np.zeros((g_batchSize, g_img_c, g_img_h, g_img_w),
                          dtype=np.float32)

    print "g_inputs", g_inputs

    print(" --- prepare outputs --- \n")
    g_fpgaOutput, fpgaHandle = xdnn.makeFPGAFloatArray(g_fpgaOutputSize *
                                                       g_batchSize)
Exemplo n.º 4
0
def prepareOutput(num):
    (fpgaOutput, fpgaHandle) \
       = xdnn.makeFPGAFloatArray(g_fpgaOutputSize * num)

    return fpgaOutput
Exemplo n.º 5
0
def prepareOutput(output_sz, batch_sz):
    (fpgaOutput, _) = xdnn.makeFPGAFloatArray(output_sz * batch_sz)

    return fpgaOutput