简体   繁体   中英

Why am I generating the same number using random.randint?

so I have this function to generate image augmentation for semantic segmentation, and I was hoping to get random image with corresponding image mask.

i = 1  # variable to iterate till images_to_generate

while i <= images_to_generate:
    number = random.randint(0, len(images))  # PIck a number to select an image & mask
    image = images[number]
    mask = masks[number]
    # print(image, mask)
    # image=random.choice(images) #Randomly select an image name
    original_image = io.imread(image)
    original_mask = io.imread(mask)
    transformed_image = None
    transformed_mask = None
    #     print(i)
    n = 0  # variable to iterate till number of transformation to apply
    transformation_count = random.randint(0, len(transformations))  # choose random number of transformation to apply on the image
    print(number)
    print(transformation_count)
    while n <= transformation_count:
        key = random.choice(list(transformations))  # randomly choosing method to call
        seed = random.randint(1,100)  # Generate seed to supply transformation functions.
        transformed_image = transformations[key](original_image, seed)
        transformed_mask = transformations[key](original_mask, seed)
        n = n + 1

    new_image_path = "%s/augmented_image_%s.jpg" % (img_augmented_path, i)
    new_mask_path = "%s/augmented_mask_%s.png" % (msk_augmented_path, i)  # Do not save as JPG
    io.imsave(new_image_path, transformed_image)
    io.imsave(new_mask_path, transformed_mask)
    i = i + 1

but I'm getting the same image number with the same transformation all the time

why does this happen?

this is because of your seed = random.seed(1,100) which is responsible for customizing the start number of the random generator. As a consequence you will always get the first same "random" number.

See full documentation here: https://www.w3schools.com/python/ref_random_seed.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM