How to log images to Neptune?

Problem

I generate model predictions after every epoch. How can I log them as images to Neptune?

Solution

Step 1

Create Neptune Context object:

1
2
3
import neptune

ctx = neptune.Context()

Step 2

Create an image that you want to log. For example:

1
2
3
4
5
import imgaug as ia
from imgaug import augmenters as iaa
import numpy as np

img = ia.quokka()

Step 3

Convert your image to PIL

1
2
3
from PIL import Image

img_pil = Image.fromarray(img)

Step 4

Create neptune.Image object

1
2
3
img_neptune = neptune.Image(name='quokka',
                            description='nice little fellow',
                            data=img_pil)

Step 5

Log image to Neptune:

1
ctx.channel_send('raw_image', img_neptune)

image

Step 5

You can log images in a loop. For example, you can augment your image and log it to Neptune:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
aug_seq = iaa.Affine(scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
                     translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
                     rotate=(-25, 25),
                     )

for run in range(20):
    img_aug= aug_seq.augment_image(img)
    img_pil_aug = Image.fromarray(img_aug)
    img_aug_neptune = neptune.Image(name='quokka_version_{}'.format(run),
                                           description='augmented little fellow (still nice)',
                                           data=img_pil_aug)
    ctx.channel_send('augmented image', img_aug_neptune)

image

See also