此函数采用预先训练好的imagenet-vgg-verydeep-19.mat模型,并使用其权重和输入返回图( model )。
def load_vgg_model(path):
...
...
...
# Constructs the graph model.
graph = {}
graph['input'] = tf.Variable(np.zeros((1, 400, 300, 3)), dtype = 'float32')
graph['conv1_1'] = _conv2d_relu(graph['input'], 0, 'conv1_1') # returns tf.nn.relu
graph['conv1_2'] = _conv2d_relu(graph['conv1_1'], 2, 'conv1_2')
graph['avgpool1'] = _avgpool(graph['conv1_2']) # returns tf.nn.avg_pool
graph['conv2_1'] = _conv2d_relu(graph['avgpool1'], 5, 'conv2_1')
....
....
graph['conv5_4'] = _conv2d_relu(graph['conv5_3'], 34, 'conv5_4')
graph['avgpool5'] = _avgpool(graph['conv5_4'])
return graph下面是TensorFlow 1中相同的实现
model = load_vgg_model("imagenet-vgg-verydeep-19.mat")
sess = tf.compat.v1.InteractiveSession()
content_image = imread("images/louvre_small.jpg")
sess.run(model['input'].assign(content_image))
out = model['conv4_2']
a_C = sess.run(out)
a_G = out但是我想知道TensorFlow 2.x中的实现
发布于 2021-07-27 11:23:21
正如在this doc中提到的,Tensorflow 2具有默认的紧急执行。代码通过构建中间图来执行。
在Tensorflow 2中,会话的创建不是required.You,可以使用Autograph函数来代替会话来创建图形。
文件上说,
TensorFlow 1.x requires users to manually stitch together an abstract syntax tree (the graph) by making tf.* API calls. It then requires users to manually compile the abstract syntax tree by passing a set of output tensors and input tensors to a session.run call. TensorFlow 2.0 executes eagerly (like Python normally does) and in 2.0, graphs and sessions should feel like implementation details.
One notable byproduct of eager execution is that tf.control_dependencies is no longer required, as all lines of code execute in order (within a tf.function, code with side effects executes in the order written).https://stackoverflow.com/questions/67962056
复制相似问题