Пример #1
0
        public static Model CreateV4Trainable(int inputSize, int classCount, ReadOnlySpan <int> strides)
        {
            if (inputSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(inputSize));
            }
            if (classCount <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(classCount));
            }

            Tensor input       = tf.keras.Input(new TensorShape(inputSize, inputSize, 3), name: "image");
            var    featureMaps = YOLOv4.Apply(input, classCount: classCount);

            var anchors = tf.constant(YOLOv4.Anchors);

            var bboxTensors = new PythonList <Tensor>();

            foreach (var(scaleIndex, featureMap) in Tools.Enumerate(featureMaps.SSBox, featureMaps.MBBox, featureMaps.LBBox))
            {
                int featuresOutputSize = (inputSize / 8) >> scaleIndex;
                var bbox = DecodeTrain(featureMap, classCount: classCount,
                                       outputSize: featuresOutputSize,
                                       anchors: anchors, strides: strides,
                                       scaleIndex: scaleIndex, xyScale: YOLOv4.XYScale);
                bboxTensors.Add(featureMap);
                bboxTensors.Add(bbox);
            }
            return(new Model(new { inputs = input, outputs = bboxTensors }.AsKwArgs()));
        }
Пример #2
0
                private void ParseSaveField()
                {
                    string field = _field.ToString();

                    if (_is_numeric_field)
                    {
                        _is_numeric_field = false;

                        double tmp;
                        if (double.TryParse(field, out tmp))
                        {
                            if (field.Contains("."))
                            {
                                _fields.Add(tmp);
                            }
                            else
                            {
                                _fields.Add((int)tmp);
                            }
                        }
                        else
                        {
                            throw PythonOps.ValueError(
                                      "invalid literal for float(): {0}", field);
                        }
                    }
                    else
                    {
                        _fields.Add(field);
                    }

                    _field.Clear();
                }
Пример #3
0
        /// <summary>
        /// Deal with dynamic shape in tensorflow cleanly.
        /// </summary>
        static PythonList <dynamic> ShapeList(ITensor tensor)
        {
            IEnumerable <int?> @static = tensor.shape.as_list();
            dynamic            dynamic = tf.shape(tensor);
            var result = new PythonList <dynamic>();

            foreach (object size in @static.Select((size, index) => size ?? (object)dynamic[index]))
            {
                result.Add(size);
            }
            return(result);
        }
Пример #4
0
            public object fetchmany(CodeContext context, int size)
            {
                PythonList result = new PythonList();
                object     item   = fetchone(context);

                for (int i = 0; i < size && item != null; ++i, item = fetchone(context))
                {
                    result.Add(item);
                }

                return(result);
            }
Пример #5
0
            public object fetchall(CodeContext context)
            {
                PythonList result = new PythonList();
                object     item   = fetchone(context);

                while (item != null)
                {
                    result.Add(item);
                    item = fetchone(context);
                }

                return(result);
            }
Пример #6
0
        public static PythonList enum_certificates(string store_name)
        {
            X509Store store = null;

            try {
                store = new X509Store(store_name, StoreLocation.LocalMachine);
                store.Open(OpenFlags.ReadOnly);
                var result = new PythonList();

                foreach (var cert in store.Certificates)
                {
                    string format = cert.GetFormat();

                    switch (format)
                    {
                    case "X509":
                        format = "x509_asn";
                        break;

                    default:
                        format = "unknown";
                        break;
                    }

                    var  set   = new SetCollection();
                    bool found = false;
                    foreach (var ext in cert.Extensions)
                    {
                        var keyUsage = ext as X509EnhancedKeyUsageExtension;
                        if (keyUsage != null)
                        {
                            foreach (var oid in keyUsage.EnhancedKeyUsages)
                            {
                                set.add(oid.Value);
                            }
                            found = true;
                            break;
                        }
                    }

                    result.Add(PythonTuple.MakeTuple(new Bytes(cert.RawData.ToList()), format, found ? set : ScriptingRuntimeHelpers.True));
                }

                return(result);
            } catch {
            } finally {
                store?.Close();
            }
            return(new PythonList());
        }
Пример #7
0
        public static PythonList <T> ToPyList <T>(this IEnumerable <T> enumerable)
        {
            if (enumerable is null)
            {
                throw new ArgumentNullException(nameof(enumerable));
            }
            var result = new PythonList <T>();

            foreach (var item in enumerable)
            {
                result.Add(item);
            }
            return(result);
        }
Пример #8
0
        public static PythonList getpwall()
        {
            var res = new PythonList();

            setpwent();
            IntPtr val = getpwent();

            while (val != IntPtr.Zero)
            {
                res.Add(Make(val));
                val = getpwent();
            }

            return(res);
        }
Пример #9
0
        public static Model CreateV4EvalOnly(int inputSize, int classCount)
        {
            if (inputSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(inputSize));
            }
            if (classCount <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(classCount));
            }

            Tensor input       = tf.keras.Input(new TensorShape(inputSize, inputSize, 3));
            var    featureMaps = YOLOv4.Apply(input, classCount: classCount);

            var bboxTensors = new PythonList <Tensor>();

            foreach (var featureMap in new[] { featureMaps.SSBox, featureMaps.MBBox, featureMaps.LBBox })
            {
                var bbox = DecodeEval(featureMap, classCount: classCount);
                bboxTensors.Add(bbox);
            }
            return(new Model(new { inputs = input, outputs = bboxTensors }.AsKwArgs()));
        }
        static (dynamic, dynamic) GenerateTestValues()
        {
            double Fun(double input) => Math.Sin(input);

            var inputs  = new PythonList <PythonList <double> >();
            var outputs = new PythonList <PythonList <double> >();

            var random = new Random();

            foreach (var _ in Enumerable.Range(0, testSize))
            {
                double x = x0 + (x1 - x0) * random.NextDouble();
                double y = Fun(x);
                inputs.Add(new PythonList <double> {
                    x
                });
                outputs.Add(new PythonList <double> {
                    y
                });
            }

            return(inputs, outputs);
        }
Пример #11
0
 internal Set(SetExpression setExpression)
     : this()
 {
     _elts = new PythonList(setExpression.Items.Count);
     foreach (AstExpression item in setExpression.Items) {
         _elts.Add(Convert(item));
     }
 }
Пример #12
0
            internal Call(CallExpression call)
                : this()
            {
                _args = PythonOps.MakeEmptyList(call.Args.Count);
                _keywords = new PythonList();
                _func = Convert(call.Target);
                foreach (Arg arg in call.Args) {

                    if (arg.Name == null)
                        _args.Add(Convert(arg.Expression));
                    else if (arg.Name == "*")
                        _starargs = Convert(arg.Expression);
                    else if (arg.Name == "**")
                        _kwargs = Convert(arg.Expression);
                    else
                        _keywords.Add(new keyword(arg));
                }
            }
Пример #13
0
 internal ClassDef(ClassDefinition def)
     : this()
 {
     _name = def.Name;
     _bases = PythonOps.MakeEmptyList(def.Bases.Count);
     foreach (AstExpression expr in def.Bases)
         _bases.Add(Convert(expr));
     _body = ConvertStatements(def.Body);
     if (def.Decorators != null) {
         _decorator_list = PythonOps.MakeEmptyList(def.Decorators.Count);
         foreach (AstExpression expr in def.Decorators)
             _decorator_list.Add(Convert(expr));
     } else
         _decorator_list = PythonOps.MakeEmptyList(0);
 }
Пример #14
0
 internal Compare(BinaryExpression expr)
     : this()
 {
     _left = Convert(expr.Left);
     _ops = PythonOps.MakeList();
     _comparators = PythonOps.MakeList();
     while (BinaryExpression.IsComparison(expr.Right)) {
         BinaryExpression right = (BinaryExpression)expr.Right;
         // start accumulating ops and comparators
         _ops.Add(Convert(expr.Operator));
         _comparators.Add(Convert(right.Left));
         expr = right;
     }
     _ops.Add(Convert(expr.Operator));
     _comparators.Add(Convert(expr.Right));
 }
Пример #15
0
 internal comprehension(ComprehensionFor listFor, ComprehensionIf[] listIfs)
     : this()
 {
     _target = Convert(listFor.Left, Store.Instance);
     _iter = Convert(listFor.List);
     _ifs = PythonOps.MakeEmptyList(listIfs.Length);
     foreach (ComprehensionIf listIf in listIfs)
         _ifs.Add(Convert(listIf.Test));
 }
Пример #16
0
            internal static PythonList Convert(ComprehensionIterator[] iters)
            {
                Generic.List<ComprehensionFor> cfCollector =
                    new Generic.List<ComprehensionFor>();
                Generic.List<Generic.List<ComprehensionIf>> cifCollector =
                    new Generic.List<Generic.List<ComprehensionIf>>();
                Generic.List<ComprehensionIf> cif = null;
                for (int i = 0; i < iters.Length; i++) {
                    if (iters[i] is ComprehensionFor) {
                        ComprehensionFor cf = (ComprehensionFor)iters[i];
                        cfCollector.Add(cf);
                        cif = new Generic.List<ComprehensionIf>();
                        cifCollector.Add(cif);
                    } else {
                        ComprehensionIf ci = (ComprehensionIf)iters[i];
                        cif.Add(ci);
                    }
                }

                PythonList comps = new PythonList();
                for (int i = 0; i < cfCollector.Count; i++)
                    comps.Add(new comprehension(cfCollector[i], cifCollector[i].ToArray()));
                return comps;
            }
Пример #17
0
            internal Tuple(TupleExpression list, expr_context ctx)
                : this()
            {
                _elts = PythonOps.MakeEmptyList(list.Items.Count);
                foreach (AstExpression expr in list.Items)
                    _elts.Add(Convert(expr, ctx));

                _ctx = ctx;
            }
Пример #18
0
            internal static PythonList Convert(IList<ComprehensionIterator> iterators)
            {
                ComprehensionIterator[] iters = new ComprehensionIterator[iterators.Count];
                iterators.CopyTo(iters, 0);

                PythonList comps = new PythonList();
                int start = 1;
                for (int i = 0; i < iters.Length; i++) {
                    if (i == 0 || iters[i] is ComprehensionIf)
                        if (i == iters.Length - 1)
                            i++;
                        else
                            continue;

                    ComprehensionIf[] ifs = new ComprehensionIf[i - start];
                    Array.Copy(iters, start, ifs, 0, ifs.Length);
                    comps.Add(new comprehension((ComprehensionFor)iters[start - 1], ifs));
                    start = i + 1;
                }
                return comps;
            }
Пример #19
0
            internal TryExcept(TryStatement stmt)
                : this()
            {
                _body = ConvertStatements(stmt.Body);

                _handlers = PythonOps.MakeEmptyList(stmt.Handlers.Count);
                foreach (TryStatementHandler tryStmt in stmt.Handlers)
                    _handlers.Add(Convert(tryStmt));

                _orelse = ConvertStatements(stmt.Else, true);
            }
Пример #20
0
 internal Delete(DelStatement stmt)
     : this()
 {
     _targets = PythonOps.MakeEmptyList(stmt.Expressions.Count);
     foreach (AstExpression expr in stmt.Expressions)
         _targets.Add(Convert(expr, Del.Instance));
 }
Пример #21
0
 internal Dict(DictionaryExpression expr)
     : this()
 {
     _keys = PythonOps.MakeEmptyList(expr.Items.Count);
     _values = PythonOps.MakeEmptyList(expr.Items.Count);
     foreach (SliceExpression item in expr.Items) {
         _keys.Add(Convert(item.SliceStart));
         _values.Add(Convert(item.SliceStop));
     }
 }
Пример #22
0
            internal Print(PrintStatement stmt)
                : this()
            {
                if (stmt.Destination != null)
                    _dest = Convert(stmt.Destination);

                _values = PythonOps.MakeEmptyList(stmt.Expressions.Count);
                foreach (AstExpression expr in stmt.Expressions)
                    _values.Add(Convert(expr));

                _nl = !stmt.TrailingComma;
            }
Пример #23
0
            internal FunctionDef(FunctionDefinition def)
                : this()
            {
                _name = def.Name;
                _args = new arguments(def.Parameters);
                _body = ConvertStatements(def.Body);

                if (def.Decorators != null) {
                    _decorators = PythonOps.MakeEmptyList(def.Decorators.Count);
                    foreach (AstExpression expr in def.Decorators)
                        _decorators.Add(Convert(expr));
                } else
                    _decorators = PythonOps.MakeEmptyList(0);
            }
Пример #24
0
        private static void QueryValueExImpl(SafeRegistryHandle handle, string valueName, out int valueKind, out object value)
        {
            valueName = valueName ?? ""; // it looks like RegQueryValueEx can fail with null, use empty string instead
            valueKind = 0;
            int dwRet;

            byte[] data   = new byte[128];
            uint   length = (uint)data.Length;

            // query the size first, reading the data as we query...
            dwRet = RegQueryValueEx(handle, valueName, IntPtr.Zero, out valueKind, data, ref length);
            while (dwRet == ERROR_MORE_DATA)
            {
                data   = new byte[data.Length * 2];
                length = (uint)data.Length;
                dwRet  = RegQueryValueEx(handle, valueName, IntPtr.Zero, out valueKind, data, ref length);
            }

            if (dwRet == PythonExceptions._OSError.ERROR_FILE_NOT_FOUND)
            {
                throw PythonExceptions.CreateThrowable(PythonExceptions.OSError, PythonExceptions._OSError.ERROR_FILE_NOT_FOUND, "The system cannot find the file specified", null, PythonExceptions._OSError.ERROR_FILE_NOT_FOUND);
            }
            if (dwRet != ERROR_SUCCESS)
            {
                throw PythonExceptions.CreateThrowable(PythonExceptions.OSError, dwRet);
            }

            // convert the result into a Python object

            switch (valueKind)
            {
            case REG_MULTI_SZ:
                PythonList list     = new PythonList();
                int        curIndex = 0;
                while (curIndex < length)
                {
                    for (int dataIndex = curIndex; dataIndex < length; dataIndex += 2)
                    {
                        if (data[dataIndex] == 0 && data[dataIndex + 1] == 0)
                        {
                            // got a full string
                            list.Add(ExtractString(data, curIndex, dataIndex));
                            curIndex = dataIndex + 2;

                            if (curIndex + 2 <= length && data[curIndex] == 0 && data[curIndex + 1] == 0)
                            {
                                // double null terminated
                                curIndex = data.Length;
                                break;
                            }
                        }
                    }

                    if (curIndex != data.Length)
                    {
                        // not null terminated
                        list.Add(ExtractString(data, curIndex, data.Length));
                    }
                }
                value = list;
                break;

            case REG_BINARY:
                var tight_fit_data = new byte[length];
                for (int i = 0; i < length; i++)
                {
                    tight_fit_data[i] = data[i];
                }
                value = length == 0 ? null : new Bytes(tight_fit_data);
                break;

            case REG_EXPAND_SZ:
            case REG_SZ:
                if (length >= 2 && data[length - 1] == 0 && data[length - 2] == 0)
                {
                    value = ExtractString(data, 0, (int)length - 2);
                }
                else
                {
                    value = ExtractString(data, 0, (int)length);
                }
                break;

            case REG_DWORD:
                if (BitConverter.IsLittleEndian)
                {
                    value = (uint)((data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0]);
                }
                else
                {
                    value = (uint)((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]);
                }
                break;

            default:
                value = null;
                break;
            }
        }
Пример #25
0
            internal Assign(AssignmentStatement stmt)
                : this()
            {
                _targets = PythonOps.MakeEmptyList(stmt.Left.Count);
                foreach (AstExpression expr in stmt.Left)
                    _targets.Add(Convert(expr, Store.Instance));

                _value = Convert(stmt.Right);
            }