如果您对字符串格式不能正确处理四舍五入的NumpyFloat32?感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于字符串格式不能正确处理四舍五入的NumpyFloat32?的
如果您对字符串格式不能正确处理四舍五入的 Numpy Float32?感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于字符串格式不能正确处理四舍五入的 Numpy Float32?的详细内容,我们还将为您解答字符串的格式不够标准明确的相关问题,并且为您提供关于'AttributeError:'numpy.float32'对象没有属性'ctypes'、/ image / Tensor Tensor(“ activation_5 / Softmax:0”,shape =(?, 4),dtype = float32)处的ValueError不是此图的元素、float32 between (0, 1) 无线传输优化、Julia Flux 错误:MethodError:没有方法匹配 *(::Chain{Tuple{Conv{2,4,typeof(identity),Array{Float32,4},Array{Float32,1}},var"#13#15" }}, ::Int64)的有价值信息。
本文目录一览:- 字符串格式不能正确处理四舍五入的 Numpy Float32?(字符串的格式不够标准明确)
- 'AttributeError:'numpy.float32'对象没有属性'ctypes'
- / image / Tensor Tensor(“ activation_5 / Softmax:0”,shape =(?, 4),dtype = float32)处的ValueError不是此图的元素
- float32 between (0, 1) 无线传输优化
- Julia Flux 错误:MethodError:没有方法匹配 *(::Chain{Tuple{Conv{2,4,typeof(identity),Array{Float32,4},Array{Float32,1}},var"#13#15" }}, ::Int64)
字符串格式不能正确处理四舍五入的 Numpy Float32?(字符串的格式不够标准明确)
如何解决字符串格式不能正确处理四舍五入的 Numpy Float32?
当将格式字符串与 Numpy 32 float 和 Numpy.round() 一起使用时,我不小心遇到了这种奇怪的行为。重现:
score=0.7827188774
npf32_score_rounded=np.round(np.float32(0.7827188774),4)
score_rounded=np.round(score,4)
print(f" {npf32_score_rounded}")
print(f" {score_rounded}")
print(npf32_score_rounded)
print(score_rounded)
#0.7827000021934509 expect to get 0.7827
#0.7827
#0.7827 # without string format,it works as expected
#0.7827
这种行为是正常的还是应该修复?
Numpy 版本 == 1.20.2
Python 版本 == 3.8.0
解决方法
Python 的 f 字符串调用对象 __format__
函数。正如在 numpy''s source code 中所见,对于浮点数,这会在之前转换为 Python float
(64 位)。
由于存储在 32 位浮点数中的值实际上是 0.782700002193450927734375
(参见 IEE 754),Python 的精度更高 float
这些小数位现在很重要。这就是为什么现在打印这些小数位的原因。感谢 @user2357112 supports Monica 指出这一点。
所以这是预期的结果。
import numpy as np
val = 0.7827188774
np_float_32 = np.round(np.float32(val),4)
assert type(np_float_32) == np.float32
np_float_64 = np.round(val,4)
assert type(np_float_64) == np.float64
# just calling print,no issues
print(np_float_32,np_float_64) # 0.7827 0.7827
# both str and repr work as intended
print(str(np_float_32),str(np_float_64)) # 0.7827 0.7827
print(repr(np_float_32),repr(np_float_64)) # 0.7827 0.7827
# behavior described by OP
print(f''{np_float_32} {np_float_64}'') # 0.7827000021934509 0.7827
# f-string without format specification calls obj.__format__('''')
# see https://github.com/numpy/numpy/blob/v1.20.2/numpy/core/src/multiarray/scalartypes.c.src#L273
# explicit conversion to python float
print(float(np_float_32),float(np_float_64)) # 0.7827000021934509 0.7827
'AttributeError:'numpy.float32'对象没有属性'ctypes'
如何解决''AttributeError:''numpy.float32''对象没有属性''ctypes''
我试图根据从左上,右上等顺序排列的点来计算图像的尺寸。因此,我以后可以执行4点透视变换和变形。 (使用opencvSharp和numpy.net)
我想从中获取最大宽度和最大高度作为整数 ndarray,但此时它抛出 “ Python.Runtime.PythonException:''AttributeError:''numpy.float32''对象没有属性''ctypes''
不太确定如何作为一个新手来解决这个问题,这是我第一次使用此OpencvSharp和numpy.NET。
ndarray rect = OrderPoints(pts);
// 4 Points order from top-left top- right etc..
ndarray tL = rect[0],tR = rect[1],bR = rect[2],bL = rect[3];
// width of new image
ndarray w1 = ((bR[0] - bL[0]) * (bR[0] - bL[0])) + ((bR[1] - bL[1]) * (bR[1] - bL[1])); // quietly throws exception here
ndarray w2 = ((tR[0] - tL[0]) * (tR[0] - tL[0])) + ((tR[1] - tL[1]) * (tR[1] - tL[1]));
w1 = np.sqrt(w1);
w2 = np.sqrt(w2);
// get max width as an interger
int[] width1 = w1.GetData<int>(); // same error blows up here
int[] width2 = w2.GetData<int>();
int maxWidth = Math.Max(width1[0],width2[0]);
ndarray h1 = ((tR[0] - bR[0]) * (tR[0] - bR[0])) + ((tR[1] - bR[1]) * (tR[1] - bR[1]));
ndarray h2 = (tL[0] - bL[0]) * (tL[0] - bL[0]) + (tL[1] - bL[1]) * (tL[1] - bL[1]);
h1 = np.sqrt(h1);
h2 = np.sqrt(h2);
int[] height = h1.GetData<int>();
int[] height2 = h2.GetData<int>();
int maxHeight = Math.Max(height[0],height2[0]);
// make a top - down view
var dst = np.array(
new[,] {
{ 0,0},{maxWidth - 1,0 },maxHeight - 1 },{0,maxHeight - 1 }
});
var rectPts = rect.GetData<Point2f>();
var dstarray = dst.GetData<Point2f>();
Mat matr = Cv2.GetPerspectiveTransform(rectPts,dstarray);
Mat output = new Mat();
Cv2.WarpPerspective(image,output,matr,new Size(maxWidth,maxHeight));
return output;
解决方法
我设法提出了一个解决方案,方法是使用repr
属性返回数据字符串,然后将其解析为float并转换为Int。
int width1 = (int)float.Parse(w1.repr);
int width2 = (int)float.Parse(w2.repr);
/ image / Tensor Tensor(“ activation_5 / Softmax:0”,shape =(?, 4),dtype = float32)处的ValueError不是此图的元素
我正在构建图像处理分类器,并且此代码是用于预测整个代码正在运行的图像的图像类的API,但此行除外(pred =
model.predict_classes(test_image)),此API在Django框架中进行并且正在使用python 2.7
如果我像往常一样运行此代码(无需制作API),这一点就可以完美运行
def classify_image(request):if request.method == ''POST'' and request.FILES[''test_image'']: fs = FileSystemStorage() fs.save(request.FILES[''test_image''].name, request.FILES[''test_image'']) test_image = cv2.imread(''media/''+request.FILES[''test_image''].name) if test_image is not None: test_image = cv2.resize(test_image, (128, 128)) test_image = np.array(test_image) test_image = test_image.astype(''float32'') test_image /= 255 print(test_image.shape) else: print(''image didnt load'') test_image = np.expand_dims(test_image, axis=0) print(test_image) print(test_image.shape) pred = model.predict_classes(test_image) print(pred)return JsonResponse(pred, safe=False)
答案1
小编典典您的test_image和tensorflow模型的输入不匹配。
# Your image shape is (, , 3)test_image = cv2.imread(''media/''+request.FILES[''test_image''].name)if test_image is not None: test_image = cv2.resize(test_image, (128, 128)) test_image = np.array(test_image) test_image = test_image.astype(''float32'') test_image /= 255 print(test_image.shape)else: print(''image didnt load'')# Your image shape is (, , 4)test_image = np.expand_dims(test_image, axis=0)print(test_image)print(test_image.shape)pred = model.predict_classes(test_image)
以上仅为假设。如果要调试,我想您应该打印图像大小并与模型定义的第一个布局进行比较。并检查尺寸(宽度,高度,深度)是否匹配
float32 between (0, 1) 无线传输优化
如何解决float32 between (0, 1) 无线传输优化
我正在寻找一种优化 float32
值的方法,该值仅包含来自 (0,1)
的无线传输值。
因为在我的情况下所有值都是正的,我已经修剪了符号位,但现在我需要一些帮助
我认为,我的下一步将是修剪所有 Fraction 尾随零,然后以某种方式优化 Exponent 部分
但我不太确定,也许对于相同的情况已经存在一些位级优化,因为使用 float32
在 (0,1)
之间存储值是非常普遍的做法
数据完整性和纠错与我的情况无关
解决方法
所以,我决定只使用 uint16_t
,其中 0=0f
和 65365=1f
对我来说已经足够了。但在内存级别,我实际上根本没有使用浮动。这种基于整数的逻辑不仅帮助我实现了更短的无线传输字节序列,而且使用更少的内存
其他选项是使用 uint32_t
,这将使用相同数量的位
转换为浮动,如果你需要的话,是这样的:
float to_float = (float) value / (float) UINT16_MAX
Julia Flux 错误:MethodError:没有方法匹配 *(::Chain{Tuple{Conv{2,4,typeof(identity),Array{Float32,4},Array{Float32,1}},var"#13#15" }}, ::Int64)
如何解决Julia Flux 错误:MethodError:没有方法匹配 *(::Chain{Tuple{Conv{2,4,typeof(identity),Array{Float32,4},Array{Float32,1}},var"#13#15" }}, ::Int64)?
using Flux
using Flux:@functor
function ConvBlock(inc,out,k,s,p,use_act)
return Chain(
Conv((k,k),inc=>out,stride = s,pad = p,bias=true),use_act ? x -> leakyrelu.(x,0.2) : x -> x
)
end
mutable struct DenseResidualBlock
residual_beta
blocks
end
@functor DenseResidualBlock
function DenseResidualBlock(inc,c = 32,residual_beta = 0.2)
blocks = []
for i in 0:4
in_channels = inc + c*i
out_channels = i<=3 ? c : inc
use_act = i<=3 ? true : false
push!(blocks,ConvBlock(in_channels,out_channels,3,1,use_act))
end
return DenseResidualBlock(residual_beta,blocks)
end
function (m::DenseResidualBlock)(x)
new_inputs = x
local out,new_inputs
for block in m.blocks
out = block(new_inputs)
new_inputs = cat(new_inputs,dims=3)
end
return m.residual_beta * out + x
end
当我运行这个
drb = DenseResidualBlock(64)
我收到此错误 错误:方法错误:没有方法匹配 *(::Chain{Tuple{Conv{2,4,typeof(identity),Array{Float32,4},1}},var"#13#15"}},::Int64)
解决方法
试试
function DenseResidualBlock(inc;c = 32,residual_beta = 0.2)
代替
function DenseResidualBlock(inc,c = 32,residual_beta = 0.2)
,
当使用两个参数调用 DenseResidualBlock
时,您的代码中存在歧义。它可以直接构造 DenseResidualBlock 结构,也可以使用 DenseResidualBlock(inc,c)
调用 residual_beta = 0.2
。如果您对 DenseResidualBlock(inc; c = 32,residual_beta = 0.2)
使用关键字参数,则会消除这种歧义。
错误消息表明,在 in_channels = inc + c*i
行,参数 c
不是预期的数字,而是无法乘以数字的 Flux.Chain
。
今天关于字符串格式不能正确处理四舍五入的 Numpy Float32?和字符串的格式不够标准明确的讲解已经结束,谢谢您的阅读,如果想了解更多关于'AttributeError:'numpy.float32'对象没有属性'ctypes'、/ image / Tensor Tensor(“ activation_5 / Softmax:0”,shape =(?, 4),dtype = float32)处的ValueError不是此图的元素、float32 between (0, 1) 无线传输优化、Julia Flux 错误:MethodError:没有方法匹配 *(::Chain{Tuple{Conv{2,4,typeof(identity),Array{Float32,4},Array{Float32,1}},var"#13#15" }}, ::Int64)的相关知识,请在本站搜索。
本文标签: