鱼C论坛

 找回密码
 立即注册
查看: 1413|回复: 2

[已解决]YOLOV1 from scratch using Keras Tensorflow 2.0

[复制链接]
匿名鱼油
匿名鱼油  发表于 2023-8-28 20:29:09 |阅读模式
60鱼币
这个网址的训练程序我都明白了,有没有大佬写一个test(predict)程序,达到可视化检测的效果呢,救救孩子吧开学作业交不上了
https://www.maskaravivek.com/post/yolov1/
就是输入dog.jpg 显示下面图像的程序,用的上面网页训练函数的权重
最佳答案
2023-8-28 20:29:10
YOLOv1的目标检测可视化程序

以下是一个使用预训练权重进行目标检测并可视化结果的示例程序。请确保已经安装了必要的依赖项,例如Keras和OpenCV。
  1. import numpy as np
  2. import cv2
  3. from keras.models import Model
  4. from keras.layers import Input
  5. from yolo_v1 import YOLO
  6. # 定义类别标签
  7. classes = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow',
  8.            'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
  9. def load_image(image_path):
  10.     # 加载图像并进行预处理(根据训练时的预处理方式进行)
  11.     image = cv2.imread(image_path)
  12.     image = cv2.resize(image, (448, 448))
  13.     image = image / 255.0
  14.     image = np.expand_dims(image, axis=0)
  15.     return image
  16. def draw_boxes(image, boxes, scores):
  17.     # 在图像上绘制检测框和类别名称
  18.     for box, score in zip(boxes, scores):
  19.         x, y, w, h = box
  20.         class_index = np.argmax(score)
  21.         class_name = classes[class_index]
  22.         confidence = score[class_index]
  23.         label = f"{class_name}: {confidence:.2f}"
  24.         cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
  25.         cv2.putText(image, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
  26.     return image
  27. # 构建模型
  28. input_image = Input(shape=(448, 448, 3))
  29. yolo_model = YOLO(input_image, training=False)
  30. model = Model(inputs=input_image, outputs=yolo_model.output)
  31. # 加载预训练权重
  32. model.load_weights('path_to_pretrained_weights')
  33. # 进行目标检测和可视化
  34. image_path = 'dog.jpg'
  35. image = load_image(image_path)
  36. output = model.predict(image)
  37. # 解析输出
  38. boxes = output[..., :4]
  39. scores = output[..., 4:]
  40. # 设置置信度阈值,过滤掉置信度较低的检测结果
  41. confidence_threshold = 0.5
  42. filtered_boxes = []
  43. filtered_scores = []
  44. for box, score in zip(boxes[0], scores[0]):
  45.     max_score = np.max(score)
  46.     if max_score > confidence_threshold:
  47.         filtered_boxes.append(box)
  48.         filtered_scores.append(score)
  49. # 绘制检测结果并显示图像
  50. result_image = draw_boxes(cv2.imread(image_path), filtered_boxes, filtered_scores)
  51. cv2.imshow("Detection Result", result_image)
  52. cv2.waitKey(0)
  53. cv2.destroyAllWindows()
复制代码

请将代码中的以下部分进行相应的修改:

- 将 path_to_pretrained_weights 替换为预训练权重文件的路径。
- 将 dog.jpg 替换为你要测试的图像文件的路径。

运行程序后,应该会显示含有检测框和类别的图像窗口。

希望这能帮到你!如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
图片1.png

最佳答案

查看完整内容

YOLOv1的目标检测可视化程序 以下是一个使用预训练权重进行目标检测并可视化结果的示例程序。请确保已经安装了必要的依赖项,例如Keras和OpenCV。 请将代码中的以下部分进行相应的修改: - 将 path_to_pretrained_weights 替换为预训练权重文件的路径。 - 将 dog.jpg 替换为你要测试的图像文件的路径。 运行程序后,应该会显示含有检测框和类别的图像窗口。 希望这能帮到你!如果还有其他问题,请随时提问。 球一个最佳答 ...
回复

使用道具 举报

发表于 2023-8-28 20:29:10 | 显示全部楼层    本楼为最佳答案   
YOLOv1的目标检测可视化程序

以下是一个使用预训练权重进行目标检测并可视化结果的示例程序。请确保已经安装了必要的依赖项,例如Keras和OpenCV。
  1. import numpy as np
  2. import cv2
  3. from keras.models import Model
  4. from keras.layers import Input
  5. from yolo_v1 import YOLO
  6. # 定义类别标签
  7. classes = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow',
  8.            'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
  9. def load_image(image_path):
  10.     # 加载图像并进行预处理(根据训练时的预处理方式进行)
  11.     image = cv2.imread(image_path)
  12.     image = cv2.resize(image, (448, 448))
  13.     image = image / 255.0
  14.     image = np.expand_dims(image, axis=0)
  15.     return image
  16. def draw_boxes(image, boxes, scores):
  17.     # 在图像上绘制检测框和类别名称
  18.     for box, score in zip(boxes, scores):
  19.         x, y, w, h = box
  20.         class_index = np.argmax(score)
  21.         class_name = classes[class_index]
  22.         confidence = score[class_index]
  23.         label = f"{class_name}: {confidence:.2f}"
  24.         cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
  25.         cv2.putText(image, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
  26.     return image
  27. # 构建模型
  28. input_image = Input(shape=(448, 448, 3))
  29. yolo_model = YOLO(input_image, training=False)
  30. model = Model(inputs=input_image, outputs=yolo_model.output)
  31. # 加载预训练权重
  32. model.load_weights('path_to_pretrained_weights')
  33. # 进行目标检测和可视化
  34. image_path = 'dog.jpg'
  35. image = load_image(image_path)
  36. output = model.predict(image)
  37. # 解析输出
  38. boxes = output[..., :4]
  39. scores = output[..., 4:]
  40. # 设置置信度阈值,过滤掉置信度较低的检测结果
  41. confidence_threshold = 0.5
  42. filtered_boxes = []
  43. filtered_scores = []
  44. for box, score in zip(boxes[0], scores[0]):
  45.     max_score = np.max(score)
  46.     if max_score > confidence_threshold:
  47.         filtered_boxes.append(box)
  48.         filtered_scores.append(score)
  49. # 绘制检测结果并显示图像
  50. result_image = draw_boxes(cv2.imread(image_path), filtered_boxes, filtered_scores)
  51. cv2.imshow("Detection Result", result_image)
  52. cv2.waitKey(0)
  53. cv2.destroyAllWindows()
复制代码

请将代码中的以下部分进行相应的修改:

- 将 path_to_pretrained_weights 替换为预训练权重文件的路径。
- 将 dog.jpg 替换为你要测试的图像文件的路径。

运行程序后,应该会显示含有检测框和类别的图像窗口。

希望这能帮到你!如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-28 20:35:57 | 显示全部楼层
以下是一个使用YOLOv1模型进行预测和可视化检测的示例程序:

  1. import numpy as np
  2. import cv2
  3. from matplotlib import pyplot as plt
  4. from tensorflow.keras.models import Model
  5. from tensorflow.keras.layers import Conv2D, Input

  6. def create_yolo_model():
  7.     # 创建YOLOv1模型结构
  8.     input_image = Input(shape=(448, 448, 3))
  9.     # ... 模型结构参考上述链接中的代码 ...

  10.     model = Model(inputs=input_image, outputs=output)
  11.     return model

  12. def load_weights(model, weights_file):
  13.     # 加载训练好的权重
  14.     # ... 读取权重文件并将权重加载到模型中 ...

  15. def preprocess_image(image):
  16.     # 图像预处理,将图像调整为模型输入尺寸
  17.     image = cv2.resize(image, (448, 448))
  18.     image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  19.     image = image.astype(np.float32)
  20.     image /= 255.0
  21.     image = np.expand_dims(image, axis=0)
  22.     return image

  23. def postprocess_predictions(predictions):
  24.     # 后处理预测结果,提取检测框和类别信息
  25.     # ... 根据模型输出解析预测结果 ...

  26. def visualize_detections(image, detections):
  27.     # 可视化检测结果
  28.     # ... 根据检测结果在图像上绘制检测框和类别信息 ...

  29. # 创建模型
  30. model = create_yolo_model()

  31. # 加载权重
  32. weights_file = 'path_to_weights_file'
  33. load_weights(model, weights_file)

  34. # 读取测试图像
  35. image_file = 'dog.jpg'
  36. image = cv2.imread(image_file)

  37. # 图像预处理
  38. input_image = preprocess_image(image)

  39. # 进行预测
  40. predictions = model.predict(input_image)

  41. # 后处理预测结果
  42. detections = postprocess_predictions(predictions)

  43. # 可视化检测结果
  44. visualize_detections(image, detections)

  45. # 显示图像
  46. plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
  47. plt.axis('off')
  48. plt.show()
复制代码


请注意,上述代码中的一些部分需要根据你的具体情况进行修改,比如模型结构的创建和权重文件的路径。另外,你可能需要安装一些额外的库,如 matplotlib 和 opencv-python 。

希望这个示例程序能帮助到你,让你能够完成作业并实现可视化检测效果。如果你遇到任何问题,请随时提问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 05:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表