5.31

所花时间(包括上课):1.5

打码量(行):160

博客量(篇):1

了解到知识点:学习调用大模型

 

 package com.example.myapp;

 

import android.content.res.AssetFileDescriptor;

import android.os.Bundle;

 

import androidx.appcompat.app.AppCompatActivity;

 

import org.tensorflow.lite.Interpreter;

 

import java.io.FileInputStream;

import java.io.IOException;

import java.nio.MappedByteBuffer;

import java.nio.channels.FileChannel;

 

public class MainActivity extends AppCompatActivity {

 

    private Interpreter tflite;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        try {

            tflite = new Interpreter(loadModelFile());

            // 模型加载成功,可以进行预测或推理操作

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

    private MappedByteBuffer loadModelFile() throws IOException {

        AssetFileDescriptor fileDescriptor = 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);

    }

 

    // 进行推理或预测的方法

    private void runInference() {

        // 示例:将输入数据传递给模型,并获取输出结果

        // float[][] input = ...; // 准备输入数据

        // float[][] output = new float[1][10]; // 假设模型输出是一个大小为10的向量

        // tflite.run(input, output);

    }

 

    @Override

    protected void onDestroy() {

        if (tflite != null) {

            tflite.close();

        }

        super.onDestroy();

    }

}

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <!-- UI 元素可以根据具体应用场景自行添加 -->

 

</RelativeLayout>

posted @ 2024-05-31 22:54  赵千万  阅读(3)  评论(0编辑  收藏  举报