/** Initializes a {@code Classifier}. */
        protected ClassifierBase(Activity activity, Device device, int numThreads)
        {
            intValues   = new int[getImageSizeX() * getImageSizeY()];
            tfliteModel = loadModelFile(activity);
            switch (device)
            {
            case Device.NNAPI:
                tfliteOptions.SetUseNNAPI(true);
                break;

            case Device.GPU:
                //gpuDelegate = new Xamarin.TensorFlow.Lite. GpuDelegate();
                //tfliteOptions.addDelegate(gpuDelegate);
                break;

            case Device.CPU:
                break;
            }
            tfliteOptions.SetNumThreads(numThreads);
            tflite  = new Interpreter(tfliteModel, tfliteOptions);
            labels  = loadLabelList(activity);
            imgData =
                ByteBuffer.AllocateDirect(
                    DIM_BATCH_SIZE
                    * getImageSizeX()
                    * getImageSizeY()
                    * DIM_PIXEL_SIZE
                    * getNumBytesPerChannel());
            imgData.Order(ByteOrder.NativeOrder());
            //LOGGER.d("Created a Tensorflow Lite Image Classifier.");
        }
예제 #2
0
        /**
         * 打开书籍文件
         *
         * @param chapter  阅读章节
         * @param position 阅读位置
         * @return 0:文件不存在或打开失败  1:打开成功
         */
        public int openBook(int chapter, int[] position)
        {
            this.currentChapter = chapter;
            this.chapterSize    = chaptersList.Count();
            if (currentChapter > chapterSize)
            {
                currentChapter = chapterSize;
            }
            string path = getBookFile(currentChapter).Path;

            try
            {
                File file   = new File(path);
                long length = file.Length();
                if (length > 10)
                {
                    mbBufferLen = (int)length;
                    // 创建文件通道,映射为MappedByteBuffer
                    mbBuff = new RandomAccessFile(file, "r")
                             .Channel
                             .Map(FileChannel.MapMode.ReadOnly, 0, length);
                    curBeginPos = position[0];
                    curEndPos   = position[1];
                    onChapterChanged(chapter);
                    mLines.Clear();
                    return(1);
                }
            }
            catch (IOException e)
            {
                e.PrintStackTrace();
            }
            return(0);
        }
예제 #3
0
        /// <summary>Free the mmap associated with this replica.</summary>
        /// <remarks>
        /// Free the mmap associated with this replica.
        /// Must be called with the cache lock held.
        /// </remarks>
        internal virtual void Munmap()
        {
            MappedByteBuffer mmap = (MappedByteBuffer)mmapData;

            NativeIO.POSIX.Munmap(mmap);
            mmapData = null;
        }
예제 #4
0
파일: Aeron.cs 프로젝트: yksi7417/Aeron.NET
            private void ConnectToDriver()
            {
                var startMs = _epochClock.Time();

                while (!_cncFile.Exists)
                {
                    if (_epochClock.Time() > startMs + _driverTimeoutMs)
                    {
                        throw new DriverTimeoutException("CnC file is created by not initialised.");
                    }

                    Thread.Yield();
                }

                _cncByteBuffer     = IoUtil.MapExistingFile(CncFile().FullName, MapMode.ReadWrite);
                _cncMetaDataBuffer = CncFileDescriptor.CreateMetaDataBuffer(_cncByteBuffer);

                int cncVersion;

                while (0 == (cncVersion = _cncMetaDataBuffer.GetInt(CncFileDescriptor.CncVersionOffset(0))))
                {
                    if (_epochClock.Time() > startMs + _driverTimeoutMs)
                    {
                        throw new DriverTimeoutException("CnC file is created by not initialised.");
                    }

                    Thread.Yield();
                }

                if (CncFileDescriptor.CNC_VERSION != cncVersion)
                {
                    throw new InvalidOperationException(
                              "aeron cnc file version not understood: version=" + cncVersion);
                }
            }
