weixin_41727316 2019-06-18 01:35 采纳率: 0%
浏览 2781
已采纳

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条回答 默认 最新

  • weixin_41727316 2019-06-19 01:04
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog