Enforce failed. Expected x_dims[i + axis] == y_dims[i], but received x_dims[i + axis]:1 != y_dims[i]:12.
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 load_image(self,path): img = cv2.imread(path) # 转灰度 img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # 转换成numpy值 img = np.array(img).astype(np.float32) 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 / 255.0 return img 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 = self.load_image(img_path + name)
#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,10000,10000], dtype='float32')
y_act = fluid.layers.data(name='y_act', shape=[-1,1,10000,10000], 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(loss)
data_train = self.data_pre_train('train', self.dataset)
data_val = self.data_pre_train('val', self.dataset)
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.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]))
y_ground.dtype = 'float32'
print(x_in.shape)
print(y_ground.shape)
loss,y_pre, act_sum, pre_sum,MAE = exe.run(main_program,
feed={image.name:x_in,y_act.name:y_ground},
fetch_list=[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)
错误信息: 图片及标签的shape是下面这两个: (1, 1, 432, 768) (1, 1, 108, 384) ---------------------------------------------------------------------------EnforceNotMet Traceback (most recent call last) in 144 EPOCH = 100 145 fcnn = FCNN('A') --> 146 fcnn.train(EPOCH) in train(self, max_epoch) 103 loss,y_pre, act_sum, pre_sum,MAE = exe.run(main_program, 104 feed={image.name:x_in,y_act.name:y_ground}, --> 105 fetch_list=[loss,y_pre, act_sum, pre_sum,MAE]) 106 #,loss = exe.run(program=fluid.default_main_program(), 107 # 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 elementwise_sub 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 1580, in square_error_cost outputs={'Out': [minus_out]}) File "", line 72, in train loss =fluid.layers.sqrt(fluid.layers.reduce_mean(fluid.layers.square_error_cost(input=y_act,label=y_pre))) File "", line 143, 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 x_dims[i + axis] == y_dims[i], but received x_dims[i + axis]:1 != y_dims[i]:12. Broadcast dimension mismatch. at [/paddle/paddle/fluid/operators/elementwise/elementwise_op_function.h:63] PaddlePaddle Call Stacks: 0 0x7f23be6afc68p void paddle::platform::EnforceNotMet::Initstd::string(std::string, char const, int) + 360 1 0x7f23be6affb7p paddle::platform::EnforceNotMet::EnforceNotMet(std::string const&, char const, int) + 87 2 0x7f23bef14c0cp paddle::operators::get_mid_dims(paddle::framework::DDim const&, paddle::framework::DDim const&, int, int, int, int) + 364 3 0x7f23bef19548p void paddle::operators::ElementwiseComputeEx<paddle::operators::SubFunctor, paddle::platform::CPUDeviceContext, float, float>(paddle::framework::ExecutionContext const&, paddle::framework::Tensor const, paddle::framework::Tensor const, int, paddle::operators::SubFunctor, paddle::framework::Tensor) + 808 4 0x7f23bf2cddb5p paddle::operators::ElementwiseSubKernel<paddle::platform::CPUDeviceContext, float>::Compute(paddle::framework::ExecutionContext const&) const + 421 5 0x7f23bf2ce013p std::_Function_handler<void (paddle::framework::ExecutionContext const&), paddle::framework::OpKernelRegistrarFunctor<paddle::platform::CPUPlace, false, 0ul, paddle::operators::ElementwiseSubKernel<paddle::platform::CPUDeviceContext, float>, paddle::operators::ElementwiseSubKernel<paddle::platform::CPUDeviceContext, double>, paddle::operators::ElementwiseSubKernel<paddle::platform::CPUDeviceContext, int>, paddle::operators::ElementwiseSubKernel<paddle::platform::CPUDeviceContext, long> >::operator()(char const, char const, int) const::{lambda(paddle::framework::ExecutionContext const&)#1 (closed)}>::M_invoke(std::Any_data const&, paddle::framework::ExecutionContext const&) + 35 6 0x7f23bf85ba96p 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 + 662 7 0x7f23bf85c594p 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 8 0x7f23bf85a76bp 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 9 0x7f23be814f3ep paddle::framework::Executor::RunPreparedContext(paddle::framework::ExecutorPrepareContext*, paddle::framework::Scope*, bool, bool, bool) + 158 10 0x7f23be817f1fp paddle::framework::Executor::Run(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool, std::vector<std::string, std::allocatorstd::string > const&, bool) + 143 11 0x7f23be6a03cep 12 0x7f23be6df0fep 13 0x7f2401c40199p PyCFunction_Call + 233 14 0x7f2401cdb3f9p PyEval_EvalFrameEx + 33545 15 0x7f2401cdd4b6p 16 0x7f2401cda5b5p PyEval_EvalFrameEx + 29893 17 0x7f2401cdd4b6p 18 0x7f2401cda5b5p PyEval_EvalFrameEx + 29893 19 0x7f2401cdb1d0p PyEval_EvalFrameEx + 32992 20 0x7f2401cdd4b6p 21 0x7f2401cdd5a8p PyEval_EvalCodeEx + 72 22 0x7f2401cdd5ebp PyEval_EvalCode + 59 23 0x7f2401cd0c5dp 24 0x7f2401c40179p PyCFunction_Call + 201 25 0x7f2401cdadbep PyEval_EvalFrameEx + 31950 26 0x7f2401c14410p _PyGen_Send + 128 27 0x7f2401cd9953p PyEval_EvalFrameEx + 26723 28 0x7f2401c14410p _PyGen_Send + 128 29 0x7f2401cd9953p PyEval_EvalFrameEx + 26723 30 0x7f2401c14410p _PyGen_Send + 128 31 0x7f2401cdad60p PyEval_EvalFrameEx + 31856 32 0x7f2401cdb1d0p PyEval_EvalFrameEx + 32992 33 0x7f2401cdb1d0p PyEval_EvalFrameEx + 32992 34 0x7f2401cdd4b6p 35 0x7f2401cdd5a8p PyEval_EvalCodeEx + 72 36 0x7f2401c1cc33p 37 0x7f2401beb33ap PyObject_Call + 106 38 0x7f2401cd56eep PyEval_EvalFrameEx + 9726 39 0x7f2401cdd4b6p 40 0x7f2401cda5b5p PyEval_EvalFrameEx + 29893 41 0x7f2401c136bap 42 0x7f2401cceaf6p 43 0x7f2401c40179p PyCFunction_Call + 201 44 0x7f2401cdadbep PyEval_EvalFrameEx + 31950 45 0x7f2401cdd4b6p 46 0x7f2401cda5b5p PyEval_EvalFrameEx + 29893 47 0x7f2401c136bap 48 0x7f2401cceaf6p 49 0x7f2401c40179p PyCFunction_Call + 201 50 0x7f2401cdadbep PyEval_EvalFrameEx + 31950 51 0x7f2401cdd4b6p 52 0x7f2401cda5b5p PyEval_EvalFrameEx + 29893 53 0x7f2401c136bap 54 0x7f2401cceaf6p 55 0x7f2401c40179p PyCFunction_Call + 201 56 0x7f2401cdadbep PyEval_EvalFrameEx + 31950 57 0x7f2401cdd4b6p 58 0x7f2401cdd5a8p PyEval_EvalCodeEx + 72 59 0x7f2401c1cb56p 60 0x7f2401beb33ap PyObject_Call + 106 61 0x7f2401cd56eep PyEval_EvalFrameEx + 9726 62 0x7f2401c14410p _PyGen_Send + 128 63 0x7f2401cdad60p PyEval_EvalFrameEx + 31856 64 0x7f2401cdb1d0p PyEval_EvalFrameEx + 32992 65 0x7f2401cdd4b6p 66 0x7f2401cdd5a8p PyEval_EvalCodeEx + 72 67 0x7f2401c1cc33p 68 0x7f2401beb33ap PyObject_Call + 106 69 0x7f2401cd56eep PyEval_EvalFrameEx + 9726 70 0x7f2401cdd4b6p 71 0x7f2401cdd5a8p PyEval_EvalCodeEx + 72 72 0x7f2401c1cb56p 73 0x7f2401beb33ap PyObject_Call + 106 74 0x7f2401d50ccap 75 0x7f2401beb33ap PyObject_Call + 106 76 0x7f2401cd74c5p PyEval_EvalFrameEx + 17365 77 0x7f2401cdd4b6p 78 0x7f2401cdd5a8p PyEval_EvalCodeEx + 72 79 0x7f2401c1cb56p 80 0x7f2401beb33ap PyObject_Call + 106 81 0x7f2401cd56eep PyEval_EvalFrameEx + 9726 82 0x7f2401cdb1d0p PyEval_EvalFrameEx + 32992 83 0x7f2401cdb1d0p PyEval_EvalFrameEx + 32992 84 0x7f2401cdb1d0p PyEval_EvalFrameEx + 32992 85 0x7f2401cdb1d0p PyEval_EvalFrameEx + 32992 86 0x7f2401cdb1d0p PyEval_EvalFrameEx + 32992 87 0x7f2401cdd4b6p 88 0x7f2401cda5b5p PyEval_EvalFrameEx + 29893 89 0x7f2401cdd4b6p 90 0x7f2401cdd5a8p PyEval_EvalCodeEx + 72 91 0x7f2401cdd5ebp PyEval_EvalCode + 59 92 0x7f2401cd0c5dp 93 0x7f2401c40179p PyCFunction_Call + 201 94 0x7f2401cdadbep PyEval_EvalFrameEx + 31950 95 0x7f2401cdd4b6p 96 0x7f2401cda5b5p PyEval_EvalFrameEx + 29893 97 0x7f2401cdd4b6p 98 0x7f2401cdd5a8p PyEval_EvalCodeEx + 72 99 0x7f2401c1cb56p