Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Video inference sample code #613

Open
siyangbing opened this issue Sep 1, 2020 · 10 comments
Open

Video inference sample code #613

siyangbing opened this issue Sep 1, 2020 · 10 comments

Comments

@siyangbing
Copy link

siyangbing commented Sep 1, 2020

I used my own video classification trained with the configuration of ucf101. It seems that there is a little overfitting. I want to use my code for inference, but I can't find a related tutorial. I can only find it, and I don't know what to do next. plaese help me @mannatsingh

from classy_vision.generic.util import load_checkpoint
from classy_vision.models import ClassyModel

# This is important: importing models here will register your custom models with Classy Vision
# so that it can instantiate them appropriately from the checkpoint file
# See more information at https://classyvision.ai/api/models.html#classy_vision.models.register_model
from classy_vision import models


# Update this with your actual directory:
checkpoint_dir = '/home/sucom/hdd_1T/project/video_rec_0831/ClassyVision/checkpoint_hand/model_phase-2198_end.torch'
checkpoint_data = load_checkpoint(checkpoint_dir)
model = ClassyModel.from_checkpoint(checkpoint_data)
@siyangbing
Copy link
Author

please help me , @mannatsingh

@mannatsingh
Copy link
Contributor

@siyangbing you're right, we don't have a tutorial for inference at the moment since the API isn't finalized.

If you want to evaluate a trained checkpoint on a test dataset, you can create a new task similar to the one you created for training, with the following differences -

task.set_test_only(True)
task.set_num_epochs(1)
task.set_dataset(test_dataset, "test")
task.set_checkpoint("path/to/checkpoint")

Note that this API is liable to change in the future.

Let me know if this works for you!

@siyangbing
Copy link
Author

siyangbing commented Sep 3, 2020

@siyangbing,您是对的,由于API尚未最终确定,我们目前没有用于推理的教程。

如果要评估测试数据集上经过训练的检查点,则可以创建一个新任务,该任务类似于您为训练而创建的任务,但有以下区别:

任务set_test_onlyTrue任务set_num_epochs1任务set_datasettest_dataset,“ test”)
 任务set_checkpoint(“路径//检查点”)

请注意,此API将来可能会更改。

让我知道这是否适合您!

Thank you very much for your reply, but for me, the task is too complicated for me to understand. I recently have a project that needs to identify a single video. Is it possible to infer a single video directly by loading a checkpoint instead of registering a data set and using tasks. What do I need to do with a single video before I can input the model for inference, please help me!
@mannatsingh

@siyangbing
Copy link
Author

@siyangbing,您是对的,由于API尚未最终确定,我们目前没有用于推理的教程。
如果要评估测试数据集上经过训练的检查点,则可以创建一个新任务,该任务类似于您为训练而创建的任务,但有以下区别:

任务set_test_onlyTrue任务set_num_epochs1任务set_datasettest_dataset,“ test”)
 任务set_checkpoint(“路径//检查点”)

请注意,此API将来可能会更改。
让我知道这是否适合您!

Thank you very much for your reply, but for me, the task is too complicated for me to understand. I recently have a project that needs to identify a single video. Is it possible to infer a single video directly by loading a checkpoint instead of registering a data set and using tasks. What do I need to do with a single video before I can input the model for inference, please help me!
@mannatsingh

please help me , the project is perfect if I can infer one video with my own model!

@mannatsingh
Copy link
Contributor

@stephenyan1231 do you have any suggestions for the best way to do this?

@siyangbing
Copy link
Author

@stephenyan1231 do you have any suggestions for the best way to do this?

it will get support in future relase?
@mannatsingh

@mannatsingh
Copy link
Contributor

@siyangbing we have good support for this for images, but that's missing a tutorial. For videos, we need to add better support!

@siyangbing
Copy link
Author

siyangbing commented Sep 9, 2020

@siyangbing我们对此图像有很好的支持,但是缺少教程。对于视频,我们需要添加更好的支持!

If there is support for video inference in the future, please let me know @mannatsingh

@shinianzhihou
Copy link

state_path = "YOUR/PATH/TO/checkpoints/checkpoint.torch"
state = torch.load(state_path)
model = ClassyModel.from_checkpoint(state)
dataset = build_dataset(state["input_args"]["config"]["dataset"]["test"])

dataset.batchsize_per_replica = 10 # to test in the single GPU with small memory

targets,outputs = [],[]
model = model.cuda().eval()
for idx,data in enumerate(dataset.iterator()):
    output = model(data['input'].cuda()).argmax(dim=1)
    outputs += list(output.detach().cpu().numpy())
    targets += list(data["target"].numpy())

from sklearn.metrics import classification_report
print(classification_report(targets, outputs, target_names=CLASSES))

This is how I make my inference. May be useful

@mannatsingh
Copy link
Contributor

Thanks for the code sample, @shinianzhihou !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants