Created
January 17, 2020 17:11
-
-
Save wbecher/a2d1689cc9f5c377e509274a7b4d60ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Crie um algoritmo para geração de 200 imagens diferentes. | |
| # Todas com 256x256. Escolha uma imagem de sua preferência. | |
| # As transformações também são de sua escolha. | |
| # Etapa 1: implementar um algoritmo para o problema acima e | |
| # contabilizar o tempo de execução. | |
| # Além das vantagens apresentadas em aula, a biblioteca | |
| # imgaug permite tirar proveito dos múltiplos núcleos dos | |
| # processadores (CPU). | |
| # Etapa 2: alterar seu algoritmo criado na etapa 1 para tirar | |
| # proveito de seus núcleos, executando novamenteo as | |
| # trsnformações e calculando o tempo das mesmas. | |
| # Dica: caso estejas utilizando uma VM, possivelmente será | |
| # necessário aumentar o número de núcleos da mesma. | |
| # Instruções para rodar: | |
| # python3 -m venv venv | |
| # source venv/bin/activate | |
| # pip install six numpy scipy Pillow matplotlib scikit-image opencv-python imageio Shapely | |
| # pip install imgaug | |
| import imageio | |
| import imgaug as ia | |
| from imgaug import augmenters as iaa | |
| import numpy as np | |
| from imgaug.augmentables.batches import UnnormalizedBatch | |
| import time | |
| time_start = time.time() | |
| np.random.bit_generator = np.random._bit_generator # fix para funcionar na última versão do numpy | |
| # Number of batches and batch size for this example | |
| nb_batches = 4 | |
| batch_size = 50 | |
| image = imageio.imread( | |
| "https://upload.wikimedia.org/wikipedia/commons/thumb/8/86/Pedro_Tonon_Geromel.jpg/230px-Pedro_Tonon_Geromel.jpg") | |
| seq = iaa.Sequential([ | |
| iaa.Affine(rotate=(-25, 25)), | |
| iaa.AdditiveGaussianNoise(scale=(10, 60)), | |
| iaa.Crop(percent=(0, 0.2)), | |
| iaa.Invert(0.05, per_channel=True), | |
| iaa.Fliplr(0.5) | |
| ]) | |
| batches = [] | |
| for _ in range(nb_batches): | |
| batches.append(UnnormalizedBatch(images=[image] * batch_size)) | |
| time_end = time.time() | |
| print("Augmentation done in %.2fs" % (time_end - time_start,)) | |
| # Show the augmented images. | |
| # Note that augment_batches() returns a generator. | |
| for images_aug in seq.augment_batches(batches, background=True): | |
| ia.imshow(ia.draw_grid(images_aug.images_aug, cols=10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment