public static INeuralNetworkImage Restore(FileStream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            // 1: read version: 2-bytes
            var buffer = new byte[2];

            stream.Read(buffer, 0, buffer.Length);
            var signal = BitConverter.ToUInt16(buffer, 0);

            if (signal != SectionType.SECTION_START_SIGNAL)
            {
                throw new Exception($"Invalid section start signal ({signal}).");
            }

            stream.Read(buffer, 0, 1);
            stream.Seek(-3, SeekOrigin.Current);

            return(buffer[0] switch
            {
                NeuralNetworkSerializer.SECTION_TYPE => NeuralNetworkSerializer.Restore(stream),
                NeuralNetworkLineSerializer.SECTION_TYPE => NeuralNetworkLineSerializer.Restore(stream),
                _ => throw new Exception($"Invalid neural network section type ({buffer[0]}).")
            });
Ejemplo n.º 2
0
        public static object Restore(string path, out byte[] extra)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            using var stream = File.OpenRead(path);

            // read file signature
            var    file_type_signature = SectionType.ReadSigniture(stream, Encoding.ASCII);
            object result;

            switch (file_type_signature)
            {
            case LearningProcessSerializer.FILE_TYPE_SIGNATURE_STRING:
                result = LearningProcessSerializer.Restore(stream);
                extra  = ReadExtra(stream);
                return(result);

            case NeuralNetworkSerializer.FILE_TYPE_SIGNATURE_STRING:
                result = NeuralNetworkSerializer.Restore(stream);
                extra  = ReadExtra(stream);
                return(result);

            case NeuralNetworkLineSerializer.FILE_TYPE_SIGNATURE_STRING:
                result = NeuralNetworkLineSerializer.Restore(stream);
                extra  = ReadExtra(stream);
                return(result);

            default:
                throw new Exception($"Invalid file type ({file_type_signature}).");
            }
        }