Skip to content

Commit

Permalink
Add files
Browse files Browse the repository at this point in the history
  • Loading branch information
pwochner authored Sep 23, 2022
0 parents commit f46f481
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 nbarlowATI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Empty file added README.md
Empty file.
1 change: 1 addition & 0 deletions catdog_classification_model/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# from .model import
35 changes: 35 additions & 0 deletions catdog_classification_model/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os
import requests
import torch
from torchvision import transforms
import numpy as np

CLASS_LABELS = ["cat", "dog"]

class CatDogClassifier:
def __init__(self):
filename = "pet_conv_net_model.pth"
if not os.path.exists(filename):
model_path = os.path.join("https://connectionsworkshop.blob.core.windows.net/pets", filename)
r = requests.get(model_path)
with open(filename, "wb") as outfile:
outfile.write(r.content)
# self.model = tf.keras.models.load_model(filename)
self.model = torch.load(filename)


def predict(self, input):
# transform input image (as required by model)
transform_input = transforms.Compose([ transforms.Resize((256,256)), transforms.Normalize(mean=[ 82.18, 139.30, 140.27],std=[49.40, 35.24, 37.86],) ])
input = input[:,:,0:3] # make sure we have only 3 channels
input = np.transpose(input, (2, 0, 1))
input = torch.from_numpy(input).type(torch.float32),
input = transform_input(input)

# make prediction
prediction = self.model(input) # TODO: is it model.predict() for tf?
prediction = prediction.detach().numpy()
max_val = np.max(prediction)
max_ind = np.argmax(prediction)
return f"{CLASS_LABELS[max_ind]}"

3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
torch
torchvision
numpy
19 changes: 19 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python
from setuptools import find_packages, setup

requirements = []
with open("requirements.txt") as f:
for line in f:
stripped = line.split("#")[0].strip()
if len(stripped) > 0:
requirements.append(stripped)

setup(
name="catdog_classification_model",
version="0.0.1",
description="scivision plugin, using a cat/dog classifier CNN model",
url="https://github.com/pwochner/catdog_classification_model",
packages=find_packages(),
install_requires=requirements,
python_requires=">=3.7",
)

0 comments on commit f46f481

Please sign in to comment.