Gradient masks

If you want to folllow along on google colab, check out this notebookarrow-up-right.

Torch dreams can be used to apply masks on different custom losses to generate beautiful blends of channels/layers. It's actually easier done than said.

import cv2
import numpy as np
import matplotlib.pyplot as plt
from torch_dreams.dreamer import dreamer
import torchvision.models as models

First off, we genrate some random noise to work on. Feel free to use any other image in your case.

Then, we generate the masks to be used using numpy.

If things go right, you'll end up with these masks.

Then, we write a helper function to conveniently generate custom loss functions to select certain channels. More on this later.

Now that we're done making the masks, let's fire up out torchdreams.dreamer on the inception_v3 and select a layer to work on. Feel free to choose any other layer(s).

Now, we generate the custom loss functions required to optimize various chanels, in my case, I only used one layer.

Then we set up the config

All that's left now is to run the optimization (this might take a while depending on the image size)

Last updated