semvec/pypi-packages huggingface dataset:
ImageAug 0.1.0.post0
Image augmentation for PyTorch

# ImageAug
Image augmentation for PyTorch

* Apply random cropped rotations without going out of image bounds
* Convert RGB to YUV color space
* Adjust brightness and contrast, and more

![Example](https://i.imgur.com/lyINe9Z.gif "Example")

[Artwork](https://twitter.com/hcnone/status/1085740161600651269) by @[hcnone](https://twitter.com/hcnone)

## Quick Start

The transformations are designed to be chained together using `torchvision.transforms.Compose`. Additionally, there is a functional module. Functional transforms give more fine-grained control if you have to build a more complex transformation pipeline.

### Install

```sh
pip3 install -r requirements.txt
python3 setup.py install
```
**Requirements:**

* Pillow
* torchvision
* numpy

### Example

```python
from torchvision.transforms import ToTensor, ToPILImage, Compose
from PIL import Image
from imageaug.transforms import Colorspace, RandomAdjustment, RandomRotatedCrop

image_filename = 'test.png'
img = Image.open(image_filename, 'r').convert("RGB")

crop_size = (64, 64)
angle_std = 90 # in degrees
# Note: apply color adjustments before a random rotated crop so that so that the
#       fillcolor for out of bounds is not randomly adjusted (this only applies
#       if you have images smaller than the crop size)
transform = Compose([
    # convert PIL Image to Tensor
    ToTensor(),
    # convert RGB to YUV colorspace
    Colorspace("rgb", "yuv"),
    # randomly adjust the brightness and contrast of channel 0 (Y: luminance)
    RandomAdjustment(0, 0.1, 0.1, rgb=False),
    # randomly adjust the contrast of channel 1 and 2 (UV: color channels)
    RandomAdjustment((1,2), 0, 0.38, rgb=False),
    # convert YUV to RGB colorspace
    Colorspace("yuv", "rgb"),
    # convert Tensor back to PIL Image
    ToPILImage(),
    # random rotated crop
    RandomRotatedCrop(crop_size, 0.0, angle_std, downscale=0.5)
])
out = transform(img)
out.save("out.png")
```

## Current Features
* Rotate and crop images within the bounds of the original image for any given degree of angle perturbation (for training samples with rotational noise)
* Convert images to and from RGB/YUV/YCH colorspace with alpha channel support
* Adjust contrast and brightness of channels
* Noise occulsion

## To-do

This project is still a work in progress.

* Uniform distribution for RandomRotatedCrop
* Color lookup table for faster conversions between colorspaces
* Add image, text, shape, and pixelation occulsions

## Project Page
Github: [https://github.com/paixi/ImageAug](https://github.com/paixi/ImageAug)
autograd-lib 0.0.7
Library to simplify autograd computations in PyTorch
Requires Python: >= 3.6

# autograd_lib

By Yaroslav Bulatov, Kazuki Osawa

Library to simplify gradient computations in PyTorch.

# example 1: per-example gradient norms

Example of using it to compute per-example gradient norms for linear layers, using trick from https://arxiv.org/abs/1510.01799

See `example_norms.py` for a runnable example. The important parts:


```
!pip install autograd-lib

from autograd_lib import autograd_lib

loss_fn = ...
data = ...
model = ...
autograd_lib.register(model)


activations = {}

def save_activations(layer, A, _):
    activations[layer] = A

with autograd_lib.module_hook(save_activations):
    output = model(data)
    loss = loss_fn(output)

norms = [torch.zeros(n)]

def per_example_norms(layer, _, B):
    A = activations[layer]
    norms[0]+=(A*A).sum(dim=1)*(B*B).sum(dim=1)

with autograd_lib.module_hook(per_example_norms):
    loss.backward()

print('per-example gradient norms squared:', norms[0])

```

# Example 2: Hessian quantities

To compute exact Hessian, Hessian diagonal and KFAC approximation for all linear layers of a ReLU network in a single pass.

See `example_hessian.py` for a self-contained example. The important parts:


```
!pip install autograd-lib

autograd_lib.register(model)

hess = defaultdict(float)
hess_diag = defaultdict(float)
hess_kfac = defaultdict(lambda: AttrDefault(float))

activations = {}
def save_activations(layer, A, _):
    activations[layer] = A

    # KFAC left factor
    hess_kfac[layer].AA += torch.einsum("ni,nj->ij", A, A)

with autograd_lib.module_hook(save_activations):
    output = model(data)
    loss = loss_fn(output, targets)

def compute_hess(layer, _, B):
    A = activations[layer]
    BA = torch.einsum("nl,ni->nli", B, A)

    # full Hessian
    hess[layer] += torch.einsum('nli,nkj->likj', BA, BA)

    # Hessian diagonal
    hess_diag[layer] += torch.einsum("ni,nj->ij", B * B, A * A)

    # KFAC right factor
    hess_kfac[layer].BB += torch.einsum("ni,nj->ij", B, B)


with autograd_lib.module_hook(compute_hess):
    autograd_lib.backward_hessian(output, loss='CrossEntropy')
```

Variations:

- `autograd_lib.backward_hessian` for Hessian
- `autograd_lib.backward_jacobian` for Jacobian squared
- `loss.backward()` for empirical Fisher Information Matrix


See autograd_lib_test.py for correctness checks against PyTorch autograd.