barisozmen / deepaugment

Discover augmentation strategies tailored for your dataset
MIT License
244 stars 41 forks source link

AssertionError when do augment policy #30

Open monkeyDemon opened 4 years ago

monkeyDemon commented 4 years ago

I write a simple script like this:

import os
from deepaugment import DeepAugment
from keras.datasets import cifar10

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
deepaug = DeepAugment(x_train, y_train)
best_policies = deepaug.optimize(300)

after run it, about one minute, I got a AssertionError:

...
...
Epoch 48/50
 - 1s - loss: 0.2101 - acc: 0.9382 - val_loss: 2.6119 - val_acc: 0.5540
Epoch 49/50
 - 1s - loss: 0.2101 - acc: 0.9347 - val_loss: 2.7725 - val_acc: 0.5430
Epoch 50/50
 - 1s - loss: 0.2075 - acc: 0.9388 - val_loss: 1.9880 - val_acc: 0.5510
fit()'s runtime:  55.3912 sec.
0, 0.567111111190584, ['rotate', 0.0, 'rotate', 0.0, 'rotate', 0.0, 'rotate', 0.0, 'rotate', 0.0, 'rotate', 0.0, 'rotate', 0.0, 'rotate', 0.0, 'rotate', 0.0, 'rotate', 0.0]
('trial:', 1, '\n', ['gamma-contrast', 0.8442657485810175, 'coarse-salt-pepper', 0.8472517387841256, 'brighten', 0.38438170729269994, 'translate-y', 0.056712977317443194, 'translate-y', 0.47766511732135, 'add-to-hue-and-saturation', 0.47997717237505744, 'emboss', 0.8360787635373778, 'sharpen', 0.6481718720511973, 'emboss', 0.9571551589530466, 'rotate', 0.8700872583584366])
Traceback (most recent call last):
  File "test.py", line 29, in <module>
    best_policies = deepaug.optimize(300)
  File "/data/ansheng/cv_strategy/autoML/deep_augment/deepaugment-master/deepaugment/deepaugment.py", line 151, in optimize
    f_val = self.objective_func.evaluate(trial_no, trial_hyperparams)
  File "/data/ansheng/cv_strategy/autoML/deep_augment/deepaugment-master/deepaugment/objective.py", line 44, in evaluate
    self.data["X_train"], self.data["y_train"], *trial_hyperparams
  File "/data/ansheng/cv_strategy/autoML/deep_augment/deepaugment-master/deepaugment/augmenter.py", line 166, in augment_by_policy
    ), "first transform is unvalid"
AssertionError: first transform is unvalid

The code that throw Error is:

X_portion_aug = transform(hyperparams[i], hyperparams[i+1], X_portion)  # first transform
assert (
    X_portion_aug.min() >= -0.1 and X_portion_aug.max() <= 255.1
), "first transform is unvalid"

It seems the code after data-augmentation is out of range [0,255].

So if the function augment_by_policy() in augmenter.py has some bug?

monkeyDemon commented 4 years ago

I find that this error is cause by the code located at 65-70 lines of augmenter.py in function transform().

elif aug_type == "brighten":
    X_aug = iaa.Add(
        (int(-40 * magnitude), int(40 * magnitude)), per_channel=0.5
    ).augment_images(
        X
    )  # brighten

The use of iaa.Add will cause X_aug out of the range [0,255], just add one line can fix the problem.

elif aug_type == "brighten":
    X_aug = iaa.Add(
        (int(-40 * magnitude), int(40 * magnitude)), per_channel=0.5
    ).augment_images(
        X
    )  # brighten
    np.clip(X_aug, 0, 255, out=X_aug)

Now my puzzle is that the transform() is a very important function, and if it has a bug, the whole program does not work properly,so the author won't miss it. So why did I encounter this mistake? Does it matter if I use Python 2 but not Python3?

HugoDel commented 4 years ago

I also encounter the same error with TF 1.13.1, python 3.5 (after patching all f-string formating), imgaug 0.3.0 (latest version at this time). I apply your patch but I got another error that I will try to fix soon.

ValueError: Got dtype 'float32', which is a forbidden dtype (bool, uint16, uint32, uint64, uint128, uint256, int32, int64, int128, int256, float16, float32, float64, float96, float128, float256).

Line 108 of augmenter.py : .AddToHueAndSaturation

HugoDel commented 4 years ago

It seems line 133 of augmenter.py, when we call _X = denormalize(X), X isn't normalized ! In fact, it has been dived by 255 twice. Try to do : (X 255) 255 and we will get the data in 0-255 scale.