From 458ef1af1fed3b56d2aabf264fb4ba12fb8cc491 Mon Sep 17 00:00:00 2001 From: Adam Wojdyla Date: Mon, 16 May 2022 03:02:18 +0200 Subject: [PATCH] run prediction --- Jenkinsfile_predict | 38 +++++++++++++++++++++++++++++++++++ lab08_deepLearining_mlflow.py | 8 ++++---- lab08_predict.py | 15 ++++++++++++++ 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 Jenkinsfile_predict create mode 100644 lab08_predict.py diff --git a/Jenkinsfile_predict b/Jenkinsfile_predict new file mode 100644 index 0000000..2e71594 --- /dev/null +++ b/Jenkinsfile_predict @@ -0,0 +1,38 @@ +pipeline { + agent { + docker { image 's444507_create_dataset_image:latest' } + } + parameters { + buildSelector( + defaultSelector: lastSuccessful(), + description: 'Which build to use for copying artifacts for predict', + name: 'BUILD_SELECTOR') + string( + defaultValue: '{\\"inputs\\": [[0.51, 0.86], [0.79, 0.79], [0.74, 0.77], [0.66, 0.73]]}', + description: 'Input file', + name: 'INPUT', + trim: true + ) + } + stages { + stage('Copy arifacts') { + steps { + copyArtifacts projectName: 's444356-training/master', selector: buildParameter('BUILD_SELECTOR') + sh "echo ${params.INPUT} > input_example.json" + } + } + stage('Run prediction on model') { + steps { + sh "python3 lab08_predict.py $epoch" + } + } + } + post { + success { + archiveArtifacts artifacts: 'CarPrices_pytorch_model.pkl, mlruns/**, my_model/**', followSymlinks: false + } + always { + emailext body: "${currentBuild.currentResult}", subject: 's444507-training', to: 'e19191c5.uam.onmicrosoft.com@emea.teams.ms' + } + } +} \ No newline at end of file diff --git a/lab08_deepLearining_mlflow.py b/lab08_deepLearining_mlflow.py index a128c14..370f435 100644 --- a/lab08_deepLearining_mlflow.py +++ b/lab08_deepLearining_mlflow.py @@ -15,7 +15,7 @@ from sklearn import preprocessing import sys import logging import mlflow -import mlflow.sklearn +import mlflow.pytorch logging.basicConfig(level=logging.WARN) logger = logging.getLogger(__name__) @@ -32,9 +32,9 @@ class Model(nn.Module): self.layer3 = nn.Linear(60, 5) def forward(self, x): - x = F.relu(self.layer1(x)) - x = F.relu(self.layer2(x)) - x = F.softmax(self.layer3(x)) # To check with the loss function + x = F.relu(self.layer1(x.float())) + x = F.relu(self.layer2(x.float())) + x = F.softmax(self.layer3(x.float())) # To check with the loss function return x diff --git a/lab08_predict.py b/lab08_predict.py new file mode 100644 index 0000000..0184729 --- /dev/null +++ b/lab08_predict.py @@ -0,0 +1,15 @@ +import mlflow +import mlflow.sklearn +import pandas as pd +import numpy as np +import json + +logged_model = 'mlruns/1/4b83e774512444188fb587288818c298/artifacts/model' +model = mlflow.pyfunc.load_model(logged_model) + +with open('my_model/input_example.json') as f: + data = json.load(f) + input_example = np.array([data['inputs'][0]]).reshape(-1,4) + +print(f'Prediction: {model.predict(input_example)}') +