Skip to content

Commit

Permalink
feat/layers: implement Into<LayerType> for all layers
Browse files Browse the repository at this point in the history
  • Loading branch information
hobofan committed Feb 29, 2016
1 parent aaacc1e commit b9a4e8f
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 44 deletions.
76 changes: 38 additions & 38 deletions examples/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,34 +117,34 @@ fn bench_alexnet() {
cfg.add_input("data", &vec![128, 3, 224, 224]);

let conv1_layer_cfg = ConvolutionConfig { num_output: 64, filter_shape: vec![11], padding: vec![2], stride: vec![4], axis: None };
cfg.add_layer(LayerConfig::new("conv1", LayerType::Convolution(conv1_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv1", conv1_layer_cfg));
cfg.add_layer(LayerConfig::new("conv1/relu", LayerType::ReLU));
let pool1_layer_cfg = PoolingConfig { mode: PoolingMode::Max, filter_shape: vec![3], stride: vec![2], padding: vec![0] };
cfg.add_layer(LayerConfig::new("pool1", LayerType::Pooling(pool1_layer_cfg)));
cfg.add_layer(LayerConfig::new("pool1", pool1_layer_cfg));

let conv2_layer_cfg = ConvolutionConfig { num_output: 192, filter_shape: vec![5], padding: vec![2], stride: vec![1], axis: None };
cfg.add_layer(LayerConfig::new("conv2", LayerType::Convolution(conv2_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv2", conv2_layer_cfg));
cfg.add_layer(LayerConfig::new("conv2/relu", LayerType::ReLU));
let pool2_layer_cfg = PoolingConfig { mode: PoolingMode::Max, filter_shape: vec![3], stride: vec![2], padding: vec![0] };
cfg.add_layer(LayerConfig::new("pool2", LayerType::Pooling(pool2_layer_cfg)));
cfg.add_layer(LayerConfig::new("pool2", pool2_layer_cfg));

let conv3_layer_cfg = ConvolutionConfig { num_output: 384, filter_shape: vec![3], padding: vec![1], stride: vec![1], axis: None };
cfg.add_layer(LayerConfig::new("conv3", LayerType::Convolution(conv3_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv3", conv3_layer_cfg));
cfg.add_layer(LayerConfig::new("conv3/relu", LayerType::ReLU));

let conv4_layer_cfg = ConvolutionConfig { num_output: 256, filter_shape: vec![3], padding: vec![1], stride: vec![1], axis: None };
cfg.add_layer(LayerConfig::new("conv4", LayerType::Convolution(conv4_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv4", conv4_layer_cfg));
cfg.add_layer(LayerConfig::new("conv4/relu", LayerType::ReLU));

let conv5_layer_cfg = ConvolutionConfig { num_output: 256, filter_shape: vec![3], padding: vec![1], stride: vec![1], axis: None };
cfg.add_layer(LayerConfig::new("conv5", LayerType::Convolution(conv5_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv5", conv5_layer_cfg));
cfg.add_layer(LayerConfig::new("conv5/relu", LayerType::ReLU));
let pool3_layer_cfg = PoolingConfig { mode: PoolingMode::Max, filter_shape: vec![3], stride: vec![2], padding: vec![0] };
cfg.add_layer(LayerConfig::new("pool3", LayerType::Pooling(pool3_layer_cfg)));
cfg.add_layer(LayerConfig::new("pool3", pool3_layer_cfg));

cfg.add_layer(LayerConfig::new("fc1", LayerType::Linear(LinearConfig { output_size: 4096 })));
cfg.add_layer(LayerConfig::new("fc2", LayerType::Linear(LinearConfig { output_size: 4096 })));
cfg.add_layer(LayerConfig::new("fc3", LayerType::Linear(LinearConfig { output_size: 1000 })));
cfg.add_layer(LayerConfig::new("fc1", LinearConfig { output_size: 4096 }));
cfg.add_layer(LayerConfig::new("fc2", LinearConfig { output_size: 4096 }));
cfg.add_layer(LayerConfig::new("fc3", LinearConfig { output_size: 1000 }));

let backend = cuda_backend();
// let native_backend = native_backend();
Expand Down Expand Up @@ -199,34 +199,34 @@ fn bench_overfeat() {
cfg.add_input("data", &vec![128, 3, 231, 231]);

let conv1_layer_cfg = ConvolutionConfig { num_output: 96, filter_shape: vec![11], padding: vec![0], stride: vec![4], axis: None };
cfg.add_layer(LayerConfig::new("conv1", LayerType::Convolution(conv1_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv1", conv1_layer_cfg));
cfg.add_layer(LayerConfig::new("conv1/relu", LayerType::ReLU));
let pool1_layer_cfg = PoolingConfig { mode: PoolingMode::Max, filter_shape: vec![2], stride: vec![2], padding: vec![0] };
cfg.add_layer(LayerConfig::new("pool1", LayerType::Pooling(pool1_layer_cfg)));
cfg.add_layer(LayerConfig::new("pool1", pool1_layer_cfg));

let conv2_layer_cfg = ConvolutionConfig { num_output: 256, filter_shape: vec![5], padding: vec![0], stride: vec![1], axis: None };
cfg.add_layer(LayerConfig::new("conv2", LayerType::Convolution(conv2_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv2", conv2_layer_cfg));
cfg.add_layer(LayerConfig::new("conv2/relu", LayerType::ReLU));
let pool2_layer_cfg = PoolingConfig { mode: PoolingMode::Max, filter_shape: vec![2], stride: vec![2], padding: vec![0] };
cfg.add_layer(LayerConfig::new("pool2", LayerType::Pooling(pool2_layer_cfg)));
cfg.add_layer(LayerConfig::new("pool2", pool2_layer_cfg));

let conv3_layer_cfg = ConvolutionConfig { num_output: 512, filter_shape: vec![3], padding: vec![1], stride: vec![1], axis: None };
cfg.add_layer(LayerConfig::new("conv3", LayerType::Convolution(conv3_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv3", conv3_layer_cfg));
cfg.add_layer(LayerConfig::new("conv3/relu", LayerType::ReLU));

let conv4_layer_cfg = ConvolutionConfig { num_output: 1024, filter_shape: vec![3], padding: vec![1], stride: vec![1], axis: None };
cfg.add_layer(LayerConfig::new("conv4", LayerType::Convolution(conv4_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv4", conv4_layer_cfg));
cfg.add_layer(LayerConfig::new("conv4/relu", LayerType::ReLU));

let conv5_layer_cfg = ConvolutionConfig { num_output: 1024, filter_shape: vec![3], padding: vec![1], stride: vec![1], axis: None };
cfg.add_layer(LayerConfig::new("conv5", LayerType::Convolution(conv5_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv5", conv5_layer_cfg));
cfg.add_layer(LayerConfig::new("conv5/relu", LayerType::ReLU));
let pool5_layer_cfg = PoolingConfig { mode: PoolingMode::Max, filter_shape: vec![2], stride: vec![2], padding: vec![0] };
cfg.add_layer(LayerConfig::new("pool5", LayerType::Pooling(pool5_layer_cfg)));
cfg.add_layer(LayerConfig::new("pool5", pool5_layer_cfg));

cfg.add_layer(LayerConfig::new("fc1", LayerType::Linear(LinearConfig { output_size: 3072 })));
cfg.add_layer(LayerConfig::new("fc2", LayerType::Linear(LinearConfig { output_size: 4096 })));
cfg.add_layer(LayerConfig::new("fc3", LayerType::Linear(LinearConfig { output_size: 1000 })));
cfg.add_layer(LayerConfig::new("fc1", LinearConfig { output_size: 3072 }));
cfg.add_layer(LayerConfig::new("fc2", LinearConfig { output_size: 4096 }));
cfg.add_layer(LayerConfig::new("fc3", LinearConfig { output_size: 1000 }));

let backend = cuda_backend();
// let native_backend = native_backend();
Expand Down Expand Up @@ -281,50 +281,50 @@ fn bench_vgg_a() {
cfg.add_input("data", &vec![64, 3, 224, 224]);

let conv1_layer_cfg = ConvolutionConfig { num_output: 64, filter_shape: vec![3], padding: vec![1], stride: vec![1], axis: None };
cfg.add_layer(LayerConfig::new("conv1", LayerType::Convolution(conv1_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv1", conv1_layer_cfg));
cfg.add_layer(LayerConfig::new("conv1/relu", LayerType::ReLU));
let pool1_layer_cfg = PoolingConfig { mode: PoolingMode::Max, filter_shape: vec![2], stride: vec![2], padding: vec![0] };
cfg.add_layer(LayerConfig::new("pool1", LayerType::Pooling(pool1_layer_cfg)));
cfg.add_layer(LayerConfig::new("pool1", pool1_layer_cfg));

let conv2_layer_cfg = ConvolutionConfig { num_output: 128, filter_shape: vec![3], padding: vec![1], stride: vec![1], axis: None };
cfg.add_layer(LayerConfig::new("conv2", LayerType::Convolution(conv2_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv2", conv2_layer_cfg));
cfg.add_layer(LayerConfig::new("conv2/relu", LayerType::ReLU));
let pool2_layer_cfg = PoolingConfig { mode: PoolingMode::Max, filter_shape: vec![2], stride: vec![2], padding: vec![0] };
cfg.add_layer(LayerConfig::new("pool2", LayerType::Pooling(pool2_layer_cfg)));
cfg.add_layer(LayerConfig::new("pool2", pool2_layer_cfg));

let conv3_layer_cfg = ConvolutionConfig { num_output: 256, filter_shape: vec![3], padding: vec![1], stride: vec![1], axis: None };
cfg.add_layer(LayerConfig::new("conv3", LayerType::Convolution(conv3_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv3", conv3_layer_cfg));
cfg.add_layer(LayerConfig::new("conv3/relu", LayerType::ReLU));

let conv4_layer_cfg = ConvolutionConfig { num_output: 256, filter_shape: vec![3], padding: vec![1], stride: vec![1], axis: None };
cfg.add_layer(LayerConfig::new("conv4", LayerType::Convolution(conv4_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv4", conv4_layer_cfg));
cfg.add_layer(LayerConfig::new("conv4/relu", LayerType::ReLU));
let pool3_layer_cfg = PoolingConfig { mode: PoolingMode::Max, filter_shape: vec![2], stride: vec![2], padding: vec![0] };
cfg.add_layer(LayerConfig::new("pool3", LayerType::Pooling(pool3_layer_cfg)));
cfg.add_layer(LayerConfig::new("pool3", pool3_layer_cfg));

let conv5_layer_cfg = ConvolutionConfig { num_output: 512, filter_shape: vec![3], padding: vec![1], stride: vec![1], axis: None };
cfg.add_layer(LayerConfig::new("conv5", LayerType::Convolution(conv5_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv5", conv5_layer_cfg));
cfg.add_layer(LayerConfig::new("conv5/relu", LayerType::ReLU));

let conv6_layer_cfg = ConvolutionConfig { num_output: 512, filter_shape: vec![3], padding: vec![1], stride: vec![1], axis: None };
cfg.add_layer(LayerConfig::new("conv6", LayerType::Convolution(conv6_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv6", conv6_layer_cfg));
cfg.add_layer(LayerConfig::new("conv6/relu", LayerType::ReLU));
let pool4_layer_cfg = PoolingConfig { mode: PoolingMode::Max, filter_shape: vec![2], stride: vec![2], padding: vec![0] };
cfg.add_layer(LayerConfig::new("pool4", LayerType::Pooling(pool4_layer_cfg)));
cfg.add_layer(LayerConfig::new("pool4", pool4_layer_cfg));

let conv7_layer_cfg = ConvolutionConfig { num_output: 512, filter_shape: vec![3], padding: vec![1], stride: vec![1], axis: None };
cfg.add_layer(LayerConfig::new("conv7", LayerType::Convolution(conv7_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv7", conv7_layer_cfg));
cfg.add_layer(LayerConfig::new("conv7/relu", LayerType::ReLU));

let conv8_layer_cfg = ConvolutionConfig { num_output: 512, filter_shape: vec![3], padding: vec![1], stride: vec![1], axis: None };
cfg.add_layer(LayerConfig::new("conv8", LayerType::Convolution(conv8_layer_cfg)));
cfg.add_layer(LayerConfig::new("conv8", conv8_layer_cfg));
cfg.add_layer(LayerConfig::new("conv8/relu", LayerType::ReLU));
let pool5_layer_cfg = PoolingConfig { mode: PoolingMode::Max, filter_shape: vec![2], stride: vec![2], padding: vec![0] };
cfg.add_layer(LayerConfig::new("pool5", LayerType::Pooling(pool5_layer_cfg)));
cfg.add_layer(LayerConfig::new("pool5", pool5_layer_cfg));
let fc1_layer_cfg = LinearConfig { output_size: 4096 };
cfg.add_layer(LayerConfig::new("fc1", LayerType::Linear(LinearConfig { output_size: 4096 })));
cfg.add_layer(LayerConfig::new("fc2", LayerType::Linear(LinearConfig { output_size: 4096 })));
cfg.add_layer(LayerConfig::new("fc3", LayerType::Linear(LinearConfig { output_size: 1000 })));
cfg.add_layer(LayerConfig::new("fc1", LinearConfig { output_size: 4096 }));
cfg.add_layer(LayerConfig::new("fc2", LinearConfig { output_size: 4096 }));
cfg.add_layer(LayerConfig::new("fc3", LinearConfig { output_size: 1000 }));

let backend = cuda_backend();
// let native_backend = native_backend();
Expand Down
4 changes: 2 additions & 2 deletions src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,10 +1103,10 @@ pub enum LayerType {

impl LayerConfig {
/// Creates a new LayerConfig
pub fn new(name: &str, layer_type: LayerType) -> LayerConfig {
pub fn new<L: Into<LayerType>>(name: &str, layer_type: L) -> LayerConfig {
LayerConfig {
name: name.to_owned(),
layer_type: layer_type,
layer_type: layer_type.into(),

outputs: Vec::new(),
inputs: Vec::new(),
Expand Down
6 changes: 6 additions & 0 deletions src/layers/common/convolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ impl ConvolutionConfig {
}
}

impl Into<LayerType> for ConvolutionConfig {
fn into(self) -> LayerType {
LayerType::Convolution(self)
}
}

#[cfg(test)]
mod tests {
use co::*;
Expand Down
6 changes: 6 additions & 0 deletions src/layers/common/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,9 @@ pub struct LinearConfig {
/// The number of output values
pub output_size: usize,
}

impl Into<LayerType> for LinearConfig {
fn into(self) -> LayerType {
LayerType::Linear(self)
}
}
6 changes: 6 additions & 0 deletions src/layers/common/pooling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ pub struct PoolingConfig {
pub padding: Vec<usize>,
}

impl Into<LayerType> for PoolingConfig {
fn into(self) -> LayerType {
LayerType::Pooling(self)
}
}

#[derive(Debug, Copy, Clone)]
/// The different modes of pooling that can be calculated.
pub enum PoolingMode {
Expand Down
6 changes: 6 additions & 0 deletions src/layers/common/sequential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ impl SequentialConfig {
}
}

impl Into<LayerType> for SequentialConfig {
fn into(self) -> LayerType {
LayerType::Sequential(self)
}
}

impl ::std::default::Default for SequentialConfig {
fn default() -> SequentialConfig {
SequentialConfig {
Expand Down
6 changes: 6 additions & 0 deletions src/layers/loss/negative_log_likelihood.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,9 @@ pub struct NegativeLogLikelihoodConfig {
/// How many different classes can be classified.
pub num_classes: usize,
}

impl Into<LayerType> for NegativeLogLikelihoodConfig {
fn into(self) -> LayerType {
LayerType::NegativeLogLikelihood(self)
}
}
6 changes: 6 additions & 0 deletions src/layers/utility/reshape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ impl ReshapeConfig {
}
}
}

impl Into<LayerType> for ReshapeConfig {
fn into(self) -> LayerType {
LayerType::Reshape(self)
}
}
6 changes: 2 additions & 4 deletions tests/layer_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@ mod layer_spec {
fn can_create_simple_network_sequential_layer() {
let mut model = SequentialConfig::default();
model.add_input("data", &vec![1, 784]);
let linear1_cfg = LinearConfig { output_size: 1568 };
model.add_layer(LayerConfig::new("linear1", LayerType::Linear(linear1_cfg)));
model.add_layer(LayerConfig::new("linear1", LinearConfig { output_size: 1568 }));
model.add_layer(LayerConfig::new("sigmoid", LayerType::Sigmoid));
let linear2_cfg = LinearConfig { output_size: 10 };
model.add_layer(LayerConfig::new("linear2", LayerType::Linear(linear2_cfg)));
model.add_layer(LayerConfig::new("linear2", LinearConfig { output_size: 10 }));

let network = Layer::from_config(cuda_backend(), &LayerConfig::new("model", LayerType::Sequential(model)));
}
Expand Down

0 comments on commit b9a4e8f

Please sign in to comment.