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

running lightly magic, the output is weird #413

Merged
merged 3 commits into from
Jun 17, 2021
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: 2 additions & 0 deletions lightly/cli/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ trainer:
gpus: 1 # Number of gpus to use for training.
max_epochs: 100 # Number of epochs to train for.
precision: 32 # If set to 16, will use half-precision.
weights_summary: 'top' # how to print the model architecture, one of {None, top, full},
#see https://pytorch-lightning.readthedocs.io/en/stable/common/trainer.html#weights-summary

# checkpoint_callback namespace: Modify the checkpoint callback
checkpoint_callback:
Expand Down
8 changes: 8 additions & 0 deletions lightly/cli/train_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ def _train_cli(cfg, is_cli_call=True):
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False

if cfg["trainer"]["weights_summary"] == "None":
cfg["trainer"]["weights_summary"] = None

if torch.cuda.is_available():
device = 'cuda'
elif cfg['trainer'] and cfg['trainer']['gpus']:
device = 'cpu'
cfg['trainer']['gpus'] = 0
else:
device = 'cpu'

if cfg['loader']['batch_size'] < 64:
msg = 'Training a self-supervised model with a small batch size: {}! '
Expand Down Expand Up @@ -152,6 +157,9 @@ def train_cli(cfg):
>>>
>>> # train model for 10 epochs
>>> lightly-train input_dir=data/ trainer.max_epochs=10
>>>
>>> # print a full summary of the model
>>> lightly-train input_dir=data/ trainer.weights_summary=full

"""
return _train_cli(cfg)
Expand Down
1 change: 1 addition & 0 deletions lightly/embedding/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def train_embedding(self, **kwargs):
min_epochs: (int) Minimum number of epochs to train
max_epochs: (int) Maximum number of epochs to train
gpus: (int) number of gpus to use
weights_summary: (str) how to print a summary of the model and weights (number, size)

Returns:
A trained encoder, ready for embedding datasets.
Expand Down
75 changes: 75 additions & 0 deletions tests/cli/test_cli_train.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import os
import re
import sys
import tempfile

import torchvision
from hydra.experimental import compose, initialize

import lightly
from tests.api_workflow.mocked_api_workflow_client import MockedApiWorkflowSetup, MockedApiWorkflowClient


class TestCLITrain(MockedApiWorkflowSetup):

@classmethod
def setUpClass(cls) -> None:
sys.modules["lightly.cli.upload_cli"].ApiWorkflowClient = MockedApiWorkflowClient

def setUp(self):
MockedApiWorkflowSetup.setUp(self)
self.create_fake_dataset()
with initialize(config_path="../../lightly/cli/config", job_name="test_app"):
self.cfg = compose(config_name="config", overrides=[
"token='123'",
f"input_dir={self.folder_path}",
"trainer.max_epochs=0"
])

def create_fake_dataset(self):
n_data = 5
self.dataset = torchvision.datasets.FakeData(size=n_data, image_size=(3, 32, 32))

self.folder_path = tempfile.mkdtemp()
sample_names = [f'img_{i}.jpg' for i in range(n_data)]
self.sample_names = sample_names
for sample_idx in range(n_data):
data = self.dataset[sample_idx]
path = os.path.join(self.folder_path, sample_names[sample_idx])
data[0].save(path)

def parse_cli_string(self, cli_words: str):
cli_words = cli_words.replace("lightly-train ", "")
cli_words = re.split("=| ", cli_words)
assert len(cli_words) % 2 == 0
dict_keys = cli_words[0::2]
dict_values = cli_words[1::2]
for key, value in zip(dict_keys, dict_values):
value = value.strip('\"')
value = value.strip('\'')
key_parts = key.split(".")
if len(key_parts) == 1:
self.cfg[key_parts[0]]= value
elif len(key_parts) == 2:
self.cfg[key_parts[0]][key_parts[1]] = value
else:
raise ValueError

def test_parse_cli_string(self):
cli_string = "lightly-train trainer.weights_summary=top"
self.parse_cli_string(cli_string)
assert self.cfg["trainer"]["weights_summary"] == 'top'

def test_train_weights_summary(self):
for weights_summary in ["None", "top", "full"]:
cli_string = f"lightly-train trainer.weights_summary={weights_summary}"
with self.subTest(cli_string):
self.parse_cli_string(cli_string)
lightly.cli.train_cli(self.cfg)

def tearDown(self) -> None:
for filename in ["embeddings.csv", "embeddings_sorted.csv"]:
try:
os.remove(filename)
except FileNotFoundError:
pass