Decode method for 21k classes
Skorkmaz88 opened this issue · 3 comments
From the tutorials,
keras.applications.imagenet_utils.decode_predictions(preds)[0]
as expected this decode expects 1k classes, and yields error :
`---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in
10 # decode the results into a list of tuples (class, description, probability)
11 # (one such list for each sample in the batch)
---> 12 print(keras.applications.imagenet_utils.decode_predictions(preds)[0])
/usr/local/lib/python3.7/dist-packages/keras/applications/imagenet_utils.py in decode_predictions(preds, top)
148 'a batch of predictions '
149 '(i.e. a 2D array of shape (samples, 1000)). '
--> 150 'Found array with shape: ' + str(preds.shape))
151 if CLASS_INDEX is None:
152 fpath = data_utils.get_file(
ValueError: decode_predictions
expects a batch of predictions (i.e. a 2D array of shape (samples, 1000)). Found array with shape: (1, 21843)`
How can we decode this ?
I've added a decode_predictions_imagenet21k
in keras_cv_attention_models package. You may try:
pip install keras-cv-attention-models
Then
import keras_efficientnet_v2 as efficientnet # Or from keras_cv_attention_models import efficientnet
from keras_cv_attention_models import imagenet
from skimage.data import chelsea
mm = efficientnet.EfficientNetV2B3(pretrained='imagenet21k', num_classes=21843)
# >>>> Load pretrained from: ~/.keras/models/efficientnetv2/efficientnetv2-b3-21k.h5
imm = tf.image.resize(chelsea(), mm.input_shape[1:-1])
imm = tf.keras.applications.imagenet_utils.preprocess_input(tf.expand_dims(imm, 0), mode='tf')
pred = mm(imm).numpy()
print(imagenet.decode_predictions_imagenet21k(pred))
# [[('n02123159', 'tiger_cat', 0.24127434),
# ('n02124075', 'Egyptian_cat', 0.14877036),
# ('n02122725', 'tom, tomcat', 0.11431442),
# ('n02120997', 'feline, felid', 0.05276237),
# ('n02122878', 'tabby, queen', 0.046945572)]]
The basic labels files are WordNet links listed in Class names for ImageNet-21k pre-trained models #7.
Thank you ! It works
Just updated. In current Github version or the next release, you may use keras_cv_attention_models .imagenet.decode_predictions
for both imagenet and imagenet21k prediction.