File: validate_resnet50_output.py

package info (click to toggle)
halide 21.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,752 kB
  • sloc: cpp: 289,334; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (30 lines) | stat: -rw-r--r-- 963 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import torch
import torchvision.models.resnet as resnet
import torch.nn as nn
import numpy as np
import sys

if __name__ == "__main__":
    assert len(sys.argv) == 3
    halide_output_file = sys.argv[1]

    seed = int(sys.argv[2])
    np.random.seed(seed)
    input_data = np.random.rand(1, 224, 224, 3).astype(np.float32)
    input_data = input_data.transpose(0, 3, 1, 2)
    image = torch.from_numpy(input_data)

    net = resnet.resnet50(pretrained=True)
    net.eval()
    result = net(image)
    # PyTorch resnet50 model doesn't compute softmax of the output.
    softmax = nn.Softmax(dim=1)
    ref_output = softmax(result)

    halide_output = np.fromfile(halide_output_file, dtype=np.float32)

    ref_output = ref_output.cpu().detach().numpy().reshape(halide_output.shape)

    np.testing.assert_almost_equal(np.argmax(halide_output), np.argmax(ref_output))
    np.testing.assert_almost_equal(halide_output, ref_output, decimal=2)
    print("Success!")