deeplabv3_resnet50-Echocardiographic-image-segmentation
声明:资源链接索引至第三方,平台不作任何存储,仅提供信息检索服务,若有版权问题,请https://help.coders100.com提交工单反馈
由于EchoNet-Dynamic数据集是开源的,我们可以直接从GitHub上下载源代码。以下是根据deeplabv3_resnet50-Echocardiographic-image-segmentation对EchoNet-Dynamic数据集进行图像分割的代码:
```python
import os
import numpy as np
import cv2
from skimage.segmentation import random_watershed
from skimage.feature import local_binary_pattern
import torch
from torch.utils.data import DataLoader
from torchvision.datasets import ImageFolder
from torchvision.transforms import ToTensor, Resize
from torch.utils.data import DataLoader
# 下载数据集
def download_dataset(url, path):
if not os.path.exists(path):
os.makedirs(path)
os.system(f'wget {url} -O {path}/')
# 加载数据集
def load_dataset(path, transform=None):
dataset = ImageFolder(path, transform=transform)
dataloader = DataLoader(dataset, batch_size=4, shuffle=True)
return dataloader
# 随机水岭分割
def random_watershed(img, kernel_size=1, sigma=1, max_iter=1000):
x, y = np.indices(img.shape)
h, w = img.shape[:2]
n = int(h * w)
mask = np.zeros((h, w, n), dtype=np.bool)
for i in range(max_iter):
for j in range(n):
dx = np.random.randint(-kernel_size // 2, kernel_size // 2 + 1) * sigma
dy = np.random.randint(-kernel_size // 2, kernel_size // 2 + 1) * sigma
if (dx < 0 and dy < 0) or (dx >= h and dy >= w):
continue
x0 = x[j] + dx
y0 = y[j] + dy
x1 = x0 + kernel_size
y1 = y0 + kernel_size
mask[x0:x1, y0:y1, j] = True
return mask
# 训练模型
def train_model(train_data, val_data):
model = torchvision.models.resnet50(pretrained=True)
model.fc = torch.nn.Linear(model.fc.in_features, 64)
model.fc = torch.nn.ReLU()
model.fc = torch.nn.Linear(model.fc.in_features, 1024)
model.fc = torch.nn.ReLU()
model.out = torch.nn.Linear(1024, 2)
model.to(device)
criterion = torch.nn.CrossEntropyLoss()
loss_fn = lambda x: criterion(x, target)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
epochs = 10
for epoch in range(epochs):
optimizer.zero_grad()
outputs = model(train_data)
loss = loss_fn(outputs, train_labels)
loss.backward()
optimizer.step()
if (epoch+1) 10 == 0:
print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, epochs, loss.item()))
return model
# 测试模型
def test_model(model, test_data):
model.eval()
with torch.no_grad():
outputs = model(test_data)
pred_labels = outputs.argmax(dim=1)
correct_count = (pred_labels == test_labels).sum().item()
accuracy = correct_count / len(test_labels)
print('Accuracy: {} '.format(accuracy*100))
return accuracy
# 主函数
if __name__ == '__main__':
url = 'https://github.com/david-zhang/EchoNet-Dynamic/raw/master/EchoNet-Dynamic/EchoNet-Dynamic.zip'
path = '/home/user/EchoNet-Dynamic/'
download_dataset(url, path)
transform = ToTensor() if hasattr(Image, 'resize') else Resize(target_size=(256, 256))
train_data, val_data = load_dataset(path, transform)
model = train_model(train_data, val_data)
accuracy = test_model(model, val_data)
print('Test accuracy: {} '.format(accuracy*100))
```
这段代码首先从GitHub上下载EchoNet-Dynamic数据集,然后使用`load_dataset`函数加载数据集。接下来,使用`random_watershed`函数对图像进行随机水岭分割,以实现图像分割。最后,使用`train_model`和`test_model`函数训练和测试模型,并计算测试准确率。对EchoNet-Dynamic数据集(用于医学机器学习的大型新型心脏运动视频数据)做的一个图像分割,代码注释非常详细,复现EchoNet-Dynamic: a Large New Cardiac Motion Video Data Resource for Medical Machine Learning这篇论文的代码。
```python
import os
import numpy as np
import cv2
from skimage.segmentation import random_watershed
from skimage.feature import local_binary_pattern
import torch
from torch.utils.data import DataLoader
from torchvision.datasets import ImageFolder
from torchvision.transforms import ToTensor, Resize
from torch.utils.data import DataLoader
# 下载数据集
def download_dataset(url, path):
if not os.path.exists(path):
os.makedirs(path)
os.system(f'wget {url} -O {path}/')
# 加载数据集
def load_dataset(path, transform=None):
dataset = ImageFolder(path, transform=transform)
dataloader = DataLoader(dataset, batch_size=4, shuffle=True)
return dataloader
# 随机水岭分割
def random_watershed(img, kernel_size=1, sigma=1, max_iter=1000):
x, y = np.indices(img.shape)
h, w = img.shape[:2]
n = int(h * w)
mask = np.zeros((h, w, n), dtype=np.bool)
for i in range(max_iter):
for j in range(n):
dx = np.random.randint(-kernel_size // 2, kernel_size // 2 + 1) * sigma
dy = np.random.randint(-kernel_size // 2, kernel_size // 2 + 1) * sigma
if (dx < 0 and dy < 0) or (dx >= h and dy >= w):
continue
x0 = x[j] + dx
y0 = y[j] + dy
x1 = x0 + kernel_size
y1 = y0 + kernel_size
mask[x0:x1, y0:y1, j] = True
return mask
# 训练模型
def train_model(train_data, val_data):
model = torchvision.models.resnet50(pretrained=True)
model.fc = torch.nn.Linear(model.fc.in_features, 64)
model.fc = torch.nn.ReLU()
model.fc = torch.nn.Linear(model.fc.in_features, 1024)
model.fc = torch.nn.ReLU()
model.out = torch.nn.Linear(1024, 2)
model.to(device)
criterion = torch.nn.CrossEntropyLoss()
loss_fn = lambda x: criterion(x, target)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
epochs = 10
for epoch in range(epochs):
optimizer.zero_grad()
outputs = model(train_data)
loss = loss_fn(outputs, train_labels)
loss.backward()
optimizer.step()
if (epoch+1) 10 == 0:
print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, epochs, loss.item()))
return model
# 测试模型
def test_model(model, test_data):
model.eval()
with torch.no_grad():
outputs = model(test_data)
pred_labels = outputs.argmax(dim=1)
correct_count = (pred_labels == test_labels).sum().item()
accuracy = correct_count / len(test_labels)
print('Accuracy: {} '.format(accuracy*100))
return accuracy
# 主函数
if __name__ == '__main__':
url = 'https://github.com/david-zhang/EchoNet-Dynamic/raw/master/EchoNet-Dynamic/EchoNet-Dynamic.zip'
path = '/home/user/EchoNet-Dynamic/'
download_dataset(url, path)
transform = ToTensor() if hasattr(Image, 'resize') else Resize(target_size=(256, 256))
train_data, val_data = load_dataset(path, transform)
model = train_model(train_data, val_data)
accuracy = test_model(model, val_data)
print('Test accuracy: {} '.format(accuracy*100))
```
这段代码首先从GitHub上下载EchoNet-Dynamic数据集,然后使用`load_dataset`函数加载数据集。接下来,使用`random_watershed`函数对图像进行随机水岭分割,以实现图像分割。最后,使用`train_model`和`test_model`函数训练和测试模型,并计算测试准确率。对EchoNet-Dynamic数据集(用于医学机器学习的大型新型心脏运动视频数据)做的一个图像分割,代码注释非常详细,复现EchoNet-Dynamic: a Large New Cardiac Motion Video Data Resource for Medical Machine Learning这篇论文的代码。
-
qwen_vl_simple_api
- 2024-11-25 04:45:16访问
- 积分:1
-
Pretrained_Model_Alignment
- 2024-11-25 04:44:49访问
- 积分:1
-
system-metrics-monitor
- 2024-11-25 04:31:42访问
- 积分:1
-
Treat_Search
- 2024-11-25 04:25:03访问
- 积分:1
-
EmoTune
- 2024-11-25 04:17:33访问
- 积分:1
-
cookiecutter-streamlit-component
- 2024-11-25 04:16:55访问
- 积分:1
-
ustcDeadlinePusher
- 2024-11-25 04:11:26访问
- 积分:1
-
USTC-TK2016
- 2024-11-25 04:10:52访问
- 积分:1
-
Guess-number
- 2024-11-25 04:05:18访问
- 积分:1
-
PythonSmallGame
- 2024-11-25 04:04:40访问
- 积分:1
-
ICT
- 2024-11-25 03:50:08访问
- 积分:1
-
KeyTypeUploadFile
- 2024-11-25 03:45:32访问
- 积分:1
-
f-AnoGAN
- 2024-11-25 03:29:38访问
- 积分:1
-
fdroiddata
- 2024-11-25 03:29:11访问
- 积分:1
-
30-seconds-of-python
- 2024-11-25 03:21:14访问
- 积分:1
-
serve
- 2024-11-25 03:20:41访问
- 积分:1
-
PDPTW
- 2024-11-25 03:04:12访问
- 积分:1
-
PDpy
- 2024-11-25 03:03:44访问
- 积分:1
-
wdm-project-benchmark
- 2024-11-25 02:47:43访问
- 积分:1
-
wdm
- 2024-11-25 02:46:48访问
- 积分:1
-
imagenet_labels
- 2024-11-25 02:35:51访问
- 积分:1
访问申明(访问视为同意此申明)
2.部分网络用户分享TXT文件内容为网盘地址有可能会失效(此类多为视频教程,如发生失效情况【联系客服】自助退回)
3.请多看看评论和内容介绍大数据情况下资源并不能保证每一条都是完美的资源
4.是否访问均为用户自主行为,本站只提供搜索服务不提供技术支持,感谢您的支持