准备神经网络模型的图像

时间:2018-02-27 11:19:33

标签: python neural-network computer-vision

我已经写了以下内容来加载和准备神经网络模型的图像,而不是 对于深度卷积神经网络 步骤:扫描 - >调整大小 - > flatten - >正常化。
我不使用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]))'或者有更好的方法来做到这一点?

1 个答案:

答案 0 :(得分:0)

首先,当您已经导入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,而不是