This README will walk you through how to set up the APIs used by the Gen Studio website:
Follow these steps to build your own image similarity model.
Note: these steps assume you have the training images stored in an Azure Blob Storage as jpgs. We have also already normalized all these images to 512x512. We also assume familiarity with Jupyter Notebooks.
- Navigate to
api/ImageSimilarity/Model
- We will use the pre-trained ResNet50 model in Keras for feature extraction in each image. This will convert each 512x512 image into a numerical vector of length 2048. We used the images available on The MET’s Open Access API.
- Start Jupyter Notebooks and open
Featurize Images.ipynb
notebook. (You may need to pip install some of the python packages)- Under the Define Constants cell, define the url and file paths to the images you will use for the model & where you want the table containing the featurized version of the images saved.
- Run the remaining cells. These cells will download each image, run them through the ResNet50 model and then save 3 files:
- preprocessedimages.pkl: Table containing the featurized image vectors.
- targets.pkl: A list of the
[name, url]
for each corresponding image inpreprocessedimages.pkl
- failed.pkl: list of the index in targets of any images we failed to featurize.
- total_i.pkl: decimal count of the total number of images analyzed
- Shutdown and close the
Featurize Images.ipynb
notebook.- Next, we build a nearest neighbors model using the vector for each image you created in the previous step. This model will be used to search for the closest visually similar image.
- Open the
Build Nearest Neighbors.ipynb
notebook- Under the Define Constants cell, define the file path where the annoy model will be saved & path to the featurized images.
- Run the remaining cells. These cells will build, train and save the annoy index.
### You have now built your nearest neighbors index!
Follow these steps to create a gpu enabled docker container for an image similarity search API. The API calls are defined in api/ImageSimilarity/deployment/app.py
Navigate to api/ImageSimilarity/deployment
Copy your targets.pkl
file and Annoy Index (annoyIndex2.ann
) which were created in the prior step into the folder
If you changed the name for either of these files, you will need to update line 25 of Dockerfile_gpu
.
Run the following command to build the docker container. If you are not using a GPU machine, use Dockerfile
instead.
docker build -t <tagname> -f Dockerfile_gpu
To test your API, start the docker on port 5000. You can now call this API at localhost:5000
nvidia-docker run -p 5000:5000 imagesimilaritymodel
Tag and publish your container
docker tag <container name> <dockerhub username>/<container name>
docker push <dockerhub username>/<container name>
Follow these steps to build your own API which generates images from proGAN.
api/BigGAN/deployment
Follow these steps to build your own API which generates images from proGAN.
api/ProGAN/deployment
These steps will walk through how to use ASK to deploy the Flask APIs.
install the Azure CLI. The Azure CLI is a command-line tool for managing Azure resources.
az group create --name myResourceGroup --location eastus
az aks create \
--resource-group myResourceGroup \
--name myAKSCluster \
--node-count 1 \
--enable-addons monitoring \
--generate-ssh-keys
kubectl
is already installed. To install it locally, use the az aks install-cli command:
az aks install-cli
kubectl
to connect to your AKS cluster with the az aks get-credentials command:
az aks get-credentials --resource-group myResourceGroup --name myAKSCluster
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
aks-nodepool1-28993262-0 Ready agent 3m18s v1.9.11
To deploy the image similarity API, navigate to api/ImageSimilarity/
kubectl apply -f BigGAN.yaml
Open your Kubernetes cluster UI with the following command
az aks browse -g myResourceGroup -n myAKSCluster