Skip to content

Commit

Permalink
Merge pull request falloutdurham#47 from MarcusFra/ch08_catfish_new
Browse files Browse the repository at this point in the history
Ch8 Change catfish_server.py & add/change bash scripts
  • Loading branch information
falloutdurham authored Oct 6, 2020
2 parents 681ba2f + c9866de commit a3ee0fe
Show file tree
Hide file tree
Showing 17 changed files with 188 additions and 52 deletions.
25 changes: 6 additions & 19 deletions chapter8/catfish/catfish_server.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
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
from urllib.request import urlopen
from shutil import copyfileobj
from tempfile import NamedTemporaryFile

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))
m.load_state_dict(torch.load(location))
return m
m = CatfishModel
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] )
std=[0.229, 0.224, 0.225])
])

def create_app():
app = Flask(__name__)


@app.route("/")
def status():
Expand All @@ -53,6 +42,4 @@ def predict():
predicted_class = CatfishClasses[torch.argmax(prediction)]
return jsonify({"image": img_url, "prediction": predicted_class})

return app
if __name__ == '__main__':
app.run(host=os.environ["CATFISH_HOST"], port=os.environ["CATFISH_PORT"])
return app
4 changes: 4 additions & 0 deletions chapter8/catfish/get-prediction.sh
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
4 changes: 4 additions & 0 deletions chapter8/catfish/run-flask-server.sh
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
4 changes: 4 additions & 0 deletions chapter8/catfish/run-waitress-server.sh
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.
9 changes: 9 additions & 0 deletions chapter8/catfish_docker_cloud/catfish_model.py
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))
57 changes: 57 additions & 0 deletions chapter8/catfish_docker_cloud/catfish_server.py
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
4 changes: 4 additions & 0 deletions chapter8/catfish_docker_cloud/get-prediction.sh
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
5 changes: 5 additions & 0 deletions chapter8/catfish_docker_cloud/run-docker.sh
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#run-model-service.sh

cd /app
waitress-serve --port ${CATFISH_PORT} --call 'catfish_server:create_app'
waitress-serve --port ${CATFISH_PORT} --call 'catfish_server:create_app'
21 changes: 21 additions & 0 deletions chapter8/catfish_docker_local/Dockerfile
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"]
9 changes: 9 additions & 0 deletions chapter8/catfish_docker_local/catfish_model.py
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))
50 changes: 50 additions & 0 deletions chapter8/catfish_docker_local/catfish_server.py
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
4 changes: 4 additions & 0 deletions chapter8/catfish_docker_local/get-prediction.sh
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
5 changes: 5 additions & 0 deletions chapter8/catfish_docker_local/run-docker.sh
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
5 changes: 5 additions & 0 deletions chapter8/catfish_docker_local/run-model-service.sh
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'
32 changes: 0 additions & 32 deletions chapter8/server.py

This file was deleted.

0 comments on commit a3ee0fe

Please sign in to comment.