Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions picasso/examples/keras-vgg16/config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# 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')
MODEL_CLS_PATH = os.path.join(base_dir, 'model.py')
MODEL_CLS_NAME = 'KerasVGG16Model'
DATA_DIR = os.path.join(base_dir, 'data-volume')
44 changes: 44 additions & 0 deletions picasso/examples/keras-vgg16/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from keras.applications.imagenet_utils import (decode_predictions,
preprocess_input)
import keras.applications.imagenet_utils
import numpy as np
from PIL import Image

from picasso.ml_frameworks.keras.model import KerasModel


VGG16_DIM = (224, 224, 3)


class KerasVGG16Model(KerasModel):

def preprocess(self, targets):
image_arrays = []
for target in targets:
im = target.resize(VGG16_DIM[:2], Image.ANTIALIAS)
im = im.convert('RGB')
arr = np.array(im).astype('float32')
image_arrays.append(arr)

all_targets = np.array(image_arrays)
return preprocess_input(all_targets)

def decode_prob(self, probability_array):
r = decode_predictions(probability_array, 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 = keras.applications.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
53 changes: 0 additions & 53 deletions picasso/examples/keras-vgg16/util.py

This file was deleted.

18 changes: 6 additions & 12 deletions picasso/examples/keras/config.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
# Note: this settings file duplicates the default settings in the top-level
# file `settings.py`. If you want to modify settings here, you must export the
# path to this file:
# 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/picasso/picasso/examples/keras/config.py
# export PICASSO_SETTINGS=/path/to/examples/keras/config.py
#
# otherwise, these settings will not be loaded.
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')
MODEL_CLS_PATH = os.path.join(base_dir, 'model.py')
MODEL_CLS_NAME = 'KerasMNISTModel'
DATA_DIR = os.path.join(base_dir, 'data-volume')
30 changes: 30 additions & 0 deletions picasso/examples/keras/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import numpy as np
from PIL import Image

from picasso.ml_frameworks.keras.model import KerasModel


MNIST_DIM = (28, 28)


class KerasMNISTModel(KerasModel):

def preprocess(self, raw_inputs):
"""Convert images into the format required by our model.

Our model requires that inputs be grayscale (mode 'L'), be resized to
`MNIST_DIM`, and be represented as float32 numpy arrays in range
[0, 1].

"""
image_arrays = []
for raw_im in raw_inputs:
im = raw_im.convert('L')
im = im.resize(MNIST_DIM, Image.ANTIALIAS)
arr = np.array(im)
image_arrays.append(arr)

inputs = np.array(image_arrays)
return inputs.reshape(len(inputs),
MNIST_DIM[0],
MNIST_DIM[1], 1).astype('float32') / 255
90 changes: 0 additions & 90 deletions picasso/examples/keras/util.py

This file was deleted.

17 changes: 8 additions & 9 deletions picasso/examples/tensorflow/config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# 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/tensorflow/config.py
#
import os

base_dir = os.path.dirname(os.path.abspath(__file__))

BACKEND_ML = 'tensorflow'
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')
BACKEND_TF_PREDICT_VAR = 'Softmax:0'
BACKEND_TF_INPUT_VAR = 'convolution2d_input_1:0'
MODEL_CLS_PATH = os.path.join(base_dir, 'model.py')
MODEL_CLS_NAME = 'TensorflowMNISTModel'
DATA_DIR = os.path.join(base_dir, 'data-volume')
34 changes: 34 additions & 0 deletions picasso/examples/tensorflow/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import numpy as np
from PIL import Image

from picasso.ml_frameworks.tensorflow.model import TFModel


MNIST_DIM = (28, 28)


class TensorflowMNISTModel(TFModel):

TF_INPUT_VAR = 'convolution2d_input_1:0'

TF_PREDICT_VAR = 'Softmax:0'

def preprocess(self, raw_inputs):
"""Convert images into the format required by our model.

Our model requires that inputs be grayscale (mode 'L'), be resized to
`MNIST_DIM`, and be represented as float32 numpy arrays in range
[0, 1].

"""
image_arrays = []
for raw_im in raw_inputs:
im = raw_im.convert('L')
im = im.resize(MNIST_DIM, Image.ANTIALIAS)
arr = np.array(im)
image_arrays.append(arr)

inputs = np.array(image_arrays)
return inputs.reshape(len(inputs),
MNIST_DIM[0],
MNIST_DIM[1], 1).astype('float32') / 255
48 changes: 0 additions & 48 deletions picasso/examples/tensorflow/util.py

This file was deleted.

Loading