Possible to train a ML model in Django?

Hi everyone, this is my first post here - hopefully it is ok to ask for advice when just learning and testing Django!

I would want to create a web app where users contribute to the same machine learning model (local server fine at this stage). This means that they would upload or choose an image (an updating image on main page based on JSON) in their browser and classify it with a button A or B. This click would either add a training example via transfer learning or, at least, save the image file with the label so that those new examples can be added to training set (folders A and B) later and then training would be called again. Thus, I am interested in creating a teachable machine that collects image+label combinations from users and learns.

I obviously know that a lot depends on the ML model; here I would appreciate hearing if this sounds reasonable in Django and, if yes, where to look for more information. I managed to get this tutorial working: https://www.youtube.com/watch?v=mgX-2_ybqNk

He uses these views for predictions and showing the images uploaded by users, I was wondering if a similar view could be called to add one image and label to dataset as well:

>   def predictImage(request):
>         print(request)
>         print(request.POST.dict())
>         fileObj = request.FILES['filePath']
>         fs=FileSystemStorage()
>         filePathName=fs.save(fileObj.name, fileObj)
>         filePathName = fs.url(filePathName)
>         testimage = '.' + filePathName
>         img = image.load_img(testimage, target_size=(img_height, img_width))
>         x = image.img_to_array(img)
>         x = x / 255
>         x = x.reshape(1, img_height, img_width, 3)
>         #with model_graph.as_default():
>         # with tf_session.as_default():
>         predi = model.predict(x)
>         import numpy as np
>         predictedLabel = labelInfo[str(np.argmax(predi[0]))]
>         context = {'filePathName':filePathName, 'predictedLabel':predictedLabel[1]}
>         return render(request, 'index.html', context)
>     def viewDataBase(request):
>         import os
>         listOfImages = os.listdir('./media/')
>         listOfImagesPath = ['./media/' + i for i in listOfImages]
>         context = {'listOfImagesPath': listOfImagesPath}
>         return render(request, 'viewDB.html', context)

Many thanks if you have time to share thoughts!

I don’t see where doing this is a problem.

Your Django view can certainly handle an uploaded file along with other data, storing that file in any location accessible to the account running the Django process.

What I would recommend is that you don’t try to run the retraining within the view. I’d use any of the job queuing systems to allow the upload to either start a retraining right then or schedule one for a later time.

There was a blog post linked to by the current issue of Django News where the author talks about using Huey as a task queue. It’s a good article talking about that author’s decision to use Huey, but just as much interest to me were the links in there to the other options.


1 Like

Excellent, thank you for answer! I will check the blog post that you mentioned, sounds promising! Best, Veera