Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Ch14_Computer_Vision/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Ch14_Computer_Vision/.idea/Ch14_Computer_Vision.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Ch14_Computer_Vision/.idea/deployment.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Ch14_Computer_Vision/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Ch14_Computer_Vision/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Ch14_Computer_Vision/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 39 additions & 16 deletions Ch14_Computer_Vision/Single_Shot_Multibox_Detection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -640,12 +640,25 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"id_cat = dict()\n",
"id_cat[0] = 'pikachu'\n",
"id_cat[0] = 'pikachu'"
]
},
{
"metadata": {
"jupyter": {
"is_executing": true
}
},
"cell_type": "code",
"source": [
"\n",
"import torch\n",
"import torch.nn as nn\n",
"\n",
"class FocalLoss(nn.Module):\n",
" def __init__(self, alpha=0.25, gamma=2, device=\"cuda:0\", eps=1e-10):\n",
Expand All @@ -658,19 +671,29 @@
" def forward(self, input, target):\n",
" p = torch.sigmoid(input)\n",
" pt = p * target.float() + (1.0 - p) * (1 - target).float()\n",
" alpha_t = (1.0 - self.alpha) * target.float() + self.alpha * (1 - target).float()\n",
" loss = - 1.0 * torch.pow((1 - pt), self.gamma) * torch.log(pt + self.eps)\n",
" return loss.sum()\n",
" \n",
" alpha_t = self.alpha * target.float() + (1.0 - self.alpha) * (1 - target).float()\n",
" loss = - alpha_t * torch.pow((1 - pt), self.gamma) * torch.log(pt + self.eps)\n",
" return loss.sum()\n"
],
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": 21,
"source": [
"\n",
"class SSDLoss(nn.Module):\n",
" def __init__(self, loc_factor, jaccard_overlap, device = \"cuda:0\", **kwargs):\n",
" super().__init__()\n",
" self.fl = FocalLoss(**kwargs)\n",
" self.loc_factor = loc_factor\n",
" self.jaccard_overlap = jaccard_overlap\n",
" \n",
"\n",
" \n",
"\n",
"\n",
" self.device = device\n",
"\n",
" def one_hot_encoding(labels, num_classes):\n",
Expand All @@ -684,23 +707,23 @@
" torch.log((x[:, 3:4] / anchors[overlap_indicies, 3:4]))\n",
" ], dim=1)\n",
"\n",
" def forward(self, class_hat, bb_hat, class_true, bb_true, anchors): \n",
" def forward(self, class_hat, bb_hat, class_true, bb_true, anchors):\n",
" loc_loss = 0.0\n",
" class_loss = 0.0\n",
" \n",
" \n",
"\n",
"\n",
" for i in range(len(class_true)): # Batch level\n",
"\n",
" class_hat_i = class_hat[i, :, :]\n",
"\n",
" bb_true_i = bb_true[i].float()\n",
" \n",
" class_true_i = class_true[i] \n",
" \n",
"\n",
" class_true_i = class_true[i]\n",
"\n",
" class_target = torch.zeros(class_hat_i.shape[0]).long().to(self.device)\n",
" \n",
"\n",
" overlap_list = d2l.find_overlap(bb_true_i.squeeze(0), anchors, self.jaccard_overlap)\n",
" \n",
"\n",
" temp_loc_loss = 0.0\n",
" for j in range(len(overlap_list)): # BB level\n",
" overlap = overlap_list[j]\n",
Expand Down