我将图像输入到预先训练的CNN(conv1)的第一层,计算2D-Convolution,然后是RELU。我想看看这(第一)层的输出。我正在使用的代码如下:
from numpy import *
import os
#from pylab import *
import numpy as np
#import matplotlib.pyplot as plt
#import matplotlib.cbook as cbook
import time
from scipy.misc import imread
from scipy.misc import imresize
import matplotlib.image as mpimg
from scipy.ndimage import filters
import urllib
from numpy import random
import tensorflow as tf
from caffe_classes import class_names
train_x = zeros((1, 227,227,3)).astype(float32)
train_y = zeros((1, 1000))
xdim = train_x.shape[1:]
ydim = train_y.shape[1]
#Read Image, and change to BGR
im1 = (imread("dog2.png")[:,:,:3]).astype(float32)
im1 = im1 - mean(im1)
im1[:, :, 0], im1[:, :, 2] = im1[:, :, 2], im1[:, :, 0]
net_data = load(open("bvlc_alexnet.npy", "rb"), encoding="latin1").item()
def conv(input, kernel, biases, k_h, k_w, c_o, s_h, s_w, padding="VALID", group=1):
c_i = input.get_shape()[-1]
assert c_i%group==0
assert c_o%group==0
convolve = lambda i, k: tf.nn.conv2d(i, k, [1, s_h, s_w, 1], padding=padding)
if group==1:
conv = convolve(input, kernel)
else:
input_groups = tf.split(input, group, 3) #tf.split(3, group, input)
kernel_groups = tf.split(kernel, group, 3) #tf.split(3, group, kernel)
output_groups = [convolve(i, k) for i,k in zip(input_groups, kernel_groups)]
conv = tf.concat(output_groups, 3) #tf.concat(3, output_groups)
return tf.reshape(tf.nn.bias_add(conv, biases), [-1]+conv.get_shape().as_list()[1:])
x = tf.placeholder(tf.float32, (None,) + xdim)
#conv1
#conv(11, 11, 96, 4, 4, padding='VALID', name='conv1')
k_h = 11; k_w = 11; c_o = 96; s_h = 4; s_w = 4
conv1W = tf.Variable(net_data["conv1"][0])
conv1b = tf.Variable(net_data["conv1"][1])
conv1_in = conv(x, conv1W, conv1b, k_h, k_w, c_o, s_h, s_w, padding="SAME", group=1)
conv1 = tf.nn.relu(conv1_in)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
output = sess.run(conv1, feed_dict = {x:[im1]})
我想打印存储在conv1中的值,所以我写道:
test = tf.Print(conv1, [conv1])
sess.run(test)
但是,运行时收到以下错误消息:
W tensorflow/core/framework/op_kernel.cc:1148] Invalid argument: Shape [-1,227,227,3] has negative dimensions
E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1,227,227,3] has negative dimensions
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[?,227,227,3], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
使用conv1.eval()也会给出相同的错误消息。我正在使用Python 3.5.2。 任何帮助表示赞赏!
答案 0 :(得分:1)
当您运行会话并请求输出时,您必须提供feed_dict以及该输出所依赖的所有占位符。这需要在每次会话运行时完成,因为单独的会话运行可以使用不同的输入(例如,这是使用相同图表执行重复推理的方式)。
在这种情况下,您在张量function include_css(){
wp_enqueue_style('main_css',get_stylesheet_uri());
}
add_action('wp_enqueue_scripts','include_css');
上运行会话,这取决于test
,而conv1
依赖于占位符x
。
更改为sess.run(test, feed_dict = {x:[im1]})
可以解决您的问题。
但是,如果你想要的只是查看张量conv1
的值,这正是调用sess.run(conv1)
的返回值,所以你可能不需要tf.Print
所有