目标检测目标检测目标检测目标检测
立即下载
资源介绍:
目标检测目标检测目标检测目标检测
import torch
from torch import nn
from torchsummary import summary
import matplotlib.pyplot as plt
import config
############################
# road cls header
############################
# -------------- 路面分类头部网络 -----------------#
class ClassifyHead(nn.Module):
""""
功能说明:路面分类 header
"""
def __init__(self,num_classes=4):
super().__init__()
# Batch norm parameters
momentum = 0.01
epsilon = 1e-3
in_channels = 448
out_channels = 1280
self.head = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(num_features=out_channels, momentum=momentum, eps=epsilon),
nn.ReLU6(inplace=True),
)
self.avgpool = torch.nn.AdaptiveAvgPool2d((1, 1))
dropout_rate = 0.3
if dropout_rate > 0:
self.dropout = nn.Dropout(dropout_rate)
else:
self.dropout = None
self.fc = torch.nn.Linear(out_channels, num_classes)
# [1 448 7 7]
def forward(self, x):
# [1,1280,7,7]
x = self.head(x)
# [1,1280,1,1]
x = self.avgpool(x)
# [1,1280]
x = x.view(x.size(0), -1)
if self.dropout is not None:
x = self.dropout(x)
# [1,4]
x = self.fc(x)
return x
if __name__ == '__main__':
num_class = config.class_num
model = ClassifyHead(num_class)
model.to('cuda')
summary(model, input_size=(448,7,7)) # input_size=(channels, H, W)
############################
# loss
############################
# 功能:分类损失计算
loss_function = nn.CrossEntropyLoss()
# 使用方法说明
# 导入:from use_in_yolox import loss_function
# 调用:loss = loss_function(outputs, labels)
# 输入格式说明:outputs:(1,class_num) labels:(class_id)
# 功能:输出训练过程中的路面分类指标
import numpy
from sklearn.metrics import classification_report
class ConfusionMatrix(object):
"""
混淆矩阵
"""
# 初始化
def __init__(self, class_num: int):
self.matrix = numpy.zeros((class_num, class_num))
self.class_num = class_num
self.pred=[]
self.true=[]
# 统计更新
def update(self, pred, label):
# p代表Predicted label、t代表True label
for p, t in zip(pred, label):
self.matrix[p, t] += 1
self.pred.append(p)
self.true.append(t)
# 输出准确度
def acc(self):
acc = 0
for i in range(self.class_num):
acc += self.matrix[i, i]
acc = acc / numpy.sum(self.matrix)
return acc
# 打印结果
def report(self):
report = classification_report(self.true, self.pred, digits=4)
return report
############################
# predict
############################
# 输入:1*class_num的tensor
# 输出:预测类别
def road_classify_decode(road_cls_output):
predict = torch.softmax(torch.squeeze(road_cls_output).cpu(), dim=0)
predicted_road_classify_result = torch.argmax(predict).numpy()
print('predicted class id: ', predicted_road_classify_result)
return predicted_road_classify_result
############################
# 验证指标 test.py
############################
# 输入:model,images, labels, confusion_matrix,device
# 输出:路面验证结果
def road_cls_validation(model,images, labels, confusion_matrix,device):
images, labels = images.to(device), labels.to(device)
road_cls_input = model(images)
confusion_matrix.update(road_cls_input.argmax(1).cpu().numpy(), labels.cpu().numpy())
return confusion_matrix.report()
# 调用示例:
# from use_in_yolox import ConfusionMatrix, road_cls_validation
# confusion_matrix = ConfusionMatrix(config.class_num)
# with torch.no_grad():
# confusion_matrix.__init__(config.class_num)
# for images, labels in test_loader:
# road_cls_valid_result = road_cls_validation(net,images, labels,confusion_matrix, device)
# print(road_cls_valid_result)
############################
# train.py
############################
# 功能: 绘制训练过程的路面分类LOSS曲线图
# 调用示例:
# confusion_matrix = ConfusionMatrix(class_num)
# for i in range(1, 总训练轮数):
# 训练
# model.train()
# confusion_matrix.__init__(config.class_num)
# for images, labels in train_loader:
# 根据输出更新混淆矩阵
# outputs = model(images)
#confusion_matrix.update(outputs.argmax(1).cpu().numpy(), labels.cpu().numpy())
# 打印: print("train accurancy", confusion_matrix.acc())
# 验证
# model.eval()
# confusion_matrix.__init__(config.class_num)
# for images, labels in val_loader:
# 根据输出更新混淆矩阵
# outputs = model(images)
#confusion_matrix.update(outputs.argmax(1).cpu().numpy(), labels.cpu().numpy())
# 打印: print("val accurancy", confusion_matrix.acc())
# 功能: 绘制训练过程的路面分类LOSS曲线图
def draw_cls_loss(train_losses,val_losses,epochs):
""""
功能说明:记录每一轮训练过程中的分类loss
"""
plt.figure()
plt.title('Road Sufrace Classify LOSS during training') # 标题
plt.plot(epochs, train_losses, label="train loss")
plt.plot(epochs, val_losses, label="val loss")
plt.xlabel('epochs')
plt.ylabel('loss')
plt.legend()
plt.grid()
# TODO
save_path = "./"
### 这里需要修改一下保存地址 ###
plt.savefig(f'{save_path}/Road Surface Classify LOSS.png')
# 调用示例:
# from use_in_yolox draw_cls_loss
# 开始训练...
# epochs = []
# t_loss = []
# for i in range(1, 总训练轮数):
# 记录epoch
# epochs.append(i + 1)
# 记录train_loss
# t_loss.append(train_loss)
# 记录val_loss
# val_loss.append(valid_loss)
# 调用该函数绘制路面分类曲线图
# draw_cls_loss(train_losses=t_loss,val_losses=val_loss,epochs=epochs)
###############################
# EfficientNet-Lite Backbone
###############################
import torch.functional as F
import math
def round_filters(filters, multiplier, divisor=8, min_width=None):
"""Calculate and round number of filters based on width multiplier."""
if not multiplier:
return filters
filters *= multiplier
min_width = min_width or divisor
new_filters = max(min_width, int(filters + divisor / 2) // divisor * divisor)
# Make sure that round down does not go down by more than 10%.
if new_filters < 0.9 * filters:
new_filters += divisor
return int(new_filters)
def round_repeats(repeats, multiplier):
"""Round number of filters based on depth multiplier."""
if not multiplier:
return repeats
return int(math.ceil(multiplier * repeats))
def drop_connect(x, drop_connect_rate, training):
if not training:
return x
keep_prob = 1.0 - drop_connect_rate
batch_size = x.shape[0]
random_tensor = keep_prob
random_tensor += torch.rand([batch_size, 1, 1, 1], dtype=x.dtype, device=x.device)
binary_mask = torch.floor(random_tensor)
x = (x / keep_prob) * binary_mask
return x
class MBConvBlock(nn.Module):
def __init__(self, inp, final_oup, k, s, expand_ratio, se_ratio, has_se=False):
super(MBConvBlock, self).__init__()
self._momentum = 0.01
self._epsilon = 1e-3
self.input_filters = inp
self.output_filters = final_oup
self.stride = s
self.expand_ratio = expand_ratio
资源文件列表:
use_in_yolox.zip 大约有1个文件
- use_in_yolox.py 14.67KB