我正在研究一个需要在Android设备上运行的机器学习项目。我是机器学习和TensorFlow的完全新手,因此,过去几周以来,我一直很艰难。根据到目前为止的经验,我必须
我经历了许多教程(所有这些教程都适用于图像数据集),但是没有任何运气。 这是我的代码
from __future__ import absolute_import, division, print_function, unicode_literals
import glob
import os
from keras.models import Sequential, load_model
import numpy as np
import pandas as pd
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
import matplotlib.pyplot as plt
import keras as k
try:
%tensorflow_version 2.x
except Exception:
pass
import tensorflow as tf
from tensorflow import keras
#from tensorflow.contrib import lite
#from tensorflow.python.compiler.tensorrt import trt_convert as lite
df = pd.read_csv("kidney2.csv")
df.head()
df.shape
columns_to_retain = ["sg", "al", "sc", "hemo", "pcv", "wbcc", "rbcc", "htn", "classification"]
df = df.drop([col for col in df.columns if not col in columns_to_retain], axis=1)
df = df.dropna(axis=0)
for column in df.columns:
if df[column].dtype == np.number:
continue
df[column] = LabelEncoder().fit_transform(df[column])
df.head()
X = df.drop(["classification"], axis=1)
y = df["classification"]
x_scaler = MinMaxScaler()
x_scaler.fit(X)
column_names = X.columns
X[column_names] = x_scaler.transform(X)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size= 0.2, shuffle=True)
def create_model():
model = Sequential()
model.add(Dense(256, input_dim=len(X.columns),
kernel_initializer=k.initializers.random_normal(seed=13), activation="relu"))
model.add(Dense(1, activation="hard_sigmoid"))
model.compile(loss='binary_crossentropy',
optimizer='adam', metrics=['accuracy'])
return model
model=create_model()
model.summary()
#checkpoint_path='training_1/cp.ckp'
#checkpoint_dir=os.path.dirname(checkpoint_path)
#Create checkpoint callback
#cp_callback=tf.keras.callbacks.ModelCheckpoint(checkpoint_path,save_weights_only=True,verbose=1)
checkpoint_directory = "training_1"
checkpoint_prefix = os.path.join(checkpoint_directory, "ckpt")
#checkpoint = tf.train.Checkpoint(optimizer=optimizer, model=model)
#status = checkpoint.restore(tf.train.latest_checkpoint(checkpoint_directory))
#train_op = optimizer.minimize( ... )
#status.assert_consumed() # Optional sanity checks.
#with tf.compat.v1.Session() as session:
# Use the Session to restore variables, or initialize them if
# tf.train.latest_checkpoint returned None.
# status.initialize_or_restore(session)
# for _ in range(num_training_steps):
# session.run(train_op)
# checkpoint.save(file_prefix=checkpoint_prefix)
model=create_model()
history = model.fit(X_train, y_train, epochs=2000, batch_size=X_train.shape[0])
model.save("kidney_model.model")
model=create_model()
model.load_weights(checkpoint_directory)
我收到以下错误
OSError: Unable to open file (unable to open file: name = 'training_1', errno = 13, error message = 'Permission denied', flags = 0, o_flags = 0)
我还尝试通过使用直接转换为tflite
saved_model_dir='kidney_model.model'
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
但是我说错了
OSError: SavedModel file does not exist at: kidney_model.model/{saved_model.pbtxt|saved_model.pb}
有什么办法可以将该模型转换为tflite?
我完全迷路了。任何帮助都非常感谢。