Пример #1
0
  def run(self, imgList, fpgaOutput_list, fpgaOutputShape_list, shapeArr):
    if self.numProcessed == 0:
      self.zmqPub = None
      if self.args['zmqpub']:
        self.zmqPub = mp_classify.ZmqResultPublisher(self.args['deviceID'])
      self.goldenMap = None


    self.numProcessed += len(imgList)
    bboxlist_for_images = self.yolo_postproc(fpgaOutput_list, args, shapeArr, biases=self.biases)

    if(not self.args['profile']):
      for i in range(min(self.args['batch_sz'], len(shapeArr))):
        print("Detected {} boxes in {}".format(len(bboxlist_for_images[i]), imgList[i]))


    if(self.args['results_dir']):
      boxes = bboxlist_for_images
      for i in range(min(self.args['batch_sz'], len(shapeArr))):
        filename = os.path.splitext(os.path.basename(imgList[i]))[0]
        out_file_txt = os.path.join(self.args['results_dir'], filename + '.txt')
        print("Saving {} boxes to {}".format(len(boxes[i]), out_file_txt)); sys.stdout.flush()
        saveDetectionDarknetStyle(out_file_txt, boxes[i], shapeArr[i])

        if(self.args['visualize']):
          out_file_png = os.path.join(self.args['results_dir'], filename + '.png')
          print("Saving result to {}".format(out_file_png)); sys.stdout.flush()
          draw_boxes(imgList[i], boxes[i], self.labels, self.colors, out_file_png)
Пример #2
0
    def run(self, imgList, fpgaOutput_list, fpgaOutputShape_list, shapeArr):

        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 = mp_classify.ZmqResultPublisher(
                    self.args['deviceID'])
            self.goldenMap = None

        self.numProcessed += len(imgList)

        firstInputShape = xdnn.CompilerJsonParser(
            self.args['netcfg']).getInputs().itervalues().next()

        if ((args['yolo_model'] == 'standard_yolo_v3')
                or (args['yolo_model'] == 'tiny_yolo_v3')):
            num_ouptut_layers = len(fpgaOutput_list)
            fpgaOutput = []
            for idx in range(num_ouptut_layers):
                fpgaOutput.append(
                    np.frombuffer(fpgaOutput_list[idx],
                                  dtype=np.float32).reshape(
                                      tuple(fpgaOutputShape_list[idx])))
            bboxlist_for_images = det_postprocess(fpgaOutput, args, shapeArr)

            for i in range(min(self.args['batch_sz'], len(shapeArr))):
                print "image: ", imgList[
                    i], " has num boxes detected  : ", len(
                        bboxlist_for_images[i])

        else:

            fpgaOutput = fpgaOutput_list[0]
            fpgaOutputShape = fpgaOutputShape_list[0]
            npout_view = np.frombuffer(fpgaOutput, dtype=np.float32)\
              .reshape(tuple(fpgaOutputShape))
            npout_view = npout_view.flatten()
            fpgaoutsz = fpgaOutputShape[1] * fpgaOutputShape[
                2] * fpgaOutputShape[3]
            bboxlist_for_images = []
            for i in range(min(self.args['batch_sz'], len(shapeArr))):
                startidx = i * fpgaoutsz
                softmaxout = npout_view[startidx:startidx + fpgaoutsz]

                # first activate first two channels of each bbox subgroup (n)
                for b in range(self.args['bboxplanes']):
                    for r in range(\
                      self.args['batchstride']*b,
                      self.args['batchstride']*b+2*self.args['groups']):
                        softmaxout[r] = sigmoid(softmaxout[r])

                    for r in range(\
                      self.args['batchstride']*b\
                        +self.args['groups']*self.args['coords'],
                      self.args['batchstride']*b\
                        +self.args['groups']*self.args['coords']+self.args['groups']):
                        softmaxout[r] = sigmoid(softmaxout[r])

                # Now softmax on all classification arrays in image
                for b in range(self.args['bboxplanes']):
                    for g in range(self.args['groups']):
                        softmax(
                            self.args['beginoffset'] +
                            b * self.args['batchstride'] +
                            g * self.args['groupstride'], softmaxout,
                            softmaxout, self.args['outsz'],
                            self.args['groups'])

                # NMS
                bboxes = nms.do_baseline_nms(
                    softmaxout, shapeArr[i][1], shapeArr[i][0],
                    firstInputShape[2], firstInputShape[3], self.args['out_w'],
                    self.args['out_h'], self.args['bboxplanes'],
                    self.args['outsz'], self.args['scorethresh'],
                    self.args['iouthresh'])
                bboxlist_for_images.append(bboxes)
                print "image: ", imgList[
                    i], " has num boxes detected  : ", len(bboxes)

        if self.args['golden'] is None:
            return

        for i in range(min(self.args['batch_sz'], len(shapeArr))):
            filename = imgList[i]
            out_file_txt = ((filename.split("/")[-1]).split(".")[0])
            out_file_txt = self.args[
                'detection_labels'] + "/" + out_file_txt + ".txt"
            out_line_list = []
            bboxes = bboxlist_for_images[i]
            for j in range(len(bboxes)):
                x, y, w, h = darknet_style_xywh(shapeArr[i][1], shapeArr[i][0],
                                                bboxes[j]["ll"]["x"],
                                                bboxes[j]["ll"]["y"],
                                                bboxes[j]['ur']['x'],
                                                bboxes[j]['ur']['y'])

                line_string = str(bboxes[j]["classid"])
                line_string = line_string + " " + str(
                    round(bboxes[j]['prob'], 3))
                line_string = line_string + " " + str(x)
                line_string = line_string + " " + str(y)
                line_string = line_string + " " + str(w)
                line_string = line_string + " " + str(h)
                out_line_list.append(line_string + "\n")

            log.info("writing this into prediction file at %s" %
                     (out_file_txt))
            with open(out_file_txt, "w") as the_file:
                for lines in out_line_list:
                    the_file.write(lines)