text
stringlengths
0
4.99k
self.num_classes = num_classes
self.head = keras.layers.Dense(num_classes, kernel_initializer=\"zeros\")
self.bit_model = module
def call(self, images):
bit_embedding = self.bit_model(images)
return self.head(bit_embedding)
model = MyBiTModel(num_classes=NUM_CLASSES, module=bit_module)
Define optimizer and loss
learning_rate = 0.003 * BATCH_SIZE / 512
# Decay learning rate by a factor of 10 at SCHEDULE_BOUNDARIES.
lr_schedule = keras.optimizers.schedules.PiecewiseConstantDecay(
boundaries=SCHEDULE_BOUNDARIES,
values=[
learning_rate,
learning_rate * 0.1,
learning_rate * 0.01,
learning_rate * 0.001,
],
)
optimizer = keras.optimizers.SGD(learning_rate=lr_schedule, momentum=0.9)
loss_fn = keras.losses.SparseCategoricalCrossentropy(from_logits=True)
Compile the model
model.compile(optimizer=optimizer, loss=loss_fn, metrics=[\"accuracy\"])
Set up callbacks
train_callbacks = [
keras.callbacks.EarlyStopping(
monitor=\"val_accuracy\", patience=2, restore_best_weights=True
)
]
Train the model
history = model.fit(
pipeline_train,
batch_size=BATCH_SIZE,
epochs=int(SCHEDULE_LENGTH / STEPS_PER_EPOCH),
steps_per_epoch=STEPS_PER_EPOCH,
validation_data=pipeline_validation,
callbacks=train_callbacks,
)
Epoch 1/400
10/10 [==============================] - 41s 1s/step - loss: 0.7440 - accuracy: 0.7844 - val_loss: 0.1837 - val_accuracy: 0.9582
Epoch 2/400
10/10 [==============================] - 8s 904ms/step - loss: 0.1499 - accuracy: 0.9547 - val_loss: 0.1094 - val_accuracy: 0.9709
Epoch 3/400
10/10 [==============================] - 8s 905ms/step - loss: 0.1674 - accuracy: 0.9422 - val_loss: 0.0874 - val_accuracy: 0.9727
Epoch 4/400
10/10 [==============================] - 8s 905ms/step - loss: 0.1314 - accuracy: 0.9578 - val_loss: 0.0829 - val_accuracy: 0.9727
Epoch 5/400
10/10 [==============================] - 8s 903ms/step - loss: 0.1336 - accuracy: 0.9500 - val_loss: 0.0765 - val_accuracy: 0.9727
Plot the training and validation metrics
def plot_hist(hist):
plt.plot(hist.history[\"accuracy\"])
plt.plot(hist.history[\"val_accuracy\"])
plt.plot(hist.history[\"loss\"])
plt.plot(hist.history[\"val_loss\"])
plt.title(\"Training Progress\")
plt.ylabel(\"Accuracy/Loss\")
plt.xlabel(\"Epochs\")
plt.legend([\"train_acc\", \"val_acc\", \"train_loss\", \"val_loss\"], loc=\"upper left\")
plt.show()
plot_hist(history)
png
Evaluate the model
accuracy = model.evaluate(pipeline_validation)[1] * 100
print(\"Accuracy: {:.2f}%\".format(accuracy))
9/9 [==============================] - 6s 646ms/step - loss: 0.0874 - accuracy: 0.9727
Accuracy: 97.27%
Conclusion
BiT performs well across a surprisingly wide range of data regimes -- from 1 example per class to 1M total examples. BiT achieves 87.5% top-1 accuracy on ILSVRC-2012, 99.4% on CIFAR-10, and 76.3% on the 19 task Visual Task Adaptation Benchmark (VTAB). On small datasets, BiT attains 76.8% on ILSVRC-2012 with 10 examples...
You can experiment further with the BigTransfer Method by following the original paper.
Use EfficientNet with weights pre-trained on imagenet for Stanford Dogs classification.
Introduction: what is EfficientNet
EfficientNet, first introduced in Tan and Le, 2019 is among the most efficient models (i.e. requiring least FLOPS for inference) that reaches State-of-the-Art accuracy on both imagenet and common image classification transfer learning tasks.
The smallest base model is similar to MnasNet, which reached near-SOTA with a significantly smaller model. By introducing a heuristic way to scale the model, EfficientNet provides a family of models (B0 to B7) that represents a good combination of efficiency and accuracy on a variety of scales. Such a scaling heuristic...
A summary of the latest updates on the model is available at here, where various augmentation schemes and semi-supervised learning approaches are applied to further improve the imagenet performance of the models. These extensions of the model can be used by updating weights without changing model architecture.
B0 to B7 variants of EfficientNet
(This section provides some details on \"compound scaling\", and can be skipped if you're only interested in using the models)
Based on the original paper people may have the impression that EfficientNet is a continuous family of models created by arbitrarily choosing scaling factor in as Eq.(3) of the paper. However, choice of resolution, depth and width are also restricted by many factors:
Resolution: Resolutions not divisible by 8, 16, etc. cause zero-padding near boundaries of some layers which wastes computational resources. This especially applies to smaller variants of the model, hence the input resolution for B0 and B1 are chosen as 224 and 240.
Depth and width: The building blocks of EfficientNet demands channel size to be multiples of 8.
Resource limit: Memory limitation may bottleneck resolution when depth and width can still increase. In such a situation, increasing depth and/or width but keep resolution can still improve performance.
As a result, the depth, width and resolution of each variant of the EfficientNet models are hand-picked and proven to produce good results, though they may be significantly off from the compound scaling formula. Therefore, the keras implementation (detailed below) only provide these 8 models, B0 to B7, instead of allow...
Keras implementation of EfficientNet