1.研究成果
AlexNet在ILSVRC-2012以超出第二名10.9个百分点夺冠
2.研究意义
- 拉开卷积神经网络统治计算机是觉得许墨
-
加速计算机视觉应用落地
3.摘要内容
- 在ILSVRC-2010的120万张图片上训练深度卷积神经网络,获得最优结果,top-1和top-5error分别为 37.5%, 17%
- 该网络(AlexNet)由5个卷积层和3个全连接层构成,共计6000万参数,65万个神经元
- 为加快训练,采用非饱和激活函数——ReLU,采用GPU训练
- 为减轻过拟合,采用Dropout
- 基于以上模型及技巧,在ILSVRC-2012以超出第二名10.9个百分点成绩夺冠
4.模型分析
卷积输出特征图:
其中,
为特征图的边宽,
为原特侦图的变宽,k为卷积核的边宽,p为padding,s为stride。
连接数量计算公式:
前一层的通道数,
为卷积核的宽,
为通道数(卷积核数量)
Layer Name | Filter | Padding | Kernel | Stride | ImgSize | Tensor Size | Weights | Biases | Parameters |
---|---|---|---|---|---|---|---|---|---|
Input Image | 227*227*3 | 0 | 0 | 0 | |||||
Conv-1 | 96 | 0 | 11*11 | 4 | (227-11+2*0)/4+1 =55 | 55*55*96 | 3*(11*11) *96=34848 | 96 | 34,944 |
MaxPool-1 | 96 | 2 | 3*3 | 2 | (55-3)/2+1=27 | 27*27*96 | 0 | 0 | 0 |
Conv-2 | 256 | 2 | 5*5 | 1 | 27*27*256 | 96*(5*5)*256= 614400 | 256 | 614,656 | |
MaxPool-2 | 256 | 0 | 3*3 | 2 | 13*13*256 | 0 | 0 | 0 | |
Conv-3 | 384 | 1 | 3*3 | 1 | 13*13*384 | 256*(3*3)*384= 884736 | 384 | 885,120 | |
Conv-4 | 384 | 1 | 3*3 | 1 | 13*13*384 | 384*(3*3)*384= 1327104 | 384 | 1,327,488 | |
Conv-5 | 256 | 1 | 3*3 | 1 | 13*13*256 | 384*(3*3)*256= 884736 | 256 | 884,992 | |
MaxPool-3 | 256 | 0 | 3*3 | 2 | 6*6*256 | 0 | 0 | 0 | |
FC-1 | 4096*1 | (6*6*256)*4096= 37748736 | 4,096 | 37,752,832 | |||||
FC-2 | 4096*1 | 4096*4096= 16777216 | 4,096 | 16,781,312 | |||||
FC-3 | 1000*1 | 4096*1000= 4096000 | 1,000 | 4,097,000 | |||||
Output | 1000*1 | 0 | 0 | 0 | |||||
Total |
5.结构特点
ReLU Nonlinearity
A four-layer convolutional neural network with ReLUs (solid line) reaches a 25% training error rate on CIFAR-10 six times fasterthan an equivalent network with tanh neurons (dashed line) .
Local Response Normalization
i:代表下标,你要计算像素值的下标,从0计算起
j:平方累加索引,代表从j~i的像素值平方求和
x,y:像素的位置,公式中用不到
a:代表feature map里面的 i 对应像素的具体值
N:每个feature map里面最内层向量的列数
k:超参数,由原型中的blas指定
α:超参数,由原型中的alpha指定
n/2:超参数,由原型中的deepth_radius指定
β:超参数,由原型中的belta指定
关注部分,这部分越大抑制情况越明显。假设有一个神经元权值巨大,周围的比较小,那么临近它的神经元在分母上的和都比较大,从而造成了对其的抑制。
Response normalization reduces our top-1 and top-5 error rates by 1.4% and 1.2%, respectively. We also verifified the effectiveness of this scheme on the CIFAR-10 dataset: a four-layer CNN achieved a 13% test error rate without normalization and 11% with normalization 3 .
使用了LRN之后在top-1和top-5上分别提高了 1.4%和1.2%。
Data Augmentation
文章中对训练图片进行了预处理,增强效果,具体如下:
- 裁剪图片
训练阶段:
1.图像统一缩放至256*256
2.随机位置裁剪出224*224区域
3.随机进行水平翻转
测试阶段:
1.图片统一缩放至256*256
2.裁剪出5个224*224区域
3.均进行水平翻转,共得到10张224*224图片
- 针对颜色图像扰动
通过PCA方法修改RGB通道的像素值,实现颜色扰动,效果有限,仅在top-1提升1个点(acc约为62.5%),实际上现在有更好办法。
常用的图像增强方法,翻转,裁剪,灰度,对比度,色彩(改变数据的分布、改变通道的pai'lie)
代码改进
LRN取消,为什么取消呢?效果平庸为啥呢?BN的介绍。
https://zhuanlan.zhihu.com/p/87117010
Dropout、reLu
https://space.bilibili.com/390756902/video
5.代码实现
函数主体部分
在本示例代码中用了预训练的参数集
在函数主题部分主要分为5个步骤:
1. 加载测试图片
2. 加载预训练模型
3. 获得分类结果
4. 在json文件中找到预测的名称并打印
5. 显示结果
if __name__ =="__main__":
# config
# alexnet 训练结果
path_state_dict = os.path.join(BASE_DIR,"..","data","alexnet-owt-4df8aa71.pth")
# 被测试的图片
path_img = os.path.join(BASE_DIR,"..","data","1.jpg")
path_classnames = os.path.join(BASE_DIR, "..", "data", "imagenet1000.json")
path_classnames_cn = os.path.join(BASE_DIR, "..", "data", "imagenet_classnames.txt")
#load class names, cls_n,cls_n_cn分别为分类的内容
cls_n, cls_n_cn = load_class_names(path_classnames,path_classnames_cn)
# 1/5 load img
img_tensor, img_rgb = process_img(path_img)
# 2/5 load model
alexnet_model = get_model(path_state_dict,True)
# 3/5 inference tensor --> vector
# time consuming:time_toc-time_tic 判断时间
with torch.no_grad():
time_tic = time.time()
outputs = alexnet_model(img_tensor)
time_toc = time.time()
# 4/5 index to class names
# 返回两个tensor , 1. 每行的最大值。2.每行最大值的位置
_, pred_int = torch.max(outputs.data, 1)
_, top5_idx = torch.topk(outputs.data, 5, dim=1)
# 如果想把CUDA tensor格式的数据改成numpy时,需要先将其转换成cpu float-tensor随后再转到numpy格式。 numpy不能读取CUDA tensor 需要将它转化为 CPU tensor
pred_idx = int(pred_int.cpu().numpy())
pred_str, pred_cn = cls_n[pred_idx], cls_n_cn[pred_idx]
print("img: {} is: {}\n{}".format(os.path.basename(path_img), pred_str, pred_cn))
print("time consuming:{:.2f}s".format(time_toc - time_tic))
# 5/5 visualization
plt.imshow(img_rgb)
plt.title("predict:{}".format(pred_str))
top5_num = top5_idx.cpu().numpy().squeeze()
text_str = [cls_n[t] for t in top5_num]
for idx in range(len(top5_num)):
plt.text(5, 15+idx*30, "top {}:{}".format(idx+1, text_str[idx]), bbox=dict(fc='yellow'))
plt.show()
功能函数
根据分类文件的路径读取json文件内容
def load_class_names(path_classnames, path_classnames_cn):
"""
@param path_classnames: imagenet1000.json 目录
@param path_classnames_cn:
@return:
"""
with open(path_classnames,"r") as f:
class_names = json.load(f)
with open(path_classnames_cn, encoding='UTF-8') as f: # 设置文件对象
class_names_cn = f.readlines()
return class_names,class_names_cn
输入图片对图片进行预处理
def process_img(path_img):
"""
# 对图片进行预处理
@param path_img:输入的预测图片
@return:
"""
norm_mean = [0.485, 0.456, 0.406]
norm_std = [0.229, 0.224, 0.225]
inference_transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop((224, 224)),
transforms.ToTensor(),
transforms.Normalize(norm_mean, norm_std),
])
# path --> img
img_rgb = Image.open(path_img).convert('RGB')
# img --> tensor
img_tensor = img_transform(img_rgb, inference_transform)
img_tensor.unsqueeze_(0) # chw --> bchw
img_tensor = img_tensor.to(device)
return img_tensor, img_rgb
def img_transform(img_rgb, inference_transform=None):
"""
将数据转换为模型读取的形式
@param img_rgb:
@param inference_transform:
@return:
"""
if inference_transform is None:
raise ValueError("找不到transform!必须有transform对img进行处理")
img_t = inference_transform(img_rgb)
return img_t
获取模型,其中model.eval代表文章中的dropout随机失活,torchsummary可以打印每层结构
def get_model(path_state_dict, param):
"""
创建模型,加载参数
@param path_state_dict:
@param param:
@return:
"""
model = models.alexnet()
pretrained_state_dict = torch.load(path_state_dict)
model.load_state_dict(pretrained_state_dict)
# drop 函数
model.eval()
# 打印torch每层形状
if param:
from torchsummary import summary
summary(model, input_size=(3,224,224), device="cpu")
model.to(device)
return model
6.效果展示
控制台打印:
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 64, 55, 55] 23,296
ReLU-2 [-1, 64, 55, 55] 0
MaxPool2d-3 [-1, 64, 27, 27] 0
Conv2d-4 [-1, 192, 27, 27] 307,392
ReLU-5 [-1, 192, 27, 27] 0
MaxPool2d-6 [-1, 192, 13, 13] 0
Conv2d-7 [-1, 384, 13, 13] 663,936
ReLU-8 [-1, 384, 13, 13] 0
Conv2d-9 [-1, 256, 13, 13] 884,992
ReLU-10 [-1, 256, 13, 13] 0
Conv2d-11 [-1, 256, 13, 13] 590,080
ReLU-12 [-1, 256, 13, 13] 0
MaxPool2d-13 [-1, 256, 6, 6] 0
AdaptiveAvgPool2d-14 [-1, 256, 6, 6] 0
Dropout-15 [-1, 9216] 0
Linear-16 [-1, 4096] 37,752,832
ReLU-17 [-1, 4096] 0
Dropout-18 [-1, 4096] 0
Linear-19 [-1, 4096] 16,781,312
ReLU-20 [-1, 4096] 0
Linear-21 [-1, 1000] 4,097,000
================================================================
Total params: 61,100,840
Trainable params: 61,100,840
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 8.38
Params size (MB): 233.08
Estimated Total Size (MB): 242.03
----------------------------------------------------------------
img: tiger cat.jpg is: tabby, tabby cat
281 n02123045 猫, tabby, tabby cat
time consuming:1.75s
Process finished with exit code 0
预测图片展示: