-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* move configuration to classes * Code style and formatting changes. * update documentation
- Loading branch information
Showing
32 changed files
with
785 additions
and
875 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
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
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,30 @@ | ||
import os | ||
|
||
base_dir = os.path.dirname(__file__) # only for default config | ||
|
||
|
||
class Default: | ||
"""Default settings for the Flask app. | ||
The Flask app uses these settings if no custom settings are defined. You | ||
can define custom settings by creating a Python module, defining global | ||
variables in that module, and setting the environment variable | ||
`PICASSO_SETTINGS` to the path to that module. | ||
If `PICASSO_SETTINGS` is not set, or if any particular setting is not | ||
defined in the indicated module, then the Flask app uses these default | ||
settings. | ||
""" | ||
# :obj:`str`: filepath of the module containing the model to run | ||
MODEL_CLS_PATH = os.path.join( | ||
base_dir, 'examples', 'keras', 'model.py') | ||
|
||
# :obj:`str`: name of model class | ||
MODEL_CLS_NAME = 'KerasMNISTModel' | ||
|
||
# :obj:`dict`: dictionary of args to pass to the `load` method of the | ||
# model instance. | ||
MODEL_LOAD_ARGS = { | ||
'data_dir': os.path.join(base_dir, 'examples', 'keras', 'data-volume'), | ||
} |
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 |
---|---|---|
@@ -1,12 +1,15 @@ | ||
# Note: By default, Flask doesn't know that this file exists. If you want | ||
# Flask to load the settings you specify here, you must set the environment | ||
# variable `PICASSO_SETTINGS` to point to this file. E.g.: | ||
# | ||
# export PICASSO_SETTINGS=/path/to/examples/keras-vgg16/config.py | ||
# | ||
import os | ||
|
||
base_dir = os.path.dirname(os.path.abspath(__file__)) | ||
|
||
BACKEND_ML = 'keras' | ||
BACKEND_PREPROCESSOR_NAME = 'preprocess' | ||
BACKEND_PREPROCESSOR_PATH = os.path.join(base_dir, 'util.py') | ||
BACKEND_POSTPROCESSOR_NAME = 'postprocess' | ||
BACKEND_POSTPROCESSOR_PATH = os.path.join(base_dir, 'util.py') | ||
BACKEND_PROB_DECODER_NAME = 'prob_decode' | ||
BACKEND_PROB_DECODER_PATH = os.path.join(base_dir, 'util.py') | ||
DATA_DIR = os.path.join(base_dir, 'data-volume') | ||
MODEL_CLS_PATH = os.path.join(base_dir, 'model.py') | ||
MODEL_CLS_NAME = 'KerasVGG16Model' | ||
MODEL_LOAD_ARGS = { | ||
'data_dir': os.path.join(base_dir, 'data-volume'), | ||
} |
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,48 @@ | ||
from keras.applications import imagenet_utils | ||
import numpy as np | ||
from PIL import Image | ||
|
||
from picasso.models.keras import KerasModel | ||
|
||
VGG16_DIM = (224, 224, 3) | ||
|
||
|
||
class KerasVGG16Model(KerasModel): | ||
|
||
def preprocess(self, raw_inputs): | ||
""" | ||
Args: | ||
raw_inputs (list of Images): a list of PIL Image objects | ||
Returns: | ||
array (float32): num images * height * width * num channels | ||
""" | ||
image_arrays = [] | ||
for raw_im in raw_inputs: | ||
im = raw_im.resize(VGG16_DIM[:2], Image.ANTIALIAS) | ||
im = im.convert('RGB') | ||
arr = np.array(im).astype('float32') | ||
image_arrays.append(arr) | ||
|
||
all_raw_inputs = np.array(image_arrays) | ||
return imagenet_utils.preprocess_input(all_raw_inputs) | ||
|
||
def decode_prob(self, class_probabilities): | ||
r = imagenet_utils.decode_predictions(class_probabilities, | ||
top=self.top_probs) | ||
results = [ | ||
[{'code': entry[0], | ||
'name': entry[1], | ||
'prob': '{:.3f}'.format(entry[2])} | ||
for entry in row] | ||
for row in r | ||
] | ||
classes = imagenet_utils.CLASS_INDEX | ||
class_keys = list(classes.keys()) | ||
class_values = list(classes.values()) | ||
|
||
for result in results: | ||
for entry in result: | ||
entry['index'] = int( | ||
class_keys[class_values.index([entry['code'], | ||
entry['name']])]) | ||
return results |
Oops, something went wrong.