Getting Started¶
Before you can use the DLPy package, you will need a running CAS server and the SWAT package. The SWAT package can make connections using the binary or HTTP protocol. Between these two options, the binary protocol will give you better performance.
Other than the CAS host and port, you just need a user name and password to connect. User names and passwords can be implemented in various ways, so you may need to see your system administrator on how to acquire an account.
To connect to a CAS server, you simply import SWAT and use the swat.CAS
class
to create a connection.
In [1]: import swat
In [2]: sess = swat.CAS(host, port, userid, password)
Next, import the DLPy package, and then build a simple convolutional neural network (CNN) model.
In [3]: from dlpy import Model, Sequential
Import DLPy layer functions
In [4]: from dlpy.layers import *
Use DLPy to create a sequential model and name it ‘Simple_CNN’
In [5]: model1 = Sequential(sess, model_table='Simple_CNN')
Now define an input layer to add to model1
# The input shape contains RGB images (3 channels)
# The model images are 224 px in height and 224 px in width
In [6]: model1.add(InputLayer(3, 224, 224))
NOTE: Input layer added.
Now, add a 2D convolution layer and a pooling layer.
# Add 2-Dimensional Convolution Layer to model1
# that has 8 filters and a kernel size of 7.
In [7]: model1.add(Conv2d(8, 7))
NOTE: Convolution layer added.
# Add Pooling Layer of size 2
In [8]: model1.add(Pooling(2))
NOTE: Pooling layer added.
Now, add an additional pair of 2D convolution and pooling layers.
# Add another 2D convolution Layer that has 8 filters
# and a kernel size of 7
In [9]: model1.add(Conv2d(8, 7))
NOTE: Convolution layer added.
# Add a pooling layer of size 2 to
# complete the second pair of layers.
In [10]: model1.add(Pooling(2))
NOTE: Pooling layer added.
Add a fully connected layer.
# Add Fully-Connected Layer with 16 units
In [11]: model1.add(Dense(16))
NOTE: Fully-connected layer added.
Finally, add the output layer.
# Add an output layer that has 2 nodes and uses
# the Softmax activation function
In [12]: model1.add(OutputLayer(act='softmax', n=2))
NOTE: Output layer added.
NOTE: Model compiled successfully.
Display a print summary of the table.
In [13]: model1.print_summary()
Layer Id Layer Type Kernel Size Stride Activation Output Size Number of Parameters FLOPS(forward pass)
0 0 Input1 input None (224, 224, 3) (0, 0) 0
1 1 Convo.1 convo (7, 7) (1, 1) Relu (224, 224, 8) (1176, 8) 59006976
2 2 Pool1 pool (2, 2) (2, 2) Max (112, 112, 8) (0, 0) 0
3 3 Convo.2 convo (7, 7) (1, 1) Relu (112, 112, 8) (3136, 8) 39337984
4 4 Pool2 pool (2, 2) (2, 2) Max (56, 56, 8) (0, 0) 0
5 5 F.C.1 fc (25088, 16) Relu 16 (401408, 0) 401408
6 6 Output1 output Softmax 2 (32, 2) 0
7 Total number of parameters Total FLOPS
8 Summary 405,770 98,746,368
Out[13]:
Layer Id Layer Type Kernel Size Stride Activation Output Size Number of Parameters FLOPS(forward pass)
0 0 Input1 input None (224, 224, 3) (0, 0) 0
1 1 Convo.1 convo (7, 7) (1, 1) Relu (224, 224, 8) (1176, 8) 59006976
2 2 Pool1 pool (2, 2) (2, 2) Max (112, 112, 8) (0, 0) 0
3 3 Convo.2 convo (7, 7) (1, 1) Relu (112, 112, 8) (3136, 8) 39337984
4 4 Pool2 pool (2, 2) (2, 2) Max (56, 56, 8) (0, 0) 0
5 5 F.C.1 fc (25088, 16) Relu 16 (401408, 0) 401408
6 6 Output1 output Softmax 2 (32, 2) 0
7 Total number of parameters Total FLOPS
8 Summary 405,770 98,746,368
Use the open source utility Graphviz to display a plot of the model network. Graphviz is available here: https://www.graphviz.org/download/. If you do not have Graphviz, skip this instruction.
In [14]: model1.plot_network()
Out[14]: <graphviz.dot.Digraph at 0x275959f8908>