利用TensorFlow实现VGG16

本文介绍如何利用TensorFlow实现VGG16网络结构。内容涵盖网络参数定义、层类型定义、网络结构搭建、训练过程以及主函数。在训练过程中,采用TFRecord作为数据输入,并对epochs进行了计算,确保数据完整遍历一次。
摘要由CSDN通过智能技术生成

上一篇文章,实现了网络的输入,这次继续完成网络的训练,网络采用VGG16的结构。其中为了方便,keep_prob无论是训练还是测试,都这成了1,大家应该根据需要feed进不同的值。网络的输入TFRecord.createBatch(),为上一篇文章中产生数据的方法。

1.定义网络参数
import tensorflow as tf
import numpy as np
import TFRecord

#定义网络参数
learning_rate = 0.001
display_step = 5
epochs = 10
keep_prob = 0.5

2.定义各种类型的层
#定义卷积操作
def conv_op(input_op, name, kh, kw, n_out, dh, dw):
    input_op = tf.convert_to_tensor(input_op)
    n_in = input_op.get_shape()[-1].value
    with tf.name_scope(name) as scope:
        kernel = tf.get_variable(scope+"w",
                                shape = [kh, kw, n_in, n_out],
                                dtype = tf.float32,
                                initializer = tf.contrib.layers.xavier_initializer_conv2d())
        conv = tf.nn.conv2d(input_op, kernel, (1, dh, dw, 1), padding = 'SAME')
        bias_init_val = tf.constant(0.0, shape = [n_out], dtype = tf.float32)
        biases = tf.Variable(bias_init_val, trainable = True, name = 'b')
        z = tf.nn.bias_add(conv, biases)
        activation = tf.nn.relu(z, name = scope)
        return activation

#定义全连接操作
def fc_op(input_op, name, n_out):
    n_in = input_op.get_shape()[-1].value
    with tf.name_scope(name) as scope:
        kernel = tf.get_variable(scope&
  • 11
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 21
    评论
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值