简体   繁体   中英

PyTorch Detecto Model: tensor incompatibiliy in predicition for a pretrained model

Trying to train a very simple model and do a image-prediction with the following code for pytorch detecto:

from detecto import core, utils, visualize

dataset = core.Dataset('images/')


model = core.Model(['rect'])

model.fit(dataset)

modelName = 'model_weights_simpleRect.pth'
model.save(modelName)   


image = utils.read_image('simple_image_to_test.jpg')
predictions = model.predict(image)

This leads to the following output:

Epoch 1 of 10
Begin iterating over training dataset
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:12<00:00,  1.56it/s]
Epoch 2 of 10
Begin iterating over training dataset
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:11<00:00,  1.80it/s]
Epoch 3 of 10
Begin iterating over training dataset
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:11<00:00,  1.80it/s]
Epoch 4 of 10
Begin iterating over training dataset
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:11<00:00,  1.79it/s]
Epoch 5 of 10
Begin iterating over training dataset
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:11<00:00,  1.79it/s]
Epoch 6 of 10
Begin iterating over training dataset
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:11<00:00,  1.80it/s]
Epoch 7 of 10
Begin iterating over training dataset
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:11<00:00,  1.78it/s]
Epoch 8 of 10
Begin iterating over training dataset
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:11<00:00,  1.80it/s]
Epoch 9 of 10
Begin iterating over training dataset
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:11<00:00,  1.78it/s]
Epoch 10 of 10
Begin iterating over training dataset
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:11<00:00,  1.80it/s]
Traceback (most recent call last):
  File "train_simpleRect_and_predict.py", line 15, in <module>
    predictions = model.predict(image)
  File "/home/std/anaconda3/envs/dri/lib/python3.7/site-packages/detecto/core.py", line 338, in predict
    preds = self._get_raw_predictions(images)
  File "/home/std/anaconda3/envs/dri/lib/python3.7/site-packages/detecto/core.py", line 294, in _get_raw_predictions
    preds = self._model(images)
  File "/home/std/anaconda3/envs/dri/lib/python3.7/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/std/anaconda3/envs/dri/lib/python3.7/site-packages/torchvision/models/detection/generalized_rcnn.py", line 52, in forward
    detections, detector_losses = self.roi_heads(features, proposals, images.image_sizes, targets)
  File "/home/std/anaconda3/envs/dri/lib/python3.7/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/std/anaconda3/envs/dri/lib/python3.7/site-packages/torchvision/models/detection/roi_heads.py", line 550, in forward
    boxes, scores, labels = self.postprocess_detections(class_logits, box_regression, proposals, image_shapes)
  File "/home/std/anaconda3/envs/dri/lib/python3.7/site-packages/torchvision/models/detection/roi_heads.py", line 474, in postprocess_detections
    pred_boxes = self.box_coder.decode(box_regression, proposals)
  File "/home/std/anaconda3/envs/dri/lib/python3.7/site-packages/torchvision/models/detection/_utils.py", line 168, in decode
    rel_codes.reshape(sum(boxes_per_image), -1), concat_boxes
RuntimeError: cannot reshape tensor of 0 elements into shape [0, -1] because the unspecified dimension size -1 can be any value and is ambiguous

How can I get more detailled information about the model dimension, where exactly in the model the tensor incompatibilies occur and how to fix it?

Add. info: I used the same code with other data and it worked.

Thank you!

The problem were wrong image dimensions in the xml-description files, which corresponded to each image.

I fixed the xml-files and the error did not occur again.

Another problem - which brought up a similar tensor-dimension error, was caused by this statement:

model = Model.load(modelName, ['rect'])

The correct version is:

model = Model()
model.load(modelName, ['rect'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM