Skip to content

Commit

Permalink
model_summary returns instead of prints
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Dec 28, 2018
1 parent ba5ba00 commit cc6673b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 38 deletions.
6 changes: 2 additions & 4 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ Github. Parentheses after an item show the name or github id of the contributor
of that change.




## 1.0.39.dev0 (Work In Progress)

### Breaking changes:
Expand All @@ -21,9 +19,9 @@ of that change.

### New:

- `Learner.to_fp32()` to go back to FP32 precision mode.
- `Learner.to_fp32()` to go back to FP32 precision mode
- `cont_cat_split` function to automatically get categorical/continuous variables (thanks to RealLankinen)
- plenty of new metrics thanks to SvenBecker: mse/mean_squared_error, mae/mean_absolute_error, rmse/root_mean_squared_error, msle/ mean_squared_logarithmic_error, explained_variance, r2_score, top_k_accuracy, KappaScore, MatthewsCorreff, Precision, Recall, FBeta
- Lots of new metrics thanks to Sven Becker: `mse/mean_squared_error`, `mae/mean_absolute_error`, `rmse/root_mean_squared_error`, `msle/ mean_squared_logarithmic_error`, `explained_variance`, `r2_score`, `top_k_accuracy`, `KappaScore`, `MatthewsCorreff`, `Precision`, `Recall`, `FBeta`

### Changed:

Expand Down
10 changes: 1 addition & 9 deletions conda/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,14 @@ requirements:
- nvidia-ml-py3
- packaging
- pillow
- python
- requests
- scipy
- spacy ==2.0.16
- regex ==2018.8.29
- thinc ==6.12.0
- cymem ==2.0.2
- spacy >=2.0.18
- typing
- pyyaml
- pytorch >=1.0
- torchvision

# Need to handle CPU vs GPU still

# Stuff only used for doc() and dev-time:
#- ipython
#- jupyter
Expand All @@ -81,8 +75,6 @@ requirements:
#- nbformat
#- traitlets

# removed these and instead are asking users to install those directly until a proper pytorch-1.0.0 and the corresponding torchvision are released on conda. otherwise can't support these two sets:

test:
imports:
- fastai
Expand Down
7 changes: 3 additions & 4 deletions fastai/basic_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ def proc_batch(self,b:Tensor)->Tensor:

def __iter__(self):
"Process and returns items from `DataLoader`."
for b in self.dl:
#y = b[1][0] if is_listy(b[1]) else b[1] # XXX: Why is this line here?
yield self.proc_batch(b)
for b in self.dl: yield self.proc_batch(b)

