当前位置: 首页 > news >正文

我们的网站企业网站推广是不是必要的

我们的网站,企业网站推广是不是必要的,网络规划设计师教程第二版电子版下载,浙江三建建设集团有限公司网站一、选题背景及动机 在现代社会中#xff0c;图像分类是计算机视觉领域的一个重要任务。动物图像分类具有广泛的应用#xff0c;例如生态学研究、动物保护、农业监测等。通过对动物图像进行自动分类#xff0c;可以帮助人们更好地了解动物种类、数量和分布情况#xff0c;…一、选题背景及动机 在现代社会中图像分类是计算机视觉领域的一个重要任务。动物图像分类具有广泛的应用例如生态学研究、动物保护、农业监测等。通过对动物图像进行自动分类可以帮助人们更好地了解动物种类、数量和分布情况从而支持相关领域的决策和研究。本研究的目标是使用卷积神经网络CNN对动物图像进行分类。通过对大量的猫、狗和野生动物图像进行训练建立一个准确分类不同动物类别的模型。该模型可以用于自动识别和分类新的动物图像从而提供快速、准确的动物分类结果。 动机 1对于宠物猫和狗的图像分类可以帮助饲养者或宠物主人快速准确地识别自己的宠物。这对于宠物寻找、宠物遗失的寻找以及宠物社交媒体的管理和组织都非常有用。 2通过在大规模的动物图像数据库中进行分类可以构建一个便捷的图像检索系统。用户可以根据感兴趣的类别如猫、狗或野生动物快速搜索和浏览相关的图像内容。 二、研究内容 1、本次训练的数据来源 https://www.kaggle.com/datasets/andrewmvd/animal-faces/data 注册登录之后下载即可。然后把下载好的数据集放在该项目路径下的data文件中 2、使用PyTorch和scikit-learn框架进行机器学习任务的实现和评估 3、技术难点 1如何准确评估模型的性能和分析分类结果 2每个类别的数据样本不一致 解决方法 1对模型进行评估并计算准确率、精确度、召回率等指标绘制混淆矩阵和学习曲线以可视化模型的性能和错误情况 2通过对数据样本多的数据集进行欠采样使所有类别的数据集统一样本数量 三、步骤 1、导入必要的库 import pandas as pd from PIL import Image import torch.nn as nn import torch.optim as optim from torch.utils.data.sampler import SubsetRandomSampler from torch.utils.data import Dataset import torchvision.transforms as transforms import matplotlib.font_manager as fm import torch import torch.nn.functional as F from sklearn.metrics import accuracy_score, precision_score, recall_score, confusion_matrix, roc_curve, auc import matplotlib.pyplot as plt import seaborn as sns from collections import Counter from sklearn.utils import resample import numpy as np2、加载数据集和对数据预处理 通过对数据的加载和数据预处理之后打印出每个类别猫、狗、野兽的图片总数并绘制出直方图更直观的表示出图片每个类别的数量。 class InvalidDatasetException(Exception):def __init__(self, len_of_paths, len_of_labels):super().__init__(fNumber of paths ({len_of_paths}) is not compatible with number of labels ({len_of_labels})) transform transforms.Compose([transforms.ToTensor()]) class AnimalDataset(Dataset):def __init__(self, img_paths, img_labels, size_of_images):self.img_paths img_pathsself.img_labels img_labelsself.size_of_images size_of_imagesif len(self.img_paths) ! len(self.img_labels):raise InvalidDatasetException(self.img_paths, self.img_labels)def __len__(self):return len(self.img_paths)def __getitem__(self, index):PIL_IMAGE Image.open(self.img_paths[index]).resize(self.size_of_images)TENSOR_IMAGE transform(PIL_IMAGE)label self.img_labels[index]return TENSOR_IMAGE, label import glob paths [] labels [] label_map {0: Cat,1: Dog,2: Wild} cat_paths glob.glob(D:/test/pythonProject/data/afhq/train/cat/*) glob.glob(D:/test/pythonProject/data/afhq/val/cat/*) #路径需要改成自己存放项目数据的路径 for cat_path in cat_paths:paths.append(cat_path)labels.append(0) dog_paths glob.glob(D:/test/pythonProject/data/afhq/train/dog/*) glob.glob(D:/test/pythonProject/data/afhq/val/dog/*) for dog_path in dog_paths:paths.append(dog_path)labels.append(1) wild_paths glob.glob(D:/test/pythonProject/data/afhq/train/wild/*) glob.glob(D:/test/pythonProject/data/afhq/val/wild/*) for wild_path in wild_paths:paths.append(wild_path)labels.append(2) data pd.DataFrame({classes: labels})num_classes len(label_map) print(总类别数:, num_classes) for class_label, class_name in label_map.items():count data[data[classes] class_label].shape[0]print(f类别 {class_name}: {count} 张照片) font_path C:/Windows/Fonts/msyh.ttc font_prop fm.FontProperties(fnamefont_path) sns.set_style(white) plot sns.countplot(xdata[classes], color#2596be) plt.figure(figsize(15, 12)) sns.despine() plot.set_title(类别分布\n, x0.1, y1, fontfont_prop, fontsize18) plot.set_ylabel(数量, x0.02, fontfont_prop, fontsize12) plot.set_xlabel(类别, fontfont_prop, fontsize15) for p in plot.patches:plot.annotate(format(p.get_height(), .0f), (p.get_x() p.get_width() / 2, p.get_height()),hacenter, vacenter, xytext(0, -20), fontfont_prop, textcoordsoffset points, size15) plt.show()运行截图 通过对以上打印的数据以及可视化的图片进行观察我们可以看到三个类别的数量存在一定的差异。虽然数量上的差距不是太大但对于训练学习结果可能会有一定的影响。为了克服类别不平衡的问题我们可以采取欠采样来平衡数据集减少数量较多的类别的样本数量。 #数据集欠采样 labels np.array(labels) paths np.array(paths) counter Counter(labels) print(原始样本数量:, counter) cat_indices np.where(labels 0)[0] dog_indices np.where(labels 1)[0] wild_indices np.where(labels 2)[0] min_samples min([len(cat_indices), len(dog_indices), len(wild_indices)]) undersampled_cat_indices resample(cat_indices, replaceFalse, n_samplesmin_samples, random_state42) undersampled_dog_indices resample(dog_indices, replaceFalse, n_samplesmin_samples, random_state42) undersampled_wild_indices resample(wild_indices, replaceFalse, n_samplesmin_samples, random_state42) undersampled_indices np.concatenate((undersampled_cat_indices, undersampled_dog_indices, undersampled_wild_indices)) undersampled_paths paths[undersampled_indices] undersampled_labels labels[undersampled_indices] counter_undersampled Counter(undersampled_labels) print(欠采样后的样本数量:, counter_undersampled) counter_undersampled Counter(undersampled_labels) categories [label_map[label] for label in counter_undersampled.keys()] sample_counts list(counter_undersampled.values())#可视化 sns.set_style(white) plt.figure(figsize(6.4, 4.8)) plot sns.countplot(xundersampled_labels, color#2596be) sns.despine() plot.set_title(类别分布\n, x0.1, y1, fontfont_prop, fontsize18) plot.set_ylabel(数量, x0.02, fontfont_prop, fontsize12) plot.set_xlabel(类别, fontfont_prop, fontsize15)for p in plot.patches:plot.annotate(format(p.get_height(), .0f), (p.get_x() p.get_width() / 2, p.get_height()),hacenter, vacenter, xytext(0, -20), fontfont_prop, textcoordsoffset points, size15)plt.show()运行结果图 在进行欠采样后每个类别的图片数量已经被扩展为一致的数量使得模型在训练过程中更加公平地对待每个类别。 3、缺失值处理 对数据进行预处理完之后需要查看是否有缺失值要检查路径和标签的数量是否匹配并打印路径和标签数量对缺失情况进行可视化 if len(undersampled_paths) ! len(undersampled_labels):raise InvalidDatasetException(len(undersampled_paths), len(undersampled_labels)) #使用字符串格式化f-string来将整型值插入到字符串中。 print(f打印paths列表的文件路径数量: {len(undersampled_paths)}) print(f打印labels列表的图片数量: {len(undersampled_labels)}) #缺失情况数据可视化 df pd.DataFrame({Path: undersampled_paths, Label: undersampled_labels}) missing_values df.isnull().sum() #绘制条形图 plt.bar(missing_values.index, missing_values.values) plt.xlabel(特征, fontpropertiesfont_prop, fontsize12) plt.ylabel(缺失值数量, fontpropertiesfont_prop, fontsize12) plt.title(缺失情况数据可视化, fontpropertiesfont_prop, fontsize18) plt.grid(False) plt.xticks(rotation90) plt.show()运行截图 通过对打印的数据以及对条形图的查看我们可以确认数据没有缺失。这意味着我们的数据集完整并且可以进行进一步的分析和处理。 4、划分数据集 对将数据集划分为训练集和测试集并创建对应的数据加载器并定义了每个批次的样本数量。 dataset AnimalDataset(undersampled_paths,undersampled_labels,(250,250)) from sklearn.model_selection import train_test_split dataset_indices list(range(0,len(dataset))) #从数据集中划分训练集和测试集 train_indices,test_indicestrain_test_split(dataset_indices,test_size0.2,random_state42) print(训练集样本数量: ,len(train_indices)) print(测试集样本数量: ,len(test_indices)) #创建训练集和测试集的采样器 train_sampler SubsetRandomSampler(train_indices) test_sampler SubsetRandomSampler(test_indices) BATCH_SIZE 128 train_loader torch.utils.data.DataLoader(dataset, batch_sizeBATCH_SIZE,samplertrain_sampler) validation_loader torch.utils.data.DataLoader(dataset, batch_sizeBATCH_SIZE,samplertest_sampler) dataset[1][0].shape images,labels next(iter(train_loader)) type(labels)运行截图 5、获取一个批次的训练数据并可视化 def add_subplot_label(ax, label):ax.text(0.5, -0.15, label, transformax.transAxes,hacenter, vacenter, fontsize12) images, labels next(iter(train_loader)) fig, axis plt.subplots(3, 5, figsize(15, 10)) for i, ax in enumerate(axis.flat):with torch.no_grad():npimg images[i].numpy()npimg np.transpose(npimg, (1, 2, 0))label label_map[int(labels[i])]ax.imshow(npimg)ax.set(title f{label})ax.grid(False)add_subplot_label(ax, f({i // axis.shape[1]}, {i % axis.shape[1]})) # 添加编号 plt.tight_layout() plt.show()运行截图 6、模型设计 定义卷积神经网络模型并设定在哪个设备上运行为后续的模型训练做准备 class CNN(nn.Module):#定义了卷积神经网络的各个层和全连接层。def __init__(self):super(CNN, self).__init__()# First well define our layersself.conv1 nn.Conv2d(3, 32, kernel_size3, stride2, padding1)self.conv2 nn.Conv2d(32, 64, kernel_size3, stride2, padding1)self.batchnorm1 nn.BatchNorm2d(64)self.conv3 nn.Conv2d(64, 128, kernel_size3, stride2, padding1)self.batchnorm2 nn.BatchNorm2d(128)self.conv4 nn.Conv2d(128, 256, kernel_size3, stride2, padding1)self.batchnorm3 nn.BatchNorm2d(256)self.maxpool nn.MaxPool2d(2, 2)self.fc1 nn.Linear(256 * 2 * 2, 512)self.fc2 nn.Linear(512, 3)#定义数据在模型中的流动def forward(self, x):x F.relu(self.conv1(x))x F.relu(self.conv2(x))x self.batchnorm1(x)x self.maxpool(x)x F.relu(self.conv3(x))x self.batchnorm2(x)x self.maxpool(x)x F.relu(self.conv4(x))x self.batchnorm3(x)x self.maxpool(x)x x.view(-1, 256 * 2 * 2)x self.fc1(x)x self.fc2(x)x F.log_softmax(x, dim1)return x #选择模型运行的设备 device torch.device(cuda if torch.cuda.is_available() else cpu)7、模型训练 执行模型的训练过程使用交叉熵损失函数和RMSprop优化器来定义损失计算和参数优化的方法设置了训练的轮次数并记录每个训练轮次的损失和准确率对每个训练轮次的损失和准确率进行可视化 model CNN().to(device) criterion nn.CrossEntropyLoss() optimizer optim.RMSprop(model.parameters(),lr1e-4) EPOCH_NUMBER 6 TRAIN_LOSS [] TRAIN_ACCURACY [] #训练过程 for epoch in range(1, EPOCH_NUMBER 1):epoch_loss 0.0correct 0total 0#遍历训练数据加载器for data_, target_ in train_loader:target_ target_.to(device).long()data_ data_.to(device).float()#清零优化器中之前的梯度准备计算当前轮次的梯度。optimizer.zero_grad()#将输入数据传递给模型获取模型的预测输出。outputs model(data_)loss criterion(outputs, target_)loss.backward()optimizer.step()epoch_loss epoch_loss loss.item()_, pred torch.max(outputs, dim1)#统计预测正确的样本数量将预测值与真实标签进行比较并累计正确预测的数量。correct correct torch.sum(pred target_).item()total target_.size(0)#记录每个训练轮次的损失和准确率并输出当前训练轮次的准确率和损失。TRAIN_LOSS.append(epoch_loss)TRAIN_ACCURACY.append(100 * correct / total)print(fEpoch {epoch}: Accuracy: {100 * correct / total}, Loss: {epoch_loss}) #可视化训练过程中的损失和准确率 plt.subplots(figsize(6, 4)) plt.plot(range(EPOCH_NUMBER), TRAIN_LOSS, colorblue, labelLoss) plt.legend() plt.xlabel(轮次, fontpropertiesfont_prop) plt.ylabel(损失值, fontpropertiesfont_prop) plt.title(训练损失, fontpropertiesfont_prop) plt.show() plt.subplots(figsize(6, 4)) plt.plot(range(EPOCH_NUMBER), TRAIN_ACCURACY, colorgreen, labelAccuracy) plt.legend() plt.xlabel(轮次, fontpropertiesfont_prop) plt.ylabel(准确率, fontpropertiesfont_prop) plt.title(训练准确率, fontpropertiesfont_prop) plt.show()运行截图 通过上面的数据以及图形我们可以观察到随着训练轮次的增加训练损失逐渐降低训练准确率逐渐提高。这表明模型在学习过程中逐渐减小了预测值与真实标签之间的差异提高了对训练数据的拟合能力。每轮的训练损失率都比上一轮的损失率低说明模型的优化算法有效地调整了参数使模型逐渐逼近最优解。也意味着模型在训练数据上的分类性能不断改善更准确地预测了样本的标签。每轮的训练准确率都比上一轮的高说明模型逐渐学习到了更多的特征和模式提高了对训练数据的分类准确性。总体来说损失下降和准确率提高是我们期望在训练过程中看到的趋势表明模型正在逐渐优化和提升性能。 8、性能评估 评估模型在每个类别上的性能并绘制ROC曲线以衡量模型的分类准确性 def predict_labels(model, data_loader):model.eval()y_pred []y_true []with torch.no_grad():for images, labels in data_loader:images images.to(device)labels labels.to(device)outputs model(images)_, predicted torch.max(outputs.data, 1)y_pred.extend(predicted.cpu().numpy())y_true.extend(labels.cpu().numpy())return np.array(y_pred), np.array(y_true) #获取预测结果 y_pred, y_true predict_labels(model, validation_loader) #计算每个类别的ROC曲线 fpr dict() tpr dict() roc_auc dict() num_classes len(label_map) for i in range(num_classes):fpr[i], tpr[i], _ roc_curve((np.array(y_true) i).astype(int), (np.array(y_pred) i).astype(int))roc_auc[i] auc(fpr[i], tpr[i]) #绘制ROC曲线 plt.figure(figsize(10, 8)) colors [b, g, r] # 每个类别的曲线颜色 for i in range(num_classes):plt.plot(fpr[i], tpr[i], colorcolors[i], lw2, label类别 {0} 的ROC曲线 (AUC {1:.2f}).format(i, roc_auc[i])) plt.plot([0, 1], [0, 1], colornavy, lw2, linestyle--) plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel(假阳性率, fontpropertiesfont_prop) plt.ylabel(真阳性率, fontpropertiesfont_prop) plt.title(接收者操作特征曲线, fontpropertiesfont_prop) plt.legend(loclower right, propfont_prop) plt.show()运行截图 从图片中可以看出来cat类别的ROC曲线相对于其他类别的曲线更加接近左上角而dog和wild类别的曲线则相对较低。这意味着在不同的阈值下模型更容易将cat类别正确分类为正例并且在cat类别上具有较高的真阳性率和较低的假阳性率。相比之下dog和wild类别在模型分类能力方面相对较弱表明模型更容易将它们错误地分类为其他类别。 9、测试 评估模型在验证集上对模型进行测试并计算评估指标准确率、精确率、召回率以及混淆矩阵并使用可视化工具将混淆矩阵进行可视化。 model.eval() # 将模型设置为评估模式 predictions [] # 存储预测结果和真实标签 true_labels [] #使用测试集进行预测 with torch.no_grad():for images, labels in validation_loader:images images.to(device)labels labels.to(device)outputs model(images) # 前向传播_, predicted torch.max(outputs.data, 1) # 获取预测结果predictions.extend(predicted.tolist()) # 存储预测结果和真实标签true_labels.extend(labels.tolist()) #将预测结果和真实标签转换为NumPy数组 predictions np.array(predictions) true_labels np.array(true_labels) accuracy accuracy_score(true_labels, predictions) # 计算准确率 precision precision_score(true_labels, predictions, averagemacro) # 计算精确率 recall recall_score(true_labels, predictions, averagemacro) # 计算召回率 confusion confusion_matrix(true_labels, predictions) # 计算混淆矩阵 # 打印评估结果 print(准确率:, accuracy) print(精确率:, precision) print(召回率:, recall) print(混淆矩阵:) print(confusion) # 可视化混淆矩阵 labels [Cat, Dog, Wild] plt.rcParams[font.sans-serif] [SimSun] plt.figure(figsize(8, 6)) sns.heatmap(confusion, annotTrue, fmtd, cmapBlues, xticklabelslabels, yticklabelslabels) plt.xlabel(预测标签) plt.ylabel(真实标签) plt.title(混淆矩阵) plt.show()运行截图 四 思考 1、换数据集行不行 比如动物数据集换成植物数据集等大家可以自行找公开数据集进行测试。 2、换模型行不行 用其它卷积神经网络模型试一试 LeNet-5‌由Yann LeCun等人于1998年提出主要用于手写数字识别。LeNet-5包含了卷积层、池化层和全连接层是第一个成功应用于数字识别任务的卷积神经网络模型。AlexNet‌由Alex Krizhevsky等人在2012年的ImageNet图像分类竞赛中提出。AlexNet采用了更深的网络结构和更大的数据集使用了ReLU激活函数和Dropout正则化技术取得了突破性的性能提升。VGGNet‌由Karen Simonyan和Andrew Zisserman在2014年提出。VGGNet的特点是使用了非常小的卷积核3x3并通过堆叠多个卷积层来增加网络的深度提高了特征提取的效果。GoogLeNet (Inception)‌由Google团队在2014年提出。GoogLeNet采用了Inception模块结构通过并行的多个卷积分支来提取不同尺度的特征并使用1x1的卷积核来降低计算复杂度。‌ResNet‌由Microsoft团队在2015年提出。ResNet引入了残差学习的思想通过跨层连接解决了深度网络训练中的梯度消失和梯度爆炸问题适用于大规模图像识别任务。‌MobileNet‌由Google团队在2017年提出。MobileNet采用了深度可分离卷积的结构减少了参数数量适用于移动设备等资源受限的场景。 3、图像出现以下情况怎么处理 1模糊 2光照不均匀 3扭曲变形 4有雨有雾 5图上除了动物外还有其它物体
http://www.w-s-a.com/news/483897/

