danielsapit commited on
Commit
3ae9a61
1 Parent(s): 5cb71e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -4
app.py CHANGED
@@ -20,6 +20,8 @@ for model_path in ['fbcnn_gray.pth','fbcnn_color.pth']:
20
 
21
  def inference(input_img, is_gray, input_quality, zoom, x_shift, y_shift):
22
 
 
 
23
  if is_gray:
24
  n_channels = 1 # set 1 for grayscale image, set 3 for color image
25
  model_name = 'fbcnn_gray.pth'
@@ -35,7 +37,7 @@ def inference(input_img, is_gray, input_quality, zoom, x_shift, y_shift):
35
  model_path = model_name
36
 
37
  if os.path.exists(model_path):
38
- print(f'loading model from {model_path}')
39
  else:
40
  print("downloading model")
41
  os.makedirs(os.path.dirname(model_path), exist_ok=True)
@@ -44,10 +46,14 @@ def inference(input_img, is_gray, input_quality, zoom, x_shift, y_shift):
44
  open(model_path, 'wb').write(r.content)
45
 
46
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 
47
 
48
  # ----------------------------------------
49
  # load model
50
  # ----------------------------------------
 
 
 
51
  model = net(in_nc=n_channels, out_nc=n_channels, nc=nc, nb=nb, act_mode='R')
52
  model.load_state_dict(torch.load(model_path), strict=True)
53
  model.eval()
@@ -55,6 +61,8 @@ def inference(input_img, is_gray, input_quality, zoom, x_shift, y_shift):
55
  v.requires_grad = False
56
  model = model.to(device)
57
 
 
 
58
  test_results = OrderedDict()
59
  test_results['psnr'] = []
60
  test_results['ssim'] = []
@@ -64,6 +72,7 @@ def inference(input_img, is_gray, input_quality, zoom, x_shift, y_shift):
64
  # (1) img_L
65
  # ------------------------------------
66
 
 
67
  if n_channels == 1:
68
  open_cv_image = Image.fromarray(input_img)
69
  open_cv_image = ImageOps.grayscale(open_cv_image)
@@ -76,21 +85,31 @@ def inference(input_img, is_gray, input_quality, zoom, x_shift, y_shift):
76
  else:
77
  open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2RGB) # RGB
78
 
 
79
  img_L = util.uint2tensor4(open_cv_image)
 
 
80
  img_L = img_L.to(device)
81
 
82
  # ------------------------------------
83
  # (2) img_E
84
  # ------------------------------------
85
-
 
86
  img_E,QF = model(img_L)
 
87
  img_E = util.tensor2single(img_E)
 
88
  img_E = util.single2uint(img_E)
89
-
 
90
  qf_input = torch.tensor([[1-input_quality/100]]).cuda() if device == torch.device('cuda') else torch.tensor([[1-input_quality/100]])
 
91
  img_E,QF = model(img_L, qf_input)
92
 
 
93
  img_E = util.tensor2single(img_E)
 
94
  img_E = util.single2uint(img_E)
95
 
96
  if img_E.ndim == 3:
@@ -111,7 +130,9 @@ def inference(input_img, is_gray, input_quality, zoom, x_shift, y_shift):
111
  in_img = in_img.resize((int(zoom_w/zoom), int(zoom_h/zoom)), Image.NEAREST)
112
  out_img = out_img.crop((zoom_left, zoom_top, zoom_right, zoom_bottom))
113
  out_img = out_img.resize((int(zoom_w/zoom), int(zoom_h/zoom)), Image.NEAREST)
114
-
 
 
115
  return img_E, in_img, out_img
116
 
117
  gr.Interface(
 
20
 
21
  def inference(input_img, is_gray, input_quality, zoom, x_shift, y_shift):
22
 
23
+ print("img size:",Image.fromarray(input_img).size)
24
+
25
  if is_gray:
26
  n_channels = 1 # set 1 for grayscale image, set 3 for color image
27
  model_name = 'fbcnn_gray.pth'
 
37
  model_path = model_name
38
 
39
  if os.path.exists(model_path):
40
+ print(f'{model_path} already exists.')
41
  else:
42
  print("downloading model")
43
  os.makedirs(os.path.dirname(model_path), exist_ok=True)
 
46
  open(model_path, 'wb').write(r.content)
47
 
48
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
49
+ print("device:",device)
50
 
51
  # ----------------------------------------
52
  # load model
53
  # ----------------------------------------
54
+
55
+ print(f'loading model from {model_path}')
56
+
57
  model = net(in_nc=n_channels, out_nc=n_channels, nc=nc, nb=nb, act_mode='R')
58
  model.load_state_dict(torch.load(model_path), strict=True)
59
  model.eval()
 
61
  v.requires_grad = False
62
  model = model.to(device)
63
 
64
+ print("Model loaded.")
65
+
66
  test_results = OrderedDict()
67
  test_results['psnr'] = []
68
  test_results['ssim'] = []
 
72
  # (1) img_L
73
  # ------------------------------------
74
 
75
+ print("#if n_channels")
76
  if n_channels == 1:
77
  open_cv_image = Image.fromarray(input_img)
78
  open_cv_image = ImageOps.grayscale(open_cv_image)
 
85
  else:
86
  open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2RGB) # RGB
87
 
88
+ print("#util.uint2tensor4(open_cv_image)")
89
  img_L = util.uint2tensor4(open_cv_image)
90
+
91
+ print("#img_L.to(device)")
92
  img_L = img_L.to(device)
93
 
94
  # ------------------------------------
95
  # (2) img_E
96
  # ------------------------------------
97
+
98
+ print("#model(img_L)")
99
  img_E,QF = model(img_L)
100
+ print("#util.tensor2single(img_E)")
101
  img_E = util.tensor2single(img_E)
102
+ print("#util.single2uint(img_E)")
103
  img_E = util.single2uint(img_E)
104
+
105
+ print("#torch.tensor([[1-input_quality/100]]).cuda() || torch.tensor([[1-input_quality/100]])")
106
  qf_input = torch.tensor([[1-input_quality/100]]).cuda() if device == torch.device('cuda') else torch.tensor([[1-input_quality/100]])
107
+ print("#util.single2uint(img_E)")
108
  img_E,QF = model(img_L, qf_input)
109
 
110
+ print("#util.tensor2single(img_E)")
111
  img_E = util.tensor2single(img_E)
112
+ print("#util.single2uint(img_E)")
113
  img_E = util.single2uint(img_E)
114
 
115
  if img_E.ndim == 3:
 
130
  in_img = in_img.resize((int(zoom_w/zoom), int(zoom_h/zoom)), Image.NEAREST)
131
  out_img = out_img.crop((zoom_left, zoom_top, zoom_right, zoom_bottom))
132
  out_img = out_img.resize((int(zoom_w/zoom), int(zoom_h/zoom)), Image.NEAREST)
133
+
134
+ print("--generating preview finished")
135
+
136
  return img_E, in_img, out_img
137
 
138
  gr.Interface(