Exemple #1
0
class SeldonModel:
    def __init__(self):
        self._model = Net()
        self._model.load_state_dict(
            torch.load("/storage/model.pkl", map_location=torch.device("cpu"))
        )
        self._model.eval()

    def predict(self, X, features_names):
        """
        Return a prediction.

        Parameters
        ----------
        X : array-like
        feature_names : array of feature names (optional)
        """
        data = transforms.ToTensor()(Image.open(io.BytesIO(X)))
        return self._model(data[None, ...]).detach().numpy()

    def send_feedback(self, features, feature_names, reward, truth):
        """
        Handle feedback

        Parameters
        ----------
        features : array - the features sent in the original predict request
        feature_names : array of feature names. May be None if not available.
        reward : float - the reward
        truth : array with correct value (optional)
        """
        print("Send feedback called")
        return []
def load_model():
    model = Net()
    model.load_state_dict(torch.load('model.pth'))
    model.eval()
    return model
Exemple #3
0
import torchvision.datasets as datasets
import torchvision.models as models
import numpy as np
import numpy.random as r
import sklearn.metrics as m
from main import Net
import glob

model_dir = 'checkpoint_lite.pth.tar'
model = Net()
model = torch.nn.DataParallel(model).cuda()
checkpoint = torch.load(model_dir)
model.load_state_dict(checkpoint['state_dict'])
criterion = nn.CrossEntropyLoss().cuda()

model.eval()

#print(model.state_dict())
#print([i[0] for i in model.named_modules()])
#exit(0)

batch_num = 100
batch_size = 64
print(batch_size)
valdir = os.path.join('/train/trainset/1/DMS/data/', 'val_phone')

normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])

val_loader = torch.utils.data.DataLoader(
    datasets.ImageFolder(
Exemple #4
0
import io
import numpy as np

from torch import nn
import torch.utils.model_zoo as model_zoo
import torch.onnx
import onnx
# import caffe2.python.onnx.backend as onnx_caffe2_backend

batch_size = 1  # just a random number
from main import Net

torch_model = Net()
torch_model.load_state_dict(
    torch.load('/home/odedf/lw_model.pth', map_location='cpu'))
torch_model.eval()

x = torch.randn(batch_size, 3, 43, 43, requires_grad=True)

torch_out = torch.onnx._export(
    torch_model,  # model being run
    x,  # model input (or a tuple for multiple inputs)
    "/home/odedf/lw_model.onnx",  # where to save the model (can be a file or file-like object)
    export_params=True
)  # store the trained parameter weights inside the model file

# Load the ONNX ModelProto object. model is a standard Python protobuf object
model = torch.onnx.load("/home/odedf/lw_model.onnx")

# prepare the caffe2 backend for executing the model this converts the ONNX model into a
# Caffe2 NetDef that can execute it. Other ONNX backends, like one for CNTK will be
Exemple #5
0
    batch_size=args.test_batch_size, shuffle=True, **kwargs)


test_model = Net()
print("hello1")
#modules = [model]
#load_ckpt(modules, "net-epoch-20_baseline", load_to_cpu=False)

test_model.load_state_dict(torch.load("net-epoch-20_baseline.pth"))
test_model.train(False)

print("hello2")
if args.cuda:
    test_model.cuda()
print("hello3")
test_model.eval()
test_loss = 0
correct = 0
for data, target in test_loader:
    if args.cuda:
        data, target = data.cuda(), target.cuda()
    data, target = Variable(data, volatile=True), Variable(target)
    output = test_model(data)
    test_loss += F.nll_loss(output, target, size_average=False).data[0] # sum up batch loss
    pred = output.data.max(1, keepdim=True)[1] # get the index of the max log-probability
    correct += pred.eq(target.data.view_as(pred)).long().cpu().sum()

test_loss /= len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.3f}%)\n'.format(
    test_loss, correct, len(test_loader.dataset),
    100. * correct / len(test_loader.dataset)))