internal NormalizedFunction(IntPtr ptr) { if (ptr == IntPtr.Zero) { throw new ArgumentException("Can not pass IntPtr.Zero", nameof(ptr)); } var functionType = typeof(TFunction); var svmFunction = functionType.GetGenericTypeDefinition(); if (!FunctionTypesRepository.Types.TryGetValue(svmFunction, out var svmFunctionType)) { throw new ArgumentException(); } var kernelType = functionType.GenericTypeArguments[1].GetGenericTypeDefinition(); if (!KernelTypesRepository.KernelTypes.TryGetValue(kernelType, out var svmKernelType)) { throw new ArgumentException(); } var elementType = functionType.GenericTypeArguments[0]; if (!KernelTypesRepository.ElementTypes.TryGetValue(elementType, out var sampleType)) { throw new ArgumentException(); } this._Parameter = new KernelBaseParameter(svmKernelType, sampleType, 0, 0); this._SvmFunctionType = svmFunctionType; this._Imp = this.CreateImp(sampleType.ToNativeMatrixElementType()); this.NativePtr = ptr; }
internal FloatImp(DlibObject parent, KernelBaseParameter kernelParameter, NativeMethods.MatrixElementType elementType, NativeMethods.SvmFunctionType functionType) : base(parent, kernelParameter, elementType, functionType) { }
public BatchTrainer() { BatchTrainerHelper.GetTypes <TScalar, TTrainer>(out _, out var svmTrainerType, out var svmKernelType, out var sampleType); this._Parameter = new KernelBaseParameter(svmKernelType, sampleType, 0, 0); this._SvmTrainerType = svmTrainerType; this._Bridge = CreateBridge(this._Parameter, this._SvmTrainerType); var error = NativeMethods.batch_trainer_new(this._Parameter.KernelType.ToNativeKernelType(), this._Parameter.SampleType.ToNativeMatrixElementType(), svmTrainerType, out var ret); switch (error) { case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{sampleType} is not supported."); case NativeMethods.ErrorType.SvmBatchTrainerNotSupport: throw new ArgumentException($"{svmTrainerType} is not supported."); case NativeMethods.ErrorType.SvmKernelNotSupport: throw new ArgumentException($"{svmKernelType} is not supported."); } this.NativePtr = ret; }
public SvmCTrainer() { var kernelType = typeof(TKernel).GetGenericTypeDefinition(); if (!KernelTypesRepository.KernelTypes.TryGetValue(kernelType, out var svmKernelType)) { throw new ArgumentException(); } var elementType = typeof(TKernel).GenericTypeArguments[0]; if (!KernelTypesRepository.ElementTypes.TryGetValue(elementType, out var sampleType)) { throw new ArgumentException(); } this._Parameter = new KernelBaseParameter(svmKernelType, sampleType, 0, 0); var error = NativeMethods.svm_c_trainer_new(svmKernelType.ToNativeKernelType(), sampleType.ToNativeMatrixElementType(), out var ret); switch (error) { case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{this._Parameter.SampleType} is not supported."); case NativeMethods.ErrorType.SvmKernelNotSupport: throw new ArgumentException($"{this._Parameter.KernelType} is not supported."); } this._Imp = CreateImp(this._Parameter); this.NativePtr = ret; }
public NormalizedFunction() { var functionType = typeof(TFunction); var svmFunction = functionType.GetGenericTypeDefinition(); if (!FunctionTypesRepository.Types.TryGetValue(svmFunction, out var svmFunctionType)) { throw new ArgumentException(); } var kernelType = functionType.GenericTypeArguments[1].GetGenericTypeDefinition(); if (!KernelTypesRepository.KernelTypes.TryGetValue(kernelType, out var svmKernelType)) { throw new ArgumentException(); } var elementType = functionType.GenericTypeArguments[0]; if (!KernelTypesRepository.ElementTypes.TryGetValue(elementType, out var sampleType)) { throw new ArgumentException(); } this._Parameter = new KernelBaseParameter(svmKernelType, sampleType, 0, 0); this._SvmFunctionType = svmFunctionType; this._Imp = this.CreateImp(sampleType.ToNativeMatrixElementType()); this.NativePtr = NativeMethods.normalized_function_new(this._Parameter.KernelType.ToNativeKernelType(), this._Parameter.SampleType.ToNativeMatrixElementType(), this._Parameter.TemplateRows, this._Parameter.TemplateColumns, svmFunctionType); }
internal DecisionFunction(IntPtr ptr, KernelBaseParameter parameter, bool isEnabledDispose = true) : base(isEnabledDispose) { this._Parameter = parameter; this.NativePtr = ptr; }
internal Imp(DlibObject parent, KernelBaseParameter kernelParameter, NativeMethods.MatrixElementType elementType, NativeMethods.SvmFunctionType functionType) { this.Parent = parent ?? throw new ArgumentNullException(nameof(parent)); this.KernelParameter = kernelParameter; this.ElementType = elementType; this.FunctionType = functionType; }
internal SvmPegasos(IntPtr ptr, KernelBaseParameter parameter, bool isEnabledDispose = true) : base(isEnabledDispose) { this._Bridge = CreateBridge(parameter); this._Parameter = parameter; this.NativePtr = ptr; }
private static Imp <TScalar> CreateImp(KernelBaseParameter parameter) { switch (parameter.SampleType) { case MatrixElementTypes.Double: return(new DoubleImp(parameter) as Imp <TScalar>); default: throw new NotSupportedException(); } }
public SvmCTrainer(TKernel kernelBase, TScalar c) { if (kernelBase == null) { throw new ArgumentNullException(nameof(kernelBase)); } kernelBase.ThrowIfDisposed(); this._Parameter = new KernelBaseParameter(kernelBase.KernelType, kernelBase.SampleType, 0, 0); this._Imp = CreateImp(this._Parameter); this.NativePtr = this._Imp.Create(kernelBase.NativePtr, c); }
private static Bridge <TScalar> CreateBridge(KernelBaseParameter parameter) { switch (parameter.SampleType) { case MatrixElementTypes.Float: return(new FloatBridge(parameter) as Bridge <TScalar>); case MatrixElementTypes.Double: return(new DoubleBridge(parameter) as Bridge <TScalar>); default: throw new NotSupportedException(); } }
internal BatchTrainer(IntPtr ptr, bool isEnabledDispose = true) : base(isEnabledDispose) { BatchTrainerHelper.GetTypes <TScalar, TTrainer>(out _, out var svmTrainerType, out var svmKernelType, out var sampleType); this._Parameter = new KernelBaseParameter(svmKernelType, sampleType, 0, 0); this._SvmTrainerType = svmTrainerType; this._Bridge = CreateBridge(this._Parameter, this._SvmTrainerType); this.NativePtr = ptr; }
internal ReducedDecisionFunctionTrainer2(IntPtr ptr, bool isEnabledDispose = true) : base(isEnabledDispose) { TrainerHelper.GetTypes <TScalar, TTrainer>(out _, out var svmTrainerType, out var svmKernelType, out var sampleType); this._Parameter = new KernelBaseParameter(svmKernelType, sampleType, 0, 0); this._SvmTrainerType = svmTrainerType; this._Imp = CreateImp(this._Parameter); this.NativePtr = ptr; }
public static DecisionFunction <TScalar, TKernel> Deserialize(string path, int templateRows, int templateColumns) { if (path == null) { throw new ArgumentNullException(nameof(path)); } if (!File.Exists(path)) { throw new FileNotFoundException($"{path} is not found", path); } KernelFactory.TryParse <TScalar>(out MatrixElementTypes sampleType); KernelFactory.TryParse <TKernel>(out SvmKernelType kernelType); var param = new KernelBaseParameter(kernelType, sampleType, templateRows, templateColumns); var str = Dlib.Encoding.GetBytes(path); var strLength = str.Length; Array.Resize(ref str, strLength + 1); str[strLength] = (byte)'\0'; var error = NativeMethods.deserialize_decision_function(str, kernelType.ToNativeKernelType(), sampleType.ToNativeMatrixElementType(), templateRows, templateColumns, out var ret, out var errorMessage); switch (error) { case NativeMethods.ErrorType.GeneralSerialization: throw new SerializationException(StringHelper.FromStdString(errorMessage, true)); case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{param.SampleType} is not supported."); case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport: throw new ArgumentException($"{nameof(param.TemplateColumns)} or {nameof(param.TemplateRows)} is not supported."); case NativeMethods.ErrorType.SvmKernelNotSupport: throw new ArgumentException($"{param.KernelType} is not supported."); } return(new DecisionFunction <TScalar, TKernel>(ret, param)); }
public SvmPegasos(TKernel kernelBase, TScalar lambda, TScalar tolerance, uint maxNumSupportVector) { if (kernelBase == null) { throw new ArgumentNullException(nameof(kernelBase)); } kernelBase.ThrowIfDisposed(); this._Parameter = new KernelBaseParameter(kernelBase); this._Bridge = CreateBridge(this._Parameter); this.NativePtr = this._Bridge.Create(kernelBase, lambda, tolerance, maxNumSupportVector); }
public BatchTrainer(TTrainer trainer, TScalar minLearningRate, bool verbose, bool useCache, int cacheSize) { trainer.ThrowIfDisposed(); BatchTrainerHelper.GetTypes <TScalar, TTrainer>(out _, out var svmTrainerType, out var svmKernelType, out var sampleType); this._Parameter = new KernelBaseParameter(svmKernelType, sampleType, 0, 0); this._SvmTrainerType = svmTrainerType; this._Bridge = CreateBridge(this._Parameter, svmTrainerType); this.NativePtr = this._Bridge.Create(trainer.NativePtr, minLearningRate, verbose, useCache, cacheSize); }
public static ReducedDecisionFunctionTrainer2 <TScalar, TKernel, TTrainer> Reduced2 <TScalar, TKernel, TTrainer>(TTrainer trainer, uint numBaseVector, double eps = 1e-3) where TScalar : struct where TKernel : KernelBase where TTrainer : Trainer <TScalar> { if (trainer == null) { throw new ArgumentNullException(nameof(trainer)); } trainer.ThrowIfDisposed(); TrainerHelper.GetTypes <TScalar, TTrainer>(out var trainerType, out var svmTrainerType, out var svmKernelType, out var sampleType); var parameter = new KernelBaseParameter(svmKernelType, sampleType, 0, 0); var error = NativeMethods.reduced2(parameter.KernelType.ToNativeKernelType(), parameter.SampleType.ToNativeMatrixElementType(), parameter.TemplateRows, parameter.TemplateColumns, svmTrainerType, trainer.NativePtr, numBaseVector, eps, out var ret); switch (error) { case NativeMethods.ErrorType.SvmTrainerNotSupport: throw new ArgumentException($"{trainerType} is not supported."); case NativeMethods.ErrorType.SvmKernelNotSupport: throw new ArgumentException($"{svmKernelType} is not supported."); } return(new ReducedDecisionFunctionTrainer2 <TScalar, TKernel, TTrainer>(ret)); }
public ReducedDecisionFunctionTrainer2(TTrainer trainer, uint numBaseVector, double eps = 1e-3) { TrainerHelper.GetTypes <TScalar, TTrainer>(out var trainerType, out var svmTrainerType, out var svmKernelType, out var sampleType); if (!(numBaseVector > 0)) { throw new ArgumentOutOfRangeException(nameof(numBaseVector)); } if (!(eps > 0)) { throw new ArgumentOutOfRangeException(nameof(eps)); } this._Parameter = new KernelBaseParameter(svmKernelType, sampleType, 0, 0); this._SvmTrainerType = svmTrainerType; this._Imp = CreateImp(this._Parameter); var error = NativeMethods.reduced_decision_function_trainer2_new(this._Parameter.KernelType.ToNativeKernelType(), this._Parameter.SampleType.ToNativeMatrixElementType(), this._Parameter.TemplateRows, this._Parameter.TemplateColumns, svmTrainerType, trainer.NativePtr, numBaseVector, eps, out var ret); switch (error) { case NativeMethods.ErrorType.SvmTrainerNotSupport: throw new ArgumentException($"{trainerType} is not supported."); case NativeMethods.ErrorType.SvmKernelNotSupport: throw new ArgumentException($"{svmKernelType} is not supported."); } this.NativePtr = ret; }
public KCentroid(TKernel kernelBase, double tolerance = 0.001d, uint maxDictionarySize = 1000000, bool removeOldestFirst = false) { if (kernelBase == null) { throw new ArgumentNullException(nameof(kernelBase)); } kernelBase.ThrowIfDisposed(); this._Bridge = CreateBridge(new KernelBaseParameter(kernelBase)); this._Parameter = new KernelBaseParameter(kernelBase); var error = NativeMethods.kcentroid_new(this._Parameter.KernelType.ToNativeKernelType(), this._Parameter.SampleType.ToNativeMatrixElementType(), this._Parameter.TemplateRows, this._Parameter.TemplateColumns, kernelBase.NativePtr, tolerance, maxDictionarySize, removeOldestFirst, out var ret); switch (error) { case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{this._Parameter.SampleType} is not supported."); case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport: throw new ArgumentException($"{nameof(this._Parameter.TemplateColumns)} or {nameof(this._Parameter.TemplateRows)} is not supported."); case NativeMethods.ErrorType.SvmKernelNotSupport: throw new ArgumentException($"{this._Parameter.KernelType} is not supported."); } this.NativePtr = ret; }
public SvmPegasos() { var kernelType = typeof(TKernel).GetGenericTypeDefinition(); if (!KernelTypesRepository.KernelTypes.TryGetValue(kernelType, out var svmKernelType)) { throw new ArgumentException(); } var elementType = typeof(TKernel).GenericTypeArguments[0]; if (!KernelTypesRepository.ElementTypes.TryGetValue(elementType, out var sampleType)) { throw new ArgumentException(); } this._Parameter = new KernelBaseParameter(svmKernelType, sampleType, 0, 0); this._Bridge = CreateBridge(this._Parameter); var error = NativeMethods.svm_pegasos_new(this._Parameter.KernelType.ToNativeKernelType(), this._Parameter.SampleType.ToNativeMatrixElementType(), out var ret); switch (error) { case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{this._Parameter.SampleType} is not supported."); case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport: throw new ArgumentException($"{nameof(this._Parameter.TemplateColumns)} or {nameof(this._Parameter.TemplateRows)} is not supported."); case NativeMethods.ErrorType.SvmKernelNotSupport: throw new ArgumentException($"{this._Parameter.KernelType} is not supported."); } this.NativePtr = ret; }
public FloatImp(KernelBaseParameter parameter) : base(parameter) { }
protected Bridge(KernelBaseParameter parameter, NativeMethods.SvmBatchTrainerType trainerType) { this.Parameter = parameter; this.TrainerType = trainerType; }
public DoubleBridge(KernelBaseParameter parameter) : base(parameter) { }
public FloatBridge(KernelBaseParameter parameter) : base(parameter) { }
protected Bridge(KernelBaseParameter parameter) { this.Parameter = parameter; }
public DoubleBridge(KernelBaseParameter parameter, NativeMethods.SvmBatchTrainerType trainerType) : base(parameter, trainerType) { }
public static Matrix <double> CrossValidateTrainer <TScalar, TTrainer>(TTrainer trainer, IEnumerable <Matrix <TScalar> > x, IEnumerable <TScalar> y, int folds) where TScalar : struct where TTrainer : Trainer <TScalar> { if (trainer == null) { throw new ArgumentNullException(nameof(trainer)); } if (x == null) { throw new ArgumentNullException(nameof(x)); } if (y == null) { throw new ArgumentNullException(nameof(y)); } if (!x.Any()) { throw new ArgumentException(); } if (!y.Any()) { throw new ArgumentException(); } if (!(1 < folds)) { throw new ArgumentException(); } trainer.ThrowIfDisposed(); x.ThrowIfDisposed(); TrainerHelper.GetTypes <TScalar, TTrainer>(out var trainerType, out var svmTrainerType, out var svmKernelType, out var sampleType); var parameter = new KernelBaseParameter(svmKernelType, sampleType, 0, 0); using (var xVector = new StdVector <Matrix <TScalar> >(x)) using (var yVector = new StdVector <TScalar>(y)) { var err = NativeMethods.cross_validate_trainer_svm_trainer(svmKernelType.ToNativeKernelType(), sampleType.ToNativeMatrixElementType(), svmTrainerType, trainer.NativePtr, parameter.TemplateRows, parameter.TemplateColumns, xVector.NativePtr, yVector.NativePtr, folds, out var ret); switch (err) { case NativeMethods.ErrorType.SvmTrainerNotSupport: throw new ArgumentException($"{trainerType} is not supported."); case NativeMethods.ErrorType.SvmKernelNotSupport: throw new ArgumentException($"{svmKernelType} is not supported."); case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{sampleType} is not supported."); } return(new Matrix <double>(ret, 1, 2)); } }
public static ProbabilisticDecisionFunction <TScalar, TKernel> TrainProbabilisticDecisionFunction <TScalar, TKernel, TTrainer>(ReducedDecisionFunctionTrainer2 <TScalar, TKernel, TTrainer> trainer, IEnumerable <Matrix <TScalar> > x, IEnumerable <TScalar> y, int folds) where TScalar : struct where TKernel : KernelBase where TTrainer : Trainer <TScalar> { if (trainer == null) { throw new ArgumentNullException(nameof(trainer)); } if (x == null) { throw new ArgumentNullException(nameof(x)); } if (y == null) { throw new ArgumentNullException(nameof(y)); } if (!x.Any()) { throw new ArgumentException(); } if (!y.Any()) { throw new ArgumentException(); } if (!(1 < folds)) { throw new ArgumentException(); } trainer.ThrowIfDisposed(); x.ThrowIfDisposed(); TrainerHelper.GetTypes <TScalar, TTrainer>(out var trainerType, out var svmTrainerType, out var svmKernelType, out var sampleType); var parameter = new KernelBaseParameter(svmKernelType, sampleType, 0, 0); using (var xVector = new StdVector <Matrix <TScalar> >(x)) using (var yVector = new StdVector <TScalar>(y)) { var err = NativeMethods.train_probabilistic_decision_function_reduced_decision_function_trainer2(svmKernelType.ToNativeKernelType(), sampleType.ToNativeMatrixElementType(), svmTrainerType, trainer.NativePtr, parameter.TemplateRows, parameter.TemplateColumns, xVector.NativePtr, yVector.NativePtr, folds, out var ret); switch (err) { case NativeMethods.ErrorType.SvmTrainerNotSupport: throw new ArgumentException($"{trainerType} is not supported."); case NativeMethods.ErrorType.SvmKernelNotSupport: throw new ArgumentException($"{svmKernelType} is not supported."); case NativeMethods.ErrorType.MatrixElementTypeNotSupport: throw new ArgumentException($"{sampleType} is not supported."); case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport: throw new ArgumentException($"{nameof(parameter.TemplateColumns)} or {nameof(parameter.TemplateRows)} is not supported."); } return(new ProbabilisticDecisionFunction <TScalar, TKernel>(ret, parameter)); } }
protected Imp(KernelBaseParameter parameter) { this.Parameter = parameter; }
public DoubleImp(KernelBaseParameter parameter) : base(parameter) { }