PaddlePaddle入门06:猫狗分类

#网络结构
def convolutional_neural_network(img):
    #卷积池化层1
    conv_pool_1 = fluid.nets.simple_img_conv_pool(
        input=img,
        filter_size=5,
        num_filters=20,
        pool_size=2,
        pool_stride=2,
        act='relu')
    conv_pool_1 = fluid.layers.batch_norm(conv_pool_1)
    #卷积池化层2
    conv_pool_2 = fluid.nets.simple_img_conv_pool(
        input=conv_pool_1,
        filter_size=5,
        num_filters=50,
        pool_size=2,
        pool_stride=2,
        act='relu')
    conv_pool_2 = fluid.layers.batch_norm(conv_pool_2)
    #卷积池化层3
    conv_pool_3 = fluid.nets.simple_img_conv_pool(
        input=conv_pool_2,
        filter_size=5,
        num_filters=50,
        pool_size=2,
        pool_stride=2,
        act='relu')
    conv_pool_2 = fluid.layers.batch_norm(conv_pool_3)
    #以softmax为激活函数的全连接输出层,10类数据输出10个数字
    prediction = fluid.layers.fc(input=conv_pool_3, size=10, act='softmax')
    return prediction
预测

图片预处理实现

def load_image(file):
    #打开图片
    im = Image.open(file)
    #将图片调整为跟训练数据一样的大小32*32,设定ANTIALIAS,即抗锯齿.resize是缩放        
    im = im.resize((32, 32), Image.ANTIALIAS)
    #建立图片矩阵 类型为float32
    im = np.array(im).astype(np.float32) #此时图片维度为(32, 32, 3)需要转换为符合image的维度
    #矩阵转置 
    im = im.transpose((2, 0, 1))                               
    #将像素值从【0-255】转换为【0-1】
    im = im / 255.0
    #print(im)       
    im = np.expand_dims(im, axis=0)
    # 保持和之前输入image维度一致
    print('im_shape的维度:',im.shape)
    return im
  • expand_dims(X, axis=0)在向量axis维度上加1
  • np.transpose数组转置
X = np.array([[[1,2],[3,4]]]) 
X = X.transpose((2,0,1)) #3(0,1,0) -> (0,0,1) 4(0,1,1)-> (1, 0, 1)
X

输出:array([[[1, 3]],
[[2, 4]]]
具体到每个元素的索引转换

with fluid.scope_guard(inference_scope):
    #从指定目录中加载 推理model(inference model)
    [inference_program, # 预测用的program
     feed_target_names, # 是一个str列表,它包含需要在推理 Program 中提供数据的变量的名称。 
     fetch_targets] = fluid.io.load_inference_model(model_save_dir,#fetch_targets:是一个 Variable 列表,从中我们可以得到推断结果。
                                                    infer_exe)     #infer_exe: 运行 inference model的 executor
    
    infer_path='timg1.jpg'
    img = Image.open(infer_path)
    plt.imshow(img)   
    plt.show()    
    
    img = load_image(infer_path)

    results = infer_exe.run(inference_program,                 #运行预测程序
                            feed={feed_target_names[0]: img},  #喂入要预测的img
                            fetch_list=fetch_targets)          #得到推测结果
    print('results',results)
    label_list = [
        "airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse",
        "ship", "truck"
        ]
    print("infer results: %s" % label_list[np.argmax(results[0])])
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值