diff --git a/demo/detection/yolov3_darknet53_pascalvoc/4026.jpeg b/demo/detection/yolov3_darknet53_pascalvoc/4026.jpeg new file mode 100644 index 0000000000000000000000000000000000000000..86190db9701dea3e3b674ec1480c9bbb1fd4bf5e Binary files /dev/null and b/demo/detection/yolov3_darknet53_pascalvoc/4026.jpeg differ diff --git a/demo/detection/yolov3_darknet53_pascalvoc/predict.py b/demo/detection/yolov3_darknet53_pascalvoc/predict.py new file mode 100644 index 0000000000000000000000000000000000000000..70a985f809582bc0f5d96e6442a7dace6cb3bc54 --- /dev/null +++ b/demo/detection/yolov3_darknet53_pascalvoc/predict.py @@ -0,0 +1,9 @@ +import paddle +import paddlehub as hub + +if __name__ == '__main__': + place = paddle.CUDAPlace(0) + paddle.disable_static() + model = hub.Module(name='yolov3_darknet53_pascalvoc', is_train=False) + model.eval() + model.predict(imgpath="4026.jpeg", filelist="/PATH/TO/JSON/FILE") diff --git a/demo/detection/yolov3_darknet53_pascalvoc/train.py b/demo/detection/yolov3_darknet53_pascalvoc/train.py new file mode 100644 index 0000000000000000000000000000000000000000..467309293d5451c7b35496d653030149a5cf30eb --- /dev/null +++ b/demo/detection/yolov3_darknet53_pascalvoc/train.py @@ -0,0 +1,24 @@ +import paddle +import paddlehub as hub +import paddle.nn as nn +from paddlehub.finetune.trainer import Trainer +from paddlehub.datasets.pascalvoc import DetectionData +import paddlehub.process.detect_transforms as T +if __name__ == "__main__": + paddle.disable_static() + + transform = T.Compose([ + T.RandomDistort(), + T.RandomExpand(fill=[0.485, 0.456, 0.406]), + T.RandomCrop(), + T.Resize(target_size=416), + T.RandomFlip(), + T.ShuffleBox(), + T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) + ]) + + train_reader = DetectionData(transform) + model = hub.Module(name='yolov3_darknet53_pascalvoc') + optimizer = paddle.optimizer.Adam(learning_rate=0.0001, parameters=model.parameters()) + trainer = Trainer(model, optimizer, checkpoint_dir='test_ckpt_img_det') + trainer.train(train_reader, epochs=5, batch_size=4, eval_dataset=train_reader, log_interval=1, save_interval=1) diff --git a/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/README.md b/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/README.md deleted file mode 100644 index 34bee6bfb0f15a38290b6c8bc737345fb11a931f..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/README.md +++ /dev/null @@ -1,159 +0,0 @@ -```shell -$ hub install mobilenet_v2_imagenet_ssld==1.0.0 -``` - -

-
MobileNet 系列的网络结构 -

