Python 类型错误:Can';t广播(10246272)>;(1024, 25088)
我试图使用and编码器从数据集中提取特征,但出现以下错误:Python 类型错误:Can';t广播(10246272)>;(1024, 25088),python,arrays,numpy,reshape,tensorflow2.0,Python,Arrays,Numpy,Reshape,Tensorflow2.0,我试图使用and编码器从数据集中提取特征,但出现以下错误: Traceback (most recent call last): File "extract_features.py", line 104, in <module> features = features.reshape((features.shape[0], 512 * 7 * 7)) ValueError: cannot reshape array of size 200704 into shape (3
Traceback (most recent call last):
File "extract_features.py", line 104, in <module>
features = features.reshape((features.shape[0], 512 * 7 * 7))
ValueError: cannot reshape array of size 200704 into shape (32,25088)
以下是功能提取器代码的一部分
# loop over the images in batches
for i in np.arange(0, len(imagePaths), bs):
# extract the batch of images and labels, then initialize the
# list of actual images that will be passed through the network
# for feature extraction
batchPaths = imagePaths[i:i + bs]
batchLabels = labels[i:i + bs]
batchImages = []
# loop over the images and labels in the current batch
for (j, imagePath) in enumerate(batchPaths):
# load the input image using the Keras helper utility
# while ensuring the image is resized to 224x224 pixels
image = load_img(imagePath, target_size=(224, 224))
image = img_to_array(image)
# preprocess the image by (1) expanding the dimensions and
# (2) subtracting the mean RGB pixel intensity from the
# ImageNet dataset
image = np.expand_dims(image, axis=0)
image = imagenet_utils.preprocess_input(image)
# add the image to the batch
batchImages.append(image)
# pass the images through the network and use the outputs as
# our actual features
batchImages = np.vstack(batchImages)
features = model.predict(batchImages, batch_size=bs)
# reshape the features so that each image is represented by
# a flattened feature vector of the `MaxPooling2D` outputs
features = features.reshape((features.shape[0], 512 * 7 * 7))
# add the features and labels to our HDF5 dataset
dataset.add(features, batchLabels)
pbar.update(i)
我正在windows上使用tensorflow 2.0和python 3.7.6
将尺寸更改为features=features.reformate((features.shape[0],128*7*7))
后,我得到了以下错误
Traceback (most recent call last):#### | ETA: 0:00:24
File "extract_features.py", line 108, in <module>
dataset.add(features, batchLabels)
File "D:\Clones\feature_extraction_try\pyimagesearch\io\hdf5datasetwriter.py", line 37, in add
self.flush()
File "D:\Clones\feature_extraction_try\pyimagesearch\io\hdf5datasetwriter.py", line 42, in flush
self.data[self.idx:i] = self.buffer["data"]
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "C:\Users\sancy\Anaconda3\envs\tensorflow2\lib\site-packages\h5py\_hl\dataset.py", line 707, in __setitem__
for fspace in selection.broadcast(mshape):
File "C:\Users\sancy\Anaconda3\envs\tensorflow2\lib\site-packages\h5py\_hl\selections.py", line 299, in broadcast
raise TypeError("Can't broadcast %s -> %s" % (target_shape, self.mshape))
TypeError: Can't broadcast (1024, 6272) -> (1024, 25088)
期待您的帮助。您的尺码似乎与32*25088=802816不符,比200704大4倍 因此,如果您这样做,它将起作用:
features=features.reforme((features.shape[0],128*7*7))
看起来您的尺寸与32*25088=802816的尺寸不匹配,比200704大4倍
因此,如果您这样做,它将起作用:
features=features.reforme((features.shape[0],128*7*7))
谢谢您的回答。我试过了,但现在我得到了“``类型错误:不能广播(10246272)->(102425088)``现在它在哪一行中断?我已经更新了问题,以便包含引用代码的完整回溯部分。请看上面,谢谢。我得到了错误,因为我没有在我的代码中更改维度。你的回答很有帮助。谢谢,谢谢你的回答。我试过了,但现在我得到了“``类型错误:不能广播(10246272)->(102425088)``现在它在哪一行中断?我已经更新了问题,以便包含引用代码的完整回溯部分。请看上面,谢谢。我得到了错误,因为我没有在我的代码中更改维度。你的回答很有帮助。非常感谢。
Traceback (most recent call last):#### | ETA: 0:00:24
File "extract_features.py", line 108, in <module>
dataset.add(features, batchLabels)
File "D:\Clones\feature_extraction_try\pyimagesearch\io\hdf5datasetwriter.py", line 37, in add
self.flush()
File "D:\Clones\feature_extraction_try\pyimagesearch\io\hdf5datasetwriter.py", line 42, in flush
self.data[self.idx:i] = self.buffer["data"]
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "C:\Users\sancy\Anaconda3\envs\tensorflow2\lib\site-packages\h5py\_hl\dataset.py", line 707, in __setitem__
for fspace in selection.broadcast(mshape):
File "C:\Users\sancy\Anaconda3\envs\tensorflow2\lib\site-packages\h5py\_hl\selections.py", line 299, in broadcast
raise TypeError("Can't broadcast %s -> %s" % (target_shape, self.mshape))
TypeError: Can't broadcast (1024, 6272) -> (1024, 25088)
def add(self, rows, labels):
# add the rows and labels to the buffer
self.buffer["data"].extend(rows)
self.buffer["labels"].extend(labels)
# check to see if the buffer needs to be flushed to disk
if len(self.buffer["data"]) >= self.bufSize:
self.flush()
def flush(self):
# write the buffers to disk then reset the buffer
i = self.idx + len(self.buffer["data"])
self.data[self.idx:i] = self.buffer["data"]
self.labels[self.idx:i] = self.buffer["labels"]
self.idx = i
self.buffer = {"data": [], "labels": []}