马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
绘画步调在天生图片的时间报错,天生了黑图,cmd窗口里提示报错信息:RuntimeWarning: invalid value encountered in cast out = np.clip(out * 255, 0, 255).astype(np.uint8) # 0~1 -> 0~255
通常是由于 NumPy 数组中存在无效的或 NaN 值,这些值在转换为 uint8 范例时会出现标题。
我原来代码:
out = np.transpose(out.data.cpu().numpy(), [0, 2, 3, 1]) # 1x3xHxW -> 1xHxWx3
out = np.clip(out, 0, 1) # clip to 0~1
out = np.clip(out * 255, 0, 255).astype(np.uint8) # 0~1 -> 0~255
在转换为 uint8 之前,利用 np.nan_to_num() 处置处罚无效值。如许可以确保数组中不会有 NaN 或 inf 影响范例转换。
修改后的代码:
out = np.transpose(out.data.cpu().numpy(), [0, 2, 3, 1]) # 1x3xHxW -> 1xHxWx3
out = np.nan_to_num(out, nan=0.0, posinf=1.0, neginf=0.0) # 处置处罚无效值
out = np.clip(out, 0, 1) # clip to 0~1
out = np.clip(out * 255, 0, 255).astype(np.uint8) # 0~1 -> 0~255
注:
这里的 np.nan_to_num 函数会将数组中的 NaN 更换为 0,正无穷大更换为 1,负无穷大更换为 0,从而制止在后续的裁剪和转换过程中出现无效值。
|