def infer(y_predict, parameters):
    """
    预测并输出模型准确率

    Args:
        y_predict: 输出层,DATADIM维稠密向量
        parameters: 训练完成的模型参数

    Return:
    """
    # 获取测试数据和训练数据,用来验证模型准确度
    train_data = get_data(train())
    test_data = get_data(test())

    # 根据train_data和test_data预测结果,output_layer表示输出层,parameters表示模型参数,input表示输入的测试数据
    probs_train = paddle.infer(output_layer=y_predict,
                               parameters=parameters,
                               input=train_data['image'])
    probs_test = paddle.infer(output_layer=y_predict,
                              parameters=parameters,
                              input=test_data['image'])

    # 计算train_accuracy和test_accuracy
    print("train_accuracy: {} %".format(calc_accuracy(probs_train,
                                                      train_data)))
    print("test_accuracy: {} %".format(calc_accuracy(probs_test, test_data)))
Esempio n. 2
0
def lambda_rank_infer(input_dim, model_path):
    """
    LambdaRank model inference interface.
    """
    output = lambda_rank(input_dim, is_infer=True)
    parameters = paddle.parameters.Parameters.from_tar(gzip.open(model_path))

    infer_query_id = None
    infer_data = []
    infer_data_num = 1

    fill_default_test = functools.partial(paddle.dataset.mq2007.test,
                                          format="listwise")
    for label, querylist in fill_default_test():
        infer_data.append([querylist])
        if len(infer_data) == infer_data_num:
            break

    # Predict score of infer_data document.
    # Re-sort the document base on predict score.
    # In descending order. then we build the ranking documents.
    predicitons = paddle.infer(output_layer=output,
                               parameters=parameters,
                               input=infer_data)
    for i, score in enumerate(predicitons):
        print i, score
Esempio n. 3
0
def model_test(X_test, Y_test, userid):
    #print(model_train.get_opt())
    # 读取训练好的模型最优参数
    path = "model_parameter" + str(userid) + "/_.tar"
    with open(path) as f:
        parameters = paddle.parameters.Parameters.from_tar(f)

    out = model_building.get_net(1)

    #X_test=X_val[:2].tolist()
    num1 = 0
    num2 = 0
    for i in range(X_test.shape[0]):
        test_batch = []
        #print(i.shape)
        test_batch.append([list(X_test[i])])

        test_batch = numpy.array(test_batch)
        #print(test_batch.shape)
        probs = paddle.infer(output_layer=out,
                             parameters=parameters,
                             input=[test_batch],
                             feeding=feeding)
        print(probs)
        labs = numpy.argsort(-probs)
        print("predict label is", labs[0][0], "real label is", Y_test[i])
        if labs[0][0] != Y_test[i]:
            num1 += 1

    print "Model accuracy:", (1 - 1.0 * num1 / X_test.shape[0])
Esempio n. 4
0
def predict(model_file, out_file, out_layer):

    with open(model_file, 'r') as mf:
        params = paddle.parameters.Parameters.from_tar(mf)

        pd_data = pd.read_csv("./data/test.csv")
        test_data = []
        for line in np.array(pd_data, dtype=np.float32):
            test_data.append(
                (np.array(line, dtype=np.float32) / 255.0 * 2.0 - 1.0, ))
        # print(test_data[0:5])
        #input = test_data.tolist()
        feeding = {'pixel': 0}
        # test_data = []
        # test_reader = creat_reader("./data/test.csv", True)
        # for line in test_reader():
        #     test_data.append()
        # # print(test_data[0:2])
        #
        # print(test_data[0:3])
        probs = paddle.infer(output_layer=out_layer,
                             parameters=params,
                             input=test_data,
                             feeding=feeding)
        lab = np.argsort(
            -probs)  # probs and lab are the results of one batch data
        print("Label is:")
        print(lab[:, 0])
        pred_y = lab[:, 0]
        result = pd.DataFrame({
            'ImageId': range(1,
                             len(pred_y) + 1),
            'Label': pred_y.astype(np.int32)
        })
        result.to_csv(out_file, index=False)
Esempio n. 5
0
def generate(gen_data, dict_size, model_path, beam_size):
    beam_gen = seqToseq_net(dict_size,
                            dict_size,
                            beam_size,
                            is_generating=True)

    with gzip.open(model_path, 'r') as f:
        parameters = paddle.parameters.Parameters.from_tar(f)

    # prob is the prediction probabilities, and id is the prediction word.
    beam_result = paddle.infer(output_layer=beam_gen,
                               parameters=parameters,
                               input=gen_data,
                               field=['prob', 'id'])

    # get the dictionary
    src_dict, trg_dict = paddle.dataset.wmt14.get_dict(dict_size)

    # the delimited element of generated sequences is -1,
    # the first element of each generated sequence is the sequence length
    seq_list = []
    seq = []
    for w in beam_result[1]:
        if w != -1:
            seq.append(w)
        else:
            seq_list.append(' '.join([trg_dict.get(w) for w in seq[1:]]))
            seq = []

    prob = beam_result[0]
    for i in xrange(gen_num):
        print "\n*******************************************************\n"
        print "src:", ' '.join([src_dict.get(w) for w in gen_data[i][0]]), "\n"
        for j in xrange(beam_size):
            print "prob = %f:" % (prob[i][j]), seq_list[i * beam_size + j]
Esempio n. 6
0
def ner_net_infer(data_reader=test_data_reader, model_file='ner_model.tar.gz'):
    test_data = []
    test_sentences = []
    for item in data_reader():
        test_data.append([item[0], item[1]])
        test_sentences.append(item[-1])
        if len(test_data) == 10:
            break

    predict = ner_net(is_train=False)

    lab_ids = paddle.infer(output_layer=predict,
                           parameters=paddle.parameters.Parameters.from_tar(
                               gzip.open(model_file)),
                           input=test_data,
                           field='id')

    flat_data = [
        word for word in itertools.chain.from_iterable(test_sentences)
    ]

    labels_reverse = {}
    for (k, v) in label_dict.items():
        labels_reverse[v] = k
    pre_lab = [labels_reverse[lab_id] for lab_id in lab_ids]

    for word, label in zip(flat_data, pre_lab):
        print word, label
Esempio n. 7
0
def main(user_id, shop_id):
    if user_id not in model_member or shop_id not in model_shop:
        raise ValueError("The input data is invalid.")

    paddle.init(use_gpu=with_gpu)
    predict = dnn_network()

    with open('../models/dnn/parameters.tar', 'r') as f:
        parameters = paddle.parameters.Parameters.from_tar(f)

    user = model_member[user_id]
    shop = model_shop[shop_id]

    infer_dict = {
        'user_id': 0,
        'shop_id': 1
    }

    prediction = paddle.infer(
        output_layer=predict,
        parameters=parameters,
        input=[[user, shop]],
        feeding=infer_dict)

    print prediction
def main():
    """
    定义网络结构、读取模型参数并预测结果
    Args:
    Return:
    """
    paddle.init(use_gpu=WITH_GPU)

    # 定义神经网络结构
    predict = network_config()

    # 读取模型参数
    if not os.path.exists('params_pass_9.tar'):
        print "Params file doesn't exists."
        return
    with open('params_pass_9.tar', 'r') as param_f:
        parameters = paddle.parameters.Parameters.from_tar(param_f)

    # 读取并预处理要预测的图片
    test_data = []
    cur_dir = os.path.dirname(os.path.realpath(__file__))
    test_data.append((load_image(cur_dir + '/image/infer_3.png'),))

    # 利用训练好的分类模型,对输入的图片类别进行预测
    probs = paddle.infer(
        output_layer=predict, parameters=parameters, input=test_data)
    lab = np.argsort(-probs)
    print "Label of image/infer_3.png is: %d" % lab[0][0]
Esempio n. 9
0
def ranknet_infer(input_dim, model_path):
    """
    RankNet model inference interface.
    """
    # we just need half_ranknet to predict a rank score,
    # which can be used in sort documents
    output = half_ranknet("right", input_dim)
    parameters = paddle.parameters.Parameters.from_tar(gzip.open(model_path))

    # load data of same query and relevance documents,
    # need ranknet to rank these candidates
    infer_query_id = []
    infer_data = []
    infer_doc_index = []

    # convert to mq2007 built-in data format
    # <query_id> <relevance_score> <feature_vector>
    plain_txt_test = functools.partial(paddle.dataset.mq2007.test,
                                       format="plain_txt")

    for query_id, relevance_score, feature_vector in plain_txt_test():
        infer_query_id.append(query_id)
        infer_data.append([feature_vector])

    # predict score of infer_data document.
    # Re-sort the document base on predict score
    # in descending order. then we build the ranking documents
    scores = paddle.infer(output_layer=output,
                          parameters=parameters,
                          input=infer_data)
    for query_id, score in zip(infer_query_id, scores):
        print "query_id : ", query_id, " score : ", score
Esempio n. 10
0
    def to_prediction(self, image_path, parameters, out):
        # 获取图片
        def load_image(file):
            im = Image.open(file)
            im = im.resize((32, 32), Image.ANTIALIAS)
            im = np.array(im).astype(np.float32)
            # PIL打开图片存储顺序为H(高度),W(宽度),C(通道)。
            # PaddlePaddle要求数据顺序为CHW,所以需要转换顺序。
            im = im.transpose((2, 0, 1))
            # CIFAR训练图片通道顺序为B(蓝),G(绿),R(红),
            # 而PIL打开图片默认通道顺序为RGB,因为需要交换通道。
            im = im[(2, 1, 0), :, :]  # BGR
            im = im.flatten()
            im = im / 255.0
            return im

        # 获得要预测的图片
        test_data = []
        test_data.append((load_image(image_path), ))

        # 获得预测结果
        probs = paddle.infer(output_layer=out,
                             parameters=parameters,
                             input=test_data)
        # 处理预测结果
        lab = np.argsort(-probs)
        # 返回概率最大的值和其对应的概率值
        return lab[0][0], probs[0][(lab[0][0])]
Esempio n. 11
0
def ranknet_infer(pass_id):
    """
  load the trained model. And predict with plain txt input
  """
    print "Begin to Infer..."
    feature_dim = 46

    # we just need half_ranknet to predict a rank score, which can be used in sort documents
    output = half_ranknet("infer", feature_dim)
    parameters = paddle.parameters.Parameters.from_tar(
        gzip.open("ranknet_params_%d.tar.gz" % (pass_id)))

    # load data of same query and relevance documents, need ranknet to rank these candidates
    infer_query_id = []
    infer_data = []
    infer_doc_index = []

    # convert to mq2007 built-in data format
    # <query_id> <relevance_score> <feature_vector>
    plain_txt_test = functools.partial(
        paddle.dataset.mq2007.test, format="plain_txt")

    for query_id, relevance_score, feature_vector in plain_txt_test():
        infer_query_id.append(query_id)
        infer_data.append([feature_vector])

    # predict score of infer_data document. Re-sort the document base on predict score
    # in descending order. then we build the ranking documents
    scores = paddle.infer(
        output_layer=output, parameters=parameters, input=infer_data)
    print scores
    for query_id, score in zip(infer_query_id, scores):
        print "query_id : ", query_id, " ranknet rank document order : ", score
Esempio n. 12
0
def test_inverse(argv):
    depth_np = np.array([-3, 2, 2, 2, 1, 2, 4, 4, 4, 2, 2, 4, 4, 4, 2, 2],
                        dtype=np.float32)

    # depth_np = 4 * np.ones((4, 4), dtype=np.float32)
    depth_np = depth_np.flatten()

    height = 4
    width = 4
    depth = pd.layer.data(name="depth",
                          type=pd.data_type.dense_vector(height * width),
                          height=height,
                          width=width)

    inv_depth = pd.layer.mixed(
        input=[pd.layer.identity_projection(input=depth)],
        act=pd.activation.Inv())

    parameters, topo = pd.parameters.create(inv_depth)
    inv_depth_np = pd.infer(output=inv_depth,
                            parameters=parameters,
                            input=[(depth_np, )],
                            feeding={'depth': 0})

    print inv_depth_np
Esempio n. 13
0
def test_gradient_weight(argv):
    weight_np = np.array([1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
                         dtype=np.float32)
    scales = [1]
    height = 4
    width = 4

    weight = pd.layer.data(name="weight",
                           type=pd.data_type.dense_vector(height * width),
                           height=height,
                           width=width)

    weight_diff = pd.layer.gradient_diff(input=weight, scales=scales)
    weight_diff = util_layers.reduce(input=weight_diff,
                                     shape=[len(scales) * 2, height, width],
                                     op='sum')
    weight_diff = util_layers.math_op(input=weight_diff,
                                      act=pd.activation.IsZero())
    weight_diff = util_layers.math_op(input=[weight_diff, weight], op='dot')
    parameters, topo = pd.parameters.create(weight_diff)

    weight_diff_np = pd.infer(output=topo,
                              parameters=parameters,
                              input=[(weight_np, )],
                              feeding={'weight': 0})

    print weight_diff_np
Esempio n. 14
0
def worker():
    # 初始化PaddlePaddle
    paddle.init(use_gpu=False, trainer_count=2)

    # 加载模型参数和预测的拓扑生成一个预测器
    with open('../models/param.tar', 'r') as param_f:
        params = paddle.parameters.Parameters.from_tar(param_f)

    # 获取分类器
    out = convolutional_neural_network()

    while True:
        # 获取数据和子队列
        data, recv_queue = sendQ.get()
        try:
            # 获取预测结果
            result = paddle.infer(output_layer=out,
                                  parameters=params,
                                  input=data)

            # 处理预测结果
            lab = np.argsort(-result)
            print lab
            # 返回概率最大的值和其对应的概率值
            result = '{"result":%d,"possibility":%f}' % (lab[0][0], result[0][
                (lab[0][0])])
            print result
            recv_queue.put((True, result))
        except:
            # 通过子队列发送异常信息
            trace = traceback.format_exc()
            print trace
            recv_queue.put((False, trace))
            continue
Esempio n. 15
0
def test():

    input_seq = [1229,199,134,1349,5,31,7,13,20,2496,1143]

    paddle.init(use_gpu=False, trainer_count=1)
    output = seq2seq_attention(
        source_dict_dim=dict_dim,
        target_dict_dim=dict_dim,
        encoder_size=512,
        decoder_size=512,
        embedding_size=512,
        is_train=False
    )

    with gzip.open('output/params_pass_1.tar.gz', 'r') as f:
        parameters = paddle.parameters.Parameters.from_tar(f)

    result = paddle.infer(
        output_layer=output,
        input=[(input_seq,)],
        parameters=parameters,
        field=['prob', 'id']
    )

    print(result)
Esempio n. 16
0
def infer(user_id, movie_id, inference, parameters, feeding):
    """
    预测指定用户对指定电影的喜好得分值
    Args:
        user_id -- 用户编号值
        movie_id -- 电影编号值
        inference -- 相似度
        parameters -- 模型参数
        feeding -- 数据映射,python字典
    Return:
    """

    # 根据已定义的用户、电影编号值从movielens数据集中读取数据信息
    user = paddle.dataset.movielens.user_info()[user_id]
    movie = paddle.dataset.movielens.movie_info()[movie_id]

    # 存储用户特征和电影特征
    feature = user.value() + movie.value()

    # 复制feeding值,并删除序列中的得分项
    infer_dict = copy.copy(feeding)
    del infer_dict['score']

    # 预测指定用户对指定电影的喜好得分值
    prediction = paddle.infer(
        output_layer=inference,
        parameters=parameters,
        input=[feature],
        feeding=infer_dict)
    score = (prediction[0][0] + 5.0) / 2
    print "[Predict] User %d Rating Movie %d With Score %.2f" % (
        user_id, movie_id, score)
Esempio n. 17
0
def lambda_rank_infer(pass_id):
    """
  lambda_rank model inference interface
  parameters:
    pass_id : inference model in pass_id
  """
    print "Begin to Infer..."
    input_dim = 46
    output = lambda_rank(input_dim)
    parameters = paddle.parameters.Parameters.from_tar(
        gzip.open("lambda_rank_params_%d.tar.gz" % (pass_id - 1)))

    infer_query_id = None
    infer_data = []
    infer_data_num = 1
    fill_default_test = functools.partial(paddle.dataset.mq2007.test,
                                          format="listwise")
    for label, querylist in fill_default_test():
        infer_data.append(querylist)
        if len(infer_data) == infer_data_num:
            break

    # predict score of infer_data document. Re-sort the document base on predict score
    # in descending order. then we build the ranking documents
    predicitons = paddle.infer(output_layer=output,
                               parameters=parameters,
                               input=infer_data)
    for i, score in enumerate(predicitons):
        print i, score
def endover(imageNames):
    test_data = []
    cur_dir = os.getcwd()
    imageName = "media/" + imageNames
    passName = "/usr/pass/2/params_pass_199.tar"
    labbesl = np.array([
        "0:土豆丝", "1:清蒸鱼", "2:松仁玉米", "3:西红柿炒鸡蛋", "4:炒西兰花", "5:虾仁", "6:包子",
        "7:皮蛋", "8:炒花生米", "9:韭菜鸡蛋"
    ])
    test_data.append((load_image(imageName), ))

    with open(passName, 'r') as f:
        parameters = paddle.parameters.Parameters.from_tar(f)

    probs = paddle.infer(output_layer=out,
                         parameters=parameters,
                         input=test_data)
    lab = np.argsort(-probs)  # probs and lab are the results of one batch data
    # print probs
    print lab
    labelName = []
    labelNum = []
    for i in range(5):
        labelName.append(labbesl[lab[0][i]])
        labelNum.append(lab[0][i])
    value = {}
    value['date'] = labelNum
    value['name'] = labelName
    print labelName
    return value
Esempio n. 19
0
def main():

    # PaddlePaddle init
    paddle.init(use_gpu=True, 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)

    # load parameters
    with open("params_pass_167.tar", 'r') as f:
        parameters = paddle.parameters.Parameters.from_tar(f)

    for sample in create_dataset.test_reader()():
        data = sample[0]
        label = sample[1]
        probs = paddle.infer(output_layer=out,
                             parameters=parameters,
                             input=[(data, )])
        prob = probs[0].tolist()
        infer_label = prob.index(max(prob))
        print(str(label) + " " + str(infer_label))
Esempio n. 20
0
def main():
    paddle.init()
    usr_combined_features = get_usr_combined_features()
    mov_combined_features = get_mov_combined_features()
    inference = paddle.layer.cos_sim(a=usr_combined_features,
                                     b=mov_combined_features,
                                     size=1,
                                     scale=5)
    cost = paddle.layer.square_error_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(
        recordio("/pfs/dlnel/public/dataset/movielens/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()

    infer_dict = copy.copy(feeding)
    del infer_dict['score']

    prediction = paddle.infer(output_layer=inference,
                              parameters=parameters,
                              input=[feature],
                              feeding=infer_dict)
    print(prediction + 5) / 2
Esempio n. 21
0
def test_gradient_dff(argv):
    depth_np = np.array([1, 2, 2, 2, 1, 2, 4, 4, 4, 2, 2, 4, 4, 4, 2, 2],
                        dtype=np.float32)

    height = 48
    width = 64

    depth_np = depth_np.reshape((4, 4))
    depth_np = cv2.resize(depth_np, (width, height))
    depth_np = depth_np.flatten()

    depth = pd.layer.data(name="depth",
                          type=pd.data_type.dense_vector(height * width),
                          height=height,
                          width=width)

    gradient_diff = pd.layer.gradient_diff(input=depth, scales=[1, 2])

    parameters, topo = pd.parameters.create(gradient_diff)
    gradient_diff_np = pd.infer(output=topo,
                                parameters=parameters,
                                input=[(depth_np, ), (depth_np, )],
                                feeding={'depth': 0})

    print gradient_diff_np
Esempio n. 22
0
 def to_prediction(self, out, parameters, test_data):
     probs = paddle.infer(output_layer=out,
                          parameters=parameters,
                          input=test_data)
     #print probs
     lab = np.argsort(-probs)
     label = lab[0][0]
     return label
Esempio n. 23
0
    def predicter(self, out_layer, parameter, input_data):

        predict = paddle.infer(output_layer=out_layer,
                               parameters=parameter,
                               input=input_data)

        lab = np.argsort(-predict)
        print 'The Label img is: %d' % lab[0][0]
Esempio n. 24
0
  def demon_geometry(self, image1, image2):
      #transform and yield
      image1_new = uts.transform(image1.copy(),
                                 height=self.params['size'][0],
                                 width=self.params['size'][1])
      image2_new = uts.transform(image2.copy(),
                                 height=self.params['size'][0],
                                 width=self.params['size'][1])
      self.intrinsic = self.params['intrinsic']

      # down sample
      test_data_bs = [(image1_new, image2_new)]
      flow, depth_inv, normal, rotation, translation = paddle.infer(
                              output=self.topo_bs,
                              parameters=self.parameters_bs,
                              input=test_data_bs,
                              feeding=self.feeding_bs);

      for i in range(3):
        test_data_it = [(image1_new, image2_new, self.intrinsic,
                         rotation, translation, depth_inv, normal)]
        flow, depth_inv, normal, rotation, translation = paddle.infer(
                                output=self.topo_it,
                                parameters=self.parameters_it,
                                input=test_data_it,
                                feeding=self.feeding_it);

      test_data_re = [(image1_new, image2_new, depth_inv)]
      depth = paddle.infer(output=self.topo_re,
                           parameters=self.parameters_re,
                           input=test_data_re,
                           feeding=self.feeding_re);

      layer_names = [self.outputs_it['flow'].name,
                     self.outputs_it['normal'].name,
                     self.outputs_re['depth_0'].name]

      height_list = [self.my_g_layer_map[x].height for x in layer_names]
      width_list = [self.my_g_layer_map[x].width for x in layer_names]

      flow, normal, depth = uts.vec2img(inputs=[flow, normal, depth],
                      height=height_list,
                      width=width_list)

      motion = np.concatenate([rotation, translation])
      return flow, normal, depth, motion
Esempio n. 25
0
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(12288))
    y_predict = paddle.layer.fc(input = x, size =1,act = paddle.activation.Sigmoid())
    y_label = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1))

    cost = paddle.layer.multi_binary_label_cross_entropy_cost(input=y_predict, label=y_label)
    parameters = paddle.parameters.create(cost)
    optimizer = paddle.optimizer.Momentum(momentum=0, learning_rate=0.005)

    trainer = paddle.trainer.SGD(
        cost=cost, parameters=parameters, update_equation=optimizer)

     # mapping data
    feeding = {'x': 0, 'y': 1}
    

    # event_handler to print training and testing info
    def event_handler(event):
        if isinstance(event, paddle.event.EndIteration):
            print "Pass %d, Batch %d, Cost %f" % (
                event.pass_id, event.batch_id, event.cost)


    # training
    reader = paddle.reader.shuffle(train(), buf_size=50000)
    batch_reader = paddle.batch(reader,batch_size=256)
    trainer.train(
        batch_reader,
        feeding=feeding,
        event_handler=event_handler,
        num_passes=2000)

    # infer test
    test_data_creator = train()
    test_data = []
    test_label = []

    for item in test_data_creator():
        test_data.append((item[0],))
        test_label.append(item[1])

    probs = paddle.infer(
        output_layer=y_predict, parameters=parameters, input=test_data)

    right_number =0
    total_number = len(test_data)
    for i in xrange(len(probs)):
        #print "label=" + str(test_label[i]) + ", predict=" + str(probs[i])
        if float(probs[i][0]) >= 0.5 and test_label[i] ==1 :
            right_number += 1
        elif float(probs[i][0]) < 0.5 and test_label[i] ==0:
            right_number += 1

    print("right_number is {0} in {1} samples".format(right_number,total_number))
Esempio n. 26
0
    def _infer(data_type, window_len, proposal_data, window_stride):
        print window_len
        json_data = load_json("/mnt/BROAD-datasets/video/meta.json")
        database = json_data['database']
        window_lens = [window_len]
        model_path = "/home/kesci/work/lstm_av_" + str(window_len) + ".tar.gz"
        # load the trained models
        if os.path.exists(model_path):
            with gzip.open(model_path, 'r') as f:
                parameters = paddle.parameters.Parameters.from_tar(f)
        index = 0
        for video in database.keys():
            dataSet = database[video]["subset"]
            if dataSet != data_type:
                continue
            try:
                with open(
                        "/mnt/BROAD-datasets/video/" + dataSet +
                        "/image_resnet50_feature/" + str(video) + ".pkl",
                        'rb') as f:
                    image_fea = np.array(cPickle.load(f))
                with open(
                        "/mnt/BROAD-datasets/video/" + dataSet +
                        "/audio_feature/" + str(video) + ".pkl", 'rb') as f:
                    audio_fea = np.array(cPickle.load(f))

            except:
                continue
            print index, video
            index += 1

            audio_fea = audio_fea[:np.shape(image_fea)[0]]
            image_fea = image_fea[:np.shape(audio_fea)[0]]
            video_fea = np.append(image_fea, audio_fea, axis=1)
            video_len = np.shape(video_fea)[0]
            this_vid_proposals = []
            inputs = []
            for pos in range(0, video_len - window_lens[0], window_stride):
                inputs.append([video_fea[pos:pos + window_lens[0]]])
            probs = paddle.infer(output_layer=prob_layer,
                                 parameters=parameters,
                                 input=inputs,
                                 field="value")
            for stride_index, prob in enumerate(probs):
                pos = stride_index * window_stride
                score = int(prob[1] * 100) / 100.0
                if score == 0.0:
                    continue
                proposal = {
                    'score': int(prob[1] * 100) / 100.0,
                    'segment': [pos, pos + window_lens[0]],
                }
                this_vid_proposals += [proposal]
            if not proposal_data['results'].has_key(video):
                proposal_data['results'][video] = this_vid_proposals
            else:
                proposal_data['results'][video] += this_vid_proposals
Esempio n. 27
0
def main():
    datadim = 3 * 320 * 240
    classdim = 4

    # 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)
    net = multilayer_perceptron(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.Parameters.from_tar(gzip.open('params_pass_99.tar'))

    with open('params_pass_199.tar', 'r') as f:
        parameters = paddle.parameters.Parameters.from_tar(f)

    for i in range(0, 10000):
        i = str(i)
        file = '../data/test/B1/' + i + '.png'
        try:
            im = Image.open(file)
        except:
            continue
        im = np.array(im).astype(np.float32)
        im = im.transpose((2, 0, 1))  # CHW
        im = im.flatten()
        im = im / 255.0
        if im.shape[0] != 230400:
            continue

        test_data = []
        test_data.append((im, ))

        probs = paddle.infer(output_layer=out,
                             parameters=parameters,
                             input=test_data)
        lab = np.argsort(
            -probs)  # probs and lab are the results of one batch data
        print "Label of image/%s.png is: %d" % (i, lab[0][0])
        #print lab[0]
        #print probs[0]
        print probs[0][lab[0][0]]
Esempio n. 28
0
    def to_prediction(self, out, parameters, test_data):

        # 开始预测
        probs = paddle.infer(output_layer=out,
                             parameters=parameters,
                             input=test_data)
        # 处理预测结果并打印
        lab = np.argsort(-probs)
        print "预测结果为: %d" % lab[0][0]
Esempio n. 29
0
def main():
    # parse the argument
    parser = argparse.ArgumentParser()
    parser.add_argument(
        'data_list',
        help='The path of data list file, which consists of one image path per line'
    )
    parser.add_argument(
        'model',
        help='The model for image classification',
        choices=[
            'alexnet', 'vgg13', 'vgg16', 'vgg19', 'resnet', 'googlenet',
            'inception-resnet-v2', 'inception_v4', 'xception'
        ])
    parser.add_argument(
        'params_path', help='The file which stores the parameters')
    args = parser.parse_args()

    # PaddlePaddle init
    paddle.init(use_gpu=True, trainer_count=1)

    image = paddle.layer.data(
        name="image", type=paddle.data_type.dense_vector(DATA_DIM))

    if args.model == 'alexnet':
        out = alexnet.alexnet(image, class_dim=CLASS_DIM)
    elif args.model == 'vgg13':
        out = vgg.vgg13(image, class_dim=CLASS_DIM)
    elif args.model == 'vgg16':
        out = vgg.vgg16(image, class_dim=CLASS_DIM)
    elif args.model == 'vgg19':
        out = vgg.vgg19(image, class_dim=CLASS_DIM)
    elif args.model == 'resnet':
        out = resnet.resnet_imagenet(image, class_dim=CLASS_DIM)
    elif args.model == 'googlenet':
        out, _, _ = googlenet.googlenet(image, class_dim=CLASS_DIM)
    elif args.model == 'inception-resnet-v2':
        assert DATA_DIM == 3 * 331 * 331 or DATA_DIM == 3 * 299 * 299
        out = inception_resnet_v2.inception_resnet_v2(
            image, class_dim=CLASS_DIM, dropout_rate=0.5, data_dim=DATA_DIM)
    elif args.model == 'inception_v4':
        out = inception_v4.inception_v4(image, class_dim=CLASS_DIM)
    elif args.model == 'xception':
        out = xception.xception(image, class_dim=CLASS_DIM)

    # load parameters
    with gzip.open(args.params_path, 'r') as f:
        parameters = paddle.parameters.Parameters.from_tar(f)

    file_list = [line.strip() for line in open(args.data_list)]
    test_data = [(paddle.image.load_and_transform(image_file, 256, 224, False)
                  .flatten().astype('float32'), ) for image_file in file_list]
    probs = paddle.infer(
        output_layer=out, parameters=parameters, input=test_data)
    lab = np.argsort(-probs)
    for file_name, result in zip(file_list, lab):
        print "Label of %s is: %d" % (file_name, result[0])
Esempio n. 30
0
def main():
    parameters_in_train , cost , inference , feeding = init.init()
    with open('model/params_pass_0.tar', 'r') as f:
        parameters = paddle.parameters.Parameters.from_tar(f)
    infer_dict = copy.copy(feeding)
    del infer_dict['isclick']
    itera = wd.train()()
    for feature in itera:
        prediction = paddle.infer(inference, parameters=parameters, input=[feature], feeding=infer_dict)
        print 'predict = '+ str((prediction[0][0]+1)/2) + ' isclick = '+ str(feature[2])
        im = np.array(camera_capture).astype(np.float32)

        im = im.transpose((2, 0, 1))  # CHW

        im = im.flatten()
        im = im / 255.0


        test_data = []
        test_data.append((im,))

        with open('params_pass_199.tar', 'r') as f:
            parameters = paddle.parameters.Parameters.from_tar(f)

        probs = paddle.infer(
            output_layer=out, parameters=parameters, input=test_data)
        lab = np.argsort(-probs)  # probs and lab are the results of one batch data
        #print "Label of image/dog.png is: %d" % lab[0][0]



        ser.write(str(lab[0][0]))
        print(str(lab[0][0]))
        #if words=="1":
        #    words="2"
        #elif words=="2":
        #    words="3"
        #elif words=="3":
        #    words="4"
        #elif words=="4":
        #    words="5"
Esempio n. 32
0
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
def main():
    datadim = 3 * 32 * 32
    classdim = 10

    # PaddlePaddle init
    paddle.init(use_gpu=with_gpu, trainer_count=7)

    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 = simple_cnn(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')

    # 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):
            # save parameters
            with open('params_pass_%d.tar' % event.pass_id, 'w') as f:
                parameters.to_tar(f)

            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)

    # Save the inference topology to protobuf.
    inference_topology = paddle.topology.Topology(layers=out)
    with open("inference_topology.pkl", 'wb') as f:
        inference_topology.serialize_for_inference(f)

    trainer.train(
        reader=paddle.batch(
            paddle.reader.shuffle(
                paddle.dataset.cifar.train10(), buf_size=50000),
            batch_size=128),
        num_passes=200,
        event_handler=event_handler,
        feeding={'image': 0,
                 'label': 1})

    # inference
    from PIL import Image
    import numpy as np
    import os

    def load_image(file):
        im = Image.open(file)
        im = im.resize((32, 32), Image.ANTIALIAS)
        im = np.array(im).astype(np.float32)
        # The storage order of the loaded image is W(widht),
        # H(height), C(channel). PaddlePaddle requires
        # the CHW order, so transpose them.
        im = im.transpose((2, 0, 1))  # CHW
        # In the training phase, the channel order of CIFAR
        # image is B(Blue), G(green), R(Red). But PIL open
        # image in RGB mode. It must swap the channel order.
        im = im[(2, 1, 0), :, :]  # BGR
        im = im.flatten()
        im = im / 255.0
        return im

    test_data = []
    cur_dir = os.path.dirname(os.path.realpath(__file__))
    test_data.append((load_image(cur_dir + '/image/dog.png'), ))

    # users can remove the comments and change the model name
    # with open('params_pass_50.tar', 'r') as f:
    #    parameters = paddle.parameters.Parameters.from_tar(f)

    probs = paddle.infer(
        output_layer=out, parameters=parameters, input=test_data)
    lab = np.argsort(-probs)  # probs and lab are the results of one batch data
    print "Label of image/dog.png is: %d" % lab[0][0]
Esempio n. 34
0
# 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
import numpy as np

paddle.init(use_gpu=False)
x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(2))
y_predict = paddle.layer.fc(input=x, size=1, act=paddle.activation.Linear())

# loading the model which generated by training
with open('params_pass_90.tar', 'r') as f:
    parameters = paddle.parameters.Parameters.from_tar(f)

# Input multiple sets of data,Output the infer result in a array.
i = [[[1, 2]], [[3, 4]], [[5, 6]]]
print paddle.infer(output_layer=y_predict, parameters=parameters, input=i)
# Will print:
# [[ -3.24491572]
#  [ -6.94668722]
#  [-10.64845848]]
Esempio n. 35
0
def main():
    paddle.init(use_gpu=False, trainer_count=1)
    is_generating = False

    # source and target dict dim.
    dict_size = 30000
    source_dict_dim = target_dict_dim = dict_size

    # train the network
    if not is_generating:
        cost = seqToseq_net(source_dict_dim, target_dict_dim)
        parameters = paddle.parameters.create(cost)

        # define optimize method and trainer
        optimizer = paddle.optimizer.Adam(
            learning_rate=5e-5,
            regularization=paddle.optimizer.L2Regularization(rate=8e-4))
        trainer = paddle.trainer.SGD(cost=cost,
                                     parameters=parameters,
                                     update_equation=optimizer)
        # define data reader
        wmt14_reader = paddle.batch(
            paddle.reader.shuffle(
                paddle.dataset.wmt14.train(dict_size), buf_size=8192),
            batch_size=5)

        # define event_handler callback
        def event_handler(event):
            if isinstance(event, paddle.event.EndIteration):
                if event.batch_id % 10 == 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()

        # start to train
        trainer.train(
            reader=wmt14_reader, event_handler=event_handler, num_passes=2)

    # generate a english sequence to french
    else:
        # use the first 3 samples for generation
        gen_creator = paddle.dataset.wmt14.gen(dict_size)
        gen_data = []
        gen_num = 3
        for item in gen_creator():
            gen_data.append((item[0], ))
            if len(gen_data) == gen_num:
                break

        beam_gen = seqToseq_net(source_dict_dim, target_dict_dim, is_generating)
        # get the pretrained model, whose bleu = 26.92
        parameters = paddle.dataset.wmt14.model()
        # prob is the prediction probabilities, and id is the prediction word. 
        beam_result = paddle.infer(
            output_layer=beam_gen,
            parameters=parameters,
            input=gen_data,
            field=['prob', 'id'])

        # get the dictionary
        src_dict, trg_dict = paddle.dataset.wmt14.get_dict(dict_size)

        # the delimited element of generated sequences is -1,
        # the first element of each generated sequence is the sequence length
        seq_list = []
        seq = []
        for w in beam_result[1]:
            if w != -1:
                seq.append(w)
            else:
                seq_list.append(' '.join([trg_dict.get(w) for w in seq[1:]]))
                seq = []

        prob = beam_result[0]
        beam_size = 3
        for i in xrange(gen_num):
            print "\n*******************************************************\n"
            print "src:", ' '.join(
                [src_dict.get(w) for w in gen_data[i][0]]), "\n"
            for j in xrange(beam_size):
                print "prob = %f:" % (prob[i][j]), seq_list[i * beam_size + j]
