forked from falloutdurham/beginners-pytorch-deep-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request falloutdurham#47 from MarcusFra/ch08_catfish_new
Ch8 Change catfish_server.py & add/change bash scripts
- Loading branch information
Showing
17 changed files
with
188 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
#run-model-service.sh | ||
|
||
curl http://127.0.0.1:8080/predict\?image_url\=https://upload.wikimedia.org/wikipedia/commons/thumb/3/36/A_domestic_shorthair_tortie-tabby_cat.jpg/412px-A_domestic_shorthair_tortie-tabby_cat.jpg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env bash | ||
#run-flask-server.sh | ||
|
||
FLASK_APP=catfish_server.py FLASK_RUN_PORT=8080 flask run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env bash | ||
#run-waitress-server.sh | ||
|
||
waitress-serve --call 'catfish_server:create_app' |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import torch.nn as nn | ||
from torchvision import models | ||
|
||
CatfishClasses = ["cat","fish"] | ||
|
||
CatfishModel = models.resnet50() | ||
CatfishModel.fc = nn.Sequential(nn.Linear(CatfishModel.fc.in_features,500), | ||
nn.ReLU(), | ||
nn.Dropout(), nn.Linear(500,2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import os | ||
import requests | ||
import torch | ||
from flask import Flask, jsonify, request | ||
from io import BytesIO | ||
from PIL import Image | ||
from shutil import copyfileobj | ||
from tempfile import NamedTemporaryFile | ||
from torchvision import transforms | ||
from urllib.request import urlopen | ||
|
||
from catfish_model import CatfishModel, CatfishClasses | ||
|
||
|
||
def load_model(): | ||
m = CatfishModel | ||
if "CATFISH_MODEL_LOCATION" in os.environ: | ||
parameter_url = os.environ["CATFISH_MODEL_LOCATION"] | ||
print(f"downloading {parameter_url}") | ||
with urlopen(parameter_url) as fsrc, NamedTemporaryFile() as fdst: | ||
copyfileobj(fsrc, fdst) | ||
m.load_state_dict(torch.load(fdst, map_location="cpu")) | ||
m.eval() | ||
return m | ||
|
||
|
||
model = load_model() | ||
|
||
img_transforms = transforms.Compose([ | ||
transforms.Resize((224,224)), | ||
transforms.ToTensor(), | ||
transforms.Normalize(mean=[0.485, 0.456, 0.406], | ||
std=[0.229, 0.224, 0.225]) | ||
]) | ||
|
||
def create_app(): | ||
app = Flask(__name__) | ||
|
||
@app.route("/") | ||
def status(): | ||
return jsonify({"status": "ok"}) | ||
|
||
@app.route("/predict", methods=['GET', 'POST']) | ||
def predict(): | ||
if request.method == 'POST': | ||
img_url = request.form.image_url | ||
else: | ||
img_url = request.args.get('image_url', '') | ||
|
||
response = requests.get(img_url) | ||
img = Image.open(BytesIO(response.content)) | ||
img_tensor = img_transforms(img).unsqueeze(0) | ||
prediction = model(img_tensor) | ||
predicted_class = CatfishClasses[torch.argmax(prediction)] | ||
return jsonify({"image": img_url, "prediction": predicted_class}) | ||
|
||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
#get-prediction.sh | ||
|
||
curl http://127.0.0.1:5000/predict?image_url=https://upload.wikimedia.org/wikipedia/commons/thumb/3/36/A_domestic_shorthair_tortie-tabby_cat.jpg/412px-A_domestic_shorthair_tortie-tabby_cat.jpg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
#run-docker.sh | ||
|
||
docker build -t catfish-service . | ||
docker run -d -p 5000:5000 --env CATFISH_MODEL_LOCATION=[URL] catfish-service:latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
FROM continuumio/miniconda3:latest | ||
|
||
ARG model_parameter_file=catfishweights.pth | ||
ARG port=5000 | ||
|
||
ENV CATFISH_PORT=$port | ||
ENV CATFISH_MODEL_LOCATION=/app/$model_parameter_file | ||
|
||
RUN conda install -y flask \ | ||
&& conda install -c pytorch torchvision \ | ||
&& conda install waitress | ||
RUN mkdir -p /app | ||
|
||
COPY ./catfish_model.py /app | ||
COPY ./catfish_server.py /app | ||
COPY ./$model_parameter_file /app/ | ||
COPY ./run-model-service.sh / | ||
|
||
EXPOSE $port | ||
|
||
ENTRYPOINT ["/run-model-service.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import torch.nn as nn | ||
from torchvision import models | ||
|
||
CatfishClasses = ["cat","fish"] | ||
|
||
CatfishModel = models.resnet50() | ||
CatfishModel.fc = nn.Sequential(nn.Linear(CatfishModel.fc.in_features,500), | ||
nn.ReLU(), | ||
nn.Dropout(), nn.Linear(500,2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import os | ||
import requests | ||
import torch | ||
from flask import Flask, jsonify, request | ||
from io import BytesIO | ||
from PIL import Image | ||
from torchvision import transforms | ||
|
||
from catfish_model import CatfishModel, CatfishClasses | ||
|
||
|
||
def load_model(): | ||
location = os.environ["CATFISH_MODEL_LOCATION"] | ||
m = CatfishModel | ||
m.load_state_dict(torch.load(location, map_location="cpu")) | ||
m.eval() | ||
return m | ||
|
||
|
||
model = load_model() | ||
|
||
img_transforms = transforms.Compose([ | ||
transforms.Resize((224,224)), | ||
transforms.ToTensor(), | ||
transforms.Normalize(mean=[0.485, 0.456, 0.406], | ||
std=[0.229, 0.224, 0.225] ) | ||
]) | ||
|
||
def create_app(): | ||
app = Flask(__name__) | ||
|
||
@app.route("/") | ||
def status(): | ||
return jsonify({"status": "ok"}) | ||
|
||
@app.route("/predict", methods=['GET', 'POST']) | ||
def predict(): | ||
if request.method == 'POST': | ||
img_url = request.form.image_url | ||
else: | ||
img_url = request.args.get('image_url', '') | ||
|
||
response = requests.get(img_url) | ||
img = Image.open(BytesIO(response.content)) | ||
img_tensor = img_transforms(img).unsqueeze(0) | ||
prediction = model(img_tensor) | ||
predicted_class = CatfishClasses[torch.argmax(prediction)] | ||
return jsonify({"image": img_url, "prediction": predicted_class}) | ||
|
||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
#get-prediction.sh | ||
|
||
curl http://127.0.0.1:5000/predict?image_url=https://upload.wikimedia.org/wikipedia/commons/thumb/3/36/A_domestic_shorthair_tortie-tabby_cat.jpg/412px-A_domestic_shorthair_tortie-tabby_cat.jpg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
#run-docker.sh | ||
|
||
docker build -t catfish-service . | ||
docker run -d -p 5000:5000 catfish-service:latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
#run-model-service.sh | ||
|
||
cd /app | ||
waitress-serve --port ${CATFISH_PORT} --call 'catfish_server:create_app' |
This file was deleted.
Oops, something went wrong.