Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from torchvision import transforms
|
| 4 |
+
from transformers import CLIPProcessor, CLIPModel
|
| 5 |
+
|
| 6 |
+
# load the CLIP model
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
| 9 |
+
model.to(device)
|
| 10 |
+
|
| 11 |
+
# load the CLIP processor
|
| 12 |
+
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 13 |
+
|
| 14 |
+
# load the image
|
| 15 |
+
image_path = "path/to/image.jpg"
|
| 16 |
+
image = Image.open(image_path)
|
| 17 |
+
|
| 18 |
+
# resize the image
|
| 19 |
+
resize = transforms.Resize((224, 224))
|
| 20 |
+
image = resize(image)
|
| 21 |
+
|
| 22 |
+
# convert the image to a tensor
|
| 23 |
+
tensor = transforms.ToTensor()(image)
|
| 24 |
+
tensor = tensor.to(device)
|
| 25 |
+
|
| 26 |
+
# get the image features using the CLIP model
|
| 27 |
+
with torch.no_grad():
|
| 28 |
+
features = model.encode_image(tensor.unsqueeze(0))
|
| 29 |
+
|
| 30 |
+
# generate variations of the image using the CLIP model and processor
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
outputs = model.generate_images(
|
| 33 |
+
features=features,
|
| 34 |
+
num_images=5, # number of different variations to generate
|
| 35 |
+
max_length=50, # maximum length of the generated caption for the variation
|
| 36 |
+
clip=processor,
|
| 37 |
+
temperature=1.0, # temperature of the sampling process
|
| 38 |
+
top_p=0.9, # top-p probability for the sampling process
|
| 39 |
+
batch_size=1,
|
| 40 |
+
device=device,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# save the generated images
|
| 44 |
+
for i, output in enumerate(outputs):
|
| 45 |
+
generated_image = transforms.functional.to_pil_image(output[0])
|
| 46 |
+
generated_image.save(f"output/image_variation_{i}.jpg")
|