/// <summary>
        /// see https://github.com/EpicGames/UnrealEngine/blob/bf95c2cbc703123e08ab54e3ceccdd47e48d224a/Engine/Source/Runtime/Engine/Classes/Engine/EngineTypes.h#L3074
        /// </summary>
        public FRepMovement SerializeRepMovement(
            VectorQuantization locationQuantizationLevel  = VectorQuantization.RoundTwoDecimals,
            RotatorQuantization rotationQuantizationLevel = RotatorQuantization.ByteComponents,
            VectorQuantization velocityQuantizationLevel  = VectorQuantization.RoundWholeNumber)
        {
            var repMovement = new FRepMovement();
            var flags       = ReadBits(2);

            repMovement.bSimulatedPhysicSleep = flags[0];
            repMovement.bRepPhysics           = flags[1];

            repMovement.Location = SerializeQuantizedVector(locationQuantizationLevel);

            switch (rotationQuantizationLevel)
            {
            case RotatorQuantization.ByteComponents:
                repMovement.Rotation = ReadRotation();
                break;

            case RotatorQuantization.ShortComponents:
                repMovement.Rotation = ReadRotationShort();
                break;
            }

            repMovement.LinearVelocity = SerializeQuantizedVector(velocityQuantizationLevel);

            if (repMovement.bRepPhysics)
            {
                repMovement.AngularVelocity = SerializeQuantizedVector(velocityQuantizationLevel);
            }

            return(repMovement);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.WriteLine("TestVectorQuantization [input file] [output file] [VQ Size]");
                return;
            }

            int VQSize                 = int.Parse(args[2]);
            VectorQuantization vq      = new VectorQuantization();
            StreamReader       sr      = new StreamReader(args[0]);
            string             strLine = null;

            while ((strLine = sr.ReadLine()) != null)
            {
                float f = float.Parse(strLine);
                vq.Add(f);
            }
            sr.Close();

            double distortion = vq.BuildCodebook(VQSize);

            Logger.WriteLine("Average distortion: {0}", distortion);
            vq.WriteCodebook(args[1]);
        }
 /// <summary>
 /// see https://github.com/EpicGames/UnrealEngine/blob/bf95c2cbc703123e08ab54e3ceccdd47e48d224a/Engine/Source/Runtime/Engine/Classes/Engine/EngineTypes.h#L3047
 /// </summary>
 public FVector SerializePropertyQuantizedVector(VectorQuantization quantizationLevel = VectorQuantization.RoundWholeNumber)
 {
     return(quantizationLevel switch
     {
         VectorQuantization.RoundTwoDecimals => ReadPackedVector(100, 30),
         VectorQuantization.RoundOneDecimal => ReadPackedVector(10, 27),
         _ => ReadPackedVector(1, 24),
     });
 public RepMovementAttribute(
     VectorQuantization locationQuantizationLevel  = VectorQuantization.RoundTwoDecimals,
     RotatorQuantization rotationQuantizationLevel = RotatorQuantization.ByteComponents,
     VectorQuantization velocityQuantizationLevel  = VectorQuantization.RoundWholeNumber)
 {
     LocationQuantizationLevel = locationQuantizationLevel;
     VelocityQuantizationLevel = velocityQuantizationLevel;
     RotationQuantizationLevel = rotationQuantizationLevel;
 }
        public void RepMovementTest(byte[] rawData, int bitCount,
                                    VectorQuantization locationQuantizationLevel  = VectorQuantization.RoundTwoDecimals,
                                    RotatorQuantization rotationQuantizationLevel = RotatorQuantization.ByteComponents,
                                    VectorQuantization velocityQuantizationLevel  = VectorQuantization.RoundWholeNumber)
        {
            var reader = new NetBitReader(rawData, bitCount);

            reader.SerializeRepMovement(locationQuantizationLevel, rotationQuantizationLevel, velocityQuantizationLevel);
            Assert.False(reader.IsError);
            Assert.True(reader.AtEnd());
        }
Beispiel #6
0
        private FRepMovement AsFRepMovement(VectorQuantization vectorQuantization)
        {
            _reader.Reset();

            FRepMovement movement = _reader.SerializeRepMovement(vectorQuantization);

            if (_reader.IsError || !_reader.AtEnd())
            {
                return(null);
            }

            return(movement);
        }
Beispiel #7
0
        /// <summary>
        /// Save feature weights into file
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="bVQ"></param>
        /// <returns></returns>
        public void SaveFeatureWeight(string filename, bool bVQ)
        {
            var filename_alpha = filename + ".alpha";
            var tofs           = new StreamWriter(filename_alpha, false);
            var bw             = new BinaryWriter(tofs.BaseStream);

            if (bVQ == true)
            {
                Logger.WriteLine("Save feature weights into a VQ model: {0}", filename_alpha);
                //Build code book
                VectorQuantization vq = new VectorQuantization();
                for (long i = 1; i <= maxid_; i++)
                {
                    vq.Add(alpha_[i]);
                }

                int    vqSize     = 256;
                double distortion = vq.BuildCodebook(vqSize);
                Logger.WriteLine("Weight vector quantization distortion: {0}", distortion);

                //VQ size
                bw.Write(vqSize);

                //Save VQ codebook into file
                for (int j = 0; j < vqSize; j++)
                {
                    bw.Write(vq.CodeBook[j]);
                }

                //Save weights
                for (long i = 1; i <= maxid_; ++i)
                {
                    bw.Write((byte)vq.ComputeVQ(alpha_[i]));
                }
            }
            else
            {
                Logger.WriteLine("Save feature weights into a normal model: {0}", filename_alpha);

                bw.Write(0);
                //Save weights
                for (long i = 1; i <= maxid_; ++i)
                {
                    bw.Write((float)alpha_[i]);
                }
            }

            bw.Close();
        }
        /// <summary>
        /// see https://github.com/EpicGames/UnrealEngine/blob/bf95c2cbc703123e08ab54e3ceccdd47e48d224a/Engine/Source/Runtime/Engine/Classes/Engine/EngineTypes.h#L3074
        /// </summary>
        public FRepMovement SerializeRepMovement(
            VectorQuantization locationQuantizationLevel  = VectorQuantization.RoundTwoDecimals,
            RotatorQuantization rotationQuantizationLevel = RotatorQuantization.ByteComponents,
            VectorQuantization velocityQuantizationLevel  = VectorQuantization.RoundWholeNumber)
        {
            var repMovement = new FRepMovement
            {
                bSimulatedPhysicSleep = ReadBit(),
                bRepPhysics           = ReadBit(),

                Location = SerializePropertyQuantizedVector(locationQuantizationLevel),
                Rotation = rotationQuantizationLevel == RotatorQuantization.ByteComponents ? ReadRotation() : ReadRotationShort(),

                LinearVelocity = SerializePropertyQuantizedVector(velocityQuantizationLevel)
            };

            if (repMovement.bRepPhysics)
            {
                repMovement.AngularVelocity = SerializePropertyQuantizedVector(velocityQuantizationLevel);
            }

            return(repMovement);
        }
Beispiel #9
0
        static void Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.WriteLine("TestVectorQuantization [input file] [output file] [VQ Size]");
                return;
            }

            int VQSize = int.Parse(args[2]);
            VectorQuantization vq = new VectorQuantization();
            StreamReader sr = new StreamReader(args[0]);
            string strLine = null;

            while ((strLine = sr.ReadLine()) != null)
            {
                float f = float.Parse(strLine);
                vq.Add(f);
            }
            sr.Close();

            double distortion = vq.BuildCodebook(VQSize);
            Logger.WriteLine("Average distortion: {0}", distortion);
            vq.WriteCodebook(args[1]);
        }
Beispiel #10
0
        private void SaveLSTMWeights(Vector4[][] weight, BinaryWriter fo, bool bVQ = false)
        {
            var w      = weight.Length;
            var h      = weight[0].Length;
            var vqSize = 256;

            Logger.WriteLine("Saving LSTM weight matrix. width:{0}, height:{1}, vq:{2}", w, h, bVQ);

            fo.Write(weight.Length);
            fo.Write(weight[0].Length);

            if (bVQ == false)
            {
                fo.Write(0);

                for (var i = 0; i < w; i++)
                {
                    for (var j = 0; j < h; j++)
                    {
                        fo.Write(weight[i][j].X);
                        fo.Write(weight[i][j].Y);
                        fo.Write(weight[i][j].Z);
                        fo.Write(weight[i][j].W);
                    }
                }
            }
            else
            {
                //Build vector quantization model
                var vqInputCell       = new VectorQuantization();
                var vqInputForgetGate = new VectorQuantization();
                var vqInputInputGate  = new VectorQuantization();
                var vqInputOutputGate = new VectorQuantization();
                for (var i = 0; i < w; i++)
                {
                    for (var j = 0; j < h; j++)
                    {
                        vqInputInputGate.Add(weight[i][j].X);
                        vqInputForgetGate.Add(weight[i][j].Y);
                        vqInputCell.Add(weight[i][j].Z);
                        vqInputOutputGate.Add(weight[i][j].W);
                    }
                }

                var distortion = vqInputInputGate.BuildCodebook(vqSize);
                Logger.WriteLine("InputInputGate distortion: {0}", distortion);

                distortion = vqInputForgetGate.BuildCodebook(vqSize);
                Logger.WriteLine("InputForgetGate distortion: {0}", distortion);

                distortion = vqInputCell.BuildCodebook(vqSize);
                Logger.WriteLine("InputCell distortion: {0}", distortion);

                distortion = vqInputOutputGate.BuildCodebook(vqSize);
                Logger.WriteLine("InputOutputGate distortion: {0}", distortion);

                fo.Write(vqSize);

                //Save InputInputGate VQ codebook into file
                for (var j = 0; j < vqSize; j++)
                {
                    fo.Write(vqInputInputGate.CodeBook[j]);
                }

                //Save InputForgetGate VQ codebook into file
                for (var j = 0; j < vqSize; j++)
                {
                    fo.Write(vqInputForgetGate.CodeBook[j]);
                }

                //Save InputCell VQ codebook into file
                for (var j = 0; j < vqSize; j++)
                {
                    fo.Write(vqInputCell.CodeBook[j]);
                }

                //Save InputOutputGate VQ codebook into file
                for (var j = 0; j < vqSize; j++)
                {
                    fo.Write(vqInputOutputGate.CodeBook[j]);
                }

                for (var i = 0; i < w; i++)
                {
                    for (var j = 0; j < h; j++)
                    {
                        fo.Write((byte)vqInputInputGate.ComputeVQ(weight[i][j].X));
                        fo.Write((byte)vqInputForgetGate.ComputeVQ(weight[i][j].Y));
                        fo.Write((byte)vqInputCell.ComputeVQ(weight[i][j].Z));
                        fo.Write((byte)vqInputOutputGate.ComputeVQ(weight[i][j].W));
                    }
                }
            }
        }
