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
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
include!(concat!(env!("OUT_DIR"), "/onnx.rs"));

use self::tensor_proto::DataType;
use self::tensor_shape_proto::{dimension, Dimension};

impl From<i64> for dimension::Value {
    fn from(v: i64) -> Self {
        dimension::Value::DimValue(v)
    }
}

impl From<String> for dimension::Value {
    fn from(v: String) -> Self {
        dimension::Value::DimParam(v)
    }
}

impl<T: Into<Dimension>> From<Vec<T>> for TensorShapeProto {
    fn from(v: Vec<T>) -> Self {
        TensorShapeProto {
            dim: v.into_iter().map(Into::into).collect(),
        }
    }
}

impl<V: Into<dimension::Value>> From<V> for Dimension {
    fn from(v: V) -> Self {
        tensor_shape_proto::Dimension {
            denotation: String::default(),
            value: Some(v.into()),
        }
    }
}

impl<K: Into<String>, V: Into<String>> From<(K, V)> for StringStringEntryProto {
    fn from((k, v): (K, V)) -> Self {
        StringStringEntryProto {
            key: k.into(),
            value: v.into(),
        }
    }
}

impl<T: Into<String>> From<T> for ValueInfoProto {
    fn from(name: T) -> Self {
        ValueInfoProto {
            name: name.into(),
            ..ValueInfoProto::default()
        }
    }
}

impl From<f32> for TensorProto {
    fn from(data: f32) -> TensorProto {
        TensorProto {
            dims: vec![1],
            float_data: vec![data],
            data_type: DataType::Float as i32,
            ..TensorProto::default()
        }
    }
}

impl From<Vec<f32>> for TensorProto {
    fn from(data: Vec<f32>) -> TensorProto {
        TensorProto {
            dims: vec![data.len() as i64],
            float_data: data,
            data_type: DataType::Float as i32,
            ..TensorProto::default()
        }
    }
}

impl From<i32> for TensorProto {
    fn from(data: i32) -> TensorProto {
        TensorProto {
            dims: vec![1],
            int32_data: vec![data],
            data_type: DataType::Int32 as i32,
            ..TensorProto::default()
        }
    }
}

impl From<Vec<i32>> for TensorProto {
    fn from(data: Vec<i32>) -> TensorProto {
        TensorProto {
            dims: vec![data.len() as i64],
            int32_data: data,
            data_type: DataType::Int32 as i32,
            ..TensorProto::default()
        }
    }
}

impl From<i64> for TensorProto {
    fn from(data: i64) -> TensorProto {
        TensorProto {
            dims: vec![1],
            int64_data: vec![data],
            data_type: DataType::Int64 as i32,
            ..TensorProto::default()
        }
    }
}

impl From<Vec<i64>> for TensorProto {
    fn from(data: Vec<i64>) -> TensorProto {
        TensorProto {
            dims: vec![data.len() as i64],
            int64_data: data,
            data_type: DataType::Int64 as i32,
            ..TensorProto::default()
        }
    }
}

impl From<f64> for TensorProto {
    fn from(data: f64) -> TensorProto {
        TensorProto {
            dims: vec![1],
            double_data: vec![data],
            data_type: DataType::Double as i32,
            ..TensorProto::default()
        }
    }
}

impl From<Vec<f64>> for TensorProto {
    fn from(data: Vec<f64>) -> TensorProto {
        TensorProto {
            dims: vec![data.len() as i64],
            double_data: data,
            data_type: DataType::Double as i32,
            ..TensorProto::default()
        }
    }
}

impl From<u64> for TensorProto {
    fn from(data: u64) -> TensorProto {
        TensorProto {
            dims: vec![1],
            uint64_data: vec![data],
            data_type: DataType::Uint64 as i32,
            ..TensorProto::default()
        }
    }
}

impl From<Vec<u64>> for TensorProto {
    fn from(data: Vec<u64>) -> TensorProto {
        TensorProto {
            dims: vec![data.len() as i64],
            uint64_data: data,
            data_type: DataType::Uint64 as i32,
            ..TensorProto::default()
        }
    }
}