Skip to content

Instantly share code, notes, and snippets.

@YimianDai
Last active October 8, 2019 17:18
Show Gist options
  • Save YimianDai/e98c82617afe05e6d1f3c7b3e014de1d to your computer and use it in GitHub Desktop.
Save YimianDai/e98c82617afe05e6d1f3c7b3e014de1d to your computer and use it in GitHub Desktop.
Gluon-CV Segmentation Test Script
  • parse_args

    • model and dataset
    • synchronized Batch Normalization
    • evaluation only
      • '--eval'
        1. whether evaluation only, evaluation 就是指是计算 pixAccmIoU 还是保存所有测试结果
        2. True 的话,就是按照 Batch 一个一个扫遍 Dataset,最后输出 pixAccmIoU
        3. False 的话,还是按照 Batch 一个一个扫遍 Dataset,但是不计算 pixAccmIoU,而是把 Batch 里面的每一个样本的预测结果的图保存下来
    • dummy benchmark
  • test

    • output folder
    • get dataset
      • ms_batchify_fn
        1. Multi-size batchify function
    • create evaluator from model
    • create metric
    • For Every Batch
      • 如果只是为了求得 evaluation metric
        1. evaluator forward
        2. update metric
        3. get metric
      • 如果是为了获取测试结果的分割图
        1. evaluator forward
        2. 对 Batch 内的每一个样本
          1. 对 Channel (class) 这一维度取最大的 score 的 index,得到分类结果的 class map
          2. 将 class map 转换成 color mask
          3. 保存 color mask
  • test_quantization

  • benchmarking

  • __name__

    • create network
      • load local pretrained weight
    • image transform
    • test or benchmarking
      • 这两个有什么区别?
        1. test 是要么得到pixAccmIoU,要么是保存所有测试结果
        2. benchmarking 是测试一秒能够处理多少图片的

如果 args.eval 是 False,那就是保存测试结果的 Mode,如果 batchify_fn 是 ms_batchify_fn

  1. data 是 一个 List,里面每个元素都是 MXNet NDArray 的 图像,3 x 512 x 512 的图像
  2. dsts 是 一个 List,里面每个元素都是图像名,因为我们的 batch size 是 16,所以里面有 16 个图像名
MultiEvalModel

我觉得 Gluon-CV 的 Segmentation 的 test.py 里面的将 batchify_fn 设置为 ms_batchify_fn 跟 MultiEvalModel 的 parallel_forward 函数是矛盾的

for (x, ctx) in zip(inputs, self.ctx_list) 因为 inputs 是一个 16 个元素的 list,而 ctx_list 是两个元素的 list,所以 其实只会每个 ctx 只会有一个样本,给个简单的小例子

>>> inputs = [1,2,3,4]   
>>> ctx_list = ['a', 'b']
>>> [[x, ctx] for (x, ctx) in zip(inputs, ctx_list)]  
# [[1, 'a'], [2, 'b']]  

print("len(inputs): ", len(inputs)) 的结果是 len(inputs): 2,但 print("inputs[0].shape: ", inputs[0].shape) 会报错,因为 inputs[0] 还是一个 Tuple,从 print("len(inputs[0]): ", len(inputs[0])) 的结果是 1,print("inputs[0][0].shape: ", inputs[0][0].shape) 的结果是 (3, 512, 512),因此真的是我上面说的问题

而且 __call__ 函数里面要求了 assert(batch == 1),因此只能处理一个样本(前面的 for (x, ctx) in zip(inputs, self.ctx_list) 其实保证了每个 GPU 上只有一个样本),因此如果要产生所有样本,需要将 batch-size 调成 GPU 数

当 eval 是 False 时

data 应该是一个 List of MXNet.NDArray,里面每个元素都是 3 x 512 x 512 的 MXNet.NDArray

$ python demo_test_tiny_fcn.py --model unet --resume './runs/iceberg/unet/default/model_best.params'

$ python test_tiny_fcn.py --batch-size 10 --split test --dataset DENTIST --model FCN --gpus 1 --eval

$ python test_tiny_fcn.py --batch-size 40 --split test --dataset Iceberg --model FCN --gpus 0,1,2,3 --eval --resume FCN_Iceberg_best_mAP.params

debug

python test_tiny_fcn.py --batch-size 10 --split test --dataset Iceberg --model FCN --gpus 0 --eval --resume FCN_DENTIST_best_mAP.params

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment