python - 调整引用另一个数组的numpy数组的大小
问题描述
mypath='/Users/sachal/Desktop/data_raw/normal_1/images'
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
images = np.asarray(np.empty(len(onlyfiles), dtype=object))
for n in range(0, len(onlyfiles)):
images[n] = cv2.imread( join(mypath,onlyfiles[n]) )
#--------------------------------------------------------------------------------
resized = np.asarray(np.empty(len(onlyfiles), dtype=object))
img_f = np.asarray(np.empty(len(onlyfiles), dtype=object))
for n in range(0, len(onlyfiles)):
resized[n] = cv2.resize(images[n],(101,101))
img_f[n] = cv2.cvtColor(resized[n], cv2.COLOR_BGR2YUV)
train_img = np.asarray(img_f)
#--------------------------------------------------------------------------------
在上面的代码中,我首先使用 opencv 加载图像,然后在第二个块中调整大小并更改它们的颜色空间。
我的批量大小是6408
,图像的尺寸是101*101*3
什么时候train_img.shape
我得到(6408,)
,当train_img[i].shape
我得到101*101*3
时,我无法训练我的神经网络模型,因为这个我想要的尺寸是6408*101*101*3
我试着用这个重塑train_img.resize(6408,101,101,3)
我得到了这个ValueError: cannot resize an array that references or is referenced
by another array in this way. Use the resize function
在拟合我的模型时,我得到了这个错误Error when checking input: expected conv2d_3_input to have 4 dimensions, but got array with shape (6408, 1)
我想知道是否可以使用当前用于加载图像的方法更改输入的尺寸。
解决方案
你不应该在dtype=object
这里使用。OpenCVndarray
无论如何都会创建图像。
这是您的代码的更正版本:
mypath='/Users/sachal/Desktop/data_raw/normal_1/images'
onlyfiles = [ f for f in os.listdir(mypath) if os.path.isfile(join(mypath,f)) ]
images = []
for file in onlyfiles:
img = cv2.imread(os.path.join(mypath,file))
resized_img = cv2.resize(img, (101, 101))
yuv_img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2YUV)
images.append(yuv_img.reshape(1, 101, 101, 3))
train_img = np.concatenate(images, axis=0)
print(train_img.shape)
在循环中,您加载每个图像,调整其大小,将其转换为 YUV,然后将其放入列表中。在循环结束时,您的列表包含所有训练图像。您可以将其传递np.concatenate
给创建一个ndarray
.
推荐阅读
- java - 将 ResultSet.getString 值与来自文本字段的输入进行比较
- angular - 如何根据来自另一台服务器的结果重定向我的前端 (Angular) 应用程序
- asp.net - 除了第一页,fancybox 不适用于 aspxgridview 页面
- vb.net - 如何使用 vb.net 或任何其他方式单击从多个文件夹中打开 .exe 文件?
- linux - 是否有平台独立的非阻塞方式来判断文件描述符是否是磁盘文件(常规/目录)
- java - 将变量从一种方法传递到另一种方法?
- react-native - 我正在使用 EXPO.IO 处理本机项目,今天它给出了错误 Unable to resolve module @babel
- php - 使用数据提供者时压倒性的 testdox 输出(具有大量数据集)
- html - 我正在尝试设置范围输入的拇指样式,但 webkit 不起作用
- c# - 如果 Employee 存在,则使用 ADO.net 返回员工角色