我正在量化一个模型。该模型采用224x224输入
我使用内置函数preprocess_input()
预处理数据,该函数减去一些中心像素
现在,当在representative_dataset_gen()
中使用带有此预处理函数的简单图像时,一切正常
def representative_dataset_gen():
pfad='./000001.jpg'
img = cv2.imread(pfad)
img = np.expand_dims(img,0).astype(np.float32)
img = preprocess_input(img)
yield [img]
但是当我使用一个生成器函数来使用多个图像时
def prepare(img):
img = np.expand_dims(img,0).astype(np.float32)
img = preprocess_input(img)
return arg
repDatagen=tf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function=prepare)
datagen=repDatagen.flow_from_directory(folderpath,target_size=size,batch_size=1)
def representative_dataset_gen():
for _ in range(10):
img = datagen.next()
yield [img]
我收到以下错误:
ValueError: Failed to convert value into readable tensor.
我猜:这是由于ImageDataGenerator(preprocessing_function=prepare)
。在tensorflow描述中,它表示:
function that will be applied on each input. The function will run after the image is resized and augmented. The function should take one argument: one image (Numpy tensor with rank 3), and should output a Numpy tensor with the same shape.
我尝试在不使用和使用np.squez()的情况下调整“prepare”函数的img输出的形状。
这导致(1244244,3)或(224224,3)。但我仍然得到了错误。我也用同样的错误尝试了tf.convert_to_tensor()
def prepare(img):
img = np.expand_dims(img,0).astype(np.float32)
img = preprocess_input(img, version=2)
img = np.squeeze(img)
arg = tf.convert_to_tensor(img, dtype=tf.float32)
return arg
有人知道如何准备输出以获得正确的张量吗?
谢谢
问题实际上是发电机的输出。 输出不是
np.array()
。无法使用np.asarray()进行转换,因为它会引发错误所以答案很简单:
数据生成器输出是一个元组
因此,只需生成第一个元素
img[0]
代码总计:
相关问题 更多 >
编程相关推荐