ivy-llc / demos

Demos and Tutorials with Ivy
https://ivy.dev/docs/demos/
19 stars 61 forks source link

ResNet18 #5

Closed zhumakhan closed 1 year ago

guillesanbri commented 1 year ago

For reference, this is the code we have in the tests regarding this model :slightly_smiling_face:

@pytest.mark.parametrize("batch_size", [1])
@pytest.mark.parametrize("image_dims", [[224, 224]])
def test_resnet_18_imagenet(
    batch_size,
    image_dims,
    dev,
):
    ivy.set_backend("torch")
    with DefaultDevice(dev):
        try:
            model = torch.hub.load(
                "pytorch/vision:v0.10.0", "resnet18", pretrained=True
            )
        except urllib.error.URLError:
            pytest.skip()
        net = ivy.to_ivy_module(model.to(ivy.as_native_dev(dev)))
        x0 = ivy.random_uniform(
            low=0.0,
            high=1.0,
            shape=[batch_size] + [3] + image_dims,
            dtype=torch.float32,
            device=dev,
        )
        x1 = ivy.random_uniform(
            low=0.0,
            high=1.0,
            shape=[batch_size] + [3] + image_dims,
            dtype=torch.float32,
            device=dev,
        )
        ret0_nc = net(x0)
        ret1_nc = net(x1)
        assert not np.allclose(ivy.to_numpy(ret0_nc), ivy.to_numpy(ret1_nc))
        comp_network = compile_graph(net, x0)
        show_graph(
            net,
            x0,
            # fname="resnet_18_imagenet.html",  # uncomment this to save the graph locally
        )
        ret0_c = comp_network(x0)
        ret1_c = comp_network(x1)
        assert not np.allclose(ivy.to_numpy(ret0_c), ivy.to_numpy(ret1_c))
        assert np.allclose(ivy.to_numpy(ret0_nc), ivy.to_numpy(ret0_c))
        assert np.allclose(ivy.to_numpy(ret1_nc), ivy.to_numpy(ret1_c))
zhumakhan commented 1 year ago

@guillesanbri I have some confusion: should resnet18 be implemented using ivy functional OR can I just load resent18 from torch.hub as in the above example?

guillesanbri commented 1 year ago

The idea is to transpile existing models, you don't have to write one in Ivy 🙂

zhumakhan commented 1 year ago

Done. https://github.com/unifyai/demos/blob/main/temp_demos/compilation_of_various_models/resnet_18.ipynb