python训练模型测试模型有哪些_训练后测试一个tensorflow cnn模型

本文档展示了如何在TensorFlow中构建一个卷积神经网络(CNN)模型,并提供了在测试模式下运行已训练模型的代码示例。模型包括卷积、池化、全连接和dropout层,最终用于分类任务。
摘要由CSDN通过智能技术生成

我创建了一个卷积神经网络的模型,我实现了训练,现在我必须创建一个函数来在测试模式下运行模型,但是我不知道我该怎么做。在

所有的数据集,都没有按照测试的模式进行数据集测试。在

我可以用与训练数据集相同的方式加载测试数据集,但这样我就不知道如何在已经训练好的模型上进行测试。在

这是模型函数import tensorflow as tf

def cnn_model_fn(X, MODE, log=False):

# INPUT LAYER

with tf.name_scope('input_layer') as scope:

input_layer = tf.reshape(X, [-1, 1000, 48, 1])

# CONVOLUTIONAL LAYER #1

with tf.name_scope('Conv1') as scope:

conv1 = tf.layers.conv2d(

inputs=input_layer,

filters=4,

kernel_size=[10, 10],

strides=(2, 2),

padding="valid",

)

if log==True:

print('[LOG:conv1]: ' + str(conv1.shape))

# apply the relu function

conv1_relu = tf.nn.relu(conv1)

if log==True:

print('[LOG:conv1_relu]: ' + str(conv1_relu.shape))

# POOLING LAYER #1

with tf.name_scope('Pool1'):

pool1 = tf.layers.max_pooling2d(

inputs=conv1_relu,

pool_size=[2, 2],

strides=2

)

if log==True:

print('[LOG:pool1]: ' + str(pool1.shape))

# CONVOLUTIONAL LAYER #2

with tf.name_scope('Conv2'):

conv2 = tf.layers.conv2d(

inputs=pool1,

filters=64,

kernel_size=[5, 5],

padding="same",

)

if log==True:

print('[LOG:conv2]: ' + str(conv2.shape))

# apply the relu function

conv2_relu = tf.nn.relu(conv2)

if log==True:

print('[LOG:conv2_relu]: ' + str(conv2_relu.shape))

# POOLING LAYER #2

with tf.name_scope('Pool2'):

pool2 = tf.layers.max_pooling2d(

inputs=conv2_relu,

pool_size=[2, 2],

strides=2

)

if log==True:

print('[LOG:pool2]: ' + str(pool2.shape))

# create a variable with the pool2 size because I need it to calculate the pool2_flat size

x = tf.TensorShape.as_list(pool2.shape)

# REDENSIFY POOL2 TO REDUCE COMPUTATIONAL LOAD

with tf.name_scope('Reshape'):

pool2_flat = tf.reshape(pool2, [-1, x[1] * x[2] * x[3]])

if log==True:

print('[LOG:pool2_flat]: ' + str(pool2_flat.shape))

# DENSE LAYER

with tf.name_scope('Dense_layer'):

dense = tf.layers.dense(

inputs=pool2_flat,

units=1024,

)

if log==True:

print('[LOG:dense]: ' + str(dense.shape))

# apply the relu function

dense_relu = tf.nn.relu(dense)

if log==True:

print('[LOG:dense_relu]: ' + str(dense_relu.shape))

# add the dropout function

with tf.name_scope('Dropout'):

dropout = tf.layers.dropout(

inputs=dense_relu,

rate=0.4,

training=MODE == tf.estimator.ModeKeys.TRAIN

)

if log==True:

print('[LOG:dropout]: ' + str(dropout.shape))

# LOGIT LAYER

with tf.name_scope('Logit_layer'):

logits = tf.layers.dense(

inputs=dropout,

units=2

)

if log==True:

print('[LOG:logits]: ' + str(logits.shape))

return logits

这是主程序

^{pr2}$

非常感谢你的帮助和你的时间。在

编辑:我解决了这个问题saver.restore(sess, save_path)

print("Initialization Complete")

len_X_test, X_test, Y_test = get_images(

files_path=dataset_path,

img_size_h=img_size_h,

img_size_w=img_size_w,

mode='TEST',

randomize=True

)

train_feed = {x: X_test, y: Y_test}

print("Testing Accuracy:"+str(sess.run(accuracy, feed_dict=train_feed)))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值