INaturalist dataset have 10 classes, each class of size 1000 images. These are split to train, test and val(75,25,25). The images are resized and then fed to the nerual network which are pretrained models, these include 'Inception3','InceptionResNetV2', 'ResNet50', 'Xception' in experimentation.
Regularization Techniques used
I have used wandb for monitoring all the models and check which is a better one for experimentation and during production.
The image below shows what a filter see in the convolutional network.
# This custom model has the 5th convolutional layer as its final layer
guided_backprop_model = tf.keras.models.Model(inputs = [model.inputs], outputs = [model.get_layer(index=-8).output])
# Here we choose only those layers that have an activation attribute
layer_dictionary = [layer for layer in guided_backprop_model.layers[1:] if hasattr(layer,'activation')]
# Define a custom gradient for the version of ReLU needed for guided backpropagation
@tf.custom_gradient
def guidedbackpropSelu(x):
def grad(dy):
return tf.cast(dy>0,"float32") * tf.cast(x>0, "float32") * dy
return tf.nn.relu(x), grad
for l in layer_dictionary:
# Change the ReLU activation to supress the negative gradients
if l.activation == tf.keras.activations.selu:
l.activation = guidedbackpropSelu
num_features = 10
size = 1
num_imag_in_row = 1
num_cols = num_features // num_imag_in_row
display_grid = np.ones((size * num_cols, num_imag_in_row * size))
k = 1
j = 1
for col in range(num_cols):
for row in range(num_imag_in_row):
channel_image = CONV5_layer_activation[j, k, col * num_imag_in_row + row]
display_grid[col * size : (col + 1) * size, row * size : (row + 1) * size] = channel_image
scale = 1. / size
plt.figure(figsize=(scale * display_grid.shape[1], scale * display_grid.shape[0]))
plt.title(vis_true_labels[index])
plt.grid(False)
plt.imshow(display_grid, aspect='auto',cmap='gray')
plt.show()
# Guided backprop gradient visualisation
grad_image = np.dstack((
target_gradient[:, :, 0],
target_gradient[:, :, 1],
target_gradient[:, :, 2],
))
grad_image = grad_image - np.min(grad_image)
grad_image = grad_image/grad_image.max()
imgplot = plt.imshow(grad_image)
plt.axis("off")
plt.show()