【问题标题】:Prepare images for a neural network model为神经网络模型准备图像
【发布时间】:2018-02-27 11:19:32
【问题描述】:

我编写了以下内容来为神经网络模型加载和准备图像,而不是 用于深度卷积神经网络。
步骤:扫描 -> 调整大小 -> 展平 -> 标准化。
我不使用 OpenCV 或过滤池方法。这是一个简单的功能,可以读取、调整大小然后展平图像。
图片扩展名为.jpg

import numpy as np
import pandas as pd
from skimage.transform import resize
import matplotlib.pylab as plt

def load_pre_images(fname_csv, path, num_px):

    """


        Parameters
        ----------
        path : str
            Path to images folder
        fname_csv : str
            Name of the CSV file that contains [Images_names, description, 
            target]
         num_px : int
            Images new size (num_px x num_px)
        Returns
        -------
        np.array(img_dataset) : numpy array
            Complete data (m x nx)
            m is the number of pictures
            nx is the dimensionality (num_px x num_px x 3) for rbg images
        count : int
            Count of the undetected images

    """

    img_dataset = []
    mydata = pd.read_csv(path + fname_csv).values
    count = 0
    for i in mydata:
        try:
            img_path = path_images + i[0] + '.jpg'  # Images names lies in the first column
            image = plt.imread(img_path)
            my_image = resize(image, (num_px, num_px)).reshape((num_px*num_px*3,1)) # Flatten
            my_image = my_image / 255  # Normalize images
            img_dataset.append(np.append(my_image, i[2]))  # Target lies in the third column
        except FileNotFoundError:
            count += 1
            continue
    return np.array(img_dataset), count

path_images = 'your path to the images folder/'
imgs, c = load_pre_images('name_of_your_csv_file.csv', path_images, 100)

使用 numpy append 'img_dataset.append(np.append(my_image, i[2]))' 好还是有更好的方法?

【问题讨论】:

  • 我不明白。您需要什么样的帮助?
  • 很抱歉。我想知道您对我的代码的看法。 (我该如何改进它?)

标签: python neural-network computer-vision


【解决方案1】:

第一件事是,当您已经导入 skimage 时,为什么还要使用 matplotlib 中的imread?使用skimage.io.imread

其次,我不太明白你想对整个 numpy.append 做些什么。这既可以使您的图像变平,也可以将目标值附加到图像的末尾,但是有更好的方法可以做到这一点。这是一个简单的代码,可以完全按照您的意愿进行操作:

import numpy as np
import pandas as pd
from skimage.transform import resize
from skimage.io import imread
import os


def load_pre_images(fname_csv, path, num_px):
    """
        Parameters
        ----------
        path : str
            Path to images folder
        fname_csv : str
            Name of the CSV file that contains [Images_names, description, 
            target]
         num_px : int
            Images new size (num_px x num_px)
        Returns
        -------
        np.array(img_dataset) : numpy array
            Complete data (m x nx)
            m is the number of pictures
            nx is the dimensionality (num_px x num_px x 3) for rbg images
        count : int
            Count of the undetected images

    """

    mydata = pd.read_csv(os.path.join(path, fname_csv)).values
    count = 0
    x = []
    y = []
    for row in mydata:
        try:
            img_path = os.path.join(path, row[0] + '.jpg')  # Images names lies in the first column
            image = imread(img_path)
            my_image = resize(image, (num_px, num_px))
            my_image = my_image / 255  # Normalize images
            my_image = my_image.reshape((-1, 1))  # Flatten image
            x.append(my_image)
            y.append(row[2])
        except FileNotFoundError:
            count += 1
            continue
    return np.asarray(x), np.asarray(y), count


path_images = 'your path to the images folder/'
imgs, labels, c = load_pre_images('name_of_your_csv_file.csv', path_images, 100)

看到我有:

  1. 删除了不必要的 matplotlib 依赖项
  2. 将图像和标签分离到不同的数组中
  3. except Exception 更改为except FileNotFoundError,因为这可能就是您想要的。不要在单个子句中捕获所有异常。这很糟糕
  4. 使用numpy.reshape 来扁平化您的图像,而不是不是为此而设计的numpy.append

【讨论】:

  • 我已经更正了我的代码中缺少的部分,因为输出应该是一个矩阵而不是一堆列表。我添加了 .reshape((num_px*num_px*3,1))
猜你喜欢
  • 1970-01-01
  • 2018-02-11
  • 2019-04-27
  • 1970-01-01
  • 1970-01-01
  • 2020-07-29
  • 2016-11-29
  • 1970-01-01
相关资源
最近更新 更多