Exemplo n.º 1
0
        public static NeuralNet ReadNeuralNet(Func <int> readInt32, Func <double> readSingle, int inputDimension, ImageCoordinates inputCoordinates)
        {
            NeuralNet nn         = new NeuralNet();
            int       layerCount = readInt32();

            int curDimension = inputDimension;
            ImageCoordinates curCoordinates = inputCoordinates;

            CropTransform cropT = null;

            for (int i = 0; i < layerCount; i++)
            {
                Layer layer = ReadLayer(i, readInt32, readSingle, curCoordinates, curDimension, out cropT);
                if (layer != null)
                {
                    nn.AddLayer(layer);
                    curDimension   = layer.OutputDimension;
                    curCoordinates = layer.OutputCoordinates;

                    if (layer.LayerType == LayerType.DATA_LAYER && cropT != null)
                    {
                        nn.AddCropTransform(cropT);
                    }
                }
            }

            // Currently disabled because perf gains are not enough, in fact things seem somewhat slower ...
            // Console.Write("Linearizing sequences of linear layers ...");
            // Coalesce affine layers for running the network more optimally
            // nn.CoalesceToVirtual();
            // GC.Collect(2);
            // Console.WriteLine("Done.");
            return(nn);
        }
Exemplo n.º 2
0
        public static Layer ReadLayer(int index, Func <int> readInt32, Func <double> readSingle,
                                      ImageCoordinates inputCoordinates,
                                      int inputDim,
                                      out CropTransform cropT
                                      )
        {
            int typeID = readInt32();

            cropT = null;
            Console.Write("Reading layer with index {0,2}, of type {1}, input dimension {2}:", index, typeID, inputDim);
            switch (typeID)
            {
            case 0:         // "Data"
                Console.WriteLine("Data");
                var res = ReadDataLayer(index, readInt32, readSingle, inputCoordinates, inputDim);
                cropT = res.Item1;
                return(res.Item2);

            case 1:         // "InnerProduct"
                Console.Write("InnerProduct");
                return(ReadInnerProductLayer(index, readInt32, readSingle, inputCoordinates, inputDim));

            case 2:         // "ReLU"
                Console.WriteLine("ReLU");
                return(ReadRectifiedLinearLayer(index, readInt32, readSingle, inputCoordinates, inputDim));

            case 3:         // "SoftmaxWithLoss"
                Console.WriteLine("SoftMax");
                return(ReadSoftmaxLayer(readInt32, readSingle));

            case 4:         // "Convolution"
                Console.WriteLine("Convolution");
                return(ReadConvolutional(index, readInt32, readSingle, inputCoordinates, inputDim));

            case 5:         // "Pooling"
                Console.Write("Pooling, ");
                return(ReadPooling(index, readInt32, readSingle, inputCoordinates));

            case 6:         // "Dropout"
                Console.Write("Dropout, ");
                return(null);

            default:
                throw new Exception("Layer type ID not recognized: " + typeID);
            }
        }
Exemplo n.º 3
0
        static Tuple <CropTransform, Layer> ReadDataLayer(
            int index,
            Func <int> readInt32, Func <double> readSingle,
            ImageCoordinates inputCoordinates,
            int inputDim)
        {
            int has_transform_param = readInt32();

            if (has_transform_param == 0)
            {
                DataLayer layer = new DataLayer(index, inputDim, inputCoordinates);
                return(new Tuple <CropTransform, Layer>(null, layer));
            }

            // Scale
            double scale     = 1.0;
            int    has_scale = readInt32();

            if (has_scale != 0)
            {
                scale = readSingle();
            }

            // Mirror
            int has_mirror = readInt32();     // ignore

            // Crop size
            CropTransform cropT_       = null;
            int           has_crop_siz = readInt32();

            if (has_crop_siz != 0)
            {
                int crop_siz = readInt32();
                cropT_ = new CropTransform(inputCoordinates, inputDim, crop_siz, true);
            }
            // Mean value
            List <double> mean_val     = new List <double>();
            int           mean_val_cnt = readInt32();

            for (int x = 0; x < mean_val_cnt; x++)
            {
                mean_val.Add(readSingle());
            }
            // Mean file
            int has_mean_file = readInt32();

            double[] mean_image = null;
            if (has_mean_file != 0)
            {
                int mean_image_siz = readInt32();
                if (mean_image_siz > 0)
                {
                    mean_image = new double[mean_image_siz];
                }
                for (int x = 0; x < mean_image_siz; x++)
                {
                    mean_image[x] = readSingle();
                }
            }

            ImageCoordinates dataLayerInputCoordinates = inputCoordinates;
            int dataLayerInputDim = inputDim;

            double[] dataLayerMeanImage = mean_image;
            if (cropT_ != null)
            {
                dataLayerInputCoordinates = cropT_.TransformedCoordinates();
                dataLayerInputDim         = cropT_.TransformedDimension();
                if (mean_image != null)
                {
                    dataLayerMeanImage = cropT_.Transform(DenseVector.OfArray(mean_image)).ToArray();
                }
            }

            Layer l = new DataLayer(
                index,
                dataLayerInputDim,
                dataLayerInputCoordinates,
                scale,
                dataLayerMeanImage,
                mean_val);

            return(new Tuple <CropTransform, Layer>(cropT_, l));
        }
Exemplo n.º 4
0
 public void AddCropTransform(CropTransform crop)
 {
     cropT = crop;
 }