小编Kas*_*kar的帖子

C++中三元运算符的意外行为

以下是我编写的代码片段:

int n,i,j;
map<int,int>mp;
vector<int>vec;
cin>>n;
for(i=0; i<n; i++)
{
    cin>>j;
    mp[j]==0? mp[j]=1,vec.push_back(j): mp[j]=1;
}
Run Code Online (Sandbox Code Playgroud)

对于for循环内的第二行,CodeBlocks-16.01 版本显示以下错误:

second operand to the conditional operator is of type 'void', but the third operand is neither a throw-expression nor of type 'void'
Run Code Online (Sandbox Code Playgroud)

但是当我将行更改为:

second operand to the conditional operator is of type 'void', but the third operand is neither a throw-expression nor of type 'void'
Run Code Online (Sandbox Code Playgroud)

没有错误。以下行有什么问题?

mp[j]==0? vec.push_back(j), mp[j]=1: mp[j]=1;
Run Code Online (Sandbox Code Playgroud)

c++ conditional-operator

4
推荐指数
1
解决办法
75
查看次数

Tensorflow - 对多个图像进行批量预测

我有一个faces列表,其中列表的每个元素都是一个形状为 ( 1, 224, 224, 3) 的 numpy 数组,即人脸图像。我有一个模型,其输入形状为(None, 224, 224, 3),输出形状为(None, 2)

现在我想对faces列表中的所有图像进行预测。当然,我可以遍历列表并一个一个地得到预测,但我想将所有图像作为一个批次处理,只使用一次调用来model.predict()更快地获得结果。

如果我像现在这样直接传递人脸列表(最后是完整的代码),我只会得到第一张图像的预测。

print(f"{len(faces)} faces found")
print(faces[0].shape)
maskPreds = model.predict(faces)
print(maskPreds)
Run Code Online (Sandbox Code Playgroud)

输出:

3 faces found
(1, 224, 224, 3)
[[0.9421933  0.05780665]]
Run Code Online (Sandbox Code Playgroud)

但是maskPreds对于 3 个图像应该是这样的:

[[0.9421933  0.05780665], 
 [0.01584494 0.98415506], 
 [0.09914105 0.9008589 ]] 
Run Code Online (Sandbox Code Playgroud)

完整代码:

print(f"{len(faces)} faces found")
print(faces[0].shape)
maskPreds = model.predict(faces)
print(maskPreds)
Run Code Online (Sandbox Code Playgroud)

注意:如果我不将每个图像从 (224, 224, 3) 转换为 ( 1, 224, 224, 3),tensorflow 会抛出错误,指出输入维度不匹配。

ValueError: Error when checking …
Run Code Online (Sandbox Code Playgroud)

python keras tensorflow

-1
推荐指数
1
解决办法
2442
查看次数

标签 统计

c++ ×1

conditional-operator ×1

keras ×1

python ×1

tensorflow ×1