Beispiel #1
0
        public static LossMmod Deserialize(ProxyDeserialize deserialize, int networkType = 0)
        {
            if (deserialize == null)
            {
                throw new ArgumentNullException(nameof(deserialize));
            }

            deserialize.ThrowIfDisposed();

            var error = NativeMethods.LossMmod_deserialize_proxy(networkType,
                                                                 deserialize.NativePtr,
                                                                 out var net,
                                                                 out var errorMessage);

            Cuda.ThrowCudaException(error);
            switch (error)
            {
            case NativeMethods.ErrorType.DnnNotSupportNetworkType:
                throw new NotSupportNetworkTypeException(networkType);

            case NativeMethods.ErrorType.GeneralSerialization:
                throw new SerializationException(StringHelper.FromStdString(errorMessage, true));
            }

            return(new LossMmod(net, networkType));
        }
Beispiel #2
0
        public static LossMmod Deserialize(string path, int networkType = 0)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!File.Exists(path))
            {
                throw new FileNotFoundException($"{path} is not found", path);
            }

            var str   = Dlib.Encoding.GetBytes(path);
            var error = NativeMethods.LossMmod_deserialize(networkType,
                                                           str,
                                                           out var net,
                                                           out var errorMessage);

            Cuda.ThrowCudaException(error);
            switch (error)
            {
            case NativeMethods.ErrorType.DnnNotSupportNetworkType:
                throw new NotSupportNetworkTypeException(networkType);

            case NativeMethods.ErrorType.GeneralSerialization:
                throw new SerializationException(StringHelper.FromStdString(errorMessage, true));
            }

            return(new LossMmod(net, networkType));
        }
Beispiel #3
0
        public static LossMulticlassLogPerPixel Deserialize(string path, int networkType = 0)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!File.Exists(path))
            {
                throw new FileNotFoundException($"{path} is not found", path);
            }

            var str       = Dlib.Encoding.GetBytes(path);
            var strLength = str.Length;

            Array.Resize(ref str, strLength + 1);
            str[strLength] = (byte)'\0';
            var error = NativeMethods.loss_multiclass_log_per_pixel_deserialize(str,
                                                                                networkType,
                                                                                out var net,
                                                                                out var errorMessage);

            Cuda.ThrowCudaException(error);
            switch (error)
            {
            case NativeMethods.ErrorType.DnnNotSupportNetworkType:
                throw new NotSupportNetworkTypeException(networkType);

            case NativeMethods.ErrorType.GeneralSerialization:
                throw new SerializationException(StringHelper.FromStdString(errorMessage, true));
            }

            return(new LossMulticlassLogPerPixel(net, networkType));
        }
        public static LossMulticlassLogPerPixel Deserialize(byte[] content, int networkType = 0)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            var error = NativeMethods.LossMulticlassLogPerPixel_deserialize2(networkType,
                                                                             content,
                                                                             content.Length,
                                                                             out var net,
                                                                             out var errorMessage);

            Cuda.ThrowCudaException(error);
            switch (error)
            {
            case NativeMethods.ErrorType.DnnNotSupportNetworkType:
                throw new NotSupportNetworkTypeException(networkType);

            case NativeMethods.ErrorType.GeneralSerialization:
                throw new SerializationException(StringHelper.FromStdString(errorMessage, true));
            }

            return(new LossMulticlassLogPerPixel(net, networkType));
        }
Beispiel #5
0
        public IEnumerable <float[]> Probability <T>(IEnumerable <Matrix <T> > images, ulong batchSize = 128)
            where T : struct
        {
            if (images == null)
            {
                throw new ArgumentNullException(nameof(images));
            }
            if (!images.Any())
            {
                throw new ArgumentException();
            }
            if (images.Any(matrix => matrix == null))
            {
                throw new ArgumentException();
            }

            images.ThrowIfDisposed();

            Matrix <T> .TryParse <T>(out var imageType);

            var templateRows    = images.First().TemplateRows;
            var templateColumns = images.First().TemplateColumns;

            var array = images.Select(matrix => matrix.NativePtr).ToArray();
            var ret   = NativeMethods.LossMulticlassLog_probability(this.NetworkType,
                                                                    this.NativePtr,
                                                                    imageType.ToNativeMatrixElementType(),
                                                                    array,
                                                                    array.Length,
                                                                    templateRows,
                                                                    templateColumns,
                                                                    (uint)batchSize,
                                                                    out var vecOut);

            using (var vector = new StdVector <float>(vecOut))
            {
                Cuda.ThrowCudaException(ret);
                switch (ret)
                {
                case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                    throw new ArgumentException($"{imageType} is not supported.");
                }

                var probabilityArray = vector.ToArray();
                var batches          = (int)batchSize;
                var classes          = probabilityArray.Length / batches;

                var probability = new List <float[]>(Enumerable.Range(0, batches).Select <int, float[]>(i => null));
                for (var index = 0; index < batches; index++)
                {
                    probability[index] = probabilityArray.Skip(index * classes).Take(classes).ToArray();
                }

                return(probability);
            }
        }
Beispiel #6
0
        public static LossMmod Deserialize(ProxyDeserialize deserialize, int networkType = 0)
        {
            if (deserialize == null)
            {
                throw new ArgumentNullException(nameof(deserialize));
            }

            deserialize.ThrowIfDisposed();

            var error = NativeMethods.loss_mmod_deserialize_proxy(deserialize.NativePtr, networkType, out var net);

            Cuda.ThrowCudaException(error);

            return(new LossMmod(net, networkType));
        }
        public static LossMulticlassLogPerPixel Deserialize(ProxyDeserialize deserialize, int networkType = 0)
        {
            if (deserialize == null)
            {
                throw new ArgumentNullException(nameof(deserialize));
            }

            deserialize.ThrowIfDisposed();

            var error = Native.loss_multiclass_log_per_pixel_deserialize_proxy(deserialize.NativePtr, networkType, out var net);

            Cuda.ThrowCudaException(error);

            return(new LossMulticlassLogPerPixel(net, networkType));
        }
Beispiel #8
0
        public OutputLabels <IEnumerable <MModRect> > Operator <T>(IEnumerable <Matrix <T> > images, ulong batchSize = 128)
            where T : struct
        {
            if (images == null)
            {
                throw new ArgumentNullException(nameof(images));
            }
            if (!images.Any())
            {
                throw new ArgumentException();
            }
            if (images.Any(matrix => matrix == null))
            {
                throw new ArgumentException();
            }

            images.ThrowIfDisposed();

            using (var vecIn = new StdVector <Matrix <T> >(images))
            {
                Matrix <T> .TryParse <T>(out var imageType);

                var templateRows    = images.First().TemplateRows;
                var templateColumns = images.First().TemplateColumns;

                // vecOut is not std::vector<std::vector<mmod_rect>*>* but std::vector<std::vector<mmod_rect>>*.
                var ret = NativeMethods.loss_mmod_operator_matrixs(this.NativePtr,
                                                                   this.NetworkType,
                                                                   imageType.ToNativeMatrixElementType(),
                                                                   vecIn.NativePtr,
                                                                   templateRows,
                                                                   templateColumns,
                                                                   batchSize,
                                                                   out var vecOut);

                Cuda.ThrowCudaException(ret);
                switch (ret)
                {
                case ErrorType.MatrixElementTypeNotSupport:
                    throw new ArgumentException($"{imageType} is not supported.");
                }

                return(new Output(vecOut));
            }
        }
Beispiel #9
0
        public static LossMmod Deserialize(string path, int networkType = 0)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!File.Exists(path))
            {
                throw new FileNotFoundException($"{path} is not found", path);
            }

            var str   = Dlib.Encoding.GetBytes(path);
            var error = NativeMethods.loss_mmod_deserialize(str, networkType, out var net);

            Cuda.ThrowCudaException(error);

            return(new LossMmod(net, networkType));
        }
Beispiel #10
0
        public static LossMulticlassLog Deserialize(string path, int networkType = 0)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!File.Exists(path))
            {
                throw new FileNotFoundException($"{path} is not found", path);
            }

            var str   = Encoding.UTF8.GetBytes(path);
            var error = Native.loss_multiclass_log_deserialize(str, networkType, out var net);

            Cuda.ThrowCudaException(error);

            return(new LossMulticlassLog(net, networkType));
        }
        public OutputLabels <Matrix <ushort> > Operator <T>(IEnumerable <Matrix <T> > images, ulong batchSize = 128)
            where T : struct
        {
            if (images == null)
            {
                throw new ArgumentNullException(nameof(images));
            }
            if (!images.Any())
            {
                throw new ArgumentException();
            }
            if (images.Any(matrix => matrix == null))
            {
                throw new ArgumentException();
            }

            images.ThrowIfDisposed();

            Matrix <T> .TryParse <T>(out var imageType);

            var templateRows    = images.First().TemplateRows;
            var templateColumns = images.First().TemplateColumns;

            // vecOut is not std::vector<Matrix<float>*>* but std::vector<Matrix<float>>*.
            var array = images.Select(matrix => matrix.NativePtr).ToArray();
            var ret   = NativeMethods.LossMulticlassLogPerPixel_operator_matrixs(this.NetworkType,
                                                                                 this.NativePtr,
                                                                                 imageType.ToNativeMatrixElementType(),
                                                                                 array,
                                                                                 array.Length,
                                                                                 templateRows,
                                                                                 templateColumns,
                                                                                 (uint)batchSize,
                                                                                 out var vecOut);

            Cuda.ThrowCudaException(ret);
            switch (ret)
            {
            case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                throw new ArgumentException($"{imageType} is not supported.");
            }

            return(new Output(vecOut));
        }
Beispiel #12
0
        /// <summary>
        /// Performs one stochastic gradient update step based on the mini-batch of data and labels supplied.
        /// </summary>
        /// <typeparam name="T">The type of element in the matrix.</typeparam>
        /// <param name="trainer">The trainer object of <see cref="LossMmod"/>.</param>
        /// <param name="data">The training data.</param>
        /// <param name="label">The label.</param>
        /// <exception cref="ArgumentNullException"><paramref name="trainer"/>, <paramref name="data"/> or <paramref name="label"/> is null.</exception>
        /// <exception cref="ObjectDisposedException"><paramref name="trainer"/> is disposed.</exception>
        /// <exception cref="NotSupportedException">The specified type of element in the matrix does not supported.</exception>
        public static void TrainOneStep <T>(DnnTrainer <LossMmod> trainer, IEnumerable <Matrix <T> > data, IEnumerable <IEnumerable <MModRect> > label)
            where T : struct
        {
            if (trainer == null)
            {
                throw new ArgumentNullException(nameof(trainer));
            }
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (label == null)
            {
                throw new ArgumentNullException(nameof(label));
            }
            if (data.Count() != label.Count())
            {
                throw new ArgumentException($"The count of {nameof(data)} must equal to {nameof(label)}'s.");
            }

            trainer.ThrowIfDisposed();

            Matrix <T> .TryParse <T>(out var dataElementTypes);

            using (var dataVec = new StdVector <Matrix <T> >(data.ToArray()))
                using (var disposer = new EnumerableDisposer <StdVector <MModRect> >(label.Select(r => new StdVector <MModRect>(r))))
                    using (var labelVec = new StdVector <StdVector <MModRect> >(disposer.Collection))
                        using (new EnumerableDisposer <StdVector <MModRect> >(labelVec))
                        {
                            var ret = NativeMethods.LossMmod_trainer_train_one_step(trainer.Type,
                                                                                    trainer.NativePtr,
                                                                                    dataElementTypes.ToNativeMatrixElementType(),
                                                                                    dataVec.NativePtr,
                                                                                    NativeMethods.MatrixElementType.UInt32,
                                                                                    labelVec.NativePtr);
                            Cuda.ThrowCudaException(ret);

                            switch (ret)
                            {
                            case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                                throw new NotSupportedException($"{dataElementTypes} does not support");
                            }
                        }
        }
        /// <summary>
        /// Trains a supervised neural network based on the given training data.
        /// </summary>
        /// <typeparam name="T">The type of element in the matrix.</typeparam>
        /// <param name="trainer">The trainer object of <see cref="LossMulticlassLogPerPixel"/>.</param>
        /// <param name="data">The training data.</param>
        /// <param name="label">The label.</param>
        /// <exception cref="ArgumentNullException"><paramref name="trainer"/>, <paramref name="data"/> or <paramref name="label"/> is null.</exception>
        /// <exception cref="ObjectDisposedException"><paramref name="trainer"/> is disposed.</exception>
        /// <exception cref="NotSupportedException">The specified type of element in the matrix does not supported.</exception>
        public static void Train <T>(DnnTrainer <LossMulticlassLogPerPixel> trainer, IEnumerable <Matrix <T> > data, IEnumerable <Matrix <ushort> > label)
            where T : struct
        {
            if (trainer == null)
            {
                throw new ArgumentNullException(nameof(trainer));
            }
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (label == null)
            {
                throw new ArgumentNullException(nameof(label));
            }
            if (data.Count() != label.Count())
            {
                throw new ArgumentException($"The count of {nameof(data)} must equal to {nameof(label)}'s.");
            }

            trainer.ThrowIfDisposed();

            Matrix <T> .TryParse <T>(out var dataElementTypes);

            using (var dataVec = new StdVector <Matrix <T> >(data))
                using (var labelVec = new StdVector <Matrix <ushort> >(label))
                {
                    var ret = NativeMethods.LossMulticlassLogPerPixel_trainer_train(trainer.Type,
                                                                                    trainer.NativePtr,
                                                                                    dataElementTypes.ToNativeMatrixElementType(),
                                                                                    dataVec.NativePtr,
                                                                                    NativeMethods.MatrixElementType.UInt32,
                                                                                    labelVec.NativePtr);
                    Cuda.ThrowCudaException(ret);

                    switch (ret)
                    {
                    case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                        throw new NotSupportedException($"{dataElementTypes} does not support");
                    }
                }
        }
Beispiel #14
0
        /// <summary>
        /// Performs one stochastic gradient update step based on the mini-batch of data and labels supplied.
        /// </summary>
        /// <typeparam name="T">The type of element in the matrix.</typeparam>
        /// <param name="trainer">The trainer object of <see cref="LossMulticlassLogPerPixel"/>.</param>
        /// <param name="data">The training data.</param>
        /// <param name="label">The label.</param>
        /// <exception cref="ArgumentNullException"><paramref name="trainer"/>, <paramref name="data"/> or <paramref name="label"/> is null.</exception>
        /// <exception cref="ObjectDisposedException"><paramref name="trainer"/> is disposed.</exception>
        /// <exception cref="NotSupportedException">The specified type of element in the matrix does not supported.</exception>
        public static void TrainOneStep <T>(DnnTrainer <LossMulticlassLogPerPixel> trainer, IEnumerable <Matrix <T> > data, IEnumerable <Matrix <ushort> > label)
            where T : struct
        {
            if (trainer == null)
            {
                throw new ArgumentNullException(nameof(trainer));
            }
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (label == null)
            {
                throw new ArgumentNullException(nameof(label));
            }

            trainer.ThrowIfDisposed();

            Matrix <T> .TryParse <T>(out var dataElementTypes);

            using (var dataVec = new StdVector <Matrix <T> >(data))
                using (var labelVec = new StdVector <Matrix <ushort> >(label))
                {
                    var ret = NativeMethods.dnn_trainer_loss_multiclass_log_per_pixel_train_one_step(trainer.NativePtr,
                                                                                                     trainer.Type,
                                                                                                     dataElementTypes.ToNativeMatrixElementType(),
                                                                                                     dataVec.NativePtr,
                                                                                                     NativeMethods.MatrixElementType.UInt16,
                                                                                                     labelVec.NativePtr);
                    Cuda.ThrowCudaException(ret);

                    switch (ret)
                    {
                    case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                        throw new NotSupportedException($"{dataElementTypes} does not support");
                    }
                }
        }
Beispiel #15
0
        public static void TestOneStep <T>(DnnTrainer <LossMetric> trainer, IEnumerable <Matrix <T> > data, IEnumerable <uint> label)
            where T : struct
        {
            if (trainer == null)
            {
                throw new ArgumentNullException(nameof(trainer));
            }
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (label == null)
            {
                throw new ArgumentNullException(nameof(label));
            }

            Matrix <T> .TryParse <T>(out var dataElementTypes);

            using (var dataVec = new StdVector <Matrix <T> >(data))
                using (var labelVec = new StdVector <uint>(label))
                {
                    var ret = NativeMethods.LossMetric_trainer_test_one_step(trainer.Type,
                                                                             trainer.NativePtr,
                                                                             dataElementTypes.ToNativeMatrixElementType(),
                                                                             dataVec.NativePtr,
                                                                             NativeMethods.MatrixElementType.UInt32,
                                                                             labelVec.NativePtr);
                    Cuda.ThrowCudaException(ret);

                    switch (ret)
                    {
                    case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                        throw new NotSupportedException($"{dataElementTypes} does not support");
                    }
                }
        }