Merge branch 'develop' of https://github.com/baidu/Paddle into convolution
commit
1e0cc74145
@ -1,9 +0,0 @@
|
|||||||
data/cifar-10-batches-py
|
|
||||||
data/cifar-out
|
|
||||||
cifar_vgg_model/*
|
|
||||||
plot.png
|
|
||||||
train.log
|
|
||||||
image_provider_copy_1.py
|
|
||||||
*pyc
|
|
||||||
train.list
|
|
||||||
test.list
|
|
@ -1,74 +0,0 @@
|
|||||||
# Copyright (c) 2016 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 paddle.v2 as paddle
|
|
||||||
|
|
||||||
__all__ = ['resnet_cifar10']
|
|
||||||
|
|
||||||
|
|
||||||
def conv_bn_layer(input,
|
|
||||||
ch_out,
|
|
||||||
filter_size,
|
|
||||||
stride,
|
|
||||||
padding,
|
|
||||||
active_type=paddle.activation.Relu(),
|
|
||||||
ch_in=None):
|
|
||||||
tmp = paddle.layer.img_conv(
|
|
||||||
input=input,
|
|
||||||
filter_size=filter_size,
|
|
||||||
num_channels=ch_in,
|
|
||||||
num_filters=ch_out,
|
|
||||||
stride=stride,
|
|
||||||
padding=padding,
|
|
||||||
act=paddle.activation.Linear(),
|
|
||||||
bias_attr=False)
|
|
||||||
return paddle.layer.batch_norm(input=tmp, act=active_type)
|
|
||||||
|
|
||||||
|
|
||||||
def shortcut(ipt, n_in, n_out, stride):
|
|
||||||
if n_in != n_out:
|
|
||||||
return conv_bn_layer(ipt, n_out, 1, stride, 0,
|
|
||||||
paddle.activation.Linear())
|
|
||||||
else:
|
|
||||||
return ipt
|
|
||||||
|
|
||||||
|
|
||||||
def basicblock(ipt, ch_out, stride):
|
|
||||||
ch_in = ch_out * 2
|
|
||||||
tmp = conv_bn_layer(ipt, ch_out, 3, stride, 1)
|
|
||||||
tmp = conv_bn_layer(tmp, ch_out, 3, 1, 1, paddle.activation.Linear())
|
|
||||||
short = shortcut(ipt, ch_in, ch_out, stride)
|
|
||||||
return paddle.layer.addto(input=[tmp, short], act=paddle.activation.Relu())
|
|
||||||
|
|
||||||
|
|
||||||
def layer_warp(block_func, ipt, features, count, stride):
|
|
||||||
tmp = block_func(ipt, features, stride)
|
|
||||||
for i in range(1, count):
|
|
||||||
tmp = block_func(tmp, features, 1)
|
|
||||||
return tmp
|
|
||||||
|
|
||||||
|
|
||||||
def resnet_cifar10(ipt, depth=32):
|
|
||||||
# depth should be one of 20, 32, 44, 56, 110, 1202
|
|
||||||
assert (depth - 2) % 6 == 0
|
|
||||||
n = (depth - 2) / 6
|
|
||||||
nStages = {16, 64, 128}
|
|
||||||
conv1 = conv_bn_layer(
|
|
||||||
ipt, ch_in=3, ch_out=16, filter_size=3, stride=1, padding=1)
|
|
||||||
res1 = layer_warp(basicblock, conv1, 16, n, 1)
|
|
||||||
res2 = layer_warp(basicblock, res1, 32, n, 2)
|
|
||||||
res3 = layer_warp(basicblock, res2, 64, n, 2)
|
|
||||||
pool = paddle.layer.img_pool(
|
|
||||||
input=res3, pool_size=8, stride=1, pool_type=paddle.pooling.Avg())
|
|
||||||
return pool
|
|
@ -1,92 +0,0 @@
|
|||||||
# Copyright (c) 2016 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 sys
|
|
||||||
|
|
||||||
import paddle.v2 as paddle
|
|
||||||
|
|
||||||
from api_v2_vgg import vgg_bn_drop
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
datadim = 3 * 32 * 32
|
|
||||||
classdim = 10
|
|
||||||
|
|
||||||
# PaddlePaddle init
|
|
||||||
paddle.init(use_gpu=False, trainer_count=1)
|
|
||||||
|
|
||||||
image = paddle.layer.data(
|
|
||||||
name="image", type=paddle.data_type.dense_vector(datadim))
|
|
||||||
|
|
||||||
# Add neural network config
|
|
||||||
# option 1. resnet
|
|
||||||
# net = resnet_cifar10(image, depth=32)
|
|
||||||
# option 2. vgg
|
|
||||||
net = vgg_bn_drop(image)
|
|
||||||
|
|
||||||
out = paddle.layer.fc(input=net,
|
|
||||||
size=classdim,
|
|
||||||
act=paddle.activation.Softmax())
|
|
||||||
|
|
||||||
lbl = paddle.layer.data(
|
|
||||||
name="label", type=paddle.data_type.integer_value(classdim))
|
|
||||||
cost = paddle.layer.classification_cost(input=out, label=lbl)
|
|
||||||
|
|
||||||
# Create parameters
|
|
||||||
parameters = paddle.parameters.create(cost)
|
|
||||||
|
|
||||||
# Create optimizer
|
|
||||||
momentum_optimizer = paddle.optimizer.Momentum(
|
|
||||||
momentum=0.9,
|
|
||||||
regularization=paddle.optimizer.L2Regularization(rate=0.0002 * 128),
|
|
||||||
learning_rate=0.1 / 128.0,
|
|
||||||
learning_rate_decay_a=0.1,
|
|
||||||
learning_rate_decay_b=50000 * 100,
|
|
||||||
learning_rate_schedule='discexp',
|
|
||||||
batch_size=128)
|
|
||||||
|
|
||||||
# End batch and end pass event handler
|
|
||||||
def event_handler(event):
|
|
||||||
if isinstance(event, paddle.event.EndIteration):
|
|
||||||
if event.batch_id % 100 == 0:
|
|
||||||
print "\nPass %d, Batch %d, Cost %f, %s" % (
|
|
||||||
event.pass_id, event.batch_id, event.cost, event.metrics)
|
|
||||||
else:
|
|
||||||
sys.stdout.write('.')
|
|
||||||
sys.stdout.flush()
|
|
||||||
if isinstance(event, paddle.event.EndPass):
|
|
||||||
result = trainer.test(
|
|
||||||
reader=paddle.batch(
|
|
||||||
paddle.dataset.cifar.test10(), batch_size=128),
|
|
||||||
feeding={'image': 0,
|
|
||||||
'label': 1})
|
|
||||||
print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics)
|
|
||||||
|
|
||||||
# Create trainer
|
|
||||||
trainer = paddle.trainer.SGD(cost=cost,
|
|
||||||
parameters=parameters,
|
|
||||||
update_equation=momentum_optimizer)
|
|
||||||
trainer.train(
|
|
||||||
reader=paddle.batch(
|
|
||||||
paddle.reader.shuffle(
|
|
||||||
paddle.dataset.cifar.train10(), buf_size=50000),
|
|
||||||
batch_size=128),
|
|
||||||
num_passes=5,
|
|
||||||
event_handler=event_handler,
|
|
||||||
feeding={'image': 0,
|
|
||||||
'label': 1})
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
@ -1,47 +0,0 @@
|
|||||||
# Copyright (c) 2016 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 paddle.v2 as paddle
|
|
||||||
|
|
||||||
__all__ = ['vgg_bn_drop']
|
|
||||||
|
|
||||||
|
|
||||||
def vgg_bn_drop(input):
|
|
||||||
def conv_block(ipt, num_filter, groups, dropouts, num_channels=None):
|
|
||||||
return paddle.networks.img_conv_group(
|
|
||||||
input=ipt,
|
|
||||||
num_channels=num_channels,
|
|
||||||
pool_size=2,
|
|
||||||
pool_stride=2,
|
|
||||||
conv_num_filter=[num_filter] * groups,
|
|
||||||
conv_filter_size=3,
|
|
||||||
conv_act=paddle.activation.Relu(),
|
|
||||||
conv_with_batchnorm=True,
|
|
||||||
conv_batchnorm_drop_rate=dropouts,
|
|
||||||
pool_type=paddle.pooling.Max())
|
|
||||||
|
|
||||||
conv1 = conv_block(input, 64, 2, [0.3, 0], 3)
|
|
||||||
conv2 = conv_block(conv1, 128, 2, [0.4, 0])
|
|
||||||
conv3 = conv_block(conv2, 256, 3, [0.4, 0.4, 0])
|
|
||||||
conv4 = conv_block(conv3, 512, 3, [0.4, 0.4, 0])
|
|
||||||
conv5 = conv_block(conv4, 512, 3, [0.4, 0.4, 0])
|
|
||||||
|
|
||||||
drop = paddle.layer.dropout(input=conv5, dropout_rate=0.5)
|
|
||||||
fc1 = paddle.layer.fc(input=drop, size=512, act=paddle.activation.Linear())
|
|
||||||
bn = paddle.layer.batch_norm(
|
|
||||||
input=fc1,
|
|
||||||
act=paddle.activation.Relu(),
|
|
||||||
layer_attr=paddle.attr.Extra(drop_rate=0.5))
|
|
||||||
fc2 = paddle.layer.fc(input=bn, size=512, act=paddle.activation.Linear())
|
|
||||||
return fc2
|
|
@ -1,21 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
# Copyright (c) 2016 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.
|
|
||||||
set -e
|
|
||||||
wget https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
|
|
||||||
tar zxf cifar-10-python.tar.gz
|
|
||||||
rm cifar-10-python.tar.gz
|
|
||||||
rm -rf cifar-out/*
|
|
||||||
echo Converting CIFAR data to images.....
|
|
||||||
python process_cifar.py ./cifar-10-batches-py ./cifar-out
|
|
@ -1,89 +0,0 @@
|
|||||||
# Copyright (c) 2016 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 sys
|
|
||||||
import os
|
|
||||||
import PIL.Image as Image
|
|
||||||
"""
|
|
||||||
Usage: python process_cifar input_dir output_dir
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
def mkdir_not_exist(path):
|
|
||||||
"""
|
|
||||||
Make dir if the path does not exist.
|
|
||||||
path: the path to be created.
|
|
||||||
"""
|
|
||||||
if not os.path.exists(path):
|
|
||||||
os.mkdir(path)
|
|
||||||
|
|
||||||
|
|
||||||
def create_dir_structure(output_dir):
|
|
||||||
"""
|
|
||||||
Create the directory structure for the directory.
|
|
||||||
output_dir: the direcotry structure path.
|
|
||||||
"""
|
|
||||||
mkdir_not_exist(os.path.join(output_dir))
|
|
||||||
mkdir_not_exist(os.path.join(output_dir, "train"))
|
|
||||||
mkdir_not_exist(os.path.join(output_dir, "test"))
|
|
||||||
|
|
||||||
|
|
||||||
def convert_batch(batch_path, label_set, label_map, output_dir, data_split):
|
|
||||||
"""
|
|
||||||
Convert CIFAR batch to the structure of Paddle format.
|
|
||||||
batch_path: the batch to be converted.
|
|
||||||
label_set: the set of labels.
|
|
||||||
output_dir: the output path.
|
|
||||||
data_split: whether it is training or testing data.
|
|
||||||
"""
|
|
||||||
data = np.load(batch_path)
|
|
||||||
for data, label, filename in zip(data['data'], data['labels'],
|
|
||||||
data['filenames']):
|
|
||||||
data = data.reshape((3, 32, 32))
|
|
||||||
data = np.transpose(data, (1, 2, 0))
|
|
||||||
label = label_map[label]
|
|
||||||
output_dir_this = os.path.join(output_dir, data_split, str(label))
|
|
||||||
output_filename = os.path.join(output_dir_this, filename)
|
|
||||||
if not label in label_set:
|
|
||||||
label_set[label] = True
|
|
||||||
mkdir_not_exist(output_dir_this)
|
|
||||||
Image.fromarray(data).save(output_filename)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
input_dir = sys.argv[1]
|
|
||||||
output_dir = sys.argv[2]
|
|
||||||
num_batch = 5
|
|
||||||
create_dir_structure(output_dir)
|
|
||||||
label_map = {
|
|
||||||
0: "airplane",
|
|
||||||
1: "automobile",
|
|
||||||
2: "bird",
|
|
||||||
3: "cat",
|
|
||||||
4: "deer",
|
|
||||||
5: "dog",
|
|
||||||
6: "frog",
|
|
||||||
7: "horse",
|
|
||||||
8: "ship",
|
|
||||||
9: "truck"
|
|
||||||
}
|
|
||||||
labels = {}
|
|
||||||
for i in range(1, num_batch + 1):
|
|
||||||
convert_batch(
|
|
||||||
os.path.join(input_dir, "data_batch_%d" % i), labels, label_map,
|
|
||||||
output_dir, "train")
|
|
||||||
convert_batch(
|
|
||||||
os.path.join(input_dir, "test_batch"), {}, label_map, output_dir,
|
|
||||||
"test")
|
|
@ -1,89 +0,0 @@
|
|||||||
# Copyright (c) 2016 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 io
|
|
||||||
import random
|
|
||||||
|
|
||||||
import paddle.utils.image_util as image_util
|
|
||||||
from paddle.trainer.PyDataProvider2 import *
|
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# {'img_size': 32,
|
|
||||||
# 'settings': a global object,
|
|
||||||
# 'color': True,
|
|
||||||
# 'mean_img_size': 32,
|
|
||||||
# 'meta': './data/cifar-out/batches/batches.meta',
|
|
||||||
# 'num_classes': 10,
|
|
||||||
# 'file_list': ('./data/cifar-out/batches/train_batch_000',),
|
|
||||||
# 'use_jpeg': True}
|
|
||||||
def hook(settings, img_size, mean_img_size, num_classes, color, meta, use_jpeg,
|
|
||||||
is_train, **kwargs):
|
|
||||||
settings.mean_img_size = mean_img_size
|
|
||||||
settings.img_size = img_size
|
|
||||||
settings.num_classes = num_classes
|
|
||||||
settings.color = color
|
|
||||||
settings.is_train = is_train
|
|
||||||
|
|
||||||
if settings.color:
|
|
||||||
settings.img_raw_size = settings.img_size * settings.img_size * 3
|
|
||||||
else:
|
|
||||||
settings.img_raw_size = settings.img_size * settings.img_size
|
|
||||||
|
|
||||||
settings.meta_path = meta
|
|
||||||
settings.use_jpeg = use_jpeg
|
|
||||||
|
|
||||||
settings.img_mean = image_util.load_meta(settings.meta_path,
|
|
||||||
settings.mean_img_size,
|
|
||||||
settings.img_size, settings.color)
|
|
||||||
|
|
||||||
settings.logger.info('Image size: %s', settings.img_size)
|
|
||||||
settings.logger.info('Meta path: %s', settings.meta_path)
|
|
||||||
settings.input_types = {
|
|
||||||
'image': dense_vector(settings.img_raw_size),
|
|
||||||
'label': integer_value(settings.num_classes)
|
|
||||||
}
|
|
||||||
|
|
||||||
settings.logger.info('DataProvider Initialization finished')
|
|
||||||
|
|
||||||
|
|
||||||
@provider(init_hook=hook, min_pool_size=0)
|
|
||||||
def processData(settings, file_list):
|
|
||||||
"""
|
|
||||||
The main function for loading data.
|
|
||||||
Load the batch, iterate all the images and labels in this batch.
|
|
||||||
file_list: the batch file list.
|
|
||||||
"""
|
|
||||||
with open(file_list, 'r') as fdata:
|
|
||||||
lines = [line.strip() for line in fdata]
|
|
||||||
random.shuffle(lines)
|
|
||||||
for file_name in lines:
|
|
||||||
with io.open(file_name.strip(), 'rb') as file:
|
|
||||||
data = cPickle.load(file)
|
|
||||||
indexes = list(range(len(data['images'])))
|
|
||||||
if settings.is_train:
|
|
||||||
random.shuffle(indexes)
|
|
||||||
for i in indexes:
|
|
||||||
if settings.use_jpeg == 1:
|
|
||||||
img = image_util.decode_jpeg(data['images'][i])
|
|
||||||
else:
|
|
||||||
img = data['images'][i]
|
|
||||||
img_feat = image_util.preprocess_img(
|
|
||||||
img, settings.img_mean, settings.img_size,
|
|
||||||
settings.is_train, settings.color)
|
|
||||||
label = data['labels'][i]
|
|
||||||
yield {
|
|
||||||
'image': img_feat.astype('float32'),
|
|
||||||
'label': int(label)
|
|
||||||
}
|
|
@ -1,221 +0,0 @@
|
|||||||
# Copyright (c) 2016 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
|
|
||||||
from PIL import Image
|
|
||||||
from cStringIO import StringIO
|
|
||||||
|
|
||||||
|
|
||||||
def resize_image(img, target_size):
|
|
||||||
"""
|
|
||||||
Resize an image so that the shorter edge has length target_size.
|
|
||||||
img: the input image to be resized.
|
|
||||||
target_size: the target resized image size.
|
|
||||||
"""
|
|
||||||
percent = (target_size / float(min(img.size[0], img.size[1])))
|
|
||||||
resized_size = int(round(img.size[0] * percent)), int(
|
|
||||||
round(img.size[1] * percent))
|
|
||||||
img = img.resize(resized_size, Image.ANTIALIAS)
|
|
||||||
return img
|
|
||||||
|
|
||||||
|
|
||||||
def flip(im):
|
|
||||||
"""
|
|
||||||
Return the flipped image.
|
|
||||||
Flip an image along the horizontal direction.
|
|
||||||
im: input image, (H x W x K) ndarrays
|
|
||||||
"""
|
|
||||||
if len(im.shape) == 3:
|
|
||||||
return im[:, :, ::-1]
|
|
||||||
else:
|
|
||||||
return im[:, ::-1]
|
|
||||||
|
|
||||||
|
|
||||||
def crop_img(im, inner_size, color=True, test=True):
|
|
||||||
"""
|
|
||||||
Return cropped image.
|
|
||||||
The size of the cropped image is inner_size * inner_size.
|
|
||||||
im: (K x H x W) ndarrays
|
|
||||||
inner_size: the cropped image size.
|
|
||||||
color: whether it is color image.
|
|
||||||
test: whether in test mode.
|
|
||||||
If False, does random cropping and flipping.
|
|
||||||
If True, crop the center of images.
|
|
||||||
"""
|
|
||||||
if color:
|
|
||||||
height, width = max(inner_size, im.shape[1]), max(inner_size,
|
|
||||||
im.shape[2])
|
|
||||||
padded_im = np.zeros((3, height, width))
|
|
||||||
startY = (height - im.shape[1]) / 2
|
|
||||||
startX = (width - im.shape[2]) / 2
|
|
||||||
endY, endX = startY + im.shape[1], startX + im.shape[2]
|
|
||||||
padded_im[:, startY:endY, startX:endX] = im
|
|
||||||
else:
|
|
||||||
im = im.astype('float32')
|
|
||||||
height, width = max(inner_size, im.shape[0]), max(inner_size,
|
|
||||||
im.shape[1])
|
|
||||||
padded_im = np.zeros((height, width))
|
|
||||||
startY = (height - im.shape[0]) / 2
|
|
||||||
startX = (width - im.shape[1]) / 2
|
|
||||||
endY, endX = startY + im.shape[0], startX + im.shape[1]
|
|
||||||
padded_im[startY:endY, startX:endX] = im
|
|
||||||
if test:
|
|
||||||
startY = (height - inner_size) / 2
|
|
||||||
startX = (width - inner_size) / 2
|
|
||||||
else:
|
|
||||||
startY = np.random.randint(0, height - inner_size + 1)
|
|
||||||
startX = np.random.randint(0, width - inner_size + 1)
|
|
||||||
endY, endX = startY + inner_size, startX + inner_size
|
|
||||||
if color:
|
|
||||||
pic = padded_im[:, startY:endY, startX:endX]
|
|
||||||
else:
|
|
||||||
pic = padded_im[startY:endY, startX:endX]
|
|
||||||
if (not test) and (np.random.randint(2) == 0):
|
|
||||||
pic = flip(pic)
|
|
||||||
return pic
|
|
||||||
|
|
||||||
|
|
||||||
def decode_jpeg(jpeg_string):
|
|
||||||
np_array = np.array(Image.open(StringIO(jpeg_string)))
|
|
||||||
if len(np_array.shape) == 3:
|
|
||||||
np_array = np.transpose(np_array, (2, 0, 1))
|
|
||||||
return np_array
|
|
||||||
|
|
||||||
|
|
||||||
def preprocess_img(im, img_mean, crop_size, is_train, color=True):
|
|
||||||
"""
|
|
||||||
Does data augmentation for images.
|
|
||||||
If is_train is false, cropping the center region from the image.
|
|
||||||
If is_train is true, randomly crop a region from the image,
|
|
||||||
and randomy does flipping.
|
|
||||||
im: (K x H x W) ndarrays
|
|
||||||
"""
|
|
||||||
im = im.astype('float32')
|
|
||||||
test = not is_train
|
|
||||||
pic = crop_img(im, crop_size, color, test)
|
|
||||||
pic -= img_mean
|
|
||||||
return pic.flatten()
|
|
||||||
|
|
||||||
|
|
||||||
def load_meta(meta_path, mean_img_size, crop_size, color=True):
|
|
||||||
"""
|
|
||||||
Return the loaded meta file.
|
|
||||||
Load the meta image, which is the mean of the images in the dataset.
|
|
||||||
The mean image is subtracted from every input image so that the expected mean
|
|
||||||
of each input image is zero.
|
|
||||||
"""
|
|
||||||
mean = np.load(meta_path)['data_mean']
|
|
||||||
border = (mean_img_size - crop_size) / 2
|
|
||||||
if color:
|
|
||||||
assert (mean_img_size * mean_img_size * 3 == mean.shape[0])
|
|
||||||
mean = mean.reshape(3, mean_img_size, mean_img_size)
|
|
||||||
mean = mean[:, border:border + crop_size, border:border +
|
|
||||||
crop_size].astype('float32')
|
|
||||||
else:
|
|
||||||
assert (mean_img_size * mean_img_size == mean.shape[0])
|
|
||||||
mean = mean.reshape(mean_img_size, mean_img_size)
|
|
||||||
mean = mean[border:border + crop_size, border:border +
|
|
||||||
crop_size].astype('float32')
|
|
||||||
return mean
|
|
||||||
|
|
||||||
|
|
||||||
def load_image(img_path, is_color=True):
|
|
||||||
"""
|
|
||||||
Load image and return.
|
|
||||||
img_path: image path.
|
|
||||||
is_color: is color image or not.
|
|
||||||
"""
|
|
||||||
img = Image.open(img_path)
|
|
||||||
img.load()
|
|
||||||
return img
|
|
||||||
|
|
||||||
|
|
||||||
def oversample(img, crop_dims):
|
|
||||||
"""
|
|
||||||
image : iterable of (H x W x K) ndarrays
|
|
||||||
crop_dims: (height, width) tuple for the crops.
|
|
||||||
Returned data contains ten crops of input image, namely,
|
|
||||||
four corner patches and the center patch as well as their
|
|
||||||
horizontal reflections.
|
|
||||||
"""
|
|
||||||
# Dimensions and center.
|
|
||||||
im_shape = np.array(img[0].shape)
|
|
||||||
crop_dims = np.array(crop_dims)
|
|
||||||
im_center = im_shape[:2] / 2.0
|
|
||||||
|
|
||||||
# Make crop coordinates
|
|
||||||
h_indices = (0, im_shape[0] - crop_dims[0])
|
|
||||||
w_indices = (0, im_shape[1] - crop_dims[1])
|
|
||||||
crops_ix = np.empty((5, 4), dtype=int)
|
|
||||||
curr = 0
|
|
||||||
for i in h_indices:
|
|
||||||
for j in w_indices:
|
|
||||||
crops_ix[curr] = (i, j, i + crop_dims[0], j + crop_dims[1])
|
|
||||||
curr += 1
|
|
||||||
crops_ix[4] = np.tile(im_center, (1, 2)) + np.concatenate(
|
|
||||||
[-crop_dims / 2.0, crop_dims / 2.0])
|
|
||||||
crops_ix = np.tile(crops_ix, (2, 1))
|
|
||||||
|
|
||||||
# Extract crops
|
|
||||||
crops = np.empty(
|
|
||||||
(10 * len(img), crop_dims[0], crop_dims[1], im_shape[-1]),
|
|
||||||
dtype=np.float32)
|
|
||||||
ix = 0
|
|
||||||
for im in img:
|
|
||||||
for crop in crops_ix:
|
|
||||||
crops[ix] = im[crop[0]:crop[2], crop[1]:crop[3], :]
|
|
||||||
ix += 1
|
|
||||||
crops[ix - 5:ix] = crops[ix - 5:ix, :, ::-1, :] # flip for mirrors
|
|
||||||
return crops
|
|
||||||
|
|
||||||
|
|
||||||
class ImageTransformer:
|
|
||||||
def __init__(self,
|
|
||||||
transpose=None,
|
|
||||||
channel_swap=None,
|
|
||||||
mean=None,
|
|
||||||
is_color=True):
|
|
||||||
self.transpose = transpose
|
|
||||||
self.channel_swap = None
|
|
||||||
self.mean = None
|
|
||||||
self.is_color = is_color
|
|
||||||
|
|
||||||
def set_transpose(self, order):
|
|
||||||
if self.is_color:
|
|
||||||
assert 3 == len(order)
|
|
||||||
self.transpose = order
|
|
||||||
|
|
||||||
def set_channel_swap(self, order):
|
|
||||||
if self.is_color:
|
|
||||||
assert 3 == len(order)
|
|
||||||
self.channel_swap = order
|
|
||||||
|
|
||||||
def set_mean(self, mean):
|
|
||||||
# mean value, may be one value per channel
|
|
||||||
if mean.ndim == 1:
|
|
||||||
mean = mean[:, np.newaxis, np.newaxis]
|
|
||||||
else:
|
|
||||||
# elementwise mean
|
|
||||||
if self.is_color:
|
|
||||||
assert len(mean.shape) == 3
|
|
||||||
self.mean = mean
|
|
||||||
|
|
||||||
def transformer(self, data):
|
|
||||||
if self.transpose is not None:
|
|
||||||
data = data.transpose(self.transpose)
|
|
||||||
if self.channel_swap is not None:
|
|
||||||
data = data[self.channel_swap, :, :]
|
|
||||||
if self.mean is not None:
|
|
||||||
data -= self.mean
|
|
||||||
return data
|
|
@ -1,20 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
# Copyright (c) 2016 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.
|
|
||||||
set -e
|
|
||||||
|
|
||||||
model=cifar_vgg_model/pass-00299/
|
|
||||||
image=data/cifar-out/test/airplane/seaplane_s_000978.png
|
|
||||||
use_gpu=1
|
|
||||||
python prediction.py $model $image $use_gpu
|
|
@ -1,159 +0,0 @@
|
|||||||
# Copyright (c) 2016 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 os, sys
|
|
||||||
import numpy as np
|
|
||||||
import logging
|
|
||||||
from PIL import Image
|
|
||||||
from optparse import OptionParser
|
|
||||||
|
|
||||||
import paddle.utils.image_util as image_util
|
|
||||||
|
|
||||||
from py_paddle import swig_paddle, DataProviderConverter
|
|
||||||
from paddle.trainer.PyDataProvider2 import dense_vector
|
|
||||||
from paddle.trainer.config_parser import parse_config
|
|
||||||
|
|
||||||
logging.basicConfig(
|
|
||||||
format='[%(levelname)s %(asctime)s %(filename)s:%(lineno)s] %(message)s')
|
|
||||||
logging.getLogger().setLevel(logging.INFO)
|
|
||||||
|
|
||||||
|
|
||||||
class ImageClassifier():
|
|
||||||
def __init__(self,
|
|
||||||
train_conf,
|
|
||||||
use_gpu=True,
|
|
||||||
model_dir=None,
|
|
||||||
resize_dim=None,
|
|
||||||
crop_dim=None,
|
|
||||||
mean_file=None,
|
|
||||||
oversample=False,
|
|
||||||
is_color=True):
|
|
||||||
"""
|
|
||||||
train_conf: network configure.
|
|
||||||
model_dir: string, directory of model.
|
|
||||||
resize_dim: int, resized image size.
|
|
||||||
crop_dim: int, crop size.
|
|
||||||
mean_file: string, image mean file.
|
|
||||||
oversample: bool, oversample means multiple crops, namely five
|
|
||||||
patches (the four corner patches and the center
|
|
||||||
patch) as well as their horizontal reflections,
|
|
||||||
ten crops in all.
|
|
||||||
"""
|
|
||||||
self.train_conf = train_conf
|
|
||||||
self.model_dir = model_dir
|
|
||||||
if model_dir is None:
|
|
||||||
self.model_dir = os.path.dirname(train_conf)
|
|
||||||
|
|
||||||
self.resize_dim = resize_dim
|
|
||||||
self.crop_dims = [crop_dim, crop_dim]
|
|
||||||
self.oversample = oversample
|
|
||||||
self.is_color = is_color
|
|
||||||
|
|
||||||
self.transformer = image_util.ImageTransformer(is_color=is_color)
|
|
||||||
self.transformer.set_transpose((2, 0, 1))
|
|
||||||
|
|
||||||
self.mean_file = mean_file
|
|
||||||
mean = np.load(self.mean_file)['data_mean']
|
|
||||||
mean = mean.reshape(3, self.crop_dims[0], self.crop_dims[1])
|
|
||||||
self.transformer.set_mean(mean) # mean pixel
|
|
||||||
gpu = 1 if use_gpu else 0
|
|
||||||
conf_args = "is_test=1,use_gpu=%d,is_predict=1" % (gpu)
|
|
||||||
conf = parse_config(train_conf, conf_args)
|
|
||||||
swig_paddle.initPaddle("--use_gpu=%d" % (gpu))
|
|
||||||
self.network = swig_paddle.GradientMachine.createFromConfigProto(
|
|
||||||
conf.model_config)
|
|
||||||
assert isinstance(self.network, swig_paddle.GradientMachine)
|
|
||||||
self.network.loadParameters(self.model_dir)
|
|
||||||
|
|
||||||
data_size = 3 * self.crop_dims[0] * self.crop_dims[1]
|
|
||||||
slots = [dense_vector(data_size)]
|
|
||||||
self.converter = DataProviderConverter(slots)
|
|
||||||
|
|
||||||
def get_data(self, img_path):
|
|
||||||
"""
|
|
||||||
1. load image from img_path.
|
|
||||||
2. resize or oversampling.
|
|
||||||
3. transformer data: transpose, sub mean.
|
|
||||||
return K x H x W ndarray.
|
|
||||||
img_path: image path.
|
|
||||||
"""
|
|
||||||
image = image_util.load_image(img_path, self.is_color)
|
|
||||||
if self.oversample:
|
|
||||||
# image_util.resize_image: short side is self.resize_dim
|
|
||||||
image = image_util.resize_image(image, self.resize_dim)
|
|
||||||
image = np.array(image)
|
|
||||||
input = np.zeros(
|
|
||||||
(1, image.shape[0], image.shape[1], 3), dtype=np.float32)
|
|
||||||
input[0] = image.astype(np.float32)
|
|
||||||
input = image_util.oversample(input, self.crop_dims)
|
|
||||||
else:
|
|
||||||
image = image.resize(self.crop_dims, Image.ANTIALIAS)
|
|
||||||
input = np.zeros(
|
|
||||||
(1, self.crop_dims[0], self.crop_dims[1], 3), dtype=np.float32)
|
|
||||||
input[0] = np.array(image).astype(np.float32)
|
|
||||||
|
|
||||||
data_in = []
|
|
||||||
for img in input:
|
|
||||||
img = self.transformer.transformer(img).flatten()
|
|
||||||
data_in.append([img.tolist()])
|
|
||||||
return data_in
|
|
||||||
|
|
||||||
def forward(self, input_data):
|
|
||||||
in_arg = self.converter(input_data)
|
|
||||||
return self.network.forwardTest(in_arg)
|
|
||||||
|
|
||||||
def forward(self, data, output_layer):
|
|
||||||
"""
|
|
||||||
input_data: py_paddle input data.
|
|
||||||
output_layer: specify the name of probability, namely the layer with
|
|
||||||
softmax activation.
|
|
||||||
return: the predicting probability of each label.
|
|
||||||
"""
|
|
||||||
input = self.converter(data)
|
|
||||||
self.network.forwardTest(input)
|
|
||||||
output = self.network.getLayerOutputs(output_layer)
|
|
||||||
# For oversampling, average predictions across crops.
|
|
||||||
# If not, the shape of output[name]: (1, class_number),
|
|
||||||
# the mean is also applicable.
|
|
||||||
return output[output_layer]['value'].mean(0)
|
|
||||||
|
|
||||||
def predict(self, image=None, output_layer=None):
|
|
||||||
assert isinstance(image, basestring)
|
|
||||||
assert isinstance(output_layer, basestring)
|
|
||||||
data = self.get_data(image)
|
|
||||||
prob = self.forward(data, output_layer)
|
|
||||||
lab = np.argsort(-prob)
|
|
||||||
logging.info("Label of %s is: %d", image, lab[0])
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
image_size = 32
|
|
||||||
crop_size = 32
|
|
||||||
multi_crop = True
|
|
||||||
config = "vgg_16_cifar.py"
|
|
||||||
output_layer = "__fc_layer_1__"
|
|
||||||
mean_path = "data/cifar-out/batches/batches.meta"
|
|
||||||
model_path = sys.argv[1]
|
|
||||||
image = sys.argv[2]
|
|
||||||
use_gpu = bool(int(sys.argv[3]))
|
|
||||||
|
|
||||||
obj = ImageClassifier(
|
|
||||||
train_conf=config,
|
|
||||||
model_dir=model_path,
|
|
||||||
resize_dim=image_size,
|
|
||||||
crop_dim=crop_size,
|
|
||||||
mean_file=mean_path,
|
|
||||||
use_gpu=use_gpu,
|
|
||||||
oversample=multi_crop)
|
|
||||||
obj.predict(image, output_layer)
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue