Hawkeye-FineGrained / Hawkeye

Open source deep learning based fine-grained image recognition toolbox built on PyTorch🔥
MIT License
569 stars 65 forks source link

t() expects a tensor with <= 2 dimensions, but self is 4D #3

Open haidongwang3629 opened 1 year ago

haidongwang3629 commented 1 year ago

Thanks for the awosome sharing! Really appearate this work!

While run the testing scripts: python3 test.py --config configs/test.yaml (i have already trained APINet )

experiment: name: test_APINet cuda: [0] dataset: name: cub root_dir: data/bird/CUB_200_2011/images meta_dir: metadata/cub batch_size: 32 num_workers: 4 transformer: resize_size: 448 image_size: 448 model: name: APINet num_classes: 200 load: results/APINet/API_res101_1/best_model.pth

it reports the following message:

Traceback (most recent call last): File "/home/haidongwang/code/Hawkeye/test.py", line 147, in <module> tester.test() File "/home/haidongwang/code/Hawkeye/test.py", line 119, in test self.validate() File "/home/haidongwang/code/Hawkeye/test.py", line 129, in validate self.batch_validate(data) File "/home/haidongwang/code/Hawkeye/test.py", line 135, in batch_validate logits = self.model(images) File "/home/haidongwang/anaconda3/envs/chenli/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl return forward_call(*input, **kwargs) File "/home/haidongwang/code/Hawkeye/model/methods/APINet.py", line 34, in forward intra_pairs, inter_pairs, intra_labels, inter_labels = self.get_pairs(pool_out, targets) File "/home/haidongwang/code/Hawkeye/model/methods/APINet.py", line 77, in get_pairs distance_matrix = pdist(embeddings).detach().cpu().numpy() File "/home/haidongwang/code/Hawkeye/model/methods/APINet.py", line 117, in pdist distance_matrix = -2 * vectors.mm(torch.t(vectors)) + vectors.pow(2).sum(dim=1).view(1, -1) \ **RuntimeError: t() expects a tensor with <= 2 dimensions, but self is 4D**

yulingchen123 commented 5 months ago

我也遇见了这个问题,请问你解决了么

yulingchen123 commented 5 months ago

我解决了,不能使用test.yaml,因为APINet的训练和验证不一样,验证阶段不使用get_pairs, 也就是, elif flag == 'val': return self.fc(pool_out) 但是直接在test.py里面改也不行,打印出来pool_out是这样的 pool out shape: torch.size([16,2048,2,2]),直接在config.py里面放 1 然后 2 然后运行test.py,就有测试结果了

7fa286cb0193401f2e6e33df9345514

至于为什么加载APINet.yaml会这样但是test.yaml就不行,我也不太清楚,我还特分了val.txt和test.txt,输出的结果就是test.txt测试集

yulingchen123 commented 5 months ago

总结就是不要加载test.yaml,加载APINet.yaml试试,在APINet.yaml里面把最佳权重路径写上

yulingchen123 commented 5 months ago

至于t() 期望张量具有 <= 2 维,但自身是 4D,是因为验证阶段不应该运行get_pairs函数,对应文章里面说的,只在训练阶段成对输入图片,验证阶段正常输入,但是test文件里面好像并没有规定val的时候不使用get_pairs(),所以我在test里面加了个val标签flag,但是随后显示pool out 张量不匹配,我始终无法解决,最后选择不加载test.yaml,加载APINet.yaml,最后运行test.py,显示结果成功 image

我解决了,不能使用test.yaml,因为APINet的训练和验证不一样,验证阶段果get_pairs,也就是, elif flag == 'val': return self.fc(pool_out) 但是直接在test.py里面改也不行,打印出来pool_out是这样的 pool out shape: torch.size([16,2048,2,2]),直接在config.py里面放 然后 然后运行test.py,就有测试结果了 至于为什么加载APINet.yaml会这样但是test.yaml就不行,我也不太清楚,我还特分了val.txt和test.txt,输出的结果就是test.txt测试集 1 2 7fa286cb0193401f2e6e33df9345514