2017-11-25 20 views
0

내가 keras와 ResNet를 양성하기 위해 노력하고, 교육 과정에 케라 체크 포인트를 추가 할 때이 오류 메시지가 표시됩니다.
여기 내 코드입니다 :형식 오류 : 개체 .__ 새로운 __ (_ hashlib.HASH)는 안전하지 않다, _hashlib.HASH를 사용 .__ 새로운 __()를 내가 사용 된 모델은 ResNet50</p> <p>I입니다 tensorflow <br>

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)가 있습니다. 예를 들어 잠금 장치가있는 경우와 같이 개체를 복사 할 수없는 많은 이유가 있지만 중요한 것은 아닙니다. 중요한 부분은 그러한 객체가 모델 구성에 어떻게 적용되었는지 파악하는 것입니다.

케라 시스 코드 (/usr/local/lib/python3.4/dist-packages/keras/engine/topology.py)를 변경하여 config 사전의 각 키/값을 복사 할 수 있습니다. 어느 것이 문제인지 확인하십시오. 예를 들어,이 객체 모델의 설정에 결국 방법을 이해하려고, 마지막으로 등을 깊이 파고를 필요한 경우 만 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") 

이상이 추가 문제가이 객체의 어떤 속성을 참조하십시오.