My code works well with VGG19 loaded from a .mat file and is used in the following function like this (I use 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, 1], padding='SAME') + b
# add a ReLU function and return
return tf.nn.relu(conv2d)
In my project, I would like to use as input of VGG19 larger images (>2000px). For this I found that I could remove the last two layers so that VGG19 works can work on them. The constraint is that VGG19 must be loaded from KERAS module related to tensorflow. I adapted the previous code like this :
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, n_layer, layer_name,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, strides=[1, 1, 1, 1], padding='SAME') + b
# add a ReLU function and return
return tf.nn.relu(conv2d)
When testing my adapted code in my project, I got the following error even if I added "tf.enable_eager_execution()" on the top of the code :
NotImplementedError: numpy() is only available when eager execution is enabled
I'm wordering if the adaptation of the function is good ? and also, how can I fix the numpy issue ? Note: I cannot upgrade tensorflow to 2.x since the hole project is coded under tensorflow 1.x Thank you for your help.