What should I do for identity connection between layers in Keras? - tensorflow

I want to consider some identity connections between layers in CNN and send input to the next layers. I used the code below for this and just concatenated the input with the other layer output and send to next layer, but I am not sure if it is true or not because the output of layer is different from what I expected. Did I use a true way for sending input to other layers and identity connections like ResNet?
wtm=Input((4,4,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)
convaux1=Concatenate(axis=3)([conv2,image])
conv3 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl3e',dilation_rate=(2,2))(convaux1)
BN=BatchNormalization()(conv3)
encoded = Conv2D(1, (5, 5), activation='relu', padding='same',name='encoded_I')(BN)
#-----------------------adding w---------------------------------------
wpad=Kr.layers.Lambda(lambda xy: xy[0] + Kr.backend.spatial_2d_padding(xy[1], padding=((0, 24), (0, 24))))
encoded_merged=wpad([encoded,wtm])
#-----------------------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)
convaux2=Concatenate(axis=3)([deconv2,image])
BNda=BatchNormalization()(convaux2)
deconv3 = Conv2D(64, (5, 5), activation='relu',padding='same', name='convl3d',dilation_rate=(2,2))(BNda)
deconv4 = Conv2D(64, (5, 5), activation='relu',padding='same', name='convl4d',dilation_rate=(2,2))(deconv3)
BNd=BatchNormalization()(deconv4)
decoded = Conv2D(1, (5, 5), activation='sigmoid', padding='same', name='decoder_output')(BNd)
model=Model(inputs=[image,wtm],outputs=decoded)

Related

Why a tensorflow model with Spatial Pyramid Pooling layers is slower than a sequential, vanilla model?

I'm training a U-net type model with a minor variation in the architecture which is the Atrous Spatial Pyramid pooling (ASPP) layer at the bottleneck after the encoder. I profiled the model during one forward pass and used tensorboard to check the tracer_view to see which part of the model has the highest latency.
Profiler Tracer View with ASPP layer
This revealed that there's a lot of idle GPU time at ASPP computation. I double checked it by removing the ASPP layer and the just connected the encoder to the decoder. In this experiment, the idle time that was previously there disappeared.
Profiler Tracer View without ASPP layer
I understand that the second model example would be a bit smaller than the former.
This is how my model looks like with ASPP layer. And to I just commented those ASPP layers out to profile the model without ASPP layers.
With ASPP
def get_custom_deeplab(image_size: tuple, num_classes: int):
"""
This model uses a vanilla CNN backbone. This model also uses upsampling2d in place of conv2d transpose
"""
input_layer = keras.Input(shape=(image_size[0], image_size[1], 3))
conv1 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(input_layer)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(pool1)
conv2 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(128, (1, 1), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(pool2)
conv3 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(128, (1, 1), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(pool3)
conv4 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
#######ASPP layers
out_1 = Conv2D(256, (1, 1), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), dilation_rate=1, padding='same')(pool4)
out_6 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), dilation_rate=6, padding='same')(pool4)
out_12 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), dilation_rate=10, padding='same')(pool4)
out_14 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), dilation_rate=14, padding='same')(pool4)
x = layers.Concatenate(axis=-1)([out_1, out_6, out_12, out_14])
########ASPP's output
x = Conv2D(256, (1, 1), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), dilation_rate=1, padding='same')(x)
x = layers.UpSampling2D(
(2,2),interpolation="bilinear",
)(x)
skip_connection_1 = pool3
x = layers.Concatenate(axis=-1)([x,skip_connection_1])
x = Conv2D(128, (1, 1), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(x)
x = Conv2D(256, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(x)
x = layers.UpSampling2D(
(2,2),interpolation="bilinear",
)(x)
skip_connection_2 = pool2
x = layers.Concatenate(axis=-1)([x,skip_connection_2])
x = Conv2D(128, (1, 1), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(x)
x = Conv2D(256, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(x)
x = layers.UpSampling2D(
(2,2),interpolation="bilinear",
)(x)
x = Conv2D(64, (3, 3), activation='relu', kernel_initializer='lecun_uniform', kernel_constraint=max_norm(3), padding='same')(x)
x = layers.UpSampling2D(
(2,2),interpolation="bilinear",
)(x)
x = Conv2D(
num_classes,
kernel_size=1,
padding="same",
use_bias=True,
kernel_initializer=keras.initializers.HeNormal(),
)(x)
return tf.keras.Model(inputs=input_layer,outputs=x)
But, I would like to know if there's any workaround to mitigate the problem of GPU idle time when the model has layers like ASPP?

having a very large loss when I am training a regression loss

I want to predict the center of the pupil from an image. so I used a CNN with 3 Dence layer.
so the input is an image and the output is a coordinate (X,Y).
my model is :
from keras.layers import Layer, Conv2D, MaxPooling2D, UpSampling2D, Dropout,Input ,concatenate, Dense
from keras.models import Model
tf.keras.layers.GlobalAveragePooling2D(
data_format=None, keepdims=False
)
def get_model():
img = Input(shape=(None, None, 3 ))
conv1_1 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(img)
conv1_2 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv1_1)
pool1 = MaxPooling2D((2, 2))(conv1_2)
conv2_1 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool1)
conv2_2 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv2_1)
conv3_1 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv2_2)
conv3_2 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv3_1)
pool3 = MaxPooling2D((2, 2))(conv3_2)
conv4_1 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool3)
conv4_2 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv4_1)
pool4 = MaxPooling2D((2, 2))(conv4_2)
conv5_1 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool4)
conv5_2 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv5_1)
conv5_2 = Dropout(0.5)(conv5_2)
conv5_3 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv5_2)
pool5 = MaxPooling2D((2, 2))(conv5_3)
conv6_1 = Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool5)
conv6_1 = Dropout(0.5)(conv6_1)
conv6_2 = Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(conv6_1)
pool6 = MaxPooling2D((2, 2))(conv6_2)
conv7_1 = Conv2D(1024, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool6)
pool7 = MaxPooling2D((2, 2))(conv7_1)
conv8_1 = Conv2D(1024, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(pool7)
Global_pooling = tf.keras.layers.GlobalAveragePooling2D()(conv8_1)
x = Dense(500, activation='relu')(Global_pooling)
x = Dense(256, activation='relu')(x)
x = Dense(128, activation='relu')(x)
prediction = Dense(2, activation='linear')(x)
model = Model(inputs=[img], outputs=[prediction])
#model.summary()
return model
and I got a very large error with "MSE" in training. what is the problem?
Is the problem with my data?
it's my link in colab: https://colab.research.google.com/drive/12hjlT6JG8IlEXYISKw5zFJE6qBDuuVi1?usp=sharing
than you for your help
(Thanks #amina for the update)
Adding the solution here in the Answer Section though it is present in the comment section for the benefit of the community.
I used " tf.keras.losses.MeanSquaredLogarithmicError() " loss
function. It makes the amount of error smaller (because of Log )and
you can understand whether training is doing well or not.

How to give an image as input and get another image as output in keras tensorflow

I have zigsaw puzzle images and I have the corresponding pairs.I want to give the image as input to the model and find the corresponding pair of it.I have made the below model which achieves a bad accuracy of 30% while in the training.But when I pass the test images array it predicts an array having all nan values.Should I change my loss function? Please check the code below the image
in_shape=(32,256,256,3)
model1=models.Sequential(
[
resize_and_rescale,
layers.Conv2D(32,(3,3),activation="relu",input_shape=in_shape,padding='same'),
layers.Dropout(0.1),
layers.Conv2D(32,(3,3),activation="relu",input_shape=in_shape,padding='same'),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64, kernel_size = (3,3), activation='relu',padding='same'),
layers.Dropout(0.1),
layers.Conv2D(64,(3,3),activation="relu",input_shape=in_shape,padding='same'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(128, kernel_size = (3,3), activation='relu',padding='same'),
layers.Dropout(0.1),
layers.Conv2D(128,(3,3),activation="relu",input_shape=in_shape,padding='same'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(256, kernel_size = (3,3), activation='relu',padding='same'),
layers.MaxPooling2D((2, 2)),
layers.Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same'),
layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Dropout(0.2),
layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same'),
layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Dropout(0.2),
layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same'),
layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Dropout(0.2),
layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same'),
layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Dropout(0.2),
layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same'),
layers.Conv2D(3, (1, 1), activation='sigmoid')
]
)
model1.build(input_shape=in_shape)
model1.compile(
optimizer='adam',
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=False),
metrics=['accuracy']
)
If you're predicting pixel values [0, 255], then you'll want to change your last layer to:
layers.Conv2D(3, (1, 1), activation='linear')
A sigmoid activation function will try to force your outputs to a range of [0, 1], whereas a linear activation will allow for regression to pixel values of the range [0, 255], assuming that's what you want.

How to solve "No Algorithm Worked" Keras Error?

I tried to develop an FCN-16 model in Keras. I initialized the weights with similar FCN-16 model weights.
def FCN8 (nClasses, input_height=256, input_width=256):
## input_height and width must be devisible by 32 because maxpooling with filter size = (2,2) is operated 5 times,
## which makes the input_height and width 2^5 = 32 times smaller
assert input_height % 32 == 0
assert input_width % 32 == 0
IMAGE_ORDERING = "channels_last"
img_input = Input(shape=(input_height, input_width, 3)) ## Assume 224,224,3
## Block 1
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_1', data_format=IMAGE_ORDERING)(
img_input)
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_2', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool', data_format=IMAGE_ORDERING)(x)
f1 = x
# Block 2
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='conv2_1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='conv2_2', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool', data_format=IMAGE_ORDERING)(x)
f2 = x
# Block 3
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_3', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool', data_format=IMAGE_ORDERING)(x)
pool3 = x
# Block 4
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_3', data_format=IMAGE_ORDERING)(x)
pool4 = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool', data_format=IMAGE_ORDERING)(
x) ## (None, 14, 14, 512)
# Block 5
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_1', data_format=IMAGE_ORDERING)(pool4)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_3', data_format=IMAGE_ORDERING)(x)
pool5 = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool', data_format=IMAGE_ORDERING)(
x)
n = 4096
o = (Conv2D(n, (7, 7), activation='relu', padding='same', name="fc6", data_format=IMAGE_ORDERING))(pool5)
conv7 = (Conv2D(n, (1, 1), activation='relu', padding='same', name="fc7", data_format=IMAGE_ORDERING))(o)
conv7 = (Conv2D(nClasses, (1, 1), activation='relu', padding='same', name="conv7_1", data_format=IMAGE_ORDERING))(conv7)
conv7_4 = Conv2DTranspose(nClasses, kernel_size=(2, 2), strides=(2, 2), data_format=IMAGE_ORDERING)(
conv7)
pool411 = (
Conv2D(nClasses, (1, 1), activation='relu', padding='same', name="pool4_11",use_bias=False, data_format=IMAGE_ORDERING))(pool4)
o = Add(name="add")([pool411, conv7_4])
o = Conv2DTranspose(nClasses, kernel_size=(16, 16), strides=(16, 16), use_bias=False, data_format=IMAGE_ORDERING)(o)
o = (Activation('softmax'))(o)
GDI= Model(img_input, o)
GDI.load_weights(Model_Weights_path)
model = Model(img_input, o)
return model
Then I did train, test split and trying to run the model as:
from keras import optimizers
sgd = optimizers.SGD(lr=1E-2, momentum=0.91,decay=5**(-4), nesterov=True)
model.compile(optimizer='sgd',loss='categorical_crossentropy',metrics=['accuracy'],)
hist1 = model.fit(X_train,y_train,validation_data=(X_test,y_test),batch_size=32,epochs=1000,verbose=2)
model.save("/content/drive/My Drive/HCI_prep/new.h5")
But this code is throwing error in the first epoch:
NotFoundError: 2 root error(s) found.
(0) Not found: No algorithm worked!
[[{{node pool4_11_3/Conv2D}}]]
[[loss_4/mul/_629]]
(1) Not found: No algorithm worked!
[[{{node pool4_11_3/Conv2D}}]]
0 successful operations.
0 derived errors ignored.
add the following to your code:
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
And then restart the python kernel.
Had the same issue.
The padding='same' for MaxPooling didn't work for me.
I changed the color_mode parameter in the train and test generators from 'rgb' to 'grayscale' and then it worked for me.
This worked for me:
import tensorflow as tf
physical_devices = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
In my case, this was solved by ending all processes, that still allocated memory on one of the GPUs. Apparently, one of them did not finish (correctly). I did not have to change any code.
My problem was that I called the model with an input_shape of (?,28,28,1) and later called it with (?,28,28,3).
import tensorflow.keras
from tensorflow.keras.models import *
IMAGE_ORDERING = 'channels_last'
# take vgg-16 pretrained model from "https://github.com/fchollet/deep-learning-models" here
pretrained_url = "https://github.com/fchollet/deep-learning-models/" \
"releases/download/v0.1/" \
"vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5"
pretrained = 'imagenet' # 'imagenet' if weights need to be initialized!
"""
Function Name: get_vgg_encoder()
Functionalities: This function defines the VGG encoder part of the FCN network
and initialize this encoder part with VGG pretrained weights.
Parameter:input_height=224, input_width=224, pretrained=pretrained
Returns: final layer of every blocks as f1,f2,f3,f4,f5
"""
def get_vgg_encoder(input_height=224, input_width=224, pretrained=pretrained):
pad = 1
# heights and weights must be divided by 32, for fcn
assert input_height % 32 == 0
assert input_width % 32 == 0
img_input = Input(shape=(input_height, input_width, 3))
# Unlike base paper, stride=1 has not been used here, because
# Keras has default stride=1
x = (ZeroPadding2D((pad, pad), data_format=IMAGE_ORDERING))(img_input)
x = Conv2D(64, (3, 3), activation='relu', padding='valid', name='block1_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool', data_format=IMAGE_ORDERING)(x)
f1 = x
# Block 2
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool', data_format=IMAGE_ORDERING)(x)
f2 = x
# Block 3
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool', data_format=IMAGE_ORDERING)(x)
x = Dropout(0.5)(x)
f3 = x
# Block 4
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool', data_format=IMAGE_ORDERING)(x)
f4 = x
# Block 5
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2', data_format=IMAGE_ORDERING)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3', data_format=IMAGE_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool', data_format=IMAGE_ORDERING)(x)
# x= Dropout(0.5)(x)
f5 = x
# Check if weights are initialised, model is learning!
if pretrained == 'imagenet':
VGG_Weights_path = tensorflow.keras.utils.get_file(
pretrained_url.split("/")[-1], pretrained_url)
Model(img_input, x).load_weights(VGG_Weights_path)
return img_input, [f1, f2, f3, f4, f5]
"""
Function Name: fcn_16()
Functionalities: This function defines the Fully Convolutional part of the FCN network
and adds skip connections to build FCN-16 network
Parameter:n_classes, encoder=get_vgg_encoder, input_height=224,input_width=224
Returns: model
"""
def fcn_16(n_classes, encoder=get_vgg_encoder, input_height=224, input_width=224):
# Take levels from the base model, i.e. vgg
img_input, levels = encoder(input_height=input_height, input_width=input_width)
[f1, f2, f3, f4, f5] = levels
o = f5
# fcn6
o = (Conv2D(4096, (7, 7), activation='relu', padding='same', data_format=IMAGE_ORDERING))(o)
o = Dropout(0.5)(o)
# fc7
o = (Conv2D(4096, (1, 1), activation='relu', padding='same', data_format=IMAGE_ORDERING))(o)
o = Dropout(0.3)(o)
conv7 = (Conv2D(1, (1, 1), activation='relu', padding='same', name="score_sal", data_format=IMAGE_ORDERING))(o)
conv7_4 = Conv2DTranspose(1, kernel_size=(4, 4), strides=(2, 2), padding='same', name="upscore_sal2",
use_bias=False, data_format=IMAGE_ORDERING)(conv7)
pool411 = (
Conv2D(1, (1, 1), activation='relu', padding='same', name="score_pool4", data_format=IMAGE_ORDERING))(f4)
# Add a crop layer
o, o2 = crop(pool411, conv7_4, img_input)
# add skip connection
o = Add()([o, o2])
# 16 x upsample
o = Conv2DTranspose(n_classes, kernel_size=(32, 32), strides=(16, 16), use_bias=False, data_format=IMAGE_ORDERING)(
o)
# crop layer
## Caffe calls crop layer that takes o and img_input as argument, it takes their difference and crops
## But keras takes it as touple, I checked the size diff and put this value manually.
## output dim was 240 , input dim was 224. 240-224=16. so 16/2=8
score = Cropping2D(cropping=((8, 8), (8, 8)), data_format=IMAGE_ORDERING)(o)
o = (Activation('sigmoid'))(score)
model = Model(img_input, o)
model.model_name = "fcn_16"
return model
This error is quite general and basically indicates that "something" went wrong. As, the variety of answers suggest the error can arise from incompatibilities of the implementation with the underlying versions of keras/tensorflow, or the filter sizes are incorrect, or or or...
There is no single solution to this. For me, it also was an input shape issue. Instead of using rgb using grayscale worked as the network expected 1 channel.

