ckyrkou commited on
Commit
82b825a
·
verified ·
1 Parent(s): 0063442

Upload 2 files

Browse files
Files changed (2) hide show
  1. SRnet.pth +3 -0
  2. app.py +32 -16
SRnet.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a2d225fc37e4908cf360e503ec7b34cebe9ce6b9905cc853a7d83b0ba026b1ba
3
+ size 67952735
app.py CHANGED
@@ -1,37 +1,49 @@
1
  import gradio as gr
2
- import onnxruntime as ort
3
  import numpy as np
4
  from PIL import Image
 
 
 
 
5
 
6
  # Load the ONNX model
7
- model_path = "modelSR.onnx" # Replace with your model path
8
- ort_session = ort.InferenceSession(model_path)
 
 
 
 
 
 
9
 
10
  # Define the superresolution function
11
  def superresolve(image):
12
  # Preprocess the image
13
- image = np.array(image.resize((256, 256))) # Resize to expected input size
14
- image = image[:, :, ::-1] # Convert to BGR (some models expect this)
15
- image = np.transpose(image, (2, 0, 1)) # Transpose to NHWC format
16
- image = image[np.newaxis, ...] # Add batch dimension
17
 
18
  # Run inference
19
- ort_inputs = {ort_session.get_inputs()[0].name: image.astype(np.float32)}
20
- ort_outs = ort_session.run(None, ort_inputs)
21
- output = ort_outs[0][0]
 
 
 
 
22
 
23
  # Postprocess the output
24
- output = output.transpose(1, 2, 0) # Transpose back to HWC
25
- output = np.clip(output, 0, 1) # Clip values to [0, 1]
26
- output = (output * 255).astype(np.uint8) # Convert to uint8 for PIL
27
- output = Image.fromarray(output[:, :, ::-1]) # Convert back to RGB
 
28
 
29
  return output
30
 
31
  # Define the Gradio interface
32
  interface = gr.Interface(
33
  fn=superresolve,
34
- inputs="image",
35
  outputs="image",
36
  title="Super Resolution",
37
  description="Upload an image to upscale its resolution.",
@@ -41,4 +53,8 @@ interface = gr.Interface(
41
  )
42
 
43
  # Launch the Gradio app
44
- interface.launch()
 
 
 
 
 
1
  import gradio as gr
 
2
  import numpy as np
3
  from PIL import Image
4
+ import torch
5
+ from torchvision.transforms import ToTensor
6
+ from torchvision import transforms
7
+
8
 
9
  # Load the ONNX model
10
+ model_path = "./SRnet.pth" # Replace with your model path
11
+
12
+ transforms = transforms.Compose([
13
+ #transforms.Resize(size=(50, 50), antialias=True),
14
+ transforms.ToTensor()
15
+ ])
16
+
17
+
18
 
19
  # Define the superresolution function
20
  def superresolve(image):
21
  # Preprocess the image
22
+ image = transforms(image)[None,...]
23
+
 
 
24
 
25
  # Run inference
26
+
27
+ net = torch.load(model_path)
28
+ net = net['arch']
29
+ net.eval()
30
+ net.cpu()
31
+
32
+ output = net(image)
33
 
34
  # Postprocess the output
35
+ output = output.permute(0,2,3,1)[0].data.numpy()
36
+ output *= 255.0
37
+ output = output.clip(0, 255)
38
+ output = Image.fromarray(np.uint8(output))
39
+
40
 
41
  return output
42
 
43
  # Define the Gradio interface
44
  interface = gr.Interface(
45
  fn=superresolve,
46
+ inputs=gr.Image(type="pil"),
47
  outputs="image",
48
  title="Super Resolution",
49
  description="Upload an image to upscale its resolution.",
 
53
  )
54
 
55
  # Launch the Gradio app
56
+ interface.launch()
57
+
58
+ # impath = './image.jpg'
59
+ # img = Image.open(impath).convert('RGB')
60
+ # superresolve(img)