import os
import torch
import torch.nn as nn
from torchvision import transforms, datasets
from vine_datasaet.utils import read_split_data
from vine_datasaet.my_dataset import get_batch
from VGG16LSTM import VGG16LSTM
from sklearn.metrics import accuracy_score
import time
from torch.utils.tensorboard import SummaryWriter
import matplotlib.pyplot as plt
start = time.time()
# 初始化参数
# root = 'D:\\workspace_py\\classicCNN\\vine_keyframes_test_3'
root = '/mntc/sst/vine_keyframes'
NUM_CLASSES = 22
EPOCH = 5
LR = 0.0001
BATCH_SIZE = 8
train_videos_path, train_videos_label, val_videos_path, val_videos_label = read_split_data(root)
num_train_data, num_val_data = len(train_videos_label), len(val_videos_label)
train_loader = get_batch(train_videos_path, train_videos_label, BATCH_SIZE, 'train')
val_loader = get_batch(val_videos_path, val_videos_label, BATCH_SIZE, 'val')
writer = SummaryWriter()
def train(model, train_loader, epoch=EPOCH, batch_size=BATCH_SIZE, lr=LR):
'''
# 使用GPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('using {} device'.format(device))
# 开始训练
net = VGG16LSTM(num_classes=NUM_CLASSES)
net = net.to(device)
'''
optimizer = torch.optim.Adam(net.parameters(), lr=lr)
loss_func = nn.CrossEntropyLoss()
# 绘图用
loss_train_list = []
acc_train_list = []
loss_val_list = []
acc_val_list = []
for e in range(epoch):
model.train()
loss = 0.0
accuracy = 0.0
for batch_idx, (train_data, train_label, batch_length) in enumerate(train_loader):
train_data = train_data.to(device)
train_label = train_label.to(device)
batch_length = batch_length.to(device)
# print(batch_length)
# print(type(batch_length))
train_prediction = model(train_data, batch_length)
# print(train_prediction)
# print(train_data.shape, train_prediction.shape, train_label.shape)
_, predict = torch.max(train_prediction, dim=1)
# predict = train_prediction.detach().to("cpu").numpy()
predict = predict.to("cpu")
true_label = train_label.detach().to("cpu").numpy()
accuracy += accuracy_score(predict, true_label)
batch_loss = loss_func(train_prediction, train_label)
optimizer.zero_grad()
batch_loss.backward()
optimizer.step()
loss += batch_loss.data.cpu().numpy()
loss = loss * batch_size / num_train_data
accuracy = accuracy * batch_size / num_train_data
print('Epoch: ', e, '\n',
'| train loss: %.5f' % loss,
'| train accuracy: %.2f' % accuracy)
loss_train_list.append(loss)
acc_train_list.append(accuracy)
# 保存训练好的数据参数
torch.save(net.state_dict(), 'VGG16LSTM.pt')
# 验证集验证
loss_val, accuracy_val = test(net, val_loader)
loss_val_list.append(loss_val)
acc_val_list.append(accuracy_val)
epoch_time = time.time()
print(' | 运行时间为:%.2f' % (epoch_time - start))
# 绘图
plt.figure(figsize=(10, 10))
plt.title("Training and Validation Loss")
plt.plot(loss_train_list, label="train")
plt.plot(loss_val_list, label="val")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.legend()
plt.show()
plt.figure(figsize=(10, 10))
plt.title("Training and Validation Accuracy")
plt.plot(acc_train_list, label="train")
plt.plot(acc_val_list, label="val")
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.legend()
plt.show()
def test(model, val_loader, batch_size=BATCH_SIZE):
'''
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('using {} device'.format(device))
m_state_dict = torch.load('VGG16LSTM.pt')
net = VGG16LSTM(NUM_CLASSES)
net.load_state_dict(m_state_dict)
# print(m_state_dict)
net = net.to(device)
'''
model.eval()
loss_func = nn.CrossEntropyLoss()
loss = 0.0
accuracy = 0.0
for batch_idx, (val_data, val_label, batch_length) in enumerate(val_loader):
val_data = val_data.to(device)
val_label = val_label.to(device)
batch_length = batch_length.to(device)
val_prediction = model(val_data, batch_length)
_, predict = torch.max(val_prediction, dim=1)
# predict = train_prediction.detach().to("cpu").numpy()
predict = predict.to("cpu")
true_label = val_label.detach().to("cpu").numpy()
accuracy += accuracy_score(predict, true_label)
batch_loss = loss_func(val_prediction, val_label)
loss += batch_loss.data.cpu().numpy()
accuracy = accuracy * batch_size / num_val_data
loss = loss * batch_size / num_val_data
print(' | val loss: %.5f' % loss,
'| val accuracy: %.2f' % accuracy)
return loss, accuracy
if __name__ == '__main__':
# 使用GPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('using {} device'.format(device))
# 开始训练
net = VGG16LSTM(num_classes=NUM_CLASSES)
net = net.to(device)
train(net, train_loader)
# test(net, val_loader)
end = time.time()
print("程序的总运行时间为:{}".format(end-start))
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 毕业设计 基于python+VGG16LSTM的关键帧视频场景识别系统源码+详细文档+全部数据资料 高分项目.zip毕业设计 基于python+VGG16LSTM的关键帧视频场景识别系统源码+详细文档+全部数据资料 高分项目.zip 【备注】 1、该项目是高分毕业设计项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过mac/window10/11/linux测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也可作为毕业设计、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
资源推荐
资源详情
资源评论
收起资源包目录
毕业设计 基于python+VGG16LSTM的关键帧视频场景识别系统源码+详细文档+全部数据资料 高分项目.zip (16个子文件)
Graduation-Project-master
Graduation Project
__init__.py 0B
main.py 5KB
VGG16LSTM.py 5KB
class_indexs.json 82B
__pycache__
VGG16LSTM.cpython-38.pyc 2KB
vine_datasaet
utils.py 4KB
__init__.py 0B
my_dataset.py 4KB
class_indexs.json 580B
__pycache__
my_dataset.cpython-38.pyc 2KB
VineVideo.cpython-38.pyc 705B
utils.cpython-38.pyc 3KB
__init__.cpython-38.pyc 143B
test.py 1024B
VineVideo.py 346B
171265889347208773632.zip 416B
共 16 条
- 1
资源评论
不走小道
- 粉丝: 3245
- 资源: 5112
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功