有任何问题欢迎在下面留言
本篇文章的代码运行界面均在Jupyter Notebook中进行
本篇文章配套的代码资源已经上传
深度学习中出现了随着网络的堆叠效果下降的现象,Resnet使用残差连接的方法解决了这个问题,让深度学习从此变得深了起来。
残差连接的做法可以表示为:
y = f ( x ) + x ( s h o r t c u t ) y = f(x)+x(shortcut) y=f(x)+x(shortcut)
其中y表示网络最后的输出,x为输入,f(x)则表示输入经过几层网络的输出结果,比如三次卷积+三次批归一化+三次relu,一般情况下f(x)就是网络的最终输出,这里再加上x就是一个残差连接的操作。
残差连接的操作保证了,x经过网络后得到的y一定比f(x)更好的结果,最差是同等效果,也就是保证了不会出现效果降低的情况。
这个shortcut是什么意思呢?因为x经过几次卷积后,可能会出现多个特征图,也就是f(x)和x的通道数不一样了,这个时候就需要对x的通道数进行调整再与f(x)相加得到y,如果通道数一样就不需要调整了
上图就是通道数没有发生变化的情况, y = f ( x ) + x y = f(x)+x y=f(x)+x,x经过两次(卷积+批归一化+ReLU)和一次(卷积+批归一化)后得到f(x),再加上x后经过ReLU就得到了最终的y
上图就是通道数发生变化的情况, y = f ( x ) + C o n v 2 d ( x ) y = f(x)+Conv2d(x) y=f(x)+Conv2d(x),x经过两次(卷积+批归一化+ReLU)和一次(卷积+批归一化)后得到f(x),x再经过一次(二维卷积+批归一化),这个二维卷积的卷积核是1x1的,经过这个二维卷积的x再加上f(x)后经过ReLU就得到了最终的y
from __future__ import absolute_import, division, print_function
import tensorflow as tf
from models import resnet50, resnet101, resnet152, resnet34
import config
from prepare_data import generate_datasets
import math
if __name__ == '__main__':
# GPU settings
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
导入项目工具包和辅助函数
配置 TensorFlow 中的 GPU 内存,:
tf.config.experimental.list_physical_devices('GPU')
:这个函数调用列出了 TensorFlow 在你的机器上可用的所有 GPUif gpus:
这个检查用来确认是否有可用的 GPU。如果有,它将继续对每一个 GPU 进行配置tf.config.experimental.set_memory_growth(gpu, True)
,这使得 GPU 上的内存增长被启用# get the original_dataset
train_dataset, valid_dataset, test_dataset, train_count, valid_count, test_count = generate_datasets()
def generate_datasets():
train_dataset, train_count = get_dataset(dataset_root_dir=config.train_dir)
valid_dataset, valid_count = get_dataset(dataset_root_dir=config.valid_dir)
test_dataset, test_count = get_dataset(dataset_root_dir=config.test_dir)
# read the original_dataset in the form of batch
train_dataset = train_dataset.shuffle(buffer_size=train_count).batch(batch_size=config.BATCH_SIZE)
valid_dataset = valid_dataset.batch(batch_size=config.BATCH_SIZE)
test_dataset = test_dataset.batch(batch_size=config.BATCH_SIZE)
return train_dataset, valid_dataset, test_dataset, train_count, valid_count, test_count
def get_dataset(dataset_root_dir):
all_image_path, all_image_label = get_images_and_labels(data_root_dir=dataset_root_dir)
# print("image_path: {}".format(all_image_path[:]))
# print("image_label: {}".format(all_image_label[:]))
# load the dataset and preprocess images
image_dataset = tf.data.Dataset.from_tensor_slices(all_image_path).map(load_and_preprocess_image)
label_dataset = tf.data.Dataset.from_tensor_slices(all_image_label)
dataset = tf.data.Dataset.zip((image_dataset, label_dataset))
image_count = len(all_image_path)
return dataset, image_count
def get_images_and_labels(data_root_dir):
# 得到所有图像路径
data_root = pathlib.Path(data_root_dir)
all_image_path = [str(path) for path in list(data_root.glob('*/*'))]
# 得到标签名字
label_names = sorted(item.name for item in data_root.glob('*/'))
# 例如:{'cats': 0, 'dogs': 1, 'panda': 2}
label_to_index = dict((index, label) for label, index in enumerate(label_names))
# 每一个图像对应的标签
all_image_label = [label_to_index[pathlib.Path(single_image_path).parent.name] for single_image_path in all_image_path]
return all_image_path, all_image_label
def load_and_preprocess_image(img_path):
# read pictures
img_raw = tf.io.read_file(img_path)
# decode pictures
img_tensor = tf.image.decode_jpeg(img_raw, channels=channels)
# resize
img_tensor = tf.image.resize(img_tensor, [image_height, image_width])
img_tensor = tf.cast(img_tensor, tf.float32)
# normalization
img = img_tensor / 255.0
return img
load_and_preprocess_image()函数:
get_images_and_labels()函数:
get_dataset()函数:
generate_datasets()函数: