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]}).")
            });
예제 #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}).");
            }
        }
 public static void Serialize(FileStream stream, INeuralNetworkImage image)
 {
     if (image is NeuralNetworkImage cu_image)
     {
         NeuralNetworkSerializer.Serialize(stream, cu_image);
     }
     else if (image is NeuralNetworkLineImage cu_line_image)
     {
         NeuralNetworkLineSerializer.Serialize(stream, cu_line_image);
     }
     else
     {
         throw new Exception(
                   "The serializer for this type of INeuralNetworkImage is not registed.");
     }
 }
        public static void Serialize(FileStream stream, NeuralNetworkLineImage image)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            byte[] buffer;

            // new version signal
            buffer = BitConverter.GetBytes(SectionType.SECTION_START_SIGNAL); // 2-bytes
            stream.Write(buffer, 0, buffer.Length);

            // serialize section type
            buffer = new byte[] { SECTION_TYPE }; // 1-bytes
            stream.Write(buffer, 0, buffer.Length);

            // serialize version
            buffer = BitConverter.GetBytes(VERSION); // 2-bytes
            stream.Write(buffer, 0, buffer.Length);

            // serialize index
            buffer = BitConverter.GetBytes(image.index); // 4-bytes
            stream.Write(buffer, 0, buffer.Length);

            // functions serializer
            var function = new FunctionSerializerCore(stream);

            // serialize images count
            buffer = BitConverter.GetBytes(image.images.Length); // 4-bytes
            stream.Write(buffer, 0, buffer.Length);

            // serialize line
            NeuralNetworkSerializer.Serialize(stream, image.images[0]);
            for (int i = 0; i < image.combiners.Length;)
            {
                function.Serialize(image.combiners[i++]);
                NeuralNetworkSerializer.Serialize(stream, image.images[i]);
            }
        }