- -模型的详情可参考[论文](https://arxiv.org/pdf/1801.04381.pdf) - -## 命令行预测 - -``` -hub run mobilenet_v2_imagenet_ssld --input_path "/PATH/TO/IMAGE" -``` - -## API - -```python -def get_expected_image_width() -``` - -返回预处理的图片宽度,也就是224。 - -```python -def get_expected_image_height() -``` - -返回预处理的图片高度,也就是224。 - -```python -def get_pretrained_images_mean() -``` - -返回预处理的图片均值,也就是 \[0.485, 0.456, 0.406\]。 - -```python -def get_pretrained_images_std() -``` - -返回预处理的图片标准差,也就是 \[0.229, 0.224, 0.225\]。 - - -```python -def context(trainable=True, pretrained=True) -``` - -**参数** - -* trainable (bool): 计算图的参数是否为可训练的; -* pretrained (bool): 是否加载默认的预训练模型。 - -**返回** - -* inputs (dict): 计算图的输入,key 为 'image', value 为图片的张量; -* outputs (dict): 计算图的输出,key 为 'classification' 和 'feature_map',其相应的值为: - * classification (paddle.fluid.framework.Variable): 分类结果,也就是全连接层的输出; - * feature\_map (paddle.fluid.framework.Variable): 特征匹配,全连接层前面的那个张量。 -* context\_prog(fluid.Program): 计算图,用于迁移学习。 - -```python -def classification(images=None, - paths=None, - batch_size=1, - use_gpu=False, - top_k=1): -``` - -**参数** - -* images (list\[numpy.ndarray\]): 图片数据,每一个图片数据的shape 均为 \[H, W, C\],颜色空间为 BGR; -* paths (list\[str\]): 图片的路径; -* batch\_size (int): batch 的大小; -* use\_gpu (bool): 是否使用 GPU 来预测; -* top\_k (int): 返回预测结果的前 k 个。 - -**返回** - -res (list\[dict\]): 分类结果,列表的每一个元素均为字典,其中 key 为识别动物的类别,value为置信度。 - -```python -def save_inference_model(dirname, - model_filename=None, - params_filename=None, - combined=True) -``` - -将模型保存到指定路径。 - -**参数** - -* dirname: 存在模型的目录名称 -* model_filename: 模型文件名称,默认为\_\_model\_\_ -* params_filename: 参数文件名称,默认为\_\_params\_\_(仅当`combined`为True时生效) -* combined: 是否将参数保存到统一的一个文件中 - -## 代码示例 - -```python -import paddlehub as hub -import cv2 - -classifier = hub.Module(name="mobilenet_v2_imagenet_ssld") - -result = classifier.classification(images=[cv2.imread('/PATH/TO/IMAGE')]) -# or -# result = classifier.classification(paths=['/PATH/TO/IMAGE']) -``` - -## 服务部署 - -PaddleHub Serving可以部署一个在线动物识别服务。 - -## 第一步:启动PaddleHub Serving - -运行启动命令: -```shell -$ hub serving start -m mobilenet_v2_imagenet_ssld -``` - -这样就完成了一个在线动物识别服务化API的部署,默认端口号为8866。 - -**NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA\_VISIBLE\_DEVICES环境变量,否则不用设置。 - -## 第二步:发送预测请求 - -配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果 - -```python -import requests -import json -import cv2 -import base64 - - -def cv2_to_base64(image): - data = cv2.imencode('.jpg', image)[1] - return base64.b64encode(data.tostring()).decode('utf8') - - -# 发送HTTP请求 -data = {'images':[cv2_to_base64(cv2.imread("/PATH/TO/IMAGE"))]} -headers = {"Content-type": "application/json"} -url = "http://127.0.0.1:8866/predict/mobilenet_v2_imagenet_ssld" -r = requests.post(url=url, headers=headers, data=json.dumps(data)) - -# 打印预测结果 -print(r.json()["results"]) -``` - -### 查看代码 - -[PaddleClas](https://github.com/PaddlePaddle/PaddleClas) - -### 依赖 - -paddlepaddle >= 1.6.2 - -paddlehub >= 1.6.0 diff --git a/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/__init__.py b/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/__init__.py deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/data_feed.py b/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/data_feed.py deleted file mode 100644 index d5ffb5efe9fdfbd143b949892aa44d851e907b41..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/data_feed.py +++ /dev/null @@ -1,84 +0,0 @@ -# coding=utf-8 -import os -import time -from collections import OrderedDict - -import cv2 -import numpy as np -from PIL import Image - -__all__ = ['reader'] - -DATA_DIM = 224 -img_mean = np.array([0.485, 0.456, 0.406]).reshape((3, 1, 1)) -img_std = np.array([0.229, 0.224, 0.225]).reshape((3, 1, 1)) - - -def resize_short(img, target_size): - percent = float(target_size) / min(img.size[0], img.size[1]) - resized_width = int(round(img.size[0] * percent)) - resized_height = int(round(img.size[1] * percent)) - img = img.resize((resized_width, resized_height), Image.LANCZOS) - return img - - -def crop_image(img, target_size, center): - width, height = img.size - size = target_size - if center == True: - w_start = (width - size) / 2 - h_start = (height - size) / 2 - else: - w_start = np.random.randint(0, width - size + 1) - h_start = np.random.randint(0, height - size + 1) - w_end = w_start + size - h_end = h_start + size - img = img.crop((w_start, h_start, w_end, h_end)) - return img - - -def process_image(img): - img = resize_short(img, target_size=256) - img = crop_image(img, target_size=DATA_DIM, center=True) - if img.mode != 'RGB': - img = img.convert('RGB') - img = np.array(img).astype('float32').transpose((2, 0, 1)) / 255 - img -= img_mean - img /= img_std - return img - - -def reader(images=None, paths=None): - """ - Preprocess to yield image. - - Args: - images (list[numpy.ndarray]): images data, shape of each is [H, W, C]. - paths (list[str]): paths to images. - - Yield: - each (collections.OrderedDict): info of original image, preprocessed image. - """ - component = list() - if paths: - for im_path in paths: - each = OrderedDict() - assert os.path.isfile( - im_path), "The {} isn't a valid file path.".format(im_path) - each['org_im_path'] = im_path - each['org_im'] = Image.open(im_path) - each['org_im_width'], each['org_im_height'] = each['org_im'].size - component.append(each) - if images is not None: - assert type(images), "images is a list." - for im in images: - each = OrderedDict() - each['org_im'] = Image.fromarray(im[:, :, ::-1]) - each['org_im_path'] = 'ndarray_time={}'.format( - round(time.time(), 6) * 1e6) - each['org_im_width'], each['org_im_height'] = each['org_im'].size - component.append(each) - - for element in component: - element['image'] = process_image(element['org_im']) - yield element diff --git a/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/label_list.txt b/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/label_list.txt deleted file mode 100644 index a509c007481d301e524e7b3c97561132dbfcc765..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/label_list.txt +++ /dev/null @@ -1,1000 +0,0 @@ -tench, Tinca tinca -goldfish, Carassius auratus -great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias -tiger shark, Galeocerdo cuvieri -hammerhead, hammerhead shark -electric ray, crampfish, numbfish, torpedo -stingray -cock -hen -ostrich, Struthio camelus -brambling, Fringilla montifringilla -goldfinch, Carduelis carduelis -house finch, linnet, Carpodacus mexicanus -junco, snowbird -indigo bunting, indigo finch, indigo bird, Passerina cyanea -robin, American robin, Turdus migratorius -bulbul -jay -magpie -chickadee -water ouzel, dipper -kite -bald eagle, American eagle, Haliaeetus leucocephalus -vulture -great grey owl, great gray owl, Strix nebulosa -European fire salamander, Salamandra salamandra -common newt, Triturus vulgaris -eft -spotted salamander, Ambystoma maculatum -axolotl, mud puppy, Ambystoma mexicanum -bullfrog, Rana catesbeiana -tree frog, tree-frog -tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui -loggerhead, loggerhead turtle, Caretta caretta -leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea -mud turtle -terrapin -box turtle, box tortoise -banded gecko -common iguana, iguana, Iguana iguana -American chameleon, anole, Anolis carolinensis -whiptail, whiptail lizard -agama -frilled lizard, Chlamydosaurus kingi -alligator lizard -Gila monster, Heloderma suspectum -green lizard, Lacerta viridis -African chameleon, Chamaeleo chamaeleon -Komodo dragon, Komodo lizard, dragon lizard, giant lizard, Varanus komodoensis -African crocodile, Nile crocodile, Crocodylus niloticus -American alligator, Alligator mississipiensis -triceratops -thunder snake, worm snake, Carphophis amoenus -ringneck snake, ring-necked snake, ring snake -hognose snake, puff adder, sand viper -green snake, grass snake -king snake, kingsnake -garter snake, grass snake -water snake -vine snake -night snake, Hypsiglena torquata -boa constrictor, Constrictor constrictor -rock python, rock snake, Python sebae -Indian cobra, Naja naja -green mamba -sea snake -horned viper, cerastes, sand viper, horned asp, Cerastes cornutus -diamondback, diamondback rattlesnake, Crotalus adamanteus -sidewinder, horned rattlesnake, Crotalus cerastes -trilobite -harvestman, daddy longlegs, Phalangium opilio -scorpion -black and gold garden spider, Argiope aurantia -barn spider, Araneus cavaticus -garden spider, Aranea diademata -black widow, Latrodectus mactans -tarantula -wolf spider, hunting spider -tick -centipede -black grouse -ptarmigan -ruffed grouse, partridge, Bonasa umbellus -prairie chicken, prairie grouse, prairie fowl -peacock -quail -partridge -African grey, African gray, Psittacus erithacus -macaw -sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita -lorikeet -coucal -bee eater -hornbill -hummingbird -jacamar -toucan -drake -red-breasted merganser, Mergus serrator -goose -black swan, Cygnus atratus -tusker -echidna, spiny anteater, anteater -platypus, duckbill, duckbilled platypus, duck-billed platypus, Ornithorhynchus anatinus -wallaby, brush kangaroo -koala, koala bear, kangaroo bear, native bear, Phascolarctos cinereus -wombat -jellyfish -sea anemone, anemone -brain coral -flatworm, platyhelminth -nematode, nematode worm, roundworm -conch -snail -slug -sea slug, nudibranch -chiton, coat-of-mail shell, sea cradle, polyplacophore -chambered nautilus, pearly nautilus, nautilus -Dungeness crab, Cancer magister -rock crab, Cancer irroratus -fiddler crab -king crab, Alaska crab, Alaskan king crab, Alaska king crab, Paralithodes camtschatica -American lobster, Northern lobster, Maine lobster, Homarus americanus -spiny lobster, langouste, rock lobster, crawfish, crayfish, sea crawfish -crayfish, crawfish, crawdad, crawdaddy -hermit crab -isopod -white stork, Ciconia ciconia -black stork, Ciconia nigra -spoonbill -flamingo -little blue heron, Egretta caerulea -American egret, great white heron, Egretta albus -bittern -crane -limpkin, Aramus pictus -European gallinule, Porphyrio porphyrio -American coot, marsh hen, mud hen, water hen, Fulica americana -bustard -ruddy turnstone, Arenaria interpres -red-backed sandpiper, dunlin, Erolia alpina -redshank, Tringa totanus -dowitcher -oystercatcher, oyster catcher -pelican -king penguin, Aptenodytes patagonica -albatross, mollymawk -grey whale, gray whale, devilfish, Eschrichtius gibbosus, Eschrichtius robustus -killer whale, killer, orca, grampus, sea wolf, Orcinus orca -dugong, Dugong dugon -sea lion -Chihuahua -Japanese spaniel -Maltese dog, Maltese terrier, Maltese -Pekinese, Pekingese, Peke -Shih-Tzu -Blenheim spaniel -papillon -toy terrier -Rhodesian ridgeback -Afghan hound, Afghan -basset, basset hound -beagle -bloodhound, sleuthhound -bluetick -black-and-tan coonhound -Walker hound, Walker foxhound -English foxhound -redbone -borzoi, Russian wolfhound -Irish wolfhound -Italian greyhound -whippet -Ibizan hound, Ibizan Podenco -Norwegian elkhound, elkhound -otterhound, otter hound -Saluki, gazelle hound -Scottish deerhound, deerhound -Weimaraner -Staffordshire bullterrier, Staffordshire bull terrier -American Staffordshire terrier, Staffordshire terrier, American pit bull terrier, pit bull terrier -Bedlington terrier -Border terrier -Kerry blue terrier -Irish terrier -Norfolk terrier -Norwich terrier -Yorkshire terrier -wire-haired fox terrier -Lakeland terrier -Sealyham terrier, Sealyham -Airedale, Airedale terrier -cairn, cairn terrier -Australian terrier -Dandie Dinmont, Dandie Dinmont terrier -Boston bull, Boston terrier -miniature schnauzer -giant schnauzer -standard schnauzer -Scotch terrier, Scottish terrier, Scottie -Tibetan terrier, chrysanthemum dog -silky terrier, Sydney silky -soft-coated wheaten terrier -West Highland white terrier -Lhasa, Lhasa apso -flat-coated retriever -curly-coated retriever -golden retriever -Labrador retriever -Chesapeake Bay retriever -German short-haired pointer -vizsla, Hungarian pointer -English setter -Irish setter, red setter -Gordon setter -Brittany spaniel -clumber, clumber spaniel -English springer, English springer spaniel -Welsh springer spaniel -cocker spaniel, English cocker spaniel, cocker -Sussex spaniel -Irish water spaniel -kuvasz -schipperke -groenendael -malinois -briard -kelpie -komondor -Old English sheepdog, bobtail -Shetland sheepdog, Shetland sheep dog, Shetland -collie -Border collie -Bouvier des Flandres, Bouviers des Flandres -Rottweiler -German shepherd, German shepherd dog, German police dog, alsatian -Doberman, Doberman pinscher -miniature pinscher -Greater Swiss Mountain dog -Bernese mountain dog -Appenzeller -EntleBucher -boxer -bull mastiff -Tibetan mastiff -French bulldog -Great Dane -Saint Bernard, St Bernard -Eskimo dog, husky -malamute, malemute, Alaskan malamute -Siberian husky -dalmatian, coach dog, carriage dog -affenpinscher, monkey pinscher, monkey dog -basenji -pug, pug-dog -Leonberg -Newfoundland, Newfoundland dog -Great Pyrenees -Samoyed, Samoyede -Pomeranian -chow, chow chow -keeshond -Brabancon griffon -Pembroke, Pembroke Welsh corgi -Cardigan, Cardigan Welsh corgi -toy poodle -miniature poodle -standard poodle -Mexican hairless -timber wolf, grey wolf, gray wolf, Canis lupus -white wolf, Arctic wolf, Canis lupus tundrarum -red wolf, maned wolf, Canis rufus, Canis niger -coyote, prairie wolf, brush wolf, Canis latrans -dingo, warrigal, warragal, Canis dingo -dhole, Cuon alpinus -African hunting dog, hyena dog, Cape hunting dog, Lycaon pictus -hyena, hyaena -red fox, Vulpes vulpes -kit fox, Vulpes macrotis -Arctic fox, white fox, Alopex lagopus -grey fox, gray fox, Urocyon cinereoargenteus -tabby, tabby cat -tiger cat -Persian cat -Siamese cat, Siamese -Egyptian cat -cougar, puma, catamount, mountain lion, painter, panther, Felis concolor -lynx, catamount -leopard, Panthera pardus -snow leopard, ounce, Panthera uncia -jaguar, panther, Panthera onca, Felis onca -lion, king of beasts, Panthera leo -tiger, Panthera tigris -cheetah, chetah, Acinonyx jubatus -brown bear, bruin, Ursus arctos -American black bear, black bear, Ursus americanus, Euarctos americanus -ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus -sloth bear, Melursus ursinus, Ursus ursinus -mongoose -meerkat, mierkat -tiger beetle -ladybug, ladybeetle, lady beetle, ladybird, ladybird beetle -ground beetle, carabid beetle -long-horned beetle, longicorn, longicorn beetle -leaf beetle, chrysomelid -dung beetle -rhinoceros beetle -weevil -fly -bee -ant, emmet, pismire -grasshopper, hopper -cricket -walking stick, walkingstick, stick insect -cockroach, roach -mantis, mantid -cicada, cicala -leafhopper -lacewing, lacewing fly -dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk -damselfly -admiral -ringlet, ringlet butterfly -monarch, monarch butterfly, milkweed butterfly, Danaus plexippus -cabbage butterfly -sulphur butterfly, sulfur butterfly -lycaenid, lycaenid butterfly -starfish, sea star -sea urchin -sea cucumber, holothurian -wood rabbit, cottontail, cottontail rabbit -hare -Angora, Angora rabbit -hamster -porcupine, hedgehog -fox squirrel, eastern fox squirrel, Sciurus niger -marmot -beaver -guinea pig, Cavia cobaya -sorrel -zebra -hog, pig, grunter, squealer, Sus scrofa -wild boar, boar, Sus scrofa -warthog -hippopotamus, hippo, river horse, Hippopotamus amphibius -ox -water buffalo, water ox, Asiatic buffalo, Bubalus bubalis -bison -ram, tup -bighorn, bighorn sheep, cimarron, Rocky Mountain bighorn, Rocky Mountain sheep, Ovis canadensis -ibex, Capra ibex -hartebeest -impala, Aepyceros melampus -gazelle -Arabian camel, dromedary, Camelus dromedarius -llama -weasel -mink -polecat, fitch, foulmart, foumart, Mustela putorius -black-footed ferret, ferret, Mustela nigripes -otter -skunk, polecat, wood pussy -badger -armadillo -three-toed sloth, ai, Bradypus tridactylus -orangutan, orang, orangutang, Pongo pygmaeus -gorilla, Gorilla gorilla -chimpanzee, chimp, Pan troglodytes -gibbon, Hylobates lar -siamang, Hylobates syndactylus, Symphalangus syndactylus -guenon, guenon monkey -patas, hussar monkey, Erythrocebus patas -baboon -macaque -langur -colobus, colobus monkey -proboscis monkey, Nasalis larvatus -marmoset -capuchin, ringtail, Cebus capucinus -howler monkey, howler -titi, titi monkey -spider monkey, Ateles geoffroyi -squirrel monkey, Saimiri sciureus -Madagascar cat, ring-tailed lemur, Lemur catta -indri, indris, Indri indri, Indri brevicaudatus -Indian elephant, Elephas maximus -African elephant, Loxodonta africana -lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens -giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca -barracouta, snoek -eel -coho, cohoe, coho salmon, blue jack, silver salmon, Oncorhynchus kisutch -rock beauty, Holocanthus tricolor -anemone fish -sturgeon -gar, garfish, garpike, billfish, Lepisosteus osseus -lionfish -puffer, pufferfish, blowfish, globefish -abacus -abaya -academic gown, academic robe, judge's robe -accordion, piano accordion, squeeze box -acoustic guitar -aircraft carrier, carrier, flattop, attack aircraft carrier -airliner -airship, dirigible -altar -ambulance -amphibian, amphibious vehicle -analog clock -apiary, bee house -apron -ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, dustbin, trash barrel, trash bin -assault rifle, assault gun -backpack, back pack, knapsack, packsack, rucksack, haversack -bakery, bakeshop, bakehouse -balance beam, beam -balloon -ballpoint, ballpoint pen, ballpen, Biro -Band Aid -banjo -bannister, banister, balustrade, balusters, handrail -barbell -barber chair -barbershop -barn -barometer -barrel, cask -barrow, garden cart, lawn cart, wheelbarrow -baseball -basketball -bassinet -bassoon -bathing cap, swimming cap -bath towel -bathtub, bathing tub, bath, tub -beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon -beacon, lighthouse, beacon light, pharos -beaker -bearskin, busby, shako -beer bottle -beer glass -bell cote, bell cot -bib -bicycle-built-for-two, tandem bicycle, tandem -bikini, two-piece -binder, ring-binder -binoculars, field glasses, opera glasses -birdhouse -boathouse -bobsled, bobsleigh, bob -bolo tie, bolo, bola tie, bola -bonnet, poke bonnet -bookcase -bookshop, bookstore, bookstall -bottlecap -bow -bow tie, bow-tie, bowtie -brass, memorial tablet, plaque -brassiere, bra, bandeau -breakwater, groin, groyne, mole, bulwark, seawall, jetty -breastplate, aegis, egis -broom -bucket, pail -buckle -bulletproof vest -bullet train, bullet -butcher shop, meat market -cab, hack, taxi, taxicab -caldron, cauldron -candle, taper, wax light -cannon -canoe -can opener, tin opener -cardigan -car mirror -carousel, carrousel, merry-go-round, roundabout, whirligig -carpenter's kit, tool kit -carton -car wheel -cash machine, cash dispenser, automated teller machine, automatic teller machine, automated teller, automatic teller, ATM -cassette -cassette player -castle -catamaran -CD player -cello, violoncello -cellular telephone, cellular phone, cellphone, cell, mobile phone -chain -chainlink fence -chain mail, ring mail, mail, chain armor, chain armour, ring armor, ring armour -chain saw, chainsaw -chest -chiffonier, commode -chime, bell, gong -china cabinet, china closet -Christmas stocking -church, church building -cinema, movie theater, movie theatre, movie house, picture palace -cleaver, meat cleaver, chopper -cliff dwelling -cloak -clog, geta, patten, sabot -cocktail shaker -coffee mug -coffeepot -coil, spiral, volute, whorl, helix -combination lock -computer keyboard, keypad -confectionery, confectionary, candy store -container ship, containership, container vessel -convertible -corkscrew, bottle screw -cornet, horn, trumpet, trump -cowboy boot -cowboy hat, ten-gallon hat -cradle -crane -crash helmet -crate -crib, cot -Crock Pot -croquet ball -crutch -cuirass -dam, dike, dyke -desk -desktop computer -dial telephone, dial phone -diaper, nappy, napkin -digital clock -digital watch -dining table, board -dishrag, dishcloth -dishwasher, dish washer, dishwashing machine -disk brake, disc brake -dock, dockage, docking facility -dogsled, dog sled, dog sleigh -dome -doormat, welcome mat -drilling platform, offshore rig -drum, membranophone, tympan -drumstick -dumbbell -Dutch oven -electric fan, blower -electric guitar -electric locomotive -entertainment center -envelope -espresso maker -face powder -feather boa, boa -file, file cabinet, filing cabinet -fireboat -fire engine, fire truck -fire screen, fireguard -flagpole, flagstaff -flute, transverse flute -folding chair -football helmet -forklift -fountain -fountain pen -four-poster -freight car -French horn, horn -frying pan, frypan, skillet -fur coat -garbage truck, dustcart -gasmask, respirator, gas helmet -gas pump, gasoline pump, petrol pump, island dispenser -goblet -go-kart -golf ball -golfcart, golf cart -gondola -gong, tam-tam -gown -grand piano, grand -greenhouse, nursery, glasshouse -grille, radiator grille -grocery store, grocery, food market, market -guillotine -hair slide -hair spray -half track -hammer -hamper -hand blower, blow dryer, blow drier, hair dryer, hair drier -hand-held computer, hand-held microcomputer -handkerchief, hankie, hanky, hankey -hard disc, hard disk, fixed disk -harmonica, mouth organ, harp, mouth harp -harp -harvester, reaper -hatchet -holster -home theater, home theatre -honeycomb -hook, claw -hoopskirt, crinoline -horizontal bar, high bar -horse cart, horse-cart -hourglass -iPod -iron, smoothing iron -jack-o'-lantern -jean, blue jean, denim -jeep, landrover -jersey, T-shirt, tee shirt -jigsaw puzzle -jinrikisha, ricksha, rickshaw -joystick -kimono -knee pad -knot -lab coat, laboratory coat -ladle -lampshade, lamp shade -laptop, laptop computer -lawn mower, mower -lens cap, lens cover -letter opener, paper knife, paperknife -library -lifeboat -lighter, light, igniter, ignitor -limousine, limo -liner, ocean liner -lipstick, lip rouge -Loafer -lotion -loudspeaker, speaker, speaker unit, loudspeaker system, speaker system -loupe, jeweler's loupe -lumbermill, sawmill -magnetic compass -mailbag, postbag -mailbox, letter box -maillot -maillot, tank suit -manhole cover -maraca -marimba, xylophone -mask -matchstick -maypole -maze, labyrinth -measuring cup -medicine chest, medicine cabinet -megalith, megalithic structure -microphone, mike -microwave, microwave oven -military uniform -milk can -minibus -miniskirt, mini -minivan -missile -mitten -mixing bowl -mobile home, manufactured home -Model T -modem -monastery -monitor -moped -mortar -mortarboard -mosque -mosquito net -motor scooter, scooter -mountain bike, all-terrain bike, off-roader -mountain tent -mouse, computer mouse -mousetrap -moving van -muzzle -nail -neck brace -necklace -nipple -notebook, notebook computer -obelisk -oboe, hautboy, hautbois -ocarina, sweet potato -odometer, hodometer, mileometer, milometer -oil filter -organ, pipe organ -oscilloscope, scope, cathode-ray oscilloscope, CRO -overskirt -oxcart -oxygen mask -packet -paddle, boat paddle -paddlewheel, paddle wheel -padlock -paintbrush -pajama, pyjama, pj's, jammies -palace -panpipe, pandean pipe, syrinx -paper towel -parachute, chute -parallel bars, bars -park bench -parking meter -passenger car, coach, carriage -patio, terrace -pay-phone, pay-station -pedestal, plinth, footstall -pencil box, pencil case -pencil sharpener -perfume, essence -Petri dish -photocopier -pick, plectrum, plectron -pickelhaube -picket fence, paling -pickup, pickup truck -pier -piggy bank, penny bank -pill bottle -pillow -ping-pong ball -pinwheel -pirate, pirate ship -pitcher, ewer -plane, carpenter's plane, woodworking plane -planetarium -plastic bag -plate rack -plow, plough -plunger, plumber's helper -Polaroid camera, Polaroid Land camera -pole -police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria -poncho -pool table, billiard table, snooker table -pop bottle, soda bottle -pot, flowerpot -potter's wheel -power drill -prayer rug, prayer mat -printer -prison, prison house -projectile, missile -projector -puck, hockey puck -punching bag, punch bag, punching ball, punchball -purse -quill, quill pen -quilt, comforter, comfort, puff -racer, race car, racing car -racket, racquet -radiator -radio, wireless -radio telescope, radio reflector -rain barrel -recreational vehicle, RV, R.V. -reel -reflex camera -refrigerator, icebox -remote control, remote -restaurant, eating house, eating place, eatery -revolver, six-gun, six-shooter -rifle -rocking chair, rocker -rotisserie -rubber eraser, rubber, pencil eraser -rugby ball -rule, ruler -running shoe -safe -safety pin -saltshaker, salt shaker -sandal -sarong -sax, saxophone -scabbard -scale, weighing machine -school bus -schooner -scoreboard -screen, CRT screen -screw -screwdriver -seat belt, seatbelt -sewing machine -shield, buckler -shoe shop, shoe-shop, shoe store -shoji -shopping basket -shopping cart -shovel -shower cap -shower curtain -ski -ski mask -sleeping bag -slide rule, slipstick -sliding door -slot, one-armed bandit -snorkel -snowmobile -snowplow, snowplough -soap dispenser -soccer ball -sock -solar dish, solar collector, solar furnace -sombrero -soup bowl -space bar -space heater -space shuttle -spatula -speedboat -spider web, spider's web -spindle -sports car, sport car -spotlight, spot -stage -steam locomotive -steel arch bridge -steel drum -stethoscope -stole -stone wall -stopwatch, stop watch -stove -strainer -streetcar, tram, tramcar, trolley, trolley car -stretcher -studio couch, day bed -stupa, tope -submarine, pigboat, sub, U-boat -suit, suit of clothes -sundial -sunglass -sunglasses, dark glasses, shades -sunscreen, sunblock, sun blocker -suspension bridge -swab, swob, mop -sweatshirt -swimming trunks, bathing trunks -swing -switch, electric switch, electrical switch -syringe -table lamp -tank, army tank, armored combat vehicle, armoured combat vehicle -tape player -teapot -teddy, teddy bear -television, television system -tennis ball -thatch, thatched roof -theater curtain, theatre curtain -thimble -thresher, thrasher, threshing machine -throne -tile roof -toaster -tobacco shop, tobacconist shop, tobacconist -toilet seat -torch -totem pole -tow truck, tow car, wrecker -toyshop -tractor -trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi -tray -trench coat -tricycle, trike, velocipede -trimaran -tripod -triumphal arch -trolleybus, trolley coach, trackless trolley -trombone -tub, vat -turnstile -typewriter keyboard -umbrella -unicycle, monocycle -upright, upright piano -vacuum, vacuum cleaner -vase -vault -velvet -vending machine -vestment -viaduct -violin, fiddle -volleyball -waffle iron -wall clock -wallet, billfold, notecase, pocketbook -wardrobe, closet, press -warplane, military plane -washbasin, handbasin, washbowl, lavabo, wash-hand basin -washer, automatic washer, washing machine -water bottle -water jug -water tower -whiskey jug -whistle -wig -window screen -window shade -Windsor tie -wine bottle -wing -wok -wooden spoon -wool, woolen, woollen -worm fence, snake fence, snake-rail fence, Virginia fence -wreck -yawl -yurt -web site, website, internet site, site -comic book -crossword puzzle, crossword -street sign -traffic light, traffic signal, stoplight -book jacket, dust cover, dust jacket, dust wrapper -menu -plate -guacamole -consomme -hot pot, hotpot -trifle -ice cream, icecream -ice lolly, lolly, lollipop, popsicle -French loaf -bagel, beigel -pretzel -cheeseburger -hotdog, hot dog, red hot -mashed potato -head cabbage -broccoli -cauliflower -zucchini, courgette -spaghetti squash -acorn squash -butternut squash -cucumber, cuke -artichoke, globe artichoke -bell pepper -cardoon -mushroom -Granny Smith -strawberry -orange -lemon -fig -pineapple, ananas -banana -jackfruit, jak, jack -custard apple -pomegranate -hay -carbonara -chocolate sauce, chocolate syrup -dough -meat loaf, meatloaf -pizza, pizza pie -potpie -burrito -red wine -espresso -cup -eggnog -alp -bubble -cliff, drop, drop-off -coral reef -geyser -lakeside, lakeshore -promontory, headland, head, foreland -sandbar, sand bar -seashore, coast, seacoast, sea-coast -valley, vale -volcano -ballplayer, baseball player -groom, bridegroom -scuba diver -rapeseed -daisy -yellow lady's slipper, yellow lady-slipper, Cypripedium calceolus, Cypripedium parviflorum -corn -acorn -hip, rose hip, rosehip -buckeye, horse chestnut, conker -coral fungus -agaric -gyromitra -stinkhorn, carrion fungus -earthstar -hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa -bolete -ear, spike, capitulum -toilet tissue, toilet paper, bathroom tissue diff --git a/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/mobilenet_v2.py b/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/mobilenet_v2.py deleted file mode 100644 index 5284ef2b36ab8209bd44aebdd3a879ecd39de7df..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/mobilenet_v2.py +++ /dev/null @@ -1,231 +0,0 @@ -# copyright (c) 2020 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. - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import paddle.fluid as fluid -from paddle.fluid.initializer import MSRA -from paddle.fluid.param_attr import ParamAttr - -__all__ = [ - 'MobileNetV2_x0_25', 'MobileNetV2_x0_5', 'MobileNetV2_x0_75', - 'MobileNetV2_x1_0', 'MobileNetV2_x1_5', 'MobileNetV2_x2_0', 'MobileNetV2' -] - - -class MobileNetV2(): - def __init__(self, scale=1.0): - self.scale = scale - - def net(self, input, class_dim=1000): - scale = self.scale - bottleneck_params_list = [ - (1, 16, 1, 1), - (6, 24, 2, 2), - (6, 32, 3, 2), - (6, 64, 4, 2), - (6, 96, 3, 1), - (6, 160, 3, 2), - (6, 320, 1, 1), - ] - - #conv1 - input = self.conv_bn_layer( - input, - num_filters=int(32 * scale), - filter_size=3, - stride=2, - padding=1, - if_act=True, - name='conv1_1') - - # bottleneck sequences - i = 1 - in_c = int(32 * scale) - for layer_setting in bottleneck_params_list: - t, c, n, s = layer_setting - i += 1 - input = self.invresi_blocks( - input=input, - in_c=in_c, - t=t, - c=int(c * scale), - n=n, - s=s, - name='conv' + str(i)) - in_c = int(c * scale) - #last_conv - input = self.conv_bn_layer( - input=input, - num_filters=int(1280 * scale) if scale > 1.0 else 1280, - filter_size=1, - stride=1, - padding=0, - if_act=True, - name='conv9') - - input = fluid.layers.pool2d( - input=input, pool_type='avg', global_pooling=True) - - output = fluid.layers.fc( - input=input, - size=class_dim, - param_attr=ParamAttr(name='fc10_weights'), - bias_attr=ParamAttr(name='fc10_offset')) - return output, input - - def conv_bn_layer(self, - input, - filter_size, - num_filters, - stride, - padding, - channels=None, - num_groups=1, - if_act=True, - name=None, - use_cudnn=True): - conv = fluid.layers.conv2d( - input=input, - num_filters=num_filters, - filter_size=filter_size, - stride=stride, - padding=padding, - groups=num_groups, - act=None, - use_cudnn=use_cudnn, - param_attr=ParamAttr(name=name + '_weights'), - bias_attr=False) - bn_name = name + '_bn' - bn = fluid.layers.batch_norm( - input=conv, - param_attr=ParamAttr(name=bn_name + "_scale"), - bias_attr=ParamAttr(name=bn_name + "_offset"), - moving_mean_name=bn_name + '_mean', - moving_variance_name=bn_name + '_variance') - if if_act: - return fluid.layers.relu6(bn) - else: - return bn - - def shortcut(self, input, data_residual): - return fluid.layers.elementwise_add(input, data_residual) - - def inverted_residual_unit(self, - input, - num_in_filter, - num_filters, - ifshortcut, - stride, - filter_size, - padding, - expansion_factor, - name=None): - num_expfilter = int(round(num_in_filter * expansion_factor)) - - channel_expand = self.conv_bn_layer( - input=input, - num_filters=num_expfilter, - filter_size=1, - stride=1, - padding=0, - num_groups=1, - if_act=True, - name=name + '_expand') - - bottleneck_conv = self.conv_bn_layer( - input=channel_expand, - num_filters=num_expfilter, - filter_size=filter_size, - stride=stride, - padding=padding, - num_groups=num_expfilter, - if_act=True, - name=name + '_dwise', - use_cudnn=False) - - linear_out = self.conv_bn_layer( - input=bottleneck_conv, - num_filters=num_filters, - filter_size=1, - stride=1, - padding=0, - num_groups=1, - if_act=False, - name=name + '_linear') - if ifshortcut: - out = self.shortcut(input=input, data_residual=linear_out) - return out - else: - return linear_out - - def invresi_blocks(self, input, in_c, t, c, n, s, name=None): - first_block = self.inverted_residual_unit( - input=input, - num_in_filter=in_c, - num_filters=c, - ifshortcut=False, - stride=s, - filter_size=3, - padding=1, - expansion_factor=t, - name=name + '_1') - - last_residual_block = first_block - last_c = c - - for i in range(1, n): - last_residual_block = self.inverted_residual_unit( - input=last_residual_block, - num_in_filter=last_c, - num_filters=c, - ifshortcut=True, - stride=1, - filter_size=3, - padding=1, - expansion_factor=t, - name=name + '_' + str(i + 1)) - return last_residual_block - - -def MobileNetV2_x0_25(): - model = MobileNetV2(scale=0.25) - return model - - -def MobileNetV2_x0_5(): - model = MobileNetV2(scale=0.5) - return model - - -def MobileNetV2_x0_75(): - model = MobileNetV2(scale=0.75) - return model - - -def MobileNetV2_x1_0(): - model = MobileNetV2(scale=1.0) - return model - - -def MobileNetV2_x1_5(): - model = MobileNetV2(scale=1.5) - return model - - -def MobileNetV2_x2_0(): - model = MobileNetV2(scale=2.0) - return model diff --git a/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/module.py b/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/module.py index a2bacc749572129cbb5c8e1a4c3257b812df416b..510b9d14c35e565d4e3eab3d36220c805cca3b6d 100644 --- a/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/module.py +++ b/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/module.py @@ -1,278 +1,209 @@ -# coding=utf-8 -from __future__ import absolute_import -from __future__ import division - -import ast -import argparse +# copyright (c) 2020 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 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 mobilenet_v2_imagenet_ssld.processor import postprocess, base64_to_cv2 -from mobilenet_v2_imagenet_ssld.data_feed import reader -from mobilenet_v2_imagenet_ssld.mobilenet_v2 import MobileNetV2 - - -@moduleinfo( - name="mobilenet_v2_imagenet_ssld", - type="CV/image_classification", - author="paddlepaddle", - author_email="paddle-dev@baidu.com", - summary= - "Mobilenet_V2 is a image classfication model, this module is trained with ImageNet-2012 dataset.", - version="1.0.0") -class MobileNetV2ImageNetSSLD(hub.Module): - def _initialize(self): - self.default_pretrained_model_path = os.path.join( - self.directory, "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] - self._set_config() - - def get_expected_image_width(self): - return 224 - - def get_expected_image_height(self): - return 224 - - def get_pretrained_images_mean(self): - im_mean = np.array([0.485, 0.456, 0.406]).reshape(1, 3) - return im_mean - - def get_pretrained_images_std(self): - im_std = np.array([0.229, 0.224, 0.225]).reshape(1, 3) - return im_std - - def _set_config(self): - """ - predictor config setting - """ - cpu_config = AnalysisConfig(self.default_pretrained_model_path) - cpu_config.disable_glog_info() - cpu_config.disable_gpu() - self.cpu_predictor = create_paddle_predictor(cpu_config) - - try: - _places = os.environ["CUDA_VISIBLE_DEVICES"] - int(_places[0]) - use_gpu = True - except: - use_gpu = False - if use_gpu: - gpu_config = AnalysisConfig(self.default_pretrained_model_path) - 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") - mobile_net = MobileNetV2() - output, feature_map = mobile_net.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 - - 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) - - def classification(self, - images=None, - paths=None, - batch_size=1, - use_gpu=False, - top_k=1): - """ - API for image classification. - - Args: - images (numpy.ndarray): data of images, shape of each is [H, W, C], color space must be BGR. - paths (list[str]): The paths of images. - batch_size (int): batch size. - use_gpu (bool): Whether to use gpu. - top_k (int): Return top k results. - - 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) - - total_num = len(all_data) - loop_num = int(np.ceil(total_num / batch_size)) - - res = list() - for iter_id in range(loop_num): - batch_data = list() - handle_id = iter_id * batch_size - for image_id in range(batch_size): - try: - batch_data.append(all_data[handle_id + image_id]) - except: - 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) - res += out - return res - - @serving - def serving_method(self, images, **kwargs): - """ - Run as a service. - """ - images_decode = [base64_to_cv2(image) for image in images] - results = self.classification(images=images_decode, **kwargs) - return results - - @runnable - 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.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.") - self.add_module_config_arg() - self.add_module_input_arg() - args = self.parser.parse_args(argvs) - results = self.classification( - paths=[args.input_path], - batch_size=args.batch_size, - use_gpu=args.use_gpu) - return results - - 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( - '--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.") - - def add_module_input_arg(self): - """ - Add the command input options. - """ - self.arg_input_group.add_argument( - '--input_path', type=str, help="path to image.") +import paddle +from paddle import ParamAttr +import paddle.nn as nn +import paddle.nn.functional as F +from paddle.nn import Conv2d, BatchNorm, Linear, Dropout +from paddle.nn import AdaptiveAvgPool2d, MaxPool2d, AvgPool2d +from paddlehub.module.module import moduleinfo +from paddlehub.module.cv_module import ImageClassifierModule + + +class ConvBNLayer(nn.Layer): + """Basic conv bn layer.""" + def __init__(self, + num_channels: int, + filter_size: int, + num_filters: int, + stride: int, + padding: int, + num_groups: int = 1, + name: str = None): + super(ConvBNLayer, self).__init__() + + self._conv = Conv2d(in_channels=num_channels, + out_channels=num_filters, + kernel_size=filter_size, + stride=stride, + padding=padding, + groups=num_groups, + weight_attr=ParamAttr(name=name + "_weights"), + bias_attr=False) + + self._batch_norm = BatchNorm(num_filters, + param_attr=ParamAttr(name=name + "_bn_scale"), + bias_attr=ParamAttr(name=name + "_bn_offset"), + moving_mean_name=name + "_bn_mean", + moving_variance_name=name + "_bn_variance") + + def forward(self, inputs: paddle.Tensor, if_act: bool = True): + y = self._conv(inputs) + y = self._batch_norm(y) + if if_act: + y = F.relu6(y) + return y + + +class InvertedResidualUnit(nn.Layer): + """Inverted Residual unit.""" + def __init__(self, num_channels: int, num_in_filter: int, num_filters: int, stride: int, filter_size: int, + padding: int, expansion_factor: int, name: str): + super(InvertedResidualUnit, self).__init__() + + num_expfilter = int(round(num_in_filter * expansion_factor)) + self._expand_conv = ConvBNLayer(num_channels=num_channels, + num_filters=num_expfilter, + filter_size=1, + stride=1, + padding=0, + num_groups=1, + name=name + "_expand") + + self._bottleneck_conv = ConvBNLayer(num_channels=num_expfilter, + num_filters=num_expfilter, + filter_size=filter_size, + stride=stride, + padding=padding, + num_groups=num_expfilter, + name=name + "_dwise") + + self._linear_conv = ConvBNLayer(num_channels=num_expfilter, + num_filters=num_filters, + filter_size=1, + stride=1, + padding=0, + num_groups=1, + name=name + "_linear") + + def forward(self, inputs: paddle.Tensor, ifshortcut: bool): + y = self._expand_conv(inputs, if_act=True) + y = self._bottleneck_conv(y, if_act=True) + y = self._linear_conv(y, if_act=False) + if ifshortcut: + y = paddle.elementwise_add(inputs, y) + return y + + +class InversiBlocks(nn.Layer): + """Inverted residual block composed by inverted residual unit.""" + def __init__(self, in_c: int, t: int, c: int, n: int, s: int, name: str): + super(InversiBlocks, self).__init__() + + self._first_block = InvertedResidualUnit(num_channels=in_c, + num_in_filter=in_c, + num_filters=c, + stride=s, + filter_size=3, + padding=1, + expansion_factor=t, + name=name + "_1") + + self._block_list = [] + for i in range(1, n): + block = self.add_sublayer(name + "_" + str(i + 1), + sublayer=InvertedResidualUnit(num_channels=c, + num_in_filter=c, + num_filters=c, + stride=1, + filter_size=3, + padding=1, + expansion_factor=t, + name=name + "_" + str(i + 1))) + self._block_list.append(block) + + def forward(self, inputs: paddle.Tensor): + y = self._first_block(inputs, ifshortcut=False) + for block in self._block_list: + y = block(y, ifshortcut=True) + return y + + +@moduleinfo(name="mobilenet_v2_imagenet_ssld", + type="cv/classification", + author="paddlepaddle", + author_email="", + summary="mobilenet_v2_imagenet_ssld is a classification model, " + "this module is trained with Imagenet dataset.", + version="1.1.0", + meta=ImageClassifierModule) +class MobileNet(nn.Layer): + """MobileNetV2""" + def __init__(self, class_dim: int = 1000, load_checkpoint: str = None): + super(MobileNet, self).__init__() + + self.class_dim = class_dim + + bottleneck_params_list = [(1, 16, 1, 1), (6, 24, 2, 2), (6, 32, 3, 2), (6, 64, 4, 2), (6, 96, 3, 1), + (6, 160, 3, 2), (6, 320, 1, 1)] + + self.conv1 = ConvBNLayer(num_channels=3, + num_filters=int(32), + filter_size=3, + stride=2, + padding=1, + name="conv1_1") + + self.block_list = [] + i = 1 + in_c = int(32) + for layer_setting in bottleneck_params_list: + t, c, n, s = layer_setting + i += 1 + block = self.add_sublayer("conv" + str(i), + sublayer=InversiBlocks(in_c=in_c, t=t, c=int(c), n=n, s=s, name="conv" + str(i))) + self.block_list.append(block) + in_c = int(c) + + self.out_c = 1280 + self.conv9 = ConvBNLayer(num_channels=in_c, + num_filters=self.out_c, + filter_size=1, + stride=1, + padding=0, + name="conv9") + + self.pool2d_avg = AdaptiveAvgPool2d(1) + + self.out = Linear(self.out_c, + class_dim, + weight_attr=ParamAttr(name="fc10_weights"), + bias_attr=ParamAttr(name="fc10_offset")) + + if load_checkpoint is not None: + model_dict = paddle.load(load_checkpoint)[0] + self.set_dict(model_dict) + print("load custom checkpoint success") + + else: + checkpoint = os.path.join(self.directory, 'mobilenet_v2_ssld.pdparams.pdparams') + if not os.path.exists(checkpoint): + os.system( + 'wget https://paddlehub.bj.bcebos.com/dygraph/image_classification/mobilenet_v2_ssld.pdparams -O ' + + checkpoint) + model_dict = paddle.load(checkpoint)[0] + self.set_dict(model_dict) + print("load pretrained checkpoint success") + + def forward(self, inputs: paddle.Tensor): + y = self.conv1(inputs, if_act=True) + for block in self.block_list: + y = block(y) + y = self.conv9(y, if_act=True) + y = self.pool2d_avg(y) + y = paddle.reshape(y, shape=[-1, self.out_c]) + y = self.out(y) + return y diff --git a/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/processor.py b/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/processor.py deleted file mode 100644 index fa8cbb502312e6ef80697ab63b767d4077b3847b..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/mobilenet_v2_imagenet_ssld/processor.py +++ /dev/null @@ -1,55 +0,0 @@ -# coding=utf-8 -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import base64 -import cv2 -import os - -import numpy as np - - -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 - - -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)) - x = np.exp(x) - tmp = np.sum(x, axis=1) - x /= tmp.reshape((x.shape[0], 1)) - else: - tmp = np.max(x) - x -= tmp - x = np.exp(x) - tmp = np.sum(x) - x /= tmp - return x - - -def postprocess(data_out, label_list, top_k): - """ - Postprocess output of network, one image at a time. - - Args: - data_out (numpy.ndarray): output data of network. - label_list (list): list of label. - top_k (int): Return top k results. - """ - output = [] - for result in data_out: - result_i = softmax(result) - output_i = {} - indexs = np.argsort(result_i)[::-1][0:top_k] - for index in indexs: - label = label_list[index].split(',')[0] - output_i[label] = float(result_i[index]) - output.append(output_i) - return output diff --git a/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/README.md b/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/README.md deleted file mode 100644 index 9ad3d9f7c9532d9e27b828a4c408c86768aa4ef5..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/README.md +++ /dev/null @@ -1,153 +0,0 @@ -```shell -$ hub install mobilenet_v3_large_imagenet_ssld==1.0.0 -``` - -## 命令行预测 - -``` -hub run mobilenet_v3_large_imagenet_ssld --input_path "/PATH/TO/IMAGE" -``` - -## API - -```python -def get_expected_image_width() -``` - -返回预处理的图片宽度,也就是224。 - -```python -def get_expected_image_height() -``` - -返回预处理的图片高度,也就是224。 - -```python -def get_pretrained_images_mean() -``` - -返回预处理的图片均值,也就是 \[0.485, 0.456, 0.406\]。 - -```python -def get_pretrained_images_std() -``` - -返回预处理的图片标准差,也就是 \[0.229, 0.224, 0.225\]。 - - -```python -def context(trainable=True, pretrained=True) -``` - -**参数** - -* trainable (bool): 计算图的参数是否为可训练的; -* pretrained (bool): 是否加载默认的预训练模型。 - -**返回** - -* inputs (dict): 计算图的输入,key 为 'image', value 为图片的张量; -* outputs (dict): 计算图的输出,key 为 'classification' 和 'feature_map',其相应的值为: - * classification (paddle.fluid.framework.Variable): 分类结果,也就是全连接层的输出; - * feature\_map (paddle.fluid.framework.Variable): 特征匹配,全连接层前面的那个张量。 -* context\_prog(fluid.Program): 计算图,用于迁移学习。 - -```python -def classification(images=None, - paths=None, - batch_size=1, - use_gpu=False, - top_k=1): -``` - -**参数** - -* images (list\[numpy.ndarray\]): 图片数据,每一个图片数据的shape 均为 \[H, W, C\],颜色空间为 BGR; -* paths (list\[str\]): 图片的路径; -* batch\_size (int): batch 的大小; -* use\_gpu (bool): 是否使用 GPU 来预测; -* top\_k (int): 返回预测结果的前 k 个。 - -**返回** - -res (list\[dict\]): 分类结果,列表的每一个元素均为字典,其中 key 为识别动物的类别,value为置信度。 - -```python -def save_inference_model(dirname, - model_filename=None, - params_filename=None, - combined=True) -``` - -将模型保存到指定路径。 - -**参数** - -* dirname: 存在模型的目录名称 -* model_filename: 模型文件名称,默认为\_\_model\_\_ -* params_filename: 参数文件名称,默认为\_\_params\_\_(仅当`combined`为True时生效) -* combined: 是否将参数保存到统一的一个文件中 - -## 代码示例 - -```python -import paddlehub as hub -import cv2 - -classifier = hub.Module(name="mobilenet_v3_large_imagenet_ssld") - -result = classifier.classification(images=[cv2.imread('/PATH/TO/IMAGE')]) -# or -# result = classifier.classification(paths=['/PATH/TO/IMAGE']) -``` - -## 服务部署 - -PaddleHub Serving可以部署一个在线动物识别服务。 - -## 第一步:启动PaddleHub Serving - -运行启动命令: -```shell -$ hub serving start -m mobilenet_v3_large_imagenet_ssld -``` - -这样就完成了一个在线动物识别服务化API的部署,默认端口号为8866。 - -**NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA\_VISIBLE\_DEVICES环境变量,否则不用设置。 - -## 第二步:发送预测请求 - -配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果 - -```python -import requests -import json -import cv2 -import base64 - - -def cv2_to_base64(image): - data = cv2.imencode('.jpg', image)[1] - return base64.b64encode(data.tostring()).decode('utf8') - - -# 发送HTTP请求 -data = {'images':[cv2_to_base64(cv2.imread("/PATH/TO/IMAGE"))]} -headers = {"Content-type": "application/json"} -url = "http://127.0.0.1:8866/predict/mobilenet_v3_large_imagenet_ssld" -r = requests.post(url=url, headers=headers, data=json.dumps(data)) - -# 打印预测结果 -print(r.json()["results"]) -``` - -### 查看代码 - -[PaddleClas](https://github.com/PaddlePaddle/PaddleClas) - -### 依赖 - -paddlepaddle >= 1.6.2 - -paddlehub >= 1.6.0 diff --git a/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/data_feed.py b/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/data_feed.py deleted file mode 100644 index d5ffb5efe9fdfbd143b949892aa44d851e907b41..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/data_feed.py +++ /dev/null @@ -1,84 +0,0 @@ -# coding=utf-8 -import os -import time -from collections import OrderedDict - -import cv2 -import numpy as np -from PIL import Image - -__all__ = ['reader'] - -DATA_DIM = 224 -img_mean = np.array([0.485, 0.456, 0.406]).reshape((3, 1, 1)) -img_std = np.array([0.229, 0.224, 0.225]).reshape((3, 1, 1)) - - -def resize_short(img, target_size): - percent = float(target_size) / min(img.size[0], img.size[1]) - resized_width = int(round(img.size[0] * percent)) - resized_height = int(round(img.size[1] * percent)) - img = img.resize((resized_width, resized_height), Image.LANCZOS) - return img - - -def crop_image(img, target_size, center): - width, height = img.size - size = target_size - if center == True: - w_start = (width - size) / 2 - h_start = (height - size) / 2 - else: - w_start = np.random.randint(0, width - size + 1) - h_start = np.random.randint(0, height - size + 1) - w_end = w_start + size - h_end = h_start + size - img = img.crop((w_start, h_start, w_end, h_end)) - return img - - -def process_image(img): - img = resize_short(img, target_size=256) - img = crop_image(img, target_size=DATA_DIM, center=True) - if img.mode != 'RGB': - img = img.convert('RGB') - img = np.array(img).astype('float32').transpose((2, 0, 1)) / 255 - img -= img_mean - img /= img_std - return img - - -def reader(images=None, paths=None): - """ - Preprocess to yield image. - - Args: - images (list[numpy.ndarray]): images data, shape of each is [H, W, C]. - paths (list[str]): paths to images. - - Yield: - each (collections.OrderedDict): info of original image, preprocessed image. - """ - component = list() - if paths: - for im_path in paths: - each = OrderedDict() - assert os.path.isfile( - im_path), "The {} isn't a valid file path.".format(im_path) - each['org_im_path'] = im_path - each['org_im'] = Image.open(im_path) - each['org_im_width'], each['org_im_height'] = each['org_im'].size - component.append(each) - if images is not None: - assert type(images), "images is a list." - for im in images: - each = OrderedDict() - each['org_im'] = Image.fromarray(im[:, :, ::-1]) - each['org_im_path'] = 'ndarray_time={}'.format( - round(time.time(), 6) * 1e6) - each['org_im_width'], each['org_im_height'] = each['org_im'].size - component.append(each) - - for element in component: - element['image'] = process_image(element['org_im']) - yield element diff --git a/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/label_list.txt b/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/label_list.txt deleted file mode 100644 index a509c007481d301e524e7b3c97561132dbfcc765..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/label_list.txt +++ /dev/null @@ -1,1000 +0,0 @@ -tench, Tinca tinca -goldfish, Carassius auratus -great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias -tiger shark, Galeocerdo cuvieri -hammerhead, hammerhead shark -electric ray, crampfish, numbfish, torpedo -stingray -cock -hen -ostrich, Struthio camelus -brambling, Fringilla montifringilla -goldfinch, Carduelis carduelis -house finch, linnet, Carpodacus mexicanus -junco, snowbird -indigo bunting, indigo finch, indigo bird, Passerina cyanea -robin, American robin, Turdus migratorius -bulbul -jay -magpie -chickadee -water ouzel, dipper -kite -bald eagle, American eagle, Haliaeetus leucocephalus -vulture -great grey owl, great gray owl, Strix nebulosa -European fire salamander, Salamandra salamandra -common newt, Triturus vulgaris -eft -spotted salamander, Ambystoma maculatum -axolotl, mud puppy, Ambystoma mexicanum -bullfrog, Rana catesbeiana -tree frog, tree-frog -tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui -loggerhead, loggerhead turtle, Caretta caretta -leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea -mud turtle -terrapin -box turtle, box tortoise -banded gecko -common iguana, iguana, Iguana iguana -American chameleon, anole, Anolis carolinensis -whiptail, whiptail lizard -agama -frilled lizard, Chlamydosaurus kingi -alligator lizard -Gila monster, Heloderma suspectum -green lizard, Lacerta viridis -African chameleon, Chamaeleo chamaeleon -Komodo dragon, Komodo lizard, dragon lizard, giant lizard, Varanus komodoensis -African crocodile, Nile crocodile, Crocodylus niloticus -American alligator, Alligator mississipiensis -triceratops -thunder snake, worm snake, Carphophis amoenus -ringneck snake, ring-necked snake, ring snake -hognose snake, puff adder, sand viper -green snake, grass snake -king snake, kingsnake -garter snake, grass snake -water snake -vine snake -night snake, Hypsiglena torquata -boa constrictor, Constrictor constrictor -rock python, rock snake, Python sebae -Indian cobra, Naja naja -green mamba -sea snake -horned viper, cerastes, sand viper, horned asp, Cerastes cornutus -diamondback, diamondback rattlesnake, Crotalus adamanteus -sidewinder, horned rattlesnake, Crotalus cerastes -trilobite -harvestman, daddy longlegs, Phalangium opilio -scorpion -black and gold garden spider, Argiope aurantia -barn spider, Araneus cavaticus -garden spider, Aranea diademata -black widow, Latrodectus mactans -tarantula -wolf spider, hunting spider -tick -centipede -black grouse -ptarmigan -ruffed grouse, partridge, Bonasa umbellus -prairie chicken, prairie grouse, prairie fowl -peacock -quail -partridge -African grey, African gray, Psittacus erithacus -macaw -sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita -lorikeet -coucal -bee eater -hornbill -hummingbird -jacamar -toucan -drake -red-breasted merganser, Mergus serrator -goose -black swan, Cygnus atratus -tusker -echidna, spiny anteater, anteater -platypus, duckbill, duckbilled platypus, duck-billed platypus, Ornithorhynchus anatinus -wallaby, brush kangaroo -koala, koala bear, kangaroo bear, native bear, Phascolarctos cinereus -wombat -jellyfish -sea anemone, anemone -brain coral -flatworm, platyhelminth -nematode, nematode worm, roundworm -conch -snail -slug -sea slug, nudibranch -chiton, coat-of-mail shell, sea cradle, polyplacophore -chambered nautilus, pearly nautilus, nautilus -Dungeness crab, Cancer magister -rock crab, Cancer irroratus -fiddler crab -king crab, Alaska crab, Alaskan king crab, Alaska king crab, Paralithodes camtschatica -American lobster, Northern lobster, Maine lobster, Homarus americanus -spiny lobster, langouste, rock lobster, crawfish, crayfish, sea crawfish -crayfish, crawfish, crawdad, crawdaddy -hermit crab -isopod -white stork, Ciconia ciconia -black stork, Ciconia nigra -spoonbill -flamingo -little blue heron, Egretta caerulea -American egret, great white heron, Egretta albus -bittern -crane -limpkin, Aramus pictus -European gallinule, Porphyrio porphyrio -American coot, marsh hen, mud hen, water hen, Fulica americana -bustard -ruddy turnstone, Arenaria interpres -red-backed sandpiper, dunlin, Erolia alpina -redshank, Tringa totanus -dowitcher -oystercatcher, oyster catcher -pelican -king penguin, Aptenodytes patagonica -albatross, mollymawk -grey whale, gray whale, devilfish, Eschrichtius gibbosus, Eschrichtius robustus -killer whale, killer, orca, grampus, sea wolf, Orcinus orca -dugong, Dugong dugon -sea lion -Chihuahua -Japanese spaniel -Maltese dog, Maltese terrier, Maltese -Pekinese, Pekingese, Peke -Shih-Tzu -Blenheim spaniel -papillon -toy terrier -Rhodesian ridgeback -Afghan hound, Afghan -basset, basset hound -beagle -bloodhound, sleuthhound -bluetick -black-and-tan coonhound -Walker hound, Walker foxhound -English foxhound -redbone -borzoi, Russian wolfhound -Irish wolfhound -Italian greyhound -whippet -Ibizan hound, Ibizan Podenco -Norwegian elkhound, elkhound -otterhound, otter hound -Saluki, gazelle hound -Scottish deerhound, deerhound -Weimaraner -Staffordshire bullterrier, Staffordshire bull terrier -American Staffordshire terrier, Staffordshire terrier, American pit bull terrier, pit bull terrier -Bedlington terrier -Border terrier -Kerry blue terrier -Irish terrier -Norfolk terrier -Norwich terrier -Yorkshire terrier -wire-haired fox terrier -Lakeland terrier -Sealyham terrier, Sealyham -Airedale, Airedale terrier -cairn, cairn terrier -Australian terrier -Dandie Dinmont, Dandie Dinmont terrier -Boston bull, Boston terrier -miniature schnauzer -giant schnauzer -standard schnauzer -Scotch terrier, Scottish terrier, Scottie -Tibetan terrier, chrysanthemum dog -silky terrier, Sydney silky -soft-coated wheaten terrier -West Highland white terrier -Lhasa, Lhasa apso -flat-coated retriever -curly-coated retriever -golden retriever -Labrador retriever -Chesapeake Bay retriever -German short-haired pointer -vizsla, Hungarian pointer -English setter -Irish setter, red setter -Gordon setter -Brittany spaniel -clumber, clumber spaniel -English springer, English springer spaniel -Welsh springer spaniel -cocker spaniel, English cocker spaniel, cocker -Sussex spaniel -Irish water spaniel -kuvasz -schipperke -groenendael -malinois -briard -kelpie -komondor -Old English sheepdog, bobtail -Shetland sheepdog, Shetland sheep dog, Shetland -collie -Border collie -Bouvier des Flandres, Bouviers des Flandres -Rottweiler -German shepherd, German shepherd dog, German police dog, alsatian -Doberman, Doberman pinscher -miniature pinscher -Greater Swiss Mountain dog -Bernese mountain dog -Appenzeller -EntleBucher -boxer -bull mastiff -Tibetan mastiff -French bulldog -Great Dane -Saint Bernard, St Bernard -Eskimo dog, husky -malamute, malemute, Alaskan malamute -Siberian husky -dalmatian, coach dog, carriage dog -affenpinscher, monkey pinscher, monkey dog -basenji -pug, pug-dog -Leonberg -Newfoundland, Newfoundland dog -Great Pyrenees -Samoyed, Samoyede -Pomeranian -chow, chow chow -keeshond -Brabancon griffon -Pembroke, Pembroke Welsh corgi -Cardigan, Cardigan Welsh corgi -toy poodle -miniature poodle -standard poodle -Mexican hairless -timber wolf, grey wolf, gray wolf, Canis lupus -white wolf, Arctic wolf, Canis lupus tundrarum -red wolf, maned wolf, Canis rufus, Canis niger -coyote, prairie wolf, brush wolf, Canis latrans -dingo, warrigal, warragal, Canis dingo -dhole, Cuon alpinus -African hunting dog, hyena dog, Cape hunting dog, Lycaon pictus -hyena, hyaena -red fox, Vulpes vulpes -kit fox, Vulpes macrotis -Arctic fox, white fox, Alopex lagopus -grey fox, gray fox, Urocyon cinereoargenteus -tabby, tabby cat -tiger cat -Persian cat -Siamese cat, Siamese -Egyptian cat -cougar, puma, catamount, mountain lion, painter, panther, Felis concolor -lynx, catamount -leopard, Panthera pardus -snow leopard, ounce, Panthera uncia -jaguar, panther, Panthera onca, Felis onca -lion, king of beasts, Panthera leo -tiger, Panthera tigris -cheetah, chetah, Acinonyx jubatus -brown bear, bruin, Ursus arctos -American black bear, black bear, Ursus americanus, Euarctos americanus -ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus -sloth bear, Melursus ursinus, Ursus ursinus -mongoose -meerkat, mierkat -tiger beetle -ladybug, ladybeetle, lady beetle, ladybird, ladybird beetle -ground beetle, carabid beetle -long-horned beetle, longicorn, longicorn beetle -leaf beetle, chrysomelid -dung beetle -rhinoceros beetle -weevil -fly -bee -ant, emmet, pismire -grasshopper, hopper -cricket -walking stick, walkingstick, stick insect -cockroach, roach -mantis, mantid -cicada, cicala -leafhopper -lacewing, lacewing fly -dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk -damselfly -admiral -ringlet, ringlet butterfly -monarch, monarch butterfly, milkweed butterfly, Danaus plexippus -cabbage butterfly -sulphur butterfly, sulfur butterfly -lycaenid, lycaenid butterfly -starfish, sea star -sea urchin -sea cucumber, holothurian -wood rabbit, cottontail, cottontail rabbit -hare -Angora, Angora rabbit -hamster -porcupine, hedgehog -fox squirrel, eastern fox squirrel, Sciurus niger -marmot -beaver -guinea pig, Cavia cobaya -sorrel -zebra -hog, pig, grunter, squealer, Sus scrofa -wild boar, boar, Sus scrofa -warthog -hippopotamus, hippo, river horse, Hippopotamus amphibius -ox -water buffalo, water ox, Asiatic buffalo, Bubalus bubalis -bison -ram, tup -bighorn, bighorn sheep, cimarron, Rocky Mountain bighorn, Rocky Mountain sheep, Ovis canadensis -ibex, Capra ibex -hartebeest -impala, Aepyceros melampus -gazelle -Arabian camel, dromedary, Camelus dromedarius -llama -weasel -mink -polecat, fitch, foulmart, foumart, Mustela putorius -black-footed ferret, ferret, Mustela nigripes -otter -skunk, polecat, wood pussy -badger -armadillo -three-toed sloth, ai, Bradypus tridactylus -orangutan, orang, orangutang, Pongo pygmaeus -gorilla, Gorilla gorilla -chimpanzee, chimp, Pan troglodytes -gibbon, Hylobates lar -siamang, Hylobates syndactylus, Symphalangus syndactylus -guenon, guenon monkey -patas, hussar monkey, Erythrocebus patas -baboon -macaque -langur -colobus, colobus monkey -proboscis monkey, Nasalis larvatus -marmoset -capuchin, ringtail, Cebus capucinus -howler monkey, howler -titi, titi monkey -spider monkey, Ateles geoffroyi -squirrel monkey, Saimiri sciureus -Madagascar cat, ring-tailed lemur, Lemur catta -indri, indris, Indri indri, Indri brevicaudatus -Indian elephant, Elephas maximus -African elephant, Loxodonta africana -lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens -giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca -barracouta, snoek -eel -coho, cohoe, coho salmon, blue jack, silver salmon, Oncorhynchus kisutch -rock beauty, Holocanthus tricolor -anemone fish -sturgeon -gar, garfish, garpike, billfish, Lepisosteus osseus -lionfish -puffer, pufferfish, blowfish, globefish -abacus -abaya -academic gown, academic robe, judge's robe -accordion, piano accordion, squeeze box -acoustic guitar -aircraft carrier, carrier, flattop, attack aircraft carrier -airliner -airship, dirigible -altar -ambulance -amphibian, amphibious vehicle -analog clock -apiary, bee house -apron -ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, dustbin, trash barrel, trash bin -assault rifle, assault gun -backpack, back pack, knapsack, packsack, rucksack, haversack -bakery, bakeshop, bakehouse -balance beam, beam -balloon -ballpoint, ballpoint pen, ballpen, Biro -Band Aid -banjo -bannister, banister, balustrade, balusters, handrail -barbell -barber chair -barbershop -barn -barometer -barrel, cask -barrow, garden cart, lawn cart, wheelbarrow -baseball -basketball -bassinet -bassoon -bathing cap, swimming cap -bath towel -bathtub, bathing tub, bath, tub -beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon -beacon, lighthouse, beacon light, pharos -beaker -bearskin, busby, shako -beer bottle -beer glass -bell cote, bell cot -bib -bicycle-built-for-two, tandem bicycle, tandem -bikini, two-piece -binder, ring-binder -binoculars, field glasses, opera glasses -birdhouse -boathouse -bobsled, bobsleigh, bob -bolo tie, bolo, bola tie, bola -bonnet, poke bonnet -bookcase -bookshop, bookstore, bookstall -bottlecap -bow -bow tie, bow-tie, bowtie -brass, memorial tablet, plaque -brassiere, bra, bandeau -breakwater, groin, groyne, mole, bulwark, seawall, jetty -breastplate, aegis, egis -broom -bucket, pail -buckle -bulletproof vest -bullet train, bullet -butcher shop, meat market -cab, hack, taxi, taxicab -caldron, cauldron -candle, taper, wax light -cannon -canoe -can opener, tin opener -cardigan -car mirror -carousel, carrousel, merry-go-round, roundabout, whirligig -carpenter's kit, tool kit -carton -car wheel -cash machine, cash dispenser, automated teller machine, automatic teller machine, automated teller, automatic teller, ATM -cassette -cassette player -castle -catamaran -CD player -cello, violoncello -cellular telephone, cellular phone, cellphone, cell, mobile phone -chain -chainlink fence -chain mail, ring mail, mail, chain armor, chain armour, ring armor, ring armour -chain saw, chainsaw -chest -chiffonier, commode -chime, bell, gong -china cabinet, china closet -Christmas stocking -church, church building -cinema, movie theater, movie theatre, movie house, picture palace -cleaver, meat cleaver, chopper -cliff dwelling -cloak -clog, geta, patten, sabot -cocktail shaker -coffee mug -coffeepot -coil, spiral, volute, whorl, helix -combination lock -computer keyboard, keypad -confectionery, confectionary, candy store -container ship, containership, container vessel -convertible -corkscrew, bottle screw -cornet, horn, trumpet, trump -cowboy boot -cowboy hat, ten-gallon hat -cradle -crane -crash helmet -crate -crib, cot -Crock Pot -croquet ball -crutch -cuirass -dam, dike, dyke -desk -desktop computer -dial telephone, dial phone -diaper, nappy, napkin -digital clock -digital watch -dining table, board -dishrag, dishcloth -dishwasher, dish washer, dishwashing machine -disk brake, disc brake -dock, dockage, docking facility -dogsled, dog sled, dog sleigh -dome -doormat, welcome mat -drilling platform, offshore rig -drum, membranophone, tympan -drumstick -dumbbell -Dutch oven -electric fan, blower -electric guitar -electric locomotive -entertainment center -envelope -espresso maker -face powder -feather boa, boa -file, file cabinet, filing cabinet -fireboat -fire engine, fire truck -fire screen, fireguard -flagpole, flagstaff -flute, transverse flute -folding chair -football helmet -forklift -fountain -fountain pen -four-poster -freight car -French horn, horn -frying pan, frypan, skillet -fur coat -garbage truck, dustcart -gasmask, respirator, gas helmet -gas pump, gasoline pump, petrol pump, island dispenser -goblet -go-kart -golf ball -golfcart, golf cart -gondola -gong, tam-tam -gown -grand piano, grand -greenhouse, nursery, glasshouse -grille, radiator grille -grocery store, grocery, food market, market -guillotine -hair slide -hair spray -half track -hammer -hamper -hand blower, blow dryer, blow drier, hair dryer, hair drier -hand-held computer, hand-held microcomputer -handkerchief, hankie, hanky, hankey -hard disc, hard disk, fixed disk -harmonica, mouth organ, harp, mouth harp -harp -harvester, reaper -hatchet -holster -home theater, home theatre -honeycomb -hook, claw -hoopskirt, crinoline -horizontal bar, high bar -horse cart, horse-cart -hourglass -iPod -iron, smoothing iron -jack-o'-lantern -jean, blue jean, denim -jeep, landrover -jersey, T-shirt, tee shirt -jigsaw puzzle -jinrikisha, ricksha, rickshaw -joystick -kimono -knee pad -knot -lab coat, laboratory coat -ladle -lampshade, lamp shade -laptop, laptop computer -lawn mower, mower -lens cap, lens cover -letter opener, paper knife, paperknife -library -lifeboat -lighter, light, igniter, ignitor -limousine, limo -liner, ocean liner -lipstick, lip rouge -Loafer -lotion -loudspeaker, speaker, speaker unit, loudspeaker system, speaker system -loupe, jeweler's loupe -lumbermill, sawmill -magnetic compass -mailbag, postbag -mailbox, letter box -maillot -maillot, tank suit -manhole cover -maraca -marimba, xylophone -mask -matchstick -maypole -maze, labyrinth -measuring cup -medicine chest, medicine cabinet -megalith, megalithic structure -microphone, mike -microwave, microwave oven -military uniform -milk can -minibus -miniskirt, mini -minivan -missile -mitten -mixing bowl -mobile home, manufactured home -Model T -modem -monastery -monitor -moped -mortar -mortarboard -mosque -mosquito net -motor scooter, scooter -mountain bike, all-terrain bike, off-roader -mountain tent -mouse, computer mouse -mousetrap -moving van -muzzle -nail -neck brace -necklace -nipple -notebook, notebook computer -obelisk -oboe, hautboy, hautbois -ocarina, sweet potato -odometer, hodometer, mileometer, milometer -oil filter -organ, pipe organ -oscilloscope, scope, cathode-ray oscilloscope, CRO -overskirt -oxcart -oxygen mask -packet -paddle, boat paddle -paddlewheel, paddle wheel -padlock -paintbrush -pajama, pyjama, pj's, jammies -palace -panpipe, pandean pipe, syrinx -paper towel -parachute, chute -parallel bars, bars -park bench -parking meter -passenger car, coach, carriage -patio, terrace -pay-phone, pay-station -pedestal, plinth, footstall -pencil box, pencil case -pencil sharpener -perfume, essence -Petri dish -photocopier -pick, plectrum, plectron -pickelhaube -picket fence, paling -pickup, pickup truck -pier -piggy bank, penny bank -pill bottle -pillow -ping-pong ball -pinwheel -pirate, pirate ship -pitcher, ewer -plane, carpenter's plane, woodworking plane -planetarium -plastic bag -plate rack -plow, plough -plunger, plumber's helper -Polaroid camera, Polaroid Land camera -pole -police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria -poncho -pool table, billiard table, snooker table -pop bottle, soda bottle -pot, flowerpot -potter's wheel -power drill -prayer rug, prayer mat -printer -prison, prison house -projectile, missile -projector -puck, hockey puck -punching bag, punch bag, punching ball, punchball -purse -quill, quill pen -quilt, comforter, comfort, puff -racer, race car, racing car -racket, racquet -radiator -radio, wireless -radio telescope, radio reflector -rain barrel -recreational vehicle, RV, R.V. -reel -reflex camera -refrigerator, icebox -remote control, remote -restaurant, eating house, eating place, eatery -revolver, six-gun, six-shooter -rifle -rocking chair, rocker -rotisserie -rubber eraser, rubber, pencil eraser -rugby ball -rule, ruler -running shoe -safe -safety pin -saltshaker, salt shaker -sandal -sarong -sax, saxophone -scabbard -scale, weighing machine -school bus -schooner -scoreboard -screen, CRT screen -screw -screwdriver -seat belt, seatbelt -sewing machine -shield, buckler -shoe shop, shoe-shop, shoe store -shoji -shopping basket -shopping cart -shovel -shower cap -shower curtain -ski -ski mask -sleeping bag -slide rule, slipstick -sliding door -slot, one-armed bandit -snorkel -snowmobile -snowplow, snowplough -soap dispenser -soccer ball -sock -solar dish, solar collector, solar furnace -sombrero -soup bowl -space bar -space heater -space shuttle -spatula -speedboat -spider web, spider's web -spindle -sports car, sport car -spotlight, spot -stage -steam locomotive -steel arch bridge -steel drum -stethoscope -stole -stone wall -stopwatch, stop watch -stove -strainer -streetcar, tram, tramcar, trolley, trolley car -stretcher -studio couch, day bed -stupa, tope -submarine, pigboat, sub, U-boat -suit, suit of clothes -sundial -sunglass -sunglasses, dark glasses, shades -sunscreen, sunblock, sun blocker -suspension bridge -swab, swob, mop -sweatshirt -swimming trunks, bathing trunks -swing -switch, electric switch, electrical switch -syringe -table lamp -tank, army tank, armored combat vehicle, armoured combat vehicle -tape player -teapot -teddy, teddy bear -television, television system -tennis ball -thatch, thatched roof -theater curtain, theatre curtain -thimble -thresher, thrasher, threshing machine -throne -tile roof -toaster -tobacco shop, tobacconist shop, tobacconist -toilet seat -torch -totem pole -tow truck, tow car, wrecker -toyshop -tractor -trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi -tray -trench coat -tricycle, trike, velocipede -trimaran -tripod -triumphal arch -trolleybus, trolley coach, trackless trolley -trombone -tub, vat -turnstile -typewriter keyboard -umbrella -unicycle, monocycle -upright, upright piano -vacuum, vacuum cleaner -vase -vault -velvet -vending machine -vestment -viaduct -violin, fiddle -volleyball -waffle iron -wall clock -wallet, billfold, notecase, pocketbook -wardrobe, closet, press -warplane, military plane -washbasin, handbasin, washbowl, lavabo, wash-hand basin -washer, automatic washer, washing machine -water bottle -water jug -water tower -whiskey jug -whistle -wig -window screen -window shade -Windsor tie -wine bottle -wing -wok -wooden spoon -wool, woolen, woollen -worm fence, snake fence, snake-rail fence, Virginia fence -wreck -yawl -yurt -web site, website, internet site, site -comic book -crossword puzzle, crossword -street sign -traffic light, traffic signal, stoplight -book jacket, dust cover, dust jacket, dust wrapper -menu -plate -guacamole -consomme -hot pot, hotpot -trifle -ice cream, icecream -ice lolly, lolly, lollipop, popsicle -French loaf -bagel, beigel -pretzel -cheeseburger -hotdog, hot dog, red hot -mashed potato -head cabbage -broccoli -cauliflower -zucchini, courgette -spaghetti squash -acorn squash -butternut squash -cucumber, cuke -artichoke, globe artichoke -bell pepper -cardoon -mushroom -Granny Smith -strawberry -orange -lemon -fig -pineapple, ananas -banana -jackfruit, jak, jack -custard apple -pomegranate -hay -carbonara -chocolate sauce, chocolate syrup -dough -meat loaf, meatloaf -pizza, pizza pie -potpie -burrito -red wine -espresso -cup -eggnog -alp -bubble -cliff, drop, drop-off -coral reef -geyser -lakeside, lakeshore -promontory, headland, head, foreland -sandbar, sand bar -seashore, coast, seacoast, sea-coast -valley, vale -volcano -ballplayer, baseball player -groom, bridegroom -scuba diver -rapeseed -daisy -yellow lady's slipper, yellow lady-slipper, Cypripedium calceolus, Cypripedium parviflorum -corn -acorn -hip, rose hip, rosehip -buckeye, horse chestnut, conker -coral fungus -agaric -gyromitra -stinkhorn, carrion fungus -earthstar -hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa -bolete -ear, spike, capitulum -toilet tissue, toilet paper, bathroom tissue diff --git a/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/mobilenet_v3.py b/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/mobilenet_v3.py deleted file mode 100644 index 78db751386e62dd27485f5b244e808d7c227bcbd..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/mobilenet_v3.py +++ /dev/null @@ -1,311 +0,0 @@ -# copyright (c) 2020 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. - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import paddle.fluid as fluid -from paddle.fluid.initializer import MSRA -from paddle.fluid.param_attr import ParamAttr - -__all__ = [ - 'MobileNetV3', 'MobileNetV3_small_x0_35', 'MobileNetV3_small_x0_5', - 'MobileNetV3_small_x0_75', 'MobileNetV3_small_x1_0', - 'MobileNetV3_small_x1_25', 'MobileNetV3_large_x0_35', - 'MobileNetV3_large_x0_5', 'MobileNetV3_large_x0_75', - 'MobileNetV3_large_x1_0', 'MobileNetV3_large_x1_25' -] - - -class MobileNetV3(): - def __init__(self, scale=1.0, model_name='small'): - self.scale = scale - self.inplanes = 16 - if model_name == "large": - self.cfg = [ - # k, exp, c, se, nl, s, - [3, 16, 16, False, 'relu', 1], - [3, 64, 24, False, 'relu', 2], - [3, 72, 24, False, 'relu', 1], - [5, 72, 40, True, 'relu', 2], - [5, 120, 40, True, 'relu', 1], - [5, 120, 40, True, 'relu', 1], - [3, 240, 80, False, 'hard_swish', 2], - [3, 200, 80, False, 'hard_swish', 1], - [3, 184, 80, False, 'hard_swish', 1], - [3, 184, 80, False, 'hard_swish', 1], - [3, 480, 112, True, 'hard_swish', 1], - [3, 672, 112, True, 'hard_swish', 1], - [5, 672, 160, True, 'hard_swish', 2], - [5, 960, 160, True, 'hard_swish', 1], - [5, 960, 160, True, 'hard_swish', 1], - ] - self.cls_ch_squeeze = 960 - self.cls_ch_expand = 1280 - elif model_name == "small": - self.cfg = [ - # k, exp, c, se, nl, s, - [3, 16, 16, True, 'relu', 2], - [3, 72, 24, False, 'relu', 2], - [3, 88, 24, False, 'relu', 1], - [5, 96, 40, True, 'hard_swish', 2], - [5, 240, 40, True, 'hard_swish', 1], - [5, 240, 40, True, 'hard_swish', 1], - [5, 120, 48, True, 'hard_swish', 1], - [5, 144, 48, True, 'hard_swish', 1], - [5, 288, 96, True, 'hard_swish', 2], - [5, 576, 96, True, 'hard_swish', 1], - [5, 576, 96, True, 'hard_swish', 1], - ] - self.cls_ch_squeeze = 576 - self.cls_ch_expand = 1280 - else: - raise NotImplementedError("mode[" + model_name + - "_model] is not implemented!") - - def net(self, input, class_dim=1000): - scale = self.scale - inplanes = self.inplanes - cfg = self.cfg - cls_ch_squeeze = self.cls_ch_squeeze - cls_ch_expand = self.cls_ch_expand - #conv1 - conv = self.conv_bn_layer( - input, - filter_size=3, - num_filters=self.make_divisible(inplanes * scale), - stride=2, - padding=1, - num_groups=1, - if_act=True, - act='hard_swish', - name='conv1') - i = 0 - inplanes = self.make_divisible(inplanes * scale) - for layer_cfg in cfg: - conv = self.residual_unit( - input=conv, - num_in_filter=inplanes, - num_mid_filter=self.make_divisible(scale * layer_cfg[1]), - num_out_filter=self.make_divisible(scale * layer_cfg[2]), - act=layer_cfg[4], - stride=layer_cfg[5], - filter_size=layer_cfg[0], - use_se=layer_cfg[3], - name='conv' + str(i + 2)) - inplanes = self.make_divisible(scale * layer_cfg[2]) - i += 1 - - conv = self.conv_bn_layer( - input=conv, - filter_size=1, - num_filters=self.make_divisible(scale * cls_ch_squeeze), - stride=1, - padding=0, - num_groups=1, - if_act=True, - act='hard_swish', - name='conv_last') - conv = fluid.layers.pool2d( - input=conv, pool_type='avg', global_pooling=True, use_cudnn=False) - conv = fluid.layers.conv2d( - input=conv, - num_filters=cls_ch_expand, - filter_size=1, - stride=1, - padding=0, - act=None, - param_attr=ParamAttr(name='last_1x1_conv_weights'), - bias_attr=False) - conv = fluid.layers.hard_swish(conv) - drop = fluid.layers.dropout(x=conv, dropout_prob=0.2) - out = fluid.layers.fc( - input=drop, - size=class_dim, - param_attr=ParamAttr(name='fc_weights'), - bias_attr=ParamAttr(name='fc_offset')) - return out, drop - - def conv_bn_layer(self, - input, - filter_size, - num_filters, - stride, - padding, - num_groups=1, - if_act=True, - act=None, - name=None, - use_cudnn=True, - res_last_bn_init=False): - conv = fluid.layers.conv2d( - input=input, - num_filters=num_filters, - filter_size=filter_size, - stride=stride, - padding=padding, - groups=num_groups, - act=None, - use_cudnn=use_cudnn, - param_attr=ParamAttr(name=name + '_weights'), - bias_attr=False) - bn_name = name + '_bn' - bn = fluid.layers.batch_norm( - input=conv, - param_attr=ParamAttr( - name=bn_name + "_scale", - regularizer=fluid.regularizer.L2DecayRegularizer( - regularization_coeff=0.0)), - bias_attr=ParamAttr( - name=bn_name + "_offset", - regularizer=fluid.regularizer.L2DecayRegularizer( - regularization_coeff=0.0)), - moving_mean_name=bn_name + '_mean', - moving_variance_name=bn_name + '_variance') - if if_act: - if act == 'relu': - bn = fluid.layers.relu(bn) - elif act == 'hard_swish': - bn = fluid.layers.hard_swish(bn) - return bn - - def make_divisible(self, v, divisor=8, min_value=None): - if min_value is None: - min_value = divisor - new_v = max(min_value, int(v + divisor / 2) // divisor * divisor) - if new_v < 0.9 * v: - new_v += divisor - return new_v - - def se_block(self, input, num_out_filter, ratio=4, name=None): - num_mid_filter = num_out_filter // ratio - pool = fluid.layers.pool2d( - input=input, pool_type='avg', global_pooling=True, use_cudnn=False) - conv1 = fluid.layers.conv2d( - input=pool, - filter_size=1, - num_filters=num_mid_filter, - act='relu', - param_attr=ParamAttr(name=name + '_1_weights'), - bias_attr=ParamAttr(name=name + '_1_offset')) - conv2 = fluid.layers.conv2d( - input=conv1, - filter_size=1, - num_filters=num_out_filter, - act='hard_sigmoid', - param_attr=ParamAttr(name=name + '_2_weights'), - bias_attr=ParamAttr(name=name + '_2_offset')) - scale = fluid.layers.elementwise_mul(x=input, y=conv2, axis=0) - return scale - - def residual_unit(self, - input, - num_in_filter, - num_mid_filter, - num_out_filter, - stride, - filter_size, - act=None, - use_se=False, - name=None): - - conv0 = self.conv_bn_layer( - input=input, - filter_size=1, - num_filters=num_mid_filter, - stride=1, - padding=0, - if_act=True, - act=act, - name=name + '_expand') - - conv1 = self.conv_bn_layer( - input=conv0, - filter_size=filter_size, - num_filters=num_mid_filter, - stride=stride, - padding=int((filter_size - 1) // 2), - if_act=True, - act=act, - num_groups=num_mid_filter, - use_cudnn=False, - name=name + '_depthwise') - if use_se: - conv1 = self.se_block( - input=conv1, num_out_filter=num_mid_filter, name=name + '_se') - - conv2 = self.conv_bn_layer( - input=conv1, - filter_size=1, - num_filters=num_out_filter, - stride=1, - padding=0, - if_act=False, - name=name + '_linear', - res_last_bn_init=True) - if num_in_filter != num_out_filter or stride != 1: - return conv2 - else: - return fluid.layers.elementwise_add(x=input, y=conv2, act=None) - - -def MobileNetV3_small_x0_35(): - model = MobileNetV3(model_name='small', scale=0.35) - return model - - -def MobileNetV3_small_x0_5(): - model = MobileNetV3(model_name='small', scale=0.5) - return model - - -def MobileNetV3_small_x0_75(): - model = MobileNetV3(model_name='small', scale=0.75) - return model - - -def MobileNetV3_small_x1_0(): - model = MobileNetV3(model_name='small', scale=1.0) - return model - - -def MobileNetV3_small_x1_25(): - model = MobileNetV3(model_name='small', scale=1.25) - return model - - -def MobileNetV3_large_x0_35(): - model = MobileNetV3(model_name='large', scale=0.35) - return model - - -def MobileNetV3_large_x0_5(): - model = MobileNetV3(model_name='large', scale=0.5) - return model - - -def MobileNetV3_large_x0_75(): - model = MobileNetV3(model_name='large', scale=0.75) - return model - - -def MobileNetV3_large_x1_0(): - model = MobileNetV3(model_name='large', scale=1.0) - return model - - -def MobileNetV3_large_x1_25(): - model = MobileNetV3(model_name='large', scale=1.25) - return model diff --git a/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/module.py b/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/module.py index 07dd93a770a13036f4a1fa74d7cdc11de7a8b2d4..7b247cb77837fc45871f2daa023e2fca6a9f5c48 100644 --- a/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/module.py +++ b/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/module.py @@ -1,278 +1,286 @@ -# coding=utf-8 -from __future__ import absolute_import -from __future__ import division - -import ast -import argparse +# copyright (c) 2020 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 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 mobilenet_v3_large_imagenet_ssld.processor import postprocess, base64_to_cv2 -from mobilenet_v3_large_imagenet_ssld.data_feed import reader -from mobilenet_v3_large_imagenet_ssld.mobilenet_v3 import MobileNetV3 - +import paddle +from paddle import ParamAttr +import paddle.nn as nn +import paddle.nn.functional as F +from paddle.nn import Conv2d, BatchNorm, Linear, Dropout +from paddle.nn import AdaptiveAvgPool2d, MaxPool2d, AvgPool2d +from paddle.regularizer import L2Decay +from paddlehub.module.module import moduleinfo +from paddlehub.module.cv_module import ImageClassifierModule -@moduleinfo( - name="mobilenet_v3_large_imagenet_ssld", - type="CV/image_classification", - author="paddlepaddle", - author_email="paddle-dev@baidu.com", - summary= - "Mobilenet_V3_Large is a image classfication model, this module is trained with ImageNet-2012 dataset.", - version="1.0.0") -class MobileNetV3Large(hub.Module): - def _initialize(self): - self.default_pretrained_model_path = os.path.join( - self.directory, "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] - self._set_config() - def get_expected_image_width(self): - return 224 +def make_divisible(v: int, divisor: int = 8, min_value: int = 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) + if new_v < 0.9 * v: + new_v += divisor + return new_v - def get_expected_image_height(self): - return 224 - def get_pretrained_images_mean(self): - im_mean = np.array([0.485, 0.456, 0.406]).reshape(1, 3) - return im_mean +@moduleinfo(name="mobilenet_v3_large_imagenet_ssld", + type="cv/classification", + author="paddlepaddle", + author_email="", + summary="mobilenet_v3_large_imagenet_ssld is a classification model, " + "this module is trained with Imagenet dataset.", + version="1.1.0", + meta=ImageClassifierModule) +class MobileNetV3Large(nn.Layer): + """MobileNetV3Large module.""" + def __init__(self, dropout_prob: float = 0.2, class_dim: int = 1000, load_checkpoint: str = None): + super(MobileNetV3Large, self).__init__() - def get_pretrained_images_std(self): - im_std = np.array([0.229, 0.224, 0.225]).reshape(1, 3) - return im_std + inplanes = 16 + self.cfg = [ + # k, exp, c, se, nl, s, + [3, 16, 16, False, "relu", 1], + [3, 64, 24, False, "relu", 2], + [3, 72, 24, False, "relu", 1], + [5, 72, 40, True, "relu", 2], + [5, 120, 40, True, "relu", 1], + [5, 120, 40, True, "relu", 1], + [3, 240, 80, False, "hard_swish", 2], + [3, 200, 80, False, "hard_swish", 1], + [3, 184, 80, False, "hard_swish", 1], + [3, 184, 80, False, "hard_swish", 1], + [3, 480, 112, True, "hard_swish", 1], + [3, 672, 112, True, "hard_swish", 1], + [5, 672, 160, True, "hard_swish", 2], + [5, 960, 160, True, "hard_swish", 1], + [5, 960, 160, True, "hard_swish", 1] + ] + self.cls_ch_squeeze = 960 + self.cls_ch_expand = 1280 - def _set_config(self): - """ - predictor config setting - """ - cpu_config = AnalysisConfig(self.default_pretrained_model_path) - cpu_config.disable_glog_info() - cpu_config.disable_gpu() - self.cpu_predictor = create_paddle_predictor(cpu_config) + self.conv1 = ConvBNLayer(in_c=3, + out_c=make_divisible(inplanes), + filter_size=3, + stride=2, + padding=1, + num_groups=1, + if_act=True, + act="hard_swish", + name="conv1") - try: - _places = os.environ["CUDA_VISIBLE_DEVICES"] - int(_places[0]) - use_gpu = True - except: - use_gpu = False - if use_gpu: - gpu_config = AnalysisConfig(self.default_pretrained_model_path) - 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) + self.block_list = [] + i = 0 + inplanes = make_divisible(inplanes) + for (k, exp, c, se, nl, s) in self.cfg: + self.block_list.append( + ResidualUnit(in_c=inplanes, + mid_c=make_divisible(exp), + out_c=make_divisible(c), + filter_size=k, + stride=s, + use_se=se, + act=nl, + name="conv" + str(i + 2))) + self.add_sublayer(sublayer=self.block_list[-1], name="conv" + str(i + 2)) + inplanes = make_divisible(c) + i += 1 - def context(self, trainable=True, pretrained=True): - """context for transfer learning. + self.last_second_conv = ConvBNLayer(in_c=inplanes, + out_c=make_divisible(self.cls_ch_squeeze), + filter_size=1, + stride=1, + padding=0, + num_groups=1, + if_act=True, + act="hard_swish", + name="conv_last") - Args: - trainable (bool): Set parameters in program to be trainable. - pretrained (bool) : Whether to load pretrained model. + self.pool = AdaptiveAvgPool2d(1) - 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") - mobile_net = MobileNetV3(model_name='large') - output, feature_map = mobile_net.net( - input=image, class_dim=len(self.label_list)) + self.last_conv = Conv2d(in_channels=make_divisible(self.cls_ch_squeeze), + out_channels=self.cls_ch_expand, + kernel_size=1, + stride=1, + padding=0, + weight_attr=ParamAttr(name="last_1x1_conv_weights"), + bias_attr=False) - 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() - } + self.dropout = Dropout(p=dropout_prob, mode="downscale_in_infer") - place = fluid.CPUPlace() - exe = fluid.Executor(place) - # pretrained - if pretrained: + self.out = Linear(self.cls_ch_expand, + class_dim, + weight_attr=ParamAttr("fc_weights"), + bias_attr=ParamAttr(name="fc_offset")) - def _if_exist(var): - b = os.path.exists( - os.path.join(self.default_pretrained_model_path, - var.name)) - return b + if load_checkpoint is not None: + model_dict = paddle.load(load_checkpoint)[0] + self.set_dict(model_dict) + print("load custom checkpoint success") - 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 + else: + checkpoint = os.path.join(self.directory, 'mobilenet_v3_large_ssld.pdparams') + if not os.path.exists(checkpoint): + os.system( + 'wget https://paddlehub.bj.bcebos.com/dygraph/image_classification/mobilenet_v3_large_ssld.pdparams -O ' + + checkpoint) + model_dict = paddle.load(checkpoint)[0] + self.set_dict(model_dict) + print("load pretrained checkpoint success") - def classification(self, - images=None, - paths=None, - batch_size=1, - use_gpu=False, - top_k=1): - """ - API for image classification. + def forward(self, inputs: paddle.Tensor): + x = self.conv1(inputs) + for block in self.block_list: + x = block(x) - Args: - images (numpy.ndarray): data of images, shape of each is [H, W, C], color space must be BGR. - paths (list[str]): The paths of images. - batch_size (int): batch size. - use_gpu (bool): Whether to use gpu. - top_k (int): Return top k results. + x = self.last_second_conv(x) + x = self.pool(x) - 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." - ) + x = self.last_conv(x) + x = F.hard_swish(x) + x = self.dropout(x) + x = paddle.reshape(x, shape=[x.shape[0], x.shape[1]]) + x = self.out(x) + return x - all_data = list() - for yield_data in reader(images, paths): - all_data.append(yield_data) - total_num = len(all_data) - loop_num = int(np.ceil(total_num / batch_size)) +class ConvBNLayer(nn.Layer): + """Basic conv bn layer.""" + def __init__(self, + in_c: int, + out_c: int, + filter_size: int, + stride: int, + padding: int, + num_groups: int = 1, + if_act: bool = True, + act: str = None, + name: str = ""): + super(ConvBNLayer, self).__init__() + self.if_act = if_act + self.act = act + self.conv = Conv2d(in_channels=in_c, + out_channels=out_c, + kernel_size=filter_size, + stride=stride, + padding=padding, + groups=num_groups, + weight_attr=ParamAttr(name=name + "_weights"), + bias_attr=False) + self.bn = BatchNorm(num_channels=out_c, + act=None, + param_attr=ParamAttr(name=name + "_bn_scale", regularizer=L2Decay(0.0)), + bias_attr=ParamAttr(name=name + "_bn_offset", regularizer=L2Decay(0.0)), + moving_mean_name=name + "_bn_mean", + moving_variance_name=name + "_bn_variance") - res = list() - for iter_id in range(loop_num): - batch_data = list() - handle_id = iter_id * batch_size - for image_id in range(batch_size): - try: - batch_data.append(all_data[handle_id + image_id]) - except: - 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) - res += out - return res + def forward(self, x: paddle.Tensor): + x = self.conv(x) + x = self.bn(x) + if self.if_act: + if self.act == "relu": + x = F.relu(x) + elif self.act == "hard_swish": + x = F.hard_swish(x) + else: + print("The activation function is selected incorrectly.") + exit() + return x - 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) +class ResidualUnit(nn.Layer): + """Residual unit for MobileNetV3.""" + def __init__(self, + in_c: int, + mid_c: int, + out_c: int, + filter_size: int, + stride: int, + use_se: int, + act: str = None, + name: str = ''): + super(ResidualUnit, self).__init__() + self.if_shortcut = stride == 1 and in_c == out_c + self.if_se = use_se - 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) + self.expand_conv = ConvBNLayer(in_c=in_c, + out_c=mid_c, + filter_size=1, + stride=1, + padding=0, + if_act=True, + act=act, + name=name + "_expand") + self.bottleneck_conv = ConvBNLayer(in_c=mid_c, + out_c=mid_c, + filter_size=filter_size, + stride=stride, + padding=int((filter_size - 1) // 2), + num_groups=mid_c, + if_act=True, + act=act, + name=name + "_depthwise") + if self.if_se: + self.mid_se = SEModule(mid_c, name=name + "_se") + self.linear_conv = ConvBNLayer(in_c=mid_c, + out_c=out_c, + filter_size=1, + stride=1, + padding=0, + if_act=False, + act=None, + name=name + "_linear") - @serving - def serving_method(self, images, **kwargs): - """ - Run as a service. - """ - images_decode = [base64_to_cv2(image) for image in images] - results = self.classification(images=images_decode, **kwargs) - return results + def forward(self, inputs: paddle.Tensor): + x = self.expand_conv(inputs) + x = self.bottleneck_conv(x) + if self.if_se: + x = self.mid_se(x) + x = self.linear_conv(x) + if self.if_shortcut: + x = paddle.elementwise_add(inputs, x) + return x - @runnable - 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.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.") - self.add_module_config_arg() - self.add_module_input_arg() - args = self.parser.parse_args(argvs) - results = self.classification( - paths=[args.input_path], - batch_size=args.batch_size, - use_gpu=args.use_gpu) - return results - 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( - '--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.") +class SEModule(nn.Layer): + """Basic model for ResidualUnit.""" + def __init__(self, channel: int, reduction: int = 4, name: str = ""): + super(SEModule, self).__init__() + self.avg_pool = AdaptiveAvgPool2d(1) + self.conv1 = Conv2d(in_channels=channel, + out_channels=channel // reduction, + kernel_size=1, + stride=1, + padding=0, + weight_attr=ParamAttr(name=name + "_1_weights"), + bias_attr=ParamAttr(name=name + "_1_offset")) + self.conv2 = Conv2d(in_channels=channel // reduction, + out_channels=channel, + kernel_size=1, + stride=1, + padding=0, + weight_attr=ParamAttr(name + "_2_weights"), + bias_attr=ParamAttr(name=name + "_2_offset")) - def add_module_input_arg(self): - """ - Add the command input options. - """ - self.arg_input_group.add_argument( - '--input_path', type=str, help="path to image.") + def forward(self, inputs: paddle.Tensor): + outputs = self.avg_pool(inputs) + outputs = self.conv1(outputs) + outputs = F.relu(outputs) + outputs = self.conv2(outputs) + outputs = F.hard_sigmoid(outputs) + return paddle.multiply(x=inputs, y=outputs, axis=0) diff --git a/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/processor.py b/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/processor.py deleted file mode 100644 index fa8cbb502312e6ef80697ab63b767d4077b3847b..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/mobilenet_v3_large_imagenet_ssld/processor.py +++ /dev/null @@ -1,55 +0,0 @@ -# coding=utf-8 -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import base64 -import cv2 -import os - -import numpy as np - - -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 - - -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)) - x = np.exp(x) - tmp = np.sum(x, axis=1) - x /= tmp.reshape((x.shape[0], 1)) - else: - tmp = np.max(x) - x -= tmp - x = np.exp(x) - tmp = np.sum(x) - x /= tmp - return x - - -def postprocess(data_out, label_list, top_k): - """ - Postprocess output of network, one image at a time. - - Args: - data_out (numpy.ndarray): output data of network. - label_list (list): list of label. - top_k (int): Return top k results. - """ - output = [] - for result in data_out: - result_i = softmax(result) - output_i = {} - indexs = np.argsort(result_i)[::-1][0:top_k] - for index in indexs: - label = label_list[index].split(',')[0] - output_i[label] = float(result_i[index]) - output.append(output_i) - return output diff --git a/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/README.md b/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/README.md deleted file mode 100644 index 2aff28d00170a7a166c2f99f2016111d53c707a8..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/README.md +++ /dev/null @@ -1,154 +0,0 @@ -```shell -$ hub install mobilenet_v2_animals==1.0.0 -``` - - -## 命令行预测 - -``` -hub run mobilenet_v2_animals --input_path "/PATH/TO/IMAGE" -``` - -## API - -```python -def get_expected_image_width() -``` - -返回预处理的图片宽度,也就是224。 - -```python -def get_expected_image_height() -``` - -返回预处理的图片高度,也就是224。 - -```python -def get_pretrained_images_mean() -``` - -返回预处理的图片均值,也就是 \[0.485, 0.456, 0.406\]。 - -```python -def get_pretrained_images_std() -``` - -返回预处理的图片标准差,也就是 \[0.229, 0.224, 0.225\]。 - - -```python -def context(trainable=True, pretrained=True) -``` - -**参数** - -* trainable (bool): 计算图的参数是否为可训练的; -* pretrained (bool): 是否加载默认的预训练模型。 - -**返回** - -* inputs (dict): 计算图的输入,key 为 'image', value 为图片的张量; -* outputs (dict): 计算图的输出,key 为 'classification' 和 'feature_map',其相应的值为: - * classification (paddle.fluid.framework.Variable): 分类结果,也就是全连接层的输出; - * feature\_map (paddle.fluid.framework.Variable): 特征匹配,全连接层前面的那个张量。 -* context\_prog(fluid.Program): 计算图,用于迁移学习。 - -```python -def classification(images=None, - paths=None, - batch_size=1, - use_gpu=False, - top_k=1): -``` - -**参数** - -* images (list\[numpy.ndarray\]): 图片数据,每一个图片数据的shape 均为 \[H, W, C\],颜色空间为 BGR; -* paths (list\[str\]): 图片的路径; -* batch\_size (int): batch 的大小; -* use\_gpu (bool): 是否使用 GPU 来预测; -* top\_k (int): 返回预测结果的前 k 个。 - -**返回** - -res (list\[dict\]): 分类结果,列表的每一个元素均为字典,其中 key 为识别动物的类别,value为置信度。 - -```python -def save_inference_model(dirname, - model_filename=None, - params_filename=None, - combined=True) -``` - -将模型保存到指定路径。 - -**参数** - -* dirname: 存在模型的目录名称 -* model_filename: 模型文件名称,默认为\_\_model\_\_ -* params_filename: 参数文件名称,默认为\_\_params\_\_(仅当`combined`为True时生效) -* combined: 是否将参数保存到统一的一个文件中 - -## 代码示例 - -```python -import paddlehub as hub -import cv2 - -classifier = hub.Module(name="mobilenet_v2_animals") - -result = classifier.classification(images=[cv2.imread('/PATH/TO/IMAGE')]) -# or -# result = classifier.classification(paths=['/PATH/TO/IMAGE']) -``` - -## 服务部署 - -PaddleHub Serving可以部署一个在线动物识别服务。 - -## 第一步:启动PaddleHub Serving - -运行启动命令: -```shell -$ hub serving start -m mobilenet_v2_animals -``` - -这样就完成了一个在线动物识别服务化API的部署,默认端口号为8866。 - -**NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA\_VISIBLE\_DEVICES环境变量,否则不用设置。 - -## 第二步:发送预测请求 - -配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果 - -```python -import requests -import json -import cv2 -import base64 - - -def cv2_to_base64(image): - data = cv2.imencode('.jpg', image)[1] - return base64.b64encode(data.tostring()).decode('utf8') - - -# 发送HTTP请求 -data = {'images':[cv2_to_base64(cv2.imread("/PATH/TO/IMAGE"))]} -headers = {"Content-type": "application/json"} -url = "http://127.0.0.1:8866/predict/mobilenet_v2_animals" -r = requests.post(url=url, headers=headers, data=json.dumps(data)) - -# 打印预测结果 -print(r.json()["results"]) -``` - -### 查看代码 - -[PaddleClas](https://github.com/PaddlePaddle/PaddleClas) - -### 依赖 - -paddlepaddle >= 1.6.2 - -paddlehub >= 1.6.0 diff --git a/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/data_feed.py b/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/data_feed.py deleted file mode 100644 index d5ffb5efe9fdfbd143b949892aa44d851e907b41..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/data_feed.py +++ /dev/null @@ -1,84 +0,0 @@ -# coding=utf-8 -import os -import time -from collections import OrderedDict - -import cv2 -import numpy as np -from PIL import Image - -__all__ = ['reader'] - -DATA_DIM = 224 -img_mean = np.array([0.485, 0.456, 0.406]).reshape((3, 1, 1)) -img_std = np.array([0.229, 0.224, 0.225]).reshape((3, 1, 1)) - - -def resize_short(img, target_size): - percent = float(target_size) / min(img.size[0], img.size[1]) - resized_width = int(round(img.size[0] * percent)) - resized_height = int(round(img.size[1] * percent)) - img = img.resize((resized_width, resized_height), Image.LANCZOS) - return img - - -def crop_image(img, target_size, center): - width, height = img.size - size = target_size - if center == True: - w_start = (width - size) / 2 - h_start = (height - size) / 2 - else: - w_start = np.random.randint(0, width - size + 1) - h_start = np.random.randint(0, height - size + 1) - w_end = w_start + size - h_end = h_start + size - img = img.crop((w_start, h_start, w_end, h_end)) - return img - - -def process_image(img): - img = resize_short(img, target_size=256) - img = crop_image(img, target_size=DATA_DIM, center=True) - if img.mode != 'RGB': - img = img.convert('RGB') - img = np.array(img).astype('float32').transpose((2, 0, 1)) / 255 - img -= img_mean - img /= img_std - return img - - -def reader(images=None, paths=None): - """ - Preprocess to yield image. - - Args: - images (list[numpy.ndarray]): images data, shape of each is [H, W, C]. - paths (list[str]): paths to images. - - Yield: - each (collections.OrderedDict): info of original image, preprocessed image. - """ - component = list() - if paths: - for im_path in paths: - each = OrderedDict() - assert os.path.isfile( - im_path), "The {} isn't a valid file path.".format(im_path) - each['org_im_path'] = im_path - each['org_im'] = Image.open(im_path) - each['org_im_width'], each['org_im_height'] = each['org_im'].size - component.append(each) - if images is not None: - assert type(images), "images is a list." - for im in images: - each = OrderedDict() - each['org_im'] = Image.fromarray(im[:, :, ::-1]) - each['org_im_path'] = 'ndarray_time={}'.format( - round(time.time(), 6) * 1e6) - each['org_im_width'], each['org_im_height'] = each['org_im'].size - component.append(each) - - for element in component: - element['image'] = process_image(element['org_im']) - yield element diff --git a/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/label_list.txt b/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/label_list.txt deleted file mode 100644 index a509c007481d301e524e7b3c97561132dbfcc765..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/label_list.txt +++ /dev/null @@ -1,1000 +0,0 @@ -tench, Tinca tinca -goldfish, Carassius auratus -great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias -tiger shark, Galeocerdo cuvieri -hammerhead, hammerhead shark -electric ray, crampfish, numbfish, torpedo -stingray -cock -hen -ostrich, Struthio camelus -brambling, Fringilla montifringilla -goldfinch, Carduelis carduelis -house finch, linnet, Carpodacus mexicanus -junco, snowbird -indigo bunting, indigo finch, indigo bird, Passerina cyanea -robin, American robin, Turdus migratorius -bulbul -jay -magpie -chickadee -water ouzel, dipper -kite -bald eagle, American eagle, Haliaeetus leucocephalus -vulture -great grey owl, great gray owl, Strix nebulosa -European fire salamander, Salamandra salamandra -common newt, Triturus vulgaris -eft -spotted salamander, Ambystoma maculatum -axolotl, mud puppy, Ambystoma mexicanum -bullfrog, Rana catesbeiana -tree frog, tree-frog -tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui -loggerhead, loggerhead turtle, Caretta caretta -leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea -mud turtle -terrapin -box turtle, box tortoise -banded gecko -common iguana, iguana, Iguana iguana -American chameleon, anole, Anolis carolinensis -whiptail, whiptail lizard -agama -frilled lizard, Chlamydosaurus kingi -alligator lizard -Gila monster, Heloderma suspectum -green lizard, Lacerta viridis -African chameleon, Chamaeleo chamaeleon -Komodo dragon, Komodo lizard, dragon lizard, giant lizard, Varanus komodoensis -African crocodile, Nile crocodile, Crocodylus niloticus -American alligator, Alligator mississipiensis -triceratops -thunder snake, worm snake, Carphophis amoenus -ringneck snake, ring-necked snake, ring snake -hognose snake, puff adder, sand viper -green snake, grass snake -king snake, kingsnake -garter snake, grass snake -water snake -vine snake -night snake, Hypsiglena torquata -boa constrictor, Constrictor constrictor -rock python, rock snake, Python sebae -Indian cobra, Naja naja -green mamba -sea snake -horned viper, cerastes, sand viper, horned asp, Cerastes cornutus -diamondback, diamondback rattlesnake, Crotalus adamanteus -sidewinder, horned rattlesnake, Crotalus cerastes -trilobite -harvestman, daddy longlegs, Phalangium opilio -scorpion -black and gold garden spider, Argiope aurantia -barn spider, Araneus cavaticus -garden spider, Aranea diademata -black widow, Latrodectus mactans -tarantula -wolf spider, hunting spider -tick -centipede -black grouse -ptarmigan -ruffed grouse, partridge, Bonasa umbellus -prairie chicken, prairie grouse, prairie fowl -peacock -quail -partridge -African grey, African gray, Psittacus erithacus -macaw -sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita -lorikeet -coucal -bee eater -hornbill -hummingbird -jacamar -toucan -drake -red-breasted merganser, Mergus serrator -goose -black swan, Cygnus atratus -tusker -echidna, spiny anteater, anteater -platypus, duckbill, duckbilled platypus, duck-billed platypus, Ornithorhynchus anatinus -wallaby, brush kangaroo -koala, koala bear, kangaroo bear, native bear, Phascolarctos cinereus -wombat -jellyfish -sea anemone, anemone -brain coral -flatworm, platyhelminth -nematode, nematode worm, roundworm -conch -snail -slug -sea slug, nudibranch -chiton, coat-of-mail shell, sea cradle, polyplacophore -chambered nautilus, pearly nautilus, nautilus -Dungeness crab, Cancer magister -rock crab, Cancer irroratus -fiddler crab -king crab, Alaska crab, Alaskan king crab, Alaska king crab, Paralithodes camtschatica -American lobster, Northern lobster, Maine lobster, Homarus americanus -spiny lobster, langouste, rock lobster, crawfish, crayfish, sea crawfish -crayfish, crawfish, crawdad, crawdaddy -hermit crab -isopod -white stork, Ciconia ciconia -black stork, Ciconia nigra -spoonbill -flamingo -little blue heron, Egretta caerulea -American egret, great white heron, Egretta albus -bittern -crane -limpkin, Aramus pictus -European gallinule, Porphyrio porphyrio -American coot, marsh hen, mud hen, water hen, Fulica americana -bustard -ruddy turnstone, Arenaria interpres -red-backed sandpiper, dunlin, Erolia alpina -redshank, Tringa totanus -dowitcher -oystercatcher, oyster catcher -pelican -king penguin, Aptenodytes patagonica -albatross, mollymawk -grey whale, gray whale, devilfish, Eschrichtius gibbosus, Eschrichtius robustus -killer whale, killer, orca, grampus, sea wolf, Orcinus orca -dugong, Dugong dugon -sea lion -Chihuahua -Japanese spaniel -Maltese dog, Maltese terrier, Maltese -Pekinese, Pekingese, Peke -Shih-Tzu -Blenheim spaniel -papillon -toy terrier -Rhodesian ridgeback -Afghan hound, Afghan -basset, basset hound -beagle -bloodhound, sleuthhound -bluetick -black-and-tan coonhound -Walker hound, Walker foxhound -English foxhound -redbone -borzoi, Russian wolfhound -Irish wolfhound -Italian greyhound -whippet -Ibizan hound, Ibizan Podenco -Norwegian elkhound, elkhound -otterhound, otter hound -Saluki, gazelle hound -Scottish deerhound, deerhound -Weimaraner -Staffordshire bullterrier, Staffordshire bull terrier -American Staffordshire terrier, Staffordshire terrier, American pit bull terrier, pit bull terrier -Bedlington terrier -Border terrier -Kerry blue terrier -Irish terrier -Norfolk terrier -Norwich terrier -Yorkshire terrier -wire-haired fox terrier -Lakeland terrier -Sealyham terrier, Sealyham -Airedale, Airedale terrier -cairn, cairn terrier -Australian terrier -Dandie Dinmont, Dandie Dinmont terrier -Boston bull, Boston terrier -miniature schnauzer -giant schnauzer -standard schnauzer -Scotch terrier, Scottish terrier, Scottie -Tibetan terrier, chrysanthemum dog -silky terrier, Sydney silky -soft-coated wheaten terrier -West Highland white terrier -Lhasa, Lhasa apso -flat-coated retriever -curly-coated retriever -golden retriever -Labrador retriever -Chesapeake Bay retriever -German short-haired pointer -vizsla, Hungarian pointer -English setter -Irish setter, red setter -Gordon setter -Brittany spaniel -clumber, clumber spaniel -English springer, English springer spaniel -Welsh springer spaniel -cocker spaniel, English cocker spaniel, cocker -Sussex spaniel -Irish water spaniel -kuvasz -schipperke -groenendael -malinois -briard -kelpie -komondor -Old English sheepdog, bobtail -Shetland sheepdog, Shetland sheep dog, Shetland -collie -Border collie -Bouvier des Flandres, Bouviers des Flandres -Rottweiler -German shepherd, German shepherd dog, German police dog, alsatian -Doberman, Doberman pinscher -miniature pinscher -Greater Swiss Mountain dog -Bernese mountain dog -Appenzeller -EntleBucher -boxer -bull mastiff -Tibetan mastiff -French bulldog -Great Dane -Saint Bernard, St Bernard -Eskimo dog, husky -malamute, malemute, Alaskan malamute -Siberian husky -dalmatian, coach dog, carriage dog -affenpinscher, monkey pinscher, monkey dog -basenji -pug, pug-dog -Leonberg -Newfoundland, Newfoundland dog -Great Pyrenees -Samoyed, Samoyede -Pomeranian -chow, chow chow -keeshond -Brabancon griffon -Pembroke, Pembroke Welsh corgi -Cardigan, Cardigan Welsh corgi -toy poodle -miniature poodle -standard poodle -Mexican hairless -timber wolf, grey wolf, gray wolf, Canis lupus -white wolf, Arctic wolf, Canis lupus tundrarum -red wolf, maned wolf, Canis rufus, Canis niger -coyote, prairie wolf, brush wolf, Canis latrans -dingo, warrigal, warragal, Canis dingo -dhole, Cuon alpinus -African hunting dog, hyena dog, Cape hunting dog, Lycaon pictus -hyena, hyaena -red fox, Vulpes vulpes -kit fox, Vulpes macrotis -Arctic fox, white fox, Alopex lagopus -grey fox, gray fox, Urocyon cinereoargenteus -tabby, tabby cat -tiger cat -Persian cat -Siamese cat, Siamese -Egyptian cat -cougar, puma, catamount, mountain lion, painter, panther, Felis concolor -lynx, catamount -leopard, Panthera pardus -snow leopard, ounce, Panthera uncia -jaguar, panther, Panthera onca, Felis onca -lion, king of beasts, Panthera leo -tiger, Panthera tigris -cheetah, chetah, Acinonyx jubatus -brown bear, bruin, Ursus arctos -American black bear, black bear, Ursus americanus, Euarctos americanus -ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus -sloth bear, Melursus ursinus, Ursus ursinus -mongoose -meerkat, mierkat -tiger beetle -ladybug, ladybeetle, lady beetle, ladybird, ladybird beetle -ground beetle, carabid beetle -long-horned beetle, longicorn, longicorn beetle -leaf beetle, chrysomelid -dung beetle -rhinoceros beetle -weevil -fly -bee -ant, emmet, pismire -grasshopper, hopper -cricket -walking stick, walkingstick, stick insect -cockroach, roach -mantis, mantid -cicada, cicala -leafhopper -lacewing, lacewing fly -dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk -damselfly -admiral -ringlet, ringlet butterfly -monarch, monarch butterfly, milkweed butterfly, Danaus plexippus -cabbage butterfly -sulphur butterfly, sulfur butterfly -lycaenid, lycaenid butterfly -starfish, sea star -sea urchin -sea cucumber, holothurian -wood rabbit, cottontail, cottontail rabbit -hare -Angora, Angora rabbit -hamster -porcupine, hedgehog -fox squirrel, eastern fox squirrel, Sciurus niger -marmot -beaver -guinea pig, Cavia cobaya -sorrel -zebra -hog, pig, grunter, squealer, Sus scrofa -wild boar, boar, Sus scrofa -warthog -hippopotamus, hippo, river horse, Hippopotamus amphibius -ox -water buffalo, water ox, Asiatic buffalo, Bubalus bubalis -bison -ram, tup -bighorn, bighorn sheep, cimarron, Rocky Mountain bighorn, Rocky Mountain sheep, Ovis canadensis -ibex, Capra ibex -hartebeest -impala, Aepyceros melampus -gazelle -Arabian camel, dromedary, Camelus dromedarius -llama -weasel -mink -polecat, fitch, foulmart, foumart, Mustela putorius -black-footed ferret, ferret, Mustela nigripes -otter -skunk, polecat, wood pussy -badger -armadillo -three-toed sloth, ai, Bradypus tridactylus -orangutan, orang, orangutang, Pongo pygmaeus -gorilla, Gorilla gorilla -chimpanzee, chimp, Pan troglodytes -gibbon, Hylobates lar -siamang, Hylobates syndactylus, Symphalangus syndactylus -guenon, guenon monkey -patas, hussar monkey, Erythrocebus patas -baboon -macaque -langur -colobus, colobus monkey -proboscis monkey, Nasalis larvatus -marmoset -capuchin, ringtail, Cebus capucinus -howler monkey, howler -titi, titi monkey -spider monkey, Ateles geoffroyi -squirrel monkey, Saimiri sciureus -Madagascar cat, ring-tailed lemur, Lemur catta -indri, indris, Indri indri, Indri brevicaudatus -Indian elephant, Elephas maximus -African elephant, Loxodonta africana -lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens -giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca -barracouta, snoek -eel -coho, cohoe, coho salmon, blue jack, silver salmon, Oncorhynchus kisutch -rock beauty, Holocanthus tricolor -anemone fish -sturgeon -gar, garfish, garpike, billfish, Lepisosteus osseus -lionfish -puffer, pufferfish, blowfish, globefish -abacus -abaya -academic gown, academic robe, judge's robe -accordion, piano accordion, squeeze box -acoustic guitar -aircraft carrier, carrier, flattop, attack aircraft carrier -airliner -airship, dirigible -altar -ambulance -amphibian, amphibious vehicle -analog clock -apiary, bee house -apron -ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, dustbin, trash barrel, trash bin -assault rifle, assault gun -backpack, back pack, knapsack, packsack, rucksack, haversack -bakery, bakeshop, bakehouse -balance beam, beam -balloon -ballpoint, ballpoint pen, ballpen, Biro -Band Aid -banjo -bannister, banister, balustrade, balusters, handrail -barbell -barber chair -barbershop -barn -barometer -barrel, cask -barrow, garden cart, lawn cart, wheelbarrow -baseball -basketball -bassinet -bassoon -bathing cap, swimming cap -bath towel -bathtub, bathing tub, bath, tub -beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon -beacon, lighthouse, beacon light, pharos -beaker -bearskin, busby, shako -beer bottle -beer glass -bell cote, bell cot -bib -bicycle-built-for-two, tandem bicycle, tandem -bikini, two-piece -binder, ring-binder -binoculars, field glasses, opera glasses -birdhouse -boathouse -bobsled, bobsleigh, bob -bolo tie, bolo, bola tie, bola -bonnet, poke bonnet -bookcase -bookshop, bookstore, bookstall -bottlecap -bow -bow tie, bow-tie, bowtie -brass, memorial tablet, plaque -brassiere, bra, bandeau -breakwater, groin, groyne, mole, bulwark, seawall, jetty -breastplate, aegis, egis -broom -bucket, pail -buckle -bulletproof vest -bullet train, bullet -butcher shop, meat market -cab, hack, taxi, taxicab -caldron, cauldron -candle, taper, wax light -cannon -canoe -can opener, tin opener -cardigan -car mirror -carousel, carrousel, merry-go-round, roundabout, whirligig -carpenter's kit, tool kit -carton -car wheel -cash machine, cash dispenser, automated teller machine, automatic teller machine, automated teller, automatic teller, ATM -cassette -cassette player -castle -catamaran -CD player -cello, violoncello -cellular telephone, cellular phone, cellphone, cell, mobile phone -chain -chainlink fence -chain mail, ring mail, mail, chain armor, chain armour, ring armor, ring armour -chain saw, chainsaw -chest -chiffonier, commode -chime, bell, gong -china cabinet, china closet -Christmas stocking -church, church building -cinema, movie theater, movie theatre, movie house, picture palace -cleaver, meat cleaver, chopper -cliff dwelling -cloak -clog, geta, patten, sabot -cocktail shaker -coffee mug -coffeepot -coil, spiral, volute, whorl, helix -combination lock -computer keyboard, keypad -confectionery, confectionary, candy store -container ship, containership, container vessel -convertible -corkscrew, bottle screw -cornet, horn, trumpet, trump -cowboy boot -cowboy hat, ten-gallon hat -cradle -crane -crash helmet -crate -crib, cot -Crock Pot -croquet ball -crutch -cuirass -dam, dike, dyke -desk -desktop computer -dial telephone, dial phone -diaper, nappy, napkin -digital clock -digital watch -dining table, board -dishrag, dishcloth -dishwasher, dish washer, dishwashing machine -disk brake, disc brake -dock, dockage, docking facility -dogsled, dog sled, dog sleigh -dome -doormat, welcome mat -drilling platform, offshore rig -drum, membranophone, tympan -drumstick -dumbbell -Dutch oven -electric fan, blower -electric guitar -electric locomotive -entertainment center -envelope -espresso maker -face powder -feather boa, boa -file, file cabinet, filing cabinet -fireboat -fire engine, fire truck -fire screen, fireguard -flagpole, flagstaff -flute, transverse flute -folding chair -football helmet -forklift -fountain -fountain pen -four-poster -freight car -French horn, horn -frying pan, frypan, skillet -fur coat -garbage truck, dustcart -gasmask, respirator, gas helmet -gas pump, gasoline pump, petrol pump, island dispenser -goblet -go-kart -golf ball -golfcart, golf cart -gondola -gong, tam-tam -gown -grand piano, grand -greenhouse, nursery, glasshouse -grille, radiator grille -grocery store, grocery, food market, market -guillotine -hair slide -hair spray -half track -hammer -hamper -hand blower, blow dryer, blow drier, hair dryer, hair drier -hand-held computer, hand-held microcomputer -handkerchief, hankie, hanky, hankey -hard disc, hard disk, fixed disk -harmonica, mouth organ, harp, mouth harp -harp -harvester, reaper -hatchet -holster -home theater, home theatre -honeycomb -hook, claw -hoopskirt, crinoline -horizontal bar, high bar -horse cart, horse-cart -hourglass -iPod -iron, smoothing iron -jack-o'-lantern -jean, blue jean, denim -jeep, landrover -jersey, T-shirt, tee shirt -jigsaw puzzle -jinrikisha, ricksha, rickshaw -joystick -kimono -knee pad -knot -lab coat, laboratory coat -ladle -lampshade, lamp shade -laptop, laptop computer -lawn mower, mower -lens cap, lens cover -letter opener, paper knife, paperknife -library -lifeboat -lighter, light, igniter, ignitor -limousine, limo -liner, ocean liner -lipstick, lip rouge -Loafer -lotion -loudspeaker, speaker, speaker unit, loudspeaker system, speaker system -loupe, jeweler's loupe -lumbermill, sawmill -magnetic compass -mailbag, postbag -mailbox, letter box -maillot -maillot, tank suit -manhole cover -maraca -marimba, xylophone -mask -matchstick -maypole -maze, labyrinth -measuring cup -medicine chest, medicine cabinet -megalith, megalithic structure -microphone, mike -microwave, microwave oven -military uniform -milk can -minibus -miniskirt, mini -minivan -missile -mitten -mixing bowl -mobile home, manufactured home -Model T -modem -monastery -monitor -moped -mortar -mortarboard -mosque -mosquito net -motor scooter, scooter -mountain bike, all-terrain bike, off-roader -mountain tent -mouse, computer mouse -mousetrap -moving van -muzzle -nail -neck brace -necklace -nipple -notebook, notebook computer -obelisk -oboe, hautboy, hautbois -ocarina, sweet potato -odometer, hodometer, mileometer, milometer -oil filter -organ, pipe organ -oscilloscope, scope, cathode-ray oscilloscope, CRO -overskirt -oxcart -oxygen mask -packet -paddle, boat paddle -paddlewheel, paddle wheel -padlock -paintbrush -pajama, pyjama, pj's, jammies -palace -panpipe, pandean pipe, syrinx -paper towel -parachute, chute -parallel bars, bars -park bench -parking meter -passenger car, coach, carriage -patio, terrace -pay-phone, pay-station -pedestal, plinth, footstall -pencil box, pencil case -pencil sharpener -perfume, essence -Petri dish -photocopier -pick, plectrum, plectron -pickelhaube -picket fence, paling -pickup, pickup truck -pier -piggy bank, penny bank -pill bottle -pillow -ping-pong ball -pinwheel -pirate, pirate ship -pitcher, ewer -plane, carpenter's plane, woodworking plane -planetarium -plastic bag -plate rack -plow, plough -plunger, plumber's helper -Polaroid camera, Polaroid Land camera -pole -police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria -poncho -pool table, billiard table, snooker table -pop bottle, soda bottle -pot, flowerpot -potter's wheel -power drill -prayer rug, prayer mat -printer -prison, prison house -projectile, missile -projector -puck, hockey puck -punching bag, punch bag, punching ball, punchball -purse -quill, quill pen -quilt, comforter, comfort, puff -racer, race car, racing car -racket, racquet -radiator -radio, wireless -radio telescope, radio reflector -rain barrel -recreational vehicle, RV, R.V. -reel -reflex camera -refrigerator, icebox -remote control, remote -restaurant, eating house, eating place, eatery -revolver, six-gun, six-shooter -rifle -rocking chair, rocker -rotisserie -rubber eraser, rubber, pencil eraser -rugby ball -rule, ruler -running shoe -safe -safety pin -saltshaker, salt shaker -sandal -sarong -sax, saxophone -scabbard -scale, weighing machine -school bus -schooner -scoreboard -screen, CRT screen -screw -screwdriver -seat belt, seatbelt -sewing machine -shield, buckler -shoe shop, shoe-shop, shoe store -shoji -shopping basket -shopping cart -shovel -shower cap -shower curtain -ski -ski mask -sleeping bag -slide rule, slipstick -sliding door -slot, one-armed bandit -snorkel -snowmobile -snowplow, snowplough -soap dispenser -soccer ball -sock -solar dish, solar collector, solar furnace -sombrero -soup bowl -space bar -space heater -space shuttle -spatula -speedboat -spider web, spider's web -spindle -sports car, sport car -spotlight, spot -stage -steam locomotive -steel arch bridge -steel drum -stethoscope -stole -stone wall -stopwatch, stop watch -stove -strainer -streetcar, tram, tramcar, trolley, trolley car -stretcher -studio couch, day bed -stupa, tope -submarine, pigboat, sub, U-boat -suit, suit of clothes -sundial -sunglass -sunglasses, dark glasses, shades -sunscreen, sunblock, sun blocker -suspension bridge -swab, swob, mop -sweatshirt -swimming trunks, bathing trunks -swing -switch, electric switch, electrical switch -syringe -table lamp -tank, army tank, armored combat vehicle, armoured combat vehicle -tape player -teapot -teddy, teddy bear -television, television system -tennis ball -thatch, thatched roof -theater curtain, theatre curtain -thimble -thresher, thrasher, threshing machine -throne -tile roof -toaster -tobacco shop, tobacconist shop, tobacconist -toilet seat -torch -totem pole -tow truck, tow car, wrecker -toyshop -tractor -trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi -tray -trench coat -tricycle, trike, velocipede -trimaran -tripod -triumphal arch -trolleybus, trolley coach, trackless trolley -trombone -tub, vat -turnstile -typewriter keyboard -umbrella -unicycle, monocycle -upright, upright piano -vacuum, vacuum cleaner -vase -vault -velvet -vending machine -vestment -viaduct -violin, fiddle -volleyball -waffle iron -wall clock -wallet, billfold, notecase, pocketbook -wardrobe, closet, press -warplane, military plane -washbasin, handbasin, washbowl, lavabo, wash-hand basin -washer, automatic washer, washing machine -water bottle -water jug -water tower -whiskey jug -whistle -wig -window screen -window shade -Windsor tie -wine bottle -wing -wok -wooden spoon -wool, woolen, woollen -worm fence, snake fence, snake-rail fence, Virginia fence -wreck -yawl -yurt -web site, website, internet site, site -comic book -crossword puzzle, crossword -street sign -traffic light, traffic signal, stoplight -book jacket, dust cover, dust jacket, dust wrapper -menu -plate -guacamole -consomme -hot pot, hotpot -trifle -ice cream, icecream -ice lolly, lolly, lollipop, popsicle -French loaf -bagel, beigel -pretzel -cheeseburger -hotdog, hot dog, red hot -mashed potato -head cabbage -broccoli -cauliflower -zucchini, courgette -spaghetti squash -acorn squash -butternut squash -cucumber, cuke -artichoke, globe artichoke -bell pepper -cardoon -mushroom -Granny Smith -strawberry -orange -lemon -fig -pineapple, ananas -banana -jackfruit, jak, jack -custard apple -pomegranate -hay -carbonara -chocolate sauce, chocolate syrup -dough -meat loaf, meatloaf -pizza, pizza pie -potpie -burrito -red wine -espresso -cup -eggnog -alp -bubble -cliff, drop, drop-off -coral reef -geyser -lakeside, lakeshore -promontory, headland, head, foreland -sandbar, sand bar -seashore, coast, seacoast, sea-coast -valley, vale -volcano -ballplayer, baseball player -groom, bridegroom -scuba diver -rapeseed -daisy -yellow lady's slipper, yellow lady-slipper, Cypripedium calceolus, Cypripedium parviflorum -corn -acorn -hip, rose hip, rosehip -buckeye, horse chestnut, conker -coral fungus -agaric -gyromitra -stinkhorn, carrion fungus -earthstar -hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa -bolete -ear, spike, capitulum -toilet tissue, toilet paper, bathroom tissue diff --git a/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/mobilenet_v3.py b/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/mobilenet_v3.py deleted file mode 100644 index 78db751386e62dd27485f5b244e808d7c227bcbd..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/mobilenet_v3.py +++ /dev/null @@ -1,311 +0,0 @@ -# copyright (c) 2020 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. - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import paddle.fluid as fluid -from paddle.fluid.initializer import MSRA -from paddle.fluid.param_attr import ParamAttr - -__all__ = [ - 'MobileNetV3', 'MobileNetV3_small_x0_35', 'MobileNetV3_small_x0_5', - 'MobileNetV3_small_x0_75', 'MobileNetV3_small_x1_0', - 'MobileNetV3_small_x1_25', 'MobileNetV3_large_x0_35', - 'MobileNetV3_large_x0_5', 'MobileNetV3_large_x0_75', - 'MobileNetV3_large_x1_0', 'MobileNetV3_large_x1_25' -] - - -class MobileNetV3(): - def __init__(self, scale=1.0, model_name='small'): - self.scale = scale - self.inplanes = 16 - if model_name == "large": - self.cfg = [ - # k, exp, c, se, nl, s, - [3, 16, 16, False, 'relu', 1], - [3, 64, 24, False, 'relu', 2], - [3, 72, 24, False, 'relu', 1], - [5, 72, 40, True, 'relu', 2], - [5, 120, 40, True, 'relu', 1], - [5, 120, 40, True, 'relu', 1], - [3, 240, 80, False, 'hard_swish', 2], - [3, 200, 80, False, 'hard_swish', 1], - [3, 184, 80, False, 'hard_swish', 1], - [3, 184, 80, False, 'hard_swish', 1], - [3, 480, 112, True, 'hard_swish', 1], - [3, 672, 112, True, 'hard_swish', 1], - [5, 672, 160, True, 'hard_swish', 2], - [5, 960, 160, True, 'hard_swish', 1], - [5, 960, 160, True, 'hard_swish', 1], - ] - self.cls_ch_squeeze = 960 - self.cls_ch_expand = 1280 - elif model_name == "small": - self.cfg = [ - # k, exp, c, se, nl, s, - [3, 16, 16, True, 'relu', 2], - [3, 72, 24, False, 'relu', 2], - [3, 88, 24, False, 'relu', 1], - [5, 96, 40, True, 'hard_swish', 2], - [5, 240, 40, True, 'hard_swish', 1], - [5, 240, 40, True, 'hard_swish', 1], - [5, 120, 48, True, 'hard_swish', 1], - [5, 144, 48, True, 'hard_swish', 1], - [5, 288, 96, True, 'hard_swish', 2], - [5, 576, 96, True, 'hard_swish', 1], - [5, 576, 96, True, 'hard_swish', 1], - ] - self.cls_ch_squeeze = 576 - self.cls_ch_expand = 1280 - else: - raise NotImplementedError("mode[" + model_name + - "_model] is not implemented!") - - def net(self, input, class_dim=1000): - scale = self.scale - inplanes = self.inplanes - cfg = self.cfg - cls_ch_squeeze = self.cls_ch_squeeze - cls_ch_expand = self.cls_ch_expand - #conv1 - conv = self.conv_bn_layer( - input, - filter_size=3, - num_filters=self.make_divisible(inplanes * scale), - stride=2, - padding=1, - num_groups=1, - if_act=True, - act='hard_swish', - name='conv1') - i = 0 - inplanes = self.make_divisible(inplanes * scale) - for layer_cfg in cfg: - conv = self.residual_unit( - input=conv, - num_in_filter=inplanes, - num_mid_filter=self.make_divisible(scale * layer_cfg[1]), - num_out_filter=self.make_divisible(scale * layer_cfg[2]), - act=layer_cfg[4], - stride=layer_cfg[5], - filter_size=layer_cfg[0], - use_se=layer_cfg[3], - name='conv' + str(i + 2)) - inplanes = self.make_divisible(scale * layer_cfg[2]) - i += 1 - - conv = self.conv_bn_layer( - input=conv, - filter_size=1, - num_filters=self.make_divisible(scale * cls_ch_squeeze), - stride=1, - padding=0, - num_groups=1, - if_act=True, - act='hard_swish', - name='conv_last') - conv = fluid.layers.pool2d( - input=conv, pool_type='avg', global_pooling=True, use_cudnn=False) - conv = fluid.layers.conv2d( - input=conv, - num_filters=cls_ch_expand, - filter_size=1, - stride=1, - padding=0, - act=None, - param_attr=ParamAttr(name='last_1x1_conv_weights'), - bias_attr=False) - conv = fluid.layers.hard_swish(conv) - drop = fluid.layers.dropout(x=conv, dropout_prob=0.2) - out = fluid.layers.fc( - input=drop, - size=class_dim, - param_attr=ParamAttr(name='fc_weights'), - bias_attr=ParamAttr(name='fc_offset')) - return out, drop - - def conv_bn_layer(self, - input, - filter_size, - num_filters, - stride, - padding, - num_groups=1, - if_act=True, - act=None, - name=None, - use_cudnn=True, - res_last_bn_init=False): - conv = fluid.layers.conv2d( - input=input, - num_filters=num_filters, - filter_size=filter_size, - stride=stride, - padding=padding, - groups=num_groups, - act=None, - use_cudnn=use_cudnn, - param_attr=ParamAttr(name=name + '_weights'), - bias_attr=False) - bn_name = name + '_bn' - bn = fluid.layers.batch_norm( - input=conv, - param_attr=ParamAttr( - name=bn_name + "_scale", - regularizer=fluid.regularizer.L2DecayRegularizer( - regularization_coeff=0.0)), - bias_attr=ParamAttr( - name=bn_name + "_offset", - regularizer=fluid.regularizer.L2DecayRegularizer( - regularization_coeff=0.0)), - moving_mean_name=bn_name + '_mean', - moving_variance_name=bn_name + '_variance') - if if_act: - if act == 'relu': - bn = fluid.layers.relu(bn) - elif act == 'hard_swish': - bn = fluid.layers.hard_swish(bn) - return bn - - def make_divisible(self, v, divisor=8, min_value=None): - if min_value is None: - min_value = divisor - new_v = max(min_value, int(v + divisor / 2) // divisor * divisor) - if new_v < 0.9 * v: - new_v += divisor - return new_v - - def se_block(self, input, num_out_filter, ratio=4, name=None): - num_mid_filter = num_out_filter // ratio - pool = fluid.layers.pool2d( - input=input, pool_type='avg', global_pooling=True, use_cudnn=False) - conv1 = fluid.layers.conv2d( - input=pool, - filter_size=1, - num_filters=num_mid_filter, - act='relu', - param_attr=ParamAttr(name=name + '_1_weights'), - bias_attr=ParamAttr(name=name + '_1_offset')) - conv2 = fluid.layers.conv2d( - input=conv1, - filter_size=1, - num_filters=num_out_filter, - act='hard_sigmoid', - param_attr=ParamAttr(name=name + '_2_weights'), - bias_attr=ParamAttr(name=name + '_2_offset')) - scale = fluid.layers.elementwise_mul(x=input, y=conv2, axis=0) - return scale - - def residual_unit(self, - input, - num_in_filter, - num_mid_filter, - num_out_filter, - stride, - filter_size, - act=None, - use_se=False, - name=None): - - conv0 = self.conv_bn_layer( - input=input, - filter_size=1, - num_filters=num_mid_filter, - stride=1, - padding=0, - if_act=True, - act=act, - name=name + '_expand') - - conv1 = self.conv_bn_layer( - input=conv0, - filter_size=filter_size, - num_filters=num_mid_filter, - stride=stride, - padding=int((filter_size - 1) // 2), - if_act=True, - act=act, - num_groups=num_mid_filter, - use_cudnn=False, - name=name + '_depthwise') - if use_se: - conv1 = self.se_block( - input=conv1, num_out_filter=num_mid_filter, name=name + '_se') - - conv2 = self.conv_bn_layer( - input=conv1, - filter_size=1, - num_filters=num_out_filter, - stride=1, - padding=0, - if_act=False, - name=name + '_linear', - res_last_bn_init=True) - if num_in_filter != num_out_filter or stride != 1: - return conv2 - else: - return fluid.layers.elementwise_add(x=input, y=conv2, act=None) - - -def MobileNetV3_small_x0_35(): - model = MobileNetV3(model_name='small', scale=0.35) - return model - - -def MobileNetV3_small_x0_5(): - model = MobileNetV3(model_name='small', scale=0.5) - return model - - -def MobileNetV3_small_x0_75(): - model = MobileNetV3(model_name='small', scale=0.75) - return model - - -def MobileNetV3_small_x1_0(): - model = MobileNetV3(model_name='small', scale=1.0) - return model - - -def MobileNetV3_small_x1_25(): - model = MobileNetV3(model_name='small', scale=1.25) - return model - - -def MobileNetV3_large_x0_35(): - model = MobileNetV3(model_name='large', scale=0.35) - return model - - -def MobileNetV3_large_x0_5(): - model = MobileNetV3(model_name='large', scale=0.5) - return model - - -def MobileNetV3_large_x0_75(): - model = MobileNetV3(model_name='large', scale=0.75) - return model - - -def MobileNetV3_large_x1_0(): - model = MobileNetV3(model_name='large', scale=1.0) - return model - - -def MobileNetV3_large_x1_25(): - model = MobileNetV3(model_name='large', scale=1.25) - return model diff --git a/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/module.py b/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/module.py index 5e24ce93d810e40d2f27b658394d3d59586b5754..5924f00b0a90be4027f90e065cdb19d4d88835e3 100644 --- a/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/module.py +++ b/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/module.py @@ -1,278 +1,276 @@ -# coding=utf-8 -from __future__ import absolute_import -from __future__ import division - -import ast -import argparse +# copyright (c) 2020 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 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 mobilenet_v3_small_imagenet_ssld.processor import postprocess, base64_to_cv2 -from mobilenet_v3_small_imagenet_ssld.data_feed import reader -from mobilenet_v3_small_imagenet_ssld.mobilenet_v3 import MobileNetV3 - +import paddle +from paddle import ParamAttr +import paddle.nn as nn +import paddle.nn.functional as F +from paddle.nn import Conv2d, BatchNorm, Linear, Dropout +from paddle.nn import AdaptiveAvgPool2d, MaxPool2d, AvgPool2d +from paddle.regularizer import L2Decay +from paddlehub.module.module import moduleinfo +from paddlehub.module.cv_module import ImageClassifierModule -@moduleinfo( - name="mobilenet_v3_small_imagenet_ssld", - type="CV/image_classification", - author="paddlepaddle", - author_email="paddle-dev@baidu.com", - summary= - "Mobilenet_V3_Small is a image classfication model, this module is trained with ImageNet-2012 dataset.", - version="1.0.0") -class MobileNetV3Small(hub.Module): - def _initialize(self): - self.default_pretrained_model_path = os.path.join( - self.directory, "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] - self._set_config() - def get_expected_image_width(self): - return 224 +def make_divisible(v, divisor=8, min_value=None): + if min_value is None: + min_value = divisor + new_v = max(min_value, int(v + divisor / 2) // divisor * divisor) + if new_v < 0.9 * v: + new_v += divisor + return new_v - def get_expected_image_height(self): - return 224 - def get_pretrained_images_mean(self): - im_mean = np.array([0.485, 0.456, 0.406]).reshape(1, 3) - return im_mean +@moduleinfo(name="mobilenet_v3_small_imagenet_ssld", + type="cv/classification", + author="paddlepaddle", + author_email="", + summary="mobilenet_v3_small_imagenet_ssld is a classification model, " + "this module is trained with Imagenet dataset.", + version="1.1.0", + meta=ImageClassifierModule) +class MobileNetV3Small(nn.Layer): + """MobileNetV3Small module.""" + def __init__(self, dropout_prob: float = 0.2, class_dim: int = 1000, load_checkpoint: str = None): + super(MobileNetV3Small, self).__init__() - def get_pretrained_images_std(self): - im_std = np.array([0.229, 0.224, 0.225]).reshape(1, 3) - return im_std + inplanes = 16 + self.cfg = [ + # k, exp, c, se, nl, s, + [3, 16, 16, True, "relu", 2], + [3, 72, 24, False, "relu", 2], + [3, 88, 24, False, "relu", 1], + [5, 96, 40, True, "hard_swish", 2], + [5, 240, 40, True, "hard_swish", 1], + [5, 240, 40, True, "hard_swish", 1], + [5, 120, 48, True, "hard_swish", 1], + [5, 144, 48, True, "hard_swish", 1], + [5, 288, 96, True, "hard_swish", 2], + [5, 576, 96, True, "hard_swish", 1], + [5, 576, 96, True, "hard_swish", 1], + ] + self.cls_ch_squeeze = 576 + self.cls_ch_expand = 1280 - def _set_config(self): - """ - predictor config setting - """ - cpu_config = AnalysisConfig(self.default_pretrained_model_path) - cpu_config.disable_glog_info() - cpu_config.disable_gpu() - self.cpu_predictor = create_paddle_predictor(cpu_config) + self.conv1 = ConvBNLayer(in_c=3, + out_c=make_divisible(inplanes), + filter_size=3, + stride=2, + padding=1, + num_groups=1, + if_act=True, + act="hard_swish", + name="conv1") - try: - _places = os.environ["CUDA_VISIBLE_DEVICES"] - int(_places[0]) - use_gpu = True - except: - use_gpu = False - if use_gpu: - gpu_config = AnalysisConfig(self.default_pretrained_model_path) - 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) + self.block_list = [] + i = 0 + inplanes = make_divisible(inplanes) + for (k, exp, c, se, nl, s) in self.cfg: + self.block_list.append( + ResidualUnit(in_c=inplanes, + mid_c=make_divisible(exp), + out_c=make_divisible(c), + filter_size=k, + stride=s, + use_se=se, + act=nl, + name="conv" + str(i + 2))) + self.add_sublayer(sublayer=self.block_list[-1], name="conv" + str(i + 2)) + inplanes = make_divisible(c) + i += 1 - def context(self, trainable=True, pretrained=True): - """context for transfer learning. + self.last_second_conv = ConvBNLayer(in_c=inplanes, + out_c=make_divisible(self.cls_ch_squeeze), + filter_size=1, + stride=1, + padding=0, + num_groups=1, + if_act=True, + act="hard_swish", + name="conv_last") - Args: - trainable (bool): Set parameters in program to be trainable. - pretrained (bool) : Whether to load pretrained model. + self.pool = AdaptiveAvgPool2d(1) - 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") - mobile_net = MobileNetV3() - output, feature_map = mobile_net.net( - input=image, class_dim=len(self.label_list)) + self.last_conv = Conv2d(in_channels=make_divisible(self.cls_ch_squeeze), + out_channels=self.cls_ch_expand, + kernel_size=1, + stride=1, + padding=0, + weight_attr=ParamAttr(name="last_1x1_conv_weights"), + bias_attr=False) - 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() - } + self.dropout = Dropout(p=dropout_prob, mode="downscale_in_infer") - place = fluid.CPUPlace() - exe = fluid.Executor(place) - # pretrained - if pretrained: + self.out = Linear(self.cls_ch_expand, + class_dim, + weight_attr=ParamAttr("fc_weights"), + bias_attr=ParamAttr(name="fc_offset")) - def _if_exist(var): - b = os.path.exists( - os.path.join(self.default_pretrained_model_path, - var.name)) - return b + if load_checkpoint is not None: + model_dict = paddle.load(load_checkpoint)[0] + self.set_dict(model_dict) + print("load custom checkpoint success") - 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 + else: + checkpoint = os.path.join(self.directory, 'mobilenet_v3_small_ssld.pdparams') + if not os.path.exists(checkpoint): + os.system( + 'wget https://paddlehub.bj.bcebos.com/dygraph/image_classification/mobilenet_v3_small_ssld.pdparams -O ' + + checkpoint) + model_dict = paddle.load(checkpoint)[0] + self.set_dict(model_dict) + print("load pretrained checkpoint success") - def classification(self, - images=None, - paths=None, - batch_size=1, - use_gpu=False, - top_k=1): - """ - API for image classification. + def forward(self, inputs: paddle.Tensor): + x = self.conv1(inputs) + for block in self.block_list: + x = block(x) - Args: - images (numpy.ndarray): data of images, shape of each is [H, W, C], color space must be BGR. - paths (list[str]): The paths of images. - batch_size (int): batch size. - use_gpu (bool): Whether to use gpu. - top_k (int): Return top k results. + x = self.last_second_conv(x) + x = self.pool(x) - 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." - ) + x = self.last_conv(x) + x = F.hard_swish(x) + x = self.dropout(x) + x = paddle.reshape(x, shape=[x.shape[0], x.shape[1]]) + x = self.out(x) + return x - all_data = list() - for yield_data in reader(images, paths): - all_data.append(yield_data) - total_num = len(all_data) - loop_num = int(np.ceil(total_num / batch_size)) +class ConvBNLayer(nn.Layer): + """Basic conv bn layer.""" + def __init__(self, + in_c: int, + out_c: int, + filter_size: int, + stride: int, + padding: int, + num_groups: int = 1, + if_act: bool = True, + act: str = None, + name: str = ""): + super(ConvBNLayer, self).__init__() + self.if_act = if_act + self.act = act + self.conv = Conv2d(in_channels=in_c, + out_channels=out_c, + kernel_size=filter_size, + stride=stride, + padding=padding, + groups=num_groups, + weight_attr=ParamAttr(name=name + "_weights"), + bias_attr=False) + self.bn = BatchNorm(num_channels=out_c, + act=None, + param_attr=ParamAttr(name=name + "_bn_scale", regularizer=L2Decay(0.0)), + bias_attr=ParamAttr(name=name + "_bn_offset", regularizer=L2Decay(0.0)), + moving_mean_name=name + "_bn_mean", + moving_variance_name=name + "_bn_variance") - res = list() - for iter_id in range(loop_num): - batch_data = list() - handle_id = iter_id * batch_size - for image_id in range(batch_size): - try: - batch_data.append(all_data[handle_id + image_id]) - except: - 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) - res += out - return res + def forward(self, x): + x = self.conv(x) + x = self.bn(x) + if self.if_act: + if self.act == "relu": + x = F.relu(x) + elif self.act == "hard_swish": + x = F.hard_swish(x) + else: + print("The activation function is selected incorrectly.") + exit() + return x - 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) +class ResidualUnit(nn.Layer): + """Residual unit for MobileNetV3.""" + def __init__(self, + in_c: int, + mid_c: int, + out_c: int, + filter_size: int, + stride: int, + use_se: bool, + act: str = None, + name: str = ''): + super(ResidualUnit, self).__init__() + self.if_shortcut = stride == 1 and in_c == out_c + self.if_se = use_se - 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) + self.expand_conv = ConvBNLayer(in_c=in_c, + out_c=mid_c, + filter_size=1, + stride=1, + padding=0, + if_act=True, + act=act, + name=name + "_expand") + self.bottleneck_conv = ConvBNLayer(in_c=mid_c, + out_c=mid_c, + filter_size=filter_size, + stride=stride, + padding=int((filter_size - 1) // 2), + num_groups=mid_c, + if_act=True, + act=act, + name=name + "_depthwise") + if self.if_se: + self.mid_se = SEModule(mid_c, name=name + "_se") + self.linear_conv = ConvBNLayer(in_c=mid_c, + out_c=out_c, + filter_size=1, + stride=1, + padding=0, + if_act=False, + act=None, + name=name + "_linear") - @serving - def serving_method(self, images, **kwargs): - """ - Run as a service. - """ - images_decode = [base64_to_cv2(image) for image in images] - results = self.classification(images=images_decode, **kwargs) - return results + def forward(self, inputs: paddle.Tensor): + x = self.expand_conv(inputs) + x = self.bottleneck_conv(x) + if self.if_se: + x = self.mid_se(x) + x = self.linear_conv(x) + if self.if_shortcut: + x = paddle.elementwise_add(inputs, x) + return x - @runnable - 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.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.") - self.add_module_config_arg() - self.add_module_input_arg() - args = self.parser.parse_args(argvs) - results = self.classification( - paths=[args.input_path], - batch_size=args.batch_size, - use_gpu=args.use_gpu) - return results - 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( - '--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.") +class SEModule(nn.Layer): + """Basic model for ResidualUnit.""" + def __init__(self, channel: int, reduction: int = 4, name: str = ""): + super(SEModule, self).__init__() + self.avg_pool = AdaptiveAvgPool2d(1) + self.conv1 = Conv2d(in_channels=channel, + out_channels=channel // reduction, + kernel_size=1, + stride=1, + padding=0, + weight_attr=ParamAttr(name=name + "_1_weights"), + bias_attr=ParamAttr(name=name + "_1_offset")) + self.conv2 = Conv2d(in_channels=channel // reduction, + out_channels=channel, + kernel_size=1, + stride=1, + padding=0, + weight_attr=ParamAttr(name + "_2_weights"), + bias_attr=ParamAttr(name=name + "_2_offset")) - def add_module_input_arg(self): - """ - Add the command input options. - """ - self.arg_input_group.add_argument( - '--input_path', type=str, help="path to image.") + def forward(self, inputs: paddle.Tensor): + outputs = self.avg_pool(inputs) + outputs = self.conv1(outputs) + outputs = F.relu(outputs) + outputs = self.conv2(outputs) + outputs = F.hard_sigmoid(outputs) + return paddle.multiply(x=inputs, y=outputs, axis=0) diff --git a/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/processor.py b/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/processor.py deleted file mode 100644 index fa8cbb502312e6ef80697ab63b767d4077b3847b..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/mobilenet_v3_small_imagenet_ssld/processor.py +++ /dev/null @@ -1,55 +0,0 @@ -# coding=utf-8 -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import base64 -import cv2 -import os - -import numpy as np - - -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 - - -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)) - x = np.exp(x) - tmp = np.sum(x, axis=1) - x /= tmp.reshape((x.shape[0], 1)) - else: - tmp = np.max(x) - x -= tmp - x = np.exp(x) - tmp = np.sum(x) - x /= tmp - return x - - -def postprocess(data_out, label_list, top_k): - """ - Postprocess output of network, one image at a time. - - Args: - data_out (numpy.ndarray): output data of network. - label_list (list): list of label. - top_k (int): Return top k results. - """ - output = [] - for result in data_out: - result_i = softmax(result) - output_i = {} - indexs = np.argsort(result_i)[::-1][0:top_k] - for index in indexs: - label = label_list[index].split(',')[0] - output_i[label] = float(result_i[index]) - output.append(output_i) - return output diff --git a/hub_module/modules/image/classification/resnet50_vd_10w/README.md b/hub_module/modules/image/classification/resnet50_vd_10w/README.md deleted file mode 100644 index c4ff29267e4175bcf7a43f42c272d670e35a46dc..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/resnet50_vd_10w/README.md +++ /dev/null @@ -1,85 +0,0 @@ -

-
ResNet 系列的网络结构 -

- -模型的详情可参考[论文](https://arxiv.org/pdf/1812.01187.pdf) - - -## API - -```python -def get_expected_image_width() -``` - -返回预处理的图片宽度,也就是224。 - -```python -def get_expected_image_height() -``` - -返回预处理的图片高度,也就是224。 - -```python -def get_pretrained_images_mean() -``` - -返回预处理的图片均值,也就是 \[0.485, 0.456, 0.406\]。 - -```python -def get_pretrained_images_std() -``` - -返回预处理的图片标准差,也就是 \[0.229, 0.224, 0.225\]。 - - -```python -def context(trainable=True, pretrained=True) -``` - -**参数** - -* trainable (bool): 计算图的参数是否为可训练的; -* pretrained (bool): 是否加载默认的预训练模型。 - -**返回** - -* inputs (dict): 计算图的输入,key 为 'image', value 为图片的张量; -* outputs (dict): 计算图的输出,key 为 'feature\_map', value为全连接层前面的那个张量。 -* context\_prog(fluid.Program): 计算图,用于迁移学习。 - - -```python -def save_inference_model(dirname, - model_filename=None, - params_filename=None, - combined=True) -``` - -将模型保存到指定路径。 - -**参数** - -* dirname: 存在模型的目录名称 -* model_filename: 模型文件名称,默认为\_\_model\_\_ -* params_filename: 参数文件名称,默认为\_\_params\_\_(仅当`combined`为True时生效) -* combined: 是否将参数保存到统一的一个文件中 - -## 代码示例 - -```python -import paddlehub as hub -import cv2 - -classifier = hub.Module(name="resnet50_vd_10w") -input_dict, output_dict, program = classifier.context(trainable=True) -``` - -### 查看代码 - -[PaddleClas](https://github.com/PaddlePaddle/PaddleClas) - -### 依赖 - -paddlepaddle >= 1.6.2 - -paddlehub >= 1.6.0 diff --git a/hub_module/modules/image/classification/resnet50_vd_10w/__init__.py b/hub_module/modules/image/classification/resnet50_vd_10w/__init__.py deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/hub_module/modules/image/classification/resnet50_vd_10w/data_feed.py b/hub_module/modules/image/classification/resnet50_vd_10w/data_feed.py deleted file mode 100644 index d5ffb5efe9fdfbd143b949892aa44d851e907b41..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/resnet50_vd_10w/data_feed.py +++ /dev/null @@ -1,84 +0,0 @@ -# coding=utf-8 -import os -import time -from collections import OrderedDict - -import cv2 -import numpy as np -from PIL import Image - -__all__ = ['reader'] - -DATA_DIM = 224 -img_mean = np.array([0.485, 0.456, 0.406]).reshape((3, 1, 1)) -img_std = np.array([0.229, 0.224, 0.225]).reshape((3, 1, 1)) - - -def resize_short(img, target_size): - percent = float(target_size) / min(img.size[0], img.size[1]) - resized_width = int(round(img.size[0] * percent)) - resized_height = int(round(img.size[1] * percent)) - img = img.resize((resized_width, resized_height), Image.LANCZOS) - return img - - -def crop_image(img, target_size, center): - width, height = img.size - size = target_size - if center == True: - w_start = (width - size) / 2 - h_start = (height - size) / 2 - else: - w_start = np.random.randint(0, width - size + 1) - h_start = np.random.randint(0, height - size + 1) - w_end = w_start + size - h_end = h_start + size - img = img.crop((w_start, h_start, w_end, h_end)) - return img - - -def process_image(img): - img = resize_short(img, target_size=256) - img = crop_image(img, target_size=DATA_DIM, center=True) - if img.mode != 'RGB': - img = img.convert('RGB') - img = np.array(img).astype('float32').transpose((2, 0, 1)) / 255 - img -= img_mean - img /= img_std - return img - - -def reader(images=None, paths=None): - """ - Preprocess to yield image. - - Args: - images (list[numpy.ndarray]): images data, shape of each is [H, W, C]. - paths (list[str]): paths to images. - - Yield: - each (collections.OrderedDict): info of original image, preprocessed image. - """ - component = list() - if paths: - for im_path in paths: - each = OrderedDict() - assert os.path.isfile( - im_path), "The {} isn't a valid file path.".format(im_path) - each['org_im_path'] = im_path - each['org_im'] = Image.open(im_path) - each['org_im_width'], each['org_im_height'] = each['org_im'].size - component.append(each) - if images is not None: - assert type(images), "images is a list." - for im in images: - each = OrderedDict() - each['org_im'] = Image.fromarray(im[:, :, ::-1]) - each['org_im_path'] = 'ndarray_time={}'.format( - round(time.time(), 6) * 1e6) - each['org_im_width'], each['org_im_height'] = each['org_im'].size - component.append(each) - - for element in component: - element['image'] = process_image(element['org_im']) - yield element diff --git a/hub_module/modules/image/classification/resnet50_vd_10w/label_list.txt b/hub_module/modules/image/classification/resnet50_vd_10w/label_list.txt deleted file mode 100644 index a509c007481d301e524e7b3c97561132dbfcc765..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/resnet50_vd_10w/label_list.txt +++ /dev/null @@ -1,1000 +0,0 @@ -tench, Tinca tinca -goldfish, Carassius auratus -great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias -tiger shark, Galeocerdo cuvieri -hammerhead, hammerhead shark -electric ray, crampfish, numbfish, torpedo -stingray -cock -hen -ostrich, Struthio camelus -brambling, Fringilla montifringilla -goldfinch, Carduelis carduelis -house finch, linnet, Carpodacus mexicanus -junco, snowbird -indigo bunting, indigo finch, indigo bird, Passerina cyanea -robin, American robin, Turdus migratorius -bulbul -jay -magpie -chickadee -water ouzel, dipper -kite -bald eagle, American eagle, Haliaeetus leucocephalus -vulture -great grey owl, great gray owl, Strix nebulosa -European fire salamander, Salamandra salamandra -common newt, Triturus vulgaris -eft -spotted salamander, Ambystoma maculatum -axolotl, mud puppy, Ambystoma mexicanum -bullfrog, Rana catesbeiana -tree frog, tree-frog -tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui -loggerhead, loggerhead turtle, Caretta caretta -leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea -mud turtle -terrapin -box turtle, box tortoise -banded gecko -common iguana, iguana, Iguana iguana -American chameleon, anole, Anolis carolinensis -whiptail, whiptail lizard -agama -frilled lizard, Chlamydosaurus kingi -alligator lizard -Gila monster, Heloderma suspectum -green lizard, Lacerta viridis -African chameleon, Chamaeleo chamaeleon -Komodo dragon, Komodo lizard, dragon lizard, giant lizard, Varanus komodoensis -African crocodile, Nile crocodile, Crocodylus niloticus -American alligator, Alligator mississipiensis -triceratops -thunder snake, worm snake, Carphophis amoenus -ringneck snake, ring-necked snake, ring snake -hognose snake, puff adder, sand viper -green snake, grass snake -king snake, kingsnake -garter snake, grass snake -water snake -vine snake -night snake, Hypsiglena torquata -boa constrictor, Constrictor constrictor -rock python, rock snake, Python sebae -Indian cobra, Naja naja -green mamba -sea snake -horned viper, cerastes, sand viper, horned asp, Cerastes cornutus -diamondback, diamondback rattlesnake, Crotalus adamanteus -sidewinder, horned rattlesnake, Crotalus cerastes -trilobite -harvestman, daddy longlegs, Phalangium opilio -scorpion -black and gold garden spider, Argiope aurantia -barn spider, Araneus cavaticus -garden spider, Aranea diademata -black widow, Latrodectus mactans -tarantula -wolf spider, hunting spider -tick -centipede -black grouse -ptarmigan -ruffed grouse, partridge, Bonasa umbellus -prairie chicken, prairie grouse, prairie fowl -peacock -quail -partridge -African grey, African gray, Psittacus erithacus -macaw -sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita -lorikeet -coucal -bee eater -hornbill -hummingbird -jacamar -toucan -drake -red-breasted merganser, Mergus serrator -goose -black swan, Cygnus atratus -tusker -echidna, spiny anteater, anteater -platypus, duckbill, duckbilled platypus, duck-billed platypus, Ornithorhynchus anatinus -wallaby, brush kangaroo -koala, koala bear, kangaroo bear, native bear, Phascolarctos cinereus -wombat -jellyfish -sea anemone, anemone -brain coral -flatworm, platyhelminth -nematode, nematode worm, roundworm -conch -snail -slug -sea slug, nudibranch -chiton, coat-of-mail shell, sea cradle, polyplacophore -chambered nautilus, pearly nautilus, nautilus -Dungeness crab, Cancer magister -rock crab, Cancer irroratus -fiddler crab -king crab, Alaska crab, Alaskan king crab, Alaska king crab, Paralithodes camtschatica -American lobster, Northern lobster, Maine lobster, Homarus americanus -spiny lobster, langouste, rock lobster, crawfish, crayfish, sea crawfish -crayfish, crawfish, crawdad, crawdaddy -hermit crab -isopod -white stork, Ciconia ciconia -black stork, Ciconia nigra -spoonbill -flamingo -little blue heron, Egretta caerulea -American egret, great white heron, Egretta albus -bittern -crane -limpkin, Aramus pictus -European gallinule, Porphyrio porphyrio -American coot, marsh hen, mud hen, water hen, Fulica americana -bustard -ruddy turnstone, Arenaria interpres -red-backed sandpiper, dunlin, Erolia alpina -redshank, Tringa totanus -dowitcher -oystercatcher, oyster catcher -pelican -king penguin, Aptenodytes patagonica -albatross, mollymawk -grey whale, gray whale, devilfish, Eschrichtius gibbosus, Eschrichtius robustus -killer whale, killer, orca, grampus, sea wolf, Orcinus orca -dugong, Dugong dugon -sea lion -Chihuahua -Japanese spaniel -Maltese dog, Maltese terrier, Maltese -Pekinese, Pekingese, Peke -Shih-Tzu -Blenheim spaniel -papillon -toy terrier -Rhodesian ridgeback -Afghan hound, Afghan -basset, basset hound -beagle -bloodhound, sleuthhound -bluetick -black-and-tan coonhound -Walker hound, Walker foxhound -English foxhound -redbone -borzoi, Russian wolfhound -Irish wolfhound -Italian greyhound -whippet -Ibizan hound, Ibizan Podenco -Norwegian elkhound, elkhound -otterhound, otter hound -Saluki, gazelle hound -Scottish deerhound, deerhound -Weimaraner -Staffordshire bullterrier, Staffordshire bull terrier -American Staffordshire terrier, Staffordshire terrier, American pit bull terrier, pit bull terrier -Bedlington terrier -Border terrier -Kerry blue terrier -Irish terrier -Norfolk terrier -Norwich terrier -Yorkshire terrier -wire-haired fox terrier -Lakeland terrier -Sealyham terrier, Sealyham -Airedale, Airedale terrier -cairn, cairn terrier -Australian terrier -Dandie Dinmont, Dandie Dinmont terrier -Boston bull, Boston terrier -miniature schnauzer -giant schnauzer -standard schnauzer -Scotch terrier, Scottish terrier, Scottie -Tibetan terrier, chrysanthemum dog -silky terrier, Sydney silky -soft-coated wheaten terrier -West Highland white terrier -Lhasa, Lhasa apso -flat-coated retriever -curly-coated retriever -golden retriever -Labrador retriever -Chesapeake Bay retriever -German short-haired pointer -vizsla, Hungarian pointer -English setter -Irish setter, red setter -Gordon setter -Brittany spaniel -clumber, clumber spaniel -English springer, English springer spaniel -Welsh springer spaniel -cocker spaniel, English cocker spaniel, cocker -Sussex spaniel -Irish water spaniel -kuvasz -schipperke -groenendael -malinois -briard -kelpie -komondor -Old English sheepdog, bobtail -Shetland sheepdog, Shetland sheep dog, Shetland -collie -Border collie -Bouvier des Flandres, Bouviers des Flandres -Rottweiler -German shepherd, German shepherd dog, German police dog, alsatian -Doberman, Doberman pinscher -miniature pinscher -Greater Swiss Mountain dog -Bernese mountain dog -Appenzeller -EntleBucher -boxer -bull mastiff -Tibetan mastiff -French bulldog -Great Dane -Saint Bernard, St Bernard -Eskimo dog, husky -malamute, malemute, Alaskan malamute -Siberian husky -dalmatian, coach dog, carriage dog -affenpinscher, monkey pinscher, monkey dog -basenji -pug, pug-dog -Leonberg -Newfoundland, Newfoundland dog -Great Pyrenees -Samoyed, Samoyede -Pomeranian -chow, chow chow -keeshond -Brabancon griffon -Pembroke, Pembroke Welsh corgi -Cardigan, Cardigan Welsh corgi -toy poodle -miniature poodle -standard poodle -Mexican hairless -timber wolf, grey wolf, gray wolf, Canis lupus -white wolf, Arctic wolf, Canis lupus tundrarum -red wolf, maned wolf, Canis rufus, Canis niger -coyote, prairie wolf, brush wolf, Canis latrans -dingo, warrigal, warragal, Canis dingo -dhole, Cuon alpinus -African hunting dog, hyena dog, Cape hunting dog, Lycaon pictus -hyena, hyaena -red fox, Vulpes vulpes -kit fox, Vulpes macrotis -Arctic fox, white fox, Alopex lagopus -grey fox, gray fox, Urocyon cinereoargenteus -tabby, tabby cat -tiger cat -Persian cat -Siamese cat, Siamese -Egyptian cat -cougar, puma, catamount, mountain lion, painter, panther, Felis concolor -lynx, catamount -leopard, Panthera pardus -snow leopard, ounce, Panthera uncia -jaguar, panther, Panthera onca, Felis onca -lion, king of beasts, Panthera leo -tiger, Panthera tigris -cheetah, chetah, Acinonyx jubatus -brown bear, bruin, Ursus arctos -American black bear, black bear, Ursus americanus, Euarctos americanus -ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus -sloth bear, Melursus ursinus, Ursus ursinus -mongoose -meerkat, mierkat -tiger beetle -ladybug, ladybeetle, lady beetle, ladybird, ladybird beetle -ground beetle, carabid beetle -long-horned beetle, longicorn, longicorn beetle -leaf beetle, chrysomelid -dung beetle -rhinoceros beetle -weevil -fly -bee -ant, emmet, pismire -grasshopper, hopper -cricket -walking stick, walkingstick, stick insect -cockroach, roach -mantis, mantid -cicada, cicala -leafhopper -lacewing, lacewing fly -dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk -damselfly -admiral -ringlet, ringlet butterfly -monarch, monarch butterfly, milkweed butterfly, Danaus plexippus -cabbage butterfly -sulphur butterfly, sulfur butterfly -lycaenid, lycaenid butterfly -starfish, sea star -sea urchin -sea cucumber, holothurian -wood rabbit, cottontail, cottontail rabbit -hare -Angora, Angora rabbit -hamster -porcupine, hedgehog -fox squirrel, eastern fox squirrel, Sciurus niger -marmot -beaver -guinea pig, Cavia cobaya -sorrel -zebra -hog, pig, grunter, squealer, Sus scrofa -wild boar, boar, Sus scrofa -warthog -hippopotamus, hippo, river horse, Hippopotamus amphibius -ox -water buffalo, water ox, Asiatic buffalo, Bubalus bubalis -bison -ram, tup -bighorn, bighorn sheep, cimarron, Rocky Mountain bighorn, Rocky Mountain sheep, Ovis canadensis -ibex, Capra ibex -hartebeest -impala, Aepyceros melampus -gazelle -Arabian camel, dromedary, Camelus dromedarius -llama -weasel -mink -polecat, fitch, foulmart, foumart, Mustela putorius -black-footed ferret, ferret, Mustela nigripes -otter -skunk, polecat, wood pussy -badger -armadillo -three-toed sloth, ai, Bradypus tridactylus -orangutan, orang, orangutang, Pongo pygmaeus -gorilla, Gorilla gorilla -chimpanzee, chimp, Pan troglodytes -gibbon, Hylobates lar -siamang, Hylobates syndactylus, Symphalangus syndactylus -guenon, guenon monkey -patas, hussar monkey, Erythrocebus patas -baboon -macaque -langur -colobus, colobus monkey -proboscis monkey, Nasalis larvatus -marmoset -capuchin, ringtail, Cebus capucinus -howler monkey, howler -titi, titi monkey -spider monkey, Ateles geoffroyi -squirrel monkey, Saimiri sciureus -Madagascar cat, ring-tailed lemur, Lemur catta -indri, indris, Indri indri, Indri brevicaudatus -Indian elephant, Elephas maximus -African elephant, Loxodonta africana -lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens -giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca -barracouta, snoek -eel -coho, cohoe, coho salmon, blue jack, silver salmon, Oncorhynchus kisutch -rock beauty, Holocanthus tricolor -anemone fish -sturgeon -gar, garfish, garpike, billfish, Lepisosteus osseus -lionfish -puffer, pufferfish, blowfish, globefish -abacus -abaya -academic gown, academic robe, judge's robe -accordion, piano accordion, squeeze box -acoustic guitar -aircraft carrier, carrier, flattop, attack aircraft carrier -airliner -airship, dirigible -altar -ambulance -amphibian, amphibious vehicle -analog clock -apiary, bee house -apron -ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, dustbin, trash barrel, trash bin -assault rifle, assault gun -backpack, back pack, knapsack, packsack, rucksack, haversack -bakery, bakeshop, bakehouse -balance beam, beam -balloon -ballpoint, ballpoint pen, ballpen, Biro -Band Aid -banjo -bannister, banister, balustrade, balusters, handrail -barbell -barber chair -barbershop -barn -barometer -barrel, cask -barrow, garden cart, lawn cart, wheelbarrow -baseball -basketball -bassinet -bassoon -bathing cap, swimming cap -bath towel -bathtub, bathing tub, bath, tub -beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon -beacon, lighthouse, beacon light, pharos -beaker -bearskin, busby, shako -beer bottle -beer glass -bell cote, bell cot -bib -bicycle-built-for-two, tandem bicycle, tandem -bikini, two-piece -binder, ring-binder -binoculars, field glasses, opera glasses -birdhouse -boathouse -bobsled, bobsleigh, bob -bolo tie, bolo, bola tie, bola -bonnet, poke bonnet -bookcase -bookshop, bookstore, bookstall -bottlecap -bow -bow tie, bow-tie, bowtie -brass, memorial tablet, plaque -brassiere, bra, bandeau -breakwater, groin, groyne, mole, bulwark, seawall, jetty -breastplate, aegis, egis -broom -bucket, pail -buckle -bulletproof vest -bullet train, bullet -butcher shop, meat market -cab, hack, taxi, taxicab -caldron, cauldron -candle, taper, wax light -cannon -canoe -can opener, tin opener -cardigan -car mirror -carousel, carrousel, merry-go-round, roundabout, whirligig -carpenter's kit, tool kit -carton -car wheel -cash machine, cash dispenser, automated teller machine, automatic teller machine, automated teller, automatic teller, ATM -cassette -cassette player -castle -catamaran -CD player -cello, violoncello -cellular telephone, cellular phone, cellphone, cell, mobile phone -chain -chainlink fence -chain mail, ring mail, mail, chain armor, chain armour, ring armor, ring armour -chain saw, chainsaw -chest -chiffonier, commode -chime, bell, gong -china cabinet, china closet -Christmas stocking -church, church building -cinema, movie theater, movie theatre, movie house, picture palace -cleaver, meat cleaver, chopper -cliff dwelling -cloak -clog, geta, patten, sabot -cocktail shaker -coffee mug -coffeepot -coil, spiral, volute, whorl, helix -combination lock -computer keyboard, keypad -confectionery, confectionary, candy store -container ship, containership, container vessel -convertible -corkscrew, bottle screw -cornet, horn, trumpet, trump -cowboy boot -cowboy hat, ten-gallon hat -cradle -crane -crash helmet -crate -crib, cot -Crock Pot -croquet ball -crutch -cuirass -dam, dike, dyke -desk -desktop computer -dial telephone, dial phone -diaper, nappy, napkin -digital clock -digital watch -dining table, board -dishrag, dishcloth -dishwasher, dish washer, dishwashing machine -disk brake, disc brake -dock, dockage, docking facility -dogsled, dog sled, dog sleigh -dome -doormat, welcome mat -drilling platform, offshore rig -drum, membranophone, tympan -drumstick -dumbbell -Dutch oven -electric fan, blower -electric guitar -electric locomotive -entertainment center -envelope -espresso maker -face powder -feather boa, boa -file, file cabinet, filing cabinet -fireboat -fire engine, fire truck -fire screen, fireguard -flagpole, flagstaff -flute, transverse flute -folding chair -football helmet -forklift -fountain -fountain pen -four-poster -freight car -French horn, horn -frying pan, frypan, skillet -fur coat -garbage truck, dustcart -gasmask, respirator, gas helmet -gas pump, gasoline pump, petrol pump, island dispenser -goblet -go-kart -golf ball -golfcart, golf cart -gondola -gong, tam-tam -gown -grand piano, grand -greenhouse, nursery, glasshouse -grille, radiator grille -grocery store, grocery, food market, market -guillotine -hair slide -hair spray -half track -hammer -hamper -hand blower, blow dryer, blow drier, hair dryer, hair drier -hand-held computer, hand-held microcomputer -handkerchief, hankie, hanky, hankey -hard disc, hard disk, fixed disk -harmonica, mouth organ, harp, mouth harp -harp -harvester, reaper -hatchet -holster -home theater, home theatre -honeycomb -hook, claw -hoopskirt, crinoline -horizontal bar, high bar -horse cart, horse-cart -hourglass -iPod -iron, smoothing iron -jack-o'-lantern -jean, blue jean, denim -jeep, landrover -jersey, T-shirt, tee shirt -jigsaw puzzle -jinrikisha, ricksha, rickshaw -joystick -kimono -knee pad -knot -lab coat, laboratory coat -ladle -lampshade, lamp shade -laptop, laptop computer -lawn mower, mower -lens cap, lens cover -letter opener, paper knife, paperknife -library -lifeboat -lighter, light, igniter, ignitor -limousine, limo -liner, ocean liner -lipstick, lip rouge -Loafer -lotion -loudspeaker, speaker, speaker unit, loudspeaker system, speaker system -loupe, jeweler's loupe -lumbermill, sawmill -magnetic compass -mailbag, postbag -mailbox, letter box -maillot -maillot, tank suit -manhole cover -maraca -marimba, xylophone -mask -matchstick -maypole -maze, labyrinth -measuring cup -medicine chest, medicine cabinet -megalith, megalithic structure -microphone, mike -microwave, microwave oven -military uniform -milk can -minibus -miniskirt, mini -minivan -missile -mitten -mixing bowl -mobile home, manufactured home -Model T -modem -monastery -monitor -moped -mortar -mortarboard -mosque -mosquito net -motor scooter, scooter -mountain bike, all-terrain bike, off-roader -mountain tent -mouse, computer mouse -mousetrap -moving van -muzzle -nail -neck brace -necklace -nipple -notebook, notebook computer -obelisk -oboe, hautboy, hautbois -ocarina, sweet potato -odometer, hodometer, mileometer, milometer -oil filter -organ, pipe organ -oscilloscope, scope, cathode-ray oscilloscope, CRO -overskirt -oxcart -oxygen mask -packet -paddle, boat paddle -paddlewheel, paddle wheel -padlock -paintbrush -pajama, pyjama, pj's, jammies -palace -panpipe, pandean pipe, syrinx -paper towel -parachute, chute -parallel bars, bars -park bench -parking meter -passenger car, coach, carriage -patio, terrace -pay-phone, pay-station -pedestal, plinth, footstall -pencil box, pencil case -pencil sharpener -perfume, essence -Petri dish -photocopier -pick, plectrum, plectron -pickelhaube -picket fence, paling -pickup, pickup truck -pier -piggy bank, penny bank -pill bottle -pillow -ping-pong ball -pinwheel -pirate, pirate ship -pitcher, ewer -plane, carpenter's plane, woodworking plane -planetarium -plastic bag -plate rack -plow, plough -plunger, plumber's helper -Polaroid camera, Polaroid Land camera -pole -police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria -poncho -pool table, billiard table, snooker table -pop bottle, soda bottle -pot, flowerpot -potter's wheel -power drill -prayer rug, prayer mat -printer -prison, prison house -projectile, missile -projector -puck, hockey puck -punching bag, punch bag, punching ball, punchball -purse -quill, quill pen -quilt, comforter, comfort, puff -racer, race car, racing car -racket, racquet -radiator -radio, wireless -radio telescope, radio reflector -rain barrel -recreational vehicle, RV, R.V. -reel -reflex camera -refrigerator, icebox -remote control, remote -restaurant, eating house, eating place, eatery -revolver, six-gun, six-shooter -rifle -rocking chair, rocker -rotisserie -rubber eraser, rubber, pencil eraser -rugby ball -rule, ruler -running shoe -safe -safety pin -saltshaker, salt shaker -sandal -sarong -sax, saxophone -scabbard -scale, weighing machine -school bus -schooner -scoreboard -screen, CRT screen -screw -screwdriver -seat belt, seatbelt -sewing machine -shield, buckler -shoe shop, shoe-shop, shoe store -shoji -shopping basket -shopping cart -shovel -shower cap -shower curtain -ski -ski mask -sleeping bag -slide rule, slipstick -sliding door -slot, one-armed bandit -snorkel -snowmobile -snowplow, snowplough -soap dispenser -soccer ball -sock -solar dish, solar collector, solar furnace -sombrero -soup bowl -space bar -space heater -space shuttle -spatula -speedboat -spider web, spider's web -spindle -sports car, sport car -spotlight, spot -stage -steam locomotive -steel arch bridge -steel drum -stethoscope -stole -stone wall -stopwatch, stop watch -stove -strainer -streetcar, tram, tramcar, trolley, trolley car -stretcher -studio couch, day bed -stupa, tope -submarine, pigboat, sub, U-boat -suit, suit of clothes -sundial -sunglass -sunglasses, dark glasses, shades -sunscreen, sunblock, sun blocker -suspension bridge -swab, swob, mop -sweatshirt -swimming trunks, bathing trunks -swing -switch, electric switch, electrical switch -syringe -table lamp -tank, army tank, armored combat vehicle, armoured combat vehicle -tape player -teapot -teddy, teddy bear -television, television system -tennis ball -thatch, thatched roof -theater curtain, theatre curtain -thimble -thresher, thrasher, threshing machine -throne -tile roof -toaster -tobacco shop, tobacconist shop, tobacconist -toilet seat -torch -totem pole -tow truck, tow car, wrecker -toyshop -tractor -trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi -tray -trench coat -tricycle, trike, velocipede -trimaran -tripod -triumphal arch -trolleybus, trolley coach, trackless trolley -trombone -tub, vat -turnstile -typewriter keyboard -umbrella -unicycle, monocycle -upright, upright piano -vacuum, vacuum cleaner -vase -vault -velvet -vending machine -vestment -viaduct -violin, fiddle -volleyball -waffle iron -wall clock -wallet, billfold, notecase, pocketbook -wardrobe, closet, press -warplane, military plane -washbasin, handbasin, washbowl, lavabo, wash-hand basin -washer, automatic washer, washing machine -water bottle -water jug -water tower -whiskey jug -whistle -wig -window screen -window shade -Windsor tie -wine bottle -wing -wok -wooden spoon -wool, woolen, woollen -worm fence, snake fence, snake-rail fence, Virginia fence -wreck -yawl -yurt -web site, website, internet site, site -comic book -crossword puzzle, crossword -street sign -traffic light, traffic signal, stoplight -book jacket, dust cover, dust jacket, dust wrapper -menu -plate -guacamole -consomme -hot pot, hotpot -trifle -ice cream, icecream -ice lolly, lolly, lollipop, popsicle -French loaf -bagel, beigel -pretzel -cheeseburger -hotdog, hot dog, red hot -mashed potato -head cabbage -broccoli -cauliflower -zucchini, courgette -spaghetti squash -acorn squash -butternut squash -cucumber, cuke -artichoke, globe artichoke -bell pepper -cardoon -mushroom -Granny Smith -strawberry -orange -lemon -fig -pineapple, ananas -banana -jackfruit, jak, jack -custard apple -pomegranate -hay -carbonara -chocolate sauce, chocolate syrup -dough -meat loaf, meatloaf -pizza, pizza pie -potpie -burrito -red wine -espresso -cup -eggnog -alp -bubble -cliff, drop, drop-off -coral reef -geyser -lakeside, lakeshore -promontory, headland, head, foreland -sandbar, sand bar -seashore, coast, seacoast, sea-coast -valley, vale -volcano -ballplayer, baseball player -groom, bridegroom -scuba diver -rapeseed -daisy -yellow lady's slipper, yellow lady-slipper, Cypripedium calceolus, Cypripedium parviflorum -corn -acorn -hip, rose hip, rosehip -buckeye, horse chestnut, conker -coral fungus -agaric -gyromitra -stinkhorn, carrion fungus -earthstar -hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa -bolete -ear, spike, capitulum -toilet tissue, toilet paper, bathroom tissue diff --git a/hub_module/modules/image/classification/resnet50_vd_10w/module.py b/hub_module/modules/image/classification/resnet50_vd_10w/module.py index 0ceebc64537a413b76aee651227ad6be4df7e6f4..d0ca0e0cba2e3c4fff4d6160c354920a3b9c25ab 100644 --- a/hub_module/modules/image/classification/resnet50_vd_10w/module.py +++ b/hub_module/modules/image/classification/resnet50_vd_10w/module.py @@ -1,128 +1,242 @@ -# coding=utf-8 -from __future__ import absolute_import -from __future__ import division - -import ast -import argparse +# copyright (c) 2020 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 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 resnet50_vd_10w.processor import postprocess, base64_to_cv2 -from resnet50_vd_10w.data_feed import reader -from resnet50_vd_10w.resnet_vd import ResNet50_vd - - -@moduleinfo( - name="resnet50_vd_10w", - type="CV/image_classification", - author="paddlepaddle", - author_email="paddle-dev@baidu.com", - summary= - "ResNet50vd is a image classfication model, this module is trained with Baidu's self-built dataset with 100,000 categories.", - version="1.0.0") -class ResNet50vd(hub.Module): - def _initialize(self): - self.default_pretrained_model_path = os.path.join( - self.directory, "model") - - def get_expected_image_width(self): - return 224 - - def get_expected_image_height(self): - return 224 - - def get_pretrained_images_mean(self): - im_mean = np.array([0.485, 0.456, 0.406]).reshape(1, 3) - return im_mean - - def get_pretrained_images_std(self): - im_std = np.array([0.229, 0.224, 0.225]).reshape(1, 3) - return im_std - - 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 '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() - feature_map = resnet_vd.net(input=image) - - name_prefix = '@HUB_{}@'.format(self.name) - inputs = {'image': name_prefix + image.name} - outputs = {'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 - - 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) +import paddle +from paddle import ParamAttr +import paddle.nn as nn +from paddle.nn import Conv2d, BatchNorm, Linear, Dropout +from paddle.nn import AdaptiveAvgPool2d, MaxPool2d, AvgPool2d +from paddle.nn.initializer import Uniform +from paddlehub.module.module import moduleinfo +from paddlehub.module.cv_module import ImageClassifierModule + + +class ConvBNLayer(nn.Layer): + """Basic conv bn layer.""" + def __init__( + self, + num_channels: int, + num_filters: int, + filter_size: int, + stride: int = 1, + groups: int = 1, + is_vd_mode: bool = False, + act: str = None, + name: str = None, + ): + super(ConvBNLayer, self).__init__() + + self.is_vd_mode = is_vd_mode + self._pool2d_avg = AvgPool2d(kernel_size=2, stride=2, padding=0, ceil_mode=True) + self._conv = Conv2d(in_channels=num_channels, + out_channels=num_filters, + kernel_size=filter_size, + stride=stride, + padding=(filter_size - 1) // 2, + groups=groups, + weight_attr=ParamAttr(name=name + "_weights"), + bias_attr=False) + if name == "conv1": + bn_name = "bn_" + name + else: + bn_name = "bn" + name[3:] + self._batch_norm = BatchNorm(num_filters, + act=act, + param_attr=ParamAttr(name=bn_name + '_scale'), + bias_attr=ParamAttr(bn_name + '_offset'), + moving_mean_name=bn_name + '_mean', + moving_variance_name=bn_name + '_variance') + + def forward(self, inputs: paddle.Tensor): + if self.is_vd_mode: + inputs = self._pool2d_avg(inputs) + y = self._conv(inputs) + y = self._batch_norm(y) + return y + + +class BottleneckBlock(nn.Layer): + """Bottleneck Block for ResNet50_vd.""" + def __init__(self, + num_channels: int, + num_filters: int, + stride: int, + shortcut: bool = True, + if_first: bool = False, + name: str = None): + super(BottleneckBlock, self).__init__() + + self.conv0 = ConvBNLayer(num_channels=num_channels, + num_filters=num_filters, + filter_size=1, + act='relu', + name=name + "_branch2a") + self.conv1 = ConvBNLayer(num_channels=num_filters, + num_filters=num_filters, + filter_size=3, + stride=stride, + act='relu', + name=name + "_branch2b") + self.conv2 = ConvBNLayer(num_channels=num_filters, + num_filters=num_filters * 4, + filter_size=1, + act=None, + name=name + "_branch2c") + + if not shortcut: + self.short = ConvBNLayer(num_channels=num_channels, + num_filters=num_filters * 4, + filter_size=1, + stride=1, + is_vd_mode=False if if_first else True, + name=name + "_branch1") + + self.shortcut = shortcut + + def forward(self, inputs: paddle.Tensor): + y = self.conv0(inputs) + conv1 = self.conv1(y) + conv2 = self.conv2(conv1) + + if self.shortcut: + short = inputs + else: + short = self.short(inputs) + y = paddle.elementwise_add(x=short, y=conv2, act='relu') + return y + + +class BasicBlock(nn.Layer): + """Basic block for ResNet50_vd.""" + def __init__(self, + num_channels: int, + num_filters: int, + stride: int, + shortcut: bool = True, + if_first: bool = False, + name: str = None): + super(BasicBlock, self).__init__() + self.stride = stride + self.conv0 = ConvBNLayer(num_channels=num_channels, + num_filters=num_filters, + filter_size=3, + stride=stride, + act='relu', + name=name + "_branch2a") + self.conv1 = ConvBNLayer(num_channels=num_filters, + num_filters=num_filters, + filter_size=3, + act=None, + name=name + "_branch2b") + + if not shortcut: + self.short = ConvBNLayer(num_channels=num_channels, + num_filters=num_filters, + filter_size=1, + stride=1, + is_vd_mode=False if if_first else True, + name=name + "_branch1") + + self.shortcut = shortcut + + def forward(self, inputs: paddle.Tensor): + y = self.conv0(inputs) + conv1 = self.conv1(y) + + if self.shortcut: + short = inputs + else: + short = self.short(inputs) + y = paddle.elementwise_add(x=short, y=conv1, act='relu') + return y + + +@moduleinfo(name="resnet50_vd_10w", + type="CV/classification", + author="paddlepaddle", + author_email="", + summary="resnet50_vd_imagenet_ssld is a classification model, " + "this module is trained with Baidu open sourced dataset.", + version="1.1.0", + meta=ImageClassifierModule) +class ResNet50_vd(nn.Layer): + """ResNet50_vd model.""" + def __init__(self, class_dim: int = 1000, load_checkpoint: str = None): + super(ResNet50_vd, self).__init__() + + self.layers = 50 + depth = [3, 4, 6, 3] + num_channels = [64, 256, 512, 1024] + num_filters = [64, 128, 256, 512] + + self.conv1_1 = ConvBNLayer(num_channels=3, num_filters=32, filter_size=3, stride=2, act='relu', name="conv1_1") + self.conv1_2 = ConvBNLayer(num_channels=32, num_filters=32, filter_size=3, stride=1, act='relu', name="conv1_2") + self.conv1_3 = ConvBNLayer(num_channels=32, num_filters=64, filter_size=3, stride=1, act='relu', name="conv1_3") + self.pool2d_max = MaxPool2d(kernel_size=3, stride=2, padding=1) + + self.block_list = [] + + for block in range(len(depth)): + shortcut = False + for i in range(depth[block]): + conv_name = "res" + str(block + 2) + chr(97 + i) + bottleneck_block = self.add_sublayer( + 'bb_%d_%d' % (block, i), + BottleneckBlock(num_channels=num_channels[block] if i == 0 else num_filters[block] * 4, + num_filters=num_filters[block], + stride=2 if i == 0 and block != 0 else 1, + shortcut=shortcut, + if_first=block == i == 0, + name=conv_name)) + self.block_list.append(bottleneck_block) + shortcut = True + + self.pool2d_avg = AdaptiveAvgPool2d(1) + self.pool2d_avg_channels = num_channels[-1] * 2 + stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0) + + self.out = Linear(self.pool2d_avg_channels, + class_dim, + weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv), name="fc_0.w_0"), + bias_attr=ParamAttr(name="fc_0.b_0")) + + if load_checkpoint is not None: + model_dict = paddle.load(load_checkpoint)[0] + self.set_dict(model_dict) + print("load custom checkpoint success") + + else: + checkpoint = os.path.join(self.directory, 'resnet50_vd_10w.pdparams') + if not os.path.exists(checkpoint): + os.system( + 'wget https://paddlehub.bj.bcebos.com/dygraph/image_classification/resnet50_vd_10w.pdparams -O ' + + checkpoint) + model_dict = paddle.load(checkpoint)[0] + self.set_dict(model_dict) + print("load pretrained checkpoint success") + + def forward(self, inputs: paddle.Tensor): + y = self.conv1_1(inputs) + y = self.conv1_2(y) + y = self.conv1_3(y) + y = self.pool2d_max(y) + for block in self.block_list: + y = block(y) + y = self.pool2d_avg(y) + y = paddle.reshape(y, shape=[-1, self.pool2d_avg_channels]) + y = self.out(y) + return y diff --git a/hub_module/modules/image/classification/resnet50_vd_10w/processor.py b/hub_module/modules/image/classification/resnet50_vd_10w/processor.py deleted file mode 100644 index fa8cbb502312e6ef80697ab63b767d4077b3847b..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/resnet50_vd_10w/processor.py +++ /dev/null @@ -1,55 +0,0 @@ -# coding=utf-8 -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import base64 -import cv2 -import os - -import numpy as np - - -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 - - -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)) - x = np.exp(x) - tmp = np.sum(x, axis=1) - x /= tmp.reshape((x.shape[0], 1)) - else: - tmp = np.max(x) - x -= tmp - x = np.exp(x) - tmp = np.sum(x) - x /= tmp - return x - - -def postprocess(data_out, label_list, top_k): - """ - Postprocess output of network, one image at a time. - - Args: - data_out (numpy.ndarray): output data of network. - label_list (list): list of label. - top_k (int): Return top k results. - """ - output = [] - for result in data_out: - result_i = softmax(result) - output_i = {} - indexs = np.argsort(result_i)[::-1][0:top_k] - for index in indexs: - label = label_list[index].split(',')[0] - output_i[label] = float(result_i[index]) - output.append(output_i) - return output diff --git a/hub_module/modules/image/classification/resnet50_vd_10w/resnet_vd.py b/hub_module/modules/image/classification/resnet50_vd_10w/resnet_vd.py deleted file mode 100755 index 643ab40313677f11e33dc6997c8abeb595682083..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/resnet50_vd_10w/resnet_vd.py +++ /dev/null @@ -1,249 +0,0 @@ -#copyright (c) 2019 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. - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import math - -import paddle -import paddle.fluid as fluid -from paddle.fluid.param_attr import ParamAttr - -__all__ = [ - "ResNet", "ResNet50_vd", "ResNet101_vd", "ResNet152_vd", "ResNet200_vd" -] - -train_parameters = { - "input_size": [3, 224, 224], - "input_mean": [0.485, 0.456, 0.406], - "input_std": [0.229, 0.224, 0.225], - "learning_strategy": { - "name": "piecewise_decay", - "batch_size": 256, - "epochs": [30, 60, 90], - "steps": [0.1, 0.01, 0.001, 0.0001] - } -} - - -class ResNet(): - def __init__(self, layers=50, is_3x3=False): - self.params = train_parameters - self.layers = layers - self.is_3x3 = is_3x3 - - def net(self, input): - is_3x3 = self.is_3x3 - layers = self.layers - supported_layers = [50, 101, 152, 200] - assert layers in supported_layers, \ - "supported layers are {} but input layer is {}".format(supported_layers, layers) - - if layers == 50: - depth = [3, 4, 6, 3] - elif layers == 101: - depth = [3, 4, 23, 3] - elif layers == 152: - depth = [3, 8, 36, 3] - elif layers == 200: - depth = [3, 12, 48, 3] - num_filters = [64, 128, 256, 512] - if is_3x3 == False: - conv = self.conv_bn_layer( - input=input, - num_filters=64, - filter_size=7, - stride=2, - act='relu') - else: - conv = self.conv_bn_layer( - input=input, - num_filters=32, - filter_size=3, - stride=2, - act='relu', - name='conv1_1') - conv = self.conv_bn_layer( - input=conv, - num_filters=32, - filter_size=3, - stride=1, - act='relu', - name='conv1_2') - conv = self.conv_bn_layer( - input=conv, - num_filters=64, - filter_size=3, - stride=1, - act='relu', - name='conv1_3') - - conv = fluid.layers.pool2d( - input=conv, - pool_size=3, - pool_stride=2, - pool_padding=1, - pool_type='max') - - for block in range(len(depth)): - for i in range(depth[block]): - if layers in [101, 152, 200] and block == 2: - if i == 0: - conv_name = "res" + str(block + 2) + "a" - else: - conv_name = "res" + str(block + 2) + "b" + str(i) - else: - conv_name = "res" + str(block + 2) + chr(97 + i) - conv = self.bottleneck_block( - input=conv, - num_filters=num_filters[block], - stride=2 if i == 0 and block != 0 else 1, - if_first=block == 0, - name=conv_name) - - pool = fluid.layers.pool2d( - input=conv, pool_size=7, pool_type='avg', global_pooling=True) - stdv = 1.0 / math.sqrt(pool.shape[1] * 1.0) - - return pool - - def conv_bn_layer(self, - input, - num_filters, - filter_size, - stride=1, - groups=1, - act=None, - name=None): - conv = fluid.layers.conv2d( - input=input, - num_filters=num_filters, - filter_size=filter_size, - stride=stride, - padding=(filter_size - 1) // 2, - groups=groups, - act=None, - param_attr=ParamAttr(name=name + "_weights"), - bias_attr=False) - if name == "conv1": - bn_name = "bn_" + name - else: - bn_name = "bn" + name[3:] - return fluid.layers.batch_norm( - input=conv, - act=act, - param_attr=ParamAttr(name=bn_name + '_scale'), - bias_attr=ParamAttr(bn_name + '_offset'), - moving_mean_name=bn_name + '_mean', - moving_variance_name=bn_name + '_variance') - - def conv_bn_layer_new(self, - input, - num_filters, - filter_size, - stride=1, - groups=1, - act=None, - name=None): - pool = fluid.layers.pool2d( - input=input, - pool_size=2, - pool_stride=2, - pool_padding=0, - pool_type='avg') - - conv = fluid.layers.conv2d( - input=pool, - num_filters=num_filters, - filter_size=filter_size, - stride=1, - padding=(filter_size - 1) // 2, - groups=groups, - act=None, - param_attr=ParamAttr(name=name + "_weights"), - bias_attr=False) - if name == "conv1": - bn_name = "bn_" + name - else: - bn_name = "bn" + name[3:] - return fluid.layers.batch_norm( - input=conv, - act=act, - param_attr=ParamAttr(name=bn_name + '_scale'), - bias_attr=ParamAttr(bn_name + '_offset'), - moving_mean_name=bn_name + '_mean', - moving_variance_name=bn_name + '_variance') - - def shortcut(self, input, ch_out, stride, name, if_first=False): - ch_in = input.shape[1] - if ch_in != ch_out or stride != 1: - if if_first: - return self.conv_bn_layer(input, ch_out, 1, stride, name=name) - else: - return self.conv_bn_layer_new( - input, ch_out, 1, stride, name=name) - else: - return input - - def bottleneck_block(self, input, num_filters, stride, name, if_first): - conv0 = self.conv_bn_layer( - input=input, - num_filters=num_filters, - filter_size=1, - act='relu', - name=name + "_branch2a") - conv1 = self.conv_bn_layer( - input=conv0, - num_filters=num_filters, - filter_size=3, - stride=stride, - act='relu', - name=name + "_branch2b") - conv2 = self.conv_bn_layer( - input=conv1, - num_filters=num_filters * 4, - filter_size=1, - act=None, - name=name + "_branch2c") - - short = self.shortcut( - input, - num_filters * 4, - stride, - if_first=if_first, - name=name + "_branch1") - - return fluid.layers.elementwise_add(x=short, y=conv2, act='relu') - - -def ResNet50_vd(): - model = ResNet(layers=50, is_3x3=True) - return model - - -def ResNet101_vd(): - model = ResNet(layers=101, is_3x3=True) - return model - - -def ResNet152_vd(): - model = ResNet(layers=152, is_3x3=True) - return model - - -def ResNet200_vd(): - model = ResNet(layers=200, is_3x3=True) - return model diff --git a/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/README.md b/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/README.md deleted file mode 100644 index a655d41792ff0f387c36afe5c3a5a8707e3617ac..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/README.md +++ /dev/null @@ -1,159 +0,0 @@ -```shell -$ hub install resnet50_vd_imagenet_ssld==1.0.0 -``` - -

-
ResNet 系列的网络结构 -

- -模型的详情可参考[论文](https://arxiv.org/pdf/1812.01187.pdf) - -## 命令行预测 - -``` -hub run resnet50_vd_imagenet_ssld --input_path "/PATH/TO/IMAGE" -``` - -## API - -```python -def get_expected_image_width() -``` - -返回预处理的图片宽度,也就是224。 - -```python -def get_expected_image_height() -``` - -返回预处理的图片高度,也就是224。 - -```python -def get_pretrained_images_mean() -``` - -返回预处理的图片均值,也就是 \[0.485, 0.456, 0.406\]。 - -```python -def get_pretrained_images_std() -``` - -返回预处理的图片标准差,也就是 \[0.229, 0.224, 0.225\]。 - - -```python -def context(trainable=True, pretrained=True) -``` - -**参数** - -* trainable (bool): 计算图的参数是否为可训练的; -* pretrained (bool): 是否加载默认的预训练模型。 - -**返回** - -* inputs (dict): 计算图的输入,key 为 'image', value 为图片的张量; -* outputs (dict): 计算图的输出,key 为 'classification' 和 'feature_map',其相应的值为: - * classification (paddle.fluid.framework.Variable): 分类结果,也就是全连接层的输出; - * feature\_map (paddle.fluid.framework.Variable): 特征匹配,全连接层前面的那个张量。 -* context\_prog(fluid.Program): 计算图,用于迁移学习。 - -```python -def classification(images=None, - paths=None, - batch_size=1, - use_gpu=False, - top_k=1): -``` - -**参数** - -* images (list\[numpy.ndarray\]): 图片数据,每一个图片数据的shape 均为 \[H, W, C\],颜色空间为 BGR; -* paths (list\[str\]): 图片的路径; -* batch\_size (int): batch 的大小; -* use\_gpu (bool): 是否使用 GPU 来预测; -* top\_k (int): 返回预测结果的前 k 个。 - -**返回** - -res (list\[dict\]): 分类结果,列表的每一个元素均为字典,其中 key 为识别动物的类别,value为置信度。 - -```python -def save_inference_model(dirname, - model_filename=None, - params_filename=None, - combined=True) -``` - -将模型保存到指定路径。 - -**参数** - -* dirname: 存在模型的目录名称 -* model_filename: 模型文件名称,默认为\_\_model\_\_ -* params_filename: 参数文件名称,默认为\_\_params\_\_(仅当`combined`为True时生效) -* combined: 是否将参数保存到统一的一个文件中 - -## 代码示例 - -```python -import paddlehub as hub -import cv2 - -classifier = hub.Module(name="resnet50_vd_imagenet_ssld") - -result = classifier.classification(images=[cv2.imread('/PATH/TO/IMAGE')]) -# or -# result = classifier.classification(paths=['/PATH/TO/IMAGE']) -``` - -## 服务部署 - -PaddleHub Serving可以部署一个在线动物识别服务。 - -## 第一步:启动PaddleHub Serving - -运行启动命令: -```shell -$ hub serving start -m resnet50_vd_imagenet_ssld -``` - -这样就完成了一个在线动物识别服务化API的部署,默认端口号为8866。 - -**NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA\_VISIBLE\_DEVICES环境变量,否则不用设置。 - -## 第二步:发送预测请求 - -配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果 - -```python -import requests -import json -import cv2 -import base64 - - -def cv2_to_base64(image): - data = cv2.imencode('.jpg', image)[1] - return base64.b64encode(data.tostring()).decode('utf8') - - -# 发送HTTP请求 -data = {'images':[cv2_to_base64(cv2.imread("/PATH/TO/IMAGE"))]} -headers = {"Content-type": "application/json"} -url = "http://127.0.0.1:8866/predict/resnet50_vd_imagenet_ssld" -r = requests.post(url=url, headers=headers, data=json.dumps(data)) - -# 打印预测结果 -print(r.json()["results"]) -``` - -### 查看代码 - -[PaddleClas](https://github.com/PaddlePaddle/PaddleClas) - -### 依赖 - -paddlepaddle >= 1.6.2 - -paddlehub >= 1.6.0 diff --git a/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/__init__.py b/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/__init__.py deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/data_feed.py b/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/data_feed.py deleted file mode 100644 index d5ffb5efe9fdfbd143b949892aa44d851e907b41..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/data_feed.py +++ /dev/null @@ -1,84 +0,0 @@ -# coding=utf-8 -import os -import time -from collections import OrderedDict - -import cv2 -import numpy as np -from PIL import Image - -__all__ = ['reader'] - -DATA_DIM = 224 -img_mean = np.array([0.485, 0.456, 0.406]).reshape((3, 1, 1)) -img_std = np.array([0.229, 0.224, 0.225]).reshape((3, 1, 1)) - - -def resize_short(img, target_size): - percent = float(target_size) / min(img.size[0], img.size[1]) - resized_width = int(round(img.size[0] * percent)) - resized_height = int(round(img.size[1] * percent)) - img = img.resize((resized_width, resized_height), Image.LANCZOS) - return img - - -def crop_image(img, target_size, center): - width, height = img.size - size = target_size - if center == True: - w_start = (width - size) / 2 - h_start = (height - size) / 2 - else: - w_start = np.random.randint(0, width - size + 1) - h_start = np.random.randint(0, height - size + 1) - w_end = w_start + size - h_end = h_start + size - img = img.crop((w_start, h_start, w_end, h_end)) - return img - - -def process_image(img): - img = resize_short(img, target_size=256) - img = crop_image(img, target_size=DATA_DIM, center=True) - if img.mode != 'RGB': - img = img.convert('RGB') - img = np.array(img).astype('float32').transpose((2, 0, 1)) / 255 - img -= img_mean - img /= img_std - return img - - -def reader(images=None, paths=None): - """ - Preprocess to yield image. - - Args: - images (list[numpy.ndarray]): images data, shape of each is [H, W, C]. - paths (list[str]): paths to images. - - Yield: - each (collections.OrderedDict): info of original image, preprocessed image. - """ - component = list() - if paths: - for im_path in paths: - each = OrderedDict() - assert os.path.isfile( - im_path), "The {} isn't a valid file path.".format(im_path) - each['org_im_path'] = im_path - each['org_im'] = Image.open(im_path) - each['org_im_width'], each['org_im_height'] = each['org_im'].size - component.append(each) - if images is not None: - assert type(images), "images is a list." - for im in images: - each = OrderedDict() - each['org_im'] = Image.fromarray(im[:, :, ::-1]) - each['org_im_path'] = 'ndarray_time={}'.format( - round(time.time(), 6) * 1e6) - each['org_im_width'], each['org_im_height'] = each['org_im'].size - component.append(each) - - for element in component: - element['image'] = process_image(element['org_im']) - yield element diff --git a/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/label_list.txt b/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/label_list.txt deleted file mode 100644 index a509c007481d301e524e7b3c97561132dbfcc765..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/label_list.txt +++ /dev/null @@ -1,1000 +0,0 @@ -tench, Tinca tinca -goldfish, Carassius auratus -great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias -tiger shark, Galeocerdo cuvieri -hammerhead, hammerhead shark -electric ray, crampfish, numbfish, torpedo -stingray -cock -hen -ostrich, Struthio camelus -brambling, Fringilla montifringilla -goldfinch, Carduelis carduelis -house finch, linnet, Carpodacus mexicanus -junco, snowbird -indigo bunting, indigo finch, indigo bird, Passerina cyanea -robin, American robin, Turdus migratorius -bulbul -jay -magpie -chickadee -water ouzel, dipper -kite -bald eagle, American eagle, Haliaeetus leucocephalus -vulture -great grey owl, great gray owl, Strix nebulosa -European fire salamander, Salamandra salamandra -common newt, Triturus vulgaris -eft -spotted salamander, Ambystoma maculatum -axolotl, mud puppy, Ambystoma mexicanum -bullfrog, Rana catesbeiana -tree frog, tree-frog -tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui -loggerhead, loggerhead turtle, Caretta caretta -leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea -mud turtle -terrapin -box turtle, box tortoise -banded gecko -common iguana, iguana, Iguana iguana -American chameleon, anole, Anolis carolinensis -whiptail, whiptail lizard -agama -frilled lizard, Chlamydosaurus kingi -alligator lizard -Gila monster, Heloderma suspectum -green lizard, Lacerta viridis -African chameleon, Chamaeleo chamaeleon -Komodo dragon, Komodo lizard, dragon lizard, giant lizard, Varanus komodoensis -African crocodile, Nile crocodile, Crocodylus niloticus -American alligator, Alligator mississipiensis -triceratops -thunder snake, worm snake, Carphophis amoenus -ringneck snake, ring-necked snake, ring snake -hognose snake, puff adder, sand viper -green snake, grass snake -king snake, kingsnake -garter snake, grass snake -water snake -vine snake -night snake, Hypsiglena torquata -boa constrictor, Constrictor constrictor -rock python, rock snake, Python sebae -Indian cobra, Naja naja -green mamba -sea snake -horned viper, cerastes, sand viper, horned asp, Cerastes cornutus -diamondback, diamondback rattlesnake, Crotalus adamanteus -sidewinder, horned rattlesnake, Crotalus cerastes -trilobite -harvestman, daddy longlegs, Phalangium opilio -scorpion -black and gold garden spider, Argiope aurantia -barn spider, Araneus cavaticus -garden spider, Aranea diademata -black widow, Latrodectus mactans -tarantula -wolf spider, hunting spider -tick -centipede -black grouse -ptarmigan -ruffed grouse, partridge, Bonasa umbellus -prairie chicken, prairie grouse, prairie fowl -peacock -quail -partridge -African grey, African gray, Psittacus erithacus -macaw -sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita -lorikeet -coucal -bee eater -hornbill -hummingbird -jacamar -toucan -drake -red-breasted merganser, Mergus serrator -goose -black swan, Cygnus atratus -tusker -echidna, spiny anteater, anteater -platypus, duckbill, duckbilled platypus, duck-billed platypus, Ornithorhynchus anatinus -wallaby, brush kangaroo -koala, koala bear, kangaroo bear, native bear, Phascolarctos cinereus -wombat -jellyfish -sea anemone, anemone -brain coral -flatworm, platyhelminth -nematode, nematode worm, roundworm -conch -snail -slug -sea slug, nudibranch -chiton, coat-of-mail shell, sea cradle, polyplacophore -chambered nautilus, pearly nautilus, nautilus -Dungeness crab, Cancer magister -rock crab, Cancer irroratus -fiddler crab -king crab, Alaska crab, Alaskan king crab, Alaska king crab, Paralithodes camtschatica -American lobster, Northern lobster, Maine lobster, Homarus americanus -spiny lobster, langouste, rock lobster, crawfish, crayfish, sea crawfish -crayfish, crawfish, crawdad, crawdaddy -hermit crab -isopod -white stork, Ciconia ciconia -black stork, Ciconia nigra -spoonbill -flamingo -little blue heron, Egretta caerulea -American egret, great white heron, Egretta albus -bittern -crane -limpkin, Aramus pictus -European gallinule, Porphyrio porphyrio -American coot, marsh hen, mud hen, water hen, Fulica americana -bustard -ruddy turnstone, Arenaria interpres -red-backed sandpiper, dunlin, Erolia alpina -redshank, Tringa totanus -dowitcher -oystercatcher, oyster catcher -pelican -king penguin, Aptenodytes patagonica -albatross, mollymawk -grey whale, gray whale, devilfish, Eschrichtius gibbosus, Eschrichtius robustus -killer whale, killer, orca, grampus, sea wolf, Orcinus orca -dugong, Dugong dugon -sea lion -Chihuahua -Japanese spaniel -Maltese dog, Maltese terrier, Maltese -Pekinese, Pekingese, Peke -Shih-Tzu -Blenheim spaniel -papillon -toy terrier -Rhodesian ridgeback -Afghan hound, Afghan -basset, basset hound -beagle -bloodhound, sleuthhound -bluetick -black-and-tan coonhound -Walker hound, Walker foxhound -English foxhound -redbone -borzoi, Russian wolfhound -Irish wolfhound -Italian greyhound -whippet -Ibizan hound, Ibizan Podenco -Norwegian elkhound, elkhound -otterhound, otter hound -Saluki, gazelle hound -Scottish deerhound, deerhound -Weimaraner -Staffordshire bullterrier, Staffordshire bull terrier -American Staffordshire terrier, Staffordshire terrier, American pit bull terrier, pit bull terrier -Bedlington terrier -Border terrier -Kerry blue terrier -Irish terrier -Norfolk terrier -Norwich terrier -Yorkshire terrier -wire-haired fox terrier -Lakeland terrier -Sealyham terrier, Sealyham -Airedale, Airedale terrier -cairn, cairn terrier -Australian terrier -Dandie Dinmont, Dandie Dinmont terrier -Boston bull, Boston terrier -miniature schnauzer -giant schnauzer -standard schnauzer -Scotch terrier, Scottish terrier, Scottie -Tibetan terrier, chrysanthemum dog -silky terrier, Sydney silky -soft-coated wheaten terrier -West Highland white terrier -Lhasa, Lhasa apso -flat-coated retriever -curly-coated retriever -golden retriever -Labrador retriever -Chesapeake Bay retriever -German short-haired pointer -vizsla, Hungarian pointer -English setter -Irish setter, red setter -Gordon setter -Brittany spaniel -clumber, clumber spaniel -English springer, English springer spaniel -Welsh springer spaniel -cocker spaniel, English cocker spaniel, cocker -Sussex spaniel -Irish water spaniel -kuvasz -schipperke -groenendael -malinois -briard -kelpie -komondor -Old English sheepdog, bobtail -Shetland sheepdog, Shetland sheep dog, Shetland -collie -Border collie -Bouvier des Flandres, Bouviers des Flandres -Rottweiler -German shepherd, German shepherd dog, German police dog, alsatian -Doberman, Doberman pinscher -miniature pinscher -Greater Swiss Mountain dog -Bernese mountain dog -Appenzeller -EntleBucher -boxer -bull mastiff -Tibetan mastiff -French bulldog -Great Dane -Saint Bernard, St Bernard -Eskimo dog, husky -malamute, malemute, Alaskan malamute -Siberian husky -dalmatian, coach dog, carriage dog -affenpinscher, monkey pinscher, monkey dog -basenji -pug, pug-dog -Leonberg -Newfoundland, Newfoundland dog -Great Pyrenees -Samoyed, Samoyede -Pomeranian -chow, chow chow -keeshond -Brabancon griffon -Pembroke, Pembroke Welsh corgi -Cardigan, Cardigan Welsh corgi -toy poodle -miniature poodle -standard poodle -Mexican hairless -timber wolf, grey wolf, gray wolf, Canis lupus -white wolf, Arctic wolf, Canis lupus tundrarum -red wolf, maned wolf, Canis rufus, Canis niger -coyote, prairie wolf, brush wolf, Canis latrans -dingo, warrigal, warragal, Canis dingo -dhole, Cuon alpinus -African hunting dog, hyena dog, Cape hunting dog, Lycaon pictus -hyena, hyaena -red fox, Vulpes vulpes -kit fox, Vulpes macrotis -Arctic fox, white fox, Alopex lagopus -grey fox, gray fox, Urocyon cinereoargenteus -tabby, tabby cat -tiger cat -Persian cat -Siamese cat, Siamese -Egyptian cat -cougar, puma, catamount, mountain lion, painter, panther, Felis concolor -lynx, catamount -leopard, Panthera pardus -snow leopard, ounce, Panthera uncia -jaguar, panther, Panthera onca, Felis onca -lion, king of beasts, Panthera leo -tiger, Panthera tigris -cheetah, chetah, Acinonyx jubatus -brown bear, bruin, Ursus arctos -American black bear, black bear, Ursus americanus, Euarctos americanus -ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus -sloth bear, Melursus ursinus, Ursus ursinus -mongoose -meerkat, mierkat -tiger beetle -ladybug, ladybeetle, lady beetle, ladybird, ladybird beetle -ground beetle, carabid beetle -long-horned beetle, longicorn, longicorn beetle -leaf beetle, chrysomelid -dung beetle -rhinoceros beetle -weevil -fly -bee -ant, emmet, pismire -grasshopper, hopper -cricket -walking stick, walkingstick, stick insect -cockroach, roach -mantis, mantid -cicada, cicala -leafhopper -lacewing, lacewing fly -dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk -damselfly -admiral -ringlet, ringlet butterfly -monarch, monarch butterfly, milkweed butterfly, Danaus plexippus -cabbage butterfly -sulphur butterfly, sulfur butterfly -lycaenid, lycaenid butterfly -starfish, sea star -sea urchin -sea cucumber, holothurian -wood rabbit, cottontail, cottontail rabbit -hare -Angora, Angora rabbit -hamster -porcupine, hedgehog -fox squirrel, eastern fox squirrel, Sciurus niger -marmot -beaver -guinea pig, Cavia cobaya -sorrel -zebra -hog, pig, grunter, squealer, Sus scrofa -wild boar, boar, Sus scrofa -warthog -hippopotamus, hippo, river horse, Hippopotamus amphibius -ox -water buffalo, water ox, Asiatic buffalo, Bubalus bubalis -bison -ram, tup -bighorn, bighorn sheep, cimarron, Rocky Mountain bighorn, Rocky Mountain sheep, Ovis canadensis -ibex, Capra ibex -hartebeest -impala, Aepyceros melampus -gazelle -Arabian camel, dromedary, Camelus dromedarius -llama -weasel -mink -polecat, fitch, foulmart, foumart, Mustela putorius -black-footed ferret, ferret, Mustela nigripes -otter -skunk, polecat, wood pussy -badger -armadillo -three-toed sloth, ai, Bradypus tridactylus -orangutan, orang, orangutang, Pongo pygmaeus -gorilla, Gorilla gorilla -chimpanzee, chimp, Pan troglodytes -gibbon, Hylobates lar -siamang, Hylobates syndactylus, Symphalangus syndactylus -guenon, guenon monkey -patas, hussar monkey, Erythrocebus patas -baboon -macaque -langur -colobus, colobus monkey -proboscis monkey, Nasalis larvatus -marmoset -capuchin, ringtail, Cebus capucinus -howler monkey, howler -titi, titi monkey -spider monkey, Ateles geoffroyi -squirrel monkey, Saimiri sciureus -Madagascar cat, ring-tailed lemur, Lemur catta -indri, indris, Indri indri, Indri brevicaudatus -Indian elephant, Elephas maximus -African elephant, Loxodonta africana -lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens -giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca -barracouta, snoek -eel -coho, cohoe, coho salmon, blue jack, silver salmon, Oncorhynchus kisutch -rock beauty, Holocanthus tricolor -anemone fish -sturgeon -gar, garfish, garpike, billfish, Lepisosteus osseus -lionfish -puffer, pufferfish, blowfish, globefish -abacus -abaya -academic gown, academic robe, judge's robe -accordion, piano accordion, squeeze box -acoustic guitar -aircraft carrier, carrier, flattop, attack aircraft carrier -airliner -airship, dirigible -altar -ambulance -amphibian, amphibious vehicle -analog clock -apiary, bee house -apron -ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, dustbin, trash barrel, trash bin -assault rifle, assault gun -backpack, back pack, knapsack, packsack, rucksack, haversack -bakery, bakeshop, bakehouse -balance beam, beam -balloon -ballpoint, ballpoint pen, ballpen, Biro -Band Aid -banjo -bannister, banister, balustrade, balusters, handrail -barbell -barber chair -barbershop -barn -barometer -barrel, cask -barrow, garden cart, lawn cart, wheelbarrow -baseball -basketball -bassinet -bassoon -bathing cap, swimming cap -bath towel -bathtub, bathing tub, bath, tub -beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon -beacon, lighthouse, beacon light, pharos -beaker -bearskin, busby, shako -beer bottle -beer glass -bell cote, bell cot -bib -bicycle-built-for-two, tandem bicycle, tandem -bikini, two-piece -binder, ring-binder -binoculars, field glasses, opera glasses -birdhouse -boathouse -bobsled, bobsleigh, bob -bolo tie, bolo, bola tie, bola -bonnet, poke bonnet -bookcase -bookshop, bookstore, bookstall -bottlecap -bow -bow tie, bow-tie, bowtie -brass, memorial tablet, plaque -brassiere, bra, bandeau -breakwater, groin, groyne, mole, bulwark, seawall, jetty -breastplate, aegis, egis -broom -bucket, pail -buckle -bulletproof vest -bullet train, bullet -butcher shop, meat market -cab, hack, taxi, taxicab -caldron, cauldron -candle, taper, wax light -cannon -canoe -can opener, tin opener -cardigan -car mirror -carousel, carrousel, merry-go-round, roundabout, whirligig -carpenter's kit, tool kit -carton -car wheel -cash machine, cash dispenser, automated teller machine, automatic teller machine, automated teller, automatic teller, ATM -cassette -cassette player -castle -catamaran -CD player -cello, violoncello -cellular telephone, cellular phone, cellphone, cell, mobile phone -chain -chainlink fence -chain mail, ring mail, mail, chain armor, chain armour, ring armor, ring armour -chain saw, chainsaw -chest -chiffonier, commode -chime, bell, gong -china cabinet, china closet -Christmas stocking -church, church building -cinema, movie theater, movie theatre, movie house, picture palace -cleaver, meat cleaver, chopper -cliff dwelling -cloak -clog, geta, patten, sabot -cocktail shaker -coffee mug -coffeepot -coil, spiral, volute, whorl, helix -combination lock -computer keyboard, keypad -confectionery, confectionary, candy store -container ship, containership, container vessel -convertible -corkscrew, bottle screw -cornet, horn, trumpet, trump -cowboy boot -cowboy hat, ten-gallon hat -cradle -crane -crash helmet -crate -crib, cot -Crock Pot -croquet ball -crutch -cuirass -dam, dike, dyke -desk -desktop computer -dial telephone, dial phone -diaper, nappy, napkin -digital clock -digital watch -dining table, board -dishrag, dishcloth -dishwasher, dish washer, dishwashing machine -disk brake, disc brake -dock, dockage, docking facility -dogsled, dog sled, dog sleigh -dome -doormat, welcome mat -drilling platform, offshore rig -drum, membranophone, tympan -drumstick -dumbbell -Dutch oven -electric fan, blower -electric guitar -electric locomotive -entertainment center -envelope -espresso maker -face powder -feather boa, boa -file, file cabinet, filing cabinet -fireboat -fire engine, fire truck -fire screen, fireguard -flagpole, flagstaff -flute, transverse flute -folding chair -football helmet -forklift -fountain -fountain pen -four-poster -freight car -French horn, horn -frying pan, frypan, skillet -fur coat -garbage truck, dustcart -gasmask, respirator, gas helmet -gas pump, gasoline pump, petrol pump, island dispenser -goblet -go-kart -golf ball -golfcart, golf cart -gondola -gong, tam-tam -gown -grand piano, grand -greenhouse, nursery, glasshouse -grille, radiator grille -grocery store, grocery, food market, market -guillotine -hair slide -hair spray -half track -hammer -hamper -hand blower, blow dryer, blow drier, hair dryer, hair drier -hand-held computer, hand-held microcomputer -handkerchief, hankie, hanky, hankey -hard disc, hard disk, fixed disk -harmonica, mouth organ, harp, mouth harp -harp -harvester, reaper -hatchet -holster -home theater, home theatre -honeycomb -hook, claw -hoopskirt, crinoline -horizontal bar, high bar -horse cart, horse-cart -hourglass -iPod -iron, smoothing iron -jack-o'-lantern -jean, blue jean, denim -jeep, landrover -jersey, T-shirt, tee shirt -jigsaw puzzle -jinrikisha, ricksha, rickshaw -joystick -kimono -knee pad -knot -lab coat, laboratory coat -ladle -lampshade, lamp shade -laptop, laptop computer -lawn mower, mower -lens cap, lens cover -letter opener, paper knife, paperknife -library -lifeboat -lighter, light, igniter, ignitor -limousine, limo -liner, ocean liner -lipstick, lip rouge -Loafer -lotion -loudspeaker, speaker, speaker unit, loudspeaker system, speaker system -loupe, jeweler's loupe -lumbermill, sawmill -magnetic compass -mailbag, postbag -mailbox, letter box -maillot -maillot, tank suit -manhole cover -maraca -marimba, xylophone -mask -matchstick -maypole -maze, labyrinth -measuring cup -medicine chest, medicine cabinet -megalith, megalithic structure -microphone, mike -microwave, microwave oven -military uniform -milk can -minibus -miniskirt, mini -minivan -missile -mitten -mixing bowl -mobile home, manufactured home -Model T -modem -monastery -monitor -moped -mortar -mortarboard -mosque -mosquito net -motor scooter, scooter -mountain bike, all-terrain bike, off-roader -mountain tent -mouse, computer mouse -mousetrap -moving van -muzzle -nail -neck brace -necklace -nipple -notebook, notebook computer -obelisk -oboe, hautboy, hautbois -ocarina, sweet potato -odometer, hodometer, mileometer, milometer -oil filter -organ, pipe organ -oscilloscope, scope, cathode-ray oscilloscope, CRO -overskirt -oxcart -oxygen mask -packet -paddle, boat paddle -paddlewheel, paddle wheel -padlock -paintbrush -pajama, pyjama, pj's, jammies -palace -panpipe, pandean pipe, syrinx -paper towel -parachute, chute -parallel bars, bars -park bench -parking meter -passenger car, coach, carriage -patio, terrace -pay-phone, pay-station -pedestal, plinth, footstall -pencil box, pencil case -pencil sharpener -perfume, essence -Petri dish -photocopier -pick, plectrum, plectron -pickelhaube -picket fence, paling -pickup, pickup truck -pier -piggy bank, penny bank -pill bottle -pillow -ping-pong ball -pinwheel -pirate, pirate ship -pitcher, ewer -plane, carpenter's plane, woodworking plane -planetarium -plastic bag -plate rack -plow, plough -plunger, plumber's helper -Polaroid camera, Polaroid Land camera -pole -police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria -poncho -pool table, billiard table, snooker table -pop bottle, soda bottle -pot, flowerpot -potter's wheel -power drill -prayer rug, prayer mat -printer -prison, prison house -projectile, missile -projector -puck, hockey puck -punching bag, punch bag, punching ball, punchball -purse -quill, quill pen -quilt, comforter, comfort, puff -racer, race car, racing car -racket, racquet -radiator -radio, wireless -radio telescope, radio reflector -rain barrel -recreational vehicle, RV, R.V. -reel -reflex camera -refrigerator, icebox -remote control, remote -restaurant, eating house, eating place, eatery -revolver, six-gun, six-shooter -rifle -rocking chair, rocker -rotisserie -rubber eraser, rubber, pencil eraser -rugby ball -rule, ruler -running shoe -safe -safety pin -saltshaker, salt shaker -sandal -sarong -sax, saxophone -scabbard -scale, weighing machine -school bus -schooner -scoreboard -screen, CRT screen -screw -screwdriver -seat belt, seatbelt -sewing machine -shield, buckler -shoe shop, shoe-shop, shoe store -shoji -shopping basket -shopping cart -shovel -shower cap -shower curtain -ski -ski mask -sleeping bag -slide rule, slipstick -sliding door -slot, one-armed bandit -snorkel -snowmobile -snowplow, snowplough -soap dispenser -soccer ball -sock -solar dish, solar collector, solar furnace -sombrero -soup bowl -space bar -space heater -space shuttle -spatula -speedboat -spider web, spider's web -spindle -sports car, sport car -spotlight, spot -stage -steam locomotive -steel arch bridge -steel drum -stethoscope -stole -stone wall -stopwatch, stop watch -stove -strainer -streetcar, tram, tramcar, trolley, trolley car -stretcher -studio couch, day bed -stupa, tope -submarine, pigboat, sub, U-boat -suit, suit of clothes -sundial -sunglass -sunglasses, dark glasses, shades -sunscreen, sunblock, sun blocker -suspension bridge -swab, swob, mop -sweatshirt -swimming trunks, bathing trunks -swing -switch, electric switch, electrical switch -syringe -table lamp -tank, army tank, armored combat vehicle, armoured combat vehicle -tape player -teapot -teddy, teddy bear -television, television system -tennis ball -thatch, thatched roof -theater curtain, theatre curtain -thimble -thresher, thrasher, threshing machine -throne -tile roof -toaster -tobacco shop, tobacconist shop, tobacconist -toilet seat -torch -totem pole -tow truck, tow car, wrecker -toyshop -tractor -trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi -tray -trench coat -tricycle, trike, velocipede -trimaran -tripod -triumphal arch -trolleybus, trolley coach, trackless trolley -trombone -tub, vat -turnstile -typewriter keyboard -umbrella -unicycle, monocycle -upright, upright piano -vacuum, vacuum cleaner -vase -vault -velvet -vending machine -vestment -viaduct -violin, fiddle -volleyball -waffle iron -wall clock -wallet, billfold, notecase, pocketbook -wardrobe, closet, press -warplane, military plane -washbasin, handbasin, washbowl, lavabo, wash-hand basin -washer, automatic washer, washing machine -water bottle -water jug -water tower -whiskey jug -whistle -wig -window screen -window shade -Windsor tie -wine bottle -wing -wok -wooden spoon -wool, woolen, woollen -worm fence, snake fence, snake-rail fence, Virginia fence -wreck -yawl -yurt -web site, website, internet site, site -comic book -crossword puzzle, crossword -street sign -traffic light, traffic signal, stoplight -book jacket, dust cover, dust jacket, dust wrapper -menu -plate -guacamole -consomme -hot pot, hotpot -trifle -ice cream, icecream -ice lolly, lolly, lollipop, popsicle -French loaf -bagel, beigel -pretzel -cheeseburger -hotdog, hot dog, red hot -mashed potato -head cabbage -broccoli -cauliflower -zucchini, courgette -spaghetti squash -acorn squash -butternut squash -cucumber, cuke -artichoke, globe artichoke -bell pepper -cardoon -mushroom -Granny Smith -strawberry -orange -lemon -fig -pineapple, ananas -banana -jackfruit, jak, jack -custard apple -pomegranate -hay -carbonara -chocolate sauce, chocolate syrup -dough -meat loaf, meatloaf -pizza, pizza pie -potpie -burrito -red wine -espresso -cup -eggnog -alp -bubble -cliff, drop, drop-off -coral reef -geyser -lakeside, lakeshore -promontory, headland, head, foreland -sandbar, sand bar -seashore, coast, seacoast, sea-coast -valley, vale -volcano -ballplayer, baseball player -groom, bridegroom -scuba diver -rapeseed -daisy -yellow lady's slipper, yellow lady-slipper, Cypripedium calceolus, Cypripedium parviflorum -corn -acorn -hip, rose hip, rosehip -buckeye, horse chestnut, conker -coral fungus -agaric -gyromitra -stinkhorn, carrion fungus -earthstar -hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa -bolete -ear, spike, capitulum -toilet tissue, toilet paper, bathroom tissue diff --git a/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/module.py b/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/module.py index 380eb839f7f8df17f6588ad0bbfd04c3c155972f..64957fa3aae40088e29f3b5a5dd9dbf67cc387da 100644 --- a/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/module.py +++ b/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/module.py @@ -1,278 +1,244 @@ -# coding=utf-8 -from __future__ import absolute_import -from __future__ import division - -import ast -import argparse +# copyright (c) 2020 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 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 resnet50_vd_imagenet_ssld.processor import postprocess, base64_to_cv2 -from resnet50_vd_imagenet_ssld.data_feed import reader -from resnet50_vd_imagenet_ssld.resnet_vd import ResNet50_vd - - -@moduleinfo( - name="resnet50_vd_imagenet_ssld", - type="CV/image_classification", - author="paddlepaddle", - author_email="paddle-dev@baidu.com", - summary= - "ResNet50vd is a image classfication model, this module is trained with ImageNet-2012 dataset.", - version="1.0.0") -class ResNet50vdDishes(hub.Module): - def _initialize(self): - self.default_pretrained_model_path = os.path.join( - self.directory, "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] - self._set_config() - - def get_expected_image_width(self): - return 224 - - def get_expected_image_height(self): - return 224 - - def get_pretrained_images_mean(self): - im_mean = np.array([0.485, 0.456, 0.406]).reshape(1, 3) - return im_mean - - def get_pretrained_images_std(self): - im_std = np.array([0.229, 0.224, 0.225]).reshape(1, 3) - return im_std - - def _set_config(self): - """ - predictor config setting - """ - cpu_config = AnalysisConfig(self.default_pretrained_model_path) - cpu_config.disable_glog_info() - cpu_config.disable_gpu() - self.cpu_predictor = create_paddle_predictor(cpu_config) - - try: - _places = os.environ["CUDA_VISIBLE_DEVICES"] - int(_places[0]) - use_gpu = True - except: - use_gpu = False - if use_gpu: - gpu_config = AnalysisConfig(self.default_pretrained_model_path) - 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 - - def classification(self, - images=None, - paths=None, - batch_size=1, - use_gpu=False, - top_k=1): - """ - API for image classification. - - Args: - images (numpy.ndarray): data of images, shape of each is [H, W, C], color space must be BGR. - paths (list[str]): The paths of images. - batch_size (int): batch size. - use_gpu (bool): Whether to use gpu. - top_k (int): Return top k results. - - 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) - - total_num = len(all_data) - loop_num = int(np.ceil(total_num / batch_size)) - - res = list() - for iter_id in range(loop_num): - batch_data = list() - handle_id = iter_id * batch_size - for image_id in range(batch_size): - try: - batch_data.append(all_data[handle_id + image_id]) - except: - 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) - 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): - """ - Run as a service. - """ - images_decode = [base64_to_cv2(image) for image in images] - results = self.classification(images=images_decode, **kwargs) - return results - - @runnable - 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.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.") - self.add_module_config_arg() - self.add_module_input_arg() - args = self.parser.parse_args(argvs) - results = self.classification( - paths=[args.input_path], - batch_size=args.batch_size, - use_gpu=args.use_gpu) - return results - - 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( - '--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.") - - def add_module_input_arg(self): - """ - Add the command input options. - """ - self.arg_input_group.add_argument( - '--input_path', type=str, help="path to image.") +import paddle +from paddle import ParamAttr +import paddle.nn as nn +from paddle.nn import Conv2d, BatchNorm, Linear, Dropout +from paddle.nn import AdaptiveAvgPool2d, MaxPool2d, AvgPool2d +from paddle.nn.initializer import Uniform +from paddlehub.module.module import moduleinfo +from paddlehub.module.cv_module import ImageClassifierModule + + +class ConvBNLayer(nn.Layer): + """Basic conv bn layer.""" + def __init__( + self, + num_channels: int, + num_filters: int, + filter_size: int, + stride: int = 1, + groups: int = 1, + is_vd_mode: bool = False, + act: str = None, + name: str = None, + ): + super(ConvBNLayer, self).__init__() + + self.is_vd_mode = is_vd_mode + self._pool2d_avg = AvgPool2d(kernel_size=2, stride=2, padding=0, ceil_mode=True) + self._conv = Conv2d(in_channels=num_channels, + out_channels=num_filters, + kernel_size=filter_size, + stride=stride, + padding=(filter_size - 1) // 2, + groups=groups, + weight_attr=ParamAttr(name=name + "_weights"), + bias_attr=False) + if name == "conv1": + bn_name = "bn_" + name + else: + bn_name = "bn" + name[3:] + self._batch_norm = BatchNorm(num_filters, + act=act, + param_attr=ParamAttr(name=bn_name + '_scale'), + bias_attr=ParamAttr(bn_name + '_offset'), + moving_mean_name=bn_name + '_mean', + moving_variance_name=bn_name + '_variance') + + def forward(self, inputs: paddle.Tensor): + if self.is_vd_mode: + inputs = self._pool2d_avg(inputs) + y = self._conv(inputs) + y = self._batch_norm(y) + return y + + +class BottleneckBlock(nn.Layer): + """Bottleneck Block for ResNet50_vd.""" + def __init__(self, + num_channels: int, + num_filters: int, + stride: int, + shortcut: bool = True, + if_first: bool = False, + name: str = None): + super(BottleneckBlock, self).__init__() + + self.conv0 = ConvBNLayer(num_channels=num_channels, + num_filters=num_filters, + filter_size=1, + act='relu', + name=name + "_branch2a") + self.conv1 = ConvBNLayer(num_channels=num_filters, + num_filters=num_filters, + filter_size=3, + stride=stride, + act='relu', + name=name + "_branch2b") + self.conv2 = ConvBNLayer(num_channels=num_filters, + num_filters=num_filters * 4, + filter_size=1, + act=None, + name=name + "_branch2c") + + if not shortcut: + self.short = ConvBNLayer(num_channels=num_channels, + num_filters=num_filters * 4, + filter_size=1, + stride=1, + is_vd_mode=False if if_first else True, + name=name + "_branch1") + + self.shortcut = shortcut + + def forward(self, inputs: paddle.Tensor): + y = self.conv0(inputs) + conv1 = self.conv1(y) + conv2 = self.conv2(conv1) + + if self.shortcut: + short = inputs + else: + short = self.short(inputs) + y = paddle.elementwise_add(x=short, y=conv2, act='relu') + return y + + +class BasicBlock(nn.Layer): + """Basic block for ResNet50_vd.""" + def __init__(self, + num_channels: int, + num_filters: int, + stride: int, + shortcut: bool = True, + if_first: bool = False, + name: str = None): + super(BasicBlock, self).__init__() + self.stride = stride + self.conv0 = ConvBNLayer(num_channels=num_channels, + num_filters=num_filters, + filter_size=3, + stride=stride, + act='relu', + name=name + "_branch2a") + self.conv1 = ConvBNLayer(num_channels=num_filters, + num_filters=num_filters, + filter_size=3, + act=None, + name=name + "_branch2b") + + if not shortcut: + self.short = ConvBNLayer(num_channels=num_channels, + num_filters=num_filters, + filter_size=1, + stride=1, + is_vd_mode=False if if_first else True, + name=name + "_branch1") + + self.shortcut = shortcut + + def forward(self, inputs: paddle.Tensor): + y = self.conv0(inputs) + conv1 = self.conv1(y) + + if self.shortcut: + short = inputs + else: + short = self.short(inputs) + y = paddle.elementwise_add(x=short, y=conv1, act='relu') + return y + + +@moduleinfo(name="resnet50_vd_imagenet_ssld", + type="CV/classification", + author="paddlepaddle", + author_email="", + summary="resnet50_vd_imagenet_ssld is a classification model, " + "this module is trained with Imagenet dataset.", + version="1.1.0", + meta=ImageClassifierModule) +class ResNet50_vd(nn.Layer): + """ResNet50_vd model.""" + def __init__(self, class_dim: int = 1000, load_checkpoint: str = None): + super(ResNet50_vd, self).__init__() + + self.layers = 50 + + depth = [3, 4, 6, 3] + num_channels = [64, 256, 512, 1024] + num_filters = [64, 128, 256, 512] + + self.conv1_1 = ConvBNLayer(num_channels=3, num_filters=32, filter_size=3, stride=2, act='relu', name="conv1_1") + self.conv1_2 = ConvBNLayer(num_channels=32, num_filters=32, filter_size=3, stride=1, act='relu', name="conv1_2") + self.conv1_3 = ConvBNLayer(num_channels=32, num_filters=64, filter_size=3, stride=1, act='relu', name="conv1_3") + self.pool2d_max = MaxPool2d(kernel_size=3, stride=2, padding=1) + + self.block_list = [] + + for block in range(len(depth)): + shortcut = False + for i in range(depth[block]): + + conv_name = "res" + str(block + 2) + chr(97 + i) + bottleneck_block = self.add_sublayer( + 'bb_%d_%d' % (block, i), + BottleneckBlock(num_channels=num_channels[block] if i == 0 else num_filters[block] * 4, + num_filters=num_filters[block], + stride=2 if i == 0 and block != 0 else 1, + shortcut=shortcut, + if_first=block == i == 0, + name=conv_name)) + self.block_list.append(bottleneck_block) + shortcut = True + + self.pool2d_avg = AdaptiveAvgPool2d(1) + self.pool2d_avg_channels = num_channels[-1] * 2 + stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0) + + self.out = Linear(self.pool2d_avg_channels, + class_dim, + weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv), name="fc_0.w_0"), + bias_attr=ParamAttr(name="fc_0.b_0")) + + if load_checkpoint is not None: + model_dict = paddle.load(load_checkpoint)[0] + self.set_dict(model_dict) + print("load custom checkpoint success") + + else: + checkpoint = os.path.join(self.directory, 'resnet50_vd_ssld.pdparams') + if not os.path.exists(checkpoint): + os.system( + 'wget https://paddlehub.bj.bcebos.com/dygraph/image_classification/resnet50_vd_ssld.pdparams -O ' + + checkpoint) + model_dict = paddle.load(checkpoint)[0] + self.set_dict(model_dict) + print("load pretrained checkpoint success") + + def forward(self, inputs: paddle.Tensor): + y = self.conv1_1(inputs) + y = self.conv1_2(y) + y = self.conv1_3(y) + y = self.pool2d_max(y) + for block in self.block_list: + y = block(y) + y = self.pool2d_avg(y) + y = paddle.reshape(y, shape=[-1, self.pool2d_avg_channels]) + y = self.out(y) + return y diff --git a/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/processor.py b/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/processor.py deleted file mode 100644 index fa8cbb502312e6ef80697ab63b767d4077b3847b..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/processor.py +++ /dev/null @@ -1,55 +0,0 @@ -# coding=utf-8 -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import base64 -import cv2 -import os - -import numpy as np - - -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 - - -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)) - x = np.exp(x) - tmp = np.sum(x, axis=1) - x /= tmp.reshape((x.shape[0], 1)) - else: - tmp = np.max(x) - x -= tmp - x = np.exp(x) - tmp = np.sum(x) - x /= tmp - return x - - -def postprocess(data_out, label_list, top_k): - """ - Postprocess output of network, one image at a time. - - Args: - data_out (numpy.ndarray): output data of network. - label_list (list): list of label. - top_k (int): Return top k results. - """ - output = [] - for result in data_out: - result_i = softmax(result) - output_i = {} - indexs = np.argsort(result_i)[::-1][0:top_k] - for index in indexs: - label = label_list[index].split(',')[0] - output_i[label] = float(result_i[index]) - output.append(output_i) - return output diff --git a/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/resnet_vd.py b/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/resnet_vd.py deleted file mode 100755 index e3f765e01c22eded6e7e6d9475d2147727b6ef3b..0000000000000000000000000000000000000000 --- a/hub_module/modules/image/classification/resnet50_vd_imagenet_ssld/resnet_vd.py +++ /dev/null @@ -1,255 +0,0 @@ -#copyright (c) 2019 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. - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import math - -import paddle -import paddle.fluid as fluid -from paddle.fluid.param_attr import ParamAttr - -__all__ = [ - "ResNet", "ResNet50_vd", "ResNet101_vd", "ResNet152_vd", "ResNet200_vd" -] - -train_parameters = { - "input_size": [3, 224, 224], - "input_mean": [0.485, 0.456, 0.406], - "input_std": [0.229, 0.224, 0.225], - "learning_strategy": { - "name": "piecewise_decay", - "batch_size": 256, - "epochs": [30, 60, 90], - "steps": [0.1, 0.01, 0.001, 0.0001] - } -} - - -class ResNet(): - def __init__(self, layers=50, is_3x3=False): - self.params = train_parameters - self.layers = layers - self.is_3x3 = is_3x3 - - def net(self, input, class_dim=1000): - is_3x3 = self.is_3x3 - layers = self.layers - supported_layers = [50, 101, 152, 200] - assert layers in supported_layers, \ - "supported layers are {} but input layer is {}".format(supported_layers, layers) - - if layers == 50: - depth = [3, 4, 6, 3] - elif layers == 101: - depth = [3, 4, 23, 3] - elif layers == 152: - depth = [3, 8, 36, 3] - elif layers == 200: - depth = [3, 12, 48, 3] - num_filters = [64, 128, 256, 512] - if is_3x3 == False: - conv = self.conv_bn_layer( - input=input, - num_filters=64, - filter_size=7, - stride=2, - act='relu') - else: - conv = self.conv_bn_layer( - input=input, - num_filters=32, - filter_size=3, - stride=2, - act='relu', - name='conv1_1') - conv = self.conv_bn_layer( - input=conv, - num_filters=32, - filter_size=3, - stride=1, - act='relu', - name='conv1_2') - conv = self.conv_bn_layer( - input=conv, - num_filters=64, - filter_size=3, - stride=1, - act='relu', - name='conv1_3') - - conv = fluid.layers.pool2d( - input=conv, - pool_size=3, - pool_stride=2, - pool_padding=1, - pool_type='max') - - for block in range(len(depth)): - for i in range(depth[block]): - if layers in [101, 152, 200] and block == 2: - if i == 0: - conv_name = "res" + str(block + 2) + "a" - else: - conv_name = "res" + str(block + 2) + "b" + str(i) - else: - conv_name = "res" + str(block + 2) + chr(97 + i) - conv = self.bottleneck_block( - input=conv, - num_filters=num_filters[block], - stride=2 if i == 0 and block != 0 else 1, - if_first=block == 0, - name=conv_name) - - pool = fluid.layers.pool2d( - input=conv, pool_size=7, pool_type='avg', global_pooling=True) - stdv = 1.0 / math.sqrt(pool.shape[1] * 1.0) - - out = fluid.layers.fc( - input=pool, - size=class_dim, - param_attr=fluid.param_attr.ParamAttr( - initializer=fluid.initializer.Uniform(-stdv, stdv))) - - return out, pool - - def conv_bn_layer(self, - input, - num_filters, - filter_size, - stride=1, - groups=1, - act=None, - name=None): - conv = fluid.layers.conv2d( - input=input, - num_filters=num_filters, - filter_size=filter_size, - stride=stride, - padding=(filter_size - 1) // 2, - groups=groups, - act=None, - param_attr=ParamAttr(name=name + "_weights"), - bias_attr=False) - if name == "conv1": - bn_name = "bn_" + name - else: - bn_name = "bn" + name[3:] - return fluid.layers.batch_norm( - input=conv, - act=act, - param_attr=ParamAttr(name=bn_name + '_scale'), - bias_attr=ParamAttr(bn_name + '_offset'), - moving_mean_name=bn_name + '_mean', - moving_variance_name=bn_name + '_variance') - - def conv_bn_layer_new(self, - input, - num_filters, - filter_size, - stride=1, - groups=1, - act=None, - name=None): - pool = fluid.layers.pool2d( - input=input, - pool_size=2, - pool_stride=2, - pool_padding=0, - pool_type='avg') - - conv = fluid.layers.conv2d( - input=pool, - num_filters=num_filters, - filter_size=filter_size, - stride=1, - padding=(filter_size - 1) // 2, - groups=groups, - act=None, - param_attr=ParamAttr(name=name + "_weights"), - bias_attr=False) - if name == "conv1": - bn_name = "bn_" + name - else: - bn_name = "bn" + name[3:] - return fluid.layers.batch_norm( - input=conv, - act=act, - param_attr=ParamAttr(name=bn_name + '_scale'), - bias_attr=ParamAttr(bn_name + '_offset'), - moving_mean_name=bn_name + '_mean', - moving_variance_name=bn_name + '_variance') - - def shortcut(self, input, ch_out, stride, name, if_first=False): - ch_in = input.shape[1] - if ch_in != ch_out or stride != 1: - if if_first: - return self.conv_bn_layer(input, ch_out, 1, stride, name=name) - else: - return self.conv_bn_layer_new( - input, ch_out, 1, stride, name=name) - else: - return input - - def bottleneck_block(self, input, num_filters, stride, name, if_first): - conv0 = self.conv_bn_layer( - input=input, - num_filters=num_filters, - filter_size=1, - act='relu', - name=name + "_branch2a") - conv1 = self.conv_bn_layer( - input=conv0, - num_filters=num_filters, - filter_size=3, - stride=stride, - act='relu', - name=name + "_branch2b") - conv2 = self.conv_bn_layer( - input=conv1, - num_filters=num_filters * 4, - filter_size=1, - act=None, - name=name + "_branch2c") - - short = self.shortcut( - input, - num_filters * 4, - stride, - if_first=if_first, - name=name + "_branch1") - - return fluid.layers.elementwise_add(x=short, y=conv2, act='relu') - - -def ResNet50_vd(): - model = ResNet(layers=50, is_3x3=True) - return model - - -def ResNet101_vd(): - model = ResNet(layers=101, is_3x3=True) - return model - - -def ResNet152_vd(): - model = ResNet(layers=152, is_3x3=True) - return model - - -def ResNet200_vd(): - model = ResNet(layers=200, is_3x3=True) - return model diff --git a/hub_module/modules/image/object_detection/yolov3_darknet53_pascalvoc/module.py b/hub_module/modules/image/object_detection/yolov3_darknet53_pascalvoc/module.py new file mode 100644 index 0000000000000000000000000000000000000000..079d89452eb55c9669ad3b3a3f5674e31fafc6d6 --- /dev/null +++ b/hub_module/modules/image/object_detection/yolov3_darknet53_pascalvoc/module.py @@ -0,0 +1,318 @@ +import os + +import paddle +import paddle.nn as nn +import paddle.nn.functional as F +from paddle.nn.initializer import Normal, Constant +from paddle.regularizer import L2Decay +from paddlehub.module.cv_module import Yolov3Module +import paddlehub.process.detect_transforms as T +from paddlehub.module.module import moduleinfo + + +class ConvBNLayer(nn.Layer): + """Basic block for Darknet""" + def __init__(self, + ch_in: int, + ch_out: int, + filter_size: int = 3, + stride: int = 1, + groups: int = 1, + padding: int = 0, + act: str = 'leakly', + is_test: bool = False): + super(ConvBNLayer, self).__init__() + + self.conv = nn.Conv2d(ch_in, + ch_out, + filter_size, + padding=padding, + stride=stride, + groups=groups, + weight_attr=paddle.ParamAttr(initializer=Normal(0., 0.02)), + bias_attr=False) + + self.batch_norm = nn.BatchNorm(num_channels=ch_out, + is_test=is_test, + param_attr=paddle.ParamAttr(initializer=Normal(0., 0.02), + regularizer=L2Decay(0.))) + self.act = act + + def forward(self, inputs: paddle.Tensor) -> paddle.Tensor: + out = self.conv(inputs) + out = self.batch_norm(out) + if self.act == "leakly": + out = F.leaky_relu(x=out, negative_slope=0.1) + return out + + +class DownSample(nn.Layer): + """Downsample block for Darknet""" + def __init__(self, + ch_in: int, + ch_out: int, + filter_size: int = 3, + stride: int = 2, + padding: int = 1, + is_test: bool = False): + super(DownSample, self).__init__() + + self.conv_bn_layer = ConvBNLayer(ch_in=ch_in, + ch_out=ch_out, + filter_size=filter_size, + stride=stride, + padding=padding, + is_test=is_test) + self.ch_out = ch_out + + def forward(self, inputs: paddle.Tensor) -> paddle.Tensor: + out = self.conv_bn_layer(inputs) + return out + + +class BasicBlock(nn.Layer): + """Basic residual block for Darknet""" + def __init__(self, ch_in: int, ch_out: int, is_test: bool = False): + super(BasicBlock, self).__init__() + + self.conv1 = ConvBNLayer(ch_in=ch_in, ch_out=ch_out, filter_size=1, stride=1, padding=0, is_test=is_test) + self.conv2 = ConvBNLayer(ch_in=ch_out, ch_out=ch_out * 2, filter_size=3, stride=1, padding=1, is_test=is_test) + + def forward(self, inputs: paddle.Tensor) -> paddle.Tensor: + conv1 = self.conv1(inputs) + conv2 = self.conv2(conv1) + out = paddle.elementwise_add(x=inputs, y=conv2, act=None) + return out + + +class LayerWarp(nn.Layer): + """Warp layer composed by basic residual blocks""" + def __init__(self, ch_in: int, ch_out: int, count: int, is_test: bool = False): + super(LayerWarp, self).__init__() + self.basicblock0 = BasicBlock(ch_in, ch_out, is_test=is_test) + self.res_out_list = [] + for i in range(1, count): + res_out = self.add_sublayer("basic_block_%d" % (i), BasicBlock(ch_out * 2, ch_out, is_test=is_test)) + self.res_out_list.append(res_out) + self.ch_out = ch_out + + def forward(self, inputs: paddle.Tensor) -> paddle.Tensor: + y = self.basicblock0(inputs) + for basic_block_i in self.res_out_list: + y = basic_block_i(y) + return y + + +class DarkNet53_conv_body(nn.Layer): + """Darknet53 + Args: + ch_in(int): Input channels, default is 3. + is_test (bool): Set the test mode, default is True. + """ + def __init__(self, ch_in: int = 3, is_test: bool = False): + super(DarkNet53_conv_body, self).__init__() + self.stages = [1, 2, 8, 8, 4] + self.stages = self.stages[0:5] + + self.conv0 = ConvBNLayer(ch_in=ch_in, ch_out=32, filter_size=3, stride=1, padding=1, is_test=is_test) + + self.downsample0 = DownSample(ch_in=32, ch_out=32 * 2, is_test=is_test) + self.darknet53_conv_block_list = [] + self.downsample_list = [] + ch_in = [64, 128, 256, 512, 1024] + + for i, stage in enumerate(self.stages): + conv_block = self.add_sublayer("stage_%d" % (i), + LayerWarp(int(ch_in[i]), 32 * (2**i), stage, is_test=is_test)) + self.darknet53_conv_block_list.append(conv_block) + + for i in range(len(self.stages) - 1): + downsample = self.add_sublayer( + "stage_%d_downsample" % i, DownSample(ch_in=32 * (2**(i + 1)), + ch_out=32 * (2**(i + 2)), + is_test=is_test)) + self.downsample_list.append(downsample) + + def forward(self, inputs: paddle.Tensor) -> paddle.Tensor: + out = self.conv0(inputs) + out = self.downsample0(out) + blocks = [] + for i, conv_block_i in enumerate(self.darknet53_conv_block_list): + out = conv_block_i(out) + blocks.append(out) + if i < len(self.stages) - 1: + out = self.downsample_list[i](out) + return blocks[-1:-4:-1] + + +class YoloDetectionBlock(nn.Layer): + """Basic block for Yolov3""" + def __init__(self, ch_in: int, channel: int, is_test: bool = True): + super(YoloDetectionBlock, self).__init__() + + assert channel % 2 == 0, \ + "channel {} cannot be divided by 2".format(channel) + + self.conv0 = ConvBNLayer(ch_in=ch_in, ch_out=channel, filter_size=1, stride=1, padding=0, is_test=is_test) + self.conv1 = ConvBNLayer(ch_in=channel, ch_out=channel * 2, filter_size=3, stride=1, padding=1, is_test=is_test) + self.conv2 = ConvBNLayer(ch_in=channel * 2, ch_out=channel, filter_size=1, stride=1, padding=0, is_test=is_test) + self.conv3 = ConvBNLayer(ch_in=channel, ch_out=channel * 2, filter_size=3, stride=1, padding=1, is_test=is_test) + self.route = ConvBNLayer(ch_in=channel * 2, ch_out=channel, filter_size=1, stride=1, padding=0, is_test=is_test) + self.tip = ConvBNLayer(ch_in=channel, ch_out=channel * 2, filter_size=3, stride=1, padding=1, is_test=is_test) + + def forward(self, inputs): + out = self.conv0(inputs) + out = self.conv1(out) + out = self.conv2(out) + out = self.conv3(out) + route = self.route(out) + tip = self.tip(route) + return route, tip + + +class Upsample(nn.Layer): + """Upsample block for Yolov3""" + def __init__(self, scale: int = 2): + super(Upsample, self).__init__() + self.scale = scale + + def forward(self, inputs: paddle.Tensor): + shape_nchw = paddle.to_tensor(inputs.shape) + shape_hw = paddle.slice(shape_nchw, axes=[0], starts=[2], ends=[4]) + shape_hw.stop_gradient = True + in_shape = paddle.cast(shape_hw, dtype='int32') + out_shape = in_shape * self.scale + out_shape.stop_gradient = True + out = F.resize_nearest(input=inputs, scale=self.scale, actual_shape=out_shape) + return out + + +@moduleinfo(name="yolov3_darknet53_pascalvoc", + type="CV/image_editing", + author="paddlepaddle", + author_email="", + summary="Yolov3 is a detection model, this module is trained with VOC dataset.", + version="1.0.0", + meta=Yolov3Module) +class YOLOv3(nn.Layer): + """YOLOV3 for detection + + Args: + ch_in(int): Input channels, default is 3. + class_num(int): Categories for detection,if dataset is voc, class_num is 20. + ignore_thresh(float): The ignore threshold to ignore confidence loss. + valid_thresh(float): Threshold to filter out bounding boxes with low confidence score. + nms_topk(int): Maximum number of detections to be kept according to the confidences after the filtering + detections based on score_threshold. + nms_posk(int): Number of total bboxes to be kept per image after NMS step. -1 means keeping all bboxes after NMS + step. + nms_thresh (float): The threshold to be used in NMS. Default: 0.3. + is_train (bool): Set the train mode, default is True. + load_checkpoint(str): Whether to load checkpoint. + """ + def __init__(self, + ch_in: int = 3, + class_num: int = 20, + ignore_thresh: float = 0.7, + valid_thresh: float = 0.005, + nms_topk: int = 400, + nms_posk: int = 100, + nms_thresh: float = 0.45, + is_train: bool = True, + load_checkpoint: str = None): + super(YOLOv3, self).__init__() + + self.is_train = is_train + self.block = DarkNet53_conv_body(ch_in=ch_in, is_test=not self.is_train) + self.block_outputs = [] + self.yolo_blocks = [] + self.route_blocks_2 = [] + self.anchor_masks = [[6, 7, 8], [3, 4, 5], [0, 1, 2]] + self.anchors = [10, 13, 16, 30, 33, 23, 30, 61, 62, 45, 59, 119, 116, 90, 156, 198, 373, 326] + self.class_num = class_num + self.ignore_thresh = ignore_thresh + self.valid_thresh = valid_thresh + self.nms_topk = nms_topk + self.nms_posk = nms_posk + self.nms_thresh = nms_thresh + ch_in_list = [1024, 768, 384] + + for i in range(3): + yolo_block = self.add_sublayer( + "yolo_detecton_block_%d" % (i), + YoloDetectionBlock(ch_in_list[i], channel=512 // (2**i), is_test=not self.is_train)) + self.yolo_blocks.append(yolo_block) + + num_filters = len(self.anchor_masks[i]) * (self.class_num + 5) + block_out = self.add_sublayer( + "block_out_%d" % (i), + nn.Conv2d(1024 // (2**i), + num_filters, + 1, + stride=1, + padding=0, + weight_attr=paddle.ParamAttr(initializer=Normal(0., 0.02)), + bias_attr=paddle.ParamAttr(initializer=Constant(0.0), regularizer=L2Decay(0.)))) + self.block_outputs.append(block_out) + + if i < 2: + route = self.add_sublayer( + "route2_%d" % i, + ConvBNLayer(ch_in=512 // (2**i), + ch_out=256 // (2**i), + filter_size=1, + stride=1, + padding=0, + is_test=(not self.is_train))) + self.route_blocks_2.append(route) + self.upsample = Upsample() + + if load_checkpoint is not None: + model_dict = paddle.load(load_checkpoint)[0] + self.set_dict(model_dict) + print("load custom checkpoint success") + + else: + checkpoint = os.path.join(self.directory, 'yolov3_darknet53_voc.pdparams') + if not os.path.exists(checkpoint): + os.system( + 'wget https://paddlehub.bj.bcebos.com/dygraph/detection/yolov3_darknet53_voc.pdparams -O ' \ + + checkpoint) + model_dict = paddle.load(checkpoint)[0] + self.set_dict(model_dict) + print("load pretrained checkpoint success") + + def transform(self, img): + if self.is_train: + transform = T.Compose([ + T.RandomDistort(), + T.RandomExpand(fill=[0.485, 0.456, 0.406]), + T.RandomCrop(), + T.Resize(target_size=416), + T.RandomFlip(), + T.ShuffleBox(), + T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) + ]) + else: + transform = T.Compose([ + T.Resize(target_size=416, interp='CUBIC'), + T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) + ]) + + return transform(img) + + def forward(self, inputs: paddle.Tensor): + outputs = [] + blocks = self.block(inputs) + route = None + for i, block in enumerate(blocks): + if i > 0: + block = paddle.concat([route, block], axis=1) + route, tip = self.yolo_blocks[i](block) + block_out = self.block_outputs[i](tip) + outputs.append(block_out) + if i < 2: + route = self.route_blocks_2[i](route) + route = self.upsample(route) + + return outputs diff --git a/paddlehub/datasets/pascalvoc.py b/paddlehub/datasets/pascalvoc.py new file mode 100644 index 0000000000000000000000000000000000000000..85945dfaf1a5770304e2986648e7dee184f4c92c --- /dev/null +++ b/paddlehub/datasets/pascalvoc.py @@ -0,0 +1,190 @@ +# coding:utf-8 +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. +# +# 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 copy +from typing import Callable + +import paddle +import numpy as np +from paddlehub.env import DATA_HOME +from pycocotools.coco import COCO + + +class DetectCatagory: + """Load label name, id and map from detection dataset. + + Args: + attrbox(Callable): Method to get detection attributes of images. + data_dir(str): Image dataset path. + + Returns: + label_names(List(str)): The dataset label names. + label_ids(List(int)): The dataset label ids. + category_to_id_map(dict): Mapping relations of category and id for images. + """ + def __init__(self, attrbox: Callable, data_dir: str): + self.attrbox = attrbox + self.img_dir = data_dir + + def __call__(self): + self.categories = self.attrbox.loadCats(self.attrbox.getCatIds()) + self.num_category = len(self.categories) + label_names = [] + label_ids = [] + for category in self.categories: + label_names.append(category['name']) + label_ids.append(int(category['id'])) + category_to_id_map = {v: i for i, v in enumerate(label_ids)} + return label_names, label_ids, category_to_id_map + + +class ParseImages: + """Prepare images for detection. + + Args: + attrbox(Callable): Method to get detection attributes of images. + data_dir(str): Image dataset path. + category_to_id_map(dict): Mapping relations of category and id for images. + + Returns: + imgs(dict): The input for detection model, it is a dict. + """ + def __init__(self, attrbox: Callable, data_dir: str, category_to_id_map: dict): + self.attrbox = attrbox + self.img_dir = data_dir + self.category_to_id_map = category_to_id_map + self.parse_gt_annotations = GTAnotations(self.attrbox, self.category_to_id_map) + + def __call__(self): + image_ids = self.attrbox.getImgIds() + image_ids.sort() + imgs = copy.deepcopy(self.attrbox.loadImgs(image_ids)) + + for img in imgs: + img['image'] = os.path.join(self.img_dir, img['file_name']) + assert os.path.exists(img['image']), "image {} not found.".format(img['image']) + box_num = 50 + img['gt_boxes'] = np.zeros((box_num, 4), dtype=np.float32) + img['gt_labels'] = np.zeros((box_num), dtype=np.int32) + img = self.parse_gt_annotations(img) + return imgs + + +class GTAnotations: + """Set gt boxes and gt labels for train. + + Args: + attrbox(Callable): Method for get detection attributes for images. + category_to_id_map(dict): Mapping relations of category and id for images. + img(dict): Input for detection model. + + Returns: + img(dict): Set specific value on the attributes of 'gt boxes' and 'gt labels' for input. + """ + def __init__(self, attrbox: Callable, category_to_id_map: dict): + self.attrbox = attrbox + self.category_to_id_map = category_to_id_map + + def box_to_center_relative(self, box: list, img_height: int, img_width: int) -> np.ndarray: + """ + Convert COCO annotations box with format [x1, y1, w, h] to + center mode [center_x, center_y, w, h] and divide image width + and height to get relative value in range[0, 1] + """ + assert len(box) == 4, "box should be a len(4) list or tuple" + x, y, w, h = box + + x1 = max(x, 0) + x2 = min(x + w - 1, img_width - 1) + y1 = max(y, 0) + y2 = min(y + h - 1, img_height - 1) + + x = (x1 + x2) / 2 / img_width + y = (y1 + y2) / 2 / img_height + w = (x2 - x1) / img_width + h = (y2 - y1) / img_height + + return np.array([x, y, w, h]) + + def __call__(self, img: dict): + img_height = img['height'] + img_width = img['width'] + anno = self.attrbox.loadAnns(self.attrbox.getAnnIds(imgIds=img['id'], iscrowd=None)) + gt_index = 0 + + for target in anno: + if target['area'] < -1: + continue + if 'ignore' in target and target['ignore']: + continue + box = self.box_to_center_relative(target['bbox'], img_height, img_width) + + if box[2] <= 0 and box[3] <= 0: + continue + img['gt_boxes'][gt_index] = box + img['gt_labels'][gt_index] = \ + self.category_to_id_map[target['category_id']] + gt_index += 1 + if gt_index >= 50: + break + return img + + +class DetectionData(paddle.io.Dataset): + """ + Dataset for image detection. + + Args: + transform(callmethod) : The method of preprocess images. + mode(str): The mode for preparing dataset. + + Returns: + DataSet: An iterable object for data iterating + """ + def __init__(self, transform: Callable, size: int = 416, mode: str = 'train'): + self.mode = mode + self.transform = transform + self.size = size + + if self.mode == 'train': + train_file_list = 'annotations/instances_train2017.json' + train_data_dir = 'train2017' + self.train_file_list = os.path.join(DATA_HOME, 'voc', train_file_list) + self.train_data_dir = os.path.join(DATA_HOME, 'voc', train_data_dir) + self.COCO = COCO(self.train_file_list) + self.img_dir = self.train_data_dir + + elif self.mode == 'test': + val_file_list = 'annotations/instances_val2017.json' + val_data_dir = 'val2017' + self.val_file_list = os.path.join(DATA_HOME, 'voc', val_file_list) + self.val_data_dir = os.path.join(DATA_HOME, 'voc', val_data_dir) + self.COCO = COCO(self.val_file_list) + self.img_dir = self.val_data_dir + + parse_dataset_catagory = DetectCatagory(self.COCO, self.img_dir) + self.label_names, self.label_ids, self.category_to_id_map = parse_dataset_catagory() + parse_images = ParseImages(self.COCO, self.img_dir, self.category_to_id_map) + self.data = parse_images() + + def __getitem__(self, idx: int): + img = self.data[idx] + im, data = self.transform(img) + out_img, gt_boxes, gt_labels, gt_scores = im, data['gt_boxes'], data['gt_labels'], data['gt_scores'] + return out_img, gt_boxes, gt_labels, gt_scores + + def __len__(self): + return len(self.data) diff --git a/paddlehub/module/cv_module.py b/paddlehub/module/cv_module.py index 80936bd6d900589b41ab4b54c37c72c21ff6253b..e8f8bf01438a0d35fb1b1204832136dc595fe8da 100644 --- a/paddlehub/module/cv_module.py +++ b/paddlehub/module/cv_module.py @@ -27,8 +27,8 @@ from PIL import Image from paddlehub.module.module import serving, RunModule from paddlehub.utils.utils import base64_to_cv2 -from paddlehub.process.transforms import ConvertColorSpace, ColorPostprocess, Resize -from paddlehub.process.functional import subtract_imagenet_mean_batch, gram_matrix +import paddlehub.process.transforms as T +import paddlehub.process.functional as Func class ImageServing(object): @@ -136,8 +136,8 @@ class ImageColorizeModule(RunModule, ImageServing): visual_ret = OrderedDict() psnrs = [] - lab2rgb = ConvertColorSpace(mode='LAB2RGB') - process = ColorPostprocess() + lab2rgb = T.ConvertColorSpace(mode='LAB2RGB') + process = T.ColorPostprocess() for i in range(batch[0].numpy().shape[0]): real = lab2rgb(np.concatenate((batch[0].numpy(), batch[3].numpy()), axis=1))[i] @@ -163,9 +163,9 @@ class ImageColorizeModule(RunModule, ImageServing): Returns: results(list[dict]) : The prediction result of each input image ''' - lab2rgb = ConvertColorSpace(mode='LAB2RGB') - process = ColorPostprocess() - resize = Resize((256, 256)) + lab2rgb = T.ConvertColorSpace(mode='LAB2RGB') + process = T.ColorPostprocess() + resize = T.Resize((256, 256)) visual_ret = OrderedDict() im = self.transforms(images, is_train=False) out_class, out_reg = self(paddle.to_tensor(im['A']), paddle.to_variable(im['hint_B']), @@ -196,6 +196,124 @@ class ImageColorizeModule(RunModule, ImageServing): return result +class Yolov3Module(RunModule, ImageServing): + def training_step(self, batch: int, batch_idx: int) -> dict: + ''' + One step for training, which should be called as forward computation. + + Args: + batch(list[paddle.Tensor]): The one batch data, which contains images, ground truth boxes, labels and scores. + batch_idx(int): The index of batch. + + Returns: + results(dict): The model outputs, such as loss. + ''' + + return self.validation_step(batch, batch_idx) + + def validation_step(self, batch: int, batch_idx: int) -> dict: + ''' + One step for validation, which should be called as forward computation. + + Args: + batch(list[paddle.Tensor]): The one batch data, which contains images, ground truth boxes, labels and scores. + batch_idx(int): The index of batch. + + Returns: + results(dict) : The model outputs, such as metrics. + ''' + img = batch[0].astype('float32') + gtbox = batch[1].astype('float32') + gtlabel = batch[2].astype('int32') + gtscore = batch[3].astype("float32") + losses = [] + outputs = self(img) + self.downsample = 32 + + for i, out in enumerate(outputs): + anchor_mask = self.anchor_masks[i] + loss = F.yolov3_loss(x=out, + gt_box=gtbox, + gt_label=gtlabel, + gt_score=gtscore, + anchors=self.anchors, + anchor_mask=anchor_mask, + class_num=self.class_num, + ignore_thresh=self.ignore_thresh, + downsample_ratio=32, + use_label_smooth=False) + losses.append(paddle.reduce_mean(loss)) + self.downsample //= 2 + + return {'loss': sum(losses)} + + def predict(self, imgpath: str, filelist: str, visualization: bool = True, save_path: str = 'result'): + ''' + Detect images + + Args: + imgpath(str): Image path . + filelist(str): Path to get label name. + visualization(bool): Whether to save result image. + save_path(str) : Path to save detected images. + + Returns: + boxes(np.ndarray): Predict box information. + scores(np.ndarray): Predict score. + labels(np.ndarray): Predict labels. + ''' + boxes = [] + scores = [] + self.downsample = 32 + im = self.transform(imgpath) + h, w, c = Func.img_shape(imgpath) + im_shape = paddle.to_tensor(np.array([[h, w]]).astype('int32')) + label_names = Func.get_label_infos(filelist) + img_data = paddle.to_tensor(np.array([im]).astype('float32')) + + outputs = self(img_data) + + for i, out in enumerate(outputs): + anchor_mask = self.anchor_masks[i] + mask_anchors = [] + for m in anchor_mask: + mask_anchors.append((self.anchors[2 * m])) + mask_anchors.append(self.anchors[2 * m + 1]) + + box, score = F.yolo_box(x=out, + img_size=im_shape, + anchors=mask_anchors, + class_num=self.class_num, + conf_thresh=self.valid_thresh, + downsample_ratio=self.downsample, + name="yolo_box" + str(i)) + + boxes.append(box) + scores.append(paddle.transpose(score, perm=[0, 2, 1])) + self.downsample //= 2 + + yolo_boxes = paddle.concat(boxes, axis=1) + yolo_scores = paddle.concat(scores, axis=2) + + pred = F.multiclass_nms(bboxes=yolo_boxes, + scores=yolo_scores, + score_threshold=self.valid_thresh, + nms_top_k=self.nms_topk, + keep_top_k=self.nms_posk, + nms_threshold=self.nms_thresh, + background_label=-1) + + bboxes = pred.numpy() + labels = bboxes[:, 0].astype('int32') + scores = bboxes[:, 1].astype('float32') + boxes = bboxes[:, 2:].astype('float32') + + if visualization: + Func.draw_boxes_on_image(imgpath, boxes, scores, labels, label_names, 0.5) + + return boxes, scores, labels + + class StyleTransferModule(RunModule, ImageServing): def training_step(self, batch: int, batch_idx: int) -> dict: ''' @@ -228,19 +346,19 @@ class StyleTransferModule(RunModule, ImageServing): y = self(batch[0]) xc = paddle.to_tensor(batch[0].numpy().copy()) - y = subtract_imagenet_mean_batch(y) - xc = subtract_imagenet_mean_batch(xc) + y = Func.subtract_imagenet_mean_batch(y) + xc = Func.subtract_imagenet_mean_batch(xc) features_y = self.getFeature(y) features_xc = self.getFeature(xc) f_xc_c = paddle.to_tensor(features_xc[1].numpy(), stop_gradient=True) content_loss = mse_loss(features_y[1], f_xc_c) - batch[1] = subtract_imagenet_mean_batch(batch[1]) + batch[1] = Func.subtract_imagenet_mean_batch(batch[1]) features_style = self.getFeature(batch[1]) - gram_style = [gram_matrix(y) for y in features_style] + gram_style = [Func.gram_matrix(y) for y in features_style] style_loss = 0. for m in range(len(features_y)): - gram_y = gram_matrix(features_y[m]) + gram_y = Func.gram_matrix(features_y[m]) gram_s = paddle.to_tensor(np.tile(gram_style[m].numpy(), (N, 1, 1, 1))) style_loss += mse_loss(gram_y, gram_s[:N, :, :]) diff --git a/paddlehub/process/detect_transforms.py b/paddlehub/process/detect_transforms.py new file mode 100644 index 0000000000000000000000000000000000000000..5a50ffb01fcaf6106a25ff74a07beb822237400c --- /dev/null +++ b/paddlehub/process/detect_transforms.py @@ -0,0 +1,342 @@ +import os +import random +from typing import Callable + +import cv2 +import numpy as np +import matplotlib +import PIL +from PIL import Image, ImageEnhance +from matplotlib import pyplot as plt + +from paddlehub.process.functional import * + +matplotlib.use('Agg') + + +class RandomDistort: + """ + Distort the input image randomly. + + Args: + lower(float): The lower bound value for enhancement, default is 0.5. + upper(float): The upper bound value for enhancement, default is 1.5. + + Returns: + img(np.ndarray): Distorted image. + data(dict): Image info and label info. + + """ + def __init__(self, lower: float = 0.5, upper: float = 1.5): + self.lower = lower + self.upper = upper + + def random_brightness(self, img: PIL.Image): + e = np.random.uniform(self.lower, self.upper) + return ImageEnhance.Brightness(img).enhance(e) + + def random_contrast(self, img: PIL.Image): + e = np.random.uniform(self.lower, self.upper) + return ImageEnhance.Contrast(img).enhance(e) + + def random_color(self, img: PIL.Image): + e = np.random.uniform(self.lower, self.upper) + return ImageEnhance.Color(img).enhance(e) + + def __call__(self, img: np.ndarray, data: dict): + ops = [self.random_brightness, self.random_contrast, self.random_color] + np.random.shuffle(ops) + img = Image.fromarray(img) + img = ops[0](img) + img = ops[1](img) + img = ops[2](img) + img = np.asarray(img) + + return img, data + + +class RandomExpand: + """ + Randomly expand images and gt boxes by random ratio. It is a data enhancement operation for model training. + + Args: + max_ratio(float): Max value for expansion ratio, default is 4. + fill(list): Initialize the pixel value of the image with the input fill value, default is None. + keep_ratio(bool): Whether image keeps ratio. + thresh(float): If random ratio does not exceed the thresh, return original images and gt boxes, default is 0.5. + + Return: + img(np.ndarray): Distorted image. + data(dict): Image info and label info. + + """ + def __init__(self, max_ratio: float = 4., fill: list = None, keep_ratio: bool = True, thresh: float = 0.5): + + self.max_ratio = max_ratio + self.fill = fill + self.keep_ratio = keep_ratio + self.thresh = thresh + + def __call__(self, img: np.ndarray, data: dict): + gtboxes = data['gt_boxes'] + + if random.random() > self.thresh: + return img, data + if self.max_ratio < 1.0: + return img, data + h, w, c = img.shape + + ratio_x = random.uniform(1, self.max_ratio) + if self.keep_ratio: + ratio_y = ratio_x + else: + ratio_y = random.uniform(1, self.max_ratio) + + oh = int(h * ratio_y) + ow = int(w * ratio_x) + off_x = random.randint(0, ow - w) + off_y = random.randint(0, oh - h) + + out_img = np.zeros((oh, ow, c)) + if self.fill and len(self.fill) == c: + for i in range(c): + out_img[:, :, i] = self.fill[i] * 255.0 + + out_img[off_y:off_y + h, off_x:off_x + w, :] = img + gtboxes[:, 0] = ((gtboxes[:, 0] * w) + off_x) / float(ow) + gtboxes[:, 1] = ((gtboxes[:, 1] * h) + off_y) / float(oh) + gtboxes[:, 2] = gtboxes[:, 2] / ratio_x + gtboxes[:, 3] = gtboxes[:, 3] / ratio_y + data['gt_boxes'] = gtboxes + img = out_img.astype('uint8') + + return img, data + + +class RandomCrop: + """ + Random crop the input image according to constraints. + + Args: + scales(list): The value of the cutting area relative to the original area, expressed in the form of \ + [min, max]. The default value is [.3, 1.]. + max_ratio(float): Max ratio of the original area relative to the cutting area, default is 2.0. + constraints(list): The value of min and max iou values, default is None. + max_trial(int): The max trial for finding a valid crop area. The default value is 50. + + Returns: + img(np.ndarray): Distorted image. + data(dict): Image info and label info. + + """ + def __init__(self, + scales: list = [0.3, 1.0], + max_ratio: float = 2.0, + constraints: list = None, + max_trial: int = 50): + self.scales = scales + self.max_ratio = max_ratio + self.constraints = constraints + self.max_trial = max_trial + + def __call__(self, img: np.ndarray, data: dict): + boxes = data['gt_boxes'] + labels = data['gt_labels'] + scores = data['gt_scores'] + + if len(boxes) == 0: + return img, data + if not self.constraints: + self.constraints = [(0.1, 1.0), (0.3, 1.0), (0.5, 1.0), (0.7, 1.0), (0.9, 1.0), (0.0, 1.0)] + + img = Image.fromarray(img) + w, h = img.size + crops = [(0, 0, w, h)] + for min_iou, max_iou in self.constraints: + for _ in range(self.max_trial): + scale = random.uniform(self.scales[0], self.scales[1]) + aspect_ratio = random.uniform(max(1 / self.max_ratio, scale * scale), \ + min(self.max_ratio, 1 / scale / scale)) + crop_h = int(h * scale / np.sqrt(aspect_ratio)) + crop_w = int(w * scale * np.sqrt(aspect_ratio)) + crop_x = random.randrange(w - crop_w) + crop_y = random.randrange(h - crop_h) + crop_box = np.array([[(crop_x + crop_w / 2.0) / w, (crop_y + crop_h / 2.0) / h, crop_w / float(w), + crop_h / float(h)]]) + iou = box_iou_xywh(crop_box, boxes) + if min_iou <= iou.min() and max_iou >= iou.max(): + crops.append((crop_x, crop_y, crop_w, crop_h)) + break + + while crops: + crop = crops.pop(np.random.randint(0, len(crops))) + crop_boxes, crop_labels, crop_scores, box_num = box_crop(boxes, labels, scores, crop, (w, h)) + + if box_num < 1: + continue + img = img.crop((crop[0], crop[1], crop[0] + crop[2], crop[1] + crop[3])).resize(img.size, Image.LANCZOS) + img = np.asarray(img) + data['gt_boxes'] = crop_boxes + data['gt_labels'] = crop_labels + data['gt_scores'] = crop_scores + return img, data + + img = np.asarray(img) + data['gt_boxes'] = boxes + data['gt_labels'] = labels + data['gt_scores'] = scores + return img, data + + +class RandomFlip: + """Flip the images and gt boxes randomly. + + Args: + thresh: Probability for random flip. + + Returns: + img(np.ndarray): Distorted image. + data(dict): Image info and label info. + """ + def __init__(self, thresh: float = 0.5): + self.thresh = thresh + + def __call__(self, img, data): + gtboxes = data['gt_boxes'] + if random.random() > self.thresh: + img = img[:, ::-1, :] + gtboxes[:, 0] = 1.0 - gtboxes[:, 0] + data['gt_boxes'] = gtboxes + return img, data + + +class Compose: + """ + Preprocess the input data according to the operators. + + Args: + transforms(list): Preprocessing operators. + + Returns: + img(np.ndarray): Preprocessed image. + data(dict): Image info and label info, default is None. + """ + def __init__(self, transforms: list): + if not isinstance(transforms, list): + raise TypeError('The transforms must be a list!') + if len(transforms) < 1: + raise ValueError('The length of transforms ' + \ + 'must be equal or larger than 1!') + self.transforms = transforms + + def __call__(self, data: dict): + + if isinstance(data, dict): + if isinstance(data['image'], str): + img = cv2.imread(data['image']) + img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) + gt_labels = data['gt_labels'].copy() + data['gt_scores'] = np.ones_like(gt_labels) + for op in self.transforms: + img, data = op(img, data) + img = img.transpose((2, 0, 1)) + return img, data + + if isinstance(data, str): + img = cv2.imread(data) + img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) + for op in self.transforms: + img, data = op(img, data) + img = img.transpose((2, 0, 1)) + return img + + +class Resize: + """ + Resize the input images. + + Args: + target_size(int): Targeted input size. + interp(str): Interpolation method. + + Returns: + img(np.ndarray): Preprocessed image. + data(dict): Image info and label info, default is None. + """ + def __init__(self, target_size: int = 512, interp: str = 'RANDOM'): + self.interp_dict = { + 'NEAREST': cv2.INTER_NEAREST, + 'LINEAR': cv2.INTER_LINEAR, + 'CUBIC': cv2.INTER_CUBIC, + 'AREA': cv2.INTER_AREA, + 'LANCZOS4': cv2.INTER_LANCZOS4 + } + self.interp = interp + if not (interp == "RANDOM" or interp in self.interp_dict): + raise ValueError("interp should be one of {}".format(self.interp_dict.keys())) + if isinstance(target_size, list) or isinstance(target_size, tuple): + if len(target_size) != 2: + raise TypeError( + 'when target is list or tuple, it should include 2 elements, but it is {}'.format(target_size)) + elif not isinstance(target_size, int): + raise TypeError("Type of target_size is invalid. Must be Integer or List or tuple, now is {}".format( + type(target_size))) + + self.target_size = target_size + + def __call__(self, img, data=None): + + if self.interp == "RANDOM": + interp = random.choice(list(self.interp_dict.keys())) + else: + interp = self.interp + img = resize(img, self.target_size, self.interp_dict[interp]) + if data is not None: + return img, data + else: + return img + + +class Normalize: + """ + Normalize the input images. + + Args: + mean(list): Mean values for normalization, default is [0.5, 0.5, 0.5]. + std(list): Standard deviation for normalization, default is [0.5, 0.5, 0.5]. + + Returns: + img(np.ndarray): Preprocessed image. + data(dict): Image info and label info, default is None. + """ + def __init__(self, mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]): + self.mean = mean + self.std = std + if not (isinstance(self.mean, list) and isinstance(self.std, list)): + raise ValueError("{}: input type is invalid.".format(self)) + from functools import reduce + if reduce(lambda x, y: x * y, self.std) == 0: + raise ValueError('{}: std is invalid!'.format(self)) + + def __call__(self, im, data=None): + + mean = np.array(self.mean)[np.newaxis, np.newaxis, :] + std = np.array(self.std)[np.newaxis, np.newaxis, :] + im = normalize(im, mean, std) + + if data is not None: + return im, data + else: + return im + + +class ShuffleBox: + """Shuffle detection information for corresponding input image.""" + def __call__(self, img, data): + gt = np.concatenate([data['gt_boxes'], data['gt_labels'][:, np.newaxis], data['gt_scores'][:, np.newaxis]], + axis=1) + idx = np.arange(gt.shape[0]) + np.random.shuffle(idx) + gt = gt[idx, :] + data['gt_boxes'], data['gt_labels'], data['gt_scores'] = gt[:, :4], gt[:, 4], gt[:, 5] + return img, data diff --git a/paddlehub/process/functional.py b/paddlehub/process/functional.py index 5f1f2f449800af734a717cb5519d631024a99d3e..3a9f41ae611fdb7b75df2edb4d46290fe368d951 100644 --- a/paddlehub/process/functional.py +++ b/paddlehub/process/functional.py @@ -11,13 +11,17 @@ # 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 cv2 import paddle +import matplotlib import numpy as np +from pycocotools.coco import COCO from PIL import Image, ImageEnhance +from matplotlib import pyplot as plt + +matplotlib.use('Agg') def normalize(im, mean, std): @@ -120,6 +124,129 @@ def get_img_file(dir_name: str) -> list: return images +def box_crop(boxes: np.ndarray, labels: np.ndarray, scores: np.ndarray, crop: list, img_shape: list): + """Crop the boxes ,labels, scores according to the given shape""" + + x, y, w, h = map(float, crop) + im_w, im_h = map(float, img_shape) + + boxes = boxes.copy() + boxes[:, 0], boxes[:, 2] = (boxes[:, 0] - boxes[:, 2] / 2) * im_w, (boxes[:, 0] + boxes[:, 2] / 2) * im_w + boxes[:, 1], boxes[:, 3] = (boxes[:, 1] - boxes[:, 3] / 2) * im_h, (boxes[:, 1] + boxes[:, 3] / 2) * im_h + + crop_box = np.array([x, y, x + w, y + h]) + centers = (boxes[:, :2] + boxes[:, 2:]) / 2.0 + mask = np.logical_and(crop_box[:2] <= centers, centers <= crop_box[2:]).all(axis=1) + + boxes[:, :2] = np.maximum(boxes[:, :2], crop_box[:2]) + boxes[:, 2:] = np.minimum(boxes[:, 2:], crop_box[2:]) + boxes[:, :2] -= crop_box[:2] + boxes[:, 2:] -= crop_box[:2] + + mask = np.logical_and(mask, (boxes[:, :2] < boxes[:, 2:]).all(axis=1)) + boxes = boxes * np.expand_dims(mask.astype('float32'), axis=1) + labels = labels * mask.astype('float32') + scores = scores * mask.astype('float32') + boxes[:, 0], boxes[:, 2] = (boxes[:, 0] + boxes[:, 2]) / 2 / w, (boxes[:, 2] - boxes[:, 0]) / w + boxes[:, 1], boxes[:, 3] = (boxes[:, 1] + boxes[:, 3]) / 2 / h, (boxes[:, 3] - boxes[:, 1]) / h + + return boxes, labels, scores, mask.sum() + + +def box_iou_xywh(box1: np.ndarray, box2: np.ndarray) -> float: + """Calculate iou by xywh""" + + assert box1.shape[-1] == 4, "Box1 shape[-1] should be 4." + assert box2.shape[-1] == 4, "Box2 shape[-1] should be 4." + + b1_x1, b1_x2 = box1[:, 0] - box1[:, 2] / 2, box1[:, 0] + box1[:, 2] / 2 + b1_y1, b1_y2 = box1[:, 1] - box1[:, 3] / 2, box1[:, 1] + box1[:, 3] / 2 + b2_x1, b2_x2 = box2[:, 0] - box2[:, 2] / 2, box2[:, 0] + box2[:, 2] / 2 + b2_y1, b2_y2 = box2[:, 1] - box2[:, 3] / 2, box2[:, 1] + box2[:, 3] / 2 + + inter_x1 = np.maximum(b1_x1, b2_x1) + inter_x2 = np.minimum(b1_x2, b2_x2) + inter_y1 = np.maximum(b1_y1, b2_y1) + inter_y2 = np.minimum(b1_y2, b2_y2) + inter_w = inter_x2 - inter_x1 + inter_h = inter_y2 - inter_y1 + inter_w[inter_w < 0] = 0 + inter_h[inter_h < 0] = 0 + + inter_area = inter_w * inter_h + b1_area = (b1_x2 - b1_x1) * (b1_y2 - b1_y1) + b2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1) + + return inter_area / (b1_area + b2_area - inter_area) + + +def draw_boxes_on_image(image_path: str, + boxes: np.ndarray, + scores: np.ndarray, + labels: np.ndarray, + label_names: list, + score_thresh: float = 0.5): + """Draw boxes on images.""" + image = np.array(Image.open(image_path)) + plt.figure() + _, ax = plt.subplots(1) + ax.imshow(image) + + image_name = image_path.split('/')[-1] + print("Image {} detect: ".format(image_name)) + colors = {} + for box, score, label in zip(boxes, scores, labels): + if score < score_thresh: + continue + if box[2] <= box[0] or box[3] <= box[1]: + continue + label = int(label) + if label not in colors: + colors[label] = plt.get_cmap('hsv')(label / len(label_names)) + x1, y1, x2, y2 = box[0], box[1], box[2], box[3] + rect = plt.Rectangle((x1, y1), x2 - x1, y2 - y1, fill=False, linewidth=2.0, edgecolor=colors[label]) + ax.add_patch(rect) + ax.text(x1, + y1, + '{} {:.4f}'.format(label_names[label], score), + verticalalignment='bottom', + horizontalalignment='left', + bbox={ + 'facecolor': colors[label], + 'alpha': 0.5, + 'pad': 0 + }, + fontsize=8, + color='white') + print("\t {:15s} at {:25} score: {:.5f}".format(label_names[int(label)], str(list(map(int, list(box)))), score)) + image_name = image_name.replace('jpg', 'png') + plt.axis('off') + plt.gca().xaxis.set_major_locator(plt.NullLocator()) + plt.gca().yaxis.set_major_locator(plt.NullLocator()) + plt.savefig("./output/{}".format(image_name), bbox_inches='tight', pad_inches=0.0) + print("Detect result save at ./output/{}\n".format(image_name)) + plt.cla() + plt.close('all') + + +def img_shape(img_path: str): + """Get image shape.""" + im = cv2.imread(img_path) + im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) + h, w, c = im.shape + return h, w, c + + +def get_label_infos(file_list: str): + """Get label names by corresponding category ids.""" + map_label = COCO(file_list) + label_names = [] + categories = map_label.loadCats(map_label.getCatIds()) + for category in categories: + label_names.append(category['name']) + return label_names + + def subtract_imagenet_mean_batch(batch: paddle.Tensor) -> paddle.Tensor: """Subtract ImageNet mean pixel-wise from a BGR image.""" mean = np.zeros(shape=batch.shape, dtype='float32') diff --git a/paddlehub/process/transforms.py b/paddlehub/process/transforms.py index d6aea1820700600fe40d86e8a72d1950f450befd..6a49cce46ff1bc7835e5fdcdd441c12099d7f8dc 100644 --- a/paddlehub/process/transforms.py +++ b/paddlehub/process/transforms.py @@ -15,6 +15,7 @@ import os import math + import random import copy from typing import Callable