MappedByteBuffer的学习

package com.taobao.danchen.bytebuffer;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class MappedFile {
    // 文件名
    private String fileName;
    // 文件路径
    private String filePath;
    // 文件对象
    private File file;
    private MappedByteBuffer mappedByteBuffer;
    private FileChannel fileChannel;
    private boolean bundSuccess = false;
    // 文件大小
    private final static long MAX_FILE_SIZE = 1024 * 1024 * 50;
    // 文件写入位置
    private long writePosition = 0;
    // 最后一次刷数据的时间
    private long lastFlushTime;
    // 上一次刷的文件位置
    private long lastFlushFilePosition = 0;
    // 最大的脏数据量,系统必须触发一次强制刷
    private long MAX_FLUSH_DATA_SIZE = 1024 * 512;
    // 最大的时间间隔,系统必须触发一次强制刷
    private long MAX_FLUSH_TIME_GAP = 1000;

    public MappedFile(String fileName, String filePath) {
	super();
	this.fileName = fileName;
	this.filePath = filePath;
	this.file = new File(filePath + "/" + fileName);
	if (!file.exists()) {
	    try {
		file.createNewFile();
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	}
    }

    /**
     * 内存映射文件绑定
     * 
     * @return
     */
    public synchronized boolean boundChannelToByteBuffer() {
	try {
	    RandomAccessFile raf = new RandomAccessFile(file, "rw");
	    this.fileChannel = raf.getChannel();
	} catch (Exception e) {
	    e.printStackTrace();
	    this.bundSuccess = false;
	    return false;
	}
	try {
	    this.mappedByteBuffer = this.fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, MAX_FILE_SIZE);
	} catch (IOException e) {
	    e.printStackTrace();
	    this.bundSuccess = false;
	    return false;
	}
	this.bundSuccess = true;
	return true;
    }

    public synchronized boolean appendData(byte[] data) throws Exception {
	if (!bundSuccess) {
	    throw new Exception("内存映射文件没有建立,请检查...");
	}
	writePosition = writePosition + data.length;
	if (writePosition >= MAX_FILE_SIZE) {
	    flush();
	    writePosition = writePosition - data.length;
	    System.out.println("File=" + file.toURI().toString() + " is writed full.");
	    System.out.println("already write data length:" + writePosition + "max file size=" + MAX_FILE_SIZE);
	    return false;
	}
	this.mappedByteBuffer.put(data);
	// 检测修改量
	if (writePosition - lastFlushFilePosition > this.MAX_FLUSH_DATA_SIZE) {
	    flush();
	}
	// 检测时间间隔
	if (System.currentTimeMillis() - lastFlushTime > this.MAX_FLUSH_TIME_GAP && writePosition > lastFlushFilePosition) {
	    flush();
	}
	return true;
    }

    public synchronized void flush() {
	this.mappedByteBuffer.force();
	this.lastFlushTime = System.currentTimeMillis();
	this.lastFlushFilePosition = writePosition;
    }

    public long getLastFlushTime() {
	return lastFlushTime;
    }

    public String getFileName() {
	return fileName;
    }

    public String getFilePath() {
	return filePath;
    }

    public boolean isBundSuccess() {
	return bundSuccess;
    }

    public File getFile() {
	return file;
    }

    public static long getMaxFileSize() {
	return MAX_FILE_SIZE;
    }

    public long getWritePosition() {
	return writePosition;
    }

    public long getLastFlushFilePosition() {
	return lastFlushFilePosition;
    }

    public long getMAX_FLUSH_DATA_SIZE() {
	return MAX_FLUSH_DATA_SIZE;
    }

    public long getMAX_FLUSH_TIME_GAP() {
	return MAX_FLUSH_TIME_GAP;
    }
}


转载自:http://zhaolinjnu.blog.sohu.com/265626071.html

### 在安卓平台中集成和使用TensorFlow深度学习模型 要在安卓平台上集成并使用TensorFlow深度学习模型,通常需要遵循一系列标准化的流程。以下是详细的说明: #### 1. **准备阶段** 确保所使用的模型已经在TensorFlow框架下训练完成,无论是自定义模型还是预训练模型均可[^1]。如果计划在移动设备上运行该模型,则需将其转换为适合移动端的轻量化格式。 #### 2. **模型转换** 将标准的TensorFlow模型转换为TensorFlow Lite格式是关键步骤之一。通过`TFLiteConverter`工具可以轻松实现这一目标。此过程会优化模型结构以适应低功耗环境下的高效执行。 ```python import tensorflow as tf converter = tf.lite.TFLiteConverter.from_saved_model('path_to_your_model') tflite_model = converter.convert() with open('model.tflite', 'wb') as f: f.write(tflite_model) ``` 上述代码展示了如何利用Python脚本来完成从保存好的TensorFlow模型文件至`.tflite`文件的转换操作[^3]。 #### 3. **集成到Android应用** 一旦获得了`.tflite`格式的模型文件,下一步就是将其嵌入到Android应用程序当中。这可以通过下载官方提供的TensorFlow Lite AAR包或者直接克隆相关仓库如`tflite-android-transformers`来进行[^4]。 - 添加依赖项到项目的`build.gradle`文件中: ```gradle dependencies { implementation 'org.tensorflow:tensorflow-lite:+' } ``` 这样设置后即可引入必要的类库支持后续开发工作。 #### 4. **调用模型进行预测** 最后一步涉及编写实际业务逻辑代码来加载已打包进APK中的TF Lite模型实例,并传入适当的数据作为输入参数从而获取输出结果[^2]。 ```java try (Interpreter interpreter = new Interpreter(loadModelFile())) { float[][] input = { ... }; // Prepare your input data here. float[][] output = new float[1][NUM_CLASSES]; interpreter.run(input, output); System.out.println(Arrays.toString(output[0])); } private MappedByteBuffer loadModelFile() throws IOException { AssetFileDescriptor fileDescriptor = this.getAssets().openFd("model.tflite"); FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor()); FileChannel fileChannel = inputStream.getChannel(); long startOffset = fileDescriptor.getStartOffset(); long declaredLength = fileDescriptor.getDeclaredLength(); return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength); } ``` 以上Java片段示范了怎样创建解释器对象以及处理数据流动的过程。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值