AlexNet结构 及 pytorch、tensorflow、keras、paddle实现ImageNet识别
环境
python3.6, paddlepaddle-gpu 1.6.3.post107
代码
# -*- coding: utf-8 -*-
# @Time : 2020/1/21 11:18
# @Author : Zhao HL
# @File : alexnet-paddle.py
import os, sys
from PIL import Image
import numpy as np
import pandas as pd
import paddle
from paddle import fluid
from paddle.fluid.layers import data, conv2d, pool2d, flatten, fc, cross_entropy, accuracy, mean
from my_utils import process_show, draw_loss_acc
# region parameters
# region paths
Data_path = "./data/my_imagenet"
Data_csv_path = "./data/my_imagenet.csv"
Model_path = 'model/'
Model_file_tf = "model/alexnet_tf.ckpt"
Model_file_keras = "model/alexnet_keras.h5"
Model_file_torch = "model/alexnet_torch.pth"
Model_file_paddle = "model/alexnet_paddle.model"
# endregion
# region image parameter
Img_size = 227
Img_chs = 3
Label_size = 1
Label_class = {'n02091244': 'Ibizan hound',
'n02114548': 'white wolf',
'n02138441': 'meerkat',
'n03584254': 'iPod',
'n03075370': 'combination lock',
'n09256479': 'coral reef',
'n03980874': 'poncho',
'n02174001': 'rhinoceros beetle',
'n03770439': 'miniskirt',
'n03773504': 'missile'}
Labels_nums = len(Label_class)
# endregion
# region net parameter
Conv1_kernel_size = 11
Conv1_chs = 96
Conv2_kernel_size = 5
Conv2_chs = 256
Conv3_kernel_size = 3
Conv3_chs = 384
Conv4_kernel_size = 3
Conv4_chs = 384
Conv5_kernel_size = 3
Conv5_chs = 256
Flatten_size = 6 * 6 * 256
Fc1_size = 4096
Fc2_size = 4096
Fc3_size = Labels_nums
# endregion
# region hpyerparameter
Learning_rate = 1e-4
Batch_size = 32
Buffer_size = 256
Infer_size = 1
Epochs = 10
Train_num = 700
Train_batch_num = Train_num // Batch_size
Val_num = 100
Val_batch_num = Val_num // Batch_size
Test_num = 200
Test_batch_num = Test_num // Batch_size
# endregion
place = fluid.CUDAPlace(0) if fluid.cuda_places() else fluid.CPUPlace()
# endregion
class MyDataset():
def __init__(self, root_path, batch_size, files_list=None,):
self.root_path = root_path
self.files_list = files_list if files_list else os.listdir(root_path)
self.size = len(files_list)
self.batch_size = batch_size
def __len__(self):
return self.size
def dataset_reader(self):
pass
files_list = self.files_list if self.files_list is not None else os.listdir(self.root_path)
def reader():
np.random.shuffle(files_list)
for file_name in files_list:
label_str = file_name[:9]
label = list(Label_class.keys()).index(label_str)
img = Image.open(os.path.join(self.root_path, file_name))
yield img, label
return paddle.batch(paddle.reader.xmap_readers(self.transform, reader, 2, Buffer_size), batch_size=self.batch_size)
def transform(self,sample):
def Normalize(image, means, stds):
for band in range(len(means)):
image[:, :, band] = image[:, :, band] / 255.0
image[:, :, band] = (image[:, :, band] - means[band]) / stds[band]
image = np.transpose(image, [2, 1, 0])
return image
pass
image, label = sample
image = image.resize((Img_size, Img_size), Image.ANTIALIAS)
image = Normalize(np.array(image).astype(np.float), [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
return image, label
class AlexNet:
def __init__(self, structShow=False):
self.structShow = structShow
self.image = data(shape=[Img_chs, Img_size, Img_size], dtype='float32', name='image')
self.label = data(shape=[Label_size], dtype='int64', name='label')
self.predict = self.get_alexNet()
def get_alexNet(self):
conv1 = conv2d(self.image, Conv1_chs, filter_size=Conv1_kernel_size, stride=4, padding=0, act='relu')
pool1 = pool2d(conv1, 3, pool_stride=2, pool_type='max')
conv2 = conv2d(pool1, Conv2_chs, filter_size=Conv2_kernel_size, padding=2, act='relu')
pool2 = pool2d(conv2, 3, pool_stride=2, pool_type='max')
conv3 = conv2d(pool2, Conv3_chs, filter_size=Conv3_kernel_size, padding=1, act='relu')
conv4 = conv2d(conv3, Conv4_chs, filter_size=Conv4_kernel_size, padding=1, act='relu')
conv5 = conv2d(conv4, Conv5_chs, filter_size=Conv5_kernel_size, padding=1, act='relu')
pool3 = pool2d(conv5, 3, pool_stride=2, pool_type='max')
flt = flatten(pool3, axis=1)
fc1 = fc(flt, Fc1_size, act='relu')
fc2 = fc(fc1, Fc2_size, act='relu')
fc3 = fc(fc1, Fc3_size, act='softmax')
if self.structShow:
print(conv1.name, conv1.shape)
print(pool1.name, pool1.shape)
print(conv2.name, conv2.shape)
print(pool2.name, pool2.shape)
print(conv3.name, conv3.shape)
print(conv4.name, conv4.shape)
print(conv5.name, conv5.shape)
print(pool3.name, pool3.shape)
print(flt.name, flt.shape)
print(fc1.name, fc1.shape)
print(fc2.name, fc2.shape)
print(fc3.name, fc3.shape)
return fc3
def train():
net = AlexNet(structShow=True)
image, label, predict = net.image, net.label, net.predict
feeder = fluid.DataFeeder(place=place, feed_list=[image, label])
df = pd.read_csv(Data_csv_path, header=0, index_col=0)
train_list = df[df['split'] == 'train']['filename'].tolist()
val_list = df[df['split'] == 'val']['filename'].tolist()
train_reader = MyDataset(Data_path, batch_size=Batch_size, files_list=train_list).dataset_reader()
val_reader = MyDataset(Data_path, batch_size=Batch_size, files_list=val_list).dataset_reader()
loss = cross_entropy(input=predict, label=label)
loss_mean = mean(loss)
acc = accuracy(input=predict, label=label)
optimizer = fluid.optimizer.AdamOptimizer(learning_rate=Learning_rate)
optimizer.minimize(loss_mean)
val_program = fluid.default_main_program().clone(for_test=True)
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
train_losses = np.ones(Epochs)
train_accs = np.ones(Epochs)
val_losses = np.ones(Epochs)
val_accs = np.ones(Epochs)
best_loss = float("inf")
best_loss_epoch = 0
for epoch in range(Epochs):
print('Epoch %d/%d:' % (epoch + 1, Epochs))
train_sum_loss = 0
train_sum_acc = 0
val_sum_loss = 0
val_sum_acc = 0
for batch_num, data in enumerate(train_reader()):
train_loss, train_acc = exe.run(program=fluid.default_main_program(), # 运行主程序
feed=feeder.feed(data), # 给模型喂入数据
fetch_list=[loss_mean, acc]) # fetch 误差、准确率
train_sum_loss += train_loss[0]
train_sum_acc += train_acc[0]
process_show(batch_num + 1, Train_num / Batch_size, train_acc, train_loss, prefix='train:')
for batch_num, data in enumerate(val_reader()):
val_loss, val_acc = exe.run(program=val_program, # 执行训练程序
feed=feeder.feed(data), # 喂入数据
fetch_list=[loss_mean, acc]) # fetch 误差、准确率
val_sum_loss += val_loss[0]
val_sum_acc += val_acc[0]
process_show(batch_num + 1, Val_num / Batch_size, val_acc, val_loss, prefix='train:')
train_sum_loss /= (Train_num // Batch_size)
train_sum_acc /= (Train_num // Batch_size)
val_sum_loss /= (Val_num // Batch_size)
val_sum_acc /= (Val_num // Batch_size)
train_losses[epoch] = train_sum_loss
train_accs[epoch] = train_sum_acc
val_losses[epoch] = val_sum_loss
val_accs[epoch] = val_sum_acc
print('average summary:\ntrain acc %.4f, loss %.4f ; val acc %.4f, loss %.4f'
% (train_sum_acc, train_sum_loss, val_sum_acc, val_sum_loss))
if val_sum_loss < best_loss:
print('val_loss improve from %.4f to %.4f, model save to %s ! \n' % (
best_loss, val_sum_loss, Model_file_paddle))
best_loss = val_sum_loss
best_loss_epoch = epoch + 1
fluid.io.save_inference_model(Model_file_paddle, # 保存推理model的路径
['image'], # 推理(inference)需要 feed 的数据
[predict], # 保存推理(inference)结果的 Variables
exe) # executor 保存 inference model
else:
print('val_loss do not improve from %.4f \n' % (best_loss))
print('best loss %.4f at epoch %d \n' % (best_loss, best_loss_epoch))
draw_loss_acc(train_losses, train_accs, 'train')
draw_loss_acc(val_losses, val_accs, 'val')
if __name__ == '__main__':
pass
# dataInfo_show(r'E:\_Python\01_deeplearning\03_AlexNet\data\my_imagenet',
# r'E:\_Python\01_deeplearning\03_AlexNet\data\my_imagenet.csv',
# r'E:\_Python\01_deeplearning\03_AlexNet\data\synset_words.txt')
# dataset_divide(r'E:\_Python\01_deeplearning\03_AlexNet\data\my_imagenet.csv')
train()
my_utils.py
# -*- coding: utf-8 -*-
# @Time : 2020/1/21 11:39
# @Author : Zhao HL
# @File : my_utils.py
import sys,os,random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
def process_show(num, nums, train_acc, train_loss, prefix='', suffix=''):
rate = num / nums
ratenum = int(round(rate, 2) * 100)
bar = '\r%s batch %3d/%d:train accuracy %.4f, train loss %00.4f [%s%s]%.1f%% %s; ' % (
prefix, num, nums, train_acc, train_loss, '#' * (ratenum//2), '_' * (50 - ratenum//2), ratenum, suffix)
sys.stdout.write(bar)
sys.stdout.flush()
if num >= nums:
print()
def dataInfo_show(data_path,csv_pth,cls_dic_path,shapesShow=True,classesShow=True):
cls_dict = get_cls_dic(cls_dic_path)
if classesShow:
print('\n'+'*'*50)
df = pd.read_csv(csv_pth)
labels = df['label'].unique()
label_cls = {label:cls_dict[label] for label in labels}
print(label_cls)
cls_count = df['label'].value_counts()
cls_count = {cls_dict[k]:v for k,v in cls_count.items()}
for k,v in cls_count.items():
print(k,v)
if shapesShow:
print('\n'+'*'*50)
shapes = []
for filename in os.listdir(data_path):
img = Image.open(os.path.join(data_path, filename))
img = np.array(img)
shapes.append(img.shape)
shapes = pd.Series(shapes)
print(shapes.value_counts())
def get_cls_dic(cls_dic_path):
# 读取类标签字典,只取第一个逗号前的信息
cls_df = pd.read_csv(cls_dic_path)
cls_df['cls'] = cls_df['info'].apply(lambda x:x[:9]).tolist()
cls_df['label'] = cls_df['info'].apply(lambda x: x[10:]).tolist()
cls_df = cls_df.drop(columns=['info','other'])
cls_dict = cls_df.set_index('cls').T.to_dict('list')
cls_dict = {k:v[0] for k,v in cls_dict.items()}
return cls_dict
def dataset_divide(csv_pth):
cls_df = pd.read_csv(csv_pth, header=0,index_col=0)
cls_df.insert(2,'split',None)
filenames = list(cls_df['filename'])
random.shuffle(filenames)
train_num,train_val_num = int(len(filenames)*0.7),int(len(filenames)*0.8)
train_names = filenames[:train_num]
val_names = filenames[train_num:train_val_num]
test_names = filenames[train_val_num:]
cls_df.loc[cls_df['filename'].isin(train_names),'split'] = 'train'
cls_df.loc[cls_df['filename'].isin(val_names), 'split'] = 'val'
cls_df.loc[cls_df['filename'].isin(test_names), 'split'] = 'test'
cls_df.to_csv(csv_pth)
def draw_loss_acc(loss,acc,type='',save_path=None):
assert len(acc) == len(loss)
x = [epoch for epoch in range(len(acc))]
plt.subplot(2, 1, 1)
plt.plot(x, acc, 'o-')
plt.title(type+' accuracy vs. epoches')
plt.ylabel('accuracy')
plt.subplot(2, 1, 2)
plt.plot(x, loss, '.-')
plt.xlabel(type+' loss vs. epoches')
plt.ylabel('loss')
plt.show()
if save_path:
plt.savefig(os.path.join(save_path,type+"_acc_loss.png"))
if __name__ == '__main__':
pass
来源:CSDN
作者:GISer_Lin
链接:https://blog.csdn.net/nominior/article/details/104081576