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()
def _train_thread(self): while not self.shutdown_training: if not self.rendering_done: # training images are not ready # print('rendering') time.sleep(0.01) continue batch_xs = self.imgs.copy() car_batch_ys = self.labels.copy() batch_xs = yolo_gluon.split_render_data(batch_xs, self.ctx) car_batch_ys = yolo_gluon.split_render_data(car_batch_ys, self.ctx) self.rendering_done = False self._train_batch(batch_xs, car_batch_ys)
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('--------------------------------------------------')
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 bs = self.batch_size # -------------------- background -------------------- # self.bg_iter_valid = yolo_gluon.load_background('val', bs, h, w) self.bg_iter_train = yolo_gluon.load_background('train', bs, h, w) self.car_renderer = RenderCar(h, w, self.classes, self.ctx[0], pre_load=False) LP_generator = licence_plate_render.LPGenerator(h, w) # -------------------- 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) imgs, LP_labels = LP_generator.add(imgs, self.LP_r_max, add_rate=0.5) batch_xs = yolo_gluon.split_render_data(imgs, self.ctx) car_batch_ys = yolo_gluon.split_render_data(labels, self.ctx) LP_batch_ys = yolo_gluon.split_render_data(LP_labels, self.ctx) self._train_batch(batch_xs, car_batch_ys, LP_batch_ys) # -------------------- show training image # -------------------- '''
print(global_variable.cyan) print('OCR Render And Train') h, w = size bg_iter_train = yolo_gluon.load_background('train', batch_size, h, w) generator = licence_plate_render.LPGenerator(*size) while True: if (backward_counter % 10 == 0 or 'bg' not in locals()): bg = yolo_gluon.ImageIter_next_batch(bg_iter_train) bg = bg.as_in_context(ctx[0]) imgs, labels = generator.render(bg) batch_xs = yolo_gluon.split_render_data(imgs, ctx) batch_ys = yolo_gluon.split_render_data(labels, ctx) train_the(batch_xs, batch_ys) #raw_input('next') elif args.mode == 'valid': print(global_variable.cyan) print('Render And Valid') bs = 16 h, w = size bg_iter = yolo_gluon.load_background('val', bs, *size) generator = licence_plate_render.LPGenerator(*size) plt.ion()