text stringlengths 0 4.99k |
|---|
# Extract input patches. |
# B, H, W, K*K*C |
input_patches = tf.image.extract_patches( |
images=x, |
sizes=[1, self.kernel_size, self.kernel_size, 1], |
strides=[1, self.stride, self.stride, 1], |
rates=[1, 1, 1, 1], |
padding=\"SAME\", |
) |
# Reshape the input patches to align with later operations. |
# B, H, W, K*K, C//G, G |
input_patches = self.input_patches_reshape(input_patches) |
# Compute the multiply-add operation of kernels and patches. |
# B, H, W, K*K, C//G, G |
output = tf.multiply(kernel, input_patches) |
# B, H, W, C//G, G |
output = tf.reduce_sum(output, axis=3) |
# Reshape the output kernel. |
# B, H, W, C |
output = self.output_reshape(output) |
# Return the output tensor and the kernel. |
return output, kernel |
Testing the Involution layer |
# Define the input tensor. |
input_tensor = tf.random.normal((32, 256, 256, 3)) |
# Compute involution with stride 1. |
output_tensor, _ = Involution( |
channel=3, group_number=1, kernel_size=5, stride=1, reduction_ratio=1, name=\"inv_1\" |
)(input_tensor) |
print(f\"with stride 1 ouput shape: {output_tensor.shape}\") |
# Compute involution with stride 2. |
output_tensor, _ = Involution( |
channel=3, group_number=1, kernel_size=5, stride=2, reduction_ratio=1, name=\"inv_2\" |
)(input_tensor) |
print(f\"with stride 2 ouput shape: {output_tensor.shape}\") |
# Compute involution with stride 1, channel 16 and reduction ratio 2. |
output_tensor, _ = Involution( |
channel=16, group_number=1, kernel_size=5, stride=1, reduction_ratio=2, name=\"inv_3\" |
)(input_tensor) |
print( |
\"with channel 16 and reduction ratio 2 ouput shape: {}\".format(output_tensor.shape) |
) |
with stride 1 ouput shape: (32, 256, 256, 3) |
with stride 2 ouput shape: (32, 128, 128, 3) |
with channel 16 and reduction ratio 2 ouput shape: (32, 256, 256, 3) |
Image Classification |
In this section, we will build an image-classifier model. There will be two models one with convolutions and the other with involutions. |
The image-classification model is heavily inspired by this Convolutional Neural Network (CNN) tutorial from Google. |
Get the CIFAR10 Dataset |
# Load the CIFAR10 dataset. |
print(\"loading the CIFAR10 dataset...\") |
(train_images, train_labels), ( |
test_images, |
test_labels, |
) = keras.datasets.cifar10.load_data() |
# Normalize pixel values to be between 0 and 1. |
(train_images, test_images) = (train_images / 255.0, test_images / 255.0) |
# Shuffle and batch the dataset. |
train_ds = ( |
tf.data.Dataset.from_tensor_slices((train_images, train_labels)) |
.shuffle(256) |
.batch(256) |
) |
test_ds = tf.data.Dataset.from_tensor_slices((test_images, test_labels)).batch(256) |
loading the CIFAR10 dataset... |
Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz |
170500096/170498071 [==============================] - 3s 0us/step |
Visualise the data |
class_names = [ |
\"airplane\", |
\"automobile\", |
\"bird\", |
\"cat\", |
\"deer\", |
\"dog\", |
\"frog\", |
\"horse\", |
\"ship\", |
\"truck\", |
] |
plt.figure(figsize=(10, 10)) |
for i in range(25): |
plt.subplot(5, 5, i + 1) |
plt.xticks([]) |
plt.yticks([]) |
plt.grid(False) |
plt.imshow(train_images[i]) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.