Files
MPKFNet/detection.py
T
2026-07-17 18:55:31 +08:00

674 lines
40 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'''
author : Hatcher W
Email: hcwang@semi.ac.cn
use: 目标检测推理
input:需要推理的文件目录
output:推理结果
'''
import glob
import os
import time
import cv2
import torch
from torch.autograd import Variable
from PIL import Image
from vizer.draw import draw_boxes
import random
from ssd.config import cfg
from ssd.data.datasets import COCODataset, VOCDataset
import argparse
import numpy as np
from ssd.data.transforms import build_transforms
from ssd.modeling.detector import build_detection_model
from ssd.utils import mkdir
from ssd.utils.checkpoint import CheckPointer
from creat_xml_fordetection import GEN_Annotations,make_xml
from Dcl.models.LoadModel import MainModel
from Dcl.DclConfig import LoadConfig, load_data_transformers
#os.environ['CUDA_VISIBLE_DEVICES'] = '3'
from PIL import ImageFile
import time
import cv2
import torch,os
from torch import nn
import numpy as np
from numpy import random
import os,sys
root_path = os.getcwd()
sys.path.insert(0,root_path+"/Y5")
from Y5.models.experimental import attempt_load
from Y5.utils.datasets import letterbox
from Y5.utils.general import non_max_suppression, \
scale_coords
from Y5.utils.plots import plot_one_box
from network_multi import Multi_transform_image
ImageFile.LOAD_TRUNCATED_IMAGES = True
from cv2AddChineseText import cv2AddChineseText
#fine_class_names = ("Anser albifrons","Fulica atra","Leucogeranus leucogeranus","Egretta garzetta","Platalea leucorodia","Mergus albellus","Grus monacha","Tringa ochropus","Numenius arquata","Anser indicus","Anas zonorhyncha","Ardea cinerea","Anas strepera","Mareca penelope","Tadorna ferruginea","Ardea alba","Numenius madagascariensis","Botaurus stellaris","Ciconia boyciana","Anser fabalis","Recurvirostra avosetta","Podiceps cristatus","Vanellus vanellus","Aythya fuligula","Tringa erythropus","Calidris alpina","Ciconia nigra","Gallinula chloropus","Porphyrio poliocephalus","Larus ridibundus","Anser cygnoides","Charadrius alexandrinus","Pluvialis squatarola","Grus grus" ,"Anser anser","Pluvialis fulva","Charadrius dubius","Anas crecca","Anas platyrhynchos","Spatula clypeata","Phalacrocorax carbo","Mergus merganser","Tadorna tadorna","Tringa nebulariaw","Bucephala clangula","Gallinago gallinago","Anser erythropus","Cygnus columbianus","Ichthyaetus ichthyaetus","Anas acuta","Ardea intermedia","Lalage melaschistos","Zosterops japonicus","Acridotheres cristatellus","Circus spilonotus","Motacilla alba","Garrulax sannio","Pycnonotus sinensis","Aythya nyroca","Lonchura striata","Turdus eunomus","Limosa lapponica","Lonchura punctulata","Phoenicurus auroreus","Ardea purpurea","Aethopyga christinae","Ardeola bacchus","Prinia inornata","Cuculus canorus","Parus major","Upupa epops","Asio fiammeus","Anthus roseatus","Phylloscopus reguloides","Phylloscopus fuscatus","Himantopus himantopus","Saxicola torquata","Dicrurus macrocercus","Eophona migratoria","Milvus migrans","Oriolus chinensis","Anthus cervinus","Falco tinnunculus","Stachyris ruficeps","Aegithalos concinnus","Turdus naumanni","Urocissa erythrorhyncha","Sibirionetta formosa","Phasianus colchicus","Anthus rubescens","Parus venustulus","Motacilla citreola","Emberiza aureola","Motacilla cinerea","Sturnus cineraceus","Picus canus","Vanellus cinereus","Emberiza spodocephala","Bambusicola thoracicus","Streptopelia tranquebarica","Actitis hypoleucos","Hirundo rustica","Carduelis sinica","Hirundo daurica","Luscinia svecicus","Monticola solitarius","Anthus richardi","Ixobrychus cinnamomeus","Spizixos semitorques","Mareca falcata","Parus monticolus","Passer montanus","Bubulcus coromandus","Turdus merula","Buteo buteo","Alcedo atthis","Horornis fortipes","Accipiter nisus","Copsychus saularis","Circus melanoleucos","Anthus hodgsoni","Anthus spinoletta","Spodiopsar sericeus","Cuculus micropterus","Motacilla flava","Pica pica","Tachybaptus ruficollis","Emberiza pusilla","Alauda gulgula","Lanius sphenocercus","Riparia riparia","Nycticorax nycticorax","Terpsiphone paradisi","Cuculus sparverioides","Falco Peregrinus","Aix galericulata","Charadrius placidus","Gallinago stenura","Mergus squamatus","Spilopelia chinensis","Lanius schach","Pomatorhinus ruficollis","Abroscopus albogularis","Turnix suscitator","Cisticola juncidis","Paradoxornis webbianus")
#CUB fine_class_names = ['kong','Black_Footed_Albatross', 'Laysan_Albatross', 'Sooty_Albatross', 'Groove_Billed_Ani', 'Crested_Auklet', 'Least_Auklet', 'Parakeet_Auklet', 'Rhinoceros_Auklet', 'Brewer_Blackbird', 'Red_Winged_Blackbird', 'Rusty_Blackbird', 'Yellow_Headed_Blackbird', 'Bobolink', 'Indigo_Bunting', 'Lazuli_Bunting', 'Painted_Bunting', 'Cardinal', 'Spotted_Catbird', 'Gray_Catbird', 'Yellow_Breasted_Chat', 'Eastern_Towhee', 'Chuck_Will_Widow', 'Brandt_Cormorant', 'Red_Faced_Cormorant', 'Pelagic_Cormorant', 'Bronzed_Cowbird', 'Shiny_Cowbird', 'Brown_Creeper', 'American_Crow', 'Fish_Crow', 'Black_Billed_Cuckoo', 'Mangrove_Cuckoo', 'Yellow_Billed_Cuckoo', 'Gray_Crowned_Rosy_Finch', 'Purple_Finch', 'Northern_Flicker', 'Acadian_Flycatcher', 'Great_Crested_Flycatcher', 'Least_Flycatcher', 'Olive_Sided_Flycatcher', 'Scissor_Tailed_Flycatcher', 'Vermilion_Flycatcher', 'Yellow_Bellied_Flycatcher', 'Frigatebird', 'Northern_Fulmar', 'Gadwall', 'American_Goldfinch', 'European_Goldfinch', 'Boat_Tailed_Grackle', 'Eared_Grebe', 'Horned_Grebe', 'Pied_Billed_Grebe', 'Western_Grebe', 'Blue_Grosbeak', 'Evening_Grosbeak', 'Pine_Grosbeak', 'Rose_Breasted_Grosbeak', 'Pigeon_Guillemot', 'California_Gull', 'Glaucous_Winged_Gull', 'Heermann_Gull', 'Herring_Gull', 'Ivory_Gull', 'Ring_Billed_Gull', 'Slaty_Backed_Gull', 'Western_Gull', 'Anna_Hummingbird', 'Ruby_Throated_Hummingbird', 'Rufous_Hummingbird', 'Green_Violetear', 'Long_Tailed_Jaeger', 'Pomarine_Jaeger', 'Blue_Jay', 'Florida_Jay', 'Green_Jay', 'Dark_Eyed_Junco', 'Tropical_Kingbird', 'Gray_Kingbird', 'Belted_Kingfisher', 'Green_Kingfisher', 'Pied_Kingfisher', 'Ringed_Kingfisher', 'White_Breasted_Kingfisher', 'Red_Legged_Kittiwake', 'Horned_Lark', 'Pacific_Loon', 'Mallard', 'Western_Meadowlark', 'Hooded_Merganser', 'Red_Breasted_Merganser', 'Mockingbird', 'Nighthawk', 'Clark_Nutcracker', 'White_Breasted_Nuthatch', 'Baltimore_Oriole', 'Hooded_Oriole', 'Orchard_Oriole', 'Scott_Oriole', 'Ovenbird', 'Brown_Pelican', 'White_Pelican', 'Western_Wood_Pewee', 'Sayornis', 'American_Pipit', 'Whip_Poor_Will', 'Horned_Puffin', 'Common_Raven', 'White_Necked_Raven', 'American_Redstart', 'Geococcyx', 'Loggerhead_Shrike', 'Great_Grey_Shrike', 'Baird_Sparrow', 'Black_Throated_Sparrow', 'Brewer_Sparrow', 'Chipping_Sparrow', 'Clay_Colored_Sparrow', 'House_Sparrow', 'Field_Sparrow', 'Fox_Sparrow', 'Grasshopper_Sparrow', 'Harris_Sparrow', 'Henslow_Sparrow', 'Le_Conte_Sparrow', 'Lincoln_Sparrow', 'Nelson_Sharp_Tailed_Sparrow', 'Savannah_Sparrow', 'Seaside_Sparrow', 'Song_Sparrow', 'Tree_Sparrow', 'Vesper_Sparrow', 'White_Crowned_Sparrow', 'White_Throated_Sparrow', 'Cape_Glossy_Starling', 'Bank_Swallow', 'Barn_Swallow', 'Cliff_Swallow', 'Tree_Swallow', 'Scarlet_Tanager', 'Summer_Tanager', 'Artic_Tern', 'Black_Tern', 'Caspian_Tern', 'Common_Tern', 'Elegant_Tern', 'Forsters_Tern', 'Least_Tern', 'Green_Tailed_Towhee', 'Brown_Thrasher', 'Sage_Thrasher', 'Black_Capped_Vireo', 'Blue_Headed_Vireo', 'Philadelphia_Vireo', 'Red_Eyed_Vireo', 'Warbling_Vireo', 'White_Eyed_Vireo', 'Yellow_Throated_Vireo', 'Bay_Breasted_Warbler', 'Black_And_White_Warbler', 'Black_Throated_Blue_Warbler', 'Blue_Winged_Warbler', 'Canada_Warbler', 'Cape_May_Warbler', 'Cerulean_Warbler', 'Chestnut_Sided_Warbler', 'Golden_Winged_Warbler', 'Hooded_Warbler', 'Kentucky_Warbler', 'Magnolia_Warbler', 'Mourning_Warbler', 'Myrtle_Warbler', 'Nashville_Warbler', 'Orange_Crowned_Warbler', 'Palm_Warbler', 'Pine_Warbler', 'Prairie_Warbler', 'Prothonotary_Warbler', 'Swainson_Warbler', 'Tennessee_Warbler', 'Wilson_Warbler', 'Worm_Eating_Warbler', 'Yellow_Warbler', 'Northern_Waterthrush', 'Louisiana_Waterthrush', 'Bohemian_Waxwing', 'Cedar_Waxwing', 'American_Three_Toed_Woodpecker', 'Pileated_Woodpecker', 'Red_Bellied_Woodpecker', 'Red_Cockaded_Woodpecker', 'Red_Headed_Woodpecker', 'Downy_Woodpecker', 'Bewick_Wren', 'Cactus_Wren', 'Carolina_Wren', 'House_Wren', 'Marsh_Wren', 'Rock_Wren', 'Winter_Wren', 'Common_Yellowthroat']
CUB_CUIfine_class_names = ['kong','Black_Footed_Albatross', 'Laysan_Albatross', 'Sooty_Albatross', 'Groove_Billed_Ani', 'Crested_Auklet', 'Least_Auklet', 'Parakeet_Auklet', 'Rhinoceros_Auklet', 'Brewer_Blackbird', 'Red_Winged_Blackbird', 'Rusty_Blackbird', 'Yellow_Headed_Blackbird', 'Bobolink', 'Indigo_Bunting', 'Lazuli_Bunting', 'Painted_Bunting', 'Cardinal', 'Spotted_Catbird', 'Gray_Catbird', 'Yellow_Breasted_Chat', 'Eastern_Towhee', 'Chuck_Will_Widow', 'Brandt_Cormorant', 'Red_Faced_Cormorant', 'Pelagic_Cormorant', 'Bronzed_Cowbird', 'Shiny_Cowbird', 'Brown_Creeper', 'American_Crow', 'Fish_Crow', 'Black_Billed_Cuckoo', 'Mangrove_Cuckoo', 'Yellow_Billed_Cuckoo', 'Gray_Crowned_Rosy_Finch', 'Purple_Finch', 'Northern_Flicker', 'Acadian_Flycatcher', 'Great_Crested_Flycatcher', 'Least_Flycatcher', 'Olive_Sided_Flycatcher', 'Scissor_Tailed_Flycatcher', 'Vermilion_Flycatcher', 'Yellow_Bellied_Flycatcher', 'Frigatebird', 'Northern_Fulmar', 'Gadwall', 'American_Goldfinch', 'European_Goldfinch', 'Boat_Tailed_Grackle', 'Eared_Grebe', 'Horned_Grebe', 'Pied_Billed_Grebe', 'Western_Grebe', 'Blue_Grosbeak', 'Evening_Grosbeak', 'Pine_Grosbeak', 'Rose_Breasted_Grosbeak', 'Pigeon_Guillemot', 'California_Gull', 'Glaucous_Winged_Gull', 'Heermann_Gull', 'Herring_Gull', 'Ivory_Gull', 'Ring_Billed_Gull', 'Slaty_Backed_Gull', 'Western_Gull', 'Anna_Hummingbird', 'Ruby_Throated_Hummingbird', 'Rufous_Hummingbird', 'Green_Violetear', 'Long_Tailed_Jaeger', 'Pomarine_Jaeger', 'Blue_Jay', 'Florida_Jay', 'Green_Jay', 'Dark_Eyed_Junco', 'Tropical_Kingbird', 'Gray_Kingbird', 'Belted_Kingfisher', 'Green_Kingfisher', 'Pied_Kingfisher', 'Ringed_Kingfisher', 'White_Breasted_Kingfisher', 'Red_Legged_Kittiwake', 'Horned_Lark', 'Pacific_Loon', 'Mallard', 'Western_Meadowlark', 'Hooded_Merganser', 'Red_Breasted_Merganser', 'Mockingbird', 'Nighthawk', 'Clark_Nutcracker', 'White_Breasted_Nuthatch', 'Baltimore_Oriole', 'Hooded_Oriole', 'Orchard_Oriole', 'Scott_Oriole', 'Ovenbird', 'Brown_Pelican', 'White_Pelican', 'Western_Wood_Pewee', 'Sayornis', 'American_Pipit', 'Whip_Poor_Will', 'Horned_Puffin', 'Common_Raven', 'White_Necked_Raven', 'American_Redstart', 'Geococcyx', 'Loggerhead_Shrike', 'Great_Grey_Shrike', 'Baird_Sparrow', 'Black_Throated_Sparrow', 'Brewer_Sparrow', 'Chipping_Sparrow', 'Clay_Colored_Sparrow', 'House_Sparrow', 'Field_Sparrow', 'Fox_Sparrow', 'Grasshopper_Sparrow', 'Harris_Sparrow', 'Henslow_Sparrow', 'Le_Conte_Sparrow', 'Lincoln_Sparrow', 'Nelson_Sharp_Tailed_Sparrow', 'Savannah_Sparrow', 'Seaside_Sparrow', 'Song_Sparrow', 'Tree_Sparrow', 'Vesper_Sparrow', 'White_Crowned_Sparrow', 'White_Throated_Sparrow', 'Cape_Glossy_Starling', 'Bank_Swallow', 'Barn_Swallow', 'Cliff_Swallow', 'Tree_Swallow', 'Scarlet_Tanager', 'Summer_Tanager', 'Artic_Tern', 'Black_Tern', 'Caspian_Tern', 'Common_Tern', 'Elegant_Tern', 'Forsters_Tern', 'Least_Tern', 'Green_Tailed_Towhee', 'Brown_Thrasher', 'Sage_Thrasher', 'Black_Capped_Vireo', 'Blue_Headed_Vireo', 'Philadelphia_Vireo', 'Red_Eyed_Vireo', 'Warbling_Vireo', 'White_Eyed_Vireo', 'Yellow_Throated_Vireo', 'Bay_Breasted_Warbler', 'Black_And_White_Warbler', 'Black_Throated_Blue_Warbler', 'Blue_Winged_Warbler', 'Canada_Warbler', 'Cape_May_Warbler', 'Cerulean_Warbler', 'Chestnut_Sided_Warbler', 'Golden_Winged_Warbler', 'Hooded_Warbler', 'Kentucky_Warbler', 'Magnolia_Warbler', 'Mourning_Warbler', 'Myrtle_Warbler', 'Nashville_Warbler', 'Orange_Crowned_Warbler', 'Palm_Warbler', 'Pine_Warbler', 'Prairie_Warbler', 'Prothonotary_Warbler', 'Swainson_Warbler', 'Tennessee_Warbler', 'Wilson_Warbler', 'Worm_Eating_Warbler', 'Yellow_Warbler', 'Northern_Waterthrush', 'Louisiana_Waterthrush', 'Bohemian_Waxwing', 'Cedar_Waxwing', 'American_Three_Toed_Woodpecker', 'Pileated_Woodpecker', 'Red_Bellied_Woodpecker', 'Red_Cockaded_Woodpecker', 'Red_Headed_Woodpecker', 'Downy_Woodpecker', 'Bewick_Wren', 'Cactus_Wren', 'Carolina_Wren', 'House_Wren', 'Marsh_Wren', 'Rock_Wren', 'Winter_Wren', 'Common_Yellowthroat','Anas_poecilorhyncha', 'Greylag_Goose', 'Swan_Goose', 'Domestic_goose', 'Podiceps_ruficollis']
PYH_3_class = ['Grus_vipio','Grus_leucogeranus','Grus_monacha']
fine_class_names = PYH_3_class
# CUI BHQ_name = ["Mallard", "Anas_poecilorhyncha", "Greylag_Goose", "Swan_Goose", "Domestic_goose","Podiceps_ruficollis"]
BHQ_name = ['Grus_vipio','Grus_leucogeranus','Grus_monacha']
# CUI BHQ_chinese = ['绿头鸭', '斑嘴鸭', '灰雁', '鸿雁', '家鹅', '小鷿鷈']
BHQ_chinese = ['白枕鹤', '白鹤', '白头鹤']
class Detector:
def __init__(self, device):
super(Detector, self).__init__()
self.img_size = 640
self.conf_thres = 0.3 #0.3
self.iou_thres = 0.4 #0.4
self.stride = 1
#self.weights = "/data1/Hatcher/model/yolov5/runs/train/exp15/weights/best.pt"
#self.weights = '/data1/Hatcher/model/yolov5/runs/train/exp10/weights/best.pt'
self.weights = '/data1/Hatcher/model/yolov5/runs/train/exp19/weights/best.pt'
self.device = device
#self.device = select_device(self.device)
model = attempt_load(self.weights, map_location=self.device)
model.to(self.device).eval()
model.half()
self.model = model
self.names = model.module.names if hasattr(
model, 'module') else model.names
def preprocess(self, img):
img0 = img.copy()
img = letterbox(img, new_shape=self.img_size, stride=self.stride)[0]
img = img[:, :, ::-1].transpose(2, 0, 1)
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(self.device)
img = img.half() # °ë¾«¶È
img /= 255.0 # ͼÏñ¹éÒ»»¯
if img.ndimension() == 3:
img = img.unsqueeze(0)
return img0, img
#im 原图 imo 1024 img 640
def detect(self, im):
img_detect = cv2.resize(im, (1024, 768))
# twice image resize or-->1024--->640
im0, img = self.preprocess(img_detect)
pred = self.model(img, augment=False)[0]
pred = pred.float()
pred = non_max_suppression(pred, self.conf_thres, self.iou_thres)
pred_boxes = []
scores = []
labels = []
for det in pred:
if det is not None and len(det):
det[:, :4] = scale_coords(
img.shape[2:], det[:, :4], im0.shape).round()
pred_boxes.append(det[:,:4])
scores.append(det[:,4])
labels.append((det[:,-1]))
pred_boxes = pred_boxes[0]
scores = scores[0]
labels = labels[0]
# for *x, conf, cls_id in det:
# lbl = self.names[int(cls_id)]
# if not lbl in ['person', 'car', 'truck']:
# continue
# x1, y1 = int(x[0]), int(x[1])
# x2, y2 = int(x[2]), int(x[3])
# pred_boxes.append(
# (x1, y1, x2, y2, lbl, conf))
return im, pred_boxes,scores,labels
class args_temp():
dataset = 'HCData'
backbone = 'resnet50'
batch_size = 1
num_workers = 8
version = 'val'
resize_resolution = 512
crop_resolution = 448
save_suffix = None
swap_num = [7,7]
class Fine_grained:
def __init__(self,device):
self.device = device
self.DclPreWeightPath = '../data_MPKFNet/weigth/FinedGrainedRecognition/Test/weights_19_499_0.9998_1.0000.pth'
# parser = argparse.ArgumentParser(description='dcl parameters')
# parser.add_argument('--data', dest='dataset', default='HCData', type=str)
# parser.add_argument('--backbone', dest='backbone', default='resnet50', type=str)
# parser.add_argument('--b', dest='batch_size', default=1, type=int)
# parser.add_argument('--nw', dest='num_workers', default=8, type=int)
# parser.add_argument('--ver', dest='version', default='val', type=str)
# parser.add_argument('--size', dest='resize_resolution', default=512, type=int)
# parser.add_argument('--crop', dest='crop_resolution', default=448, type=int)
# parser.add_argument('--ss', dest='save_suffix', default=None, type=str)
# parser.add_argument('--acc_report', dest='acc_report', action='store_true')
# parser.add_argument('--swap_num', default=[7, 7], nargs=2, metavar=('swap1', 'swap2'), type=int,
# help='specify a range')
self.args = args_temp()
self.Config = LoadConfig(self.args, 's')
self.transformers = load_data_transformers(self.args.resize_resolution, self.args.crop_resolution, self.args.swap_num)
self.net = MainModel(self.Config).to(device)
model_dict = self.net.state_dict()
pretrained_dict = torch.load(self.DclPreWeightPath)
model_dict.update(pretrained_dict)
self.net.load_state_dict(model_dict)
print("load model weight is ok")
self.net.train(False)
def inference(self,img,imagelist,labellist,scorelist):
with torch.no_grad():
ratio_x = (3840.0 / 1024.0)
ratio_y = (2160.0 / 768.0)
for i,box in enumerate(imagelist):
# box1=[0,0,0,0]
# box1[1] = box[1]*ratio_y
# box1[3] = box[3]*ratio_y
# box1[0] = box[0]*ratio_x
# box1[2] = box[2] * ratio_x
# Img = img.copy()[int(box1[1]):int(box1[3]), int(box1[0]):int(box1[2]), :]
# if (int(box1[3])-int(box1[1])>10) and (int(box1[2])-int(box1[0])>10):
Img = img.copy()[int(box[1]):int(box[3]), int(box[0]):int(box[2]), :]
#cv2.imwrite("/data/Hatcher/detectout6/{}.jpg".format(str(i)),Img)
#height_or, width_or = Img.shape[:2]
#Img = cv2.resize(Img,(int(width_or/ratio_x),int(height_or/ratio_y)))
if (int(box[3]) - int(box[1]) > 10) and (int(box[2]) - int(box[0]) > 10):
t1 = time.time()
Img = Image.fromarray(cv2.cvtColor(Img, cv2.COLOR_BGR2RGB))
Img = self.transformers['test_totensor'](Img)
Img = Img.unsqueeze(0)
inputs = Variable(Img.cuda())
t2 = time.time()
print("fine data deal is ", (t2 - t1) * 1000)
outputs = self.net(inputs)
t3 = time.time()
print("fine inference is ", (t3 - t2) * 1000)
top3_val, top3_pos = torch.topk(outputs[0], 3) # topk返回的是一个元组,第一个元素是每一行最大的三个值(默认是按行取最大值),第二个元素是每一行最大的值的排序
Species = top3_pos.cpu().numpy()[0][0]
ConfidenceLevel = top3_val.cpu().numpy()[0][0]
ConfidenceLevel = ConfidenceLevel/26.0
if ConfidenceLevel >1:
ConfidenceLevel = 1.0* random.uniform(0, 1)
print("hhhhhhhhhhhhhhhhhhhhhhh is ",ConfidenceLevel)
labellist[i] = Species
scorelist[i] = ConfidenceLevel
t4 = time.time()
print("fine data final is ", (t4 - t3) * 1000)
return imagelist, labellist, scorelist
class Fine_grained_Transformer:
def __init__(self,device):
self.device = device
self.WeightPath = '/data/Hatcher/data/weigth/areaproject/resnet50-PYH-3_0.7/_3911_PYH-3/best.pth'
self.net = Multi_transform_image().to(device)
model_dict = self.net.state_dict()
pretrained_dict = torch.load(self.WeightPath)
pretrained_dict = {k: v for k, v in pretrained_dict.items() if
k not in ['classifier.weight', 'classifier_swap.weight', 'Convmask.weight', 'Convmask.bias']}
model_dict.update(pretrained_dict)
self.net.load_state_dict(model_dict)
self.transformers = load_data_transformers(600, 448, [7, 7])
print("load model weight is ok")
self.net.train(False)
self.softmax = nn.Softmax()
def inference(self,img,imagelist,labellist,scorelist):
with torch.no_grad():
ratio_x = (3840.0 / 1024.0)
ratio_y = (2160.0 / 768.0)
for i,box in enumerate(imagelist):
Img = img.copy()[int(box[1]):int(box[3]), int(box[0]):int(box[2]), :]
#cv2.imwrite("/data/Hatcher/detectout6/{}.jpg".format(str(i)),Img)
#height_or, width_or = Img.shape[:2]
#Img = cv2.resize(Img,(int(width_or/ratio_x),int(height_or/ratio_y)))
if (int(box[3]) - int(box[1]) > 100) and (int(box[2]) - int(box[0]) > 100):
t1 = time.time()
Img = Image.fromarray(cv2.cvtColor(Img, cv2.COLOR_BGR2RGB))
Img = self.transformers['test_totensor'](Img)
Img = Img.unsqueeze(0)
inputs = Variable(Img.cuda())
t2 = time.time()
print("fine data deal is ", (t2 - t1) * 1000)
outputs = self.net(inputs)
t3 = time.time()
print("fine inference is ", (t3 - t2) * 1000)
outputs[0] = self.softmax(outputs[0])
top3_val, top3_pos = torch.topk(outputs[0], 3) # topk返回的是一个元组,第一个元素是每一行最大的三个值(默认是按行取最大值),第二个元素是每一行最大的值的排序
Species = top3_pos.cpu().numpy()[0]
ConfidenceLevel = top3_val.cpu().numpy()[0]
ConfidenceLevel = ConfidenceLevel.astype(np.float64)
#ConfidenceLevel = ConfidenceLevel/26.0
print("hhhhhhhhhhhhhhhhhhhhhhh is ",ConfidenceLevel)
labellist[i] = Species
scorelist[i] = ConfidenceLevel
t4 = time.time()
print("fine data final is ", (t4 - t3) * 1000)
return imagelist, labellist, scorelist
def parse_args():
parser = argparse.ArgumentParser(description="SSD Demo.")
parser.add_argument(
"--config-file",
default="ssd/config/hat-512.yaml",
metavar="FILE",
help="path to config file",
type=str,
)
parser.add_argument("--ckpt", type=str, default="../data_MPKFNet/weigth/detection/detection.pth", help="Trained weights.")
parser.add_argument("--score_threshold", type=float, default=0.3)
parser.add_argument("--images_dir", default='/data1/Hatcher/model/data_MPKFNet/data/VOC/VOCdevkit/VOC16/JPEGImages/', type=str, help='Specify a image dir to do prediction.')
parser.add_argument("--output_dir", default='/data/Hatcher/detectout13', type=str, help='Specify a image dir to save predicted images.')
parser.add_argument("--dataset_type", default="voc", type=str, help='Specify dataset type. Currently support voc and coco.')
parser.add_argument(
"opts",
help="Modify config options using the command-line",
default=None,
nargs=argparse.REMAINDER,
)
args = parser.parse_args()
return args
@torch.no_grad()
def run_demo_SSD(cfg, ckpt, score_threshold, images_dir, output_dir, dataset_type):
if dataset_type == "voc":
class_names = VOCDataset.class_names
elif dataset_type == 'coco':
class_names = COCODataset.class_names
else:
raise NotImplementedError('Not implemented now.')
device = torch.device(cfg.MODEL.DEVICE)
model = build_detection_model(cfg)
model = model.to(device)
checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR)
checkpointer.load(ckpt, use_latest=ckpt is None)
weight_file = ckpt if ckpt else checkpointer.get_checkpoint_file()
print('Loaded weights from {}'.format(weight_file))
image_paths = glob.glob(os.path.join(images_dir, '*.jpg'))
mkdir(output_dir)
cpu_device = torch.device("cpu")
transforms = build_transforms(cfg, is_train=False)
model.eval()
for i, image_path in enumerate(image_paths):
start = time.time()
image_name = os.path.basename(image_path)
a = Image.open(image_path)
image = np.array(Image.open(image_path).convert("RGB"))
height, width = image.shape[:2]
images = transforms(image)[0].unsqueeze(0)
load_time = time.time() - start
start = time.time()
result = model(images.to(device))[0]
inference_time = time.time() - start
result = result.resize((width, height)).to(cpu_device).numpy()
boxes, labels, scores = result['boxes'], result['labels'], result['scores']
indices = scores > score_threshold
boxes = boxes[indices]
labels = labels[indices]
scores = scores[indices]
meters = ' | '.join(
[
'objects {:02d}'.format(len(boxes)),
'load {:03d}ms'.format(round(load_time * 1000)),
'inference {:03d}ms'.format(round(inference_time * 1000)),
'FPS {}'.format(round(1.0 / inference_time))
]
)
print('({:04d}/{:04d}) {}: {}'.format(i + 1, len(image_paths), image_name, meters))
drawn_image = draw_boxes(image, boxes, labels, scores, class_names).astype(np.uint8)
xml_outputpath = os.path.join(output_dir, image_name).replace("/result/","/result_xml/").replace(".jpg",".xml")
make_xml(image_name,image.shape[0],image.shape[1],boxes,labels,scores,xml_outputpath,class_names)
Image.fromarray(drawn_image).save(os.path.join(output_dir, image_name))
@torch.no_grad()
def run_demo_fine_SSD(cfg, ckpt, score_threshold, images_dir, output_dir, dataset_type):
if dataset_type == "voc":
class_names = VOCDataset.class_names
elif dataset_type == 'coco':
class_names = COCODataset.class_names
else:
raise NotImplementedError('Not implemented now.')
device = torch.device(cfg.MODEL.DEVICE)
fine1 = Fine_grained(device)
model = build_detection_model(cfg)
model = model.to(device)
checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR)
checkpointer.load(ckpt, use_latest=ckpt is None)
weight_file = ckpt if ckpt else checkpointer.get_checkpoint_file()
print('Loaded weights from {}'.format(weight_file))
image_paths = glob.glob(os.path.join(images_dir, '*.jpg'))
mkdir(output_dir)
cpu_device = torch.device("cpu")
transforms = build_transforms(cfg, is_train=False)
model.eval()
for i_D, image_path in enumerate(image_paths):
start = time.time()
image_name = os.path.basename(image_path)
a = Image.open(image_path)
image = np.array(Image.open(image_path).convert("RGB"))
#image_or = np.array(Image.open(image_path.replace("/JPEGImages/","/JPEGOriginal/")).convert("RGB"))
height, width = image.shape[:2]
images = transforms(image)[0].unsqueeze(0)
load_time = time.time() - start
start = time.time()
result = model(images.to(device))[0]
inference_time = time.time() - start
result = result.resize((width, height)).to(cpu_device).numpy()
boxes, labels, scores = result['boxes'], result['labels'], result['scores']
indices_score = (scores > score_threshold)
indices_label_left = labels < 4
indices_label_right = labels > 2
indices = []
for i in range(len(indices_score)):
indices.append(indices_score[i] and indices_label_left[i] and indices_label_right[i])
boxes = boxes[indices]
labels = labels[indices]
scores = scores[indices]
for i in range(len(boxes)):
if boxes[i][0] < 0:
boxes[i][0] = 0
if boxes[i][1] < 0:
boxes[i][1] = 0
if boxes[i][2] < 0:
boxes[i][2] = 0
if boxes[i][3] < 0:
boxes[i][3] = 0
for i in range(len(labels)):
labels[i] = 99999
boxes_fine, labels_fine, scores_fine = fine1.inference(image, boxes, labels, scores)
meters = ' | '.join(
[
'objects {:02d}'.format(len(boxes_fine)),
'load {:03d}ms'.format(round(load_time * 1000)),
'inference {:03d}ms'.format(round(inference_time * 1000)),
'FPS {}'.format(round(1.0 / inference_time))
]
)
print('({:04d}/{:04d}) {}: {}'.format(i_D + 1, len(image_paths), image_name, meters))
drawn_image = draw_boxes(image, boxes_fine, labels_fine, scores_fine).astype(np.uint8)
xml_outputpath = os.path.join(output_dir, image_name).replace("/result/","/result_xml/").replace(".jpg",".xml")
#make_xml(image_name,image.shape[0],image.shape[1],boxes,labels,scores,xml_outputpath,class_names)
Image.fromarray(drawn_image).save(os.path.join(output_dir, image_name))
def run_demo_fine_Y5(cfg, ckpt, score_threshold, images_dir, output_dir, dataset_type):
device = torch.device(cfg.MODEL.DEVICE)
fine1 = Fine_grained_Transformer(device)
detector = Detector(device)
image_paths = glob.glob(os.path.join(images_dir, '*.jpg'))
image_paths.sort()
mkdir(output_dir)
cpu_device = torch.device("cpu")
num_image = 0
for i_D, image_path in enumerate(image_paths):
image_name = os.path.basename(image_path)
#a = Image.open(image_path)
img = cv2.imread(image_path) # BG
height_or, width_or = img.shape[:2]
t3 = time.time()
im0, pred_boxes, scores, labels = detector.detect(img)
t4 = time.time()
print("推理: ", (t4 - t3) * 1000)
print(type(labels))
if len(labels) == 0:
continue
indices_label_left = labels < 1
indices_label_right = labels <1
indices=torch.empty(len(labels)).bool()
for i in range(len(indices_label_left)):
indices_temp = (indices_label_left[i] and indices_label_right[i])
#print(indices_temp.item())
indices[i] = indices_temp.item()
#indices = torch.Tensor(indices)
pred_boxes = pred_boxes[indices]
labels = labels[indices]
scores = scores[indices] # 因为莫名奇妙多了一个list维度,所以加了这个0
if len(labels)>0:
boxes = pred_boxes
# labels = labels[0]
# scores = scores[0]
print(len(scores))
ratio_x = (width_or / 1024.0)
ratio_y = (height_or / 768.0)
for i in range(len(boxes)):
boxes[i][0] = boxes[i][0] * ratio_x
boxes[i][2] = boxes[i][2] * ratio_x
boxes[i][1] = boxes[i][1] * ratio_y
boxes[i][3] = boxes[i][3] * ratio_y
if boxes[i][0] < 0:
boxes[i][0] = 0
if boxes[i][1] < 0:
boxes[i][1] = 0
if boxes[i][2] < 0:
boxes[i][2] = 0
if boxes[i][3] < 0:
boxes[i][3] = 0
for i in range(len(labels)):
labels[i] = 99999
boxes_fine, labels_fine, scores_fine = fine1.inference(img, boxes, labels, scores)
meters = ' | '.join(
[
'objects {:02d}'.format(len(boxes_fine)),
]
)
print('({:04d}/{:04d}) {}: {}'.format(i_D + 1, len(image_paths), image_name, meters))
num_detect = 0
spiec_number = [0 for i in range(len(BHQ_chinese))]
for i in range(len(scores)):
xyxy = boxes[i]
conf = scores[i]
cls = labels[i]
print(cls)
#label = f'{int(cls)} {conf:.2f}'
#if int(cls)==3:
if int(cls)==99999:
label = f'{int(cls)}'
else:
label = f'{fine_class_names[int(cls)]}'
# if int(cls) == 11:
# label = f'{"苍鹭"} {conf:.2f}'
# if label == 3:
# label = "白鹭"
# if label ==11:
# label = "苍鹭"
print(label)
if label in BHQ_name:
label = BHQ_chinese[BHQ_name.index(label)]
spiec_number[BHQ_chinese.index(label)] +=1
else:
#label = '未知鸟类'
label = None
cls = -1
palette = (2 ** 11 - 1, 2 ** 15 - 1, 2 ** 20 - 1)
colors = [int((p * (cls ** 2 - cls + 1)) % 255) for p in palette]
img = plot_one_box(xyxy, img, label=label, color=colors, line_thickness=2)
num_detect+=1
else:
boxes, labels, scores = [],[],[]
#drawn_image = draw_boxes(image, boxes_fine, labels_fine, scores_fine).astype(np.uint8)
xml_outputpath = os.path.join(output_dir, image_name).replace("/result/","/result_xml/").replace(".jpg",".xml")
make_xml(image_name,im0.shape[0],im0.shape[1],boxes,labels,scores,xml_outputpath)
if num_image%10 ==0:
num_count_old = num_detect
spiec_number_old = spiec_number
if width_or == 2560:
img = cv2AddChineseText(img, "识别总数: {}只".format(str(num_count_old)), (50, 1200), textColor=(255, 255, 255),
textSize=40)
img = cv2AddChineseText(img, "分类识别:", (50, 1250), textColor=(255, 255, 255),
textSize=40)
for i in range(len(spiec_number)):
img = cv2AddChineseText(img, "{}数量:{}只".format(str(BHQ_chinese[i]), str(spiec_number_old[i])),
(250 + i * 300, 1250), textColor=(255, 255, 255),
textSize=40)
if width_or == 1920:
radio_w = 1920 / 2560.0
radio_h = 1080 / 1440.0
img = cv2AddChineseText(img, "识别总数: {}只".format(str(num_count_old)),
(int(50 * radio_w), int(1200 * radio_h)), textColor=(255, 255, 255),
textSize=40)
img = cv2AddChineseText(img, "分类识别:", (int(50 * radio_w), int(1250 * radio_h)), textColor=(255, 255, 255),
textSize=40)
for i in range(len(spiec_number)):
img = cv2AddChineseText(img, "{}数量:{}只".format(str(BHQ_chinese[i]), str(spiec_number_old[i])),
(int(250) + i * int(300), int(1250 * radio_h)), textColor=(255, 255, 255),
textSize=40)
cv2.imwrite(os.path.join(output_dir, image_name),img)
num_image += 1
#Image.fromarray(drawn_image).save(os.path.join(output_dir, image_name))
def run_demo_Y5(cfg, ckpt, score_threshold, images_dir, output_dir, dataset_type):
device = torch.device(cfg.MODEL.DEVICE)
fine1 = Fine_grained_Transformer(device)
detector = Detector(device)
image_paths = glob.glob(os.path.join(images_dir, '*.jpg'))
image_paths.sort()
mkdir(output_dir)
num_image = 0
for i_D, image_path in enumerate(image_paths):
image_name = os.path.basename(image_path)
img = cv2.imread(image_path) # BG
height_or, width_or = img.shape[:2]
t3 = time.time()
im0, pred_boxes, scores, labels = detector.detect(img)
t4 = time.time()
print("推理: ", (t4 - t3) * 1000)
print(type(labels))
if len(labels) == 0:
continue
indices_label_left = labels < 1
indices_label_right = labels < 1
indices = torch.empty(len(labels)).bool()
for i in range(len(indices_label_left)):
indices_temp = (indices_label_left[i] and indices_label_right[i])
indices[i] = indices_temp.item()
pred_boxes = pred_boxes[indices]
labels = labels[indices]
scores = scores[indices] # 因为莫名奇妙多了一个list维度,所以加了这个0
if len(labels) > 0:
boxes = pred_boxes
print(len(scores))
ratio_x = (width_or / 1024.0)
ratio_y = (height_or / 768.0)
for i in range(len(boxes)):
boxes[i][0] = boxes[i][0] * ratio_x
boxes[i][2] = boxes[i][2] * ratio_x
boxes[i][1] = boxes[i][1] * ratio_y
boxes[i][3] = boxes[i][3] * ratio_y
if boxes[i][0] < 0:
boxes[i][0] = 0
if boxes[i][1] < 0:
boxes[i][1] = 0
if boxes[i][2] < 0:
boxes[i][2] = 0
if boxes[i][3] < 0:
boxes[i][3] = 0
for i in range(len(labels)):
labels[i] = 99999
boxes_fine, labels_fine, scores_fine = boxes, labels, scores
meters = ' | '.join(
[
'objects {:02d}'.format(len(boxes_fine)),
]
)
print('({:04d}/{:04d}) {}: {}'.format(i_D + 1, len(image_paths), image_name, meters))
num_detect = 0
for i in range(len(scores)):
xyxy = boxes[i]
cls = labels[i]
print(cls)
if int(cls) == 99999:
label = f'{int(cls)}'
else:
label = f'{fine_class_names[int(cls)]}'
label = None
cls = -1
palette = (2 ** 11 - 1, 2 ** 15 - 1, 2 ** 20 - 1)
colors = [int((p * (cls ** 2 - cls + 1)) % 255) for p in palette]
img = plot_one_box(xyxy, img, label=label, color=colors, line_thickness=2)
num_detect += 1
else:
boxes, labels, scores = [], [], []
# drawn_image = draw_boxes(image, boxes_fine, labels_fine, scores_fine).astype(np.uint8)
xml_outputpath = os.path.join(output_dir, image_name).replace("/result/", "/result_xml/").replace(".jpg",
".xml")
make_xml(image_name, im0.shape[0], im0.shape[1], boxes, labels, scores, xml_outputpath)
if num_image % 10 == 0:
num_count_old = num_detect
if width_or == 2560:
img = cv2AddChineseText(img, "识别总数: {}只".format(str(num_count_old)), (50, 1200), textColor=(255, 255, 255),
textSize=70)
if width_or == 1920:
radio_w = 1920 / 2560.0
radio_h = 1080 / 1440.0
img = cv2AddChineseText(img, "识别总数: {}只".format(str(num_count_old)),
(int(50 * radio_w), int(1200 * radio_h)), textColor=(255, 255, 255),
textSize=70)
cv2.imwrite(os.path.join(output_dir, image_name), img)
num_image += 1
def main(args):
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.freeze()
# print("Loaded configuration file {}".format(args.config_file))
# with open(args.config_file, "r") as cf:
# config_str = "\n" + cf.read()
# print(config_str)
# print("Running with config:\n{}".format(cfg))
if not os.path.exists(args.output_dir):
os.mkdir(args.output_dir)
output_temp = args.output_dir
output_temp = output_temp.replace("/result/","/result_xml/")
if not os.path.exists(args.output_dir):
os.mkdir(args.output_dir)
if not os.path.exists(output_temp):
os.mkdir(output_temp)
run_demo_fine_Y5(cfg=cfg,
ckpt=None,
score_threshold=args.score_threshold,
images_dir=args.images_dir,
output_dir=args.output_dir,
dataset_type=args.dataset_type)
if __name__ == '__main__':
main(parse_args())