Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    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])
Exemplo n.º 3
0
 def __init__(self, input_size=(416, 416), ckpt_path=None, score_threshold=0.4, num_threshold=0.45):
     self.input_size    = input_size
     self.ckpt_path    = ckpt_path
     self.score_threshold = score_threshold
     self.num_threshold = num_threshold
     self.class_name   = read_class_names(cfgs.CLASSES)
     self.num_classes  = len(self.class_name)
     self.input_data = tf.placeholder(dtype=tf.float32, name='input_data')
     self.trainable = tf.placeholder(dtype=tf.bool, name="training")
     self.detector   = YOLOV3(self.input_data, self.trainable)
Exemplo n.º 4
0
    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
Exemplo n.º 5
0
def convert_pascal_to_tfrecord(dataset_path,
                               save_path,
                               record_capacity=2000,
                               shuffling=False):
    """
    convert pascal dataset to rfrecord
    :param img_path:
    :param xml_path:
    :param save_path:
    :param record_capacity:
    :return:
    """
    index_name = read_class_names(cfgs.CLASSES)
    name_index = {}
    for index, name in index_name.items():
        name_index[name] = int(index)
    years = [s.strip() for s in FLAGS.year.split(',')]
    # record_file = os.path.join(FLAGS.save_dir, FLAGS.save_name+'.tfrecord')

    # get image and xml list
    img_name_list = []
    img_xml_list = []

    for year in years:
        img_path = os.path.join(dataset_path, 'VOC' + year, FLAGS.image_dir)
        xml_path = os.path.join(dataset_path, 'VOC' + year, FLAGS.xml_dir)
        xml_list = [
            xml_file for xml_file in glob.glob(os.path.join(xml_path, '*.xml'))
        ]
        img_list = [
            os.path.join(
                img_path,
                os.path.basename(xml).replace('xml', FLAGS.img_format))
            for xml in xml_list
        ]
        img_name_list.extend(img_list)
        img_xml_list.extend(xml_list)

    if shuffling:
        shuffled_index = list(range(len(img_name_list)))
        random.seed(0)
        random.shuffle(shuffled_index)
        img_name_shuffle = [img_name_list[index] for index in shuffled_index]
        img_xml_shuffle = [img_xml_list[index] for index in shuffled_index]
        img_name_list = img_name_shuffle
        img_xml_list = img_xml_shuffle

    remainder_num = len(img_name_list) % record_capacity
    if remainder_num == 0:
        num_record = int(len(img_name_list) / record_capacity)
    else:
        num_record = int(len(img_name_list) / record_capacity) + 1

    num_samples = 0
    for index in range(num_record):
        record_filename = os.path.join(save_path, f'{index}.record')
        write = tf.io.TFRecordWriter(record_filename)
        if index < num_record - 1:
            sub_img_list = img_name_list[index * record_capacity:(index + 1) *
                                         record_capacity]
            sub_xml_list = img_xml_list[index * record_capacity:(index + 1) *
                                        record_capacity]
        else:
            sub_img_list = img_name_list[(index * record_capacity):(
                index * record_capacity + remainder_num)]
            sub_xml_list = img_xml_list[(index * record_capacity):(
                index * record_capacity + remainder_num)]

        try:
            for img_file, xml_file in zip(sub_img_list, sub_xml_list):

                image, shape, bboxes, labels, labels_text, difficult, truncated = process_image(
                    img_file, xml_file, class_name=name_index)

                image_record = serialize_example(img_file, image, labels,
                                                 labels_text, bboxes, shape,
                                                 difficult, truncated)
                write.write(record=image_record)

                num_samples += 1
                view_bar(message='\nConversion progress',
                         num=num_samples,
                         total=len(img_name_list))
        except Exception as e:
            print(e)
            continue
        write.close()
    print('\nThere are {0} samples convert to {1}'.format(
        num_samples, save_path))
