def __init__(self, dataset_type): if dataset_type == 'second_train': batch_size = cfg.TRAIN.SECONDBATCH_SIZE dataset_type = 'train' else: batch_size = cfg.TRAIN.BATCH_SIZE self.annot_path = cfg.TRAIN.ANNOT_PATH if dataset_type == 'train' else cfg.TEST.ANNOT_PATH self.input_sizes = cfg.TRAIN.INPUT_SIZE if dataset_type == 'train' else cfg.TEST.INPUT_SIZE self.batch_size = batch_size if dataset_type == 'train' else cfg.TEST.BATCH_SIZE self.data_aug = cfg.TRAIN.DATA_AUG if dataset_type == 'train' else cfg.TEST.DATA_AUG self.train_input_sizes = cfg.TRAIN.INPUT_SIZE self.strides = np.array(cfg.YOLO.STRIDES) self.classes = utils.read_class_names(cfg.YOLO.CLASSES) self.num_classes = len(self.classes) self.anchors = np.array(utils.get_anchors(cfg.YOLO.ANCHORS)) self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.max_bbox_per_scale = 150 self.annotations = self.load_annotations(dataset_type) self.num_samples = len(self.annotations) self.num_batchs = int(np.ceil(self.num_samples / self.batch_size)) self.batch_count = 0
def __init__(self, input_data, trainable,score_threshold=0.3,iou_threshold=0.45): ''' :param input_data: :param trainable: ''' self.trainable = trainable self.classes = utils.read_class_names(cfg.YOLO.CLASSES)#类别列表 self.num_class = len(self.classes)#类别数量 self.strides = np.array(cfg.YOLO.STRIDES) self.anchors = utils.get_anchors(cfg.YOLO.ANCHORS)#anchors self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.iou_loss_thresh = cfg.YOLO.IOU_LOSS_THRESH self.upsample_method = cfg.YOLO.UPSAMPLE_METHOD#上采样方式 self.per_cls_maxboxes = 200 # 一张图像上,每一类别的检测结果做大数量 try: self.conv_lbbox,self.conv_mbbox,self.conv_sbbox = self.__build_network(input_data) except: raise NotImplementedError("Can not build up yolov3 network!") # 对输出加上name属性,这样在固化模型,生成.pb文件时,可以找到指定的节点 with tf.variable_scope('pred_sbbox'):# 检测小目标,和anchor[0]对应 self.pred_sbbox = self.decode(self.conv_sbbox,self.anchors[0],self.strides[0])# self.conv_sbbox.shaep:(batch_size,52,52,255),strides[0]=8, 52*8=416(416/52=8),一个特征点代表8*8的图像,检测小目标,对应的self.anchors[0]中的anchor宽高值必须是小目标的 with tf.variable_scope('pred_mbbox'): self.pred_mbbox = self.decode(self.conv_mbbox,self.anchors[1],self.strides[1]) with tf.variable_scope('pred_lbbox'): self.pred_lbbox = self.decode(self.conv_lbbox,self.anchors[2],self.strides[2]) with tf.variable_scope('pred_res'): # 最终检测结果 self.pred_res_boxes = self._get_pred_bboxes(input_data,score_threshold,iou_threshold)
def __init__(self): self.input_size = cfg.TEST.INPUT_SIZE self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.classes = {0: 'front', 1: 'rear'} self.num_classes = len(self.classes) self.anchors = np.array(utils.get_anchors(cfg.YOLO.ANCHORS)) self.score_threshold = cfg.TEST.SCORE_THRESHOLD self.iou_threshold = cfg.TEST.IOU_THRESHOLD self.moving_ave_decay = cfg.YOLO.MOVING_AVE_DECAY self.annotation_path = cfg.TEST.ANNOT_PATH self.weight_file = cfg.TEST.WEIGHT_FILE self.write_image = cfg.TEST.WRITE_IMAGE self.write_image_path = cfg.TEST.WRITE_IMAGE_PATH self.test_video_path = cfg.TEST.TEST_VIDEO_PATH self.predict_video_path = cfg.TEST.PREDICT_VIDEO_PATH with tf.name_scope('input'): self.input_data = tf.placeholder(dtype=tf.float32, shape=[1, 544, 544, 3], name='input_data') self.trainable = tf.placeholder(dtype=tf.bool, name='trainable') model = YOLOV3(self.input_data, self.trainable, 1) self.pred_sbbox, self.pred_mbbox, self.pred_lbbox = model.pred_sbbox, model.pred_mbbox, model.pred_lbbox self.conv_sbbox, self.conv_mbbox, self.conv_lbbox = model.conv_sbbox, model.conv_mbbox, model.conv_lbbox with tf.name_scope('ema'): ema_obj = tf.train.ExponentialMovingAverage(self.moving_ave_decay) self.sess = tf.Session(config=tf.ConfigProto( allow_soft_placement=True)) self.saver = tf.train.Saver() self.saver.restore(self.sess, self.weight_file)
def load_config(CLASSES): RETURN_STRIDES = np.array(STRIDES) RETURN_ANCHORS = utils.get_anchors(ANCHORS, False) RETURN_XYSCALE = XYSCALE RETURN_NUM_CLASS = len(utils.read_class_names(CLASSES)) return RETURN_STRIDES, RETURN_ANCHORS, RETURN_NUM_CLASS, RETURN_XYSCALE
def __init__(self, dataset_type): self.annot_path = cfg.TRAIN.ANNOT_PATH if dataset_type == 'train' else cfg.TEST.ANNOT_PATH self.input_sizes = cfg.TRAIN.INPUT_SIZE if dataset_type == 'train' else cfg.TEST.INPUT_SIZE self.batch_size = cfg.TRAIN.BATCH_SIZE if dataset_type == 'train' else cfg.TEST.BATCH_SIZE self.data_aug = cfg.TRAIN.DATA_AUG if dataset_type == 'train' else cfg.TEST.DATA_AUG self.train_input_sizes = cfg.TRAIN.INPUT_SIZE self.strides = np.array(cfg.YOLO.STRIDES) self.classes = utils.read_class_names(cfg.YOLO.CLASSES) self.num_classes = len(self.classes) self.anchors = np.array(utils.get_anchors(cfg.YOLO.ANCHORS)) #array([[[ 10., 13.], # [ 16., 30.], # [ 33., 23.]], # # [[ 30., 61.], # [ 62., 45.], # [ 59., 119.]], # # [[116., 90.], # [156., 198.], # [373., 326.]]], dtype=float32) self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.max_bbox_per_scale = 150 self.annotations = self.load_annotations(dataset_type) self.num_samples = len(self.annotations) self.num_batchs = int(np.ceil(self.num_samples / self.batch_size)) self.batch_count = 0
def __init__(self, input_data, trainable): self.trainable = trainable # 训练 self.classes = utils.read_class_names(cfg.YOLO.CLASSES) # 类别 self.num_class = len(self.classes) # 类别长度 self.strides = np.array(cfg.YOLO.STRIDES) # 预测物体的大中小 self.anchors = utils.get_anchors( cfg.YOLO.ANCHORS, is_tiny=True) # 数据集的Anchors, tiny为2 x 3 self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE # Anchors 拉伸 self.iou_loss_thresh = cfg.YOLO.IOU_LOSS_THRESH # 阀值 self.upsample_method = cfg.YOLO.UPSAMPLE_METHOD # 上采样方法 try: if cfg.YOLO.DSC: self.conv_lbbox, self.conv_mbbox = self.__build_nework_DSC( input_data) else: self.conv_lbbox, self.conv_mbbox = self.__build_nework( input_data) except: raise NotImplementedError("Can not build up YOLOv3-tiny network!") # 中物体 with tf.variable_scope('pred_mbbox'): self.pred_mbbox = self.decode(self.conv_mbbox, self.anchors[0], self.strides[0]) # 大物体 with tf.variable_scope('pred_lbbox'): self.pred_lbbox = self.decode(self.conv_lbbox, self.anchors[1], self.strides[1])
def __init__(self, input_data): self.classes = utils.read_class_names(cfg.YOLO.CLASSES) self.num_class = len(self.classes) self.strides = np.array(cfg.YOLO.STRIDES) self.anchors = utils.get_anchors(cfg.YOLO.ANCHORS) self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.iou_loss_thresh = cfg.YOLO.IOU_LOSS_THRESH self.upsample_method = cfg.YOLO.UPSAMPLE_METHOD self.x = input_data try: self.conv_lbbox, self.conv_mbbox, self.conv_sbbox = self.__build_nework( input_data) except: raise NotImplementedError("Can not build up yolov3 network!") self.pred_sbbox = self.decode(self.conv_sbbox, self.anchors[0], self.strides[0]) self.pred_mbbox = self.decode(self.conv_mbbox, self.anchors[1], self.strides[1]) self.pred_lbbox = self.decode(self.conv_lbbox, self.anchors[2], self.strides[2]) self.model = tf.keras.Model( self.x, [self.pred_sbbox, self.pred_mbbox, self.pred_lbbox])
def __init__(self): self.input_size = cfg.TEST.INPUT_SIZE self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.classes = utils.read_class_names(cfg.YOLO.CLASSES) self.num_classes = len(self.classes) self.anchors = np.array(utils.get_anchors(cfg.YOLO.ANCHORS)) self.score_threshold = cfg.TEST.SCORE_THRESHOLD self.iou_threshold = cfg.TEST.IOU_THRESHOLD self.moving_ave_decay = cfg.YOLO.MOVING_AVE_DECAY self.annotation_path = cfg.TEST.ANNOT_PATH self.weight_file = cfg.TEST.WEIGHT_FILE self.write_image = cfg.TEST.WRITE_IMAGE self.write_image_path = cfg.TEST.WRITE_IMAGE_PATH self.show_label = cfg.TEST.SHOW_LABEL with tf.name_scope('input'): self.input_data = tf.placeholder(dtype=tf.float32, name='input_data') self.trainable = tf.placeholder(dtype=tf.bool, name='trainable') # model = YOLOV3(self.input_data, self.trainable) # self.pred_sbbox, self.pred_mbbox, self.pred_lbbox = model.pred_sbbox, model.pred_mbbox, model.pred_lbbox self.return_tensors, self.graph = get_tensors() # with tf.name_scope('ema'): # ema_obj = tf.train.ExponentialMovingAverage(self.moving_ave_decay) self.sess = tf.Session( graph=self.graph, config=tf.ConfigProto(allow_soft_placement=True))
def __init__(self): self.input_size = cfg.TEST.INPUT_SIZE self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.classes = utils.read_class_names(cfg.YOLO.CLASSES) self.num_classes = len(self.classes) self.anchors = np.array(utils.get_anchors(cfg.YOLO.ANCHORS)) self.score_threshold = cfg.TEST.SCORE_THRESHOLD self.iou_threshold = cfg.TEST.IOU_THRESHOLD self.moving_ave_decay = cfg.YOLO.MOVING_AVE_DECAY self.annotation_path = cfg.TEST.ANNOT_PATH self.weight_file = cfg.TEST.WEIGHT_FILE self.write_image = cfg.TEST.WRITE_IMAGE self.write_image_path = cfg.TEST.WRITE_IMAGE_PATH self.show_label = cfg.TEST.SHOW_LABEL with tf.name_scope('input'): self.input_data = tf.placeholder(dtype=tf.float32, name='input_data') self.trainable = tf.placeholder(dtype=tf.bool, name='trainable') model = YOLOV3() #(self.input_data, self.trainable) sqback = backbone.squeezenet('sqz_full.mat') sqback.forward(imgs=self.input_data, trainable=False) model.forward(sqback, False) self.pred_sbbox, self.pred_mbbox, self.pred_lbbox = model.pred_sbbox, model.pred_mbbox, model.pred_lbbox with tf.name_scope('ema'): ema_obj = tf.train.ExponentialMovingAverage(self.moving_ave_decay) self.sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) self.saver = tf.train.Saver(ema_obj.variables_to_restore()) self.saver.restore(self.sess, self.weight_file)
def __init__(self, input_data, trainable): self.trainable = trainable self.classes = utils.read_class_names(cfg.YOLO.CLASSES) ## dict: idx to name self.num_class = len(self.classes) self.strides = np.array(cfg.YOLO.STRIDES) #(3,) [8, 16, 32] self.anchors = utils.get_anchors(cfg.YOLO.ANCHORS) ##(3,3,2) 分别代表三层feature_map对应3个anchor w,h self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE # 3 self.iou_loss_thresh = cfg.YOLO.IOU_LOSS_THRESH # 0.5 self.upsample_method = cfg.YOLO.UPSAMPLE_METHOD try: self.conv_lbbox, self.conv_mbbox, self.conv_sbbox = self.__build_nework(input_data) except: raise NotImplementedError("Can not build up yolov3 network!") with tf.variable_scope('pred_sbbox'): self.pred_sbbox = self.decode(self.conv_sbbox, self.anchors[0], self.strides[0]) with tf.variable_scope('pred_mbbox'): self.pred_mbbox = self.decode(self.conv_mbbox, self.anchors[1], self.strides[1]) with tf.variable_scope('pred_lbbox'): self.pred_lbbox = self.decode(self.conv_lbbox, self.anchors[2], self.strides[2])
def __init__(self, train_flag=True): """ :param train_flag: 是否是训练,默认训练 """ self.train_flag = train_flag # 训练数据 if train_flag: self.data_file_path = cfg.TRAIN.TRAIN_DATA_PATH self.batch_size = cfg.TRAIN.TRAIN_BATCH_SIZE pass # 验证数据 else: self.data_file_path = cfg.TRAIN.VAL_DATA_PATH self.batch_size = cfg.TRAIN.VAL_BATCH_SIZE pass self.train_input_size_list = cfg.TRAIN.INPUT_SIZE_LIST self.strides = np.array(cfg.YOLO.STRIDES) self.classes = utils.read_class_names(cfg.COMMON.CLASS_FILE_PATH) self.class_num = len(self.classes) self.anchor_list = utils.get_anchors(cfg.COMMON.ANCHOR_FILE_PATH) self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.max_bbox_per_scale = cfg.COMMON.MAX_BBOX_PER_SCALE self.annotations = self.read_annotations() self.sample_num = len(self.annotations) self.batch_num = int(np.ceil(self.sample_num / self.batch_size)) self.batch_count = 0 pass
def __init__(self, input_data, trainable): self.trainable = trainable self.classes = utils.read_class_names(cfg.YOLO.CLASSES) self.num_class = len(self.classes) self.strides = np.array(cfg.YOLO.TINY_STRIDES) self.anchors = utils.get_anchors(cfg.YOLO.TINY_ANCHORS, is_tiny=True) self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.iou_loss_thresh = cfg.YOLO.IOU_LOSS_THRESH self.upsample_method = cfg.YOLO.UPSAMPLE_METHOD self.input_size = cfg.TRAIN.INPUT_SIZE self.frame_size = (cfg.TRAIN.INPUT_SIZE, cfg.TRAIN.INPUT_SIZE) try: self.conv_lbbox, self.conv_mbbox = self.__build_nework(input_data) except: raise NotImplementedError("Can not build up yolov3 network!") # with tf.variable_scope('pred_sbbox'): # self.pred_sbbox = self.decode(self.conv_sbbox, self.anchors[0], self.strides[0]) with tf.variable_scope('pred_mbbox'): self.pred_mbbox = self.decode(self.conv_mbbox, self.anchors[0], self.strides[0]) with tf.variable_scope('pred_lbbox'): self.pred_lbbox = self.decode(self.conv_lbbox, self.anchors[1], self.strides[1])
def __init__(self, input_data, trainable): self.trainable = trainable self.num_class = 4 self.strides = np.array(cfg.YOLO.STRIDES) self.anchors = utils.get_anchors(cfg.YOLO.ANCHORS) self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.iou_loss_thresh = cfg.YOLO.IOU_LOSS_THRESH self.upsample_method = cfg.YOLO.UPSAMPLE_METHOD try: self.conv_lbbox, self.conv_mbbox, self.conv_sbbox = self.__build_nework( input_data) except: raise NotImplementedError("Can not build up yolov3 network!") with tf.variable_scope('pred_sbbox'): self.pred_sbbox = self.decode(self.conv_sbbox, self.anchors[0], self.strides[0]) with tf.variable_scope('pred_mbbox'): self.pred_mbbox = self.decode(self.conv_mbbox, self.anchors[1], self.strides[1]) with tf.variable_scope('pred_lbbox'): self.pred_lbbox = self.decode(self.conv_lbbox, self.anchors[2], self.strides[2])
def __init__(self, is_training: bool, dataset_type: str = "converted_coco"): self.dataset_type = dataset_type self.annot_path = ( cfg.TRAIN.ANNOT_PATH if is_training else cfg.TEST.ANNOT_PATH ) self.input_sizes = ( cfg.TRAIN.INPUT_SIZE if is_training else cfg.TEST.INPUT_SIZE ) self.batch_size = ( cfg.TRAIN.BATCH_SIZE if is_training else cfg.TEST.BATCH_SIZE ) self.data_aug = cfg.TRAIN.DATA_AUG if is_training else cfg.TEST.DATA_AUG self.train_input_sizes = cfg.TRAIN.INPUT_SIZE self.strides = np.array(cfg.YOLO.STRIDES) self.classes = utils.read_class_names(cfg.YOLO.CLASSES) self.num_classes = len(self.classes) self.anchors = np.array(utils.get_anchors(cfg.YOLO.ANCHORS)) self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.max_bbox_per_scale = 150 self.annotations = self.load_annotations() self.num_samples = len(self.annotations) self.num_batchs = int(np.ceil(self.num_samples / self.batch_size)) self.batch_count = 0
def __init__(self, input_data, trainable): self.trainable = trainable self.classes = utils.read_class_names(config.YOLO.CLASSES) self.class_num = len(self.classes) self.strides = np.array(config.YOLO.STRIDES) # [8,16,32] (3,) self.anchors = utils.get_anchors(config.YOLO.ANCHORS) # (3,3,2) self.anchor_per_scale = config.YOLO.ANCHOR_PER_SCALE # 3 self.iou_loss_thresh = config.YOLO.IOU_LOSS_THRESH # 阈值 self.upsample_method = config.YOLO.UPSAMPLE_METHOD # 上采样的方法 try: self.conv_smt_box, self.conv_mid_box, self.conv_big_box = self.yolo_v3_network( input_data) except: raise NotImplementedError("Can not build up yolov3_network!") with tf.variable_scope('pred_smt_box'): # shape = (52,52, 3 * (self.class_num + 5) #[3,2] #8 self.pred_smt_box = self.decode(self.conv_smt_box, self.anchors[0], self.strides[0]) with tf.variable_scope('pred_mid_box'): # shape = (26, 26, 3 * (self.class_num + 5) #[3,2] #16 self.pred_mid_box = self.decode(self.conv_mid_box, self.anchors[1], self.strides[1]) with tf.variable_scope('pred_big_box'): # shape = (13, 13, 3 * (self.class_num + 5) #[3,2] #32 self.pred_big_box = self.decode(self.conv_big_box, self.anchors[2], self.strides[2])
def __init_(self, input_data, trainable): self.trainable = trainable self.classes = utils.read_class_names(cfg.YOLO.CLASSES) self.num_class = len(self.classes) self.strides = np.array(cfg.YOLO.STRIDES) self.anchors = utils.get_anchors(cfg.YOLO.ANCHORS) self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.iou_loss_thresh = cfg.YOLO.iou_loss_threshself self.upsample_method = cfg.YOLO.UPSAMPLE_METHOD try: self.conv_lbbox, self.conv_mbbox, self.conv_sbbox = self.object_detection_network( input_data) except: raise NotImplementedError("YOLOv3 network can't be built") with tf.variable_scope('pred_sbbox'): self.pred_sbbox = self.decode(self.conv_sbbox, self.anchors[0], self.strides[0]) with tf.variable_scope('pred_mbbox'): self.pred_mbbox = self.decode(self.conv_mbbox, self.anchors[1], self.strides[1]) with tf.variable_scope('pred_lbbox'): self.pred_lbbox = self.decode(self.conv_lbbox, self.anchors[2], self.strides[2])
def __init__(self, dataset_type, opts): """Prepare info for generating data Args: dataset_type: one of {"train", "test"} opts: configuration of type dict """ self.annot_path = opts[dataset_type]["annot_path"] self.batch_size = opts[dataset_type]["batch_size"] self.data_aug = opts[dataset_type]["data_aug"] self.use_mosaic_input = opts["train"]["use_mosaic_input"] self.use_color_augment = opts["train"]["use_color_augment"] self.train_input_sizes = opts["train"]["input_size"] self.strides = np.array(opts["yolo"]["strides"]) classes = utils.read_class_names(opts["yolo"]["classes"]) self.num_classes = len(classes) self.anchors = np.array(utils.get_anchors(opts["yolo"]["anchors"])) self.anchor_per_scale = opts["yolo"]["anchor_per_scale"] self.data_name = opts["yolo"]["data_name"] self.max_bbox_per_scale = 150 self.dataset_type = dataset_type self.annotations = self.load_annotations(self.annot_path) self.num_samples = len(self.annotations) self.num_batches = int(np.ceil(self.num_samples / self.batch_size)) self.batch_count = 0 self.dump_files = None self.read_index = 0 self.images = None self.cache_images = None self.use_pre_load = opts["train"]["use_pre_load"]
def __init__(self, train_or_test_datsets): self.path1 = r"./data/dataset/antenna_train.txt" self.path2 = r"./data/dataset/antenna_test.txt" self.annot_path = self.path1 if train_or_test_datsets == 'train' else self.path2 self.input_sizes = [416] if train_or_test_datsets == 'train' else 416 self.batch_size = 12 if train_or_test_datsets == 'train' else 3 self.data_aug = True if train_or_test_datsets == 'train' else False self.train_input_sizes = [416] self.strides = np.array([8, 16, 32]) self.path3 = r"./data/classes/antenna.names" self.classes = utils.read_class_names(self.path3) self.num_classes = len(self.classes) print("num", self.num_classes) self.path4 = r"./data/anchors/basline_anchors.txt" self.anchors = np.array(utils.get_anchors(self.path4)) # print("12self.anchor",self.anchors) self.anchor_per_scale = 3 self.max_bbox_per_scale = 150 self.annotations = self.load_annotations(train_or_test_datsets) self.num_samples = len(self.annotations) self.num_batchs = int(np.ceil(self.num_samples / self.batch_size)) self.batch_count = 0
def __init__(self, dataset_type): # dataset_type 用于选择训练集和测试集 self.annot_path = cfg.TRAIN.ANNOT_PATH if dataset_type == 'train' else cfg.TEST.ANNOT_PATH self.input_sizes = cfg.TRAIN.INPUT_SIZE if dataset_type == 'train' else cfg.TEST.INPUT_SIZE # TRAIN self.input_sizes = 416 # TEST self.input_sizes = 544 self.batch_size = cfg.TRAIN.BATCH_SIZE if dataset_type == 'train' else cfg.TEST.BATCH_SIZE # TRAIN self.batch_size = 4 # TEST self.batch_size = 2 self.data_aug = cfg.TRAIN.DATA_AUG if dataset_type == 'train' else cfg.TEST.DATA_AUG # TRAIN=True 参数可训练 # TEST =False 参数冻结不可训练 self.train_input_sizes = cfg.TRAIN.INPUT_SIZE # 416 self.strides = np.array(cfg.YOLO.STRIDES) # [8, 16, 32,] self.classes = utils.read_class_names(cfg.YOLO.CLASSES) # self.classes = 1 2 3 4 5 6 7 8 9 0 十个数字 self.num_classes = len(self.classes) self.anchors = np.array(utils.get_anchors(cfg.YOLO.ANCHORS)) # [3,3,2] self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE # anchors通过聚类求出,可以加快训练速度,但对最终训练精度无影响 # anchors分为三个步长,每个步长包含三个尺寸的边框,实现不同精度的检验 self.max_bbox_per_scale = 150 #每个尺寸下最多有150个候选框 self.annotations = self.load_annotations(dataset_type) self.num_samples = len(self.annotations) self.num_batchs = int(np.ceil(self.num_samples / self.batch_size)) # np.ceil向上取整,一个batch取多少样本 int(1000/4)=250 # np.ceil 计算大于等于该值的最小值 self.batch_count = 0
def main(argv): flags = parser( description="freeze yolov3 graph from checkpoint file").parse_args() print("=> the input image size is [%d, %d]" % (flags.image_h, flags.image_w)) anchors = utils.get_anchors(flags.anchors_path, flags.image_h, flags.image_w) model = yolov3.yolov3(flags.num_classes, anchors) with tf.Graph().as_default() as graph: sess = tf.Session(graph=graph) inputs = tf.placeholder(tf.float32, [1, flags.image_h, flags.image_w, 3 ]) # placeholder for detector inputs print("=>", inputs) with tf.variable_scope('yolov3'): feature_map = model.forward(inputs, is_training=False) boxes, confs, probs = model.predict(feature_map) scores = confs * probs print("=>", boxes.name[:-2], scores.name[:-2]) cpu_out_node_names = [boxes.name[:-2], scores.name[:-2]] boxes, scores, labels = utils.gpu_nms( boxes, scores, flags.num_classes, score_thresh=flags.score_threshold, iou_thresh=flags.iou_threshold) print("=>", boxes.name[:-2], scores.name[:-2], labels.name[:-2]) gpu_out_node_names = [ boxes.name[:-2], scores.name[:-2], labels.name[:-2] ] feature_map_1, feature_map_2, feature_map_3 = feature_map saver = tf.train.Saver(var_list=tf.global_variables(scope='yolov3')) if flags.convert: if not os.path.exists(flags.weights_path): url = 'https://github.com/YunYang1994/tensorflow-yolov3/releases/download/v1.0/yolov3.weights' for i in range(3): time.sleep(1) print("=> %s does not exists ! " % flags.weights_path) print("=> It will take a while to download it from %s" % url) print('=> Downloading yolov3 weights ... ') wget.download(url, flags.weights_path) load_ops = utils.load_weights(tf.global_variables(scope='yolov3'), flags.weights_path) sess.run(load_ops) save_path = saver.save(sess, save_path=flags.ckpt_file) print('=> model saved in path: {}'.format(save_path)) if flags.freeze: saver.restore(sess, flags.ckpt_file) print('=> checkpoint file restored from ', flags.ckpt_file) utils.freeze_graph(sess, './checkpoint/yolov3_cpu_nms.pb', cpu_out_node_names) utils.freeze_graph(sess, './checkpoint/yolov3_gpu_nms.pb', gpu_out_node_names)
def __init__(self, input_data, trainable): self.trainable = trainable self.classes = utils.read_class_names(cfg.YOLO.CLASSES) self.num_class = len(self.classes) self.strides = np.array(cfg.YOLO.STRIDES) self.anchors = utils.get_anchors(cfg.YOLO.ANCHORS) self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.iou_loss_thresh = cfg.YOLO.IOU_LOSS_THRESH self.upsample_method = cfg.YOLO.UPSAMPLE_METHOD try: # 得到yolo的三个尺度输出:(13, 13, 256); (26, 26, 256); (52, 52, 256) self.conv_lbbox, self.conv_mbbox, self.conv_sbbox = self.build_nework( input_data) except: raise AttributeError("Can not build up yolov3 network!") with tf.variable_scope('pred_sbbox'): self.pred_sbbox = self.decode(self.conv_sbbox, self.anchors[0], self.strides[0]) with tf.variable_scope('pred_mbbox'): self.pred_mbbox = self.decode(self.conv_mbbox, self.anchors[1], self.strides[1]) with tf.variable_scope('pred_lbbox'): self.pred_lbbox = self.decode(self.conv_lbbox, self.anchors[2], self.strides[2])
def __init__(self, dataset_type): self.annot_path = cfg.TRAIN.ANNOT_PATH if dataset_type == 'train' else cfg.TEST.ANNOT_PATH self.input_sizes = cfg.TRAIN.INPUT_SIZE if dataset_type == 'train' else cfg.TEST.INPUT_SIZE self.batch_size = cfg.TRAIN.BATCH_SIZE if dataset_type == 'train' else cfg.TEST.BATCH_SIZE self.data_aug = cfg.TRAIN.DATA_AUG if dataset_type == 'train' else cfg.TEST.DATA_AUG self.train_input_sizes = cfg.TRAIN.INPUT_SIZE self.strides = np.array(cfg.YOLO.STRIDES) self.classes = utils.read_class_names(cfg.YOLO.CLASSES) self.num_classes = len(self.classes) self.anchors = np.array(utils.get_anchors(cfg.YOLO.ANCHORS)) self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.max_bbox_per_scale = 150 self.annotations = self.load_annotations(dataset_type) self.num_samples = len(self.annotations) self.num_batchs = int(np.ceil(self.num_samples / self.batch_size)) self.batch_count = 0 self.new_aug = False if self.new_aug: gpu_options = tf.GPUOptions(visible_device_list='') config = tf.ConfigProto(gpu_options=gpu_options) self.sess = tf.Session(config=config) self.aug_fun = self.augGraph(self.sess)
def __init__(self, dataset_type): self.dataset_path = cfg.TRAIN.DATASET_PATH if dataset_type == 'train': self.input_sizes = cfg.TRAIN.INPUT_SIZE self.batch_size = cfg.TRAIN.BATCH_SIZE self.data_aug = cfg.TRAIN.DATA_AUG elif dataset_type == 'val': self.input_sizes = cfg.VAL.INPUT_SIZE self.batch_size = cfg.VAL.BATCH_SIZE self.data_aug = cfg.VAL.DATA_AUG else: self.input_sizes = cfg.TEST.INPUT_SIZE self.batch_size = cfg.TEST.BATCH_SIZE self.data_aug = cfg.TEST.DATA_AUG self.dataset_type = dataset_type self.train_input_sizes = cfg.TRAIN.INPUT_SIZE self.strides = np.array(cfg.YOLO.STRIDES) self.classes = utils.read_class_names(cfg.YOLO.CLASSES) self.num_classes = len(self.classes) self.anchors = np.array(utils.get_anchors(cfg.YOLO.ANCHORS)) self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.max_bbox_per_scale = 200 self.meta_datas = self.load_data(dataset_type) self.num_samples = len(self.meta_datas) self.idx = 0
def __init__(self, input_data, trainable, tiny=False): self.trainable = trainable self.classes = utils.read_class_names(cfg.YOLO.CLASSES) self.num_class = len(self.classes) self.strides = np.array(cfg.YOLO.STRIDES) self.anchors = utils.get_anchors(cfg.YOLO.ANCHORS) self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.iou_loss_thresh = cfg.YOLO.IOU_LOSS_THRESH self.upsample_method = cfg.YOLO.UPSAMPLE_METHOD if tiny: try: self.conv_lbbox, self.conv_mbbox, self.conv_sbbox = self.__build_tiny_3l( input_data) except: raise NotImplementedError( "Can not build up yolov3-tiny-3l network!") with tf.variable_scope('pred_sbbox'): self.pred_sbbox, self.sdetections = self.decode( self.conv_sbbox, self.anchors[0], self.strides[0]) with tf.variable_scope('pred_mbbox'): self.pred_mbbox, self.mdetections = self.decode( self.conv_mbbox, self.anchors[1], self.strides[1]) with tf.variable_scope('pred_lbbox'): self.pred_lbbox, self.ldetections = self.decode( self.conv_lbbox, self.anchors[2], self.strides[2]) self.detection_boxes = self.__detection_boxes( tf.concat( [self.sdetections, self.mdetections, self.ldetections], axis=1)) else: try: self.conv_lbbox, self.conv_mbbox, self.conv_sbbox = self.__build_nework( input_data) except: raise NotImplementedError("Can not build up yolov3 network!") with tf.variable_scope('pred_sbbox'): self.pred_sbbox, self.sdetections = self.decode( self.conv_sbbox, self.anchors[0], self.strides[0]) with tf.variable_scope('pred_mbbox'): self.pred_mbbox, self.mdetections = self.decode( self.conv_mbbox, self.anchors[1], self.strides[1]) with tf.variable_scope('pred_lbbox'): self.pred_lbbox, self.ldetections = self.decode( self.conv_lbbox, self.anchors[2], self.strides[2]) self.detection_boxes = self.__detection_boxes( tf.concat( [self.sdetections, self.mdetections, self.ldetections], axis=1))
def convert_weights(self): print(f"=> the input image size is [{self.img_h}, {self.img_w}]") anchors = utils.get_anchors(self.anchors_path, self.img_h, self.img_w) model = yolov3.yolov3(self.num_classes, anchors) with tf.Graph().as_default() as graph: sess = tf.Session(graph=graph) inputs = tf.placeholder(tf.float32, [1, self.img_h, self.img_w, 1 ]) # placeholder for detector inputs print("=>", inputs) with tf.variable_scope('yolov3'): feature_map = model.forward(inputs, n_filters_dn=self.n_filters_dn, n_strides_dn=self.n_strides_dn, n_ksizes_dn=self.n_ksizes_dn, is_training=False) boxes, confs, probs = model.predict(feature_map) scores = confs * probs print("=>", boxes.name[:-2], scores.name[:-2]) cpu_out_node_names = [boxes.name[:-2], scores.name[:-2]] boxes, scores, labels = utils.gpu_nms(boxes, scores, self.num_classes) print("=>", boxes.name[:-2], scores.name[:-2], labels.name[:-2]) gpu_out_node_names = [ boxes.name[:-2], scores.name[:-2], labels.name[:-2] ] saver = tf.train.Saver(var_list=tf.global_variables( scope='yolov3')) if self.convert: load_ops = utils.load_weights( tf.global_variables(scope='yolov3'), self.weights_dir) sess.run(load_ops) save_path = saver.save(sess, save_path=self.checkpoint_dir) print(f'=> model saved in path: {save_path}') if self.freeze: ckpt_idx = self.checkpoint_dir + '-' + str( self.checkpoint_step) try: saver.restore(sess, ckpt_idx) except: print( f"Error: you tried to restore a checkpoint ({self.checkpoint_dir}) that doesn't exist." ) print( "Please clear the network and retrain, or load a different checkpoint by changing the steps parameter." ) print('=> checkpoint file restored from ', ckpt_idx) utils.freeze_graph(sess, '../../data/checkpoint/yolov3_cpu_nms.pb', cpu_out_node_names) utils.freeze_graph(sess, '../../data/checkpoint/yolov3_gpu_nms.pb', gpu_out_node_names)
def main(_argv): if FLAGS.tiny: STRIDES = np.array(cfg.YOLO.STRIDES_TINY) ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS_TINY, FLAGS.tiny) XYSCALE = cfg.YOLO.XYSCALE_TINY else: STRIDES = np.array(cfg.YOLO.STRIDES) if FLAGS.model == 'yolov4': ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS, FLAGS.tiny) else: ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS_V3, FLAGS.tiny) XYSCALE = cfg.YOLO.XYSCALE NUM_CLASS = len(utils.read_class_names(cfg.YOLO.CLASSES)) input_size = FLAGS.size image_path = FLAGS.image original_image = cv2.imread(image_path) original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) original_image_size = original_image.shape[:2] image_data = utils.image_preprocess(np.copy(original_image), [input_size, input_size]) image_data = image_data[np.newaxis, ...].astype(np.float32) pred_bbox = grpc_yolov4_client(FLAGS.host, FLAGS.model, image_data, shape_size=FLAGS.size) if FLAGS.model == 'yolov4': if FLAGS.tiny: pred_bbox = utils.postprocess_bbbox(pred_bbox, ANCHORS, STRIDES, XYSCALE) else: pred_bbox = utils.postprocess_bbbox(pred_bbox, ANCHORS, STRIDES, XYSCALE) else: pred_bbox = utils.postprocess_bbbox(pred_bbox, ANCHORS, STRIDES) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.25) bboxes = utils.nms(bboxes, 0.213, method='nms') image = utils.draw_bbox(original_image, bboxes) image = Image.fromarray(image) image.show() image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB) cv2.imwrite(FLAGS.output, image)
def main(_argv): if FLAGS.tiny: STRIDES = np.array(cfg.YOLO.STRIDES_TINY) ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS_TINY, FLAGS.tiny) else: STRIDES = np.array(cfg.YOLO.STRIDES) if FLAGS.model == 'yolov4': ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS, FLAGS.tiny) else: ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS_V3, FLAGS.tiny) NUM_CLASS = len(utils.read_class_names(cfg.YOLO.CLASSES)) XYSCALE = cfg.YOLO.XYSCALE input_size = FLAGS.size video_path = FLAGS.video print("Video from: ", video_path ) vid = cv2.VideoCapture(video_path) if FLAGS.framework == 'tf': input_layer = tf.keras.layers.Input([input_size, input_size, 3]) if FLAGS.tiny: feature_maps = YOLOv3_tiny(input_layer, NUM_CLASS) bbox_tensors = [] for i, fm in enumerate(feature_maps): bbox_tensor = decode(fm, NUM_CLASS, i) bbox_tensors.append(bbox_tensor) model = tf.keras.Model(input_layer, bbox_tensors) utils.load_weights_tiny(model, FLAGS.weights) else: if FLAGS.model == 'yolov3': feature_maps = YOLOv3(input_layer, NUM_CLASS) bbox_tensors = [] for i, fm in enumerate(feature_maps): bbox_tensor = decode(fm, NUM_CLASS, i) bbox_tensors.append(bbox_tensor) model = tf.keras.Model(input_layer, bbox_tensors) utils.load_weights_v3(model, FLAGS.weights) elif FLAGS.model == 'yolov4': feature_maps = YOLOv4(input_layer, NUM_CLASS) bbox_tensors = [] for i, fm in enumerate(feature_maps): bbox_tensor = decode(fm, NUM_CLASS, i) bbox_tensors.append(bbox_tensor) model = tf.keras.Model(input_layer, bbox_tensors)
def __init__(self,input_data,trainable): self.nnlib = ModelSkeleton(trainable) self.trainable = trainable self.strides = np.array(cfg.COCO_STRIDES) self.anchors = utils.get_anchors(cfg.COCO_ANCHORS) self.classes = utils.read_class_names(cfg.COCO_NAMES) self.num_class = len(self.classes) self.darknet53_output = self._darknet53(input_data)
def __init__(self): self.classes = utils.read_class_names(cfg.YOLO.CLASSES) self.num_class = len(self.classes) self.strides = np.array(cfg.YOLO.STRIDES) self.anchors = utils.get_anchors(cfg.YOLO.ANCHORS) self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.iou_loss_thresh = cfg.YOLO.IOU_LOSS_THRESH self.upsample_method = cfg.YOLO.UPSAMPLE_METHOD
def on_init(): global model global anchors gpus = tf.config.experimental.list_physical_devices('GPU') if gpus: # Restrict TensorFlow to only allocate 1GB of memory on the first GPU try: tf.config.experimental.set_virtual_device_configuration( gpus[0], [ tf.config.experimental.VirtualDeviceConfiguration( memory_limit=1024) ]) logical_gpus = tf.config.experimental.list_logical_devices('GPU') sys.stdout.write( f"{len(gpus)} Physical GPUs, {len(logical_gpus)} Logical GPUs\n" ) sys.stdout.flush() except RuntimeError as e: # Virtual devices must be set before GPUs have been initialized sys.stdout.write(e) sys.stdout.flush() input_layer = tf.keras.layers.Input([input_size, input_size, 3]) feature_maps = YOLOv4(input_layer, num_classes) bbox_tensors = [] for i, fm in enumerate(feature_maps): bbox_tensor = decode(fm, num_classes, i) bbox_tensors.append(bbox_tensor) model = tf.keras.Model(input_layer, bbox_tensors) if weights.split(".")[len(weights.split(".")) - 1] == "weights": utils.load_weights(model, weights) else: model.load_weights(weights).expect_partial() if model_name == "yolov3": anchors = utils.get_anchors(ANCHOR_V3_DEFAULT) else: anchors = utils.get_anchors(ANCHOR_V4_DEFAULT) return True