EnforceNotMet: Invoke operator conv2d error.
Created by: 1286667673
代码及报错信息如下: import random import cv2 import os import math import paddle import numpy as np import paddle.fluid as fluid
class FCNN: def init(self,dataset): self.dataset = dataset self.LEARNING_RATE = 1e-4 def data_pre_train(self,kind,dataset): img_path = './data/data8297/formatted_trainval_gray_resize1/shanghaitech_part_' + dataset + 'patches_9/' + kind + '/' print(img_path) den_path = './data/data8297/formatted_trainval_gray_resize1/shanghaitech_part' + dataset + '_patches_9/' + kind + '_den/' img_names = os.listdir(img_path) img_num = len(img_names)
data = []
for i in range(1, img_num + 1):
print(i)
if i % 100 == 0:
print(i, '/', img_num)
name = img_names[i - 1]
img = cv2.imread(img_path + name, 0)
height, width = img.shape[:2]
size = (int(width * 0.4), int(height * 0.4))
img = cv2.resize(img, size, interpolation=cv2.INTER_AREA)
img = (img - 127.5) / 128
img = np.array(img)
den = np.loadtxt(open(den_path + name[:-4] + '.csv'), delimiter=",")
den_quarter = np.zeros((math.ceil(den.shape[0] / 4),math.ceil(den.shape[1] / 4)))
for i in range(len(den_quarter)-1):
for j in range(len(den_quarter[0])-1):
for p in range(4):
for q in range(4):
den_quarter[i][j] += den[i * 4 + p][j * 4 + q]
data.append([img, den_quarter])
print('load dataset finished')
return data
def conv2d_neural_network(self,img):
# 第一个卷积-池化层
S_conv_1 = fluid.layers.conv2d(input=img,filter_size=5,num_filters=24,act="relu")
S_conv_pool_1 = fluid.layers.pool2d(input=S_conv_1, pool_size=2, pool_stride=2)
# 第二个卷积-池化层
S_conv_2 = fluid.layers.conv2d(input=S_conv_pool_1,filter_size=3,num_filters=48,act="relu")
S_conv_pool_2 = fluid.layers.pool2d(input=S_conv_2, pool_size=2, pool_stride=2)
# 第三个卷积
S_conv_pool_3 = fluid.layers.conv2d(input =S_conv_pool_2,num_filters=24,filter_size=3,act="relu")
# 第四个卷积
S_conv_pool_4 = fluid.layers.conv2d(input =S_conv_pool_3,num_filters=12,filter_size=3,act="relu")
y_predict = fluid.layers.conv2d(input =S_conv_pool_4,num_filters=12,filter_size=1,stride=1)
return y_predict
def train(self,max_epoch):
model_save_dir = '/home/aistudio/data/data8297/MODELA/'
#定义输入数据的占位符
image= fluid.layers.data(name = 'image',shape = [-1,1,780,640], dtype='float32')
y_act = fluid.layers.data(name='y_act', shape=[-1,1,480,360], dtype='float32')
y_pre = self.conv2d_neural_network(image)
loss =fluid.layers.sqrt(fluid.layers.reduce_mean(fluid.layers.square_error_cost(input=y_act,label=y_pre)))
avg_loss = fluid.layers.mean(loss)
act_sum = fluid.layers.reduce_mean(y_act)
pre_sum = fluid.layers.reduce_mean(y_pre)
MAE = fluid.layers.abs(act_sum - pre_sum)
optimizer = fluid.optimizer.Adam(learning_rate=0.001)
optimizer.minimize(avg_loss)
data_train = self.data_pre_train('train', self.dataset)
data_val = self.data_pre_train('val', self.dataset)
#main_program = fluid.default_main_program()
#startup_program = fluid.default_startup_program()
#exe = fluid.Executor()
place = fluid.CPUPlace()
exe = fluid.Executor(place)
# 进行参数初始化
exe.run(fluid.default_startup_program())
main_program = fluid.default_main_program()
best_mae = 10000
for epoch in range(max_epoch):
#training process
epoch_mae = 0
random.shuffle(data_train)
# 创建一个使用CPU的执行器
for i in range(len(data_train)):
data = data_train[i]
print(data)
#x_in = np.array(data[0]).reshape(1, 1, data[0].shape[0], data[0].shape[1])
#y_ground = np.array(data[1]).reshape(1, 1, data[1].shape[0], data[1].shape[1])
x_in = np.reshape(data[0], (-1,1, data[0].shape[0], data[0].shape[1]))
y_ground = np.reshape(data[1], (-1, 1,data[1].shape[0], data[1].shape[1]))
loss,y_pre, act_sum, pre_sum,MAE = exe.run(main_program,
feed={'image':x_in,'y_act':y_ground},
fetch_list=[avg_loss,y_pre, act_sum, pre_sum,MAE])
#_,loss = exe.run(program=fluid.default_main_program(),
# feed={'image':x_in,'y_act':y_ground},
# fetch_list=[train_step,loss])
if i % 500 == 0:
print('epoch', epoch, 'step', i, 'mae:', m)
epoch_mae += m
epoch_mae /= len(data_train)
print('epoch', epoch + 1, 'train_mae:', epoch_mae)
#validation process
val_mae = 0
val_mse = 0
for i in range(len(data_val)):
data = data_val[i]
x_in = np.reshape(data[0], (1, data[0].shape[0], data[0].shape[1], 1))
y_ground = np.reshape(data[1], (1, data[1].shape[0], data[1].shape[1], 1))
act_s, pre_s, m = exe.run(main_program,
feed=feeder.feed(x_in,y_ground),
fetch_list=[act_sum, pre_sum,MAE])
val_mae += m
val_mse += (act_s - pre_s) * (act_s - pre_s)
val_mae /= len(data_val)
val_mse = math.sqrt(val_mse / len(data_val))
print('epoch', epoch, 'valid_mae:', val_mae, 'valid_mse:', val_mse)
if val_mae < best_mae:
best_mae = val_mae
print('best mae so far, saving model.')
if not os.path.exists(model_save_dir):
os.makedirs(model_save_dir)
#print 'save models to %s' % (model_path)
# 保存模型
fluid.io.save_inference_model(model_path, ['img'], [predict], exe)
else:
print('best mae:', best_mae)
print('**************************')
EPOCH = 100 fcnn = FCNN('A') fcnn.train(EPOCH)
140 EPOCH = 100 141 fcnn = FCNN('A') --> 142 fcnn.train(EPOCH) in train(self, max_epoch) 99 loss,y_pre, act_sum, pre_sum,MAE = exe.run(main_program, 100 feed={'image':x_in,'y_act':y_ground}, --> 101 fetch_list=[avg_loss,y_pre, act_sum, pre_sum,MAE]) 102 #,loss = exe.run(program=fluid.default_main_program(), 103 # feed={'image':x_in,'y_act':y_ground}, /opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/paddle/fluid/executor.py in run(self, program, feed, fetch_list, feed_var_name, fetch_var_name, scope, return_numpy, use_program_cache) 563 scope=scope, 564 return_numpy=return_numpy, --> 565 use_program_cache=use_program_cache) 566 else: 567 if fetch_list and program.is_data_parallel and program.program and ( /opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/paddle/fluid/executor.py in run(self, program, exe, feed, fetch_list, feed_var_name, fetch_var_name, scope, return_numpy, use_program_cache) 640 641 self.feed_data(program, feed, feed_var_name, scope) --> 642 exe.run(program.desc, scope, 0, True, True, fetch_var_name) 643 outs = self.fetch_data(fetch_list, fetch_var_name, scope) 644 if return_numpy: EnforceNotMet: Invoke operator conv2d error. Python Callstacks: File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/paddle/fluid/framework.py", line 1654, in append_op attrs=kwargs.get("attrs", None)) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/paddle/fluid/layer_helper.py", line 43, in append_op return self.main_program.current_block().append_op(*args, **kwargs) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/paddle/fluid/layers/nn.py", line 2073, in conv2d 'fuse_relu_before_depthwise_conv': False File "", line 44, in conv2d_neural_network S_conv_1 = fluid.layers.conv2d(input=img,filter_size=5,num_filters=24,act="relu") File "", line 62, in train y_pre = self.conv2d_neural_network(image) File "", line 139, in fcnn.train(EPOCH) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 3265, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 3189, in run_ast_nodes if (yield from self.run_code(code, result)): File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 3018, in run_cell_async interactivity=interactivity, compiler=compiler, result=result) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/IPython/core/async_helpers.py", line 67, in pseudo_sync_runner coro.send(None) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2843, in run_cell return runner(coro) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2817, in run_cell raw_cell, store_history, silent, shell_futures) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/ipykernel/zmqshell.py", line 536, in run_cell return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/ipykernel/ipkernel.py", line 294, in do_execute res = shell.run_cell(code, store_history=store_history, silent=silent) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/tornado/gen.py", line 326, in wrapper yielded = next(result) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 534, in execute_request user_expressions, allow_stdin, File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/tornado/gen.py", line 326, in wrapper yielded = next(result) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 267, in dispatch_shell yield gen.maybe_future(handler(stream, idents, msg)) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/tornado/gen.py", line 326, in wrapper yielded = next(result) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 357, in process_one yield gen.maybe_future(dispatch(*args)) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/tornado/gen.py", line 1147, in run yielded = self.gen.send(value) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/tornado/gen.py", line 1233, in inner self.run() File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/tornado/stack_context.py", line 300, in null_wrapper return fn(*args, *kwargs) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/tornado/ioloop.py", line 758, in _run_callback ret = callback() File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/asyncio/events.py", line 127, in _run self._callback(self._args) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/asyncio/base_events.py", line 1425, in _run_once handle._run() File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/asyncio/base_events.py", line 421, in run_forever self._run_once() File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/tornado/platform/asyncio.py", line 132, in start self.asyncio_loop.run_forever() File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 505, in start self.io_loop.start() File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/traitlets/config/application.py", line 658, in launch_instance app.start() File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/ipykernel_launcher.py", line 16, in app.launch_new_instance() File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/runpy.py", line 85, in _run_code exec(code, run_globals) File "/opt/conda/envs/python35-paddle120-env/lib/python3.5/runpy.py", line 193, in _run_module_as_main "main", mod_spec) C++ Callstacks: Enforce failed. Expected input_data_type == filter_data_type, but received input_data_type:6 != filter_data_type:5. input and filter data type should be consistent at [/paddle/paddle/fluid/operators/conv_op.cc:116] PaddlePaddle Call Stacks: 0 0x7f6841658c68p void paddle::platform::EnforceNotMet::Initstd::string(std::string, char const, int) + 360 1 0x7f6841658fb7p paddle::platform::EnforceNotMet::EnforceNotMet(std::string const&, char const, int) + 87 2 0x7f6841e01122p paddle::operators::ConvOp::GetExpectedKernelType(paddle::framework::ExecutionContext const&) const + 1842 3 0x7f68428048f3p paddle::framework::OperatorWithKernel::RunImpl(paddle::framework::Scope const&, boost::variant<paddle::platform::CUDAPlace, paddle::platform::CPUPlace, paddle::platform::CUDAPinnedPlace, boost::detail::variant::void, boost::detail::variant::void, boost::detail::variant::void, boost::detail::variant::void, boost::detail::variant::void, boost::detail::variant::void, boost::detail::variant::void, boost::detail::variant::void, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> const&, paddle::framework::RuntimeContext*) const + 243 4 0x7f6842805594p paddle::framework::OperatorWithKernel::RunImpl(paddle::framework::Scope const&, boost::variant<paddle::platform::CUDAPlace, paddle::platform::CPUPlace, paddle::platform::CUDAPinnedPlace, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> const&) const + 292 5 0x7f684280376bp paddle::framework::OperatorBase::Run(paddle::framework::Scope const&, boost::variant<paddle::platform::CUDAPlace, paddle::platform::CPUPlace, paddle::platform::CUDAPinnedPlace, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> const&) + 267 6 0x7f68417bdf3ep paddle::framework::Executor::RunPreparedContext(paddle::framework::ExecutorPrepareContext*, paddle::framework::Scope*, bool, bool, bool) + 158 7 0x7f68417c0f1fp paddle::framework::Executor::Run(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool, std::vector<std::string, std::allocatorstd::string > const&, bool) + 143 8 0x7f68416493cep 9 0x7f68416880fep 10 0x7f6884c1d199p PyCFunction_Call + 233 11 0x7f6884cb83f9p PyEval_EvalFrameEx + 33545 12 0x7f6884cba4b6p 13 0x7f6884cb75b5p PyEval_EvalFrameEx + 29893 14 0x7f6884cba4b6p 15 0x7f6884cb75b5p PyEval_EvalFrameEx + 29893 16 0x7f6884cb81d0p PyEval_EvalFrameEx + 32992 17 0x7f6884cba4b6p 18 0x7f6884cba5a8p PyEval_EvalCodeEx + 72 19 0x7f6884cba5ebp PyEval_EvalCode + 59 20 0x7f6884cadc5dp 21 0x7f6884c1d179p PyCFunction_Call + 201 22 0x7f6884cb7dbep PyEval_EvalFrameEx + 31950 23 0x7f6884bf1410p _PyGen_Send + 128 24 0x7f6884cb6953p PyEval_EvalFrameEx + 26723 25 0x7f6884bf1410p _PyGen_Send + 128 26 0x7f6884cb6953p PyEval_EvalFrameEx + 26723 27 0x7f6884bf1410p _PyGen_Send + 128 28 0x7f6884cb7d60p PyEval_EvalFrameEx + 31856 29 0x7f6884cb81d0p PyEval_EvalFrameEx + 32992 30 0x7f6884cb81d0p PyEval_EvalFrameEx + 32992 31 0x7f6884cba4b6p 32 0x7f6884cba5a8p PyEval_EvalCodeEx + 72 33 0x7f6884bf9c33p 34 0x7f6884bc833ap PyObject_Call + 106 35 0x7f6884cb26eep PyEval_EvalFrameEx + 9726 36 0x7f6884cba4b6p 37 0x7f6884cb75b5p PyEval_EvalFrameEx + 29893 38 0x7f6884bf06bap 39 0x7f6884cabaf6p 40 0x7f6884c1d179p PyCFunction_Call + 201 41 0x7f6884cb7dbep PyEval_EvalFrameEx + 31950 42 0x7f6884cba4b6p 43 0x7f6884cb75b5p PyEval_EvalFrameEx + 29893 44 0x7f6884bf06bap 45 0x7f6884cabaf6p 46 0x7f6884c1d179p PyCFunction_Call + 201 47 0x7f6884cb7dbep PyEval_EvalFrameEx + 31950 48 0x7f6884cba4b6p 49 0x7f6884cb75b5p PyEval_EvalFrameEx + 29893 50 0x7f6884bf06bap 51 0x7f6884cabaf6p 52 0x7f6884c1d179p PyCFunction_Call + 201 53 0x7f6884cb7dbep PyEval_EvalFrameEx + 31950 54 0x7f6884cba4b6p 55 0x7f6884cba5a8p PyEval_EvalCodeEx + 72 56 0x7f6884bf9b56p 57 0x7f6884bc833ap PyObject_Call + 106 58 0x7f6884cb26eep PyEval_EvalFrameEx + 9726 59 0x7f6884bf1410p _PyGen_Send + 128 60 0x7f6884cb7d60p PyEval_EvalFrameEx + 31856 61 0x7f6884cb81d0p PyEval_EvalFrameEx + 32992 62 0x7f6884cba4b6p 63 0x7f6884cba5a8p PyEval_EvalCodeEx + 72 64 0x7f6884bf9c33p 65 0x7f6884bc833ap PyObject_Call + 106 66 0x7f6884cb26eep PyEval_EvalFrameEx + 9726 67 0x7f6884cba4b6p 68 0x7f6884cba5a8p PyEval_EvalCodeEx + 72 69 0x7f6884bf9b56p 70 0x7f6884bc833ap PyObject_Call + 106 71 0x7f6884d2dccap 72 0x7f6884bc833ap PyObject_Call + 106 73 0x7f6884cb44c5p PyEval_EvalFrameEx + 17365 74 0x7f6884cba4b6p 75 0x7f6884cba5a8p PyEval_EvalCodeEx + 72 76 0x7f6884bf9b56p 77 0x7f6884bc833ap PyObject_Call + 106 78 0x7f6884cb26eep PyEval_EvalFrameEx + 9726 79 0x7f6884cb81d0p PyEval_EvalFrameEx + 32992 80 0x7f6884cb81d0p PyEval_EvalFrameEx + 32992 81 0x7f6884cb81d0p PyEval_EvalFrameEx + 32992 82 0x7f6884cb81d0p PyEval_EvalFrameEx + 32992 83 0x7f6884cb81d0p PyEval_EvalFrameEx + 32992 84 0x7f6884cba4b6p 85 0x7f6884cb75b5p PyEval_EvalFrameEx + 29893 86 0x7f6884cba4b6p 87 0x7f6884cba5a8p PyEval_EvalCodeEx + 72 88 0x7f6884cba5ebp PyEval_EvalCode + 59 89 0x7f6884cadc5dp 90 0x7f6884c1d179p PyCFunction_Call + 201 91 0x7f6884cb7dbep PyEval_EvalFrameEx + 31950 92 0x7f6884cba4b6p 93 0x7f6884cb75b5p PyEval_EvalFrameEx + 29893 94 0x7f6884cba4b6p 95 0x7f6884cba5a8p PyEval_EvalCodeEx + 72 96 0x7f6884bf9b56p 97 0x7f6884bc833ap PyObject_Call + 106 98 0x7f6884d06ba1p 99 0x7f6884d074a5p Py_Main + 1493