SQL Server Save keras model to database

46scxncf  于 12个月前  发布在  其他
关注(0)|答案(4)|浏览(93)

I created a keras model (tensorflow) and want to store it in my MS SQL Server database. What is the best way to do that? pyodbc.Binary(model) throws an error. I would prefer a way without storing the model in the file system first.

Thanks for any help

v440hwme

v440hwme1#

It seems that there is no clean solution to directly store a model incl. weights into the database. I decided to store the model as h5 file in the filesystem and upload it from there into the database as a backup. For predictions I load anyway the model from the filesystem as it is much faster than getting it from the database for each prediction.

7kqas0il

7kqas0il2#

The best approach would be to save it as a file in the system and just save the path in the database. This technique is usually used to store large files like images since databases usually struggle with them.

jslywgbw

jslywgbw3#

Well this answer comes a little too late but since there are no useful answers online, based on my intensive research, I thought I'll post this insight. Maybe some will find it helpful.

I have not tried it with a keras model, but this approach worked with a pytorch model. But I can imagine that it should work for keras or tensorflow as well.

Step 1: load the model and convert to a bytes buffer

model = torch.load(r'model.pth')

buffer = io.BytesIO()
torch.save(model, buffer)
model_data = buffer.getvalue()

Step 2: create a table in your SQL Server database either in SQL directly or using ORM (I did it using Python and Flask SQL Alchemy). The column data type (where the model will be saved) should be VARBINARY(MAX) (or db.PickleType using Flask-SQL-Alchemy).

connection_string = 'DRIVER={SQL Server};Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False' 
connection = pyodbc.connect(connection_string)
cursor = connection.cursor()

query="INSERT INTO Models (Model) VALUES (?)"
cursor.execute("SET IDENTITY_INSERT Models ON")
cursor.execute(query, model_data)
cursor.execute("SET IDENTITY_INSERT Models OFF")

connection.commit()

#to get the model from the db:
query="SELECT Model FROM Models"
cursor.execute(query)
row = cursor.fetchone()
model_data = row.Model

buffer = io.BytesIO(model_data)
loaded_model = torch.load(buffer)

cursor.close()
connection.close()

TA-DA

pxq42qpu

pxq42qpu4#

Well, you can save a model as a JSON like:

json_string = model.to_json()

And then cast it to string and save normally to the BD.

Or use native MS SQL solution for JSON fields explained here .

相关问题