def save2dirs(files, save_dir, files_nr, dir_name): files = wmlu.list_to_2dlist(files, files_nr) for i, lfiles in enumerate(files): cur_save_dir = osp.join(save_dir, dir_name + f"_{i}") wmlu.create_empty_dir(cur_save_dir, remove_if_exists=False) for f in lfiles: wmlu.try_link(f, cur_save_dir)
def build_inference_net(self): try: if not os.path.exists(self.log_dir): wmlu.create_empty_dir(self.log_dir) if not os.path.exists(self.ckpt_dir): wmlu.create_empty_dir(self.ckpt_dir) except: pass ''' When inference, self.data is just a tensor ''' data = {IMAGE: self.data} DataLoader.detection_image_summary(data, name="data_source") self.input_data = data '''if self.cfg.GLOBAL.DEBUG: data[IMAGE] = tf.Print(data[IMAGE],[tf.shape(data[IMAGE]),data[ORG_HEIGHT],data[ORG_WIDTH],data[HEIGHT],data[WIDTH]],summarize=100, name="XXXXX")''' self.res_data, loss_dict = self.model.forward(data) config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True self.top_variable_name_scope = "Model" print("batch_norm_ops.") wmlu.show_list( [x.name for x in tf.get_collection(tf.GraphKeys.UPDATE_OPS)])
def multi_thread_to_tfrecords_by_files(self,files, output_dir,shuffling=False,fidx=0): wmlu.create_empty_dir(output_dir,remove_if_exists=True,yes_to_all=True) if shuffling: random.seed(time.time()) random.shuffle(files) wmlu.show_list(files[:100]) if len(files)>100: print("...") print(f"Total {len(files)} files.") sys.stdout.flush() files = wmlu.list_to_2dlist(files,SAMPLES_PER_FILES) files_data = list(enumerate(files)) if fidx != 0: _files_data = [] for fid,file_d in files_data: _files_data.append([fid+fidx,file_d]) files_data = _files_data sys.stdout.flush() pool = Pool(13) pool.map(functools.partial(self.make_tfrecord,output_dir=output_dir),files_data) #list(map(functools.partial(self.make_tfrecord,output_dir=output_dir),files_data)) pool.close() pool.join() print('\nFinished converting the dataset total %d examples.!'%(len(files)))
def trans_dir(src_dir, out_dir): sub_dirs = wmlu.recurse_get_subdir_in_dir(src_dir) pattern = "img_{:05d}.jpg" wmlu.create_empty_dir(out_dir, remove_if_exists=False) for sd in sub_dirs: if sd[-1] == "/": sd = sd[:-1] rsd = osp.join(src_dir, sd) files = glob.glob(osp.join(rsd, "*.jpg")) if len(files) == 0: continue save_name = osp.join(out_dir, sd + f"_{data_type}_{len(files)}.np") save_dir_name = osp.dirname(save_name) wmlu.create_empty_dir(save_dir_name, remove_if_exists=False) all_frames = [] for i in range(len(files)): file_name = pattern.format(i + 1) file_path = osp.join(rsd, file_name) if not osp.exists(file_path): print(f"File {file_path} not exists, len={len(files)}") continue with open(file_path, "rb") as f: data = f.read() if args.new_short > 2: img = wmli.decode_img(data) img = wmli.resize_short_size(img, args.new_short) data = wmli.encode_img(img) all_frames.append(data) print(f"Save {save_name}") with open(save_name, "wb") as f: pickle.dump(all_frames, f)
def trans_data(data_dir,save_dir): global name_to_id_dict wmlu.show_dict(name_to_id_dict) wmlu.create_empty_dir(save_dir,remove_if_exists=False) def name_to_id(x): return name_to_id_dict[x] ignored_labels = ["construction--barrier--ambiguous","construction--barrier--separator"] data = LabelMeData(label_text2id=name_to_id, shuffle=False) data.read_data(data_dir) for i,x in enumerate(data.get_items()): full_path, img_info, category_ids, category_names, boxes, binary_mask, area, is_crowd, num_annotations_skipped = x if len(category_ids) == 0: print(f"Skip {full_path}") continue new_mask = odm.dense_mask_to_sparse_mask(binary_mask,category_ids,default_label=255) #r_base_name = wmlu.base_name(full_path) r_base_name = f"IMG_{i+1:05d}" base_name = r_base_name+".png" save_path = os.path.join(save_dir,base_name) if resize_size is not None: new_mask = wmli.resize_img(new_mask,resize_size,keep_aspect_ratio=True,interpolation=cv2.INTER_NEAREST) img = wmli.imread(full_path) img = wmli.resize_img(img,resize_size,keep_aspect_ratio=True) img_save_path = os.path.join(save_dir,r_base_name+".jpg") wmli.imwrite(img_save_path,img) new_mask = new_mask.astype(np.uint8) if os.path.exists(save_path): print(f"WARNING: File {save_path} exists.") cv2.imwrite(save_path,new_mask) sys.stdout.write(f"\r{i}")
def main(_): args = default_argument_parser().parse_args() test_dir = args.test_data_dir save_dir = args.save_data_dir wmlu.create_empty_dir(save_dir, remove_if_exists=True) files = glob.glob(os.path.join(test_dir, "*.jpg")) m = predm.PredictModel() m.restoreVariables() m.remove_batch() def id_to_text(id): return m.trainer.category_index[id] for file in files: img = wmli.imread(file) img = np.expand_dims(img, axis=0) m.predictImages(img) save_path = os.path.join(save_dir, os.path.basename(file)) xml_path = wmlu.change_suffix(save_path, "xml") shutil.copy(file, save_path) labels = [id_to_text(id) for id in m.res_data[RD_LABELS]] pvt.writeVOCXml(xml_path, m.res_data[RD_BOXES], labels) if RD_FULL_SIZE_MASKS in m.res_data: annotations = lmt.trans_odresult_to_annotations_list(m.res_data) json_path = wmlu.change_suffix(save_path, "json") lmt.save_labelme_datav1(json_path, file, img, annotations, label_to_text=id_to_text) img_save_path = wmlu.change_suffix(xml_path, "jpg") wmli.imwrite(img_save_path, m.res_data[RD_RESULT_IMAGE])
def savePBFile(self,pb_path,output_names): sess = self.sess #constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def,['bboxes','labels','probs','lens']) print("Output names: ",self.output_names) print("pb path: ",pb_path) wmlu.create_empty_dir(os.path.dirname(pb_path),remove_if_exists=False) constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def,output_names) with tf.gfile.FastGFile(pb_path, mode='wb') as f: f.write(constant_graph.SerializeToString())
def save_data(data, save_dir): for k, v in data.items(): tsd = osp.join(save_dir, str(k)) wmlu.create_empty_dir(tsd, False) for f in v: dir_name = wmlu.base_name(osp.dirname(f)) if dir_name == "": print(f"Get dir name faild {f}.") name = dir_name + "_" + osp.join(osp.basename(f)) os.link(f, osp.join(tsd, name))
def forward(self,inputs): if self.tmp_path.startswith("/tmp"): wmlu.create_empty_dir(self.tmp_path,remove_if_exists=True,yes_to_all=True) print("inputs shape:",inputs.shape) raw_path = os.path.join(self.tmp_path,"input.raw") input_list = os.path.join(self.tmp_path,"file_list.txt") output_dir = os.path.join(self.tmp_path,"output") if not os.path.isdir(output_dir): os.makedirs(output_dir) with open(input_list, "w") as f: if self.output_layers is not None: v = f"#{self.output_layers[0]}" for x in self.output_layers[1:]: v += f" {x}" v += "\n" f.write(v) f.write(raw_path) self.to_snpe_raw(inputs,raw_path) cmd = "{} --container {} --input_list {} --output_dir {}".format(self.bin, self.model_path, input_list, output_dir) print(f"CMD:{cmd}") print(f"All output files.") os.system(cmd) all_files = wmlu.recurse_get_filepath_in_dir(output_dir,suffix=".raw") wmlu.show_list(all_files) print("-------------------------------") res_data = [] output_dir = "/home/wj/0day/output" #for DEBUG if self.output_names is not None: for name,shape in zip(self.output_names,self.output_shapes): path = os.path.join(output_dir,"Result_0",name+self.output_suffix) if not os.path.exists(path): print(f"{path} not exits") res_data.append(None) else: print(f"Read from {path}") td = np.fromfile(path,dtype=np.float32) if shape is not None: td = np.reshape(td,shape) res_data.append(td) else: path = all_files[0] print(f"Use ") td = np.fromfile(path, dtype=np.float32) res_data.append(td) return res_data
def main(_): is_training = False args = default_argument_parser().parse_args() cfg = setup(args) data_loader = DataLoader(cfg=cfg, is_training=is_training) data_args = DATASETS_REGISTRY[cfg.DATASETS.TEST] data, num_classes = data_loader.load_data(*data_args, batch_size=1, is_training=False) cfg.MODEL.ROI_HEADS.NUM_CLASSES = num_classes cfg.MODEL.SSD.NUM_CLASSES = num_classes cfg.MODEL.RETINANET.NUM_CLASSES = num_classes cfg.MODEL.CENTERNET.NUM_CLASSES = num_classes cfg.MODEL.YOLACT.NUM_CLASSES = num_classes cfg.MODEL.FCOS.NUM_CLASSES = num_classes cfg.MODEL.NUM_CLASSES = num_classes cfg.DATASETS.NUM_CLASSES = num_classes cfg.freeze() config.set_global_cfg(cfg) model = PredictModel(cfg=cfg,is_remove_batch=True) model.restoreVariables() data_path = args.test_data_dir if os.path.isdir(data_path): files = wmlu.recurse_get_filepath_in_dir(data_path,suffix=".jpg") else: files = [data_path] save_path = args.save_data_dir wmlu.create_empty_dir(save_path,remove_if_exists=True) for file in files: img = wmli.imread(file) imgs = np.expand_dims(img,axis=0) res = model.predictImages(imgs) if RD_MASKS in res: r_img = odv.draw_bboxes_and_mask(img,res[RD_LABELS],res[RD_PROBABILITY],res[RD_BOXES], res[RD_MASKS], show_text=True) else: r_img = odv.bboxes_draw_on_imgv2(img,res[RD_LABELS],res[RD_PROBABILITY],res[RD_BOXES], text_fn=text_fn, show_text=True) name = wmlu.base_name(file) img_save_path = os.path.join(save_path,name+".png") wmli.imwrite(img_save_path,r_img)
def trans_data(data_dir,save_dir,beg,end): global name_to_id_dict wmlu.show_dict(name_to_id_dict) wmlu.create_empty_dir(save_dir,remove_if_exists=False) def name_to_id(x): return name_to_id_dict[x] ignored_labels = ["construction--barrier--ambiguous","construction--barrier--separator"] data = MapillaryVistasData(label_text2id=name_to_id, shuffle=False, ignored_labels=ignored_labels, label_map=None, sub_dir_name="validation", #sub_dir_name="training", allowed_labels_fn=list(name_to_id_dict.keys())) data.read_data(data_dir) def filter(full_path,_): base_name = wmlu.base_name(full_path)+".png" save_path = os.path.join(save_dir,base_name) if os.path.exists(save_path): print(f"File {save_path} exists.") return False print(f"File {save_path} not exists.") return True for i,x in enumerate(data.get_items(beg,end,filter=filter)): full_path, img_info, category_ids, category_names, boxes, binary_mask, area, is_crowd, num_annotations_skipped = x if len(category_ids) == 0: print(f"Skip {full_path}") continue new_mask = odm.dense_mask_to_sparse_mask(binary_mask,category_ids,default_label=255) base_name = wmlu.base_name(full_path)+".png" save_path = os.path.join(save_dir,base_name) new_mask = new_mask.astype(np.uint8) if os.path.exists(save_path): print(f"WARNING: File {save_path} exists.") cv2.imwrite(save_path,new_mask) sys.stdout.write(f"\r{i}")
append_to_dict(res, 0, data) else: data = wmlu.list_to_2dlistv2(data, split_nr) for i, d in enumerate(data): append_to_dict(res, i, d) return res def save_data(data, save_dir): for k, v in data.items(): tsd = osp.join(save_dir, str(k)) wmlu.create_empty_dir(tsd, False) for f in v: dir_name = wmlu.base_name(osp.dirname(f)) if dir_name == "": print(f"Get dir name faild {f}.") name = dir_name + "_" + osp.join(osp.basename(f)) os.link(f, osp.join(tsd, name)) #shutil.copy(f,osp.join(tsd,name)) if __name__ == "__main__": data_dir = "/home/wj/ai/mldata/basketball_datasets/multisports/rawframes" save_dir = "/home/wj/ai/mldata/basketball_objs/data2label" data_dir = "/home/wj/ai/mldata/0day/basketball" save_dir = "/home/wj/ai/mldata/basketball_objs/data2label/ba1" wmlu.create_empty_dir(save_dir, False) data = sample_in_dir(data_dir, 150, 1) print(f"Save_path {save_dir}") save_data(data, save_dir)
l, r = pair if res[l, 0] < res[r, 0] or res[l, 2] < 0.1 or res[r, 2] < 0.1: is_good = False break head_bbox = self.get_head_pos(mpii_kps) if is_good and head_bbox is not None and self.kps_in_bbox( coco_kps[self.coco_idxs], head_bbox): res[self.coco_idxs] = coco_kps[self.coco_idxs] return res if __name__ == "__main__": file_path = '/home/wj/ai/mldata1/crowd_pose/CrowdPose/crowdpose_train.json' images_dir = '/home/wj/ai/mldata1/crowd_pose/images' save_dir = '/home/wj/ai/mldata1/crowd_pose/tmp/vis' wmlu.create_empty_dir(save_dir, remove_if_exists=False) datas = read_crowd_pose(file_path) do_vis = True for data in datas: image_name, kps, bbox = data image = osp.join(images_dir, image_name) img = wmli.imread(image) img = odv.draw_keypoints(img, kps, no_line=True) t_bboxes = np.array([bbox]) t_bboxes = odb.npchangexyorder(t_bboxes) img = odv.draw_bboxes(img, bboxes=t_bboxes, is_relative_coordinate=False) save_path = osp.join(save_dir, image_name) wmli.imwrite(save_path, img)
def _create_tf_record_from_coco_annotations(annotations_file, image_dir, output_path, include_masks, is_train_data=True, img_filter=None, name="coco_train"): """Loads COCO annotation json files and converts to tf.Record format. Args: annotations_file: JSON file containing bounding box annotations. image_dir: Directory containing the image files. output_path: Path to output tf.Record file. include_masks: Whether to include instance segmentations masks (PNG encoded) in the result. default: False. """ with tf.gfile.GFile(annotations_file, 'r') as fid: groundtruth_data = json.load(fid) images = groundtruth_data['images'] if img_filter is not None: images = list(filter(img_filter, images)) print(f"Image len {len(images)}.") category_index = label_map_util.create_category_index( groundtruth_data['categories']) annotations_index = {} if 'annotations' in groundtruth_data: tf.logging.info( 'Found groundtruth annotations. Building annotations index.') for annotation in groundtruth_data['annotations']: image_id = annotation['image_id'] if image_id not in annotations_index: annotations_index[image_id] = [] annotations_index[image_id].append(annotation) missing_annotation_count = 0 for image in images: image_id = image['id'] if image_id not in annotations_index: missing_annotation_count += 1 annotations_index[image_id] = [] tf.logging.info('%d images are missing annotations.', missing_annotation_count) tf.logging.info('writing to output path: %s', output_path) wmlu.create_empty_dir(output_path) total_num_annotations_skipped = 0 fidx = 0 idx = 0 while True: fidx += 1 save_path = get_save_path(output_path, name, fidx) if idx % 100 == 0: tf.logging.info('On image %d of %d', idx, len(images)) if is_train_data and (TRAIN_SIZE_LIMIT is not None) and ( idx > TRAIN_SIZE_LIMIT): break elif (not is_train_data) and (VAL_SIZE_LIMIT is not None) and ( idx > VAL_SIZE_LIMIT): break with tf.python_io.TFRecordWriter(save_path) as writer: j = 0 while j < IMAGE_PER_RECORD: image = images[idx] annotations_list = annotations_index[image['id']] _, tf_example, num_annotations_skipped, repeat_nr = create_tf_example( image, annotations_list, image_dir, category_index, include_masks) if tf_example is not None: total_num_annotations_skipped += num_annotations_skipped assert repeat_nr > 0, f"Error repeat nr {repeat_nr}" for i in range(repeat_nr): writer.write(tf_example.SerializeToString()) j += 1 idx += 1 if idx >= len(images): break if idx >= len(images): break tf.logging.info('Finished writing, skipped %d annotations.', total_num_annotations_skipped)
help="path to test data dir") parser.add_argument("--save_dir", default="/2_data/wj/mldata/coco/coco_results", type=str, help="path to save data dir") return parser if __name__ == "__main__": args = default_argument_parser().parse_args() save_path = args.save_dir dataset = PascalVOCData() # data_path0 = get_data_dir2("annotationed_data/verify_p03_1020_1_proc_m") data_path = args.test_dir if not dataset.read_data(data_path): exit(0) wmlu.create_empty_dir(save_path, remove_if_exists=True, yes_to_all=True) for data in dataset.get_items(): full_path, shape, category_ids, category_names, boxes, binary_masks, area, is_crowd, num_annotations_skipped = data print(f"Process {full_path}") img = wmli.imread(full_path) base_name = wmlu.base_name(full_path) + "_a.jpg" save_file_path = os.path.join(save_path, base_name) img = odv.draw_bboxes(img, classes=category_names, bboxes=boxes, show_text=True, text_color=(255., 0., 0.)) wmli.imsave(save_file_path, img)
def build_net_run_on_multi_gpus_nccl(self): if not os.path.exists(self.log_dir): wmlu.create_empty_dir(self.log_dir) if not os.path.exists(self.ckpt_dir): wmlu.create_empty_dir(self.ckpt_dir) '''if self.cfg.GLOBAL.DEBUG: data[IMAGE] = tf.Print(data[IMAGE],[tf.shape(data[IMAGE]),data[ORG_HEIGHT],data[ORG_WIDTH],data[HEIGHT],data[WIDTH]],summarize=100, name="XXXXX")''' all_loss_dict = {} steps = self.cfg.SOLVER.STEPS print("Train steps:", steps) lr = wnn.build_learning_rate( self.cfg.SOLVER.BASE_LR, global_step=self.global_step, lr_decay_type=self.cfg.SOLVER.LR_DECAY_TYPE, steps=steps, decay_factor=self.cfg.SOLVER.LR_DECAY_FACTOR, total_steps=steps[-1], min_lr=1e-6, warmup_steps=self.cfg.SOLVER.WARMUP_ITERS) tf.summary.scalar("lr", lr) self.max_train_step = steps[-1] if self.cfg.SOLVER.OPTIMIZER == "Momentum": opt = wnn.str2optimizer( "Momentum", lr, momentum=self.cfg.SOLVER.OPTIMIZER_momentum) else: opt = wnn.str2optimizer(self.cfg.SOLVER.OPTIMIZER, lr) tower_grads = [] if len(self.gpus) == 0: self.gpus = [0] if len(self.cfg.SOLVER.TRAIN_SCOPES) > 1: train_scopes = self.cfg.SOLVER.TRAIN_SCOPES else: train_scopes = None if len(self.cfg.SOLVER.TRAIN_REPATTERN) > 1: train_repattern = self.cfg.SOLVER.TRAIN_REPATTERN else: train_repattern = None for i in range(len(self.gpus)): scope = tf.get_variable_scope() if i > 0: #scope._reuse = tf.AUTO_REUSE scope.reuse_variables() with tf.device(f"/gpu:{i}"): with tf.device(":/cpu:0"): data = self.data.get_next() self.input_data = data with tf.name_scope(f"GPU{self.gpus[i]}"): with tf.device(":/cpu:0"): DataLoader.detection_image_summary( data, name=f"data_source{i}") self.res_data, loss_dict = self.model.forward(data) loss_values = [] for k, v in loss_dict.items(): all_loss_dict[k + f"_stage{i}"] = v tf.summary.scalar(f"loss/{k}", v) ## #v = tf.Print(v,[k,tf.is_nan(v), tf.is_inf(v)]) ## v = tf.cond(tf.logical_or(tf.is_nan(v), tf.is_inf(v)), lambda: tf.zeros_like(v), lambda: v) loss_values.append(v) scope._reuse = tf.AUTO_REUSE '''if (i==0) and len(tf.get_collection(GRADIENT_DEBUG_COLLECTION))>0: total_loss_sum = tf.add_n(loss_values) xs = tf.get_collection(GRADIENT_DEBUG_COLLECTION) grads = tf.gradients(total_loss_sum,xs) grads = [tf.reduce_sum(tf.abs(x)) for x in grads] loss_values[0] = tf.Print(loss_values[0],grads+["grads"],summarize=100)''' grads, total_loss, variables_to_train = wnn.nget_train_opv3( optimizer=opt, loss=loss_values, scopes=train_scopes, re_pattern=train_repattern) # if self.cfg.SOLVER.FILTER_NAN_AND_INF_GRADS: grads = [list(x) for x in grads] for i, (g, v) in enumerate(grads): try: if g is not None: g = tf.where( tf.logical_or(tf.is_nan(g), tf.is_inf(g)), tf.random_normal( shape=wmlt. combined_static_and_dynamic_shape(g), stddev=1e-5), g) except: print(f"Error {g}/{v}") raise Exception("Error") grads[i][0] = g # tower_grads.append(grads) ######################## '''tower_grads[0] = [list(x) for x in tower_grads[0]] for i,(g,v) in enumerate(tower_grads[0]): tower_grads[0][i][0] = tf.Print(g,["B_"+v.name,tf.reduce_min(g),tf.reduce_mean(g),tf.reduce_max(g)])''' ######################## if self.cfg.SOLVER.CLIP_NORM > 1: avg_grads = wnn.average_grads_nccl( tower_grads, clip_norm=self.cfg.SOLVER.CLIP_NORM) else: avg_grads = wnn.average_grads_nccl(tower_grads, clip_norm=None) '''avg_grads = [list(x) for x in avg_grads] for i,(g,v) in enumerate(avg_grads): avg_grads[i][0] = tf.Print(g,[v.name,tf.reduce_min(g),tf.reduce_mean(g),tf.reduce_max(g)])''' opt0 = wnn.apply_gradientsv3(avg_grads, self.global_step, opt) opt1 = wnn.get_batch_norm_ops() self.train_op = tf.group(opt0, opt1) self.total_loss, self.variables_to_train = total_loss, variables_to_train self.loss_dict = all_loss_dict config = tf.ConfigProto(allow_soft_placement=True) #config.gpu_options.allow_growth = True self.sess = tf.Session(config=config) if self.debug_tf: self.sess = tfdbg.LocalCLIDebugWrapperSession(self.sess) print("variables to train:") wmlu.show_list(self.variables_to_train) for v in self.variables_to_train: wsummary.histogram_or_scalar(v, v.name[:-2]) wnn.log_moving_variable() self.saver = tf.train.Saver(max_to_keep=100) tf.summary.scalar("total_loss", self.total_loss) self.summary = tf.summary.merge_all() self.summary_writer = tf.summary.FileWriter(self.log_dir, self.sess.graph) init = tf.global_variables_initializer() self.sess.run(init) print("batch_norm_ops.") wmlu.show_list( [x.name for x in tf.get_collection(tf.GraphKeys.UPDATE_OPS)])
def view_data(name, save_dir, nr=20): print(f"View {name}") raw_name = name names = name.split("--") if names[0] == "void" or "ambiguous" in raw_name: return if "road" not in raw_name: return for x in names: save_dir = os.path.join(save_dir, x) wmlu.create_empty_dir(save_dir, remove_if_exists=False) allowed_names = [raw_name] NAME2ID = {} ID2NAME = {} def name_to_id(x): global lid if x in NAME2ID: return NAME2ID[x] else: NAME2ID[x] = lid ID2NAME[lid] = x lid += 1 return NAME2ID[x] data = MapillaryVistasData(label_text2id=name_to_id, shuffle=False, ignored_labels=None, label_map=None, allowed_labels_fn=allowed_names) data.read_data(wmlu.home_dir("ai/mldata/mapillary_vistas")) i = 0 for x in data.get_items(): full_path, img_info, category_ids, category_names, boxes, binary_mask, area, is_crowd, num_annotations_skipped = x img = wmli.imread(full_path) def text_fn(classes, scores): return f"{ID2NAME[classes]}" if len(category_ids) == 0: continue wmlu.show_dict(NAME2ID) odv.draw_bboxes_and_maskv2(img=img, classes=category_ids, scores=None, bboxes=boxes, masks=binary_mask, color_fn=None, text_fn=text_fn, thickness=4, show_text=True, fontScale=0.8) base_name = os.path.basename(full_path) save_path = os.path.join(save_dir, base_name) wmli.imwrite(save_path, img) i += 1 if i >= nr: break
def main(_): is_training = False args = default_argument_parser().parse_args() cfg = setup(args) data_loader = DataLoader(cfg=cfg, is_training=is_training) data_args = DATASETS_REGISTRY[cfg.DATASETS.TEST] data, num_classes = data_loader.load_data(*data_args, batch_size=1, is_training=False) cfg.MODEL.ROI_HEADS.NUM_CLASSES = num_classes cfg.MODEL.SSD.NUM_CLASSES = num_classes cfg.MODEL.RETINANET.NUM_CLASSES = num_classes cfg.MODEL.CENTERNET.NUM_CLASSES = num_classes cfg.MODEL.YOLACT.NUM_CLASSES = num_classes cfg.MODEL.FCOS.NUM_CLASSES = num_classes cfg.DATASETS.NUM_CLASSES = num_classes cfg.freeze() config.set_global_cfg(cfg) model = PredictModel(cfg=cfg, is_remove_batch=True) model.restoreVariables() save_path = args.save_data_dir wmlu.create_empty_dir(save_path, remove_if_exists=True) metrics = COCOEvaluation(num_classes=90) items = eval_dataset() for data in items: full_path, shape, gt_labels, category_names, gt_boxes, binary_masks, area, is_crowd, num_annotations_skipped = data img = wmli.imread(full_path) imgs = np.expand_dims(img, axis=0) res = model.predictImages(imgs) if RD_MASKS in res: r_img = odv.draw_bboxes_and_mask(img, res[RD_LABELS], res[RD_PROBABILITY], res[RD_BOXES], res[RD_MASKS], show_text=True) else: r_img = odv.bboxes_draw_on_imgv2(img, res[RD_LABELS], res[RD_PROBABILITY], res[RD_BOXES], text_fn=text_fn, show_text=True) kwargs = {} kwargs['gtboxes'] = gt_boxes kwargs['gtlabels'] = gt_labels kwargs['boxes'] = res[RD_BOXES] kwargs['labels'] = res[RD_LABELS] kwargs['probability'] = res[RD_PROBABILITY] kwargs['img_size'] = shape metrics(**kwargs) if model.step % 100 == 0: metrics.show() name = wmlu.base_name(full_path) img_save_path = os.path.join(save_path, name + ".png") wmli.imwrite(img_save_path, r_img) metrics.show()
def main(_): is_training = False args = default_argument_parser().parse_args() cfg = setup(args) data_args = DATASETS_REGISTRY[cfg.DATASETS.TEST] cfg.MODEL.NUM_CLASSES = data_args[2] data_loader = DataLoader(cfg=cfg, is_training=is_training) data, num_classes = data_loader.load_data(*data_args, batch_size=1, is_training=False) cfg.MODEL.ROI_HEADS.NUM_CLASSES = num_classes cfg.MODEL.SSD.NUM_CLASSES = num_classes cfg.MODEL.RETINANET.NUM_CLASSES = num_classes cfg.MODEL.CENTERNET.NUM_CLASSES = num_classes cfg.MODEL.DEEPLAB.NUM_CLASSES = num_classes cfg.MODEL.YOLACT.NUM_CLASSES = num_classes cfg.MODEL.FCOS.NUM_CLASSES = num_classes cfg.MODEL.NUM_CLASSES = num_classes cfg.DATASETS.NUM_CLASSES = num_classes cfg.freeze() config.set_global_cfg(cfg) #model = PredictModel(cfg=cfg,is_remove_batch=False,input_shape=[1,256,480,3],input_name="input",input_dtype=tf.float32) model = PredictModel(cfg=cfg, is_remove_batch=False, input_shape=[1, None, None, 3], input_name="input", input_dtype=tf.float32) #model = PredictModel(cfg=cfg,is_remove_batch=False,input_shape=[1,None,None,3],input_name="input",input_dtype=tf.float32) rename_dict = {RD_BOXES: "bboxes", RD_PROBABILITY: "probs", RD_ID: "id"} model.remove_batch_and_rename(rename_dict) model.restoreVariables() names = [ "shared_head/heat_ct/Conv_1/BiasAdd", "shared_head/ct_regr/Conv_1/BiasAdd", "shared_head/hw_regr/Conv_1/BiasAdd", "shared_head/l2_normalize" ] #model.savePBFile("/home/wj/0day/test.pb",["bboxes","probs","id"]) #model.savePBFile("/home/wj/0day/mot_large.pb",names) model.savePBFile("/home/wj/0day/motv3.pb", names) #return tracker = build_mot(cfg, model) #model.savePBFile("/home/wj/0day/test.pb",names) #return #tracker = JDETracker(model) #tracker = CPPTracker(model) path = '/home/wj/ai/mldata/MOT/MOT20/test/MOT20-04' path = '/home/wj/ai/mldata/MOT/MOT20/test_1img' path = '/home/wj/ai/mldata/MOT/MOT15/test2/TUD-Crossing/img1' path = '/home/wj/ai/mldata/pose3d/basketball2.mp4' path = '/home/wj/ai/mldata/pose3d/tennis1.mp4' #files = wmlu.recurse_get_filepath_in_dir(args.test_data_dir,suffix=".jpg") files = wmlu.recurse_get_filepath_in_dir(path, suffix=".jpg") #save_path = args.save_data_dir save_dir = '/home/wj/ai/mldata/MOT/output2' save_path = osp.join(save_dir, osp.basename(path)) wmlu.create_empty_dir(save_dir, remove_if_exists=True, yes_to_all=True) writer = wmli.VideoWriter(save_path) reader = wmli.VideoReader(path) for img in reader: img = wmli.resize_width(img, 960) objs = tracker.update(img) img = tracker.draw_tracks(img, objs) writer.write(img) writer.release()
def copy_to_dir(files, save_dir): wmlu.create_empty_dir(save_dir, remove_if_exists=False) for f in files: wmlu.safe_copy(f, save_dir)
import os import wml_utils as wmlu from object_detection2.snpe_toolkit.toolkit import * save_dir = "/home/wj/0day/data" src_dir = "/home/wj/ai/mldata/MOT/MOT15/test/ETH-Crossing/img1" dir_path_in_target_file = None max_file_nr = 20 input_size = (256, 480) if dir_path_in_target_file is None: dir_path_in_target_file = save_dir wmlu.create_empty_dir(save_dir) images = wmlu.recurse_get_filepath_in_dir(src_dir, suffix=".jpg") if max_file_nr is not None: images = images[:max_file_nr] #output_layers = [] output_layers = [ "shared_head/l2_normalize/Square", "shared_head/hw_regr/Conv_1/Conv2D", "shared_head/ct_regr/Conv_1/Conv2D", "shared_head/heat_ct/Conv_1/Conv2D" ], input_file_list = os.path.join(save_dir, "input.txt") with open(input_file_list, "w") as f: if output_layers is not None and len(output_layers) > 0: v = f"#{output_layers[0]}" for x in output_layers[1:]: v += f" {x}" v += "\n" f.write(v)
remove_nr = 0 remove_labels = [] bboxes2remove = [] for i,l in enumerate(labels_names): if l in labels: remove_nr += 1 bboxes2remove.append(bboxes[i]) remove_labels.append(l) continue _bboxes.append(bboxes[i]) _labels_name.append(l) if remove_nr==0: wmlu.try_link(img_file, save_dir) shutil.copy(xml_file,save_dir) else: print(f"{wmlu.base_name(xml_file)} remove {remove_nr} labels, labels is {remove_labels}") img_save_path = osp.join(save_dir,osp.basename(img_file)) xml_save_path = osp.join(save_dir,osp.basename(xml_file)) img = wmli.imread(img_file) img = wmli.remove_boxes_of_img(img,np.array(bboxes2remove).astype(np.int32)) wmli.imwrite(img_save_path,img) write_voc_xml(xml_save_path,img_save_path,shape,_bboxes,_labels_name,is_relative_coordinate=False) if __name__ == "__main__": args = parse_args() files = wmlu.recurse_get_filepath_in_dir(args.src_dir,suffix=".xml") wmlu.create_empty_dir(args.out_dir,remove_if_exists=False) for xml_file in files: trans_one_file(xml_file,args.out_dir,args.labels,args.img_ext)
def build_net(self): if not os.path.exists(self.log_dir): wmlu.create_empty_dir(self.log_dir) if not os.path.exists(self.ckpt_dir): wmlu.create_empty_dir(self.ckpt_dir) with tf.device(":/cpu:0"): data = self.data.get_next() DataLoader.detection_image_summary(data, name="data_source") self.input_data = data '''if self.cfg.GLOBAL.DEBUG: data[IMAGE] = tf.Print(data[IMAGE],[tf.shape(data[IMAGE]),data[ORG_HEIGHT],data[ORG_WIDTH],data[HEIGHT],data[WIDTH]],summarize=100, name="XXXXX")''' self.res_data, loss_dict = self.model.forward(data) if self.model.is_training: for k, v in loss_dict.items(): tf.summary.scalar(f"loss/{k}", v) v = tf.cond(tf.logical_or(tf.is_nan(v), tf.is_inf(v)), lambda: tf.zeros_like(v), lambda: v) tf.losses.add_loss(v) elif self.cfg.GLOBAL.SUMMARY_LEVEL <= SummaryLevel.RESEARCH: research = self.cfg.GLOBAL.RESEARCH if 'result_classes' in research: print("replace labels with gtlabels.") labels = odt.replace_with_gtlabels( bboxes=self.res_data[RD_BOXES], labels=self.res_data[RD_LABELS], length=self.res_data[RD_LENGTH], gtbboxes=data[GT_BOXES], gtlabels=data[GT_LABELS], gtlength=data[GT_LENGTH]) self.res_data[RD_LABELS] = labels if 'result_bboxes' in research: print("replace bboxes with gtbboxes.") bboxes = odt.replace_with_gtbboxes( bboxes=self.res_data[RD_BOXES], labels=self.res_data[RD_LABELS], length=self.res_data[RD_LENGTH], gtbboxes=data[GT_BOXES], gtlabels=data[GT_LABELS], gtlength=data[GT_LENGTH]) self.res_data[RD_BOXES] = bboxes self.loss_dict = loss_dict if not self.model.is_training and self.cfg.GLOBAL.GPU_MEM_FRACTION > 0.1: gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=self. cfg.GLOBAL.GPU_MEM_FRACTION) config = tf.ConfigProto(allow_soft_placement=True, gpu_options=gpu_options) else: config = tf.ConfigProto(allow_soft_placement=True) if not self.model.is_training and self.cfg.GLOBAL.GPU_MEM_FRACTION <= 0.1: config.gpu_options.allow_growth = True self.sess = tf.Session(config=config) self.top_variable_name_scope = "Model" if self.model.is_training: steps = self.cfg.SOLVER.STEPS print("Train steps:", steps) lr = wnn.build_learning_rate( self.cfg.SOLVER.BASE_LR, global_step=self.global_step, lr_decay_type=self.cfg.SOLVER.LR_DECAY_TYPE, steps=steps, decay_factor=self.cfg.SOLVER.LR_DECAY_FACTOR, total_steps=steps[-1], warmup_steps=self.cfg.SOLVER.WARMUP_ITERS) tf.summary.scalar("lr", lr) opt = wnn.str2optimizer("Momentum", lr, momentum=0.9) self.max_train_step = steps[-1] self.train_op, self.total_loss, self.variables_to_train = wnn.nget_train_op( self.global_step, optimizer=opt, clip_norm=self.cfg.SOLVER.CLIP_NORM) print("variables to train:") wmlu.show_list(self.variables_to_train) for v in self.variables_to_train: wsummary.histogram_or_scalar(v, v.name[:-2]) wnn.log_moving_variable() self.saver = tf.train.Saver(max_to_keep=100) tf.summary.scalar(self.cfg.GLOBAL.PROJ_NAME + "_total_loss", self.total_loss) self.summary = tf.summary.merge_all() self.summary_writer = tf.summary.FileWriter(self.log_dir, self.sess.graph) init = tf.global_variables_initializer() self.sess.run(init) print("batch_norm_ops.") wmlu.show_list( [x.name for x in tf.get_collection(tf.GraphKeys.UPDATE_OPS)])