Giter Site home page Giter Site logo

tella26 / augmentation-pytorch-transforms Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gatsby2016/augmentation-pytorch-transforms

0.0 0.0 0.0 11.16 MB

Image data augmentation on-the-fly by add new class on transforms in PyTorch and torchvision.

Python 100.00%

augmentation-pytorch-transforms's Introduction

Augmentation-PyTorch-Transforms

Image data augmentation on-the-fly by adding new class on transforms in PyTorch and torchvision.

Normally, we from torchvision import transforms for transformation, but some specific transformations (especially for histology image augmentation) are missing.

Thus, we add 4 new transforms class on the basic of torchvision.transforms pyfile, which we named as myTransforms.py.

You can call and use it in the same form as torchvision.transforms. Or, you can refer to dataAug_myTransforms.py.

Also, you can check the actual effect of myTransforms for data augmentation :)

New transforms classes included in myTransforms

HEDJitter

Randomly perturbe the HED color space value on an RGB pathological image[1].

  1. Disentangle the hematoxylin and eosin color channels by color deconvolution[2] method using a fixed matrix.
  2. Perturbe the hematoxylin, eosin and DAB stains independently.
  3. Transform the resulting stains into regular RGB color space.

Args

  • theta (float): How much to jitter HED color space,
  • then, alpha is chosen from a uniform distribution [1-theta, 1+theta]
  • betti is chosen from a uniform distribution [-theta, theta]
  • the jitter formula is $s' = \alpha * s + \betti$

Example

import myTransforms
imagename = '../data/10-05074_353_49_8178.png'
img = Image.open(imagename) # read the image
	
preprocess = myTransforms.HEDJitter(theta=0.05)
print(preprocess)
	
HEPerimg = preprocess(img)
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(HEPerimg)
plt.show()

HEDjitter

References
[1]. Tellez, D., Balkenhol, M., Otte-Höller, I., van de Loo, R., Vogels, R., Bult, P., ... & Litjens, G. (2018). Whole-slide mitosis detection in H&E breast histology using PHH3 as a reference to train distilled stain-invariant convolutional networks. IEEE transactions on medical imaging, 37(9), 2126-2136.
[2]. Ruifrok, A. C., & Johnston, D. A. (2001). Quantification of histochemical staining by color deconvolution. Analytical and quantitative cytology and histology, 23(4), 291-299.

RandomElastic

Random Elastic transformation by CV2 method on image by alpha, sigma parameter.
WARNING: This transform class will spend a lot of CPU time for preprocessing.

Args

  • alpha (float): alpha value for Elastic transformation, factor on dx, dy
    -- if alpha is 0, output is the same as origin, whatever the sigma;
    -- if alpha is 1, output only depends on sigma parameter;
    -- if alpha < 1 or > 1, it zoom in or out the sigma-relevant dx, dy.
  • sigma (float): sigma value for elastic transformation, should be $\in (0.05,0.1)$
  • mask (PIL Image) For processing on GroundTruth of segmentation task, if not assign, set None. only used in __call__ function

Example

import myTransforms
imagename = '../data/10-05074_353_49_8178.png'
img = Image.open(imagename) # read the image
	
preprocess = myTransforms.RandomElastic(alpha=2, sigma=0.06)
print(preprocess)
	
elasticimg = preprocess(img, mask=None)
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(elasticimg)
plt.show()

Elastic

References
affine and elastic transform
cv2.warpAffine
scipy.ndimage.map_coordinates

RandomAffineCV2

Random Affine transformation by CV2 method on image by alpha parameter.
It is different from torchvision.transforms.RandomAffine, which is implemented by PIL.Image method. We can set BORDER_REFLECT for the area outside the transform in the output image while original RandomAffine can only fill by a specified value.

Args

  • alpha (float): alpha value for affine transformation
  • mask (PIL Image) For processing on GroundTruth of segmentation task, if not assign, set None.

Example

import myTransforms
imagename = '../data/10-05074_353_49_8178.png'
img = Image.open(imagename) # read the image
	
preprocess = myTransforms.RandomAffineCV2(alpha=0.1)#alpha \in [0,0.15]
print(preprocess)
	
affinecvimg = preprocess(img)
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(affinecvimg)
plt.show()

RandomAffineCV2

RandomGaussBlur

Random Gauss Blurring on image by radius parameter. Args

  • radius (list, tuple): radius range for selecting from; you'd better set it < 2 especially for histopathological image task.

Example

import myTransforms
imagename = '../data/10-05074_353_49_8178.png'
img = Image.open(imagename) # read the image
	
preprocess = myTransforms.RandomGaussBlur(radius=[0.5, 1.5])
print(preprocess)
	
blurimg = preprocess(img)
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(blurimg)
plt.show()

GaussianBlur

AutoRandomRotation

change torchvision.transforms.RandomRotation for auto-random select angle from [0, 90, 180, 270] for rotating the image.

Example

import myTransforms
imagename = '../data/10-05074_353_49_8178.png'
img = Image.open(imagename) # read the image
	
preprocess = myTransforms.AutoRandomRotation()
print(preprocess)
	
rotateimg = preprocess(img)
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(rotateimg)
plt.show()

augmentation-pytorch-transforms's People

Contributors

gatsby2016 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.