Serving pytorch models on an API in one line.
Project description
torch-deploy
Installation
To install:
pip install pytorch-deploy
You also have to install torch
and torchvision
. You can do so here.
Usage
Deploying a pretrained ResNet-18:
import torch
import torchvision.models as models
from torch_deploy import deploy
resnet18 = models.resnet18(pretrained=True)
resnet18.eval()
deploy(resnet18, pre=torch.tensor)
The default host and port is 0.0.0.0:8000.
Endpoints
You can access the docs for the endpoints at "host:port/docs" after running python server.py
.
/predict
Request body: application/json
Response body: application/json
Here's an example of how to use to use the /predict endpoint.
import requests
from PIL import Image
import numpy as np
from torchvision import transforms
im = Image.open('palm.jpg')
resize = transforms.Resize(224)
to_tensor = transforms.ToTensor()
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
tensor = normalize(to_tensor(resize(im))).unsqueeze(0)
body = {"inputs": tensor.tolist()}
r = requests.post("http://127.0.0.1:8000/predict", json=body)
response = r.json()
output = np.array(response["output"])
Note that you need to send the model input in the request JSON body under the field "inputs". If you want to send a tensor or a numpy array in the request, you need to turn it into a list first.
The output of the model will be in the response JSON body under the "output" field.
Sample response format:
response = {"output": (your numpy array as a list here)}
/predict_image
Request body: multipart/form-data
Response body: application/json
Here's an example of how to use to use the /predict_image endpoint.
import requests
import numpy as np
filename = "../palm.jpg"
files = {'file': open(filename, "rb")}
r = requests.post("http://127.0.0.1:8000/predict_image", files=files)
response = r.json()
output = np.array(response["output"])
print(np.argmax(output))
The file is uploaded with the content type "multipart/form-data". This requires minimal work on the client side and is compatible with standard file upload requests.
Documentation
torch_deploy.deploy(
model: nn.Module,
pre: Union[List[Callable], Callable] = None,
post: Union[List[Callable], Callable] = None,
host: str = "0.0.0.0",
port: int = 8000,
ssl_keyfile: str = None,
ssl_certfile: str = None,
ssl_ca_certs: str = None,
logdir: str = "./deploy_logs/",
inference_fn: str = None
)
Easily converts a pytorch model to API for production usage.
model
: A PyTorch model which subclasses nn.Module and is callable. Model used for the API.pre
: A function or list of functions to be applied to the input.post
: Function or list of functions applied to model output before being sent as a response.host
: The address for serving the model.port
: The port for serving the model.ssl_keyfile
,ssl_certfile
,ssl_ca_certs
: SSL configurations that are passed to uvicornlogfile
: Filename to create a file that stores date, ip address, and size of input for each access of the API. IfNone
, no file will be created.inference_fn
: Name of the method of the model that should be called for the inputs. IfNone
, the model itself will be called (Ifmodel
is ann.Module
then it's equivalent to callingmodel.forward(inputs)
).
Examples
There are some sample code in the examples/ directory.
Currently In Progress
Still working on an OAuth2 login system that requires correct user credentials to use torch-deploy.
Dependencies
torch, torchvision, fastapi, uvicorn, requests, numpy, pydantic
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytorch-deploy-0.0.6.tar.gz
.
File metadata
- Download URL: pytorch-deploy-0.0.6.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55ed3fbbe060d27300370068d47866b0afcd11c0bff815d11d5ef7263dc4f2e7 |
|
MD5 | 29730c8e9c38b51b51f963b38031aa12 |
|
BLAKE2b-256 | ee5945870e57c51605e4e7cc94ec6d0d3a7a7e767ac7968dd9426f1011db74d5 |
File details
Details for the file pytorch_deploy-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: pytorch_deploy-0.0.6-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a2394464d693c3422994b62b7df7fff1cbce2d28ac67a38f876920efd6d8675 |
|
MD5 | 6ea2e6ac3172d0956f6cfe7d7549b633 |
|
BLAKE2b-256 | 9a9db2c3dd38bb7b54c97858762641f264e50bcd308616215be0eab16ac26224 |