具体原理可以参考: https://blog.csdn.net/qq_37982109/article/details/105193638
1·读取VGG16的参数类
import os
import math
import numpy as np
import tensorflow as tf
from PIL import Image
import time
VGG_MEAN = [103.939, 116.779, 123.68]
class VGGNet:
"""Builds VGG-16 net structure,
load parameters from pre-train models.
"""
def __init__(self, data_dict):
self.data_dict = data_dict
# 处理卷积层
def get_conv_filter(self, name):
return tf.constant(self.data_dict[name][0], name='conv')
# 获取权重
def get_fc_weight(self, name):
return tf.constant(self.data_dict[name][0], name='fc')
# 获取偏置
def get_bias(self, name):
return tf.constant(self.data_dict[name][1], name='bias')
# 创建卷积层
def conv_layer(self, x, name):
"""Builds convolution layer."""
with tf.name_scope(name):
conv_w = self.get_conv_filter(name)
conv_b = self.get_bias(name)
h = tf.nn.conv2d(x, conv_w, [1, 1, 1, 1], padding='SAME')
h = tf.nn.bias_add(h, conv_b)
h = tf.nn.relu(h)
return h
# pooling层
def pooling_layer(self, x, name):
"""Builds pooling layer."""
return tf.nn.max_pool(x,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME',
name=name)
# 全连接层
def fc_layer(self, x, name, activation=tf.nn.relu):
"""Builds fully-connected layer."""
with tf.name_scope(