Skip to content

Instantly share code, notes, and snippets.

@vfdev-5
Last active November 8, 2021 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfdev-5/a5bd5b1477b1c82a87a0f9e25c727664 to your computer and use it in GitHub Desktop.
Save vfdev-5/a5bd5b1477b1c82a87a0f9e25c727664 to your computer and use it in GitHub Desktop.
PyTorch vs OpenCV vs Scikit-Image vs Scipy vs Pillow vs TF nearest interpolation comparision with resize and rescale ops

Results

Output for 1.9.0

pytorch: 1.9.0
skimage: 0.18.3
opencv: 4.5.3
scipy: 1.7.1
Pillow: 8.3.2
TensorFlow: 2.6.0


------ Check resize op ------

- check output size: 2 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 86.83333587646484
PTH: [153. 241.]
SkI: [193.  77.]
Error: PyTorch result is different from Pillow result. MAE: 86.83333333333333
PTH: [153. 241.]
PIL: [193.  77.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 88.41666412353516
Error: PyTorch result is different from TF result. MAE: 86.83333587646484

- check output size: 10 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 88.75
PTH: [153.  40. 254. 246. 159. 241.  10.  47. 131. 234.]
SkI: [145. 252. 227.   5. 228. 106.  64.  10.   1.  52.]
Error: PyTorch result is different from Pillow result. MAE: 89.29666666666667
PTH: [153.  40. 254. 246. 159. 241.  10.  47. 131. 234.]
PIL: [145. 252. 227.   5. 228. 106.  64. 244.   1.  52.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 87.66999816894531
Error: PyTorch result is different from TF result. MAE: 88.75

- check output size: 16 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 88.90885162353516
PTH: [153. 181.  65. 254.  59.  63. 159.  52. 241.  86.]
SkI: [145.   8. 252.  17.  35.   5.  59. 111. 106. 206.]
Error: PyTorch result is different from Pillow result. MAE: 88.90885416666667
PTH: [153. 181.  65. 254.  59.  63. 159.  52. 241.  86.]
PIL: [145.   8. 252.  17.  35.   5.  59. 111. 106. 206.]
PyTorch result matches OpenCV INTER_NEAREST result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
Error: PyTorch result is different from TF result. MAE: 88.90885162353516

- check output size: 20 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 82.72333526611328
PTH: [153. 248.  40.  65. 254.  59. 246. 154. 159.  52.]
SkI: [153. 181.  65.  93. 187.  59.  63. 159.  36.  32.]
Error: PyTorch result is different from Pillow result. MAE: 81.44833333333334
PTH: [153. 248.  40.  65. 254.  59. 246. 154. 159.  52.]
PIL: [153. 181.  65.  93. 187.  59.  63. 154.  36.  32.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 82.72333526611328
Error: PyTorch result is different from TF result. MAE: 82.72333526611328

- check output size: 24 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 74.85821533203125
PTH: [153. 248. 181.  65.  93. 254.  59. 246.  63. 159.]
SkI: [153. 181.  40.  65. 254. 187.  59.  63. 154. 159.]
Error: PyTorch result is different from Pillow result. MAE: 62.88483796296296
PTH: [153. 248. 181.  65.  93. 254.  59. 246.  63. 159.]
PIL: [153. 181.  40.  65.  93. 187.  59.  63. 154. 159.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 45.02430725097656
Error: PyTorch result is different from TF result. MAE: 74.85821533203125

- check output size: 30 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 66.76629638671875
PTH: [153. 248. 181.  40.  65.  93. 254. 187.  59. 246.]
SkI: [153. 248. 181.  40.  65.  93. 254.  59. 246.  63.]
Error: PyTorch result is different from Pillow result. MAE: 60.63851851851852
PTH: [153. 248. 181.  40.  65.  93. 254. 187.  59. 246.]
PIL: [153. 248. 181.  40.  65.  93. 254. 187. 246.  63.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 60.638519287109375
Error: PyTorch result is different from TF result. MAE: 66.76629638671875

- check output size: 32 <- 32
PyTorch result matches Scikit-image result.
PyTorch result matches Pillow result.
PyTorch result matches OpenCV INTER_NEAREST result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
PyTorch result matches TF result.

- check output size: 36 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 59.06301498413086
PTH: [153. 153. 248. 181.  40.  65.  93. 254. 187.  59.]
SkI: [153. 248. 181.  40.  65.  65.  93. 254. 187.  59.]
Error: PyTorch result is different from Pillow result. MAE: 59.06301440329218
PTH: [153. 153. 248. 181.  40.  65.  93. 254. 187.  59.]
PIL: [153. 248. 181.  40.  65.  65.  93. 254. 187.  59.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 47.17155456542969
Error: PyTorch result is different from TF result. MAE: 59.06301498413086

- check output size: 40 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 55.266666412353516
PTH: [153. 153. 248. 181.  40.  65.  65.  93. 254. 187.]
SkI: [153. 248. 181. 181.  40.  65.  93. 254. 254. 187.]
Error: PyTorch result is different from Pillow result. MAE: 52.56166666666667
PTH: [153. 153. 248. 181.  40.  65.  65.  93. 254. 187.]
PIL: [153. 248. 181. 181.  40.  65.  93.  93. 254. 187.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 52.80479049682617
Error: PyTorch result is different from TF result. MAE: 55.266666412353516

- check output size: 42 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 52.50944900512695
PTH: [153. 153. 248. 181.  40.  40.  65.  93. 254. 254.]
SkI: [153. 248. 248. 181.  40.  65.  65.  93. 254. 187.]
Error: PyTorch result is different from Pillow result. MAE: 49.97392290249433
PTH: [153. 153. 248. 181.  40.  40.  65.  93. 254. 254.]
PIL: [153. 248. 248. 181.  40.  65.  65.  93. 254. 187.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 47.168556213378906
Error: PyTorch result is different from TF result. MAE: 52.50944900512695

- check output size: 64 <- 32
PyTorch result matches Scikit-image result.
PyTorch result matches Pillow result.
PyTorch result matches OpenCV INTER_NEAREST result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
PyTorch result matches TF result.

- check output size: 72 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 34.035430908203125
PTH: [153. 153. 153. 248. 248. 181. 181.  40.  40.  65.]
SkI: [153. 153. 248. 248. 181. 181. 181.  40.  40.  65.]
Error: PyTorch result is different from Pillow result. MAE: 28.26999742798354
PTH: [153. 153. 153. 248. 248. 181. 181.  40.  40.  65.]
PIL: [153. 153. 248. 248. 181. 181. 181.  40.  40.  65.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 18.03382110595703
Error: PyTorch result is different from TF result. MAE: 34.035430908203125

--> 0 of OpenCV INTER_NEAREST result mismatches
--> 9 of OpenCV INTER_NEAREST_EXACT result mismatches
--> 10 of Scikit-Image result mismatches
--> 10 of Pillow result mismatches
--> 10 of TensorFlow result mismatches


------ Check rescale op ------

recompute_scale_factor: True

- check scale_factor=0.1234
Error: different shapes: opencv (4, 4), (4, 4) vs sk-img (4, 4) vs scipy (4, 4) vs pth (3, 3)

- check scale_factor=0.5
Error: PyTorch result is different from Scikit-image result. MAE: 88.90885162353516
Error: PyTorch result is different from Scipy result. MAE: 88.90885162353516
PyTorch result matches OpenCV result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.

- check scale_factor=0.6
Error: PyTorch result is different from Scikit-image result. MAE: 83.61126708984375
Error: PyTorch result is different from Scipy result. MAE: 83.61126708984375
PyTorch result matches OpenCV result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 81.30101776123047

- check scale_factor=0.7
Error: PyTorch result is different from Scikit-image result. MAE: 78.77823638916016
Error: PyTorch result is different from Scipy result. MAE: 78.77823638916016
Error: PyTorch result is different from OpenCV result. MAE: 34.127410888671875
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 73.97933959960938

- check scale_factor=0.9999
Error: different shapes: opencv (32, 32), (32, 32) vs sk-img (32, 32) vs scipy (32, 32) vs pth (31, 31)

- check scale_factor=1.0
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
PyTorch result matches OpenCV result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.

- check scale_factor=1.00001
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
PyTorch result matches OpenCV result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.

- check scale_factor=1.3
Error: different shapes: opencv (42, 42), (42, 42) vs sk-img (42, 42) vs scipy (42, 42) vs pth (41, 41)

- check scale_factor=1.71
Error: different shapes: opencv (55, 55), (55, 55) vs sk-img (55, 55) vs scipy (55, 55) vs pth (54, 54)

- check scale_factor=1.8
Error: different shapes: opencv (58, 58), (58, 58) vs sk-img (58, 58) vs scipy (58, 58) vs pth (57, 57)

- check scale_factor=1.9999
Error: different shapes: opencv (64, 64), (64, 64) vs sk-img (64, 64) vs scipy (64, 64) vs pth (63, 63)

- check scale_factor=2.0
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
PyTorch result matches OpenCV result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.

- check scale_factor=2.3
Error: different shapes: opencv (74, 74), (74, 74) vs sk-img (74, 74) vs scipy (74, 74) vs pth (73, 73)

- check scale_factor=2.72
Error: PyTorch result is different from Scikit-image result. MAE: 28.029813766479492
Error: PyTorch result is different from Scipy result. MAE: 28.029813766479492
Error: PyTorch result is different from OpenCV result. MAE: 1.807196021080017
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 26.52005958557129

- check scale_factor=2.9999
Error: different shapes: opencv (96, 96), (96, 96) vs sk-img (96, 96) vs scipy (96, 96) vs pth (95, 95)

- check scale_factor=3.0
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
PyTorch result matches OpenCV result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.

- check scale_factor=3.000001
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
Error: PyTorch result is different from OpenCV result. MAE: 45.80577087402344
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.

- check scale_factor=3.34
Error: different shapes: opencv (107, 107), (107, 107) vs sk-img (107, 107) vs scipy (107, 107) vs pth (106, 106)

- check scale_factor=3.6789
Error: different shapes: opencv (118, 118), (118, 118) vs sk-img (118, 118) vs scipy (118, 118) vs pth (117, 117)

--> 10 of size mismatches
--> 3 of OpenCV INTER_NEAREST result mismatches
--> 3 of OpenCV INTER_NEAREST_EXACT result mismatches
--> 4 of Scikit-Image result mismatches
--> 4 of Scipy result mismatches

Output for PTH PR

PyTorch code: https://github.com/Quansight/pytorch/pull/new/vfdev-5/fix-nearest-upsampling PR: pytorch/pytorch#64501

pytorch: 1.11.0a0+git12d4b58
skimage: 0.19.0.dev0
opencv: 4.5.4-dev
scipy: 1.7.2
Pillow: 8.4.0
TensorFlow: 2.7.0
------ Check resize op ------
- check output size: 2 <- 32
PyTorch result matches Scikit-image result.
PyTorch result matches Pillow result.
Error: PyTorch result is different from OpenCV INTER_NEAREST result. MAE: 86.83333587646484
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 71.58333587646484
PyTorch result matches TF result.
- check output size: 10 <- 32
PyTorch result matches Scikit-image result.
Error: PyTorch result is different from Pillow result. MAE: 18.32
PTH: [145. 252. 227. 5. 228. 106. 64. 10. 1. 52.]
PIL: [145. 252. 227. 5. 228. 106. 64. 244. 1. 52.]
Error: PyTorch result is different from OpenCV INTER_NEAREST result. MAE: 88.75
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 31.486665725708008
PyTorch result matches TF result.
- check output size: 16 <- 32
PyTorch result matches Scikit-image result.
PyTorch result matches Pillow result.
Error: PyTorch result is different from OpenCV INTER_NEAREST result. MAE: 88.90885162353516
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 88.90885162353516
PyTorch result matches TF result.
- check output size: 20 <- 32
PyTorch result matches Scikit-image result.
Error: PyTorch result is different from Pillow result. MAE: 7.401666666666666
PTH: [153. 181. 65. 93. 187. 59. 63. 159. 36. 32.]
PIL: [153. 181. 65. 93. 187. 59. 63. 154. 36. 32.]
Error: PyTorch result is different from OpenCV INTER_NEAREST result. MAE: 82.72333526611328
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
PyTorch result matches TF result.
- check output size: 24 <- 32
PyTorch result matches Scikit-image result.
Error: PyTorch result is different from Pillow result. MAE: 25.729166666666668
PTH: [153. 181. 40. 65. 254. 187. 59. 63. 154. 159.]
PIL: [153. 181. 40. 65. 93. 187. 59. 63. 154. 159.]
Error: PyTorch result is different from OpenCV INTER_NEAREST result. MAE: 74.85821533203125
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 47.66840362548828
PyTorch result matches TF result.
- check output size: 30 <- 32
PyTorch result matches Scikit-image result.
Error: PyTorch result is different from Pillow result. MAE: 10.907777777777778
PTH: [153. 248. 181. 40. 65. 93. 254. 59. 246. 63.]
PIL: [153. 248. 181. 40. 65. 93. 254. 187. 246. 63.]
Error: PyTorch result is different from OpenCV INTER_NEAREST result. MAE: 66.76629638671875
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 10.907777786254883
PyTorch result matches TF result.
- check output size: 32 <- 32
PyTorch result matches Scikit-image result.
PyTorch result matches Pillow result.
PyTorch result matches OpenCV INTER_NEAREST result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
PyTorch result matches TF result.
- check output size: 36 <- 32
PyTorch result matches Scikit-image result.
PyTorch result matches Pillow result.
Error: PyTorch result is different from OpenCV INTER_NEAREST result. MAE: 59.06301498413086
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 17.54218101501465
PyTorch result matches TF result.
- check output size: 40 <- 32
PyTorch result matches Scikit-image result.
Error: PyTorch result is different from Pillow result. MAE: 4.247083333333333
PTH: [153. 248. 181. 181. 40. 65. 93. 254. 254. 187.]
PIL: [153. 248. 181. 181. 40. 65. 93. 93. 254. 187.]
Error: PyTorch result is different from OpenCV INTER_NEAREST result. MAE: 55.266666412353516
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 4.139791488647461
PyTorch result matches TF result.
- check output size: 42 <- 32
PyTorch result matches Scikit-image result.
Error: PyTorch result is different from Pillow result. MAE: 3.7811791383219955
PTH: [153. 248. 248. 181. 40. 65. 65. 93. 254. 187.]
PIL: [153. 248. 248. 181. 40. 65. 65. 93. 254. 187.]
Error: PyTorch result is different from OpenCV INTER_NEAREST result. MAE: 52.50944900512695
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 7.8412699699401855
PyTorch result matches TF result.
- check output size: 64 <- 32
PyTorch result matches Scikit-image result.
PyTorch result matches Pillow result.
PyTorch result matches OpenCV INTER_NEAREST result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
PyTorch result matches TF result.
- check output size: 72 <- 32
PyTorch result matches Scikit-image result.
Error: PyTorch result is different from Pillow result. MAE: 7.198302469135802
PTH: [153. 153. 248. 248. 181. 181. 181. 40. 40. 65.]
PIL: [153. 153. 248. 248. 181. 181. 181. 40. 40. 65.]
Error: PyTorch result is different from OpenCV INTER_NEAREST result. MAE: 34.035430908203125
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 18.13715362548828
PyTorch result matches TF result.
--> 10 of OpenCV INTER_NEAREST result mismatches
--> 9 of OpenCV INTER_NEAREST_EXACT result mismatches
--> 0 of Scikit-Image result mismatches
--> 7 of Pillow result mismatches
--> 0 of TensorFlow result mismatches
------ Check rescale op ------
recompute_scale_factor: True
- check scale_factor=0.1234
Error: different shapes: opencv (4, 4), (4, 4) vs sk-img (4, 4) vs scipy (4, 4) vs pth (3, 3)
- check scale_factor=0.5
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
Error: PyTorch result is different from OpenCV result. MAE: 88.90885162353516
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 88.90885162353516
- check scale_factor=0.6
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
Error: PyTorch result is different from OpenCV result. MAE: 83.61126708984375
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 8.895660400390625
- check scale_factor=0.7
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
Error: PyTorch result is different from OpenCV result. MAE: 85.09297180175781
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 14.369146347045898
- check scale_factor=0.9999
Error: different shapes: opencv (32, 32), (32, 32) vs sk-img (32, 32) vs scipy (32, 32) vs pth (31, 31)
- check scale_factor=1.0
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
PyTorch result matches OpenCV result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
- check scale_factor=1.00001
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
PyTorch result matches OpenCV result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
- check scale_factor=1.3
Error: different shapes: opencv (42, 42), (42, 42) vs sk-img (42, 42) vs scipy (42, 42) vs pth (41, 41)
- check scale_factor=1.71
Error: different shapes: opencv (55, 55), (55, 55) vs sk-img (55, 55) vs scipy (55, 55) vs pth (54, 54)
- check scale_factor=1.8
Error: different shapes: opencv (58, 58), (58, 58) vs sk-img (58, 58) vs scipy (58, 58) vs pth (57, 57)
- check scale_factor=1.9999
Error: different shapes: opencv (64, 64), (64, 64) vs sk-img (64, 64) vs scipy (64, 64) vs pth (63, 63)
- check scale_factor=2.0
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
PyTorch result matches OpenCV result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
- check scale_factor=2.3
Error: different shapes: opencv (74, 74), (74, 74) vs sk-img (74, 74) vs scipy (74, 74) vs pth (73, 73)
- check scale_factor=2.72
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
Error: PyTorch result is different from OpenCV result. MAE: 29.576122283935547
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 1.8809177875518799
- check scale_factor=2.9999
Error: different shapes: opencv (96, 96), (96, 96) vs sk-img (96, 96) vs scipy (96, 96) vs pth (95, 95)
- check scale_factor=3.0
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
PyTorch result matches OpenCV result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
- check scale_factor=3.000001
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
Error: PyTorch result is different from OpenCV result. MAE: 45.80577087402344
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
- check scale_factor=3.34
Error: different shapes: opencv (107, 107), (107, 107) vs sk-img (107, 107) vs scipy (107, 107) vs pth (106, 106)
- check scale_factor=3.6789
Error: different shapes: opencv (118, 118), (118, 118) vs sk-img (118, 118) vs scipy (118, 118) vs pth (117, 117)
--> 10 of size mismatches
--> 5 of OpenCV INTER_NEAREST result mismatches
--> 4 of OpenCV INTER_NEAREST_EXACT result mismatches
--> 0 of Scikit-Image result mismatches
--> 0 of Scipy result mismatches
pytorch: 1.11.0a0+git12d4b58
skimage: 0.19.0.dev0
opencv: 4.5.4-dev
scipy: 1.7.2
Pillow: 8.4.0
TensorFlow: 2.7.0
------ Check resize op ------
- check output size: 2 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 86.83333587646484
PTH: [153. 241.]
SkI: [193. 77.]
Error: PyTorch result is different from Pillow result. MAE: 86.83333333333333
PTH: [153. 241.]
PIL: [193. 77.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 88.41666412353516
Error: PyTorch result is different from TF result. MAE: 86.83333587646484
- check output size: 10 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 88.75
PTH: [153. 40. 254. 246. 159. 241. 10. 47. 131. 234.]
SkI: [145. 252. 227. 5. 228. 106. 64. 10. 1. 52.]
Error: PyTorch result is different from Pillow result. MAE: 89.29666666666667
PTH: [153. 40. 254. 246. 159. 241. 10. 47. 131. 234.]
PIL: [145. 252. 227. 5. 228. 106. 64. 244. 1. 52.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 87.66999816894531
Error: PyTorch result is different from TF result. MAE: 88.75
- check output size: 16 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 88.90885162353516
PTH: [153. 181. 65. 254. 59. 63. 159. 52. 241. 86.]
SkI: [145. 8. 252. 17. 35. 5. 59. 111. 106. 206.]
Error: PyTorch result is different from Pillow result. MAE: 88.90885416666667
PTH: [153. 181. 65. 254. 59. 63. 159. 52. 241. 86.]
PIL: [145. 8. 252. 17. 35. 5. 59. 111. 106. 206.]
PyTorch result matches OpenCV INTER_NEAREST result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
Error: PyTorch result is different from TF result. MAE: 88.90885162353516
- check output size: 20 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 82.72333526611328
PTH: [153. 248. 40. 65. 254. 59. 246. 154. 159. 52.]
SkI: [153. 181. 65. 93. 187. 59. 63. 159. 36. 32.]
Error: PyTorch result is different from Pillow result. MAE: 81.44833333333334
PTH: [153. 248. 40. 65. 254. 59. 246. 154. 159. 52.]
PIL: [153. 181. 65. 93. 187. 59. 63. 154. 36. 32.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 82.72333526611328
Error: PyTorch result is different from TF result. MAE: 82.72333526611328
- check output size: 24 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 74.85821533203125
PTH: [153. 248. 181. 65. 93. 254. 59. 246. 63. 159.]
SkI: [153. 181. 40. 65. 254. 187. 59. 63. 154. 159.]
Error: PyTorch result is different from Pillow result. MAE: 62.88483796296296
PTH: [153. 248. 181. 65. 93. 254. 59. 246. 63. 159.]
PIL: [153. 181. 40. 65. 93. 187. 59. 63. 154. 159.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 45.02430725097656
Error: PyTorch result is different from TF result. MAE: 74.85821533203125
- check output size: 30 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 66.76629638671875
PTH: [153. 248. 181. 40. 65. 93. 254. 187. 59. 246.]
SkI: [153. 248. 181. 40. 65. 93. 254. 59. 246. 63.]
Error: PyTorch result is different from Pillow result. MAE: 60.63851851851852
PTH: [153. 248. 181. 40. 65. 93. 254. 187. 59. 246.]
PIL: [153. 248. 181. 40. 65. 93. 254. 187. 246. 63.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 60.638519287109375
Error: PyTorch result is different from TF result. MAE: 66.76629638671875
- check output size: 32 <- 32
PyTorch result matches Scikit-image result.
PyTorch result matches Pillow result.
PyTorch result matches OpenCV INTER_NEAREST result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
PyTorch result matches TF result.
- check output size: 36 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 59.06301498413086
PTH: [153. 153. 248. 181. 40. 65. 93. 254. 187. 59.]
SkI: [153. 248. 181. 40. 65. 65. 93. 254. 187. 59.]
Error: PyTorch result is different from Pillow result. MAE: 59.06301440329218
PTH: [153. 153. 248. 181. 40. 65. 93. 254. 187. 59.]
PIL: [153. 248. 181. 40. 65. 65. 93. 254. 187. 59.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 47.17155456542969
Error: PyTorch result is different from TF result. MAE: 59.06301498413086
- check output size: 40 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 55.266666412353516
PTH: [153. 153. 248. 181. 40. 65. 65. 93. 254. 187.]
SkI: [153. 248. 181. 181. 40. 65. 93. 254. 254. 187.]
Error: PyTorch result is different from Pillow result. MAE: 52.56166666666667
PTH: [153. 153. 248. 181. 40. 65. 65. 93. 254. 187.]
PIL: [153. 248. 181. 181. 40. 65. 93. 93. 254. 187.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 52.80479049682617
Error: PyTorch result is different from TF result. MAE: 55.266666412353516
- check output size: 42 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 52.50944900512695
PTH: [153. 153. 248. 181. 40. 40. 65. 93. 254. 254.]
SkI: [153. 248. 248. 181. 40. 65. 65. 93. 254. 187.]
Error: PyTorch result is different from Pillow result. MAE: 49.97392290249433
PTH: [153. 153. 248. 181. 40. 40. 65. 93. 254. 254.]
PIL: [153. 248. 248. 181. 40. 65. 65. 93. 254. 187.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 47.168556213378906
Error: PyTorch result is different from TF result. MAE: 52.50944900512695
- check output size: 64 <- 32
PyTorch result matches Scikit-image result.
PyTorch result matches Pillow result.
PyTorch result matches OpenCV INTER_NEAREST result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
PyTorch result matches TF result.
- check output size: 72 <- 32
Error: PyTorch result is different from Scikit-image result. MAE: 34.035430908203125
PTH: [153. 153. 153. 248. 248. 181. 181. 40. 40. 65.]
SkI: [153. 153. 248. 248. 181. 181. 181. 40. 40. 65.]
Error: PyTorch result is different from Pillow result. MAE: 28.26999742798354
PTH: [153. 153. 153. 248. 248. 181. 181. 40. 40. 65.]
PIL: [153. 153. 248. 248. 181. 181. 181. 40. 40. 65.]
PyTorch result matches OpenCV INTER_NEAREST result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 18.03382110595703
Error: PyTorch result is different from TF result. MAE: 34.035430908203125
--> 0 of OpenCV INTER_NEAREST result mismatches
--> 9 of OpenCV INTER_NEAREST_EXACT result mismatches
--> 10 of Scikit-Image result mismatches
--> 10 of Pillow result mismatches
--> 10 of TensorFlow result mismatches
------ Check rescale op ------
recompute_scale_factor: True
- check scale_factor=0.1234
Error: different shapes: opencv (4, 4), (4, 4) vs sk-img (4, 4) vs scipy (4, 4) vs pth (3, 3)
- check scale_factor=0.5
Error: PyTorch result is different from Scikit-image result. MAE: 88.90885162353516
Error: PyTorch result is different from Scipy result. MAE: 88.90885162353516
PyTorch result matches OpenCV result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
- check scale_factor=0.6
Error: PyTorch result is different from Scikit-image result. MAE: 83.61126708984375
Error: PyTorch result is different from Scipy result. MAE: 83.61126708984375
PyTorch result matches OpenCV result.
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 81.30101776123047
- check scale_factor=0.7
Error: PyTorch result is different from Scikit-image result. MAE: 78.77823638916016
Error: PyTorch result is different from Scipy result. MAE: 78.77823638916016
Error: PyTorch result is different from OpenCV result. MAE: 34.127410888671875
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 73.97933959960938
- check scale_factor=0.9999
Error: different shapes: opencv (32, 32), (32, 32) vs sk-img (32, 32) vs scipy (32, 32) vs pth (31, 31)
- check scale_factor=1.0
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
PyTorch result matches OpenCV result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
- check scale_factor=1.00001
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
PyTorch result matches OpenCV result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
- check scale_factor=1.3
Error: different shapes: opencv (42, 42), (42, 42) vs sk-img (42, 42) vs scipy (42, 42) vs pth (41, 41)
- check scale_factor=1.71
Error: different shapes: opencv (55, 55), (55, 55) vs sk-img (55, 55) vs scipy (55, 55) vs pth (54, 54)
- check scale_factor=1.8
Error: different shapes: opencv (58, 58), (58, 58) vs sk-img (58, 58) vs scipy (58, 58) vs pth (57, 57)
- check scale_factor=1.9999
Error: different shapes: opencv (64, 64), (64, 64) vs sk-img (64, 64) vs scipy (64, 64) vs pth (63, 63)
- check scale_factor=2.0
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
PyTorch result matches OpenCV result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
- check scale_factor=2.3
Error: different shapes: opencv (74, 74), (74, 74) vs sk-img (74, 74) vs scipy (74, 74) vs pth (73, 73)
- check scale_factor=2.72
Error: PyTorch result is different from Scikit-image result. MAE: 28.029813766479492
Error: PyTorch result is different from Scipy result. MAE: 28.029813766479492
Error: PyTorch result is different from OpenCV result. MAE: 1.807196021080017
Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. MAE: 26.52005958557129
- check scale_factor=2.9999
Error: different shapes: opencv (96, 96), (96, 96) vs sk-img (96, 96) vs scipy (96, 96) vs pth (95, 95)
- check scale_factor=3.0
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
PyTorch result matches OpenCV result.
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
- check scale_factor=3.000001
PyTorch result matches Scikit-image result.
PyTorch result matches Scipy result.
Error: PyTorch result is different from OpenCV result. MAE: 45.80577087402344
PyTorch result matches OpenCV INTER_NEAREST_EXACT result.
- check scale_factor=3.34
Error: different shapes: opencv (107, 107), (107, 107) vs sk-img (107, 107) vs scipy (107, 107) vs pth (106, 106)
- check scale_factor=3.6789
Error: different shapes: opencv (118, 118), (118, 118) vs sk-img (118, 118) vs scipy (118, 118) vs pth (117, 117)
--> 10 of size mismatches
--> 3 of OpenCV INTER_NEAREST result mismatches
--> 3 of OpenCV INTER_NEAREST_EXACT result mismatches
--> 4 of Scikit-Image result mismatches
--> 4 of Scipy result mismatches
import numpy as np
import torch
import torch.nn.functional as F
import cv2
import skimage
import scipy
import PIL
import tensorflow as tf
from skimage.transform import rescale, resize
from scipy import ndimage
from PIL import Image
print("pytorch:", torch.__version__)
print("skimage:", skimage.__version__)
print("opencv:", cv2.__version__)
print("scipy:", scipy.__version__)
print("Pillow:", PIL.__version__)
print("TensorFlow:", tf.__version__)
torch.manual_seed(11)
t_in = torch.randint(0, 256, size=(1, 3, 32, 32), dtype=torch.float)
a_in = t_in.permute(0, 2, 3, 1)[0, ...].numpy()
pth_mode = "nearest-exact"
####### Check resize op
print("\n\n------ Check resize op ------")
test_sizes = [
2, 10, 16, 20, 24, 30,
32, 36, 40, 42,
64, 72,
]
print()
ocv_res_mismatches = []
ocv_res2_mismatches = []
skimg_res_mismatches = []
pillow_res_mismatches = []
tf_res_mismatches = []
for size in test_sizes:
print(f"- check output size: {size} <- 32")
t_out = F.interpolate(t_in, size=size, mode=pth_mode)
a_cv2_out = cv2.resize(a_in, dsize=(size, size), interpolation=cv2.INTER_NEAREST)
a_cv2_out2 = cv2.resize(a_in, dsize=(size, size), interpolation=cv2.INTER_NEAREST_EXACT)
a_skimg_out = resize(a_in, (size, size), order=0, preserve_range=True, anti_aliasing=False)
pil_in = Image.fromarray(a_in.astype("uint8"))
pil_out = pil_in.resize((size, size), resample=Image.NEAREST)
a_pil_out = np.array(pil_out).astype("float")
tf_in = tf.reshape(tf.constant(a_in), (1, ) + a_in.shape)
tf_out = tf.image.resize(tf_in, [size, size], method='nearest')[0, ...]
a_tf_out = np.array(tf_out)
a_t_out = t_out.permute(0, 2, 3, 1)[0, ...].numpy()
if not np.allclose(a_t_out, a_skimg_out):
print(
"Error: PyTorch result is different from Scikit-image result. "
f"MAE: {np.mean(np.abs(a_t_out - a_skimg_out))}"
f"\nPTH: {a_t_out[:10, 0, 0]}"
f"\nSkI: {a_skimg_out[:10, 0, 0]}"
)
skimg_res_mismatches.append(size)
else:
print("PyTorch result matches Scikit-image result.")
if not np.allclose(a_t_out, a_pil_out):
print(
"Error: PyTorch result is different from Pillow result. "
f"MAE: {np.mean(np.abs(a_t_out - a_pil_out))}"
f"\nPTH: {a_t_out[:10, 0, 0]}"
f"\nPIL: {a_pil_out[:10, 0, 0]}"
)
pillow_res_mismatches.append(size)
else:
print("PyTorch result matches Pillow result.")
if not np.allclose(a_t_out, a_cv2_out):
print(
"Error: PyTorch result is different from OpenCV INTER_NEAREST result. "
f"MAE: {np.mean(np.abs(a_t_out - a_cv2_out))}"
)
ocv_res_mismatches.append(size)
else:
print("PyTorch result matches OpenCV INTER_NEAREST result.")
if not np.allclose(a_t_out, a_cv2_out2):
print(
"Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. "
f"MAE: {np.mean(np.abs(a_t_out - a_cv2_out2))}"
)
ocv_res2_mismatches.append(size)
else:
print("PyTorch result matches OpenCV INTER_NEAREST_EXACT result.")
if not np.allclose(a_t_out, a_tf_out):
print(
"Error: PyTorch result is different from TF result. "
f"MAE: {np.mean(np.abs(a_t_out - a_tf_out))}"
)
tf_res_mismatches.append(size)
else:
print("PyTorch result matches TF result.")
print()
print(f"--> {len(ocv_res_mismatches)} of OpenCV INTER_NEAREST result mismatches")
print(f"--> {len(ocv_res2_mismatches)} of OpenCV INTER_NEAREST_EXACT result mismatches")
print(f"--> {len(skimg_res_mismatches)} of Scikit-Image result mismatches")
print(f"--> {len(pillow_res_mismatches)} of Pillow result mismatches")
print(f"--> {len(tf_res_mismatches)} of TensorFlow result mismatches")
####### Check rescale op
# No Pillow equivalent op
print("\n\n------ Check rescale op ------")
test_scales = [
0.1234, 0.5, 0.6, 0.7, 0.9999,
1.0, 1.00001, 1.3, 1.71, 1.8, 1.9999,
2.0, 2.3, 2.72, 2.9999,
3.0, 3.000001, 3.34, 3.6789
]
print()
recompute_scale_factor = True # matches scikit-image
print("recompute_scale_factor:", recompute_scale_factor)
print()
size_mismatches = []
ocv_res_mismatches = []
ocv_res2_mismatches = []
skimg_res_mismatches = []
scipy_res_mismatches = []
for scale_factor in test_scales:
print(f"- check scale_factor={scale_factor}")
t_out = F.interpolate(t_in, scale_factor=scale_factor, recompute_scale_factor=recompute_scale_factor, mode=pth_mode)
a_cv2_out = cv2.resize(a_in, dsize=None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_NEAREST)
a_cv2_out2 = cv2.resize(a_in, dsize=None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_NEAREST_EXACT)
a_skimg_out = rescale(a_in, (scale_factor, scale_factor), order=0, preserve_range=True, multichannel=True, anti_aliasing=False)
a_scipy_out = ndimage.zoom(a_in, (scale_factor, scale_factor, 1.0), order=0, grid_mode=True, mode="grid-constant")
if not (a_cv2_out2.shape[:-1] == a_cv2_out.shape[:-1] == a_skimg_out.shape[:-1] == a_scipy_out.shape[:-1] == t_out.shape[2:]):
print(
"Error: different shapes: "
f"opencv {a_cv2_out.shape[:-1]}, {a_cv2_out2.shape[:-1]} vs sk-img {a_skimg_out.shape[:-1]} "
f"vs scipy {a_scipy_out.shape[:-1]} vs pth {tuple(t_out.shape[2:])}"
"\n"
)
size_mismatches.append(scale_factor)
continue
a_t_out = t_out.permute(0, 2, 3, 1)[0, ...].numpy()
if not np.allclose(a_t_out, a_skimg_out):
print(
"Error: PyTorch result is different from Scikit-image result. "
f"MAE: {np.mean(np.abs(a_t_out - a_skimg_out))}"
)
skimg_res_mismatches.append(scale_factor)
else:
print("PyTorch result matches Scikit-image result.")
if not np.allclose(a_t_out, a_scipy_out):
print(
"Error: PyTorch result is different from Scipy result. "
f"MAE: {np.mean(np.abs(a_t_out - a_scipy_out))}"
)
scipy_res_mismatches.append(scale_factor)
else:
print("PyTorch result matches Scipy result.")
if not np.allclose(a_t_out, a_cv2_out):
print(
"Error: PyTorch result is different from OpenCV result. "
f"MAE: {np.mean(np.abs(a_t_out - a_cv2_out))}"
)
ocv_res_mismatches.append(scale_factor)
else:
print("PyTorch result matches OpenCV result.")
if not np.allclose(a_t_out, a_cv2_out2):
print(
"Error: PyTorch result is different from OpenCV INTER_NEAREST_EXACT result. "
f"MAE: {np.mean(np.abs(a_t_out - a_cv2_out2))}"
)
ocv_res2_mismatches.append(scale_factor)
else:
print("PyTorch result matches OpenCV INTER_NEAREST_EXACT result.")
print()
print(f"--> {len(size_mismatches)} of size mismatches")
print(f"--> {len(ocv_res_mismatches)} of OpenCV INTER_NEAREST result mismatches")
print(f"--> {len(ocv_res2_mismatches)} of OpenCV INTER_NEAREST_EXACT result mismatches")
print(f"--> {len(skimg_res_mismatches)} of Scikit-Image result mismatches")
print(f"--> {len(scipy_res_mismatches)} of Scipy result mismatches")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment