Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
reBEN Training Scripts
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RSiM
reBEN Training Scripts
Commits
fee1bd29
Commit
fee1bd29
authored
3 weeks ago
by
Leonard Wayne Hackel
Browse files
Options
Downloads
Patches
Plain Diff
Adding an inference example code snipped
parent
7f03609c
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
scripts/inference_example.py
+37
-0
37 additions, 0 deletions
scripts/inference_example.py
with
37 additions
and
0 deletions
scripts/inference_example.py
0 → 100644
+
37
−
0
View file @
fee1bd29
"""
This script loads a pretrained model from the Huggingface Hub and evaluates it on a random input.
"""
import
torch
from
huggingface_hub
import
HfApi
from
reben_publication.BigEarthNetv2_0_ImageClassifier
import
BigEarthNetv2_0_ImageClassifier
__author__
=
"
Leonard Hackel - BIFOLD/RSiM TU Berlin
"
def
download_and_evaluate_model
(
model_name
:
str
=
"
BIFOLD-BigEarthNetv2-0/resnet50-s2-v0.2.0
"
,
batch_size
:
int
=
4
,
):
# Check if the model exists in the Huggingface Hub
api
=
HfApi
()
assert
api
.
repo_exists
(
model_name
),
f
"
Model
{
model_name
}
does not exist in the Huggingface Hub.
"
# Load the model
model
=
BigEarthNetv2_0_ImageClassifier
.
from_pretrained
(
model_name
)
model
.
eval
()
# Test the model with a random input
channels
=
model
.
config
.
channels
image_size
=
model
.
config
.
image_size
x
=
torch
.
randn
(
batch_size
,
channels
,
image_size
,
image_size
)
print
(
"
Input:
"
,
x
.
shape
)
y
=
model
(
x
)
assert
y
.
shape
[
0
]
==
batch_size
,
f
"
Expected batch size
{
batch_size
}
, got
{
y
.
shape
[
0
]
}
.
"
print
(
"
Output:
"
,
y
.
shape
)
if
__name__
==
"
__main__
"
:
download_and_evaluate_model
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment