while creating VGGSEGNET giving following error - python

while implementing VGGSEGNET giving me following error
Below is VGGSEGNET code
Function call is like- model=VGGSegnet(n_classes=1, input_height=224, input_width=224)
On following both versions getting same error
tf version=2.4.1 & 2.2.0
keras version=2.4.3 & 2.4.3
python=3.7 and python 3.8
def VGGSegnet(n_classes, input_height, input_width, vgg_level=3, pretrained_weights = None):
img_input = Input(shape=(input_height, input_width,3 ))
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1', data_format='channels_last')(img_input)
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2', data_format='channels_last')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool1', data_format='channels_last')(x)
f1 = x
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1', data_format='channels_last')(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2', data_format='channels_last')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool', data_format='channels_last')(x)
f2 = x
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1', data_format='channels_last')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2', data_format='channels_last')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3', data_format='channels_last')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool1', data_format='channels_last')(x)
f3 = x
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1', data_format='channels_last')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2', data_format='channels_last')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3', data_format='channels_last')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool1', data_format='channels_last')(x)
f4 = x
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1', data_format='channels_last')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2', data_format='channels_last')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3', data_format='channels_last')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool1', data_format='channels_last')(x)
f5 = x
x = Flatten(name='flatten')(x)
x = Dense(4096, activation='relu', name='fc1')(x)
x = Dense(4096, activation='relu', name='fc2')(x)
x = Dense(1000, activation='softmax', name='predictions')(x)
vgg = Model(img_input, x)
vgg.load_weights("image-segmentation-keras-py3-master/Models/vgg16_weights_th_dim_ordering_th_kernels.hdf5")
levels = [f1, f2, f3, f4, f5]
o = levels[vgg_level]
o = ZeroPadding2D((1,1),data_format='channels_last')(o)
o = Conv2D(512,(3,3),padding='valid',data_format='channels_last')(o)
o = BatchNormalization()(o)
o = UpSampling2D((2,2),data_format='channels_last')(o)
o = ZeroPadding2D((1,1),data_format='channels_last')(o)
o = Conv2D(256,(3,3),padding='valid',data_format='channels_last')(o)
o = BatchNormalization()(o)
o = UpSampling2D((2,2),data_format='channels_last')(o)
o = ZeroPadding2D((1,1),data_format='channels_last')(o)
o = Conv2D(128,(3,3),padding='valid',data_format='channels_last')(o)
o = BatchNormalization()(o)
o = UpSampling2D((2, 2), data_format='channels_last')(o)
o = ZeroPadding2D((1, 1), data_format='channels_last')(o)
o = Conv2D(64, (3, 3), padding='valid', data_format='channels_last')(o)
o = BatchNormalization()(o)
o = UpSampling2D((2, 2), data_format='channels_last')(o)
o = ZeroPadding2D((1, 1), data_format='channels_last')(o)
o = Conv2D(32, (3, 3), padding='valid', data_format='channels_last')(o)
o = BatchNormalization()(o)
#o = UpSampling2D((2, 2), data_format='channels_last')(o)
#o = ZeroPadding2D((1, 1), data_format='channels_last')(o)
o = Conv2D(n_classes,(3,3),padding='same',data_format='channels_last')(o)
#o = BatchNormalization()(o)
o_shape = Model(img_input,o).output_shape
#outputHeight = o_shape[2]
#outputWidth = o_shape[3]
outputHeight = o_shape[2]
outputWidth = o_shape[1]
#o = (Reshape((outputHeight*outputWidth, -1)))(o)
#o = (Permute((1,2)))(o)
o = (Activation('sigmoid'))(o)
model = Model(img_input,o)
model.outputWidth = outputWidth
model.outputHeight = outputHeight
if(pretrained_weights):
model.load_weights(pretrained_weights)
return model
error block
Traceback (most recent call last):
File "/scratch/pkasar.dbatu/training/VGGSEGNET_224_224_working_on_20_03_21_on_augmented_images_of_size_256_by_256.py", line 248, in <module>
model=VGGSegnet(n_classes=1, input_height=224, input_width=224)
File "/scratch/pkasar.dbatu/training/VGGSEGNET_224_224_working_on_20_03_21_on_augmented_images_of_size_256_by_256.py", line 56, in VGGSegnet
vgg.load_weights("image-segmentation-keras-py3-master/Models/vgg16_weights_th_dim_ordering_th_kernels.hdf5")
File "/home/pkasar.dbatu/.conda/envs/dl_new/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 2234, in load_weights
hdf5_format.load_weights_from_hdf5_group(f, self.layers)
File "/home/pkasar.dbatu/.conda/envs/dl_new/lib/python3.8/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 710, in load_weights_from_hdf5_group
K.batch_set_value(weight_value_tuples)
File "/home/pkasar.dbatu/.conda/envs/dl_new/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py", line 201, in wrapper
return target(*args, **kwargs)
File "/home/pkasar.dbatu/.conda/envs/dl_new/lib/python3.8/site-packages/tensorflow/python/keras/backend.py", line 3706, in batch_set_value
x.assign(np.asarray(value, dtype=dtype(x)))
File "/home/pkasar.dbatu/.conda/envs/dl_new/lib/python3.8/site-packages/tensorflow/python/distribute/values.py", line 781, in assign
return values_util.on_write_assign(
File "/home/pkasar.dbatu/.conda/envs/dl_new/lib/python3.8/site-packages/tensorflow/python/distribute/values_util.py", line 140, in on_write_assign
return var._update( # pylint: disable=protected-access
File "/home/pkasar.dbatu/.conda/envs/dl_new/lib/python3.8/site-packages/tensorflow/python/distribute/values.py", line 940, in _update
return self._update_cross_replica(update_fn, value, **kwargs)
File "/home/pkasar.dbatu/.conda/envs/dl_new/lib/python3.8/site-packages/tensorflow/python/distribute/values.py", line 893, in _update_cross_replica
return self.distribute_strategy.extended.update(
File "/home/pkasar.dbatu/.conda/envs/dl_new/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py", line 2494, in update
return self._update(var, fn, args, kwargs, group)
File "/home/pkasar.dbatu/.conda/envs/dl_new/lib/python3.8/site-packages/tensorflow/python/distribute/mirrored_strategy.py", line 710, in _update
fn(v, *distribute_utils.select_replica_mirrored(i, args),
File "/home/pkasar.dbatu/.conda/envs/dl_new/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py", line 572, in wrapper
return func(*args, **kwargs)
File "/home/pkasar.dbatu/.conda/envs/dl_new/lib/python3.8/site-packages/tensorflow/python/distribute/values_util.py", line 139, in <lambda>
assign_fn = lambda var, *a, **kw: var.assign(*a, **kw)
File "/home/pkasar.dbatu/.conda/envs/dl_new/lib/python3.8/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 888, in assign
raise ValueError(
ValueError: Cannot assign to variable block1_conv1/kernel:0 due to variable shape (3, 3, 3, 64) and value shape (3, 3, 64, 3) are incompatible
I am doing segmentation task using iou as performance metric.
Help me out.
Thank you in advance

Related

Predict Image loose saturation compare to the original after training

I am trying super-resolution of RGB images. I have an input image and output image I run the model and when I predict the output the image loses saturation compared to the original image. The image size is 512x512 pixels. I don't know if this happening because of the conv2dtranspose layer.
ImagedataGenerator
tf.config.run_functions_eagerly(True)
import os
import numpy as np
import cv2
def generator(idir,odir,batch_size,shuffle ):
i_list=os.listdir(idir)
o_list=os.listdir(odir)
batch_index=0
batch_size = batch_size
sample_count=len(i_list)
while True:
input_image_batch=[]
output_image_batch=[]
i_list=os.listdir(idir)
o_list=os.listdir(odir)
batch_size = batch_size
sample_count=len(i_list)
for i in range(batch_index * batch_size, (batch_index + 1) * batch_size ):
#iterate for a batch
j=i % sample_count # cycle j value over range of available images
k=j % batch_size # cycle k value over batch size
if shuffle == True: # if shuffle select a random integer between 0 and sample_count-1 to pick as the image=label pair
m=np.random.randint(low=0, high=sample_count-1, size=None, dtype=int)
else:
m=j
path_to_in_img=os.path.join(idir,i_list[m])
path_to_out_img=os.path.join(odir,i_list[m])
input_image=cv2.imread(path_to_in_img)
input_image=cv2.resize(input_image,(512,512))
input_image = cv2.cvtColor(input_image,cv2.COLOR_BGR2RGB)#create the target image from the input image
output_image=cv2.imread(path_to_out_img)
output_image=cv2.resize(output_image,(512,512))
output_image = cv2.cvtColor(output_image,cv2.COLOR_BGR2RGB)
input_image_batch.append(input_image)
output_image_batch.append(output_image)
input_image_array=np.array(input_image_batch)
input_image_array = input_image_array / 255.0
output_image_array=np.array(output_image_batch)
output_image_array = output_image_array / 255.0
batch_index= batch_index + 1
yield (input_image_array, output_image_array)
if batch_index * batch_size > sample_count:
batch_index=0
Model
from keras.models import Model
from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate, Conv2DTranspose, BatchNormalization, Dropout, Lambda
from keras.metrics import MeanIoU
kernel_initializer = 'he_uniform' # also try 'he_normal' but model not converging...
################################################################
def simple_unet_model(IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS):
#Build the model
inputs = Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
#s = Lambda(lambda x: x / 255)(inputs) #No need for this if we normalize our inputs beforehand
s = inputs
#Contraction path
c1 = Conv2D(16, (3, 3), kernel_initializer = kernel_initializer,activation='relu',padding='same')(s)
c1 = Dropout(0.5)(c1)
p1 = MaxPooling2D((2, 2))(c1)
c2 = Conv2D(32, (3, 3), kernel_initializer = kernel_initializer,activation='relu',padding='same')(p1)
c2 = Dropout(0.5)(c2)
p2 = MaxPooling2D((2, 2))(c2)
c3 = Conv2D(64, (3, 3), kernel_initializer = kernel_initializer,activation='relu', padding='same')(p2)
c3 = Dropout(0.5)(c3)
p3 = MaxPooling2D((2, 2))(c3)
c4 = Conv2D(128, (3, 3), kernel_initializer = kernel_initializer, activation='relu', padding='same')(p3)
c4 = Dropout(0.5)(c4)
p4 = MaxPooling2D(pool_size=(2, 2))(c4)
c5 = Conv2D(256, (3, 3), kernel_initializer = kernel_initializer,activation='relu', padding='same')(p4)
c5 = Dropout(0.5)(c5)
#Expansive path
u6 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(c5)
u6 = concatenate([u6, c4])
c6 = Conv2D(128, (3, 3),kernel_initializer = kernel_initializer,activation='relu', padding='same')(u6)
c6 = Dropout(0.5)(c6)
u7 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(c6)
u7 = concatenate([u7, c3])
c7 = Conv2D(64, (3, 3),kernel_initializer = kernel_initializer,activation='relu', padding='same')(u7)
c7 = Dropout(0.5)(c7)
u8 = Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(c7)
u8 = concatenate([u8, c2])
c8 = Conv2D(32, (3, 3),kernel_initializer = kernel_initializer,activation='relu', padding='same')(u8)
c8 = Dropout(0.5)(c8)
u9 = Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same')(c8)
u9 = concatenate([u9, c1], axis=3)
c9 = Conv2D(16, (3, 3),kernel_initializer = kernel_initializer,activation='relu', padding='same')(u9)
c9 = Dropout(0.5)(c9)
outputs = Conv2D(3, (1, 1), activation='sigmoid')(c9)
model = Model(inputs, outputs)
model.summary()
return model
model.compile(optimizer='adam',loss='mean_squared_error',metrics=[psnr,ssim,tf.losses.mean_squared_error])

How to reshape data to a 2D CNN input?

everyone!
I've a problem with data reshaping to fed my 2D CNN.
My model is
cnn_input = Input(shape=(1, 800, 26), name='cnn_input')
cnn_conv1 = Conv2D(filters=16, kernel_size=2, strides=(1, 1), padding='same')(cnn_input)
cnn_batch_norm1 = BatchNormalization()(cnn_conv1)
cnn_relu1 = ReLU()(cnn_batch_norm1)
cnn_pool1 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), data_format='channels_first')(cnn_relu1)
cnn_drop1 = Dropout(0.2)(cnn_pool1)
cnn_conv2 = Conv2D(filters=32, kernel_size=2, strides=(1, 1), padding='same')(cnn_drop1)
cnn_batch_norm2 = BatchNormalization()(cnn_conv2)
cnn_relu2 = ReLU()(cnn_batch_norm2)
cnn_pool2 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), data_format='channels_first')(cnn_relu2)
cnn_drop2 = Dropout(0.2)(cnn_pool2)
cnn_conv3 = Conv2D(filters=64, kernel_size=2, strides=(1, 1), padding='same')(cnn_drop2)
cnn_batch_norm3 = BatchNormalization()(cnn_conv3)
cnn_relu3 = ReLU()(cnn_batch_norm3)
cnn_pool3 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), data_format='channels_first')(cnn_relu3)
cnn_drop3 = Dropout(0.2)(cnn_pool3)
cnn_conv4 = Conv2D(filters=128, kernel_size=2, strides=(1, 1), padding='same')(cnn_drop3)
cnn_batch_norm4 = BatchNormalization()(cnn_conv4)
cnn_relu4 = ReLU()(cnn_batch_norm4)
cnn_pool4 = MaxPooling2D(pool_size=(4, 4), strides=(4, 4), data_format='channels_first')(cnn_relu4)
cnn_drop4 = Dropout(0.2)(cnn_pool4)
cnn_conv5 = Conv2D(filters=64, kernel_size=2, strides=(1, 1), padding='same')(cnn_drop4)
cnn_batch_norm5 = BatchNormalization()(cnn_conv5)
cnn_relu5 = ReLU()(cnn_batch_norm5)
cnn_pool5 = MaxPooling2D(pool_size=(4, 4), strides=(4, 4), data_format='channels_first')(cnn_relu5)
cnn_drop5 = Dropout(0.2)(cnn_pool5)
cnn_flat = Flatten()(cnn_drop5)
cnn_output = Dense(256)(cnn_flat)
cnn = Model(inputs=cnn_input, outputs=cnn_output, name='PCNNA')
and the shape of my data is
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)
>>> (800, 26) (200, 26) (800,) (200,)
But before fit the model, I made the following reshape :
X_train = np.reshape(X_train, (1, 800, 26))
and when I run fit() I get this error:
ValueError: Error when checking input: expected pcnna_input to have 4 dimensions, but got array with shape (1, 800, 26)

AttributeError: can't set attribute,

I have this vgg model. Which I am using to extract an image features. But I am getting an attribute error: I can't seem to understand the error. I am using keras 2.2.4 and tensorflow 1.13.1 with python 3.7.3. I am using anaconda with spyder as IDE.
import numpy as np
import warnings
#tf.compat.v1.reset_default_graph()
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import GlobalMaxPooling2D
from tensorflow.keras.layers import GlobalAveragePooling2D
from tensorflow.keras.preprocessing import image
from tensorflow.python.keras.utils import layer_utils , get_source_inputs
from tensorflow.python.keras.utils.data_utils import get_file
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.applications.imagenet_utils import decode_predictions
from tensorflow.python.keras.applications.imagenet_utils import preprocess_input
from keras_applications.imagenet_utils import _obtain_input_shape
#from keras.engine.topology import get_source_inputs
#from tensorflow.python.keras.utils.layer_utils import get_source_inputs
WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5'
WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
def VGG16(include_top=True, weights='imagenet',
input_tensor=None, input_shape=None,
pooling=None,
classes=1000):
if weights not in {'imagenet', None}:
raise ValueError('The `weights` argument should be either '
'`None` (random initialization) or `imagenet` '
'(pre-training on ImageNet).')
if weights == 'imagenet' and include_top and classes != 1000:
raise ValueError('If using `weights` as imagenet with `include_top`'
' as true, `classes` should be 1000')
# Determine proper input shape
input_shape = _obtain_input_shape(input_shape,
default_size=224,
min_size=48,
data_format=K.image_data_format(),
require_flatten=include_top)
if input_tensor is None:
img_input = Input(shape=input_shape)
else:
if not K.is_keras_tensor(input_tensor):
img_input = Input(tensor=input_tensor, shape=input_shape)
else:
img_input = input_tensor
# Block 1
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input)
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
# Block 2
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)
# Block 3
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)
# Block 4
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)
# Block 5
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)
if include_top:
# Classification block
x = Flatten(name='flatten')(x)
x = Dense(4096, activation='relu', name='fc1')(x)
x = Dense(4096, activation='relu', name='fc2')(x)
x = Dense(classes, activation='softmax', name='predictions')(x)
else:
if pooling == 'avg':
x = GlobalAveragePooling2D()(x)
elif pooling == 'max':
x = GlobalMaxPooling2D()(x)
# Ensure that the model takes into account
# any potential predecessors of `input_tensor`.
if input_tensor is not None:
inputs = get_source_inputs(input_tensor)
else:
inputs = img_input
# Create model.
model = Model(inputs, x, name='vgg16')
# load weights
if weights == 'imagenet':
if include_top:
weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels.h5',
WEIGHTS_PATH,
)
else:
weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',
WEIGHTS_PATH_NO_TOP,
cache_subdir='models')
model.load_weights(weights_path)
if K.backend() == 'theano':
layer_utils.convert_all_kernels_in_model(model)
if K.image_data_format() == 'channels_first':
if include_top:
maxpool = model.get_layer(name='block5_pool')
shape = maxpool.output_shape[1:]
dense = model.get_layer(name='fc1')
layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first')
if K.backend() == 'tensorflow':
warnings.warn('You are using the TensorFlow backend, yet you '
'are using the Theano '
'image data format convention '
'(`image_data_format="channels_first"`). '
'For best performance, set '
'`image_data_format="channels_last"` in '
'your Keras config '
'at ~/.keras/keras.json.')
model.layers.pop() # Get rid of the classification layer
model.outputs = [model.layers[-1].output]
model.layers[-1].outbound_nodes = [ ]
return model```
I am getting the following error on the second last line of the code:
File "/home/natsu/Final_code/vgg16.py", line 200, in VGG16
model.layers[-1].outbound_nodes = []
File "/home/natsu/anaconda3/envs/py-env/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1639, in __setattr__
super(Layer, self).__setattr__(name, value)
AttributeError: can't set attribute```
I was able to execute your code successfully in tensorflow version 1.15 without any modifications.
I was able to execute your code successfully in tensorflow version 2.2.0 with minor modifications shown below -
Modify,
from tensorflow.python.keras.utils import layer_utils , get_source_inputs
to
from tensorflow.python.keras.utils import layer_utils
from tensorflow.python.keras.utils.layer_utils import get_source_inputs
Can you please upgrade the tensorflow version and check if the error is fixed.
Hope this answers your question. Happy Learning.

