深度残差网络的代码实现
深度残差网络是中微软kaiming he等提出的,在ILSVRC 2015中的classification,detection,localization任务取得了冠军,且在COCO比赛上也夺冠【论文】。在这里对深度残差网络使用keras实现,这里使用的数据集是notmnist,字母A-J,十个字母的识别。(数据集小,渣渣笔记本上运行)
1. 读取数据
import os
import numpy as np
import cv2
import matplotlib.pyplot as plt
import tqdm
train_path='../datasets/notmnist/notMNIST_large/'
test_path='../datasets/notmnist/notMNIST_small/'
img_size=28
num_channels=3
# 读取文件夹图像
def read_letters(folder, size=None):
image_names=os.listdir(folder,)
images=[]
for index, img_name in enumerate(image_names):
if size and index>size-1: break
image=cv2.imread(folder+img_name)
if not image is None:
images.append(image)
return np.array(images)
A=read_letters(train_path+'A/', 100)
print('complete!')
plt.imshow(A[66])
plt.show()
complete!
def concate(x, x_):
if not x is None:
x=np.concatenate([x, x_], axis=0)
else: x=x_
return x
def get_data(train_size=None, test_size=None):
letters=['A', 'B','C','D','E','F','G','H','I','J']
x_train=y_train=x_test=y_test=None
for index, letter in tqdm.tqdm(enumerate(letters)):
x_=read_letters(train_path+letter+'/', train_size)
x_train=concate(x_train, x_)
y_=np.ndarray([x_.shape[0]], dtype=np.uint8)
y_[:]=index
y_train=concate(y_train, y_)
x_=read_letters(test_path+letter+&#