python 'if __name__ == "__main__":' 错误,直接执行测试(Terminal 运行正常)

main.py中的测试函数

import os.path as osp
import pickle
import model
import weights
import test

def get_tf_implementation(weights_path, all_layer_outs=False):
print("Making Structure")
tf_model = model.make_deng_tf_test(verbose=True, all_layer_outs=all_layer_outs)
print("Loading Weights")
tf_model = weights.load_weights(tf_model, weights_path, verbose=True)
return tf_model

def get_test_data(cache_file):
print("Loading Test Data")
if osp.exists(cache_file):
    with open(cache_file, 'rb') as fid:
        roidb = pickle.load(fid)
    print('data is loaded from {}'.format(cache_file))
print(f"Number of Images to test: {len(roidb)}")
return roidb

def get_weighted_tf_implementation(weights_path, all_layer_outs=False):
tf_model = get_tf_implementation(weights_path, all_layer_outs)
model.show_model_info(tf_model)
return tf_model

def test_tf_implementation(cache_file, weights_path, all_layer_outs=False):
# Get Weighted Model
tf_model = get_weighted_tf_implementation(weights_path, all_layer_outs)

# Load Cached Test Data
roidb = get_test_data(cache_file)

# Test Network
results = test.test_net(tf_model, roidb)
return results

if name == "__main__ 代码段:

if __name__ == "__main__":
results = test_tf_implementation(cache_file="roidb_test_19_smol.pkl", weights_path="rgbd_det_iter_40000.h5")

Terminal运行一切正常,给cache-file和weights-path赋值,然后正常执行test_tf_implementation.

不知道问题出在哪里

错误提示:

Using TensorFlow backend.

EE

ERROR: main.get_test_data

Traceback (most recent call last):
File "/Users/xijiejiao/anaconda2/envs/python3/lib/python3.6/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
TypeError: get_test_data() missing 1 required positional argument: 'cache_file'
-------------------- >> begin captured logging << --------------------
tensorflow: Level 1: Registering FakeQuantWithMinMaxArgs () in gradient.
tensorflow: Level 1: Registering FakeQuantWithMinMaxVars () in gradient.


Error
Traceback (most recent call last):
File "/Users/xijiejiao/anaconda2/envs/python3/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
yield
File "/Users/xijiejiao/anaconda2/envs/python3/lib/python3.6/unittest/case.py", line 605, in run
testMethod()
File "/Users/xijiejiao/anaconda2/envs/python3/lib/python3.6/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
Exception: get_test_data() missing 1 required positional argument: 'cache_file'
-------------------- >> begin captured logging << --------------------
tensorflow: Level 1: Registering FakeQuantWithMinMaxArgs () in gradient.
tensorflow: Level 1: Registering FakeQuantWithMinMaxVars () in gradient.


Error
Traceback (most recent call last):
File "/Users/xijiejiao/anaconda2/envs/python3/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
yield
File "/Users/xijiejiao/anaconda2/envs/python3/lib/python3.6/unittest/case.py", line 605, in run
testMethod()
File "/Users/xijiejiao/anaconda2/envs/python3/lib/python3.6/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
TypeError: test_tf_implementation() missing 2 required positional arguments: 'cache_file' and 'weights_path'

tensorflow: Level 1: Registering NcclAllReduce () in gradient.
tensorflow: Level 1: Registering NcclReduce () in gradient.
tensorflow: Level 1: Registering NcclBroadcast () in gradient.


ERROR: main.test_tf_implementation

Traceback (most recent call last):
File "/Users/xijiejiao/anaconda2/envs/python3/lib/python3.6/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
TypeError: test_tf_implementation() missing 2 required positional arguments: 'cache_file' and 'weights_path'


Ran 2 tests in 0.020s

FAILED (errors=2)

Process finished with exit code 1

Assertion failed

Assertion failed

Assertion failed

Assertion failed

4个回答

已解决。不应该在main函数中加入test内容。pycharm会直接进行测试内容,跳过赋值部分。termianly运行不会出现类似问题。
解决方法就是将测试函数重新放在一个新的文件里。在main函数中import,并进行调用。

if __name__ == "__main__":
    results = test_tf_implementation(cache_file="roidb_test_19_smol.pkl", weights_path="rgbd_det_iter_40000.h5")

你把报错贴出来呀,报错不贴出来,怎么分析。

qq_37347705
小熊之怒 回复weixin_41727316: get_test_data() missing 1 required positional argument: 'cache_file' 从这个提示可以看到,你的参数,cache_file=".pkl"没有正确加载进去,包括后面的weights_path, 没有正确加载(可能是路径问题),如果真不知道怎么分析,可以在执行函数前,打印下这个cahe_file。从错误提示的点 一点点进入逻辑分析。
一年多之前 回复
weixin_41727316
weixin_41727316 已更新
一年多之前 回复
weixin_41727316
weixin_41727316 已更新
一年多之前 回复

这个报错感觉不是很直接啊,试试绝对路径试试

立即提问