TypeError:object .__ new __(_ hashlib.HASH)不安全,使用_hashlib.HASH .__ new __()

时间:2017-11-25 09:31:39

标签: python tensorflow keras hashlib

我正在尝试用keras训练一个ResNet,tensorflow
我使用的模型是ResNet50

当我在训练过程中添加keras检查点时,我收到此错误消息 这是我的代码:

def train(batch_size, nb_classes = 5):

    x_train, y_train, x_test, y_test = loadData(nb_classes) 

    x_train = (x_train.astype(np.float32) - 127.5)/127.5
    x_test = (x_test.astype(np.float32) - 127.5)/127.5

    r_model =model(nb_classes,dropout= True)
    model = multi_gpu_model(r_model, gpus=4)
    optim = SGD(lr=0.00001, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer = optim,metrics=['accuracy'])


    model.load_weights('/../weights1',True)
    checkpointer = ModelCheckpoint(filepath='/.../weights2', save_best_only=True)
    model.fit(x_train, y_train, epochs=10, batch_size=128,validation_data=(x_test, y_test),shuffle=True,callbacks=[checkpointer])

添加此行时程序崩溃:
    checkpointer = ModelCheckpoint(filepath='/.../weights2', save_best_only=True)

我在互联网上搜索,我认为在我使用的某些功能中必须使用deepcopy
但我该如何解决呢?

错误讯息:

TypeError    Traceback (most recent call last)
<ipython-input-11-d4fe46633cc3> in <module>()
----> 1 train(64)

<ipython-input-10-d02154ab30e5> in train(batch_size, nb_classes)
     18     model.load_weights('/.../weights1',True)
     19     checkpointer = ModelCheckpoint(filepath='/.../weights2', save_best_only=True)
---> 20     history = model.fit(x_train, y_train, epochs=10, batch_size=128,validation_data=(x_test, y_test),shuffle=True,callbacks=[checkpointer])


/usr/local/lib/python3.4/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1629                               initial_epoch=initial_epoch,
   1630                               steps_per_epoch=steps_per_epoch,
