Integration of Machine Learning with DevOps

Integration of Machine Learning with DevOps

Here we will be Integrating Machine Learning with the DevOps .. (GitHub , Jenkins , Docker )

Lets get started with the implementation ...

1. Create container image that’s has Python3 and Keras or numpy installed using dockerfile 

FROM python:3


WORKDIR /usr/src/mlops


COPY req.txt ./


RUN pip install --upgrade pip 


RUN pip install --no-cache-dir -r req.txt


COPY . .


CMD [ "python", "./main.py" ]

  • This will create a Docker Image for Machine Learning models .
  • req.txt file contains the following libraries ..
Keras
numpy
tensorflow
pandas

Note : pip will configure all the dependencies required for the libraries .

  • I have given a extra line in the Dockerfile "COPY . . " . It can be used to either copy your python code directly to the image while building or we can also use mount while launching the image .

Job1 : Pull the GitHub repository automatically when developers push repository to GitHub.

  • For this we will be using GitHub Web-hooks integrated with Jenkins.
No alt text provided for this image
  • Now creating Jenkins job to automatically trigger and download the files from GitHub repository .
No alt text provided for this image
No alt text provided for this image
  • Now the Job is created ..when the developer push code to the GitHub ..it will automatically trigger and download the code in the workspace folder .
  • You can get the code "main.py" from my GitHub repository .

Note : I have manually copied my dataset to "/root/ws/" for the code to automaticaly load it . (Don't waste internet by pushing in GitHub and Pulling it on workspace..)

Job2 : Launch the image based on the ML code.

  • Here I have used the same image for ML Models (CNN , ANN ) ....as CNN only requires additional Keras and Tenserflow rest libraries are same for both .
sudo docker run -v /root/ws:/usr/src/mlops --name model act3:v1

Note : Job2 is Downstream it will automatically start when the job1 is finished i.e, Job Chaining .

  • When the image is launched it will start training the model and predict accuracy or metrics.
  • The Accuracy of the model is stored in "acc.txt" file .

Note : Log of the Model training can be accessibleby Console Oputput in Jenkins .

No alt text provided for this image

Job3 : if metrics accuracy is less than 80% , then tweak the machine learning model architecture (Tweaking Model).

filename="acc.txt"


while IFS= read -r line
do 

if [[ $line >= 0.8* ]] 
then
    printline="yes"       
    echo "-----------------------"
    echo "MODEL HAS TRAINED SUCESSFULLY"
    echo "-----------------------"   
else
	echo"MODEL ACCURACY IS LOW"
fi


if [[ $printline == "yes" ]] ;
	then
		echo "-----------------------"
		echo "MODEL ACCURACY IS $line"
		echo "-----------------------"         
else
		echo"MODEL ACCURACY IS LOW"
fi  


if [[ $line < 0.8* ]] 
then       
    printline="no"
    if sudo docker ps -a | grep model
    then
        sudo docker rm -f model 
        echo "MODEL REMOVED"
    else
        echo "TWEAK ERROR"
    fi
else
fi


if [[ $printline == "no" ]] 
then
    echo "-----------------------"
	echo "MODEL ACCURACY IS LESS $line"
	echo "-----------------------" 
	echo "TWEAKING MODEL" 


	sed -i '64i model.add(Dense(units=10,activation="relu",kernel_initializer="he_normal" )) ' main.py


	sudo docker run -v /root/ws/:/usr/src/mlops --name tuning act3:v1       
else
echo " Couldn't add Dense Layer"
fi




done < "$filename"

Note : Here you can add as per your requirement to tweak the model . I have done as per my understanding .. !! can add

  • This is also a Downstream project it will be automatically triggered by job2.
  • When the Tuning is done it will relaunch the job3 to check the accuracy .

Job4: Retrain the model or notify that the best model is being created

  • If the Accuracy is Less than 80% then the tweak will launch the tuning model and the Developer will be notified by mail .
filename="acc.txt"


while IFS= read -r line
do 

if [[ $line < 0.8* ]] 
then
    exit 1 
else
	exit 0
fi
  • If Accuracy is less then it will send the job failed mail to Developer & If it's a Success it will send the job success mail .
  • Configure the E-mail Notification Plugin in the Jenkins .

Job5: Monitoring

  • It will monitor the ML images if job2 or the tuning of the model job3 failed due to any error it will automatically start the model .
if ( sudo docker ps -a | grep model )
then 
echo "working "
else
sudo docker run -v /root/ws/:/usr/src/mlops --name model act3:v1
fi


if (sudo docker ps -a | grep tuning )
then 
    if sudo docker ps | grep model
    then
       sudo docker rm -f model
    else
       echo "tuned "       
    fi

elif (sudo docker ps -a | grep tuning)
then
    echo"working"
else
    sudo docker run -v /root/ws/:/usr/src/mlops --name tuning act3:v1
fi

  • This will launch the images if it isn't running and relaunch the job3 and job4 for the model accuracy predictions and tweaking ( by using downstream or upstream based on your requirements ).

Outputs :

No alt text provided for this image
No alt text provided for this image

Implementation isn't time consuming ..requirements gathering is and everything depend on trial and error ..so keep trying ..!!

Well thanks to this activity I got to learn many new things by doing it practically & looking forward to learning by implementing various upcoming activities ... !!!

Hope you liked it .. Integration of Machine Learning Model with DevOps (Docker , Jenkins , GitHub ) .... \\(*^*)//

To view or add a comment, sign in

More articles by Mohamed Furqan

Explore content categories