Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAM support #915

Open
Tracked by #1126
mudler opened this issue Aug 17, 2023 · 7 comments · May be fixed by #1246
Open
Tracked by #1126

SAM support #915

mudler opened this issue Aug 17, 2023 · 7 comments · May be fixed by #1246

Comments

@mudler
Copy link
Owner

mudler commented Aug 17, 2023

This is a tracker for adding https://github.com/facebookresearch/segment-anything as a backend for a new LocalAI endpoint returning image masks

@mudler mudler added the enhancement New feature or request label Aug 17, 2023
@mudler mudler self-assigned this Aug 17, 2023
@localai-bot
Copy link
Contributor

⚠️⚠️⚠️⚠️⚠️

Hi! I'm a bot running with LocalAI ( a crazy experiment of @mudler ) - please beware that I might hallucinate sometimes!

but.... I can also be funny or helpful 😸 and I can provide generally speaking good tips or places where to look after in the documentation or in the code based on what you wrote in the issue.

Don't engage in conversation with me,
I don't support (yet) replying!

⚠️⚠️⚠️⚠️⚠️

To add the Segment Anything backend to LocalAI, you can follow these steps:

  1. Install the Segment Anything library using pip or conda.
  2. Add the required dependencies to your LocalAI installation by running the following command in your terminal:
pip install tensorflow==3.9.0-rc1

This will install TensorFlow 3.9.0-rc1, which is a requirement for Segment Anything.
3. Clone the Segment Anything repository and add it to your LocalAI installation by running the following command in your terminal:

git clone https://github.com/facebookresearch/segment-anything.git
cd segment-anything
pip install --target /usr/local/lib/python3.8/site-packages .

This will install Segment Anything in the LocalAI installation directory and add it to your Python environment.
4. Add the following code to your LocalAI model to use the Segment Anthing backend:

import tensorflow as tf
from tensorflow import keras
from keras import layers
from keras.preprocessing import image
from keras_segment_anything import SegmentAnything
# Load the pre-trained model
model = tf.keras.models.load_model('path/to/model')
# Create a Segment Anything object with the model
sa = Segment_Anything(model)
# Define the image to segment
image = image.load_img('path/to/image', target_size=(256, 256))
image = tf.keras.preprocessing.image.augmentation.random_rotate(image, 40)
mask = sa.segment_anything(image)
# Display the mask
plt.imshow(mask)
plt.axis('off')

This code will use the Segment Anthing backend to generate a binary mask of the image. You can modify it to fit your specific use case.
5. Test the new endpoint by calling it in your code and passing an image path as input:

import localai
localai.api_key = 'YOUR_API_KEY'
localai.use_gpu = True
sa = localai.LocalAI('segment-anything')
image_path = 'path/to/image'
mask = sa.segment_anything(image_path)
# Display the mask
plt.imshow(mask)
plt.axis('off')

This code will use the Segment Anything backend to generate a binary mask of the image at the specified path. You can modify it to fit your specific use case.

Sources:

@mudler
Copy link
Owner Author

mudler commented Sep 6, 2023

@Aisuko
Copy link
Collaborator

Aisuko commented Oct 11, 2023

It means we have two more new cpp based backend are need to implement:

  • llama
  • sam

@mudler
Copy link
Owner Author

mudler commented Oct 11, 2023

just for reference this ticket's intention is for https://github.com/YavorGIvanov/sam.cpp and/or https://github.com/facebookresearch/segment-anything

@Aisuko Aisuko self-assigned this Nov 5, 2023
@Aisuko
Copy link
Collaborator

Aisuko commented Nov 5, 2023

I will try https://github.com/facebookresearch/segment-anything

@Aisuko Aisuko linked a pull request Nov 5, 2023 that will close this issue
1 task
@fishfree
Copy link

Maybe https://github.com/pytorch-labs/segment-anything-fast is a better choice.

@jtwolfe
Copy link
Contributor

jtwolfe commented Apr 16, 2024

clapping yes SAM please <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants