Package: jdarray
Cloud-Native Neural Search?Framework forAnyKind of DataJina is a neural search framework that empowers anyone to build SOTA and scalable deep learning search applications in minutes.⏱️Save time-Thedesign pattern of neural search systems. Native support for PyTorch/Keras/ONNX/Paddle. Build solutions in just minutes.🌌All data types- Process, index, query, and understand videos, images, long/short text, audio, source code, PDFs, etc.🌩️Local & cloud friendly- Distributed architecture, scalable & cloud-native from day one. Same developer experience on both local and cloud.🍱Own your stack- Keep end-to-end stack ownership of your solution. Avoid integration pitfalls you get with
fragmented, multi-vendor, generic legacy tools.Installpip install -U jinaMore install options including Conda, Docker, Windowscan be found here.DocumentationGet StartedWe promise you can build a scalable ResNet-powered image search service in 20 minutes or less, from scratch. If not, you can forget about Jina.Basic ConceptsDocument, Executor, and Flow are three fundamental concepts in Jina.Documentis the basic data type in Jina;Executoris how Jina processes Documents;Flowis how Jina streamlines and distributes Executors.Leveraging these three components, let's build an app thatfind similar images using ResNet50.ResNet50 Image Search in 20 Lines💡 Preliminaries:download dataset,install PyTorch & TorchvisionfromjinaimportDocumentArray,Documentdefpreproc(d:Document):return(d.load_uri_to_image_blob()# load.set_image_blob_normalization()# normalize color.set_image_blob_channel_axis(-1,0))# switch color axisdocs=DocumentArray.from_files('img/*.jpg').apply(preproc)importtorchvisionmodel=torchvision.models.resnet50(pretrained=True)# load ResNet50docs.embed(model,device='cuda')# embed via GPU to speedupq=(Document(uri='img/00021.jpg')# build query image & preprocess.load_uri_to_image_blob().set_image_blob_normalization().set_image_blob_channel_axis(-1,0))q.embed(model)# embedq.match(docs)# find top-20 nearest neighbours, done!Done! Now printq.matchesand you'll see the URIs of the most similar images.Add three lines of code to visualize them:forminq.matches:m.set_image_blob_channel_axis(0,-1).set_image_blob_inv_normalization()q.matches.plot_image_sprites()Sweet! FYI, you can use Keras, ONNX, or PaddlePaddle for the embedding model. Jina supports them well.As-a-Service in 10 Extra LinesWith an extremely trivial refactoring and ten extra lines of code, you can make the local script a ready-to-serve service:Import what we need.fromjinaimportDocument,DocumentArray,Executor,Flow,requestsCopy-paste the preprocessing step and wrap it viaExecutor:classPreprocImg(Executor):@requestsdeffoo(self,docs:DocumentArray,**kwargs):fordindocs:(d.load_uri_to_image_blob()# load.set_image_blob_normalization()# normalize color.set_image_blob_channel_axis(-1,0))# switch color axisCopy-paste the embedding step and wrap it viaExecutor:classEmbedImg(Executor):def__init__(self,**kwargs):super().__init__(**kwargs)importtorchvisionself.model=torchvision.models.resnet50(pretrained=True)@requestsdeffoo(self,docs:DocumentArray,**kwargs):docs.embed(self.model)Wrap the matching step into anExecutor:classMatchImg(Executor):_da=DocumentArray()@requests(on='/index')defindex(self,docs:DocumentArray,**kwargs):self._da.extend(docs)@requests(on='/search')deffoo(self,docs:DocumentArray,**kwargs):docs.match(self._da)fordindocs.traverse_flat('r,m'):# only require for visualizationd.convert_uri_to_datauri()# convert to dataurid.pop('embedding','blob')# remove unnecessary fields for save bandwidthConnect allExecutors in aFlow, scale embedding to 3:f=Flow(port_expose=12345,protocol='http').add(uses=PreprocImg).add(uses=EmbedImg,replicas=3).add(uses=MatchImg)Plot it viaf.plot('flow.svg')and you get:Index image data and serve REST query publicly:withf:f.post('/index',DocumentArray.from_files('img/*.jpg'),show_progress=True,request_size=8)f.block()Done! Now query it viacurland you get the most similar images:Or go tohttp://0.0.0.0:12345/docsand test requests via a Swagger UI:Or use a Python client to access the service:fromjinaimportClient,Documentfromjina.types.requestimportResponsedefprint_matches(resp:Response):# the callback function invoked when task is doneforidx,dinenumerate(resp.docs[0].matches):# print top-3 matchesprint(f'[{idx}]{d.scores["cosine"].value:2f}: "{d.uri}"')c=Client(protocol='http',port=12345)# connect to localhost:12345c.post('/search',Document(uri='img/00021.jpg'),on_done=print_matches)At this point, you probably have taken 15 minutes but here we are: an image search service with rich features:✅ Solution as microservices✅ Scale in/out any component✅ Query via HTTP/WebSocket/gRPC/Client✅ Distribute/Dockerize components✅ Async/non-blocking I/O✅ Extendable REST interfaceDeploy to Kubernetes in 7 MinutesHave another seven minutes? We'll show you how to bring your service to the next level by deploying it to Kubernetes.Create a Kubernetes cluster and get credentials (example in GCP,more K8s providers here):gcloudcontainerclusterscreatetest--machine-typee2-highmem-2--num-nodes1--zoneeurope-west3-a
gcloudcontainerclustersget-credentialstest--zoneeurope-west3-a--projectjina-showcaseMove eachExecutorclass to a separate folder with one Python file in each:PreprocImg-> 📁preproc_img/exec.pyEmbedImg-> 📁embed_img/exec.pyMatchImg-> 📁match_img/exec.pyPush all Executors toJina Hub:jinahubpushpreproc_img
jinahubpushembed_img
jinahubpushembed_imgYou will get three Hub Executors that can be used via Docker container.AdjustFlowa bit and open it:f=Flow(name='readme-flow',port_expose=12345,infrastructure='k8s').add(uses='jinahub+docker://PreprocImg').add(uses='jinahub+docker://EmbedImg',replicas=3).add(uses='jinahub+docker://MatchImg')withf:f.block()Intrigued?Find more about Jina from our docs.Run Quick Demo👗 Fashion image search:jina hello fashion🤖 QA chatbot:pip install "jina[demo]" && jina hello chatbot📰 Multimodal search:pip install "jina[demo]" && jina hello multimodal🍴 Fork the source of a demo to your folder:jina hello fork fashion ../my-proj/SupportJoin ourSlack communityto chat to our engineers about your use cases, questions, and
support queries.Join ourEngineering All Handsmeet-up to
discuss your use case and learn Jina's new features.When?The second Tuesday of every monthWhere?Zoom (see our public calendar/.ical/Meetup
group)
andlive stream on YouTubeSubscribe to the latest video tutorials on ourYouTube channelJoin UsJina is backed byJina AIand licensed underApache-2.0.We are actively hiringAI engineers, solution engineers to build the next neural search
ecosystem in open source.ContributingWe welcome all kinds of contributions from the open-source community, individuals and partners. We owe our success to
your active involvement.Release cycles and development stagesContributing guidelinesCode of conduct