Related
classifier.add(Conv2D(32, (3, 3), input_shape = (150, 150, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2,2)))
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2,2)))
classifier.add(Conv2D(64, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2,2)))
I'm trying to make food detection system using CNN
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
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
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)
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