Beispiel #11
0
        //Save matrix into file as binary format
        public static void SaveMatrix(Matrix <float> mat, BinaryWriter fo, bool bVQ = false)
        {
            //Save the width and height of the matrix
            fo.Write(mat.Width);
            fo.Write(mat.Height);

            if (bVQ == false)
            {
                Logger.WriteLine("Saving matrix without VQ...");
                fo.Write(0); // non-VQ

                //Save the data in matrix
                for (var r = 0; r < mat.Height; r++)
                {
                    for (var c = 0; c < mat.Width; c++)
                    {
                        fo.Write(mat[r][c]);
                    }
                }
            }
            else
            {
                //Build vector quantization matrix
                var vqSize = 256;
                var vq     = new VectorQuantization();
                Logger.WriteLine("Saving matrix with VQ {0}...", vqSize);

                var valSize = 0;
                for (var i = 0; i < mat.Height; i++)
                {
                    for (var j = 0; j < mat.Width; j++)
                    {
                        vq.Add(mat[i][j]);
                        valSize++;
                    }
                }

                if (vqSize > valSize)
                {
                    vqSize = valSize;
                }

                var distortion = vq.BuildCodebook(vqSize);
                Logger.WriteLine("Distortion: {0}, vqSize: {1}", distortion, vqSize);

                //Save VQ codebook into file
                fo.Write(vqSize);
                for (var j = 0; j < vqSize; j++)
                {
                    fo.Write(vq.CodeBook[j]);
                }

                //Save the data in matrix
                for (var r = 0; r < mat.Height; r++)
                {
                    for (var c = 0; c < mat.Width; c++)
                    {
                        fo.Write((byte)vq.ComputeVQ(mat[r][c]));
                    }
                }
            }
        }
Beispiel #12
0
        public bool BuildVQModel(string strFileName)
        {
            int vqSize = 256;

            if (entireTermList == null || entireTermList.Count == 0)
            {
                return(false);
            }

            StreamWriter fo = new StreamWriter(strFileName);
            BinaryWriter bw = new BinaryWriter(fo.BaseStream);

            // Save the word vectors
            bw.Write(entireTermList.Count); //Vocabulary size
            bw.Write(vectorSize);           //Vector size
            bw.Write(vqSize);               //VQ size

            Logger.WriteLine("vocabulary size: {0}, vector size: {1}, vq size: {2}", entireTermList.Count, vectorSize, vqSize);

            //Create word and VQ values mapping table
            Dictionary <string, List <byte> > vqResult = new Dictionary <string, List <byte> >();

            foreach (Term term in entireTermList)
            {
                vqResult.Add(term.strTerm, new List <byte>());
            }

            Logger.WriteLine("Dims Distortion:");
            for (int i = 0; i < vectorSize; i++)
            {
                //Generate VQ values for each dimension
                VectorQuantization vq = new VectorQuantization();
                for (int j = 0; j < entireTermList.Count; j++)
                {
                    vq.Add(entireTermList[j].vector[i]);
                }
                double distortion = vq.BuildCodebook(vqSize);
                Logger.WriteLine("Dim {0}: {1}", i, distortion);

                for (int j = 0; j < entireTermList.Count; j++)
                {
                    byte vqValue = (byte)vq.ComputeVQ(entireTermList[j].vector[i]);
                    vqResult[entireTermList[j].strTerm].Add(vqValue);
                }

                //Save VQ codebook into model file
                for (int j = 0; j < vqSize; j++)
                {
                    bw.Write(vq.CodeBook[j]);
                }
            }

            foreach (KeyValuePair <string, List <byte> > pair in vqResult)
            {
                if (pair.Value.Count != vectorSize)
                {
                    throw new Exception(String.Format("word {0} has inconsistent vector size: orginial size is {1}, vq size is {2}",
                                                      pair.Key, vectorSize, pair.Value.Count));
                }

                //term string
                bw.Write(pair.Key);
                //term vector
                for (int b = 0; b < pair.Value.Count; b++)
                {
                    bw.Write(pair.Value[b]);
                }
            }

            bw.Flush();
            fo.Flush();
            fo.Close();

            return(true);
        }
Beispiel #13
0
        //Save matrix into file as binary format
        public static void SaveMatrix(Matrix<double> mat, BinaryWriter fo, bool bVQ = false)
        {
            //Save the width and height of the matrix
            fo.Write(mat.Width);
            fo.Write(mat.Height);

            if (bVQ == false)
            {
                Logger.WriteLine("Saving matrix without VQ...");
                fo.Write(0); // non-VQ

                //Save the data in matrix
                for (int r = 0; r < mat.Height; r++)
                {
                    for (int c = 0; c < mat.Width; c++)
                    {
                        fo.Write(mat[r][c]);
                    }
                }
            }
            else
            {
                //Build vector quantization matrix
                int vqSize = 256;
                VectorQuantization vq = new VectorQuantization();
                Logger.WriteLine("Saving matrix with VQ {0}...", vqSize);

                int valSize = 0;
                for (int i = 0; i < mat.Height; i++)
                {
                    for (int j = 0; j < mat.Width; j++)
                    {
                        vq.Add(mat[i][j]);
                        valSize++;
                    }
                }

                if (vqSize > valSize)
                {
                    vqSize = valSize;
                }

                double distortion = vq.BuildCodebook(vqSize);
                Logger.WriteLine("Distortion: {0}, vqSize: {1}", distortion, vqSize);

                //Save VQ codebook into file
                fo.Write(vqSize);
                for (int j = 0; j < vqSize; j++)
                {
                    fo.Write(vq.CodeBook[j]);
                }

                //Save the data in matrix
                for (int r = 0; r < mat.Height; r++)
                {
                    for (int c = 0; c < mat.Width; c++)
                    {
                        fo.Write((byte)vq.ComputeVQ(mat[r][c]));
                    }
                }
            }
        }
Beispiel #14
0
        private void saveLSTMWeight(Vector4[][] weight, BinaryWriter fo, bool bVQ = false)
        {
            int w = weight.Length;
            int h = weight[0].Length;
            int vqSize = 256;

            Logger.WriteLine("Saving LSTM weight matrix. width:{0}, height:{1}, vq:{2}", w, h, bVQ);

            fo.Write(weight.Length);
            fo.Write(weight[0].Length);

            if (bVQ == false)
            {
                fo.Write(0);

                for (int i = 0; i < w; i++)
                {
                    for (int j = 0; j < h; j++)
                    {
                        fo.Write(weight[i][j].X);
                        fo.Write(weight[i][j].Y);
                        fo.Write(weight[i][j].Z);
                        fo.Write(weight[i][j].W);
                    }
                }
            }
            else
            {
                //Build vector quantization model
                VectorQuantization vqInputCell = new VectorQuantization();
                VectorQuantization vqInputForgetGate = new VectorQuantization();
                VectorQuantization vqInputInputGate = new VectorQuantization();
                VectorQuantization vqInputOutputGate = new VectorQuantization();
                for (int i = 0; i < w; i++)
                {
                    for (int j = 0; j < h; j++)
                    {
                        vqInputInputGate.Add(weight[i][j].X);
                        vqInputForgetGate.Add(weight[i][j].Y);
                        vqInputCell.Add(weight[i][j].Z);
                        vqInputOutputGate.Add(weight[i][j].W);
                    }
                }

                double distortion = 0.0;

                distortion = vqInputInputGate.BuildCodebook(vqSize);
                Logger.WriteLine("InputInputGate distortion: {0}", distortion);

                distortion = vqInputForgetGate.BuildCodebook(vqSize);
                Logger.WriteLine("InputForgetGate distortion: {0}", distortion);

                distortion = vqInputCell.BuildCodebook(vqSize);
                Logger.WriteLine("InputCell distortion: {0}", distortion);

                distortion = vqInputOutputGate.BuildCodebook(vqSize);
                Logger.WriteLine("InputOutputGate distortion: {0}", distortion);

                fo.Write(vqSize);

                //Save InputInputGate VQ codebook into file
                for (int j = 0; j < vqSize; j++)
                {
                    fo.Write(vqInputInputGate.CodeBook[j]);
                }

                //Save InputForgetGate VQ codebook into file
                for (int j = 0; j < vqSize; j++)
                {
                    fo.Write(vqInputForgetGate.CodeBook[j]);
                }

                //Save InputCell VQ codebook into file
                for (int j = 0; j < vqSize; j++)
                {
                    fo.Write(vqInputCell.CodeBook[j]);
                }

                //Save InputOutputGate VQ codebook into file
                for (int j = 0; j < vqSize; j++)
                {
                    fo.Write(vqInputOutputGate.CodeBook[j]);
                }

                for (int i = 0; i < w; i++)
                {
                    for (int j = 0; j < h; j++)
                    {
                        fo.Write((byte)vqInputInputGate.ComputeVQ(weight[i][j].X));
                        fo.Write((byte)vqInputForgetGate.ComputeVQ(weight[i][j].Y));
                        fo.Write((byte)vqInputCell.ComputeVQ(weight[i][j].Z));
                        fo.Write((byte)vqInputOutputGate.ComputeVQ(weight[i][j].W));
                    }
                }
            }
        }
Beispiel #15
0
        private void saveLSTMWeight(LSTMWeight[][] weight, BinaryWriter fo)
        {
            int w = weight.Length;
            int h = weight[0].Length;
            int vqSize = 256;

            Logger.WriteLine("Saving LSTM weight matrix. width:{0}, height:{1}, vqSize:{2}", w, h, vqSize);

            fo.Write(weight.Length);
            fo.Write(weight[0].Length);

            //Build vector quantization model
            VectorQuantization vq = new VectorQuantization();
            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    vq.Add(weight[i][j].wInputCell);
                    vq.Add(weight[i][j].wInputForgetGate);
                    vq.Add(weight[i][j].wInputInputGate);
                    vq.Add(weight[i][j].wInputOutputGate);
                }
            }


            double distortion = vq.BuildCodebook(vqSize);
            Logger.WriteLine("Distortion: {0}", distortion);

            //Save VQ codebook into file
            fo.Write(vqSize);
            for (int j = 0; j < vqSize; j++)
            {
                fo.Write(vq.CodeBook[j]);
            }

            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    fo.Write((byte)vq.ComputeVQ(weight[i][j].wInputCell));
                    fo.Write((byte)vq.ComputeVQ(weight[i][j].wInputForgetGate));
                    fo.Write((byte)vq.ComputeVQ(weight[i][j].wInputInputGate));
                    fo.Write((byte)vq.ComputeVQ(weight[i][j].wInputOutputGate));
                }
            }

        }