コード例 #1
0
def predict():

    input_size = (416, 416)
    tf.app.flags.DEFINE_string('image_file', './images/person.jpg', 'image_path')
    FLAGS = tf.app.flags.FLAGS
    image_file = FLAGS.image_file
    image = cv2.imread(image_file)
    image_shape = image.shape[:2]
    image_cp = preprocess_image(image, input_size)

    images = tf.placeholder(tf.float32, [1, input_size[0], input_size[1], 3])
    detection_feat = darknet(images)
    feat_sizes = input_size[0] // 32, input_size[1] // 32
    detection_results = decode(detection_feat, feat_sizes, len(class_names), anchors)

    checkpoint_path = "./checkpoint_dir/yolo2_coco.ckpt"
    #checkpoint_path = "/Users/xiang/Downloads/DeepLearning_tutorials-master/ObjectDetections/yolo2/checkpoint_dir/yolo2_coco.ckpt"
    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, checkpoint_path)
        bboxes, obj_probs, class_probs = sess.run(detection_results, feed_dict={images: image_cp})

    bboxes, scores, class_inds = postprocess(bboxes, obj_probs, class_probs,
                                             image_shape=image_shape)
    img_detection = draw_detection(image, bboxes, scores, class_inds, class_names)
    cv2.imwrite("./res/detection.jpg", img_detection)
    cv2.imshow("detection results", img_detection)
    print('*****Click the window,and press any key to close!*****')

    cv2.waitKey(0)
    cv2.destroyAllWindows()
コード例 #2
0
def main():
    input_size = (416, 416)
    # image_file = "/home/zdq/darknet/data/1.jpg"
    # image = cv2.imread(image_file)
    image_shape = image.shape[:2]
    image_cp = preprocess_image(image, input_size)

    images = tf.placeholder(tf.float32, [1, input_size[0], input_size[1], 3])
    detection_feat = darknet(images)
    feat_sizes = input_size[0] // 32, input_size[1] // 32
    detection_results = decode(detection_feat, feat_sizes, len(class_names),
                               anchors)

    checkpoint_path = "/home/zdq/YOLO/checkpoint_dir/yolo2_coco.ckpt"
    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, checkpoint_path)
        bboxes, obj_probs, class_probs = sess.run(detection_results,
                                                  feed_dict={images: image_cp})

    bboxes, scores, class_inds = postprocess(bboxes,
                                             obj_probs,
                                             class_probs,
                                             image_shape=image_shape)
    img_detection = draw_detection(image, bboxes, scores, class_inds,
                                   class_names)  #回归框,得到矩阵框的X左右,Y下像素坐标
    print('\n')

    # #检测车道线的像素,并放入字典中
    # lane_cor = {}
    # #手动选取ROI,待修改
    # vertices = np.array([[(110, 194), (110, 0), (150, 0), (150, 194)]], dtype=np.int32)
    # roi = region_of_interest(line_img, vertices)
    # # cv2.imshow("roi", roi)
    # for i in range(0, (roi.shape)[0]):
    #     for j in range(0, (roi.shape)[1]):
    #         if roi[i, j, 2] == 255:         #roi[i,j,num]这里num代表着BGR第几通道
    #             lane_cor[i] = j
    # print("The coodinate of the detected_lane y:x")
    #
    # print(lane_cor)
    #
    # global box
    # if (utils.box[0] + m * (utils.box[2] - utils.box[0])) <= lane_cor[utils.box[3]] <= (utils.box[2] - m * (utils.box[2] - utils.box[0])):
    #     print("The car is on the solid line!!!")
    # else:
    #     print("The car is permitted~")

    # mix1 = weight_add(img_detection, line_img, alpha=0.7, belta=1, gamma=0.)
    # mixed = weight_add(img_detection,roi , alpha=0.7, belta=1, gamma=0.)
    # cv2.imshow("mix1", mix1)
    # cv2.imshow("mixed",mixed)
    #
    cv2.imshow("detection results", img_detection)
    cv2.imwrite("/home/zdq/PycharmProjects/YOLOv2/detection.jpg",
                img_detection)
    cv2.waitKey(0)
    return img_detection
コード例 #3
0
def camera_detect():

    tf.app.flags.DEFINE_string('video', False, 'Whether to output video file')
    FLAGS = tf.app.flags.FLAGS

    input_size = (416, 416)

    cv2.namedWindow("camera")
    capture = cv2.VideoCapture(0)            #开启摄像头
    success, image = capture.read()

    images = tf.placeholder(tf.float32, [1, input_size[0], input_size[1], 3])
    detection_feat = darknet(images)

    if FLAGS.video:
        fps = 1
        size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)),
                int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
        videoWriter = cv2.VideoWriter('./test/result.avi',
        cv2.VideoWriter_fourcc('M','J', 'P', 'G'), fps, size)

    num = 1

    while success and cv2.waitKey(1) == -1:
        cv2.imshow('camera', image)
        image_shape = image.shape[:2]
        image_cp = preprocess_image(image, input_size)
        
        feat_sizes = input_size[0] // 32, input_size[1] // 32
        detection_results = decode(detection_feat, feat_sizes, len(class_names), anchors)

        checkpoint_path = "./checkpoint_dir/yolo2_coco.ckpt"
        #checkpoint_path = "/Users/xiang/Downloads/DeepLearning_tutorials-master/ObjectDetections/yolo2/checkpoint_dir/yolo2_coco.ckpt"
        saver = tf.train.Saver()
        with tf.Session() as sess:
            saver.restore(sess, checkpoint_path)
            bboxes, obj_probs, class_probs = sess.run(detection_results, feed_dict={images: image_cp})

        bboxes, scores, class_inds = postprocess(bboxes, obj_probs, class_probs,
                                                 image_shape=image_shape)
        img_detection = draw_detection(image, bboxes, scores, class_inds, class_names)
        
        if FLAGS.video:
            videoWriter.write(img_detection)
        else:
            cv2.imwrite("./test/"+str(num)+"test.jpg", img_detection)

        success, image = capture.read()

        num += 1

    cv2.destroyWindow("camera")
    capture.release()
コード例 #4
0
def creat_model(classes,
                load_pretrained=True,
                freeze_body=2,
                weights_path='imagenet_models/darknet53.h5'):

    K.clear_session()
    image_input = Input(shape=(None, None, 3))
    darknet53 = darknet_body(image_input)

    if load_pretrained:
        darknet53.load_weights(weights_path, by_name=True, skip_mismatch=True)
    print('load_weights succeed!!!')

    darknet_model = darknet(darknet53, classes)
    model = Model(image_input, darknet_model.output)

    return model
コード例 #5
0
tf.reset_default_graph()  #重设图
input_size = (416, 416)
image_file = "car.jpg"
image = cv2.imread(image_file)
image_shape = image.shape[:2]
image_cp = preprocess_image(image, input_size)
"""
image = Image.open(image_file)
image_cp = image.resize(input_size, Image.BICUBIC)
image_cp = np.array(image_cp, dtype=np.float32)/255.0
image_cp = np.expand_dims(image_cp, 0)
#print(image_cp)
"""

images = tf.placeholder(tf.float32, [1, input_size[0], input_size[1], 3])
detection_feat = darknet(images)
feat_sizes = input_size[0] // 32, input_size[1] // 32
start = time.clock()
detection_results = decode(detection_feat, feat_sizes, len(class_names),
                           anchors)

checkpoint_path = "./checkpoint_dir/yolo2_coco.ckpt"
saver = tf.train.Saver()
with tf.Session() as sess:
    saver.restore(sess, checkpoint_path)
    bboxes, obj_probs, class_probs = sess.run(detection_results,
                                              feed_dict={images: image_cp})

