Code Monkey home page Code Monkey logo

oodcls's Introduction

OodCls

oodCls.py 中定义了一个 Oodcls 类,封装了一个基于 PyTorch 的分类模型,对 mnist 数据集中的图像,可以正确识别所代表的数字(0-9);同时,对于非数字的图像,识别为OOD类(10)。

使用方法

初始化模型

在使用 OodCls 进行分类预测之前,需要将训练好的模型加载到类中。可以使用以下代码进行模型初始化和加载。

from oodcls import OodCls

ood_cls = OodCls()

进行预测

要使用 OodCls 进行分类预测,需要将输入数据传递给接口函数 classify 。该函数的参数是一个 n*1*28*28 的tensor(n是batch的大小,每个 1*28*28 的tensor表示的数字图像),输出是整数型n维tensor(n是batch的大小,每个整数在0~10范围内,代表分类结果)。

# 进行预测,结果保存在n维tensor里

preds = ood_classifier.classify(imgs)

示例

在以下示例中,我们将使用 OodCls 类对一组随机数据进行分类预测。

from oodcls import OodCls

# 初始化 OodCls 类
ood_cls = OodCls()

# 准备输入数据imgs
......
# 如果数据在GPU里,则需要将数据加入CPU里
imgs = torch.tensor([item.cpu().detach().numpy() for item in imgs]).cuda()

# 进行预测
preds = ood_classifier.classify(imgs)

print("Predict Label is:", [i.item() for i in preds.data])

oodcls's People

Contributors

b2eeze avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.