import os
import shutil
import time
from tqdm import tqdm
import random
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# import pandas as pd
import PIL.Image
from IPython.display import Image
# from sklearn.metrics import confusion_matrix
import torch
import torch.nn as nn
import torchvision
from torchvision import models, transforms, datasets
from img_process import process_img
path_train = "dataset/imgs/train"
path_test = "dataset/imgs/test"
device = torch.device("cuda:0")
print(device)
print(torch.cuda.get_device_name(device))
train_loader, test_loader = process_img(path_train, True)
def train_model(model, criterion, optimizer, scheduler, n_epochs=5):
losses = []
accuracies = []
test_accuracies = []
# set the model to train mode initially
model.train()
for epoch in tqdm(range(n_epochs)):
since = time.time()
running_loss = 0.0
running_correct = 0.0
for data in train_loader:
# get the inputs and assign them to cuda
inputs, labels = data
inputs = inputs.to(device)
labels = labels.to(device)
optimizer.zero_grad()
# forward + backward + optimize
outputs = model(inputs)
_, predicted = torch.max(outputs.data, 1)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# calculate the loss/acc later
running_loss += loss.item()
running_correct += (labels == predicted).sum().item()
epoch_duration = time.time() - since
epoch_loss = running_loss / len(train_loader)
epoch_acc = 100 / 32 * running_correct / len(train_loader)
print("Epoch %s, duration: %d s, loss: %.4f, acc: %.4f" % (epoch + 1, epoch_duration, epoch_loss, epoch_acc))
losses.append(epoch_loss)
accuracies.append(epoch_acc)
# switch the model to eval mode to evaluate on test data
model.eval()
test_acc = eval_model(model)
test_accuracies.append(test_acc)
# re-set the model to train mode after validating
model.train()
scheduler.step(test_acc)
since = time.time()
print('Finished Training')
return model, losses, accuracies, test_accuracies
def eval_model(model):
correct = 0.0
total = 0.0
with torch.no_grad():
for i, data in enumerate(test_loader, 0):
images, labels = data
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
test_acc = 100.0 * correct / total
print('Accuracy of the network on the test images: %d %%' % (
test_acc))
return test_acc
if __name__=='__main__':
model_ft = models.mobilenet_v3_small(pretrained=True)
num_ftrs = model_ft.classifier[3].in_features
model_ft.classifier[3] = nn.Linear(num_ftrs, 10) # No. of classes = 10
model_ft = model_ft.to(device)
criterion = nn.CrossEntropyLoss().to(device)
optimizer = torch.optim.SGD(model_ft.parameters(), lr=0.01, momentum=0.9)
lrscheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', patience=3, threshold=0.9)
model_ft, training_losses, training_accs, test_accs = train_model(model_ft, criterion, optimizer, lrscheduler,
n_epochs=5)
torch.save(model_ft.state_dict(), "checkpoint/model-driver")
没有合适的资源?快使用搜索试试~ 我知道了~
基于mobilenetv3实现的分心驾驶行为检测python源码+模型(支持图片和视频检测).zip
共14个文件
py:6个
xml:5个
gitignore:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 69 浏览量
2023-09-06
17:51:16
上传
评论
收藏 5.5MB ZIP 举报
温馨提示
【资源说明】 基于mobilenetv3实现的分心驾驶行为检测python源码+模型(支持图片和视频检测).zip 基于mobilenetv3实现的分心驾驶行为检测python源码+模型(支持图片和视频检测).zip 基于mobilenetv3实现的分心驾驶行为检测python源码+模型(支持图片和视频检测).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
资源推荐
资源详情
资源评论
收起资源包目录
基于mobilenetv3实现的分心驾驶行为检测python源码+模型(支持图片和视频检测).zip (14个子文件)
checkpoint
mobilenet.pth 5.96MB
setup.py 183B
predict.py 3KB
trainer.py 4KB
video_pro.py 1KB
.idea
vcs.xml 180B
misc.xml 300B
Mobilenet.iml 329B
inspectionProfiles
Project_Default.xml 1KB
profiles_settings.xml 174B
modules.xml 270B
.gitignore 176B
img_process.py 2KB
test.py 511B
共 14 条
- 1
资源评论
Make程序设计
- 粉丝: 6822
- 资源: 3575
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功