相关文章:

  • 做网站分什么软件品牌设计培训
  • 太原网站设计排名设计本装修效果图
  • 网站个人中心模板石家庄网站系统开发
  • 优秀的电子商务网站教育公司网站建设文案
  • 网站开发市场成本网站链接推广工具
  • 猪八戒做网站排名常州seo博客
  • wordpress 网站遭篡改如何优化公司的网站
  • 汉中公司做网站网站建设的风格设置
  • 网站建议怎么写怎么做网页连接
  • 站长工具seo综合查询下载安装软件平台搭建包括哪几个方面
  • 做网站怎么存放视频支付功能网站建设
  • 庆阳手机网站设计兰州网站的优化
  • 企业网站托管有必要吗项目管理资格证书
  • 检索类的网站建设个人博客网页模板图片
  • 贵阳网站建设搜q479185700做网站有什么语言好
  • 制作公司主页网站贵阳网站建设技术托管
  • 广西建设网站网址多少钱南京江北新区地图
  • 网站建设及优化 赣icp外包服务美剧
  • wordpress添加菜单深圳优化网站排名
  • 免费下载建设银行官方网站重点专业建设验收网站
  • 建行官方网站登录怎样制作悬浮的WordPress
  • 建设一个网站需要几个角色广告设计与制作就业前景
  • 侵入别人的网站怎么做怎么修改网站排版
  • 网站如何提交百度收录什么最便宜网站建设
  • 商丘网站建设想象力网络做公司网站需要准备什么
  • 滁州新手跨境电商建站哪家好网站推广运作怎么做
  • 烟台有没有做网站大连建设工程信息网专家库
  • 网站建设明确细节商贸有限公司的经营范围
  • 南宁微网站开发做的好的有哪些网站
  • 好的素材下载网站读书网网站建设策划书