def segmenter_data_transform(imb, shift=0, rotate=0, scale=0, normalize_pctwise=(20,95), istest=False):
if isinstance(imb, tuple) and len(imb) == 2:
imgs,labels = imb
else:
imgs = imb
# rotate image if training
if rotate>0:
for i in xrange(imgs.shape[0]):
degrees = rotate if istest else np.clip(np.random.normal(),-2,2)*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')
#rescale
if scale>0:
assert(scale>0 and scale<=0.5);
for i in xrange(imgs.shape[0]):
sc = 1 + (scale if istest else np.clip(np.random.normal(),-2,2)*scale);
imgs[i,0] = rescale(imgs[i,0],sc);
if isinstance(imb, tuple):
labels[i,0] = rescale(labels[i,0], sc);
#shift
if shift>0 and not istest:
for i in xrange(imgs.shape[0]):
x,y = np.random.randint(-shift,shift,2);
imgs[i,0] = img_shift(imgs[i,0], (x,y));
if isinstance(imb, tuple):
labels[i,0] = img_shift(labels[i,0], (x,y));
imgs = nn.utils.floatX(imgs)/255.0;
for i in xrange(imgs.shape[0]):
pclow, pchigh = normalize_pctwise
if isinstance(pclow,tuple):
pclow = np.random.randint(pclow[0],pclow[1]);
pchigh = np.random.randint(pchigh[0],pchigh[1]);
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.
if isinstance(imb,tuple):
labels = nn.utils.floatX(labels)/255.0;
return imgs,labels
else:
return imgs;
评论列表
文章目录