Skip to content

Commit

Permalink
add generate flowers recordio
Browse files Browse the repository at this point in the history
  • Loading branch information
chengduoZH committed Apr 2, 2018
1 parent 4f4c4c9 commit a5690df
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
34 changes: 34 additions & 0 deletions fluid/SE-ResNeXt-152/generate_flowers_recordio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) 2018 PaddlePaddle 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 as np
import paddle
import paddle.fluid as fluid
import paddle.dataset.flowers as flowers

batch_size = 12
data_shape = [3, 224, 224]

with fluid.program_guard(fluid.Program(), fluid.Program()):
reader = paddle.batch(flowers.train(), batch_size=batch_size)
feeder = fluid.DataFeeder(
feed_list=[ # order is image and label
fluid.layers.data(
name='image', shape=data_shape, dtype='float32'),
fluid.layers.data(
name='label', shape=[1], dtype='int64'),
],
place=fluid.CPUPlace())
fluid.recordio_writer.convert_reader_to_recordio_file(
'./flowers_bs_12_3_224_224.recordio', reader, feeder)
34 changes: 16 additions & 18 deletions fluid/SE-ResNeXt-152/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,19 @@ def net_conf(image, label, class_dim):
return out, avg_cost, accuracy, accuracy5


def add_optimizer(args, avg_cost):
#optimizer = fluid.optimizer.SGD(learning_rate=0.002)
optimizer = fluid.optimizer.Momentum(
learning_rate=fluid.layers.piecewise_decay(
boundaries=[100], values=[0.1, 0.2]),
momentum=0.9,
regularization=fluid.regularizer.L2Decay(1e-4))
optimizer.minimize(avg_cost)

if args.use_mem_opt:
fluid.memory_optimize(fluid.default_main_program())


def train_parallel_do(args):

class_dim = 1000
Expand Down Expand Up @@ -253,16 +266,7 @@ def train_parallel_do(args):
avg_cost = fluid.layers.mean(x=cost)
accuracy = fluid.layers.accuracy(input=out, label=label)

#optimizer = fluid.optimizer.SGD(learning_rate=0.002)
optimizer = fluid.optimizer.Momentum(
learning_rate=fluid.layers.piecewise_decay(
boundaries=[100], values=[0.1, 0.2]),
momentum=0.9,
regularization=fluid.regularizer.L2Decay(1e-4))
opts = optimizer.minimize(avg_cost)

if args.use_mem_opt:
fluid.memory_optimize(fluid.default_main_program())
add_optimizer(args, avg_cost)

place = fluid.CUDAPlace(0)
# place = fluid.CPUPlace()
Expand Down Expand Up @@ -324,7 +328,7 @@ def train_parallel_exe(args):

with fluid.program_guard(main, startup):
reader = fluid.layers.open_recordio_file(
filename='./flowers.recordio',
filename='./flowers_bs_12_3_224_224.recordio',
shapes=[[-1, 3, 224, 224], [-1, 1]],
lod_levels=[0, 0],
dtypes=['float32', 'int64'])
Expand All @@ -336,13 +340,7 @@ def train_parallel_exe(args):
prediction, avg_cost, accuracy, accuracy5 = net_conf(image, label,
class_dim)

#optimizer = fluid.optimizer.SGD(learning_rate=0.002)
optimizer = fluid.optimizer.Momentum(
learning_rate=fluid.layers.piecewise_decay(
boundaries=[100], values=[0.1, 0.2]),
momentum=0.9,
regularization=fluid.regularizer.L2Decay(1e-4))
opts = optimizer.minimize(avg_cost)
add_optimizer(args, avg_cost)

if args.use_mem_opt:
fluid.memory_optimize(fluid.default_main_program())
Expand Down

0 comments on commit a5690df

Please sign in to comment.