dsb_utils.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:diagnose-heart 作者: woshialex 项目源码 文件源码
def segmenter_data_transform(imb, rotate=None, normalize_pctwise=False):
    if isinstance(imb, tuple) and len(imb) == 2:
        imgs,labels = imb
    else:
        imgs = imb
    # rotate image if training
    if rotate is not None:
        for i in xrange(imgs.shape[0]):
            degrees = float(np.random.randint(rotate[0], rotate[1])) if \
                    isinstance(rotate, tuple) else rotate
            imgs[i,0] = scipy.misc.imrotate(imgs[i,0], degrees, interp='bilinear')
            if isinstance(imb, tuple):
                labels[i,0] = scipy.misc.imrotate(labels[i,0], degrees, interp='bilinear')
    # assume they are square
    sz = c.fcn_img_size
    x,y = np.random.randint(0,imgs.shape[2]-sz,2) if imgs.shape[2] > sz else (0,0)
    imgs = nn.utils.floatX(imgs[:,:, x:x+sz, y:y+sz])/255.
    if not normalize_pctwise:
        pad = imgs.shape[2] // 5
        cut = imgs[:,0,pad:-pad,pad:-pad]
        mu = cut.mean(axis=(1,2)).reshape(imgs.shape[0],1,1,1)
        sigma = cut.std(axis=(1,2)).reshape(imgs.shape[0],1,1,1)
        imgs = (imgs - mu) / sigma
        imgs = np.minimum(3, np.maximum(-3, imgs))
    else:
        pclow, pchigh = normalize_pctwise if isinstance(normalize_pctwise, tuple) else (20,70)
        for i in xrange(imgs.shape[0]):
            pl,ph = np.percentile(imgs[i],(pclow, pchigh))
            imgs[i] = exposure.rescale_intensity(imgs[i], in_range=(pl, ph));
            imgs[i] = 2*imgs[i]/imgs[i].max() - 1.
        # or other rescaling here to approximate ~ N(0,1)
    if isinstance(imb, tuple):
        labels = nn.utils.floatX(labels[:,:, x:x+sz, y:y+sz])
        return imgs, labels
    return imgs
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号