What is the best way to use the architecture of defined models from tf.keras.applications for non-image dataset?

I'm trying to use models from tf.keras.applications such as VGG16 for my non-image data for my sequential classification task.
My X_train input shape = (# samples, window size, # columns)
Number of classes = 2
What would be the best way to copy architecture of the model and modify parameter details such as input shapes for input/hidden/output layers?
Thanks!
If you are looking for a quick way to find and modify the code that defines the architecture of VGG16 then looking at the source code of Keras would be the easiest one:
# Block 1
x = layers.Conv2D(64, (3, 3),
activation='relu',
padding='same',
name='block1_conv1')(img_input)
x = layers.Conv2D(64, (3, 3),
activation='relu',
padding='same',
name='block1_conv2')(x)
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
# Block 2
x = layers.Conv2D(128, (3, 3),
activation='relu',
padding='same',
name='block2_conv1')(x)
x = layers.Conv2D(128, (3, 3),
activation='relu',
padding='same',
name='block2_conv2')(x)
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)
# Block 3
x = layers.Conv2D(256, (3, 3),
activation='relu',
padding='same',
name='block3_conv1')(x)
x = layers.Conv2D(256, (3, 3),
activation='relu',
padding='same',
name='block3_conv2')(x)
x = layers.Conv2D(256, (3, 3),
activation='relu',
padding='same',
name='block3_conv3')(x)
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)
# Block 4
x = layers.Conv2D(512, (3, 3),
activation='relu',
padding='same',
name='block4_conv1')(x)
x = layers.Conv2D(512, (3, 3),
activation='relu',
padding='same',
name='block4_conv2')(x)
x = layers.Conv2D(512, (3, 3),
activation='relu',
padding='same',
name='block4_conv3')(x)
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)
# Block 5
x = layers.Conv2D(512, (3, 3),
activation='relu',
padding='same',
name='block5_conv1')(x)
x = layers.Conv2D(512, (3, 3),
activation='relu',
padding='same',
name='block5_conv2')(x)
x = layers.Conv2D(512, (3, 3),
activation='relu',
padding='same',
name='block5_conv3')(x)
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)
if include_top:
# Classification block
x = layers.Flatten(name='flatten')(x)
x = layers.Dense(4096, activation='relu', name='fc1')(x)
x = layers.Dense(4096, activation='relu', name='fc2')(x)
x = layers.Dense(classes, activation='softmax', name='predictions')(x)
else:
if pooling == 'avg':
x = layers.GlobalAveragePooling2D()(x)
elif pooling == 'max':
x = layers.GlobalMaxPooling2D()(x)
# Ensure that the model takes into account
# any potential predecessors of `input_tensor`.
if input_tensor is not None:
inputs = keras_utils.get_source_inputs(input_tensor)
else:
inputs = img_input
# Create model.
model = models.Model(inputs, x, name='vgg16')