Getting dynamic shape of tensors - tensorflow

I want to create a custom metric in Keras with tensorflow backend.
Say, we have a minimalized metric, where we want to get the shape of a dynamic shaped tensor. I'm trying to do this as below:
def metric(y_true, y_pred):
y_num = (tf.shape(y_true))[0]
K.get_session().run(y_num)
return anything
But I always get the error message:
InvalidArgumentError: You must feed a value for placeholder tensor 'conv2d_47_target' with dtype float and shape [?,?,?,?]
[[Node: conv2d_47_target = Placeholder[dtype=DT_FLOAT, shape=[?,?,?,?], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
[[Node: metrics/metric/strided_slice/_1079 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_11_metrics/metric/strided_slice", tensor_type=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
I can't figure out, where could be the problem.
And if there is some problem in my code, how can I still get the shape some other way?
Any help would be highly appreciated.
UPDATE:
Model code:
ACTIVATION = "relu"
def convolution_block(x, filters, size, strides=(1,1), padding='same', activation=True):
x = Conv2D(filters, size, strides=strides, padding=padding)(x)
x = BatchNormalization()(x)
if activation == True:
x = Activation(ACTIVATION)(x)
return x
def residual_block(blockInput, num_filters=16):
x = Activation(ACTIVATION)(blockInput)
x = BatchNormalization()(x)
x = convolution_block(x, num_filters, (3,3) )
x = convolution_block(x, num_filters, (3,3), activation=False)
x = Add()([x, blockInput])
return x
# Build model
def build_model(input_layer, start_neurons, DropoutRatio = 0.5):
# 101 -> 50
conv1 = Conv2D(start_neurons * 1, (3, 3), activation=None, padding="same")(input_layer)
conv1 = residual_block(conv1,start_neurons * 1)
conv1 = residual_block(conv1,start_neurons * 1)
conv1 = Activation(ACTIVATION)(conv1)
pool1 = MaxPooling2D((2, 2))(conv1)
pool1 = Dropout(DropoutRatio/2)(pool1)
# 50 -> 25
conv2 = Conv2D(start_neurons * 2, (3, 3), activation=None, padding="same")(pool1)
conv2 = residual_block(conv2,start_neurons * 2)
conv2 = residual_block(conv2,start_neurons * 2)
conv2 = Activation(ACTIVATION)(conv2)
pool2 = MaxPooling2D((2, 2))(conv2)
pool2 = Dropout(DropoutRatio)(pool2)
# 25 -> 12
conv3 = Conv2D(start_neurons * 4, (3, 3), activation=None, padding="same")(pool2)
conv3 = residual_block(conv3,start_neurons * 4)
conv3 = residual_block(conv3,start_neurons * 4)
conv3 = Activation(ACTIVATION)(conv3)
pool3 = MaxPooling2D((2, 2))(conv3)
pool3 = Dropout(DropoutRatio)(pool3)
# 12 -> 6
conv4 = Conv2D(start_neurons * 8, (3, 3), activation=None, padding="same")(pool3)
conv4 = residual_block(conv4,start_neurons * 8)
conv4 = residual_block(conv4,start_neurons * 8)
conv4 = Activation(ACTIVATION)(conv4)
pool4 = MaxPooling2D((2, 2))(conv4)
pool4 = Dropout(DropoutRatio)(pool4)
# Middle
convm = Conv2D(start_neurons * 16, (3, 3), activation=None, padding="same")(pool4)
convm = residual_block(convm,start_neurons * 16)
convm = residual_block(convm,start_neurons * 16)
convm = Activation(ACTIVATION)(convm)
# 6 -> 12
deconv4 = Conv2DTranspose(start_neurons * 8, (3, 3), strides=(2, 2), padding="same")(convm)
uconv4 = concatenate([deconv4, conv4])
uconv4 = Dropout(DropoutRatio)(uconv4)
uconv4 = Conv2D(start_neurons * 8, (3, 3), activation=None, padding="same")(uconv4)
uconv4 = residual_block(uconv4,start_neurons * 8)
uconv4 = residual_block(uconv4,start_neurons * 8)
uconv4 = Activation(ACTIVATION)(uconv4)
# 12 -> 25
#deconv3 = Conv2DTranspose(start_neurons * 4, (3, 3), strides=(2, 2), padding="same")(uconv4)
deconv3 = Conv2DTranspose(start_neurons * 4, (3, 3), strides=(2, 2), padding="valid")(uconv4)
uconv3 = concatenate([deconv3, conv3])
uconv3 = Dropout(DropoutRatio)(uconv3)
uconv3 = Conv2D(start_neurons * 4, (3, 3), activation=None, padding="same")(uconv3)
uconv3 = residual_block(uconv3,start_neurons * 4)
uconv3 = residual_block(uconv3,start_neurons * 4)
uconv3 = Activation(ACTIVATION)(uconv3)
# 25 -> 50
deconv2 = Conv2DTranspose(start_neurons * 2, (3, 3), strides=(2, 2), padding="same")(uconv3)
uconv2 = concatenate([deconv2, conv2])
uconv2 = Dropout(DropoutRatio)(uconv2)
uconv2 = Conv2D(start_neurons * 2, (3, 3), activation=None, padding="same")(uconv2)
uconv2 = residual_block(uconv2,start_neurons * 2)
uconv2 = residual_block(uconv2,start_neurons * 2)
uconv2 = Activation(ACTIVATION)(uconv2)
# 50 -> 101
#deconv1 = Conv2DTranspose(start_neurons * 1, (3, 3), strides=(2, 2), padding="same")(uconv2)
deconv1 = Conv2DTranspose(start_neurons * 1, (3, 3), strides=(2, 2), padding="valid")(uconv2)
uconv1 = concatenate([deconv1, conv1])
uconv1 = Dropout(DropoutRatio)(uconv1)
uconv1 = Conv2D(start_neurons * 1, (3, 3), activation=None, padding="same")(uconv1)
uconv1 = residual_block(uconv1,start_neurons * 1)
uconv1 = residual_block(uconv1,start_neurons * 1)
uconv1 = Activation(ACTIVATION)(uconv1)
uconv1 = Dropout(DropoutRatio/2)(uconv1)
output_layer = Conv2D(1, (1,1), padding="same", activation="sigmoid")(uconv1)
#output_layer = Conv2D(1, (1,1), padding="same", activation="sigmoid")(uconv1)
return output_layer

Related

Simultaneously training two CNN models

I wish to train two CNN models in the same training loop where the input of the second model is the feature map generated by the forward pass on the first model.
def custom_training(inputd, x_train, y_train, x_val, y_val, n_epochs):
optimizer = tf.keras.optimizers.Adam()
ce_loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)
loss_train = np.zeros(shape=(n_epochs,), dtype=np.float32)
acc_train = np.zeros(shape=(n_epochs,), dtype=np.float32)
loss_val = np.zeros(shape=(n_epochs,))
acc_val = np.zeros(shape=(n_epochs,))
unet_model = tf.keras.Model(inputd, build_unet(inputd, 16))
#ds_model = DS_module()
n_batches = len(generator_train)
print(n_batches)
for epoch in range(n_epochs):
epoch_loss_avg = tf.keras.metrics.Mean() # Keeping track of the training loss
epoch_acc_avg = tf.keras.metrics.Mean() # Keeping track of the training accuracy
for batch in range(n_batches):
x, y = generator_train[batch]
with tf.GradientTape() as tape: # Forward pass
y_ = unet_model(x, training=True)
loss = ce_loss(y_true=y, y_pred=y_)
k = compute_K(y_)
print(k)
ds_model = tf.keras.Model(y_, DS_module(y_, k, num_class))
grad = tape.gradient(loss, unet_model.trainable_variables) # Backpropagation
optimizer.apply_gradients(zip(grad, unet_model.trainable_variables)) # Update network weights
epoch_loss_avg(loss)
epoch_acc_avg(accuracy_score(y_true=y, y_pred=np.argmax(y_, axis=-1)))
#generator.on_epoch_end()
loss_train[epoch] = epoch_loss_avg.result()
acc_train[epoch] = epoch_acc_avg.result()
y_ = unet_model.predict(x_val) # Validation predictions
loss_val[epoch] = ce_loss(y_true=y_val, y_pred=y_).numpy()
acc_val[epoch] = accuracy_score(y_true=y_val, y_pred=np.argmax(y_, axis=-1))
Below are the content of the two models, which consist of Unet, and another convolutional model, which will take as input the feature map generated by the Unet model:
def build_unet(input_layer, start_neurons):
# 128 -> 64
conv1 = Conv2D(start_neurons * 1, (3, 3), activation="relu", padding="same")(input_layer)
conv1 = Conv2D(start_neurons * 1, (3, 3), activation="relu", padding="same")(conv1)
pool1 = MaxPooling2D((2, 2))(conv1)
pool1 = Dropout(0.25)(pool1)
# 64 -> 32
conv2 = Conv2D(start_neurons * 2, (3, 3), activation="relu", padding="same")(pool1)
conv2 = Conv2D(start_neurons * 2, (3, 3), activation="relu", padding="same")(conv2)
pool2 = MaxPooling2D((2, 2))(conv2)
pool2 = Dropout(0.5)(pool2)
# 32 -> 16
conv3 = Conv2D(start_neurons * 4, (3, 3), activation="relu", padding="same")(pool2)
conv3 = Conv2D(start_neurons * 4, (3, 3), activation="relu", padding="same")(conv3)
pool3 = MaxPooling2D((2, 2))(conv3)
pool3 = Dropout(0.5)(pool3)
# 16 -> 8
conv4 = Conv2D(start_neurons * 8, (3, 3), activation="relu", padding="same")(pool3)
conv4 = Conv2D(start_neurons * 8, (3, 3), activation="relu", padding="same")(conv4)
pool4 = MaxPooling2D((2, 2))(conv4)
pool4 = Dropout(0.5)(pool4)
# Middle
convm = Conv2D(start_neurons * 16, (3, 3), activation="relu", padding="same")(pool4)
convm = Conv2D(start_neurons * 16, (3, 3), activation="relu", padding="same")(convm)
# 8 -> 16
deconv4 = Conv2DTranspose(start_neurons * 8, (3, 3), strides=(2, 2), padding="same")(convm)
uconv4 = concatenate([deconv4, conv4])
uconv4 = Dropout(0.5)(uconv4)
uconv4 = Conv2D(start_neurons * 8, (3, 3), activation="relu", padding="same")(uconv4)
uconv4 = Conv2D(start_neurons * 8, (3, 3), activation="relu", padding="same")(uconv4)
# 16 -> 32
deconv3 = Conv2DTranspose(start_neurons * 4, (3, 3), strides=(2, 2), padding="same")(uconv4)
uconv3 = concatenate([deconv3, conv3])
uconv3 = Dropout(0.5)(uconv3)
uconv3 = Conv2D(start_neurons * 4, (3, 3), activation="relu", padding="same")(uconv3)
uconv3 = Conv2D(start_neurons * 4, (3, 3), activation="relu", padding="same")(uconv3)
# 32 -> 64
deconv2 = Conv2DTranspose(start_neurons * 2, (3, 3), strides=(2, 2), padding="same")(uconv3)
uconv2 = concatenate([deconv2, conv2])
uconv2 = Dropout(0.5)(uconv2)
uconv2 = Conv2D(start_neurons * 2, (3, 3), activation="relu", padding="same")(uconv2)
uconv2 = Conv2D(start_neurons * 2, (3, 3), activation="relu", padding="same")(uconv2)
# 64 -> 128
deconv1 = Conv2DTranspose(start_neurons * 1, (3, 3), strides=(2, 2), padding="same")(uconv2)
uconv1 = concatenate([deconv1, conv1])
uconv1 = Dropout(0.5)(uconv1)
uconv1 = Conv2D(start_neurons * 1, (3, 3), activation="relu", padding="same")(uconv1)
uconv1 = Conv2D(start_neurons * 1, (3, 3), activation="relu", padding="same")(uconv1)
feature_maps = Conv2D(start_neurons, (1, 1), activation='relu')(uconv1)
return feature_maps
#tf.function
def DS_module(feature_map, prototypes, num_class):
ED=DS1(prototypes, 16)(feature_map)
ED_ac=DS1_activate(prototypes)(ED)
mass_prototypes=DS2(prototypes, num_class)(ED_ac)
mass_prototypes_omega=DS2_omega(prototypes, num_class)(mass_prototypes)
mass_Dempster=DS3_Dempster(prototypes, num_class)(mass_prototypes_omega)
pignistic=DM_pignistic(num_class)(mass_Dempster)
pignistic=DS3_normalize()(pignistic)
outputs = pignistic
return outputs
When I execute this code, i obtain the following error:
Error message obtained
This is the content of compute_k function, it executes PCA, Kmeans and Silhouette score on the feature map.
def compute_K(featureMap):
X = tf.reshape(featureMap, [featureMap.shape[0]*featureMap.shape[1]*featureMap.shape[2], featureMap.shape[3]])
pca = PCA(n_components=2)
principalComponents = pca.fit_transform(X)
principalDf = pd.DataFrame(data = principalComponents, columns = ['principal component 1', 'principal component 2'])
X = principalDf.to_numpy()
print(len(X))
#range_n_clusters = [3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 15, 16, 17, 18, 19, 20]
range_n_clusters = [3, 4, 5, 6, 7]
silhouette_avg_n_clusters = []
max_sillhouette = 0
max_k = 0
plt.figure(figsize=(15, 7))
plt.subplot(111)
for n_clusters in range_n_clusters:
# Initialize the clusterer with n_clusters value and a random generator
# seed of 10 for reproducibility.
clusterer = KMeans(n_clusters=n_clusters, random_state=42)
cluster_labels = clusterer.fit_predict(X)
silhouette_avg = silhouette_score(X, cluster_labels)
print("For n_clusters =", n_clusters,
"The average silhouette_score is :", silhouette_avg)
silhouette_avg_n_clusters.append(silhouette_avg)
if (n_clusters == 2):
max_sillhouette = silhouette_avg
max_k = n_clusters
if (max_sillhouette< silhouette_avg):
max_sillhouette = silhouette_avg
max_k = n_clusters
return max_k

MutableGraphView::SortTopologically error: detected edge(s) creating cycle(s)

2022󈚩󈚶 13:53:37.492047: E tensorflow/core/grappler/optimizers/meta_optimizer.cc:828] layout failed: INVALID_ARGUMENT: MutableGraphView::SortTopologically error: detected edge(s) creating cycle(s) {'sequential/conv_lstm2d_3/while/body/_675/sequential/conv_lstm2d_3/while/mul_6' ‑> 'sequential/conv_lstm2d_3/while/body/_675/sequential/conv_lstm2d_3/while/add_5', 'sequential/conv_lstm2d_3/while/body/_675/sequential/conv_lstm2d_3/while/clip_by_value' ‑> 'sequential/conv_lstm2d_3/while/body/_675/sequential/conv_lstm2d_3/while/mul_7', 'sequential/conv_lstm2d_3/while/body/_675/sequential/conv_lstm2d_3/while/convolution_6' ‑> 'sequential/conv_lstm2d_3/while/body/_675/sequential/conv_lstm2d_3/while/add_4', 'sequential/conv_lstm2d_3/while/body/_675/sequential/conv_lstm2d_3/while/clip_by_value_2' ‑> 'sequential/conv_lstm2d_3/while/body/_675/sequential/conv_lstm2d_3/while/mul_9', 'sequential/conv_lstm2d_2/while/body/_447/sequential/conv_lstm2d_2/while/mul_6' ‑> 'sequential/conv_lstm2d_2/while/body/_447/sequential/conv_lstm2d_2/while/add_5', 'sequential/conv_lstm2d_2/while/body/_447/sequential/conv_lstm2d_2/while/clip_by_value_2' ‑> 'sequential/conv_lstm2d_2/while/body/_447/sequential/conv_lstm2d_2/while/mul_9', 'sequential/conv_lstm2d_2/while/body/_447/sequential/conv_lstm2d_2/while/clip_by_value' ‑> 'sequential/conv_lstm2d_2/while/body/_447/sequential/conv_lstm2d_2/while/mul_7', 'sequential/conv_lstm2d_2/while/body/_447/sequential/conv_lstm2d_2/while/convolution_6' ‑> 'sequential/conv_lstm2d_2/while/body/_447/sequential/conv_lstm2d_2/while/add_4'}.
model.add(ConvLSTM2D(filters = 4, kernel_size = (3, 3), activation = 'tanh',data_format = "channels_last",
recurrent_dropout=0.2, return_sequences=True, input_shape = (SEQUENCE_LENGTH,
IMAGE_HEIGHT, IMAGE_WIDTH, 3)))
model.add(MaxPooling3D(pool_size=(1, 2, 2), padding='same', data_format='channels_last'))
model.add(TimeDistributed(Dropout(0.2)))
model.add(ConvLSTM2D(filters = 8, kernel_size = (3, 3), activation = 'tanh', data_format = "channels_last",
recurrent_dropout=0.2, return_sequences=True))
model.add(MaxPooling3D(pool_size=(1, 2, 2), padding='same', data_format='channels_last'))
model.add(TimeDistributed(Dropout(0.2)))
model.add(ConvLSTM2D(filters = 14, kernel_size = (3, 3), activation = 'tanh', data_format = "channels_last",
recurrent_dropout=0.2, return_sequences=True))
model.add(MaxPooling3D(pool_size=(1, 2, 2), padding='same', data_format='channels_last'))
model.add(TimeDistributed(Dropout(0.2)))
model.add(ConvLSTM2D(filters = 16, kernel_size = (3, 3), activation = 'tanh', data_format = "channels_last",
recurrent_dropout=0.2, return_sequences=True))
model.add(MaxPooling3D(pool_size=(1, 2, 2), padding='same', data_format='channels_last'))
#model.add(TimeDistributed(Dropout(0.2)))
model.add(Flatten())
model.add(Dense(len(CLASSES_LIST), activation = "softmax"))$

implementing unpooling layer in u-net, InvalidArgumentError is occurred

I am using EM_dataset segmentation,
keras2.3.1 and tensorflow 2,
in google colab
Here is my code. This is a U-net.
def unpool(pool, ind, ksize=[1,2,2,1], scope='unpool'):
"""
Unpooling layer after max_pool_with_argmax.
Args:
pool: max pooled output tensor
ind: argmax indices
ksize: ksize is the same as for the pool
Return:
ret: unpooling tensor
"""
with tf.compat.v1.variable_scope(scope):
input_shape = tf.shape(pool)
output_shape = [input_shape[0], input_shape[1] * ksize[1], input_shape[2] * ksize[2], input_shape[3]]
flat_input_size = tf.reduce_prod(input_shape)
flat_output_shape = [output_shape[0], output_shape[1] * output_shape[2] * output_shape[3]]
pool_ = tf.reshape(pool, [flat_input_size])
batch_range = tf.reshape(tf.range(tf.cast(output_shape[0], tf.int64), dtype=ind.dtype), shape=[input_shape[0], 1, 1, 1])
b = tf.ones_like(ind) * batch_range
b1 = tf.reshape(b, [flat_input_size, 1])
ind_ = tf.reshape(ind, [flat_input_size, 1])
ind_ = tf.concat([b1, ind_], 1)
ret = tf.scatter_nd(ind_, pool_, shape=tf.cast(flat_output_shape, tf.int64))
ret = tf.reshape(ret, output_shape)
set_input_shape = pool.get_shape()
set_output_shape = [set_input_shape[0], set_input_shape[1] * ksize[1], set_input_shape[2] * ksize[2], set_input_shape[3]]
ret.set_shape(set_output_shape)
#print(set_output_shape)
return ret
also I am using ImageDataGenerator in keras
inputs = Input(shape=(160, 160, 1))
# encorder
c1 = layers.BatchNormalization()(inputs)
c1 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(c1)
c1 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(c1)
#p1 = layers.MaxPooling2D((2, 2))(c1) # 80 80
p1, argmax1 = tf.nn.max_pool_with_argmax(input=c1, ksize=(1,2,2,1), strides=(1,2,2,1), padding='SAME')
c2 = layers.BatchNormalization()(p1)
c2 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(c2)
c2 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(c2)
#p2 = layers.MaxPooling2D((2, 2))(c2) # 40 40
p2, argmax2 = tf.nn.max_pool_with_argmax(input=c2, ksize=(1,2,2,1), strides=(1,2,2,1), padding='SAME')
c3 = layers.BatchNormalization()(p2)
c3 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(c3)
c3 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(c3)
#p3 = layers.MaxPooling2D((2, 2))(c3) # 20 20
p3, argmax3 = tf.nn.max_pool_with_argmax(input=c3, ksize=(1,2,2,1), strides=(1,2,2,1), padding='SAME')
c4 = layers.BatchNormalization()(p3)
c4 = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(c4)
c4 = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(c4)
#p4 = layers.MaxPooling2D(pool_size=(2, 2))(c4) # 10 10
p4, argmax4 = tf.nn.max_pool_with_argmax(input=c4, ksize=(1,2,2,1), strides=(1,2,2,1), padding='SAME')
c5 = layers.BatchNormalization()(p4)
c5 = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(c5)
c5 = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(c5)
#p5 = layers.MaxPooling2D(pool_size=(2, 2))(c5) # 5 5
p5, argmax5 = tf.nn.max_pool_with_argmax(input=c5, ksize=(1,2,2,1), strides=(1,2,2,1), padding='SAME')
c55 = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(p5)
c55 = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(c55)
u6 = layers.concatenate([unpool(c55, argmax5), c5])
c6 = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(u6)
c6 = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(c6)
u71 = layers.concatenate([unpool(c6,argmax4), c4])
c71 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(u71)
c61 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(c71)
u7 = layers.concatenate([unpool(c61,argmax3), c3])
c7 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(u7)
c7 = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(c7)
u8 = layers.concatenate([unpool(c7,argmax2), c2])
c8 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(u8)
c8 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(c8)
u9 = layers.concatenate([unpool(c8,argmax1), c1], axis=3)
c9 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(u9)
c9 = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(c9)
outputs = layers.Conv2D(1, (1, 1), activation='sigmoid')(c9)
Network is fine but If i do fit, Error is occured
InvalidArgumentError: Input to reshape is a tensor with 12800 values, but the requested shape has 25600
[[node functional_33/tf_op_layer_Reshape_393/Reshape_393 (defined at :4) ]] [Op:__inference_train_function_56708]
I don't know why this error is occured...
please some help...
As the error suggests that it requires input image size to be 160x160 = 25600 values. but the images given input to the model aren't of the required size. you can resize all the images that are required to fit into the model to 160x160x1 and then fit them to model, it should work then.

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')