Reputation: 25
When we use Transforms from torchvision or albumentations we have functions like Random Crop and Random Brightness Contrast that are applied to produce augmented images. Is there any possible way to store the exact transformations applied to an image to get the corresponding image?
Upvotes: 1
Views: 178
Reputation: 901
Yes, there is, but it will require a little extra work on your part. Most of the random transformation classes have a get_params
method (or _get_params
if using the newer v2
namespace) which generates the random parameters for the augmentation, which are then passed to the functional transformations along with the image.
If you want to store the parameters for each transformation during training, the easiest way to go about it would probably be to subclass the Transform, and add a log the values of interest. A minimal example using the old API:
import logging
import torchvision
import torchvision.transforms.functional as F
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class MyRandomCrop(torchvision.transforms.RandomCrop):
def forward(self, img):
i, j, h, w = self.get_params(img, self.size)
logger.info("Cropping image with parameters %d %d %d %d", i, j, h, w)
return F.crop(img, i, j, h, w)
image = torchvision.io.read_image("/path/to/image.jpg")
transform = MyRandomCrop(size=(100, 100))
image = transform(image)
>>> INFO:__main__:Cropping image with parameters 789 769 100 100
Upvotes: 0