snehilchatterjee commited on
Commit
29abfef
·
verified ·
1 Parent(s): b8f82c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -17
app.py CHANGED
@@ -137,6 +137,41 @@ class MiniSRGAN(nn.Module):
137
 
138
  return x, feat
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
 
141
  def build_generator():
142
 
@@ -262,8 +297,6 @@ def translate_image(image, sharpen, model_name, save):
262
  model=build_generator().to(device)
263
  model.load_state_dict(torch.load('./weights/mobile_sr.pt', map_location=torch.device('cpu')))
264
 
265
-
266
-
267
  low_res = transform(resized_image)
268
  low_res = low_res.unsqueeze(dim=0).to(device)
269
  model.eval()
@@ -274,15 +307,6 @@ def translate_image(image, sharpen, model_name, save):
274
 
275
  sr_img = Image.fromarray((((fake_imgs[0] + 1) / 2) * 255).astype(np.uint8))
276
 
277
- elif(model_name=='EDSR'):
278
- model = EdsrModel.from_pretrained('eugenesiow/edsr-base', scale=4)
279
- inputs = ImageLoader.load_image(resized_image)
280
- with torch.no_grad():
281
- preds = model(inputs)
282
-
283
- sr_img = preds.data.cpu().numpy()
284
- sr_img = sr_img[0].transpose((1, 2, 0)) * 255.0
285
- sr_img = Image.fromarray(sr_img.astype(np.uint8))
286
  elif(model_name=='MiniSRGAN'):
287
  model = MiniSRGAN().to(device)
288
  model.load_state_dict(torch.load('./weights/miniSRGAN.pt', map_location=torch.device('cpu')))
@@ -300,9 +324,9 @@ def translate_image(image, sharpen, model_name, save):
300
  output = output.transpose(1, 2, 0)
301
  sr_img = Image.fromarray((output * 255.0).astype(np.uint8))
302
 
303
- elif(model_name=='MiniSRResNET'):
304
- model = MiniSRGAN().to(device)
305
- model.load_state_dict(torch.load('./weights/miniSRResNET.pt', map_location=torch.device('cpu')))
306
 
307
  inputs = np.array(resized_image)
308
  inputs = (inputs / 127.5) - 1.0
@@ -335,13 +359,13 @@ def translate_image(image, sharpen, model_name, save):
335
  sr_img.save('super_resolved_image.png')
336
 
337
  return sr_img
338
-
339
  interface = gr.Interface(
340
  fn=translate_image,
341
  inputs=[
342
  gr.Image(type="pil"),
343
  gr.Checkbox(label="Sharpen Image"),
344
- gr.Radio(choices=["MobileSR", "MiniSRGAN", "MiniSRResNET"], label="Select Model", value="MobileSR"),
345
  gr.Radio(choices=["True", "False"], label="Save Output", value="False")
346
  ],
347
  outputs=gr.Image(type="pil", label="Translated Image"),
@@ -350,6 +374,5 @@ interface = gr.Interface(
350
  allow_flagging='never'
351
  )
352
 
353
-
354
  # Launch the Gradio app
355
  interface.launch()
 
137
 
138
  return x, feat
139
 
140
+ class TinySRGAN(nn.Module):
141
+
142
+ def __init__(self, img_feat = 3, n_feats = 32, kernel_size = 3, num_block = 6, act = nn.PReLU(), scale=4):
143
+ super(TinySRGAN, self).__init__()
144
+
145
+ self.conv01 = conv(in_channel = img_feat, out_channel = n_feats, kernel_size = 9, BN = False, act = act)
146
+
147
+ resblocks = [ResBlock(channels = n_feats, kernel_size = 3, act = act) for _ in range(num_block)]
148
+ self.body = nn.Sequential(*resblocks)
149
+
150
+ self.conv02 = conv(in_channel = n_feats, out_channel = n_feats, kernel_size = 3, BN = True, act = None)
151
+
152
+ if(scale == 4):
153
+ upsample_blocks = [Upsampler(channel = n_feats, kernel_size = 3, scale = 2, act = act) for _ in range(2)]
154
+ else:
155
+ upsample_blocks = [Upsampler(channel = n_feats, kernel_size = 3, scale = scale, act = act)]
156
+
157
+ self.tail = nn.Sequential(*upsample_blocks)
158
+
159
+ self.last_conv = conv(in_channel = n_feats, out_channel = img_feat, kernel_size = 3, BN = False, act = nn.Tanh())
160
+
161
+ def forward(self, x):
162
+
163
+ x = self.conv01(x)
164
+ _skip_connection = x
165
+
166
+ x = self.body(x)
167
+ x = self.conv02(x)
168
+ feat = x + _skip_connection
169
+
170
+ x = self.tail(feat)
171
+ x = self.last_conv(x)
172
+
173
+ return x, feat
174
+
175
 
176
  def build_generator():
177
 
 
297
  model=build_generator().to(device)
298
  model.load_state_dict(torch.load('./weights/mobile_sr.pt', map_location=torch.device('cpu')))
299
 
 
 
300
  low_res = transform(resized_image)
301
  low_res = low_res.unsqueeze(dim=0).to(device)
302
  model.eval()
 
307
 
308
  sr_img = Image.fromarray((((fake_imgs[0] + 1) / 2) * 255).astype(np.uint8))
309
 
 
 
 
 
 
 
 
 
 
310
  elif(model_name=='MiniSRGAN'):
311
  model = MiniSRGAN().to(device)
312
  model.load_state_dict(torch.load('./weights/miniSRGAN.pt', map_location=torch.device('cpu')))
 
324
  output = output.transpose(1, 2, 0)
325
  sr_img = Image.fromarray((output * 255.0).astype(np.uint8))
326
 
327
+ elif(model_name=='TinySRGAN'):
328
+ model = TinySRGAN().to(device)
329
+ model.load_state_dict(torch.load('./weights/tinySRGAN.pt', map_location=torch.device('cpu')))
330
 
331
  inputs = np.array(resized_image)
332
  inputs = (inputs / 127.5) - 1.0
 
359
  sr_img.save('super_resolved_image.png')
360
 
361
  return sr_img
362
+
363
  interface = gr.Interface(
364
  fn=translate_image,
365
  inputs=[
366
  gr.Image(type="pil"),
367
  gr.Checkbox(label="Sharpen Image"),
368
+ gr.Radio(choices=["MobileSR", "MiniSRGAN", "TinySRGAN"], label="Select Model", value="MobileSR"),
369
  gr.Radio(choices=["True", "False"], label="Save Output", value="False")
370
  ],
371
  outputs=gr.Image(type="pil", label="Translated Image"),
 
374
  allow_flagging='never'
375
  )
376
 
 
377
  # Launch the Gradio app
378
  interface.launch()