vendredi 24 janvier 2020

How to rotate tensor image randomly

I want to rotate my images in parallel, using 'map', in the preprocessing stage.

The problem is that every image is rotated for the same direction (after one random number generated). But I want that each image will have different degree of rotation.

This is my code:

import tensorflow_addons as tfa
import math
import random
def rotate_tensor(image, label):
    degree = random.random()*360
    image = tfa.image.rotate(image, degree * math.pi / 180, interpolation='BILINEAR')
    return image, label

rotated_test_set = rps_test_raw.map(rotate_tensor).batch(batch_size).prefetch(1)

I tried to change the seed every call for the function:

import tensorflow_addons as tfa
import math
import random
seed_num = 0
def rotate_tensor(image, label):
    seed_num += 1
    random.seed(seed_num)
    degree = random.random()*360
    image = tfa.image.rotate(image, degree * math.pi / 180, interpolation='BILINEAR')
    return image, label

rotated_test_set = rps_test_raw.map(rotate_tensor).batch(batch_size).prefetch(1)

But I get:

UnboundLocalError: local variable 'seed_num' referenced before assignment

I use tf2, but I don't think it matter much (beside the code to rotate the image).




Aucun commentaire:

Enregistrer un commentaire