700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > VGG网络模型

VGG网络模型

时间:2024-05-04 21:52:55

相关推荐

VGG网络模型

import tensorflow as tf#卷积函数def conv2d(x, W, b, strides=1):x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')x = tf.nn.bias_add(x, b)return tf.nn.relu(x)#池化函数def maxpool2d(x, k=2):return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],padding='SAME')#权重参数weights = {# 3x3 conv, 3 input, 24 outputs'wc1': tf.Variable(tf.truncated_normal(shape=[3, 3, 3, 64], stddev=0.1, dtype=tf.float32),name='weights', dtype=tf.float32),'wc2': tf.Variable(tf.truncated_normal(shape=[3, 3, 3, 64], stddev=0.1, dtype=tf.float32),name='weights', dtype=tf.float32),'wc3': tf.Variable(tf.truncated_normal(shape=[3, 3, 64, 128], stddev=0.1, dtype=tf.float32),name='weights', dtype=tf.float32),'wc4': tf.Variable(tf.truncated_normal(shape=[3, 3, 128, 128], stddev=0.1, dtype=tf.float32),name='weights', dtype=tf.float32),'wc5': tf.Variable(tf.truncated_normal(shape=[3, 3, 128, 256], stddev=0.1, dtype=tf.float32),name='weights', dtype=tf.float32),'wc6': tf.Variable(tf.truncated_normal(shape=[3, 3, 256, 256], stddev=0.1, dtype=tf.float32),name='weights', dtype=tf.float32),'wc7': tf.Variable(tf.truncated_normal(shape=[3, 3, 256, 256], stddev=0.1, dtype=tf.float32),name='weights', dtype=tf.float32),'wc8': tf.Variable(tf.truncated_normal(shape=[3, 3, 256, 512], stddev=0.1, dtype=tf.float32),name='weights', dtype=tf.float32),'wc9': tf.Variable(tf.truncated_normal(shape=[3, 3, 512, 512], stddev=0.1, dtype=tf.float32),name='weights', dtype=tf.float32),'wc10':tf.Variable(tf.truncated_normal(shape=[3, 3, 512, 512], stddev=0.1, dtype=tf.float32),name='weights', dtype=tf.float32),'wc11': tf.Variable(tf.truncated_normal(shape=[3, 3, 512, 512], stddev=0.1, dtype=tf.float32),name='weights', dtype=tf.float32),'wc12': tf.Variable(tf.truncated_normal(shape=[3, 3, 512, 512], stddev=0.1, dtype=tf.float32),name='weights', dtype=tf.float32),'wc13': tf.Variable(tf.truncated_normal(shape=[3, 3, 512, 512], stddev=0.1, dtype=tf.float32),name='weights', dtype=tf.float32),# fully connected, 32*32*96 inputs, 1024 outputs'wd1': tf.Variable(tf.truncated_normal(shape=[4 * 4 * 512, 1024], stddev=0.1, dtype=tf.float32),name='weights', dtype=tf.float32),'wd2': tf.Variable(tf.truncated_normal(shape=[1024, 1024], stddev=0.1, dtype=tf.float32),name='weights', dtype=tf.float32),# 1024 inputs, 10 outputs (class prediction)'out': tf.Variable(tf.truncated_normal(shape=[1024, 10], stddev=0.1, dtype=tf.float32),name='weights', dtype=tf.float32)}#偏置参数biases = {'bc1': tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[64]),name='biases', dtype=tf.float32),'bc2': tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[64]),name='biases', dtype=tf.float32),'bc3': tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[128]),name='biases', dtype=tf.float32),'bc4': tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[128]),name='biases', dtype=tf.float32),'bc5': tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[256]),name='biases', dtype=tf.float32),'bc6': tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[256]),name='biases', dtype=tf.float32),'bc7': tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[256]),name='biases', dtype=tf.float32),'bc8': tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[512]),name='biases', dtype=tf.float32),'bc9': tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[512]),name='biases', dtype=tf.float32),'bc10': tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[512]),name='biases', dtype=tf.float32),'bc11': tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[512]),name='biases', dtype=tf.float32),'bc12': tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[512]),name='biases', dtype=tf.float32),'bc13': tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[512]),name='biases', dtype=tf.float32),'bd1': tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[1024]),name='biases', dtype=tf.float32),'bd2': tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[1024]),name='biases', dtype=tf.float32),'out': tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[10]),name='biases', dtype=tf.float32)}#VGG模型def inference(images, weights,biases, dropout):with tf.name_scope('conv1'):conv1 = conv2d(images, weights['wc1'], biases['bc1'])with tf.name_scope('conv2'):conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])with tf.name_scope('pool1'):pool1 = maxpool2d(conv2, k=2)print('pool1尺寸',pool1.shape)with tf.name_scope('conv3'):conv3 = conv2d(pool1, weights['wc3'], biases['bc3'])with tf.name_scope('conv4'):conv4 = conv2d(conv3, weights['wc4'], biases['bc4'])with tf.name_scope('pool2'):pool2 = maxpool2d(conv4, k=2)print('pool2尺寸',pool2.shape)with tf.name_scope('conv5'):conv5 = conv2d(pool2, weights['wc5'], biases['bc5'])with tf.name_scope('conv6'):conv6 = conv2d(conv5, weights['wc6'], biases['bc6'])with tf.name_scope('conv7'):conv7 = conv2d(conv6, weights['wc7'], biases['bc7'])with tf.name_scope('pool3'):pool3 = maxpool2d(conv7, k=2)print('pool3尺寸', pool3.shape)with tf.name_scope('conv8'):conv8 = conv2d(pool3, weights['wc8'], biases['bc8'])with tf.name_scope('conv9'):conv9 = conv2d(conv8, weights['wc9'], biases['bc9'])with tf.name_scope('conv10'):conv10 = conv2d(conv9, weights['wc10'], biases['bc10'])with tf.name_scope('pool4'):pool4 = maxpool2d(conv10, k=2)print('pool4尺寸', pool4.shape)with tf.name_scope('conv11'):conv11 = conv2d(pool4, weights['wc11'], biases['bc11'])with tf.name_scope('conv12'):conv12 = conv2d(conv11, weights['wc12'], biases['bc12'])with tf.name_scope('conv13'):conv13 = conv2d(conv12, weights['wc13'], biases['bc13'])with tf.name_scope('pool5'):pool5 = maxpool2d(conv13, k=2)print('pool5尺寸', pool5.shape)#用1*1的卷积代替全连接层with tf.name_scope('fc1'):fc1 = tf.reshape(pool5, [-1, weights['wd1'].get_shape().as_list()[0]])fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])fc1 = tf.nn.relu(fc1)with tf.name_scope('dropout1'):fc1 = tf.nn.dropout(fc1, dropout)with tf.name_scope('fc2'):fc2 = tf.add(tf.matmul(fc1, weights['wd2']), biases['bd2'])fc2 = tf.nn.relu(fc2)with tf.name_scope('dropout2'):fc2 = tf.nn.dropout(fc2, dropout)with tf.name_scope('fc3'):out = tf.add(tf.matmul(fc2, weights['out']), biases['out'])return out#softmax损失函数def losses(logits, labels):with tf.name_scope('loss'):cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels,name='xentropy_per_example')loss = tf.reduce_mean(cross_entropy, name='loss')tf.summary.scalar('loss' + '/loss', loss)return loss#Adam梯度下降def trainning(loss, learning_rate):with tf.name_scope('optimizer'):optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)global_step = tf.Variable(0, name='global_step', trainable=False)train_op = optimizer.minimize(loss, global_step=global_step)return train_op#预测准确度def evaluation(logits, labels):with tf.name_scope('accuracy'):correct = tf.nn.in_top_k(logits, labels, 1)correct = tf.cast(correct, tf.float16)accuracy = tf.reduce_mean(correct)tf.summary.scalar('accuracy' + '/accuracy', accuracy)return accuracy

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。