Spaces:
Sleeping
Sleeping
add intermediate steps
Browse files- .idea/misc.xml +1 -1
- .idea/style_transfer_app.iml +1 -1
- __pycache__/model.cpython-313.pyc +0 -0
- __pycache__/utils.cpython-313.pyc +0 -0
- app.py +13 -8
- model.py +9 -1
- utils.py +4 -5
.idea/misc.xml
CHANGED
|
@@ -3,5 +3,5 @@
|
|
| 3 |
<component name="Black">
|
| 4 |
<option name="sdkName" value="Python 3.13" />
|
| 5 |
</component>
|
| 6 |
-
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.13
|
| 7 |
</project>
|
|
|
|
| 3 |
<component name="Black">
|
| 4 |
<option name="sdkName" value="Python 3.13" />
|
| 5 |
</component>
|
| 6 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.13 virtualenv at ~/Desktop/my_ai_apps/transfer_image_style/.venv" project-jdk-type="Python SDK" />
|
| 7 |
</project>
|
.idea/style_transfer_app.iml
CHANGED
|
@@ -4,7 +4,7 @@
|
|
| 4 |
<content url="file://$MODULE_DIR$">
|
| 5 |
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
| 6 |
</content>
|
| 7 |
-
<orderEntry type="jdk" jdkName="Python 3.13
|
| 8 |
<orderEntry type="sourceFolder" forTests="false" />
|
| 9 |
</component>
|
| 10 |
</module>
|
|
|
|
| 4 |
<content url="file://$MODULE_DIR$">
|
| 5 |
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
| 6 |
</content>
|
| 7 |
+
<orderEntry type="jdk" jdkName="Python 3.13 virtualenv at ~/Desktop/my_ai_apps/transfer_image_style/.venv" jdkType="Python SDK" />
|
| 8 |
<orderEntry type="sourceFolder" forTests="false" />
|
| 9 |
</component>
|
| 10 |
</module>
|
__pycache__/model.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/model.cpython-313.pyc and b/__pycache__/model.cpython-313.pyc differ
|
|
|
__pycache__/utils.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/utils.cpython-313.pyc and b/__pycache__/utils.cpython-313.pyc differ
|
|
|
app.py
CHANGED
|
@@ -5,7 +5,7 @@ from model import generate_image
|
|
| 5 |
from utils import load_model, load_image, im_convert, device
|
| 6 |
|
| 7 |
|
| 8 |
-
|
| 9 |
|
| 10 |
max_image_size = 400
|
| 11 |
|
|
@@ -17,8 +17,12 @@ def generate(content: torch.Tensor, style: torch.Tensor, alpha_slider: float):
|
|
| 17 |
|
| 18 |
target_img = content_img.clone().requires_grad_(True).to(
|
| 19 |
device) # Initialize the target image as a clone of the original content image
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
def check_inputs(img1, img2):
|
|
@@ -46,9 +50,10 @@ with gr.Blocks() as demo:
|
|
| 46 |
alpha_slider = gr.Slider(0, 1, value=1, step=0.1, label="Blending Ratio")
|
| 47 |
submit_button = gr.Button("Blend Images", "Generate", variant="primary", interactive=False)
|
| 48 |
|
| 49 |
-
output = gr.Image(label="Blended Image")
|
|
|
|
| 50 |
|
| 51 |
-
submit_button.click(generate, inputs=[content_image, style_image, alpha_slider], outputs=output)
|
| 52 |
|
| 53 |
# When images change, check if both are uploaded to enable the button
|
| 54 |
content_image.change(fn=check_inputs, inputs=[content_image, style_image], outputs=submit_button)
|
|
@@ -56,7 +61,7 @@ with gr.Blocks() as demo:
|
|
| 56 |
|
| 57 |
|
| 58 |
# Launch the demo!
|
| 59 |
-
demo.launch()
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 5 |
from utils import load_model, load_image, im_convert, device
|
| 6 |
|
| 7 |
|
| 8 |
+
model = load_model(d=device)
|
| 9 |
|
| 10 |
max_image_size = 400
|
| 11 |
|
|
|
|
| 17 |
|
| 18 |
target_img = content_img.clone().requires_grad_(True).to(
|
| 19 |
device) # Initialize the target image as a clone of the original content image
|
| 20 |
+
|
| 21 |
+
steps = 500
|
| 22 |
+
for target, status in generate_image(model=model, content=content_img, style=style_img, target=target_img, steps = steps, content_wt=alpha_slider):
|
| 23 |
+
yield im_convert(target), status
|
| 24 |
+
|
| 25 |
+
# return target
|
| 26 |
|
| 27 |
|
| 28 |
def check_inputs(img1, img2):
|
|
|
|
| 50 |
alpha_slider = gr.Slider(0, 1, value=1, step=0.1, label="Blending Ratio")
|
| 51 |
submit_button = gr.Button("Blend Images", "Generate", variant="primary", interactive=False)
|
| 52 |
|
| 53 |
+
output = gr.Image(type="pil", label="Blended Image")
|
| 54 |
+
status = gr.Textbox(label="Progress")
|
| 55 |
|
| 56 |
+
submit_button.click(generate, inputs=[content_image, style_image, alpha_slider], outputs=[output, status])
|
| 57 |
|
| 58 |
# When images change, check if both are uploaded to enable the button
|
| 59 |
content_image.change(fn=check_inputs, inputs=[content_image, style_image], outputs=submit_button)
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
# Launch the demo!
|
| 64 |
+
# demo.launch()
|
| 65 |
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
demo.launch()
|
model.py
CHANGED
|
@@ -46,7 +46,15 @@ def generate_image(model: nn.Module, content: torch.Tensor, style: torch.Tensor,
|
|
| 46 |
total_loss.backward()
|
| 47 |
optimizer.step()
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
|
| 52 |
|
|
|
|
| 46 |
total_loss.backward()
|
| 47 |
optimizer.step()
|
| 48 |
|
| 49 |
+
if ii % 10 == 0:
|
| 50 |
+
status = f"Processing step {ii} of {steps}"
|
| 51 |
+
if ii == steps:
|
| 52 |
+
status = "✅ Completed"
|
| 53 |
+
yield target, status
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# return target
|
| 57 |
+
|
| 58 |
|
| 59 |
|
| 60 |
|
utils.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import torch
|
| 2 |
-
import torch.optim as optim
|
| 3 |
from torchvision import transforms, models
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
|
@@ -10,14 +9,14 @@ device = torch.device("cuda" if torch.cuda.is_available() else "mps" if torch.mp
|
|
| 10 |
|
| 11 |
def load_model(d=device):
|
| 12 |
weights = models.VGG19_Weights.DEFAULT
|
| 13 |
-
|
| 14 |
|
| 15 |
# https://pytorch.org/docs/stable/generated/torch.Tensor.requires_grad_.html
|
| 16 |
-
for param in
|
| 17 |
param.requires_grad_(False)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
return
|
| 21 |
|
| 22 |
|
| 23 |
# max_size limits the image size to 400 pixel
|
|
|
|
| 1 |
import torch
|
|
|
|
| 2 |
from torchvision import transforms, models
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
|
|
|
| 9 |
|
| 10 |
def load_model(d=device):
|
| 11 |
weights = models.VGG19_Weights.DEFAULT
|
| 12 |
+
model = models.vgg19(weights=weights).features # only uses the feature layers of the model
|
| 13 |
|
| 14 |
# https://pytorch.org/docs/stable/generated/torch.Tensor.requires_grad_.html
|
| 15 |
+
for param in model.parameters():
|
| 16 |
param.requires_grad_(False)
|
| 17 |
|
| 18 |
+
model.to(device=d)
|
| 19 |
+
return model
|
| 20 |
|
| 21 |
|
| 22 |
# max_size limits the image size to 400 pixel
|