卷积神经网络-奥特曼识别

数据集

四种奥特曼图片_数据集-飞桨AI Studio星河社区 (baidu.com)

中间的隐藏层 已经使用参数的空间

Conv2D卷积层

ReLU激活层

MaxPool2D最大池化层

AdaptiveAvgPool2D自适应的平均池化

Linear全链接层

Dropout放置过拟合,随机丢弃神经元

python 复制代码
--------------------------------------------------------------------------------
   Layer (type)          Input Shape          Output Shape         Param #    
================================================================================
     Conv2D-1        [[50, 3, 227, 227]]   [50, 64, 227, 227]       1,792     
      ReLU-1        [[50, 64, 227, 227]]   [50, 64, 227, 227]         0       
     Conv2D-2       [[50, 64, 227, 227]]   [50, 64, 227, 227]      36,928     
      ReLU-2        [[50, 64, 227, 227]]   [50, 64, 227, 227]         0       
    MaxPool2D-1     [[50, 64, 227, 227]]   [50, 64, 113, 113]         0       
     Conv2D-3       [[50, 64, 113, 113]]  [50, 128, 113, 113]      73,856     
      ReLU-3        [[50, 128, 113, 113]] [50, 128, 113, 113]         0       
     Conv2D-4       [[50, 128, 113, 113]] [50, 128, 113, 113]      147,584    
      ReLU-4        [[50, 128, 113, 113]] [50, 128, 113, 113]         0       
    MaxPool2D-2     [[50, 128, 113, 113]]  [50, 128, 56, 56]          0       
     Conv2D-5        [[50, 128, 56, 56]]   [50, 256, 56, 56]       295,168    
      ReLU-5         [[50, 256, 56, 56]]   [50, 256, 56, 56]          0       
     Conv2D-6        [[50, 256, 56, 56]]   [50, 256, 56, 56]       590,080    
      ReLU-6         [[50, 256, 56, 56]]   [50, 256, 56, 56]          0       
     Conv2D-7        [[50, 256, 56, 56]]   [50, 256, 56, 56]       590,080    
      ReLU-7         [[50, 256, 56, 56]]   [50, 256, 56, 56]          0       
    MaxPool2D-3      [[50, 256, 56, 56]]   [50, 256, 28, 28]          0       
     Conv2D-8        [[50, 256, 28, 28]]   [50, 512, 28, 28]      1,180,160   
      ReLU-8         [[50, 512, 28, 28]]   [50, 512, 28, 28]          0       
     Conv2D-9        [[50, 512, 28, 28]]   [50, 512, 28, 28]      2,359,808   
      ReLU-9         [[50, 512, 28, 28]]   [50, 512, 28, 28]          0       
     Conv2D-10       [[50, 512, 28, 28]]   [50, 512, 28, 28]      2,359,808   
      ReLU-10        [[50, 512, 28, 28]]   [50, 512, 28, 28]          0       
    MaxPool2D-4      [[50, 512, 28, 28]]   [50, 512, 14, 14]          0       
     Conv2D-11       [[50, 512, 14, 14]]   [50, 512, 14, 14]      2,359,808   
      ReLU-11        [[50, 512, 14, 14]]   [50, 512, 14, 14]          0       
     Conv2D-12       [[50, 512, 14, 14]]   [50, 512, 14, 14]      2,359,808   
      ReLU-12        [[50, 512, 14, 14]]   [50, 512, 14, 14]          0       
     Conv2D-13       [[50, 512, 14, 14]]   [50, 512, 14, 14]      2,359,808   
      ReLU-13        [[50, 512, 14, 14]]   [50, 512, 14, 14]          0       
    MaxPool2D-5      [[50, 512, 14, 14]]    [50, 512, 7, 7]           0       
AdaptiveAvgPool2D-1   [[50, 512, 7, 7]]     [50, 512, 7, 7]           0       
     Linear-1           [[50, 25088]]          [50, 4096]        102,764,544  
      ReLU-14           [[50, 4096]]           [50, 4096]             0       
     Dropout-1          [[50, 4096]]           [50, 4096]             0       
     Linear-2           [[50, 4096]]           [50, 4096]        16,781,312   
      ReLU-15           [[50, 4096]]           [50, 4096]             0       
     Dropout-2          [[50, 4096]]           [50, 4096]             0       
     Linear-3           [[50, 4096]]            [50, 4]            16,388     
================================================================================
Total params: 134,276,932
Trainable params: 134,276,932
Non-trainable params: 0
--------------------------------------------------------------------------------
Input size (MB): 29.49
Forward/backward pass size (MB): 11120.24
Params size (MB): 512.23
Estimated Total Size (MB): 11661.95
--------------------------------------------------------------------------------

如果paddle还没配置的话建议去网上搜一下,这里就不给链接了

用于训练模型的代码

python 复制代码
import paddle
from paddle.io import Dataset,DataLoader
import os
from PIL import Image
import numpy as np
import paddle.vision.transforms as T
import matplotlib.pyplot as plt
from paddle.vision.datasets import DatasetFolder

