resnet_model.py 1.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 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.

import numpy
import paddle
import sys, os
import numpy as np
import paddle.fluid as fluid
import paddle.static as static

paddle.enable_static()

resnet_input = static.data(
25 26
    name="resnet_input", shape=[1, 160, 7, 7], dtype='float32'
)
27 28 29
label = static.data(name="label", shape=[1, 960, 7, 7], dtype='float32')
d = paddle.nn.functional.relu6(resnet_input)
f = static.nn.conv2d(
30 31
    input=d, num_filters=960, filter_size=1, stride=1, padding=0, dilation=1
)
32
g = static.nn.conv2d(
33 34
    input=f, num_filters=160, filter_size=1, stride=1, padding=0, dilation=1
)
35
i = static.nn.conv2d(
36 37
    input=g, num_filters=960, filter_size=1, stride=1, padding=0, dilation=1
)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
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())

53 54 55
fluid.io.save_inference_model(
    "./resnet_model", [resnet_input.name], [temp7], exe
)
56
print('res', temp7.name)