/usr/include/InsightToolkit/Review/itkNeuralNetworkFileWriter.txx is in libinsighttoolkit3-dev 3.20.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | /*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkNeuralNetworkFileWriter.txx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __itkNeuralNetworkFileWriter_txx
#define __itkNeuralNetworkFileWriter_txx
#include <itksys/ios/sstream>
#include "itkNeuralNetworkFileWriter.h"
namespace itk
{
/** Constructor */
template<class TNetwork>
NeuralNetworkFileWriter<TNetwork>
::NeuralNetworkFileWriter()
{
this->m_FileName = "";
this->m_WriteWeightValuesType = Self::BINARY; //Default: binary output
}
template<class TNetwork>
void
NeuralNetworkFileWriter<TNetwork>
//Avoiding VS6 error::SetInput( const TNetwork* network )
::SetInput( TNetwork* network )
{
this->m_Network = network;
}
/** Destructor */
template<class TNetwork>
NeuralNetworkFileWriter<TNetwork>
::~NeuralNetworkFileWriter()
{
this->ClearFields();
}
template<class TNetwork>
void
NeuralNetworkFileWriter<TNetwork>
::ClearFields()
{
for (FieldsContainerType::size_type i = 0; i < this->m_Fields.size(); i++)
{
delete this->m_Fields[i];
}
this->m_Fields.clear();
}
template<class TNetwork>
void
NeuralNetworkFileWriter<TNetwork>
::PrintSelf( std::ostream& os, Indent indent ) const
{
Superclass::PrintSelf( os, indent );
}
template<class TNetwork>
const TNetwork *
NeuralNetworkFileWriter<TNetwork>
::GetInput() const
{
return this->m_Network.GetPointer();
}
/** Update the Writer */
template<class TNetwork>
void
NeuralNetworkFileWriter<TNetwork>
::Update()
{
this->m_OutputFile.open( this->m_FileName.c_str(), std::ios::binary | std::ios::out);
if(!this->m_OutputFile.is_open())
{
itkExceptionMacro("NeuralNetworkFileReader Write: Cannot open file");
return;
}
MET_FieldRecordType * mF;
mF = new MET_FieldRecordType;
if(MET_SystemByteOrderMSB())
{
MET_InitWriteField(mF, "BinaryDataByteOrderMSB", MET_STRING,
strlen("True"), "True");
}
else
{
MET_InitWriteField(mF, "BinaryDataByteOrderMSB", MET_STRING,
strlen("False"), "False");
}
this->m_Fields.push_back(mF);
mF = new MET_FieldRecordType;
MET_InitWriteField(mF, "ObjectType", MET_STRING,
strlen( this->m_Network->GetNameOfClass()), this->m_Network->GetNameOfClass());
this->m_Fields.push_back(mF);
mF = new MET_FieldRecordType;
MET_InitWriteField(mF, "NLayers", MET_UINT, this->m_Network->GetNumOfLayers());
this->m_Fields.push_back(mF);
mF = new MET_FieldRecordType;
MET_InitWriteField(mF, "NWeightSets",
MET_UINT, this->m_Network->GetNumOfWeightSets());
this->m_Fields.push_back(mF);
std::cout<<"Num of Weights = "<< this->m_Network->GetNumOfWeightSets()<<std::endl;
mF = new MET_FieldRecordType;
MET_InitWriteField(mF, "WeightValuesType", MET_UINT, this->m_WriteWeightValuesType);
mF->terminateRead=true;
this->m_Fields.push_back(mF);
if(!MET_Write(this->m_OutputFile, & this->m_Fields,'='))
{
itkExceptionMacro("MetaObject: Write: MET_Write Failed");
}
this->ClearFields();
//Get Layer Information for each layer
for(int i=0; i< this->m_Network->GetNumOfLayers(); i++)
{
LayerBaseConstPointer layerPtr = this->m_Network->GetLayer(i);
mF = new MET_FieldRecordType;
MET_InitWriteField(mF, "Layer_Id", MET_INT, i);
this->m_Fields.push_back(mF);
mF = new MET_FieldRecordType;
MET_InitWriteField(mF, "NumNodes", MET_INT, layerPtr->GetNumberOfNodes());
this->m_Fields.push_back(mF);
mF = new MET_FieldRecordType;
MET_InitWriteField(mF, "LayerType", MET_STRING,
strlen(layerPtr->GetNameOfClass ()), layerPtr->GetNameOfClass ());
this->m_Fields.push_back(mF);
typename TNetwork::TransferFunctionInterfaceType::ConstPointer tf = layerPtr->GetActivationFunction();
{
mF = new MET_FieldRecordType;
if (tf.IsNotNull())
{
MET_InitWriteField(mF, "TransferFunction", MET_STRING,
strlen(tf->GetNameOfClass()), tf->GetNameOfClass());
}
else
{
MET_InitWriteField(mF, "TransferFunction", MET_STRING,
strlen("NULL"), "NULL");
}
this->m_Fields.push_back(mF);
}
//NOTE: One and Two HiddenLayerBackpropogation networks don't have InputFunction Nodes.
typename TNetwork::InputFunctionInterfaceType::ConstPointer inputf = layerPtr->GetNodeInputFunction();
{
mF = new MET_FieldRecordType;
if ( inputf.IsNotNull() )
{
MET_InitWriteField(mF, "InputFunction", MET_STRING,
strlen(inputf->GetNameOfClass()), inputf->GetNameOfClass());
}
else
{
MET_InitWriteField(mF, "InputFunction", MET_STRING,
strlen("NULL"), "NULL");
}
mF->terminateRead=true;
this->m_Fields.push_back(mF);
}
}
if(!MET_Write(this->m_OutputFile, & this->m_Fields,'='))
{
itkExceptionMacro("MetaObject: Write: MET_Write Failed");
}
this->ClearFields();
for(int j=0; j< this->m_Network->GetNumOfWeightSets(); j++)
{
//typename Statistics::WeightSetBase<typename TNetwork::MeasurementVectorType, typename TNetwork::TargetVectorType>::ConstPointer
const typename TNetwork::LayerInterfaceType::WeightSetInterfaceType * const
weightset = this->m_Network->GetWeightSet(j);
mF = new MET_FieldRecordType;
MET_InitWriteField(mF, "WeightSet_Id", MET_INT,weightset->GetWeightSetId());
this->m_Fields.push_back(mF);
mF = new MET_FieldRecordType;
MET_InitWriteField(mF, "WeightSetType", MET_STRING,
strlen(weightset->GetNameOfClass ()),weightset->GetNameOfClass ());
this->m_Fields.push_back(mF);
mF = new MET_FieldRecordType;
MET_InitWriteField(mF, "SRC_Layer", MET_INT,weightset->GetInputLayerId());
this->m_Fields.push_back(mF);
mF = new MET_FieldRecordType;
MET_InitWriteField(mF, "DEST_Layer", MET_INT,weightset->GetOutputLayerId());
this->m_Fields.push_back(mF);
}
if(!MET_Write(this->m_OutputFile, & this->m_Fields,'='))
{
itkExceptionMacro("MetaObject: Write: MET_Write Failed");
}
//Writeout the weight values
this->ClearFields();
for(int j=0; j< this->m_Network->GetNumOfWeightSets(); j++)
{
//typename Statistics::WeightSetBase<typename TNetwork::MeasurementVectorType, typename TNetwork::TargetVectorType>::ConstPointer
const typename TNetwork::LayerInterfaceType::WeightSetInterfaceType * const
weightset = this->m_Network->GetWeightSet(j);
unsigned int rows = weightset->GetNumberOfOutputNodes();
unsigned int cols = weightset->GetNumberOfInputNodes();
switch(this->m_WriteWeightValuesType)
{
//ASCII only works for very small networks (i.e. less than 256 weights),
//and the MetaIO mechanism is not desigend for the way that this is used
//to write these files out.
// Comment this code out until it can be robustly written.
case ASCII:
{
// create local scope
{
std::cout << "UNSUPPORTED: ASCII only works for very small network types" << std::endl;
mF = new MET_FieldRecordType;
MET_InitWriteField(mF, "WeightValues", MET_FLOAT_ARRAY,
weightset->GetNumberOfOutputNodes()*weightset->GetNumberOfInputNodes(),
weightset->GetWeightValues());
this->m_Fields.push_back(mF);
}
// create local scope
{
if(!MET_Write(this->m_OutputFile, & this->m_Fields,'='))
{
itkExceptionMacro("MetaObject: Write: MET_Write Failed");
return;
}
}
}
break;
case BINARY:
{
//
// TODO: This is hardcoded to double for the weight values.
// Do the ITK Neural Nets allow single precision NN?
this->m_OutputFile.write( (char *)weightset->GetWeightValues(),
rows * cols * sizeof(double));
}
break;
default:
itkExceptionMacro("Unsupported type given");
break;
}
}
}
} // namespace itk
#endif
|