import tensorflow as tf
import numpy as np
'''
实现一个AlexNet类
方法:
def build_net 建立神经网络
def make_sort 对于一个输入的图片,矩阵化并分类
def learn 从data_cannel中读取强化后的图片,进行梯度训练
成员变量:(先这几个)
:self.LR 训练速度
:self.DP Dp层的DP率
:self.n_classes Label的种类数
'''
class AlexNet():
def __init__(self,learning_rate,drop_out,n_classes):
self.LR = learning_rate
self.DP = drop_out
self.n_classes = n_classes
self.Sess = tf.compat.v1.Session()
self.Steps = 0
self.Loss_list = list()
self.bulid_net()
self.Sess.run(tf.compat.v1.global_variables_initializer())
def bulid_net(self):
'''
建立一个AlexNet网络
将图片天气分为风、雷、雨、雪、雾、晴六类,作为训练集和验证集的数据
简单解释:
包含8个带权重的层;前5层是卷积层,剩下的3层是全连接层。最后一层全连接层的输出是1000维softmax的输入,softmax会产生1000类标签的分布。
:param w:卷积核四维解释张量,[height, width, in_channels, out_channels],即卷积核高度、宽度、输入通道和输出通道
:param b:用于bias矩阵层加入value,即bias矩阵的维度
:param strides:步长,[默认1,宽步长,高步长,默认1]
'''
tf.compat.v1.disable_eager_execution()
self.imput = tf.compat.v1.placeholder(tf.float32, [None, 227, 227, 3], name="input_image")
self.label = tf.compat.v1.placeholder(tf.float32, [None, 6], name="input_label")
with tf.compat.v1.variable_scope('conv2d_1'):
filter_shape_1 = [11,11,3,48]
w1 = tf.Variable(tf.compat.v1.random_normal(filter_shape_1), name="w1")
b1 = tf.Variable(tf.compat.v1.random_normal([48]), name="b1")
conv1 = tf.nn.conv2d(self.imput, w1, strides=[1,4,4,1], padding='SAME')
bias1 = tf.nn.bias_add(conv1, b1)
relu1 = tf.nn.relu(bias1)
pool1 = tf.nn.max_pool(relu1, ksize=[1, 3, 3, 1],strides=[1, 2, 2, 1],padding='SAME')
norm1 = tf.nn.lrn(pool1, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75)
norm1 = tf.nn.dropout(norm1, self.DP)
with tf.compat.v1.variable_scope('conv2d_2'):
filter_shape_2 = [5,5,48,128]
w2 = tf.Variable(tf.compat.v1.random_normal(filter_shape_2), name="w2")
b2 = tf.Variable(tf.compat.v1.random_normal([128]), name="b2")
conv2 = tf.nn.conv2d(norm1, w2, strides=[1,1,1,1], padding='SAME')
bias2 = tf.nn.bias_add(conv2, b2)
relu2 = tf.nn.relu(bias2)
pool2 = tf.nn.max_pool(relu2, ksize=[1, 3, 3, 1],strides=[1, 2, 2, 1],padding='SAME')
norm2 = tf.nn.lrn(pool2, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75)
norm2 = tf.nn.dropout(norm2,self.DP)
with tf.compat.v1.variable_scope('conv2d_3'):
filter_shape_3 = [3,3,128,192]
w3 = tf.Variable(tf.compat.v1.random_normal(filter_shape_3), name="w3")
b3 = tf.Variable(tf.compat.v1.random_normal([192]), name="b3")
conv3 = tf.nn.conv2d(norm2, w3, strides=[1,1,1,1], padding='SAME')
bias3 = tf.nn.bias_add(conv3, b3)
relu3 = tf.nn.relu(bias3)
with tf.compat.v1.variable_scope('conv2d_4'):
filter_shape_4 = [3,3,192,192]
w4 = tf.Variable(tf.compat.v1.random_normal(filter_shape_4), name="w4")
b4 = tf.Variable(tf.compat.v1.random_normal([192]), name="b4")
conv4 = tf.nn.conv2d(relu3, w4, strides=[1,1,1,1], padding='SAME')
bias4 = tf.nn.bias_add(conv4, b4)
relu4 = tf.nn.relu(bias4)
with tf.compat.v1.variable_scope('conv2d_5'):
filter_shape_5 = [3,3,192,128]
w5 = tf.Variable(tf.compat.v1.random_normal(filter_shape_5), name="w5")
b5 = tf.Variable(tf.compat.v1.random_normal([128]), name="b5")
conv5 = tf.nn.conv2d(relu4, w5, strides=[1,1,1,1], padding='SAME')
bias5 = tf.nn.bias_add(conv5, b5)
relu5 = tf.nn.relu(bias5)
pool5 = tf.nn.max_pool(relu5, ksize=[1, 3, 3, 1],strides=[1, 2, 2, 1],padding='SAME')
with tf.compat.v1.variable_scope('dense_1'):
wd1 = tf.Variable(tf.compat.v1.random_normal([4*4*128, 1024]))
bd1 = tf.Variable(tf.compat.v1.random_normal([1024]))
Vector1 = tf.reshape(pool5, [-1, wd1.get_shape().as_list()[0]])
MatMul1 = tf.matmul(Vector1, wd1) + bd1
Dense1 = tf.nn.relu(MatMul1)
with tf.compat.v1.variable_scope('output'):
wd2 = tf.Variable(tf.compat.v1.random_normal([1024, 1024]))
bd2 = tf.Variable(tf.compat.v1.random_normal([1024]))
MatMul2 = tf.matmul(Dense1, wd2) + bd2
relu = tf.nn.relu(MatMul2)
w_out = tf.Variable(tf.compat.v1.random_normal([1024, self.n_classes]))
b_out = tf.Variable(tf.compat.v1.random_normal([self.n_classes]))
self.output = tf.matmul(relu, w_out) + b_out
with tf.compat.v1.variable_scope('loss'):
self.Loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(self.output, self.label))
with tf.compat.v1.variable_scope('Optimazer'):
self.Optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=self.LR).minimize(self.Loss)
def learn(self, images, labels):
'''
需要实现的功能:
接受图像流数据进行训练,_train_op待定,这个参数决定是否走完全程神经网络
除此之外,保存loss值
'''
self.Steps += 1
print(images.shape)
print(labels.shape)
_, cost = self.Sess.run(
[self.Optimizer, self.Loss],
feed_dict={
self.imput: images,
self.label: labels
}
)
self.Loss_list.append(cost)
def make_sort(self, image):
'''
:param image: 输入的图片
:return: 一个分类标号
'''
'''label_values = self.sess.run(label,feed_dict={self.imput: image})
label = np.argmax(label_values)
return label'''