예제 #1
0
 def load_model(self, config_file="cfg/yolov3.cfg"):
     """
     Load model from config
     :param config_file: full or relative path to config.
     :return:
     """
     self.config_params['config_file'] = config_file
     self.model = darknet.Darknet(config_file)
예제 #2
0
def camera_detect(save_path='camera_deomo.avi'):
    net = darknet.Darknet().cuda()
    print("Loading model...")
    net.module_list.load_state_dict(torch.load('yolov3.pth'))
    print("YOLO has been loaded")

    cap = cv2.VideoCapture(0)
    assert cap.isOpened(), 'Cannot open camera'
    frame_width = int(cap.get(3))
    frame_height = int(cap.get(4))
    print((frame_width, frame_height))

    out = cv2.VideoWriter('{}'.format(save_path),
                          cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 15,
                          (frame_width, frame_height))

    start = time.time()
    cnt = 0
    while cap.isOpened():
        cnt += 1
        ret, frame = cap.read()
        if ret:
            loc = net.get_all_predict(frame)
            frame = darknet.print_rectangle(frame, loc)

            out.write(frame)
            cv2.imshow("yolov3_camera_demo", frame)

            key = cv2.waitKey(1)
            if key & 0xFF == ord('q'):
                break
            continue
        else:
            break

    total_time = time.time() - start
    FPS = round(cnt / total_time, 2)
    print("the average FPS is {}".format(FPS))
예제 #3
0
def video_detect_save(videoPath, save_path='outp.avi'):
    net = darknet.Darknet().cuda()
    print("Loading model...")
    net.module_list.load_state_dict(torch.load('yolov3.pth'))
    print("YOLO has been loaded")

    cap = cv2.VideoCapture(videoPath)

    assert os.path.exists(
        videoPath
    ), "the denoted video file does not exist,please check it again."
    assert cap.isOpened(), "faile to open the denoted vido."

    frame_width = int(cap.get(3))
    frame_height = int(cap.get(4))

    fps = cap.get(cv2.CAP_PROP_FPS)
    out = cv2.VideoWriter('{}'.format(save_path),
                          cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps,
                          (frame_width, frame_height))

    while cap.isOpened():
        ret, frame = cap.read()
        if ret:
            loc = net.get_all_predict(frame)
            frame = darknet.print_rectangle(frame, loc)

            out.write(frame)
            cv2.imshow("yolo video detect", frame)

            key = cv2.waitKey(1)
            if key & 0xFF == ord('q'):
                break
            continue
        else:
            break
예제 #4
0
import numpy as np
root = Tk()
root.title('Yolov3_detect_object')
root.geometry("1500x600")

filename = ''
path_Yolo = os.getcwd()
print(path_Yolo)
# Set the location and name of the cfg file
cfg_file = path_Yolo + '\\cfg\\yolov3.cfg'
# Set the location and name of the pre-trained weights file
weight_file = path_Yolo + '\\weights\\yolov3.weights'
# Set the location and name of the COCO object classes file
namesfile = path_Yolo + '\\data\\coco.names'
# Load the network architecture
m = darknet.Darknet(cfg_file)

# Load the pre-trained weights
m.load_weights(weight_file)
# Load the COCO object classes
class_names = utils.load_class_names(namesfile)


def clear():
    """Function for clear button: Clear the picture path"""
    my_text.delete(1.0, END)


def get_text():
    """Function for detect button: Get the picture path, and start detecting object """
    i_path = my_text.get(1.0, END)
예제 #5
0
        tflogger = config.TensorBoardLogger(msglogger.logdir)
        pylogger = config.PythonLogger(msglogger)

    if args.train:
        # Create dataloaders and the table for dataset size information:
        train_labels_name = os.listdir(os.path.join(args.label_dir, 'train'))
        test_label_name = os.listdir(os.path.join(args.label_dir, 'test'))
        train_labels = []
        for i in train_labels_name:
            train_labels.append(os.path.join(args.label_dir, 'train', i))
        test_labels = os.path.join(args.label_dir, 'test', test_label_name[0])
        dataloaders, dataset_sizes = dataset.create_dataloader(
            args, args.data_dir, train_labels, test_labels)

    # Create the darknet and load weights here:
    model = darknet.Darknet(args.config_path, img_size=args.image_size)
    if args.pretrained:
        model.load_darknet_weights(args.weight_path)

    model.arch = args.arch
    model.dataset = args.dataset
    model.input_shape = (1, 3, args.image_size, args.image_size
                         )  # For channel first.
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    print("Leverage {} device to run this task.".format(device))
    if args.cpu:
        device = torch.device("cpu")
    model.to(device)

    optimizer = None
    compress_scheduler = None