how do I solve this error:'Tensor' object is not callable when I want to fill a tensor?

when I want to fill a tensor with the value of another tensor it produces this error:
Traceback (most recent call last):
File "", line 80, in
wtm_Fill=wfill(temp(0,0))
TypeError: 'Tensor' object is not callable
wtm=Input((28,28,1))
image = Input((28, 28, 1))
conv1 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl1e',dilation_rate=(2,2))(image)
conv2 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl2e',dilation_rate=(2,2))(conv1)
conv3 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl3e',dilation_rate=(2,2))(conv2)
#conv3 = Conv2D(8, (3, 3), activation='relu', padding='same', name='convl3e', kernel_initializer='Orthogonal',bias_initializer='glorot_uniform')(conv2)
BN=BatchNormalization()(conv3)
#DrO1=Dropout(0.25,name='Dro1')(BN)
encoded = Conv2D(1, (5, 5), activation='relu', padding='same',name='encoded_I',dilation_rate=(2,2))(BN)
#-----------------------adding w---------------------------------------
temp=tf.reshape(wtm,(28,28))
wfill=Kr.layers.Lambda(lambda x:tf.fill([28,28],x))
wtm_Fill=wfill(temp(0,0))
add_const = Kr.layers.Lambda(lambda x: x[0] + x[1])
encoded_merged = add_const([encoded,wtm])
I need sth like this:
wtm=
0 1 1
1 1 0
0 0 1
wtm(0,0)=0 so I want to produce this
new tensor with shape(28,28,1)
0 0 0 ... 0
.
. 0 0 ... 0
0 0 0 ... 0
from keras.layers import Input, Concatenate, GaussianNoise,Dropout,BatchNormalization
from keras.layers import Conv2D, AtrousConv2D
from keras.models import Model
from keras.datasets import mnist
from keras.callbacks import TensorBoard
from keras import backend as K
from keras import layers
import matplotlib.pyplot as plt
import tensorflow as tf
import keras as Kr
from keras.optimizers import SGD,RMSprop,Adam
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import EarlyStopping
from keras.callbacks import ModelCheckpoint
import numpy as np
import pylab as pl
import matplotlib.cm as cm
import keract
from matplotlib import pyplot
from keras import optimizers
from keras import regularizers
from tensorflow.python.keras.layers import Lambda;
#-----------------building w train---------------------------------------------
w_expand=np.zeros((49999,28,28),dtype='float32')
wv_expand=np.zeros((9999,28,28),dtype='float32')
wt_random=np.random.randint(2, size=(49999,4,4))
wt_random=wt_random.astype(np.float32)
wv_random=np.random.randint(2, size=(9999,4,4))
wv_random=wv_random.astype(np.float32)
w_expand[:,:4,:4]=wt_random
wv_expand[:,:4,:4]=wv_random
x,y,z=w_expand.shape
w_expand=w_expand.reshape((x,y,z,1))
x,y,z=wv_expand.shape
wv_expand=wv_expand.reshape((x,y,z,1))
#-----------------building w test---------------------------------------------
w_test = np.random.randint(2,size=(1,4,4))
w_test=w_test.astype(np.float32)
wt_expand=np.zeros((1,28,28),dtype='float32')
wt_expand[:,0:4,0:4]=w_test
wt_expand=wt_expand.reshape((1,28,28,1))
#-----------------------encoder------------------------------------------------
#------------------------------------------------------------------------------
wtm=Input((28,28,1))
image = Input((28, 28, 1))
conv1 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl1e',dilation_rate=(2,2))(image)
conv2 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl2e',dilation_rate=(2,2))(conv1)
conv3 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl3e',dilation_rate=(2,2))(conv2)
BN=BatchNormalization()(conv3)
encoded = Conv2D(1, (5, 5), activation='relu', padding='same',name='encoded_I',dilation_rate=(2,2))(BN)
temp=tf.reshape(wtm,(28,28))
wfill=Kr.layers.Lambda(lambda x:tf.fill([28,28],x))
wtm_Fill=wfill(temp[0,0])
add_const = Kr.layers.Lambda(lambda x: x[0] + x[1])
encoded_merged = add_const([encoded,wtm_Fill])
#wfill=Kr.layers.Lambda(lambda x:tf.fill([28,28],x))
#value=wtm[0][0][0]
#x=tf.fill((28,28,1),value)
#add_const = Kr.layers.Lambda(lambda x: x[0] + x[1])
#encoded_merged = add_const([encoded,x])
#encoder=Model(inputs=[image,wtm], outputs= encoded_merged ,name='encoder')
#encoder.summary()
#-----------------------decoder------------------------------------------------
#------------------------------------------------------------------------------
deconv1 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl1d',dilation_rate=(2,2))(encoded_merged)
deconv2 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl2d',dilation_rate=(2,2))(deconv1)
deconv3 = Conv2D(64, (5, 5), activation='relu',padding='same', name='convl3d',dilation_rate=(2,2))(deconv2)
deconv4 = Conv2D(64, (5, 5), activation='relu',padding='same', name='convl4d',dilation_rate=(2,2))(deconv3)
BNd=BatchNormalization()(deconv3)
decoded = Conv2D(1, (5, 5), activation='sigmoid', padding='same', name='decoder_output',dilation_rate=(2,2))(BNd)
model=Model(inputs=[image,wtm],outputs=decoded)
decoded_noise = GaussianNoise(0.5)(decoded)
#----------------------w extraction------------------------------------
convw1 = Conv2D(64, (3,3), activation='relu', padding='same', name='conl1w',dilation_rate=(2,2))(decoded_noise)
convw2 = Conv2D(64, (3, 3), activation='relu', padding='same', name='convl2w',dilation_rate=(2,2))(convw1)
convw3 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conl3w',dilation_rate=(2,2))(convw2)
convw4 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conl4w',dilation_rate=(2,2))(convw3)
convw5 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conl5w',dilation_rate=(2,2))(convw4)
convw6 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conl6w',dilation_rate=(2,2))(convw5)
pred_w = Conv2D(1, (1, 1), activation='sigmoid', padding='same', name='reconstructed_W',dilation_rate=(2,2))(convw6)
w_extraction=Model(inputs=[image,wtm],outputs=[decoded,pred_w])
w_extraction.summary()
this is my new code but after implementation it produces this error:
Traceback (most recent call last):
File "", line 106, in
model=Model(inputs=[image,wtm],outputs=decoded)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\legacy\interfaces.py",
line 91, in wrapper
return func(*args, **kwargs)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py",
line 93, in init
self._init_graph_network(*args, **kwargs)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py",
line 231, in _init_graph_network
self.inputs, self.outputs)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py",
line 1366, in _map_graph_network
tensor_index=tensor_index)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py",
line 1353, in build_map
node_index, tensor_index)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py",
line 1353, in build_map
node_index, tensor_index)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py",
line 1353, in build_map
node_index, tensor_index)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py",
line 1353, in build_map
node_index, tensor_index)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py",
line 1353, in build_map
node_index, tensor_index)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py",
line 1353, in build_map
node_index, tensor_index)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py",
line 1353, in build_map
node_index, tensor_index)
File
"D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py",
line 1325, in build_map
node = layer._inbound_nodes[node_index]
AttributeError: 'NoneType' object has no attribute '_inbound_nodes'
To produce such tensor you need to:
Suppose tensor shape is (28,28,1)
value = tensor[0][0][0]
wtm = tf.fill((10,10,1), value)
This will output tensor of shape (10,10,1) filled with value

