Why predict.py script not work for googlenet?
Created by: jueljust
i got no label error when i try to do predict with googlenet model, but i've announce the is_test and is_predict arguments. i could find the reason for this issue. the predict script modified base on demo/sentiment/predict.py and demo/image_classification/predict.py, keep the same structure and settings. but i got the error like this:
python predict.py runs/pass-00000/ evaluate.txt 0
I0504 12:55:08.882659 29769 Util.cpp:166] commandline: --use_gpu=0
I0504 12:55:08.947487 29769 GradientMachine.cpp:75] Loading parameters from runs/pass-00000/
<py_paddle.swig_paddle.Arguments; proxy of <Swig Object of type 'Arguments *' at 0xb07f240> >
F0504 12:55:57.603358 29769 Matrix.cpp:3271] Check failed: dynamic_cast<CpuIVector*>(&label)
*** Check failure stack trace: ***
@ 0x7f1692b1448d google::LogMessage::Fail()
@ 0x7f1692b162ff google::LogMessage::SendToLog()
@ 0x7f1692b14033 google::LogMessage::Flush()
@ 0x7f1692b16c1e google::LogMessageFatal::~LogMessageFatal()
@ 0x7f1692a5b4b9 paddle::CpuMatrix::oneHotCrossEntropy()
@ 0x7f16928ea695 paddle::CostLayer::forward()
@ 0x7f1692939440 paddle::NeuralNetwork::forward()
@ 0x7f169280b8a5 _wrap_GradientMachine_forward
@ 0x7f16ba7ed00a PyEval_EvalFrameEx
@ 0x7f16ba7eee3d PyEval_EvalCodeEx
@ 0x7f16ba7ec33c PyEval_EvalFrameEx
@ 0x7f16ba7eee3d PyEval_EvalCodeEx
@ 0x7f16ba7ec33c PyEval_EvalFrameEx
@ 0x7f16ba7ec4bd PyEval_EvalFrameEx
@ 0x7f16ba7ec4bd PyEval_EvalFrameEx
@ 0x7f16ba7eee3d PyEval_EvalCodeEx
@ 0x7f16ba7eef42 PyEval_EvalCode
@ 0x7f16ba80837f (unknown)
@ 0x7f16ba80953e PyRun_FileExFlags
@ 0x7f16ba80a7c9 PyRun_SimpleFileExFlags
@ 0x7f16ba81b91f Py_Main
@ 0x7f16b9a43b35 __libc_start_main
@ 0x40071e (unknown)
@ (nil) (unknown)
job=test command got the same error
paddle train \
--job=test \
--use_gpu=false \
--config=googlenet.py \
--init_model_path=runs/pass-00000 \
--config_args=is_predict=1,is_test=1,use_gpu=0 \
--predict_output_dir=result
[INFO 2017-05-04 13:22:28,228 layers.py:2189] output for conv_o2: c = 128, h = 4, w = 4, size = 2048
[INFO 2017-05-04 13:22:33,438 networks.py:1472] The input order is [input]
[INFO 2017-05-04 13:22:33,439 networks.py:1478] The output order is [output3]
I0504 13:22:33.441318 9064 Trainer.cpp:141] trainer: in testing mode
I0504 13:22:33.441360 9064 Trainer.cpp:148] trainer mode: Testing
I0504 13:22:33.525315 9064 PyDataProvider2.cpp:243] loading dataprovider provider::process
I0504 13:22:34.014806 9064 GradientMachine.cpp:75] Loading parameters from runs/pass-00000
F0504 13:23:22.886416 9064 Matrix.cpp:3271] Check failed: dynamic_cast<CpuIVector*>(&label)
*** Check failure stack trace: ***
@ 0x8541cd google::LogMessage::Fail()
@ 0x85603f google::LogMessage::SendToLog()
@ 0x853d73 google::LogMessage::Flush()
@ 0x85695e google::LogMessageFatal::~LogMessageFatal()
@ 0x78b779 paddle::CpuMatrix::oneHotCrossEntropy()
@ 0x5ed2f5 paddle::CostLayer::forward()
@ 0x63c5e0 paddle::NeuralNetwork::forward()
@ 0x6eddc8 paddle::Tester::forwardOneBatch()
@ 0x6ee892 paddle::Tester::testOneBatchById()
@ 0x6eebaf paddle::Tester::testOnePass()
@ 0x6ef02f paddle::Tester::test()
@ 0x53bff2 main
@ 0x7fdafde31b35 __libc_start_main
@ 0x549b99 (unknown)
@ (nil) (unknown)
/opt/paddle/bin/paddle: line 113: 9064 Aborted (core dumped) ${DEBUGGER} $MYDIR/../opt/paddle/bin/paddle_trainer ${@:2}
predict.py
import os, sys
import numpy as np
import logging
from skimage import io
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,
batch_size=128,
is_color=True):
self.train_conf = train_conf
self.model_dir = model_dir
self.batch_size = batch_size
if model_dir is None:
self.model_dir = os.path.dirname(train_conf)
self.is_color = is_color
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 * 224 * 224
slots = [dense_vector(data_size)]
self.converter = DataProviderConverter(slots)
def predict(self, data_file):
with open(data_file, 'r') as fdata:
batch = []
sample_ids = []
for line in fdata:
ex = line.strip().split(' ')
file_name = ex[0]
label = ex[1]
sample_id = os.path.basename(file_name)[:-4]
#print [sample_id, file_name, label]
img = io.imread(file_name).flatten()/256.0
batch.append([img.tolist()])
sample_ids.append(sample_id)
if len(batch) == self.batch_size:
self.batch_predict(batch, sample_ids)
batch = []
sample_ids = []
if len(batch) > 0:
self.batch_predict(batch, sample_ids)
batch = []
sample_ids = []
def batch_predict(self, data_batch, sample_ids):
#print data_batch
input = self.converter(data_batch)
print input
output = self.network.forwardTest(input)
print output
prob = output[0]["value"]
labs = np.argsort(-prob)
result = zip(prob, labs)
for idx, (prob, lab) in enumerate(result):
one_result = {
"idx": idx,
"argmax": int(lab[0]),
#"label_text": self.label[lab[0]],
"probs": map(float, prob),
"sample_id": sample_ids[idx]
}
print json.dumps(one_result, ensure_ascii=False)
if __name__ == '__main__':
config = "googlenet.py"
model_path = sys.argv[1]
test_list = sys.argv[2]
use_gpu = bool(int(sys.argv[3]))
obj = ImageClassifier(
train_conf=config,
model_dir=model_path,
use_gpu=use_gpu)
obj.predict(test_list)
googlenet.py
#!/usr/bin/env python
from paddle.trainer_config_helpers import *
height = 224
width = 224
num_class = 2
batch_size = get_config_arg('batch_size', int, 128)
is_predict = get_config_arg('is_predict', bool, False)
is_test = get_config_arg('is_predict', bool, False)
args = {'height': height, 'width': width, 'color': True, 'num_class': num_class, "is_test": is_test, 'is_predict': is_predict}
define_py_data_sources2("train.list", "test.list", module="provider", obj="process", args=args)
settings(
batch_size=batch_size,
learning_rate=0.01 / batch_size,
learning_method=MomentumOptimizer(0.9),
regularization=L2Regularization(0.0005 * batch_size))
def inception2(name, input, channels, \
filter1,
filter3R, filter3,
filter5R, filter5,
proj):
conv1 = name + '_1'
conv3r = name + '_3r'
conv3 = name + '_3'
conv5r = name + '_5r'
conv5 = name + '_5'
maxpool = name + '_max'
convproj = name + '_proj'
cov1 = img_conv_layer(
name=conv1,
input=input,
filter_size=1,
num_channels=channels,
num_filters=filter1,
stride=1,
padding=0)
cov3r = img_conv_layer(
name=conv3r,
input=input,
filter_size=1,
num_channels=channels,
num_filters=filter3R,
stride=1,
padding=0)
cov3 = img_conv_layer(
name=conv3,
input=cov3r,
filter_size=3,
num_filters=filter3,
stride=1,
padding=1)
cov5r = img_conv_layer(
name=conv5r,
input=input,
filter_size=1,
num_channels=channels,
num_filters=filter5R,
stride=1,
padding=0)
cov5 = img_conv_layer(
name=conv5,
input=cov5r,
filter_size=5,
num_filters=filter5,
stride=1,
padding=2)
pool1 = img_pool_layer(
name=maxpool,
input=input,
pool_size=3,
num_channels=channels,
stride=1,
padding=1)
covprj = img_conv_layer(
name=convproj,
input=pool1,
filter_size=1,
num_filters=proj,
stride=1,
padding=0)
cat = concat_layer(name=name, input=[cov1, cov3, cov5, covprj])
return cat
def inception(name, input, channels, \
filter1,
filter3R, filter3,
filter5R, filter5,
proj):
conv1 = name + '_1'
conv3r = name + '_3r'
conv3 = name + '_3'
conv5r = name + '_5r'
conv5 = name + '_5'
maxpool = name + '_max'
convproj = name + '_proj'
cov1 = conv_projection(
name=conv1,
input=input,
filter_size=1,
num_channels=channels,
num_filters=filter1,
stride=1,
padding=0)
cov3r = img_conv_layer(
name=conv3r,
input=input,
filter_size=1,
num_channels=channels,
num_filters=filter3R,
stride=1,
padding=0)
cov3 = conv_projection(
name=conv3,
input=cov3r,
filter_size=3,
num_filters=filter3,
stride=1,
padding=1)
cov5r = img_conv_layer(
name=conv5r,
input=input,
filter_size=1,
num_channels=channels,
num_filters=filter5R,
stride=1,
padding=0)
cov5 = conv_projection(
name=conv5,
input=cov5r,
filter_size=5,
num_filters=filter5,
stride=1,
padding=2)
pool1 = img_pool_layer(
name=maxpool,
input=input,
pool_size=3,
num_channels=channels,
stride=1,
padding=1)
covprj = conv_projection(
name=convproj,
input=pool1,
filter_size=1,
num_filters=proj,
stride=1,
padding=0)
cat = concat_layer(
name=name,
input=[cov1, cov3, cov5, covprj],
bias_attr=True,
act=ReluActivation())
return cat
lab = data_layer(name="label", size=num_class)
data = data_layer(name="input", size=3 * height * width)
# stage 1
conv1 = img_conv_layer(
name="conv1",
input=data,
filter_size=7,
num_channels=3,
num_filters=64,
stride=2,
padding=3)
pool1 = img_pool_layer(
name="pool1", input=conv1, pool_size=3, num_channels=64, stride=2)
# stage 2
conv2_1 = img_conv_layer(
name="conv2_1",
input=pool1,
filter_size=1,
num_filters=64,
stride=1,
padding=0)
conv2_2 = img_conv_layer(
name="conv2_2",
input=conv2_1,
filter_size=3,
num_filters=192,
stride=1,
padding=1)
pool2 = img_pool_layer(
name="pool2", input=conv2_2, pool_size=3, num_channels=192, stride=2)
# stage 3
ince3a = inception2("ince3a", pool2, 192, 64, 96, 128, 16, 32, 32)
ince3b = inception2("ince3b", ince3a, 256, 128, 128, 192, 32, 96, 64)
pool3 = img_pool_layer(
name="pool3", input=ince3b, num_channels=480, pool_size=3, stride=2)
# stage 4
ince4a = inception2("ince4a", pool3, 480, 192, 96, 208, 16, 48, 64)
ince4b = inception2("ince4b", ince4a, 512, 160, 112, 224, 24, 64, 64)
ince4c = inception2("ince4c", ince4b, 512, 128, 128, 256, 24, 64, 64)
ince4d = inception2("ince4d", ince4c, 512, 112, 144, 288, 32, 64, 64)
ince4e = inception2("ince4e", ince4d, 528, 256, 160, 320, 32, 128, 128)
pool4 = img_pool_layer(
name="pool4", input=ince4e, num_channels=832, pool_size=3, stride=2)
# stage 5
ince5a = inception2("ince5a", pool4, 832, 256, 160, 320, 32, 128, 128)
ince5b = inception2("ince5b", ince5a, 832, 384, 192, 384, 48, 128, 128)
pool5 = img_pool_layer(
name="pool5",
input=ince5b,
num_channels=1024,
pool_size=7,
stride=7,
pool_type=AvgPooling())
# We remove loss1 and loss2 for all system when testing benchmark
# output 1
pool_o1 = img_pool_layer(name="pool_o1", input=ince4a, num_channels=512, pool_size=5, stride=3, pool_type=AvgPooling())
conv_o1 = img_conv_layer(name="conv_o1", input=pool_o1, filter_size=1, num_filters=128, stride=1, padding=0)
fc_o1 = fc_layer(name="fc_o1", input=conv_o1, size=1024, layer_attr=ExtraAttr(drop_rate=0.7), act=ReluActivation())
out1 = fc_layer(name="output1", input=fc_o1, size=num_class, act=SoftmaxActivation())
loss1 = cross_entropy(name='loss1', input=out1, label=lab, coeff=0.3)
# output 2
pool_o2 = img_pool_layer(name="pool_o2", input=ince4d, num_channels=528, pool_size=5, stride=3, pool_type=AvgPooling())
conv_o2 = img_conv_layer(name="conv_o2", input=pool_o2, filter_size=1, num_filters=128, stride=1, padding=0)
fc_o2 = fc_layer(name="fc_o2", input=conv_o2, size=1024, layer_attr=ExtraAttr(drop_rate=0.7), act=ReluActivation())
out2 = fc_layer(name="output2", input=fc_o2, size=num_class, act=SoftmaxActivation())
loss2 = cross_entropy(name='loss2', input=out2, label=lab, coeff=0.3)
# output 3
dropout = dropout_layer(name="dropout", input=pool5, dropout_rate=0.4)
out3 = fc_layer(name="output3", input=dropout, size=num_class, act=SoftmaxActivation())
loss3 = cross_entropy(name='loss3', input=out3, label=lab)
if is_predict:
outputs(out3)
else:
outputs(loss3)
provider.py
import io, os
import random
import numpy as np
import datetime
from skimage import io
import cv2
from paddle.trainer.PyDataProvider2 import *
def initHook(settings, height, width, color, num_class, is_test, is_predict, **kwargs):
settings.height = height
settings.width = width
settings.color = color
settings.num_class = num_class
settings.is_test = is_test
settings.is_predict = is_predict
if settings.color:
settings.data_size = settings.height * settings.width * 3
else:
settings.data_size = settings.height * settings.width
if is_predict:
settings.slots = [dense_vector(settings.data_size)]
else:
settings.slots = [dense_vector(settings.data_size), integer_value(1)]
@provider(init_hook=initHook, pool_size=768)
def process(settings, file_list):
with open(file_list, 'r') as fdata:
for line_count, line in enumerate(fdata):
ex = line.strip().split(' ')
file_name = ex[0]
img = img=io.imread(file_name).flatten()/256.0
if settings.is_predict:
yield img.astype('float32')
else:
label = ex[1]
lab = int(label)
yield img.astype('float32'), int(lab)