bboxes, scores, class_inds = postprocess(bboxes,
                                         obj_probs,
                                         class_probs,
コード例 #6
0
def get_flops(cfg,img_h,img_w):
    flops = 0
    in_channles=3
    for l in cfg:
        if l != 'M':
            flops += in_channles*l[0]*l[1]*l[1]*img_h*img_w
            in_channles = l[0]
        else:
            img_h=img_h/2
            img_w=img_w/2
    return flops

loss = np.random.random(7200)
loss_rank = np.argsort(loss)
cfg=[[32,3],'M',[64,3],'M',[128,3],[64,1],[128,3],'M',[256,3],[128,1],[256,3],'M',[512,3],[256,1],[512,3],[256,1],[512,3],'M',[1024,3],[512,1],[1024,3],[512,1],[1024,3]]
origin_flops=get_flops(cfg,224,224)
pruned_model_cfg,pruned_layer_cfg = generate_pruned_cfg(loss_rank, cfg, int(7200*args.percent))
pruned_flops=get_flops(pruned_model_cfg,224,224)
print pruned_model_cfg

model=torch.load(args.model)
print model

pruned_model = darknet(cfg=pruned_model_cfg)
pruned_model.cuda()
print pruned_model
pruned_model_init(model, pruned_model, pruned_layer_cfg)
print 'origin model flops: %d, pruned_model flops: %d'%(origin_flops,pruned_flops)
torch.save(pruned_model, './models/prune/prune_%.2f_darknet19.pkl'%args.percent)
print('pruned model saved at models/prune')
コード例 #7
0
# image = cv2.imread(image_file)
image_shape = image.shape[:2]  #只能取wh,channel=3不取

# copy,resize 416*416,归一化,在第0维增加存放batchsize维度
image_cp = preprocess_image(image, input_size)
"""
image = Image.open(image_file)
image_cp = image.resize(input_size, Image.BICUBIC)
image_cp = np.array(image_cp, dtype=np.float32)/255.0
image_cp = np.expand_dims(image_cp, 0)
#print(image_cp)
"""

# (1)输入图片进入darknet19网络得到特征图,并进行解码得到:xmin xmax表示的边界框,置信度,类别概率
images = tf.placeholder(tf.float32, [1, input_size[0], input_size[1], 3])
detection_feat = darknet(images)  #darknet网络输出的特征图
feat_sizes = input_size[0] // 32, input_size[1] // 32  #特征图尺寸是图像下采样32倍
detection_results = decode(detection_feat, feat_sizes, len(class_names),
                           anchors)  #解码

checkpoint_path = "/home/zdq/YOLO/checkpoint_dir/yolo2_coco.ckpt"
saver = tf.train.Saver()
with tf.Session() as sess:
    saver.restore(sess, checkpoint_path)
    bboxes, obj_probs, class_probs = sess.run(detection_results,
                                              feed_dict={images: image_cp})

# (2)筛选解码后回归边界框——NMS(post process后期处理)
bboxes, scores, class_inds = postprocess(bboxes,
                                         obj_probs,
                                         class_probs,
コード例 #8
0
ファイル: processor.py プロジェクト: jalywang123/tl-YOLOv2
 def __init__(self, model_path, img_size=(416, 416)):
     self.model_path = model_path
     self.img_size = img_size
     self.darknet = darknet(self.model_path)
コード例 #9
0
ファイル: demo.py プロジェクト: kaka7/DeepLearning_tutorials
input_size = (416, 416)
image_file = "./images/car.jpg"
image = cv2.imread(image_file)
image_shape = image.shape[:2]
image_cp = preprocess_image(image, input_size)
"""
image = Image.open(image_file)
image_cp = image.resize(input_size, Image.BICUBIC)
image_cp = np.array(image_cp, dtype=np.float32)/255.0
image_cp = np.expand_dims(image_cp, 0)
#print(image_cp)
"""


images = tf.placeholder(tf.float32, [1, input_size[0], input_size[1], 3])
detection_feat = darknet(images)
feat_sizes = input_size[0] // 32, input_size[1] // 32
detection_results = decode(detection_feat, feat_sizes, len(class_names), anchors)

checkpoint_path = "./checkpoint_dir/yolo2_coco.ckpt"
saver = tf.train.Saver()
with tf.Session() as sess:
    saver.restore(sess, checkpoint_path)
    bboxes, obj_probs, class_probs = sess.run(detection_results, feed_dict={images: image_cp})

bboxes, scores, class_inds = postprocess(bboxes, obj_probs, class_probs,
                                         image_shape=image_shape)
img_detection = draw_detection(image, bboxes, scores, class_inds, class_names)
cv2.imwrite("detection.jpg", img_detection)
cv2.imshow("detection results", img_detection)
コード例 #10
0
])

train_dataset = torchvision.datasets.ImageFolder(
    root='/media/lulugay/PC/CCCV-30/train_set', transform=train_data_transform)
train_loader = torch.utils.data.DataLoader(train_dataset,
                                           batch_size=args.batch_size,
                                           shuffle=True,
                                           num_workers=12)
test_dataset = torchvision.datasets.ImageFolder(
    root='/media/lulugay/PC/CCCV-30/test_set', transform=test_data_transform)
test_loader = torch.utils.data.DataLoader(test_dataset,
                                          batch_size=args.test_batch_size,
                                          shuffle=True,
                                          num_workers=12)

model = darknet()
model.cuda()

start_epoch = args.start_epoch
total_epoch = args.epochs

if start_epoch != 0:
    model.load_state_dict(torch.load(args.resume))

history_score = np.zeros((total_epoch + 1, 4))

for epoch in range(start_epoch, total_epoch):
    start = time.time()
    print('epoch%d...' % epoch)
    lr = poly(args.lr, 4, total_epoch, epoch)
    optimizer = torch.optim.SGD(model.parameters(), lr, momentum=0.9)