Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何重塑一个数据到fir预测模型?_Python_Numpy_Keras_Cv2 - Fatal编程技术网

Python 如何重塑一个数据到fir预测模型?

Python 如何重塑一个数据到fir预测模型?,python,numpy,keras,cv2,Python,Numpy,Keras,Cv2,我需要读取两幅图像,将它们转换为150x150大小,然后将它们添加到一个阵列中,该阵列需要重新塑造为(2150150,3)的形状,以便适合keras模型。我很难理解numpy的重塑方法是如何工作的,我需要如何利用它 我的代码: import cv2 import numpy def loadAndReshape(target, path): targetImage = cv2.imread(path) targetImage = cv2.cvtColor(targetImage

我需要读取两幅图像,将它们转换为150x150大小,然后将它们添加到一个阵列中,该阵列需要重新塑造为(2150150,3)的形状,以便适合keras模型。我很难理解numpy的重塑方法是如何工作的,我需要如何利用它

我的代码:

import cv2
import numpy

def loadAndReshape(target, path):
    targetImage = cv2.imread(path)
    targetImage = cv2.cvtColor(targetImage, cv2.COLOR_BGR2RGB)
    targetImage = cv2.resize(targetImage, dsize=(150, 150)) / 255
    targetImage = targetImage.reshape(1, 150, 150, 3).astype('float32')
    numpy.append(target, targetImage)

targetImages = numpy.ndarray((2, 150, 150, 3))
loadAndReshape(targetImages, './/test1.jpg')
loadAndReshape(targetImages, './/test2.jpg')

重塑
targetImages
没有问题,但最终
targetImages
仍将是一个空阵列。如何输出我的模型所需的数组?

函数“numpy.append”并没有像我想的那样在原地工作。 相反,您可以执行smth,如:

mport cv2
import numpy as np

def loadAndReshape(image_list, path):
    targetImage = cv2.imread(path)
    targetImage = cv2.cvtColor(targetImage, cv2.COLOR_BGR2RGB)
    targetImage = cv2.resize(targetImage, dsize=(150, 150)) / 255
    targetImage = targetImage.reshape(1, 150, 150, 3).astype('float32')
    image_list.append(targetImage)

targetImages = []
loadAndReshape(targetImages, './/test1.jpg')
loadAndReshape(targetImages, './/test2.jpg')
.
.
.
targetImages = np.concatenate(targetImages)

numpy.append返回副本,请参阅

您可以尝试以下方法:

import cv2
import numpy

def loadAndReshape(path):
    targetImage = cv2.imread(path)
    targetImage = cv2.cvtColor(targetImage, cv2.COLOR_BGR2RGB)
    targetImage = cv2.resize(targetImage, dsize=(150, 150)) / 255
    targetImage = targetImage.reshape(1, 150, 150, 3).astype('float32')
    return targetImage

li = []
li.append(loadAndReshape('.//test1.jpg'))
li.append(loadAndReshape('.//test2.jpg'))
targetImages = np.array(li)

非常感谢,不知怎的,我错过了它不在的地方。