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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use ffi;
use util;
use std::path::Path;
use blob::Blob;
use common::Phase;
use std::ptr::Unique;
pub struct Net {
net: Unique<ffi::Struct_CaffeNet>,
}
impl Drop for Net {
fn drop(&mut self) {
unsafe { ffi::CaffeNetFree(self.net.get_mut()) }
}
}
impl Net {
pub fn new(path: &Path, phase: Phase) -> Net {
let path = util::to_c_str(path.to_str().unwrap());
let phase = match phase {
Phase::Train => ffi::CAFFE_TRAIN,
Phase::Test => ffi::CAFFE_TEST,
};
let net = unsafe { Unique::new(ffi::CaffeNetInit(path, phase)) };
Net::wrap(net)
}
pub fn wrap(net: Unique<ffi::Struct_CaffeNet>) -> Net {
assert!(!net.is_null());
Net { net: net }
}
pub fn copy_trained_layers_from(&mut self, path: &Path) {
let path = util::to_c_str(path.to_str().unwrap());
unsafe { ffi::CaffeNetCopyTrainedLayersFrom(self.net.get_mut(), path) }
}
pub fn blob(&mut self, name: &str) -> Blob {
let name = util::to_c_str(name);
let blob = unsafe {
Unique::new(ffi::CaffeNetGetBlobByName(self.net.get_mut(), name))
};
Blob::wrap(blob)
}
pub fn forward_prefilled(&mut self) {
unsafe { ffi::CaffeNetForwardPrefilled(self.net.get_mut()) }
}
}