jamino30 commited on
Commit
3ef9484
·
verified ·
1 Parent(s): 4234289

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +43 -33
app.py CHANGED
@@ -12,7 +12,8 @@ import torchvision.models as models
12
  import gradio as gr
13
  from gradio_imageslider import ImageSlider
14
 
15
- device = 'cuda' if torch.cuda.is_available() else 'cpu'
 
16
  print('DEVICE:', device)
17
 
18
  class VGG_19(nn.Module):
@@ -83,7 +84,7 @@ style_options = {
83
  style_options = {k: f'./style_images/{v}' for k, v in style_options.items()}
84
 
85
  @spaces.GPU
86
- def inference(content_image, style_image, progress=gr.Progress(track_tqdm=True)):
87
  yield None
88
  print('-'*15)
89
  print('STYLE:', style_image)
@@ -94,7 +95,7 @@ def inference(content_image, style_image, progress=gr.Progress(track_tqdm=True))
94
 
95
  print('CONTENT IMG SIZE:', original_size)
96
 
97
- iters = 100
98
  lr = 1e-1
99
  alpha = 1
100
  beta = 1
@@ -102,8 +103,8 @@ def inference(content_image, style_image, progress=gr.Progress(track_tqdm=True))
102
  st = time.time()
103
  generated_img = content_img.clone().requires_grad_(True)
104
  optimizer = optim.Adam([generated_img], lr=lr)
105
-
106
- for iter in tqdm(range(iters+1)):
107
  generated_features = model(generated_img)
108
  content_features = model(content_img)
109
  style_features = model(style_img)
@@ -134,31 +135,40 @@ def inference(content_image, style_image, progress=gr.Progress(track_tqdm=True))
134
  yield content_image, save_img(generated_img, original_size)
135
 
136
 
137
- interface = gr.Interface(
138
- fn=inference,
139
- inputs=[
140
- gr.Image(label='Content', type='pil', sources=['upload'], elem_id='content'),
141
- gr.Dropdown(choices=list(style_options.keys()), label='Style', value='Starry Night', type='value', elem_id='style'),
142
- ],
143
- outputs=[
144
- ImageSlider(position=0.15, label='Output', show_download_button=True, interactive=False, elem_id='output'),
145
- ],
146
- title="🖼️ Neural Style Transfer",
147
- api_name='style',
148
- allow_flagging='auto',
149
- examples=[
150
- # page 1
151
- ['./content_images/TajMahal.jpg', 'Starry Night'],
152
- ['./content_images/GoldenRetriever.jpg', 'Lego Bricks'],
153
- ['./content_images/Beach.jpg', 'Oil Painting'],
154
- ['./content_images/StandingOnCliff.png', 'Great Wave'],
155
- # page 2
156
- ['./content_images/Surfer.jpg', 'Starry Night'],
157
- ['./content_images/CameraGirl.jpg', 'Lego Bricks'],
158
- ['./content_images/NYCSkyline.jpg', 'Oil Painting'],
159
- ['./content_images/GoldenRetriever.jpg', 'Great Wave'],
160
- ],
161
- examples_per_page=len(style_options),
162
- cache_examples='lazy',
163
- clear_btn=None
164
- ).launch(inbrowser=True)
 
 
 
 
 
 
 
 
 
 
12
  import gradio as gr
13
  from gradio_imageslider import ImageSlider
14
 
15
+ device = 'mps'
16
+ # device = 'cuda' if torch.cuda.is_available() else 'cpu'
17
  print('DEVICE:', device)
18
 
19
  class VGG_19(nn.Module):
 
84
  style_options = {k: f'./style_images/{v}' for k, v in style_options.items()}
85
 
86
  @spaces.GPU
87
+ def inference(content_image, style_image, style_strength, progress=gr.Progress(track_tqdm=True)):
88
  yield None
89
  print('-'*15)
90
  print('STYLE:', style_image)
 
95
 
96
  print('CONTENT IMG SIZE:', original_size)
97
 
98
+ iters = style_strength
99
  lr = 1e-1
100
  alpha = 1
101
  beta = 1
 
103
  st = time.time()
104
  generated_img = content_img.clone().requires_grad_(True)
105
  optimizer = optim.Adam([generated_img], lr=lr)
106
+
107
+ for iter in tqdm(range(iters)):
108
  generated_features = model(generated_img)
109
  content_features = model(content_img)
110
  style_features = model(style_img)
 
135
  yield content_image, save_img(generated_img, original_size)
136
 
137
 
138
+ examples = [
139
+ # page 1
140
+ ['./content_images/TajMahal.jpg', 'Starry Night'],
141
+ ['./content_images/GoldenRetriever.jpg', 'Lego Bricks'],
142
+ ['./content_images/Beach.jpg', 'Oil Painting'],
143
+ ['./content_images/StandingOnCliff.png', 'Great Wave'],
144
+ # page 2
145
+ ['./content_images/Surfer.jpg', 'Starry Night'],
146
+ ['./content_images/CameraGirl.jpg', 'Lego Bricks'],
147
+ ['./content_images/NYCSkyline.jpg', 'Oil Painting'],
148
+ ['./content_images/GoldenRetriever.jpg', 'Great Wave'],
149
+ ]
150
+
151
+ with gr.Blocks(title='🖼️ Neural Style Transfer') as demo:
152
+ gr.HTML("<h1 style='text-align: center'>🖼️ Neural Style Transfer</h1>")
153
+ with gr.Row():
154
+ with gr.Column():
155
+ content_image = gr.Image(label='Content', type='pil', sources=['upload'])
156
+ style_dropdown = gr.Dropdown(choices=list(style_options.keys()), label='Style', value='Starry Night', type='value')
157
+ with gr.Accordion('Advanced Settings', open=False):
158
+ style_strength = gr.Slider(label='Style Strength', minimum=1, maximum=200, step=1, value=100)
159
+ submit_button = gr.Button('Submit')
160
+ with gr.Column():
161
+ output_image = ImageSlider(position=0.25, label='Output', show_download_button=True, interactive=False)
162
+
163
+ submit_button.click(fn=inference, inputs=[content_image, style_dropdown, style_strength], outputs=[output_image])
164
+
165
+ gr.Examples(
166
+ fn=inference,
167
+ examples=examples,
168
+ inputs=[content_image, style_dropdown],
169
+ outputs=[output_image],
170
+ examples_per_page=len(style_options),
171
+ cache_examples='lazy',
172
+ )
173
+
174
+ demo.launch(inbrowser=True, show_api=True)