예제 #5
0
        /// <exception cref="System.Exception"/>
        public virtual void TestMlock()
        {
            Assume.AssumeTrue(NativeIO.IsAvailable());
            FilePath TestFile = new FilePath(new FilePath(Runtime.GetProperty("test.build.data"
                                                                              , "build/test/data")), "testMlockFile");
            int BufLen = 12289;

            byte[] buf    = new byte[BufLen];
            int    bufSum = 0;

            for (int i = 0; i < buf.Length; i++)
            {
                buf[i]  = unchecked ((byte)(i % 60));
                bufSum += buf[i];
            }
            FileOutputStream fos = new FileOutputStream(TestFile);

            try
            {
                fos.Write(buf);
                fos.GetChannel().Force(true);
            }
            finally
            {
                fos.Close();
            }
            FileInputStream fis     = null;
            FileChannel     channel = null;

            try
            {
                // Map file into memory
                fis     = new FileInputStream(TestFile);
                channel = fis.GetChannel();
                long             fileSize = channel.Size();
                MappedByteBuffer mapbuf   = channel.Map(FileChannel.MapMode.ReadOnly, 0, fileSize);
                // mlock the buffer
                NativeIO.POSIX.Mlock(mapbuf, fileSize);
                // Read the buffer
                int sum = 0;
                for (int i_1 = 0; i_1 < fileSize; i_1++)
                {
                    sum += mapbuf.Get(i_1);
                }
                Assert.Equal("Expected sums to be equal", bufSum, sum);
                // munmap the buffer, which also implicitly unlocks it
                NativeIO.POSIX.Munmap(mapbuf);
            }
            finally
            {
                if (channel != null)
                {
                    channel.Close();
                }
                if (fis != null)
                {
                    fis.Close();
                }
            }
        }
예제 #6
0
 internal ClientMmap(ShortCircuitReplica replica, MappedByteBuffer map, bool anchored
                     )
 {
     this.replica  = replica;
     this.map      = map;
     this.anchored = anchored;
 }
예제 #7
0
        Load(long length, FileInputStream blockIn, FileInputStream metaIn, string blockFileName
             )
        {
            Org.Apache.Hadoop.Hdfs.Server.Datanode.Fsdataset.Impl.MappableBlock mappableBlock
                = null;
            MappedByteBuffer mmap         = null;
            FileChannel      blockChannel = null;

            try
            {
                blockChannel = blockIn.GetChannel();
                if (blockChannel == null)
                {
                    throw new IOException("Block InputStream has no FileChannel.");
                }
                mmap = blockChannel.Map(FileChannel.MapMode.ReadOnly, 0, length);
                NativeIO.POSIX.GetCacheManipulator().Mlock(blockFileName, mmap, length);
                VerifyChecksum(length, metaIn, blockChannel, blockFileName);
                mappableBlock = new Org.Apache.Hadoop.Hdfs.Server.Datanode.Fsdataset.Impl.MappableBlock
                                    (mmap, length);
            }
            finally
            {
                IOUtils.CloseQuietly(blockChannel);
                if (mappableBlock == null)
                {
                    if (mmap != null)
                    {
                        NativeIO.POSIX.Munmap(mmap);
                    }
                }
            }
            // unmapping also unlocks
            return(mappableBlock);
        }
예제 #8
0
        public LogBuffers(string logFileName)
        {
            var fileInfo = new FileInfo(logFileName);

            var logLength  = fileInfo.Length;
            var termLength = LogBufferDescriptor.ComputeTermLength(logLength);

            LogBufferDescriptor.CheckTermLength(termLength);

            _termLength = termLength;

            // if log length exceeds MAX_INT we need multiple mapped buffers, (see FileChannel.map doc).
            if (logLength < int.MaxValue)
            {
                var mappedBuffer = IoUtil.MapExistingFile(logFileName);

                _mappedByteBuffers = new[] { mappedBuffer };

                var metaDataSectionOffset = termLength * LogBufferDescriptor.PARTITION_COUNT;

                for (var i = 0; i < LogBufferDescriptor.PARTITION_COUNT; i++)
                {
                    var metaDataOffset = metaDataSectionOffset + (i * LogBufferDescriptor.TERM_META_DATA_LENGTH);

                    _atomicBuffers[i] = new UnsafeBuffer(mappedBuffer.Pointer, i * termLength, termLength);
                    _atomicBuffers[i + LogBufferDescriptor.PARTITION_COUNT] = new UnsafeBuffer(mappedBuffer.Pointer, metaDataOffset, LogBufferDescriptor.TERM_META_DATA_LENGTH);
                }

                _atomicBuffers[_atomicBuffers.Length - 1] = new UnsafeBuffer(mappedBuffer.Pointer, (int)(logLength - LogBufferDescriptor.LOG_META_DATA_LENGTH), LogBufferDescriptor.LOG_META_DATA_LENGTH);
            }
            else
            {
                _mappedByteBuffers = new MappedByteBuffer[LogBufferDescriptor.PARTITION_COUNT + 1];
                var metaDataSectionOffset = termLength * (long)LogBufferDescriptor.PARTITION_COUNT;
                var metaDataSectionLength = (int)(logLength - metaDataSectionOffset);

                var memoryMappedFile     = IoUtil.OpenMemoryMappedFile(logFileName);
                var metaDataMappedBuffer = new MappedByteBuffer(memoryMappedFile, metaDataSectionOffset, metaDataSectionLength);

                _mappedByteBuffers[_mappedByteBuffers.Length - 1] = metaDataMappedBuffer;

                for (var i = 0; i < LogBufferDescriptor.PARTITION_COUNT; i++)
                {
                    _mappedByteBuffers[i] = new MappedByteBuffer(memoryMappedFile, termLength * (long)i, termLength);

                    _atomicBuffers[i] = new UnsafeBuffer(_mappedByteBuffers[i].Pointer, termLength);
                    _atomicBuffers[i + LogBufferDescriptor.PARTITION_COUNT] = new UnsafeBuffer(metaDataMappedBuffer.Pointer, i * LogBufferDescriptor.TERM_META_DATA_LENGTH, LogBufferDescriptor.TERM_META_DATA_LENGTH);
                }

                _atomicBuffers[_atomicBuffers.Length - 1] = new UnsafeBuffer(metaDataMappedBuffer.Pointer, metaDataSectionLength - LogBufferDescriptor.LOG_META_DATA_LENGTH, LogBufferDescriptor.LOG_META_DATA_LENGTH);
            }

            // TODO try/catch

            foreach (var buffer in _atomicBuffers)
            {
                buffer.VerifyAlignment();
            }
        }
예제 #9
0
 public virtual void Close()
 {
     if (mmap != null)
     {
         NativeIO.POSIX.Munmap(mmap);
         mmap = null;
     }
 }
예제 #10
0
 /// <summary>Unmaps the block from memory.</summary>
 /// <remarks>
 /// Unmaps the block from memory. See munmap(2).
 /// There isn't any portable way to unmap a memory region in Java.
 /// So we use the sun.nio method here.
 /// Note that unmapping a memory region could cause crashes if code
 /// continues to reference the unmapped code.  However, if we don't
 /// manually unmap the memory, we are dependent on the finalizer to
 /// do it, and we have no idea when the finalizer will run.
 /// </remarks>
 /// <param name="buffer">The buffer to unmap.</param>
 public static void Munmap(MappedByteBuffer buffer)
 {
     if (buffer is DirectBuffer)
     {
         Cleaner cleaner = ((DirectBuffer)buffer).Cleaner();
         cleaner.Clean();
     }
 }
예제 #11
0
        /// <summary>
        /// Create the buffer which wraps the section in the CnC file for the counter values.
        /// </summary>
        /// <param name="buffer">         for the CnC file. </param>
        /// <param name="metaDataBuffer"> within the CnC file. </param>
        /// <returns> a buffer which wraps the section in the CnC file for the counter values. </returns>
        public static UnsafeBuffer CreateCountersValuesBuffer(MappedByteBuffer buffer, IDirectBuffer metaDataBuffer)
        {
            var offset = END_OF_METADATA_OFFSET + metaDataBuffer.GetInt(ToDriverBufferLengthOffset(0)) +
                         metaDataBuffer.GetInt(ToClientsBufferLengthOffset(0)) +
                         metaDataBuffer.GetInt(CountersMetaDataBufferLengthOffset(0));

            return(new UnsafeBuffer(buffer.Pointer, offset, metaDataBuffer.GetInt(CountersValuesBufferLengthOffset(0))));
        }
예제 #12
0
        /// <summary>
        /// Manage a CnC file given a buffer and offsets of version and timestamp.
        /// </summary>
        /// <param name="buffer">            for the CnC fields </param>
        /// <param name="versionFieldOffset">   for the version field </param>
        /// <param name="timestampFieldOffset"> for the timestamp field </param>
        public MarkFile(UnsafeBuffer buffer, int versionFieldOffset, int timestampFieldOffset)
        {
            ValidateOffsets(versionFieldOffset, timestampFieldOffset);

            this.parentDir            = null;
            this.markFile             = null;
            this.mappedBuffer         = null;
            this.buffer               = buffer;
            this.versionFieldOffset   = versionFieldOffset;
            this.timestampFieldOffset = timestampFieldOffset;
        }
예제 #13
0
파일: IoUtil.cs 프로젝트: lanicon/Aeron.NET
        public static MappedByteBuffer MapNewOrExistingFile(FileInfo cncFile, long length)
        {
            var fileAccess             = FileAccess.ReadWrite;
            var fileShare              = FileShare.ReadWrite | FileShare.Delete;
            var memoryMappedFileAccess = MemoryMappedFileAccess.ReadWrite;

            var f                = new FileStream(cncFile.FullName, FileMode.OpenOrCreate, fileAccess, fileShare);
            var mmFile           = MemoryMappedFile.CreateFromFile(f, null, length, memoryMappedFileAccess, HandleInheritability.None, false);
            var mappedByteBuffer = new MappedByteBuffer(mmFile, 0, length);

            return(mappedByteBuffer);
        }
예제 #14
0
        /// <summary>
        /// Map a pre-existing CnC file if one present and is active.
        ///
        /// Total length of CnC file will be mapped until <seealso cref="#close()"/> is called.
        /// </summary>
        /// <param name="directory">             for the CnC file </param>
        /// <param name="filename">              of the CnC file </param>
        /// <param name="versionFieldOffset">    to use for version field access </param>
        /// <param name="timestampFieldOffset">  to use for timestamp field access </param>
        /// <param name="timeoutMs">             for the activity check (in milliseconds) and for how long to wait for file to exist </param>
        /// <param name="epochClock">            to use for time checks </param>
        /// <param name="versionCheck">          to use for existing CnC file and version field </param>
        /// <param name="logger">                to use to signal progress or null </param>
        public MarkFile(DirectoryInfo directory, string filename, int versionFieldOffset, int timestampFieldOffset,
                        long timeoutMs, IEpochClock epochClock, Action <int> versionCheck, Action <string> logger)
        {
            ValidateOffsets(versionFieldOffset, timestampFieldOffset);

            this.parentDir    = directory;
            this.markFile     = new FileInfo(Path.Combine(directory.FullName, filename));
            this.mappedBuffer = MapExistingCncFile(markFile, versionFieldOffset, timestampFieldOffset, timeoutMs,
                                                   epochClock, versionCheck, logger);
            this.buffer               = new UnsafeBuffer(mappedBuffer);
            this.versionFieldOffset   = versionFieldOffset;
            this.timestampFieldOffset = timestampFieldOffset;
        }
예제 #15
0
        public static MappedByteBuffer MapNewOrExistingCncFile(FileInfo cncFile, bool shouldPreExist,
                                                               int versionFieldOffset, int timestampFieldOffset, long totalFileLength, long timeoutMs,
                                                               IEpochClock epochClock, Action <int> versionCheck, Action <string> logger)
        {
            MappedByteBuffer cncByteBuffer = null;

            try
            {
                cncByteBuffer = IoUtil.MapNewOrExixtingFile(cncFile, totalFileLength);

                UnsafeBuffer cncBuffer = new UnsafeBuffer(cncByteBuffer);

                if (shouldPreExist)
                {
                    int cncVersion = cncBuffer.GetIntVolatile(versionFieldOffset);

                    if (null != logger)
                    {
                        logger("INFO: CnC file exists: " + cncFile);
                    }

                    versionCheck(cncVersion);

                    long timestamp    = cncBuffer.GetLongVolatile(timestampFieldOffset);
                    long now          = epochClock.Time();
                    long timestampAge = now - timestamp;

                    if (null != logger)
                    {
                        logger("INFO: heartbeat is (ms): " + timestampAge);
                    }

                    if (timestampAge < timeoutMs)
                    {
                        throw new System.InvalidOperationException("Active CnC file detected");
                    }
                }
            }
            catch (Exception)
            {
                if (null != cncByteBuffer)
                {
                    IoUtil.Unmap(cncByteBuffer);
                }

                throw;
            }

            return(cncByteBuffer);
        }
예제 #16
0
        private void LoadTensorflowModel()
        {
            var assets = Android.App.Application.Context.Assets;
            AssetFileDescriptor fileDescriptor = assets.OpenFd(MODEL_FILE);
            FileInputStream     inputStream    = new FileInputStream(fileDescriptor.FileDescriptor);
            FileChannel         fileChannel    = inputStream.Channel;
            long startOffset    = fileDescriptor.StartOffset;
            long declaredLength = fileDescriptor.DeclaredLength;

            _model = fileChannel.Map(FileChannel.MapMode.ReadOnly, startOffset, declaredLength);
            Interpreter.Options options = new Interpreter.Options();
            options.SetNumThreads(THREAD_NUM);
            _interpreter = new Interpreter(_model, options);
        }
 /** Closes the interpreter and model to release resources. */
 public void close()
 {
     if (tflite != null)
     {
         tflite.Close();
         tflite = null;
     }
     //if (gpuDelegate != null)
     //{
     //    gpuDelegate.Close();
     //    gpuDelegate = null;
     //}
     tfliteModel = null;
 }
예제 #18
0
        /// <summary>
        /// Create a CnC file if none present. Checking if an active CnC file exists and is active. Existing CnC file
        /// is used if not active.
        ///
        /// Total length of CnC file will be mapped until <seealso cref="#close()"/> is called.
        /// </summary>
        /// <param name="markFile">               to use </param>
        /// <param name="shouldPreExist">        or not </param>
        /// <param name="versionFieldOffset">    to use for version field access </param>
        /// <param name="timestampFieldOffset">  to use for timestamp field access </param>
        /// <param name="totalFileLength">       to allocate when creating new CnC file </param>
        /// <param name="timeoutMs">             for the activity check (in milliseconds) </param>
        /// <param name="epochClock">            to use for time checks </param>
        /// <param name="versionCheck">          to use for existing CnC file and version field </param>
        /// <param name="logger">                to use to signal progress or null </param>
        public MarkFile(FileInfo markFile, bool shouldPreExist, int versionFieldOffset, int timestampFieldOffset,
                        int totalFileLength, long timeoutMs, IEpochClock epochClock, Action <int> versionCheck,
                        Action <string> logger)
        {
            ValidateOffsets(versionFieldOffset, timestampFieldOffset);

            this.parentDir    = markFile.Directory;
            this.markFile     = markFile;
            this.mappedBuffer = MapNewOrExistingCncFile(markFile, shouldPreExist, versionFieldOffset,
                                                        timestampFieldOffset, totalFileLength, timeoutMs, epochClock, versionCheck, logger);

            this.buffer               = new UnsafeBuffer(mappedBuffer.Pointer, totalFileLength);
            this.versionFieldOffset   = versionFieldOffset;
            this.timestampFieldOffset = timestampFieldOffset;
        }
        private MachineCounters()
        {
            // Client stream id should be unique per machine, not per media driver.
            // Temp is ok, we only need to avoid same stream id among concurrently
            // running clients (within 10 seconds), not forever. If a machine
            // restarts and cleans temp there are no other running client and
            // we could start from 0 again.

            var dirPath =
                Path.Combine(Path.GetTempPath(), "aeron_client_counters"); // any name, but do not start with 'aeron-'

            Directory.CreateDirectory(dirPath);
            var counterFile = Path.Combine(dirPath, ClientCountersFileName);

            _clientCounters = IoUtil.MapNewOrExistingFile(new FileInfo(counterFile), 4096);
        }
