在kears模型中有用到backend,在训练时没有异常,在load model进行predict时,居然报错:name ‘backend’ is not defined。
import keras.backend as backend
y = Lambda(lambda z: z[:, :, :, j * c:(j + 1) * c]
if backend.image_data_format() == 'channels_last' else
lambda z: z[:, j * c:(j + 1) * c, :, :])(x)
解决方式如下:
在loadmodel时,自定义字典custom_objects里面加入‘backend’:backend
import keras.backend as backend
model = load_model('./unet_' + label + '_20.h5',
custom_objects={'focal_loss_fixed': focal_loss(),
‘backend':backend},compile=False)