Skip to content

Instantly share code, notes, and snippets.

@velikodniy
Created October 31, 2019 13:26
Show Gist options
  • Save velikodniy/a75b5f27ac6c87ef408a5645c9223ea3 to your computer and use it in GitHub Desktop.
Save velikodniy/a75b5f27ac6c87ef408a5645c9223ea3 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import torch.nn.functional as F"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"A = torch.tensor([\n",
" [[1, 1, 2, 2],\n",
" [1, 1, 2, 3],\n",
" [1, 1, 3, 3]]\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"B = torch.tensor([\n",
" [[1, 1, 2, 2],\n",
" [1, 1, 2, 2],\n",
" [1, 3, 3, 2]]\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"A_oh = F.one_hot(A)\n",
"B_oh = F.one_hot(B)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"int_AB = A_oh & B_oh\n",
"union_AB = A_oh | B_oh "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"iou = int_AB.sum(1).sum(1).type(torch.float32) / union_AB.sum(1).sum(1).type(torch.float32)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[0.8333, 0.6000, 0.2500]])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iou[:, 1:]"
]
},
{
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment