PyTorch lets you download pre-trained models for classification here, but it's not clear how to go from the output tensor of probabilities back to the actual class labels. Any ideas?
See example here: https://discuss.pytorch.org/t/imagenet-classes/4923
https://www.learnopencv.com/pytorch-for-beginners-image-classification-using-pre-trained-models/
You need to download imagenet_classes.txt
Related
I am trying to train a model with the following data
The image is the input and 14 features must be predicted.
Could I please know how to go about training such a model?
Thank you.
These are not really features as far as I am concerned. These are classes and if I got it correctly, your images sometimes belong to more than one classes.
This is a very broad question but I think here might be a good start to learn more about multi-label image classification.
Note that your model should not be much different than an image classification model that is used for cifar10 challenge, for example. But you need to structure your data and choose your loss function accordingly.
I am a beginner of pytorch ,when i read the source code of a project about mask rcnn .I don't konw from where can i get some information about some methods that i don't understand .The official documentation doesn't seem very detailed?
# load an instance segmentation model pre-trained pre-trained on COCO
model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)
# get number of input features for the classifier
in_features = model.roi_heads.box_predictor.cls_score.in_features
# replace the pre-trained head with a new one
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
Just like code above ,I could not get detailed information about the " roi_head" attribute from doc of model .From where i can learn about it?
You won't be able to find such thing on the documentation. You'll have to dive into the source code. Object Detection APIs, especially anchor-based two-stage approaches, are a little bit complex, and they tend to have too many components and hyper-parameters. PyTorch team already made an incredible job making this API modular and kinda easy-to-use. In the specific case of roi_heads you can take a look here to learn more about it. In general, all components can be found in torchvision/models/detection.
Anyway, you can always open an issue, requesting them to expand the documentation. Or we can even do it ourselves and make a pull request :)
From the docstring of FasterRCNN, box_predictor returns two things
box_predictor (nn.Module): module that takes the output of box_head
and returns the classification logits and box regression deltas.
So, cls_score is the classification logits.
Box regression deltas are stored in bbox_pred
Next, cls_score is nothing but a Dense layer with input and output shape defined by in_channels and num_classes (here num_classes= 2 according to the tut).
self.cls_score = nn.Linear(in_channels, num_classes)
in_channels is the input shape of cls_score. Because we want to replace the pre-trained head, we need to preserve information about in_channels in the new head.
# new head
FastRCNNPredictor(in_features, num_classes)
P.S: In Tensorflow you can just freeze the learned layers and train the head only. These Pytorch lines of code achieve the same thing.
I'm working with a Deep Learning model which has a ResNet-50 as backbone pretrained on ImageNet. The dataset that I'm using is the CUB-200, which is a set of 200 species of birds. For this reason I think that could be good to have a pretrained model on a dataset that has a similar domain and I found that the iNaturalist one could be the one that I'm looking for.
The problem is that I didn't find any pretrained model for Pytorch, but only a Tensorflow one here.
I tried to convert it using the MDNN library, but it needs also the '.ckpt.meta' file extend and I have only the '.ckpt'.
This is an example of how to use the MDNN library to convert a tf model to torch:
mmconvert -sf tensorflow -in imagenet_resnet_v2_152.ckpt.meta -iw imagenet_resnet_v2_152.ckpt --dstNode MMdnn_Output -df pytorch -om tf_to_pytorch_resnet_152.pth
Could anyone help me with it?
I have BERT-base model checkpoints which I trained from scratch in Tensorflow. How can I use those checkpoints to predict masked word in a given sentence?
Like, let say sentence is,
"[CLS] abc pqr [MASK] xyz [SEP]"
And I want to predict word at [MASK] position.
How can I do it?
I searched a lot online but everyone is using BERT for their task specific classification tasks.
Not using BERT to predict masked word.
Please help me in solving this prediction problem.
I created data using create_pretraining_data.py & trained model from scratch using run_pretraining.py from official BERT repo (https://github.com/google-research/bert)
I have searched in issues in official bert repo. But didn't found any solution.
Also looked at code in that repo. They're using Estimator which they are training not using from checkpoints weights.
Didn't found any way to use way to use Tensorflow checkpoints of BERT-base model (trained from scratch) to predict word masked token (i.e. [MASK]).
Do you definitely need to start from a TF checkpoint? If you can use one of the pretrained models used in the pytorch-transformers library, I wrote a library for doing exactly this: FitBERT.
If you have to start with a TF checkpoint, there are scripts for converting from a TF checkpoint to something pytorch-transformers can use, link, and after converting you should be able to use FitBERT, or you can just see what we're doing in the code.
I built a CNN model for image classification using the Keras library. However training takes many hours. Once I trained my model, how can I use it without training once more? I mean after I trained my model, I want to use it many times.
Because I will use my model in android studio.
Any help is appreciated
Thank YOU...
EDIT
When I wrote this question, I did not know the save model and load.model, in the answers you see the appropriate usage of them.
You can easily save your model after the training process by using:
model.save('my_model.h5')
you can later load that model by using:
model = load_model('my_model.h5')
for more details have a look at the documentation: https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model