Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Refine ActionDataSample structure #2658

Merged
merged 13 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"label = '../tools/data/kinetics/label_map_k400.txt'\n",
"results = inference_recognizer(model, video)\n",
"\n",
"pred_scores = results.pred_scores.item.tolist()\n",
"pred_scores = results.pred_score.tolist()\n",
"score_tuples = tuple(zip(range(len(pred_scores)), pred_scores))\n",
"score_sorted = sorted(score_tuples, key=itemgetter(1), reverse=True)\n",
"top5_label = score_sorted[:5]\n",
Expand Down
2 changes: 1 addition & 1 deletion demo/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def main():
model = init_recognizer(cfg, args.checkpoint, device=args.device)
pred_result = inference_recognizer(model, args.video)

pred_scores = pred_result.pred_scores.item.tolist()
pred_scores = pred_result.pred_score.tolist()
score_tuples = tuple(zip(range(len(pred_scores)), pred_scores))
score_sorted = sorted(score_tuples, key=itemgetter(1), reverse=True)
top5_label = score_sorted[:5]
Expand Down
2 changes: 1 addition & 1 deletion demo/demo_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def main():
raise NotImplementedError('Demo works on extracted audio features')
pred_result = inference_recognizer(model, args.audio)

pred_scores = pred_result.pred_scores.item.tolist()
pred_scores = pred_result.pred_score.tolist()
score_tuples = tuple(zip(range(len(pred_scores)), pred_scores))
score_sorted = sorted(score_tuples, key=itemgetter(1), reverse=True)
top5_label = score_sorted[:5]
Expand Down
2 changes: 1 addition & 1 deletion demo/demo_skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def main():
model = init_recognizer(config, args.checkpoint, args.device)
result = inference_skeleton(model, pose_results, (h, w))

max_pred_index = result.pred_scores.item.argmax().item()
max_pred_index = result.pred_score.argmax().item()
label_map = [x.strip() for x in open(args.label_map).readlines()]
action_label = label_map[max_pred_index]

Expand Down
6 changes: 3 additions & 3 deletions demo/demo_video_structuralize.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def skeleton_based_action_recognition(args, pose_results, h, w):
skeleton_model = init_recognizer(
skeleton_config, args.skeleton_checkpoint, device=args.device)
result = inference_skeleton(skeleton_model, pose_results, (h, w))
action_idx = result.pred_scores.item.argmax().item()
action_idx = result.pred_score.argmax().item()
return label_map[action_idx]


Expand All @@ -382,7 +382,7 @@ def rgb_based_action_recognition(args):
rgb_config.model.backbone.pretrained = None
rgb_model = init_recognizer(rgb_config, args.rgb_checkpoint, args.device)
action_results = inference_recognizer(rgb_model, args.video)
rgb_action_result = action_results.pred_scores.item.argmax().item()
rgb_action_result = action_results.pred_score.argmax().item()
label_map = [x.strip() for x in open(args.label_map).readlines()]
return label_map[rgb_action_result]

Expand Down Expand Up @@ -460,7 +460,7 @@ def skeleton_based_stdet(args, label_map, human_detections, pose_results,

output = inference_recognizer(skeleton_stdet_model, fake_anno)
# for multi-label recognition
score = output.pred_scores.item.tolist()
score = output.pred_score.tolist()
for k in range(len(score)): # 81
if k not in label_map:
continue
Expand Down
Binary file modified demo/fuse/bone.pkl
Binary file not shown.
Binary file modified demo/fuse/joint.pkl
Binary file not shown.
2 changes: 1 addition & 1 deletion demo/long_video_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def inference(model, data, args, frame_queue):

result = inference_recognizer(
model, cur_data, test_pipeline=args.test_pipeline)
scores = result.pred_scores.item.tolist()
scores = result.pred_score.tolist()

if args.stride > 0:
pred_stride = int(args.sample_length * args.stride)
Expand Down
3,792 changes: 1,896 additions & 1,896 deletions demo/mmaction2_tutorial.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/webcam_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def inference():
# Forward the model
with torch.no_grad():
result = model.test_step(cur_data)[0]
scores = result.pred_scores.item.tolist()
scores = result.pred_score.tolist()
scores = np.array(scores)
score_cache.append(scores)
scores_sum += scores
Expand Down
17 changes: 9 additions & 8 deletions docs/en/get_started/guide_to_framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ class VideoPack(BaseTransform):
def transform(self, results):
packed_results = dict()
inputs = to_tensor(results['imgs'])
data_sample = ActionDataSample().set_gt_labels(results['label'])
data_sample = ActionDataSample()
data_sample.set_gt_label(results['label'])
metainfo = {k: results[k] for k in self.meta_keys if k in results}
data_sample.set_metainfo(metainfo)
packed_results['inputs'] = inputs
Expand Down Expand Up @@ -219,7 +220,7 @@ print('num_clips: ', data_sample.num_clips)
print('clip_len: ', data_sample.clip_len)

# Get label of the inputs
print('label: ', data_sample.gt_labels.item)
print('label: ', data_sample.gt_label)
```

```
Expand Down Expand Up @@ -321,7 +322,7 @@ print('num_clips: ', data_sample.num_clips)
print('clip_len: ', data_sample.clip_len)

# Get label of the inputs
print('label: ', data_sample.gt_labels.item)
print('label: ', data_sample.gt_label)

from mmengine.runner import Runner

Expand Down Expand Up @@ -481,7 +482,7 @@ class ClsHeadZelda(BaseModule):

def loss(self, feats, data_samples):
cls_scores = self(feats)
labels = torch.stack([x.gt_labels.item for x in data_samples])
labels = torch.stack([x.gt_label for x in data_samples])
labels = labels.squeeze()

if labels.shape == torch.Size([]):
Expand Down Expand Up @@ -589,8 +590,8 @@ with torch.no_grad():
data_batch_test = copy.deepcopy(batched_packed_results)
data = model.data_preprocessor(data_batch_test, training=False)
predictions = model(**data, mode='predict')
print('Label of Sample[0]', predictions[0].gt_labels.item)
print('Scores of Sample[0]', predictions[0].pred_scores.item)
print('Label of Sample[0]', predictions[0].gt_label)
print('Scores of Sample[0]', predictions[0].pred_score)
```

```shell
Expand Down Expand Up @@ -661,8 +662,8 @@ class AccuracyMetric(BaseMetric):
data_samples = copy.deepcopy(data_samples)
for data_sample in data_samples:
result = dict()
scores = data_sample['pred_scores']['item'].cpu().numpy()
label = data_sample['gt_labels']['item'].item()
scores = data_sample['pred_score'].cpu().numpy()
label = data_sample['gt_label'].item()
result['scores'] = scores
result['label'] = label
self.results.append(result)
Expand Down
2 changes: 1 addition & 1 deletion docs/en/get_started/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ label_file = 'tools/data/kinetics/label_map_k400.txt'
model = init_recognizer(config_file, checkpoint_file, device='cpu') # or device='cuda:0'
pred_result = inference_recognizer(model, video_file)

pred_scores = pred_result.pred_scores.item.tolist()
pred_scores = pred_result.pred_score.tolist()
score_tuples = tuple(zip(range(len(pred_scores)), pred_scores))
score_sorted = sorted(score_tuples, key=itemgetter(1), reverse=True)
top5_label = score_sorted[:5]
Expand Down
16 changes: 8 additions & 8 deletions docs/zh_cn/get_started/guide_to_framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class VideoPack(BaseTransform):
def transform(self, results):
packed_results = dict()
inputs = to_tensor(results['imgs'])
data_sample = ActionDataSample().set_gt_labels(results['label'])
data_sample = ActionDataSample().set_gt_label(results['label'])
metainfo = {k: results[k] for k in self.meta_keys if k in results}
data_sample.set_metainfo(metainfo)
packed_results['inputs'] = inputs
Expand Down Expand Up @@ -220,7 +220,7 @@ print('num_clips: ', data_sample.num_clips)
print('clip_len: ', data_sample.clip_len)

# 获取输入的标签
print('label: ', data_sample.gt_labels.item)
print('label: ', data_sample.gt_label)
```

```
Expand Down Expand Up @@ -322,7 +322,7 @@ print('num_clips: ', data_sample.num_clips)
print('clip_len: ', data_sample.clip_len)

# 获取输入的标签
print('label: ', data_sample.gt_labels.item)
print('label: ', data_sample.gt_label)

from mmengine.runner import Runner

Expand Down Expand Up @@ -482,7 +482,7 @@ class ClsHeadZelda(BaseModule):

def loss(self, feats, data_samples):
cls_scores = self(feats)
labels = torch.stack([x.gt_labels.item for x in data_samples])
labels = torch.stack([x.gt_label for x in data_samples])
labels = labels.squeeze()

if labels.shape == torch.Size([]):
Expand Down Expand Up @@ -590,8 +590,8 @@ with torch.no_grad():
data_batch_test = copy.deepcopy(batched_packed_results)
data = model.data_preprocessor(data_batch_test, training=False)
predictions = model(**data, mode='predict')
print('Label of Sample[0]', predictions[0].gt_labels.item)
print('Scores of Sample[0]', predictions[0].pred_scores.item)
print('Label of Sample[0]', predictions[0].gt_label)
print('Scores of Sample[0]', predictions[0].pred_score)
```

```shell
Expand Down Expand Up @@ -662,8 +662,8 @@ class AccuracyMetric(BaseMetric):
data_samples = copy.deepcopy(data_samples)
for data_sample in data_samples:
result = dict()
scores = data_sample['pred_scores']['item'].cpu().numpy()
label = data_sample['gt_labels']['item'].item()
scores = data_sample['pred_score'].cpu().numpy()
label = data_sample['gt_label'].item()
result['scores'] = scores
result['label'] = label
self.results.append(result)
Expand Down
2 changes: 1 addition & 1 deletion docs/zh_cn/get_started/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ label_file = 'tools/data/kinetics/label_map_k400.txt'
model = init_recognizer(config_file, checkpoint_file, device='cpu') # or device='cuda:0'
pred_result = inference_recognizer(model, video_file)

pred_scores = pred_result.pred_scores.item.tolist()
pred_scores = pred_result.pred_score.tolist()
score_tuples = tuple(zip(range(len(pred_scores)), pred_scores))
score_sorted = sorted(score_tuples, key=itemgetter(1), reverse=True)
top5_label = score_sorted[:5]
Expand Down
4 changes: 2 additions & 2 deletions mmaction/apis/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def inference_recognizer(model: nn.Module,

Returns:
:obj:`ActionDataSample`: The inference results. Specifically, the
predicted scores are saved at ``result.pred_scores.item``.
predicted scores are saved at ``result.pred_score``.
"""

if test_pipeline is None:
Expand Down Expand Up @@ -131,7 +131,7 @@ def inference_skeleton(model: nn.Module,

Returns:
:obj:`ActionDataSample`: The inference results. Specifically, the
predicted scores are saved at ``result.pred_scores.item``.
predicted scores are saved at ``result.pred_score``.
"""
if test_pipeline is None:
cfg = model.cfg
Expand Down
4 changes: 2 additions & 2 deletions mmaction/apis/inferencers/actionrecog_inferencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,6 @@ def pred2dict(self, data_sample: ActionDataSample) -> Dict:
dict: The output dictionary.
"""
result = {}
result['pred_labels'] = data_sample.pred_labels.item.tolist()
result['pred_scores'] = data_sample.pred_scores.item.tolist()
result['pred_labels'] = data_sample.pred_label.tolist()
result['pred_scores'] = data_sample.pred_score.tolist()
return result
21 changes: 5 additions & 16 deletions mmaction/datasets/transforms/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,19 @@
import numpy as np
import torch
from mmcv.transforms import BaseTransform, to_tensor
from mmengine.structures import InstanceData, LabelData
from mmengine.structures import InstanceData

from mmaction.registry import TRANSFORMS
from mmaction.structures import ActionDataSample


@TRANSFORMS.register_module()
class PackActionInputs(BaseTransform):
"""Pack the input data for the recognition.

PackActionInputs first packs one of 'imgs', 'keypoint' and 'audios' into
the `packed_results['inputs']`, which are the three basic input modalities
for the task of rgb-based, skeleton-based and audio-based action
recognition, as well as spatio-temporal action detection in the case
of 'img'. Next, it prepares a `data_sample` for the task of action
recognition (only a single label of `torch.LongTensor` format, which is
saved in the `data_sample.gt_labels.item`) or spatio-temporal action
detection respectively. Then, it saves the meta keys defined in
the `meta_keys` in `data_sample.metainfo`, and packs the `data_sample`
into the `packed_results['data_samples']`.
"""Pack the inputs data.

Args:
collect_keys (tuple[str], optional): The keys to be collected
to ``packed_results['inputs']``. Defaults to ``
meta_keys (Sequence[str]): The meta keys to saved in the
`metainfo` of the `data_sample`.
Defaults to ``('img_shape', 'img_key', 'video_id', 'timestamp')``.
Expand Down Expand Up @@ -95,9 +86,7 @@ def transform(self, results: Dict) -> Dict:
bboxes=to_tensor(results['proposals']))

if 'label' in results:
label_data = LabelData()
label_data.item = to_tensor(results['label'])
data_sample.gt_labels = label_data
data_sample.set_gt_label(results['label'])

img_meta = {k: results[k] for k in self.meta_keys if k in results}
data_sample.set_metainfo(img_meta)
Expand Down
Loading
Loading