-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add stub cosmoflow * Add argparse to cosmoflow * Fix arg parse of input shape * Add model compilation * Guess get_data() * Refactor into build_model * Finish cosmoflow (see full commit message!) It seems cosmoflow is finished but it still doesn't work, because ATM `do_tensorflow.py` does only classification (see line 77, where it applies `to_categorical` to `y_train`). So to make it work - you have to comment out that line. * Remove to_categorical() * Change resnet, vggs, xception to use sparse CCE * Add cosmoflow test * Add cosmoflow/pytorch.py * Move `proc_params()` to separate file * Add support for channels_first * Finish pytorch inference for cosmoflow * Remove unused cosmoflow model * Add `Regression` class to `helpers_torch.py` * Make pytorch cosmoflow a regression * Add test/pytorch/test_cosmoflow.py Co-authored-by: Emil VATAI <vatai@x1carbon.subliminal>
- Loading branch information
Showing
6 changed files
with
130 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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 @@ | ||
import argparse | ||
import ast | ||
|
||
|
||
def proc_params(params, unparsed_args): | ||
"""Process args for CosmoFlow in (D1, D2, D3, Channels) tuple.""" | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--input_shape", default="128, 128, 128, 4") | ||
parser.add_argument("--target_size", default=4) | ||
parser.add_argument("--dropout", default=0) | ||
|
||
args, unparsed = parser.parse_known_args(unparsed_args) | ||
|
||
params["input_shape"] = ast.literal_eval(args.input_shape) | ||
params["target_size"] = args.target_size | ||
params["dropout"] = args.dropout | ||
|
||
assert unparsed == [] |
This file contains 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,64 @@ | ||
import numpy as np | ||
import torch.nn as nn | ||
|
||
from ..helpers_torch import Regression | ||
from .params import proc_params | ||
|
||
|
||
class scale_1p2(nn.Module): | ||
def forward(self, input): | ||
return 1.2 * input | ||
|
||
|
||
def build_model(input_shape, target_size, dropout=0): | ||
shape = np.array(input_shape[:-1], dtype=np.int) | ||
conv_args = { | ||
"in_channels": input_shape[-1], | ||
"out_channels": 16, | ||
"kernel_size": 2, | ||
} | ||
maxpool_args = dict(kernel_size=2) | ||
|
||
layers = [ | ||
nn.Conv3d(**conv_args), | ||
nn.LeakyReLU(), | ||
nn.MaxPool3d(**maxpool_args), | ||
] | ||
shape = (shape - 1) // 2 | ||
|
||
conv_args["in_channels"] = 16 | ||
for _ in range(4): | ||
layers += [ | ||
nn.Conv3d(**conv_args), | ||
nn.LeakyReLU(), | ||
nn.MaxPool3d(**maxpool_args), | ||
] | ||
shape = (shape - 1) // 2 | ||
|
||
flat_shape = np.prod(shape) * 16 | ||
layers += [ | ||
nn.Flatten(), | ||
nn.Dropout(dropout), | ||
# | ||
nn.Linear(flat_shape, 128), | ||
nn.LeakyReLU(), | ||
nn.Dropout(), | ||
# | ||
nn.Linear(128, 64), | ||
nn.LeakyReLU(), | ||
nn.Dropout(), | ||
# | ||
nn.Linear(64, target_size), | ||
nn.Tanh(), | ||
scale_1p2(), | ||
] | ||
|
||
return nn.Sequential(*layers) | ||
|
||
|
||
def get_kernel(params, unparsed_args): | ||
"""Construct the CosmoFlow 3D CNN model""" | ||
|
||
proc_params(params, unparsed_args) | ||
net = build_model(params["input_shape"], params["target_size"], params["dropout"]) | ||
return Regression(params, net) |
This file contains 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 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 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,25 @@ | ||
import logging | ||
import os | ||
import unittest | ||
|
||
from benchmarker.benchmarker import run | ||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" | ||
|
||
|
||
class CosmoflowTests(unittest.TestCase): | ||
def setUp(self): | ||
self.args = [ | ||
"--problem=cosmoflow", | ||
"--framework=pytorch", | ||
"--problem_size=1", | ||
"--batch_size=1", | ||
"--nb_epoch=1", | ||
] | ||
|
||
def test_cosmoflow(self): | ||
run(self.args) | ||
|
||
def test_cosmoflow_inference(self): | ||
run(self.args + ["--mode=inference"]) |