Skip to content

Commit ba4fef9

Browse files
authored
Add files via upload
1 parent 8ffe603 commit ba4fef9

File tree

8 files changed

+828
-0
lines changed

8 files changed

+828
-0
lines changed

Experiments/BigDL/AlexNet.scala

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2016 The BigDL Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.intel.analytics.bigdl.models.alexnet
18+
19+
import com.intel.analytics.bigdl._
20+
import com.intel.analytics.bigdl.mkl.Memory
21+
import com.intel.analytics.bigdl.numeric.NumericFloat
22+
import com.intel.analytics.bigdl.nn._
23+
24+
object AlexNet {
25+
def apply(classNum: Int, hasDropout : Boolean = true): Module[Float] = {
26+
val model = Sequential()
27+
model.add(SpatialConvolution(3, 96, 11, 11, 4, 4, 0, 0, 1, false).setName("conv1"))
28+
model.add(ReLU(true).setName("relu1"))
29+
model.add(SpatialCrossMapLRN(5, 0.0001, 0.75).setName("norm1"))
30+
model.add(SpatialMaxPooling(3, 3, 2, 2).setName("pool1"))
31+
model.add(SpatialConvolution(96, 256, 5, 5, 1, 1, 2, 2, 2).setName("conv2"))
32+
model.add(ReLU(true).setName("relu2"))
33+
model.add(SpatialCrossMapLRN(5, 0.0001, 0.75).setName("norm2"))
34+
model.add(SpatialMaxPooling(3, 3, 2, 2).setName("pool2"))
35+
model.add(SpatialConvolution(256, 384, 3, 3, 1, 1, 1, 1).setName("conv3"))
36+
model.add(ReLU(true).setName("relu3"))
37+
model.add(SpatialConvolution(384, 384, 3, 3, 1, 1, 1, 1, 2).setName("conv4"))
38+
model.add(ReLU(true).setName("relu4"))
39+
model.add(SpatialConvolution(384, 256, 3, 3, 1, 1, 1, 1, 2).setName("conv5"))
40+
model.add(ReLU(true).setName("relu5"))
41+
model.add(SpatialMaxPooling(3, 3, 2, 2).setName("pool5"))
42+
model.add(View(256 * 6 * 6))
43+
model.add(Linear(256 * 6 * 6, 4096).setName("fc6"))
44+
model.add(ReLU(true).setName("relu6"))
45+
if (hasDropout) model.add(Dropout(0.5).setName("drop6"))
46+
model.add(Linear(4096, 4096).setName("fc7"))
47+
model.add(ReLU(true).setName("relu7"))
48+
if (hasDropout) model.add(Dropout(0.5).setName("drop7"))
49+
model.add(Linear(4096, classNum).setName("fc8"))
50+
model.add(LogSoftMax().setName("loss"))
51+
model
52+
}
53+
54+
def graph(classNum: Int, hasDropout : Boolean = true): Module[Float] = {
55+
val conv1 = SpatialConvolution(3, 96, 11, 11, 4, 4, 0, 0, 1, false)
56+
.setName("conv1").inputs()
57+
val relu1 = ReLU(true).setName("relu1").inputs(conv1)
58+
val norm1 = SpatialCrossMapLRN(5, 0.0001, 0.75).setName("norm1").inputs(relu1)
59+
val pool1 = SpatialMaxPooling(3, 3, 2, 2).setName("pool1").inputs(norm1)
60+
val conv2 = SpatialConvolution(96, 256, 5, 5, 1, 1, 2, 2, 2).setName("conv2").inputs(pool1)
61+
val relu2 = ReLU(true).setName("relu2").inputs(conv2)
62+
val norm2 = SpatialCrossMapLRN(5, 0.0001, 0.75).setName("norm2").inputs(relu2)
63+
val pool2 = SpatialMaxPooling(3, 3, 2, 2).setName("pool2").inputs(norm2)
64+
val conv3 = SpatialConvolution(256, 384, 3, 3, 1, 1, 1, 1).setName("conv3").inputs(pool2)
65+
val relu3 = ReLU(true).setName("relu3").inputs(conv3)
66+
val conv4 = SpatialConvolution(384, 384, 3, 3, 1, 1, 1, 1, 2).setName("conv4").inputs(relu3)
67+
val relu4 = ReLU(true).setName("relu4").inputs(conv4)
68+
val conv5 = SpatialConvolution(384, 256, 3, 3, 1, 1, 1, 1, 2).setName("conv5").inputs(relu4)
69+
val relu5 = ReLU(true).setName("relu5").inputs(conv5)
70+
val pool5 = SpatialMaxPooling(3, 3, 2, 2).setName("pool5").inputs(relu5)
71+
val view1 = View(256 * 6 * 6).inputs(pool5)
72+
val fc6 = Linear(256 * 6 * 6, 4096).setName("fc6").inputs(view1)
73+
val relu6 = ReLU(true).setName("relu6").inputs(fc6)
74+
val drop6 = if (hasDropout) Dropout(0.5).setName("drop6").inputs(relu6) else relu6
75+
val fc7 = Linear(4096, 4096).setName("fc7").inputs(drop6)
76+
val relu7 = ReLU(true).setName("relu7").inputs(fc7)
77+
val drop7 = if (hasDropout) Dropout(0.5).setName("drop7").inputs(relu7) else relu7
78+
val fc8 = Linear(4096, classNum).setName("fc8").inputs(drop7)
79+
val loss = LogSoftMax().setName("loss").inputs(fc8)
80+
Graph(conv1, loss)
81+
}
82+
83+
}

Experiments/BigDL/AlexNet1.scala

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2016 The BigDL Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.intel.analytics.bigdl.models.alexnet
18+
19+
import com.intel.analytics.bigdl._
20+
import com.intel.analytics.bigdl.mkl.Memory
21+
import com.intel.analytics.bigdl.numeric.NumericFloat
22+
import com.intel.analytics.bigdl.nn._
23+
24+
object AlexNet {
25+
def apply(classNum: Int, hasDropout : Boolean = true): Module[Float] = {
26+
val model = Sequential()
27+
model.add(SpatialConvolution(3, 96, 11, 11, 4, 4, 0, 0, 1, false).setName("conv1"))
28+
model.add(ReLU(true).setName("relu1"))
29+
model.add(SpatialCrossMapLRN(5, 0.0001, 0.75).setName("norm1"))
30+
model.add(SpatialMaxPooling(3, 3, 2, 2).setName("pool1"))
31+
model.add(SpatialConvolution(96, 256, 5, 5, 1, 1, 2, 2, 2).setName("conv2"))
32+
model.add(ReLU(true).setName("relu2"))
33+
model.add(SpatialCrossMapLRN(5, 0.0001, 0.75).setName("norm2"))
34+
model.add(SpatialMaxPooling(3, 3, 2, 2).setName("pool2"))
35+
model.add(SpatialConvolution(256, 384, 3, 3, 1, 1, 1, 1).setName("conv3"))
36+
model.add(ReLU(true).setName("relu3"))
37+
model.add(SpatialConvolution(384, 384, 3, 3, 1, 1, 1, 1, 2).setName("conv4"))
38+
model.add(ReLU(true).setName("relu4"))
39+
model.add(SpatialConvolution(384, 256, 3, 3, 1, 1, 1, 1, 2).setName("conv5"))
40+
model.add(ReLU(true).setName("relu5"))
41+
model.add(SpatialMaxPooling(3, 3, 2, 2).setName("pool5"))
42+
model.add(View(256 * 6 * 6))
43+
model.add(Linear(256 * 6 * 6, 4096).setName("fc6"))
44+
model.add(ReLU(true).setName("relu6"))
45+
if (hasDropout) model.add(Dropout(0.5).setName("drop6"))
46+
model.add(Linear(4096, 4096).setName("fc7"))
47+
model.add(ReLU(true).setName("relu7"))
48+
if (hasDropout) model.add(Dropout(0.5).setName("drop7"))
49+
model.add(Linear(4096, classNum).setName("fc8"))
50+
model.add(LogSoftMax().setName("loss"))
51+
model
52+
}
53+
54+
def graph(classNum: Int, hasDropout : Boolean = true): Module[Float] = {
55+
val conv1 = SpatialConvolution(3, 96, 11, 11, 4, 4, 0, 0, 1, false)
56+
.setName("conv1").inputs()
57+
val relu1 = ReLU(true).setName("relu1").inputs(conv1)
58+
val norm1 = SpatialCrossMapLRN(5, 0.0001, 0.75).setName("norm1").inputs(relu1)
59+
val pool1 = SpatialMaxPooling(3, 3, 2, 2).setName("pool1").inputs(norm1)
60+
val conv2 = SpatialConvolution(96, 256, 5, 5, 1, 1, 2, 2, 2).setName("conv2").inputs(pool1)
61+
val relu2 = ReLU(true).setName("relu2").inputs(conv2)
62+
val norm2 = SpatialCrossMapLRN(5, 0.0001, 0.75).setName("norm2").inputs(relu2)
63+
val pool2 = SpatialMaxPooling(3, 3, 2, 2).setName("pool2").inputs(norm2)
64+
val conv3 = SpatialConvolution(256, 384, 3, 3, 1, 1, 1, 1).setName("conv3").inputs(pool2)
65+
val relu3 = ReLU(true).setName("relu3").inputs(conv3)
66+
val conv4 = SpatialConvolution(384, 384, 3, 3, 1, 1, 1, 1, 2).setName("conv4").inputs(relu3)
67+
val relu4 = ReLU(true).setName("relu4").inputs(conv4)
68+
val conv5 = SpatialConvolution(384, 256, 3, 3, 1, 1, 1, 1, 2).setName("conv5").inputs(relu4)
69+
val relu5 = ReLU(true).setName("relu5").inputs(conv5)
70+
val pool5 = SpatialMaxPooling(3, 3, 2, 2).setName("pool5").inputs(relu5)
71+
val view1 = View(256 * 6 * 6).inputs(pool5)
72+
val fc6 = Linear(256 * 6 * 6, 4096).setName("fc6").inputs(view1)
73+
val relu6 = ReLU(true).setName("relu6").inputs(fc6)
74+
val drop6 = if (hasDropout) Dropout(0.5).setName("drop6").inputs(relu6) else relu6
75+
val fc7 = Linear(4096, 4096).setName("fc7").inputs(drop6)
76+
val relu7 = ReLU(true).setName("relu7").inputs(fc7)
77+
val drop7 = if (hasDropout) Dropout(0.5).setName("drop7").inputs(relu7) else relu7
78+
val fc8 = Linear(4096, classNum).setName("fc8").inputs(drop7)
79+
val loss = LogSoftMax().setName("loss").inputs(fc8)
80+
Graph(conv1, loss)
81+
}
82+
83+
}

Experiments/BigDL/README.md

+112
Original file line numberDiff line numberDiff line change
@@ -1 +1,113 @@
1+
# LeNet5 Model on MNIST
12

3+
LeNet5 is a classical CNN model used in digital number classification. For detail information,
4+
please refer to <http://yann.lecun.com/exdb/lenet/>.
5+
6+
## Prepare MNIST Data
7+
You can download the MNIST Data from [here](http://yann.lecun.com/exdb/mnist/). Unzip all the
8+
files and put them in one folder(e.g. mnist).
9+
10+
There're four files. **train-images-idx3-ubyte** contains train images,
11+
**train-labels-idx1-ubyte** is train label file, **t10k-images-idx3-ubyte** has validation images
12+
and **t10k-labels-idx1-ubyte** contains validation labels. For more detail, please refer to the
13+
download page.
14+
15+
After you uncompress the gzip files, these files may be renamed by some uncompress tools, e.g. **train-images-idx3-ubyte** is renamed
16+
to **train-images.idx3-ubyte**. Please change the name back before you run the example.
17+
18+
## Get the JAR
19+
You can build one by refer to the
20+
[Build Page](https://bigdl-project.github.io/master/#ScalaUserGuide/install-build-src/) from the source code.
21+
22+
## Train the Model
23+
### Use Apache Spark
24+
Local mode, example command
25+
```
26+
spark-submit \
27+
--master local[physical_core_number] \
28+
--driver-class-path dist/lib/bigdl-VERSION-jar-with-dependencies.jar \
29+
--class com.intel.analytics.bigdl.models.lenet.Train \
30+
dist/lib/bigdl-VERSION-jar-with-dependencies.jar \
31+
-f path_to_mnist_folder \
32+
-b batch_size \
33+
--checkpoint ./model
34+
```
35+
Standalone cluster mode, example command
36+
```
37+
spark-submit \
38+
--master spark://... \
39+
--executor-cores cores_per_executor \
40+
--total-executor-cores total_cores_for_the_job \
41+
--driver-class-path dist/lib/bigdl-VERSION-jar-with-dependencies.jar \
42+
--class com.intel.analytics.bigdl.models.lenet.Train \
43+
dist/lib/bigdl-VERSION-jar-with-dependencies.jar \
44+
-f path_to_mnist_folder \
45+
-b batch_size \
46+
--checkpoint ./model
47+
```
48+
Yarn cluster mode, example command
49+
```
50+
spark-submit \
51+
--master yarn \
52+
--deploy-mode client \
53+
--executor-cores cores_per_executor \
54+
--num-executors executors_number \
55+
--driver-class-path dist/lib/bigdl-VERSION-jar-with-dependencies.jar \
56+
--class com.intel.analytics.bigdl.models.lenet.Train \
57+
dist/lib/bigdl-VERSION-jar-with-dependencies.jar \
58+
-f path_to_mnist_folder \
59+
-b batch_size \
60+
--checkpoint ./model
61+
```
62+
In the above commands
63+
* -f: where you put your MNIST data
64+
* --checkpoint: Where you cache the model/train_state snapshot. You should input a folder and
65+
make sure the folder is created when you run this example. The model snapshot will be named as
66+
model.#iteration_number, and train state will be named as state.#iteration_number. Note that if
67+
there are some files already exist in the folder, the old file will not be overwrite for the
68+
safety of your model files.
69+
* -b: The mini-batch size. It is expected that the mini-batch size is a multiple of node_number * core_number.
70+
71+
## Test Model
72+
The above commands will cache the model in specified path(--checkpoint). Run this command will
73+
use the model to do a validation.
74+
75+
Spark local mode, example command
76+
```
77+
spark-submit \
78+
--master local[physical_core_number] \
79+
--class com.intel.analytics.bigdl.models.lenet.Test \
80+
dist/lib/bigdl-VERSION-jar-with-dependencies.jar \
81+
-f path_to_mnist_folder \
82+
--model ./model/model.iteration \
83+
-b batch_size
84+
```
85+
Standalone cluster mode, example command
86+
```
87+
spark-submit \
88+
--master spark://... \
89+
--executor-cores cores_per_executor \
90+
--total-executor-cores total_cores_for_the_job \
91+
--class com.intel.analytics.bigdl.models.lenet.Test \
92+
dist/lib/bigdl-VERSION-jar-with-dependencies.jar \
93+
-f path_to_mnist_folder \
94+
--model ./model/model.iteration_number \
95+
-b batch_size
96+
```
97+
Yarn cluster mode, example command
98+
```
99+
spark-submit \
100+
--master yarn \
101+
--deploy-mode client \
102+
--executor-cores cores_per_executor \
103+
--num-executors executors_number \
104+
--class com.intel.analytics.bigdl.models.lenet.Test \
105+
dist/lib/bigdl-VERSION-jar-with-dependencies.jar \
106+
-f path_to_mnist_folder \
107+
--model ./model/model.iteration_number \
108+
-b batch_size
109+
```
110+
In the above command
111+
* -f: where you put your MNIST data
112+
* --model: the model snapshot file
113+
* -b: The mini-batch size. It is expected that the mini-batch size is a multiple of node_number * core_number.

Experiments/BigDL/Test.scala

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2016 The BigDL Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.intel.analytics.bigdl.models.alexnet
18+
19+
import java.nio.file.Paths
20+
21+
import com.intel.analytics.bigdl.dataset.DataSet
22+
import com.intel.analytics.bigdl.dataset.image._
23+
import com.intel.analytics.bigdl.models.lenet.Utils._
24+
import com.intel.analytics.bigdl.nn.Module
25+
import com.intel.analytics.bigdl.optim.{Top1Accuracy, Top5Accuracy, Validator}
26+
import com.intel.analytics.bigdl.utils.Engine
27+
import org.apache.log4j.{Level, Logger}
28+
import org.apache.spark.SparkContext
29+
30+
object Test {
31+
Logger.getLogger("org").setLevel(Level.ERROR)
32+
Logger.getLogger("akka").setLevel(Level.ERROR)
33+
Logger.getLogger("breeze").setLevel(Level.ERROR)
34+
35+
36+
import Utils._
37+
38+
def main(args: Array[String]): Unit = {
39+
testParser.parse(args, new TestParams()).foreach { param =>
40+
val conf = Engine.createSparkConf().setAppName("Test AlexNet on CIFAR")
41+
.set("spark.akka.frameSize", 64.toString)
42+
val sc = new SparkContext(conf)
43+
Engine.init
44+
45+
val partitionNum = Engine.nodeNumber() * Engine.coreNumber()
46+
val rddData = sc.parallelize(Utils.loadTest(param.folder), partitionNum)
47+
val transformer = BytesToBGRImg() -> BGRImgNormalizer(testMean, testStd) -> BGRImgToSample()
48+
val evaluationSet = transformer(rddData)
49+
50+
val model = Module.load[Float](param.model)
51+
val result = model.evaluate(evaluationSet,
52+
Array(new Top1Accuracy[Float]), Some(param.batchSize))
53+
54+
result.foreach(r => println(s"${r._2} is ${r._1}"))
55+
sc.stop()
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)