简体   繁体   English

TypeError:训练keras模型时无法pickle _thread.lock对象

[英]TypeError: can't pickle _thread.lock objects when training keras model

I was trying to train a unet with a keras model and right when training the first epoch I get a type error: 我正在尝试使用keras模型训练一个unet,并且在训练第一个纪元时我得到了一个类型错误:

TypeError: can't pickle _thread.lock objects, please see error message below TypeError:无法pickle _thread.lock对象,请参阅下面的错误消息

I have already tried downgrading tensorflow and adding scripts in the seq2seq.py script in ...tensorflow\\contrib\\legacy_seq2seq\\python\\ops as suggested in answers to similar questions, but this did not solve the problem for me. 我已经尝试降级tensorflow并在seq2seq.py脚本中添加脚本... tensorflow \\ contrib \\ _delav_seq2seq \\ python \\ ops,如同类似问题的答案所示,但这并没有解决我的问题。

My code: 我的代码:

def train_network():
    conf = get_train_conf()
    epochs = conf['epochs']
    batch_size = conf['batch_size']

    train_path = get_data_path('train')
    validation_path = get_data_path('valid')
    print('data input paths are:\n {}\n and:\n {}'.format(train_path, 
    validation_path))

    print('counting samples..')
    print('number of train, validation samples are:')
    print(count_samples(train_path), count_samples(validation_path))

    # train data
    image_generator_train = 
    image.ImageDataGenerator(**conf['augmentation_args'])
    mask_generator_train = 
    image.ImageDataGenerator(**conf['augmentation_args'])
    train_generator = get_generator(batch_size, train_path + '/unmasked', 
    True, image_generator_train, color_mode='grayscale')
    train_generator_mask = get_generator(batch_size, train_path + '/masked', 
    True, mask_generator_train, color_mode=conf['mask_color_mode'])
    train_gen = zip(train_generator, train_generator_mask)
    num_train_samples = train_generator.n

    # validation data - don't augment
    image_generator_validation = image.ImageDataGenerator()
    mask_generator_validation = image.ImageDataGenerator()
    valid_generator = get_generator(batch_size, validation_path + '/unmasked', 
    True, image_generator_validation, color_mode='grayscale')
    valid_generator_mask = get_generator(batch_size, validation_path + 
    '/masked', True, mask_generator_validation, 
    color_mode=conf['mask_color_mode'])
    valid_gen = zip(valid_generator, valid_generator_mask)
    num_valid_samples = valid_generator.n

    steps_per_epoch, validation_steps = (conf['steps_per_epoch'], 
    conf['validation_steps'])
    print('{} rounds of training will take place'.format(epochs))
    print('{} training steps per round'.format(steps_per_epoch))
    print('{} validation steps per round'.format(validation_steps))
    print('(with a batch size of {})'.format(batch_size))
    print('{} training samples will be generated per epoch'.format(batch_size 
    * steps_per_epoch))
    print('{} validation samples will be generated per 
    epoch'.format(batch_size * validation_steps))
    answer = input("would you like to continue? y/n?\n").lower()
    if not answer.lower() in ('yes', 'y'):
        return

    model = None
    print(conf)
    if conf['train_from_checkpoint']:
        print('loading model from last checkpoint')
        model = 
    pw_evaluate.load_model_from_wts(pw_evaluate.get_latest_model_file_name())
    else:
        model = unet(classes=conf['num_classes'])
        #model = get_simple_unet()

    model.compile(
        optimizer=keras.optimizers.Adam(
        lr=conf['learning_rate']),
        loss=conf['loss_function'],
        metrics=conf['metrics'])

    #model.compile(optimizer=keras.optimizers.Adam(2e-4), 
    loss=pw_loss.dice_coef_loss, metrics=conf['metrics'])

    time_st = str(datetime.utcfromtimestamp(time.time()).strftime('%Y-%m-%d 
    %H:%M:%S'))
    weight_saver = ModelCheckpoint('weights.' + time_st + '.{epoch:02d}- 
    {val_loss:.2f}.h5',
        save_best_only=True,
        save_weights_only=True,
        verbose=1,
        period=conf['period'])

    hist = model.fit_generator(
        train_gen,
        validation_data=valid_gen,
        validation_steps=validation_steps,
        steps_per_epoch=steps_per_epoch,
        epochs=epochs,
        callbacks=[weight_saver],
        verbose=1,
        use_multiprocessing=True)

    plot_file_name = 'training_plot_' + time_st + '.pdf'
    print('finished training, plotting history to:')
    print(plot_file_name)
    figures = [loss_figure(hist), accuracy_figure(hist)]
    plot_history(plot_file_name, figures)

    log_file_name = 'log_results_' + time_st + '.txt'
    print('logging results to:')
    print(log_file_name)
    log_results(hist, log_file_name, conf)

The full stacktrace: 完整的堆栈跟踪:

  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\utils\data_utils.py", line 666, in _run
    with closing(self.executor_fn(_SHARED_SEQUENCES)) as executor:
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\utils\data_utils.py", line 661, in <lambda>
    initargs=(seqs, self.random_seed))
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py", line 119, in Pool
    context=self.get_context())
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 176, in __init__
    self._repopulate_pool()
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 241, in _repopulate_pool
    w.start()
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
TypeError: can't pickle _thread.lock objects

