Android E/ModelDownloadManager:模型与TFLite不兼容,且应用程序未升级,请勿下载
我尝试使用Firebase ML将我的模型部署到我的Android应用程序。我遵循了以下教程:。但该模型始终无法在应用程序中下载,在日志中我有以下错误:E/ModelDownloadManager:该模型与TFLite不兼容,且该应用程序未升级,请勿下载 我用于模型下载的代码是:Android E/ModelDownloadManager:模型与TFLite不兼容,且应用程序未升级,请勿下载,android,android-studio,tensorflow,tensorflow-lite,firebase-mlkit,Android,Android Studio,Tensorflow,Tensorflow Lite,Firebase Mlkit,我尝试使用Firebase ML将我的模型部署到我的Android应用程序。我遵循了以下教程:。但该模型始终无法在应用程序中下载,在日志中我有以下错误:E/ModelDownloadManager:该模型与TFLite不兼容,且该应用程序未升级,请勿下载 我用于模型下载的代码是: private fun setupClassifier() { configureRemoteConfig() remoteConfig.fetchAndActivate()
private fun setupClassifier() {
configureRemoteConfig()
remoteConfig.fetchAndActivate()
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val modelName = remoteConfig.getString("model_name")
val downloadTrace = firebasePerformance.newTrace("download_model")
downloadTrace.start()
downloadModel(modelName)
.addOnSuccessListener {
downloadTrace.stop()
}
} else {
showToast("Failed to fetch model name.")
}
}
}
private fun configureRemoteConfig() {
remoteConfig = Firebase.remoteConfig
val configSettings = remoteConfigSettings {
minimumFetchIntervalInSeconds = 3600
}
remoteConfig.setConfigSettingsAsync(configSettings)
}
private fun downloadModel(modelName: String): Task<Void> {
val remoteModel = FirebaseCustomRemoteModel.Builder(modelName).build()
val firebaseModelManager = FirebaseModelManager.getInstance()
return firebaseModelManager
.isModelDownloaded(remoteModel)
.continueWithTask { task ->
// Create update condition if model is already downloaded, otherwise create download
// condition.
val conditions = if (task.result != null && task.result == true) {
FirebaseModelDownloadConditions.Builder()
.requireWifi()
.build() // Update condition that requires wifi.
} else {
FirebaseModelDownloadConditions.Builder().build() // Download condition.
}
firebaseModelManager.download(remoteModel, conditions)
}
.addOnSuccessListener {
firebaseModelManager.getLatestModelFile(remoteModel)
.addOnCompleteListener {
val model = it.result
if (model == null) {
showToast("Failed to get model file.")
} else {
showToast("Downloaded remote model: $modelName")
tomatoDiseaseClassifier.initialize(model)
}
}
}
.addOnFailureListener {
showToast("Model download failed for $modelName, please check your connection.")
}
}
我也尝试过其他型号,但我得到了同样的错误
更新强>
I修复了该bug,问题是firebase ml模型解释器库已被弃用,必须用firebase ml modeldownloader库替换。()
train_datagen=ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
validation_split=0.2,
horizontal_flip=True)
test_datagen=ImageDataGenerator(rescale=1./255)
img_width,img_height =256,256
input_shape=(img_width,img_height,3)
batch_size =32
train_generator =train_datagen.flow_from_directory(train_dir,
target_size=(img_width,img_height),
batch_size=batch_size)
test_generator=test_datagen.flow_from_directory(test_dir,shuffle=True,
target_size=(img_width,img_height),
batch_size=batch_size)
validation_generator = train_datagen.flow_from_directory(
train_dir, # same directory as training data
target_size=(img_height, img_width),
batch_size=batch_size)
model = Sequential()
model.add(Conv2D(32, (5, 5),input_shape=input_shape,activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(Conv2D(32, (3, 3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(512,activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(128,activation='relu'))
model.add(Dense(num_classes,activation='softmax'))
model.summary()
opt=keras.optimizers.Adam(lr=0.001)
model.compile(optimizer=opt,loss='categorical_crossentropy',metrics=['accuracy'])
train=model.fit(train_generator,
epochs=15,
steps_per_epoch=train_generator.samples // batch_size,
validation_data=validation_generator,
validation_steps= validation_generator.samples// batch_size,verbose=1)
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tfmodel = converter.convert()
tflite_models_dir = pathlib.Path("./tmp/")
tflite_models_dir.mkdir(exist_ok=True, parents=True)
tflite_model_quant_file = tflite_models_dir/"tomato_disease_quant.tflite"
tflite_model_quant_file.write_bytes(tfmodel)