Error when checking model input: expected no data, but got:

I want to extract a part of the SSD512 architecture into a separate model. The function that returns the required model is below:
def ssd_512_end(image_size,
n_classes,
mode='training',
l2_regularization=0.0005,
min_scale=None,
max_scale=None,
scales=None,
aspect_ratios_global=None,
aspect_ratios_per_layer=[[1.0, 2.0, 0.5],
[1.0, 2.0, 0.5, 3.0, 1.0/3.0],
[1.0, 2.0, 0.5, 3.0, 1.0/3.0],
[1.0, 2.0, 0.5, 3.0, 1.0/3.0],
[1.0, 2.0, 0.5, 3.0, 1.0/3.0],
[1.0, 2.0, 0.5],
[1.0, 2.0, 0.5]],
two_boxes_for_ar1=True,
steps=[8, 16, 32, 64, 128, 256, 512],
offsets=None,
clip_boxes=False,
variances=[0.1, 0.1, 0.2, 0.2],
coords='centroids',
normalize_coords=True,
subtract_mean=[123, 117, 104],
divide_by_stddev=None,
swap_channels=[2, 1, 0],
confidence_thresh=0.01,
iou_threshold=0.45,
top_k=200,
nms_max_output_size=400,
return_predictor_sizes=False):
n_predictor_layers = 7 # The number of predictor conv layers in the network is 7 for the original SSD512
n_classes += 1 # Account for the background class.
l2_reg = l2_regularization # Make the internal name shorter.
img_height, img_width, img_channels = image_size[0], image_size[1], image_size[2]
############################################################################
# Get a few exceptions out of the way.
############################################################################
if aspect_ratios_global is None and aspect_ratios_per_layer is None:
raise ValueError("`aspect_ratios_global` and `aspect_ratios_per_layer` cannot both be None. At least one needs to be specified.")
if aspect_ratios_per_layer:
if len(aspect_ratios_per_layer) != n_predictor_layers:
raise ValueError("It must be either aspect_ratios_per_layer is None or len(aspect_ratios_per_layer) == {}, but len(aspect_ratios_per_layer) == {}.".format(n_predictor_layers, len(aspect_ratios_per_layer)))
if (min_scale is None or max_scale is None) and scales is None:
raise ValueError("Either `min_scale` and `max_scale` or `scales` need to be specified.")
if scales:
if len(scales) != n_predictor_layers+1:
raise ValueError("It must be either scales is None or len(scales) == {}, but len(scales) == {}.".format(n_predictor_layers+1, len(scales)))
else: # If no explicit list of scaling factors was passed, compute the list of scaling factors from `min_scale` and `max_scale`
scales = np.linspace(min_scale, max_scale, n_predictor_layers+1)
if len(variances) != 4:
raise ValueError("4 variance values must be pased, but {} values were received.".format(len(variances)))
variances = np.array(variances)
if np.any(variances <= 0):
raise ValueError("All variances must be >0, but the variances given are {}".format(variances))
if (not (steps is None)) and (len(steps) != n_predictor_layers):
raise ValueError("You must provide at least one step value per predictor layer.")
if (not (offsets is None)) and (len(offsets) != n_predictor_layers):
raise ValueError("You must provide at least one offset value per predictor layer.")
############################################################################
# Compute the anchor box parameters.
############################################################################
# Set the aspect ratios for each predictor layer. These are only needed for the anchor box layers.
if aspect_ratios_per_layer:
aspect_ratios = aspect_ratios_per_layer
else:
aspect_ratios = [aspect_ratios_global] * n_predictor_layers
# Compute the number of boxes to be predicted per cell for each predictor layer.
# We need this so that we know how many channels the predictor layers need to have.
if aspect_ratios_per_layer:
n_boxes = []
for ar in aspect_ratios_per_layer:
if (1 in ar) & two_boxes_for_ar1:
n_boxes.append(len(ar) + 1) # +1 for the second box for aspect ratio 1
else:
n_boxes.append(len(ar))
else: # If only a global aspect ratio list was passed, then the number of boxes is the same for each predictor layer
if (1 in aspect_ratios_global) & two_boxes_for_ar1:
n_boxes = len(aspect_ratios_global) + 1
else:
n_boxes = len(aspect_ratios_global)
n_boxes = [n_boxes] * n_predictor_layers
if steps is None:
steps = [None] * n_predictor_layers
if offsets is None:
offsets = [None] * n_predictor_layers
############################################################################
# Define functions for the Lambda layers below.
############################################################################
def identity_layer(tensor):
return tensor
def input_mean_normalization(tensor):
return tensor - np.array(subtract_mean)
def input_stddev_normalization(tensor):
return tensor / np.array(divide_by_stddev)
def input_channel_swap(tensor):
if len(swap_channels) == 3:
return K.stack([tensor[...,swap_channels[0]], tensor[...,swap_channels[1]], tensor[...,swap_channels[2]]], axis=-1)
elif len(swap_channels) == 4:
return K.stack([tensor[...,swap_channels[0]], tensor[...,swap_channels[1]], tensor[...,swap_channels[2]], tensor[...,swap_channels[3]]], axis=-1)
############################################################################
# Build the network.
############################################################################
x = Input(shape=(img_height, img_width, img_channels))
# The following identity layer is only needed so that the subsequent lambda layers can be optional.
x1 = Lambda(identity_layer, output_shape=(img_height, img_width, img_channels), name='identity_layer')(x)
if not (subtract_mean is None):
x1 = Lambda(input_mean_normalization, output_shape=(img_height, img_width, img_channels), name='input_mean_normalization')(x1)
if not (divide_by_stddev is None):
x1 = Lambda(input_stddev_normalization, output_shape=(img_height, img_width, img_channels), name='input_stddev_normalization')(x1)
if swap_channels:
x1 = Lambda(input_channel_swap, output_shape=(img_height, img_width, img_channels), name='input_channel_swap')(x1)
conv1_1 = Conv2D(64, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv1_1')(x1)
conv1_2 = Conv2D(64, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv1_2')(conv1_1)
pool1 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same', name='pool1')(conv1_2)
conv2_1 = Conv2D(128, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv2_1')(pool1)
conv2_2 = Conv2D(128, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv2_2')(conv2_1)
pool2 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same', name='pool2')(conv2_2)
conv3_1 = Conv2D(256, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv3_1')(pool2)
conv3_2 = Conv2D(256, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv3_2')(conv3_1)
conv3_3 = Conv2D(256, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv3_3')(conv3_2)
pool3 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same', name='pool3')(conv3_3)
conv4_1 = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv4_1')(pool3)
conv4_2 = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv4_2')(conv4_1)
conv4_3 = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv4_3')(conv4_2)
pool4 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same', name='pool4')(conv4_3)
conv5_1 = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv5_1')(pool4)
conv5_2 = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv5_2')(conv5_1)
conv5_3 = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv5_3')(conv5_2)
pool5 = MaxPooling2D(pool_size=(3, 3), strides=(1, 1), padding='same', name='pool5')(conv5_3)
fc6 = Conv2D(1024, (3, 3), dilation_rate=(6, 6), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='fc6')(pool5)
fc7 = Conv2D(1024, (1, 1), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='fc7')(fc6)
conv6_1 = Conv2D(256, (1, 1), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv6_1')(fc7)
conv6_1 = ZeroPadding2D(padding=((1, 1), (1, 1)), name='conv6_padding')(conv6_1)
conv6_2 = Conv2D(512, (3, 3), strides=(2, 2), activation='relu', padding='valid', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv6_2')(conv6_1)
conv7_1 = Conv2D(128, (1, 1), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv7_1')(conv6_2)
conv7_1 = ZeroPadding2D(padding=((1, 1), (1, 1)), name='conv7_padding')(conv7_1)
conv7_2 = Conv2D(256, (3, 3), strides=(2, 2), activation='relu', padding='valid', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv7_2')(conv7_1)
conv8_1 = Conv2D(128, (1, 1), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv8_1')(conv7_2)
conv8_1 = ZeroPadding2D(padding=((1, 1), (1, 1)), name='conv8_padding')(conv8_1)
conv8_2 = Conv2D(256, (3, 3), strides=(2, 2), activation='relu', padding='valid', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv8_2')(conv8_1)
conv9_1 = Conv2D(128, (1, 1), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv9_1')(conv8_2)
conv9_1 = ZeroPadding2D(padding=((1, 1), (1, 1)), name='conv9_padding')(conv9_1)
conv9_2 = Conv2D(256, (3, 3), strides=(2, 2), activation='relu', padding='valid', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv9_2')(conv9_1)
conv10_1 = Conv2D(128, (1, 1), activation='relu', padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv10_1')(conv9_2)
conv10_1 = ZeroPadding2D(padding=((1, 1), (1, 1)), name='conv10_padding')(conv10_1)
conv10_2 = Conv2D(256, (4, 4), strides=(1, 1), activation='relu', padding='valid', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv10_2')(conv10_1)
conv4_3_input = Input(tensor = conv4_3, name = 'conv4_3_input')
fc7_input = Input(tensor = fc7, name = 'fc7_input')
conv6_2_input = Input(tensor = conv6_2, name = 'conv6_2_input')
conv7_2_input = Input(tensor = conv7_2, name = 'conv7_2_input')
conv8_2_input = Input(tensor = conv8_2, name = 'conv8_2_input')
conv9_2_input = Input(tensor = conv9_2, name = 'conv9_2_input')
conv10_2_input = Input(tensor = conv10_2, name = 'conv10_2_input')
# Feed conv4_3 into the L2 normalization layer
conv4_3_norm = L2Normalization(gamma_init=20, name='conv4_3_norm')(conv4_3)
### Build the convolutional predictor layers on top of the base network
# We precidt `n_classes` confidence values for each box, hence the confidence predictors have depth `n_boxes * n_classes`
# Output shape of the confidence layers: `(batch, height, width, n_boxes * n_classes)`
conv4_3_norm_mbox_conf = Conv2D(n_boxes[0] * n_classes, (3, 3), padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv4_3_norm_mbox_conf')(conv4_3_norm)
fc7_mbox_conf = Conv2D(n_boxes[1] * n_classes, (3, 3), padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='fc7_mbox_conf')(fc7)
conv6_2_mbox_conf = Conv2D(n_boxes[2] * n_classes, (3, 3), padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv6_2_mbox_conf')(conv6_2)
conv7_2_mbox_conf = Conv2D(n_boxes[3] * n_classes, (3, 3), padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv7_2_mbox_conf')(conv7_2)
conv8_2_mbox_conf = Conv2D(n_boxes[4] * n_classes, (3, 3), padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv8_2_mbox_conf')(conv8_2)
conv9_2_mbox_conf = Conv2D(n_boxes[5] * n_classes, (3, 3), padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv9_2_mbox_conf')(conv9_2)
conv10_2_mbox_conf = Conv2D(n_boxes[6] * n_classes, (3, 3), padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv10_2_mbox_conf')(conv10_2)
# We predict 4 box coordinates for each box, hence the localization predictors have depth `n_boxes * 4`
# Output shape of the localization layers: `(batch, height, width, n_boxes * 4)`
conv4_3_norm_mbox_loc = Conv2D(n_boxes[0] * 4, (3, 3), padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv4_3_norm_mbox_loc')(conv4_3_norm)
fc7_mbox_loc = Conv2D(n_boxes[1] * 4, (3, 3), padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='fc7_mbox_loc')(fc7)
conv6_2_mbox_loc = Conv2D(n_boxes[2] * 4, (3, 3), padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv6_2_mbox_loc')(conv6_2)
conv7_2_mbox_loc = Conv2D(n_boxes[3] * 4, (3, 3), padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv7_2_mbox_loc')(conv7_2)
conv8_2_mbox_loc = Conv2D(n_boxes[4] * 4, (3, 3), padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv8_2_mbox_loc')(conv8_2)
conv9_2_mbox_loc = Conv2D(n_boxes[5] * 4, (3, 3), padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv9_2_mbox_loc')(conv9_2)
conv10_2_mbox_loc = Conv2D(n_boxes[6] * 4, (3, 3), padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv10_2_mbox_loc')(conv10_2)
### Generate the anchor boxes (called "priors" in the original Caffe/C++ implementation, so I'll keep their layer names)
# Output shape of anchors: `(batch, height, width, n_boxes, 8)`
conv4_3_norm_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[0], next_scale=scales[1], aspect_ratios=aspect_ratios[0],
two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[0], this_offsets=offsets[0], clip_boxes=clip_boxes,
variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv4_3_norm_mbox_priorbox')(conv4_3_norm_mbox_loc)
fc7_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[1], next_scale=scales[2], aspect_ratios=aspect_ratios[1],
two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[1], this_offsets=offsets[1], clip_boxes=clip_boxes,
variances=variances, coords=coords, normalize_coords=normalize_coords, name='fc7_mbox_priorbox')(fc7_mbox_loc)
conv6_2_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[2], next_scale=scales[3], aspect_ratios=aspect_ratios[2],
two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[2], this_offsets=offsets[2], clip_boxes=clip_boxes,
variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv6_2_mbox_priorbox')(conv6_2_mbox_loc)
conv7_2_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[3], next_scale=scales[4], aspect_ratios=aspect_ratios[3],
two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[3], this_offsets=offsets[3], clip_boxes=clip_boxes,
variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv7_2_mbox_priorbox')(conv7_2_mbox_loc)
conv8_2_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[4], next_scale=scales[5], aspect_ratios=aspect_ratios[4],
two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[4], this_offsets=offsets[4], clip_boxes=clip_boxes,
variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv8_2_mbox_priorbox')(conv8_2_mbox_loc)
conv9_2_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[5], next_scale=scales[6], aspect_ratios=aspect_ratios[5],
two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[5], this_offsets=offsets[5], clip_boxes=clip_boxes,
variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv9_2_mbox_priorbox')(conv9_2_mbox_loc)
conv10_2_mbox_priorbox = AnchorBoxes(img_height, img_width, this_scale=scales[6], next_scale=scales[7], aspect_ratios=aspect_ratios[6],
two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[6], this_offsets=offsets[6], clip_boxes=clip_boxes,
variances=variances, coords=coords, normalize_coords=normalize_coords, name='conv10_2_mbox_priorbox')(conv10_2_mbox_loc)
### Reshape
# Reshape the class predictions, yielding 3D tensors of shape `(batch, height * width * n_boxes, n_classes)`
# We want the classes isolated in the last axis to perform softmax on them
conv4_3_norm_mbox_conf_reshape = Reshape((-1, n_classes), name='conv4_3_norm_mbox_conf_reshape')(conv4_3_norm_mbox_conf)
fc7_mbox_conf_reshape = Reshape((-1, n_classes), name='fc7_mbox_conf_reshape')(fc7_mbox_conf)
conv6_2_mbox_conf_reshape = Reshape((-1, n_classes), name='conv6_2_mbox_conf_reshape')(conv6_2_mbox_conf)
conv7_2_mbox_conf_reshape = Reshape((-1, n_classes), name='conv7_2_mbox_conf_reshape')(conv7_2_mbox_conf)
conv8_2_mbox_conf_reshape = Reshape((-1, n_classes), name='conv8_2_mbox_conf_reshape')(conv8_2_mbox_conf)
conv9_2_mbox_conf_reshape = Reshape((-1, n_classes), name='conv9_2_mbox_conf_reshape')(conv9_2_mbox_conf)
conv10_2_mbox_conf_reshape = Reshape((-1, n_classes), name='conv10_2_mbox_conf_reshape')(conv10_2_mbox_conf)
# Reshape the box predictions, yielding 3D tensors of shape `(batch, height * width * n_boxes, 4)`
# We want the four box coordinates isolated in the last axis to compute the smooth L1 loss
conv4_3_norm_mbox_loc_reshape = Reshape((-1, 4), name='conv4_3_norm_mbox_loc_reshape')(conv4_3_norm_mbox_loc)
fc7_mbox_loc_reshape = Reshape((-1, 4), name='fc7_mbox_loc_reshape')(fc7_mbox_loc)
conv6_2_mbox_loc_reshape = Reshape((-1, 4), name='conv6_2_mbox_loc_reshape')(conv6_2_mbox_loc)
conv7_2_mbox_loc_reshape = Reshape((-1, 4), name='conv7_2_mbox_loc_reshape')(conv7_2_mbox_loc)
conv8_2_mbox_loc_reshape = Reshape((-1, 4), name='conv8_2_mbox_loc_reshape')(conv8_2_mbox_loc)
conv9_2_mbox_loc_reshape = Reshape((-1, 4), name='conv9_2_mbox_loc_reshape')(conv9_2_mbox_loc)
conv10_2_mbox_loc_reshape = Reshape((-1, 4), name='conv10_2_mbox_loc_reshape')(conv10_2_mbox_loc)
# Reshape the anchor box tensors, yielding 3D tensors of shape `(batch, height * width * n_boxes, 8)`
conv4_3_norm_mbox_priorbox_reshape = Reshape((-1, 8), name='conv4_3_norm_mbox_priorbox_reshape')(conv4_3_norm_mbox_priorbox)
fc7_mbox_priorbox_reshape = Reshape((-1, 8), name='fc7_mbox_priorbox_reshape')(fc7_mbox_priorbox)
conv6_2_mbox_priorbox_reshape = Reshape((-1, 8), name='conv6_2_mbox_priorbox_reshape')(conv6_2_mbox_priorbox)
conv7_2_mbox_priorbox_reshape = Reshape((-1, 8), name='conv7_2_mbox_priorbox_reshape')(conv7_2_mbox_priorbox)
conv8_2_mbox_priorbox_reshape = Reshape((-1, 8), name='conv8_2_mbox_priorbox_reshape')(conv8_2_mbox_priorbox)
conv9_2_mbox_priorbox_reshape = Reshape((-1, 8), name='conv9_2_mbox_priorbox_reshape')(conv9_2_mbox_priorbox)
conv10_2_mbox_priorbox_reshape = Reshape((-1, 8), name='conv10_2_mbox_priorbox_reshape')(conv10_2_mbox_priorbox)
### Concatenate the predictions from the different layers
# Axis 0 (batch) and axis 2 (n_classes or 4, respectively) are identical for all layer predictions,
# so we want to concatenate along axis 1, the number of boxes per layer
# Output shape of `mbox_conf`: (batch, n_boxes_total, n_classes)
mbox_conf = Concatenate(axis=1, name='mbox_conf')([conv4_3_norm_mbox_conf_reshape,
fc7_mbox_conf_reshape,
conv6_2_mbox_conf_reshape,
conv7_2_mbox_conf_reshape,
conv8_2_mbox_conf_reshape,
conv9_2_mbox_conf_reshape,
conv10_2_mbox_conf_reshape])
# Output shape of `mbox_loc`: (batch, n_boxes_total, 4)
mbox_loc = Concatenate(axis=1, name='mbox_loc')([conv4_3_norm_mbox_loc_reshape,
fc7_mbox_loc_reshape,
conv6_2_mbox_loc_reshape,
conv7_2_mbox_loc_reshape,
conv8_2_mbox_loc_reshape,
conv9_2_mbox_loc_reshape,
conv10_2_mbox_loc_reshape])
# Output shape of `mbox_priorbox`: (batch, n_boxes_total, 8)
mbox_priorbox = Concatenate(axis=1, name='mbox_priorbox')([conv4_3_norm_mbox_priorbox_reshape,
fc7_mbox_priorbox_reshape,
conv6_2_mbox_priorbox_reshape,
conv7_2_mbox_priorbox_reshape,
conv8_2_mbox_priorbox_reshape,
conv9_2_mbox_priorbox_reshape,
conv10_2_mbox_priorbox_reshape])
# The box coordinate predictions will go into the loss function just the way they are,
# but for the class predictions, we'll apply a softmax activation layer first
mbox_conf_softmax = Activation('softmax', name='mbox_conf_softmax')(mbox_conf)
# Concatenate the class and box predictions and the anchors to one large predictions vector
# Output shape of `predictions`: (batch, n_boxes_total, n_classes + 4 + 8)
predictions = Concatenate(axis=2, name='predictions')([mbox_conf_softmax, mbox_loc, mbox_priorbox])
end_model = Model(inputs = [conv4_3_input,fc7_input,conv6_2_input,conv7_2_input,conv8_2_input,conv9_2_input,conv10_2_input], outputs = predictions)
return end_model
So when I try to train the model, it gives me the following error:
ValueError Traceback (most recent call last)
<ipython-input-11-467c36610dec> in <module>()
85 validation_data=val_gen_end,
86 validation_steps=ceil(val_dataset_size/batch_size),
---> 87 initial_epoch=initial_epoch)
88
89 # history = model.fit_generator(generator=train_generator,
c:\python36\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
85 warnings.warn('Update your `' + object_name +
86 '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 87 return func(*args, **kwargs)
88 wrapper._original_function = func
89 return wrapper
c:\python36\lib\site-packages\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
2040 outs = self.train_on_batch(x, y,
2041 sample_weight=sample_weight,
-> 2042 class_weight=class_weight)
2043
2044 if not isinstance(outs, list):
c:\python36\lib\site-packages\keras\engine\training.py in train_on_batch(self, x, y, sample_weight, class_weight)
1754 sample_weight=sample_weight,
1755 class_weight=class_weight,
-> 1756 check_batch_axis=True)
1757 if self.uses_learning_phase and not isinstance(K.learning_phase(), int):
1758 ins = x + y + sample_weights + [1.]
c:\python36\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size)
1376 self._feed_input_shapes,
1377 check_batch_axis=False,
-> 1378 exception_prefix='input')
1379 y = _standardize_input_data(y, self._feed_output_names,
1380 output_shapes,
c:\python36\lib\site-packages\keras\engine\training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
56 raise ValueError('Error when checking model ' +
57 exception_prefix + ': '
---> 58 'expected no data, but got:', data)
59 return []
60 if data is None:
ValueError: ('Error when checking model input: expected no data, but got:', [array([[[[1.62046947e+01, 0.00000000e+00, 0.00000000e+00, ...
Since 'end_model' has 7 inputs, I am passing a generator which will yield 7 input data appropriately.
Where am I going wrong? Please help!
Thank you.
You don't give x = Input(shape=(img_height, img_width, img_channels)) as input to Model that is why technically your model has no inputs. The other inputs you have are computed from x as you specify the tensor argument for example in Input(tensor = conv4_3, name = 'conv4_3_input') where conv4_3 is computed from x.
So you need specify the original input when creating the model:
end_model = Model(x, predictions)

Categories