예제 #20
0
파일: IoUtil.cs 프로젝트: lanicon/Aeron.NET
        public static MappedByteBuffer MapNewFile(FileInfo cncFile, long length, bool fillWithZeros = true)
        {
            var fileAccess             = FileAccess.ReadWrite;
            var fileShare              = FileShare.ReadWrite | FileShare.Delete;
            var memoryMappedFileAccess = MemoryMappedFileAccess.ReadWrite;
            var f                = new FileStream(cncFile.FullName, FileMode.CreateNew, fileAccess, fileShare);
            var mmFile           = MemoryMappedFile.CreateFromFile(f, null, length, memoryMappedFileAccess, HandleInheritability.None, false);
            var mappedByteBuffer = new MappedByteBuffer(mmFile, 0, length);

            if (fillWithZeros)
            {
                mappedByteBuffer.FillWithZeros();
            }

            return(mappedByteBuffer);
        }
예제 #21
0
        /// <summary>
        /// Create a CnC directory and file if none present. Checking if an active CnC file exists and is active. Old CnC
        /// file is deleted and recreated if not active.
        ///
        /// Total length of CnC file will be mapped until <seealso cref="#close()"/> is called.
        /// </summary>
        /// <param name="directory">             for the CnC file </param>
        /// <param name="filename">              of the CnC file </param>
        /// <param name="warnIfDirectoryExists"> for logging purposes </param>
        /// <param name="dirDeleteOnStart">      if desired </param>
        /// <param name="versionFieldOffset">    to use for version field access </param>
        /// <param name="timestampFieldOffset">  to use for timestamp field access </param>
        /// <param name="totalFileLength">       to allocate when creating new CnC file </param>
        /// <param name="timeoutMs">             for the activity check (in milliseconds) </param>
        /// <param name="epochClock">            to use for time checks </param>
        /// <param name="versionCheck">          to use for existing CnC file and version field </param>
        /// <param name="logger">                to use to signal progress or null </param>
        public MarkFile(DirectoryInfo directory, string filename, bool warnIfDirectoryExists, bool dirDeleteOnStart,
                        int versionFieldOffset, int timestampFieldOffset, int totalFileLength, long timeoutMs,
                        IEpochClock epochClock, Action <int> versionCheck, Action <string> logger)
        {
            ValidateOffsets(versionFieldOffset, timestampFieldOffset);

            EnsureDirectoryExists(directory, filename, warnIfDirectoryExists, dirDeleteOnStart, versionFieldOffset,
                                  timestampFieldOffset, timeoutMs, epochClock, versionCheck, logger);

            this.parentDir            = directory;
            this.markFile             = new FileInfo(Path.Combine(directory.Name, filename));
            this.mappedBuffer         = MapNewFile(markFile, totalFileLength);
            this.buffer               = new UnsafeBuffer(mappedBuffer.Pointer, totalFileLength);
            this.versionFieldOffset   = versionFieldOffset;
            this.timestampFieldOffset = timestampFieldOffset;
        }
예제 #22
0
        public static MappedByteBuffer MapExistingCncFile(FileInfo cncFile, int versionFieldOffset,
                                                          int timestampFieldOffset, long timeoutMs, IEpochClock epochClock, Action <int> versionCheck,
                                                          Action <string> logger)
        {
            long startTimeMs = epochClock.Time();

            while (true)
            {
                while (!cncFile.Exists)
                {
                    if (epochClock.Time() > (startTimeMs + timeoutMs))
                    {
                        throw new InvalidOperationException("CnC file not found: " + cncFile.FullName);
                    }

                    Sleep(16);
                }

                MappedByteBuffer cncByteBuffer = MapExistingFile(cncFile, logger);
                UnsafeBuffer     cncBuffer     = new UnsafeBuffer(cncByteBuffer);

                int cncVersion;
                while (0 == (cncVersion = cncBuffer.GetIntVolatile(versionFieldOffset)))
                {
                    if (epochClock.Time() > (startTimeMs + timeoutMs))
                    {
                        throw new InvalidOperationException("CnC file is created but not initialised.");
                    }

                    Sleep(1);
                }

                versionCheck(cncVersion);

                while (0 == cncBuffer.GetLongVolatile(timestampFieldOffset))
                {
                    if (epochClock.Time() > (startTimeMs + timeoutMs))
                    {
                        throw new InvalidOperationException("No non-0 timestamp detected.");
                    }

                    Sleep(1);
                }

                return(cncByteBuffer);
            }
        }
예제 #23
0
        public void Initialize()
        {
            if (Initialized)
            {
                return;
            }

            ByteArrayBacked        = new UnsafeBuffer(new byte[BufferCapacity], 0, BufferCapacity);
            AlignedByteArrayBacked =
                new UnsafeBuffer(BufferUtil.AllocateDirectAligned(BufferCapacity, BitUtil.CACHE_LINE_LENGTH));

            UnmanagedBacked = new UnsafeBuffer(Marshal.AllocHGlobal(BufferCapacity), BufferCapacity);

            TempFileName     = Path.GetTempFileName();
            MappedByteBuffer = new MappedByteBuffer(MemoryMappedFile.CreateFromFile(TempFileName,
                                                                                    FileMode.OpenOrCreate, null, BufferCapacity));

            MemoryMappedFileBacked = new UnsafeBuffer(MappedByteBuffer.Pointer, BufferCapacity);

            Initialized = true;
        }
예제 #24
0
        public List <ImageClassificationModel> Classify(byte[] image)
        {
            MappedByteBuffer mappedByteBuffer = GetModelAsMappedByteBuffer();

            // TODO: Find a solution that is not being deprecated.
            Xamarin.TensorFlow.Lite.Interpreter interpreter = new Xamarin.TensorFlow.Lite.Interpreter(mappedByteBuffer);

            //To resize the image, we first need to get its required width and height
            Xamarin.TensorFlow.Lite.Tensor tensor = interpreter.GetInputTensor(0);
            int[] shape = tensor.Shape();

            int width  = shape[1];
            int height = shape[2];

            ByteBuffer byteBuffer = GetPhotoAsByteBuffer(image, width, height);

            //use StreamReader to import the labels from labels.txt
            StreamReader streamReader = new StreamReader(Application.Context.Assets.Open("labels.txt"));

            //Transform labels.txt into List<string>
            List <string> labels = streamReader.ReadToEnd().Split('\n').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToList();

            //Convert our two-dimensional array into a Java.Lang.Object, the required input for Xamarin.TensorFlow.List.Interpreter
            float[][]        outputLocations = new float[1][] { new float[labels.Count] };
            Java.Lang.Object outputs         = Java.Lang.Object.FromArray(outputLocations);

            interpreter.Run(byteBuffer, outputs);
            float[][] classificationResult = outputs.ToArray <float[]>();

            //Map the classificationResult to the labels and sort the result to find which label has the highest probability
            List <ImageClassificationModel> classificationModelList = new List <ImageClassificationModel>();

            for (int i = 0; i < labels.Count; i++)
            {
                string label = labels[i]; classificationModelList.Add(new ImageClassificationModel(label, classificationResult[0][i]));
            }

            return(classificationModelList);
        }
예제 #25
0
        public static void EnsureDirectoryExists(DirectoryInfo directory, string filename, bool warnIfDirectoryExists,
                                                 bool dirDeleteOnStart, int versionFieldOffset, int timestampFieldOffset, long timeoutMs,
                                                 IEpochClock epochClock, Action <int> versionCheck, Action <string> logger)
        {
            FileInfo cncFile = new FileInfo(Path.Combine(directory.FullName, filename));

            if (directory.Exists)
            {
                if (warnIfDirectoryExists && null != logger)
                {
                    logger("WARNING: " + directory + " already exists.");
                }

                if (!dirDeleteOnStart)
                {
                    int offset = Math.Min(versionFieldOffset, timestampFieldOffset);
                    int length = Math.Max(versionFieldOffset, timestampFieldOffset) + BitUtil.SIZE_OF_LONG - offset;
                    MappedByteBuffer cncByteBuffer = MapExistingFile(cncFile, logger, offset, length);

                    try
                    {
                        if (IsActive(cncByteBuffer, epochClock, timeoutMs, versionFieldOffset, timestampFieldOffset,
                                     versionCheck, logger))
                        {
                            throw new System.InvalidOperationException("Active CnC file detected");
                        }
                    }
                    finally
                    {
                        IoUtil.Unmap(cncByteBuffer);
                    }
                }

                IoUtil.Delete(directory, false);
            }

            IoUtil.EnsureDirectoryExists(directory, directory.ToString());
        }
예제 #26
0
 internal virtual MappedByteBuffer LoadMmapInternal()
 {
     try
     {
         FileChannel      channel = dataStream.GetChannel();
         MappedByteBuffer mmap    = channel.Map(FileChannel.MapMode.ReadOnly, 0, Math.Min(int.MaxValue
                                                                                          , channel.Size()));
         if (Log.IsTraceEnabled())
         {
             Log.Trace(this + ": created mmap of size " + channel.Size());
         }
         return(mmap);
     }
     catch (IOException e)
     {
         Log.Warn(this + ": mmap error", e);
         return(null);
     }
     catch (RuntimeException e)
     {
         Log.Warn(this + ": mmap error", e);
         return(null);
     }
 }
예제 #27
0
        public static bool IsActive(MappedByteBuffer cncByteBuffer, IEpochClock epochClock, long timeoutMs,
                                    int versionFieldOffset, int timestampFieldOffset, Action <int> versionCheck, Action <string> logger)
        {
            if (null == cncByteBuffer)
            {
                return(false);
            }

            UnsafeBuffer cncBuffer = new UnsafeBuffer(cncByteBuffer);

            long startTimeMs = epochClock.Time();
            int  cncVersion;

            while (0 == (cncVersion = cncBuffer.GetIntVolatile(versionFieldOffset)))
            {
                if (epochClock.Time() > (startTimeMs + timeoutMs))
                {
                    throw new System.InvalidOperationException("CnC file is created but not initialised.");
                }

                Sleep(1);
            }

            versionCheck(cncVersion);

            long timestamp    = cncBuffer.GetLongVolatile(timestampFieldOffset);
            long now          = epochClock.Time();
            long timestampAge = now - timestamp;

            if (null != logger)
            {
                logger("INFO: heartbeat is (ms): " + timestampAge);
            }

            return(timestampAge <= timeoutMs);
        }
예제 #28
0
 public UnsafeBuffer(MappedByteBuffer buffer)
 {
     Wrap(buffer.Pointer, 0, (int)buffer.Capacity);
 }
예제 #29
0
 public static UnsafeBuffer CreateToDriverBuffer(MappedByteBuffer buffer, IDirectBuffer metaDataBuffer)
 {
     return(new UnsafeBuffer(buffer.Pointer, END_OF_METADATA_OFFSET, metaDataBuffer.GetInt(ToDriverBufferLengthOffset(0))));
 }
예제 #30
0
 public static UnsafeBuffer CreateMetaDataBuffer(MappedByteBuffer buffer)
 {
     return(new UnsafeBuffer(buffer.Pointer, 0, BitUtil.SIZE_OF_INT + META_DATA_LENGTH));
 }