From 2c0a3546d139839d0255e2487f77439dac00e069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20=C5=81=C4=85czkowski?= Date: Fri, 19 Apr 2024 11:40:54 +0200 Subject: [PATCH] IUM_06 - add evaluation Jenkinsfile, update Dockerfile, update models/Jenkinsfile, update predict.py script --- Dockerfile | 1 + evaluation/Jenkinsfile | 40 ++++++++++++++++++++++++++++++++++++++++ models/Jenkinsfile | 1 + predict.py | 16 +++++++++++++++- 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 evaluation/Jenkinsfile diff --git a/Dockerfile b/Dockerfile index 98ecec8..1eabfc8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,6 +20,7 @@ WORKDIR /app # Python scripts COPY download_dataset.py ./ COPY get_stats.py ./ +COPY NeuralNetwork.py ./ COPY create_model.py ./ COPY predict.py ./ diff --git a/evaluation/Jenkinsfile b/evaluation/Jenkinsfile new file mode 100644 index 0000000..3ac4e65 --- /dev/null +++ b/evaluation/Jenkinsfile @@ -0,0 +1,40 @@ +pipeline { + agent any + + stages { + stage('Clone repository') { + steps { + checkout scm + } + } + + stage('Copy artifacts') { + agent { + dockerfile { + filename 'Dockerfile' + reuseNode true + } + } + + steps { + copyArtifacts(projectName: 's464863-training/main', filter: 'models/model.pth, datasets/test.csv', selector: lastSuccessful()) + copyArtifacts(projectName: 's464863-evaluation/main', filter: 'metrics.csv', selector: lastSuccessful(), optional: true) + } + } + + stage('Evaluate model') { + agent { + dockerfile { + filename 'Dockerfile' + reuseNode true + } + } + + steps { + sh "chmod +x ./predict.py" + sh "python3 ./predict.py" + archiveArtifacts artifacts: 'predictions.csv, metrics.csv', onlyIfSuccessful: true + } + } + } +} \ No newline at end of file diff --git a/models/Jenkinsfile b/models/Jenkinsfile index a8fba8f..9e287b7 100644 --- a/models/Jenkinsfile +++ b/models/Jenkinsfile @@ -51,6 +51,7 @@ pipeline { sh "chmod +x ./create_model.py" sh "python3 ./create_model.py" archiveArtifacts artifacts: 'models/model.pth, datasets/*', onlyIfSuccessful: true + build job: 's464863-evaluation/main', wait: false } } } diff --git a/predict.py b/predict.py index 68871b1..b4193a9 100644 --- a/predict.py +++ b/predict.py @@ -3,8 +3,9 @@ import os import pandas as pd import numpy as np -from NeuralNetwork import NeuralNetwork +from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score +from NeuralNetwork import NeuralNetwork # Load model if it exists if os.path.exists('./models/model.pth'): @@ -29,5 +30,18 @@ if os.path.exists('./models/model.pth'): # Save predictions to CSV pd.DataFrame(y_pred, columns=['Prediction']).to_csv('predictions.csv', index=False) + + # Calculate metrics + accuracy = accuracy_score(y_test, y_pred) + precision = precision_score(y_test, y_pred) + recall = recall_score(y_test, y_pred) + f1 = f1_score(y_test, y_pred) + + # Save metrics to CSV (append mode, if file exists, if not, create it) + if not os.path.exists('metrics.csv'): + pd.DataFrame([[accuracy, precision, recall, f1]], columns=['Accuracy', 'Precision', 'Recall', 'F1']).to_csv('metrics.csv', index=False) + else: + # without header + pd.DataFrame([[accuracy, precision, recall, f1]], columns=['Accuracy', 'Precision', 'Recall', 'F1']).to_csv('metrics.csv', index=False, mode='a', header=False) else: raise FileNotFoundError('Model not found')