text
stringlengths
0
4.99k
display(Image(img_path))
# Prepare image
img_array = preprocess_input(get_img_array(img_path, size=img_size))
# Print what the two top predicted classes are
preds = model.predict(img_array)
print(\"Predicted:\", decode_predictions(preds, top=2)[0])
jpeg
Predicted: [('n02112137', 'chow', 4.611241), ('n02124075', 'Egyptian_cat', 4.3817368)]
We generate class activation heatmap for \"chow,\" the class index is 260
heatmap = make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=260)
save_and_display_gradcam(img_path, heatmap)
jpeg
We generate class activation heatmap for \"egyptian cat,\" the class index is 285
heatmap = make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=285)
save_and_display_gradcam(img_path, heatmap)
jpeg
Implement Gradient Centralization to improve training performance of DNNs.
Introduction
This example implements Gradient Centralization, a new optimization technique for Deep Neural Networks by Yong et al., and demonstrates it on Laurence Moroney's Horses or Humans Dataset. Gradient Centralization can both speedup training process and improve the final generalization performance of DNNs. It operates direc...
This example requires TensorFlow 2.2 or higher as well as tensorflow_datasets which can be installed with this command:
pip install tensorflow-datasets
We will be implementing Gradient Centralization in this example but you could also use this very easily with a package I built, gradient-centralization-tf.
Setup
from time import time
import tensorflow as tf
import tensorflow_datasets as tfds
from tensorflow.keras import layers
from tensorflow.keras.optimizers import RMSprop
Prepare the data
For this example, we will be using the Horses or Humans dataset.
num_classes = 2
input_shape = (300, 300, 3)
dataset_name = \"horses_or_humans\"
batch_size = 128
AUTOTUNE = tf.data.AUTOTUNE
(train_ds, test_ds), metadata = tfds.load(
name=dataset_name,
split=[tfds.Split.TRAIN, tfds.Split.TEST],
with_info=True,
as_supervised=True,
)
print(f\"Image shape: {metadata.features['image'].shape}\")
print(f\"Training images: {metadata.splits['train'].num_examples}\")
print(f\"Test images: {metadata.splits['test'].num_examples}\")
Image shape: (300, 300, 3)
Training images: 1027
Test images: 256
Use Data Augmentation
We will rescale the data to [0, 1] and perform simple augmentations to our data.
rescale = layers.Rescaling(1.0 / 255)
data_augmentation = tf.keras.Sequential(
[
layers.RandomFlip(\"horizontal_and_vertical\"),
layers.RandomRotation(0.3),
layers.RandomZoom(0.2),
]
)
def prepare(ds, shuffle=False, augment=False):
# Rescale dataset
ds = ds.map(lambda x, y: (rescale(x), y), num_parallel_calls=AUTOTUNE)
if shuffle:
ds = ds.shuffle(1024)
# Batch dataset
ds = ds.batch(batch_size)
# Use data augmentation only on the training set
if augment:
ds = ds.map(
lambda x, y: (data_augmentation(x, training=True), y),
num_parallel_calls=AUTOTUNE,
)
# Use buffered prefecting
return ds.prefetch(buffer_size=AUTOTUNE)
Rescale and augment the data
train_ds = prepare(train_ds, shuffle=True, augment=True)