Compare commits

..

No commits in common. "main" and "sacred" have entirely different histories.
main ... sacred

20 changed files with 40 additions and 247 deletions

3
.dvc/.gitignore vendored
View File

@ -1,3 +0,0 @@
/config.local
/tmp
/cache

View File

@ -1,4 +0,0 @@
[core]
remote = ium_ssh_remote
['remote "ium_ssh_remote"']
url = ssh://ium-sftp@tzietkiewicz.vm.wmi.amu.edu.pl

View File

@ -1,3 +0,0 @@
# Add patterns of files dvc should ignore, which could improve
# the performance. Learn more at
# https://dvc.org/doc/user-guide/dvcignore

2
.gitignore vendored
View File

@ -20,5 +20,3 @@ evaluation_results.txt
model_out
trend.png
sacred_runs
mlruns
/data

View File

@ -5,13 +5,11 @@ WORKDIR /app
ADD ./requirements.txt .
RUN pip install -r requirements.txt
ADD ./download_dataset.sh .
ADD . .
ARG KAGGLE_USERNAME
ARG KAGGLE_KEY
RUN chmod a+x *.sh
RUN ./download_dataset.sh
ADD . .
CMD python train_model.py

View File

@ -1,41 +0,0 @@
pipeline {
parameters {
string(
defaultValue: 'marcinkostrzewski',
description: 'Kaggle username',
name: 'KAGGLE_USERNAME',
trim: false
)
password(
defaultValue: '',
description: 'Kaggle token taken from kaggle.json file, as described in https://github.com/Kaggle/kaggle-api#api-credentials',
name: 'KAGGLE_KEY'
)
}
agent {
dockerfile {
additionalBuildArgs "--build-arg KAGGLE_USERNAME=${params.KAGGLE_USERNAME} --build-arg KAGGLE_KEY=${params.KAGGLE_KEY} -t s444409-create-dataset"
}
}
stages {
stage("Run DVC") {
steps{
withCredentials([
sshUserPrivateKey(
credentialsId: '48ac7004-216e-4260-abba-1fe5db753e18',
keyFileVariable: 'IUM_SFTP_KEY',
passphraseVariable: '',
usernameVariable: 'USER'
)
]) {
sh 'dvc remote modify --local ium_ssh_remote keyfile $IUM_SFTP_KEY'
sh 'dvc remote modify --local ium_ssh_remote password IUM@2021'
sh 'dvc pull'
sh 'dvc repro'
}
}
}
}
}

View File

@ -1,34 +0,0 @@
pipeline {
agent {
docker {
image 's444409-create-dataset'
}
}
parameters {
string(
defaultValue: '{\\"inputs\\": [[0.51, 0.86], [0.79, 0.79], [0.74, 0.77], [0.66, 0.73]]}',
description: 'Input',
name: 'INPUT',
trim: true
)
buildSelector(
defaultSelector: lastSuccessful(),
description: 'Which build to use for copying artifacts',
name: 'BUILD_SELECTOR'
)
}
stages {
stage('Get dataset from artifact') {
steps {
copyArtifacts projectName: 's444356-training/master', selector: buildParameter('BUILD_SELECTOR')
}
}
stage('Predict values using model from artifact') {
steps {
sh "echo ${params.INPUT} > input_example.json"
sh "python predict_s444356.py"
}
}
}
}

View File

@ -1,16 +0,0 @@
pipeline {
agent {
docker {
image 's444409-create-dataset'
args '-v /mlruns:/mlruns'
}
}
stages {
stage('Predict values using model from artifact') {
steps {
sh "python predict_s444356-from-registry.py"
}
}
}
}

View File

@ -23,7 +23,6 @@ pipeline {
agent {
docker {
image 's444409-create-dataset'
args '-v /mlruns:/mlruns'
}
}
@ -35,14 +34,13 @@ pipeline {
}
stage('Train model') {
steps {
sh "python train_model.py -e ${params.EPOCHS} -b ${params.BATCHSIZE}"
archiveArtifacts artifacts: 'model_out', onlyIfSuccessful: true
archiveArtifacts artifacts: 'mlruns/**', onlyIfSuccessful: true
sh 'rm -r mlruns'
sh "python train_model.py with 'epochs=${params.EPOCHS}' 'batch_size=${params.BATCHSIZE}'"
}
}
stage('Evaluate model') {
stage('Archive model and evaluate it') {
steps {
archiveArtifacts artifacts: 'model_out', onlyIfSuccessful: true
archiveArtifacts artifacts: 'sacred_runs/**', onlyIfSuccessful: true
build job: "s444409-evaluation/${params.BRANCH}/", parameters: [string(name: 'BRANCH', value: "${params.BRANCH}")]
}
}

View File

@ -1,14 +0,0 @@
name: ium_s444409
docker_env:
image: s444409-create-dataset
volumes: ["/tmp/mlruns:/mlruns:rw"]
entry_points:
main:
parameters:
epochs: {type: float, default: 5}
batch_size: {type: float, default: 64}
command: "python train_model.py -e {epochs} -b {batch_size}"
eval:
command: "python eval_model.py"

View File

@ -1,5 +0,0 @@
outs:
- md5: 107d31dc52f58274023d18db2f0c5b7a.dir
size: 24456416
nfiles: 17
path: data

View File

@ -1,4 +0,0 @@
schema: '2.0'
stages:
prepare:
cmd: download_dataset.sh

View File

@ -1,16 +0,0 @@
stages:
prepare:
cmd: download_dataset.sh
train:
cmd: python3 train_model.py
deps:
- data/Plant_1_Generation_data.csv.train
params:
- batch_size
- epochs
eval:
deps:
- model_out
cmd: python3 eval_model.py

View File

@ -1,11 +0,0 @@
name: ium444409
channels:
- conda-forge
- defaults
dependencies:
- kaggle
- pandas
- pytorch
- matplotlib
- mlflow
prefix: C:\Users\komar\.conda\envs\ium444409

View File

@ -24,7 +24,7 @@ class MLP(nn.Module):
def forward(self, x):
x = x.view(x.size(0), -1)
return self.layers(x.float())
return self.layers(x)
class PlantsDataset(Dataset):

View File

@ -1,2 +0,0 @@
batch_size: 64
epochs: 5

View File

@ -1,14 +0,0 @@
import json
import mlflow
import numpy as np
logged_model = '/mlruns/13/da5c6167bb45403fa35569849a1fbc13/artifacts/model'
loaded_model = mlflow.pyfunc.load_model(logged_model)
with open(f'{logged_model}/input_example.json') as f:
data = json.load(f)
input_example = np.array([data['inputs'][0]], dtype=np.float64).reshape(-1, 2)
print(f'Prediction: {loaded_model.predict(input_example)}')

View File

@ -1,14 +0,0 @@
import json
import mlflow
import numpy as np
logged_model = 'mlruns/1/4b83e774512444188fb587288818c298/artifacts/model'
loaded_model = mlflow.pyfunc.load_model(logged_model)
with open('input_example.json') as f:
data = json.load(f)
input_example = np.array([data['inputs'][0]], dtype=np.float64).reshape(-1, 2)
print(f'Prediction: {loaded_model.predict(input_example)}')

View File

@ -4,8 +4,4 @@ torch==1.11.0
numpy~=1.22.3
matplotlib==3.5.2
sacred==0.8.2
pymongo==4.1.1
mlflow==1.25.1
dvc==2.10.2
dvc-ssh==0.0.1a0
paramiko==2.11.0
pymongo==4.1.1

View File

@ -1,11 +1,12 @@
import argparse
from urllib.parse import urlparse
import mlflow
import numpy as np
import pandas as pd
import torch
from sacred.observers import FileStorageObserver, MongoObserver
from torch import nn
from torch.utils.data import DataLoader
from torch.utils.data import DataLoader, Dataset
from sacred import Experiment
from model import PlantsDataset, MLP, train, test
@ -14,30 +15,13 @@ default_epochs = 5
device = "cuda" if torch.cuda.is_available() else "cpu"
# mlflow.set_tracking_uri("http://172.17.0.1:5000")
mlflow.set_experiment("s444409")
def setup_args():
args_parser = argparse.ArgumentParser(prefix_chars='-')
args_parser.add_argument('-b', '--batchSize', type=int, default=default_batch_size)
args_parser.add_argument('-e', '--epochs', type=int, default=default_epochs)
return args_parser.parse_args()
if __name__ == "__main__":
args = setup_args()
batch_size = args.batchSize
epochs = args.epochs
def main(batch_size, epochs, _run):
print(f"Using {device} device")
plant_test = PlantsDataset('data/Plant_1_Generation_Data.csv.test')
plant_train = PlantsDataset('data/Plant_1_Generation_Data.csv.train')
input_example = np.array([plant_test.x_train.numpy()[0]])
train_dataloader = DataLoader(plant_train, batch_size=batch_size)
test_dataloader = DataLoader(plant_test, batch_size=batch_size)
@ -51,37 +35,37 @@ if __name__ == "__main__":
loss_fn = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
for t in range(epochs):
print(f"Epoch {t + 1}\n-------------------------------")
train(train_dataloader, model, loss_fn, optimizer)
last_loss = test(test_dataloader, model, loss_fn)
_run.log_scalar('training.loss', last_loss, t)
print("Done!")
torch.save(model.state_dict(), './model_out')
print("Model saved in ./model_out file.")
with mlflow.start_run() as run:
print("MLflow run experiment_id: {0}".format(run.info.experiment_id))
print("MLflow run artifact_uri: {0}".format(run.info.artifact_uri))
mlflow.log_param("batch_size", batch_size)
mlflow.log_param("epochs", epochs)
for t in range(epochs):
print(f"Epoch {t + 1}\n-------------------------------")
train(train_dataloader, model, loss_fn, optimizer)
last_loss = test(test_dataloader, model, loss_fn)
mlflow.log_metric("rmse", last_loss)
with torch.no_grad():
preds = model(plant_test.x_train)
signature = mlflow.models.signature.infer_signature(plant_test.x_train.numpy(), preds.numpy())
tracking_url_type_store = urlparse(mlflow.get_tracking_uri()).scheme
if tracking_url_type_store != "file":
mlflow.log_artifact('model.py')
mlflow.pytorch.log_model(
model,
"s444409",
registered_model_name="s444409",
signature=signature,
input_example=input_example,
code_paths=['model.py']
)
else:
mlflow.pytorch.log_model(model, "s444409", signature=signature, input_example=input_example,
code_paths=['model.py'])
def setup_experiment():
ex = Experiment('Predict power output for a given time')
ex.observers.append(FileStorageObserver('sacred_runs'))
ex.observers.append(MongoObserver(url='mongodb://admin:IUM_2021@172.17.0.1:27017',
db_name='sacred'))
return ex
ex = setup_experiment()
@ex.config
def experiment_config():
batch_size = 64
epochs = 5
@ex.automain
def run(batch_size, epochs, _run):
main(batch_size, epochs, _run)
ex.add_artifact('model_out')