import numpy as np
import torch
from torch.utils.data import DataLoader
from dataset import BrainMRIdataset
from torchvision import transforms
import matplotlib.pyplot as plt
from Unet import UNet
import torch.nn as nn
import torch.optim as optim
from tqdm import tqdm
train_img_path = './data/train/image/'
train_label_path = './data/train/label/'
test_img_path = './data/test/image/'
test_label_path = './data/test/label'
train_transform = transforms.Compose([transforms.Resize((256,256)),transforms.ToTensor()])
test_transform = transforms.Compose([transforms.Resize((256,256)),transforms.ToTensor()])
# 加载数据集
trainSet = BrainMRIdataset(image_dir=train_img_path,mask_dir=train_label_path,transform=train_transform)
testSet = BrainMRIdataset(image_dir=test_img_path,mask_dir=test_label_path,transform=test_transform)
# 样本的个数
len_trainSet = len(trainSet)
len_testSet = len(testSet)
# 载入数据集
trainLoader = DataLoader(trainSet,batch_size=4,shuffle=True)
testLoader = DataLoader(testSet,batch_size=4,shuffle=False)
# 实例化模型
model =UNet(in_channels=3,num_classes=1)
loss_fn = nn.BCEWithLogitsLoss() # 损失函数
optimizer = optim.Adam(model.parameters(),lr=0.0001)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model.to(device)
# train
def fit(epoch, model, train_loader, test_loader):
correct = 0
running_loss = 0
epoch_IOU = []
# 训练模式
model.train()
for x, y in tqdm(train_loader):
x = x.to(device)
y = y.to(device)
y_pred = model(x) # forward
loss = loss_fn(y_pred, y) # 损失函数
optimizer.zero_grad() # 梯度清零
loss.backward() # backward
optimizer.step() # 梯度下降
with torch.no_grad():
y_pred = torch.sigmoid(y_pred)
y_pred = (y_pred > 0.5).float() # 预测二值化
correct += (y_pred == y).sum().item()
running_loss += loss.item()
# 计算 iou
intersection = y * y_pred # 计算交集 pred ∩ true
temp = y_pred + y # pred + true
union = temp - intersection # 计算并集:A ∪ B = A + B - A ∩ B
batch_iou = intersection.sum() / (union.sum() + 1e-8)
epoch_IOU.append(batch_iou.item())
epoch_loss = running_loss / len_trainSet
epoch_acc = correct / (len_trainSet*256*256)
test_correct = 0
test_running_loss = 0
epoch_test_IOU = []
model.eval()
with torch.no_grad():
for x, y in tqdm(test_loader):
x = x.to(device)
y = y.to(device)
y_pred = model(x)
loss = loss_fn(y_pred, y)
y_pred = torch.sigmoid(y_pred)
y_pred = (y_pred > 0.5).float() # 预测二值化
test_correct += (y_pred == y).sum().item()
test_running_loss += loss.item()
# 计算 iou
intersection = y * y_pred # 计算交集 pred ∩ true
temp = y_pred + y # pred + true
union = temp - intersection # 计算并集:A ∪ B = A + B - A ∩ B
batch_test_iou = intersection.sum() / (union.sum() + 1e-8)
epoch_test_IOU.append(batch_test_iou.item())
epoch_test_loss = test_running_loss / len_testSet
epoch_test_acc = test_correct / (len_testSet*256*256)
# save model
static_dict = model.state_dict()
torch.save(static_dict,'./log/pre_train_{}_train_iou_{}_test_iou_{}.pth'.format(epoch+1,round(np.mean(epoch_IOU),5),round(np.mean(epoch_test_IOU),5)))
print('epoch:', epoch + 1)
print('train loss:', round(epoch_loss, 5))
print('train accuracy:', round(epoch_acc, 5))
print('train IOU:',round(np.mean(epoch_IOU),5))
print('test loss:', round(epoch_test_loss, 5))
print('test accuracy:', round(epoch_test_acc, 5))
print('test IOU:', round(np.mean(epoch_test_IOU), 5)),
if __name__ == '__main__':
epochs = 50
for epoch in range(epochs):
# 加载预训练权重
#model.load_state_dict(torch.load('./log/49_train_iou_0.86304_test_iou_0.81689.pth'))
fit(epoch, model, train_loader=trainLoader,test_loader=testLoader)
print('training over !!!')
没有合适的资源?快使用搜索试试~ 我知道了~
风力发电机风扇语义分割数据集(包含python训练代码)
共2000个文件
tif:1994个
py:6个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 91 浏览量
2024-10-26
18:50:53
上传
评论 1
收藏 810.93MB ZIP 举报
温馨提示
风力发电机风扇语义分割数据集 包含含有大量图片及对应的标签图片和python训练代码,可用于语义分割算法训练 资源中包含: predict.py split_data.py train.py Unet.py dataset.py pre_process.py DATASET ========= 说明 ================= 资源来源于网络分析,仅供参考。如有侵权请告知。
资源推荐
资源详情
资源评论
收起资源包目录
风力发电机风扇语义分割数据集(包含python训练代码) (2000个子文件)
train.py 4KB
Unet.py 3KB
split_data.py 2KB
predict.py 2KB
pre_process.py 2KB
dataset.py 1KB
TCGA_DU_6408_19860521_25.tif 198KB
TCGA_DU_7301_19911112_25.tif 198KB
TCGA_DU_5871_19941206_20.tif 198KB
TCGA_DU_7294_19890104_27.tif 198KB
TCGA_DU_6404_19850629_37.tif 198KB
TCGA_HT_8113_19930809_18.tif 198KB
TCGA_DU_8168_19970503_16.tif 198KB
TCGA_HT_7602_19951103_11.tif 198KB
TCGA_HT_7693_19950520_11.tif 198KB
TCGA_DU_8164_19970111_24.tif 198KB
TCGA_DU_6401_19831001_37.tif 198KB
TCGA_DU_A5TT_19980318_48.tif 198KB
TCGA_HT_7882_19970125_24.tif 198KB
TCGA_FG_A4MT_20020212_7.tif 198KB
TCGA_FG_A4MU_20030903_15.tif 198KB
TCGA_DU_7299_19910417_27.tif 198KB
TCGA_FG_6689_20020326_36.tif 198KB
TCGA_DU_7304_19930325_18.tif 198KB
TCGA_DU_8168_19970503_28.tif 198KB
TCGA_DU_7294_19890104_25.tif 198KB
TCGA_DU_5855_19951217_22.tif 198KB
TCGA_FG_A4MU_20030903_9.tif 198KB
TCGA_CS_5396_20010302_17.tif 198KB
TCGA_DU_6404_19850629_35.tif 198KB
TCGA_DU_6399_19830416_26.tif 198KB
TCGA_HT_A61A_20000127_35.tif 198KB
TCGA_DU_7013_19860523_20.tif 198KB
TCGA_HT_7882_19970125_27.tif 198KB
TCGA_DU_7014_19860618_34.tif 198KB
TCGA_DU_6407_19860514_23.tif 198KB
TCGA_HT_8113_19930809_14.tif 198KB
TCGA_DU_7306_19930512_19.tif 198KB
TCGA_DU_7008_19830723_21.tif 198KB
TCGA_DU_8165_19970205_13.tif 198KB
TCGA_HT_7684_19950816_13.tif 198KB
TCGA_HT_7877_19980917_20.tif 198KB
TCGA_HT_7473_19970826_14.tif 198KB
TCGA_DU_8163_19961119_16.tif 198KB
TCGA_HT_8018_19970411_10.tif 198KB
TCGA_DU_A5TU_19980312_17.tif 198KB
TCGA_DU_6399_19830416_23.tif 198KB
TCGA_FG_8189_20030516_33.tif 198KB
TCGA_DU_8167_19970402_24.tif 198KB
TCGA_DU_A5TT_19980318_53.tif 198KB
TCGA_CS_5395_19981004_12.tif 198KB
TCGA_HT_7694_19950404_13.tif 198KB
TCGA_DU_7298_19910324_14.tif 198KB
TCGA_DU_6400_19830518_31.tif 198KB
TCGA_FG_6689_20020326_31.tif 198KB
TCGA_DU_7008_19830723_34.tif 198KB
TCGA_DU_7019_19940908_21.tif 198KB
TCGA_CS_4941_19960909_12.tif 198KB
TCGA_HT_8107_19980708_10.tif 198KB
TCGA_DU_8167_19970402_21.tif 198KB
TCGA_HT_7856_19950831_29.tif 198KB
TCGA_FG_6691_20020405_21.tif 198KB
TCGA_HT_8106_19970727_13.tif 198KB
TCGA_DU_5874_19950510_23.tif 198KB
TCGA_DU_6408_19860521_40.tif 198KB
TCGA_HT_7882_19970125_19.tif 198KB
TCGA_HT_8113_19930809_16.tif 198KB
TCGA_DU_7301_19911112_19.tif 198KB
TCGA_DU_A5TW_19980228_26.tif 198KB
TCGA_HT_7882_19970125_12.tif 198KB
TCGA_CS_6188_20010812_15.tif 198KB
TCGA_DU_A5TY_19970709_22.tif 198KB
TCGA_HT_A61A_20000127_34.tif 198KB
TCGA_FG_7637_20000922_27.tif 198KB
TCGA_DU_7010_19860307_36.tif 198KB
TCGA_DU_7298_19910324_8.tif 198KB
TCGA_HT_7608_19940304_19.tif 198KB
TCGA_FG_5964_20010511_12.tif 198KB
TCGA_HT_A61B_19991127_42.tif 198KB
TCGA_DU_7008_19830723_18.tif 198KB
TCGA_DU_8164_19970111_23.tif 198KB
TCGA_DU_8163_19961119_17.tif 198KB
TCGA_HT_8105_19980826_28.tif 198KB
TCGA_HT_7473_19970826_10.tif 198KB
TCGA_DU_7300_19910814_16.tif 198KB
TCGA_CS_6290_20000917_7.tif 198KB
TCGA_HT_8107_19980708_12.tif 198KB
TCGA_CS_6669_20020102_12.tif 198KB
TCGA_HT_7855_19951020_9.tif 198KB
TCGA_FG_6692_20020606_19.tif 198KB
TCGA_FG_6690_20020226_32.tif 198KB
TCGA_DU_A5TT_19980318_52.tif 198KB
TCGA_FG_6690_20020226_36.tif 198KB
TCGA_FG_A4MU_20030903_18.tif 198KB
TCGA_DU_7304_19930325_28.tif 198KB
TCGA_FG_6690_20020226_25.tif 198KB
TCGA_FG_6688_20020215_26.tif 198KB
TCGA_DU_5853_19950823_26.tif 198KB
TCGA_DU_5851_19950428_13.tif 198KB
TCGA_FG_6691_20020405_24.tif 198KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
abments
- 粉丝: 2182
- 资源: 2023
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于微信小程序的社团活动助手php.zip
- 懂球短视频微信小程序SpringBoot.zip
- java项目,毕业设计-医患档案管理系统
- 船检测8-YOLO(v5至v11)、COCO、Paligemma、TFRecord、VOC数据集合集.rar
- 好用的网络链接监测工具,支持设置各项ping参数(时延,包长等),支持日志记录
- stm32f407进行直流电机pid调速源程序
- java项目,毕业设计-医院固定资产系统
- 经典好用 的网卡管理 工具,支持多IP绑定,静态路由配置,可永久 保存
- C# WPF客户询单管理系统.zip(源码+数据库文件)
- java项目,毕业设计-在线外卖系统
- 机器学习四大名著,入门学习,中间反复研读都适用
- C# 键盘按键禁用拦截.zip
- 剪映【下载这个,直接安装与原来的共存、不显示VIP直接用】.apk
- 简单易用的一个端口转发及代理工具,可实现地址及端口映射
- stm32f103官方DSP库测试程序 可做128点、256点的fft运算,时间很短
- PHP遍历二叉树的实现,深度优先,广度优先,非递归实现
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功