Exception in thread Thread-29:
Traceback (most recent call last):
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\utils\data_utils.py", line 666, in _run
    with closing(self.executor_fn(_SHARED_SEQUENCES)) as executor:
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\utils\data_utils.py", line 661, in <lambda>
    initargs=(seqs, self.random_seed))
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py", line 119, in Pool
    context=self.get_context())
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 176, in __init__
    self._repopulate_pool()
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 241, in _repopulate_pool
    w.start()
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
TypeError: can't pickle _thread.lock objects

Using TensorFlow backend.
Using TensorFlow backend.
2019-06-04 11:45:59.121653: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2019-06-04 11:45:59.122454: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
Traceback (most recent call last):
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py", line 115, in _main
  File "<string>", line 1, in <module>
    self = reduction.pickle.load(from_parent)
EOFError: Ran out of input
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\Users\esp13\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py", line 115, in _main
    self = reduction.pickle.load(from_parent)
EOFError: Ran out of input

The pickle _thread.lock -error origniated from the use_multiprocessing=True -parameter in the fit_generator() -method. pickle _thread.lock -error来自fit_generator() -method中的use_multiprocessing=True -parameter。 Presumably, the root cause was the usage of a generator which was not thread-safe; 据推测,根本原因是使用不是线程安全的发电机; thus, multpile processes tried to alter the same data. 因此,多堆进程试图改变相同的数据。

Disabling this by setting use_multiprocessing=False helped to avoid the error. 通过设置use_multiprocessing=False来禁用此功能有助于避免错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras模型:TypeError:无法腌制_thread.lock对象 - Keras model: TypeError: can't pickle _thread.lock objects Keras:TypeError:无法使用KerasClassifier来pickle _thread.lock对象 - Keras: TypeError: can't pickle _thread.lock objects with KerasClassifier Keras 2,TypeError:无法pickle _thread.lock对象 - Keras 2, TypeError: can't pickle _thread.lock objects TypeError:从Keras-Openface项目中转储nn4_small2_pretrained模型时无法腌制_thread.lock对象 - TypeError: can't pickle _thread.lock objects while dumping nn4_small2_pretrained model from Keras-Openface Project Keras Lambda图层和变量:“TypeError:无法pickle _thread.lock对象” - Keras Lambda layer and variables : “TypeError: can't pickle _thread.lock objects” Joblib错误:TypeError:无法腌制_thread.lock对象 - Joblib error: TypeError: can't pickle _thread.lock objects 收到TypeError:无法腌制_thread.lock对象 - Getting TypeError: can't pickle _thread.lock objects 使用 Queue() 进行多处理:TypeError: can't pickle _thread.lock objects - Multiprocessing with Queue(): TypeError: can't pickle _thread.lock objects 在将Queue传递给子进程中的线程时,如何解决“ TypeError:无法腌制_thread.lock对象” - How to fix 'TypeError: can't pickle _thread.lock objects' when passing a Queue to a thread in a child process 当joblib具有&gt; = 2个作业时,keras.wrappers无法使_thread.lock对象腌制 - keras.wrappers can't pickle _thread.lock objects when joblib has >= 2 jobs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM