def val(valloader, yolo): """Validation wrapper Args valloader: (Dataloader) validation data loader yolo: (nn.Module) YOLOv3 model """ class_names = config.datasets[args.dataset]['class_names'] tbar = tqdm(valloader, ncols=80, ascii=True) tbar.set_description("evaluation") for batch_idx, (names, inputs, targets) in enumerate(tbar): inputs = inputs.cuda() detections = yolo(inputs) for idx, name in enumerate(names): img_path = opj(config.datasets[args.dataset]['val_imgs'], name) img_name = img_path.split('/')[-1] try: detection = detections[detections[:, 0] == 0] except Exception: detection = torch.Tensor([]) save_path = opj(config.evaluate['result_dir'], img_name) draw_detection(img_path, detection, yolo.reso, class_names, save_path)
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()
def main(): input_size = (416,416) image_file = './yolo2_data/car.jpg' image = cv2.imread(image_file) image_shape = image.shape[:2] #只取wh,channel=3不取 # copy、resize416*416、归一化、在第0维增加存放batchsize维度 image_cp = preprocess_image(image,input_size) # 【1】输入图片进入darknet19网络得到特征图,并进行解码得到:xmin xmax表示的边界框、置信度、类别概率 tf_image = tf.placeholder(tf.float32,[1,input_size[0],input_size[1],3]) model_output = darknet_slim(tf_image) # darknet19网络输出的特征图 output_sizes = input_size[0]//32, input_size[1]//32 # 特征图尺寸是图片下采样32倍 output_decoded = decode(model_output=model_output,output_sizes=output_sizes, num_class=len(class_names),anchors=anchors) # 解码 model_path = "models/model.ckpt" init_fn = slim.assign_from_checkpoint_fn(model_path,slim.get_variables()) with tf.Session() as sess: init_fn(sess) bboxes,obj_probs,class_probs = sess.run(output_decoded,feed_dict={tf_image:image_cp}) # 【2】筛选解码后的回归边界框——NMS(post process后期处理) bboxes,scores,class_max_index = postprocess(bboxes,obj_probs,class_probs,image_shape=image_shape) # 【3】绘制筛选后的边界框 img_detection = draw_detection(image, bboxes, scores, class_max_index, class_names) cv2.imwrite("./yolo2_data/detection.jpg", img_detection) print('YOLO_v2 detection has done!') cv2.imshow("detection_results", img_detection) cv2.waitKey(0)
def val(valloader, yolo, save_img=True): """Validation wrapper @Args valloader: (Dataloader) validation data loader yolo: (nn.Module) YOLOv3 model save_img: (bool) whether to save images during validation """ mAPs = [] tbar = tqdm(valloader, ncols=80) for batch_idx, (names, inputs, targets) in enumerate(tbar): inputs = inputs.cuda() detections = yolo(inputs) mAP_batch = mAP(detections, targets, args.reso) mAPs += mAP_batch tbar.set_description("mAP=%.2f" % (np.mean(mAPs) * 100)) if save_img == True and batch_idx % 4 == 0: img_path = opj(config.datasets[args.dataset]['val_imgs'], names[0]) img_name = img_path.split('/')[-1] try: detection = detections[detections[:, 0] == 0] except Exception: img = Image.open(img_path) else: img = draw_detection(img_path, detection, yolo.reso, type='pred') img.save(opj(config.evaluate['result_dir'], img_name)) return mAPs
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
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()
def main(): anchors = cfg.ANCHORS class_names = cfg.COCONAME input_size = (416,416) image_file = './yolo2_data/train.jpg' tf_image = tf.placeholder(tf.float32,[1,input_size[0],input_size[1],3]) net = YOLONet(False) output_sizes = input_size[0]//32, input_size[1]//32 # 特征图尺寸是图片下采样32倍 output_decoded = decode(model_output=net.logits,output_sizes=output_sizes, num_class=len(class_names),anchors=anchors) # 解码 model_path = "./model.ckpt" saver = tf.train.Saver() cap = cv2.VideoCapture('./video.mp4') with tf.Session() as sess: sess.run(tf.global_variables_initializer()) #saver.restore(sess,model_path) #bboxes,obj_probs,class_probs = sess.run(output_decoded,feed_dict={tf_image:image_cp}) ret, _ = cap.read() while ret: ret, frame = cap.read() image = frame image_shape = image.shape[:2] #只取wh,channel=3不取 # copy、resize416*416、归一化、在第0维增加存放batchsize维度 image_cp = preprocess_image(image,input_size) bboxes,obj_probs,class_probs = sess.run(output_decoded,feed_dict={net.images: image_cp}) #img1 = sess.run(net.img1,feed_dict={net.images: image_cp}) bboxes,scores,class_max_index = postprocess(bboxes,obj_probs,class_probs,image_shape=image_shape) print(scores) print(class_max_index) img_detection = draw_detection(image, bboxes, scores, class_max_index, class_names) #cv2.imwrite("./yolo2_data/detection.jpg", img_detection) #print('YOLO_v2 detection has done!') cv2.imshow("detection_results", img_detection) cv2.waitKey(10) ret, frame = cap.read()
def main(): s = time.time() input_size = (416, 416) image_file = '/Users/aaron/Study/机器学习/深度学习/YOLOv3/darknet/data/dog.jpg' image = cv2.imread(image_file) image_shape = image.shape[:2] # 只取wh,channel=3不取 # copy、resize416*416、归一化、在第0维增加存放batchsize维度 image_cp = preprocess_image(image, input_size) # 【1】输入图片进入darknet19网络得到特征图,并进行解码得到:xmin xmax表示的边界框、置信度、类别概率 tf_image = tf.placeholder(tf.float32, [1, input_size[0], input_size[1], 3]) model_output = darknet(tf_image) # darknet19网络输出的特征图 output_sizes = input_size[0] // 32, input_size[1] // 32 # 特征图尺寸是图片下采样32倍 output_decoded = decode(model_output=model_output, output_sizes=output_sizes, num_class=len(class_names), anchors=anchors) # 解码 model_path = "./yolo2_model/yolo2_coco.ckpt" saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, model_path) bboxes, obj_probs, class_probs = sess.run( output_decoded, feed_dict={tf_image: image_cp}) # 【2】筛选解码后的回归边界框——NMS(post process后期处理) bboxes, scores, class_max_index = postprocess(bboxes, obj_probs, class_probs, image_shape=image_shape) # 【3】绘制筛选后的边界框 img_detection = draw_detection(image, bboxes, scores, class_max_index, class_names) cv2.imwrite("./yolo2_data/detection.jpg", img_detection) print('YOLO_v2 detection has done!') e = time.time() print(e - s) cv2.imshow("detection_results", img_detection) cv2.waitKey(0)
def main(): anchors = cfg.ANCHORS class_names = cfg.COCONAME input_size = (416, 416) image_file = './yolo2_data/car.jpg' image = cv2.imread(image_file) image_shape = image.shape[:2] tf_image = tf.placeholder(tf.float32, [1, input_size[0], input_size[1], 3]) net = YOLONet(False) output_sizes = input_size[0] // 32, input_size[1] // 32 output_decoded = decode(model_output=net.logits, output_sizes=output_sizes, num_class=len(class_names), anchors=anchors) image_cp = preprocess_image(image, input_size) model_path = "./model.ckpt" saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, model_path) #bboxes,obj_probs,class_probs = sess.run(output_decoded,feed_dict={tf_image:image_cp}) bboxes, obj_probs, class_probs = sess.run( output_decoded, feed_dict={net.images: image_cp}) #img1= sess.run([net.img1],feed_dict={net.images: image_cp}) #print(img1) #print(img2) #print(obj_probs) #print(sess.run(tf.reduce_max(obj_probs),feed_dict={net.images: image_cp})) bboxes, scores, class_max_index = postprocess(bboxes, obj_probs, class_probs, image_shape=image_shape) print(scores) print(class_max_index) img_detection = draw_detection(image, bboxes, scores, class_max_index, class_names) #cv2.imwrite("./yolo2_data/detection.jpg", img_detection) #print('YOLO_v2 detection has done!') cv2.imshow("detection_results", img_detection) cv2.waitKey(-1)
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, image_shape=image_shape) #plt_bboxes(image,class_inds,scores,bboxes) img_detection = draw_detection(image, bboxes, scores, class_inds, class_names) end = time.clock() cv2.imwrite("detection.jpg", img_detection) cv2.startWindowThread() cv2.imshow("detection results", img_detection) print(end - start) cv2.waitKey(0) # waitkey代表读取键盘的输入,括号里的数字代表等待多长时间,单位ms。 0代表一直等待
""" 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) cv2.waitKey(0)
image_cp = preprocess_image(image) #图像预处理,resize image, normalization归一化, 增加一个在第0的维度--batch_size tf_image = tf.placeholder(tf.float32,[1,input_size[0],input_size[1],3]) #定义placeholder model_output = darknet(tf_image) #网络的输出 output_sizes = input_size[0]//32, input_size[1]//32 # 特征图尺寸是图片下采样32倍 #这个函数返回框的坐标(左上角-右下角),目标置信度,类别置信度 output_decoded = decode(model_output=model_output,output_sizes=output_sizes, num_class=len(class_names),anchors=anchors) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) #初始化tensorflow全局变量 saver = tf.train.Saver() saver.restore(sess, model_path) #把模型加载到当前session中 bboxes, obj_probs, class_probs = sess.run(output_decoded, feed_dict={tf_image: image_cp}) #这个函数返回框的坐标,目标置信度,类别置信度 bboxes,scores,class_max_index = postprocess(bboxes,obj_probs,class_probs,image_shape=image_shape) #得到候选框之后的处理,先留下阈值大于0.5的框,然后再放入非极大值抑制中去 colors = generate_colors(class_names) img_detection = draw_detection(image, bboxes, scores, class_max_index, class_names, colors) #得到图片 cv2.imwrite(image_detection, img_detection) cv2.imshow("detection_results", img_detection) #显示图片 cv2.waitKey(0) #等待按任意键退出
print(colored("\n==>", 'blue'), emojify("Prepare data ... :coffee:\n")) img_datasets, dataloader = prepare_demo_dataset(config.demo['images_dir'], args.reso) print("Number of demo images:", len(img_datasets)) print(colored("\n==>", 'blue'), emojify("Loading network ... :hourglass:\n")) yolo = YOLOv3(cfg, args.reso).cuda() start_epoch, start_iteration = args.checkpoint.split('.') start_epoch, start_iteration, state_dict = load_checkpoint( opj(config.CKPT_ROOT, args.dataset), int(start_epoch), int(start_iteration) ) yolo.load_state_dict(state_dict) print("Model starts training from epoch %d iteration %d" % (start_epoch, start_iteration)) print(colored("\n==>", 'blue'), emojify("Evaluation ...\n")) yolo.eval() for batch_idx, (img_path, inputs) in enumerate(tqdm(dataloader, ncols=80)): inputs = inputs.cuda() detections = yolo(inputs) # take idx 0 detections = detections[detections[:, 0] == 0] path = img_path[0] img_name = path.split('/')[-1] img = draw_detection(path, detections.data, args.reso, type='pred') img.save(opj(config.demo['result_dir'], img_name)) print(emojify("Done! :+1:\n"))