From d66f44a3dc6c1a5eebbd9ea4aefe03a184ee74b3 Mon Sep 17 00:00:00 2001 From: s424714 Date: Wed, 10 May 2023 14:47:19 +0200 Subject: [PATCH] feat: add jenkins for evaluate pipeline, create saving accuracy in file --- Jenkinsfile-eval | 84 +++++++++++++++++++++++++++++++++++++++++++++ src/evaluate.py | 5 +-- src/main.py | 3 +- src/models/utils.py | 12 +++++++ 4 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 Jenkinsfile-eval diff --git a/Jenkinsfile-eval b/Jenkinsfile-eval new file mode 100644 index 0000000..6a58574 --- /dev/null +++ b/Jenkinsfile-eval @@ -0,0 +1,84 @@ +node { + stage('Preparation') { + properties([ + pipelineTriggers([ + // TODO change auto run after train + upstream( + threshold: hudson.model.Result.SUCCESS, + upstreamProjects: 's424714-create-dataset' + ) + ]), + copyArtifactPermission('*'), + parameters([ + buildSelector( + defaultSelector: lastSuccessful(), + description: 'Which build to use for copying artifacts', + name: 'BUILD_SELECTOR' + ), + + ]) + ]) + } + + stage('Git clone') { + //cloning git repo + checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 's424714', url: 'https://git.wmi.amu.edu.pl/s424714/ium_424714']]]) + } + + stage('Dockerfile build') { + + + sh "chmod +x -R ${env.WORKSPACE}" + copyArtifacts fingerprintArtifacts: true, projectName: 's424714-create-dataset', selector: buildParameter('BUILD_SELECTOR') + //TODO from train + // copyArtifacts fingerprintArtifacts: true, projectName: 's424714-create-dataset', selector: buildParameter('BUILD_SELECTOR') + + def dockerImage = docker.build("s424714-model") + + dockerImage.inside { + withEnv(["TRANSFORMERS_CACHE=./.cache"]) { + stage("Docker: cloning artifacts"){ + sh 'mkdir -p ./data/dataset' + sh 'mv -t ./data/dataset train.csv test.csv val.csv' + sh 'mv -t ./data True.csv Fake.csv' + sh 'mv model.pt ./results/model.pt' + } + stage("Docker: Running training model") + { + sh 'mkdir -p ./.cache' + // sh "" + sh 'python ./src/main.py --test ' + sh "cp ./results/*.csv ${WORKSPACE}" + } + + } + } + + } + + stage('Saving artefacts') { + echo 'Goodbye!' + sh 'ls' + archiveArtifacts artifacts: '*.csv' + } + stage('Drawing plot') { + plot csvFileName: 'plot-accuracy.csv', + csvSeries: [[ + file: 'acc.csv', + exclusionValues: '', + displayTableFlag: false, + inclusionFlag: 'OFF', + url: '']], + group: 'Plot Group', + title: 'Accuracy', + style: 'line', + exclZero: false, + keepRecords: false, + logarithmic: false, + numBuilds: '', + useDescr: false, + yaxis: '', + yaxisMaximum: '', + yaxisMinimum: '' + } +} \ No newline at end of file diff --git a/src/evaluate.py b/src/evaluate.py index 052a7ce..2fab89a 100644 --- a/src/evaluate.py +++ b/src/evaluate.py @@ -44,5 +44,6 @@ def evaluate( results.extend(output.argmax(dim=1).tolist()) total_acc_test += acc - print(f"Test Accuracy: {total_acc_test / len(test_data): .3f}") - return results + accuracy = round(total_acc_test / len(test_data), 3) + print(f"Test Accuracy: {accuracy: .3f}") + return results, accuracy diff --git a/src/main.py b/src/main.py index a81190a..93297b0 100644 --- a/src/main.py +++ b/src/main.py @@ -75,9 +75,10 @@ if __name__ == "__main__": # evaluating model if args.test: model = utils.load_model(model=BertClassifier(), model_path=args.model_path) # loading model from model.pt file - results = evaluate( + results, accuracy = evaluate( model=model, test_data=test_data, batch_size=BATCH_SIZE, ) utils.save_results(labels=test_data["label"], results=results, file_path=args.results_path) + utils.save_data_to_csv("./results/acc.csv", accuracy) diff --git a/src/models/utils.py b/src/models/utils.py index ca5465f..1a4cd4d 100644 --- a/src/models/utils.py +++ b/src/models/utils.py @@ -32,3 +32,15 @@ class utils: df = pd.DataFrame({"labels": labels, "results": results}) df.to_csv(file_path, index=False) + + @staticmethod + def save_data_to_csv(file_path: str, data: float) -> None: + file_path = Path(file_path) + + df = pd.DataFrame([data]) + df.to_csv(file_path, mode="a", header=False, index=False) + + +if __name__ == "__main__": + x = 1.2 + utils.save_data_to_csv("./results/eval.csv", x)