【卷积神经网络作业实现人脸的关键点定位功能】
下面是完成这道题目的代码import os import cv2 import numpy as np import pandas as pd import torch import torch.nn as nn from torch.utils.data import Dataset,DataLoader from torchvision import transforms import matplotlib.pyplot as plt1. 数据集定义匹配你的目录结构)class FaceKeypointDataset(Dataset): def __init__(self,txt_path,img_dir,transformNone): self.df pd.read_csv(txt_path,sepr\s,headerNone,on_bad_linesskip) self.img_dir img_dir self.transform transform def __len__(self): return len(self.df) def __getitem__(self,idx): #读取图片名和路径 img_nameself.df.iloc[idx,0] img_pathos.path.join(self.img_dir,img_name) img cv2.imread(img_path) img cv2.cvtColor(img,cv2,COLOR_BGR2RGB) img cv2.resize(img,(64,64)) #读取10个关键点坐标 kptsself.df.iloc[idx,1:11].values.astype(np.float32) #坐标归一化 kpts[0::2]/64.0#x坐标 kpts[1::2]/64.0#y坐标 if self.transform: img self.transform(img) return img,torch.tensor(kpts,dtypetorch.float32)2. 卷积神经网络模型class KeypointNet(nn.Module): def __init__(self): super().__init__() #卷积层提取特征 self.conv_layers nn.Sequential( nn.Conv2d(3,16,kernel_size3,padding1), nn.RELU(), nn.MaxPool2d(2,2), nn.Conv2d(16,32,kernel_size 3,padding1), nn.ReLU(), nn.MaxPool2d(2,2), nn.Conv2d(32,64,kernel_size3,padding1), nn.ReLU(), nn.MaxPool2d(2,2), ) #全连接层输出10个关键点坐标 self.fc_layersnn.Sequential( nn.Linear(64*8*8,256), nn.ReLU(), nn.Linear(256,10)#输出层10个神经元 ) def forward(self,x): xself.conv_layers(x) xx.view(x.size(0),-1) xself.fc_layers(x) retrun x3. 训练主程序def main(): #配置设备 devicetorch.device(cuda if torch.cuda.is_available() else cpu) print(f使用设备:{device}) #数据预处理 transform transforms.Compose([ transfroms.ToTensor(), transforms.Normalize(mean[0.5,0.5,0.5],std[0.5,0.5,0.5]) ]) train_dataset FaceKeypointDataset(txt_pathtrain.txt, img_dirimgdata, transformtransform) test_dataset FaceKeypointDataset(txt_pathtest.txt, img_dirimgdata, transformtransform) train_loader DataLoader(train_dataset,batch_size8,shuffleTrue) test_loader DataLoader(test_dataset,batch_size8,shuffleFalse) #初始化模型损失函数优化器 model KeypointNet().to(device) criterion nn.L1loss() optimizer torch.optim.Adam(model.parameters(),lr1e-3) #训练参数 num_ephchs30 train_loss_history [] test_loss_history[] print(\n开始训练) for epoch in range(num_epochs): model.train() train_loss0.0 for imgs,kpts in train_loader: imgs,kptsimgs.to(device),kpts.to(device) #前向传播 outputs model(imgs) loss criterion(outputs,kpts) #反向传播 optimizer.zero_grad() loss.backward() optimizer.step() train_lossloss.item() * imgs.size(0) #计算平均训练损失 train_loss/ len(train_dataset) train_loss_history.append(train_loss) #测试阶段 model.eval() test_loss0.0 with torch.no_grad(): for imgs,kpts in test_loader: imgs,kpts imgs.to(device),kpts.to(device) outputs model(imgs) test_loss criterion(outputs,kpts).item()*imgs.size(0) test_loss / len(test_dataset) test_loss_history.append(test_loss) print(fEpoch [{epoch1}/{num_epochs}} | Train Loss:{train_loss:.6f} | Test Loss:{test_loss:.6f}) print(训练完成) plt.figure(figsize(10,5)) plt.plot(train_loss_history,label Train Loss) plt.plot(test_loss_history,labelTest Loss) plt.xlabel(Epoch) plt.ylabel(L1 Loss) plt.title(Training and Testing Loss Curve) plt.lengend() plt.grid(True) plt.savefig(loss_curve.png) plt.show() torch.save(model.state_dict(), face_keypoint_model.pth) print(模型已保存为 face_keypoint_model.pth) if __name__ __main__: main()运行结果
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2470691.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!