1
|
pip install h5py
|
1
2
|
pip install numpy scipy
pip install pillow
|
1
|
sudo pip install keras
|
1
2
|
>>>
import
kerasUsing TensorFlow backend.
>>> keras.__version__
'2.0.4'
|
1
2
3
4
5
6
|
{
"floatx"
:
"float32"
,
"epsilon"
:
1e
-
07
,
"backend"
:
"tensorflow"
,
"image_data_format"
:
"channels_last"
}
|
1
|
import
keras
|
1
2
|
from
keras.models
import
Sequential
models
=
Sequential()
|
1
2
3
|
from
keras.layers
import
Dense, Activation, Conv2D, MaxPooling2D, Flatten, Dropout
model.add(Conv2D(
64
, (
3
,
3
), activation
=
'relu'
, input_shape
=
(
100
,
100
,
32
)))
# This ads a Convolutional layer with 64 filters of size 3 * 3 to the graph
|
1
|
model.add(Conv2D(
64
, (
3
,
3
), activation
=
'relu'
, input_shape
=
(
100
,
100
,
32
)))
|
1
|
model.add(MaxPooling2D(pool_size
=
(
2
,
2
)))
|
1
|
model.add(Dense(
256
, activation
=
'relu'
))
|
1
|
model.add(Dropout(
0.5
))
|
1
|
model.add(Conv2D(
32
, (
3
,
3
), activation
=
'relu'
, input_shape
=
(
224
,
224
,
3
)))
|
1
|
model.
compile
(loss
=
'binary_crossentropy'
, optimizer
=
'rmsprop'
)
|
1
2
3
|
from
keras.optimizers
import
SGD
sgd
=
SGD(lr
=
0.01
, decay
=
1e
-
6
, momentum
=
0.9
, nesterov
=
True
)
model.
compile
(loss
=
'categorical_crossentropy'
, optimizer
=
sgd)
|
1
|
model.fit(x_train, y_train, batch_size
=
32
, epochs
=
10
, validation_data(x_val, y_val))
|
1
|
score
=
model.evaluate(x_test, y_test, batch_size
=
32
)
|
1
2
3
4
5
6
7
|
import
keras
from
keras.models
import
Sequential
from
keras.layers
import
Dense
import
numpy as np
trX
=
np.linspace(
-
1
,
1
,
101
)
trY
=
3
*
trX
+
np.random.randn(
*
trX.shape)
*
0.33
|
1
2
|
model
=
Sequential()
model.add(Dense(input_dim
=
1
, output_dim
=
1
, init
=
'uniform'
, activation
=
'linear'
))
|
1
2
3
4
5
|
weights
=
model.layers[
0
].get_weights()
w_init
=
weights[
0
][
0
][
0
]
b_init
=
weights[
1
][
0
]
print
(
'Linear regression model is initialized with weights w: %.2f, b: %.2f'
%
(w_init, b_init))
## Linear regression model is initialized with weight w: -0.03, b: 0.00
|
1
|
model.
compile
(optimizer
=
'sgd'
, loss
=
'mse'
)
|
1
|
model.fit(trX, trY, nb_epoch
=
200
, verbose
=
1
)
|
1
2
3
4
5
6
|
weights
=
model.layers[
0
].get_weights()
w_final
=
weights[
0
][
0
][
0
]
b_final
=
weights[
1
][
0
]
print
(
'Linear regression model is trained to have weight w: %.2f, b: %.2f'
%
(w_final, b_final))
##Linear regression model is trained to have weight w: 2.94, b: 0.08
|
1
|
model.save_weights(
"my_model.h5"
)
|
1
|
model.load_weights(
'my_model_weights.h5'
)
|
1
|
from
keras.models
import
Model
|
1
2
3
4
|
from
keras.layers
import
Input
## First, define the vision modules
digit_input
=
Input
(shape
=
(
1
,
28
,
28
))
|
1
2
3
4
|
x
=
Conv2D(
64
, (
3
,
3
))(digit_input)
x
=
Conv2D(
64
, (
3
,
3
))(x)
x
=
MaxPooling2D((
2
,
2
))(x)
out
=
Flatten()(x)
|
1
|
vision_model
=
Model(digit_input, out)
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
img_input
=
Input
(shape
=
input_shape)
# Block 1
x
=
Conv2D(
64
, (
3
,
3
), activation
=
'relu'
, padding
=
'same'
, name
=
'block1_conv1'
)(img_input)
x
=
Conv2D(
64
, (
3
,
3
), activation
=
'relu'
, padding
=
'same'
, name
=
'block1_conv2'
)(x)
x
=
MaxPooling2D((
2
,
2
), strides
=
(
2
,
2
), name
=
'block1_pool'
)(x)
# Block 2
x
=
Conv2D(
128
, (
3
,
3
), activation
=
'relu'
, padding
=
'same'
, name
=
'block2_conv1'
)(x)
x
=
Conv2D(
128
, (
3
,
3
), activation
=
'relu'
, padding
=
'same'
, name
=
'block2_conv2'
)(x)
x
=
MaxPooling2D((
2
,
2
), strides
=
(
2
,
2
), name
=
'block2_pool'
)(x)
# Block 3
x
=
Conv2D(
256
, (
3
,
3
), activation
=
'relu'
, padding
=
'same'
, name
=
'block3_conv1'
)(x)
x
=
Conv2D(
256
, (
3
,
3
), activation
=
'relu'
, padding
=
'same'
, name
=
'block3_conv2'
)(x)
x
=
Conv2D(
256
, (
3
,
3
), activation
=
'relu'
, padding
=
'same'
, name
=
'block3_conv3'
)(x)
x
=
MaxPooling2D((
2
,
2
), strides
=
(
2
,
2
), name
=
'block3_pool'
)(x)
# Block 4
x
=
Conv2D(
512
, (
3
,
3
), activation
=
'relu'
, padding
=
'same'
, name
=
'block4_conv1'
)(x)
x
=
Conv2D(
512
, (
3
,
3
), activation
=
'relu'
, padding
=
'same'
, name
=
'block4_conv2'
)(x)
x
=
Conv2D(
512
, (
3
,
3
), activation
=
'relu'
, padding
=
'same'
, name
=
'block4_conv3'
)(x)
x
=
MaxPooling2D((
2
,
2
), strides
=
(
2
,
2
), name
=
'block4_pool'
)(x)
# Block 5
x
=
Conv2D(
512
, (
3
,
3
), activation
=
'relu'
, padding
=
'same'
, name
=
'block5_conv1'
)(x)
x
=
Conv2D(
512
, (
3
,
3
), activation
=
'relu'
, padding
=
'same'
, name
=
'block5_conv2'
)(x)
x
=
Conv2D(
512
, (
3
,
3
), activation
=
'relu'
, padding
=
'same'
, name
=
'block5_conv3'
)(x)
x
=
MaxPooling2D((
2
,
2
), strides
=
(
2
,
2
), name
=
'block5_pool'
)(x)
x
=
Flatten(name
=
'flatten'
)(x)
x
=
Dense(
4096
, activation
=
'relu'
, name
=
'fc1'
)(x)
x
=
Dense(
4096
, activation
=
'relu'
, name
=
'fc2'
)(x)
x
=
Dense(classes, activation
=
'softmax'
, name
=
'predictions'
)(x)
|
1
2
3
4
5
6
7
8
9
|
model
=
applications.VGG16(weights
=
'imagenet'
)
img
=
image.load_img(
'cat.jpeg'
, target_size
=
(
224
,
224
))
x
=
image.img_to_array(img)
x
=
np.expand_dims(x, axis
=
0
)
x
=
preprocess_input(x)
preds
=
model.predict(x)
for
results
in
decode_predictions(preds):
for
result
in
results:
print
(
'Probability %0.2f%% => [%s]'
%
(
100
*
result[
2
], result[
1
]))
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
# Squeeze part of fire module with 1 * 1 convolutions, followed by Relu
x
=
Convolution2D(squeeze, (
1
,
1
), padding
=
'valid'
, name
=
'fire2/squeeze1x1'
)(x)
x
=
Activation(
'relu'
, name
=
'fire2/relu_squeeze1x1'
)(x)
#Expand part has two portions, left uses 1 * 1 convolutions and is called expand1x1
left
=
Convolution2D(expand, (
1
,
1
), padding
=
'valid'
, name
=
'fire2/expand1x1'
)(x)
left
=
Activation(
'relu'
, name
=
'fire2/relu_expand1x1'
)(left)
#Right part uses 3 * 3 convolutions and is called expand3x3, both of these are follow#ed by Relu layer, Note that both receive x as input as designed.
right
=
Convolution2D(expand, (
3
,
3
), padding
=
'same'
, name
=
'fire2/expand3x3'
)(x)
right
=
Activation(
'relu'
, name
=
'fire2/relu_expand3x3'
)(right)
# Final output of Fire Module is concatenation of left and right.
x
=
concatenate([left, right], axis
=
3
, name
=
'fire2/concat'
)
|
1
2
3
4
5
|
sq1x1
=
"squeeze1x1"
exp1x1
=
"expand1x1"
exp3x3
=
"expand3x3"
relu
=
"relu_"
WEIGHTS_PATH
=
"https://github.com/rcmalli/keras-squeezenet/releases/download/v1.0/squeezenet_weights_tf_dim_ordering_tf_kernels.h5"
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
sq1x1
=
"squeeze1x1"
exp1x1
=
"expand1x1"
exp3x3
=
"expand3x3"
relu
=
"relu_"
def
fire_module(x, fire_id, squeeze
=
16
, expand
=
64
):
s_id
=
'fire'
+
str
(fire_id)
+
'/'
x
=
Convolution2D(squeeze, (
1
,
1
), padding
=
'valid'
, name
=
s_id
+
sq1x1)(x)
x
=
Activation(
'relu'
, name
=
s_id
+
relu
+
sq1x1)(x)
left
=
Convolution2D(expand, (
1
,
1
), padding
=
'valid'
, name
=
s_id
+
exp1x1)(x)
left
=
Activation(
'relu'
, name
=
s_id
+
relu
+
exp1x1)(left)
right
=
Convolution2D(expand, (
3
,
3
), padding
=
'same'
, name
=
s_id
+
exp3x3)(x)
right
=
Activation(
'relu'
, name
=
s_id
+
relu
+
exp3x3)(right)
x
=
concatenate([left, right], axis
=
3
, name
=
s_id
+
'concat'
)
return
x
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
x
=
Convolution2D(
64
, (
3
,
3
), strides
=
(
2
,
2
), padding
=
'valid'
, name
=
'conv1'
)(img_input)
x
=
Activation(
'relu'
, name
=
'relu_conv1'
)(x)
x
=
MaxPooling2D(pool_size
=
(
3
,
3
), strides
=
(
2
,
2
), name
=
'pool1'
)(x)
x
=
fire_module(x, fire_id
=
2
, squeeze
=
16
, expand
=
64
)
x
=
fire_module(x, fire_id
=
3
, squeeze
=
16
, expand
=
64
)
x
=
MaxPooling2D(pool_size
=
(
3
,
3
), strides
=
(
2
,
2
), name
=
'pool3'
)(x)
x
=
fire_module(x, fire_id
=
4
, squeeze
=
32
, expand
=
128
)
x
=
fire_module(x, fire_id
=
5
, squeeze
=
32
, expand
=
128
)
x
=
MaxPooling2D(pool_size
=
(
3
,
3
), strides
=
(
2
,
2
), name
=
'pool5'
)(x)
x
=
fire_module(x, fire_id
=
6
, squeeze
=
48
, expand
=
192
)
x
=
fire_module(x, fire_id
=
7
, squeeze
=
48
, expand
=
192
)
x
=
fire_module(x, fire_id
=
8
, squeeze
=
64
, expand
=
256
)
x
=
fire_module(x, fire_id
=
9
, squeeze
=
64
, expand
=
256
)
x
=
Dropout(
0.5
, name
=
'drop9'
)(x)
x
=
Convolution2D(classes, (
1
,
1
), padding
=
'valid'
, name
=
'conv10'
)(x)
x
=
Activation(
'relu'
, name
=
'relu_conv10'
)(x)
x
=
GlobalAveragePooling2D()(x)
out
=
Activation(
'softmax'
, name
=
'loss'
)(x)
model
=
Model(inputs, out, name
=
'squeezenet'
)
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
import
numpy as np
from
keras_squeezenet
import
SqueezeNet
from
keras.applications.imagenet_utils
import
preprocess_input, decode_predictions
from
keras.preprocessing
import
image
model
=
SqueezeNet()
img
=
image.load_img(
'pexels-photo-280207.jpeg'
, target_size
=
(
227
,
227
))
x
=
image.img_to_array(img)
x
=
np.expand_dims(x, axis
=
0
)
x
=
preprocess_input(x)
preds
=
model.predict(x)
all_results
=
decode_predictions(preds)
for
results
in
all_results:
for
result
in
results:
print
(
'Probability %0.2f%% => [%s]'
%
(
100
*
result[
2
], result[
1
]))
|