Skip to content

Commit

Permalink
update resnet50_vd_wildanimals (#2068)
Browse files Browse the repository at this point in the history
* updete resnet50_vd_wildanimals

* update
  • Loading branch information
jm12138 authored Nov 4, 2022
1 parent ee36bfa commit 350407c
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@
* 1.0.0

初始发布

* 1.1.0

移除 Fluid API

- ```shell
$ hub install resnet50_vd_wildanimals==1.0.0
$ hub install resnet50_vd_wildanimals==1.1.0
```
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@
* 1.0.0

First release

* 1.1.0

Remove Fluid API

- ```shell
$ hub install resnet50_vd_wildanimals==1.0.0
$ hub install resnet50_vd_wildanimals==1.1.0
```
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# coding=utf-8
import os
import time
from collections import OrderedDict

import cv2
import numpy as np
from PIL import Image

Expand Down
142 changes: 38 additions & 104 deletions modules/image/classification/resnet50_vd_wildanimals/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
from __future__ import absolute_import
from __future__ import division

import ast
import argparse
import ast
import os

import numpy as np
import paddle.fluid as fluid
import paddlehub as hub
from paddle.fluid.core import PaddleTensor, AnalysisConfig, create_paddle_predictor
from paddlehub.module.module import moduleinfo, runnable, serving
from paddlehub.common.paddle_helper import add_vars_prefix
from paddle.inference import Config
from paddle.inference import create_predictor

from resnet50_vd_wildanimals.processor import postprocess, base64_to_cv2
from resnet50_vd_wildanimals.data_feed import reader
from resnet50_vd_wildanimals.resnet_vd import ResNet50_vd
from .data_feed import reader
from .processor import base64_to_cv2
from .processor import postprocess
from paddlehub.module.module import moduleinfo
from paddlehub.module.module import runnable
from paddlehub.module.module import serving


@moduleinfo(
Expand All @@ -25,10 +25,11 @@
author_email="",
summary=
"ResNet50vd is a image classfication model, this module is trained with IFAW's self-built wild animals dataset.",
version="1.0.0")
class ResNet50vdWildAnimals(hub.Module):
def _initialize(self):
self.default_pretrained_model_path = os.path.join(self.directory, "model")
version="1.1.0")
class ResNet50vdWildAnimals:

def __init__(self):
self.default_pretrained_model_path = os.path.join(self.directory, "model", "model")
label_file = os.path.join(self.directory, "label_list.txt")
with open(label_file, 'r', encoding='utf-8') as file:
self.label_list = file.read().split("\n")[:-1]
Expand All @@ -52,10 +53,12 @@ def _set_config(self):
"""
predictor config setting.
"""
cpu_config = AnalysisConfig(self.default_pretrained_model_path)
model = self.default_pretrained_model_path + '.pdmodel'
params = self.default_pretrained_model_path + '.pdiparams'
cpu_config = Config(model, params)
cpu_config.disable_glog_info()
cpu_config.disable_gpu()
self.cpu_predictor = create_paddle_predictor(cpu_config)
self.cpu_predictor = create_predictor(cpu_config)

try:
_places = os.environ["CUDA_VISIBLE_DEVICES"]
Expand All @@ -64,58 +67,10 @@ def _set_config(self):
except:
use_gpu = False
if use_gpu:
gpu_config = AnalysisConfig(self.default_pretrained_model_path)
gpu_config = Config(model, params)
gpu_config.disable_glog_info()
gpu_config.enable_use_gpu(memory_pool_init_size_mb=1000, device_id=0)
self.gpu_predictor = create_paddle_predictor(gpu_config)

def context(self, trainable=True, pretrained=True):
"""context for transfer learning.
Args:
trainable (bool): Set parameters in program to be trainable.
pretrained (bool) : Whether to load pretrained model.
Returns:
inputs (dict): key is 'image', corresponding vaule is image tensor.
outputs (dict): key is :
'classification', corresponding value is the result of classification.
'feature_map', corresponding value is the result of the layer before the fully connected layer.
context_prog (fluid.Program): program for transfer learning.
"""
context_prog = fluid.Program()
startup_prog = fluid.Program()
with fluid.program_guard(context_prog, startup_prog):
with fluid.unique_name.guard():
image = fluid.layers.data(name="image", shape=[3, 224, 224], dtype="float32")
resnet_vd = ResNet50_vd()
output, feature_map = resnet_vd.net(input=image, class_dim=len(self.label_list))

name_prefix = '@HUB_{}@'.format(self.name)
inputs = {'image': name_prefix + image.name}
outputs = {'classification': name_prefix + output.name, 'feature_map': name_prefix + feature_map.name}
add_vars_prefix(context_prog, name_prefix)
add_vars_prefix(startup_prog, name_prefix)
global_vars = context_prog.global_block().vars
inputs = {key: global_vars[value] for key, value in inputs.items()}
outputs = {key: global_vars[value] for key, value in outputs.items()}

place = fluid.CPUPlace()
exe = fluid.Executor(place)
# pretrained
if pretrained:

def _if_exist(var):
b = os.path.exists(os.path.join(self.default_pretrained_model_path, var.name))
return b

fluid.io.load_vars(exe, self.default_pretrained_model_path, context_prog, predicate=_if_exist)
else:
exe.run(startup_prog)
# trainable
for param in context_prog.global_block().iter_parameters():
param.trainable = trainable
return inputs, outputs, context_prog
self.gpu_predictor = create_predictor(gpu_config)

def classification(self, images=None, paths=None, batch_size=1, use_gpu=False, top_k=1):
"""
Expand All @@ -131,15 +86,6 @@ def classification(self, images=None, paths=None, batch_size=1, use_gpu=False, t
Returns:
res (list[dict]): The classfication results.
"""
if use_gpu:
try:
_places = os.environ["CUDA_VISIBLE_DEVICES"]
int(_places[0])
except:
raise RuntimeError(
"Environment Variable CUDA_VISIBLE_DEVICES is not set correctly. If you wanna use gpu, please set CUDA_VISIBLE_DEVICES as cuda_device_id."
)

all_data = list()
for yield_data in reader(images, paths):
all_data.append(yield_data)
Expand All @@ -158,32 +104,19 @@ def classification(self, images=None, paths=None, batch_size=1, use_gpu=False, t
pass
# feed batch image
batch_image = np.array([data['image'] for data in batch_data])
batch_image = PaddleTensor(batch_image.copy())
predictor_output = self.gpu_predictor.run([batch_image]) if use_gpu else self.cpu_predictor.run(
[batch_image])
out = postprocess(data_out=predictor_output[0].as_ndarray(), label_list=self.label_list, top_k=top_k)

predictor = self.gpu_predictor if use_gpu else self.cpu_predictor
input_names = predictor.get_input_names()
input_handle = predictor.get_input_handle(input_names[0])
input_handle.copy_from_cpu(batch_image.copy())
predictor.run()
output_names = predictor.get_output_names()
output_handle = predictor.get_output_handle(output_names[0])

out = postprocess(data_out=output_handle.copy_to_cpu(), label_list=self.label_list, top_k=top_k)
res += out
return res

def save_inference_model(self, dirname, model_filename=None, params_filename=None, combined=True):
if combined:
model_filename = "__model__" if not model_filename else model_filename
params_filename = "__params__" if not params_filename else params_filename
place = fluid.CPUPlace()
exe = fluid.Executor(place)

program, feeded_var_names, target_vars = fluid.io.load_inference_model(
dirname=self.default_pretrained_model_path, executor=exe)

fluid.io.save_inference_model(
dirname=dirname,
main_program=program,
executor=exe,
feeded_var_names=feeded_var_names,
target_vars=target_vars,
model_filename=model_filename,
params_filename=params_filename)

@serving
def serving_method(self, images, **kwargs):
"""
Expand All @@ -198,11 +131,10 @@ def run_cmd(self, argvs):
"""
Run as a command.
"""
self.parser = argparse.ArgumentParser(
description="Run the {} module.".format(self.name),
prog='hub run {}'.format(self.name),
usage='%(prog)s',
add_help=True)
self.parser = argparse.ArgumentParser(description="Run the {} module.".format(self.name),
prog='hub run {}'.format(self.name),
usage='%(prog)s',
add_help=True)
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(
title="Config options", description="Run configuration for controlling module behavior, not required.")
Expand All @@ -216,8 +148,10 @@ def add_module_config_arg(self):
"""
Add the command config options.
"""
self.arg_config_group.add_argument(
'--use_gpu', type=ast.literal_eval, default=False, help="whether use GPU or not.")
self.arg_config_group.add_argument('--use_gpu',
type=ast.literal_eval,
default=False,
help="whether use GPU or not.")
self.arg_config_group.add_argument('--batch_size', type=ast.literal_eval, default=1, help="batch size.")
self.arg_config_group.add_argument('--top_k', type=ast.literal_eval, default=1, help="Return top k results.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from __future__ import print_function

import base64
import cv2
import os

import cv2
import numpy as np


Expand All @@ -18,7 +17,6 @@ def base64_to_cv2(b64str):


def softmax(x):
orig_shape = x.shape
if len(x.shape) > 1:
tmp = np.max(x, axis=1)
x -= tmp.reshape((x.shape[0], 1))
Expand Down
Loading

0 comments on commit 350407c

Please sign in to comment.