未验证 提交 1c423b6b 编写于 作者: jm_12138's avatar jm_12138 提交者: GitHub

update falsr_c (#2186)

上级 d0355914
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
|模型名称|falsr_c| |模型名称|falsr_c|
| :--- | :---: | | :--- | :---: |
|类别|图像-图像编辑| |类别|图像-图像编辑|
|网络|falsr_c| |网络|falsr_c|
|数据集|DIV2k| |数据集|DIV2k|
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
## 一、模型基本信息 ## 一、模型基本信息
- ### 应用效果展示 - ### 应用效果展示
- 样例结果示例(左为原图,右为效果图): - 样例结果示例(左为原图,右为效果图):
<p align="center"> <p align="center">
<img src="https://user-images.githubusercontent.com/35907364/133558583-0b7049db-ed1f-4a16-8676-f2141fcb3dee.png" width = "450" height = "300" hspace='10'/> <img src="https://user-images.githubusercontent.com/35907364/130899031-a6f8c58a-5cb7-4105-b990-8cca5ae15368.png" width = "450" height = "300" hspace='10'/> <img src="https://user-images.githubusercontent.com/35907364/133558583-0b7049db-ed1f-4a16-8676-f2141fcb3dee.png" width = "450" height = "300" hspace='10'/> <img src="https://user-images.githubusercontent.com/35907364/130899031-a6f8c58a-5cb7-4105-b990-8cca5ae15368.png" width = "450" height = "300" hspace='10'/>
...@@ -149,19 +149,23 @@ ...@@ -149,19 +149,23 @@
print("save image as falsr_c_X2.png") print("save image as falsr_c_X2.png")
``` ```
- ### Gradio APP 支持
从 PaddleHub 2.3.1 开始支持使用链接 http://127.0.0.1:8866/gradio/falsr_c 在浏览器中访问 falsr_c 的 Gradio APP。
## 五、更新历史 ## 五、更新历史
* 1.0.0 * 1.0.0
初始发布 初始发布
* 1.1.0 * 1.1.0
移除 fluid API 移除 fluid API
* 1.2.0
添加 Gradio APP 支持
```shell ```shell
$ hub install falsr_c == 1.1.0 $ hub install falsr_c == 1.2.0
``` ```
# falsr_c # falsr_c
|Module Name|falsr_c| |Module Name|falsr_c|
| :--- | :---: | | :--- | :---: |
|Category |Image editing| |Category |Image editing|
|Network |falsr_c| |Network |falsr_c|
|Dataset|DIV2k| |Dataset|DIV2k|
...@@ -11,10 +11,10 @@ ...@@ -11,10 +11,10 @@
|Latest update date|2021-02-26| |Latest update date|2021-02-26|
## I. Basic Information ## I. Basic Information
- ### Application Effect Display - ### Application Effect Display
- Sample results: - Sample results:
<p align="center"> <p align="center">
<img src="https://user-images.githubusercontent.com/35907364/133558583-0b7049db-ed1f-4a16-8676-f2141fcb3dee.png" width = "450" height = "300" hspace='10'/> <img src="https://user-images.githubusercontent.com/35907364/130899031-a6f8c58a-5cb7-4105-b990-8cca5ae15368.png" width = "450" height = "300" hspace='10'/> <img src="https://user-images.githubusercontent.com/35907364/133558583-0b7049db-ed1f-4a16-8676-f2141fcb3dee.png" width = "450" height = "300" hspace='10'/> <img src="https://user-images.githubusercontent.com/35907364/130899031-a6f8c58a-5cb7-4105-b990-8cca5ae15368.png" width = "450" height = "300" hspace='10'/>
...@@ -88,7 +88,7 @@ ...@@ -88,7 +88,7 @@
* output\_dir (str): Save path of images, "dcscn_output" by default. * output\_dir (str): Save path of images, "dcscn_output" by default.
- **Return** - **Return**
* res (list\[dict\]): The list of model results, where each element is dict and each field is: * res (list\[dict\]): The list of model results, where each element is dict and each field is:
* save\_path (str, optional): Save path of the result, save_path is '' if no image is saved. * save\_path (str, optional): Save path of the result, save_path is '' if no image is saved.
* data (numpy.ndarray): Result of super resolution. * data (numpy.ndarray): Result of super resolution.
...@@ -153,19 +153,23 @@ ...@@ -153,19 +153,23 @@
print("save image as falsr_c_X2.png") print("save image as falsr_c_X2.png")
``` ```
- ### Gradio APP support
Starting with PaddleHub 2.3.1, the Gradio APP for falsr_c is supported to be accessed in the browser using the link http://127.0.0.1:8866/gradio/falsr_c.
## V. Release Note ## V. Release Note
- 1.0.0 * 1.0.0
First release First release
* 1.1.0
- 1.1.0
Remove Fluid API Remove Fluid API
* 1.2.0
Add Gradio APP support.
```shell ```shell
$ hub install falsr_c == 1.1.0 $ hub install falsr_c == 1.2.0
``` ```
# -*- coding:utf-8 -*-
import os import os
import time import time
from collections import OrderedDict from collections import OrderedDict
...@@ -6,7 +5,6 @@ from collections import OrderedDict ...@@ -6,7 +5,6 @@ from collections import OrderedDict
import cv2 import cv2
import numpy as np import numpy as np
__all__ = ['reader'] __all__ = ['reader']
......
...@@ -12,30 +12,32 @@ ...@@ -12,30 +12,32 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import argparse
import ast import ast
import os import os
import argparse
import numpy as np import numpy as np
import paddle from paddle.inference import Config
import paddle.jit from paddle.inference import create_predictor
import paddle.static
from paddle.inference import Config, create_predictor
from paddlehub.module.module import moduleinfo, runnable, serving
from .data_feed import reader from .data_feed import reader
from .processor import postprocess, base64_to_cv2, cv2_to_base64, check_dir from .processor import base64_to_cv2
from .processor import check_dir
from .processor import cv2_to_base64
@moduleinfo( from .processor import postprocess
name="falsr_c", from paddlehub.module.module import moduleinfo
type="CV/image_editing", from paddlehub.module.module import runnable
author="paddlepaddle", from paddlehub.module.module import serving
author_email="",
summary="falsr_c is a super resolution model.",
version="1.1.0") @moduleinfo(name="falsr_c",
type="CV/image_editing",
author="paddlepaddle",
author_email="",
summary="falsr_c is a super resolution model.",
version="1.2.0")
class Falsr_C: class Falsr_C:
def __init__(self): def __init__(self):
self.default_pretrained_model_path = os.path.join(self.directory, "falsr_c_model", "model") self.default_pretrained_model_path = os.path.join(self.directory, "falsr_c_model", "model")
self._set_config() self._set_config()
...@@ -44,8 +46,8 @@ class Falsr_C: ...@@ -44,8 +46,8 @@ class Falsr_C:
""" """
predictor config setting predictor config setting
""" """
model = self.default_pretrained_model_path+'.pdmodel' model = self.default_pretrained_model_path + '.pdmodel'
params = self.default_pretrained_model_path+'.pdiparams' params = self.default_pretrained_model_path + '.pdiparams'
cpu_config = Config(model, params) cpu_config = Config(model, params)
cpu_config.disable_glog_info() cpu_config.disable_glog_info()
cpu_config.disable_gpu() cpu_config.disable_gpu()
...@@ -110,13 +112,12 @@ class Falsr_C: ...@@ -110,13 +112,12 @@ class Falsr_C:
output_names = predictor.get_output_names() output_names = predictor.get_output_names()
output_handle = predictor.get_output_handle(output_names[0]) output_handle = predictor.get_output_handle(output_names[0])
output = np.expand_dims(output_handle.copy_to_cpu(), axis=1) output = np.expand_dims(output_handle.copy_to_cpu(), axis=1)
out = postprocess( out = postprocess(data_out=output,
data_out=output, org_im=all_data[i]['org_im'],
org_im=all_data[i]['org_im'], org_im_shape=all_data[i]['org_im_shape'],
org_im_shape=all_data[i]['org_im_shape'], org_im_path=all_data[i]['org_im_path'],
org_im_path=all_data[i]['org_im_path'], output_dir=output_dir,
output_dir=output_dir, visualization=visualization)
visualization=visualization)
res.append(out) res.append(out)
return res return res
...@@ -135,11 +136,10 @@ class Falsr_C: ...@@ -135,11 +136,10 @@ class Falsr_C:
""" """
Run as a command. Run as a command.
""" """
self.parser = argparse.ArgumentParser( self.parser = argparse.ArgumentParser(description="Run the {} module.".format(self.name),
description="Run the {} module.".format(self.name), prog='hub run {}'.format(self.name),
prog='hub run {}'.format(self.name), usage='%(prog)s',
usage='%(prog)s', add_help=True)
add_help=True)
self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required") self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
self.arg_config_group = self.parser.add_argument_group( self.arg_config_group = self.parser.add_argument_group(
...@@ -147,8 +147,10 @@ class Falsr_C: ...@@ -147,8 +147,10 @@ class Falsr_C:
self.add_module_config_arg() self.add_module_config_arg()
self.add_module_input_arg() self.add_module_input_arg()
args = self.parser.parse_args(argvs) args = self.parser.parse_args(argvs)
results = self.reconstruct( results = self.reconstruct(paths=[args.input_path],
paths=[args.input_path], use_gpu=args.use_gpu, output_dir=args.output_dir, visualization=args.visualization) use_gpu=args.use_gpu,
output_dir=args.output_dir,
visualization=args.visualization)
if args.save_dir is not None: if args.save_dir is not None:
check_dir(args.save_dir) check_dir(args.save_dir)
self.save_inference_model(args.save_dir) self.save_inference_model(args.save_dir)
...@@ -159,14 +161,22 @@ class Falsr_C: ...@@ -159,14 +161,22 @@ class Falsr_C:
""" """
Add the command config options. Add the command config options.
""" """
self.arg_config_group.add_argument( self.arg_config_group.add_argument('--use_gpu',
'--use_gpu', type=ast.literal_eval, default=False, help="whether use GPU or not") type=ast.literal_eval,
self.arg_config_group.add_argument( default=False,
'--output_dir', type=str, default='falsr_c_output', help="The directory to save output images.") help="whether use GPU or not")
self.arg_config_group.add_argument( self.arg_config_group.add_argument('--output_dir',
'--save_dir', type=str, default='falsr_c_save_model', help="The directory to save model.") type=str,
self.arg_config_group.add_argument( default='falsr_c_output',
'--visualization', type=ast.literal_eval, default=True, help="whether to save output as images.") help="The directory to save output images.")
self.arg_config_group.add_argument('--save_dir',
type=str,
default='falsr_c_save_model',
help="The directory to save model.")
self.arg_config_group.add_argument('--visualization',
type=ast.literal_eval,
default=True,
help="whether to save output as images.")
def add_module_input_arg(self): def add_module_input_arg(self):
""" """
...@@ -174,11 +184,20 @@ class Falsr_C: ...@@ -174,11 +184,20 @@ class Falsr_C:
""" """
self.arg_input_group.add_argument('--input_path', type=str, help="path to image.") self.arg_input_group.add_argument('--input_path', type=str, help="path to image.")
def create_gradio_app(self):
if __name__ == "__main__": import gradio as gr
module = Falsr_C() import tempfile
#module.reconstruct(paths=["BSD100_001.png","BSD100_002.png", "Set5_003.png"]) import os
import cv2 from PIL import Image
img = cv2.imread("BSD100_001.png").astype('float32')
res = module.reconstruct(images=[img]) def inference(image, use_gpu=False):
module.save_inference_model() with tempfile.TemporaryDirectory() as temp_dir:
self.reconstruct(paths=[image], use_gpu=use_gpu, visualization=True, output_dir=temp_dir)
return Image.open(os.path.join(temp_dir, os.listdir(temp_dir)[0]))
interface = gr.Interface(
inference,
[gr.inputs.Image(type="filepath"), gr.Checkbox(label='use_gpu')],
gr.outputs.Image(type="ndarray"),
title='falsr_c')
return interface
# -*- coding:utf-8 -*- import base64
import os import os
import time import time
import base64
import cv2 import cv2
import numpy as np import numpy as np
......
...@@ -3,18 +3,19 @@ import shutil ...@@ -3,18 +3,19 @@ import shutil
import unittest import unittest
import cv2 import cv2
import requests
import numpy as np import numpy as np
import paddlehub as hub import requests
import paddlehub as hub
os.environ['CUDA_VISIBLE_DEVICES'] = '0' os.environ['CUDA_VISIBLE_DEVICES'] = '0'
class TestHubModule(unittest.TestCase): class TestHubModule(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls) -> None: def setUpClass(cls) -> None:
img_url = 'https://unsplash.com/photos/1sLIu1XKQrY/download?ixid=MnwxMjA3fDB8MXxhbGx8MTJ8fHx8fHwyfHwxNjYyMzQxNDUx&force=true&w=640' img_url = 'https://unsplash.com/photos/1sLIu1XKQrY/download?ixid=MnwxMjA3fDB8MXxhbGx8MTJ8fHx8fHwyfHwxNjYyMzQxNDUx&force=true&w=120'
if not os.path.exists('tests'): if not os.path.exists('tests'):
os.makedirs('tests') os.makedirs('tests')
response = requests.get(img_url) response = requests.get(img_url)
...@@ -30,50 +31,26 @@ class TestHubModule(unittest.TestCase): ...@@ -30,50 +31,26 @@ class TestHubModule(unittest.TestCase):
shutil.rmtree('falsr_c_output') shutil.rmtree('falsr_c_output')
def test_reconstruct1(self): def test_reconstruct1(self):
results = self.module.reconstruct( results = self.module.reconstruct(paths=['tests/test.jpg'], use_gpu=False, visualization=False)
paths=['tests/test.jpg'],
use_gpu=False,
visualization=False
)
self.assertIsInstance(results[0]['data'], np.ndarray) self.assertIsInstance(results[0]['data'], np.ndarray)
def test_reconstruct2(self): def test_reconstruct2(self):
results = self.module.reconstruct( results = self.module.reconstruct(images=[cv2.imread('tests/test.jpg')], use_gpu=False, visualization=False)
images=[cv2.imread('tests/test.jpg')],
use_gpu=False,
visualization=False
)
self.assertIsInstance(results[0]['data'], np.ndarray) self.assertIsInstance(results[0]['data'], np.ndarray)
def test_reconstruct3(self): def test_reconstruct3(self):
results = self.module.reconstruct( results = self.module.reconstruct(images=[cv2.imread('tests/test.jpg')], use_gpu=False, visualization=True)
images=[cv2.imread('tests/test.jpg')],
use_gpu=False,
visualization=True
)
self.assertIsInstance(results[0]['data'], np.ndarray) self.assertIsInstance(results[0]['data'], np.ndarray)
def test_reconstruct4(self): def test_reconstruct4(self):
results = self.module.reconstruct( results = self.module.reconstruct(images=[cv2.imread('tests/test.jpg')], use_gpu=True, visualization=False)
images=[cv2.imread('tests/test.jpg')],
use_gpu=True,
visualization=False
)
self.assertIsInstance(results[0]['data'], np.ndarray) self.assertIsInstance(results[0]['data'], np.ndarray)
def test_reconstruct5(self): def test_reconstruct5(self):
self.assertRaises( self.assertRaises(AssertionError, self.module.reconstruct, paths=['no.jpg'])
AssertionError,
self.module.reconstruct,
paths=['no.jpg']
)
def test_reconstruct6(self): def test_reconstruct6(self):
self.assertRaises( self.assertRaises(AttributeError, self.module.reconstruct, images=['test.jpg'])
AttributeError,
self.module.reconstruct,
images=['test.jpg']
)
def test_save_inference_model(self): def test_save_inference_model(self):
self.module.save_inference_model('./inference/model') self.module.save_inference_model('./inference/model')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册