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 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 # -------------------- '''
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 test_add(self, b): batch_iter = yolo_gluon.load_background('val', b, h, w) for batch in batch_iter: imgs = batch.data[0].as_in_context(ctx[0]) # b*RGB*w*h labels = batch.label[0].as_in_context(ctx[0]) # b*L*5 #imgs = nd.zeros((b, 3, self.h, self.w), ctx=gpu(0))*0.5 tic = time.time() imgs, labels = self.add(imgs / 255, labels) #print(time.time()-tic) for i, img in enumerate(imgs): R, G, B = img.transpose((1, 2, 0)).split(num_outputs=3, axis=-1) img = nd.concat(B, G, R, dim=-1).asnumpy() print(labels[i]) cv2.imshow('%d' % i, img) if cv2.waitKey(0) & 0xFF == ord('q'): break
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')
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')
trainer = gluon.Trainer(net.collect_params(), 'adam', {'learning_rate': LR}) log_dir = args.version + '/logs' summary_writer = SummaryWriter(logdir=log_dir, verbose=True) # 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')
''' 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)