forked from wffjwbbf/ComDesignProject
manually merge the branch of evalution
This commit is contained in:
parent
2276a6a511
commit
49ace92524
|
|
@ -1,3 +0,0 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="jdk" jdkName="Python 3.9 (python)" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="ignoredPackages">
|
||||
<value>
|
||||
<list size="20">
|
||||
<item index="0" class="java.lang.String" itemvalue="scipy" />
|
||||
<item index="1" class="java.lang.String" itemvalue="six" />
|
||||
<item index="2" class="java.lang.String" itemvalue="threadpoolctl" />
|
||||
<item index="3" class="java.lang.String" itemvalue="Werkzeug" />
|
||||
<item index="4" class="java.lang.String" itemvalue="sklearn" />
|
||||
<item index="5" class="java.lang.String" itemvalue="wrapt" />
|
||||
<item index="6" class="java.lang.String" itemvalue="gast" />
|
||||
<item index="7" class="java.lang.String" itemvalue="MarkupSafe" />
|
||||
<item index="8" class="java.lang.String" itemvalue="numpy" />
|
||||
<item index="9" class="java.lang.String" itemvalue="tensorflow-io-gcs-filesystem" />
|
||||
<item index="10" class="java.lang.String" itemvalue="gunicorn" />
|
||||
<item index="11" class="java.lang.String" itemvalue="click" />
|
||||
<item index="12" class="java.lang.String" itemvalue="Jinja2" />
|
||||
<item index="13" class="java.lang.String" itemvalue="charset-normalizer" />
|
||||
<item index="14" class="java.lang.String" itemvalue="keras" />
|
||||
<item index="15" class="java.lang.String" itemvalue="tensorflow-cpu" />
|
||||
<item index="16" class="java.lang.String" itemvalue="itsdangerous" />
|
||||
<item index="17" class="java.lang.String" itemvalue="tf-estimator-nightly" />
|
||||
<item index="18" class="java.lang.String" itemvalue="Flask" />
|
||||
<item index="19" class="java.lang.String" itemvalue="google-auth" />
|
||||
</list>
|
||||
</value>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="MarkdownSettingsMigration">
|
||||
<option name="stateVersion" value="1" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (python)" project-jdk-type="Python SDK" />
|
||||
</project>
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/ReIDMixLearning.iml" filepath="$PROJECT_DIR$/.idea/ReIDMixLearning.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,121 @@
|
|||
import matplotlib.pyplot as plt
|
||||
import torch
|
||||
from torchvision import transforms
|
||||
import numpy as np
|
||||
import cv2
|
||||
import os
|
||||
|
||||
from model import my_model
|
||||
import param
|
||||
|
||||
|
||||
def extractidfeature(id_path: str, extractor, p_device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")):
|
||||
img_id = cv2.imread(id_path)
|
||||
raw_transformer = transforms.Compose([
|
||||
transforms.ToPILImage(),
|
||||
transforms.Resize((128,64)),# hxw
|
||||
transforms.ToTensor(),
|
||||
])
|
||||
tensor_id = extractor(raw_transformer(img_id).unsqueeze(dim=0).to(p_device))
|
||||
|
||||
return tensor_id.detach().cpu().numpy()
|
||||
|
||||
if __name__ == '__main__':
|
||||
my_option = param.Parameters()
|
||||
|
||||
extractor = my_model.RestNet18() # load model in head
|
||||
extractor.load_state_dict(torch.load(my_option.weights_reid))
|
||||
extractor = extractor.to(my_option.device)
|
||||
|
||||
# gallery_index = 3
|
||||
gallery_path = f"./mydataset/gallery/"
|
||||
classes = os.listdir(gallery_path)
|
||||
cmc_sum = []
|
||||
cmc_final = []
|
||||
for query_index in range(1, 6+1): #query是从1开始的
|
||||
|
||||
query_path = f"./mydataset/query/cam{query_index}/" # Query path
|
||||
#Gallary path,这里直接对gallary文件夹下所有图片进行分析
|
||||
print("--------------query cam is ", query_index)
|
||||
for cls in classes:
|
||||
if cls != f"{query_index}":
|
||||
print("——————————gallery cam is", cls)
|
||||
gallery_path = f"./mydataset/gallery/{cls}/"
|
||||
query_feature_list = [] # 计算每个query的特征向量保存在列表中
|
||||
acck_l_list = [] # 二维列表(列表的列表),其元素(也是列表)存储单个query的"acck"("cmc")
|
||||
cmc_k_list = [] # 存储最终的CMC数字(所有query取平均)
|
||||
query_list = os.listdir(query_path) # 为每张图片生成路径并保存在列表变量中
|
||||
query_list.sort()
|
||||
gallery_list = os.listdir(gallery_path) # 为每张图片生成路径并保存在列表变量中
|
||||
with torch.no_grad():
|
||||
# 对于每个query,(这句话加在本循环内每个注释)
|
||||
for path in query_list:
|
||||
distance_dict = {}
|
||||
parsed_query_id = path.split("_")[1]
|
||||
parsed_query_id = parsed_query_id.split(".")[0]
|
||||
parsed_query_id = int(parsed_query_id)
|
||||
if parsed_query_id == 4 and cls == "5":
|
||||
continue
|
||||
print(parsed_query_id)
|
||||
id_feature = extractidfeature(query_path+path, extractor)
|
||||
query_feature_list.append(id_feature)
|
||||
counter = 0
|
||||
# 计算每个gallary图片到目标query的特征距离
|
||||
for p in gallery_list:
|
||||
if counter%10 == 9: # 抽样,相当于设置帧间隔
|
||||
img_path = gallery_path+p
|
||||
gallery_feature = extractidfeature(img_path, extractor)
|
||||
distance_dict[p] = np.sum(np.abs(id_feature-gallery_feature))
|
||||
counter += 1
|
||||
matched = sorted(distance_dict.items(), key=lambda x : x[1]) # 按特征距离排序
|
||||
# 储存k从前1到前10名的是否(存在命中的ID)
|
||||
temp_acck = [int(matched[0][0].split("_")[2].split(".")[0])==parsed_query_id] # ID相同为True,否则False
|
||||
# 获取前k个acck情况
|
||||
for i in range(1,11):
|
||||
parsed_gallery_id = matched[i][0].split("_")[2].split(".")[0]
|
||||
parsed_gallery_id = int(parsed_gallery_id) # 解析出gallary的ID
|
||||
print(parsed_gallery_id) # 解析出gallary的ID
|
||||
temp_acck.append(parsed_gallery_id==parsed_query_id or temp_acck[i-1]) # or用意是表明如果Acc(k-1)已经为True,那么Acck必然为True(递推)
|
||||
print(temp_acck)
|
||||
acck_l_list.append(temp_acck)
|
||||
# 计算不同k下cmc的具体数值
|
||||
for k in range(1, 11):
|
||||
acc_counter = 0.0
|
||||
length = len(acck_l_list)
|
||||
for results in acck_l_list:
|
||||
acc_counter += results[k]
|
||||
cmc_k_list.append(acc_counter/length)
|
||||
#打印以供检查
|
||||
print(cmc_k_list)
|
||||
cmc_sum.append(cmc_k_list)
|
||||
k_index = [k for k in range(1, 11)]
|
||||
#plot figure并保存(不会显示)
|
||||
plt.clf()
|
||||
plt.plot(k_index, cmc_k_list)
|
||||
plt.xlabel("k")
|
||||
plt.ylabel("ACCK")
|
||||
plt.ylim(0.0,1.05)
|
||||
plt.title(f"CMC:query_cam{query_index} and gallery {cls}")
|
||||
for x, y in zip(k_index, cmc_k_list):
|
||||
plt.text(x, y + 0.02, str(round(y, 3)), ha='center', va='bottom', fontsize=10.5)
|
||||
plt.draw()
|
||||
plt.savefig(f"./CMC/query_cam{query_index}_and_gallery_{cls}.jpg")
|
||||
for k in range(10):
|
||||
acc_counter = 0.0
|
||||
length = len(cmc_sum)
|
||||
for results in cmc_sum:
|
||||
acc_counter += results[k]
|
||||
cmc_final.append(acc_counter / length)
|
||||
print(cmc_final)
|
||||
k_index = [k for k in range(1, 11)]
|
||||
# plot figure并保存(不会显示)
|
||||
plt.clf()
|
||||
plt.plot(k_index, cmc_final)
|
||||
plt.xlabel("k")
|
||||
plt.ylabel("ACCK")
|
||||
plt.ylim(0.0, 1.05)
|
||||
plt.title(f"CMC:query and gallery")
|
||||
for x, y in zip(k_index, cmc_final):
|
||||
plt.text(x, y + 0.02, str(round(y,3)), ha='center', va='bottom', fontsize=10.5)
|
||||
plt.draw()
|
||||
plt.savefig(f"./CMC/query_gallery.jpg")
|
||||
|
|
@ -0,0 +1,118 @@
|
|||
#####
|
||||
# 保存query和前十个配准的gallery图像,并计算mAP(mean average precision)
|
||||
######
|
||||
import shutil
|
||||
import matplotlib.pyplot as plt
|
||||
import torch
|
||||
from torchvision import transforms
|
||||
import numpy as np
|
||||
import cv2
|
||||
import os
|
||||
|
||||
from model import my_model
|
||||
import param
|
||||
|
||||
|
||||
def extractidfeature(id_path: str, extractor, p_device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")):
|
||||
img_id = cv2.imread(id_path)
|
||||
raw_transformer = transforms.Compose([
|
||||
transforms.ToPILImage(),
|
||||
transforms.Resize((128,64)),# hxw
|
||||
transforms.ToTensor(),
|
||||
])
|
||||
tensor_id = extractor(raw_transformer(img_id).unsqueeze(dim=0).to(p_device))
|
||||
|
||||
return tensor_id.detach().cpu().numpy()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
my_option = param.Parameters()
|
||||
dislist = {}
|
||||
|
||||
extractor = my_model.RestNet18() # load model in head
|
||||
extractor.load_state_dict(torch.load(my_option.weights_reid))
|
||||
extractor = extractor.to(my_option.device)
|
||||
|
||||
gallery_index = 4
|
||||
camera_index = 1 # 第几个摄像头
|
||||
query_path = f"./mydataset/query/cam{camera_index}/"
|
||||
galla_path = f"./mydataset/gallery/{gallery_index}/"
|
||||
|
||||
gallary_list = os.listdir(galla_path)
|
||||
count = 0
|
||||
number_query = 12
|
||||
|
||||
#####计算mAP使用的变量
|
||||
mAP = 0
|
||||
precision = 0
|
||||
sum_precision = 0
|
||||
AP = 0
|
||||
sum_AP = 0
|
||||
number_true_gallery = 0
|
||||
#######
|
||||
|
||||
with torch.no_grad():
|
||||
for i in range(1, 1+number_query):
|
||||
count = 0
|
||||
dislist.clear()
|
||||
id_try = extractidfeature(query_path+f"/{camera_index}_{i}.jpg", extractor)
|
||||
for gal in gallary_list:
|
||||
count += 1
|
||||
if count % 5 == 0:
|
||||
id_tar = extractidfeature(galla_path+'/'+gal, extractor)
|
||||
dist = np.sum(np.abs(id_try-id_tar))
|
||||
dislist[gal] = float(dist)
|
||||
# if count > 10000:
|
||||
# break
|
||||
matched = sorted(dislist.items(), key=lambda x : x[1])
|
||||
print(matched)
|
||||
try:
|
||||
os.mkdir("match/" + f"{camera_index}_{i}")
|
||||
except:
|
||||
pass
|
||||
|
||||
img = plt.imread(query_path+f"/{camera_index}_{i}.jpg")
|
||||
plt.subplot(1, 11, 1)
|
||||
plt.title(f"q_{i}_g_{gallery_index}")
|
||||
plt.imshow(img)
|
||||
plt.xticks([])
|
||||
plt.yticks([])
|
||||
ranking = 1
|
||||
ranking_true_gallery = 1
|
||||
for path in matched[0:10]:
|
||||
s_path = galla_path+'/'+path[0]
|
||||
t_path = "match/" + f"{camera_index}_{i}" + "/" + path[0]
|
||||
# print(s_path, t_path)
|
||||
shutil.copy(s_path, t_path)
|
||||
ID = path[0].split('_')
|
||||
# print(ID)
|
||||
ID = int(ID[2].split(".")[0])
|
||||
print(i, ID)
|
||||
img = plt.imread(s_path)
|
||||
plt.subplot(1, 11, ranking+1)
|
||||
if ID == i:
|
||||
plt.title(ranking, color='black')
|
||||
precision = ranking_true_gallery/ranking
|
||||
sum_precision += precision
|
||||
ranking_true_gallery += 1
|
||||
|
||||
else:
|
||||
plt.title(ranking, color='red')
|
||||
plt.imshow(img)
|
||||
plt.xticks([])
|
||||
plt.yticks([])
|
||||
ranking += 1
|
||||
# plt.show()
|
||||
plt.draw()
|
||||
plt.savefig(f"query{i}_cam{camera_index}_and_gallery_{gallery_index}.jpg")
|
||||
number_true_gallery = ranking_true_gallery-1
|
||||
if 0 != number_true_gallery:
|
||||
AP = sum_precision / number_true_gallery
|
||||
else:
|
||||
AP = 0
|
||||
sum_precision = 0
|
||||
sum_AP += AP
|
||||
print("query_", i, "AP==", AP)
|
||||
mAP = sum_AP/number_query
|
||||
print("mAP == ", mAP)
|
||||
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
import shutil
|
||||
import matplotlib.pyplot as plt
|
||||
import torch
|
||||
from torchvision import transforms
|
||||
import numpy as np
|
||||
import cv2
|
||||
import os
|
||||
|
||||
from model import my_model
|
||||
import param
|
||||
|
||||
|
||||
|
||||
def extractidfeature(id_path: str, extractor, p_device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")):
|
||||
img_id = cv2.imread(id_path)
|
||||
raw_transformer = transforms.Compose([
|
||||
transforms.ToPILImage(),
|
||||
transforms.Resize((128,64)),# hxw
|
||||
transforms.ToTensor(),
|
||||
])
|
||||
tensor_id = extractor(raw_transformer(img_id).unsqueeze(dim=0).to(p_device))
|
||||
|
||||
return tensor_id.detach().cpu().numpy()
|
||||
|
||||
# example
|
||||
if __name__ == '__main__':
|
||||
dislist = {}
|
||||
extractor_model_path = "./deep_sort/deep/checkpoint/market_bot_R50-ibn.pth"
|
||||
cfg_path = "./fastreid/cfgs/Market1501/bagtricks_R50-ibn.yml"
|
||||
query_path = "D:\\study\\python\\1zhouxue\\UESTC_ReID_Dataset_V3\\query\\cam6"
|
||||
galla_path = "D:\\study\\python\\1zhouxue\\UESTC_ReID_Dataset_V3\\gallery"
|
||||
camera_index = 6 # 第几个摄像头
|
||||
gallary_list = os.listdir(galla_path)
|
||||
count = 0
|
||||
|
||||
my_option = param.Parameters()
|
||||
|
||||
extractor = my_model.RestNet18() # load model in head
|
||||
extractor.load_state_dict(torch.load(my_option.weights_reid))
|
||||
extractor = extractor.to(my_option.device)
|
||||
|
||||
with torch.no_grad():
|
||||
for i in range(1, 7):
|
||||
count = 0
|
||||
dislist.clear()
|
||||
id_try = extractidfeature(query_path+f"/{camera_index}_{i}.jpg", extractor)
|
||||
for gal in gallary_list:
|
||||
count += 1
|
||||
if count % 5 == 0:
|
||||
id_tar = extractidfeature(galla_path+'/'+gal, extractor)
|
||||
dist = np.sum(np.abs(id_try-id_tar))
|
||||
dislist[gal] = float(dist)
|
||||
if count > 10000:
|
||||
break
|
||||
matched = sorted(dislist.items(), key=lambda x : x[1])
|
||||
print(matched)
|
||||
try:
|
||||
os.mkdir("match/" + f"{camera_index}_{i}")
|
||||
except:
|
||||
pass
|
||||
|
||||
img = plt.imread(query_path+f"/{camera_index}_{i}.jpg")
|
||||
plt.subplot(1, 11, 1)
|
||||
plt.title(f"query_{i}")
|
||||
plt.imshow(img)
|
||||
plt.xticks([])
|
||||
plt.yticks([])
|
||||
x = 1
|
||||
for path in matched[0:10]:
|
||||
s_path = galla_path+'/'+path[0]
|
||||
t_path = "match/" + f"{camera_index}_{i}" + "/" + path[0]
|
||||
print(s_path, t_path)
|
||||
shutil.copy(s_path, t_path)
|
||||
|
||||
ID = path[0].split('_')
|
||||
# print(ID)
|
||||
ID = int(ID[0])
|
||||
img = plt.imread(s_path)
|
||||
plt.subplot(1, 11, x+1)
|
||||
if ID == i:
|
||||
plt.title(x)
|
||||
else:
|
||||
plt.title(x, color='red')
|
||||
plt.imshow(img)
|
||||
plt.xticks([])
|
||||
plt.yticks([])
|
||||
x += 1
|
||||
plt.show()
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1,16 @@
|
|||
import torch
|
||||
|
||||
class Parameters(object):
|
||||
def __init__(self) -> None:
|
||||
self.weights_yolo = "./best.pt"
|
||||
self.weights_reid = "./resnet18.pth"
|
||||
self.conf_thres = 0.35
|
||||
self.iou_thres = 0.70
|
||||
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
||||
self.nosave = False
|
||||
self.classes = [0]
|
||||
self.agnostic_nms = None
|
||||
self.augment = None
|
||||
self.query_index = 1
|
||||
self.gallary_index= 2
|
||||
pass
|
||||
4
train.py
4
train.py
|
|
@ -46,6 +46,10 @@ if __name__ == '__main__':
|
|||
raw_transformer = transforms.Compose([
|
||||
transforms.ToPILImage(),
|
||||
transforms.Resize((128,64)),# hxw
|
||||
transforms.RandomResizedCrop((100,50)),
|
||||
transforms.RandomAutocontrast(),
|
||||
transforms.RandomHorizontalFlip(),
|
||||
transforms.Resize((128,64)),# hxw
|
||||
transforms.ToTensor(),
|
||||
])
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue