Simultaneously training two CNN models - tensorflow

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

Related

Getting Cuda error if I use BCE loss and works fine with BCEWithLogitsLoss but getting huge losses

I am working on SRGAN model and I created the model and used MSE loss and BCE loss functions to calculate image losses.
If I use BCEWithLogitsLoss, then the code works but theres huge losses. But I tried using BCE loss and it throws CUDA error. Can someone suggest me where did i went wrong and how can I fix the cuda error.
Optimizers are Adam with learning rate 0.0001
Attached the generator and discriminator
class Generator(nn.Module):
def __init__(self, no_of_blocks=16):
super(Generator, self).__init__()
#First layer
self.CV_Block1 = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=9, stride=1, padding=4),
nn.PReLU())
#Residual Blocks
Blocks = []
for _ in range(no_of_blocks):
Blocks.append(Residual_Block(64))
self.Blocks = nn.Sequential(*Blocks)
# Second convolution layer
self.CV_Block3 = nn.Sequential(
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(64, 0.8)
)
# self.Upsample = nn.Sequential(
# UP_Sampling(64, 2),
# UP_Sampling(64, 2)
# )
upsampling = []
for _ in range(2):
upsampling.append(UP_Sampling(64))
self.upsampling = nn.Sequential(*upsampling)
#Final Layer
self.CV_Block_end = nn.Sequential(
nn.Conv2d(64, 3, kernel_size=9, stride=1, padding=4),
nn.Tanh()
)
# self._initialize_weights()
def forward(self, x): #: torch.Tensor
CV1_out = self.CV_Block1(x)
Res_Out = self.Blocks(CV1_out)
CV3_Out = self.CV_Block3(Res_Out)
out = torch.add(CV1_out, CV3_Out)
out = self.upsampling(out)
out = self.CV_Block_end(out)
# out = torch.clamp_(out, 0.0, 1.0)
# gen_out = self.CV_Block_end(x)
return out
class Discriminator(nn.Module):
def __init__(self,input_shape):
super(Discriminator, self).__init__()
# self.input_shape = input_shape
# in_height, in_width = self.input_shape
# patch_h, patch_w = int(in_height / 2 ** 4), int(in_width / 2 ** 4)
# self.output_shape = (1, patch_h, patch_w)
self.features = nn.Sequential(
# input size. (3) x 96 x 96
nn.Conv2d(3, 64, (3, 3), (1, 1), (1, 1), bias=True),
nn.LeakyReLU(0.2, True),
# state size. (64) x 48 x 48
nn.Conv2d(64, 64, (3, 3), (2, 2), (1, 1), bias=False),
nn.BatchNorm2d(64),
nn.LeakyReLU(0.2, True),
nn.Conv2d(64, 128, (3, 3), (1, 1), (1, 1), bias=False),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2, True),
# state size. (128) x 24 x 24
nn.Conv2d(128, 128, (3, 3), (2, 2), (1, 1), bias=False),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2, True),
nn.Conv2d(128, 256, (3, 3), (1, 1), (1, 1), bias=False),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2, True),
# state size. (256) x 12 x 12
nn.Conv2d(256, 256, (3, 3), (2, 2), (1, 1), bias=False),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2, True),
nn.Conv2d(256, 512, (3, 3), (1, 1), (1, 1), bias=False),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.2, True),
# state size. (512) x 6 x 6
nn.Conv2d(512, 512, (3, 3), (2, 2), (1, 1), bias=False),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.2, True),
)
self.classifier = nn.Sequential(
nn.Linear(512 * 6 * 6, 1024),
nn.LeakyReLU(0.2, True),
nn.Linear(1024, 1),
nn.Sigmoid(),
)
def forward(self, x):
out = self.features(x)
# out = torch.flatten(out, 1)
# out = self.classifier(out)
return out
Error "device-side assert triggered" triggered inside BCELoss when inputs not in [0..1] (so it not a probabilities).
You got it because remove block classifier block with nn.Sigmoid
# out = self.classifier(out)
There a similar question:
loss.backward() gives RuntimeError: CUDA error: device-side assert triggered

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"))$

how to reduce as much as possible the bootleneck in autoencoder?

dears
i have the following code:
inpt = Input(shape=(160,1))
# Input is 160 samples, 20 ms for sampling rate of 8 kHz
# Of course speech can be wide-band. One should take care then
conv1 = Convolution1D(512,3,activation='relu',padding='same',strides=1)(inpt)
conv2 = Convolution1D(128,3,activation='relu',padding='same',strides=1)(conv1)
pool1 = MaxPooling1D(pool_size=2, strides=None, padding='valid')(conv2)
conv3 = Convolution1D(256,3,activation='relu',padding='same',strides=1)(pool1)
conv4 = Convolution1D(256,3,activation='relu',padding='same',strides=1)(conv3)
pool2 = MaxPooling1D(pool_size=2, strides=None, padding='valid')(conv4)
conv5 = Convolution1D(256,3,activation='relu',padding='same',strides=1)(pool2)
conv6 = Convolution1D(128,3,activation='relu',padding='same',strides=1)(conv5)
pool3 = MaxPooling1D(pool_size=2, strides=None, padding='valid')(conv6)
conv7 = Convolution1D(128,3,activation='relu',padding='same',strides=1)(pool3)
conv8 = Convolution1D(64,3,activation='relu',padding='same',strides=1)(conv7)
pool4 = MaxPooling1D(pool_size=2, strides=None, padding='valid')(conv8)
conv9 = Convolution1D(32,3,activation='relu',padding='same',strides=1)(pool4)
conv10 = Convolution1D(16,3,activation='relu',padding='same',strides=1)(conv9)
############################# EXTRA
conv10 = Convolution1D( 8, kernel_size = (3), activation='relu', padding='same')(conv10)
pool4 = MaxPooling1D(pool_size = (2), padding='same')(conv10)
conv10 = Convolution1D( 8, 3, activation='relu', padding='same')(pool4)
encoded = Convolution1D( 8, 3, activation='relu', padding='same')(conv10)
#############
the bottleneck here has length 6920 if the input is 27000 signal
I want to reduce the bottlenack into only 400 , how to do that , the amendement sould start from the part extra
I tried to add extra conv and pool but the length can't less than 6920.
There are many different ways you could get the length you want:
Increase the pooling sizes along the way:
pool = MaxPooling1D(pool_size = (4))(prev) # or you could use higher numbers
Use VALID padding in Conv and Pool layers:
pool = MaxPooling1D(pool_size = (4), padding='valid')(prev)
conv10 = Convolution1D( 8, 3, activation='relu', padding='valid')(prev)
You could also use higher stride sizes in Pool and Conv layer
pool = MaxPooling1D(pool_size = (4), strides=4, padding='valid')(prev)
conv10 = Convolution1D( 8, 3, strides=4, activation='relu', padding='valid')(prev)
I created a draft for you as follows:
an encoder taking inputs of shape (batch_size, 160, 1), outputting vectors of shape (batch_size, 1, 4)
a decoder taking inputs of shape (batch_size, 1, 4), same as encoder output
a combined encoder_decoder model
The encoder:
from tensorflow.keras.layers import Input, Convolution1D, MaxPooling1D, GlobalAveragePooling1D, UpSampling1D
import tensorflow as tf
inpt = Input(shape=(160,1))
# Input is 160 samples, 20 ms for sampling rate of 8 kHz
# Of course speech can be wide-band. One should take care then
conv1 = Convolution1D(512,3,activation='relu',padding='same',strides=1)(inpt)
conv2 = Convolution1D(128,3,activation='relu',padding='same',strides=1)(conv1)
pool1 = MaxPooling1D(pool_size=2, strides=None, padding='valid')(conv2)
conv3 = Convolution1D(256,3,activation='relu',padding='same',strides=1)(pool1)
conv4 = Convolution1D(256,3,activation='relu',padding='same',strides=1)(conv3)
pool2 = MaxPooling1D(pool_size=2, strides=None, padding='valid')(conv4)
conv5 = Convolution1D(256,3,activation='relu',padding='same',strides=1)(pool2)
conv6 = Convolution1D(128,3,activation='relu',padding='same',strides=1)(conv5)
pool3 = MaxPooling1D(pool_size=2, strides=None, padding='valid')(conv6)
conv7 = Convolution1D(128,3,activation='relu',padding='same',strides=1)(pool3)
conv8 = Convolution1D(64,3,activation='relu',padding='same',strides=1)(conv7)
pool4 = MaxPooling1D(pool_size=6, strides=None, padding='valid')(conv8)
conv9 = Convolution1D(32,3,activation='relu',padding='same',strides=1)(pool4)
conv10 = Convolution1D(4,3,activation='relu',padding='same',strides=1)(conv9)
encoded = MaxPooling1D(pool_size=3)(conv10)
encoder = tf.keras.Model(inputs=inpt, outputs=encoded)
encoder.summary()
The decoder:
input_decoder = Input(shape = (1, 4) ) #############
upsmp1 = UpSampling1D(size=2)(input_decoder)
conv11 = Convolution1D( 4, 3, activation='relu', padding='same')(upsmp1)
upsmp1 = UpSampling1D(size=8)(conv11)
conv11 = Convolution1D( 8, 3, activation='relu', padding='same')(upsmp1)
conv12 = Convolution1D( 8, 3, activation='relu', padding='same')(conv11)
pool4 = UpSampling1D(size=10)(conv12)
conv10 = Convolution1D( 1, kernel_size = (3), activation='relu', padding='same')(pool4)
decoder = tf.keras.Model(inputs=input_decoder, outputs=conv10)
decoder.summary()
The combined encoder decoder:
encoder_decoder = tf.keras.Model(inputs=inpt, outputs=decoder(encoded))
encoder_decoder.summary()

Getting dynamic shape of tensors

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

Keras: Error with Training. Dimension is not what is expected

I am developing a project where I am trying to finetune the last layer of a VGG-FACE model.
But everytime when I try to do the fitting stage I've got the same error:
Traceback (most recent call last):
File "freeze_2.py", line 258, in <module>
model=entrenamos_modelo('vggface_weights_tensorflow.h5')
File "freeze_2.py", line 168, in entrenamos_modelo
model2.fit(train_data,label, nb_epoch=nb_epoch, batch_size=64)
File "/imatge/psereno/workspace/venv-tfg/local/lib/python2.7/site-packages/keras/engine/training.py", line 1057, in fit
batch_size=batch_size)
File "/imatge/psereno/workspace/venv-tfg/local/lib/python2.7/site-packages/keras/engine/training.py", line 984, in _standardize_user_data
exception_prefix='model input')
File "/imatge/psereno/workspace/venv-tfg/local/lib/python2.7/site-packages/keras/engine/training.py", line 111, in standardize_input_data
str(array.shape))
Exception: Error when checking model input: expected input_2 to have shape (None, 3, 224, 224) but got array with shape (1576, 4096, 1, 1)
Here is the code I am using:
from keras.models import Model
from keras.layers import Input, Convolution2D, ZeroPadding2D, MaxPooling2D, Flatten, Dropout, Activation
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Dropout
from keras.optimizers import SGD
from keras.layers import merge
from keras.models import Merge
import cv2, numpy as np
from PIL import Image
from keras.preprocessing.image import ImageDataGenerator
'''OJOOOOO: PARA QUE FUNCIONE BIEN:
nano ~/.keras/keras.json
{
"image_dim_ordering": "th",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}
'''
def bottleneck():
datagen = ImageDataGenerator(rescale=1.)
generator = datagen.flow_from_directory(train_data_dir,
target_size=(img_width, img_height),
batch_size=32,
class_mode=None,
shuffle=False)
pad1_1 = ZeroPadding2D(padding=(1, 1), name='in_train')(img)
conv1_1 = Convolution2D(64, 3, 3, activation='relu', name='conv1_1')(pad1_1)
pad1_2 = ZeroPadding2D(padding=(1, 1))(conv1_1)
conv1_2 = Convolution2D(64, 3, 3, activation='relu', name='conv1_2')(pad1_2)
pool1 = MaxPooling2D((2, 2), strides=(2, 2))(conv1_2)
pad2_1 = ZeroPadding2D((1, 1), trainable=False)(pool1)
conv2_1 = Convolution2D(128, 3, 3, activation='relu', name='conv2_1')(pad2_1)
pad2_2 = ZeroPadding2D((1, 1), trainable=False)(conv2_1)
conv2_2 = Convolution2D(128, 3, 3, activation='relu', name='conv2_2')(pad2_2)
pool2 = MaxPooling2D((2, 2), strides=(2, 2), trainable=False)(conv2_2)
pad3_1 = ZeroPadding2D((1, 1))(pool2)
conv3_1 = Convolution2D(256, 3, 3, activation='relu', name='conv3_1')(pad3_1)
pad3_2 = ZeroPadding2D((1, 1))(conv3_1)
conv3_2 = Convolution2D(256, 3, 3, activation='relu', name='conv3_2')(pad3_2)
pad3_3 = ZeroPadding2D((1, 1))(conv3_2)
conv3_3 = Convolution2D(256, 3, 3, activation='relu', name='conv3_3')(pad3_3)
pool3 = MaxPooling2D((2, 2), strides=(2, 2), trainable=False)(conv3_3)
pad4_1 = ZeroPadding2D((1, 1))(pool3)
conv4_1 = Convolution2D(512, 3, 3, activation='relu', name='conv4_1')(pad4_1)
pad4_2 = ZeroPadding2D((1, 1))(conv4_1)
conv4_2 = Convolution2D(512, 3, 3, activation='relu', name='conv4_2')(pad4_2)
pad4_3 = ZeroPadding2D((1, 1))(conv4_2)
conv4_3 = Convolution2D(512, 3, 3, activation='relu', name='conv4_3')(pad4_3)
pool4 = MaxPooling2D((2, 2), strides=(2, 2))(conv4_3)
pad5_1 = ZeroPadding2D((1, 1))(pool4)
conv5_1 = Convolution2D(512, 3, 3, activation='relu', name='conv5_1')(pad5_1)
pad5_2 = ZeroPadding2D((1, 1))(conv5_1)
conv5_2 = Convolution2D(512, 3, 3, activation='relu', name='conv5_2') (pad5_2)
pad5_3 = ZeroPadding2D((1, 1))(conv5_2)
conv5_3 = Convolution2D(512, 3, 3, activation='relu', name='conv5_3')(pad5_3)
pool5 = MaxPooling2D((2, 2), strides=(2, 2))(conv5_3)
fc6 = Convolution2D(4096, 7, 7, activation='relu', name='fc6')(pool5)
fc6_drop = Dropout(0.5)(fc6)
model = Model(input=img, output=fc6_drop)
bottleneck_features_train = model.predict_generator(generator, nb_train_samples)
np.save(open('features.npy', 'w'), bottleneck_features_train)
def entrenamos_modelo(weights_path=None):
train_data = np.load(open('features.npy'))
print(train_data.shape)
train_labels = np.array(
[0] * (nb_train_samples / 8) + [1] * (nb_train_samples / 8) + [2] * (nb_train_samples / 8) + [3] * (
nb_train_samples / 8) + [4] * (nb_train_samples / 8) + [5] * (nb_train_samples / 8) + [6] * (
nb_train_samples / 8) + [7] * (nb_train_samples / 8))
lbl1 = np.array([[1, 0, 0, 0, 0, 0, 0, 0], ] * 197)
lbl2 = np.array([[0, 1, 0, 0, 0, 0, 0, 0], ] * 197)
lbl3 = np.array([[0, 0, 1, 0, 0, 0, 0, 0], ] * 197)
lbl4 = np.array([[0, 0, 0, 1, 0, 0, 0, 0], ] * 197)
lbl5 = np.array([[0, 0, 0, 0, 1, 0, 0, 0], ] * 197)
lbl6 = np.array([[0, 0, 0, 0, 0, 1, 0, 0], ] * 197)
lbl7 = np.array([[0, 0, 0, 0, 0, 0, 1, 0], ] * 197)
lbl8 = np.array([[0, 0, 0, 0, 0, 0, 0, 1], ] * 197)
label = np.concatenate([lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8])
'''train_labels --> loss='sparse_categorical_crossentropy'
labels --> loss='categorical_crossentropy'
'''
#MODEL VGG (the old model)
pad1_1 = ZeroPadding2D(padding=(1, 1), trainable=False, input_shape=(4096, 1, 1), name='in_train')(img)
conv1_1 = Convolution2D(64, 3, 3, activation='relu', name='conv1_1', trainable=False)(pad1_1)
pad1_2 = ZeroPadding2D(padding=(1, 1), trainable=False)(conv1_1)
conv1_2 = Convolution2D(64, 3, 3, activation='relu', name='conv1_2', trainable=False)(pad1_2)
pool1 = MaxPooling2D((2, 2), strides=(2, 2), trainable=False)(conv1_2)
pad2_1 = ZeroPadding2D((1, 1), trainable=False)(pool1)
conv2_1 = Convolution2D(128, 3, 3, activation='relu', name='conv2_1', trainable=False)(pad2_1)
pad2_2 = ZeroPadding2D((1, 1), trainable=False)(conv2_1)
conv2_2 = Convolution2D(128, 3, 3, activation='relu', name='conv2_2', trainable=False)(pad2_2)
pool2 = MaxPooling2D((2, 2), strides=(2, 2), trainable=False)(conv2_2)
pad3_1 = ZeroPadding2D((1, 1), trainable=False)(pool2)
conv3_1 = Convolution2D(256, 3, 3, activation='relu', name='conv3_1', trainable=False)(pad3_1)
pad3_2 = ZeroPadding2D((1, 1), trainable=False)(conv3_1)
conv3_2 = Convolution2D(256, 3, 3, activation='relu', name='conv3_2', trainable=False)(pad3_2)
pad3_3 = ZeroPadding2D((1, 1), trainable=False)(conv3_2)
conv3_3 = Convolution2D(256, 3, 3, activation='relu', name='conv3_3', trainable=False)(pad3_3)
pool3 = MaxPooling2D((2, 2), strides=(2, 2), trainable=False)(conv3_3)
pad4_1 = ZeroPadding2D((1, 1), trainable=False)(pool3)
conv4_1 = Convolution2D(512, 3, 3, activation='relu', name='conv4_1', trainable=False)(pad4_1)
pad4_2 = ZeroPadding2D((1, 1), trainable=False)(conv4_1)
conv4_2 = Convolution2D(512, 3, 3, activation='relu', name='conv4_2', trainable=False)(pad4_2)
pad4_3 = ZeroPadding2D((1, 1), trainable=False)(conv4_2)
conv4_3 = Convolution2D(512, 3, 3, activation='relu', name='conv4_3', trainable=False)(pad4_3)
pool4 = MaxPooling2D((2, 2), strides=(2, 2) , trainable=False)(conv4_3)
pad5_1 = ZeroPadding2D((1, 1) , trainable=False)(pool4)
conv5_1 = Convolution2D(512, 3, 3, activation='relu', name='conv5_1', trainable=False)(pad5_1)
pad5_2 = ZeroPadding2D((1, 1), trainable=False)(conv5_1)
conv5_2 = Convolution2D(512, 3, 3, activation='relu', name='conv5_2', trainable=False)(pad5_2)
pad5_3 = ZeroPadding2D((1, 1), trainable=False)(conv5_2)
conv5_3 = Convolution2D(512, 3, 3, activation='relu', name='conv5_3', trainable=False)(pad5_3)
pool5 = MaxPooling2D((2, 2), strides=(2, 2), trainable=False)(conv5_3)
fc6 = Convolution2D(4096, 7, 7, activation='relu', name='fc6', trainable=False)(pool5)
fc6_drop = Dropout(0.5)(fc6)
#We TRAIN this layer
fc7 = Convolution2D(4096, 1, 1, activation='relu', name='fc7', trainable=False)(fc6_drop)
fc7_drop = Dropout(0.5)(fc7)
fc8 = Convolution2D(2622, 1, 1, name='fc8', trainable=False)(fc7_drop)
flat = Flatten()(fc8)
out = Activation('softmax')(flat)
model = Model(input=img, output=out)
#We load the weight of the old model so when we construct ours we dont have to retrain all of it.
if weights_path:
model.load_weights(weights_path)
# We construct our new model: first 14 layers of the old + two new ones. The new FC has to be trained and the Softmax layer too.
fc7_n = Convolution2D(4096, 1, 1, activation='relu', name='fc7_n', trainable=True, input_shape=train_data.shape[1:])(fc6_drop)
fc7_drop_n = Dropout(0.5)(fc7_n)
fc8_n = Convolution2D(8, 1, 1, name='fc8_n', trainable=False)(fc7_drop_n)
flat_n = Flatten(name='flat_n')(fc8_n)
out_n = Activation('softmax')(flat_n)
model2 = Model(input=img, output=out_n)
#model2.summary()
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model2.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])
'''train_labels --> loss='sparse_categorical_crossentropy'
labels --> loss='categorical_crossentropy'
'''
model2.fit(train_data,label, nb_epoch=nb_epoch, batch_size=64)
print('Model Trained')
#We save the weights so we can load them in our model
model2.save_weights(pesos_entrenados) # always save your weights after training or during training
#We have two options: 1) Return the model here or in the vgg_trained_model Function
return model2
if __name__ == "__main__":
im = Image.open('A.J._Buckley.jpg')
im = im.resize((224, 224))
im = np.array(im).astype(np.float32)
im = im.transpose((2, 0, 1))
im = np.expand_dims(im, axis=0)
# For the training stage
img_width, img_height = 224, 224
img = Input(shape=(3, img_height, img_width))
train_data_dir = 'merge/train'
pesos_entrenados='Modelo_Reentrenado.h5'
# validation_data_dir = 'data/validation'
nb_train_samples = 1576 # 197 per class and we have 8 classes (8 emotions)
nb_validation_samples = 0
nb_epoch = 20
# Stages to construct the model
bottleneck() #Reduce the computational cost
model=entrenamos_modelo('vggface_weights_tensorflow.h5') #Construction of the model
#model.summary()
out = model.predict(im)
print(out[0][0])
The 'vggface_weights_tensorflow.h5' are a conversion from the model weights in theano to tensorflow. I have used the following script to convert the weights:
model = Model(input=img, output=out)
weights_path = 'vgg-face-keras.h5'
model.load_weights(weights_path)
ops = []
for layer in model.layers:
if layer.__class__.__name__ in ['Convolution1D', 'Convolution2D', 'Convolution3D', 'AtrousConvolution2D']:
original_w = K.get_value(layer.W)
converted_w = convert_kernel(original_w)
ops.append(tf.assign(layer.W, converted_w).op)
K.get_session().run(ops)
model.save_weights('vggface_weights_tensorflow.h5')
The original scripts are from here:
https://gist.github.com/EncodeTS/6bbe8cb8bebad7a672f0d872561782d9
If anyone knows how to solve the problem I will always be grateful.
Have you resized all your images so they are 224 by 224?
The error looks like the model expects a list of images of any size, with 3 channels (RGB), 224 wide and 224 height. It looks more like its the input of your data rather than anything with Keras/the code. Try making sure your directory and data are set up as the model expects?