Impact of Image Flattening

22 Jan, 2021
Flattening is a technique that is used to convert multi-dimensional arrays into a 1-D array, it is generally used in Deep Learning while feeding the 1-D array information to the classification model.
What is the need for Flattening of an Image?
Multi-Dimensional arrays take more amount of memory while 1-D arrays take less memory, which is the most important reason why we flatten the Image Array before processing/feeding the information to our model. In most cases, we will be dealing with a dataset which contains a large amount of images thus flattening helps in decreasing the memory as well as reducing the time to train the model.
Step 1: Importing the necessary libraries
Python3
import numpy as npimport pandas as pdimport cv2 as cvfrom google.colab.patches import cv2_imshowfrom skimage import iofrom PIL import Imageimport matplotlib.pylab as pltfrom numpy import arrayfrom sys import getsizeof
Step 2: Fetching a random image through web
Python3
#Fetching the url and showing the image using cv2_imshowurls=["https://iiif.lib.ncsu.edu/iiif/0052574/full/800,/0/default.jpg"]for url in urls:  image = io.imread(url)  cv2_imshow(image)  print('\n')
Step 3: Transforming the image into a multi-dimensional array
Python3
#Getting the multi-dimensional array from the imagearray1 = array(image)#Memory occupied by the multi-dimensional arraysize1 = getsizeof(array1)print(array1)
Step 4: Now Flattening the multi-dimensional array using flatten() function
Python3
#Using Flatten function on array 1 to convert the multi-dimensional # array to 1-D arrayarray2 = array1.flatten()#Memory occupied by array 2size2 = getsizeof(array2)#displaying the 1-D arrayprint(array2)
Step5: Results of Flattening
Python3
#Print's the two different size's of the arrayprint(f"Size of Multidimensional Image : {size1}")print(f"Size of Flattened Image : {size2}")difference = size1 - size2#Print's the difference of memory between the size of Multidimensional & 1-D arrayprint("Size difference in the images: ", difference)
Size of Multidimensional Image : 1324928 
Size of Flattened Image : 1324896
Size difference in the images: 32
Step 6: Full Code
Python3
#importing librariesimport numpy as npimport pandas as pdimport cv2 as cvfrom google.colab.patches import cv2_imshowfrom skimage import iofrom PIL import Imageimport matplotlib.pylab as pltfrom numpy import arrayfrom sys import getsizeof  #Fetching the url and showing the image using cv2_imshowurls=["https://iiif.lib.ncsu.edu/iiif/0052574/full/800,/0/default.jpg"]for url in urls:  image = io.imread(url)  cv2_imshow(image)  print('\n')  #Getting the multi-dimensional array from the imagearray1 = array(image)#Memory occupied by the multi-dimensional arraysize1 = getsizeof(array1)print(array1)  #Using Flatten function on array 1 to convert the multi-dimensional # array to 1-D arrayarray2 = array1.flatten()#Memory occupied by array 2size2 = getsizeof(array2)#displaying the 1-D arrayprint(array2)  #Print's the two different size's of the arrayprint(f"Size of Multidimensional Image : {size1}")print(f"Size of Flattened Image : {size2}")difference = size1 - size2#Print's the difference of memory between the size of Multidimensional & 1-D arrayprint(difference)
Conclusion:
After running the whole code we see that there is not a major difference in memory used in the multi-dimensional image array and the flattened array. Then people may ask why we are doing the flattening when the effect is negligible. In a large datasets when we are dealing with thousands of images the net amount of the memory saved due to all the images accumulates to be pretty big.
Image-Processing
Machine Learning
Python
Machine Learning
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.