Browse Source

[to #42322933]"fix: set the eps and momentum of BN consistent with training"

To keep consistent between training and evaluation, change the eps and momentum of BN.
        Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/10554451
master
xianzhe.xxz yingda.chen 2 years ago
parent
commit
88e8d4291a
3 changed files with 18 additions and 6 deletions
  1. +5
    -2
      modelscope/models/cv/tinynas_detection/backbone/tinynas.py
  2. +8
    -0
      modelscope/models/cv/tinynas_detection/detector.py
  3. +5
    -4
      tests/pipelines/test_tinynas_detection.py

+ 5
- 2
modelscope/models/cv/tinynas_detection/backbone/tinynas.py View File

@@ -269,8 +269,11 @@ class TinyNAS(nn.Module):
the_block_class = block_info['class']
if the_block_class == 'ConvKXBNRELU':
if use_focus:
the_block = Focus(block_info['in'], block_info['out'],
block_info['k'])
the_block = Focus(
block_info['in'],
block_info['out'],
block_info['k'],
act=act)
else:
the_block = ConvKXBNRELU(
block_info['in'],


+ 8
- 0
modelscope/models/cv/tinynas_detection/detector.py View File

@@ -6,6 +6,7 @@ import pickle

import cv2
import torch
import torch.nn as nn
import torchvision

from modelscope.metainfo import Models
@@ -47,6 +48,7 @@ class SingleStageDetector(TorchModel):
self.backbone = build_backbone(self.cfg.model.backbone)
self.neck = build_neck(self.cfg.model.neck)
self.head = build_head(self.cfg.model.head)
self.apply(self.init_bn)

self.load_pretrain_model(model_path)

@@ -59,6 +61,12 @@ class SingleStageDetector(TorchModel):
new_state_dict[k] = v
self.load_state_dict(new_state_dict, strict=True)

def init_bn(self, M):
for m in M.modules():
if isinstance(m, nn.BatchNorm2d):
m.eps = 1e-3
m.momentum = 0.03

def inference(self, x):

if self.training:


+ 5
- 4
tests/pipelines/test_tinynas_detection.py View File

@@ -20,16 +20,16 @@ class TinynasObjectDetectionTest(unittest.TestCase, DemoCompatibilityCheck):
Tasks.image_object_detection, model='damo/cv_tinynas_detection')
result = tinynas_object_detection(
'data/test/images/image_detection.jpg')
print(result)
print('airdet', result)

@unittest.skip('will be enabled after damoyolo officially released')
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_run_damoyolo(self):
tinynas_object_detection = pipeline(
Tasks.image_object_detection,
model='damo/cv_tinynas_object-detection_damoyolo')
result = tinynas_object_detection(
'data/test/images/image_detection.jpg')
print(result)
print('damoyolo', result)

@unittest.skip('demo compatibility test is only enabled on a needed-basis')
def test_demo_compatibility(self):
@@ -39,7 +39,8 @@ class TinynasObjectDetectionTest(unittest.TestCase, DemoCompatibilityCheck):
def test_image_object_detection_auto_pipeline(self):
test_image = 'data/test/images/image_detection.jpg'
tinynas_object_detection = pipeline(
Tasks.image_object_detection, model='damo/cv_tinynas_detection')
Tasks.image_object_detection,
model='damo/cv_tinynas_object-detection_damoyolo')
result = tinynas_object_detection(test_image)
tinynas_object_detection.show_result(test_image, result,
'demo_ret.jpg')


Loading…
Cancel
Save