2017-11-25 10 views
0

私はkerasでResNetを養成しようとしています、tensorflow
私が使っていたモデルはResNet50はTypeError:オブジェクト.__新しい__(_ hashlib.HASH)が安全ではない、_hashlib.HASHを使用.__新しい__()

Iでありますトレーニングプロセスにケラスチェックポイントを追加すると、このエラーメッセージが表示されます。
ここに私のコードです:

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__() 

答えて

0

これは私があなたのコードスニペットから問題を発見することができないような答えはありませんが、ここでは大まかに何が起こっているかです。 Kerasはあなたのモデルのチェックポイントを保存しようとしています(あなたがcheckpointer = ...行内に質問したときのように)。これを実行している間に、モデルのconfigurationdeepcopyを実行します。どういうわけか、この構成のどこかに深いところに、コピーできないオブジェクト(_hashlib.HASH)があります。オブジェクトにコピーが不可能な理由はたくさんあります。たとえば、ロックがある場合などですが、重要ではありません。重要な部分は、そのようなオブジェクトがモデルの設定にどのようにしたのかを理解することです。

config辞書の各キー/値をコピーするようにケラスコードのコピーを変更できます(/usr/local/lib/python3.4/dist-packages/keras/engine/topology.py)。どちらが問題かを見てください。たとえば、など、掘るより深い必要な場合は、単にthis code

for k, v in config.items(): 
    print("Copying " + str(k) + " -> " + str(v)) 
    k_copy = copy.deepcopy(k) 
    v_copy = copy.deepcopy(v) 
    print(" copied sucessfully") 

の上にこれを追加し、このオブジェクトの属性が問題であるかを確認最後に、このオブジェクトは、モデルの設定で終わったかを理解してみてください。

関連する問題