def main():
    hvd.init()
    gpus = tf.config.experimental.list_physical_devices('GPU')
    for gpu in gpus:
        tf.config.experimental.set_memory_growth(gpu, True)
    if gpus:
        tf.config.experimental.set_visible_devices(gpus[hvd.local_rank()],
                                                   'GPU')
    tf.config.threading.intra_op_parallelism_threads = 1  # Avoid pool of Eigen threads
    tf.config.threading.inter_op_parallelism_threads = max(
        2, 40 // hvd.size() - 2)
    os.environ['TF_CUDNN_DETERMINISTIC'] = '1'

    cmdline = add_cli_args()
    FLAGS, unknown_args = cmdline.parse_known_args()
    if not FLAGS.xla_off:
        tf.config.optimizer.set_jit(True)
    if not FLAGS.fp32:
        tf.config.optimizer.set_experimental_options(
            {"auto_mixed_precision": True})

    data = create_dataset(FLAGS.train_data_dir,
                          FLAGS.batch_size,
                          validation=False)
    validation_data = create_dataset(FLAGS.validation_data_dir,
                                     FLAGS.batch_size,
                                     validation=True)

    if FLAGS.model == 'resnet50':
        if not FLAGS.fine_tune:
            model = resnet.ResNet50(weights=None,
                                    weight_decay=FLAGS.l2_weight_decay,
                                    pooling='avg',
                                    classes=FLAGS.num_classes)
        else:
            model = resnet.ResNet50(weights='imagenet',
                                    classes=FLAGS.num_classes)
    elif FLAGS.model == 'resnet50v2':
        if not FLAGS.fine_tune:
            model = resnet.ResNet50V2(weights=None,
                                      weight_decay=FLAGS.l2_weight_decay,
                                      pooling='avg',
                                      classes=FLAGS.num_classes)
        else:
            model = resnet.ResNet50V2(weights='imagenet',
                                      classes=FLAGS.num_classes)
    elif FLAGS.model == 'resnet50v2_evo':
        if not FLAGS.fine_tune:
            model = resnet_evo.ResNet50V2(weights=None,
                                          weight_decay=FLAGS.l2_weight_decay,
                                          pooling='avg',
                                          classes=FLAGS.num_classes)
    elif FLAGS.model == 'darknet53':
        model = darknet.Darknet(weight_decay=FLAGS.l2_weight_decay)
    model.summary()
    learning_rate = (FLAGS.learning_rate * hvd.size() * FLAGS.batch_size) / 256
    steps_per_epoch = int(
        (FLAGS.train_dataset_size / (FLAGS.batch_size * hvd.size())))

    scheduler = tf.keras.optimizers.schedules.PiecewiseConstantDecay(
        boundaries=[
            steps_per_epoch * 25, steps_per_epoch * 55, steps_per_epoch * 75
        ],  # 5 epochs for warmup
        values=[
            learning_rate, learning_rate * 0.1, learning_rate * 0.01,
            learning_rate * 0.001
        ])

    scheduler = WarmupScheduler(optimizer=scheduler,
                                initial_learning_rate=learning_rate /
                                hvd.size(),
                                warmup_steps=steps_per_epoch * 5)
    opt = tf.keras.optimizers.SGD(
        learning_rate=scheduler, momentum=FLAGS.momentum,
        nesterov=True)  # needs momentum correction term

    if not FLAGS.fp32:
        opt = tf.train.experimental.enable_mixed_precision_graph_rewrite(
            opt, loss_scale=128.)

    loss_func = tf.keras.losses.CategoricalCrossentropy(
        label_smoothing=FLAGS.label_smoothing,
        reduction=tf.keras.losses.Reduction.SUM_OVER_BATCH_SIZE)

    if hvd.rank() == 0:
        model_dir = os.path.join(
            FLAGS.model +
            datetime.datetime.now().strftime("_%Y-%m-%d_%H-%M-%S"))
        path_logs = os.path.join(os.getcwd(), model_dir, 'log.csv')
        os.mkdir(model_dir)
        logging.basicConfig(
            filename=path_logs,
            filemode='a',
            format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
            datefmt='%H:%M:%S',
            level=logging.DEBUG)
        logging.info("Training Logs")
        logger = logging.getLogger('logger')
        logger.info('Batch Size: %f, Learning Rate: %f, Momentum: %f' % \
                    (FLAGS.batch_size, FLAGS.learning_rate, FLAGS.momentum))

    hvd.allreduce(tf.constant(0))

    start_time = time()
    curr_step = tf.Variable(initial_value=0, dtype=tf.int32)
    best_validation_accuracy = 0.0
    for epoch in range(FLAGS.num_epochs):
        if hvd.rank() == 0:
            print('Starting training Epoch %d/%d' % (epoch, FLAGS.num_epochs))
        training_score = 0
        for batch, (images, labels) in enumerate(tqdm(data)):
            # momentum correction (V2 SGD absorbs LR into the update term)
            prev_lr = opt._optimizer.learning_rate(curr_step - 1)
            curr_lr = opt._optimizer.learning_rate(curr_step)
            momentum_correction_factor = curr_lr / prev_lr
            opt._optimizer.momentum = opt._optimizer.momentum * momentum_correction_factor
            loss, score = train_step(model,
                                     opt,
                                     loss_func,
                                     images,
                                     labels,
                                     batch == 0 and epoch == 0,
                                     fp32=FLAGS.fp32)
            # restore momentum
            opt._optimizer.momentum = FLAGS.momentum
            training_score += score.numpy()
            curr_step.assign_add(1)
        training_accuracy = training_score / (FLAGS.batch_size * (batch + 1))
        average_training_accuracy = hvd.allreduce(
            tf.constant(training_accuracy))
        average_training_loss = hvd.allreduce(tf.constant(loss))

        if hvd.rank() == 0:
            print('Starting validation Epoch %d/%d' %
                  (epoch, FLAGS.num_epochs))
        validation_score = 0
        counter = 0
        for images, labels in tqdm(validation_data):
            loss, score = validation_step(images, labels, model, loss_func)
            validation_score += score.numpy()
            counter += 1
        validation_accuracy = validation_score / (FLAGS.batch_size * counter)
        average_validation_accuracy = hvd.allreduce(
            tf.constant(validation_accuracy))
        average_validation_loss = hvd.allreduce(tf.constant(loss))

        if hvd.rank() == 0:
            info_str = 'Epoch: %d, Train Accuracy: %f, Train Loss: %f, Validation Accuracy: %f, Validation Loss: %f LR:%f' % \
                    (epoch, average_training_accuracy, average_training_loss, average_validation_accuracy, average_validation_loss, scheduler(curr_step))
            print(info_str)
            logger.info(info_str)
            if average_validation_accuracy > best_validation_accuracy:
                logger.info("Found new best accuracy, saving checkpoint ...")
                best_validation_accuracy = average_validation_accuracy
                model.save('{}-best/{}'.format(FLAGS.model_dir, FLAGS.model))
    if hvd.rank() == 0:
        logger.info('Total Training Time: %f' % (time() - start_time))
예제 #7
0
import torch
import torch.nn as nn
import cv2
import darknet
from ipdb import set_trace

if __name__ == '__main__':
    net = darknet.Darknet().cuda()
    net.eval()

    print("Loading model...")
    net.module_list.load_state_dict(torch.load('yolov3.pth'))
    print("YOLO has been loaded")

    img = cv2.imread("imgs/1.jpg")
    loc = net.get_all_predict(img)

    img = darknet.print_rectangle(img, loc)
    save_path = 'demo.jpg'

    cv2.imwrite(save_path, img)
    print("save at {}".format(save_path))

    #cv2.namedWindow("detection",0)
    #cv2.resizeWindow("detection", 1440, 900);

    cv2.imshow("detection", img)
    cv2.waitKey(0)
    exit(0)
从这里下载 coco.names 文件:https://raw.githubusercontent.com/ayooshkathuria/YOLO_v3_tutorial_from_scratch/master/data/coco.names。这个文件包含了 COCO 数据集中目标的名称。在你的检测器目录中创建一个文件夹 data。如果你使用的 Linux,你可以使用以下命令实现:
mkdir data
cd data
wget https://raw.githubusercontent.com/ayooshkathuria/YOLO_v3_tutorial_from_scratch/master/data/coco.names
"""

# 将类别文件载入到我们的程序中。
num_classes = 80
# load_classes 是在 util.py 中定义的一个函数,其会返回一个字典——将每个类别的索引映射到其名称的字符串。
classes = util.load_classes("data/coco.names")

# Set up the neural network
# 初始化网络并载入权重。
print("Loading network.....")
# darknet.Darknet类中初始化时得到了网络结构和网络的参数信息,保存在net_info,module_list中
model = darknet.Darknet(args.cfgfile)
# 将权重文件载入,并复制给对应的网络结构model中
model.load_weights(args.weightsfile)
print("Network successfully loaded")

# 网络输入数据大小
# model类中net_info是一个字典。
# ’height’是图片的宽高,因为图片缩放到416x416,所以宽高一样大
model.net_info["height"] = args.reso
#inp_dim是网络输入图片尺寸(如416*416)
inp_dim = np.int(model.net_info["height"])
# 如果设定的输入图片的尺寸不是32的位数或者不大于32,抛出异常
assert inp_dim % 32 == 0
assert inp_dim > 32

# If there's a GPU availible, put the model on GPU