Package: img2vec-pytorch
Image 2 Vec with PyTorchMedium post on building the first version from scratch:https://becominghuman.ai/extract-a-feature-vector-for-any-image-with-pytorch-9717561d1d4cLooking for a simpler image vector integration for your project?  Check out our free API athttps://latentvector.spaceApplications of image embeddings:Ranking for recommender systemsClustering images to different categoriesClassification tasksImage compressionAvailable modelsModel nameReturn vector lengthResnet-18512Alexnet4096Vgg-114096Densenet1024efficientnet_b01280efficientnet_b11280efficientnet_b21408efficientnet_b31536efficientnet_b41792efficientnet_b52048efficientnet_b62304efficientnet_b72560InstallationTested on Python 3.6 and torchvision 0.11.0 (nightly, 2021-09-25)Requires Pytorch:http://pytorch.org/conda install -c pytorch-nightly torchvisionpip install img2vec_pytorchRun testpython -m img2vec_pytorch.test_img_to_vecUsing img2vec as a libraryfromimg2vec_pytorchimportImg2VecfromPILimportImage# Initialize Img2Vec with GPUimg2vec=Img2Vec(cuda=True)# Read in an image (rgb format)img=Image.open('test.jpg')# Get a vector from img2vec, returned as a torch FloatTensorvec=img2vec.get_vec(img,tensor=True)# Or submit a listvectors=img2vec.get_vec(list_of_PIL_images)For running the example, you will additionally need:Pillow:pip install PillowSklearnpip install scikit-learnRunning the examplegit clone https://github.com/christiansafka/img2vec.gitcd img2vec/examplepython test_img_similarity.pyExpected outputWhich filename would you like similarities for?
cat.jpg
0.72832 cat2.jpg
0.641478 catdog.jpg
0.575845 face.jpg
0.516689 face2.jpg

Which filename would you like similarities for?
face2.jpg
0.668525 face.jpg
0.516689 cat.jpg
0.50084 cat2.jpg
0.484863 catdog.jpgTry adding your own photos!Img2Vec Paramscuda= (True, False)   # Run on GPU?     default: Falsemodel= ('resnet-18', 'alexnet', 'vgg', 'densenet')   # Which model to use?     default: 'resnet-18'Advanced usersRead only file systemsIf you use this library from the app running in read only environment (for example, docker container),
specify writable directory where app can store pre-trained models.exportTORCH_HOME=/tmp/torchAdditional Parameterslayer= 'layer_name' or int   # For advanced users, which layer of the model to extract the output from.   default: 'avgpool'layer_output_size= int   # Size of the output of your selected layerResnet-18Defaults: (layer = 'avgpool', layer_output_size = 512)Layer parameter must be an string representing the name of a  layer belowconv1=nn.Conv2d(3,64,kernel_size=7,stride=2,padding=3,bias=False)bn1=nn.BatchNorm2d(64)relu=nn.ReLU(inplace=True)maxpool=nn.MaxPool2d(kernel_size=3,stride=2,padding=1)layer1=self._make_layer(block,64,layers[0])layer2=self._make_layer(block,128,layers[1],stride=2)layer3=self._make_layer(block,256,layers[2],stride=2)layer4=self._make_layer(block,512,layers[3],stride=2)avgpool=nn.AvgPool2d(7)fc=nn.Linear(512*block.expansion,num_classes)AlexnetDefaults: (layer = 2, layer_output_size = 4096)Layer parameter must be an integer representing one of the layers belowalexnet.classifier=nn.Sequential(7.nn.Dropout(),<-output_size=92166.nn.Linear(256*6*6,4096),<-output_size=40965.nn.ReLU(inplace=True),<-output_size=40964.nn.Dropout(),<-output_size=40963.nn.Linear(4096,4096),<-output_size=40962.nn.ReLU(inplace=True),<-output_size=40961.nn.Linear(4096,num_classes),<-output_size=4096)VggDefaults: (layer = 2, layer_output_size = 4096)vgg.classifier=nn.Sequential(nn.Linear(512*7*7,4096),nn.ReLU(True),nn.Dropout(),nn.Linear(4096,4096),nn.ReLU(True),nn.Dropout(),nn.Linear(4096,num_classes),)DensenetDefaults: (layer = 1 from features, layer_output_size = 1024)densenet.features=nn.Sequential(OrderedDict([('conv0',nn.Conv2d(3,num_init_features,kernel_size=7,stride=2,padding=3,bias=False)),('norm0',nn.BatchNorm2d(num_init_features)),('relu0',nn.ReLU(inplace=True)),('pool0',nn.MaxPool2d(kernel_size=3,stride=2,padding=1)),]))EfficientNetDefaults: (layer = 1 from features, layer_output_size = 1280 for efficientnet_b0 model)To-doBenchmark speed and accuracyAdd ability to fine-tune on input dataExport documentation to a normal place