Exemplo n.º 6
0
    def __init__(self):
        self.anchor_per_scale = cfgs.ANCHOR_PER_SCALE
        self.classes = tools.read_class_names(cfgs.CLASSES)
        self.num_classes = len(self.classes)
        self.learn_rate_init = cfgs.LEARNING_RATE_INIT
        self.learn_rate_end = cfgs.LEARNING_RATE_END
        self.first_stage_epochs = cfgs.FIRST_STAGE_EPOCHS
        self.second_stage_epochs = cfgs.SECOND_STAGE_EPOCHS
        self.warmup_periods = cfgs.WARMUP_EPOCHS
        self.time = time.strftime('%Y-%m-%d-%H-%M-%S',
                                  time.localtime(time.time()))
        self.moving_ave_decay = cfgs.MOVING_AVE_DECAY
        self.max_bbox_per_scale = 150
        self.log_dir = os.path.join(cfgs.SUMMARY_PATH, cfgs.VERSION)
        self.train_dataset = Dataset(is_training=True)
        self.test_dataset = Dataset(is_training=False)
        self.steps_per_period = self.train_dataset.num_steps_per_epoches
        self.test_steps_per_period = self.train_dataset.num_steps_per_epoches
        self.train_data_batch = self.train_dataset.dataset_tfrecord(
            batch_size=cfgs.TRAIN_BATCH_SIZE, is_training=True)
        self.test_data_batch = self.test_dataset.dataset_tfrecord(
            batch_size=cfgs.TEST_BATCH_SIZE, is_training=False)

        self.sess = tf.Session(config=tf.ConfigProto(
            allow_soft_placement=True,
            gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.8,
                                      allow_growth=True)))

        with tf.name_scope('define_input'):
            self.input_data = tf.placeholder(dtype=tf.float32,
                                             name='input_data')
            self.label_sbbox = tf.placeholder(dtype=tf.float32,
                                              name='label_sbbox')
            self.label_mbbox = tf.placeholder(dtype=tf.float32,
                                              name='label_mbbox')
            self.label_lbbox = tf.placeholder(dtype=tf.float32,
                                              name='label_lbbox')
            self.true_sbboxes = tf.placeholder(dtype=tf.float32,
                                               name='sbboxes')
            self.true_mbboxes = tf.placeholder(dtype=tf.float32,
                                               name='mbboxes')
            self.true_lbboxes = tf.placeholder(dtype=tf.float32,
                                               name='lbboxes')
            self.trainable = tf.placeholder(dtype=tf.bool, name='training')

        with tf.name_scope("define_loss"):
            self.model = YOLOV3(self.input_data, self.trainable)
            # self.net_var = tf.global_variables()
            # get loader and saver
            self.loader, self.checkpoint_path = self.model.get_restorer(
                is_training=True)
            # self.global_step = tf.train.get_or_create_global_step()

            self.giou_loss, self.conf_loss, self.prob_loss = self.model.compute_loss(
                self.label_sbbox, self.label_mbbox, self.label_lbbox,
                self.true_sbboxes, self.true_mbboxes, self.true_lbboxes)
            self.loss = self.giou_loss + self.conf_loss + self.prob_loss

        with tf.name_scope('learning_rate'):
            self.global_step = tf.Variable(1.0,
                                           dtype=tf.float64,
                                           trainable=False,
                                           name='global_step')
            # # self.global_step = self.model.global_step
            # # self.global_step = tf.train.get_or_create_global_step()
            # warmup_steps = tf.constant(self.warmup_periods * self.steps_per_period,
            #                             dtype=tf.float64, name='warmup_steps')
            # train_steps = tf.constant( (self.first_stage_epochs + self.second_stage_epochs)* self.steps_per_period,
            #                             dtype=tf.float64, name='train_steps')
            # self.learn_rate = tf.cond(
            #     pred=self.global_step < warmup_steps,
            #     true_fn=lambda: self.global_step / warmup_steps * self.learn_rate_init,
            #     false_fn=lambda: self.learn_rate_end + 0.5 * (self.learn_rate_init - self.learn_rate_end) *
            #                         (1 + tf.cos(
            #                             (self.global_step - warmup_steps) / (train_steps - warmup_steps) * np.pi))
            # )
            warmup_steps = int(self.warmup_periods * self.steps_per_period)
            total_train_step = int(
                (self.first_stage_epochs + self.second_stage_epochs) *
                self.steps_per_period)
            self.learning_rate = self.model.cosine_decay_with_warmup(
                learning_rate_base=self.learn_rate_init,
                learning_rate_end=self.learn_rate_end,
                total_decay_steps=total_train_step,
                warmup_steps=warmup_steps,
                global_step=self.global_step)
            global_step_update = tf.assign_add(self.global_step, 1.0)
        with tf.name_scope("define_weight_decay"):
            moving_ave = tf.train.ExponentialMovingAverage(
                self.moving_ave_decay).apply(tf.trainable_variables())

        with tf.name_scope("define_first_stage_train"):
            self.first_stage_trainable_var_list = []
            for var in tf.trainable_variables():
                var_name = var.op.name
                var_name_mess = str(var_name).split('/')
                if var_name_mess[1] in [
                        'conv_sbbox', 'conv_mbbox', 'conv_lbbox'
                ]:
                    self.first_stage_trainable_var_list.append(var)

            first_stage_optimizer = tf.compat.v1.train.AdamOptimizer(
                self.learning_rate).minimize(
                    self.loss, var_list=self.first_stage_trainable_var_list)
            with tf.control_dependencies(
                    tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
                with tf.control_dependencies(
                    [first_stage_optimizer, global_step_update]):
                    with tf.control_dependencies([moving_ave]):
                        self.train_op_with_frozen_variables = tf.no_op()

        with tf.name_scope("define_second_stage_train"):
            second_stage_trainable_var_list = tf.trainable_variables()
            second_stage_optimizer = tf.compat.v1.train.AdamOptimizer(
                self.learning_rate).minimize(
                    self.loss, var_list=second_stage_trainable_var_list)

            with tf.control_dependencies(
                    tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
                with tf.control_dependencies(
                    [second_stage_optimizer, global_step_update]):
                    with tf.control_dependencies([moving_ave]):
                        self.train_op_with_all_variables = tf.no_op()

        with tf.name_scope('loader_and_saver'):
            # self.loader = tf.train.Saver(self.net_var)
            self.saver = tf.train.Saver(tf.global_variables(), max_to_keep=10)

        with tf.name_scope('summary'):
            tf.summary.scalar("learning_rate", self.learning_rate)
            tf.summary.scalar("giou_loss", self.giou_loss)
            tf.summary.scalar("conf_loss", self.conf_loss)
            tf.summary.scalar("prob_loss", self.prob_loss)
            tf.summary.scalar("total_loss", self.loss)

            if os.path.exists(self.log_dir): shutil.rmtree(self.log_dir)
            tools.makedir(self.log_dir)
            self.write_op = tf.summary.merge_all()
            self.summary_writer = tf.summary.FileWriter(self.log_dir,
                                                        graph=self.sess.graph)