transforms=T.Compose([T.Resize([227,227]),T.RandomRotation(degrees=[-10,10]),T.ColorJitter(0.4,0.4,0.4,0.4),T.ToTensor()])
dataset=DatasetFolder("aoteman",extensions=[".jpg"],transform=transforms)
#使用paddle.io.random_split切分训练集和测试集
from paddle.io import random_split
train_size=int(0.8*len(dataset))
test_size=len(dataset)-train_size
train_dataset,test_dataset=random_split(dataset=dataset,lengths=[train_size,test_size])
print(len(train_dataset),len(test_dataset))

# plt.figure(figsize=[3,3])
# for idx,data in enumerate(train_dataset):
#     plt.subplot(3,3,idx+1)
#     im=data[0];label=data[1]
#     im=im.reshape([224,224,3])
#     plt.imshow(im)
#     if idx+1>=9:
#         break
# plt.show()

print(dataset.class_to_idx)

net=paddle.vision.models.vgg16(pretrained=True, num_classes=4)
paddle.summary(net,(50,3,227,227))

#网络配置
lr=0.001
batch_size=50
#预训练模型优化器 Adam优化器
opt =paddle.optimizer.Adam(learning_rate=lr,parameters=net.classifier.parameters())
#损失函数
loss_fn=paddle.nn.CrossEntropyLoss()
#训练模式
net.train()
model=paddle.Model(net)
model.prepare(optimizer=opt,loss=loss_fn,metrics=paddle.metric.Accuracy())
import time
vsdl=paddle.callbacks.VisualDL(log_dir='vsdl/trainlog'+str(time.time()))
# model.load('mymodel/vgg_aoteman')
# res=model.predict()
model.fit(train_data=train_dataset,eval_data=test_dataset, batch_size=batch_size,
          epochs=1, verbose=1,shuffle=True,callbacks=vsdl)
model.save('mymodel/vgg_aoteman')

用于预测模型的代码

python 复制代码
import math

import paddle
import paddle.vision.transforms as T

from PIL import Image
from paddle.vision.datasets import DatasetFolder
import numpy as np

transforms = T.Compose([T.Resize([227, 227]), T.ToTensor()])
# 使用paddle.io.random_split切分训练集和测试集

img = Image.open('aoteman/predict_demo.jpg')#输入图片
img.show()
img = transforms(img)
img = img.unsqueeze(0)

start_index = 0  # 开始切片的索引
end_index = 3    # 结束切片的索引
axes = [1]       # 要切片的轴(通道轴)
img = paddle.slice(img, axes=axes, starts=[start_index], ends=[end_index])



net = paddle.vision.models.vgg16(pretrained=True, num_classes=4)
# 网络配置
lr = 0.001
batch_size = 50
# 预训练模型优化器 Adam优化器
opt = paddle.optimizer.Adam(learning_rate=lr, parameters=net.classifier.parameters())
# 损失函数
loss_fn = paddle.nn.CrossEntropyLoss()
# 训练模式
net.train()
model = paddle.Model(net)
model.prepare(optimizer=opt, loss=loss_fn, metrics=paddle.metric.Accuracy())
import time

vsdl = paddle.callbacks.VisualDL(log_dir='vsdl/trainlog' + str(time.time()))
model.load('mymodel/vgg_aoteman')

# print(img)
res = model.predict_batch(img)

sum=0
maxx=-1000000
idx=0
for i in range(4):
    # sum+=math.exp(res[0][0][i])
    if res[0][0][i]>maxx:
        maxx=res[0][0][i]
        idx=i
    # print(res[0][0][i])
# print(res)
# print(math.exp(res[0][0][idx])/sum*100,end='%:   ')
if idx==0:
    print("迪迦")
elif idx==1:
    print('杰克')
elif idx==2:
    print('赛文')
else:
    print('泰罗')
相关推荐
成富41 分钟前
文本转SQL(Text-to-SQL),场景介绍与 Spring AI 实现
数据库·人工智能·sql·spring·oracle
CSDN云计算1 小时前
如何以开源加速AI企业落地,红帽带来新解法
人工智能·开源·openshift·红帽·instructlab
艾派森1 小时前
大数据分析案例-基于随机森林算法的智能手机价格预测模型
人工智能·python·随机森林·机器学习·数据挖掘
hairenjing11231 小时前
在 Android 手机上从SD 卡恢复数据的 6 个有效应用程序
android·人工智能·windows·macos·智能手机
小蜗子1 小时前
Multi‐modal knowledge graph inference via media convergenceand logic rule
人工智能·知识图谱
SpikeKing1 小时前
LLM - 使用 LLaMA-Factory 微调大模型 环境配置与训练推理 教程 (1)
人工智能·llm·大语言模型·llama·环境配置·llamafactory·训练框架
黄焖鸡能干四碗2 小时前
信息化运维方案,实施方案,开发方案,信息中心安全运维资料(软件资料word)
大数据·人工智能·软件需求·设计规范·规格说明书
2 小时前
开源竞争-数据驱动成长-11/05-大专生的思考
人工智能·笔记·学习·算法·机器学习
ctrey_2 小时前
2024-11-4 学习人工智能的Day21 openCV(3)
人工智能·opencv·学习
攻城狮_Dream2 小时前
“探索未来医疗:生成式人工智能在医疗领域的革命性应用“
人工智能·设计·医疗·毕业