rmayormartins commited on
Commit
f2bcbc8
1 Parent(s): a2d5435

Subindo arquivos

Browse files
Files changed (5) hide show
  1. README.md +38 -6
  2. app.py +126 -0
  3. example1.jpg +0 -0
  4. example2.jpg +0 -0
  5. requirements.txt +7 -0
README.md CHANGED
@@ -1,13 +1,45 @@
1
  ---
2
- title: Neural Style Tf Deepdreaming
3
- emoji: 🐨
4
  colorFrom: blue
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 4.37.2
8
  app_file: app.py
9
  pinned: false
10
- license: ecl-2.0
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: neural-style-tf-deepdreaming
3
+ emoji: 🖼️➕🖼️🤖
4
  colorFrom: blue
5
+ colorTo: magenta
6
  sdk: gradio
7
+ sdk_version: "4.12.0"
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
+ # Combining Images with Deep Dreaming and Style Transfer
13
+
14
+ This project is an experimental application v1.0 that combines two images using Deep Dreaming and Style Transfer techniques to create unique and visually appealing results.
15
+
16
+ ## Project Overview
17
+
18
+ This application allows users to upload two images and adjust parameters to combine them using Deep Dreaming (a technique that amplifies patterns in images using a convolutional neural network) and Style Transfer (which applies the style of one image to another using a neural network). The interface provides sliders to adjust the weight of each image, the density of the style, and the sharpness of the content image.
19
+
20
+ ## Technical Details
21
+
22
+ The project utilizes the following technologies:
23
+ - **Neural Style Transfer**: Implemented using TensorFlow and a pre-trained model from TensorFlow Hub.
24
+ - **Deep Dreaming**: Utilizes a pre-trained VGG19 model from PyTorch to extract and amplify image features.
25
+ - **Gradio**: Provides an interactive web interface for users to upload images and adjust parameters.
26
+
27
+ ## License
28
+
29
+ ecl
30
+
31
+ ## Developer Information
32
+
33
+ Developed by Ramon Mayor Martins, Ph.D. (2024)
34
+ - Email: [email protected]
35
+ - Homepage: https://rmayormartins.github.io/
36
+ - Twitter: @rmayormartins
37
+ - GitHub: https://github.com/rmayormartins
38
+
39
+ ## Acknowledgements
40
+
41
+ Special thanks to Instituto Federal de Santa Catarina (Federal Institute of Santa Catarina) IFSC-São José-Brazil.
42
+
43
+ ## Contact
44
+
45
+ For any queries or suggestions, please contact the developer using the information provided above.
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torchvision.models as models
4
+ import torchvision.transforms as transforms
5
+ from PIL import Image
6
+ import numpy as np
7
+ import gradio as gr
8
+ import tensorflow as tf
9
+ import tensorflow_hub as hub
10
+ import cv2
11
+
12
+ # Conf
13
+ IMAGE_SIZE = (256, 256)
14
+ style_transfer_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
15
+
16
+ # (VGG19)
17
+ vgg = models.vgg19(weights=models.VGG19_Weights.IMAGENET1K_V1).features
18
+ vgg.eval()
19
+
20
+ #
21
+ def load_image_pytorch(image, transform=None):
22
+ if transform:
23
+ image = transform(image).unsqueeze(0)
24
+ return image
25
+
26
+ #
27
+ transform = transforms.Compose([
28
+ transforms.Resize((224, 224)),
29
+ transforms.ToTensor(),
30
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
31
+ ])
32
+
33
+ # Deep Dreaming
34
+ def deep_dream(model, image, iterations, lr):
35
+ for i in range(iterations):
36
+ image.requires_grad = True
37
+ features = model(image)
38
+ loss = features.norm()
39
+ loss.backward()
40
+ with torch.no_grad():
41
+ image += lr * image.grad
42
+ image = image.clamp(0, 1)
43
+ image.grad = None
44
+ return image
45
+
46
+ #
47
+ def load_image_tensorflow(image):
48
+ image = cv2.resize(image, IMAGE_SIZE, interpolation=cv2.INTER_AREA)
49
+ image = image.astype(np.float32)[np.newaxis, ...] / 255.
50
+ if image.shape[-1] == 4:
51
+ image = image[..., :3]
52
+ return image
53
+
54
+ #
55
+ def apply_sharpness(image, intensity):
56
+ kernel = np.array([[0, -intensity, 0],
57
+ [-intensity, 1 + 4 * intensity, -intensity],
58
+ [0, -intensity, 0]])
59
+ sharp_image = cv2.filter2D(image, -1, kernel)
60
+ return np.clip(sharp_image, 0, 255)
61
+
62
+ #
63
+ def interpolate_images(baseline, target, alpha):
64
+ return baseline + alpha * (target - baseline)
65
+
66
+ # + Deep Dreaming e Transferência de Estilo
67
+ def combine_images(image1, image2, weight1, style_density, content_sharpness):
68
+ # Carregar e pré-processar as imagens
69
+ image1_pil = Image.fromarray(image1)
70
+ image2_pil = Image.fromarray(image2)
71
+
72
+ image1_pytorch = load_image_pytorch(image1_pil, transform)
73
+ image2_pytorch = load_image_pytorch(image2_pil, transform)
74
+
75
+ #
76
+ weight2 = 1 - weight1
77
+ mixed_image_pytorch = weight1 * image1_pytorch + weight2 * image2_pytorch
78
+
79
+ # Deep Dreaming
80
+ mixed_image_pytorch = deep_dream(vgg, mixed_image_pytorch, iterations=20, lr=0.01)
81
+
82
+ #
83
+ mixed_image_pytorch = mixed_image_pytorch.squeeze(0).permute(1, 2, 0).detach().numpy()
84
+ mixed_image_pytorch = (mixed_image_pytorch * 255).astype(np.uint8)
85
+
86
+ #
87
+ content_image = load_image_tensorflow(mixed_image_pytorch)
88
+ style_image = load_image_tensorflow(image2)
89
+
90
+ #
91
+ content_image_sharp = apply_sharpness(content_image[0], intensity=content_sharpness)
92
+ content_image_sharp = content_image_sharp[np.newaxis, ...]
93
+
94
+ # Transferência de Estilo
95
+ stylized_image = style_transfer_model(tf.constant(content_image_sharp), tf.constant(style_image))[0]
96
+
97
+ #
98
+ stylized_image = interpolate_images(baseline=content_image[0], target=stylized_image.numpy(), alpha=style_density)
99
+ stylized_image = np.array(stylized_image * 255, np.uint8)
100
+ stylized_image = np.squeeze(stylized_image)
101
+
102
+ return stylized_image
103
+
104
+ #
105
+ example1 = np.array(Image.open("example1.jpg"))
106
+ example2 = np.array(Image.open("example2.jpg"))
107
+
108
+ # Gradio
109
+ interface = gr.Interface(
110
+ fn=combine_images,
111
+ inputs=[
112
+ gr.Image(type="numpy", label="Imagem 1"),
113
+ gr.Image(type="numpy", label="Imagem 2"),
114
+ gr.Slider(minimum=0, maximum=1, value=0.5, label="Peso da Imagem 1"),
115
+ gr.Slider(minimum=0, maximum=1, value=0.5, label="Densidade do Estilo"),
116
+ gr.Slider(minimum=0, maximum=10, value=1, label="Nitidez do Conteúdo")
117
+ ],
118
+ outputs="image",
119
+ title="Combinação de Imagens com Deep Dreaming e Transferência de Estilo",
120
+ description="Ajuste os pesos e a densidade do estilo para combinar e estilizar as imagens.",
121
+ examples=[
122
+ ["example1.jpg", "example2.jpg", 0.5, 0.5, 1]
123
+ ]
124
+ )
125
+
126
+ interface.launch()
example1.jpg ADDED
example2.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ torch==2.0.1
2
+ torchvision==0.15.2
3
+ Pillow==9.5.0
4
+ gradio
5
+ tensorflow==2.12.0
6
+ tensorflow_hub==0.13.0
7
+ opencv-python==4.7.0.72