📜  图像扁平化的影响

📅  最后修改于: 2022-05-13 01:54:26.107000             🧑  作者: Mango

图像扁平化的影响

扁平化是一种用于将多维数组转换为一维数组的技术,它通常用于深度学习,同时将一维数组信息提供给分类模型。

图像展平需要什么?

多维数组占用更多内存,而一维数组占用更少内存,这是我们在处理/将信息提供给模型之前将图像数组展平的最重要原因。在大多数情况下,我们将处理包含大量图像的数据集,因此扁平化有助于减少内存并减少训练模型的时间。

第 1 步:导入必要的库

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


Python3
#Fetching the url and showing the image using cv2_imshow
urls=["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')


Python3
#Getting the multi-dimensional array from the image
array1 = array(image)
#Memory occupied by the multi-dimensional array
size1 = getsizeof(array1)
print(array1)


Python3
#Using Flatten function on array 1 to convert the multi-dimensional 
# array to 1-D array
array2 = array1.flatten()
#Memory occupied by array 2
size2 = getsizeof(array2)
#displaying the 1-D array
print(array2)


Python3
#Print's the two different size's of the array
print(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 array
print("Size difference in the images: ", difference)


Python3
#importing libraries
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
  
#Fetching the url and showing the image using cv2_imshow
urls=["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 image
array1 = array(image)
#Memory occupied by the multi-dimensional array
size1 = getsizeof(array1)
print(array1)
  
#Using Flatten function on array 1 to convert the multi-dimensional 
# array to 1-D array
array2 = array1.flatten()
#Memory occupied by array 2
size2 = getsizeof(array2)
#displaying the 1-D array
print(array2)
  
#Print's the two different size's of the array
print(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 array
print(difference)


第 2 步:通过网络获取随机图像

蟒蛇3

#Fetching the url and showing the image using cv2_imshow
urls=["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')




第 3 步:将图像转换为多维数组

蟒蛇3

#Getting the multi-dimensional array from the image
array1 = array(image)
#Memory occupied by the multi-dimensional array
size1 = getsizeof(array1)
print(array1)

第 4 步:现在使用 flatten()函数展平多维数组

蟒蛇3

#Using Flatten function on array 1 to convert the multi-dimensional 
# array to 1-D array
array2 = array1.flatten()
#Memory occupied by array 2
size2 = getsizeof(array2)
#displaying the 1-D array
print(array2)

Step5:展平的结果

蟒蛇3

#Print's the two different size's of the array
print(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 array
print("Size difference in the images: ", difference)
Size of Multidimensional Image : 1324928 
Size of Flattened Image : 1324896
Size difference in the images: 32

第 6 步:完整代码

蟒蛇3

#importing libraries
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
  
#Fetching the url and showing the image using cv2_imshow
urls=["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 image
array1 = array(image)
#Memory occupied by the multi-dimensional array
size1 = getsizeof(array1)
print(array1)
  
#Using Flatten function on array 1 to convert the multi-dimensional 
# array to 1-D array
array2 = array1.flatten()
#Memory occupied by array 2
size2 = getsizeof(array2)
#displaying the 1-D array
print(array2)
  
#Print's the two different size's of the array
print(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 array
print(difference)


结论:

运行整个代码后,我们看到多维图像数组和展平数组使用的内存没有太大区别。那么人们可能会问,当效果可以忽略不计时,我们为什么要进行扁平化。在大型数据集中,当我们处理数千张图像时,由于所有图像累积而节省的净内存量非常大。