我试图使用Firebase和自定义模型制作一个图像分类Android应用。
解释器无法运行并给出此错误:
“执行Firebase ML任务时发生内部错误”
模型以(1,224,224,3)作为图像输入形状 并给出(1,1001)作为输出。
请帮帮我。
我的代码:
public class TensorflowStuff implements Classifier
{
FirebaseCustomRemoteModel cloudModel;
FirebaseCustomLocalModel localModel;
FirebaseModelInterpreter interpreter;
FirebaseModelInputOutputOptions inoutOptions;
float[] probs = new float[5];
String [] labels = new String[1001];
FirebaseModelInterpreterOptions options;
public TensorflowStuff(){}
public static Classifier make(AssetManager am, String modelName, String labelName, int imgSize) throws IOException {
final TensorflowStuff tf = new TensorflowStuff();
tf.cloudModel = new FirebaseCustomRemoteModel.Builder(modelName).build();
tf.localModel = new FirebaseCustomLocalModel.Builder().setAssetFilePath("model.tflite").build();
FirebaseModelDownloadConditions conditions = new FirebaseModelDownloadConditions.Builder().requireWifi().build();
FirebaseModelManager.getInstance().download(tf.cloudModel,conditions).addOnSuccessListener(new OnSuccessListener<Void>() {
public void onSuccess(Void v){
}
});
FirebaseModelManager.getInstance().isModelDownloaded(tf.cloudModel).addOnSuccessListener(new OnSuccessListener<Boolean>() {
@Override
public void onSuccess(Boolean downloaded) {
FirebaseModelInterpreterOptions options;
if (downloaded) {
options = new FirebaseModelInterpreterOptions.Builder(tf.cloudModel).build();
System.out.println("________DOWNLOADED____________");
}else{
options = new FirebaseModelInterpreterOptions.Builder(tf.localModel).build();
Log.i("tag","NOT DOWNLOADED");
}
try {
tf.interpreter = FirebaseModelInterpreter.getInstance(options);
} catch (FirebaseMLException e) {
e.printStackTrace();
Log.i("tag","INTERPRETOR HASSLE____________");
}
}
});
try {
tf.inoutOptions = new FirebaseModelInputOutputOptions.Builder()
.setInputFormat(0, FirebaseModelDataType.FLOAT32, new int[]{1, imgSize, imgSize, 3})
.setOutputFormat(0, FirebaseModelDataType.FLOAT32, new int[]{1, 1001}).build();
System.out.println("_______ inoutDONE ________");
} catch (FirebaseMLException e) {
e.printStackTrace();
System.out.println("_______________inout ERROR______________");
}
BufferedReader br = new BufferedReader(new InputStreamReader(am.open(labelName)));
String line;
int i = 0;
while ((line = br.readLine()) != null) {
tf.labels[i] = line;
System.out.println("_______"+line);
i++;
}
br.close();
return tf;
}
@Override
public List<Recogonition> recImg(Bitmap bm) {
bm.createScaledBitmap(bm, 224, 224, false);
int batchNum = 0;
float[][][][]input = new float[1][224][224][3];
for (int x = 0; x < 224; x++) {
for (int y = 0; y < 224; y++) {
int pixel = bm.getPixel(x, y);
input[batchNum][x][y][0] = (Color.red(pixel) - 127) / 128.0f;
input[batchNum][x][y][1] = (Color.green(pixel) - 127) / 128.0f;
input[batchNum][x][y][2] = (Color.blue(pixel) - 127) / 128.0f;
}
}
FirebaseModelInputs
inputs = null;
try {
inputs = new FirebaseModelInputs.Builder().add(input).build();
System.out.println("________ INPUT DONE ___________");
} catch (FirebaseMLException e) {
e.printStackTrace();
System.out.println("INPUT ERROR ___________");
}
System.out.println("______RUNNING__________");
interpreter.run(inputs, inoutOptions).addOnSuccessListener(new OnSuccessListener<FirebaseModelOutputs>() {
public void onSuccess(FirebaseModelOutputs result) {
float[][] results = new float[1][5];
results = result.getOutput(0);
probs = results[0];
Log.i("tag","________ProBS__"+probs);
System.out.println("______ RAN __________");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
System.out.println("___ Run Error__"+e);
}
});
PriorityQueue<Recogonition> pq = new PriorityQueue<Recogonition>(3, new Comparator<Recogonition>() {
@Override
public int compare(Recogonition o1, Recogonition o2) {
return Float.compare(o1.getConfidence(),o2.getConfidence());
}
});
for(int i= 0 ;i < probs.length;i++) {
pq.add(new Recogonition(""+i,labels[i],probs[i]));
}
ArrayList<Recogonition> finalResult = new ArrayList<>();
for(int i =0;i <=3;i++) {
finalResult.add(pq.poll());
System.out.println("__________"+pq.poll());
}
return finalResult;
}
public void close() {
interpreter.close();
}
}