问题描述
我的代码与从.mat文件加载的VGG19一起使用时效果很好,并用于以下功能(我使用tensorflow 1.14.0):
#initial method : load VGG19 via file and function conv2D_relu
VGG19 = scipy.io.loadmat('imagenet-vgg-verydeep-19.mat')
VGG19_layers = VGG19['layers'][0]
def conv2d_relu_old(prev_layer,n_layer,layer_name,VGG19_layers):
# get weights for this layer:
weights = VGG19_layers[n_layer][0][0][2][0][0]
W = tf.constant(weights)
bias = VGG19_layers[n_layer][0][0][2][0][1]
b = tf.constant(np.reshape(bias,(bias.size)))
# create a conv2d layer
conv2d = tf.nn.Conv2d(prev_layer,filter=W,strides=[1,1,1],padding='SAME') + b
# add a ReLU function and return
return tf.nn.relu(conv2d)
在我的项目中,我想将VGG19较大图像(> 2000px)用作输入。为此,我发现可以删除最后两层,以便VGG19可以在它们上工作。约束条件是必须从与张量流相关的KERAS模块中加载VGG19。我像这样修改了以前的代码:
from tensorflow.keras.applications.vgg19 import VGG19
model = VGG19(weights="imagenet",include_top=False,input_tensor=Input(shape=(1200,1600,3))) #to remove the two TOP layer in order to put larger images as inputs
VGG19_layers = model.layers
def _conv2d_relu_new(prev_layer,VGG19_layers):
# get weights for this layer:
if n_layer==0:
n_layer=n_layer+1
weights = tf.constant(VGG19_layers[n_layer].weights[0].numpy())
W = tf.constant(weights)
bias = tf.constant(VGG19_layers[n_layer].bias[:].numpy())
b = tf.constant(np.reshape(bias,bias.shape[0]))
# create a conv2d layer
conv2d = tf.nn.Conv2d(prev_layer,W,padding='SAME') + b
# add a ReLU function and return
return tf.nn.relu(conv2d)
在我的项目中测试经过修改的代码时,即使在代码顶部添加了“ tf.enable_eager_execution()”,我仍然遇到以下错误:
NotImplementedError: numpy() is only available when eager execution is enabled
如果功能的适应性好,我在措辞吗?而且,我该如何解决numpy问题? 注意:我无法将tensorflow升级到2.x,因为Hole项目被编码在tensorflow 1.x下 谢谢您的帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)