Example #1
0
def main():
  args = xdnn_io.processCommandLine()

  runner = Runner(args['vitis_rundir'])
  inTensors = runner.get_input_tensors()
  outTensors = runner.get_output_tensors()
  batch_sz = args['batch_sz']
  if batch_sz == -1:
    # use Runner's suggested batch size
    batch_sz = inTensors[0].dims[0]

  if args['golden']:
    goldenMap = xdnn_io.getGoldenMap(args['golden'])
    top5Count = 0
    top1Count = 0

  fpgaBlobs = []
  for io in [inTensors, outTensors]:
    blobs = []
    for t in io:
      shape = (batch_sz,) + tuple([t.dims[i] for i in range(t.ndims)][1:])
      blobs.append(np.empty((shape), dtype=np.float32, order='C'))
    fpgaBlobs.append(blobs)

  img_paths = xdnn_io.getFilePaths(args['images'])
  labels = xdnn_io.get_labels(args['labels'])
  xdnnCPUOp = xdnn.XDNNCPUOp("%s/weights.h5" % args['vitis_rundir'])
  fcOutput = np.empty((batch_sz, args['outsz'],), dtype=np.float32, order='C')

  fpgaInput = fpgaBlobs[0][0]
  for i in range(0, len(img_paths), batch_sz):
    pl = []
    # fill tensor input data from image file
    for j, p in enumerate(img_paths[i:i + batch_sz]):
      img, _ = xdnn_io.loadImageBlobFromFile(p,
        args['img_raw_scale'], args['img_mean'], args['img_input_scale'],
        fpgaInput.shape[2], fpgaInput.shape[3])
      pl.append(p)
      np.copyto(fpgaInput[j], img)

    jid = runner.execute_async(fpgaBlobs[0], fpgaBlobs[1])
    runner.wait(jid)

    xdnnCPUOp.computeFC(fpgaBlobs[1][0], fcOutput)
    softmaxOut = xdnnCPUOp.computeSoftmax(fcOutput)
    if args['golden']:
      for j,p in enumerate(img_paths[i:i + batch_sz]):
        top1Count += xdnn_io.isTopK(softmaxOut[j], goldenMap, p, labels, 1)
        top5Count += xdnn_io.isTopK(softmaxOut[j], goldenMap, p, labels, 5)
    else:
      xdnn_io.printClassification(softmaxOut, pl, labels)

  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.) )
Example #2
0
    def run(self, imgList, fpgaOutput_list, fpgaOutputShape_list, shape_list):
        fpgaOutput = fpgaOutput_list[0]
        fpgaOutputShape = fpgaOutputShape_list[0]
        if self.numProcessed == 0:
            self.startTime = timeit.default_timer()
            self.labels = xdnn_io.get_labels(self.args['labels'])
            self.zmqPub = None
            if self.args['zmqpub']:
                self.zmqPub = ZmqResultPublisher(self.args['deviceID'])
            self.goldenMap = None
            if self.args['golden']:
                self.goldenMap = xdnn_io.getGoldenMap(self.args['golden'])
                self.top5Count = 0
                self.top1Count = 0
            self.fcOutput = np.empty((
                self.args['batch_sz'],
                self.args['outsz'],
            ),
                                     dtype=np.float32,
                                     order='C')

        self.numProcessed += len(imgList)

        npout_view = fpgaOutput
        if self.cpuOp._weight is not None:
            self.cpuOp.computeFC(npout_view, self.fcOutput)
        else:
            self.fcOutput = npout_view
        smaxOutput = self.cpuOp.computeSoftmax(self.fcOutput)

        if self.args['golden']:
            for i, p in enumerate(imgList):
                #topk = xdnn_io.getTopK( smaxOutput[i], self.labels, 1)
                #print(imgList[i], topk)
                self.top1Count += xdnn_io.isTopK(\
                  smaxOutput[i], self.goldenMap, p, self.labels, 1)
                self.top5Count += xdnn_io.isTopK(\
                  smaxOutput[i], self.goldenMap, p, self.labels, 5)

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