Streamlit : TypeError: a bytes-like object is required, not 'Tensor'

Streamlit : TypeError: a bytes-like object is required, not 'Tensor'

我正在进行风格转换任务,我的模型 returns 是一个张量。最近我使用 torchvision.utils

保存该图像
torchvision.utils.save_image(genarated_image, result_path)

现在我已将相同的图像传递给 streamlit。

def image_input():
content_file = st.sidebar.file_uploader("Choose a Content Image", type=["png", "jpg", "jpeg"])
if content_file is not None:
    content = Image.open(content_file)
    content = np.array(content)  # pil to cv
    content = cv2.cvtColor(content, cv2.COLOR_RGB2BGR)
else:
    st.warning("Upload an Image OR Untick the Upload Button)")
    st.stop()

WIDTH = st.sidebar.select_slider('QUALITY (May reduce the speed)', list(range(150, 501, 50)), value=200)
content = imutils.resize(content, width=WIDTH)
generated = genarate_image(content)
st.sidebar.image(content, width=300, channels='BGR')
st.image(generated, channels='BGR', clamp=True)

但现在 streamlit 给我这个错误。

TypeError: a bytes-like object is required, not 'Tensor'

有没有办法将张量转换为“类字节对象”?

将tonsor转为PIL Image即可解决

from torchvision import transforms

def trans_tensor_to_pil(tensor_img):
pil_image = transforms.ToPILImage()(tensor_img.squeeze_(0))
return pil_image