Merge pull request #2375 from luotao1/v1_demo
remove duplicated examples, and rename demo to v1_api_demogangliao-patch-1
commit
b15b26374b
@ -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)
|
@ -1,54 +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.
|
||||
|
||||
from paddle.utils.preprocess_img import ImageClassificationDatasetCreater
|
||||
from optparse import OptionParser
|
||||
|
||||
|
||||
def option_parser():
|
||||
parser = OptionParser(usage="usage: python preprcoess.py "\
|
||||
"-i data_dir [options]")
|
||||
parser.add_option(
|
||||
"-i",
|
||||
"--input",
|
||||
action="store",
|
||||
dest="input",
|
||||
help="Input data directory.")
|
||||
parser.add_option(
|
||||
"-s",
|
||||
"--size",
|
||||
action="store",
|
||||
dest="size",
|
||||
help="Processed image size.")
|
||||
parser.add_option(
|
||||
"-c",
|
||||
"--color",
|
||||
action="store",
|
||||
dest="color",
|
||||
help="whether to use color images.")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
options, args = option_parser()
|
||||
data_dir = options.input
|
||||
processed_image_size = int(options.size)
|
||||
color = options.color == "1"
|
||||
data_creator = ImageClassificationDatasetCreater(
|
||||
data_dir, processed_image_size, color)
|
||||
data_creator.train_list_name = "train.txt"
|
||||
data_creator.test_list_name = "test.txt"
|
||||
data_creator.num_per_batch = 1000
|
||||
data_creator.overwrite = True
|
||||
data_creator.create_batches()
|
@ -1,22 +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
|
||||
|
||||
data_dir=./data/cifar-out
|
||||
|
||||
python preprocess.py -i $data_dir -s 32 -c 1
|
||||
|
||||
echo "data/cifar-out/batches/train.txt" > train.list
|
||||
echo "data/cifar-out/batches/test.txt" > test.list
|
@ -1,32 +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
|
||||
config=vgg_16_cifar.py
|
||||
output=./cifar_vgg_model
|
||||
log=train.log
|
||||
|
||||
paddle train \
|
||||
--config=$config \
|
||||
--dot_period=10 \
|
||||
--log_period=100 \
|
||||
--test_all_data_in_one_period=1 \
|
||||
--use_gpu=1 \
|
||||
--trainer_count=1 \
|
||||
--num_passes=300 \
|
||||
--save_dir=$output \
|
||||
2>&1 | tee $log
|
||||
paddle usage -l $log -e $? -n "image_classification_train" >/dev/null 2>&1
|
||||
|
||||
python -m paddle.utils.plotcurve -i $log > plot.png
|
@ -1,58 +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.
|
||||
|
||||
from paddle.trainer_config_helpers import *
|
||||
|
||||
is_predict = get_config_arg("is_predict", bool, False)
|
||||
|
||||
####################Data Configuration ##################
|
||||
if not is_predict:
|
||||
data_dir = 'data/cifar-out/batches/'
|
||||
meta_path = data_dir + 'batches.meta'
|
||||
|
||||
args = {
|
||||
'meta': meta_path,
|
||||
'mean_img_size': 32,
|
||||
'img_size': 32,
|
||||
'num_classes': 10,
|
||||
'use_jpeg': 1,
|
||||
'color': "color"
|
||||
}
|
||||
|
||||
define_py_data_sources2(
|
||||
train_list="train.list",
|
||||
test_list="train.list",
|
||||
module='image_provider',
|
||||
obj='processData',
|
||||
args=args)
|
||||
|
||||
######################Algorithm Configuration #############
|
||||
settings(
|
||||
batch_size=128,
|
||||
learning_rate=0.1 / 128.0,
|
||||
learning_method=MomentumOptimizer(0.9),
|
||||
regularization=L2Regularization(0.0005 * 128))
|
||||
|
||||
#######################Network Configuration #############
|
||||
data_size = 3 * 32 * 32
|
||||
label_size = 10
|
||||
img = data_layer(name='image', size=data_size)
|
||||
# small_vgg is predefined in trainer_config_helpers.networks
|
||||
predict = small_vgg(input_image=img, num_channels=3, num_classes=label_size)
|
||||
|
||||
if not is_predict:
|
||||
lbl = data_layer(name="label", size=label_size)
|
||||
outputs(classification_cost(input=predict, label=lbl))
|
||||
else:
|
||||
outputs(predict)
|
@ -1,5 +0,0 @@
|
||||
dataprovider.pyc
|
||||
empty.list
|
||||
train.log
|
||||
output
|
||||
train.list
|
@ -1,3 +0,0 @@
|
||||
This folder contains scripts used in PaddlePaddle introduction.
|
||||
- use `bash train.sh` to train a simple linear regression model
|
||||
- use `python evaluate_model.py` to read model parameters. You can see that `w` and `b` are very close to [2, 0.3].
|
@ -1,58 +0,0 @@
|
||||
import paddle.v2 as paddle
|
||||
import paddle.v2.dataset.uci_housing as uci_housing
|
||||
|
||||
|
||||
def main():
|
||||
# init
|
||||
paddle.init(use_gpu=False, trainer_count=1)
|
||||
|
||||
# network config
|
||||
x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13))
|
||||
y_predict = paddle.layer.fc(input=x,
|
||||
param_attr=paddle.attr.Param(name='w'),
|
||||
size=1,
|
||||
act=paddle.activation.Linear(),
|
||||
bias_attr=paddle.attr.Param(name='b'))
|
||||
y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1))
|
||||
cost = paddle.layer.mse_cost(input=y_predict, label=y)
|
||||
|
||||
# create parameters
|
||||
parameters = paddle.parameters.create(cost)
|
||||
|
||||
# create optimizer
|
||||
optimizer = paddle.optimizer.Momentum(momentum=0)
|
||||
|
||||
trainer = paddle.trainer.SGD(cost=cost,
|
||||
parameters=parameters,
|
||||
update_equation=optimizer)
|
||||
|
||||
# event_handler to print training and testing info
|
||||
def event_handler(event):
|
||||
if isinstance(event, paddle.event.EndIteration):
|
||||
if event.batch_id % 100 == 0:
|
||||
print "Pass %d, Batch %d, Cost %f" % (
|
||||
event.pass_id, event.batch_id, event.cost)
|
||||
|
||||
if isinstance(event, paddle.event.EndPass):
|
||||
if (event.pass_id + 1) % 10 == 0:
|
||||
result = trainer.test(
|
||||
reader=paddle.batch(
|
||||
uci_housing.test(), batch_size=2),
|
||||
feeding={'x': 0,
|
||||
'y': 1})
|
||||
print "Test %d, %.2f" % (event.pass_id, result.cost)
|
||||
|
||||
# training
|
||||
trainer.train(
|
||||
reader=paddle.batch(
|
||||
paddle.reader.shuffle(
|
||||
uci_housing.train(), buf_size=500),
|
||||
batch_size=2),
|
||||
feeding={'x': 0,
|
||||
'y': 1},
|
||||
event_handler=event_handler,
|
||||
num_passes=30)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -1,26 +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.
|
||||
|
||||
from paddle.trainer.PyDataProvider2 import *
|
||||
import random
|
||||
|
||||
|
||||
# define data types of input: 2 real numbers
|
||||
@provider(
|
||||
input_types={'x': dense_vector(1),
|
||||
'y': dense_vector(1)}, use_seq=False)
|
||||
def process(settings, input_file):
|
||||
for i in xrange(2000):
|
||||
x = random.random()
|
||||
yield {'x': [x], 'y': [2 * x + 0.3]}
|
@ -1,39 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
# 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.
|
||||
"""
|
||||
Print model parameters in last model
|
||||
|
||||
Usage:
|
||||
python evaluate_model.py
|
||||
"""
|
||||
import numpy as np
|
||||
import os
|
||||
|
||||
|
||||
def load(file_name):
|
||||
with open(file_name, 'rb') as f:
|
||||
f.read(16) # skip header for float type.
|
||||
return np.fromfile(f, dtype=np.float32)
|
||||
|
||||
|
||||
def main():
|
||||
print 'w=%.6f, b=%.6f from pass 29' % (load('output/pass-00029/w'),
|
||||
load('output/pass-00029/b'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -1,22 +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
|
||||
|
||||
paddle train \
|
||||
--config=trainer_config.py \
|
||||
--save_dir=./output \
|
||||
--num_passes=30 \
|
||||
2>&1 |tee 'train.log'
|
||||
paddle usage -l "train.log" -e $? -n "introduction" >/dev/null 2>&1
|
@ -1,38 +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.
|
||||
|
||||
from paddle.trainer_config_helpers import *
|
||||
|
||||
# 1. read data. Suppose you saved above python code as dataprovider.py
|
||||
define_py_data_sources2(
|
||||
train_list=['no_matter.txt'],
|
||||
test_list=None,
|
||||
module='dataprovider',
|
||||
obj='process',
|
||||
args={})
|
||||
|
||||
# 2. learning algorithm
|
||||
settings(batch_size=12, learning_rate=1e-3, learning_method=MomentumOptimizer())
|
||||
|
||||
# 3. Network configuration
|
||||
x = data_layer(name='x', size=1)
|
||||
y = data_layer(name='y', size=1)
|
||||
y_predict = fc_layer(
|
||||
input=x,
|
||||
param_attr=ParamAttr(name='w'),
|
||||
size=1,
|
||||
act=LinearActivation(),
|
||||
bias_attr=ParamAttr(name='b'))
|
||||
cost = mse_cost(input=y_predict, label=y)
|
||||
outputs(cost)
|
@ -1,137 +0,0 @@
|
||||
import paddle.v2 as paddle
|
||||
import gzip
|
||||
|
||||
|
||||
def softmax_regression(img):
|
||||
predict = paddle.layer.fc(input=img,
|
||||
size=10,
|
||||
act=paddle.activation.Softmax())
|
||||
return predict
|
||||
|
||||
|
||||
def multilayer_perceptron(img):
|
||||
# The first fully-connected layer
|
||||
hidden1 = paddle.layer.fc(input=img, size=128, act=paddle.activation.Relu())
|
||||
# The second fully-connected layer and the according activation function
|
||||
hidden2 = paddle.layer.fc(input=hidden1,
|
||||
size=64,
|
||||
act=paddle.activation.Relu())
|
||||
# The thrid fully-connected layer, note that the hidden size should be 10,
|
||||
# which is the number of unique digits
|
||||
predict = paddle.layer.fc(input=hidden2,
|
||||
size=10,
|
||||
act=paddle.activation.Softmax())
|
||||
return predict
|
||||
|
||||
|
||||
def convolutional_neural_network(img):
|
||||
# first conv layer
|
||||
conv_pool_1 = paddle.networks.simple_img_conv_pool(
|
||||
input=img,
|
||||
filter_size=5,
|
||||
num_filters=20,
|
||||
num_channel=1,
|
||||
pool_size=2,
|
||||
pool_stride=2,
|
||||
act=paddle.activation.Tanh())
|
||||
# second conv layer
|
||||
conv_pool_2 = paddle.networks.simple_img_conv_pool(
|
||||
input=conv_pool_1,
|
||||
filter_size=5,
|
||||
num_filters=50,
|
||||
num_channel=20,
|
||||
pool_size=2,
|
||||
pool_stride=2,
|
||||
act=paddle.activation.Tanh())
|
||||
# The first fully-connected layer
|
||||
fc1 = paddle.layer.fc(input=conv_pool_2,
|
||||
size=128,
|
||||
act=paddle.activation.Tanh())
|
||||
# The softmax layer, note that the hidden size should be 10,
|
||||
# which is the number of unique digits
|
||||
predict = paddle.layer.fc(input=fc1,
|
||||
size=10,
|
||||
act=paddle.activation.Softmax())
|
||||
return predict
|
||||
|
||||
|
||||
def main():
|
||||
paddle.init(use_gpu=False, trainer_count=1)
|
||||
|
||||
# define network topology
|
||||
images = paddle.layer.data(
|
||||
name='pixel', type=paddle.data_type.dense_vector(784))
|
||||
label = paddle.layer.data(
|
||||
name='label', type=paddle.data_type.integer_value(10))
|
||||
|
||||
# Here we can build the prediction network in different ways. Please
|
||||
# choose one by uncomment corresponding line.
|
||||
predict = softmax_regression(images)
|
||||
#predict = multilayer_perceptron(images)
|
||||
#predict = convolutional_neural_network(images)
|
||||
|
||||
cost = paddle.layer.classification_cost(input=predict, label=label)
|
||||
|
||||
try:
|
||||
with gzip.open('params.tar.gz', 'r') as f:
|
||||
parameters = paddle.parameters.Parameters.from_tar(f)
|
||||
except IOError:
|
||||
parameters = paddle.parameters.create(cost)
|
||||
|
||||
optimizer = paddle.optimizer.Momentum(
|
||||
learning_rate=0.1 / 128.0,
|
||||
momentum=0.9,
|
||||
regularization=paddle.optimizer.L2Regularization(rate=0.0005 * 128))
|
||||
|
||||
trainer = paddle.trainer.SGD(cost=cost,
|
||||
parameters=parameters,
|
||||
update_equation=optimizer)
|
||||
|
||||
lists = []
|
||||
|
||||
def event_handler(event):
|
||||
if isinstance(event, paddle.event.EndIteration):
|
||||
if event.batch_id % 1000 == 0:
|
||||
print "Pass %d, Batch %d, Cost %f, %s" % (
|
||||
event.pass_id, event.batch_id, event.cost, event.metrics)
|
||||
|
||||
with gzip.open('params.tar.gz', 'w') as f:
|
||||
parameters.to_tar(f)
|
||||
|
||||
elif isinstance(event, paddle.event.EndPass):
|
||||
result = trainer.test(reader=paddle.batch(
|
||||
paddle.dataset.mnist.test(), batch_size=128))
|
||||
print "Test with Pass %d, Cost %f, %s\n" % (
|
||||
event.pass_id, result.cost, result.metrics)
|
||||
lists.append((event.pass_id, result.cost,
|
||||
result.metrics['classification_error_evaluator']))
|
||||
|
||||
trainer.train(
|
||||
reader=paddle.batch(
|
||||
paddle.reader.shuffle(
|
||||
paddle.dataset.mnist.train(), buf_size=8192),
|
||||
batch_size=128),
|
||||
event_handler=event_handler,
|
||||
num_passes=100)
|
||||
|
||||
# find the best pass
|
||||
best = sorted(lists, key=lambda list: float(list[1]))[0]
|
||||
print 'Best pass is %s, testing Avgcost is %s' % (best[0], best[1])
|
||||
print 'The classification accuracy is %.2f%%' % (100 - float(best[2]) * 100)
|
||||
|
||||
test_creator = paddle.dataset.mnist.test()
|
||||
test_data = []
|
||||
for item in test_creator():
|
||||
test_data.append((item[0], ))
|
||||
if len(test_data) == 100:
|
||||
break
|
||||
|
||||
# output is a softmax layer. It returns probabilities.
|
||||
# Shape should be (100, 10)
|
||||
probs = paddle.infer(
|
||||
output_layer=predict, parameters=parameters, input=test_data)
|
||||
print probs.shape
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -1,10 +0,0 @@
|
||||
log.txt
|
||||
data/meta.bin
|
||||
data/ml-1m
|
||||
data/ratings.dat.train
|
||||
data/ratings.dat.test
|
||||
data/train.list
|
||||
data/test.list
|
||||
dataprovider_copy_1.py
|
||||
*.pyc
|
||||
output
|
@ -1,125 +0,0 @@
|
||||
import paddle.v2 as paddle
|
||||
import cPickle
|
||||
import copy
|
||||
|
||||
|
||||
def main():
|
||||
paddle.init(use_gpu=False)
|
||||
movie_title_dict = paddle.dataset.movielens.get_movie_title_dict()
|
||||
uid = paddle.layer.data(
|
||||
name='user_id',
|
||||
type=paddle.data_type.integer_value(
|
||||
paddle.dataset.movielens.max_user_id() + 1))
|
||||
usr_emb = paddle.layer.embedding(input=uid, size=32)
|
||||
|
||||
usr_gender_id = paddle.layer.data(
|
||||
name='gender_id', type=paddle.data_type.integer_value(2))
|
||||
usr_gender_emb = paddle.layer.embedding(input=usr_gender_id, size=16)
|
||||
|
||||
usr_age_id = paddle.layer.data(
|
||||
name='age_id',
|
||||
type=paddle.data_type.integer_value(
|
||||
len(paddle.dataset.movielens.age_table)))
|
||||
usr_age_emb = paddle.layer.embedding(input=usr_age_id, size=16)
|
||||
|
||||
usr_job_id = paddle.layer.data(
|
||||
name='job_id',
|
||||
type=paddle.data_type.integer_value(paddle.dataset.movielens.max_job_id(
|
||||
) + 1))
|
||||
|
||||
usr_job_emb = paddle.layer.embedding(input=usr_job_id, size=16)
|
||||
|
||||
usr_combined_features = paddle.layer.fc(
|
||||
input=[usr_emb, usr_gender_emb, usr_age_emb, usr_job_emb],
|
||||
size=200,
|
||||
act=paddle.activation.Tanh())
|
||||
|
||||
mov_id = paddle.layer.data(
|
||||
name='movie_id',
|
||||
type=paddle.data_type.integer_value(
|
||||
paddle.dataset.movielens.max_movie_id() + 1))
|
||||
mov_emb = paddle.layer.embedding(input=mov_id, size=32)
|
||||
|
||||
mov_categories = paddle.layer.data(
|
||||
name='category_id',
|
||||
type=paddle.data_type.sparse_binary_vector(
|
||||
len(paddle.dataset.movielens.movie_categories())))
|
||||
|
||||
mov_categories_hidden = paddle.layer.fc(input=mov_categories, size=32)
|
||||
|
||||
mov_title_id = paddle.layer.data(
|
||||
name='movie_title',
|
||||
type=paddle.data_type.integer_value_sequence(len(movie_title_dict)))
|
||||
mov_title_emb = paddle.layer.embedding(input=mov_title_id, size=32)
|
||||
mov_title_conv = paddle.networks.sequence_conv_pool(
|
||||
input=mov_title_emb, hidden_size=32, context_len=3)
|
||||
|
||||
mov_combined_features = paddle.layer.fc(
|
||||
input=[mov_emb, mov_categories_hidden, mov_title_conv],
|
||||
size=200,
|
||||
act=paddle.activation.Tanh())
|
||||
|
||||
inference = paddle.layer.cos_sim(
|
||||
a=usr_combined_features, b=mov_combined_features, size=1, scale=5)
|
||||
cost = paddle.layer.mse_cost(
|
||||
input=inference,
|
||||
label=paddle.layer.data(
|
||||
name='score', type=paddle.data_type.dense_vector(1)))
|
||||
|
||||
parameters = paddle.parameters.create(cost)
|
||||
|
||||
trainer = paddle.trainer.SGD(cost=cost,
|
||||
parameters=parameters,
|
||||
update_equation=paddle.optimizer.Adam(
|
||||
learning_rate=1e-4))
|
||||
feeding = {
|
||||
'user_id': 0,
|
||||
'gender_id': 1,
|
||||
'age_id': 2,
|
||||
'job_id': 3,
|
||||
'movie_id': 4,
|
||||
'category_id': 5,
|
||||
'movie_title': 6,
|
||||
'score': 7
|
||||
}
|
||||
|
||||
def event_handler(event):
|
||||
if isinstance(event, paddle.event.EndIteration):
|
||||
if event.batch_id % 100 == 0:
|
||||
print "Pass %d Batch %d Cost %.2f" % (
|
||||
event.pass_id, event.batch_id, event.cost)
|
||||
|
||||
trainer.train(
|
||||
reader=paddle.batch(
|
||||
paddle.reader.shuffle(
|
||||
paddle.dataset.movielens.train(), buf_size=8192),
|
||||
batch_size=256),
|
||||
event_handler=event_handler,
|
||||
feeding=feeding,
|
||||
num_passes=1)
|
||||
|
||||
user_id = 234
|
||||
movie_id = 345
|
||||
|
||||
user = paddle.dataset.movielens.user_info()[user_id]
|
||||
movie = paddle.dataset.movielens.movie_info()[movie_id]
|
||||
|
||||
feature = user.value() + movie.value()
|
||||
|
||||
def reader():
|
||||
yield feature
|
||||
|
||||
infer_dict = copy.copy(feeding)
|
||||
del infer_dict['score']
|
||||
|
||||
prediction = paddle.infer(
|
||||
output=inference,
|
||||
parameters=parameters,
|
||||
reader=paddle.batch(
|
||||
reader, batch_size=32),
|
||||
feeding=infer_dict)
|
||||
print(prediction + 5) / 2
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -1,30 +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.
|
||||
from paddle.trainer.PyDataProvider2 import *
|
||||
|
||||
|
||||
def meta_to_header(meta, name):
|
||||
metas = meta[name]['__meta__']['raw_meta']
|
||||
for each_meta in metas:
|
||||
slot_name = each_meta.get('name', '%s_id' % name)
|
||||
if each_meta['type'] == 'id':
|
||||
yield slot_name, integer_value(each_meta['max'])
|
||||
elif each_meta['type'] == 'embedding':
|
||||
is_seq = each_meta['seq'] == 'sequence'
|
||||
yield slot_name, integer_value(
|
||||
len(each_meta['dict']),
|
||||
seq_type=SequenceType.SEQUENCE
|
||||
if is_seq else SequenceType.NO_SEQUENCE)
|
||||
elif each_meta['type'] == 'one_hot_dense':
|
||||
yield slot_name, dense_vector(len(each_meta['dict']))
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue