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 17
import os
import sys

18 19
import numpy
import numpy as np
20 21

import paddle
22 23 24 25 26 27
import paddle.fluid as fluid
import paddle.static as static

paddle.enable_static()

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

56 57 58
fluid.io.save_inference_model(
    "./resnet_model", [resnet_input.name], [temp7], exe
)
59
print('res', temp7.name)