def __init__(self): self.input_size = cfgs.TEST_INPUT_SIZE self.anchor_per_scale = cfgs.ANCHOR_PER_SCALE self.classes = tools.read_class_names(cfgs.CLASSES) self.num_classes = len(self.classes) self.anchors = np.array(tools.get_anchors(cfgs.ANCHORS)) self.score_threshold = cfgs.TEST_SCORE_THRESHOLD self.iou_threshold = cfgs.TEST_IOU_THRESHOLD self.moving_ave_decay = cfgs.MOVING_AVE_DECAY self.annotation_path = cfgs.TEST_ANNOT_PATH self.weight_file = os.path.join(cfgs.TRAINED_CKPT, cfgs.VERSION, "yolov3_loss=11.0706.ckpt-6") self.write_image = cfgs.TEST_WRITE_IMAGE self.write_image_path = cfgs.TEST_SAVE_IMAGE_PATH self.show_label = cfgs.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 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, gpu_options=tf.GPUOptions( allow_growth=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 = tools.read_class_names(cfgs.CLASSES) self.num_class = len(self.classes) self.num_class = len(self.classes) self.strides = np.array(cfgs.STRIDES) self.anchors = tools.get_anchors(cfgs.ANCHORS) self.anchor_per_scale = cfgs.ANCHOR_PER_SCALE self.iou_loss_thresh = cfgs.IOU_LOSS_THRESH self.upsample_method = cfgs.UPSAMPLE_METHOD try: with tf.variable_scope(cfgs.MODEL_NAME, default_name="yolo_v3"): 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_args(self): Log.add_log("message: begin to initial images path") # init imgs path for voc_dir in self.data_dirs: Log.add_log("message: initial the image folder: " + voc_dir) ls_names = os.listdir(voc_dir) total_imgs = len(ls_names) curr_index = 0 for img_name in ls_names: img_path = path.join(voc_dir, img_name) if not path.isfile(img_path): Log.add_log("warning:VOC image'" + str(img_path) + "'is not a file") continue if not tools.is_pic(img_path): continue label_path = img_path.replace("JPEGImages", "Annotations") label_path = label_path.replace(img_name.split('.')[-1], "xml") if not path.isfile(label_path): Log.add_log("warning:VOC label'" + str(label_path) + "'is not a file") continue self.imgs_path.append(img_path) self.labels_path.append(label_path) self.num_imgs += 1 if curr_index % 100 == 0: print("\rCurrent progress:{:03f}%".format( curr_index / total_imgs * 100), end="") curr_index += 1 print("") Log.add_log("message:initialize VOC dataset complete, there are " + str(self.num_imgs) + " pictures in all") # init steps of one epoch self.steps_per_epoch = int(np.ceil(self.num_imgs / self.batch_size)) if self.num_imgs <= 0: Log.add_log("error:there are 0 pictures to train in all") raise ValueError("there are 0 pictures to train in all") # get anchors if self.anchors is None: self.anchors = tools.get_anchors( self.labels_path, self.imgs_path, target_size=[self.size, self.size], k=9) self.anchors = np.asarray(self.anchors).astype(np.float32).reshape( [-1, 2]) / [self.size, self.size] Log.add_log("message:Data:anchors_ori:" + str(self.anchors * [self.size, self.size])) Log.add_log("message:Data:self.anchors:" + str(self.anchors)) return
def __init__(self, is_training=False): self.is_training = is_training self.record_dir = cfgs.TRAIN_RECORD_DIR if is_training else cfgs.TEST_RECORD_DIR self.input_sizes = cfgs.TRAIN_INPUT_SIZE if self.is_training else cfgs.TRAIN_INPUT_SIZE self.batch_size = cfgs.TRAIN_BATCH_SIZE if self.is_training else cfgs.TEST_BATCH_SIZE self.train_input_size = 416 self.strides = np.array(cfgs.STRIDES) self.num_classes = cfgs.NUM_CLASSES self.anchors = np.array(tools.get_anchors(cfgs.ANCHORS)) self.anchor_per_scale = cfgs.ANCHOR_PER_SCALE self.max_bbox_per_scale = 150 self.num_samples = self.get_num_samples() self.num_steps_per_epoches = int(np.ceil( self.get_num_samples()/ self.batch_size))
def __init__(self, dataset_type): self.TFRECORD_DIR = cfg.TRAIN.TFRECORD_DIR if dataset_type == 'train' else cfg.TEST.TFRECORD_DIR 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.TXT_DIR = cfg.TRAIN.TXT_DIR if dataset_type == 'train' else cfg.TEST.TXT_DIR self.strides = np.array(cfg.YOLO.STRIDES) self.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE self.data_aug = cfg.TRAIN.DATA_AUG self.output_sizes = [self.input_sizes] // self.strides self.classes = cfg.YOLO.CLASSES self.num_classes = len(self.classes) self.max_bbox_per_scale = cfg.YOLO.MAX_PER_SCALE self.anchors = np.array(tools.get_anchors(cfg.YOLO.ANCHORS)) self.num_samples = tools.get_num_annatations(self.TXT_DIR) self.num_batchs = int(np.ceil(self.num_samples / self.batch_size))
def __init__(self, is_training=True): self.is_training = is_training self.annot_path = cfgs.TRAIN_ANNOT_PATH if self.is_training else cfgs.TEST_ANNOT_PATH self.input_sizes = cfgs.TRAIN_INPUT_SIZE if self.is_training else cfgs.TRAIN_INPUT_SIZE self.batch_size = cfgs.TRAIN_BATCH_SIZE if self.is_training else cfgs.TEST_BATCH_SIZE self.train_input_sizes = cfgs.TRAIN_INPUT_SIZE self.strides = np.array(cfgs.STRIDES) self.classes = tools.read_class_names(cfgs.CLASSES) self.num_classes = len(self.classes) self.anchors = np.array(tools.get_anchors(cfgs.ANCHORS)) self.anchor_per_scale = cfgs.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
# -*- coding: utf-8 -*- import tensorflow as tf import numpy as np import utils.tools as tools from core.iou import bbox_giou_tf, bbox_iou_tf from configuration import cfg NUM_CLASS = len(cfg.YOLO.CLASSES) ANCHORS = tools.get_anchors('./data/anchors/basline_anchors.txt') STRIDES = np.array(cfg.YOLO.STRIDES) IOU_LOSS_THRESH = cfg.YOLO.IOU_LOSS_THRES def decode(conv_output, i=0): ''' decode the tensor from model :param conv_output: :param i: index of output of model :return: ''' conv_shape = tf.shape(conv_output) batch_size = conv_shape[0] output_size = conv_shape[1] conv_output = tf.reshape( conv_output, (batch_size, output_size, output_size, 3, 5 + NUM_CLASS)) conv_raw_dxdy = conv_output[:, :, :, :, 0:2] conv_raw_dwdh = conv_output[:, :, :, :, 2:4] conv_raw_conf = conv_output[:, :, :, :, 4:5]