我想离线训练keras预训练的resnet50模型,但是我无法加载模型。
当我设置weights='imagenet'
时,它可以工作。它自动下载imagenet权重文件。
from keras.applications.resnet import ResNet50
base_model = ResNet50(include_top=False, weights='resnet', input_shape=(w,h,3),pooling='avg')
但是,当我手动下载相同的权重文件并设置weights=resnet_weights_path
时,它会抛出ValueError。
(w,h) = 224,224
resnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
base_model = ResNet50(include_top=False, weights=resnet_weights_path, input_shape=(w,h,3),pooling='avg')
ValueError:形状(1,1,256,512)和(512,128,1,1)是不兼容的。
全面回溯:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-18-7683562fa2b9> in <module>
1 resnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
2 base_model = ResNet50(include_top=False, weights=resnet_weights_path,
----> 3 pooling='avg')
4 base_model.summary()
/opt/conda/lib/python3.6/site-packages/keras/applications/__init__.py in wrapper(*args, **kwargs)
18 kwargs['models'] = models
19 kwargs['utils'] = utils
---> 20 return base_fun(*args, **kwargs)
21
22 return wrapper
/opt/conda/lib/python3.6/site-packages/keras/applications/resnet.py in ResNet50(*args, **kwargs)
12 @keras_modules_injection
13 def ResNet50(*args, **kwargs):
---> 14 return resnet.ResNet50(*args, **kwargs)
15
16
/opt/conda/lib/python3.6/site-packages/keras_applications/resnet_common.py in ResNet50(include_top, weights, input_tensor, input_shape, pooling, classes, **kwargs)
433 input_tensor, input_shape,
434 pooling, classes,
--> 435 **kwargs)
436
437
/opt/conda/lib/python3.6/site-packages/keras_applications/resnet_common.py in ResNet(stack_fn, preact, use_bias, model_name, include_top, weights, input_tensor, input_shape, pooling, classes, **kwargs)
411 model.load_weights(weights_path)
412 elif weights is not None:
--> 413 model.load_weights(weights)
414
415 return model
/opt/conda/lib/python3.6/site-packages/keras/engine/saving.py in load_wrapper(*args, **kwargs)
490 os.remove(tmp_filepath)
491 return res
--> 492 return load_function(*args, **kwargs)
493
494 return load_wrapper
/opt/conda/lib/python3.6/site-packages/keras/engine/network.py in load_weights(self, filepath, by_name, skip_mismatch, reshape)
1228 else:
1229 saving.load_weights_from_hdf5_group(
-> 1230 f, self.layers, reshape=reshape)
1231 if hasattr(f, 'close'):
1232 f.close()
/opt/conda/lib/python3.6/site-packages/keras/engine/saving.py in load_weights_from_hdf5_group(f, layers, reshape)
1235 ' elements.')
1236 weight_value_tuples += zip(symbolic_weights, weight_values)
-> 1237 K.batch_set_value(weight_value_tuples)
1238
1239
/opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in batch_set_value(tuples)
2958 `value` should be a Numpy array.
2959 """
-> 2960 tf_keras_backend.batch_set_value(tuples)
2961
2962
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/keras/backend.py in batch_set_value(tuples)
3321 with ops.init_scope():
3322 for x, value in tuples:
-> 3323 x.assign(np.asarray(value, dtype=dtype(x)))
3324 else:
3325 with get_graph().as_default():
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/ops/resource_variable_ops.py in assign(self, value, use_locking, name, read_value)
817 with _handle_graph(self.handle):
818 value_tensor = ops.convert_to_tensor(value, dtype=self.dtype)
--> 819 self._shape.assert_is_compatible_with(value_tensor.shape)
820 assign_op = gen_resource_variable_ops.assign_variable_op(
821 self.handle, value_tensor, name=name)
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/tensor_shape.py in assert_is_compatible_with(self, other)
1108 """
1109 if not self.is_compatible_with(other):
-> 1110 raise ValueError("Shapes %s and %s are incompatible" % (self, other))
1111
1112 def most_specific_compatible_shape(self, other):
ValueError: Shapes (1, 1, 256, 512) and (512, 128, 1, 1) are incompatible
发布于 2020-02-09 11:27:32
这一问题可能是由于keras版本造成的。我使用的当前keras版本是2.3.1
。
要解决问题,请执行以下操作:
weights='imagenet'
运行代码。它下载权重文件automatically.发布于 2020-02-08 09:27:51
它们是一种形状不匹配,由于矢量形状不匹配会引起问题,因此根据权重改变结构是无法解决的。
从这里下载权重,然后再试一次。这些是角角本身给出的权重。
WEIGHTS_PATH = ('https://github.com/fchollet/deep-learning-models/'
'releases/download/v0.2/'
'resnet50_weights_tf_dim_ordering_tf_kernels.h5')
WEIGHTS_PATH_NO_TOP = ('https://github.com/fchollet/deep-learning-models/'
'releases/download/v0.2/'
'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')
发布于 2021-03-17 17:18:48
对于加载Resnet50以供脱机使用的简单解决方案,可以尝试通过设置参数weights ='imagenet'
自动加载权重。
from keras.applications.resnet import ResNet50
base_model = ResNet50(include_top=False, weights='imagenet', input_shape=(w,h,3), pooling='avg')
使用
base_model.save("model_name.h5")
然后,可以将其脱机加载为模型(体系结构+权重)。
from tensorflow.keras.models import load_model
resnet = load_model('model_name.h5')
https://stackoverflow.com/questions/60119041
复制相似问题