2using System.Collections.Generic;
4using System.Runtime.InteropServices;
6using System.Threading.Tasks;
51 m_file = H5F.open(strFile, H5F.OpenMode.ACC_RDONLY);
53 m_log.
FAIL(
"Failed opening HDF5 file: '" + strFile +
"'!");
63 private int get_num_links(H5GroupId hG)
65 H5GInfo info = H5G.getInfo(hG);
66 return (
int)info.nLinks;
69 private string get_name_by_idx(H5GroupId hg,
int i)
71 return H5L.getNameByIndex(hg,
".", H5IndexType.NAME, H5IterationOrder.NATIVE, i);
74 private Tuple<H5DataSetId, int> load_nd_datasetEx(
Blob<T> blob,
string strDatasetName,
bool bReshape,
int nMinDim = 1,
int nMaxDim =
int.MaxValue, H5GroupId
id =
null,
bool bAllowSingleItems =
false)
76 H5DataSetId ds =
null;
77 int nSingleItemSize = 0;
82 ds = H5D.open(
id, strDatasetName);
84 ds = H5D.open(m_file, strDatasetName);
87 m_log.FAIL(
"Failed to find the dataset '" + strDatasetName +
"'!");
90 H5DataSpaceId dsSpace = H5D.getSpace(ds);
92 m_log.FAIL(
"Failed to get the dataset space!");
94 int nDims = H5S.getSimpleExtentNDims(dsSpace);
95 m_log.CHECK_GE(nDims, nMinDim,
"The dataset dim is out of range!");
96 m_log.CHECK_LE(nDims, nMaxDim,
"The dataset dim is out of range!");
98 long[] rgDims = H5S.getSimpleExtentDims(dsSpace);
101 H5DataTypeId dsType = H5D.getType(ds);
103 m_log.FAIL(
"Failed to get the dataset type!");
105 H5T.H5TClass dataClass = H5T.getClass(dsType);
108 case H5T.H5TClass.FLOAT:
109 m_log.WriteLine(
"Datatype class: H5T_FLOAT");
112 case H5T.H5TClass.INTEGER:
113 m_log.WriteLine(
"Datatype class: H5T_INTEGER");
117 m_log.FAIL(
"Unsupported datatype class: " + dataClass.ToString());
121 List<int> rgBlobDims =
new List<int>();
122 for (
int i = 0; i < nDims; i++)
124 rgBlobDims.Add((
int)rgDims[i]);
135 if (!bAllowSingleItems || (rgBlobDims.Count == 1 && rgBlobDims[0] != 1))
137 string strSrcShape =
Utility.ToString<
int>(rgBlobDims);
138 m_log.FAIL(
"Cannot load blob from hdf5; shape mismatch. Source shape = " + strSrcShape +
", target shape = " + blob.
shape_string);
141 if (rgBlobDims.Count == 1)
142 nSingleItemSize = rgBlobDims[0];
146 catch (Exception excpt)
157 return new Tuple<H5DataSetId, int>(ds, nSingleItemSize);
170 public void load_nd_dataset(
Blob<T> blob,
string strDatasetName,
bool bReshape =
false,
int nMinDim = 1,
int nMaxDim =
int.MaxValue, H5GroupId
id =
null,
bool bAllowSingleItems =
false)
172 H5DataSetId ds =
null;
173 int nSingleItemSize = 0;
177 Tuple<H5DataSetId, int> ds1 = load_nd_datasetEx(blob, strDatasetName, bReshape, nMinDim, nMaxDim,
id, bAllowSingleItems);
179 nSingleItemSize = ds1.Item2;
181 H5DataTypeId dsType = H5D.getType(ds);
182 int nSize = H5T.getSize(dsType);
184 if (nSize ==
sizeof(
double))
186 double[] rgBuffer =
new double[blob.
count()];
187 H5Array<double> rgData =
new H5Array<double>(rgBuffer);
189 H5D.read<
double>(ds, dsType, rgData);
191 if (!bAllowSingleItems || nSingleItemSize == 0)
196 else if (nSize ==
sizeof(
float))
198 float[] rgBuffer =
new float[blob.
count()];
199 H5Array<float> rgData =
new H5Array<float>(rgBuffer);
201 H5D.read<
float>(ds, dsType, rgData);
203 if (!bAllowSingleItems || nSingleItemSize == 0)
208 else if (nSize ==
sizeof(
byte))
210 byte[] rgBuffer =
new byte[blob.
count()];
211 H5Array<byte> rgData =
new H5Array<byte>(rgBuffer);
213 H5D.read<
byte>(ds, dsType, rgData);
215 float[] rgf = rgBuffer.Select(p1 => (
float)p1).ToArray();
219 m_log.FAIL(
"The dataset size of '" + nSize.ToString() +
"' is not supported!");
221 catch (Exception excpt)
223 m_log.FAIL(excpt.Message);
244 m_file = H5F.open(strFile, H5F.OpenMode.ACC_RDONLY);
246 m_log.
FAIL(
"Failed opening HDF5 weights file: '" + strFile +
"'!");
249 H5GroupId hData =
null;
250 H5GroupId hLayer =
null;
254 hData = H5G.open(m_file,
"data");
255 if (hData ==
null || hData.Id <= 0)
256 m_log.FAIL(
"Failed to open the 'data' data stream within the HDF5 weights file: '" + strFile +
"'!");
258 int nNumLayers = get_num_links(hData);
260 for (
int i = 0; i < nNumLayers; i++)
262 string strSrcLayerName = get_name_by_idx(hData, i);
266 m_log.WriteLine(
"Ignoring source layer '" + strSrcLayerName +
"'.");
271 m_log.WriteLine(
"Copying source layer '" + strSrcLayerName +
"'.");
275 hLayer = H5G.open(hData, strSrcLayerName);
276 if (hLayer ==
null || hLayer.Id <= 0)
277 m_log.FAIL(
"Failed to open '" + strSrcLayerName +
"' layer in data stream within the HDF5 weights file: '" + strFile +
"'!");
280 int nNumSourceParams = get_num_links(hLayer);
281 m_log.CHECK_LE(nNumSourceParams, targetBlobs.
Count,
"Incompatible number of blobs for layer '" + strSrcLayerName +
"'!");
283 for (
int j = 0; j < nNumSourceParams; j++)
285 string strDatasetName = j.ToString();
287 if (!H5L.Exists(hLayer, strDatasetName))
298 m_log.FAIL(
"Incompatible number of blobs for layer '" + strSrcLayerName +
"'!");
302 load_nd_dataset(targetBlobs[j], strDatasetName,
false, 1,
int.MaxValue, hLayer,
true);
The Log class provides general output in text form.
void FAIL(string str)
Causes a failure which throws an exception with the desciptive text.
The Utility class provides general utility funtions.
static double[] ConvertVec(float[] rgf)
Convert an array of float to an array of generics.
The BlobCollection contains a list of Blobs.
int Count
Returns the number of items in the collection.
The Blob is the main holder of data that moves through the Layers of the Net.
void SetData(T[] rgData, int nCount=-1, bool bSetCount=true)
Sets a number of items within the Blob's data.
string shape_string
Returns a string describing the Blob's shape.
T[] mutable_cpu_data
Get data from the GPU and bring it over to the host, or Set data from the Host and send it over to th...
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
List< int > shape()
Returns an array where each element contains the shape of an axis of the Blob.
int count()
Returns the total number of items in the Blob.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Connects Layer's together into a direct acrylic graph (DAG) specified by a NetParameter
List< Layer< T > > layers
Returns the layers.
int layer_index_by_name(string strLayer)
Returns a Layer's index given its name.
DictionaryEx< string, int > param_names_index
Returns the dictionary look for parameter names to their indexes.
List< int > param_owners
Returns the list of parameter owner indexes.
The HDF5 object provides HDF5 dataset support to the HDF5DataLayer.
void Dispose()
Release all resources uses.
void CopyTrainedLayersFromHDF5(Net< T > net, Log log, string strFile)
Copy the weights from an HDF5 file into a Net.
HDF5()
The constructor used when loading weights into a Net.
HDF5(CudaDnn< T > cuda, Log log, string strFile)
The constructor.
void load_nd_dataset(Blob< T > blob, string strDatasetName, bool bReshape=false, int nMinDim=1, int nMaxDim=int.MaxValue, H5GroupId id=null, bool bAllowSingleItems=false)
Creates a new dataset from an HDF5 data file.
The HDF5Load interface is used to load weights into objects like a Net.
void CopyTrainedLayersFromHDF5(Net< T > net, Log log, string strFile)
Copy the weights from an HDF5 file into a Net.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
The MyCaffe.common namespace contains common MyCaffe classes.
The MyCaffe.layers.hdf5 namespace contains all HDF5 related layers.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...