generated from HephaestusProject/template
-
Notifications
You must be signed in to change notification settings - Fork 0
feature#8 #9
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
Open
seungheondoh
wants to merge
1
commit into
master
Choose a base branch
from
feature#8
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feature#8 #9
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
version: harmoniccnn | ||
type: HarmoincCNN | ||
params: | ||
# CNN Parameters | ||
n_channels: 128 | ||
sample_rate: 16000 | ||
n_fft : 513 | ||
n_mels : 128 | ||
n_class : 50 | ||
n_harmonic : 6 | ||
semitone_scale : 2 | ||
learn_bw : only_Q |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
version: pv00 | ||
type: DataPipeline | ||
dataset: | ||
type: MTATDataset | ||
path: ../dataset/mtat | ||
input_length: 80000 | ||
dataloader: | ||
type: DataLoader | ||
params: | ||
batch_size: 16 | ||
num_workers: 8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
version: rv00 | ||
type: AutotaggingRunner | ||
optimizer: | ||
type: Adam | ||
params: | ||
learning_rate: 1e-5 | ||
scale_factor: 5 | ||
scheduler: | ||
type: ExponentialLR | ||
params: | ||
gamma: 0.95 | ||
trainer: | ||
type: Trainer | ||
params: | ||
max_epochs: 100 | ||
gpus: 1 | ||
distributed_backend: dp # train.py: ddp, evaluate.py: dp | ||
benchmark: False | ||
deterministic: True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,88 @@ | ||
""" | ||
This script was made by Nick at 19/07/20. | ||
To implement code for evaluating your model. | ||
""" | ||
from argparse import ArgumentParser, Namespace | ||
import json | ||
from pathlib import Path | ||
|
||
from omegaconf import DictConfig, OmegaConf | ||
from pytorch_lightning import Trainer, seed_everything | ||
import torch | ||
|
||
from src.model.net import HarmonicCNN | ||
from src.task.pipeline import DataPipeline | ||
from src.task.runner import AutotaggingRunner | ||
|
||
|
||
def get_config(args: Namespace) -> DictConfig: | ||
parent_config_dir = Path("conf") | ||
child_config_dir = parent_config_dir / args.dataset | ||
model_config_dir = child_config_dir / "model" | ||
pipeline_config_dir = child_config_dir / "pipeline" | ||
runner_config_dir = child_config_dir / "runner" | ||
|
||
config = OmegaConf.create() | ||
model_config = OmegaConf.load(model_config_dir / f"{args.model}.yaml") | ||
pipeline_config = OmegaConf.load(pipeline_config_dir / f"{args.pipeline}.yaml") | ||
runner_config = OmegaConf.load(runner_config_dir / f"{args.runner}.yaml") | ||
config.update(model=model_config, pipeline=pipeline_config, runner=runner_config) | ||
return config | ||
|
||
def main(args) -> None: | ||
seed_everything(42) | ||
config = get_config(args) | ||
|
||
# prepare dataloader | ||
pipeline = DataPipeline(pipline_config=config.pipeline) | ||
|
||
dataset = pipeline.get_dataset( | ||
pipeline.dataset_builder, | ||
config.pipeline.dataset.path, | ||
args.type, | ||
config.pipeline.dataset.input_length | ||
) | ||
dataloader = pipeline.get_dataloader( | ||
dataset, | ||
shuffle=False, | ||
drop_last=True, | ||
**pipeline.pipeline_config.dataloader.params, | ||
) | ||
model = HarmonicCNN(**config.model.params) | ||
runner = AutotaggingRunner(model, config.runner) | ||
|
||
checkpoint_path = ( | ||
f"exp/{args.dataset}/{args.model}/{args.runner}/{args.checkpoint}.ckpt" | ||
) | ||
state_dict = torch.load(checkpoint_path) | ||
runner.load_state_dict(state_dict.get("state_dict")) | ||
|
||
trainer = Trainer( | ||
**config.runner.trainer.params, logger=False, checkpoint_callback=False | ||
) | ||
results_path = Path(f"exp/{args.dataset}/{args.model}/{args.runner}/results.json") | ||
|
||
if results_path.exists(): | ||
with open(results_path, mode="r") as io: | ||
results = json.load(io) | ||
|
||
result = trainer.test(runner, test_dataloaders=dataloader) | ||
results.update({"checkpoint": args.checkpoint, f"{args.type}": result}) | ||
|
||
else: | ||
results = {} | ||
result = trainer.test(runner, test_dataloaders=dataloader) | ||
results.update({"checkpoint": args.checkpoint, f"{args.type}": result}) | ||
|
||
with open( | ||
f"exp/{args.dataset}/{args.model}/{args.runner}/results.json", mode="w" | ||
) as io: | ||
json.dump(results, io, indent=4) | ||
|
||
if __name__ == "__main__": | ||
parser = ArgumentParser() | ||
parser.add_argument("--type", default="TEST", type=str, choices=["TRAIN", "VALID", "TEST"]) | ||
parser.add_argument("--model", default="HarmonicCNN", type=str) | ||
parser.add_argument("--dataset", default="mtat", type=str, choices=["mtat"]) | ||
parser.add_argument("--pipeline", default="pv00", type=str) | ||
parser.add_argument("--runner", default="rv00", type=str) | ||
parser.add_argument("--reproduce", default=False, action="store_true") | ||
parser.add_argument("--checkpoint", default="epoch=37-roc_auc=0.8806-pr_auc=0.3905", type=str) | ||
args = parser.parse_args() | ||
main(args) |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
benchmark: false | ||
deterministic: true | ||
distributed_backend: dp | ||
gamma: 0.95 | ||
gpus: 1 | ||
learning_rate: 1.0e-05 | ||
max_epochs: 100 | ||
scale_factor: 5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"checkpoint": "epoch=37-roc_auc=0.8806-pr_auc=0.3905", | ||
"TEST": [ | ||
{ | ||
"val_loss": 0.15560948848724365, | ||
"roc_auc": 0.8677473068237305, | ||
"pr_auc": 0.3685624301433563 | ||
} | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
""" | ||
This script was made by Nick at 19/07/20. | ||
To implement code for metric (e.g. NLL loss). | ||
""" | ||
import torch.nn as nn | ||
from pytorch_lightning.metrics.sklearns import AUROC, AveragePrecision | ||
|
||
roc_auc = AUROC(average='macro') | ||
average_precision = AveragePrecision(average='macro') | ||
|
||
def get_auc(y_score, y_true): | ||
# for Validation sanity check: | ||
if y_true.shape[0] == 1: | ||
return 0,0 | ||
else: | ||
roc_aucs = roc_auc(y_score.flatten(0,1), y_true.flatten(0,1)) | ||
pr_aucs = average_precision(y_score.flatten(0,1), y_true.flatten(0,1)) | ||
return roc_aucs, pr_aucs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import pickle | ||
|
||
from omegaconf import DictConfig | ||
from typing import Optional, Callable | ||
from torch.utils.data import DataLoader, Dataset | ||
from pytorch_lightning import LightningDataModule | ||
from ..data import MTATDataset | ||
|
||
class DataPipeline(LightningDataModule): | ||
def __init__(self, pipline_config: DictConfig) -> None: | ||
super(DataPipeline, self).__init__() | ||
self.pipeline_config = pipline_config | ||
self.dataset_builder = MTATDataset | ||
|
||
def setup(self, stage: Optional[str] = None): | ||
if stage == "fit" or stage is None: | ||
self.train_dataset = DataPipeline.get_dataset( | ||
self.dataset_builder, | ||
self.pipeline_config.dataset.path, | ||
"TRAIN", | ||
self.pipeline_config.dataset.input_length | ||
) | ||
|
||
self.val_dataset = DataPipeline.get_dataset(self.dataset_builder, | ||
self.pipeline_config.dataset.path, | ||
"VALID", | ||
self.pipeline_config.dataset.input_length) | ||
|
||
if stage == "test" or stage is None: | ||
self.test_dataset = DataPipeline.get_dataset(self.dataset_builder, | ||
self.pipeline_config.dataset.path, | ||
"TEST", | ||
self.pipeline_config.dataset.input_length) | ||
|
||
def train_dataloader(self) -> DataLoader: | ||
return DataPipeline.get_dataloader(self.train_dataset, | ||
batch_size=self.pipeline_config.dataloader.params.batch_size, | ||
num_workers=self.pipeline_config.dataloader.params.num_workers, | ||
drop_last=True, | ||
shuffle=True) | ||
|
||
def val_dataloader(self) -> DataLoader: | ||
return DataPipeline.get_dataloader(self.val_dataset, | ||
batch_size=self.pipeline_config.dataloader.params.batch_size, | ||
num_workers=self.pipeline_config.dataloader.params.num_workers, | ||
drop_last=True, | ||
shuffle=False) | ||
|
||
def test_dataloader(self) -> DataLoader: | ||
return DataPipeline.get_dataloader(self.test_dataset, | ||
batch_size=self.pipeline_config.dataloader.params.batch_size, | ||
num_workers=self.pipeline_config.dataloader.params.num_workers, | ||
drop_last=True, | ||
shuffle=False) | ||
|
||
@classmethod | ||
def get_dataset(cls, dataset_builder:Callable, root, split, length) -> Dataset: | ||
dataset = dataset_builder(root, split, length) | ||
return dataset | ||
|
||
@classmethod | ||
def get_dataloader(cls, dataset: Dataset, batch_size: int, num_workers: int, shuffle: bool, drop_last: bool, | ||
**kwargs) -> DataLoader: | ||
return DataLoader(dataset, | ||
batch_size=batch_size, | ||
num_workers=num_workers, | ||
shuffle=shuffle, | ||
drop_last=drop_last, | ||
**kwargs) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 구문의 의도가 무엇인지 알 수 있을까요?