test only test

本文介绍了一个测试用例,用于验证配置项的正确注册与存在性检查。通过设置单个及多个配置项,并确保它们被成功注册到指定组中。
only test
import time
import logging
log = logging.getLogger('test');

class ConfigurationFunctionTest(TestCase):
    
    def testSetSingleConfigItem(self):
        value = IntegerValue(SHOP_GROUP, 'SingleItem')
        config_register(value)
        self.assert_(config_exists(SHOP_GROUP, 'SingleItem'))
        
    def testSetTwoConfigItems(self):
        s = [IntegerValue(SHOP_GROUP, 'testTwoA'), StringValue(SHOP_GROUP, 'testTwoB')]
        config_register_list(*s)
        
        self.assert_(config_exists(SHOP_GROUP, 'testTwoA'))
        self.assert_(config_exists(SHOP_GROUP, 'testTwoB'))
 
only test
 
   1: import time
   2: import logging
   3: log = logging.getLogger('test');
   4:  
   5: class ConfigurationFunctionTest(TestCase):
   6:     
   7:     def testSetSingleConfigItem(self):
   8:         value = IntegerValue(SHOP_GROUP, 'SingleItem')
   9:         config_register(value)
  10:         self.assert_(config_exists(SHOP_GROUP, 'SingleItem'))
  11:         
  12:     def testSetTwoConfigItems(self):
  13:         s = [IntegerValue(SHOP_GROUP, 'testTwoA'), StringValue(SHOP_GROUP, 'testTwoB')]
  14:         config_register_list(*s)
  15:         
  16:         self.assert_(config_exists(SHOP_GROUP, 'testTwoA'))
  17:         self.assert_(config_exists(SHOP_GROUP, 'testTwoB'))

 

test 3:

   1: import time
   2: import logging
   3: log = logging.getLogger('test');
   4:  
   5: class ConfigurationFunctionTest(TestCase):
   6:     
   7:     def testSetSingleConfigItem(self):
   8:         value = IntegerValue(SHOP_GROUP, 'SingleItem')
   9:         config_register(value)
  10:         self.assert_(config_exists(SHOP_GROUP, 'SingleItem'))
  11:         
  12:     def testSetTwoConfigItems(self):
  13:         s = [IntegerValue(SHOP_GROUP, 'testTwoA'), StringValue(SHOP_GROUP, 'testTwoB')]
  14:         config_register_list(*s)
  15:         
  16:         self.assert_(config_exists(SHOP_GROUP, 'testTwoA'))
  17:         self.assert_(config_exists(SHOP_GROUP, 'testTwoB'))

 

test 4:

   1: import time
   2: import logging
   3: log = logging.getLogger('test');
   4:  
   5: class ConfigurationFunctionTest(TestCase):
   6:     
   7:     def testSetSingleConfigItem(self):
   8:         value = IntegerValue(SHOP_GROUP, 'SingleItem')
   9:         config_register(value)
  10:         self.assert_(config_exists(SHOP_GROUP, 'SingleItem'))
  11:         
  12:     def testSetTwoConfigItems(self):
  13:         s = [IntegerValue(SHOP_GROUP, 'testTwoA'), StringValue(SHOP_GROUP, 'testTwoB')]
  14:         config_register_list(*s)
  15:         
  16:         self.assert_(config_exists(SHOP_GROUP, 'testTwoA'))
  17:         self.assert_(config_exists(SHOP_GROUP, 'testTwoB'))
### 如何使用 `--test_only` 参数进行 PyTorch 测试 在 PyTorch 中,许多训练脚本支持通过命令行参数来控制程序的行为。对于测试模式而言,通常会有一个名为 `--test_only` 的选项用于仅执行测试而不启动训练过程。 当设置此标志时,代码逻辑应跳过训练阶段并直接加载预训练权重文件来进行评估操作[^1]。具体实现方式取决于各个项目的结构设计,在一些开源项目中可以找到类似的配置方法作为参考[^4]。 下面是一个简单的例子展示如何构建这样的功能: ```python import argparse from pathlib import Path import torch from torchvision import models, transforms from PIL import Image def main(args): # 初始化设备 device = 'cuda' if torch.cuda.is_available() and not args.cpu else 'cpu' # 加载模型 model = models.resnet50(pretrained=False) checkpoint = torch.load(args.weights_path, map_location=device) model.load_state_dict(checkpoint['model']) model.to(device).eval() if args.test_only: transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) img = Image.open(Path('path_to_test_image')) input_tensor = transform(img).unsqueeze(0).to(device) with torch.no_grad(): output = model(input_tensor) print(f'Test prediction: {output.argmax().item()}') if __name__ == '__main__': parser = argparse.ArgumentParser(description='Test-only mode.') parser.add_argument('--weights-path', type=str, required=True, help='Path to the pretrained weights file.') parser.add_argument('--test-only', action='store_true', help='Run test only without training.') parser.add_argument('--cpu', action='store_true', help='Force using CPU even when GPU is available.') arguments = parser.parse_args() main(arguments) ``` 上述代码展示了如何定义一个接受 `--test_only` 命令行参数的小型应用程序框架,并根据该参数的存在与否决定是否进入测试流程。注意这里还包含了其他辅助性的参数如指定权重量路径以及强制CPU运算等选项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值