Example #1
0
def main():
    img = load_and_preprocess_style()

    shape = img.shape[1:]
    vgg = VGG16_AvgPool(shape)
    symbolic_conv_outputs = [
        layer.get_output_at(1) for layer in vgg.layers
        if layer.__class__ == Conv2D
    ]

    style_model = Model(vgg.input, symbolic_conv_outputs)
    style_outputs = [K.variable(y) for y in style_model.predict(img)]
    print(len(style_outputs))

    loss = 0
    for symbolic, actual in zip(symbolic_conv_outputs, style_outputs):
        loss += style_loss(symbolic[0], actual[0])

    gradients = K.gradients(loss, style_model.input)
    get_loss_grads = K.function(inputs=[style_model.input],
                                outputs=[loss] + gradients)

    def get_loss_grads_wrapper(x):
        l, g = get_loss_grads([x.reshape(img.shape)])
        return l.astype(np.float64), g.flatten().astype(np.float64)

    img = minimize_loss(get_loss_grads_wrapper, 10, shape)
    img = np.reshape(img, newshape=(1, shape[0], shape[1], 3))
    img = unpreprocess(img[0].copy())
    img = scale_img(img)

    cv2.imshow('style', img)
    cv2.waitKey(0)
Example #2
0
def main():
    img = load_and_preprocess_content()
    shape = img.shape[1:]
    content_model = VGG16_AvgPool_CutOff(shape, 10)
    target = K.variable(content_model.predict(img))

    mean_squared_loss = K.mean(K.square(target - content_model.output))
    gradients = K.gradients(mean_squared_loss, content_model.input)
    get_loss_grads = K.function(inputs=[content_model.input],
                                outputs=[mean_squared_loss] + gradients)

    def get_loss_grads_wrapper(x):
        l, g = get_loss_grads([x.reshape(img.shape)])
        return l.astype(np.float64), g.flatten().astype(np.float64)

    img = minimize_loss(get_loss_grads_wrapper, 10, shape)
    final_img = img.reshape(*shape)
    final_img = unpreprocess(final_img)
    final_img = scale_img(final_img)
    cv2.imshow('final_img', final_img)
    cv2.waitKey(0)
from utils import minimize
from utils import VGG_Slice
from utils import load_img_and_preprocess, unpreprocess, scale_
''' Path configurations '''
path_to_images = os.path.join(os.getcwd(), '../Images')
path_to_content = os.path.join(path_to_images, 'content')
path_to_style = os.path.join(path_to_images, 'style')
path_to_save = './output_content_decode'

# 1 - Load Content Image

path_to_image = 'content/elephant.jpg'
content_image = load_img_and_preprocess(path_to_image)
h, w = content_image.shape[1:3]

plt.imshow(scale_(unpreprocess(content_image).squeeze(0)))
plt.show()

# 3 - Define the parameters of the image

shape = content_image.shape[1:]
batch_shape = content_image.shape

# 5 - Create the Content Network and the Target Variable

content_net = VGG_Slice(shape, 11)
content_target = K.variable(content_net.predict(content_image))

# 7 - Calculate the loss and gradients respect to input

# 7.1 - Content loss
Example #4
0
from keras.applications.vgg16 import preprocess_input

from utils import minimize
from utils import VGG16_AvgPool, style_Loss
from utils import load_img_and_preprocess, unpreprocess, scale_
''' Path configurations '''
path_to_images = os.path.join(os.getcwd(), '../Images')
path_to_content = os.path.join(path_to_images, 'content')
path_to_style = os.path.join(path_to_images, 'style')
path_to_save = './output_style_decode'

# 2 - Load Style Image

path_to_image = 'styles/lesdemoisellesdavignon.jpg'
style_image = load_img_and_preprocess(path_to_image)
plt.imshow(scale_(unpreprocess(style_image).squeeze(0)))

# 3 - Define the parameters of the image

shape = style_image.shape[1:]
batch_shape = style_image.shape

# 6 - Create the Style Network

vgg = VGG16_AvgPool(shape)

# We need to deal with several outputs at different layers of the network
symbolic_conv_outputs = [
    layer.get_output_at(1) for layer in vgg.layers \
  if layer.name.endswith('conv1')
]
Example #5
0
symbolic_conv_outputs = [
    layer.get_output_at(1) for layer in vgg.layers if layer.__class__ == Conv2D
]
style_model = Model(vgg.input, symbolic_conv_outputs)
style_outputs = [K.variable(y) for y in style_model.predict(style_image)]

loss = K.mean(K.square(content_target - content_model.output))
for w, symbolic, actual in zip(weights, symbolic_conv_outputs, style_outputs):
    loss += w * style_loss(symbolic[0], actual[0])

gradients = K.gradients(loss, vgg.input)
get_loss_grads = K.function(inputs=[vgg.input], outputs=[loss] + gradients)


def get_loss_grads_wrapper(x):
    l, g = get_loss_grads([x.reshape(content_image.shape)])
    return l.astype(np.float64), g.flatten().astype(np.float64)


final_img = minimize_loss(get_loss_grads_wrapper, epochs, shape)
final_img = np.reshape(final_img, newshape=(1, shape[0], shape[1], 3))
final_img = unpreprocess(final_img[0].copy())
#final_img = scale_img(final_img)

result = np.hstack((unpreprocess(content_image[0].copy()),
                    unpreprocess(style_image[0].copy()), final_img))
cv2.imwrite('result/' + str(time.time()) + '.jpg', result)

cv2.imwrite(str(time.time()) + '.jpg', final_img)
keypress = cv2.waitKey(10000)