Initial public project cleanup
This commit is contained in:
commit
2034b04519
95 files changed
+16583
No files matched your search
@@ -0,0 +1,100 @@
|
||||
"""
|
||||
整理现有数据集脚本
|
||||
将 数据集/数据集/仿真数据 中的图像整理到标准格式
|
||||
"""
|
||||
import os
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
import random
|
||||
|
||||
def organize_existing_dataset():
|
||||
"""整理现有的光图像数据集"""
|
||||
|
||||
# 源数据路径 (使用绝对路径)
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
project_root = os.path.dirname(script_dir)
|
||||
source_base = os.path.join(project_root, "数据集", "数据集", "仿真数据", "单模态", "w=-0.5", "受畸变", "Cn=10-13")
|
||||
|
||||
# 目标路径
|
||||
output_dir = os.path.join(project_root, "data", "processed")
|
||||
train_dir = os.path.join(output_dir, "train")
|
||||
val_dir = os.path.join(output_dir, "val")
|
||||
|
||||
# 创建目标目录
|
||||
os.makedirs(train_dir, exist_ok=True)
|
||||
os.makedirs(val_dir, exist_ok=True)
|
||||
|
||||
print("开始整理数据集...")
|
||||
print(f"源目录: {source_base}")
|
||||
print(f"目标目录: {output_dir}")
|
||||
|
||||
total_images = 0
|
||||
train_count = 0
|
||||
val_count = 0
|
||||
|
||||
# 遍历l=1到l=10 (对应class_0到class_9,因为l=1表示1个光瓣)
|
||||
for l_value in range(1, 11):
|
||||
source_class_dir = os.path.join(source_base, f"l={l_value}")
|
||||
|
||||
if not os.path.exists(source_class_dir):
|
||||
print(f"警告: {source_class_dir} 不存在,跳过")
|
||||
continue
|
||||
|
||||
# 类别ID (l=1对应class_1, l=10对应class_10,但我们可能需要class_0到class_9)
|
||||
# 根据需求,l值直接对应光瓣数量
|
||||
class_id = l_value - 1 # l=1 -> class_0 (1个光瓣)
|
||||
|
||||
# 创建类别目录
|
||||
train_class_dir = os.path.join(train_dir, f"class_{class_id}")
|
||||
val_class_dir = os.path.join(val_dir, f"class_{class_id}")
|
||||
os.makedirs(train_class_dir, exist_ok=True)
|
||||
os.makedirs(val_class_dir, exist_ok=True)
|
||||
|
||||
# 获取所有图像
|
||||
images = [f for f in os.listdir(source_class_dir)
|
||||
if f.lower().endswith(('.jpg', '.jpeg', '.png'))]
|
||||
|
||||
if len(images) == 0:
|
||||
print(f"警告: {source_class_dir} 中没有图像")
|
||||
continue
|
||||
|
||||
# 随机打乱
|
||||
random.shuffle(images)
|
||||
|
||||
# 分割为训练集和验证集 (80:20)
|
||||
split_idx = int(len(images) * 0.8)
|
||||
train_images = images[:split_idx]
|
||||
val_images = images[split_idx:]
|
||||
|
||||
# 复制训练集图像
|
||||
for img_name in train_images:
|
||||
src = os.path.join(source_class_dir, img_name)
|
||||
dst = os.path.join(train_class_dir, img_name)
|
||||
shutil.copy2(src, dst)
|
||||
train_count += 1
|
||||
|
||||
# 复制验证集图像
|
||||
for img_name in val_images:
|
||||
src = os.path.join(source_class_dir, img_name)
|
||||
dst = os.path.join(val_class_dir, img_name)
|
||||
shutil.copy2(src, dst)
|
||||
val_count += 1
|
||||
|
||||
total_images += len(images)
|
||||
print(f"class_{class_id} (l={l_value}): {len(train_images)} 训练, {len(val_images)} 验证")
|
||||
|
||||
print(f"\n数据集整理完成!")
|
||||
print(f"总图像数: {total_images}")
|
||||
print(f"训练集: {train_count} 张")
|
||||
print(f"验证集: {val_count} 张")
|
||||
print(f"\n数据集位置: {output_dir}")
|
||||
print("\n下一步: 运行训练脚本")
|
||||
print("cd pc_training")
|
||||
print("python train.py --data_dir ../data/processed --pretrained --epochs 50")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 设置随机种子以保证可重复性
|
||||
random.seed(42)
|
||||
|
||||
organize_existing_dataset()
|
||||
@@ -0,0 +1,131 @@
|
||||
"""
|
||||
数据集下载和准备脚本
|
||||
"""
|
||||
import os
|
||||
import argparse
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def create_dummy_dataset(output_dir, num_classes=10, images_per_class=100):
|
||||
"""
|
||||
创建示例数据集结构
|
||||
|
||||
Args:
|
||||
output_dir: 输出目录
|
||||
num_classes: 类别数量
|
||||
images_per_class: 每个类别的图像数量
|
||||
"""
|
||||
print(f"创建数据集结构: {output_dir}")
|
||||
|
||||
# 创建训练集和验证集
|
||||
for split in ['train', 'val']:
|
||||
split_dir = os.path.join(output_dir, split)
|
||||
os.makedirs(split_dir, exist_ok=True)
|
||||
|
||||
# 创建类别目录
|
||||
for class_id in range(num_classes):
|
||||
class_dir = os.path.join(split_dir, f'class_{class_id}')
|
||||
os.makedirs(class_dir, exist_ok=True)
|
||||
|
||||
# 创建占位符文件
|
||||
readme_path = os.path.join(class_dir, 'README.txt')
|
||||
with open(readme_path, 'w', encoding='utf-8') as f:
|
||||
f.write(f'类别 {class_id}: {class_id}个光瓣\n')
|
||||
f.write(f'请在此目录放置 {images_per_class} 张图像\n')
|
||||
f.write('支持的格式: .jpg, .jpeg, .png\n')
|
||||
|
||||
print("数据集结构创建完成!")
|
||||
print("\n目录结构:")
|
||||
print(f"{output_dir}/")
|
||||
print(" ├── train/")
|
||||
print(" │ ├── class_0/ (0个光瓣)")
|
||||
print(" │ ├── class_1/ (1个光瓣)")
|
||||
print(" │ └── ...")
|
||||
print(" └── val/")
|
||||
print(" ├── class_0/")
|
||||
print(" └── ...")
|
||||
print("\n请将图像放入对应的类别目录中")
|
||||
|
||||
|
||||
def split_dataset(source_dir, output_dir, train_ratio=0.8):
|
||||
"""
|
||||
将数据集分割为训练集和验证集
|
||||
|
||||
Args:
|
||||
source_dir: 源数据目录
|
||||
output_dir: 输出目录
|
||||
train_ratio: 训练集比例
|
||||
"""
|
||||
import random
|
||||
|
||||
print(f"分割数据集: {source_dir} -> {output_dir}")
|
||||
|
||||
# 创建输出目录
|
||||
train_dir = os.path.join(output_dir, 'train')
|
||||
val_dir = os.path.join(output_dir, 'val')
|
||||
os.makedirs(train_dir, exist_ok=True)
|
||||
os.makedirs(val_dir, exist_ok=True)
|
||||
|
||||
# 遍历类别
|
||||
for class_name in os.listdir(source_dir):
|
||||
class_path = os.path.join(source_dir, class_name)
|
||||
if not os.path.isdir(class_path):
|
||||
continue
|
||||
|
||||
# 获取所有图像
|
||||
images = [f for f in os.listdir(class_path)
|
||||
if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
||||
|
||||
# 随机打乱
|
||||
random.shuffle(images)
|
||||
|
||||
# 分割
|
||||
split_idx = int(len(images) * train_ratio)
|
||||
train_images = images[:split_idx]
|
||||
val_images = images[split_idx:]
|
||||
|
||||
# 创建类别目录
|
||||
train_class_dir = os.path.join(train_dir, class_name)
|
||||
val_class_dir = os.path.join(val_dir, class_name)
|
||||
os.makedirs(train_class_dir, exist_ok=True)
|
||||
os.makedirs(val_class_dir, exist_ok=True)
|
||||
|
||||
# 复制文件
|
||||
for img in train_images:
|
||||
src = os.path.join(class_path, img)
|
||||
dst = os.path.join(train_class_dir, img)
|
||||
shutil.copy2(src, dst)
|
||||
|
||||
for img in val_images:
|
||||
src = os.path.join(class_path, img)
|
||||
dst = os.path.join(val_class_dir, img)
|
||||
shutil.copy2(src, dst)
|
||||
|
||||
print(f"{class_name}: {len(train_images)} 训练, {len(val_images)} 验证")
|
||||
|
||||
print("数据集分割完成!")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description='数据集准备工具')
|
||||
parser.add_argument('--mode', type=str, choices=['create', 'split'],
|
||||
required=True, help='操作模式')
|
||||
parser.add_argument('--output', type=str, required=True,
|
||||
help='输出目录')
|
||||
parser.add_argument('--source', type=str, default=None,
|
||||
help='源数据目录(split模式)')
|
||||
parser.add_argument('--num_classes', type=int, default=10,
|
||||
help='类别数量(create模式)')
|
||||
parser.add_argument('--train_ratio', type=float, default=0.8,
|
||||
help='训练集比例(split模式)')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.mode == 'create':
|
||||
create_dummy_dataset(args.output, args.num_classes)
|
||||
elif args.mode == 'split':
|
||||
if args.source is None:
|
||||
print("错误: split模式需要指定--source参数")
|
||||
else:
|
||||
split_dataset(args.source, args.output, args.train_ratio)
|
||||
@@ -0,0 +1,37 @@
|
||||
#!/bin/bash
|
||||
# PYNQ Z2 环境配置脚本
|
||||
|
||||
echo "=== PYNQ Z2 环境配置 ==="
|
||||
|
||||
# 更新系统
|
||||
echo "更新系统包..."
|
||||
sudo apt-get update
|
||||
|
||||
# 安装必要的Python包
|
||||
echo "安装Python依赖..."
|
||||
pip3 install numpy opencv-python pillow matplotlib tqdm
|
||||
|
||||
# 安装PYNQ相关包
|
||||
echo "安装PYNQ包..."
|
||||
pip3 install pynq
|
||||
|
||||
# 创建工作目录
|
||||
echo "创建工作目录..."
|
||||
mkdir -p ~/mobilenet_project/{models,images,results}
|
||||
|
||||
# 设置权限
|
||||
echo "设置权限..."
|
||||
sudo chmod 666 /dev/mem
|
||||
|
||||
# 检查FPGA设备
|
||||
echo "检查FPGA设备..."
|
||||
ls -l /dev/xdevcfg
|
||||
|
||||
echo "=== 配置完成 ==="
|
||||
echo "请将以下文件传输到PYNQ板卡:"
|
||||
echo " 1. bitstream文件 (.bit)"
|
||||
echo " 2. 硬件描述文件 (.hwh)"
|
||||
echo " 3. 推理脚本 (inference.py, utils.py)"
|
||||
echo ""
|
||||
echo "使用方法:"
|
||||
echo " python3 inference.py --bitstream mobilenet.bit --image test.jpg"
|
||||
@@ -0,0 +1,64 @@
|
||||
@echo off
|
||||
REM 测试数据打包脚本
|
||||
|
||||
echo ========================================
|
||||
echo 测试数据打包工具
|
||||
echo ========================================
|
||||
echo.
|
||||
|
||||
REM 检查PowerShell
|
||||
where powershell >nul 2>&1
|
||||
if %errorlevel% neq 0 (
|
||||
echo 错误: 未找到PowerShell
|
||||
pause
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo 正在打包测试数据...
|
||||
echo.
|
||||
|
||||
REM 方案1: 仅测试数据(推荐给老师)
|
||||
echo [1/3] 打包测试数据集(2.1 MB)...
|
||||
powershell -Command "Compress-Archive -Path 'data\processed\val' -DestinationPath '测试数据集.zip' -Force"
|
||||
if exist "测试数据集.zip" (
|
||||
echo ✓ 测试数据集.zip 已创建
|
||||
for %%A in ("测试数据集.zip") do echo 大小: %%~zA 字节
|
||||
) else (
|
||||
echo ✗ 创建失败
|
||||
)
|
||||
echo.
|
||||
|
||||
REM 方案2: 测试数据 + 模型
|
||||
echo [2/3] 打包测试数据和模型(约30 MB)...
|
||||
powershell -Command "$files = @('data\processed\val', 'data\models\best_model.pth', 'data\models\model.onnx', '测试数据说明.md'); Compress-Archive -Path $files -DestinationPath '测试数据和模型.zip' -Force"
|
||||
if exist "测试数据和模型.zip" (
|
||||
echo ✓ 测试数据和模型.zip 已创建
|
||||
for %%A in ("测试数据和模型.zip") do echo 大小: %%~zA 字节
|
||||
) else (
|
||||
echo ✗ 创建失败
|
||||
)
|
||||
echo.
|
||||
|
||||
REM 方案3: 完整项目(用于答辩)
|
||||
echo [3/3] 打包完整项目(约35 MB)...
|
||||
powershell -Command "$files = @('pc_training', 'finn_deployment', 'data\models', 'data\processed\val', 'docs', '*.md'); Compress-Archive -Path $files -DestinationPath '完整项目.zip' -Force"
|
||||
if exist "完整项目.zip" (
|
||||
echo ✓ 完整项目.zip 已创建
|
||||
for %%A in ("完整项目.zip") do echo 大小: %%~zA 字节
|
||||
) else (
|
||||
echo ✗ 创建失败
|
||||
)
|
||||
echo.
|
||||
|
||||
echo ========================================
|
||||
echo 打包完成!
|
||||
echo ========================================
|
||||
echo.
|
||||
echo 生成的文件:
|
||||
echo 1. 测试数据集.zip - 仅测试数据(推荐给老师)
|
||||
echo 2. 测试数据和模型.zip - 测试数据 + 训练模型
|
||||
echo 3. 完整项目.zip - 完整项目(用于答辩)
|
||||
echo.
|
||||
echo 说明文档: 测试数据说明.md
|
||||
echo.
|
||||
pause
|
||||
@@ -0,0 +1,122 @@
|
||||
"""
|
||||
测试数据打包工具
|
||||
|
||||
功能:
|
||||
1. 打包测试数据集(给老师)
|
||||
2. 打包测试数据+模型
|
||||
3. 打包完整项目(用于答辩)
|
||||
"""
|
||||
import os
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def create_zip(source_paths, output_name, description):
|
||||
"""
|
||||
创建ZIP压缩包
|
||||
|
||||
参数:
|
||||
source_paths: 源文件/目录列表
|
||||
output_name: 输出文件名
|
||||
description: 描述信息
|
||||
"""
|
||||
print(f"\n正在打包: {description}")
|
||||
print(f"输出文件: {output_name}")
|
||||
|
||||
try:
|
||||
with zipfile.ZipFile(output_name, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
||||
for source in source_paths:
|
||||
if os.path.isfile(source):
|
||||
# 添加文件
|
||||
zipf.write(source, os.path.basename(source))
|
||||
print(f" + {source}")
|
||||
elif os.path.isdir(source):
|
||||
# 添加目录
|
||||
for root, dirs, files in os.walk(source):
|
||||
for file in files:
|
||||
file_path = os.path.join(root, file)
|
||||
arcname = os.path.relpath(file_path, os.path.dirname(source))
|
||||
zipf.write(file_path, arcname)
|
||||
print(f" + {source}/ ({len(list(Path(source).rglob('*')))} 个文件)")
|
||||
|
||||
# 显示文件大小
|
||||
size_mb = os.path.getsize(output_name) / (1024 * 1024)
|
||||
print(f"✓ 完成! 大小: {size_mb:.2f} MB")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ 失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
print("=" * 60)
|
||||
print("测试数据打包工具")
|
||||
print("=" * 60)
|
||||
|
||||
# 切换到项目根目录
|
||||
os.chdir(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
# 方案1: 仅测试数据(推荐给老师)
|
||||
print("\n[方案1] 测试数据集(推荐给老师)")
|
||||
create_zip(
|
||||
source_paths=['data/processed/val'],
|
||||
output_name='测试数据集.zip',
|
||||
description='100张测试图像,10个类别'
|
||||
)
|
||||
|
||||
# 方案2: 测试数据 + 模型
|
||||
print("\n[方案2] 测试数据和模型")
|
||||
create_zip(
|
||||
source_paths=[
|
||||
'data/processed/val',
|
||||
'data/models/best_model.pth',
|
||||
'data/models/model.onnx',
|
||||
'测试数据说明.md'
|
||||
],
|
||||
output_name='测试数据和模型.zip',
|
||||
description='测试数据 + 训练好的模型 + 说明文档'
|
||||
)
|
||||
|
||||
# 方案3: 完整项目(用于答辩)
|
||||
print("\n[方案3] 完整项目(用于答辩)")
|
||||
|
||||
# 收集所有.md文件
|
||||
md_files = [f for f in os.listdir('.') if f.endswith('.md')]
|
||||
|
||||
create_zip(
|
||||
source_paths=[
|
||||
'pc_training',
|
||||
'finn_deployment',
|
||||
'data/models',
|
||||
'data/processed/val',
|
||||
'docs'
|
||||
] + md_files,
|
||||
output_name='完整项目.zip',
|
||||
description='完整项目代码、模型、文档'
|
||||
)
|
||||
|
||||
# 总结
|
||||
print("\n" + "=" * 60)
|
||||
print("打包完成!")
|
||||
print("=" * 60)
|
||||
print("\n生成的文件:")
|
||||
|
||||
for filename in ['测试数据集.zip', '测试数据和模型.zip', '完整项目.zip']:
|
||||
if os.path.exists(filename):
|
||||
size_mb = os.path.getsize(filename) / (1024 * 1024)
|
||||
print(f" ✓ {filename:25s} ({size_mb:6.2f} MB)")
|
||||
else:
|
||||
print(f" ✗ {filename:25s} (未生成)")
|
||||
|
||||
print("\n推荐:")
|
||||
print(" - 给老师: 测试数据集.zip + 测试数据说明.md")
|
||||
print(" - 演示用: 测试数据和模型.zip")
|
||||
print(" - 答辩用: 完整项目.zip")
|
||||
print("\n说明文档: 测试数据说明.md")
|
||||
print("=" * 60)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in new issue
Block a user