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

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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料