示例#1
0
def setup(opts):
checkpoint = opts['checkpoint'
use_gpu = True if torch.cuda.is_available() else False
# Load the model from the Pytorch Hub
model = torch.hub.load('facebookresearch/pytorch_GAN_zoo:hub',
                       'PGAN', model_name=checkpoint,
                       pretrained=True, useGPU=use_gpu)

return model
@runway.command('generate',
               inputs={ 'z': runway.vector(length=512, sampling_std=0.5)},
               outputs={ 'image': runway.image })
def generate(model, inputs):
# Generate ♾ infinite ♾ images
   z = inputs['z']
   latents = z.reshape((1, 559))
   latents = torch.from_numpy(latents)
                  
with torch.no_grad():

       generated_image = model.test(latents.float())
   generated_image = generated_image.clamp(min=-1, max=1)
   generated_image = ((generated_image + 1.0) * 255 / 2.0)
# Now generated_image contains our generated image!

return generated_image[0].permute(1, 2, 0).numpy().astype(np.uint8)

if __name__ == '__main__':
runway.run(port=5232)
示例#2
0
import numpy as np
from scipy.misc import toimage
from tqdm import tqdm
from pytorch_pretrained_biggan import (BigGAN, one_hot_from_names, truncated_noise_sample,
                                       save_as_images, display_in_terminal)

 @runway.setup(options={"checkpoint": runway.category(description="Pretrained checkpoints to use.",
                                      choices=['celebAHQ-512', 'celebAHQ-256', 'celeba'],
                                      default='celebAHQ-512')})
 def setup(opts):
   checkpoint = opts['checkpoint']
                          'PGAN', model_name=checkpoint,
 return model

 @runway.command('generate',
               inputs={ 'z': runway.vector(length=512, sampling_std=0.5)},
               outputs={ 'image': runway.image })
  def generate(model, inputs):
  # Generate ♾ infinite ♾ images
   z = inputs['z']
   latents = z.reshape((1, 559))
   latents = torch.from_numpy(latents)
-   noise, _ = model.buildNoiseData(1)
    with torch.no_grad():
       generated_image = model.test(latents.float())
   generated_image = generated_image.clamp(min=-1, max=1)
   generated_image = ((generated_image + 1.0) * 255 / 2.0)
    # Now generated_image contains our generated image! 🌞
   return generated_image[0].permute(1, 2, 0).numpy().astype(np.uint8)

 if __name__ == '__main__':
from keras.preprocessing import image
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
from keras.models import Model
import numpy as np
import runway

@runway.setup
def setup():
  base_model = VGG16(weights='imagenet')
  model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1').output)
  return model

@runway.command('extract_features', inputs={'image': runway.image}, outputs={'features': runway.vector(4096)})
def extract_features(model, inputs):
  x = np.array(inputs['image'].resize((224, 224)))
  x = np.expand_dims(x, axis=0)
  x = preprocess_input(x)
  features = model.predict(x)[0]
  return features.flatten()

if __name__ == "__main__":
  runway.run()
import lucid.optvis.param as param
import lucid.optvis.render as render
import lucid.optvis.transform as transform
import time
import runway
import random

@runway.setup(options={})
def setup(opts):
    model = models.InceptionV1()
    model.load_graphdef()
    return model


generate_inputs = {
    'z': runway.vector(length=1, sampling_std=0.5),
    'layer': runway.category(choices=["Mixed3a", "Mixed4a", "Mixed5a"]
    , default="Mixed4a"),
    'steps': runway.number(min=64, max=512, step=10, default=128)
}

@runway.command('generate', inputs=generate_inputs, outputs={'image': runway.image})
def convert(model, inputs):
    print(inputs['z'])
    print(inputs['layer'])
    num_neurons = {'mixed3a':255, 'mixed4a':507, 'mixed5a':831}
    start = time.time()

    #set up parameters
    layer = inputs['layer'].lower()
    neuron = int(np.clip(np.abs(np.float(inputs['z'])) * 1000, 0, num_neurons[layer]))
示例#5
0
    tflib.init_tf()
    with open(opts['checkpoint'], 'rb') as file:
        _G, _D, Gs = pickle.load(file, encoding='latin1')
    noise_vars = [
        var for name, var in Gs.components.synthesis.vars.items()
        if name.startswith('noise')
    ]
    rnd = np.random.RandomState()
    tflib.set_vars(
        {var: rnd.randn(*var.shape.as_list())
         for var in noise_vars})
    return Gs


generate_inputs = {
    'z': runway.vector(512, sampling_std=0.5),
    'truncation': runway.number(min=0, max=1, default=0.8, step=0.01)
}


@runway.command('generate',
                inputs=generate_inputs,
                outputs={'image': runway.image})
def convert(model, inputs):
    z = inputs['z']
    truncation = inputs['truncation']
    latents = z.reshape((1, 512))
    images = model.run(latents,
                       None,
                       truncation_psi=truncation,
                       randomize_noise=False,
    use_gpu = torch.cuda.is_available()
    config_file = "config.config_wake"
    params = argparse.Namespace(**importlib.import_module(config_file).params)
    model = BPEmbVaeSampler(lang=params.bpemb['lang'],
                            vs=params.bpemb['vs'],
                            dim=params.bpemb['dim'],
                            decode_from=model_path,
                            params=params,
                            cuda=use_gpu)
    return model


@runway.command('generate',
                inputs={
                    'z':
                    runway.vector(length=64),
                    'temperature':
                    runway.number(default=0.5, min=0.05, max=2.0, step=0.05)
                },
                outputs={'out': runway.text})
def generate(model, inputs):
    z = torch.from_numpy(inputs['z']).float().unsqueeze(0).to(model.device)
    temperature = inputs['temperature']
    with torch.no_grad():
        return model.sample(z, temperature)[0]


@runway.command('reconstruct',
                inputs={
                    'in':
                    runway.text,
    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True

    # init session
    saver = tf.train.Saver()
    sess = tf.Session(config=tfconfig)
    saver.restore(sess, ckpt.model_checkpoint_path)
    print('Restored from {}'.format(ckpt.model_checkpoint_path))

    return sess


@runway.command('caption',
                inputs={'image': runway.image},
                outputs={
                    'captions': runway.vector(12),
                    'scores': runway.vector(12),
                    'boxes': runway.vector(12)
                })
def caption(sess, inp):
    img = np.array(inp['image'])
    scores, boxes, captions = im_detect(sess, net, img, None, use_box_at=-1)
    pos_dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32,
                                                                copy=False)
    keep = nms(pos_dets, cfg.TEST.NMS)
    pos_dets = pos_dets[keep, :]
    pos_scores = scores[keep]
    pos_captions = [sentence(vocab, captions[idx]) for idx in keep]
    pos_boxes = boxes[keep, :]
    return dict(captions=np.array(pos_captions),
                scores=np.array(pos_scores),
from keras.applications.resnet50 import ResNet50
from keras.applications.resnet50 import preprocess_input
import numpy as np
import runway


@runway.setup
def setup():
    return ResNet50(weights='imagenet', include_top=False, pooling='avg')


@runway.command('extract_features',
                inputs={'image': runway.image},
                outputs={'features': runway.vector(2048)})
def extract_features(model, inputs):
    x = np.array(inputs['image'].resize((224, 224)))
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    features = model.predict(x)[0]
    return features.flatten()


if __name__ == "__main__":
    runway.run()
示例#9
0
    use_gpu = True if torch.cuda.is_available() else False
    config_file = "config.config_poetry_500k_sample"
    params = argparse.Namespace(**importlib.import_module(config_file).params)
    model = BPEmbVaeSampler(lang='en',
                            vs=10000,
                            dim=100,
                            decode_from=model_path,
                            params=params,
                            cuda=use_gpu)
    return model


@runway.command('generate',
                inputs={
                    'z':
                    runway.vector(length=32),
                    'temperature':
                    runway.number(default=0.5, min=0.05, max=2.0, step=0.05)
                },
                outputs={'out': runway.text})
def generate(model, inputs):
    z = torch.from_numpy(inputs['z']).float().unsqueeze(0)
    temperature = inputs['temperature']
    with torch.no_grad():
        return model.sample(z, temperature)[0]


@runway.command('reconstruct',
                inputs={
                    'in':
                    runway.text,
示例#10
0
@runway.setup(options={'checkpoint': runway.file(extension='.pkl')})
def setup(opts):
    global Gs
    tflib.init_tf()
    if opts['checkpoint'] is None:
        opts['checkpoint'] = 'checkpoints\\network-snapshot-000600.pkl'
    with open(opts['checkpoint'], 'rb') as file:
        _G, _D, Gs = pickle.load(file, encoding='latin1')
    noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]
    rnd = np.random.RandomState()
    tflib.set_vars({var: rnd.randn(*var.shape.as_list()) for var in noise_vars})
    return Gs


generate_inputs = {
    'z': runway.vector(256, sampling_std=0.5),
    'truncation': runway.number(min=0, max=1, default=0.8, step=0.01)
}

for i in range(4):
    for j in range(4):
        generate_inputs.update({'style_%i/%i' %(i, j): runway.number(min=0, max=1, default=0.5, step=0.01)})

@runway.command('generate', inputs=generate_inputs, outputs={'image': runway.image})
def convert(model, inputs):
    z = inputs['z']
    truncation = inputs['truncation']
    latent = z.reshape((1, 256))
    style_mix = np.zeros([4, 4])
    for i in range(4):
        for j in range(4):