Ejemplo n.º 1
0
import runway
from runway.data_types import category, vector, image
from your_code import model

@runway.setup
def setup():
    return model()

sample_inputs= {
    "z": vector(length=512, description="The seed used to generate an output image."),
    "category": category(choices=["day", "night"])
}

sample_outputs = {
    "image": image(width=1024, height=1024)
}

# Descriptions are used to document each data type and `@runway.command()`; Their values appear
# in the app as tooltips. Writing detailed descriptions for each model option goes a long way
# towards helping users learn how to interact with your model. Write descriptions as full
# sentences.
sample_description = "Generate a new image based on a z-vector and an output style: Day or night."
@runway.command("sample",
                inputs=sample_inputs,
                outputs=sample_outputs,
                description=sample_description)
def sample(model, inputs):
    # The parameters passed to a function decorated by @runway.command() are:
    #   1. The return value of a function wrapped by @runway.setup(), usually a model.
    #   2. The inputs sent with the HTTP request to the /<command_name> endpoint,
    #      as defined by the inputs keyword argument delivered to @runway.command().
Ejemplo n.º 2
0
import runway
from runway.data_types import category, vector, image
from your_image_generation_model import big_model, little_model


# The setup() function runs once when the model is initialized, and will run
# again for each well formed HTTP POST request to http://localhost:9000/setup.
@runway.setup(options={'model_size': category(choices=['big', 'little'])})
def setup(opts):
    if opts['model_size'] == 'big':
        return big_model()
    else:
        return little_model()


inputs = {'noise_vector': vector(length=128, description='A random seed.')}
outputs = {'image': image(width=512, height=512)}


# The @runway.command() decorator is used to create interfaces to call functions
# remotely via an HTTP endpoint. This lets you send data to, or get data from,
# your model. Each command creates an HTTP route that the Runway app will use
# to communicate with your model (e.g. POST /generate). Multiple commands
# can be defined for the same model.
@runway.command('generate',
                inputs=inputs,
                outputs=outputs,
                description='Generate an image.')
def generate(model, input_args):
    # Functions wrapped by @runway.command() receive two arguments:
    # 1. Whatever is returned by a function wrapped by @runway.setup(),
import runway
from runway.data_types import category, vector, image
from your_code import model


@runway.setup
def setup():
    return model()


sample_inputs = {
    "z": vector(length=512),
    "category": category(choices=["day", "night"])
}

sample_outputs = {"image": image(width=1024, height=1024)}


@runway.command("sample", inputs=sample_inputs, outputs=sample_outputs)
def sample(model, inputs):
    # The parameters passed to a function decorated by @runway.command() are:
    #   1. The return value of a function wrapped by @runway.setup(), usually a model
    #   2. The inputs sent with the HTTP request to the /<command_name> endpoint,
    #      as defined by the inputs keyword argument delivered to @runway.command().
    img = model.sample(z=inputs["z"], category=inputs["category"])
    return {"image": img}
Ejemplo n.º 4
0
def test_meta(capsys):

    rw = RunwayModel()

    @rw.setup(options={'initialization_array': array(item_type=text)})
    def setup(opts):
        pass

    kwargs_1 = {
        'inputs': {
            'image': image,
            'vector': vector(length=5)
        },
        'outputs': {
            'label': text
        }
    }

    @rw.command('command_1', **kwargs_1)
    def command_1(opts):
        pass

    kwargs_2 = {
        'description': 'This command is used for testing.',
        'inputs': {
            'any': any_type,
            'file': file
        },
        'outputs': {
            'number': number(min=10, max=100)
        }
    }

    @rw.command('command_2', **kwargs_2)
    def command_2(opts):
        pass

    expected_manifest = {
        'options': [{
            'minLength': 0,
            'type': 'array',
            'name': 'initialization_array',
            'description': None,
            'itemType': {
                'default': '',
                'minLength': 0,
                'type': 'text',
                'name': 'text_array_item',
                'description': None
            }
        }],
        'commands': [{
            'name':
            'command_2',
            'description':
            'This command is used for testing.',
            'inputs': [
                {
                    'type': 'any',
                    'name': 'any',
                    'description': None,
                },
                {
                    'type': 'file',
                    'name': 'file',
                    'description': None,
                },
            ],
            'outputs': [
                {
                    'name': 'number',
                    'min': 10,
                    'default': 10,
                    'max': 100,
                    'type': 'number',
                    'description': None
                },
            ]
        }, {
            'name':
            'command_1',
            'description':
            None,
            'inputs': [
                {
                    'channels': 3,
                    'type': 'image',
                    'name': 'image',
                    'description': None,
                    'defaultOutputFormat': 'JPEG'
                },
                {
                    'samplingMean': 0,
                    'length': 5,
                    'type': 'vector',
                    'name': 'vector',
                    'samplingStd': 1,
                    'default': None,
                    'description': None
                },
            ],
            'outputs': [{
                'default': '',
                'minLength': 0,
                'type': 'text',
                'name': 'label',
                'description': None
            }]
        }]
    }

    # RW_META should not be set during testing
    os.environ['RW_META'] = '1'

    rw.run(debug=True,
           model_options={'initialization_array': ['one', 'two', 'three']})
    std = capsys.readouterr()
    manifest = json.loads(std.out.strip('\n'))

    # DeepDiff is required here because Python2 handles stdin encoding strangely
    # and because dict order is not guaranteed in Python2. I ran up a tree
    # trying to get this comparison working without relying on a lib, but
    # ultimately it was just wasting my time.
    diff = DeepDiff(manifest, expected_manifest, ignore_order=True)
    assert len(diff.keys()) == 0
    assert std.err == ''

    os.environ['RW_META'] = '0'
Ejemplo n.º 5
0
most_likely_description = "Most likely class of corresponding face"
detect_and_classify_description = (
    "Detect faces in given image and return their bounding boxes sorted "
    "largest to smallest along with probabilities of each face's emotion "
    "classification and most likely class.\n Probabilities correspond to:"
    f" {', '.join(emotion_classifier.EMOTIONS)}")


@runway.command(
    'detect_and_classify',
    inputs={'image': image},
    outputs={
        'faces':
        array(item_type=image_bounding_box, description=faces_description),
        'probabilities':
        array(item_type=vector(length=len(emotion_classifier.EMOTIONS)),
              description=probabilities_description),
        'most_likely_classes':
        array(item_type=text, description=most_likely_description)
    },
    description=detect_and_classify_description)
def detect_and_classify(model, inputs):
    """Detect and classify faces in given image.

    Returns bounding box of each face sorted largest to smallest along with
    probabilities of each face's emotion classification and most likely class.

    probabilities correspond to emotion_classifier.EMOTIONS
    """
    image = inputs['image']
Ejemplo n.º 6
0
def random_sample(result_of_setup, args):
    # TODO: Come back, I think there is a bug here...
    # we should be returning a serialized version of the data, not a deserialized version...
    vec = vector(length=args["length"])
    rand = np.random.random_sample(args["length"])
    return {"vector": vec.deserialize(rand)}
Ejemplo n.º 7
0
import runway
from runway.data_types import vector, number
import numpy as np

inputs = {"length": number(min=1)}
outputs = {"vector": vector(length=512)}


@runway.command("random_sample", inputs=inputs, outputs=outputs)
def random_sample(result_of_setup, args):
    # TODO: Come back, I think there is a bug here...
    # we should be returning a serialized version of the data, not a deserialized version...
    vec = vector(length=args["length"])
    rand = np.random.random_sample(args["length"])
    return {"vector": vec.deserialize(rand)}


runway.run()

# curl -H "content-type: application/json" -d '{"length": 128}' http://localhost:8000/random_sample
Ejemplo n.º 8
0
    with dnnlib.util.open_url(URL_FFHQ) as f:
        generator_network, discriminator_network, Gs_network = pickle.load(f)
    generator = Generator(Gs_network, 1, randomize_noise=False)
    perceptual_model = PerceptualModel(256, layer=9, batch_size=1)
    perceptual_model.build_perceptual_model(generator.generated_image)
    return perceptual_model, generator


INPUTS = {
    'reference': image,
    'iterations': number(default=500, min=1, max=10000),
    'learning_rate': number(default=1, step=0.01, min=0, max=3)
}


@runway.command('autoencode', inputs=INPUTS, outputs={'output': vector(512)})
def autoencode(model, inputs):
    perceptual_model, generator = model
    perceptual_model.set_reference_images([inputs['reference']])
    op = perceptual_model.optimize(generator.dlatent_variable,
                                   iterations=inputs['iterations'],
                                   learning_rate=inputs['learning_rate'])
    pbar = tqdm(op, leave=False, total=inputs['iterations'])
    print('Processing image...')
    for loss in pbar:
        pbar.set_description('Loss: %.2f' % loss)
    generated_images = generator.generate_images()
    generated_dlatents = generator.get_dlatents()
    generator.reset_dlatents()
    return generated_dlatents[0]
Ejemplo n.º 9
0

desc = """\
Infers embeddings for input. Returns an array with the lines of
text and an array with the corresponding embeddings for each line.
"""


@runway.command(name='embed',
                inputs={
                    'text': text(),
                    'tokenize_sentences': boolean(default=True)
                },
                outputs={
                    'sentences': array(item_type=text),
                    'embeddings': array(item_type=vector(length=512))
                },
                description=desc)
def embed(model, args):
    if args['tokenize_sentences']:
        sentences = sent_tokenize(args['text'])
    else:
        sentences = args['text'].split("\n")
    print('[EMBED] Embedding {} sentences'.format(len(sentences)))
    results = model.embed(sentences)
    return {'sentences': sentences, 'embeddings': results}


if __name__ == '__main__':
    runway.run(host='0.0.0.0', port=8000)
Ejemplo n.º 10
0
# class little_model(big_model):
#     pass


# The setup() function runs once when the model is initialized, and will run
# again for each well formed HTTP POST request to http://localhost:9000/setup.
@runway.setup(options={'model_size': category(choices=['big', 'little'])})
def setup(opts):
    if opts['model_size'] == 'big':
        return big_model()
    else:
        return little_model()


inputs = {'noise_vector': vector(length=128)}
outputs = {'image': image(width=512, height=512)}


# The @runway.command() decorator is used to create interfaces to call functions
# remotely via an HTTP endpoint. This lets you send data to, or get data from,
# your model. Each command creates an HTTP route that the Runway app will use
# to communicate with your model (e.g. POST /generate). Multiple commands
# can be defined for the same model.
@runway.command('generate', inputs=inputs, outputs=outputs)
def generate(model, input_args):
    # Functions wrapped by @runway.command() receive two arguments:
    # 1. Whatever is returned by a function wrapped by @runway.setup(),
    #    usually a model.
    # 2. The input arguments sent by the remote caller via HTTP. These values
    #    match the schema defined by inputs.