#![cfg_attr( all(feature = "test-integrations", feature = "nightly"), feature(generic_const_exprs) )] #[cfg(all(feature = "test-integrations", feature = "nightly"))] mod model { use dfdx::prelude::*; type BasicBlock = Residual<( Conv2D, BatchNorm2D, ReLU, Conv2D, BatchNorm2D, )>; type Downsample = GeneralizedResidual< ( Conv2D, BatchNorm2D, ReLU, Conv2D, BatchNorm2D, ), (Conv2D, BatchNorm2D), >; type Head = ( Conv2D<3, 64, 7, 2, 3>, BatchNorm2D<64>, ReLU, MaxPool2D<3, 2, 1>, ); pub type Resnet18 = ( Head, (BasicBlock<64>, ReLU, BasicBlock<64>, ReLU), (Downsample<64, 128>, ReLU, BasicBlock<128>, ReLU), (Downsample<128, 256>, ReLU, BasicBlock<256>, ReLU), (Downsample<256, 512>, ReLU, BasicBlock<512>, ReLU), (AvgPoolGlobal, Linear<512, NUM_CLASSES>), ); } #[test] #[cfg(all(feature = "test-integrations", feature = "nightly"))] fn test_resnet18_f32_inference() { use dfdx::prelude::*; let dev: AutoDevice = Default::default(); let mut model = dev.build_module::, f32>(); model.load("./tests/resnet18.npz").unwrap(); let mut x: Tensor, f32, _> = dev.zeros(); x.load_from_npy("./tests/resnet18_x.npy").unwrap(); let mut y: Tensor, f32, _> = dev.zeros(); y.load_from_npy("./tests/resnet18_y.npy").unwrap(); let p = model.forward(x.clone()); let p = p.array(); let y = y.array(); for i in 0..10 { for j in 0..1000 { assert!( (p[i][j] - y[i][j]).abs() <= 1e-5, "p[{i}][{j}]={} y[{i}][{j}]={}", p[i][j], y[i][j] ); } } let p2 = model.forward(x); assert_eq!(p, p2.array()); }