“历史遗留”的学习记录
Cifar10数据集—tensorflow2—VGG16网络—tensorboard可视化查看
(PS:时隔久远,但还是能运行)
- 子程序包括:数据集加载处理、VGG16网络搭建、变学习率训练
- 对数据集进行VGG16模型训练
- 保存运行日志在文件夹中
- 可设置数据增强
import tensorflow as tf
import numpy as np
import pickle as p
from tensorflow.keras import datasets, layers, Sequential, metrics
import datetime
from matplotlib import pyplot as plt
import io
import os
from tensorflow.keras import models, optimizers, regularizers
from tensorflow.keras.callbacks import LearningRateScheduler
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense
from tensorflow import keras
# os.environ['CUDA_VISIBLE_DEVICES'] = '0'
weight_decay = 5e-4
batch_size = 128
learning_rate = 1e-2
dropout_rate = 0.5
epoch_num = 10
def load_CIFAR_batch(filename):
""" load single batch of cifar """
with open(filename, 'rb')as f:
datadict = p.load(f, encoding='iso-8859-1')
X = datadict['data']
Y = datadict['labels']
X = X.reshape(10000, 3, 32, 32)
Y = np.array(Y)
return X, Y
def load_CIFAR(Foldername):
train_data = np.zeros([50000, 32, 32, 3], dtype=np.float32)
train_label = np.zeros([50000, 10], dtype=np.float32)
test_data = np.zeros([10000, 32, 32, 3], dtype=np.float32)
test_label = np.zeros([10000, 10], dtype=np.float32)
for sample in range(5):
X, Y = load_CIFAR_batch(Foldername + "/data_batch_" + str(sample + 1))
for i in range(3):
train_data[10000 * sample:10000 * (sample + 1), :, :, i] = X[:, i, :, :]
for i in range