Example #1
0
File: YOLO.py Project: wushian/YOLO
    def render_and_train(self):
        print(global_variable.green)
        print('Render And Train')
        print(global_variable.reset_color)
        # -------------------- show training image # --------------------

        self.batch_size = 1
        ax = yolo_cv.init_matplotlib_figure()

        h, w = self.size
        # -------------------- background -------------------- #
        self.bg_iter_valid = yolo_gluon.load_background(
            'val', self.batch_size, h, w)
        self.bg_iter_train = yolo_gluon.load_background(
            'train', self.batch_size, h, w)
        self.car_renderer = RenderCar(h,
                                      w,
                                      self.classes,
                                      self.ctx[0],
                                      pre_load=False)

        # -------------------- main loop -------------------- #
        while True:
            if (self.backward_counter % 10 == 0 or 'bg' not in locals()):
                bg = yolo_gluon.ImageIter_next_batch(self.bg_iter_train)
                bg = bg.as_in_context(self.ctx[0])

            # -------------------- render dataset -------------------- #
            imgs, labels = self.car_renderer.render(bg,
                                                    'train',
                                                    render_rate=0.5,
                                                    pascal_rate=0.1)

            batch_xs = yolo_gluon.split_render_data(imgs, self.ctx)
            car_batch_ys = yolo_gluon.split_render_data(labels, self.ctx)

            self._train_batch(batch_xs, car_bys=car_batch_ys)

            # -------------------- show training image # --------------------
            if self.use_fp16:
                img = img.astype('float32')

            img = yolo_gluon.batch_ndimg_2_cv2img(batch_xs[0])[0]
            img = yolo_cv.cv2_add_bbox(img,
                                       car_batch_ys[0][0, 0].asnumpy(),
                                       4,
                                       use_r=0)
            yolo_cv.matplotlib_show_img(ax, img)
            print(car_batch_ys[0][0])
            raw_input()
Example #2
0
    def _train_or_valid(self, mode):
        print(global_variable.cyan)
        print(mode)
        print(global_variable.reset_color)
        if mode == 'val':
            self.batch_size = 1
            ax = yolo_cv.init_matplotlib_figure()
            # self.net = yolo_gluon.init_executor(
            #    self.export_file, self.size, self.ctx[0])

        # -------------------- background -------------------- #
        LP_generator = licence_plate_render.LPGenerator(*self.size)
        bg_iter = yolo_gluon.load_background(mode, self.batch_size, *self.size)

        # -------------------- main loop -------------------- #
        self.backward_counter = 0
        while True:
            if (self.backward_counter % 3 == 0 or 'bg' not in locals()):
                bg = yolo_gluon.ImageIter_next_batch(bg_iter)
                bg = bg.as_in_context(self.ctx[0]) / 255.

            # -------------------- render dataset -------------------- #
            imgs, labels = LP_generator.add(bg, self.LP_r_max, add_rate=0.5)

            if mode == 'train':
                batch_xs = yolo_gluon.split_render_data(imgs, self.ctx)
                batch_ys = yolo_gluon.split_render_data(labels, self.ctx)
                self._train_batch_LP(batch_xs, batch_ys)

            elif mode == 'val':
                batch_out = self.net(imgs)
                pred = self.predict_LP(batch_out)

                img = yolo_gluon.batch_ndimg_2_cv2img(imgs)[0]

                labels = labels.asnumpy()

                img, _ = LP_generator.project_rect_6d.add_edges(
                    img, labels[0, 0, 1:7])

                img, clipped_LP = LP_generator.project_rect_6d.add_edges(
                    img, pred[1:])

                yolo_cv.matplotlib_show_img(ax, img)
                print(labels)
                print(pred)
                raw_input('--------------------------------------------------')
Example #3
0
    def valid(self):
        print(global_variable.cyan)
        print('Valid')

        bs = 1  # batch size = 1
        h, w = self.size

        # init two matplotlib figures
        ax1 = yolo_cv.init_matplotlib_figure()
        ax2 = yolo_cv.init_matplotlib_figure()

        # init radar figure for vizualizing class distribution
        radar_prob = yolo_cv.RadarProb(self.num_class, self.classes)

        # init background, LP adder, car renderer
        BG_iter = yolo_gluon.load_background('val', bs, h, w)
        LP_generator = licence_plate_render.LPGenerator(h, w)
        car_renderer = RenderCar(h, w, self.classes, self.ctx[0], pre_load=False)

        for bg in BG_iter:
            # select background
            bg = bg.data[0].as_in_context(self.ctx[0])  # b*RGB*w*h
            # render images, type(imgs) = mxnet.ndarray
            imgs, labels = car_renderer.render(bg, 'valid', pascal_rate=0.5, render_rate=0.9)
            imgs, LP_labels = LP_generator.add(imgs, self.LP_r_max, add_rate=0.8)

            # return all_output[::-1], [LP_output]
            x1, x2, x3, LP_x = self.net.forward(is_train=False, data=imgs)
            outs = self.predict([x1, x2, x3])
            LP_outs = self.predict_LP([LP_x])

            # convert ndarray to np.array
            img = yolo_gluon.batch_ndimg_2_cv2img(imgs)[0]

            # draw licence plate border
            img, clipped_LP = LP_generator.project_rect_6d.add_edges(img, LP_outs[0, 1:])
            yolo_cv.matplotlib_show_img(ax2, clipped_LP)

            # draw car border
            img = yolo_cv.cv2_add_bbox(img, labels[0, 0].asnumpy(), 4, use_r=0)
            img = yolo_cv.cv2_add_bbox(img, outs[0], 5, use_r=0)
            yolo_cv.matplotlib_show_img(ax1, img)

            # vizualize class distribution
            radar_prob.plot(outs[0, 0], outs[0, -self.num_class:])
            raw_input('next')
Example #4
0
    def valid(self):
        print(global_variable.cyan)
        print('Valid')

        bs = 1
        h, w = self.size
        ax1 = yolo_cv.init_matplotlib_figure()
        radar_prob = yolo_cv.RadarProb(self.num_class, self.classes)

        BG_iter = yolo_gluon.load_background('val', bs, h, w)
        car_renderer = RenderCar(h,
                                 w,
                                 self.classes,
                                 self.ctx[0],
                                 pre_load=False)

        for bg in BG_iter:
            # -------------------- get image -------------------- #
            bg = bg.data[0].as_in_context(self.ctx[0])  # b*RGB*w*h
            imgs, labels = car_renderer.render(bg,
                                               'valid',
                                               pascal_rate=0.5,
                                               render_rate=0.9)

            # -------------------- predict -------------------- #
            net_out = self.net.forward(is_train=False, data=imgs)
            # net_out = [x1, x2, x3], which shapes are
            # (1L, 640L, 3L, 30L), (1L, 160L, 3L, 30L), (1L, 40L, 3L, 30L)
            outs = self.predict(net_out)

            # -------------------- show -------------------- #
            img = yolo_gluon.batch_ndimg_2_cv2img(imgs)[0]

            img = yolo_cv.cv2_add_bbox(img, labels[0, 0].asnumpy(), 4, use_r=0)
            img = yolo_cv.cv2_add_bbox(img, outs[0], 5, use_r=0)
            yolo_cv.matplotlib_show_img(ax1, img)

            radar_prob.plot3d(outs[0, 0], outs[0, -self.num_class:])
            raw_input('next')
Example #5
0
        axs.append(fig.add_subplot(4, 4, i + 1))

    for bg in bg_iter:
        bg = bg.data[0].as_in_context(ctx[0])
        imgs, labels = generator.render(bg)
        score_x, class_x = net(imgs)
        print(score_x.shape)
        print(class_x.shape)
        imgs = yolo_gluon.batch_ndimg_2_cv2img(imgs)
        for i in range(bs):
            ax = axs[i]
            s = score_x[i]
            s = nd.sigmoid(s.reshape(-1)).asnumpy()
            p = class_x[i, 0].asnumpy()
            p = np.argmax(p, axis=-1)
            yolo_cv.matplotlib_show_img(ax, imgs[i])
            ax.plot(range(8, 384, 16), (1 - s) * 160)
            ax.axis('off')

            s = np.concatenate(([0], s, [0]))
            # zero-dimensional arrays cannot be concatenated
            # Find peaks
            text = ''
            for i in range(24):
                if s[i + 1] > 0.2 and s[i + 1] > s[i + 2] and s[i + 1] > s[i]:
                    c = int(p[i])
                    text = text + cls_names[c]
            print(text)

        raw_input('press Enter to next batch....')