Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleHub
提交
50dd5bf5
P
PaddleHub
项目概览
PaddlePaddle
/
PaddleHub
大约 1 年 前同步成功
通知
282
Star
12117
Fork
2091
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
200
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleHub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
200
Issue
200
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
50dd5bf5
编写于
6月 15, 2021
作者:
W
wuzewu
提交者:
GitHub
6月 15, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add GhostNet.
上级
d39d7dfa
变更
12
展开全部
显示空白变更内容
内联
并排
Showing
12 changed file
with
6064 addition
and
0 deletion
+6064
-0
modules/image/classification/ghostnet_x0_5_imagenet/README.md
...les/image/classification/ghostnet_x0_5_imagenet/README.md
+192
-0
modules/image/classification/ghostnet_x0_5_imagenet/label_list.txt
...mage/classification/ghostnet_x0_5_imagenet/label_list.txt
+1000
-0
modules/image/classification/ghostnet_x0_5_imagenet/module.py
...les/image/classification/ghostnet_x0_5_imagenet/module.py
+324
-0
modules/image/classification/ghostnet_x1_0_imagenet/README.md
...les/image/classification/ghostnet_x1_0_imagenet/README.md
+192
-0
modules/image/classification/ghostnet_x1_0_imagenet/label_list.txt
...mage/classification/ghostnet_x1_0_imagenet/label_list.txt
+1000
-0
modules/image/classification/ghostnet_x1_0_imagenet/module.py
...les/image/classification/ghostnet_x1_0_imagenet/module.py
+324
-0
modules/image/classification/ghostnet_x1_3_imagenet/README.md
...les/image/classification/ghostnet_x1_3_imagenet/README.md
+192
-0
modules/image/classification/ghostnet_x1_3_imagenet/label_list.txt
...mage/classification/ghostnet_x1_3_imagenet/label_list.txt
+1000
-0
modules/image/classification/ghostnet_x1_3_imagenet/module.py
...les/image/classification/ghostnet_x1_3_imagenet/module.py
+324
-0
modules/image/classification/ghostnet_x1_3_imagenet_ssld/README.md
...mage/classification/ghostnet_x1_3_imagenet_ssld/README.md
+192
-0
modules/image/classification/ghostnet_x1_3_imagenet_ssld/label_list.txt
...classification/ghostnet_x1_3_imagenet_ssld/label_list.txt
+1000
-0
modules/image/classification/ghostnet_x1_3_imagenet_ssld/module.py
...mage/classification/ghostnet_x1_3_imagenet_ssld/module.py
+324
-0
未找到文件。
modules/image/classification/ghostnet_x0_5_imagenet/README.md
0 → 100644
浏览文件 @
50dd5bf5
```
shell
$
hub
install
ghostnet_x0_5_imagenet
==
1.0.0
```
## 命令行预测
```
shell
$
hub run ghostnet_x0_5_imagenet
--input_path
"/PATH/TO/IMAGE"
--top_k
5
```
## 脚本预测
```
python
import
paddle
import
paddlehub
as
hub
if
__name__
==
'__main__'
:
model
=
hub
.
Module
(
name
=
'ghostnet_x0_5_imagenet'
,)
result
=
model
.
predict
([
PATH
/
TO
/
IMAGE
])
```
## Fine-tune代码步骤
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 定义数据预处理方式
```
python
import
paddlehub.vision.transforms
as
T
transforms
=
T
.
Compose
([
T
.
Resize
((
256
,
256
)),
T
.
CenterCrop
(
224
),
T
.
Normalize
(
mean
=
[
0.485
,
0.456
,
0.406
],
std
=
[
0.229
,
0.224
,
0.225
])],
to_rgb
=
True
)
```
'transforms' 数据增强模块定义了丰富的数据预处理方式,用户可按照需求替换自己需要的数据预处理方式。
### Step2: 下载数据集并使用
```
python
from
paddlehub.datasets
import
Flowers
flowers
=
Flowers
(
transforms
)
flowers_validate
=
Flowers
(
transforms
,
mode
=
'val'
)
```
*
transforms(Callable): 数据预处理方式。
*
mode(str): 选择数据模式,可选项有 'train', 'test', 'val', 默认为'train'。
'hub.datasets.Flowers()' 会自动从网络下载数据集并解压到用户目录下'$HOME/.paddlehub/dataset'目录。
### Step3: 加载预训练模型
```
python
import
paddlehub
as
hub
model
=
hub
.
Module
(
name
=
'ghostnet_x0_5_imagenet'
,
label_list
=
[
"roses"
,
"tulips"
,
"daisy"
,
"sunflowers"
,
"dandelion"
],
load_checkpoint
=
None
)
```
*
name(str): 选择预训练模型的名字。
*
label_list(list): 设置标签对应分类类别, 默认为Imagenet2012类别。
*
load _checkpoint(str): 模型参数地址。
PaddleHub提供许多图像分类预训练模型,如xception、mobilenet、efficientnet等,详细信息参见
[
图像分类模型
](
https://www.paddlepaddle.org.cn/hub?filter=en_category&value=ImageClassification
)
。
如果想尝试efficientnet模型,只需要更换Module中的'name'参数即可.
```
python
import
paddlehub
as
hub
# 更换name参数即可无缝切换efficientnet模型, 代码示例如下
module
=
hub
.
Module
(
name
=
"efficientnetb7_imagenet"
)
```
**NOTE:**
目前部分模型还没有完全升级到2.0版本,敬请期待。
### Step4: 选择优化策略和运行配置
```
python
import
paddle
from
paddlehub.finetune.trainer
import
Trainer
optimizer
=
paddle
.
optimizer
.
Adam
(
learning_rate
=
0.001
,
parameters
=
model
.
parameters
())
trainer
=
Trainer
(
model
,
optimizer
,
checkpoint_dir
=
'img_classification_ckpt'
)
trainer
.
train
(
flowers
,
epochs
=
100
,
batch_size
=
32
,
eval_dataset
=
flowers_validate
,
save_interval
=
1
)
```
#### 优化策略
Paddle2.0rc提供了多种优化器选择,如'SGD', 'Adam', 'Adamax'等,详细参见
[
策略
](
https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/optimizer/optimizer/Optimizer_cn.html
)
。
其中'Adam':
*
learning_rate: 全局学习率。默认为1e-3;
*
parameters: 待优化模型参数。
#### 运行配置
'Trainer' 主要控制Fine-tune的训练,包含以下可控制的参数:
*
model: 被优化模型;
*
optimizer: 优化器选择;
*
use_vdl: 是否使用vdl可视化训练过程;
*
checkpoint_dir: 保存模型参数的地址;
*
compare_metrics: 保存最优模型的衡量指标;
'trainer.train' 主要控制具体的训练过程,包含以下可控制的参数:
*
train_dataset: 训练时所用的数据集;
*
epochs: 训练轮数;
*
batch_size: 训练的批大小,如果使用GPU,请根据实际情况调整batch_size;
*
num_workers: works的数量,默认为0;
*
eval_dataset: 验证集;
*
log_interval: 打印日志的间隔, 单位为执行批训练的次数。
*
save_interval: 保存模型的间隔频次,单位为执行训练的轮数。
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在'${CHECKPOINT_DIR}/best_model'目录下,其中'${CHECKPOINT_DIR}'目录为Fine-tune时所选择的保存checkpoint的目录。
我们使用该模型来进行预测。predict.py脚本如下:
```
python
import
paddle
import
paddlehub
as
hub
if
__name__
==
'__main__'
:
model
=
hub
.
Module
(
name
=
'ghostnet_x0_5_imagenet'
,
label_list
=
[
"roses"
,
"tulips"
,
"daisy"
,
"sunflowers"
,
"dandelion"
],
load_checkpoint
=
'/PATH/TO/CHECKPOINT'
)
result
=
model
.
predict
([
'flower.jpg'
])
```
参数配置正确后,请执行脚本'python predict.py', 加载模型具体可参见
[
加载
](
https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/framework/io/load_cn.html#load
)
。
**NOTE:**
进行预测时,所选择的module,checkpoint_dir,dataset必须和Fine-tune所用的一样。
## 服务部署
PaddleHub Serving可以部署一个在线分类任务服务
## Step1: 启动PaddleHub Serving
运行启动命令:
```
shell
$
hub serving start
-m
ghostnet_x0_5_imagenet
```
这样就完成了一个分类任务服务化API的部署,默认端口号为8866。
**NOTE:**
如使用GPU预测,则需要在启动服务之前,请设置CUDA_VISIBLE_DEVICES环境变量,否则不用设置。
## Step2: 发送预测请求
配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
```
python
import
requests
import
json
import
cv2
import
base64
import
numpy
as
np
def
cv2_to_base64
(
image
):
data
=
cv2
.
imencode
(
'.jpg'
,
image
)[
1
]
return
base64
.
b64encode
(
data
.
tostring
()).
decode
(
'utf8'
)
def
base64_to_cv2
(
b64str
):
data
=
base64
.
b64decode
(
b64str
.
encode
(
'utf8'
))
data
=
np
.
fromstring
(
data
,
np
.
uint8
)
data
=
cv2
.
imdecode
(
data
,
cv2
.
IMREAD_COLOR
)
return
data
# 发送HTTP请求
org_im
=
cv2
.
imread
(
'/PATH/TO/IMAGE'
)
data
=
{
'images'
:[
cv2_to_base64
(
org_im
)],
'top_k'
:
2
}
headers
=
{
"Content-type"
:
"application/json"
}
url
=
"http://127.0.0.1:8866/predict/ghostnet_x0_5_imagenet"
r
=
requests
.
post
(
url
=
url
,
headers
=
headers
,
data
=
json
.
dumps
(
data
))
data
=
r
.
json
()[
"results"
][
'data'
]
```
### 查看代码
[
PaddleClas
](
https://github.com/PaddlePaddle/PaddleClas
)
### 依赖
paddlepaddle >= 2.0.0
paddlehub >= 2.0.0
modules/image/classification/ghostnet_x0_5_imagenet/label_list.txt
0 → 100644
浏览文件 @
50dd5bf5
tench
goldfish
great white shark
tiger shark
hammerhead
electric ray
stingray
cock
hen
ostrich
brambling
goldfinch
house finch
junco
indigo bunting
robin
bulbul
jay
magpie
chickadee
water ouzel
kite
bald eagle
vulture
great grey owl
European fire salamander
common newt
eft
spotted salamander
axolotl
bullfrog
tree frog
tailed frog
loggerhead
leatherback turtle
mud turtle
terrapin
box turtle
banded gecko
common iguana
American chameleon
whiptail
agama
frilled lizard
alligator lizard
Gila monster
green lizard
African chameleon
Komodo dragon
African crocodile
American alligator
triceratops
thunder snake
ringneck snake
hognose snake
green snake
king snake
garter snake
water snake
vine snake
night snake
boa constrictor
rock python
Indian cobra
green mamba
sea snake
horned viper
diamondback
sidewinder
trilobite
harvestman
scorpion
black and gold garden spider
barn spider
garden spider
black widow
tarantula
wolf spider
tick
centipede
black grouse
ptarmigan
ruffed grouse
prairie chicken
peacock
quail
partridge
African grey
macaw
sulphur-crested cockatoo
lorikeet
coucal
bee eater
hornbill
hummingbird
jacamar
toucan
drake
red-breasted merganser
goose
black swan
tusker
echidna
platypus
wallaby
koala
wombat
jellyfish
sea anemone
brain coral
flatworm
nematode
conch
snail
slug
sea slug
chiton
chambered nautilus
Dungeness crab
rock crab
fiddler crab
king crab
American lobster
spiny lobster
crayfish
hermit crab
isopod
white stork
black stork
spoonbill
flamingo
little blue heron
American egret
bittern
crane
limpkin
European gallinule
American coot
bustard
ruddy turnstone
red-backed sandpiper
redshank
dowitcher
oystercatcher
pelican
king penguin
albatross
grey whale
killer whale
dugong
sea lion
Chihuahua
Japanese spaniel
Maltese dog
Pekinese
Shih-Tzu
Blenheim spaniel
papillon
toy terrier
Rhodesian ridgeback
Afghan hound
basset
beagle
bloodhound
bluetick
black-and-tan coonhound
Walker hound
English foxhound
redbone
borzoi
Irish wolfhound
Italian greyhound
whippet
Ibizan hound
Norwegian elkhound
otterhound
Saluki
Scottish deerhound
Weimaraner
Staffordshire bullterrier
American Staffordshire terrier
Bedlington terrier
Border terrier
Kerry blue terrier
Irish terrier
Norfolk terrier
Norwich terrier
Yorkshire terrier
wire-haired fox terrier
Lakeland terrier
Sealyham terrier
Airedale
cairn
Australian terrier
Dandie Dinmont
Boston bull
miniature schnauzer
giant schnauzer
standard schnauzer
Scotch terrier
Tibetan terrier
silky terrier
soft-coated wheaten terrier
West Highland white terrier
Lhasa
flat-coated retriever
curly-coated retriever
golden retriever
Labrador retriever
Chesapeake Bay retriever
German short-haired pointer
vizsla
English setter
Irish setter
Gordon setter
Brittany spaniel
clumber
English springer
Welsh springer spaniel
cocker spaniel
Sussex spaniel
Irish water spaniel
kuvasz
schipperke
groenendael
malinois
briard
kelpie
komondor
Old English sheepdog
Shetland sheepdog
collie
Border collie
Bouvier des Flandres
Rottweiler
German shepherd
Doberman
miniature pinscher
Greater Swiss Mountain dog
Bernese mountain dog
Appenzeller
EntleBucher
boxer
bull mastiff
Tibetan mastiff
French bulldog
Great Dane
Saint Bernard
Eskimo dog
malamute
Siberian husky
dalmatian
affenpinscher
basenji
pug
Leonberg
Newfoundland
Great Pyrenees
Samoyed
Pomeranian
chow
keeshond
Brabancon griffon
Pembroke
Cardigan
toy poodle
miniature poodle
standard poodle
Mexican hairless
timber wolf
white wolf
red wolf
coyote
dingo
dhole
African hunting dog
hyena
red fox
kit fox
Arctic fox
grey fox
tabby
tiger cat
Persian cat
Siamese cat
Egyptian cat
cougar
lynx
leopard
snow leopard
jaguar
lion
tiger
cheetah
brown bear
American black bear
ice bear
sloth bear
mongoose
meerkat
tiger beetle
ladybug
ground beetle
long-horned beetle
leaf beetle
dung beetle
rhinoceros beetle
weevil
fly
bee
ant
grasshopper
cricket
walking stick
cockroach
mantis
cicada
leafhopper
lacewing
dragonfly
damselfly
admiral
ringlet
monarch
cabbage butterfly
sulphur butterfly
lycaenid
starfish
sea urchin
sea cucumber
wood rabbit
hare
Angora
hamster
porcupine
fox squirrel
marmot
beaver
guinea pig
sorrel
zebra
hog
wild boar
warthog
hippopotamus
ox
water buffalo
bison
ram
bighorn
ibex
hartebeest
impala
gazelle
Arabian camel
llama
weasel
mink
polecat
black-footed ferret
otter
skunk
badger
armadillo
three-toed sloth
orangutan
gorilla
chimpanzee
gibbon
siamang
guenon
patas
baboon
macaque
langur
colobus
proboscis monkey
marmoset
capuchin
howler monkey
titi
spider monkey
squirrel monkey
Madagascar cat
indri
Indian elephant
African elephant
lesser panda
giant panda
barracouta
eel
coho
rock beauty
anemone fish
sturgeon
gar
lionfish
puffer
abacus
abaya
academic gown
accordion
acoustic guitar
aircraft carrier
airliner
airship
altar
ambulance
amphibian
analog clock
apiary
apron
ashcan
assault rifle
backpack
bakery
balance beam
balloon
ballpoint
Band Aid
banjo
bannister
barbell
barber chair
barbershop
barn
barometer
barrel
barrow
baseball
basketball
bassinet
bassoon
bathing cap
bath towel
bathtub
beach wagon
beacon
beaker
bearskin
beer bottle
beer glass
bell cote
bib
bicycle-built-for-two
bikini
binder
binoculars
birdhouse
boathouse
bobsled
bolo tie
bonnet
bookcase
bookshop
bottlecap
bow
bow tie
brass
brassiere
breakwater
breastplate
broom
bucket
buckle
bulletproof vest
bullet train
butcher shop
cab
caldron
candle
cannon
canoe
can opener
cardigan
car mirror
carousel
carpenters kit
carton
car wheel
cash machine
cassette
cassette player
castle
catamaran
CD player
cello
cellular telephone
chain
chainlink fence
chain mail
chain saw
chest
chiffonier
chime
china cabinet
Christmas stocking
church
cinema
cleaver
cliff dwelling
cloak
clog
cocktail shaker
coffee mug
coffeepot
coil
combination lock
computer keyboard
confectionery
container ship
convertible
corkscrew
cornet
cowboy boot
cowboy hat
cradle
crane
crash helmet
crate
crib
Crock Pot
croquet ball
crutch
cuirass
dam
desk
desktop computer
dial telephone
diaper
digital clock
digital watch
dining table
dishrag
dishwasher
disk brake
dock
dogsled
dome
doormat
drilling platform
drum
drumstick
dumbbell
Dutch oven
electric fan
electric guitar
electric locomotive
entertainment center
envelope
espresso maker
face powder
feather boa
file
fireboat
fire engine
fire screen
flagpole
flute
folding chair
football helmet
forklift
fountain
fountain pen
four-poster
freight car
French horn
frying pan
fur coat
garbage truck
gasmask
gas pump
goblet
go-kart
golf ball
golfcart
gondola
gong
gown
grand piano
greenhouse
grille
grocery store
guillotine
hair slide
hair spray
half track
hammer
hamper
hand blower
hand-held computer
handkerchief
hard disc
harmonica
harp
harvester
hatchet
holster
home theater
honeycomb
hook
hoopskirt
horizontal bar
horse cart
hourglass
iPod
iron
jack-o-lantern
jean
jeep
jersey
jigsaw puzzle
jinrikisha
joystick
kimono
knee pad
knot
lab coat
ladle
lampshade
laptop
lawn mower
lens cap
letter opener
library
lifeboat
lighter
limousine
liner
lipstick
Loafer
lotion
loudspeaker
loupe
lumbermill
magnetic compass
mailbag
mailbox
maillot
maillot
manhole cover
maraca
marimba
mask
matchstick
maypole
maze
measuring cup
medicine chest
megalith
microphone
microwave
military uniform
milk can
minibus
miniskirt
minivan
missile
mitten
mixing bowl
mobile home
Model T
modem
monastery
monitor
moped
mortar
mortarboard
mosque
mosquito net
motor scooter
mountain bike
mountain tent
mouse
mousetrap
moving van
muzzle
nail
neck brace
necklace
nipple
notebook
obelisk
oboe
ocarina
odometer
oil filter
organ
oscilloscope
overskirt
oxcart
oxygen mask
packet
paddle
paddlewheel
padlock
paintbrush
pajama
palace
panpipe
paper towel
parachute
parallel bars
park bench
parking meter
passenger car
patio
pay-phone
pedestal
pencil box
pencil sharpener
perfume
Petri dish
photocopier
pick
pickelhaube
picket fence
pickup
pier
piggy bank
pill bottle
pillow
ping-pong ball
pinwheel
pirate
pitcher
plane
planetarium
plastic bag
plate rack
plow
plunger
Polaroid camera
pole
police van
poncho
pool table
pop bottle
pot
potters wheel
power drill
prayer rug
printer
prison
projectile
projector
puck
punching bag
purse
quill
quilt
racer
racket
radiator
radio
radio telescope
rain barrel
recreational vehicle
reel
reflex camera
refrigerator
remote control
restaurant
revolver
rifle
rocking chair
rotisserie
rubber eraser
rugby ball
rule
running shoe
safe
safety pin
saltshaker
sandal
sarong
sax
scabbard
scale
school bus
schooner
scoreboard
screen
screw
screwdriver
seat belt
sewing machine
shield
shoe shop
shoji
shopping basket
shopping cart
shovel
shower cap
shower curtain
ski
ski mask
sleeping bag
slide rule
sliding door
slot
snorkel
snowmobile
snowplow
soap dispenser
soccer ball
sock
solar dish
sombrero
soup bowl
space bar
space heater
space shuttle
spatula
speedboat
spider web
spindle
sports car
spotlight
stage
steam locomotive
steel arch bridge
steel drum
stethoscope
stole
stone wall
stopwatch
stove
strainer
streetcar
stretcher
studio couch
stupa
submarine
suit
sundial
sunglass
sunglasses
sunscreen
suspension bridge
swab
sweatshirt
swimming trunks
swing
switch
syringe
table lamp
tank
tape player
teapot
teddy
television
tennis ball
thatch
theater curtain
thimble
thresher
throne
tile roof
toaster
tobacco shop
toilet seat
torch
totem pole
tow truck
toyshop
tractor
trailer truck
tray
trench coat
tricycle
trimaran
tripod
triumphal arch
trolleybus
trombone
tub
turnstile
typewriter keyboard
umbrella
unicycle
upright
vacuum
vase
vault
velvet
vending machine
vestment
viaduct
violin
volleyball
waffle iron
wall clock
wallet
wardrobe
warplane
washbasin
washer
water bottle
water jug
water tower
whiskey jug
whistle
wig
window screen
window shade
Windsor tie
wine bottle
wing
wok
wooden spoon
wool
worm fence
wreck
yawl
yurt
web site
comic book
crossword puzzle
street sign
traffic light
book jacket
menu
plate
guacamole
consomme
hot pot
trifle
ice cream
ice lolly
French loaf
bagel
pretzel
cheeseburger
hotdog
mashed potato
head cabbage
broccoli
cauliflower
zucchini
spaghetti squash
acorn squash
butternut squash
cucumber
artichoke
bell pepper
cardoon
mushroom
Granny Smith
strawberry
orange
lemon
fig
pineapple
banana
jackfruit
custard apple
pomegranate
hay
carbonara
chocolate sauce
dough
meat loaf
pizza
potpie
burrito
red wine
espresso
cup
eggnog
alp
bubble
cliff
coral reef
geyser
lakeside
promontory
sandbar
seashore
valley
volcano
ballplayer
groom
scuba diver
rapeseed
daisy
yellow ladys slipper
corn
acorn
hip
buckeye
coral fungus
agaric
gyromitra
stinkhorn
earthstar
hen-of-the-woods
bolete
ear
toilet tissue
modules/image/classification/ghostnet_x0_5_imagenet/module.py
0 → 100644
浏览文件 @
50dd5bf5
# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
os
import
math
from
typing
import
Union
import
paddle
import
paddle.nn
as
nn
import
paddle.nn.functional
as
F
import
paddlehub.vision.transforms
as
T
import
numpy
as
np
from
paddle
import
ParamAttr
from
paddle.nn.initializer
import
Uniform
,
KaimingNormal
from
paddlehub.module.module
import
moduleinfo
from
paddlehub.module.cv_module
import
ImageClassifierModule
class
ConvBNLayer
(
nn
.
Layer
):
def
__init__
(
self
,
in_channels
,
out_channels
,
kernel_size
,
stride
=
1
,
groups
=
1
,
act
=
"relu"
,
name
=
None
):
super
(
ConvBNLayer
,
self
).
__init__
()
self
.
_conv
=
nn
.
Conv2D
(
in_channels
=
in_channels
,
out_channels
=
out_channels
,
kernel_size
=
kernel_size
,
stride
=
stride
,
padding
=
(
kernel_size
-
1
)
//
2
,
groups
=
groups
,
weight_attr
=
ParamAttr
(
initializer
=
KaimingNormal
(),
name
=
name
+
"_weights"
),
bias_attr
=
False
)
bn_name
=
name
+
"_bn"
self
.
_batch_norm
=
nn
.
BatchNorm
(
num_channels
=
out_channels
,
act
=
act
,
param_attr
=
ParamAttr
(
name
=
bn_name
+
"_scale"
,
regularizer
=
paddle
.
regularizer
.
L2Decay
(
0.0
)),
bias_attr
=
ParamAttr
(
name
=
bn_name
+
"_offset"
,
regularizer
=
paddle
.
regularizer
.
L2Decay
(
0.0
)),
moving_mean_name
=
bn_name
+
"_mean"
,
moving_variance_name
=
bn_name
+
"_variance"
)
def
forward
(
self
,
inputs
):
y
=
self
.
_conv
(
inputs
)
y
=
self
.
_batch_norm
(
y
)
return
y
class
SEBlock
(
nn
.
Layer
):
def
__init__
(
self
,
num_channels
,
reduction_ratio
=
4
,
name
=
None
):
super
(
SEBlock
,
self
).
__init__
()
self
.
pool2d_gap
=
nn
.
AdaptiveAvgPool2D
(
1
)
self
.
_num_channels
=
num_channels
stdv
=
1.0
/
math
.
sqrt
(
num_channels
*
1.0
)
med_ch
=
num_channels
//
reduction_ratio
self
.
squeeze
=
nn
.
Linear
(
num_channels
,
med_ch
,
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
),
name
=
name
+
"_1_weights"
),
bias_attr
=
ParamAttr
(
name
=
name
+
"_1_offset"
))
stdv
=
1.0
/
math
.
sqrt
(
med_ch
*
1.0
)
self
.
excitation
=
nn
.
Linear
(
med_ch
,
num_channels
,
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
),
name
=
name
+
"_2_weights"
),
bias_attr
=
ParamAttr
(
name
=
name
+
"_2_offset"
))
def
forward
(
self
,
inputs
):
pool
=
self
.
pool2d_gap
(
inputs
)
pool
=
paddle
.
squeeze
(
pool
,
axis
=
[
2
,
3
])
squeeze
=
self
.
squeeze
(
pool
)
squeeze
=
F
.
relu
(
squeeze
)
excitation
=
self
.
excitation
(
squeeze
)
excitation
=
paddle
.
clip
(
x
=
excitation
,
min
=
0
,
max
=
1
)
excitation
=
paddle
.
unsqueeze
(
excitation
,
axis
=
[
2
,
3
])
out
=
paddle
.
multiply
(
inputs
,
excitation
)
return
out
class
GhostModule
(
nn
.
Layer
):
def
__init__
(
self
,
in_channels
,
output_channels
,
kernel_size
=
1
,
ratio
=
2
,
dw_size
=
3
,
stride
=
1
,
relu
=
True
,
name
=
None
):
super
(
GhostModule
,
self
).
__init__
()
init_channels
=
int
(
math
.
ceil
(
output_channels
/
ratio
))
new_channels
=
int
(
init_channels
*
(
ratio
-
1
))
self
.
primary_conv
=
ConvBNLayer
(
in_channels
=
in_channels
,
out_channels
=
init_channels
,
kernel_size
=
kernel_size
,
stride
=
stride
,
groups
=
1
,
act
=
"relu"
if
relu
else
None
,
name
=
name
+
"_primary_conv"
)
self
.
cheap_operation
=
ConvBNLayer
(
in_channels
=
init_channels
,
out_channels
=
new_channels
,
kernel_size
=
dw_size
,
stride
=
1
,
groups
=
init_channels
,
act
=
"relu"
if
relu
else
None
,
name
=
name
+
"_cheap_operation"
)
def
forward
(
self
,
inputs
):
x
=
self
.
primary_conv
(
inputs
)
y
=
self
.
cheap_operation
(
x
)
out
=
paddle
.
concat
([
x
,
y
],
axis
=
1
)
return
out
class
GhostBottleneck
(
nn
.
Layer
):
def
__init__
(
self
,
in_channels
,
hidden_dim
,
output_channels
,
kernel_size
,
stride
,
use_se
,
name
=
None
):
super
(
GhostBottleneck
,
self
).
__init__
()
self
.
_stride
=
stride
self
.
_use_se
=
use_se
self
.
_num_channels
=
in_channels
self
.
_output_channels
=
output_channels
self
.
ghost_module_1
=
GhostModule
(
in_channels
=
in_channels
,
output_channels
=
hidden_dim
,
kernel_size
=
1
,
stride
=
1
,
relu
=
True
,
name
=
name
+
"_ghost_module_1"
)
if
stride
==
2
:
self
.
depthwise_conv
=
ConvBNLayer
(
in_channels
=
hidden_dim
,
out_channels
=
hidden_dim
,
kernel_size
=
kernel_size
,
stride
=
stride
,
groups
=
hidden_dim
,
act
=
None
,
name
=
name
+
"_depthwise_depthwise"
# looks strange due to an old typo, will be fixed later.
)
if
use_se
:
self
.
se_block
=
SEBlock
(
num_channels
=
hidden_dim
,
name
=
name
+
"_se"
)
self
.
ghost_module_2
=
GhostModule
(
in_channels
=
hidden_dim
,
output_channels
=
output_channels
,
kernel_size
=
1
,
relu
=
False
,
name
=
name
+
"_ghost_module_2"
)
if
stride
!=
1
or
in_channels
!=
output_channels
:
self
.
shortcut_depthwise
=
ConvBNLayer
(
in_channels
=
in_channels
,
out_channels
=
in_channels
,
kernel_size
=
kernel_size
,
stride
=
stride
,
groups
=
in_channels
,
act
=
None
,
name
=
name
+
"_shortcut_depthwise_depthwise"
# looks strange due to an old typo, will be fixed later.
)
self
.
shortcut_conv
=
ConvBNLayer
(
in_channels
=
in_channels
,
out_channels
=
output_channels
,
kernel_size
=
1
,
stride
=
1
,
groups
=
1
,
act
=
None
,
name
=
name
+
"_shortcut_conv"
)
def
forward
(
self
,
inputs
):
x
=
self
.
ghost_module_1
(
inputs
)
if
self
.
_stride
==
2
:
x
=
self
.
depthwise_conv
(
x
)
if
self
.
_use_se
:
x
=
self
.
se_block
(
x
)
x
=
self
.
ghost_module_2
(
x
)
if
self
.
_stride
==
1
and
self
.
_num_channels
==
self
.
_output_channels
:
shortcut
=
inputs
else
:
shortcut
=
self
.
shortcut_depthwise
(
inputs
)
shortcut
=
self
.
shortcut_conv
(
shortcut
)
return
paddle
.
add
(
x
=
x
,
y
=
shortcut
)
@
moduleinfo
(
name
=
"ghostnet_x0_5_imagenet"
,
type
=
"CV/classification"
,
author
=
"paddlepaddle"
,
author_email
=
""
,
summary
=
"ghostnet_x0_5_imagenet is a classification model, "
"this module is trained with Imagenet dataset."
,
version
=
"1.0.0"
,
meta
=
ImageClassifierModule
)
class
GhostNet
(
nn
.
Layer
):
def
__init__
(
self
,
label_list
:
list
=
None
,
load_checkpoint
:
str
=
None
):
super
(
GhostNet
,
self
).
__init__
()
if
label_list
is
not
None
:
self
.
labels
=
label_list
class_dim
=
len
(
self
.
labels
)
else
:
label_list
=
[]
label_file
=
os
.
path
.
join
(
self
.
directory
,
'label_list.txt'
)
files
=
open
(
label_file
)
for
line
in
files
.
readlines
():
line
=
line
.
strip
(
'
\n
'
)
label_list
.
append
(
line
)
self
.
labels
=
label_list
class_dim
=
len
(
self
.
labels
)
self
.
cfgs
=
[
# k, t, c, SE, s
[
3
,
16
,
16
,
0
,
1
],
[
3
,
48
,
24
,
0
,
2
],
[
3
,
72
,
24
,
0
,
1
],
[
5
,
72
,
40
,
1
,
2
],
[
5
,
120
,
40
,
1
,
1
],
[
3
,
240
,
80
,
0
,
2
],
[
3
,
200
,
80
,
0
,
1
],
[
3
,
184
,
80
,
0
,
1
],
[
3
,
184
,
80
,
0
,
1
],
[
3
,
480
,
112
,
1
,
1
],
[
3
,
672
,
112
,
1
,
1
],
[
5
,
672
,
160
,
1
,
2
],
[
5
,
960
,
160
,
0
,
1
],
[
5
,
960
,
160
,
1
,
1
],
[
5
,
960
,
160
,
0
,
1
],
[
5
,
960
,
160
,
1
,
1
]
]
self
.
scale
=
0.5
output_channels
=
int
(
self
.
_make_divisible
(
16
*
self
.
scale
,
4
))
self
.
conv1
=
ConvBNLayer
(
in_channels
=
3
,
out_channels
=
output_channels
,
kernel_size
=
3
,
stride
=
2
,
groups
=
1
,
act
=
"relu"
,
name
=
"conv1"
)
# build inverted residual blocks
idx
=
0
self
.
ghost_bottleneck_list
=
[]
for
k
,
exp_size
,
c
,
use_se
,
s
in
self
.
cfgs
:
in_channels
=
output_channels
output_channels
=
int
(
self
.
_make_divisible
(
c
*
self
.
scale
,
4
))
hidden_dim
=
int
(
self
.
_make_divisible
(
exp_size
*
self
.
scale
,
4
))
ghost_bottleneck
=
self
.
add_sublayer
(
name
=
"_ghostbottleneck_"
+
str
(
idx
),
sublayer
=
GhostBottleneck
(
in_channels
=
in_channels
,
hidden_dim
=
hidden_dim
,
output_channels
=
output_channels
,
kernel_size
=
k
,
stride
=
s
,
use_se
=
use_se
,
name
=
"_ghostbottleneck_"
+
str
(
idx
)))
self
.
ghost_bottleneck_list
.
append
(
ghost_bottleneck
)
idx
+=
1
# build last several layers
in_channels
=
output_channels
output_channels
=
int
(
self
.
_make_divisible
(
exp_size
*
self
.
scale
,
4
))
self
.
conv_last
=
ConvBNLayer
(
in_channels
=
in_channels
,
out_channels
=
output_channels
,
kernel_size
=
1
,
stride
=
1
,
groups
=
1
,
act
=
"relu"
,
name
=
"conv_last"
)
self
.
pool2d_gap
=
nn
.
AdaptiveAvgPool2D
(
1
)
in_channels
=
output_channels
self
.
_fc0_output_channels
=
1280
self
.
fc_0
=
ConvBNLayer
(
in_channels
=
in_channels
,
out_channels
=
self
.
_fc0_output_channels
,
kernel_size
=
1
,
stride
=
1
,
act
=
"relu"
,
name
=
"fc_0"
)
self
.
dropout
=
nn
.
Dropout
(
p
=
0.2
)
stdv
=
1.0
/
math
.
sqrt
(
self
.
_fc0_output_channels
*
1.0
)
self
.
fc_1
=
nn
.
Linear
(
self
.
_fc0_output_channels
,
class_dim
,
weight_attr
=
ParamAttr
(
name
=
"fc_1_weights"
,
initializer
=
Uniform
(
-
stdv
,
stdv
)),
bias_attr
=
ParamAttr
(
name
=
"fc_1_offset"
))
if
load_checkpoint
is
not
None
:
self
.
model_dict
=
paddle
.
load
(
load_checkpoint
)
self
.
set_dict
(
self
.
model_dict
)
print
(
"load custom checkpoint success"
)
else
:
checkpoint
=
os
.
path
.
join
(
self
.
directory
,
'model.pdparams'
)
self
.
model_dict
=
paddle
.
load
(
checkpoint
)
self
.
set_dict
(
self
.
model_dict
)
print
(
"load pretrained checkpoint success"
)
def
transforms
(
self
,
images
:
Union
[
str
,
np
.
ndarray
]):
transforms
=
T
.
Compose
([
T
.
Resize
((
256
,
256
)),
T
.
CenterCrop
(
224
),
T
.
Normalize
(
mean
=
[
0.485
,
0.456
,
0.406
],
std
=
[
0.229
,
0.224
,
0.225
])
],
to_rgb
=
True
)
return
transforms
(
images
).
astype
(
'float32'
)
def
forward
(
self
,
inputs
):
x
=
self
.
conv1
(
inputs
)
for
ghost_bottleneck
in
self
.
ghost_bottleneck_list
:
x
=
ghost_bottleneck
(
x
)
x
=
self
.
conv_last
(
x
)
feature
=
self
.
pool2d_gap
(
x
)
x
=
self
.
fc_0
(
feature
)
x
=
self
.
dropout
(
x
)
x
=
paddle
.
reshape
(
x
,
shape
=
[
-
1
,
self
.
_fc0_output_channels
])
x
=
self
.
fc_1
(
x
)
return
x
,
feature
def
_make_divisible
(
self
,
v
,
divisor
,
min_value
=
None
):
"""
This function is taken from the original tf repo.
It ensures that all layers have a channel number that is divisible by 8
It can be seen here:
https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
"""
if
min_value
is
None
:
min_value
=
divisor
new_v
=
max
(
min_value
,
int
(
v
+
divisor
/
2
)
//
divisor
*
divisor
)
# Make sure that round down does not go down by more than 10%.
if
new_v
<
0.9
*
v
:
new_v
+=
divisor
return
new_v
modules/image/classification/ghostnet_x1_0_imagenet/README.md
0 → 100644
浏览文件 @
50dd5bf5
```
shell
$
hub
install
ghostnet_x1_0_imagenet
==
1.0.0
```
## 命令行预测
```
shell
$
hub run ghostnet_x1_0_imagenet
--input_path
"/PATH/TO/IMAGE"
--top_k
5
```
## 脚本预测
```
python
import
paddle
import
paddlehub
as
hub
if
__name__
==
'__main__'
:
model
=
hub
.
Module
(
name
=
'ghostnet_x1_0_imagenet'
,)
result
=
model
.
predict
([
PATH
/
TO
/
IMAGE
])
```
## Fine-tune代码步骤
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 定义数据预处理方式
```
python
import
paddlehub.vision.transforms
as
T
transforms
=
T
.
Compose
([
T
.
Resize
((
256
,
256
)),
T
.
CenterCrop
(
224
),
T
.
Normalize
(
mean
=
[
0.485
,
0.456
,
0.406
],
std
=
[
0.229
,
0.224
,
0.225
])],
to_rgb
=
True
)
```
'transforms' 数据增强模块定义了丰富的数据预处理方式,用户可按照需求替换自己需要的数据预处理方式。
### Step2: 下载数据集并使用
```
python
from
paddlehub.datasets
import
Flowers
flowers
=
Flowers
(
transforms
)
flowers_validate
=
Flowers
(
transforms
,
mode
=
'val'
)
```
*
transforms(Callable): 数据预处理方式。
*
mode(str): 选择数据模式,可选项有 'train', 'test', 'val', 默认为'train'。
'hub.datasets.Flowers()' 会自动从网络下载数据集并解压到用户目录下'$HOME/.paddlehub/dataset'目录。
### Step3: 加载预训练模型
```
python
import
paddlehub
as
hub
model
=
hub
.
Module
(
name
=
'ghostnet_x1_0_imagenet'
,
label_list
=
[
"roses"
,
"tulips"
,
"daisy"
,
"sunflowers"
,
"dandelion"
],
load_checkpoint
=
None
)
```
*
name(str): 选择预训练模型的名字。
*
label_list(list): 设置标签对应分类类别, 默认为Imagenet2012类别。
*
load _checkpoint(str): 模型参数地址。
PaddleHub提供许多图像分类预训练模型,如xception、mobilenet、efficientnet等,详细信息参见
[
图像分类模型
](
https://www.paddlepaddle.org.cn/hub?filter=en_category&value=ImageClassification
)
。
如果想尝试efficientnet模型,只需要更换Module中的'name'参数即可.
```
python
import
paddlehub
as
hub
# 更换name参数即可无缝切换efficientnet模型, 代码示例如下
module
=
hub
.
Module
(
name
=
"efficientnetb7_imagenet"
)
```
**NOTE:**
目前部分模型还没有完全升级到2.0版本,敬请期待。
### Step4: 选择优化策略和运行配置
```
python
import
paddle
from
paddlehub.finetune.trainer
import
Trainer
optimizer
=
paddle
.
optimizer
.
Adam
(
learning_rate
=
0.001
,
parameters
=
model
.
parameters
())
trainer
=
Trainer
(
model
,
optimizer
,
checkpoint_dir
=
'img_classification_ckpt'
)
trainer
.
train
(
flowers
,
epochs
=
100
,
batch_size
=
32
,
eval_dataset
=
flowers_validate
,
save_interval
=
1
)
```
#### 优化策略
Paddle2.0rc提供了多种优化器选择,如'SGD', 'Adam', 'Adamax'等,详细参见
[
策略
](
https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/optimizer/optimizer/Optimizer_cn.html
)
。
其中'Adam':
*
learning_rate: 全局学习率。默认为1e-3;
*
parameters: 待优化模型参数。
#### 运行配置
'Trainer' 主要控制Fine-tune的训练,包含以下可控制的参数:
*
model: 被优化模型;
*
optimizer: 优化器选择;
*
use_vdl: 是否使用vdl可视化训练过程;
*
checkpoint_dir: 保存模型参数的地址;
*
compare_metrics: 保存最优模型的衡量指标;
'trainer.train' 主要控制具体的训练过程,包含以下可控制的参数:
*
train_dataset: 训练时所用的数据集;
*
epochs: 训练轮数;
*
batch_size: 训练的批大小,如果使用GPU,请根据实际情况调整batch_size;
*
num_workers: works的数量,默认为0;
*
eval_dataset: 验证集;
*
log_interval: 打印日志的间隔, 单位为执行批训练的次数。
*
save_interval: 保存模型的间隔频次,单位为执行训练的轮数。
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在'${CHECKPOINT_DIR}/best_model'目录下,其中'${CHECKPOINT_DIR}'目录为Fine-tune时所选择的保存checkpoint的目录。
我们使用该模型来进行预测。predict.py脚本如下:
```
python
import
paddle
import
paddlehub
as
hub
if
__name__
==
'__main__'
:
model
=
hub
.
Module
(
name
=
'ghostnet_x1_0_imagenet'
,
label_list
=
[
"roses"
,
"tulips"
,
"daisy"
,
"sunflowers"
,
"dandelion"
],
load_checkpoint
=
'/PATH/TO/CHECKPOINT'
)
result
=
model
.
predict
([
'flower.jpg'
])
```
参数配置正确后,请执行脚本'python predict.py', 加载模型具体可参见
[
加载
](
https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/framework/io/load_cn.html#load
)
。
**NOTE:**
进行预测时,所选择的module,checkpoint_dir,dataset必须和Fine-tune所用的一样。
## 服务部署
PaddleHub Serving可以部署一个在线分类任务服务
## Step1: 启动PaddleHub Serving
运行启动命令:
```
shell
$
hub serving start
-m
ghostnet_x1_0_imagenet
```
这样就完成了一个分类任务服务化API的部署,默认端口号为8866。
**NOTE:**
如使用GPU预测,则需要在启动服务之前,请设置CUDA_VISIBLE_DEVICES环境变量,否则不用设置。
## Step2: 发送预测请求
配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
```
python
import
requests
import
json
import
cv2
import
base64
import
numpy
as
np
def
cv2_to_base64
(
image
):
data
=
cv2
.
imencode
(
'.jpg'
,
image
)[
1
]
return
base64
.
b64encode
(
data
.
tostring
()).
decode
(
'utf8'
)
def
base64_to_cv2
(
b64str
):
data
=
base64
.
b64decode
(
b64str
.
encode
(
'utf8'
))
data
=
np
.
fromstring
(
data
,
np
.
uint8
)
data
=
cv2
.
imdecode
(
data
,
cv2
.
IMREAD_COLOR
)
return
data
# 发送HTTP请求
org_im
=
cv2
.
imread
(
'/PATH/TO/IMAGE'
)
data
=
{
'images'
:[
cv2_to_base64
(
org_im
)],
'top_k'
:
2
}
headers
=
{
"Content-type"
:
"application/json"
}
url
=
"http://127.0.0.1:8866/predict/ghostnet_x1_0_imagenet"
r
=
requests
.
post
(
url
=
url
,
headers
=
headers
,
data
=
json
.
dumps
(
data
))
data
=
r
.
json
()[
"results"
][
'data'
]
```
### 查看代码
[
PaddleClas
](
https://github.com/PaddlePaddle/PaddleClas
)
### 依赖
paddlepaddle >= 2.0.0
paddlehub >= 2.0.0
modules/image/classification/ghostnet_x1_0_imagenet/label_list.txt
0 → 100644
浏览文件 @
50dd5bf5
tench
goldfish
great white shark
tiger shark
hammerhead
electric ray
stingray
cock
hen
ostrich
brambling
goldfinch
house finch
junco
indigo bunting
robin
bulbul
jay
magpie
chickadee
water ouzel
kite
bald eagle
vulture
great grey owl
European fire salamander
common newt
eft
spotted salamander
axolotl
bullfrog
tree frog
tailed frog
loggerhead
leatherback turtle
mud turtle
terrapin
box turtle
banded gecko
common iguana
American chameleon
whiptail
agama
frilled lizard
alligator lizard
Gila monster
green lizard
African chameleon
Komodo dragon
African crocodile
American alligator
triceratops
thunder snake
ringneck snake
hognose snake
green snake
king snake
garter snake
water snake
vine snake
night snake
boa constrictor
rock python
Indian cobra
green mamba
sea snake
horned viper
diamondback
sidewinder
trilobite
harvestman
scorpion
black and gold garden spider
barn spider
garden spider
black widow
tarantula
wolf spider
tick
centipede
black grouse
ptarmigan
ruffed grouse
prairie chicken
peacock
quail
partridge
African grey
macaw
sulphur-crested cockatoo
lorikeet
coucal
bee eater
hornbill
hummingbird
jacamar
toucan
drake
red-breasted merganser
goose
black swan
tusker
echidna
platypus
wallaby
koala
wombat
jellyfish
sea anemone
brain coral
flatworm
nematode
conch
snail
slug
sea slug
chiton
chambered nautilus
Dungeness crab
rock crab
fiddler crab
king crab
American lobster
spiny lobster
crayfish
hermit crab
isopod
white stork
black stork
spoonbill
flamingo
little blue heron
American egret
bittern
crane
limpkin
European gallinule
American coot
bustard
ruddy turnstone
red-backed sandpiper
redshank
dowitcher
oystercatcher
pelican
king penguin
albatross
grey whale
killer whale
dugong
sea lion
Chihuahua
Japanese spaniel
Maltese dog
Pekinese
Shih-Tzu
Blenheim spaniel
papillon
toy terrier
Rhodesian ridgeback
Afghan hound
basset
beagle
bloodhound
bluetick
black-and-tan coonhound
Walker hound
English foxhound
redbone
borzoi
Irish wolfhound
Italian greyhound
whippet
Ibizan hound
Norwegian elkhound
otterhound
Saluki
Scottish deerhound
Weimaraner
Staffordshire bullterrier
American Staffordshire terrier
Bedlington terrier
Border terrier
Kerry blue terrier
Irish terrier
Norfolk terrier
Norwich terrier
Yorkshire terrier
wire-haired fox terrier
Lakeland terrier
Sealyham terrier
Airedale
cairn
Australian terrier
Dandie Dinmont
Boston bull
miniature schnauzer
giant schnauzer
standard schnauzer
Scotch terrier
Tibetan terrier
silky terrier
soft-coated wheaten terrier
West Highland white terrier
Lhasa
flat-coated retriever
curly-coated retriever
golden retriever
Labrador retriever
Chesapeake Bay retriever
German short-haired pointer
vizsla
English setter
Irish setter
Gordon setter
Brittany spaniel
clumber
English springer
Welsh springer spaniel
cocker spaniel
Sussex spaniel
Irish water spaniel
kuvasz
schipperke
groenendael
malinois
briard
kelpie
komondor
Old English sheepdog
Shetland sheepdog
collie
Border collie
Bouvier des Flandres
Rottweiler
German shepherd
Doberman
miniature pinscher
Greater Swiss Mountain dog
Bernese mountain dog
Appenzeller
EntleBucher
boxer
bull mastiff
Tibetan mastiff
French bulldog
Great Dane
Saint Bernard
Eskimo dog
malamute
Siberian husky
dalmatian
affenpinscher
basenji
pug
Leonberg
Newfoundland
Great Pyrenees
Samoyed
Pomeranian
chow
keeshond
Brabancon griffon
Pembroke
Cardigan
toy poodle
miniature poodle
standard poodle
Mexican hairless
timber wolf
white wolf
red wolf
coyote
dingo
dhole
African hunting dog
hyena
red fox
kit fox
Arctic fox
grey fox
tabby
tiger cat
Persian cat
Siamese cat
Egyptian cat
cougar
lynx
leopard
snow leopard
jaguar
lion
tiger
cheetah
brown bear
American black bear
ice bear
sloth bear
mongoose
meerkat
tiger beetle
ladybug
ground beetle
long-horned beetle
leaf beetle
dung beetle
rhinoceros beetle
weevil
fly
bee
ant
grasshopper
cricket
walking stick
cockroach
mantis
cicada
leafhopper
lacewing
dragonfly
damselfly
admiral
ringlet
monarch
cabbage butterfly
sulphur butterfly
lycaenid
starfish
sea urchin
sea cucumber
wood rabbit
hare
Angora
hamster
porcupine
fox squirrel
marmot
beaver
guinea pig
sorrel
zebra
hog
wild boar
warthog
hippopotamus
ox
water buffalo
bison
ram
bighorn
ibex
hartebeest
impala
gazelle
Arabian camel
llama
weasel
mink
polecat
black-footed ferret
otter
skunk
badger
armadillo
three-toed sloth
orangutan
gorilla
chimpanzee
gibbon
siamang
guenon
patas
baboon
macaque
langur
colobus
proboscis monkey
marmoset
capuchin
howler monkey
titi
spider monkey
squirrel monkey
Madagascar cat
indri
Indian elephant
African elephant
lesser panda
giant panda
barracouta
eel
coho
rock beauty
anemone fish
sturgeon
gar
lionfish
puffer
abacus
abaya
academic gown
accordion
acoustic guitar
aircraft carrier
airliner
airship
altar
ambulance
amphibian
analog clock
apiary
apron
ashcan
assault rifle
backpack
bakery
balance beam
balloon
ballpoint
Band Aid
banjo
bannister
barbell
barber chair
barbershop
barn
barometer
barrel
barrow
baseball
basketball
bassinet
bassoon
bathing cap
bath towel
bathtub
beach wagon
beacon
beaker
bearskin
beer bottle
beer glass
bell cote
bib
bicycle-built-for-two
bikini
binder
binoculars
birdhouse
boathouse
bobsled
bolo tie
bonnet
bookcase
bookshop
bottlecap
bow
bow tie
brass
brassiere
breakwater
breastplate
broom
bucket
buckle
bulletproof vest
bullet train
butcher shop
cab
caldron
candle
cannon
canoe
can opener
cardigan
car mirror
carousel
carpenters kit
carton
car wheel
cash machine
cassette
cassette player
castle
catamaran
CD player
cello
cellular telephone
chain
chainlink fence
chain mail
chain saw
chest
chiffonier
chime
china cabinet
Christmas stocking
church
cinema
cleaver
cliff dwelling
cloak
clog
cocktail shaker
coffee mug
coffeepot
coil
combination lock
computer keyboard
confectionery
container ship
convertible
corkscrew
cornet
cowboy boot
cowboy hat
cradle
crane
crash helmet
crate
crib
Crock Pot
croquet ball
crutch
cuirass
dam
desk
desktop computer
dial telephone
diaper
digital clock
digital watch
dining table
dishrag
dishwasher
disk brake
dock
dogsled
dome
doormat
drilling platform
drum
drumstick
dumbbell
Dutch oven
electric fan
electric guitar
electric locomotive
entertainment center
envelope
espresso maker
face powder
feather boa
file
fireboat
fire engine
fire screen
flagpole
flute
folding chair
football helmet
forklift
fountain
fountain pen
four-poster
freight car
French horn
frying pan
fur coat
garbage truck
gasmask
gas pump
goblet
go-kart
golf ball
golfcart
gondola
gong
gown
grand piano
greenhouse
grille
grocery store
guillotine
hair slide
hair spray
half track
hammer
hamper
hand blower
hand-held computer
handkerchief
hard disc
harmonica
harp
harvester
hatchet
holster
home theater
honeycomb
hook
hoopskirt
horizontal bar
horse cart
hourglass
iPod
iron
jack-o-lantern
jean
jeep
jersey
jigsaw puzzle
jinrikisha
joystick
kimono
knee pad
knot
lab coat
ladle
lampshade
laptop
lawn mower
lens cap
letter opener
library
lifeboat
lighter
limousine
liner
lipstick
Loafer
lotion
loudspeaker
loupe
lumbermill
magnetic compass
mailbag
mailbox
maillot
maillot
manhole cover
maraca
marimba
mask
matchstick
maypole
maze
measuring cup
medicine chest
megalith
microphone
microwave
military uniform
milk can
minibus
miniskirt
minivan
missile
mitten
mixing bowl
mobile home
Model T
modem
monastery
monitor
moped
mortar
mortarboard
mosque
mosquito net
motor scooter
mountain bike
mountain tent
mouse
mousetrap
moving van
muzzle
nail
neck brace
necklace
nipple
notebook
obelisk
oboe
ocarina
odometer
oil filter
organ
oscilloscope
overskirt
oxcart
oxygen mask
packet
paddle
paddlewheel
padlock
paintbrush
pajama
palace
panpipe
paper towel
parachute
parallel bars
park bench
parking meter
passenger car
patio
pay-phone
pedestal
pencil box
pencil sharpener
perfume
Petri dish
photocopier
pick
pickelhaube
picket fence
pickup
pier
piggy bank
pill bottle
pillow
ping-pong ball
pinwheel
pirate
pitcher
plane
planetarium
plastic bag
plate rack
plow
plunger
Polaroid camera
pole
police van
poncho
pool table
pop bottle
pot
potters wheel
power drill
prayer rug
printer
prison
projectile
projector
puck
punching bag
purse
quill
quilt
racer
racket
radiator
radio
radio telescope
rain barrel
recreational vehicle
reel
reflex camera
refrigerator
remote control
restaurant
revolver
rifle
rocking chair
rotisserie
rubber eraser
rugby ball
rule
running shoe
safe
safety pin
saltshaker
sandal
sarong
sax
scabbard
scale
school bus
schooner
scoreboard
screen
screw
screwdriver
seat belt
sewing machine
shield
shoe shop
shoji
shopping basket
shopping cart
shovel
shower cap
shower curtain
ski
ski mask
sleeping bag
slide rule
sliding door
slot
snorkel
snowmobile
snowplow
soap dispenser
soccer ball
sock
solar dish
sombrero
soup bowl
space bar
space heater
space shuttle
spatula
speedboat
spider web
spindle
sports car
spotlight
stage
steam locomotive
steel arch bridge
steel drum
stethoscope
stole
stone wall
stopwatch
stove
strainer
streetcar
stretcher
studio couch
stupa
submarine
suit
sundial
sunglass
sunglasses
sunscreen
suspension bridge
swab
sweatshirt
swimming trunks
swing
switch
syringe
table lamp
tank
tape player
teapot
teddy
television
tennis ball
thatch
theater curtain
thimble
thresher
throne
tile roof
toaster
tobacco shop
toilet seat
torch
totem pole
tow truck
toyshop
tractor
trailer truck
tray
trench coat
tricycle
trimaran
tripod
triumphal arch
trolleybus
trombone
tub
turnstile
typewriter keyboard
umbrella
unicycle
upright
vacuum
vase
vault
velvet
vending machine
vestment
viaduct
violin
volleyball
waffle iron
wall clock
wallet
wardrobe
warplane
washbasin
washer
water bottle
water jug
water tower
whiskey jug
whistle
wig
window screen
window shade
Windsor tie
wine bottle
wing
wok
wooden spoon
wool
worm fence
wreck
yawl
yurt
web site
comic book
crossword puzzle
street sign
traffic light
book jacket
menu
plate
guacamole
consomme
hot pot
trifle
ice cream
ice lolly
French loaf
bagel
pretzel
cheeseburger
hotdog
mashed potato
head cabbage
broccoli
cauliflower
zucchini
spaghetti squash
acorn squash
butternut squash
cucumber
artichoke
bell pepper
cardoon
mushroom
Granny Smith
strawberry
orange
lemon
fig
pineapple
banana
jackfruit
custard apple
pomegranate
hay
carbonara
chocolate sauce
dough
meat loaf
pizza
potpie
burrito
red wine
espresso
cup
eggnog
alp
bubble
cliff
coral reef
geyser
lakeside
promontory
sandbar
seashore
valley
volcano
ballplayer
groom
scuba diver
rapeseed
daisy
yellow ladys slipper
corn
acorn
hip
buckeye
coral fungus
agaric
gyromitra
stinkhorn
earthstar
hen-of-the-woods
bolete
ear
toilet tissue
modules/image/classification/ghostnet_x1_0_imagenet/module.py
0 → 100644
浏览文件 @
50dd5bf5
# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
os
import
math
from
typing
import
Union
import
paddle
import
paddle.nn
as
nn
import
paddle.nn.functional
as
F
import
paddlehub.vision.transforms
as
T
import
numpy
as
np
from
paddle
import
ParamAttr
from
paddle.nn.initializer
import
Uniform
,
KaimingNormal
from
paddlehub.module.module
import
moduleinfo
from
paddlehub.module.cv_module
import
ImageClassifierModule
class
ConvBNLayer
(
nn
.
Layer
):
def
__init__
(
self
,
in_channels
,
out_channels
,
kernel_size
,
stride
=
1
,
groups
=
1
,
act
=
"relu"
,
name
=
None
):
super
(
ConvBNLayer
,
self
).
__init__
()
self
.
_conv
=
nn
.
Conv2D
(
in_channels
=
in_channels
,
out_channels
=
out_channels
,
kernel_size
=
kernel_size
,
stride
=
stride
,
padding
=
(
kernel_size
-
1
)
//
2
,
groups
=
groups
,
weight_attr
=
ParamAttr
(
initializer
=
KaimingNormal
(),
name
=
name
+
"_weights"
),
bias_attr
=
False
)
bn_name
=
name
+
"_bn"
self
.
_batch_norm
=
nn
.
BatchNorm
(
num_channels
=
out_channels
,
act
=
act
,
param_attr
=
ParamAttr
(
name
=
bn_name
+
"_scale"
,
regularizer
=
paddle
.
regularizer
.
L2Decay
(
0.0
)),
bias_attr
=
ParamAttr
(
name
=
bn_name
+
"_offset"
,
regularizer
=
paddle
.
regularizer
.
L2Decay
(
0.0
)),
moving_mean_name
=
bn_name
+
"_mean"
,
moving_variance_name
=
bn_name
+
"_variance"
)
def
forward
(
self
,
inputs
):
y
=
self
.
_conv
(
inputs
)
y
=
self
.
_batch_norm
(
y
)
return
y
class
SEBlock
(
nn
.
Layer
):
def
__init__
(
self
,
num_channels
,
reduction_ratio
=
4
,
name
=
None
):
super
(
SEBlock
,
self
).
__init__
()
self
.
pool2d_gap
=
nn
.
AdaptiveAvgPool2D
(
1
)
self
.
_num_channels
=
num_channels
stdv
=
1.0
/
math
.
sqrt
(
num_channels
*
1.0
)
med_ch
=
num_channels
//
reduction_ratio
self
.
squeeze
=
nn
.
Linear
(
num_channels
,
med_ch
,
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
),
name
=
name
+
"_1_weights"
),
bias_attr
=
ParamAttr
(
name
=
name
+
"_1_offset"
))
stdv
=
1.0
/
math
.
sqrt
(
med_ch
*
1.0
)
self
.
excitation
=
nn
.
Linear
(
med_ch
,
num_channels
,
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
),
name
=
name
+
"_2_weights"
),
bias_attr
=
ParamAttr
(
name
=
name
+
"_2_offset"
))
def
forward
(
self
,
inputs
):
pool
=
self
.
pool2d_gap
(
inputs
)
pool
=
paddle
.
squeeze
(
pool
,
axis
=
[
2
,
3
])
squeeze
=
self
.
squeeze
(
pool
)
squeeze
=
F
.
relu
(
squeeze
)
excitation
=
self
.
excitation
(
squeeze
)
excitation
=
paddle
.
clip
(
x
=
excitation
,
min
=
0
,
max
=
1
)
excitation
=
paddle
.
unsqueeze
(
excitation
,
axis
=
[
2
,
3
])
out
=
paddle
.
multiply
(
inputs
,
excitation
)
return
out
class
GhostModule
(
nn
.
Layer
):
def
__init__
(
self
,
in_channels
,
output_channels
,
kernel_size
=
1
,
ratio
=
2
,
dw_size
=
3
,
stride
=
1
,
relu
=
True
,
name
=
None
):
super
(
GhostModule
,
self
).
__init__
()
init_channels
=
int
(
math
.
ceil
(
output_channels
/
ratio
))
new_channels
=
int
(
init_channels
*
(
ratio
-
1
))
self
.
primary_conv
=
ConvBNLayer
(
in_channels
=
in_channels
,
out_channels
=
init_channels
,
kernel_size
=
kernel_size
,
stride
=
stride
,
groups
=
1
,
act
=
"relu"
if
relu
else
None
,
name
=
name
+
"_primary_conv"
)
self
.
cheap_operation
=
ConvBNLayer
(
in_channels
=
init_channels
,
out_channels
=
new_channels
,
kernel_size
=
dw_size
,
stride
=
1
,
groups
=
init_channels
,
act
=
"relu"
if
relu
else
None
,
name
=
name
+
"_cheap_operation"
)
def
forward
(
self
,
inputs
):
x
=
self
.
primary_conv
(
inputs
)
y
=
self
.
cheap_operation
(
x
)
out
=
paddle
.
concat
([
x
,
y
],
axis
=
1
)
return
out
class
GhostBottleneck
(
nn
.
Layer
):
def
__init__
(
self
,
in_channels
,
hidden_dim
,
output_channels
,
kernel_size
,
stride
,
use_se
,
name
=
None
):
super
(
GhostBottleneck
,
self
).
__init__
()
self
.
_stride
=
stride
self
.
_use_se
=
use_se
self
.
_num_channels
=
in_channels
self
.
_output_channels
=
output_channels
self
.
ghost_module_1
=
GhostModule
(
in_channels
=
in_channels
,
output_channels
=
hidden_dim
,
kernel_size
=
1
,
stride
=
1
,
relu
=
True
,
name
=
name
+
"_ghost_module_1"
)
if
stride
==
2
:
self
.
depthwise_conv
=
ConvBNLayer
(
in_channels
=
hidden_dim
,
out_channels
=
hidden_dim
,
kernel_size
=
kernel_size
,
stride
=
stride
,
groups
=
hidden_dim
,
act
=
None
,
name
=
name
+
"_depthwise_depthwise"
# looks strange due to an old typo, will be fixed later.
)
if
use_se
:
self
.
se_block
=
SEBlock
(
num_channels
=
hidden_dim
,
name
=
name
+
"_se"
)
self
.
ghost_module_2
=
GhostModule
(
in_channels
=
hidden_dim
,
output_channels
=
output_channels
,
kernel_size
=
1
,
relu
=
False
,
name
=
name
+
"_ghost_module_2"
)
if
stride
!=
1
or
in_channels
!=
output_channels
:
self
.
shortcut_depthwise
=
ConvBNLayer
(
in_channels
=
in_channels
,
out_channels
=
in_channels
,
kernel_size
=
kernel_size
,
stride
=
stride
,
groups
=
in_channels
,
act
=
None
,
name
=
name
+
"_shortcut_depthwise_depthwise"
# looks strange due to an old typo, will be fixed later.
)
self
.
shortcut_conv
=
ConvBNLayer
(
in_channels
=
in_channels
,
out_channels
=
output_channels
,
kernel_size
=
1
,
stride
=
1
,
groups
=
1
,
act
=
None
,
name
=
name
+
"_shortcut_conv"
)
def
forward
(
self
,
inputs
):
x
=
self
.
ghost_module_1
(
inputs
)
if
self
.
_stride
==
2
:
x
=
self
.
depthwise_conv
(
x
)
if
self
.
_use_se
:
x
=
self
.
se_block
(
x
)
x
=
self
.
ghost_module_2
(
x
)
if
self
.
_stride
==
1
and
self
.
_num_channels
==
self
.
_output_channels
:
shortcut
=
inputs
else
:
shortcut
=
self
.
shortcut_depthwise
(
inputs
)
shortcut
=
self
.
shortcut_conv
(
shortcut
)
return
paddle
.
add
(
x
=
x
,
y
=
shortcut
)
@
moduleinfo
(
name
=
"ghostnet_x1_0_imagenet"
,
type
=
"CV/classification"
,
author
=
"paddlepaddle"
,
author_email
=
""
,
summary
=
"ghostnet_x1_0_imagenet is a classification model, "
"this module is trained with Imagenet dataset."
,
version
=
"1.0.0"
,
meta
=
ImageClassifierModule
)
class
GhostNet
(
nn
.
Layer
):
def
__init__
(
self
,
label_list
:
list
=
None
,
load_checkpoint
:
str
=
None
):
super
(
GhostNet
,
self
).
__init__
()
if
label_list
is
not
None
:
self
.
labels
=
label_list
class_dim
=
len
(
self
.
labels
)
else
:
label_list
=
[]
label_file
=
os
.
path
.
join
(
self
.
directory
,
'label_list.txt'
)
files
=
open
(
label_file
)
for
line
in
files
.
readlines
():
line
=
line
.
strip
(
'
\n
'
)
label_list
.
append
(
line
)
self
.
labels
=
label_list
class_dim
=
len
(
self
.
labels
)
self
.
cfgs
=
[
# k, t, c, SE, s
[
3
,
16
,
16
,
0
,
1
],
[
3
,
48
,
24
,
0
,
2
],
[
3
,
72
,
24
,
0
,
1
],
[
5
,
72
,
40
,
1
,
2
],
[
5
,
120
,
40
,
1
,
1
],
[
3
,
240
,
80
,
0
,
2
],
[
3
,
200
,
80
,
0
,
1
],
[
3
,
184
,
80
,
0
,
1
],
[
3
,
184
,
80
,
0
,
1
],
[
3
,
480
,
112
,
1
,
1
],
[
3
,
672
,
112
,
1
,
1
],
[
5
,
672
,
160
,
1
,
2
],
[
5
,
960
,
160
,
0
,
1
],
[
5
,
960
,
160
,
1
,
1
],
[
5
,
960
,
160
,
0
,
1
],
[
5
,
960
,
160
,
1
,
1
]
]
self
.
scale
=
1.0
output_channels
=
int
(
self
.
_make_divisible
(
16
*
self
.
scale
,
4
))
self
.
conv1
=
ConvBNLayer
(
in_channels
=
3
,
out_channels
=
output_channels
,
kernel_size
=
3
,
stride
=
2
,
groups
=
1
,
act
=
"relu"
,
name
=
"conv1"
)
# build inverted residual blocks
idx
=
0
self
.
ghost_bottleneck_list
=
[]
for
k
,
exp_size
,
c
,
use_se
,
s
in
self
.
cfgs
:
in_channels
=
output_channels
output_channels
=
int
(
self
.
_make_divisible
(
c
*
self
.
scale
,
4
))
hidden_dim
=
int
(
self
.
_make_divisible
(
exp_size
*
self
.
scale
,
4
))
ghost_bottleneck
=
self
.
add_sublayer
(
name
=
"_ghostbottleneck_"
+
str
(
idx
),
sublayer
=
GhostBottleneck
(
in_channels
=
in_channels
,
hidden_dim
=
hidden_dim
,
output_channels
=
output_channels
,
kernel_size
=
k
,
stride
=
s
,
use_se
=
use_se
,
name
=
"_ghostbottleneck_"
+
str
(
idx
)))
self
.
ghost_bottleneck_list
.
append
(
ghost_bottleneck
)
idx
+=
1
# build last several layers
in_channels
=
output_channels
output_channels
=
int
(
self
.
_make_divisible
(
exp_size
*
self
.
scale
,
4
))
self
.
conv_last
=
ConvBNLayer
(
in_channels
=
in_channels
,
out_channels
=
output_channels
,
kernel_size
=
1
,
stride
=
1
,
groups
=
1
,
act
=
"relu"
,
name
=
"conv_last"
)
self
.
pool2d_gap
=
nn
.
AdaptiveAvgPool2D
(
1
)
in_channels
=
output_channels
self
.
_fc0_output_channels
=
1280
self
.
fc_0
=
ConvBNLayer
(
in_channels
=
in_channels
,
out_channels
=
self
.
_fc0_output_channels
,
kernel_size
=
1
,
stride
=
1
,
act
=
"relu"
,
name
=
"fc_0"
)
self
.
dropout
=
nn
.
Dropout
(
p
=
0.2
)
stdv
=
1.0
/
math
.
sqrt
(
self
.
_fc0_output_channels
*
1.0
)
self
.
fc_1
=
nn
.
Linear
(
self
.
_fc0_output_channels
,
class_dim
,
weight_attr
=
ParamAttr
(
name
=
"fc_1_weights"
,
initializer
=
Uniform
(
-
stdv
,
stdv
)),
bias_attr
=
ParamAttr
(
name
=
"fc_1_offset"
))
if
load_checkpoint
is
not
None
:
self
.
model_dict
=
paddle
.
load
(
load_checkpoint
)
self
.
set_dict
(
self
.
model_dict
)
print
(
"load custom checkpoint success"
)
else
:
checkpoint
=
os
.
path
.
join
(
self
.
directory
,
'model.pdparams'
)
self
.
model_dict
=
paddle
.
load
(
checkpoint
)
self
.
set_dict
(
self
.
model_dict
)
print
(
"load pretrained checkpoint success"
)
def
transforms
(
self
,
images
:
Union
[
str
,
np
.
ndarray
]):
transforms
=
T
.
Compose
([
T
.
Resize
((
256
,
256
)),
T
.
CenterCrop
(
224
),
T
.
Normalize
(
mean
=
[
0.485
,
0.456
,
0.406
],
std
=
[
0.229
,
0.224
,
0.225
])
],
to_rgb
=
True
)
return
transforms
(
images
).
astype
(
'float32'
)
def
forward
(
self
,
inputs
):
x
=
self
.
conv1
(
inputs
)
for
ghost_bottleneck
in
self
.
ghost_bottleneck_list
:
x
=
ghost_bottleneck
(
x
)
x
=
self
.
conv_last
(
x
)
feature
=
self
.
pool2d_gap
(
x
)
x
=
self
.
fc_0
(
feature
)
x
=
self
.
dropout
(
x
)
x
=
paddle
.
reshape
(
x
,
shape
=
[
-
1
,
self
.
_fc0_output_channels
])
x
=
self
.
fc_1
(
x
)
return
x
,
feature
def
_make_divisible
(
self
,
v
,
divisor
,
min_value
=
None
):
"""
This function is taken from the original tf repo.
It ensures that all layers have a channel number that is divisible by 8
It can be seen here:
https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
"""
if
min_value
is
None
:
min_value
=
divisor
new_v
=
max
(
min_value
,
int
(
v
+
divisor
/
2
)
//
divisor
*
divisor
)
# Make sure that round down does not go down by more than 10%.
if
new_v
<
0.9
*
v
:
new_v
+=
divisor
return
new_v
modules/image/classification/ghostnet_x1_3_imagenet/README.md
0 → 100644
浏览文件 @
50dd5bf5
```
shell
$
hub
install
ghostnet_x1_3_imagenet
==
1.0.0
```
## 命令行预测
```
shell
$
hub run ghostnet_x1_3_imagenet
--input_path
"/PATH/TO/IMAGE"
--top_k
5
```
## 脚本预测
```
python
import
paddle
import
paddlehub
as
hub
if
__name__
==
'__main__'
:
model
=
hub
.
Module
(
name
=
'ghostnet_x1_3_imagenet'
,)
result
=
model
.
predict
([
PATH
/
TO
/
IMAGE
])
```
## Fine-tune代码步骤
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 定义数据预处理方式
```
python
import
paddlehub.vision.transforms
as
T
transforms
=
T
.
Compose
([
T
.
Resize
((
256
,
256
)),
T
.
CenterCrop
(
224
),
T
.
Normalize
(
mean
=
[
0.485
,
0.456
,
0.406
],
std
=
[
0.229
,
0.224
,
0.225
])],
to_rgb
=
True
)
```
'transforms' 数据增强模块定义了丰富的数据预处理方式,用户可按照需求替换自己需要的数据预处理方式。
### Step2: 下载数据集并使用
```
python
from
paddlehub.datasets
import
Flowers
flowers
=
Flowers
(
transforms
)
flowers_validate
=
Flowers
(
transforms
,
mode
=
'val'
)
```
*
transforms(Callable): 数据预处理方式。
*
mode(str): 选择数据模式,可选项有 'train', 'test', 'val', 默认为'train'。
'hub.datasets.Flowers()' 会自动从网络下载数据集并解压到用户目录下'$HOME/.paddlehub/dataset'目录。
### Step3: 加载预训练模型
```
python
import
paddlehub
as
hub
model
=
hub
.
Module
(
name
=
'ghostnet_x1_3_imagenet'
,
label_list
=
[
"roses"
,
"tulips"
,
"daisy"
,
"sunflowers"
,
"dandelion"
],
load_checkpoint
=
None
)
```
*
name(str): 选择预训练模型的名字。
*
label_list(list): 设置标签对应分类类别, 默认为Imagenet2012类别。
*
load _checkpoint(str): 模型参数地址。
PaddleHub提供许多图像分类预训练模型,如xception、mobilenet、efficientnet等,详细信息参见
[
图像分类模型
](
https://www.paddlepaddle.org.cn/hub?filter=en_category&value=ImageClassification
)
。
如果想尝试efficientnet模型,只需要更换Module中的'name'参数即可.
```
python
import
paddlehub
as
hub
# 更换name参数即可无缝切换efficientnet模型, 代码示例如下
module
=
hub
.
Module
(
name
=
"efficientnetb7_imagenet"
)
```
**NOTE:**
目前部分模型还没有完全升级到2.0版本,敬请期待。
### Step4: 选择优化策略和运行配置
```
python
import
paddle
from
paddlehub.finetune.trainer
import
Trainer
optimizer
=
paddle
.
optimizer
.
Adam
(
learning_rate
=
0.001
,
parameters
=
model
.
parameters
())
trainer
=
Trainer
(
model
,
optimizer
,
checkpoint_dir
=
'img_classification_ckpt'
)
trainer
.
train
(
flowers
,
epochs
=
100
,
batch_size
=
32
,
eval_dataset
=
flowers_validate
,
save_interval
=
1
)
```
#### 优化策略
Paddle2.0rc提供了多种优化器选择,如'SGD', 'Adam', 'Adamax'等,详细参见
[
策略
](
https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/optimizer/optimizer/Optimizer_cn.html
)
。
其中'Adam':
*
learning_rate: 全局学习率。默认为1e-3;
*
parameters: 待优化模型参数。
#### 运行配置
'Trainer' 主要控制Fine-tune的训练,包含以下可控制的参数:
*
model: 被优化模型;
*
optimizer: 优化器选择;
*
use_vdl: 是否使用vdl可视化训练过程;
*
checkpoint_dir: 保存模型参数的地址;
*
compare_metrics: 保存最优模型的衡量指标;
'trainer.train' 主要控制具体的训练过程,包含以下可控制的参数:
*
train_dataset: 训练时所用的数据集;
*
epochs: 训练轮数;
*
batch_size: 训练的批大小,如果使用GPU,请根据实际情况调整batch_size;
*
num_workers: works的数量,默认为0;
*
eval_dataset: 验证集;
*
log_interval: 打印日志的间隔, 单位为执行批训练的次数。
*
save_interval: 保存模型的间隔频次,单位为执行训练的轮数。
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在'${CHECKPOINT_DIR}/best_model'目录下,其中'${CHECKPOINT_DIR}'目录为Fine-tune时所选择的保存checkpoint的目录。
我们使用该模型来进行预测。predict.py脚本如下:
```
python
import
paddle
import
paddlehub
as
hub
if
__name__
==
'__main__'
:
model
=
hub
.
Module
(
name
=
'ghostnet_x1_3_imagenet'
,
label_list
=
[
"roses"
,
"tulips"
,
"daisy"
,
"sunflowers"
,
"dandelion"
],
load_checkpoint
=
'/PATH/TO/CHECKPOINT'
)
result
=
model
.
predict
([
'flower.jpg'
])
```
参数配置正确后,请执行脚本'python predict.py', 加载模型具体可参见
[
加载
](
https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/framework/io/load_cn.html#load
)
。
**NOTE:**
进行预测时,所选择的module,checkpoint_dir,dataset必须和Fine-tune所用的一样。
## 服务部署
PaddleHub Serving可以部署一个在线分类任务服务
## Step1: 启动PaddleHub Serving
运行启动命令:
```
shell
$
hub serving start
-m
ghostnet_x1_3_imagenet
```
这样就完成了一个分类任务服务化API的部署,默认端口号为8866。
**NOTE:**
如使用GPU预测,则需要在启动服务之前,请设置CUDA_VISIBLE_DEVICES环境变量,否则不用设置。
## Step2: 发送预测请求
配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
```
python
import
requests
import
json
import
cv2
import
base64
import
numpy
as
np
def
cv2_to_base64
(
image
):
data
=
cv2
.
imencode
(
'.jpg'
,
image
)[
1
]
return
base64
.
b64encode
(
data
.
tostring
()).
decode
(
'utf8'
)
def
base64_to_cv2
(
b64str
):
data
=
base64
.
b64decode
(
b64str
.
encode
(
'utf8'
))
data
=
np
.
fromstring
(
data
,
np
.
uint8
)
data
=
cv2
.
imdecode
(
data
,
cv2
.
IMREAD_COLOR
)
return
data
# 发送HTTP请求
org_im
=
cv2
.
imread
(
'/PATH/TO/IMAGE'
)
data
=
{
'images'
:[
cv2_to_base64
(
org_im
)],
'top_k'
:
2
}
headers
=
{
"Content-type"
:
"application/json"
}
url
=
"http://127.0.0.1:8866/predict/ghostnet_x1_3_imagenet"
r
=
requests
.
post
(
url
=
url
,
headers
=
headers
,
data
=
json
.
dumps
(
data
))
data
=
r
.
json
()[
"results"
][
'data'
]
```
### 查看代码
[
PaddleClas
](
https://github.com/PaddlePaddle/PaddleClas
)
### 依赖
paddlepaddle >= 2.0.0
paddlehub >= 2.0.0
modules/image/classification/ghostnet_x1_3_imagenet/label_list.txt
0 → 100644
浏览文件 @
50dd5bf5
tench
goldfish
great white shark
tiger shark
hammerhead
electric ray
stingray
cock
hen
ostrich
brambling
goldfinch
house finch
junco
indigo bunting
robin
bulbul
jay
magpie
chickadee
water ouzel
kite
bald eagle
vulture
great grey owl
European fire salamander
common newt
eft
spotted salamander
axolotl
bullfrog
tree frog
tailed frog
loggerhead
leatherback turtle
mud turtle
terrapin
box turtle
banded gecko
common iguana
American chameleon
whiptail
agama
frilled lizard
alligator lizard
Gila monster
green lizard
African chameleon
Komodo dragon
African crocodile
American alligator
triceratops
thunder snake
ringneck snake
hognose snake
green snake
king snake
garter snake
water snake
vine snake
night snake
boa constrictor
rock python
Indian cobra
green mamba
sea snake
horned viper
diamondback
sidewinder
trilobite
harvestman
scorpion
black and gold garden spider
barn spider
garden spider
black widow
tarantula
wolf spider
tick
centipede
black grouse
ptarmigan
ruffed grouse
prairie chicken
peacock
quail
partridge
African grey
macaw
sulphur-crested cockatoo
lorikeet
coucal
bee eater
hornbill
hummingbird
jacamar
toucan
drake
red-breasted merganser
goose
black swan
tusker
echidna
platypus
wallaby
koala
wombat
jellyfish
sea anemone
brain coral
flatworm
nematode
conch
snail
slug
sea slug
chiton
chambered nautilus
Dungeness crab
rock crab
fiddler crab
king crab
American lobster
spiny lobster
crayfish
hermit crab
isopod
white stork
black stork
spoonbill
flamingo
little blue heron
American egret
bittern
crane
limpkin
European gallinule
American coot
bustard
ruddy turnstone
red-backed sandpiper
redshank
dowitcher
oystercatcher
pelican
king penguin
albatross
grey whale
killer whale
dugong
sea lion
Chihuahua
Japanese spaniel
Maltese dog
Pekinese
Shih-Tzu
Blenheim spaniel
papillon
toy terrier
Rhodesian ridgeback
Afghan hound
basset
beagle
bloodhound
bluetick
black-and-tan coonhound
Walker hound
English foxhound
redbone
borzoi
Irish wolfhound
Italian greyhound
whippet
Ibizan hound
Norwegian elkhound
otterhound
Saluki
Scottish deerhound
Weimaraner
Staffordshire bullterrier
American Staffordshire terrier
Bedlington terrier
Border terrier
Kerry blue terrier
Irish terrier
Norfolk terrier
Norwich terrier
Yorkshire terrier
wire-haired fox terrier
Lakeland terrier
Sealyham terrier
Airedale
cairn
Australian terrier
Dandie Dinmont
Boston bull
miniature schnauzer
giant schnauzer
standard schnauzer
Scotch terrier
Tibetan terrier
silky terrier
soft-coated wheaten terrier
West Highland white terrier
Lhasa
flat-coated retriever
curly-coated retriever
golden retriever
Labrador retriever
Chesapeake Bay retriever
German short-haired pointer
vizsla
English setter
Irish setter
Gordon setter
Brittany spaniel
clumber
English springer
Welsh springer spaniel
cocker spaniel
Sussex spaniel
Irish water spaniel
kuvasz
schipperke
groenendael
malinois
briard
kelpie
komondor
Old English sheepdog
Shetland sheepdog
collie
Border collie
Bouvier des Flandres
Rottweiler
German shepherd
Doberman
miniature pinscher
Greater Swiss Mountain dog
Bernese mountain dog
Appenzeller
EntleBucher
boxer
bull mastiff
Tibetan mastiff
French bulldog
Great Dane
Saint Bernard
Eskimo dog
malamute
Siberian husky
dalmatian
affenpinscher
basenji
pug
Leonberg
Newfoundland
Great Pyrenees
Samoyed
Pomeranian
chow
keeshond
Brabancon griffon
Pembroke
Cardigan
toy poodle
miniature poodle
standard poodle
Mexican hairless
timber wolf
white wolf
red wolf
coyote
dingo
dhole
African hunting dog
hyena
red fox
kit fox
Arctic fox
grey fox
tabby
tiger cat
Persian cat
Siamese cat
Egyptian cat
cougar
lynx
leopard
snow leopard
jaguar
lion
tiger
cheetah
brown bear
American black bear
ice bear
sloth bear
mongoose
meerkat
tiger beetle
ladybug
ground beetle
long-horned beetle
leaf beetle
dung beetle
rhinoceros beetle
weevil
fly
bee
ant
grasshopper
cricket
walking stick
cockroach
mantis
cicada
leafhopper
lacewing
dragonfly
damselfly
admiral
ringlet
monarch
cabbage butterfly
sulphur butterfly
lycaenid
starfish
sea urchin
sea cucumber
wood rabbit
hare
Angora
hamster
porcupine
fox squirrel
marmot
beaver
guinea pig
sorrel
zebra
hog
wild boar
warthog
hippopotamus
ox
water buffalo
bison
ram
bighorn
ibex
hartebeest
impala
gazelle
Arabian camel
llama
weasel
mink
polecat
black-footed ferret
otter
skunk
badger
armadillo
three-toed sloth
orangutan
gorilla
chimpanzee
gibbon
siamang
guenon
patas
baboon
macaque
langur
colobus
proboscis monkey
marmoset
capuchin
howler monkey
titi
spider monkey
squirrel monkey
Madagascar cat
indri
Indian elephant
African elephant
lesser panda
giant panda
barracouta
eel
coho
rock beauty
anemone fish
sturgeon
gar
lionfish
puffer
abacus
abaya
academic gown
accordion
acoustic guitar
aircraft carrier
airliner
airship
altar
ambulance
amphibian
analog clock
apiary
apron
ashcan
assault rifle
backpack
bakery
balance beam
balloon
ballpoint
Band Aid
banjo
bannister
barbell
barber chair
barbershop
barn
barometer
barrel
barrow
baseball
basketball
bassinet
bassoon
bathing cap
bath towel
bathtub
beach wagon
beacon
beaker
bearskin
beer bottle
beer glass
bell cote
bib
bicycle-built-for-two
bikini
binder
binoculars
birdhouse
boathouse
bobsled
bolo tie
bonnet
bookcase
bookshop
bottlecap
bow
bow tie
brass
brassiere
breakwater
breastplate
broom
bucket
buckle
bulletproof vest
bullet train
butcher shop
cab
caldron
candle
cannon
canoe
can opener
cardigan
car mirror
carousel
carpenters kit
carton
car wheel
cash machine
cassette
cassette player
castle
catamaran
CD player
cello
cellular telephone
chain
chainlink fence
chain mail
chain saw
chest
chiffonier
chime
china cabinet
Christmas stocking
church
cinema
cleaver
cliff dwelling
cloak
clog
cocktail shaker
coffee mug
coffeepot
coil
combination lock
computer keyboard
confectionery
container ship
convertible
corkscrew
cornet
cowboy boot
cowboy hat
cradle
crane
crash helmet
crate
crib
Crock Pot
croquet ball
crutch
cuirass
dam
desk
desktop computer
dial telephone
diaper
digital clock
digital watch
dining table
dishrag
dishwasher
disk brake
dock
dogsled
dome
doormat
drilling platform
drum
drumstick
dumbbell
Dutch oven
electric fan
electric guitar
electric locomotive
entertainment center
envelope
espresso maker
face powder
feather boa
file
fireboat
fire engine
fire screen
flagpole
flute
folding chair
football helmet
forklift
fountain
fountain pen
four-poster
freight car
French horn
frying pan
fur coat
garbage truck
gasmask
gas pump
goblet
go-kart
golf ball
golfcart
gondola
gong
gown
grand piano
greenhouse
grille
grocery store
guillotine
hair slide
hair spray
half track
hammer
hamper
hand blower
hand-held computer
handkerchief
hard disc
harmonica
harp
harvester
hatchet
holster
home theater
honeycomb
hook
hoopskirt
horizontal bar
horse cart
hourglass
iPod
iron
jack-o-lantern
jean
jeep
jersey
jigsaw puzzle
jinrikisha
joystick
kimono
knee pad
knot
lab coat
ladle
lampshade
laptop
lawn mower
lens cap
letter opener
library
lifeboat
lighter
limousine
liner
lipstick
Loafer
lotion
loudspeaker
loupe
lumbermill
magnetic compass
mailbag
mailbox
maillot
maillot
manhole cover
maraca
marimba
mask
matchstick
maypole
maze
measuring cup
medicine chest
megalith
microphone
microwave
military uniform
milk can
minibus
miniskirt
minivan
missile
mitten
mixing bowl
mobile home
Model T
modem
monastery
monitor
moped
mortar
mortarboard
mosque
mosquito net
motor scooter
mountain bike
mountain tent
mouse
mousetrap
moving van
muzzle
nail
neck brace
necklace
nipple
notebook
obelisk
oboe
ocarina
odometer
oil filter
organ
oscilloscope
overskirt
oxcart
oxygen mask
packet
paddle
paddlewheel
padlock
paintbrush
pajama
palace
panpipe
paper towel
parachute
parallel bars
park bench
parking meter
passenger car
patio
pay-phone
pedestal
pencil box
pencil sharpener
perfume
Petri dish
photocopier
pick
pickelhaube
picket fence
pickup
pier
piggy bank
pill bottle
pillow
ping-pong ball
pinwheel
pirate
pitcher
plane
planetarium
plastic bag
plate rack
plow
plunger
Polaroid camera
pole
police van
poncho
pool table
pop bottle
pot
potters wheel
power drill
prayer rug
printer
prison
projectile
projector
puck
punching bag
purse
quill
quilt
racer
racket
radiator
radio
radio telescope
rain barrel
recreational vehicle
reel
reflex camera
refrigerator
remote control
restaurant
revolver
rifle
rocking chair
rotisserie
rubber eraser
rugby ball
rule
running shoe
safe
safety pin
saltshaker
sandal
sarong
sax
scabbard
scale
school bus
schooner
scoreboard
screen
screw
screwdriver
seat belt
sewing machine
shield
shoe shop
shoji
shopping basket
shopping cart
shovel
shower cap
shower curtain
ski
ski mask
sleeping bag
slide rule
sliding door
slot
snorkel
snowmobile
snowplow
soap dispenser
soccer ball
sock
solar dish
sombrero
soup bowl
space bar
space heater
space shuttle
spatula
speedboat
spider web
spindle
sports car
spotlight
stage
steam locomotive
steel arch bridge
steel drum
stethoscope
stole
stone wall
stopwatch
stove
strainer
streetcar
stretcher
studio couch
stupa
submarine
suit
sundial
sunglass
sunglasses
sunscreen
suspension bridge
swab
sweatshirt
swimming trunks
swing
switch
syringe
table lamp
tank
tape player
teapot
teddy
television
tennis ball
thatch
theater curtain
thimble
thresher
throne
tile roof
toaster
tobacco shop
toilet seat
torch
totem pole
tow truck
toyshop
tractor
trailer truck
tray
trench coat
tricycle
trimaran
tripod
triumphal arch
trolleybus
trombone
tub
turnstile
typewriter keyboard
umbrella
unicycle
upright
vacuum
vase
vault
velvet
vending machine
vestment
viaduct
violin
volleyball
waffle iron
wall clock
wallet
wardrobe
warplane
washbasin
washer
water bottle
water jug
water tower
whiskey jug
whistle
wig
window screen
window shade
Windsor tie
wine bottle
wing
wok
wooden spoon
wool
worm fence
wreck
yawl
yurt
web site
comic book
crossword puzzle
street sign
traffic light
book jacket
menu
plate
guacamole
consomme
hot pot
trifle
ice cream
ice lolly
French loaf
bagel
pretzel
cheeseburger
hotdog
mashed potato
head cabbage
broccoli
cauliflower
zucchini
spaghetti squash
acorn squash
butternut squash
cucumber
artichoke
bell pepper
cardoon
mushroom
Granny Smith
strawberry
orange
lemon
fig
pineapple
banana
jackfruit
custard apple
pomegranate
hay
carbonara
chocolate sauce
dough
meat loaf
pizza
potpie
burrito
red wine
espresso
cup
eggnog
alp
bubble
cliff
coral reef
geyser
lakeside
promontory
sandbar
seashore
valley
volcano
ballplayer
groom
scuba diver
rapeseed
daisy
yellow ladys slipper
corn
acorn
hip
buckeye
coral fungus
agaric
gyromitra
stinkhorn
earthstar
hen-of-the-woods
bolete
ear
toilet tissue
modules/image/classification/ghostnet_x1_3_imagenet/module.py
0 → 100644
浏览文件 @
50dd5bf5
# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
os
import
math
from
typing
import
Union
import
paddle
import
paddle.nn
as
nn
import
paddle.nn.functional
as
F
import
paddlehub.vision.transforms
as
T
import
numpy
as
np
from
paddle
import
ParamAttr
from
paddle.nn.initializer
import
Uniform
,
KaimingNormal
from
paddlehub.module.module
import
moduleinfo
from
paddlehub.module.cv_module
import
ImageClassifierModule
class
ConvBNLayer
(
nn
.
Layer
):
def
__init__
(
self
,
in_channels
,
out_channels
,
kernel_size
,
stride
=
1
,
groups
=
1
,
act
=
"relu"
,
name
=
None
):
super
(
ConvBNLayer
,
self
).
__init__
()
self
.
_conv
=
nn
.
Conv2D
(
in_channels
=
in_channels
,
out_channels
=
out_channels
,
kernel_size
=
kernel_size
,
stride
=
stride
,
padding
=
(
kernel_size
-
1
)
//
2
,
groups
=
groups
,
weight_attr
=
ParamAttr
(
initializer
=
KaimingNormal
(),
name
=
name
+
"_weights"
),
bias_attr
=
False
)
bn_name
=
name
+
"_bn"
self
.
_batch_norm
=
nn
.
BatchNorm
(
num_channels
=
out_channels
,
act
=
act
,
param_attr
=
ParamAttr
(
name
=
bn_name
+
"_scale"
,
regularizer
=
paddle
.
regularizer
.
L2Decay
(
0.0
)),
bias_attr
=
ParamAttr
(
name
=
bn_name
+
"_offset"
,
regularizer
=
paddle
.
regularizer
.
L2Decay
(
0.0
)),
moving_mean_name
=
bn_name
+
"_mean"
,
moving_variance_name
=
bn_name
+
"_variance"
)
def
forward
(
self
,
inputs
):
y
=
self
.
_conv
(
inputs
)
y
=
self
.
_batch_norm
(
y
)
return
y
class
SEBlock
(
nn
.
Layer
):
def
__init__
(
self
,
num_channels
,
reduction_ratio
=
4
,
name
=
None
):
super
(
SEBlock
,
self
).
__init__
()
self
.
pool2d_gap
=
nn
.
AdaptiveAvgPool2D
(
1
)
self
.
_num_channels
=
num_channels
stdv
=
1.0
/
math
.
sqrt
(
num_channels
*
1.0
)
med_ch
=
num_channels
//
reduction_ratio
self
.
squeeze
=
nn
.
Linear
(
num_channels
,
med_ch
,
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
),
name
=
name
+
"_1_weights"
),
bias_attr
=
ParamAttr
(
name
=
name
+
"_1_offset"
))
stdv
=
1.0
/
math
.
sqrt
(
med_ch
*
1.0
)
self
.
excitation
=
nn
.
Linear
(
med_ch
,
num_channels
,
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
),
name
=
name
+
"_2_weights"
),
bias_attr
=
ParamAttr
(
name
=
name
+
"_2_offset"
))
def
forward
(
self
,
inputs
):
pool
=
self
.
pool2d_gap
(
inputs
)
pool
=
paddle
.
squeeze
(
pool
,
axis
=
[
2
,
3
])
squeeze
=
self
.
squeeze
(
pool
)
squeeze
=
F
.
relu
(
squeeze
)
excitation
=
self
.
excitation
(
squeeze
)
excitation
=
paddle
.
clip
(
x
=
excitation
,
min
=
0
,
max
=
1
)
excitation
=
paddle
.
unsqueeze
(
excitation
,
axis
=
[
2
,
3
])
out
=
paddle
.
multiply
(
inputs
,
excitation
)
return
out
class
GhostModule
(
nn
.
Layer
):
def
__init__
(
self
,
in_channels
,
output_channels
,
kernel_size
=
1
,
ratio
=
2
,
dw_size
=
3
,
stride
=
1
,
relu
=
True
,
name
=
None
):
super
(
GhostModule
,
self
).
__init__
()
init_channels
=
int
(
math
.
ceil
(
output_channels
/
ratio
))
new_channels
=
int
(
init_channels
*
(
ratio
-
1
))
self
.
primary_conv
=
ConvBNLayer
(
in_channels
=
in_channels
,
out_channels
=
init_channels
,
kernel_size
=
kernel_size
,
stride
=
stride
,
groups
=
1
,
act
=
"relu"
if
relu
else
None
,
name
=
name
+
"_primary_conv"
)
self
.
cheap_operation
=
ConvBNLayer
(
in_channels
=
init_channels
,
out_channels
=
new_channels
,
kernel_size
=
dw_size
,
stride
=
1
,
groups
=
init_channels
,
act
=
"relu"
if
relu
else
None
,
name
=
name
+
"_cheap_operation"
)
def
forward
(
self
,
inputs
):
x
=
self
.
primary_conv
(
inputs
)
y
=
self
.
cheap_operation
(
x
)
out
=
paddle
.
concat
([
x
,
y
],
axis
=
1
)
return
out
class
GhostBottleneck
(
nn
.
Layer
):
def
__init__
(
self
,
in_channels
,
hidden_dim
,
output_channels
,
kernel_size
,
stride
,
use_se
,
name
=
None
):
super
(
GhostBottleneck
,
self
).
__init__
()
self
.
_stride
=
stride
self
.
_use_se
=
use_se
self
.
_num_channels
=
in_channels
self
.
_output_channels
=
output_channels
self
.
ghost_module_1
=
GhostModule
(
in_channels
=
in_channels
,
output_channels
=
hidden_dim
,
kernel_size
=
1
,
stride
=
1
,
relu
=
True
,
name
=
name
+
"_ghost_module_1"
)
if
stride
==
2
:
self
.
depthwise_conv
=
ConvBNLayer
(
in_channels
=
hidden_dim
,
out_channels
=
hidden_dim
,
kernel_size
=
kernel_size
,
stride
=
stride
,
groups
=
hidden_dim
,
act
=
None
,
name
=
name
+
"_depthwise_depthwise"
# looks strange due to an old typo, will be fixed later.
)
if
use_se
:
self
.
se_block
=
SEBlock
(
num_channels
=
hidden_dim
,
name
=
name
+
"_se"
)
self
.
ghost_module_2
=
GhostModule
(
in_channels
=
hidden_dim
,
output_channels
=
output_channels
,
kernel_size
=
1
,
relu
=
False
,
name
=
name
+
"_ghost_module_2"
)
if
stride
!=
1
or
in_channels
!=
output_channels
:
self
.
shortcut_depthwise
=
ConvBNLayer
(
in_channels
=
in_channels
,
out_channels
=
in_channels
,
kernel_size
=
kernel_size
,
stride
=
stride
,
groups
=
in_channels
,
act
=
None
,
name
=
name
+
"_shortcut_depthwise_depthwise"
# looks strange due to an old typo, will be fixed later.
)
self
.
shortcut_conv
=
ConvBNLayer
(
in_channels
=
in_channels
,
out_channels
=
output_channels
,
kernel_size
=
1
,
stride
=
1
,
groups
=
1
,
act
=
None
,
name
=
name
+
"_shortcut_conv"
)
def
forward
(
self
,
inputs
):
x
=
self
.
ghost_module_1
(
inputs
)
if
self
.
_stride
==
2
:
x
=
self
.
depthwise_conv
(
x
)
if
self
.
_use_se
:
x
=
self
.
se_block
(
x
)
x
=
self
.
ghost_module_2
(
x
)
if
self
.
_stride
==
1
and
self
.
_num_channels
==
self
.
_output_channels
:
shortcut
=
inputs
else
:
shortcut
=
self
.
shortcut_depthwise
(
inputs
)
shortcut
=
self
.
shortcut_conv
(
shortcut
)
return
paddle
.
add
(
x
=
x
,
y
=
shortcut
)
@
moduleinfo
(
name
=
"ghostnet_x1_3_imagenet"
,
type
=
"CV/classification"
,
author
=
"paddlepaddle"
,
author_email
=
""
,
summary
=
"ghostnet_x1_3_imagenet is a classification model, "
"this module is trained with Imagenet dataset."
,
version
=
"1.0.0"
,
meta
=
ImageClassifierModule
)
class
GhostNet
(
nn
.
Layer
):
def
__init__
(
self
,
label_list
:
list
=
None
,
load_checkpoint
:
str
=
None
):
super
(
GhostNet
,
self
).
__init__
()
if
label_list
is
not
None
:
self
.
labels
=
label_list
class_dim
=
len
(
self
.
labels
)
else
:
label_list
=
[]
label_file
=
os
.
path
.
join
(
self
.
directory
,
'label_list.txt'
)
files
=
open
(
label_file
)
for
line
in
files
.
readlines
():
line
=
line
.
strip
(
'
\n
'
)
label_list
.
append
(
line
)
self
.
labels
=
label_list
class_dim
=
len
(
self
.
labels
)
self
.
cfgs
=
[
# k, t, c, SE, s
[
3
,
16
,
16
,
0
,
1
],
[
3
,
48
,
24
,
0
,
2
],
[
3
,
72
,
24
,
0
,
1
],
[
5
,
72
,
40
,
1
,
2
],
[
5
,
120
,
40
,
1
,
1
],
[
3
,
240
,
80
,
0
,
2
],
[
3
,
200
,
80
,
0
,
1
],
[
3
,
184
,
80
,
0
,
1
],
[
3
,
184
,
80
,
0
,
1
],
[
3
,
480
,
112
,
1
,
1
],
[
3
,
672
,
112
,
1
,
1
],
[
5
,
672
,
160
,
1
,
2
],
[
5
,
960
,
160
,
0
,
1
],
[
5
,
960
,
160
,
1
,
1
],
[
5
,
960
,
160
,
0
,
1
],
[
5
,
960
,
160
,
1
,
1
]
]
self
.
scale
=
1.3
output_channels
=
int
(
self
.
_make_divisible
(
16
*
self
.
scale
,
4
))
self
.
conv1
=
ConvBNLayer
(
in_channels
=
3
,
out_channels
=
output_channels
,
kernel_size
=
3
,
stride
=
2
,
groups
=
1
,
act
=
"relu"
,
name
=
"conv1"
)
# build inverted residual blocks
idx
=
0
self
.
ghost_bottleneck_list
=
[]
for
k
,
exp_size
,
c
,
use_se
,
s
in
self
.
cfgs
:
in_channels
=
output_channels
output_channels
=
int
(
self
.
_make_divisible
(
c
*
self
.
scale
,
4
))
hidden_dim
=
int
(
self
.
_make_divisible
(
exp_size
*
self
.
scale
,
4
))
ghost_bottleneck
=
self
.
add_sublayer
(
name
=
"_ghostbottleneck_"
+
str
(
idx
),
sublayer
=
GhostBottleneck
(
in_channels
=
in_channels
,
hidden_dim
=
hidden_dim
,
output_channels
=
output_channels
,
kernel_size
=
k
,
stride
=
s
,
use_se
=
use_se
,
name
=
"_ghostbottleneck_"
+
str
(
idx
)))
self
.
ghost_bottleneck_list
.
append
(
ghost_bottleneck
)
idx
+=
1
# build last several layers
in_channels
=
output_channels
output_channels
=
int
(
self
.
_make_divisible
(
exp_size
*
self
.
scale
,
4
))
self
.
conv_last
=
ConvBNLayer
(
in_channels
=
in_channels
,
out_channels
=
output_channels
,
kernel_size
=
1
,
stride
=
1
,
groups
=
1
,
act
=
"relu"
,
name
=
"conv_last"
)
self
.
pool2d_gap
=
nn
.
AdaptiveAvgPool2D
(
1
)
in_channels
=
output_channels
self
.
_fc0_output_channels
=
1280
self
.
fc_0
=
ConvBNLayer
(
in_channels
=
in_channels
,
out_channels
=
self
.
_fc0_output_channels
,
kernel_size
=
1
,
stride
=
1
,
act
=
"relu"
,
name
=
"fc_0"
)
self
.
dropout
=
nn
.
Dropout
(
p
=
0.2
)
stdv
=
1.0
/
math
.
sqrt
(
self
.
_fc0_output_channels
*
1.0
)
self
.
fc_1
=
nn
.
Linear
(
self
.
_fc0_output_channels
,
class_dim
,
weight_attr
=
ParamAttr
(
name
=
"fc_1_weights"
,
initializer
=
Uniform
(
-
stdv
,
stdv
)),
bias_attr
=
ParamAttr
(
name
=
"fc_1_offset"
))
if
load_checkpoint
is
not
None
:
self
.
model_dict
=
paddle
.
load
(
load_checkpoint
)
self
.
set_dict
(
self
.
model_dict
)
print
(
"load custom checkpoint success"
)
else
:
checkpoint
=
os
.
path
.
join
(
self
.
directory
,
'model.pdparams'
)
self
.
model_dict
=
paddle
.
load
(
checkpoint
)
self
.
set_dict
(
self
.
model_dict
)
print
(
"load pretrained checkpoint success"
)
def
transforms
(
self
,
images
:
Union
[
str
,
np
.
ndarray
]):
transforms
=
T
.
Compose
([
T
.
Resize
((
256
,
256
)),
T
.
CenterCrop
(
224
),
T
.
Normalize
(
mean
=
[
0.485
,
0.456
,
0.406
],
std
=
[
0.229
,
0.224
,
0.225
])
],
to_rgb
=
True
)
return
transforms
(
images
).
astype
(
'float32'
)
def
forward
(
self
,
inputs
):
x
=
self
.
conv1
(
inputs
)
for
ghost_bottleneck
in
self
.
ghost_bottleneck_list
:
x
=
ghost_bottleneck
(
x
)
x
=
self
.
conv_last
(
x
)
feature
=
self
.
pool2d_gap
(
x
)
x
=
self
.
fc_0
(
feature
)
x
=
self
.
dropout
(
x
)
x
=
paddle
.
reshape
(
x
,
shape
=
[
-
1
,
self
.
_fc0_output_channels
])
x
=
self
.
fc_1
(
x
)
return
x
,
feature
def
_make_divisible
(
self
,
v
,
divisor
,
min_value
=
None
):
"""
This function is taken from the original tf repo.
It ensures that all layers have a channel number that is divisible by 8
It can be seen here:
https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
"""
if
min_value
is
None
:
min_value
=
divisor
new_v
=
max
(
min_value
,
int
(
v
+
divisor
/
2
)
//
divisor
*
divisor
)
# Make sure that round down does not go down by more than 10%.
if
new_v
<
0.9
*
v
:
new_v
+=
divisor
return
new_v
modules/image/classification/ghostnet_x1_3_imagenet_ssld/README.md
0 → 100644
浏览文件 @
50dd5bf5
```
shell
$
hub
install
ghostnet_x1_3_imagenet_ssld
==
1.0.0
```
## 命令行预测
```
shell
$
hub run ghostnet_x1_3_imagenet_ssld
--input_path
"/PATH/TO/IMAGE"
--top_k
5
```
## 脚本预测
```
python
import
paddle
import
paddlehub
as
hub
if
__name__
==
'__main__'
:
model
=
hub
.
Module
(
name
=
'ghostnet_x1_3_imagenet_ssld'
,)
result
=
model
.
predict
([
PATH
/
TO
/
IMAGE
])
```
## Fine-tune代码步骤
使用PaddleHub Fine-tune API进行Fine-tune可以分为4个步骤。
### Step1: 定义数据预处理方式
```
python
import
paddlehub.vision.transforms
as
T
transforms
=
T
.
Compose
([
T
.
Resize
((
256
,
256
)),
T
.
CenterCrop
(
224
),
T
.
Normalize
(
mean
=
[
0.485
,
0.456
,
0.406
],
std
=
[
0.229
,
0.224
,
0.225
])],
to_rgb
=
True
)
```
'transforms' 数据增强模块定义了丰富的数据预处理方式,用户可按照需求替换自己需要的数据预处理方式。
### Step2: 下载数据集并使用
```
python
from
paddlehub.datasets
import
Flowers
flowers
=
Flowers
(
transforms
)
flowers_validate
=
Flowers
(
transforms
,
mode
=
'val'
)
```
*
transforms(Callable): 数据预处理方式。
*
mode(str): 选择数据模式,可选项有 'train', 'test', 'val', 默认为'train'。
'hub.datasets.Flowers()' 会自动从网络下载数据集并解压到用户目录下'$HOME/.paddlehub/dataset'目录。
### Step3: 加载预训练模型
```
python
import
paddlehub
as
hub
model
=
hub
.
Module
(
name
=
'ghostnet_x1_3_imagenet_ssld'
,
label_list
=
[
"roses"
,
"tulips"
,
"daisy"
,
"sunflowers"
,
"dandelion"
],
load_checkpoint
=
None
)
```
*
name(str): 选择预训练模型的名字。
*
label_list(list): 设置标签对应分类类别, 默认为Imagenet2012类别。
*
load _checkpoint(str): 模型参数地址。
PaddleHub提供许多图像分类预训练模型,如xception、mobilenet、efficientnet等,详细信息参见
[
图像分类模型
](
https://www.paddlepaddle.org.cn/hub?filter=en_category&value=ImageClassification
)
。
如果想尝试efficientnet模型,只需要更换Module中的'name'参数即可.
```
python
import
paddlehub
as
hub
# 更换name参数即可无缝切换efficientnet模型, 代码示例如下
module
=
hub
.
Module
(
name
=
"efficientnetb7_imagenet"
)
```
**NOTE:**
目前部分模型还没有完全升级到2.0版本,敬请期待。
### Step4: 选择优化策略和运行配置
```
python
import
paddle
from
paddlehub.finetune.trainer
import
Trainer
optimizer
=
paddle
.
optimizer
.
Adam
(
learning_rate
=
0.001
,
parameters
=
model
.
parameters
())
trainer
=
Trainer
(
model
,
optimizer
,
checkpoint_dir
=
'img_classification_ckpt'
)
trainer
.
train
(
flowers
,
epochs
=
100
,
batch_size
=
32
,
eval_dataset
=
flowers_validate
,
save_interval
=
1
)
```
#### 优化策略
Paddle2.0rc提供了多种优化器选择,如'SGD', 'Adam', 'Adamax'等,详细参见
[
策略
](
https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/optimizer/optimizer/Optimizer_cn.html
)
。
其中'Adam':
*
learning_rate: 全局学习率。默认为1e-3;
*
parameters: 待优化模型参数。
#### 运行配置
'Trainer' 主要控制Fine-tune的训练,包含以下可控制的参数:
*
model: 被优化模型;
*
optimizer: 优化器选择;
*
use_vdl: 是否使用vdl可视化训练过程;
*
checkpoint_dir: 保存模型参数的地址;
*
compare_metrics: 保存最优模型的衡量指标;
'trainer.train' 主要控制具体的训练过程,包含以下可控制的参数:
*
train_dataset: 训练时所用的数据集;
*
epochs: 训练轮数;
*
batch_size: 训练的批大小,如果使用GPU,请根据实际情况调整batch_size;
*
num_workers: works的数量,默认为0;
*
eval_dataset: 验证集;
*
log_interval: 打印日志的间隔, 单位为执行批训练的次数。
*
save_interval: 保存模型的间隔频次,单位为执行训练的轮数。
## 模型预测
当完成Fine-tune后,Fine-tune过程在验证集上表现最优的模型会被保存在'${CHECKPOINT_DIR}/best_model'目录下,其中'${CHECKPOINT_DIR}'目录为Fine-tune时所选择的保存checkpoint的目录。
我们使用该模型来进行预测。predict.py脚本如下:
```
python
import
paddle
import
paddlehub
as
hub
if
__name__
==
'__main__'
:
model
=
hub
.
Module
(
name
=
'ghostnet_x1_3_imagenet_ssld'
,
label_list
=
[
"roses"
,
"tulips"
,
"daisy"
,
"sunflowers"
,
"dandelion"
],
load_checkpoint
=
'/PATH/TO/CHECKPOINT'
)
result
=
model
.
predict
([
'flower.jpg'
])
```
参数配置正确后,请执行脚本'python predict.py', 加载模型具体可参见
[
加载
](
https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/api/paddle/framework/io/load_cn.html#load
)
。
**NOTE:**
进行预测时,所选择的module,checkpoint_dir,dataset必须和Fine-tune所用的一样。
## 服务部署
PaddleHub Serving可以部署一个在线分类任务服务
## Step1: 启动PaddleHub Serving
运行启动命令:
```
shell
$
hub serving start
-m
ghostnet_x1_3_imagenet_ssld
```
这样就完成了一个分类任务服务化API的部署,默认端口号为8866。
**NOTE:**
如使用GPU预测,则需要在启动服务之前,请设置CUDA_VISIBLE_DEVICES环境变量,否则不用设置。
## Step2: 发送预测请求
配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果
```
python
import
requests
import
json
import
cv2
import
base64
import
numpy
as
np
def
cv2_to_base64
(
image
):
data
=
cv2
.
imencode
(
'.jpg'
,
image
)[
1
]
return
base64
.
b64encode
(
data
.
tostring
()).
decode
(
'utf8'
)
def
base64_to_cv2
(
b64str
):
data
=
base64
.
b64decode
(
b64str
.
encode
(
'utf8'
))
data
=
np
.
fromstring
(
data
,
np
.
uint8
)
data
=
cv2
.
imdecode
(
data
,
cv2
.
IMREAD_COLOR
)
return
data
# 发送HTTP请求
org_im
=
cv2
.
imread
(
'/PATH/TO/IMAGE'
)
data
=
{
'images'
:[
cv2_to_base64
(
org_im
)],
'top_k'
:
2
}
headers
=
{
"Content-type"
:
"application/json"
}
url
=
"http://127.0.0.1:8866/predict/ghostnet_x1_3_imagenet_ssld"
r
=
requests
.
post
(
url
=
url
,
headers
=
headers
,
data
=
json
.
dumps
(
data
))
data
=
r
.
json
()[
"results"
][
'data'
]
```
### 查看代码
[
PaddleClas
](
https://github.com/PaddlePaddle/PaddleClas
)
### 依赖
paddlepaddle >= 2.0.0
paddlehub >= 2.0.0
modules/image/classification/ghostnet_x1_3_imagenet_ssld/label_list.txt
0 → 100644
浏览文件 @
50dd5bf5
此差异已折叠。
点击以展开。
modules/image/classification/ghostnet_x1_3_imagenet_ssld/module.py
0 → 100644
浏览文件 @
50dd5bf5
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录