Last active
January 27, 2021 09:34
-
-
Save ggosiang/c8c72d9da9f5ec83a3d49bd189acba32 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"! git clone https://github.com/openai/CLIP.git" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"! pip install ftfy regex tqdm" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import os\n", | |
"os.chdir('CLIP')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import torch\n", | |
"import clip\n", | |
"from PIL import Image\n", | |
"from IPython.display import Image as display_image\n", | |
"\n", | |
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", | |
"model, preprocess = clip.load(\"ViT-B/32\", device=device)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def predict(image_file, class_text):\n", | |
" image = preprocess(Image.open(image_file)).unsqueeze(0).to(device)\n", | |
" text = clip.tokenize(class_text).to(device)\n", | |
"\n", | |
" with torch.no_grad():\n", | |
" image_features = model.encode_image(image)\n", | |
" text_features = model.encode_text(text)\n", | |
"\n", | |
" logits_per_image, logits_per_text = model(image, text)\n", | |
" probs = logits_per_image.softmax(dim=-1).cpu().numpy()\n", | |
"\n", | |
" pil_img = display_image(filename=image_file)\n", | |
" display(pil_img)\n", | |
" \n", | |
" for i in range(len(class_text)):\n", | |
" print(f\"{class_text[i]}: {probs[0][i]}\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"image_file = '../puipui-1.png'\n", | |
"class_text = [\"traffic jam\", \"traffic smooth\", \"emergency\"]\n", | |
"predict(image_file, class_text)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"image_file = '../puipui-2.png'\n", | |
"class_text = [\"traffic jam\", \"traffic smooth\", \"emergency\"]\n", | |
"predict(image_file, class_text)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"image_file = '../puipui-3.png'\n", | |
"class_text = [\"disappointment\", \"hesitation\", \"expectation\"]\n", | |
"predict(image_file, class_text)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"image_file = '../puipui-4.png'\n", | |
"class_text = [\"spring\", \"summer\", \"fall\", \"winter\"]\n", | |
"predict(image_file, class_text)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment