resnet_model.py 1.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2021 CINN Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15 16

import paddle
17
from paddle import fluid, static
18 19 20 21

paddle.enable_static()

resnet_input = static.data(
22 23
    name="resnet_input", shape=[1, 160, 7, 7], dtype='float32'
)
24 25 26
label = static.data(name="label", shape=[1, 960, 7, 7], dtype='float32')
d = paddle.nn.functional.relu6(resnet_input)
f = static.nn.conv2d(
27 28
    input=d, num_filters=960, filter_size=1, stride=1, padding=0, dilation=1
)
29
g = static.nn.conv2d(
30 31
    input=f, num_filters=160, filter_size=1, stride=1, padding=0, dilation=1
)
32
i = static.nn.conv2d(
33 34
    input=g, num_filters=960, filter_size=1, stride=1, padding=0, dilation=1
)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
j1 = paddle.scale(i, scale=2.0, bias=0.5)
j = paddle.scale(j1, scale=2.0, bias=0.5)
temp7 = paddle.nn.functional.relu(j)

cost = paddle.nn.functional.square_error_cost(temp7, label)
avg_cost = paddle.mean(cost)

optimizer = paddle.optimizer.SGD(learning_rate=0.001)
optimizer.minimize(avg_cost)

cpu = paddle.CPUPlace()
exe = static.Executor(cpu)

exe.run(static.default_startup_program())

50 51 52
fluid.io.save_inference_model(
    "./resnet_model", [resnet_input.name], [temp7], exe
)
53
print('res', temp7.name)