Python 类型错误:can';t pickle\u thread.RLock对象
在检查了关于Stackoverflow的所有现有答案后,我还没有发现为什么这不起作用,或者我的情况有什么问题 我正在使用Python3。以下是我的模型构建功能:Python 类型错误:can';t pickle\u thread.RLock对象,python,tensorflow,keras,conv-neural-network,pickle,Python,Tensorflow,Keras,Conv Neural Network,Pickle,在检查了关于Stackoverflow的所有现有答案后,我还没有发现为什么这不起作用,或者我的情况有什么问题 我正在使用Python3。以下是我的模型构建功能: def upsample_and_concat(x1,x2,output_channels,in_channels,layer): pool_size = 2 deconv_filter = tf.Variable(tf.truncated_normal([pool_size, pool_size, output_channel
def upsample_and_concat(x1,x2,output_channels,in_channels,layer):
pool_size = 2
deconv_filter = tf.Variable(tf.truncated_normal([pool_size, pool_size, output_channels, in_channels], stddev=0.02))
deconvtf=tf.nn.conv2d_transpose(x1, deconv_filter, tf.shape(x2), strides=[1, pool_size, pool_size, 1])
deconv_output = tf.concat([deconvtf, x2], 3)
deconv_output.set_shape([None, None, None, output_channels * 2])
return deconv_output
def Depth_to_space_tf(input):
return tf.depth_to_space(input, 2)
def build_model():
inputs=keras.layers.Input(shape=(None, None, 4))
conv1=keras.layers.Conv2D(filters=256,kernel_size=(3,3),strides=(1,1),padding='same',name='conv1_1')(inputs)
conv1=keras.layers.LeakyReLU(alpha=0.2,name='conv1_1_relu')(conv1)
conv1=keras.layers.Conv2D(filters=256,kernel_size=(3,3),strides=(1,1),padding='same',name='conv1_2')(conv1)
conv1=keras.layers.LeakyReLU(alpha=0.2,name='conv1_2_relu')(conv1)
pool1=keras.layers.MaxPooling2D(pool_size=(2,2),padding="same",name='pool1')(conv1)
conv2=keras.layers.Conv2D(filters=512,kernel_size=(3,3),strides=(1,1),padding='same',name='conv2_1')(pool1)
conv2=keras.layers.LeakyReLU(alpha=0.2,name='conv2_1_relu')(conv2)
conv2=keras.layers.Conv2D(filters=512,kernel_size=(3,3),strides=(1,1),padding='same',name='conv2_2')(conv2)
conv2=keras.layers.LeakyReLU(alpha=0.2,name='conv2_2_relu')(conv2)
up6=keras.layers.core.Lambda(upsample_and_concat,arguments={'x2':conv1,'output_channels':256,'in_channels':512,'layer':'upsample_concat_1'},name='upsample_concat_1')(conv2)
conv6=keras.layers.Conv2D(filters=256,kernel_size=(3,3),strides=(1,1),padding='same',name='conv6_1')(up6)
conv6=keras.layers.LeakyReLU(alpha=0.2,name='conv6_1_relu')(conv6)
conv6=keras.layers.Conv2D(filters=256,kernel_size=(3,3),strides=(1,1),padding='same',name='conv6_2')(conv6)
conv6=keras.layers.LeakyReLU(alpha=0.2,name='conv6_2_relu')(conv6)
conv7=keras.layers.Conv2D(filters=12,kernel_size=(1,1),strides=(1,1),name='conv10')(conv6)
predictions = keras.layers.core.Lambda(Depth_to_space_tf,name='depth_to_space')(conv7)
model = keras.models.Model(inputs=inputs, outputs=predictions)
return model
以下是我的数据加载程序代码:
class DataLoader(keras.utils.Sequence):
def __init__(self,params,data):
self.epochs=params.epochs
self.input_dir=params.input_dir
self.gt_dir=params.gt_dir
self.train_ids=data.train_ids
self.shuffled_ids=np.random.permutation(len(self.train_ids))
for index, val in np.ndenumerate(self.shuffled_ids):
print ('index:{}, image:{}'.format(index[0], val))
self.epoch_counter=0
self.ps=params.ps
def on_epoch_end(self):
self.shuffled_ids=np.random.permutation(len(self.train_ids))
print("in on epoch end")
def __len__(self):
'Denotes the number of batches per epoch'
return len(self.train_ids)
def __getitem__(self, ind):
'Generates data containing batch_size samples'
train_id = self.train_ids[self.shuffled_ids[ind]]
in_files = glob.glob(self.input_dir + '%05d_00*.ARW' % train_id)
in_path = in_files[np.random.randint(0, len(in_files))]
in_fn = os.path.basename(in_path)
gt_files = glob.glob(self.gt_dir + '%05d_00*.ARW' % train_id)
gt_path = gt_files[0]
gt_fn = os.path.basename(gt_path)
in_exposure = float(in_fn[9:-5])
gt_exposure = float(gt_fn[9:-5])
ratio = min(gt_exposure / in_exposure, 300)
st = time.time()
raw = rawpy.imread(in_path)
input_images = np.expand_dims(pack_raw(raw), axis=0) * ratio
gt_raw = rawpy.imread(gt_path)
im = gt_raw.postprocess(use_camera_wb=True,
half_size=False,
no_auto_bright=True, output_bps=16)
gt_images = np.expand_dims(np.float32(im / 65535.0),axis=0)
H = input_images.shape[1]
W = input_images.shape[2]
xx = np.random.randint(0, W - self.ps)
yy = np.random.randint(0, H - self.ps)
input_patch = input_images[:, yy:yy + self.ps, xx:xx + self.ps, :]
gt_patch = gt_images[:, yy * 2:yy * 2 + self.ps * 2, xx * 2:xx * 2 + self.ps * 2, :]
if np.random.randint(2) == 1:
input_patch = np.flip(input_patch, axis=1)
gt_patch = np.flip(gt_patch, axis=1)
if np.random.randint(2) == 1:
input_patch = np.flip(input_patch, axis=2)
gt_patch = np.flip(gt_patch, axis=2)
if np.random.randint(2) == 1:
input_patch = np.transpose(input_patch, (0, 2, 1, 3))
gt_patch = np.transpose(gt_patch, (0, 2, 1, 3))\
input_patch = np.minimum(input_patch, 1.0)
return (input_patch,gt_patch)
在model.fit_生成器中使用dataloader类的实例时
callbacks = [
ModelCheckpoint(filepath=save_fname, monitor='loss', verbose=1, save_best_only=True)
]
当模型试图将检查点保存到文件中时,我正好遇到以下错误:
TypeError:无法pickle\u thread.RLock对象
完整堆栈跟踪:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-52-fde8b84b2b2d> in <module>()
1 """ ================ TRAIN THE MODEL ================ """
2 steps_per_epoch=1
----> 3 model_history=train_generator(model,dataloader,params.epochs,steps_per_epoch,save_fname)
32 frames
<ipython-input-48-2a9e71ef3338> in train_generator(model, data_gen, epochs, steps_per_epoch, save_fname)
11 max_queue_size=20,
12 workers=7,
---> 13 use_multiprocessing=True
14 )
15 print("Model Training completed!!! Jay Yogeshwar!!!")
/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1416 use_multiprocessing=use_multiprocessing,
1417 shuffle=shuffle,
-> 1418 initial_epoch=initial_epoch)
1419
1420 @interfaces.legacy_generator_methods_support
/usr/local/lib/python3.6/dist-packages/keras/engine/training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
249 break
250
--> 251 callbacks.on_epoch_end(epoch, epoch_logs)
252 epoch += 1
253 if callback_model.stop_training:
/usr/local/lib/python3.6/dist-packages/keras/callbacks.py in on_epoch_end(self, epoch, logs)
77 logs = logs or {}
78 for callback in self.callbacks:
---> 79 callback.on_epoch_end(epoch, logs)
80
81 def on_batch_begin(self, batch, logs=None):
/usr/local/lib/python3.6/dist-packages/keras/callbacks.py in on_epoch_end(self, epoch, logs)
444 self.model.save_weights(filepath, overwrite=True)
445 else:
--> 446 self.model.save(filepath, overwrite=True)
447 else:
448 if self.verbose > 0:
/usr/local/lib/python3.6/dist-packages/keras/engine/network.py in save(self, filepath, overwrite, include_optimizer)
1088 raise NotImplementedError
1089 from ..models import save_model
-> 1090 save_model(self, filepath, overwrite, include_optimizer)
1091
1092 def save_weights(self, filepath, overwrite=True):
/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py in save_model(model, filepath, overwrite, include_optimizer)
380
381 try:
--> 382 _serialize_model(model, f, include_optimizer)
383 finally:
384 if opened_new_file:
/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py in _serialize_model(model, f, include_optimizer)
81 model_config = {}
82 model_config['class_name'] = model.__class__.__name__
---> 83 model_config['config'] = model.get_config()
84 model_config = json.dumps(model_config, default=get_json_type)
85 model_config = model_config.encode('utf-8')
/usr/local/lib/python3.6/dist-packages/keras/engine/network.py in get_config(self)
929 model_outputs.append([layer.name, new_node_index, tensor_index])
930 config['output_layers'] = model_outputs
--> 931 return copy.deepcopy(config)
932
933 @classmethod
/usr/lib/python3.6/copy.py in deepcopy(x, memo, _nil)
148 copier = _deepcopy_dispatch.get(cls)
149 if copier:
--> 150 y = copier(x, memo)
151 else:
152 try:
/usr/lib/python3.6/copy.py in _deepcopy_dict(x, memo, deepcopy)
238 memo[id(x)] = y
239 for key, value in x.items():
--> 240 y[deepcopy(key, memo)] = deepcopy(value, memo)
241 return y
242 d[dict] = _deepcopy_dict
/usr/lib/python3.6/copy.py in deepcopy(x, memo, _nil)
148 copier = _deepcopy_dispatch.get(cls)
149 if copier:
--> 150 y = copier(x, memo)
151 else:
152 try:
/usr/lib/python3.6/copy.py in _deepcopy_list(x, memo, deepcopy)
213 append = y.append
214 for a in x:
--> 215 append(deepcopy(a, memo))
216 return y
217 d[list] = _deepcopy_list
/usr/lib/python3.6/copy.py in deepcopy(x, memo, _nil)
148 copier = _deepcopy_dispatch.get(cls)
149 if copier:
--> 150 y = copier(x, memo)
151 else:
152 try:
/usr/lib/python3.6/copy.py in _deepcopy_dict(x, memo, deepcopy)
238 memo[id(x)] = y
239 for key, value in x.items():
--> 240 y[deepcopy(key, memo)] = deepcopy(value, memo)
241 return y
242 d[dict] = _deepcopy_dict
/usr/lib/python3.6/copy.py in deepcopy(x, memo, _nil)
148 copier = _deepcopy_dispatch.get(cls)
149 if copier:
--> 150 y = copier(x, memo)
151 else:
152 try:
/usr/lib/python3.6/copy.py in _deepcopy_dict(x, memo, deepcopy)
238 memo[id(x)] = y
239 for key, value in x.items():
--> 240 y[deepcopy(key, memo)] = deepcopy(value, memo)
241 return y
242 d[dict] = _deepcopy_dict
/usr/lib/python3.6/copy.py in deepcopy(x, memo, _nil)
148 copier = _deepcopy_dispatch.get(cls)
149 if copier:
--> 150 y = copier(x, memo)
151 else:
152 try:
/usr/lib/python3.6/copy.py in _deepcopy_dict(x, memo, deepcopy)
238 memo[id(x)] = y
239 for key, value in x.items():
--> 240 y[deepcopy(key, memo)] = deepcopy(value, memo)
241 return y
242 d[dict] = _deepcopy_dict
/usr/lib/python3.6/copy.py in deepcopy(x, memo, _nil)
178 y = x
179 else:
--> 180 y = _reconstruct(x, memo, *rv)
181
182 # If is its own copy, don't memoize.
/usr/lib/python3.6/copy.py in _reconstruct(x, memo, func, args, state, listiter, dictiter, deepcopy)
278 if state is not None:
279 if deep:
--> 280 state = deepcopy(state, memo)
281 if hasattr(y, '__setstate__'):
282 y.__setstate__(state)
/usr/lib/python3.6/copy.py in deepcopy(x, memo, _nil)
148 copier = _deepcopy_dispatch.get(cls)
149 if copier:
--> 150 y = copier(x, memo)
151 else:
152 try:
/usr/lib/python3.6/copy.py in _deepcopy_dict(x, memo, deepcopy)
238 memo[id(x)] = y
239 for key, value in x.items():
--> 240 y[deepcopy(key, memo)] = deepcopy(value, memo)
241 return y
242 d[dict] = _deepcopy_dict
/usr/lib/python3.6/copy.py in deepcopy(x, memo, _nil)
178 y = x
179 else:
--> 180 y = _reconstruct(x, memo, *rv)
181
182 # If is its own copy, don't memoize.
/usr/lib/python3.6/copy.py in _reconstruct(x, memo, func, args, state, listiter, dictiter, deepcopy)
278 if state is not None:
279 if deep:
--> 280 state = deepcopy(state, memo)
281 if hasattr(y, '__setstate__'):
282 y.__setstate__(state)
/usr/lib/python3.6/copy.py in deepcopy(x, memo, _nil)
148 copier = _deepcopy_dispatch.get(cls)
149 if copier:
--> 150 y = copier(x, memo)
151 else:
152 try:
/usr/lib/python3.6/copy.py in _deepcopy_dict(x, memo, deepcopy)
238 memo[id(x)] = y
239 for key, value in x.items():
--> 240 y[deepcopy(key, memo)] = deepcopy(value, memo)
241 return y
242 d[dict] = _deepcopy_dict
/usr/lib/python3.6/copy.py in deepcopy(x, memo, _nil)
178 y = x
179 else:
--> 180 y = _reconstruct(x, memo, *rv)
181
182 # If is its own copy, don't memoize.
/usr/lib/python3.6/copy.py in _reconstruct(x, memo, func, args, state, listiter, dictiter, deepcopy)
278 if state is not None:
279 if deep:
--> 280 state = deepcopy(state, memo)
281 if hasattr(y, '__setstate__'):
282 y.__setstate__(state)
/usr/lib/python3.6/copy.py in deepcopy(x, memo, _nil)
148 copier = _deepcopy_dispatch.get(cls)
149 if copier:
--> 150 y = copier(x, memo)
151 else:
152 try:
/usr/lib/python3.6/copy.py in _deepcopy_dict(x, memo, deepcopy)
238 memo[id(x)] = y
239 for key, value in x.items():
--> 240 y[deepcopy(key, memo)] = deepcopy(value, memo)
241 return y
242 d[dict] = _deepcopy_dict
/usr/lib/python3.6/copy.py in deepcopy(x, memo, _nil)
167 reductor = getattr(x, "__reduce_ex__", None)
168 if reductor:
--> 169 rv = reductor(4)
170 else:
171 reductor = getattr(x, "__reduce__", None)
TypeError: can't pickle _thread.RLock objects
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
1''”=====================================================================================================================================================================================================================
每个历元2个步骤=1
---->3模型历史=列车生成器(模型、数据加载器、参数历元、每个历元的步数、保存名称)
32帧
列车内发电机(型号、数据生成、历元、每历元步数、保存名称)
11最大队列大小=20,
12名工人=7名,
--->13使用多处理=真
14 )
15打印(“模特训练完成!!!杰伊·约格斯瓦尔!!!”)
/包装器中的usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py(*args,**kwargs)
89 warnings.warn('Update your`'+object\u name+'`调用+
90'Keras 2 API:'+签名,堆栈级别=2)
--->91返回函数(*args,**kwargs)
92包装器._原始函数=func
93返回包装器
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py-in-fit\u生成器(self、生成器、每个历元的步骤、历元、冗余、回调、验证数据、验证步骤、类权重、最大队列大小、工人、使用多处理、无序、初始历元)
1416使用多处理=使用多处理,
1417洗牌=洗牌,
->1418初始_历元=初始_历元)
1419
1420@interfaces.legacy\u生成器\u方法\u支持
/usr/local/lib/python3.6/dist-packages/keras/engine/training\u generator.py in-fit\u generator(模型、生成器、每个历元的步骤、历元、冗余、回调、验证数据、验证步骤、类权重、最大队列大小、工作人员、使用多处理、无序、初始历元)
249休息
250
-->251回调。在\u epoch\u结束时(epoch,epoch\u日志)
252时代+=1
253如果回调\u model.stop\u培训:
/usr/local/lib/python3.6/dist-packages/keras/callbacks.py in_epoch_end(self、epoch、logs)
77日志=日志或{}
78对于self.callbacks中的回调:
--->79.回拨。在\u epoch\u结束时(epoch,日志)
80
81批次开始时的def(自身、批次、日志=无):
/usr/local/lib/python3.6/dist-packages/keras/callbacks.py in_epoch_end(self、epoch、logs)
444 self.model.save_权重(filepath,overwrite=True)
445其他:
-->446 self.model.save(filepath,overwrite=True)
447其他:
448如果self.verbose>0:
/保存中的usr/local/lib/python3.6/dist-packages/keras/engine/network.py(self、filepath、overwrite、include\u optimizer)
1088升起未执行错误
1089 from..models导入save_model
->1090保存\u模型(自身、文件路径、覆盖、包含\u优化器)
1091
1092 def保存权重(self、filepath、overwrite=True):
/save_模型中的usr/local/lib/python3.6/dist-packages/keras/engine/saving.py(模型、文件路径、覆盖、包含_优化器)
380
381试试:
-->382 _序列化_模型(模型,f,包含_优化器)
383最后:
384如果打开新文件:
/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py在序列化模型中(模型,f,包含优化器)
81型号_配置={}
82模型配置['class\u name']=模型__
--->83 model_config['config']=model.get_config()
84 model\u config=json.dumps(model\u config,缺省值=get\u json\u type)
85型号配置=型号配置编码('utf-8')
/get_config(self)中的usr/local/lib/python3.6/dist-packages/keras/engine/network.py
929 model_outputs.append([layer.name,new_node_index,tensor_index])
930配置['output_layers']=模型_输出
-->931返回副本。deepcopy(配置)
932
933@classmethod
/deepcopy中的usr/lib/python3.6/copy.py(x,memo,_nil)
148复印机=_deepcopy_dispatch.get(cls)
149如果是复印机:
-->150 y=复印机(x,备忘)
151其他:
请尝试:
/usr/lib/python3.6/copy.py in_deepcopy_dict(x,memo,deepcopy)
238备忘录[id(x)]=y
239对于键,值为x.items():
-->240 y[深度复制(键,备忘录)]=深度复制(值,备忘录)
241返回y
242 d[dict]=\u deepcopy\u dict
/deepcopy中的usr/lib/python3.6/copy.py(x,memo,_nil)
148复印机=_deepcopy_dispatch.get(cls)
149如果是复印机:
-->150 y=复印机(x,备忘)
151其他:
请尝试:
/usr/lib/python3.6/copy.py在deepcopy列表中(x,memo,deepcopy)
213 append=y.append
214对于x中的a:
-->215追加(副本(备忘录))
216返回y
217 d[列表]=\u深度复制\u列表
/deepcopy中的usr/lib/python3.6/copy.py(x,memo,_nil)
148复印机=_deepcopy_dispatch.get(cls)
149如果是复印机:
-->150 y=复印机(x,备忘)
151其他:
请尝试:
/usr/lib/python3.6/copy.py在deepcopy目录中