错误说明 有效处理输入的前置处理功能
def foo(inp):
inp = keras.preprocessing.image.img_to_array(inp)
return keras.preprocessing.image.array_to_img(inp)
导致TypeError:当我执行from tensorflow import keras
而不是import keras
时,* =:'Image'和'float'的操作数类型不受支持,另一种阻止错误的方法是替换backg_subtr:
def foo(inp):
return inp
环境:带有tf.keras v2.1.2-tf的tensorflow-gpu后端(v1.05)。 Keras v2.1.5。在没有tensorflow 2.1且没有单独的keras软件包的新环境中,我遇到相同的错误。
问题:
背景:我正在尝试将代码迁移到tensorflow 2.1中的单个后端keras。我的第一步是用tf.keras替换所有调用keras的函数。 conda install tensorflow=2.1
不会安装枕头(这是必需的),所以也许我现在使用的枕头版本不是正确的枕头,也许还有其他必需的软件包正在安装,但没有正确的版本
产生错误的代码示例(数据是一组80x80 8位RGB .png文件。):
useTfKeras = True
if useTfKeras:
from tensorflow import keras
else:
import keras
import numpy as np
def backg_subtr(inp):
inp = keras.preprocessing.image.img_to_array(inp)
# if useTfKeras:
# return inp
return keras.preprocessing.image.array_to_img(inp)
pathin = 'D:\\Data\\val_set\\'
thumb_shape = (80,80)
input_shape = thumb_shape + (3,) # keras.backend.image_data_format() == "channels_first"
sample_names_use = ['Type A', 'Type B', 'Type C']
test_datagen = keras.preprocessing.image.ImageDataGenerator(
rescale = 1./255,
preprocessing_function=backg_subtr
)
thumb_generator = test_datagen.flow_from_directory(
pathin,
target_size=thumb_shape,
batch_size=1,
class_mode="sparse",
shuffle=False,
follow_links=True
)
x_thumbs = np.empty((len(thumb_generator),) + input_shape, dtype=np.float32)
y_presco = np.zeros((len(thumb_generator),),"int8")-1 # initialize the prescore to -1
for idx2 in range(len(thumb_generator)):
(x_thumbs[idx2,],y_presco[idx2,]) = thumb_generator.next()
回溯:
runfile('C:/proj/DL_code/code/MWE_Generator bug.py', wdir='C:/proj/DL_code_/code')
Traceback (most recent call last):
File "<ipython-input-54-f6170b62f133>", line 1, in <module>
runfile('C:/proj/DL_code/code/MWE_Generator bug.py', wdir='C:/proj/DL_code/code')
File "C:\Users\CoumansF\AppData\Local\Continuum\anaconda3\envs\DLenv\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\CoumansF\AppData\Local\Continuum\anaconda3\envs\DLenv\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/proj/DL_code/code/MWE_Generator bug.py", line 40, in <module>
(x_thumbs[idx2,],y_presco[idx2,]) = thumb_generator.next()
File "C:\Users\CoumansF\AppData\Local\Continuum\anaconda3\envs\DLenv\lib\site-packages\tensorflow\python\keras\_impl\keras\preprocessing\image.py", line 1243, in next
return self._get_batches_of_transformed_samples(index_array)
File "C:\Users\CoumansF\AppData\Local\Continuum\anaconda3\envs\DLenv\lib\site-packages\tensorflow\python\keras\_impl\keras\preprocessing\image.py", line 1208, in _get_batches_of_transformed_samples
x = self.image_data_generator.standardize(x)
File "C:\Users\CoumansF\AppData\Local\Continuum\anaconda3\envs\DLenv\lib\site-packages\tensorflow\python\keras\_impl\keras\preprocessing\image.py", line 590, in standardize
x *= self.rescale
TypeError: unsupported operand type(s) for *=: 'Image' and 'float'