Paddle
Paddle copied to clipboard
elementwise_max传入的数据类型不一致时,没有做处理
elementwise_max传入的数据类型不一致时,没有做处理,比如int和float,float32和float64等
import numpy as np
import paddle.fluid as fluid
def get_img():
prediction = np.random.rand(3, img_h, img_w)
gt = (np.random.rand(1, img_h, img_w) * 3).astype('int16')
return prediction, gt
def get_batch_generator(batch_size, total_step=1):
def do_get_batch():
for i in range(total_step):
predictions = []
gts = []
for j in range(batch_size):
prediction, gt = get_img()
predictions.append(prediction)
gts.append(gt)
yield np.array(predictions), np.array(gts)
batches = do_get_batch()
return batches
def test_eltwise_op(input1, input2):
input_2_expand = fluid.layers.expand(input2, [1, 3, 1, 1])
print input1.shape, type(input1)
print input_2_expand.shape, type(input_2_expand)
out_put = fluid.layers.elementwise_max(input1, input_2_expand.astype('float32'))
return out_put
def test_eltwise_op_2(input1):
out_put = fluid.layers.elementwise_max(input1, (fluid.layers.assign(np.array([0.00001]))))
return out_put
if __name__ == '__main__':
img_w = 5
img_h = 4
prediction = fluid.layers.data(name='prediction', shape=[3, img_h, img_w], dtype='float32')
gt = fluid.layers.data(name='gt', shape=[1, img_h, img_w], dtype='float32')
out_put = test_eltwise_op(prediction, gt)
batches = get_batch_generator(batch_size=1)
place = fluid.CPUPlace()
feeder = fluid.DataFeeder(place=place, feed_list=[prediction, gt])
fetch_list = [out_put]
exe = fluid.Executor(place)
for batch in batches:
prediction = batch[0]
gt = batch[1]
res_fetch_list = exe.run(feed={'prediction': prediction, 'gt': gt}, fetch_list=fetch_list)
# print("paddle res_fetch_list={}".format(res_fetch_list))