#%% import tensorflow as tf #%% def inference(images, batch_size, n_classes): '''Build the model Args: images: image batch, 4D tensor, tf.float32, [batch_size, width, height, channels] Returns: output tensor with the computed logits, float, [batch_size, n_classes] ''' #conv1, shape = [kernel size, kernel size, channels, kernel numbers] with tf.variable_scope('conv1') as scope: weights = tf.get_variable('weights', shape = [3,3,3, 16], dtype = tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.1,dtype=tf.float32)) biases = tf.get_variable('biases', shape=[16], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) conv = tf.nn.conv2d(images, weights, strides=[1,1,1,1], padding='SAME') pre_activation = tf.nn.bias_add(conv, biases) conv1 = tf.nn.relu(pre_activation, name= scope.name) #pool1 and norm1 with tf.variable_scope('pooling1_lrn') as scope: pool1 = tf.nn.max_pool(conv1, ksize=[1,3,3,1],strides=[1,2,2,1], padding='SAME', name='pooling1') norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001/9.0, beta=0.75,name='norm1') #conv2 with tf.variable_scope('conv2') as scope: weights = tf.get_variable('weights', shape=[3,3,16,16], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.1,dtype=tf.float32)) biases = tf.get_variable('biases', shape=[16], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) conv = tf.nn.conv2d(norm1, weights, strides=[1,1,1,1],padding='SAME') pre_activation = tf.nn.bias_add(conv, biases) conv2 = tf.nn.relu(pre_activation, name='conv2') #pool2 and norm2 with tf.variable_scope('pooling2_lrn') as scope: norm2 = tf.nn.lrn(conv2, depth_radius=4, bias=1.0, alpha=0.001/9.0, beta=0.75,name='norm2') pool2 = tf.nn.max_pool(norm2, ksize=[1,3,3,1], strides=[1,1,1,1], padding='SAME',name='pooling2') #local3 with tf.variable_scope('local3') as scope: reshape = tf.reshape(pool2, shape=[batch_size, -1]) dim = reshape.get_shape()[1].value weights = tf.get_variable('weights', shape=[dim,128], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.005,dtype=tf.float32)) biases = tf.get_variable('biases', shape=[128], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) local3 = tf.nn.relu(tf.matmul(reshape, weights) + biases, name=scope.name) #local4 with tf.variable_scope('local4') as scope: weights = tf.get_variable('weights', shape=[128,128], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.005,dtype=tf.float32)) biases = tf.get_variable('biases', shape=[128], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) local4 = tf.nn.relu(tf.matmul(local3, weights) + biases, name='local4') # softmax with tf.variable_scope('softmax_linear') as scope: weights = tf.get_variable('softmax_linear', shape=[128, n_classes], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.005,dtype=tf.float32)) biases = tf.get_variable('biases', shape=[n_classes], dtype=tf.float32, initializer=tf.constant_initializer(0.1)) softmax_linear = tf.add(tf.matmul(local4, weights), biases, name='softmax_linear') return softmax_linear #%% #def inference (input_tensor, train, regularizer): # # 声明第一层神经网络的变量并完成前向传播过程。这个过程和6.3.1小节中介绍的一致。 # # 通过使用不同的命名空间来隔离不同层的变量,这可以让每一层中的变量命名只需要考虑在当前层的作用,而不需要担心重名的问题。 # # 和标准LeNet-5模型不大一样,这里定义卷积层的输入为28*28*1的原始MNIST图片像素。 # # 因为卷积层中使用了全0填充,所以输出为28*28*32的矩阵。 # with tf.variable_scope('layer1-conv1',reuse =tf.AUTO_REUSE): # # 这里使用tf.get_variable或tf.Variable没有本质区别,因为在训练或是测试中没有在同一个程序中多次调用这个函数。 # # 如果在同一个程序中多次调用,在第一次调用之后需要将reuse参数置为True。 # conv1_weights = tf.get_variable( # "weight", [CONV1_SIZE, CONV1_SIZE, NUM_CHANNELS, CONV1_DEEP], # initializer = tf.truncated_normal_initializer(stddev=0.1) # ) # conv1_biases = tf.get_variable("bias", [CONV1_DEEP], initializer=tf.constant_initializer(0.0)) # # 使用边长为5,深度为32的过滤器,过滤器移动的步长为1,且使用全0填充 # conv1 = tf.nn.conv2d(input_tensor, conv1_weights, strides=[1, 1, 1, 1], padding='SAME') # relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases)) # # # 实现第二层池化层的前向传播过程。 # # 这里选用最大池化层,池化层过滤器的边长为2,使用全0填充且移动的步长为2。 # # 这一层的输入是上一层的输出,也就是28*28*32的矩阵。输出为14*14*32的矩阵。 # with tf.name_scope('layer2-pool'): # pool1 = tf.nn.max_pool(relu1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # # # 声明第三层卷积层的变量并实现前向传播过程。 # # 这一层的输入为14*14*32的矩阵,输出为14*14*64的矩阵。 # with tf.variable_scope('layer3-conv2',reuse =tf.AUTO_REUSE): # conv2_weights = tf.get_variable( # "weight", [CONV2_SIZE, CONV2_SIZE, CONV1_DEEP, CONV2_DEEP], # initializer = tf.truncated_normal_initializer(stddev=0.1) # ) # conv2_biases = tf.get_variable("bias", [CONV2_DEEP], initializer=tf.constant_initializer(0.0)) # # 使用边长为5,深度为64的过滤器,过滤器移动的步长为1,且使用全0填充 # conv2 = tf.nn.conv2d(pool1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME') # relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases)) # # # 实现第四层池化层的前向传播过程。 # # 这一层和第二层的结构是一样的。这一层的输入为14*14*64的矩阵,输出为7*7*64的矩阵。 # with tf.name_scope('layer4-poo2'): # pool2 = tf.nn.max_pool(relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # # # 将第四层池化层的输出转化为第五层全连接层的输入格式。 # # 第四层的输出为7*7*64的矩阵,然而第五层全连接层需要的输入格式为向量,所以在这里需要将这个7*7*64的矩阵拉直成一个向量。 # # pool2.get_shape函数可以得到�
相关知识
tensorflow识别花朵
【实战】tensorflow 花卉识别
Tensorflow五种花卉分类
花卉识别(tensorflow)
使用TensorFlow给花朵分类
基于tensorflow的花卉识别
TensorFlow学习记录(八)
基于TensorFlow Lite实现的Android花卉识别应用
深度学习入门——基于TensorFlow的鸢尾花分类实现(TensorFlow
机器学习花朵图像分类
网址: tensorflow五种花朵分类识别 https://m.huajiangbk.com/newsview456704.html
上一篇: 安卓识花app软件下载 |
下一篇: 四季有花,处处是景——莲花镇龙洞 |