医学图像预处理----RandomFlip(随机翻转)

class RandomFlip(object):
   """
   Randomly Flip image by user specified axes
   """

   def __init__(self, axes):
      self.name = 'Flip'
      assert len(axes)>0 and len(axes)<=3
      self.axes = axes

   def __call__(self, sample):
      image, label = sample['image'], sample['label']

      flip = np.random.randint(2, size=1)[0]
      if flip:
         flipFilter = sitk.FlipImageFilter()
         for image_channel in range(len(image)):
            flipFilter.SetFlipAxes(self.axes)
            image[image_channel] = flipFilter.Execute(image[image_channel])
         label = flipFilter.Execute(label)

      return {'image': image, 'label': label}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本文将介绍如何使用 MMDetection 框架来实现校园行人实例分割,基于 Mask-RCNN 算法。MMDetection 是一个基于 PyTorch 的开源目标检测框架,它提供了丰富的预训练模型和方便的工具。 #### 1. 安装 MMDetection MMDetection 的安装可以参考官方文档,这里不再赘述。需要注意的是,MMDetection 的安装需要先安装 PyTorch。 #### 2. 数据集准备 我们使用校园行人数据集作为示例数据集,可以从 [这里](https://drive.google.com/drive/folders/1vztfS4Q1XaBxNjNp9fVl9d5jv1iO3mfc) 下载。该数据集包含了 160 张校园场景图片,其中包含了行人的实例分割标注。 将数据集下载后解压,将图片和标注文件放在同一目录下。标注文件是 `.json` 格式的,可以使用 Python 的 `json` 库读取。下面是读取标注文件的示例代码: ```python import json with open('annotations.json', 'r') as f: annotations = json.load(f) ``` #### 3. 数据集转换 MMDetection 使用 COCO 格式的数据集,因此需要将我们的数据集转换成 COCO 格式。MMDetection 提供了一个命令行工具 `tools/convert_datasets.py`,可以将常见数据集格式转换成 COCO 格式。这里我们需要将我们的数据集转换成 COCO 格式。 ```bash python tools/convert_datasets.py --input_type pedestrian --output_type coco --input_folder path/to/images --output_folder path/to/annotations --json_file path/to/annotations.json ``` 其中 `--input_type` 指定输入数据集的格式,这里是 `pedestrian`,表示我们的数据集是校园行人数据集。`--output_type` 指定输出数据集的格式,这里是 `coco`。`--input_folder` 指定图片所在的文件夹,`--output_folder` 指定标注文件所在的文件夹,`--json_file` 指定标注文件的路径。 转换完成后,在 `path/to/annotations` 文件夹下会生成 COCO 格式的标注文件 `annotations.json` 和 COCO 格式的图片文件夹 `images`。 #### 4. 配置文件 MMDetection 使用配置文件来指定模型、数据集、训练参数等。在 `configs` 文件夹下有各种预定义的配置文件,我们可以在这些配置文件的基础上进行修改。 这里我们使用 `configs/mask_rcnn/mask_rcnn_r50_fpn_1x_coco.py` 作为基础配置文件。这个配置文件定义了 Mask-RCNN 算法在 COCO 数据集上的训练参数和模型结构。我们需要修改其中的数据集路径和类别数。 ```python # 数据集相关配置 dataset_type = 'CocoDataset' data_root = 'path/to/annotations/' img_norm_cfg = dict( mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True) train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations', with_bbox=True, with_mask=True), dict(type='Resize', img_scale=(1333, 800), keep_ratio=True), dict(type='RandomFlip', flip_ratio=0.5), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size_divisor=32), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks']), ] test_pipeline = [ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(1333, 800), flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict(type='RandomFlip'), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size_divisor=32), dict(type='ImageToTensor', keys=['img']), dict(type='Collect', keys=['img']), ]) ] data = dict( samples_per_gpu=2, workers_per_gpu=2, train=dict( type=dataset_type, ann_file=data_root + 'annotations.json', img_prefix=data_root + 'images/', pipeline=train_pipeline), val=dict( type=dataset_type, ann_file=data_root + 'annotations.json', img_prefix=data_root + 'images/', pipeline=test_pipeline), test=dict( type=dataset_type, ann_file=data_root + 'annotations.json', img_prefix=data_root + 'images/', pipeline=test_pipeline)) evaluation = dict(metric=['bbox', 'segm']) # 类别数 num_classes = 1 ``` 其中 `data_root` 指定数据集所在的根目录,这里是 COCO 格式的标注文件和图片所在的文件夹。`num_classes` 指定类别数,这里只有一类行人。 #### 5. 训练模型 使用以下命令开始训练模型: ```bash python tools/train.py configs/mask_rcnn/mask_rcnn_r50_fpn_1x_coco.py ``` 训练完成后,会在 `work_dirs` 文件夹下生成训练好的模型。 #### 6. 模型测试 使用以下命令测试模型: ```bash python tools/test.py configs/mask_rcnn/mask_rcnn_r50_fpn_1x_coco.py work_dirs/mask_rcnn_r50_fpn_1x_coco/latest.pth --out result.pkl ``` 测试完成后,会在当前目录下生成 `result.pkl` 文件,其中包含了测试结果。 #### 7. 结果可视化 可以使用以下代码将测试结果可视化: ```python import pickle import cv2 import numpy as np from mmdet.apis import init_detector, inference_detector # 加载模型 config_file = 'configs/mask_rcnn/mask_rcnn_r50_fpn_1x_coco.py' checkpoint_file = 'work_dirs/mask_rcnn_r50_fpn_1x_coco/latest.pth' model = init_detector(config_file, checkpoint_file) # 加载测试结果 with open('result.pkl', 'rb') as f: results = pickle.load(f) # 可视化结果 for i, result in enumerate(results): img = cv2.imread(f'path/to/images/{i}.jpg') masks = result[0] for j, mask in enumerate(masks): mask = (mask > 0).astype(np.uint8) contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(img, contours, -1, (0, 0, 255), 2) cv2.imshow('result', img) cv2.waitKey() ``` 其中 `result` 是测试结果,是一个列表,每个元素是一个长度为 2 的元组,包含了行人的实例分割结果和检测结果。`img` 是原始图片。将实例分割结果可视化后可以得到以下效果图: ![result](https://img-blog.csdn.net/20180930153107224?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RldmVsb3BtZW50/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/75)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值