Integration of Deep Learning with DevOps
Chirag Vaishnav ????
Helping You Grow | SDE @slice | Ex. ElectricPe | Gold Medalist in B.Tech | Ex. GDSC lead
Earlier, we had to manually set the value of each hyperparameter for increasing the accuracy of a deep learning model. But now DevOps has solved our problem to a great extent. By integrating Deep learning with Devops this process can be automatically done.
Today, I am going to show you my project in which I have used jenkins for the automation process and integrated it with my deep learning model. I have used fashion_mnist as my dataset.
Steps for automation-
- We will write a code, save it as .py extension and push the code the github.
fashion_mnist.py
git_bash
2. In my workspace folder in redhat, I have created a Dockerfile which will create a docker image having python3, keras and tensorfollow installed in it. So that I can create my own container and deploy my deep learning model from it.
Dockerfile
After this use this command to build the image using Dockerfile.
docker build -t pyos:v3 .
Syntax- docker build -t {name of image:version} {location}
2. I have created Build Pipeline of 6 jobs in jenkins for the automation process. I am going to show each job one by one.
Build Pipeline
i. ml_job1- This job will automatically copy the files from my github repository to my workspace in redhat. Go to Source Code Management and click on git and then write the link of your github repo in Repository URL.
ml_job1
sudo cp -v -r -f * /mlops_project/files
Write this code in the Execute Shell option of Build Section.
ii. ml_job2- This job will create the container corresponding to your code i.e If you are using CNN then it will create container corresponding to CNN image.
ml_job2
if grep “keras” /mlops_project/files/fashion_mnist.py then if sudo docker ps | grep mlpy then echo “already running” else sudo docker run -dit -v /mlops_project/files:/root/ws — name mlpy pyos:v3 fi else “This is not a deep learning code” fi
If the build of this project is unstable then automatically ml_job6 will run which will again launch the container. I will show the code of ml_job6 later in my article.
iii. ml_job3- This job will automatically run the deep learning code from the container. And in the code there is function which will save the accuracy in output.txt file . You will find the code in my github repository.
sudo docker exec mlpy python3 /root/ws/fashion_mnist.py
iv. ml_job4- This job will check the accuracy. If the accuracy is less than 99% then it will tweak the model i.e increase the epochs and add one convolution and max pooling layer ,otherwise it will send the mail to the developer.
ml_job4
read=$(sudo cat /mlops_project/files/output.txt)echo $read*100 | bc -lif (( $(echo “$read > 0.99” | bc -l) )); then echo “Achieved accuracy” exit 1 else cd /mlops_project/filessudo sed -i ‘/^epoch=.*/a epoch=epoch+5’ fashion_mnist.py sudo sed -i “55i model.add(Conv2D(filters=64, kernel_size=2, padding=’same’, activation=’relu’, input_shape=(28,28,1)))” fashion_mnist.pysudo sed -i “56i model.add(MaxPool2D(pool_size=2))” fashion_mnist.pyfi
Here, I have used ‘sed’ command for inserting more layers and increasing the epochs in my code file.
If accuracy above 99% is achieved then the it will deliberately fail the model so that mail will be sent to the developer.
You fill find this is in Editable Email Notification in post build section.
ml_job4
v. ml_job5- This job will again train the model and save the accuracy in output.txt file. After this if build is stable then it will again go to ml_job4 and check for accuracy. This process will continue till 99% accuracy is not achieved.
sudo docker exec mlpy python3 /root/ws/fashion_mnist.py
vi. ml_job6- In case due to any problem the conatiner fails then this job will launch a new container and start the processes again. (training, checking accuracy , etc.).
ml_job6
if sudo docker ps | grep mlpy then echo “already running” elif sudo docker ps -a | grep mlpy then sudo docker start mlpy else sudo docker run -dit -v /mlops_project/files:/root/ws — name mlpy pyos:v3 fi
Output-
After training my model for the first time using 5 epochs by ml_job3, I got 89.47% accuracy.
output- ml_job3
As it is less than 99% so ml_job4 has performed certain changes in my model .
output- ml_job4
After ml_job5 again trained the model using 10 epochs, accuracy was 98.15% which is still less than 99% .
output- ml_job5
So, again ml_job4 added one more convolution and max. pool layer and increased the epochs to 20.
ml_job5 again trained the model and got accuracy 99.42% which is greater than 99% .
output- ml_job5
final accuracy
So, the build deliberately failed and it sent email to the developer that the model has achieved accuracy more than the desired accuracy.
You can change other hyperparameters also to increase the accuracy.