Create Augmented Data Using Albumentations Library in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to colorific in this video we are going to see how we can create augmented data using albumentation library in python albumation library comes with a lot of augmented function which was basically developed as a computer vision tool to enhance the size of original data that could be helpful for deep convolution neural network performances if you use kerala you might know that there are already built-in functions that help you create augmented data but i really prefer to prepare my data of images and keep it aside using augmented libraries instead of using keras built-in functions so let's start programming before i take you to my spider editor of python let me show you first that i have already created first unique 10 images of amnest dataset and i have done a video on how we can read and write amnest data set from tensorflow collection of data sets i have placed a link to this video i prefer you to watch that video before this one so you can see inside my input image path i have my 10 images of administrator set starting from 0 to 9 i am going to augment them using albumentation library so as you can see in my spider editor i have already imported imported libraries that i am going to use in this video one of which is number in most of the cases we have to use number functions this is very handy and the next library i'm going to use is to read and write images using image i o remember that here i have not used opencv function because uh opencv reads an image by default in vgr color channel and if you don't tackle it carefully you might be getting up messed with the color channels and to read and write directly listing i am going to use the list dire function of library os this is our unique program that i'm going to make this video about to install albumentations library you can use this command which is vip install dash capital u and albumentation this is the name of this library and once you finish installing it you can import it by import implementations and keeping it alice as a so instead of using its complete name which is lengthy we can call every function or every augment library simply using a next i am going to read my input images that i shown you before there are 10 amnesty images starting from zero digit to nine paths to my input is specified inside input underscore path next i am going to specify the output path where i will be saving the augmented images over the input images and to read images for my input directory i am going to call list dial function which takes parameter of input path now because each input image is named as digits so i am going to simply sort them based on their names now i am going to run my program written so far you can see in my variable explorer that the input images have been uploaded inside input underscore images variable and the size of this list is done if i click on its values you can see that the name of each image is picked up correctly before i start calling the augment functions for augmentations library let me take you first to its documentation so you can see that the home page is authentications.ai and from here i am going to documentation you can see that for every purpose albumentation offers you a unique set of library so that you can create your own augmented data set from black role menu if you scroll down a little bit and search for api reference click on albumentations and then click on experimental transforms you will be taken to all the possible libraries and from here you can pick up a specific name so for example the first name of augment function is flip symmetric key points which will help you create the augmentation by flipping the input horizontally around the y-axis if you keep scrolling down you will find all of the possible transforms and if you go back scroll down in left menu skip the first api reference and jump to api references here you will see all possible augment libraries for example this library is related to crop transform these transforms we have this this is basic transforms crop transform geometric transforms and if we go to the first transforms you can see that the most basic augment functions are placed here so the purpose of showing you this documentation is by looking it carefully you can find the required library of augmentation now that i have shown you how we can know correct name of augment augmentation lab augmentation function now let's write a code to see how we can use these functions so using the alias a i am going to call the first augmentation function horizontal flip and its first parameter is p it is default parameter and it represents the probability of applying the transform and you can mention its value between 0 and one if the value is smaller towards zero it means the minor change will be applied but if you keep it one the strong change will be applied based on the augmentation function so remember that here we only are creating a function of albumentation by passing its probability and this will return a transformation function of argumentation and then using this return function we can transform our input images how we can do that let's see so because my input images are done i am going to create a loop that will run through my 10 input images through iterator i and for each iteration i am going to print the number of iteration to see the progress of a loop next i am going to create a complete part to my input image by concatenating the complete path of input image with the name of image that will be inside our iterator i here i am going to create a user defined function named as read underscore image which will take my complete path of input image including its name and this function will return an array of my input image so image will be the variable holding the output image returned from this function so first i am going to define this function that will take complete url of input image next using image i o dot i am read function i am going to read this image and put it inside image variable finally i am going to return this image already i have defined my reading input image function next i am going to augment the input image and to do that again i am going to create another user defined function named as augment underscore and underscore button and this function will take two parameters the first parameter will be the transformation function or augmentation function that i have just created and inside it the transformation will be horizontal flip the second parameter is the input image on which the augmentation will be performed and this function will retain the augmented image which will be saved inside a image variable so let's so let's define this function so i'm going to keep the parameters name same as the names that are used in column of the function so here is the way that we can perform augmentation using our transform function as i told you before that this transformation only returns the augmentation function so here we are calling the augmentation function which takes only one parameter the name of parameter is image and its value is image which is the name of input image array and the type of output should be an image and to specify that we have to mention it as an array of element image in this function returns an augmented image and to hold that image we have created another variable which is a img this is only one line that we can use to perform augmentation on our input image finally we are going to do it on our augmented image so now that we have created our augmented image next we are going to write it or save it in our output directory and to do that i am going to create another user defined function named as save underscore augmented underscore image and this function will take three parameters the first parameter will be the augmented image array second parameter will be the index of image array so that the output path is concatenated according to the name of image and the final parameter is the destination or the complete path of my output directory next i am going to define this function first parameter is the same name second parameter is the name of image last parameter is the path to output directory by concatenating the output directory with the name of image i have created the complete path of my output next using image io.i am write function i am going to write this augmented image inside my prepared destination which is uri now i am going to run my program written so far so you can see that i have printed information over each iteration and save augmented image now let me take you to my output directory so i am inside my output directory which is augmented images and you can see that i have successfully generated horizontally versions of my input image for example this is two and if i open and you can see each of the digit is horizontally flipped but in the case where i want to use more than one augmentation function how can i do that yes of course we can do that and let's see how for example in this program i have decided to use more than one augmentation functions which are horizontal flip vertical flip and gaussian noise and the method of using all of the other augmentation function inside implementations library is the same i used horizontal flip next based on these augmentation functions i am going to create an array so that i can create a loop over each augmentation function to be applied on each input image so i am going to create a dictionary named as list a g f i'm going to create first element and the method is create a key using double quotes and pass a value to this key using column and in this case i am going to use the key as the name of my augmentation function and each key will have a complete function calling using alias a so let's see how we can do this so my first element inside the dictionary is about horizontal flip and its key will be horizontal flip and its value will be a string which will hold a complete calling off function using alias a and parameter passing of probabilities to create another element inside the dictionary i am going to do the same for vertical flip vertical flip key and complete function calling of vertical flip next i am going to create my third element because in this case i am going to use only three augmentation functions so third element will be my last element inside dictionary so this phenomenon key is counting noise and its value is again a string with complete calling of function using alias a and the parameter probability passing so i am going to remove these lines we don't need them i'm going to run this line you can see in my variable explorer that the dictionary has been created successfully which contains three elements key wise and value-wise next i want to create separate variables so that each variable contains keys and values separately and we can do that by calling dot keys and dot values functions over dictionary and converted them into list this will give us a list of keys that will be assigned to variable keys and to hold the values separately inside the variable values we have done the same called dot values over dictionary and converted the return values into list and assign them into variable values let's run these lines you can see my keys and values are separately placed inside keys and values variable next i am going to run a loop over list of augmentations using my keys and created a range over total length of keys and here the total length of keys is three because i have used only three augmentation functions next this whole block will be inside the outer lobe to keep it clear i am going to comment it as the end of inner loop and here the end of outer loop so this time i don't want to print any input image iteration information rather than i am going to print the iteration on my list of augmentations before i continue writing let me tell you first that inside the values variable each value is a name of function which is stored as a string i don't want to use this string as a string but rather i want to convert this string to a function call and to do this we can use dot eval function in python let's see how can we do that so because the length of variables keys and values is same and the order is also synchronized i do not have to i trade separately over values i can simply use the article k to index through values and to convert the string to a function call and to convert a string so that it behaves as a calling of function i am going to call eval function of python and inside this i am going to pass the name of function inside array of values by indexing through kite rater and this will return a converted string as a function which will be our augmentation handler now because i have three augmentation functions i do not want to save all of the augmented images inside one folder rather than using these keys i want to create a separate folder with the same name as the name of keys so that if the alteration goes through horizontal flip every augmented image using horizontal flip will be saved inside the horizontal flip directory and to do that first i need to prepare the output folder and the art folder name i am going to take from keels by indexing through k my input images are same in the loop is same i have not changed augment and return function is also the same but right before saving the augmented image first we need to create output folder and to do that first i have concatenated the output path with the folder that i just saved using key indexing and i have ended it with slash next i am going to create an if statement to see if my output path is already created or not if not create a directory and to do that we need to first import os library and this time because our destination is now inside just variable we are going to change it with output path so that our output image is saved in this new destination now i am going to run the program so you can see that the program has finished running and it has iterated through each element inside the list of augmentations now let me take you to my output directory so that we can see the output is saved correctly or not as you can see that the augmented underscore images is my output directory and these are the images we generated previously and these are directories that we generated this time in case of using multiple augmentation functions and you can see that each directory is generated based on the name of augmentation key inside the dictionary and if i click on it you can see that each image has been successfully augmented based on the corresponding augmentation function so this is vertical flip you can see that the four digit is what we flip and if i go to gaussian noise has been gaussian noise has been introduced in each input image if i zoom it you can see that there are surrounding pixels in 2a scales so this is how you can use multiple augmentation functions using a list or dictionary from albumentations library this is it for this video thank you for watching i hope you find it helpful if you did do like and share also subscribe the channel and hit the bell icon so that you get notified whenever i upload a new video if you have any suggestions leave them in comments section i will do consider it thank you
Info
Channel: Coderific
Views: 58
Rating: undefined out of 5
Keywords: python, dataframe, csv, writing, reading, to csv, pandas, numpy, tensorflow, dataset, mnist, pca, principal component analysis, scikit, matplot, pyplot, scipy, curve_fitting, optimization function, scatter, video, feature extraction, opencv, optical flow, maps, skvideo.io, farneback, datasets, images, augmenation, albumentations
Id: AzrWv2zO_Uo
Channel Id: undefined
Length: 17min 58sec (1078 seconds)
Published: Thu Oct 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.