Skip to content

uint8 利用cast层转换为float32的疑问 #11725

@dyning

Description

@dyning

@panyx0718

程序如下,初始化一个float32的numpy,然后通过numpy转为uint8,之后输入到paddle,通过cast转回float32,但是最终转换后的值和原始float32的值有diff,不知什么原因?
输出:
data numpy float32: [-148. -148. -148.]
data numpy uint8: [108 108 108]
paddle float32: [108. 108. 108.]

程序:
import paddle
import paddle.fluid as fluid
data = fluid.layers.data(name='data', shape=[3, 224, 224], dtype='uint8')
data_fl = fluid.layers.cast(x=data, dtype='float32')
#fluid.layers.Print(data_fl, summarize=10)
place = fluid.CUDAPlace(0)
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
img_blob = np.zeros((1, 224, 224, 3), dtype = np.float32)
img_blob = img_blob - 148
print "data numpy float32:", img_blob[0, 0, 0, 0:10]
img_blob = img_blob.astype(np.uint8)
print "data numpy uint8:", img_blob[0, 0, 0, 0:10]
img_blob_data = fluid.LoDTensor()
img_blob_data.set(img_blob, place)
result = exe.run(fluid.default_main_program(), feed={'data':img_blob_data}, fetch_list=[data_fl.name])
print "paddle float32:", result[0][0, 0, 0, 0:10]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions