7 Fashion 数据识别

利用上篇训练好的模型,如何在实际中使用。

import  os
from  PIL import  Image
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Model
from  tensorflow.keras.layers import  Flatten ,Dense,Conv1D,MaxPool1D



class Mymodel(Model):
    def __init__(self):
        super(Mymodel, self).__init__()

        self.c1 = Conv1D(filters=256,kernel_size=2,padding='same',activation='relu')
        self.m1 = MaxPool1D(pool_size=2,strides=2,padding='same')
        self.flatten = Flatten()
        self.d1 = Dense(units=128,activation=tf.keras.activations.relu)
        self.d2 = Dense(units=10,activation=tf.keras.activations.softmax)

    def call(self,x):

        x = self.c1(x)
        x = self.m1(x)
        x = self.flatten(x)
        x = self.d1(x)
        y = self.d2(x)

        return y


model = Mymodel()

checkpoint_save_path = './models/fashionconv/fashion.ckpt'
if os.path.exists(checkpoint_save_path+'.index'):
    print('-------------load model-----------')
    model.load_weights(checkpoint_save_path)
while 1 :
    perNum = int(input('请输入要识别的图片:'))

    predict_label = ['t-shirt','trouser','pullover','dress','coat','sandal','shirt','sneaker','bag','ankle_boot']

    img_path = './data/class4/FASHION_FC/{}.jpeg'.format(perNum)

    img= Image.open(img_path)

    img = img.resize((28,28),Image.ANTIALIAS)

    img_arr = np.array(img.convert('L'))

    for i in range(28):
        for j in range(28):
            if img_arr[i][j]< 200:
                img_arr[i][j] = 255
            else:
                img_arr[i][j] = 0


    img_arr = img_arr  / 255.0

    x_predict = img_arr[tf.newaxis,...]

    result = model.predict(x_predict)

    pred = tf.argmax(result,axis=1)

    print('\n')

    i = tf.gather(pred,0)

    print(predict_label[i])

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值