How do I deploy my model?

username: Superklez
cooked: <p>Let’s say I build and train a network on handwritten data, how do I obtain what the network learned and generate new fonts? Or like what if I worked on image filters for photos, how do I use it for real-world applications? I’m not so sure on how I can deploy a model to real-world problems.</p>

username: pascal_notsawo
cooked: <p>All generally depend on the size of your application, its architecture, …</p>
<p>I would take the simple case of a <a href="https://www.djangoproject.com/" rel="noopener nofollow ugc">Django</a> application (or any application with an interface allowing the user to upload a photo, click on a button to analyze and receive the corresponding catecter as output).</p>
<p>Let’s suppose that you have finished training (and validating…) your model (let’s say of myModel class, inheriting of course from <a href="https://pytorch.org/docs/stable/generated/torch.nn.Module.html" rel="noopener nofollow ugc">torch.nn.Module</a>) and that you have saved it (with <a href="https://pytorch.org/docs/stable/generated/torch.save.html" rel="noopener nofollow ugc">toch.save</a> for example) : you have a ‘path/to/my/model/file’ file.</p>
<p>Let’s suppose that you have developed a small django application with a view <code>main</code> that takes the image uploaded by the user, switches to your model and returns the output to the front-end.<br>
You must first move the file containing your model into your django application, as well as the myModel class.</p>
<pre><code class="lang-auto">def  myModel(...) :
	...
	...

def main(request) :
	...
	...
	img = request.GET["image"]
	
	img = conver_img_to_vector(img)
	
	img = torch.tensor(img)

	model = myModel(your params)
	
	model.load_state_dict('path/to/my/model/file')

	label = model(img) 
	return label
</code></pre>
<p>The main drawback of this method is that you have to load your model every time a new image is sent. One way to correct this is to initialize your model in the <code>__init__.py</code> file of your application:</p>
<pre><code class="lang-auto">model = myModel(your params)
model.load_state_dict('path/to/my/model/file')
</code></pre>
<p>Create a <code>django view</code> that you will call at the very beginning of your application to load your model.</p>
<p>In <code>views.py</code>.</p>
<pre><code class="lang-auto">import model # equivalent to : form __init__ import model
...
...
def main(request) :
	...
	img = request.GET["image"]
	
	img = conver_img_to_vector(img)
	
	img = torch.tensor(img)

	label = model(img) 

	return label
</code></pre>
<p>With this the instance of your model will be the same for the whole lifetime of the application (application scope, singleton design pattern…) : you will just be able to update the <code>'path/to/my/model/file'</code> file from time to time and reload your application, without destroying the structure of the application.</p>
<p>But in many cases the web application execution environment is not a python environment like Django (Angular, …), or we don’t have enough space to store the models in the context of the application (case of mobile applications). In this case, instead of developing a single frontend+backend application like here, you’ll rather set up a Django api that receives an image request, calls the model and sends back the answer label: with this type of architecture any application made in any framework will be able to interrogate your api to make predictions, without really knowing what’s going on behind it.</p>
<p>This is just a very simplified description of what is done in practice for reasons of simplification.</p>