Exemplo n.º 1
0
def send_prediction_on_photo(update, context):

    chat_id = update.message.chat_id
    print("Got image from {}".format(chat_id))

    # получаем информацию о картинке
    image_info = update.message.photo[-1]
    image_file = context.bot.get_file(image_info)

    if chat_id in first_image_file:
        # первая картинка, которая к нам пришла станет content image, а вторая style image
        content_image_stream = BytesIO()
        first_image_file[chat_id].download(out=content_image_stream)
        del first_image_file[chat_id]

        style_image_stream = BytesIO()
        image_file.download(out=style_image_stream)
        print(style_image_stream, content_image_stream)
        go = StyleTransferModel()
        output = go.start_learning(content_image_stream, style_image_stream)

        # теперь отправим назад фото
        output_stream = BytesIO()
        output.save(output_stream, format='PNG')
        output_stream.seek(0)
        context.bot.send_photo(chat_id, photo=output_stream)
        print("Sent Photo to user")
    else:
        first_image_file[chat_id] = image_file
Exemplo n.º 2
0
def main(args):
    # you may need this code to train multiple instances on a single GPU
    # sess_config = tf.ConfigProto(allow_soft_placement=True)
    # sess_config.gpu_options.allow_growth=True
    # sess_config.gpu_options.per_process_gpu_memory_fraction = 0.45
    # remember to pass sess_config to Model

    style_image_path, _ = os.path.splitext(args['style_image_path'])
    _, style_image = os.path.split(style_image_path)

    args['model_name'] = style_image

    model = StyleTransferModel('model',
                               args,
                               log_tensorboard=True,
                               save=True,
                               device='/gpu:0')
    model.train()
Exemplo n.º 3
0
def start(update, context):
    global photo_1_file, photo_2_file, content_image_stream, style_image_stream, output_stream, model
    model = StyleTransferModel()
    content_image_stream = BytesIO()
    style_image_stream = BytesIO()
    output_stream = BytesIO()
    update.message.reply_text('Хочешь я круто стилизую твое фото?\n'
                              'Высылай фото для стилизации!')
    return PHOTO_1
Exemplo n.º 4
0
    def run(self):
        model = StyleTransferModel()
        model.num_steps = self.num_steps
        model.image_size = self.image_size
        model.progress_lambda = lambda progress: self.progress_action(progress)
        model.should_terminate_lambda = lambda: self.cancelled
        output = model.transfer_style(self.content_image, self.style_image)

        return output
Exemplo n.º 5
0
def main():
    set_global_seed()
    args_file = 'args.yaml'
    args = load_args(args_file)
    cmd_args = parse_cmd_args()

    args['eval_image_path'] = cmd_args.image
    image = imread(cmd_args.image)
    h, w, c = image.shape
    h = ceil(h / 4) * 4
    w = ceil(w / 4) * 4
    args['image_shape'] = (h, w, c)

    model = StyleTransferModel('model', args, device='/gpu:0')
    model.restore(cmd_args.checkpoint)
    model.evaluate(eval_image=True)
Exemplo n.º 6
0
from model import StyleTransferModel
from fast_neural_style.neural_style.neural_style import transfer
from telegram_token import token
import numpy as np
from PIL import Image
from io import BytesIO

# В бейзлайне пример того, как мы можем обрабатывать две картинки, пришедшие от пользователя.
# При реалиазации первого алгоритма это Вам не понадобится, так что можете убрать загрузку второй картинки.
# Если решите делать модель, переносящую любой стиль, то просто вернете код)

model = StyleTransferModel()
first_image_file = {}
info = {}


def send_prediction_on_photo(bot, update):
    # Нам нужно получить две картинки, чтобы произвести перенос стиля, но каждая картинка приходит в
    # отдельном апдейте, поэтому в простейшем случае мы будем сохранять id первой картинки в память,
    # чтобы, когда уже придет вторая, мы могли загрузить в память уже сами картинки и обработать их.
    chat_id = update.message.chat_id
    print("Got image from {}".format(chat_id))

    # получаем информацию о картинке
    image_info = update.message.photo[-1]
    image_file = bot.get_file(image_info)

    if chat_id in first_image_file:

        # первая картинка, которая к нам пришла станет content image, а вторая style image
        content_image_stream = BytesIO()
Exemplo n.º 7
0
def pre_process_image(path):
    img = load_img(path)
    img = tf.keras.applications.vgg19.preprocess_input(img)
    return img


# Content Loss
def content_loss_f(content, generated):
    return tf.losses.mean_squared_error(content, generated)


##
content_img = load_img(config.CONTENT_IMG)
style_img = load_img(config.STYLE_IMG)

model = StyleTransferModel(CONTENT_L, STYLE_L)

style_targets = model(style_img)['style']
content_target = model(content_img)['content']

num_style_layers = len(STYLE_L)
num_content_layers = len(CONTENT_L)


# Style Loss
def loss_f(outputs):
    style_outputs = outputs['style']
    content_outputs = outputs['content']

    style_loss = tf.add_n([
        tf.reduce_mean((style_outputs[name] - style_targets[name])**2)
Exemplo n.º 8
0
import os, shutil
import logging
from tempfile import mkstemp
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram import InlineQueryResultVoice, InlineQueryResultAudio
from telegram.ext.dispatcher import run_async
from io import BytesIO
from model import StyleTransferModel
from config import token
import subprocess
import torch

first_image_file = {}
model_stndrt = StyleTransferModel(imsize=256,
                                  num_steps=500,
                                  style_weight=100000,
                                  content_weight=1,
                                  content_layers=[4],
                                  style_layers=[1, 2, 3, 4, 5])
model_strong = StyleTransferModel(imsize=256,
                                  num_steps=1000,
                                  style_weight=1000000,
                                  content_weight=1,
                                  content_layers=[4, 5, 6, 7, 8, 9],
                                  style_layers=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
model = model_stndrt


# Часть бота, отвечающая за рестайлинг
@run_async
def take_photo(bot, update):
    chat_id = update.message.chat_id
Exemplo n.º 9
0
				24]


args = {
'sample_rate': sr,
'start_lr': 0.001,
'steps': 50,

'content_layer_weights': [1],
'style_layer_weights': [0.2 for _ in range(5)],
'content_weight': 100,
'style_weight': 0.01,
}


model = StyleTransferModel(loss_net, content_layers, style_layers)
model.set_up_training(sess, content_audio, style_audio, args)


tf.set_random_seed(1)
sess.run(model.init)


for i in range(args['steps']):
	t = time.time()
	loss, _, _ = sess.run(model.train_op)
	t = time.time() - t
	print('\rloss: %.3f, used %.3fs    ' % (loss, t))
	if i % 10 == 0:
		with open('img_step_%d.wav' % i, 'wb') as file:
			file.write(sess.run(model.write_audio))