Hello everyone, in this video we are going to implement our inference function. Because this function will be called from the flask application will write in a way that allows users to upload an image which will be processed and based on that retrieve all closest looking images. As you know, in the previous video, we have saved the training set vectors, which will be called and using this function to compare an uploaded image with every single image vector from the training set function returns the indices of the closest images. So we are not going to return images exactly, but indices of the closest images and by locating paths are the closest images, we can easily show them to a user. Okay, let's jump straight to the implementation. The first thing that we have to do is to read the uploaded image by using our image loader function.
The first argument will be uploaded image path, which is also the app argument of the inference function, and the second argument is image size. Okay, now we need to use our pre trained model to retrieve and create vectors for a newly uploaded image. To do that, right, dense underscore to underscore features, and the same that for for equals to session dot run, and in that provides brackets and set the list of variables model dot dense underscore to underscore features and model dot dense underscore for underscore features. Now we need to provide our feed dict to this run method, first we need to set model dot inputs because our network takes four dimensional inputs, and loaded image is free dimensional image height image with an chiles. We'll put our loaded image to a list and the drop rate will be set to Finally, now that we have created our fifth dict provided to our run function by providing fed dict equals feta dict.
Before comparing vectors, we need to check which distance matrix is selected. If the distance matrix is Hamming, we are going to pre process our vector one way, and completely differently if the distance metric is picked as a callsign define closest IDs to none. And we are going to use that to return IDs. So if nothing is found, the function will return none. Now let's write the code for the case that hamming distance is selected. Firstly, we are going to set closest IDs to something and let's find what the something is.
In the case of hamming distance. Before comparing our vectors, we need to binarize our newly defined data to binarize our dense two features type MP Were thence to features less than 0.5. Set zero. Otherwise set one. Do the same thing for dense for features. Now that we have binarized layers vectors, let's tag them together to create our final image vector.
We'll do that by using MP dot h stack of list of variables in this case, dense two features and dense four features. Now we have everything to compare vectors and get closest IDs after closest IDs, right, hamming distance, then provide first argument to be training set factors. Take these from our inference function argument. And the second part is the uploaded image vector for Anton lavey, that 54 cosine distance We'll do everything but binarization copy these two lines and change hamming distance into cost and distance return closest ideas and we are done with our inference function. So far if you have any questions or comments, please post them in the comment section. Otherwise assume the next time