Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
outillage
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Alice Brenon
outillage
Commits
38de3c27
Commit
38de3c27
authored
1 year ago
by
Alice Brenon
Browse files
Options
Downloads
Patches
Plain Diff
Take GPU detection out into a base class
parent
02253ba3
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
scripts/ML/gpu.py
+10
-0
10 additions, 0 deletions
scripts/ML/gpu.py
scripts/ML/predict.py
+3
-10
3 additions, 10 deletions
scripts/ML/predict.py
with
13 additions
and
10 deletions
scripts/ML/gpu.py
0 → 100644
+
10
−
0
View file @
38de3c27
import
torch
class
WithGPU
:
def
__init__
(
self
):
if
torch
.
cuda
.
is_available
():
print
(
'
We will use the GPU:
'
,
torch
.
cuda
.
get_device_name
(
0
))
self
.
device
=
torch
.
device
(
"
cuda
"
)
else
:
print
(
'
No GPU available, using the CPU instead.
'
)
self
.
device
=
torch
.
device
(
"
cpu
"
)
This diff is collapsed.
Click to expand it.
scripts/ML/predict.py
+
3
−
10
View file @
38de3c27
#!/usr/bin/env python3
from
gpu
import
WithGPU
import
numpy
import
pandas
import
pickle
import
sklearn
from
sys
import
argv
import
torch
from
tqdm
import
tqdm
from
transformers
import
BertForSequenceClassification
,
BertTokenizer
,
TextClassificationPipeline
class
Classifier
:
class
Classifier
(
WithGPU
)
:
"""
A class wrapping all the different models and classes used throughout a
classification task:
...
...
@@ -22,12 +22,11 @@ class Classifier:
containing the texts to classify
"""
def
__init__
(
self
,
root_path
):
self
.
device
=
torch
.
device
(
"
cuda
"
if
torch
.
cuda
.
is_available
()
else
"
cpu
"
)
WithGPU
.
__init__
(
self
)
self
.
_init_tokenizer
()
self
.
_init_model
(
root_path
)
self
.
_init_pipe
()
self
.
_init_encoder
(
f
"
{
root_path
}
/label_encoder.pkl
"
)
self
.
log
()
def
_init_model
(
self
,
path
):
bert
=
BertForSequenceClassification
.
from_pretrained
(
path
)
...
...
@@ -48,12 +47,6 @@ class Classifier:
with
open
(
path
,
'
rb
'
)
as
pickled
:
self
.
encoder
=
pickle
.
load
(
pickled
)
def
log
(
self
):
if
self
.
device
.
type
==
'
cpu
'
:
print
(
'
No GPU available, using the CPU instead.
'
)
else
:
print
(
'
We will use the GPU:
'
,
torch
.
cuda
.
get_device_name
(
0
))
def
__call__
(
self
,
text_generator
):
tokenizer_kwargs
=
{
'
padding
'
:
True
,
'
truncation
'
:
True
,
'
max_length
'
:
512
}
predictions
=
[]
...
...
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