numpy, pytorch, vscode自测题

1.对于指定数组,取特定值,其它设置为0, 比如pytorch segmentation结果包含多个类,只取特定类,如何做?

#classp = [0,1,2,2,3,0,5,2]这样,后面box,score,mask与之对应
ids = torch.where(classp==0)#选择人类别
classp = classp[ids]
box = box[ids]
score = score[ids]
mask = mask[ids[0], :]

  类似的也可以更简单

#classp = [0,1,2,2,3,0,5,2]这样,后面box,score,mask与之对应
ids = (classp==0)#选择人类别
classp = classp[ids]
box = box[ids]
score = score[ids]
mask = mask[ids, :]

  

2. pytorch在inference阶段,如何保证模型没有梯度信息?

  使用with torch.no_grad(), 修饰

with torch.no_grad():
            cudnn.fastest = True
            torch.set_default_tensor_type('torch.cuda.FloatTensor')
            print('yolact loading model...', end='')
            net = Yolact()
            net.load_weights(config.yolact['model_path'])
            net.eval()
            print(' Done.')
            self.net = net.cuda()
            self.net.detect.use_fast_nms = True
            self.net.detect.use_cross_class_nms = False

3.如何把numpy数组,转化为pytorch tensor, 在转换到cuda上

  torch.from_numpy(np_array).cuda().float()

4.vs code里面如何调试一个文件?如何加入调试参数,很多args?如何使用conda环境的python?如何指定调试的工作目录?

  1)通过图形界面操作,生成launch.json

  2)编辑,加入args:["--thresh=0.5", "--cuda"

  3)加入pythonPath:"/home/silva/anaconda3/envs/py372/bin/python"

 4)加入cwd:"/home/silva/work"

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "pythonPath": "/home/silva/anaconda3/envs/py372/bin/python",
            "cwd": "${fileDirname}",
            "args": [
                "--trained_model=weights/yolact_resnet50_54_800000.pth",
                "--score_threshold=0.15",
                "--top_k=15",
                "--video_multiframe=4",
                "--video=0"
            ]
        }
    ]
}

5. numpy array 作为函数参数传递,是作为引用传递,还是拷贝传递?

  以前经常搞错,原来是引用传递。在函数内部,修改了array, 那么调用函数的那个array也会修改。

  def abc(arr): arr = np.zeros((3,3))

  调用a=np.ones((3,3)), 那么abc(a), 执行后,print(a)是多少?不变

  如果def abc(arr): arr+=3, abc(a)执行后?增加了3

如果def abc(arr): arr[1][1]=34, abc(a)执行后?a对应元素变为了34

参考 https://stackoverflow.com/questions/11585793/are-numpy-arrays-passed-by-reference

发布了159 篇原创文章 · 获赞 55 · 访问量 36万+

猜你喜欢

转载自blog.csdn.net/northeastsqure/article/details/103520399
今日推荐