Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
Issue #39: fixed random cropping
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Rykov committed Mar 7, 2017
1 parent e57abbe commit d520f37
Showing 1 changed file with 45 additions and 46 deletions.
91 changes: 45 additions & 46 deletions SSD_training.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -180,51 +180,50 @@
" img_w = img.shape[1]\n",
" img_h = img.shape[0]\n",
" img_area = img_w * img_h\n",
" for i in range(self.crop_attempts):\n",
" random_scale = np.random.random()\n",
" random_scale *= (self.crop_area_range[1] -\n",
" self.crop_area_range[0])\n",
" random_scale += self.crop_area_range[0]\n",
" target_area = random_scale * img_area\n",
" random_ratio = np.random.random()\n",
" random_ratio *= (self.aspect_ratio_range[1] -\n",
" self.aspect_ratio_range[0])\n",
" random_ratio += self.aspect_ratio_range[0]\n",
" w = np.round(np.sqrt(target_area * random_ratio)) \n",
" h = np.round(np.sqrt(target_area / random_ratio)) \n",
" if np.random.random() < 0.5:\n",
" w, h = h, w\n",
" w = min(w, img_w)\n",
" w_rel = w / img_w\n",
" w = int(w)\n",
" h = min(h, img_w)\n",
" h_rel = h / img_h\n",
" h = int(h)\n",
" x = np.random.random() * (img_w - w)\n",
" x_rel = x / img_w\n",
" x = int(x)\n",
" y = np.random.random() * (img_h - h)\n",
" y_rel = y / img_h\n",
" y = int(y)\n",
" img = img[y:y+h, x:x+w]\n",
" new_targets = []\n",
" for box in targets:\n",
" cx = 0.5 * (box[0] + box[2])\n",
" cy = 0.5 * (box[1] + box[3])\n",
" if (x_rel < cx < x_rel + w_rel and\n",
" y_rel < cy < y_rel + h_rel):\n",
" xmin = (box[0] - x) / w_rel\n",
" ymin = (box[1] - y) / h_rel\n",
" xmax = (box[2] - x) / w_rel\n",
" ymax = (box[3] - y) / h_rel\n",
" xmin = max(0, xmin)\n",
" ymin = max(0, ymin)\n",
" xmax = min(1, xmax)\n",
" ymax = min(1, ymax)\n",
" box[:4] = [xmin, ymin, xmax, ymax]\n",
" new_targets.append(box)\n",
" new_targets = np.asarray(new_targets).reshape(-1, targets.shape[1])\n",
" return img, new_targets\n",
" random_scale = np.random.random()\n",
" random_scale *= (self.crop_area_range[1] -\n",
" self.crop_area_range[0])\n",
" random_scale += self.crop_area_range[0]\n",
" target_area = random_scale * img_area\n",
" random_ratio = np.random.random()\n",
" random_ratio *= (self.aspect_ratio_range[1] -\n",
" self.aspect_ratio_range[0])\n",
" random_ratio += self.aspect_ratio_range[0]\n",
" w = np.round(np.sqrt(target_area * random_ratio)) \n",
" h = np.round(np.sqrt(target_area / random_ratio))\n",
" if np.random.random() < 0.5:\n",
" w, h = h, w\n",
" w = min(w, img_w)\n",
" w_rel = w / img_w\n",
" w = int(w)\n",
" h = min(h, img_h)\n",
" h_rel = h / img_h\n",
" h = int(h)\n",
" x = np.random.random() * (img_w - w)\n",
" x_rel = x / img_w\n",
" x = int(x)\n",
" y = np.random.random() * (img_h - h)\n",
" y_rel = y / img_h\n",
" y = int(y)\n",
" img = img[y:y+h, x:x+w]\n",
" new_targets = []\n",
" for box in targets:\n",
" cx = 0.5 * (box[0] + box[2])\n",
" cy = 0.5 * (box[1] + box[3])\n",
" if (x_rel < cx < x_rel + w_rel and\n",
" y_rel < cy < y_rel + h_rel):\n",
" xmin = (box[0] - x_rel) / w_rel\n",
" ymin = (box[1] - y_rel) / h_rel\n",
" xmax = (box[2] - x_rel) / w_rel\n",
" ymax = (box[3] - y_rel) / h_rel\n",
" xmin = max(0, xmin)\n",
" ymin = max(0, ymin)\n",
" xmax = min(1, xmax)\n",
" ymax = min(1, ymax)\n",
" box[:4] = [xmin, ymin, xmax, ymax]\n",
" new_targets.append(box)\n",
" new_targets = np.asarray(new_targets).reshape(-1, targets.shape[1])\n",
" return img, new_targets\n",
" \n",
" def generate(self, train=True):\n",
" while True:\n",
Expand Down Expand Up @@ -531,7 +530,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
"version": "2.7.13"
}
},
"nbformat": 4,
Expand Down

0 comments on commit d520f37

Please sign in to comment.