text stringlengths 0 4.99k |
|---|
# First convolution block without batch normalization. |
x = layers.Conv2D(filters=filters, kernel_size=7, strides=1, padding=\"same\")(inputs) |
x = layers.LeakyReLU(0.2)(x) |
# Second convolution block with batch normalization. |
x = layers.Conv2D(filters=filters, kernel_size=1, strides=1, padding=\"same\")(x) |
x = layers.LeakyReLU(0.2)(x) |
x = layers.BatchNormalization()(x) |
# Intermediate resizing as a bottleneck. |
bottleneck = layers.Resizing( |
*TARGET_SIZE, interpolation=interpolation |
)(x) |
# Residual passes. |
for _ in range(num_res_blocks): |
x = res_block(bottleneck) |
# Projection. |
x = layers.Conv2D( |
filters=filters, kernel_size=3, strides=1, padding=\"same\", use_bias=False |
)(x) |
x = layers.BatchNormalization()(x) |
# Skip connection. |
x = layers.Add()([bottleneck, x]) |
# Final resized image. |
x = layers.Conv2D(filters=3, kernel_size=7, strides=1, padding=\"same\")(x) |
final_resize = layers.Add()([naive_resize, x]) |
return tf.keras.Model(inputs, final_resize, name=\"learnable_resizer\") |
learnable_resizer = get_learnable_resizer() |
Visualize the outputs of the learnable resizing module |
Here, we visualize how the resized images would look like after being passed through the random weights of the resizer. |
sample_images, _ = next(iter(train_ds)) |
plt.figure(figsize=(16, 10)) |
for i, image in enumerate(sample_images[:6]): |
image = image / 255 |
ax = plt.subplot(3, 4, 2 * i + 1) |
plt.title(\"Input Image\") |
plt.imshow(image.numpy().squeeze()) |
plt.axis(\"off\") |
ax = plt.subplot(3, 4, 2 * i + 2) |
resized_image = learnable_resizer(image[None, ...]) |
plt.title(\"Resized Image\") |
plt.imshow(resized_image.numpy().squeeze()) |
plt.axis(\"off\") |
WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). |
WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). |
WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). |
WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). |
WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). |
WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). |
png |
Model building utility |
def get_model(): |
backbone = tf.keras.applications.DenseNet121( |
weights=None, |
include_top=True, |
classes=2, |
input_shape=((TARGET_SIZE[0], TARGET_SIZE[1], 3)), |
) |
backbone.trainable = True |
inputs = layers.Input((INP_SIZE[0], INP_SIZE[1], 3)) |
x = layers.Rescaling(scale=1.0 / 255)(inputs) |
x = learnable_resizer(x) |
outputs = backbone(x) |
return tf.keras.Model(inputs, outputs) |
The structure of the learnable image resizer module allows for flexible integrations with different vision models. |
Compile and train our model with learnable resizer |
model = get_model() |
model.compile( |
loss=keras.losses.CategoricalCrossentropy(label_smoothing=0.1), |
optimizer=\"sgd\", |
metrics=[\"accuracy\"], |
) |
model.fit(train_ds, validation_data=validation_ds, epochs=EPOCHS) |
Epoch 1/5 |
146/146 [==============================] - 49s 247ms/step - loss: 0.6956 - accuracy: 0.5697 - val_loss: 0.6958 - val_accuracy: 0.5103 |
Epoch 2/5 |
146/146 [==============================] - 33s 216ms/step - loss: 0.6685 - accuracy: 0.6117 - val_loss: 0.6955 - val_accuracy: 0.5387 |
Epoch 3/5 |
146/146 [==============================] - 33s 216ms/step - loss: 0.6542 - accuracy: 0.6190 - val_loss: 0.7410 - val_accuracy: 0.5684 |
Epoch 4/5 |
146/146 [==============================] - 33s 216ms/step - loss: 0.6357 - accuracy: 0.6576 - val_loss: 0.9322 - val_accuracy: 0.5314 |
Epoch 5/5 |
146/146 [==============================] - 33s 215ms/step - loss: 0.6224 - accuracy: 0.6745 - val_loss: 0.6526 - val_accuracy: 0.6672 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.