Skip to content
Snippets Groups Projects
speed_test_biovol.py 5.2 KiB
Newer Older
Filip Lux's avatar
Filip Lux committed
# coding: utf-8

Filip Lux's avatar
Filip Lux committed
# ============================================================================================= #
#  Author:       Samuel Šuľan, Lucia Hradecká, Filip Lux                                        #
#  Copyright:    Lucia Hradecká     : lucia.d.hradecka@gmail.com                                #
#                Filip Lux          : lux.filip@gmail.com                                       #
#                                                                                               #
#  MIT License.                                                                                 #
#                                                                                               #
#  Permission is hereby granted, free of charge, to any person obtaining a copy                 #
#  of this software and associated documentation files (the "Software"), to deal                #
#  in the Software without restriction, including without limitation the rights                 #
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell                    #
#  copies of the Software, and to permit persons to whom the Software is                        #
#  furnished to do so, subject to the following conditions:                                     #
#                                                                                               #
#  The above copyright notice and this permission notice shall be included in all               #
#  copies or substantial portions of the Software.                                              #
#                                                                                               #
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR                   #
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,                     #
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE                  #
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER                       #
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,                #
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE                #
#  SOFTWARE.                                                                                    #
# ============================================================================================= #

from src.core.composition import *
from src.augmentations import *
Filip Lux's avatar
Filip Lux committed


size_sample = [(1, 32, 32, 32, 1), (4, 32, 32, 32, 5), (4, 64, 64, 64, 5), (4, 128, 128, 128, 5)]
# size_sample = [(1, 32, 32, 32)]
Filip Lux's avatar
Filip Lux committed

num_repeat = 100
# num_repeat = 10
Filip Lux's avatar
Filip Lux committed

augmentations_to_check = [
    AffineTransform(angles=(22.5, 22.5, 22.5), translation=(20, 20, 20), scale=(0.8, 0.8, 0.8), p=1),
    RandomAffineTransform(angle_limit=(22.5, 22.5, 22.5), translation_limit=(20, 20, 20),
                          scaling_limit=(0.8, 0.8, 0.8), p=1),
    Scale(scales=(0.8, 1, 1.25), p=1),
    RandomScale(scaling_limit=(0.8, 1.25), p=1),
    Flip(p=1),
    RandomFlip(p=1),
    RandomRotate90(p=1),
    GaussianBlur(p=1),
    RandomGaussianBlur(p=1),
    GaussianNoise(p=1),
    PoissonNoise(p=1),
    HistogramEqualization(p=1),
    Normalize(p=1),
    NormalizeMeanStd(mean=0.1, std=1, p=1),
    RandomBrightnessContrast(p=0.1),
    RandomGamma(p=1),
Filip Lux's avatar
Filip Lux committed
]


Lucia D. Hradecka's avatar
Lucia D. Hradecka committed
def single_transform(iterations, size, augmentation):
    times = np.zeros(iterations)
Filip Lux's avatar
Filip Lux committed
    for i in range(iterations):
Filip Lux's avatar
Filip Lux committed
        test = np.random.uniform(low=0, high=1, size=size)
Lucia D. Hradecka's avatar
Lucia D. Hradecka committed
        aug = Compose(transforms=[augmentation], p=1)
Filip Lux's avatar
Filip Lux committed
        data = {'image': test}
Filip Lux's avatar
Filip Lux committed
        second_time = time.time()
        aug_data = aug(**data)
        time_spent = time.time() - second_time
        _ = aug_data['image'].shape
        times[i] = time_spent * 1000  # convert from seconds to milliseconds
    return times
Filip Lux's avatar
Filip Lux committed


def transformation_speed_benchmark(iterations):
    f = open(f'./runtime-{num_repeat}_iterations-biovol.txt', 'w')
Filip Lux's avatar
Filip Lux committed

    for i, augmentation in enumerate(augmentations_to_check):
Lucia D. Hradecka's avatar
Lucia D. Hradecka committed

        aug_name = augmentation.__class__.__name__
        print(aug_name)

Filip Lux's avatar
Filip Lux committed
        for size in size_sample:
            test_sample = np.random.uniform(low=0, high=1, size=size)
            test = test_sample.copy()

            aug = Compose(transforms=[augmentation], p=1)
            data = {'image': test}

            # run for the first time to allocate memory etc.
Filip Lux's avatar
Filip Lux committed
            first_time = time.time()
            aug_data = aug(**data)
            first_result = (time.time() - first_time) * 1000  # convert to milliseconds
            times_arr = single_transform(iterations, size, augmentation)

            # log
            log_message = f'Runtime in seconds. ' \
                          f'FirstRun: {first_result:.3f}, Average: {times_arr.mean():.3f}, ' \
                          f'Maximum: {times_arr.max():.3f}, Std: {times_arr.std():.3f}. ' \
                          f'(Transform: {aug_name}, Iterations: {iterations}, ImageSize: {size})\n'
Lucia D. Hradecka's avatar
Lucia D. Hradecka committed
            f.write(log_message)
            print(log_message)
Filip Lux's avatar
Filip Lux committed

    f.close()


if __name__ == '__main__':
Lucia D. Hradecka's avatar
Lucia D. Hradecka committed
    transformation_speed_benchmark(num_repeat)