-> 1631                               validation_steps=validation_steps)
   1632 
   1633     def evaluate(self, x=None, y=None,

/usr/local/lib/python3.4/dist-packages/keras/engine/training.py in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
   1231                             for l, o in zip(out_labels, val_outs):
   1232                                 epoch_logs['val_' + l] = o
-> 1233             callbacks.on_epoch_end(epoch, epoch_logs)
   1234             if callback_model.stop_training:
   1235                 break

/usr/local/lib/python3.4/dist-packages/keras/callbacks.py in on_epoch_end(self, epoch, logs)
     71         logs = logs or {}
     72         for callback in self.callbacks:
---> 73             callback.on_epoch_end(epoch, logs)
     74 
     75     def on_batch_begin(self, batch, logs=None):

/usr/local/lib/python3.4/dist-packages/keras/callbacks.py in on_epoch_end(self, epoch, logs)
    412                             self.model.save_weights(filepath, overwrite=True)
    413                         else:
--> 414                             self.model.save(filepath, overwrite=True)
    415                     else:
    416                         if self.verbose > 0:

/usr/local/lib/python3.4/dist-packages/keras/engine/topology.py in save(self, filepath, overwrite, include_optimizer)
   2554         """
   2555         from ..models import save_model
-> 2556         save_model(self, filepath, overwrite, include_optimizer)
   2557 
   2558     def save_weights(self, filepath, overwrite=True):

/usr/local/lib/python3.4/dist-packages/keras/models.py in save_model(model, filepath, overwrite, include_optimizer)
    105         f.attrs['model_config'] = json.dumps({
    106             'class_name': model.__class__.__name__,
--> 107             'config': model.get_config()
    108         }, default=get_json_type).encode('utf8')
    109 

/usr/local/lib/python3.4/dist-packages/keras/engine/topology.py in get_config(self)
   2395             model_outputs.append([layer.name, new_node_index, tensor_index])
   2396         config['output_layers'] = model_outputs
-> 2397         return copy.deepcopy(config)
   2398 
   2399     @classmethod

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.4/copy.py in _deepcopy_dict(x, memo)
    244     memo[id(x)] = y
    245     for key, value in x.items():
--> 246         y[deepcopy(key, memo)] = deepcopy(value, memo)
    247     return y
    248 d[dict] = _deepcopy_dict

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.4/copy.py in _deepcopy_list(x, memo)
    217     memo[id(x)] = y
    218     for a in x:
--> 219         y.append(deepcopy(a, memo))
    220     return y
    221 d[list] = _deepcopy_list

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.4/copy.py in _deepcopy_dict(x, memo)
    244     memo[id(x)] = y
    245     for key, value in x.items():
--> 246         y[deepcopy(key, memo)] = deepcopy(value, memo)
    247     return y
    248 d[dict] = _deepcopy_dict

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.4/copy.py in _deepcopy_dict(x, memo)
    244     memo[id(x)] = y
    245     for key, value in x.items():
--> 246         y[deepcopy(key, memo)] = deepcopy(value, memo)
    247     return y
    248 d[dict] = _deepcopy_dict

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.4/copy.py in _deepcopy_tuple(x, memo)
    224     y = []
    225     for a in x:
--> 226         y.append(deepcopy(a, memo))
    227     # We're not going to put the tuple in the memo, but it's still important we
    228     # check for it, in case the tuple contains recursive mutable structures.

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.4/copy.py in _deepcopy_tuple(x, memo)
    224     y = []
    225     for a in x:
--> 226         y.append(deepcopy(a, memo))
    227     # We're not going to put the tuple in the memo, but it's still important we
    228     # check for it, in case the tuple contains recursive mutable structures.

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    180                             raise Error(
    181                                 "un(deep)copyable object of type %s" % cls)
--> 182                 y = _reconstruct(x, rv, 1, memo)
    183 
    184     # If is its own copy, don't memoize.

/usr/lib/python3.4/copy.py in _reconstruct(x, info, deep, memo)
    298     if state:
    299         if deep:
--> 300             state = deepcopy(state, memo)
    301         if hasattr(y, '__setstate__'):
    302             y.__setstate__(state)

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.4/copy.py in _deepcopy_dict(x, memo)
    244     memo[id(x)] = y
    245     for key, value in x.items():
--> 246         y[deepcopy(key, memo)] = deepcopy(value, memo)
    247     return y
    248 d[dict] = _deepcopy_dict

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    180                             raise Error(
    181                                 "un(deep)copyable object of type %s" % cls)
--> 182                 y = _reconstruct(x, rv, 1, memo)
    183 
    184     # If is its own copy, don't memoize.

/usr/lib/python3.4/copy.py in _reconstruct(x, info, deep, memo)
    298     if state:
    299         if deep:
--> 300             state = deepcopy(state, memo)
    301         if hasattr(y, '__setstate__'):
    302             y.__setstate__(state)

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.4/copy.py in _deepcopy_dict(x, memo)
    244     memo[id(x)] = y
    245     for key, value in x.items():
--> 246         y[deepcopy(key, memo)] = deepcopy(value, memo)
    247     return y
    248 d[dict] = _deepcopy_dict

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.4/copy.py in _deepcopy_dict(x, memo)
    244     memo[id(x)] = y
    245     for key, value in x.items():
--> 246         y[deepcopy(key, memo)] = deepcopy(value, memo)
    247     return y
    248 d[dict] = _deepcopy_dict

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.4/copy.py in _deepcopy_method(x, memo)
    251 
    252 def _deepcopy_method(x, memo): # Copy instance methods
--> 253     return type(x)(x.__func__, deepcopy(x.__self__, memo))
    254 _deepcopy_dispatch[types.MethodType] = _deepcopy_method
    255 

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    180                             raise Error(
    181                                 "un(deep)copyable object of type %s" % cls)
--> 182                 y = _reconstruct(x, rv, 1, memo)
    183 
    184     # If is its own copy, don't memoize.

/usr/lib/python3.4/copy.py in _reconstruct(x, info, deep, memo)
    298     if state:
    299         if deep:
--> 300             state = deepcopy(state, memo)
    301         if hasattr(y, '__setstate__'):
    302             y.__setstate__(state)

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.4/copy.py in _deepcopy_dict(x, memo)
    244     memo[id(x)] = y
    245     for key, value in x.items():
--> 246         y[deepcopy(key, memo)] = deepcopy(value, memo)
    247     return y
    248 d[dict] = _deepcopy_dict

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.4/copy.py in _deepcopy_dict(x, memo)
    244     memo[id(x)] = y
    245     for key, value in x.items():
--> 246         y[deepcopy(key, memo)] = deepcopy(value, memo)
    247     return y
    248 d[dict] = _deepcopy_dict

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    180                             raise Error(
    181                                 "un(deep)copyable object of type %s" % cls)
--> 182                 y = _reconstruct(x, rv, 1, memo)
    183 
    184     # If is its own copy, don't memoize.

/usr/lib/python3.4/copy.py in _reconstruct(x, info, deep, memo)
    298     if state:
    299         if deep:
--> 300             state = deepcopy(state, memo)
    301         if hasattr(y, '__setstate__'):
    302             y.__setstate__(state)

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.4/copy.py in _deepcopy_dict(x, memo)
    244     memo[id(x)] = y
    245     for key, value in x.items():
--> 246         y[deepcopy(key, memo)] = deepcopy(value, memo)
    247     return y
    248 d[dict] = _deepcopy_dict

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.4/copy.py in _deepcopy_dict(x, memo)
    244     memo[id(x)] = y
    245     for key, value in x.items():
--> 246         y[deepcopy(key, memo)] = deepcopy(value, memo)
    247     return y
    248 d[dict] = _deepcopy_dict

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    180                             raise Error(
    181                                 "un(deep)copyable object of type %s" % cls)
--> 182                 y = _reconstruct(x, rv, 1, memo)
    183 
    184     # If is its own copy, don't memoize.

/usr/lib/python3.4/copy.py in _reconstruct(x, info, deep, memo)
    298     if state:
    299         if deep:
--> 300             state = deepcopy(state, memo)
    301         if hasattr(y, '__setstate__'):
    302             y.__setstate__(state)

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.4/copy.py in _deepcopy_dict(x, memo)
    244     memo[id(x)] = y
    245     for key, value in x.items():
--> 246         y[deepcopy(key, memo)] = deepcopy(value, memo)
    247     return y
    248 d[dict] = _deepcopy_dict

/usr/lib/python3.4/copy.py in deepcopy(x, memo, _nil)
    180                             raise Error(
    181                                 "un(deep)copyable object of type %s" % cls)
--> 182                 y = _reconstruct(x, rv, 1, memo)
    183 
    184     # If is its own copy, don't memoize.

/usr/lib/python3.4/copy.py in _reconstruct(x, info, deep, memo)
    293     if deep:
    294         args = deepcopy(args, memo)
--> 295     y = callable(*args)
    296     memo[id(x)] = y
    297 

/usr/lib/python3.4/copyreg.py in __newobj__(cls, *args)
     86 
     87 def __newobj__(cls, *args):
---> 88     return cls.__new__(cls, *args)
     89 
     90 def __newobj_ex__(cls, args, kwargs):

TypeError: object.__new__(_hashlib.HASH) is not safe, use _hashlib.HASH.__new__()

1 个答案:

答案 0 :(得分:0)

这不是答案,因为我无法从您的代码段中发现问题,但这里大概是发生了什么。 Keras正在尝试保存模型的检查点(正如您在checkpointer = ...行时所问的那样)。在执行此操作时,它会执行模型configurationdeepcopy。不知何故,在这个配置的深处,有一个无法复制的对象(_hashlib.HASH)。对象可以不可复制的原因有很多,例如,如果它们有锁,但它并不重要。重要的是弄清楚这样的对象是如何进入模型配置的。

您可以更改keras代码的副本(在/usr/local/lib/python3.4/dist-packages/keras/engine/topology.py中)以尝试复制{{1}的每个键/值}字典,看看哪一个是问题。例如,将其添加到this code

之上
config

如果需要深入挖掘并查看此对象的哪个属性是一个问题,等等。最后,尝试了解此对象在模型配置中的结果。