如何在GoogleColab中读取图像数据集进行深入学习?

2024-06-16 12:11:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我是深入学习的初学者。我对如何在GoogleColab中读取图像数据集感到困惑。基本上,数据集由2个用于列车和测试图像的文件夹和2个用于列车和测试标签的csv文件组成。现在我需要识别图像的舞蹈模式,首先读取数据,然后分割数据

但是,我尝试使用以下代码读取数据集:

zip_path = '/content/0664343c9a8f11ea.zip'
with ZipFile(zip_path) as z:
    data = z.namelist()

该代码以列表的形式工作并读取数据。稍后,我将无法将其分为训练和测试,以创建神经网络。而且每个图像的大小都不一样,所以我应该如何处理呢

请帮忙。我们将不胜感激

谢谢 普拉奇


Tags: 文件csv数据path代码图像文件夹标签
1条回答
网友
1楼 · 发布于 2024-06-16 12:11:06

有许多方法可以读取图像以输入模型。一种基本方法是将图像转换为numpy数组。使用图像的zip文件,可以执行以下步骤来获取图像的numpy数组。这将适用于任何Python内核,无论是GoogleColab还是您的本地内核

  1. 解压图像
  2. 获取图像的路径
  3. 阅读图像&;转换为numpy数组
import zipfile  # unziping 
import glob  # finding image paths
import numpy as np  # creating numpy arrays
from skimage.io import imread  # reading images
from skimage.transform import resize  # resizing images

# 1. Unzip images
path = 'your zip file path'
with zipfile.ZipFile(path, 'r') as zip_ref:
    zip_ref.extractall('path for extracted images')

# 2. Obtain paths of images (.png used for example)
img_list = sorted(glob.glob('path for extracted images/*.png'))

# 3. Read images & convert to numpy arrays
## create placeholding numpy arrays
IMG_SIZE = 256 (image resolution of 256 x 256 used for example)
x_data = np.empty((len(img_list), IMG_SIZE, IMG_SIZE, 1), dtype=np.float32)

## read and convert to arrays
for i, img_path in enumerate(img_list):
    # read image
    img = imread(img_path)
    # resize image (1 channel used for example; 1 for gray-scale, 3 for RGB-scale)
    img = resize(img, output_shape=(IMG_SIZE, IMG_SIZE, 1), preserve_range=True)
    # save to numpy array
    x_data[i] = img

毕竟,您有一个numpy数组x_data,包含您的图像。然后可以使用此阵列来训练或测试您的模型。 希望这有帮助

相关问题 更多 >