Esempio n. 36
0
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)
    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,
                                 is_local=False,
                                 pserver_spec="localhost:3000")

    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)

        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
Esempio n. 37
0
def main():
    # 初始化定义跑模型的设备
    paddle.init(use_gpu=with_gpu, trainer_count=1)

    # 读取数据
    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))

    # 调用之前定义的网络结构
    predict = convolutional_neural_network_org(images)#原网络结构    
    # predict = convolutional_neural_network(images)

    # 定义损失函数
    cost = paddle.layer.classification_cost(input=predict, label=label)

    # 指定训练相关的参数
    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 = []

    # 定义event_handler,输出训练过程中的结果
    def event_handler(event):
        if isinstance(event, paddle.event.EndIteration):
            if event.batch_id % 100 == 0:
                print "Pass %d, Batch %d, Cost %f, %s" % (
                    event.pass_id, event.batch_id, event.cost, event.metrics)
        if isinstance(event, paddle.event.EndPass):
            # 保存参数
            with open('params_pass_%d.tar' % event.pass_id, 'w') as f:
                parameters.to_tar(f)

            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=64),
        event_handler=event_handler,
        num_passes=50)

    # 找到训练误差最小的一次结果
    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)

    # 加载数据   
    def load_image(file):
        im = Image.open(file).convert('L')
        im = im.resize((28, 28), Image.ANTIALIAS)
        im = np.array(im).astype(np.float32).flatten()
        im = im / 255.0
        return im

    # 测试结果
    test_data = []
    cur_dir = os.path.dirname(os.path.realpath(__file__))
    test_data.append((load_image(cur_dir + '/image/infer_3.png'), ))

    probs = paddle.infer(
        output_layer=predict, parameters=parameters, input=test_data)
    lab = np.argsort(-probs)  # probs and lab are the results of one batch data
    print "Label of image/infer_3.png is: %d" % lab[0][0]