Spaces:
Runtime error
Runtime error
Delete clipseg/Quickstart.ipynb
Browse files- clipseg/Quickstart.ipynb +0 -107
clipseg/Quickstart.ipynb
DELETED
|
@@ -1,107 +0,0 @@
|
|
| 1 |
-
{
|
| 2 |
-
"cells": [
|
| 3 |
-
{
|
| 4 |
-
"cell_type": "code",
|
| 5 |
-
"execution_count": null,
|
| 6 |
-
"metadata": {},
|
| 7 |
-
"outputs": [],
|
| 8 |
-
"source": [
|
| 9 |
-
"import torch\n",
|
| 10 |
-
"import requests\n",
|
| 11 |
-
"\n",
|
| 12 |
-
"! wget https://owncloud.gwdg.de/index.php/s/ioHbRzFx6th32hn/download -O weights.zip\n",
|
| 13 |
-
"! unzip -d weights -j weights.zip\n",
|
| 14 |
-
"from models.clipseg import CLIPDensePredT\n",
|
| 15 |
-
"from PIL import Image\n",
|
| 16 |
-
"from torchvision import transforms\n",
|
| 17 |
-
"from matplotlib import pyplot as plt\n",
|
| 18 |
-
"\n",
|
| 19 |
-
"# load model\n",
|
| 20 |
-
"model = CLIPDensePredT(version='ViT-B/16', reduce_dim=64)\n",
|
| 21 |
-
"model.eval();\n",
|
| 22 |
-
"\n",
|
| 23 |
-
"# non-strict, because we only stored decoder weights (not CLIP weights)\n",
|
| 24 |
-
"model.load_state_dict(torch.load('weights/rd64-uni.pth', map_location=torch.device('cpu')), strict=False);"
|
| 25 |
-
]
|
| 26 |
-
},
|
| 27 |
-
{
|
| 28 |
-
"cell_type": "markdown",
|
| 29 |
-
"metadata": {},
|
| 30 |
-
"source": [
|
| 31 |
-
"Load and normalize `example_image.jpg`. You can also load through an URL."
|
| 32 |
-
]
|
| 33 |
-
},
|
| 34 |
-
{
|
| 35 |
-
"cell_type": "code",
|
| 36 |
-
"execution_count": null,
|
| 37 |
-
"metadata": {},
|
| 38 |
-
"outputs": [],
|
| 39 |
-
"source": [
|
| 40 |
-
"# load and normalize image\n",
|
| 41 |
-
"input_image = Image.open('example_image.jpg')\n",
|
| 42 |
-
"\n",
|
| 43 |
-
"# or load from URL...\n",
|
| 44 |
-
"# image_url = 'https://farm5.staticflickr.com/4141/4856248695_03475782dc_z.jpg'\n",
|
| 45 |
-
"# input_image = Image.open(requests.get(image_url, stream=True).raw)\n",
|
| 46 |
-
"\n",
|
| 47 |
-
"transform = transforms.Compose([\n",
|
| 48 |
-
" transforms.ToTensor(),\n",
|
| 49 |
-
" transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n",
|
| 50 |
-
" transforms.Resize((352, 352)),\n",
|
| 51 |
-
"])\n",
|
| 52 |
-
"img = transform(input_image).unsqueeze(0)"
|
| 53 |
-
]
|
| 54 |
-
},
|
| 55 |
-
{
|
| 56 |
-
"cell_type": "markdown",
|
| 57 |
-
"metadata": {},
|
| 58 |
-
"source": [
|
| 59 |
-
"Predict and visualize (this might take a few seconds if running without GPU support)"
|
| 60 |
-
]
|
| 61 |
-
},
|
| 62 |
-
{
|
| 63 |
-
"cell_type": "code",
|
| 64 |
-
"execution_count": null,
|
| 65 |
-
"metadata": {},
|
| 66 |
-
"outputs": [],
|
| 67 |
-
"source": [
|
| 68 |
-
"prompts = ['a glass', 'something to fill', 'wood', 'a jar']\n",
|
| 69 |
-
"\n",
|
| 70 |
-
"# predict\n",
|
| 71 |
-
"with torch.no_grad():\n",
|
| 72 |
-
" preds = model(img.repeat(4,1,1,1), prompts)[0]\n",
|
| 73 |
-
"\n",
|
| 74 |
-
"# visualize prediction\n",
|
| 75 |
-
"_, ax = plt.subplots(1, 5, figsize=(15, 4))\n",
|
| 76 |
-
"[a.axis('off') for a in ax.flatten()]\n",
|
| 77 |
-
"ax[0].imshow(input_image)\n",
|
| 78 |
-
"[ax[i+1].imshow(torch.sigmoid(preds[i][0])) for i in range(4)];\n",
|
| 79 |
-
"[ax[i+1].text(0, -15, prompts[i]) for i in range(4)];"
|
| 80 |
-
]
|
| 81 |
-
}
|
| 82 |
-
],
|
| 83 |
-
"metadata": {
|
| 84 |
-
"interpreter": {
|
| 85 |
-
"hash": "800ed241f7db2bd3aa6942aa3be6809cdb30ee6b0a9e773dfecfa9fef1f4c586"
|
| 86 |
-
},
|
| 87 |
-
"kernelspec": {
|
| 88 |
-
"display_name": "Python 3",
|
| 89 |
-
"language": "python",
|
| 90 |
-
"name": "python3"
|
| 91 |
-
},
|
| 92 |
-
"language_info": {
|
| 93 |
-
"codemirror_mode": {
|
| 94 |
-
"name": "ipython",
|
| 95 |
-
"version": 3
|
| 96 |
-
},
|
| 97 |
-
"file_extension": ".py",
|
| 98 |
-
"mimetype": "text/x-python",
|
| 99 |
-
"name": "python",
|
| 100 |
-
"nbconvert_exporter": "python",
|
| 101 |
-
"pygments_lexer": "ipython3",
|
| 102 |
-
"version": "3.8.10"
|
| 103 |
-
}
|
| 104 |
-
},
|
| 105 |
-
"nbformat": 4,
|
| 106 |
-
"nbformat_minor": 4
|
| 107 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|