def _render_thread(self): h, w = self.size self.car_renderer = RenderCar(h, w, self.classes, self.ctx[0], pre_load=render_thread_pre_load) self.bg_iter_valid = yolo_gluon.load_background( 'val', self.batch_size, h, w) bg_iter_train = yolo_gluon.load_background('train', self.batch_size, h, w) self.labels = nd.array([0]) while not self.shutdown_training: if self.rendering_done: #print('render done') time.sleep(0.01) continue # ready to render new images if (self.backward_counter % 10 == 0 or 'bg' not in locals()): bg = yolo_gluon.ImageIter_next_batch(bg_iter_train) bg = bg.as_in_context(self.ctx[0]) # change an other batch of background self.imgs, self.labels = self.car_renderer.render(bg, 'train', render_rate=0.5, pascal_rate=0.2) self.rendering_done = True
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_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 # -------------------- '''
# prob, score = net(nd.zeros((1,3,160,384), ctx=ctx[0])) # summary_writer.add_graph(get_feature) # print(prob.shape, score.shape) if not os.path.exists(backup_dir): os.makedirs(backup_dir) 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
def _deg_2_rad(deg): return deg * math.pi / 180. _join = os.path.join if __name__ == '__main__': ctx = [gpu(0)] h, w = 320, 512 ax = yolo_cv.init_matplotlib_figure() bg_iter = yolo_gluon.load_background('val', 1, h, w) car_renderer = RenderCar(h, w, [[0, 0]], ctx[0], pre_load=False) LP_generator = licence_plate_render.LPGenerator(h, w) while True: bg = yolo_gluon.ImageIter_next_batch(bg_iter).as_in_context(ctx[0]) imgs, labels = car_renderer.render(bg, 'valid', render_rate=0.9, pascal_rate=0.5) L1 = labels[0, 0] imgs, L2 = LP_generator.add(imgs, [45, 60, 45]) img = yolo_gluon.batch_ndimg_2_cv2img(imgs)[0] img = yolo_cv.cv2_add_bbox(img, L1.asnumpy(), 4) print(L2) yolo_cv.matplotlib_show_img(ax, img) raw_input()