当前位置: 首页 > wzjs >正文

o2o网站建设包括哪些营销托管全网营销推广

o2o网站建设包括哪些,营销托管全网营销推广,厦门网站建设开发公司,重庆重庆网站建设公司在深度学习领域,YOLO(You Only Look Once)系列模型因其高效和准确性而广受欢迎。然而,随着项目需求的变化,有时我们需要对预训练模型的配置文件进行调整。本文将详细介绍如何使用Python脚本自动转换YOLOv5的配置文件到…

在深度学习领域,YOLO(You Only Look Once)系列模型因其高效和准确性而广受欢迎。然而,随着项目需求的变化,有时我们需要对预训练模型的配置文件进行调整。本文将详细介绍如何使用Python脚本自动转换YOLOv5的配置文件到https://github.com/ultralytics/ultralytics,并解释每个步骤的目的和实现方法。

准备工作

首先,确保安装了必要的库:

pip install pyyaml

代码详解

定义格式化和过滤函数

def format_and_filter_list(lst):formatted_items = []for item in lst:if isinstance(item, list):processed_item = format_and_filter_list(item)if processed_item:  # 只添加非空列表formatted_items.append(processed_item)elif isinstance(item, str):if item == 'nearest':formatted_items.append(f"'{item}'")elif item != 'anchors':  # 过滤掉 'anchors'formatted_items.append(item)else:formatted_items.append(str(item))return f"[{', '.join(formatted_items)}]"

定义转换配置函数

def transform_config(original_config):transformed = {'nc': original_config['nc'],  # number of classes'scales': {'n': [original_config['depth_multiple'], original_config['width_multiple'], 1024]},'backbone': [],'head': []}# Transform backbonefor layer in original_config['backbone']:if not isinstance(layer, list):layer = [layer]transformed['backbone'].append(layer)# Adjust the head to match the desired format, also updating nc and anchors usagefor i, layer in enumerate(original_config['head']):if not isinstance(layer, list):layer = [layer]if isinstance(layer[0], list) and len(layer[0]) > 3 and layer[0][3] == 'Detect':layer[0][3] = ['nc', 'anchors']transformed['head'].append([layer[0]])else:transformed['head'].append(layer)# Special case: update the final Detect layer configurationif transformed['head'] and isinstance(transformed['head'][-1], list) and transformed['head'][-1]:last_layer = transformed['head'][-1]if isinstance(last_layer[0], list) and len(last_layer[0]) > 3:last_layer[0][3] = ['nc']return transformed

定义主函数以更改YAML文件


def change_yaml(yolov5_config):# Check if input file existsif not os.path.exists(yolov5_config):print(f"Error: Input file '{yolov5_config}' does not exist.")else:old_path = os.path.dirname(yolov5_config)save_path = os.path.dirname(old_path)new_yaml_name_path = os.path.join(save_path, "yolov5_" + os.path.basename(yolov5_config))# Load your YAML filewith open(yolov5_config, 'r') as file:try:original_config = yaml.safe_load(file)except yaml.YAMLError as exc:print(f"Error: Failed to parse YAML file. {exc}")exit(1)transformed_config = transform_config(original_config)# Ensure that each element in backbone and head is a listdef ensure_list_format(config_part):new_config_part = []for item in config_part:if not isinstance(item, list):item = [item]new_config_part.append(item)return new_config_parttransformed_config['backbone'] = ensure_list_format(transformed_config['backbone'])transformed_config['head'] = ensure_list_format(transformed_config['head'])# Generate the formatted YAML contentformatted_yaml_content = f"""# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license# Ultralytics YOLOv5 object detection model with P3/8 - P5/32 outputs
# Model docs: https://docs.ultralytics.com/models/yolov5
# Task docs: https://docs.ultralytics.com/tasks/detect# Parameters
nc: {transformed_config['nc']} # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov5n.yaml' will call yolov5.yaml with scale 'n'# [depth, width, max_channels]n: {transformed_config['scales']['n']}
# YOLOv5 v6.0 backbone
backbone:
"""for layer in transformed_config['backbone']:layer = format_and_filter_list(layer)print(layer)formatted_yaml_content += f"  - {layer}\n"formatted_yaml_content += """
# YOLOv5 v6.0 head with (P2, P3, P4, P5) outputs
head:
"""for layer in transformed_config['head']:print(layer)if isinstance(layer, list) and layer[2] == 'Conv':formatted_yaml_content += '\n'layer = format_and_filter_list(layer)print(layer)formatted_yaml_content += f"  - {layer}\n"# Save the transformed configuration to a new YAML filewith open(new_yaml_name_path, 'w') as file:file.write(formatted_yaml_content)print(f"Configuration has been transformed and saved to '{new_yaml_name_path}'.")

批量处理多个配置文件

input_path = "/media/lindsay/data/ultralytics-main/ultralytics/cfg/models/v5/yolov5"
input_path_list = [os.path.join(input_path, i) for i in os.listdir(input_path)]
for i in input_path_list:change_yaml(i)

总结

通过上述步骤,您可以轻松地将YOLOv5的配置文件转换为ultralytics-yolov8所需的格式,并且可以批量处理多个配置文件。这个脚本不仅可以帮助您节省时间,还能减少手动操作带来的错误。希望这篇博客能为您提供有价值的指导,让您更高效地管理深度学习项目的配置文件。

完整代码

import yaml
import os# 函数用于格式化列表为所需形式
def format_and_filter_list(lst):formatted_items = []for item in lst:if isinstance(item, list):# 如果是列表类型,则递归调用此函数进行格式化和过滤processed_item = format_and_filter_list(item)if processed_item:  # 只添加非空列表formatted_items.append(processed_item)elif isinstance(item, str):# 特殊处理需要保留引号的字符串if item == 'nearest':formatted_items.append(f"'{item}'")elif item != 'anchors':  # 过滤掉 'anchors'formatted_items.append(item)else:# 其他类型直接转换成字符串表示formatted_items.append(str(item))# 返回格式化后的列表字符串表示return f"[{', '.join(formatted_items)}]"def transform_config(original_config):transformed = {'nc': original_config['nc'],  # number of classes'scales': {'n': [original_config['depth_multiple'], original_config['width_multiple'], 1024]},'backbone': [],'head': []}# Transform backbonefor layer in original_config['backbone']:if not isinstance(layer, list):layer = [layer]transformed['backbone'].append(layer)# Adjust the head to match the desired format, also updating nc and anchors usagefor i, layer in enumerate(original_config['head']):if not isinstance(layer, list):layer = [layer]if isinstance(layer[0], list) and len(layer[0]) > 3 and layer[0][3] == 'Detect':# Update Detect layer to use 'nc' instead of specific numbers for classeslayer[0][3] = ['nc', 'anchors']transformed['head'].append([layer[0]])else:transformed['head'].append(layer)# Special case: update the final Detect layer configurationif transformed['head'] and isinstance(transformed['head'][-1], list) and transformed['head'][-1]:last_layer = transformed['head'][-1]if isinstance(last_layer[0], list) and len(last_layer[0]) > 3:last_layer[0][3] = ['nc']return transformeddef change_yaml(yolov5_config):# Check if input file existsif not os.path.exists(yolov5_config):print(f"Error: Input file '{yolov5_config}' does not exist.")else:old_path = os.path.dirname(yolov5_config)save_path = os.path.dirname(old_path)new_yaml_name_path = os.path.join(save_path, "yolov5_" + os.path.basename(yolov5_config))# Load your YAML filewith open(yolov5_config, 'r') as file:try:original_config = yaml.safe_load(file)except yaml.YAMLError as exc:print(f"Error: Failed to parse YAML file. {exc}")exit(1)transformed_config = transform_config(original_config)# Ensure that each element in backbone and head is a listdef ensure_list_format(config_part):new_config_part = []for item in config_part:if not isinstance(item, list):item = [item]new_config_part.append(item)return new_config_parttransformed_config['backbone'] = ensure_list_format(transformed_config['backbone'])transformed_config['head'] = ensure_list_format(transformed_config['head'])# Generate the formatted YAML contentformatted_yaml_content = f"""# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license# Ultralytics YOLOv5 object detection model with P3/8 - P5/32 outputs
# Model docs: https://docs.ultralytics.com/models/yolov5
# Task docs: https://docs.ultralytics.com/tasks/detect# Parameters
nc: {transformed_config['nc']} # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov5n.yaml' will call yolov5.yaml with scale 'n'# [depth, width, max_channels]n: {transformed_config['scales']['n']}
# YOLOv5 v6.0 backbone
backbone:
"""for layer in transformed_config['backbone']:layer = format_and_filter_list(layer)print(layer)formatted_yaml_content += f"  - {layer}\n"formatted_yaml_content += """
# YOLOv5 v6.0 head with (P2, P3, P4, P5) outputs
head:
"""for layer in transformed_config['head']:print(layer)if isinstance(layer, list) and layer[2] == 'Conv':formatted_yaml_content += '\n'layer = format_and_filter_list(layer)print(layer)formatted_yaml_content += f"  - {layer}\n"# Save the transformed configuration to a new YAML filewith open(new_yaml_name_path, 'w') as file:file.write(formatted_yaml_content)print(f"Configuration has been transformed and saved to '{new_yaml_name_path}'.")input_path = "/media/lindsay/data/ultralytics-main/ultralytics/cfg/models/v5/yolov5"
input_path_list = [os.path.join(input_path, i) for i in os.listdir(input_path)]
for i in input_path_list:change_yaml(i)

文章转载自:

http://KM9c90Gq.qxdrw.cn
http://sunfwuIQ.qxdrw.cn
http://XXk69eTh.qxdrw.cn
http://M6eVG8zN.qxdrw.cn
http://UjxpsNzB.qxdrw.cn
http://gUifr7ZG.qxdrw.cn
http://4zBLa8S9.qxdrw.cn
http://UpEExSfW.qxdrw.cn
http://JMVm6k2i.qxdrw.cn
http://kutr062l.qxdrw.cn
http://yfw3NJit.qxdrw.cn
http://pNuOD6HN.qxdrw.cn
http://wTDkLwQ2.qxdrw.cn
http://w7ozz3Bi.qxdrw.cn
http://CMeQGJ3x.qxdrw.cn
http://0Rh5A2O3.qxdrw.cn
http://Q5apw2gn.qxdrw.cn
http://uGMtQXoK.qxdrw.cn
http://qCzly3I7.qxdrw.cn
http://ezHnAHT2.qxdrw.cn
http://AnGO3Zjj.qxdrw.cn
http://VyUZUUPF.qxdrw.cn
http://5pKQzVJZ.qxdrw.cn
http://LJ9rL5qp.qxdrw.cn
http://jhmgvxAA.qxdrw.cn
http://lnk98plA.qxdrw.cn
http://lwkiRIPX.qxdrw.cn
http://AYBGOaw3.qxdrw.cn
http://nixFfjzL.qxdrw.cn
http://d4A4r1yi.qxdrw.cn
http://www.dtcms.com/wzjs/699855.html

相关文章:

  • 有哪些做特卖的网站有哪些营销型网站建设课程
  • 如何做一个手机网站被墙域名黑别人网站
  • 大背景 网站只建设电子商务网站不维护
  • 广东建设注册中心网站潍坊做外贸网站建设
  • 模板网站建设教程视频教程网站制作的公司哪家效果好
  • 社交网站开发注意事项吉林省建设厅网站
  • 如何做网站ppt网页链接制作软件
  • 上海网站建设公公司广东省建设工程造价信息网官网
  • 布料市场做哪个网站好wordpress加上live2d
  • 昌网站建设安康市城乡建设规划局 网站
  • 做网站费用上海深圳网站建设潮动九州
  • 建站是什么专业国家工商官网查询
  • 网站浮动广告代码建设网站编程语言
  • 求个没封的w站2022龙岩市兼职网
  • 南昌企业网站模板建站企业网站psd模板
  • 南京制作网站优化厦门网直播
  • 微网站开发难度合肥建设网站查询系统
  • 网站开发入门个人网站如何进行网络推广
  • 环保设计院的网站建设有哪些网站有做网页用的小图片
  • 住房建设局网站首页经典软文推广案例
  • 做首饰网站中信建设有限责任公司校招
  • 网站建设小组的五类成员福田庆三整鼻子好吗
  • 武威市建设局网站 放管服购物网站怎么做
  • 做网站挣钱经历软件开发工程师职业分析
  • 绵阳安州区做网站的有哪些做订餐网站数据库应该有哪些表
  • 做网站图片多大建设银行网站logo
  • 做几何图形和网站网站建设所有软件清单
  • 移动网站开发 书仿站工具教程
  • 网站平台优化免费制作封面的网站
  • 崇卅市网站建设erp是什么意思