0

I have a logo. What I am doing now is I am setting probability weights for all of these functions and the logo goes to only one of them at a time depending on its probability value. This is my code:

augmentation_func = [(random_rotation, [brand_logo]),
                             (blurring, [brand_logo]),
                             (compression, [brand_logo]),
                             (portion_covering, [brand_logo, brand_logo_width, brand_logo_height]),
                             (perspective_transform, [logo_image, brand_logo_width, brand_logo_height]),
                             (overlaying, [logo_path]),
                             (logo_background, [logo_path])]

        (augs, args), = random.choices(augmentation_func, weights=[10,5,15,20,20,15,15])

        new_logo = random_resize(augs(*args), gameplay)

However, this isn't sufficient. The logo should be able to go through multiple functions. For example. A logo could potentially go through random_rotation(), perspective_transformation() and overlaying(). Whether it will go through a function or not should depend on its probability weight. This is my idea:

enter image description here

How can I adjust my code so that the logo goes through many transformation functions depending on the weights?

Onur-Andros Ozbek
  • 2,998
  • 2
  • 29
  • 78

1 Answers1

1

Is the chance that each operation occurs independent of the others? Then random.choice() is not the correct solution, since it chooses only one item. Rather, for each item, choose it with its corresponding probability. For example:

if random.randrange(0, 100) < 10:
    # Crop the image (in place) at a 10% chance.
    # The chance of cropping is independent
    # of the chances of any other operation.
    brand_logo = cropping(brand_logo)
    
if random.randrange(0, 100) < 15:
    # Rotate the image (in place) at a 15% chance.
    # The chance of rotating is independent
    # of the chances of any other operation.
    brand_logo = rotating(brand_logo)
Peter O.
  • 32,158
  • 14
  • 82
  • 96
  • So in your logic, you are taking the original image and doing both cropping and rotating. What I need is first do cropping (if probability permits) and then do rotating on the cropped image (if probability permits) – Onur-Andros Ozbek Jan 19 '21 at 05:01
  • That is how the example works: the `cropping()` call is done at a 10% chance, and then, independently, the `rotating()` call is done at a 15% chance. I will make this answer clearer. – Peter O. Jan 19 '21 at 05:03
  • So if cropping happens, does the output of cropping get sent to rotating? – Onur-Andros Ozbek Jan 19 '21 at 05:06
  • Yes it does. This is not necessarily clear from the example, but assume the code modifies `brand_logo` in place. – Peter O. Jan 19 '21 at 05:06
  • It would do so if you reassigned `brand_logo = cropping(brand_logo)` like this.. – Onur-Andros Ozbek Jan 19 '21 at 05:12
  • Edited. However, my main point is that you should "roll" for each operation separately, rather than "draw one of them out of a hat", so to speak. – Peter O. Jan 19 '21 at 05:13