数据集
四种奥特曼图片_数据集-飞桨AI Studio星河社区 (baidu.com)
中间的隐藏层 已经使用参数的空间
Conv2D卷积层
ReLU激活层
MaxPool2D最大池化层
AdaptiveAvgPool2D
自适应的平均池化
Linear全链接层
Dropout放置过拟合,随机丢弃神经元
--------------------------------------------------------------------------------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还没配置的话建议去网上搜一下,这里就不给链接了
用于训练模型的代码
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 DatasetFoldertransforms=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')
用于预测模型的代码
import mathimport paddle
import paddle.vision.transforms as Tfrom PIL import Image
from paddle.vision.datasets import DatasetFolder
import numpy as nptransforms = 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 timevsdl = 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('泰罗')