@classmethod
def create(cls, dataset:Dataset, bs:int=64, shuffle:bool=False, device:torch.device=defaults.device,
Expand Down Expand Up @@ -189,7 +187,7 @@ def empty_val(self)->bool:
if not hasattr(self, 'valid_dl') or self.valid_dl is None: return True
if hasattr(self.valid_ds, 'items') and len(self.valid_ds.items) == 0: return True
return (len(self.valid_ds) == 0)

@property
def batch_size(self): return self.train_dl.batch_size
@batch_size.setter
Expand All @@ -216,3 +214,4 @@ def sanity_check(self):
except: pass
warn(message)
print(final_message)

17 changes: 9 additions & 8 deletions fastai/callbacks/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,21 @@ def model_summary(m:Collection[nn.Module], n:int=70):
"Print a summary of `m` using a output text width of `n` chars"
info = layers_info(m)
header = ["Layer (type)", "Output Shape", "Param #", "Trainable"]
print("=" * n)
print(f"{header[0]:<20} {header[1]:<20} {header[2]:<10} {header[3]:<10}")
print("=" * n)
res = "=" * n + "\n"
res += f"{header[0]:<20} {header[1]:<20} {header[2]:<10} {header[3]:<10}\n"
res += "=" * n + "\n"
total_params = 0
total_trainable_params = 0
for layer, size, params, trainable in info:
total_params += int(params)
total_trainable_params += int(params) * trainable
params, size, trainable = str(params), str(list(size)), str(trainable)
print(f"{layer:<20} {size:<20} {params:<10} {trainable:<10}")
print("_" * n)
print("\nTotal params: ", total_params)
print("Total trainable params: ", total_trainable_params)
print("Total non-trainable params: ", total_params - total_trainable_params)
res += f"{layer:<20} {size:<20} {params:<10} {trainable:<10}\n"
res += "_" * n + "\n"
res += f"\nTotal params: {total_params}\n"
res += f"Total trainable params: {total_trainable_params}\n"
res += f"Total non-trainable params: {total_params - total_trainable_params}\n"
return res

Learner.summary = model_summary

6 changes: 1 addition & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ def to_list(buffer): return list(filter(None, map(cleanup, buffer.splitlines()))
# dependencies to skip for now:
# - cupy - is only required for QRNNs - sgugger thinks later he will get rid of this dep.
#
# XXX: when spacy==2.0.18 is on anaconda channel, put it in place (it's already on pypi) and remove its deps: cymem, regex, thinc (and update meta.yaml with the same)
requirements = to_list("""
bottleneck # performance-improvement for numpy
cymem==2.0.2 # remove once spacy==2.0.18 is on anaconda channel
dataclasses ; python_version<'3.7'
fastprogress>=0.1.18
matplotlib
Expand All @@ -41,11 +39,9 @@ def to_list(buffer): return list(filter(None, map(cleanup, buffer.splitlines()))
packaging
Pillow
pyyaml
regex==2018.01.10 # remove once spacy==2.0.18 is on anaconda channel
requests
scipy
spacy==2.0.16
thinc==6.12.0 # remove once spacy==2.0.18 is on anaconda channel
spacy>=2.0.18
torch
torchvision
typing
Expand Down
5 changes: 3 additions & 2 deletions tests/test_callbacks_csv_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ def test_logger():
csv_df = learn.csv_logger.read_logged_file()
recorder_df = create_metrics_dataframe(learn)
pd.testing.assert_frame_equal(csv_df, recorder_df, check_exact=False, check_less_precise=True)
stdout_df = convert_into_dataframe(buffer)
pd.testing.assert_frame_equal(csv_df, stdout_df, check_exact=False, check_less_precise=True)
# Disabled since this doesn't work under `pytest -s`
# stdout_df = convert_into_dataframe(buffer)
# pd.testing.assert_frame_equal(csv_df, stdout_df, check_exact=False, check_less_precise=True)

@pytest.fixture(scope="module", autouse=True)
def cleanup(request):
Expand Down
12 changes: 6 additions & 6 deletions tests/test_callbacks_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ def test_model_summary_vision():
path = untar_data(URLs.MNIST_TINY)
data = ImageDataBunch.from_folder(path, ds_tfms=([], []), bs=2)
learn = create_cnn(data, models.resnet18, metrics=accuracy)
model_summary(learn)
_ = model_summary(learn)

@pytest.mark.xfail(reason = "Expected Fail, text models not supported yet.")
def test_model_summary_text():
path = untar_data(URLs.IMDB_SAMPLE)
data_lm = TextLMDataBunch.from_csv(path, 'texts.csv')
learn = language_model_learner(data_lm, pretrained_model=None)
model_summary(learn)
_ = model_summary(learn)

def test_model_summary_tabular():
path = untar_data(URLs.ADULT_SAMPLE)
Expand All @@ -31,7 +31,7 @@ def test_model_summary_tabular():
.label_from_df(cols=dep_var)
.databunch(bs=2))
learn = tabular_learner(data, layers=[200,100], metrics=accuracy)
model_summary(learn)
_ = model_summary(learn)

def test_model_summary_collab():
path = untar_data(URLs.ML_SAMPLE)
Expand All @@ -40,10 +40,10 @@ def test_model_summary_collab():
data = CollabDataBunch.from_df(ratings, seed=42, bs=2)
y_range = [0,5.5]
learn = collab_learner(data, n_factors=50, y_range=y_range)
model_summary(learn)
_ = model_summary(learn)

def test_model_summary_nn_module():
model_summary(nn.Conv2d(16,16,3,padding=1))
_ = model_summary(nn.Conv2d(16,16,3,padding=1))

def test_model_summary_nn_modules():
class BasicBlock(nn.Module):
Expand All @@ -55,5 +55,5 @@ def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
return x
model_summary(BasicBlock())
_ = model_summary(BasicBlock())

0 comments on commit cc6673b

Please sign in to comment.