This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
/
test_model.py
129 lines (107 loc) · 4.34 KB
/
test_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any
import pytest
import pytorch_lightning as pl
import torch
from torch import nn
from torch.nn import functional as F
from flash import ClassificationTask
from flash.tabular import TabularClassifier
from flash.text import SummarizationTask, TextClassifier
from flash.vision import ImageClassifier
# ======== Mock functions ========
class DummyDataset(torch.utils.data.Dataset):
def __getitem__(self, index: int) -> Any:
return torch.rand(1, 28, 28), torch.randint(10, size=(1, )).item()
def __len__(self) -> int:
return 100
# ================================
@pytest.mark.parametrize("metrics", [None, pl.metrics.Accuracy(), {"accuracy": pl.metrics.Accuracy()}])
def test_classificationtask_train(tmpdir: str, metrics: Any):
model = nn.Sequential(nn.Flatten(), nn.Linear(28 * 28, 10), nn.LogSoftmax())
train_dl = torch.utils.data.DataLoader(DummyDataset())
val_dl = torch.utils.data.DataLoader(DummyDataset())
task = ClassificationTask(model, F.nll_loss, metrics=metrics)
trainer = pl.Trainer(fast_dev_run=True, default_root_dir=tmpdir)
result = trainer.fit(task, train_dl, val_dl)
assert result
result = trainer.test(task, val_dl)
assert "test_nll_loss" in result[0]
def test_classificationtask_task_predict():
model = nn.Sequential(nn.Flatten(), nn.Linear(28 * 28, 10))
task = ClassificationTask(model)
ds = DummyDataset()
expected = list(range(10))
# single item
x0, _ = ds[0]
pred0 = task.predict(x0)
assert pred0[0] in expected
# list
x1, _ = ds[1]
pred1 = task.predict([x0, x1])
assert all(c in expected for c in pred1)
assert pred0[0] == pred1[0]
def test_classificationtask_trainer_predict(tmpdir):
model = nn.Sequential(nn.Flatten(), nn.Linear(28 * 28, 10))
task = ClassificationTask(model)
ds = DummyDataset()
batch_size = 3
predict_dl = torch.utils.data.DataLoader(ds, batch_size=batch_size, collate_fn=task.data_pipeline.collate_fn)
trainer = pl.Trainer(default_root_dir=tmpdir)
expected = list(range(10))
predictions = trainer.predict(task, predict_dl)
predictions = predictions[0] # TODO(tchaton): why do we need this?
for pred in predictions[:-1]:
# check batch sizes are correct
assert len(pred) == batch_size
assert all(c in expected for c in pred)
# check size of last batch (not full)
assert len(predictions[-1]) == len(ds) % batch_size
def test_task_datapipeline_save(tmpdir):
model = nn.Sequential(nn.Flatten(), nn.Linear(28 * 28, 10))
train_dl = torch.utils.data.DataLoader(DummyDataset())
task = ClassificationTask(model, F.nll_loss)
# to check later
task.data_pipeline.test = True
# generate a checkpoint
trainer = pl.Trainer(
default_root_dir=tmpdir,
limit_train_batches=1,
max_epochs=1,
progress_bar_refresh_rate=0,
weights_summary=None,
logger=False,
)
trainer.fit(task, train_dl)
path = str(tmpdir / "model.ckpt")
trainer.save_checkpoint(path)
# load from file
task = ClassificationTask.load_from_checkpoint(path, model=model)
assert task.data_pipeline.test
@pytest.mark.parametrize(
["cls", "filename"],
[
(ImageClassifier, "image_classification_model.pt"),
(TabularClassifier, "tabular_classification_model.pt"),
(TextClassifier, "text_classification_model.pt"),
(SummarizationTask, "summarization_model_xsum.pt"),
# (TranslationTask, "translation_model_en_ro.pt"), todo: reduce model size or create CI friendly file size
]
)
def test_model_download(tmpdir, cls, filename):
url = "https://flash-weights.s3.amazonaws.com/"
with tmpdir.as_cwd():
task = cls.load_from_checkpoint(url + filename)
assert isinstance(task, cls)