-
Notifications
You must be signed in to change notification settings - Fork 488
/
Copy pathbuild.rs
33 lines (26 loc) · 1.09 KB
/
build.rs
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
31
32
33
/// This build script does the following:
/// 1. Loads PyTorch weights into a model record.
/// 2. Saves the model record to a file using the `NamedMpkFileRecorder`.
use std::path::Path;
use burn::{
backend::NdArray,
record::{FullPrecisionSettings, NamedMpkFileRecorder, Recorder},
};
use burn_import::pytorch::PyTorchFileRecorder;
// Basic backend type (not used directly here).
type B = NdArray<f32>;
fn main() {
let device = Default::default();
// Load PyTorch weights into a model record.
let record: model::ModelRecord<B> = PyTorchFileRecorder::<FullPrecisionSettings>::default()
.load("pytorch/mnist.pt".into(), &device)
.expect("Failed to decode state");
// Save the model record to a file.
let recorder = NamedMpkFileRecorder::<FullPrecisionSettings>::default();
// Save into the OUT_DIR directory so that the model can be loaded by the
let out_dir = std::env::var("OUT_DIR").unwrap();
let file_path = Path::new(&out_dir).join("model/mnist");
recorder
.record(record, file_path)
.expect("Failed to save model record");
}