【发布时间】: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