提交 9796a1ee 编写于 作者: D dangqingqing

code formatting

上级 7443cd4d
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import os,sys import os, sys
import cPickle import cPickle
import numpy as np import numpy as np
from PIL import Image from PIL import Image
...@@ -24,7 +24,8 @@ from paddle.trainer.PyDataProvider2 import dense_vector ...@@ -24,7 +24,8 @@ from paddle.trainer.PyDataProvider2 import dense_vector
from paddle.trainer.config_parser import parse_config from paddle.trainer.config_parser import parse_config
import logging import logging
logging.basicConfig(format='[%(levelname)s %(asctime)s %(filename)s:%(lineno)s] %(message)s') logging.basicConfig(
format='[%(levelname)s %(asctime)s %(filename)s:%(lineno)s] %(message)s')
logging.getLogger().setLevel(logging.INFO) logging.getLogger().setLevel(logging.INFO)
...@@ -32,24 +33,28 @@ def vis_square(data, fname): ...@@ -32,24 +33,28 @@ def vis_square(data, fname):
import matplotlib import matplotlib
matplotlib.use('Agg') matplotlib.use('Agg')
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
"""Take an array of shape (n, height, width) or (n, height, width, 3) """Take an array of shape (n, height, width) or (n, height, width, 3)
and visualize each (height, width) thing in a grid of size approx. sqrt(n) by sqrt(n)""" and visualize each (height, width) thing in a grid of size approx. sqrt(n) by sqrt(n)"""
# normalize data for display # normalize data for display
data = (data - data.min()) / (data.max() - data.min()) data = (data - data.min()) / (data.max() - data.min())
# force the number of filters to be square # force the number of filters to be square
n = int(np.ceil(np.sqrt(data.shape[0]))) n = int(np.ceil(np.sqrt(data.shape[0])))
padding = (((0, n ** 2 - data.shape[0]), padding = (
(0, 1), (0, 1)) # add some space between filters ((0, n**2 - data.shape[0]), (0, 1),
+ ((0, 0),) * (data.ndim - 3)) # don't pad the last dimension (if there is one) (0, 1)) # add some space between filters
data = np.pad(data, padding, mode='constant', constant_values=1) # pad with ones (white) + ((0, 0), ) *
(data.ndim - 3)) # don't pad the last dimension (if there is one)
data = np.pad(data, padding, mode='constant',
constant_values=1) # pad with ones (white)
# tile the filters into an image # tile the filters into an image
data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1))) data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(
range(4, data.ndim + 1)))
data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:]) data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])
plt.imshow(data, cmap='gray') plt.imshow(data, cmap='gray')
plt.savefig(fname) plt.savefig(fname)
plt.axis('off') plt.axis('off')
class ImageClassifier(): class ImageClassifier():
def __init__(self, def __init__(self,
train_conf, train_conf,
...@@ -70,24 +75,25 @@ class ImageClassifier(): ...@@ -70,24 +75,25 @@ class ImageClassifier():
self.oversample = oversample self.oversample = oversample
self.is_color = is_color self.is_color = is_color
self.transformer = image_util.ImageTransformer(is_color = is_color) self.transformer = image_util.ImageTransformer(is_color=is_color)
self.transformer.set_transpose((2,0,1)) self.transformer.set_transpose((2, 0, 1))
self.transformer.set_channel_swap((2,1,0)) self.transformer.set_channel_swap((2, 1, 0))
self.mean_file = mean_file self.mean_file = mean_file
if self.mean_file is not None: if self.mean_file is not None:
mean = np.load(self.mean_file)['mean'] mean = np.load(self.mean_file)['mean']
mean = mean.reshape(3, self.crop_dims[0], self.crop_dims[1]) mean = mean.reshape(3, self.crop_dims[0], self.crop_dims[1])
self.transformer.set_mean(mean) # mean pixel self.transformer.set_mean(mean) # mean pixel
else: else:
# if you use three mean value, set like: # if you use three mean value, set like:
# this three mean value is calculated from ImageNet. # this three mean value is calculated from ImageNet.
self.transformer.set_mean(np.array([103.939,116.779,123.68])) self.transformer.set_mean(np.array([103.939, 116.779, 123.68]))
conf_args = "use_gpu=%d,is_predict=1" % (int(use_gpu)) conf_args = "use_gpu=%d,is_predict=1" % (int(use_gpu))
conf = parse_config(train_conf, conf_args) conf = parse_config(train_conf, conf_args)
swig_paddle.initPaddle("--use_gpu=%d" % (int(use_gpu))) swig_paddle.initPaddle("--use_gpu=%d" % (int(use_gpu)))
self.network = swig_paddle.GradientMachine.createFromConfigProto(conf.model_config) self.network = swig_paddle.GradientMachine.createFromConfigProto(
conf.model_config)
assert isinstance(self.network, swig_paddle.GradientMachine) assert isinstance(self.network, swig_paddle.GradientMachine)
self.network.loadParameters(self.model_dir) self.network.loadParameters(self.model_dir)
...@@ -112,14 +118,14 @@ class ImageClassifier(): ...@@ -112,14 +118,14 @@ class ImageClassifier():
if self.oversample: if self.oversample:
image = image_util.resize_image(image, self.resize_dim) image = image_util.resize_image(image, self.resize_dim)
image = np.array(image) image = np.array(image)
input = np.zeros((1, image.shape[0], image.shape[1], 3), input = np.zeros(
dtype=np.float32) (1, image.shape[0], image.shape[1], 3), dtype=np.float32)
input[0] = image.astype(np.float32) input[0] = image.astype(np.float32)
input = image_util.oversample(input, self.crop_dims) input = image_util.oversample(input, self.crop_dims)
else: else:
image = image.resize(self.crop_dims, Image.ANTIALIAS) image = image.resize(self.crop_dims, Image.ANTIALIAS)
input = np.zeros((1, self.crop_dims[0], self.crop_dims[1], 3), input = np.zeros(
dtype=np.float32) (1, self.crop_dims[0], self.crop_dims[1], 3), dtype=np.float32)
input[0] = np.array(image).astype(np.float32) input[0] = np.array(image).astype(np.float32)
data_in = [] data_in = []
...@@ -146,63 +152,75 @@ class ImageClassifier(): ...@@ -146,63 +152,75 @@ class ImageClassifier():
res[name] = output[name].mean(0) res[name] = output[name].mean(0)
return res return res
def option_parser(): def option_parser():
usage = "%prog -c config -i data_list -w model_dir [options]" usage = "%prog -c config -i data_list -w model_dir [options]"
parser = OptionParser(usage="usage: %s" % usage) parser = OptionParser(usage="usage: %s" % usage)
parser.add_option("--job", parser.add_option(
action="store", "--job",
dest="job_type", action="store",
choices=['predict', 'extract',], dest="job_type",
default='predict', choices=[
help="The job type. \ 'predict',
'extract',
],
default='predict',
help="The job type. \
predict: predicting,\ predict: predicting,\
extract: extract features") extract: extract features")
parser.add_option("--conf", parser.add_option(
action="store", "--conf",
dest="train_conf", action="store",
default='models/vgg.py', dest="train_conf",
help="network config") default='models/vgg.py',
parser.add_option("--data", help="network config")
action="store", parser.add_option(
dest="data_file", "--data",
default='image/dog.png', action="store",
help="image list") dest="data_file",
parser.add_option("--model", default='image/dog.png',
action="store", help="image list")
dest="model_path", parser.add_option(
default=None, "--model",
help="model path") action="store",
parser.add_option("-c", dest="model_path",
dest="cpu_gpu", default=None,
action="store_false", help="model path")
help="Use cpu mode.") parser.add_option(
parser.add_option("-g", "-c", dest="cpu_gpu", action="store_false", help="Use cpu mode.")
dest="cpu_gpu", parser.add_option(
default=True, "-g",
action="store_true", dest="cpu_gpu",
help="Use gpu mode.") default=True,
parser.add_option("--mean", action="store_true",
action="store", help="Use gpu mode.")
dest="mean", parser.add_option(
default='data/mean.meta', "--mean",
help="The mean file.") action="store",
parser.add_option("--multi_crop", dest="mean",
action="store_true", default='data/mean.meta',
dest="multi_crop", help="The mean file.")
default=False, parser.add_option(
help="Wether to use multiple crops on image.") "--multi_crop",
action="store_true",
dest="multi_crop",
default=False,
help="Wether to use multiple crops on image.")
return parser.parse_args() return parser.parse_args()
def main(): def main():
options, args = option_parser() options, args = option_parser()
mean = 'data/mean.meta' if not options.mean else options.mean mean = 'data/mean.meta' if not options.mean else options.mean
conf = 'models/vgg.py' if not options.train_conf else options.train_conf conf = 'models/vgg.py' if not options.train_conf else options.train_conf
obj = ImageClassifier(conf, obj = ImageClassifier(
32,32, conf,
options.model_path, 32,
use_gpu=options.cpu_gpu, 32,
mean_file=mean, options.model_path,
oversample=options.multi_crop) use_gpu=options.cpu_gpu,
mean_file=mean,
oversample=options.multi_crop)
image_path = options.data_file image_path = options.data_file
if options.job_type == 'predict': if options.job_type == 'predict':
output_layer = '__fc_layer_2__' output_layer = '__fc_layer_2__'
...@@ -219,5 +237,6 @@ def main(): ...@@ -219,5 +237,6 @@ def main():
fea = features[output_layer].reshape(dshape) fea = features[output_layer].reshape(dshape)
vis_square(fea, 'fea_conv0.png') vis_square(fea, 'fea_conv0.png')
if __name__ == '__main__': if __name__ == '__main__':
main() main()
...@@ -16,6 +16,7 @@ import numpy as np ...@@ -16,6 +16,7 @@ import numpy as np
import cPickle import cPickle
from paddle.trainer.PyDataProvider2 import * from paddle.trainer.PyDataProvider2 import *
def initializer(settings, mean_path, is_train, **kwargs): def initializer(settings, mean_path, is_train, **kwargs):
settings.is_train = is_train settings.is_train = is_train
settings.input_size = 3 * 32 * 32 settings.input_size = 3 * 32 * 32
...@@ -37,7 +38,4 @@ def process(settings, file_list): ...@@ -37,7 +38,4 @@ def process(settings, file_list):
labels = batch['labels'] labels = batch['labels']
for im, lab in zip(images, labels): for im, lab in zip(images, labels):
im = im - settings.mean im = im - settings.mean
yield { yield {'image': im.astype('float32'), 'label': int(lab)}
'image': im.astype('float32'),
'label': int(lab)
}
...@@ -59,6 +59,7 @@ def shortcut(ipt, n_in, n_out, stride): ...@@ -59,6 +59,7 @@ def shortcut(ipt, n_in, n_out, stride):
else: else:
return ipt return ipt
def basicblock(ipt, ch_out, stride): def basicblock(ipt, ch_out, stride):
ch_in = ipt.num_filters ch_in = ipt.num_filters
tmp = conv_bn_layer(ipt, ch_out, 3, stride, 1) tmp = conv_bn_layer(ipt, ch_out, 3, stride, 1)
...@@ -66,6 +67,7 @@ def basicblock(ipt, ch_out, stride): ...@@ -66,6 +67,7 @@ def basicblock(ipt, ch_out, stride):
short = shortcut(ipt, ch_in, ch_out, stride) short = shortcut(ipt, ch_in, ch_out, stride)
return addto_layer(input=[ipt, short], act=ReluActivation()) return addto_layer(input=[ipt, short], act=ReluActivation())
def bottleneck(ipt, ch_out, stride): def bottleneck(ipt, ch_out, stride):
ch_in = ipt.num_filter ch_in = ipt.num_filter
tmp = conv_bn_layer(ipt, ch_out, 1, stride, 0) tmp = conv_bn_layer(ipt, ch_out, 1, stride, 0)
...@@ -74,55 +76,49 @@ def bottleneck(ipt, ch_out, stride): ...@@ -74,55 +76,49 @@ def bottleneck(ipt, ch_out, stride):
short = shortcut(ipt, ch_in, ch_out, stride) short = shortcut(ipt, ch_in, ch_out, stride)
return addto_layer(input=[ipt, short], act=ReluActivation()) return addto_layer(input=[ipt, short], act=ReluActivation())
def layer_warp(block_func, ipt, features, count, stride): def layer_warp(block_func, ipt, features, count, stride):
tmp = block_func(ipt, features, stride) tmp = block_func(ipt, features, stride)
for i in range(1, count): for i in range(1, count):
tmp = block_func(tmp, features, 1) tmp = block_func(tmp, features, 1)
return tmp return tmp
def resnet_imagenet(ipt, depth=50): def resnet_imagenet(ipt, depth=50):
cfg = {18 : ([2,2,2,1], basicblock), cfg = {
34 : ([3,4,6,3], basicblock), 18: ([2, 2, 2, 1], basicblock),
50 : ([3,4,6,3], bottleneck), 34: ([3, 4, 6, 3], basicblock),
101: ([3,4,23,3], bottleneck), 50: ([3, 4, 6, 3], bottleneck),
152: ([3,8,36,3], bottleneck)} 101: ([3, 4, 23, 3], bottleneck),
152: ([3, 8, 36, 3], bottleneck)
}
stages, block_func = cfg[depth] stages, block_func = cfg[depth]
tmp = conv_bn_layer(ipt, tmp = conv_bn_layer(
ch_in=3, ipt, ch_in=3, ch_out=64, filter_size=7, stride=2, padding=3)
ch_out=64,
filter_size=7,
stride=2,
padding=3)
tmp = img_pool_layer(input=tmp, pool_size=3, stride=2) tmp = img_pool_layer(input=tmp, pool_size=3, stride=2)
tmp = layer_warp(block_func, tmp, 64, stages[0], 1) tmp = layer_warp(block_func, tmp, 64, stages[0], 1)
tmp = layer_warp(block_func, tmp, 128, stages[1], 2) tmp = layer_warp(block_func, tmp, 128, stages[1], 2)
tmp = layer_warp(block_func, tmp, 256, stages[2], 2) tmp = layer_warp(block_func, tmp, 256, stages[2], 2)
tmp = layer_warp(block_func, tmp, 512, stages[3], 2) tmp = layer_warp(block_func, tmp, 512, stages[3], 2)
tmp = img_pool_layer(input=tmp, tmp = img_pool_layer(
pool_size=7, input=tmp, pool_size=7, stride=1, pool_type=AvgPooling())
stride=1,
pool_type=AvgPooling())
tmp = fc_layer(input=tmp, size=1000, act=SoftmaxActivation()) tmp = fc_layer(input=tmp, size=1000, act=SoftmaxActivation())
return tmp return tmp
def resnet_cifar10(ipt, depth=56): def resnet_cifar10(ipt, depth=56):
assert((depth - 2) % 6 == 0, 'depth should be one of 20, 32, 44, 56, 110, 1202') assert ((depth - 2) % 6 == 0,
'depth should be one of 20, 32, 44, 56, 110, 1202')
n = (depth - 2) / 6 n = (depth - 2) / 6
nStages = {16, 64, 128} nStages = {16, 64, 128}
tmp = conv_bn_layer(ipt, tmp = conv_bn_layer(
ch_in=3, ipt, ch_in=3, ch_out=16, filter_size=3, stride=1, padding=1)
ch_out=16,
filter_size=3,
stride=1,
padding=1)
tmp = layer_warp(basicblock, tmp, 16, n, 1) tmp = layer_warp(basicblock, tmp, 16, n, 1)
tmp = layer_warp(basicblock, tmp, 32, n, 2) tmp = layer_warp(basicblock, tmp, 32, n, 2)
tmp = layer_warp(basicblock, tmp, 64, n, 2) tmp = layer_warp(basicblock, tmp, 64, n, 2)
tmp = img_pool_layer(input=tmp, tmp = img_pool_layer(
pool_size=8, input=tmp, pool_size=8, stride=1, pool_type=AvgPooling())
stride=1,
pool_type=AvgPooling())
return tmp return tmp
......
...@@ -30,7 +30,7 @@ settings( ...@@ -30,7 +30,7 @@ settings(
learning_rate_decay_b=50000 * 100, learning_rate_decay_b=50000 * 100,
learning_rate_schedule='discexp', learning_rate_schedule='discexp',
learning_method=MomentumOptimizer(0.9), learning_method=MomentumOptimizer(0.9),
regularization=L2Regularization(0.0005 * 128),) regularization=L2Regularization(0.0005 * 128), )
def vgg_bn_drop(input): def vgg_bn_drop(input):
...@@ -54,19 +54,13 @@ def vgg_bn_drop(input): ...@@ -54,19 +54,13 @@ def vgg_bn_drop(input):
tmp = conv_block(tmp, 512, 3, [0.4, 0.4, 0]) tmp = conv_block(tmp, 512, 3, [0.4, 0.4, 0])
tmp = dropout_layer(input=tmp, dropout_rate=0.5) tmp = dropout_layer(input=tmp, dropout_rate=0.5)
tmp = fc_layer( tmp = fc_layer(input=tmp, size=512, act=LinearActivation())
input=tmp, tmp = batch_norm_layer(
size=512, input=tmp, act=ReluActivation(), layer_attr=ExtraAttr(drop_rate=0.5))
act=LinearActivation()) tmp = fc_layer(input=tmp, size=512, act=LinearActivation())
tmp = batch_norm_layer(input=tmp,
act=ReluActivation(),
layer_attr=ExtraAttr(drop_rate=0.5))
tmp = fc_layer(
input=tmp,
size=512,
act=LinearActivation())
return tmp return tmp
datadim = 3 * 32 * 32 datadim = 3 * 32 * 32
classdim = 10 classdim = 10
data = data_layer(name='image', size=datadim) data = data_layer(name='image', size=datadim)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册