deepfaceleb替代参数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了deepfaceleb替代参数相关的知识,希望对你有一定的参考价值。
参考技术A 第一次运行时,会让你输入各个参数值,输入方法如下:[0] Which GPU indexes to choose? :请输入GPU序号
问句前的[0]表示你直接按回车后,会使用的默认值你可以自行输入值,值的可选项通常在问句中会给出
如果不了解这个选项的作用,输入英文问号?按回车,会给出详细说明
当模型创建后,第N次执行训练脚本时,选择好模型和GPU后,短暂停顿后会有一行提示“Press enter in 2 seconds to override model settings”“请在2秒内按enter回车以重新修改模型参数设置”就是字面意思。如果2秒内没按enter,就会沿用上一次训练时的参数进行训练
编组变量参数 - __arglist 或替代
【中文标题】编组变量参数 - __arglist 或替代【英文标题】:marshaling variable arguments - __arglist or alternative 【发布时间】:2014-10-10 17:25:33 【问题描述】:描述我正在尝试解决的问题的最佳方式是用代码说话。我在这个论坛上看到很多 __arglist 问题,但没有很多有用的答案。我知道应该避免使用 _arglist,所以我对替代方法持开放态度
在一个 C++ 模块中,我有如下内容
void SomeFunction(LPCWSTR pszFormat, va_args args)
// this function is not exported...
// it is designed to take a format specifier and a list of variable
// arguments and "printf" it into a buffer. This code
// allocates buffer and uses _vsnwprintf_s to format the
// string.
// I really do not have much flexibility to rewrite this function
// so please steer away from scrutinizing this. it is what is
// and I need to call it from C#.
::_vsnwprintf_s(somebuff, buffsize, _TRUNCATE, pszFormat, args)
__declspec(dllexport) void __cdecl ExportedSomeFunction(LPCWSTR pszFormat, ...)
// the purpose of this method is to export SomeFunction to C# code.
// it handles any marshaling. I can change this however it makes sense
va_list args ;
va_start(args, pszFormat) ;
SomeFunction(pszFormat, args) ;
va_end(args) ;
在另一个 C# 模块中,我有处理所有编组到 C++ DLL 的代码。 目的是从用户代码中隐藏原生 API 和编组的所有复杂性。 最终目标是 C++ 开发人员或 C# 开发人员进行相同的 API 调用,但代码编写一次并导出到两者
[DllImport("mymodule.dll", CallingConvention=CallingConvention.Cdecl)]
private static extern void ExportedSomeFunction(
[MarshalAs(UnmanagedType.LPWStr)] out string strPath,
/* ? what goes here ? */);
void SomeFunction(string strFormat, /*? what goes here ?*/ )
// handles marshalling incoming data to what ever is needed by exported C function
ExportedSomeFunction(strFormat, /*? something ?*/ ) ;
那么其他模块中的用户代码应该是这样的......
SomeFunction("my format: %ld, %s", 5, "Some Useless string") ;
这很理想,但我准备好接受了
SomeFunction("my format: %ld, %s", __arglist(5, "Some Useless string")) ;
我不在乎数据是如何编组的。如果我使用 __arglist 或某个数组,我不在乎,只要我最终得到一个 va_args
__arglist貌似解决了,可以成功调用
ExportedSomeFunction(strFormat, __arglist(5, "Some Useless string")) ;
但我不知道如何使用可变参数调用 C# SomeFunction 并将 __arglist 传递给导出的函数。
SomeFunction("my format: %ld, %s", __arglist(5, "Some Useless string")) ;
我无法让它工作....
[DllImport("mymodule.dll", CallingConvention=CallingConvention.Cdecl)]
private static extern void ExportedSomeFunction(
[MarshalAs(UnmanagedType.LPWStr)] out string strPath,
__arglist);
void SomeFunction(string strFormat, __arglist )
ExportedSomeFunction(strFormat, __arglist) ; // error cannot convert from RuntimeArgumentHandle to __arglist
这会编译,但不会产生所需的结果。在 C++ 中接收到的参数列表是错误的。
private static extern void ExportedSomeFunction(
[MarshalAs(UnmanagedType.LPWStr)] out string strPath,
RuntimeArgumentHandle args);
【问题讨论】:
理智的方法是编写方法的重载,每个都使用 __arglist() 来传递特定的参数。你应该在你有几个的时候完成。 【参考方案1】:这是我如何解决这个问题的建议。看看varargs.h
,它是 VisualStudio 的一部分。这让您对va_list
的含义有所了解。你可以看到:typedef char * va_list;
。这只是一个指针。
不仅__arglist
无证,I don't think it works correctly on 64-bit processes.
您需要在 C# 端动态构建va_list
。我相信,这是比无证 __arglist
更好的解决方案,而且它似乎运行良好。对于 C#,您想使用 params[]
,而在 C++ 接收端,则使用 va_list
。每个可变参数函数都应该有以v...
开头的函数,例如vsprintf
,接收va_list
,而不是摆弄堆栈中的参数。
将此美丽复制/粘贴到您的解决方案中:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
// Author: Chris Eelmaa
namespace ConsoleApplication1
#region VariableCombiner
class CombinedVariables : IDisposable
readonly IntPtr _ptr;
readonly IList<IDisposable> _disposables;
bool _disposed;
public CombinedVariables(VariableArgument[] args)
_disposables = new List<IDisposable>();
_ptr = Marshal.AllocHGlobal(args.Sum(arg => arg.GetSize()));
var curPtr = _ptr;
foreach (var arg in args)
_disposables.Add(arg.Write(curPtr));
curPtr += arg.GetSize();
public IntPtr GetPtr()
if(_disposed)
throw new InvalidOperationException("Disposed already.");
return _ptr;
public void Dispose()
if (!_disposed)
_disposed = true;
foreach (var disposable in _disposables)
disposable.Dispose();
Marshal.FreeHGlobal(_ptr);
#endregion
#region VariableArgument
abstract class VariableArgument
#region SentinelDispose
protected static readonly IDisposable SentinelDisposable =
new SentinelDispose();
class SentinelDispose : IDisposable
public void Dispose()
#endregion
public abstract IDisposable Write(IntPtr buffer);
public virtual int GetSize()
return IntPtr.Size;
public static implicit operator VariableArgument(int input)
return new VariableIntegerArgument(input);
public static implicit operator VariableArgument(string input)
return new VariableStringArgument(input);
public static implicit operator VariableArgument(double input)
return new VariableDoubleArgument(input);
#endregion
#region VariableIntegerArgument
sealed class VariableIntegerArgument : VariableArgument
readonly int _value;
public VariableIntegerArgument(int value)
_value = value;
public override IDisposable Write(IntPtr buffer)
Marshal.Copy(new[] _value , 0, buffer, 1);
return SentinelDisposable;
#endregion
#region VariableDoubleArgument
sealed class VariableDoubleArgument : VariableArgument
readonly double _value;
public VariableDoubleArgument(double value)
_value = value;
public override int GetSize()
return 8;
public override IDisposable Write(IntPtr buffer)
Marshal.Copy(new[] _value , 0, buffer, 1);
return SentinelDisposable;
#endregion
#region VariableStringArgument
sealed class VariableStringArgument : VariableArgument
readonly string _value;
public VariableStringArgument(string value)
_value = value;
public override IDisposable Write(IntPtr buffer)
var ptr = Marshal.StringToHGlobalAnsi(_value);
Marshal.Copy(new[] ptr, 0, buffer, 1);
return new StringArgumentDisposable(ptr);
#region StringArgumentDisposable
class StringArgumentDisposable : IDisposable
IntPtr _ptr;
public StringArgumentDisposable(IntPtr ptr)
_ptr = ptr;
public void Dispose()
if (_ptr != IntPtr.Zero)
Marshal.FreeHGlobal(_ptr);
_ptr = IntPtr.Zero;
#endregion
#endregion
及用法示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace ConsoleApplication1
class Program
static void Main(string[] args)
Console.WriteLine(
AmazingSPrintf("I am %s, %d years old, %f meters tall!",
"Chris",
24,
1.94));
static string AmazingSPrintf(string format, params VariableArgument[] args)
if (!args.Any())
return format;
using (var combinedVariables = new CombinedVariables(args))
var bufferCapacity = _vscprintf(format, combinedVariables.GetPtr());
var stringBuilder = new StringBuilder(bufferCapacity + 1);
vsprintf(stringBuilder, format, combinedVariables.GetPtr());
return stringBuilder.ToString();
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int vsprintf(
StringBuilder buffer,
string format,
IntPtr ptr);
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int _vscprintf(
string format,
IntPtr ptr);
CombinedVariables
类用于构建va_list
,然后您可以将其传递给您的C++ 方法void SomeFunction(LPCWSTR pszFormat, va_list args)
。
您需要注意VariableStringArgument
,因为它目前适用于 ANSI。您可能正在寻找Marshal.StringToHGlobalUni
。
【讨论】:
这是我倾向于的一种方法,但希望有一个不太复杂的解决方案。也许我错过了一些明显的东西。但是这种方法的优点是验证参数是格式指定将要理解的东西。感谢您的提示。【参考方案2】:请注意,va_list
和 ...
之间存在细微差别。 printf
使用 ...
,而 vprintf
使用 va_list
。 va_list
通常是指向...
的第一个元素的指针。 __arglist
代表...
。
va_list
可以使用@Erti的代码,或者我的代码:
public class VaList : IDisposable
protected readonly List<GCHandle> handles = new List<GCHandle>();
public VaList(bool unicode, params object[] args)
if (args == null)
throw new ArgumentNullException("args");
// The first handle is for the bytes array
handles.Add(default(GCHandle));
int total = 0;
var bytes = new PrimitiveToBytes[args.Length];
for (int i = 0; i < args.Length; i++)
int size = Convert(unicode, args[i], ref bytes[i]);
bytes[i].Size = size;
total += size;
// Instead of a byte[] we use a IntPtr[], so copying elements
// inside is faster (perhaps :-) )
var buffer = new IntPtr[total / IntPtr.Size];
handles[0] = GCHandle.Alloc(buffer, GCHandleType.Pinned);
for (int i = 0, j = 0; i < args.Length; i++)
buffer[j++] = bytes[i].IntPtr;
// long or double with IntPtr == 4
if (bytes[i].Size > IntPtr.Size)
buffer[j++] = (IntPtr)bytes[i].Int32High;
// Overload this to handle other types
protected virtual int Convert(bool unicode, object arg, ref PrimitiveToBytes primitiveToBytes)
int size;
if (arg == null)
primitiveToBytes.IntPtr = IntPtr.Zero;
size = IntPtr.Size;
else
Type type = arg.GetType();
TypeCode typeHandle = Type.GetTypeCode(type);
switch (typeHandle)
case TypeCode.Boolean:
// Boolean converted to Int32
primitiveToBytes.Int32 = (bool)arg ? 1 : 0;
size = IntPtr.Size;
break;
case TypeCode.SByte:
primitiveToBytes.SByte = (sbyte)arg;
size = IntPtr.Size;
break;
case TypeCode.Byte:
primitiveToBytes.Byte = (byte)arg;
size = IntPtr.Size;
break;
case TypeCode.Int16:
primitiveToBytes.Int16 = (short)arg;
size = IntPtr.Size;
break;
case TypeCode.UInt16:
primitiveToBytes.UInt16 = (ushort)arg;
size = IntPtr.Size;
break;
case TypeCode.Int32:
primitiveToBytes.Int32 = (int)arg;
size = IntPtr.Size;
break;
case TypeCode.UInt32:
primitiveToBytes.UInt32 = (uint)arg;
size = IntPtr.Size;
break;
case TypeCode.Int64:
primitiveToBytes.Int64 = (long)arg;
size = sizeof(long);
break;
case TypeCode.UInt64:
primitiveToBytes.UInt64 = (ulong)arg;
size = sizeof(ulong);
break;
case TypeCode.Single:
// Single converted to Double
primitiveToBytes.Double = (double)(float)arg;
size = sizeof(double);
break;
case TypeCode.Double:
primitiveToBytes.Double = (double)arg;
size = sizeof(double);
break;
case TypeCode.Char:
if (unicode)
primitiveToBytes.UInt16 = (char)arg;
else
byte[] bytes = Encoding.Default.GetBytes(new[] (char)arg );
if (bytes.Length > 0)
primitiveToBytes.B0 = bytes[0];
if (bytes.Length > 1)
primitiveToBytes.B1 = bytes[1];
if (bytes.Length > 2)
primitiveToBytes.B2 = bytes[2];
if (bytes.Length > 3)
primitiveToBytes.B3 = bytes[3];
size = IntPtr.Size;
break;
case TypeCode.String:
string str = (string)arg;
GCHandle handle;
if (unicode)
handle = GCHandle.Alloc(str, GCHandleType.Pinned);
else
byte[] bytes = new byte[Encoding.Default.GetByteCount(str) + 1];
Encoding.Default.GetBytes(str, 0, str.Length, bytes, 0);
handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
handles.Add(handle);
primitiveToBytes.IntPtr = handle.AddrOfPinnedObject();
size = IntPtr.Size;
break;
case TypeCode.Object:
if (type == typeof(IntPtr))
primitiveToBytes.IntPtr = (IntPtr)arg;
size = IntPtr.Size;
else if (type == typeof(UIntPtr))
primitiveToBytes.UIntPtr = (UIntPtr)arg;
size = UIntPtr.Size;
else if (!type.IsValueType)
GCHandle handle = GCHandle.Alloc(arg, GCHandleType.Pinned);
primitiveToBytes.IntPtr = handle.AddrOfPinnedObject();
size = IntPtr.Size;
else
throw new NotSupportedException();
break;
default:
throw new NotSupportedException();
return size;
~VaList()
Dispose(false);
public void Dispose()
Dispose(true);
protected virtual void Dispose(bool disposing)
for (int i = 0; i < handles.Count; i++)
if (handles[i].IsAllocated)
handles[i].Free();
handles.Clear();
public IntPtr AddrOfPinnedObject()
if (handles.Count == 0)
throw new ObjectDisposedException(GetType().Name);
return handles[0].AddrOfPinnedObject();
[StructLayout(LayoutKind.Explicit)]
protected struct PrimitiveToBytes
[FieldOffset(0)]
public byte B0;
[FieldOffset(1)]
public byte B1;
[FieldOffset(2)]
public byte B2;
[FieldOffset(3)]
public byte B3;
[FieldOffset(4)]
public byte B4;
[FieldOffset(5)]
public byte B5;
[FieldOffset(6)]
public byte B6;
[FieldOffset(7)]
public byte B7;
[FieldOffset(4)]
public int Int32High;
[FieldOffset(0)]
public byte Byte;
[FieldOffset(0)]
public sbyte SByte;
[FieldOffset(0)]
public short Int16;
[FieldOffset(0)]
public ushort UInt16;
[FieldOffset(0)]
public int Int32;
[FieldOffset(0)]
public uint UInt32;
[FieldOffset(0)]
public long Int64;
[FieldOffset(0)]
public ulong UInt64;
[FieldOffset(0)]
public float Single;
[FieldOffset(0)]
public double Double;
[FieldOffset(0)]
public IntPtr IntPtr;
[FieldOffset(0)]
public UIntPtr UIntPtr;
[FieldOffset(8)]
public int Size;
使用示例:
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int vprintf(string format, IntPtr ptr);
和
using (var list = new VaList(false, // Ansi encoding
true, // bool test
short.MinValue + 1, int.MinValue + 2, long.MinValue + 3, // signed
ushort.MaxValue - 4, uint.MaxValue - 5, ulong.MaxValue - 6, // unsigned
float.MaxValue, double.MaxValue, // float/double
'A', "Foo", Encoding.Default.GetBytes("Bar\0"), null, // char/string
IntPtr.Size == sizeof(int) ? (IntPtr)(int.MinValue + 7) : (IntPtr)(long.MinValue + 7), // signed ptr
UIntPtr.Size == sizeof(uint) ? (UIntPtr)(uint.MaxValue - 8) : (UIntPtr)(ulong.MaxValue - 8))) // unsigned ptr
vprintf("%d\n %hd\n %d\n %lld\n %hu\n %u\n %llu\n %f\n %f\n %c\n %s\n %s\n %s\n %p\n %p\n", list.AddrOfPinnedObject());
请注意,此代码仅与适用于 Intel x86/x64 的 Visual C++ 兼容。 ARM 使用另一种格式,而 GCC 仍然使用其他格式。
【讨论】:
以上是关于deepfaceleb替代参数的主要内容,如果未能解决你的问题,请参考以下文章
[python][deepface][原创]使用deepface进行年龄预测
[python][deepface][原创]使用deepface进行种族识别
[python][deepface][原创]使用deepface进行性别预测
[python][deepface][原创]使用deepface进行表情识别