Impact of Image Flattening
Last Updated :
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 np
import pandas as pd
import cv2 as cv
from google.colab.patches import cv2_imshow
from skimage import io
from PIL import Image
import matplotlib.pylab as plt
from numpy import array
from sys import getsizeof
|
Step 2: Fetching a random image through web
Python3
for url in urls:
image = io.imread(url)
cv2_imshow(image)
print ( '\n' )
|
Step 3: Transforming the image into a multi-dimensional array
Python3
array1 = array(image)
size1 = getsizeof(array1)
print (array1)
|
Step 4: Now Flattening the multi-dimensional array using flatten() function
Python3
array2 = array1.flatten()
size2 = getsizeof(array2)
print (array2)
|
Step5: Results of Flattening
Python3
print (f "Size of Multidimensional Image : {size1}" )
print (f "Size of Flattened Image : {size2}" )
difference = size1 - size2
print ( "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
import numpy as np
import pandas as pd
import cv2 as cv
from google.colab.patches import cv2_imshow
from skimage import io
from PIL import Image
import matplotlib.pylab as plt
from numpy import array
from sys import getsizeof
for url in urls:
image = io.imread(url)
cv2_imshow(image)
print ( '\n' )
array1 = array(image)
size1 = getsizeof(array1)
print (array1)
array2 = array1.flatten()
size2 = getsizeof(array2)
print (array2)
print (f "Size of Multidimensional Image : {size1}" )
print (f "Size of Flattened Image : {size2}" )
difference = size1 - size2
print (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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...