风格迁移数据集_AI实战Tensorflow⾃定义数据集和迁移学习
(附代码下载)
⾃定义数据集
做深度学习项⽬时,我们⼀般都不⽤⽹上公开的数据集,⽽是⽤⾃⼰制作的数据集。那么,怎么⽤Tensorflow2.0来制作⾃⼰的数据集并把数据喂给神经⽹络呢?且看这篇⽂章慢慢道来。
Pokemon Datasets
这篇⽂章我们⽤的datasets是Pokemon datasets,也就是⽪卡丘电影中的⼀些⾓⾊,如下图所⽰:
数据集下载
提取码:dsxl
数据集划分
由上图可知,60%的数据集⽤来train,20%的数据集⽤来validation,同样20%⽤来test。
四个步骤
Load data:加载数据
Build model:建⽴模型
Train-Val-Test:训练和测试
Transfer Learning:迁移模型
加载数据
⾸先对数据进⾏预处理,把像素值的Numpy类型转换为Tensor类型,并归⼀化到[0~1]。把数据集的标签做one-hot编码。
def preprocess(x,y):
# x: 图⽚的路径,y:图⽚的数字编码
x = ad_file(x)
x = tf.image.decode_jpeg(x, channels=3) # RGBA
x = size(x, [244, 244])
return x, y
数据集标准处理流程
代码中load_pokemon⽤的是⾃⼰的数据集写的代码,具体可阅读pokemon.py⽂件。
# 创建训练集Datset对象
images, labels, table = load_pokemon('pokemon',mode='train')
db_train = tf.data.Dataset.from_tensor_slices((images, labels))
db_train = db_train.shuffle(1000).map(preprocess).batch(batchsz)
# 创建验证集Datset对象
images2, labels2, table = load_pokemon('pokemon',mode='val')
db_val = tf.data.Dataset.from_tensor_slices((images2, labels2))
db_val = db_val.map(preprocess).batch(batchsz)
# 创建测试集Datset对象
images3, labels3, table = load_pokemon('pokemon',mode='test')
db_test = tf.data.Dataset.from_tensor_slices((images3, labels3))
db_test = db_test.map(preprocess).batch(batchsz)
图⽚数据增强及标准化
⼀般数据集较少的话需要使⽤数据增强以增加数据集,防⽌训练⽹络过拟合。⽐如旋转⾓度、裁剪等,并归⼀化到[0~1]。把数据集的标签做one-hot编码。所⽰代码如下:
# x = tf.image.random_flip_left_right(x)
x = tf.image.random_flip_up_down(x)
x = tf.image.random_crop(x, [224,224,3])
# x: [0,255]=> -1~1
x = tf.cast(x, dtype=tf.float32) / 255.
x = normalize(x)
tensorflow入门教程y = tf.convert_to_tensor(y)
y = tf.one_hot(y, depth=5)
建⽴⽹络
神经⽹络从零开始训练,backbone⽤李沐⼤神的resnet⽹络。详细代码请查看resnet.py⽂件。部分代码如下:
class ResNet(keras.Model):
def __init__(self, num_classes, initial_filters=16, **kwargs):
super(ResNet, self).__init__(**kwargs)
self.stem = layers.Conv2D(initial_filters, 3, strides=3, padding='valid')
self.blocks = dels.Sequential([
ResnetBlock(initial_filters * 2, strides=3),
ResnetBlock(initial_filters * 2, strides=1),
# layers.Dropout(rate=0.5),
ResnetBlock(initial_filters * 4, strides=3),
ResnetBlock(initial_filters * 4, strides=1),
ResnetBlock(initial_filters * 8, strides=2),
ResnetBlock(initial_filters * 8, strides=1),
ResnetBlock(initial_filters * 16, strides=2),
ResnetBlock(initial_filters * 16, strides=1),
])
self.final_bn = layers.BatchNormalization()
self.avg_pool = layers.GlobalMaxPool2D()
self.fc = layers.Dense(num_classes)
def call(self, inputs, training=None):
# print('x:',inputs.shape)
out = self.stem(inputs,training=training)
out = lu(out)
# print('stem:',out.shape)
out = self.blocks(out, training=training)
# print('res:',out.shape)
out = self.final_bn(out, training=training)
# out = lu(out)
out = self.avg_pool(out)
# print('avg_pool:',out.shape)
out = self.fc(out)
# print('out:',out.shape)
return out
训练和测试
部分代码如下:
resnet = keras.Sequential([
layers.Conv2D(16,5,3),
layers.MaxPool2D(3,3),
layers.ReLU(),
layers.Conv2D(64,5,3),
layers.MaxPool2D(2,2),
layers.ReLU(),
layers.Flatten(),
layers.Dense(64),
layers.ReLU(),
layers.Dense(5)
])
resnet = ResNet(5)
resnet.build(input_shape=(4, 224, 224, 3))
resnet.summary()
early_stopping = EarlyStopping(
monitor='val_accuracy',
min_delta=0.001,
patience=5
)
resnetpile(optimizer=optimizers.Adam(lr=1e-3),
loss=losses.CategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
resnet.fit(db_train, validation_data=db_val, validation_freq=1, epochs=100,
callbacks=[early_stopping])
resnet.evaluate(db_test)
迁移⽹络学习
⽹络可以丛零开始训练,也可以从别的训练好的参数模型迁移过来,本次实战⽤Tensorflow预训练的vgg19模型来加载训练,从⽽加快训练过程。
迁移学习的原理如下图所⽰:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论