Question

Most efficient way to load machine learning model (.h5) into web app?

I have trained a tensorflow model and saved it in the form of a .h5 file, about the size of 700mb. What’s the most efficient way to load this into the web app. I have tried storing it on digitalOcean space, but loading is not very efficient, takes about over 45 seconds. Any tips for speeding this up? Below is the code I used to load the model.

def load():
    session1 = session.Session()
    client = session1.client('s3',
                            region_name='nyc3',
                            endpoint_url=DO_SPACES_ENDPOINT,
                            aws_access_key_id=DO_SPACES_ACCESS_KEY,
                            aws_secret_access_key=DO_SPACES_SECRET_KEY)

    model_object = client.get_object(Bucket=BUCKET_NAME, Key=WEIGHTS_FILE)
    model_content = model_object['Body'].read()
    with tempfile.NamedTemporaryFile(suffix='.h5', delete=False) as fp:
        fp.write(model_content)
        model_path = fp.name
    return model_path

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Jeff Braunstein
DigitalOcean Employee
DigitalOcean Employee badge
May 2, 2023

Hi There, can you share where you are running this app relative to the bucket? Are they in the same datacenter?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel