进展---一些笔记

把OCMR的fs的shape写进csv文件

import numpy as np
import math
from ismrmrdtools import transform
import ocmr_unet.read_ocmr as read
import os

files = sorted(list(pathlib.Path('/home/ilkay/Documents/ruru/pg_mri/datasets/ocmr/train/').iterdir()))
out_list = []
for fname in sorted(files):
    kData, _ = read.read_ocmr(fname)
    dim_kData = kData.shape;
    CH = dim_kData[3];
    SLC = dim_kData[6]
    kData_tmp = np.mean(kData, axis=8)  # average the k-space if average > 1

    im_coil = transform.transform_kspace_to_image(kData_tmp, [0, 1])  # IFFT (2D image)
    im_sos = np.sqrt(np.sum(np.abs(im_coil) ** 2, 3))  # Sum of Square
    # print('Dimension of Image (with ReadOut ovesampling): ', im_sos.shape)
    RO = im_sos.shape[0]
    image = im_sos[math.floor(RO / 4):math.floor(RO / 4 * 3), :, :]  # Remove RO oversampling
    # print('Dimension of Image (without ReadOout ovesampling): ', image.shape)
    print(f'{os.path.basename(fname)}    {kData.shape}         {image.shape}')
    out_list.append([os.path.basename(fname), kData.shape, image.shape])
with open("save_file.csv", "w", newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(["file", "kData", "fully-sampled image"])   # 先写入columns_name
    writer.writerows(out_list) 

dice loss

def dice_loss(predict, gt_mask):
    predict = predict.flatten()
    gt_mask = gt_mask.flatten()
    assert (predict.shape == gt_mask.shape)

    intersection = 2.0 * (predict * gt_mask).sum()
    union = (predict * predict).sum() + (gt_mask * gt_mask).sum()
    dice_coefficient = intersection / union
    loss = 1.0 - dice_coefficient
    return loss

你可能感兴趣的:(MR图像重建,深度学习,python,机器学习)