MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
MyCaffeModelData.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7
8namespace MyCaffe.basecode
9{
13 public class MyCaffeModelData
14 {
15 string m_strModelDescription;
16 string m_strSolverDescription = null;
17 byte[] m_rgWeights;
18 byte[] m_rgImageMean;
19 string m_strLastModelFileName = "";
20 string m_strLastSolverFileName = "";
21 string m_strLastImageMeanFileName = "";
22 string m_strLastWeightsFileName = "";
23 string m_strOriginalDownloadFile = null;
24 List<int> m_rgInputShape = new List<int>();
25
34 public MyCaffeModelData(string strModelDesc, byte[] rgWeights, byte[] rgImageMean = null, string strSolverDescription = null, List<int> rgInputShape = null)
35 {
36 m_strModelDescription = strModelDesc;
37 m_rgWeights = rgWeights;
38 m_rgImageMean = rgImageMean;
39 m_strSolverDescription = strSolverDescription;
40
41 if (rgInputShape != null)
42 m_rgInputShape = rgInputShape;
43 }
44
48 public List<int> InputShape
49 {
50 get { return m_rgInputShape; }
51 set { m_rgInputShape = value; }
52 }
53
57 public string ModelDescription
58 {
59 get { return m_strModelDescription; }
60 set { m_strModelDescription = value; }
61 }
62
66 public string SolverDescription
67 {
68 get { return m_strSolverDescription; }
69 set { m_strSolverDescription = value; }
70 }
71
75 public byte[] Weights
76 {
77 get { return m_rgWeights; }
78 }
79
83 public byte[] ImageMean
84 {
85 get { return m_rgImageMean; }
86 }
87
92 {
93 get { return m_strOriginalDownloadFile; }
94 set { m_strOriginalDownloadFile = value; }
95 }
96
102 public void Save(string strFolder, string strName)
103 {
104 string strModel = strFolder.TrimEnd('\\') + "\\" + strName + "_model_desc.prototxt";
105
106 if (File.Exists(strModel))
107 File.Delete(strModel);
108
109 m_strLastModelFileName = strModel;
110 using (StreamWriter sr = new StreamWriter(strModel))
111 {
112 sr.WriteLine(m_strModelDescription);
113 }
114
115 if (!string.IsNullOrEmpty(m_strSolverDescription))
116 {
117 string strSolver = strFolder.TrimEnd('\\') + "\\" + strName + "_solver_desc.prototxt";
118
119 if (File.Exists(strSolver))
120 File.Delete(strSolver);
121
122 m_strLastSolverFileName = strSolver;
123 using (StreamWriter sr = new StreamWriter(strSolver))
124 {
125 sr.WriteLine(strSolver);
126 }
127 }
128
129 if (m_rgWeights != null)
130 {
131 string strWts = strFolder.TrimEnd('\\') + "\\" + strName + "_weights.mycaffemodel";
132
133 if (File.Exists(strWts))
134 File.Delete(strWts);
135
136 m_strLastWeightsFileName = strWts;
137 using (FileStream fs = new FileStream(strWts, FileMode.CreateNew, FileAccess.Write))
138 using (BinaryWriter bw = new BinaryWriter(fs))
139 {
140 bw.Write(m_rgWeights);
141 }
142 }
143 else
144 {
145 m_strLastWeightsFileName = "";
146 }
147
148 if (m_rgImageMean != null)
149 {
150 string strWts = strFolder.TrimEnd('\\') + "\\" + strName + "_image_mean.bin";
151
152 if (File.Exists(strWts))
153 File.Delete(strWts);
154
155 m_strLastImageMeanFileName = strWts;
156 using (FileStream fs = new FileStream(strWts, FileMode.CreateNew, FileAccess.Write))
157 using (BinaryWriter bw = new BinaryWriter(fs))
158 {
159 bw.Write(m_rgImageMean);
160 }
161 }
162 else
163 {
164 m_strLastImageMeanFileName = "";
165 }
166 }
167
172 {
173 get { return m_strLastModelFileName; }
174 }
175
180 {
181 get { return m_strLastSolverFileName; }
182 }
183
188 {
189 get { return m_strLastWeightsFileName; }
190 }
191
196 {
197 get { return m_strLastImageMeanFileName; }
198 }
199 }
200}
The MyCaffeModelData object contains the model descriptor, model weights and optionally the image mea...
string LastSavedModeDescriptionFileName
Returns the file name of the last saved model description file.
void Save(string strFolder, string strName)
Save the model data to the specified folder under the specified name.
MyCaffeModelData(string strModelDesc, byte[] rgWeights, byte[] rgImageMean=null, string strSolverDescription=null, List< int > rgInputShape=null)
The constructor.
byte[] Weights
Returns the model weights.
string SolverDescription
Get/set the solver description.
string LastSavedImageMeanFileName
Returns the file name of the last saved image mean file.
string OriginalDownloadFile
Specifies the original download file, if any.
byte[] ImageMean
Returns the image mean if one was specified, or null.
string LastSavedWeightsFileName
Returns the file name of the last saved weights file.
string LastSaveSolverDescriptionFileName
Returns the file name of the last saved solver description file.
List< int > InputShape
Get/set the data input shape.
string ModelDescription
Get/set the model descriptor.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12