R-CNN/SPP-Net
R-CNN
- 2013
- region proposals with CNNs
- selective search
- extract region proposals(~2000개)
- Supervised pre-training : Caffe CNN library
- Domain-specific fine-tuning : VOC, N=20 / ILSVRC2013, N=200
SPP-net
- 2014
- spatial pyramid pooing
- convolutional features -> R-CNN : 2000번 / SPP-net : 1번 (R-CNN 보다 24~102배 더 빠름)
- image -> crop/wrap 을 안한다.
- image -> conv layers -> spatial pyramid pooing
- spatial pyramid pooing : 예) 1+4+16=21개
- SPP은 항상 고정된 사이즈가 나온다.
- Bag-of-words => spatial pyramid Matching(SPM) ==> SPP 로 확장(크기고정)
# %pip install -U selective-search
import selective_search
dir(selective_search)
['__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__path__',
'__spec__',
'box_filter',
'core',
'measure',
'selective_search',
'structure',
'util']
from skimage.data import astronaut
data = astronaut()
import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
plt.imshow(data)
<matplotlib.image.AxesImage at 0x16c5defa0>
# %pip install -U mglearn
import mglearn
# selective_search 는 plot_agglomerative_algorithm 와 비슷
mglearn.plot_agglomerative.plot_agglomerative_algorithm()
selective_search.selective_search(data)
[(0, 0, 512, 512),
(290, 367, 491, 512),
(221, 180, 282, 249),
(221, 217, 247, 246),
(435, 122, 460, 274),
(323, 206, 386, 264),
(389, 19, 417, 85),
(267, 27, 307, 68),
(175, 194, 195, 243),
(148, 425, 238, 462),
(41, 75, 137, 218),
(160, 427, 242, 512),
(360, 237, 500, 458),
(240, 308, 291, 512),
(240, 308, 277, 512),
(382, 0, 436, 186),
(393, 0, 436, 186),
(400, 0, 424, 80),
(110, 241, 223, 417),
(123, 260, 223, 417),
(360, 276, 375, 348),
(244, 308, 277, 512),
(242, 180, 282, 249),
(411, 29, 423, 80),
(185, 144, 238, 182),
(156, 100, 184, 148),
(170, 427, 242, 512),
(0, 0, 95, 512),
(0, 0, 64, 160),
(0, 118, 95, 512),
(10, 0, 37, 129),
(20, 20, 37, 129),
(364, 237, 500, 458),
(367, 237, 453, 360),
(367, 278, 453, 360),
(338, 417, 378, 456),
(0, 118, 95, 419),
(0, 118, 95, 370),
(0, 118, 95, 275),
(0, 147, 77, 275),
(370, 275, 512, 484),
(17, 0, 512, 512),
(396, 0, 512, 285),
(41, 75, 102, 210),
(133, 348, 208, 424),
(49, 420, 152, 509),
(49, 420, 152, 499),
(49, 445, 152, 499),
(113, 126, 304, 319),
(113, 132, 304, 319),
(393, 237, 446, 292),
(403, 237, 446, 292),
(30, 0, 74, 86),
(278, 342, 512, 512),
(278, 352, 512, 512),
(352, 352, 512, 512),
(0, 226, 38, 370),
(113, 132, 304, 277),
(125, 132, 304, 277),
(133, 164, 304, 277),
(133, 215, 250, 273),
(409, 0, 512, 285),
(280, 342, 328, 388),
(13, 118, 95, 237),
(411, 117, 432, 181),
(244, 322, 275, 512),
(0, 300, 23, 411),
(409, 19, 512, 285),
(10, 147, 77, 275),
(367, 290, 453, 360),
(0, 478, 26, 512),
(17, 0, 420, 512),
(17, 199, 240, 512),
(174, 278, 240, 441),
(453, 52, 486, 103),
(393, 0, 436, 185),
(402, 0, 436, 185),
(116, 126, 175, 167),
(364, 357, 512, 512),
(52, 0, 420, 512),
(52, 0, 405, 269),
(52, 0, 405, 251),
(172, 77, 294, 191),
(172, 77, 260, 177),
(174, 390, 238, 441),
(364, 344, 500, 458),
(244, 334, 275, 512),
(52, 0, 396, 251),
(191, 0, 396, 251),
(257, 0, 396, 251),
(17, 199, 237, 512),
(21, 329, 125, 424),
(414, 19, 512, 285),
(24, 431, 45, 482),
(348, 43, 405, 243),
(348, 100, 405, 243),
(52, 0, 303, 169),
(69, 420, 132, 465),
(0, 226, 38, 334),
(10, 147, 72, 257),
(0, 147, 19, 237),
(420, 19, 512, 285),
(157, 43, 303, 158),
(157, 43, 303, 130),
(250, 43, 303, 130),
(191, 0, 279, 31),
(26, 329, 125, 424),
(26, 329, 125, 409),
(17, 205, 237, 512),
(0, 226, 34, 329),
(52, 0, 286, 169),
(26, 452, 166, 512),
(26, 452, 91, 512),
(191, 248, 256, 264),
(0, 226, 34, 323),
(136, 226, 301, 319),
(172, 362, 200, 390),
(212, 218, 420, 512),
(340, 218, 420, 331),
(340, 240, 420, 331),
(340, 254, 420, 331),
(348, 104, 405, 243),
(133, 153, 295, 269),
(133, 153, 289, 269),
(206, 217, 289, 269),
(358, 104, 405, 243),
(212, 234, 382, 512),
(52, 0, 227, 169),
(52, 0, 216, 169),
(52, 0, 216, 155),
(424, 19, 512, 285),
(301, 178, 349, 238),
(317, 183, 347, 238),
(442, 19, 512, 282),
(125, 132, 184, 227),
(258, 217, 289, 268),
(230, 0, 279, 22),
(136, 230, 301, 319),
(20, 223, 90, 306),
(13, 119, 81, 237),
(278, 354, 369, 512),
(280, 342, 322, 388),
(449, 358, 500, 458),
(449, 358, 489, 405),
(409, 180, 424, 222),
(54, 0, 116, 30),
(133, 153, 262, 260),
(133, 153, 249, 255),
(92, 141, 136, 169),
(225, 234, 382, 512),
(259, 217, 289, 268),
(0, 0, 40, 160),
(30, 177, 72, 257),
(272, 349, 313, 512),
(133, 168, 227, 255),
(220, 88, 270, 158),
(17, 205, 237, 481),
(29, 205, 237, 481),
(29, 205, 211, 469),
(56, 205, 196, 344),
(225, 328, 270, 512),
(225, 336, 270, 512),
(239, 336, 270, 512),
(136, 230, 301, 302),
(408, 421, 463, 484),
(157, 51, 280, 115),
(230, 234, 382, 372),
(250, 234, 382, 372),
(254, 234, 382, 372),
(258, 234, 382, 372),
(258, 237, 382, 372),
(277, 237, 382, 372),
(257, 0, 396, 231),
(156, 153, 249, 246),
(301, 178, 349, 220),
(147, 188, 227, 255),
(147, 188, 202, 255),
(456, 19, 512, 272),
(458, 19, 512, 126),
(311, 0, 370, 33),
(99, 389, 237, 481),
(99, 389, 161, 481),
(157, 55, 263, 115),
(364, 357, 491, 488),
(133, 168, 180, 203),
(157, 66, 189, 115),
(75, 459, 122, 497),
(22, 11, 64, 121),
(458, 19, 512, 57),
(220, 88, 270, 150),
(220, 101, 266, 150),
(257, 154, 325, 231),
(364, 357, 491, 466),
(395, 357, 491, 466),
(279, 237, 382, 372),
(102, 483, 175, 512),
(116, 139, 153, 167),
(377, 104, 405, 243),
(478, 454, 512, 512),
(478, 466, 512, 512),
(275, 388, 313, 512),
(188, 55, 263, 86),
(414, 114, 456, 269),
(136, 230, 301, 300),
(92, 141, 136, 164),
(246, 404, 270, 512),
(54, 0, 103, 26),
(75, 0, 103, 26),
(278, 387, 322, 512),
(278, 387, 322, 503),
(13, 119, 81, 235),
(2, 239, 34, 323),
(404, 407, 480, 488),
(5, 239, 34, 301),
(409, 407, 480, 486),
(26, 329, 120, 409),
(212, 468, 260, 512),
(456, 96, 512, 272),
(256, 338, 275, 512),
(113, 180, 139, 244),
(133, 348, 195, 424),
(133, 348, 185, 424),
(468, 37, 512, 126),
(260, 0, 396, 211),
(437, 27, 462, 205),
(281, 182, 325, 231),
(29, 268, 211, 469),
(456, 96, 512, 265),
(29, 268, 128, 375),
(29, 268, 126, 375),
(136, 230, 261, 300),
(304, 354, 369, 406),
(433, 407, 480, 486),
(180, 191, 249, 246),
(180, 196, 246, 246),
(188, 61, 263, 86),
(156, 153, 243, 219),
(370, 275, 512, 482),
(279, 387, 322, 488),
(424, 421, 463, 484),
(44, 268, 126, 375),
(169, 254, 261, 300),
(453, 52, 482, 96),
(492, 473, 512, 512),
(442, 246, 512, 282),
(433, 407, 471, 446),
(364, 344, 464, 385),
(290, 387, 322, 479),
(370, 290, 458, 357),
(242, 186, 263, 249),
(224, 502, 256, 512),
(283, 499, 310, 512),
(5, 499, 26, 512),
(121, 491, 175, 512),
(247, 490, 257, 510),
(492, 488, 512, 512),
(62, 484, 73, 509),
(102, 483, 158, 512),
(212, 481, 250, 512),
(79, 480, 166, 512),
(71, 480, 112, 509),
(160, 478, 182, 499),
(0, 478, 15, 512),
(278, 474, 295, 503),
(75, 474, 107, 497),
(492, 473, 507, 504),
(215, 471, 228, 502),
(243, 468, 260, 501),
(233, 467, 248, 494),
(478, 466, 500, 512),
(256, 466, 270, 512),
(225, 466, 240, 496),
(156, 465, 193, 512),
(160, 463, 196, 512),
(492, 462, 512, 508),
(31, 460, 44, 489),
(96, 459, 122, 481),
(275, 458, 287, 512),
(404, 456, 445, 488),
(49, 456, 91, 494),
(416, 455, 436, 473),
(30, 455, 91, 512),
(480, 454, 505, 502),
(256, 452, 264, 481),
(26, 452, 61, 495),
(424, 451, 454, 484),
(36, 451, 67, 478),
(279, 447, 292, 488),
(439, 445, 480, 486),
(57, 445, 152, 499),
(242, 444, 256, 475),
(403, 442, 416, 480),
(69, 442, 132, 465),
(202, 438, 238, 476),
(25, 437, 45, 482),
(290, 432, 305, 479),
(24, 431, 38, 475),
(347, 429, 369, 459),
(187, 429, 237, 444),
(147, 428, 237, 468),
(453, 427, 471, 446),
(351, 427, 372, 462),
(148, 426, 236, 462),
(168, 425, 238, 444),
(408, 424, 431, 463),
(338, 424, 353, 456),
(424, 421, 463, 464),
(425, 421, 442, 433),
(463, 420, 491, 466),
(81, 420, 126, 451),
(347, 417, 378, 456),
(335, 415, 380, 471),
(174, 415, 230, 441),
(260, 413, 275, 512),
(246, 411, 258, 444),
(409, 409, 439, 437),
(433, 407, 463, 436),
(246, 404, 266, 466),
(480, 403, 500, 458),
(275, 401, 293, 449),
(158, 400, 198, 422),
(100, 400, 161, 481),
(153, 397, 195, 415),
(0, 396, 40, 512),
(166, 394, 204, 424),
(17, 394, 116, 456),
(211, 390, 238, 428),
(99, 389, 124, 420),
(465, 388, 487, 420),
(275, 388, 313, 473),
(293, 387, 322, 442),
(209, 387, 238, 424),
(154, 387, 191, 411),
(476, 383, 489, 405),
(175, 382, 208, 419),
(292, 381, 317, 410),
(21, 381, 45, 419),
(38, 380, 114, 424),
(369, 379, 491, 512),
(293, 375, 487, 512),
(172, 374, 196, 390),
(437, 373, 489, 448),
(139, 373, 185, 398),
(332, 369, 369, 385),
(290, 367, 396, 512),
(280, 367, 312, 423),
(248, 366, 258, 418),
(138, 366, 169, 386),
(364, 365, 442, 394),
(159, 364, 176, 378),
(173, 362, 200, 388),
(107, 362, 125, 393),
(60, 362, 70, 388),
(286, 361, 322, 388),
(360, 359, 398, 373),
(251, 359, 254, 401),
(449, 358, 480, 398),
(395, 357, 471, 432),
(375, 357, 400, 369),
(343, 354, 369, 379),
(304, 354, 359, 406),
(455, 353, 512, 484),
(283, 353, 299, 374),
(154, 353, 194, 367),
(352, 352, 404, 367),
(192, 352, 218, 417),
(146, 352, 169, 366),
(272, 349, 291, 453),
(179, 349, 208, 399),
(156, 349, 197, 362),
(122, 349, 211, 469),
(133, 348, 179, 424),
(305, 346, 328, 376),
(26, 346, 48, 384),
(288, 345, 321, 367),
(395, 344, 464, 385),
(364, 344, 401, 354),
(280, 342, 317, 368),
(427, 339, 457, 361),
(256, 338, 273, 428),
(239, 336, 249, 472),
(372, 335, 453, 360),
(244, 334, 265, 383),
(28, 333, 53, 374),
(378, 331, 412, 341),
(258, 331, 283, 359),
(0, 331, 39, 419),
(46, 329, 120, 409),
(237, 328, 257, 444),
(23, 328, 38, 370),
(240, 325, 250, 350),
(247, 322, 261, 350),
(62, 313, 126, 375),
(272, 312, 291, 344),
(230, 312, 253, 331),
(117, 312, 127, 360),
(45, 312, 56, 337),
(250, 308, 277, 342),
(361, 303, 375, 348),
(0, 301, 17, 343),
(252, 300, 277, 312),
(0, 299, 26, 338),
(231, 297, 251, 318),
(207, 297, 240, 319),
(29, 295, 97, 373),
(151, 294, 202, 341),
(2, 294, 22, 323),
(254, 293, 300, 339),
(370, 292, 387, 336),
(372, 290, 458, 357),
(367, 290, 400, 345),
(106, 289, 128, 349),
(277, 283, 311, 349),
(348, 282, 357, 321),
(371, 281, 388, 291),
(353, 279, 370, 331),
(224, 279, 278, 302),
(397, 278, 418, 294),
(175, 278, 240, 427),
(360, 276, 374, 333),
(373, 275, 512, 482),
(129, 275, 139, 343),
(5, 275, 17, 301),
(448, 274, 512, 286),
(371, 272, 408, 291),
(403, 271, 446, 292),
(169, 269, 261, 300),
(329, 268, 382, 362),
(44, 268, 122, 363),
(160, 266, 196, 305),
(218, 264, 250, 273),
(424, 261, 460, 285),
(250, 260, 310, 312),
(123, 260, 223, 407),
(496, 259, 512, 272),
(183, 259, 222, 273),
(206, 258, 267, 269),
(22, 258, 38, 334),
(397, 257, 415, 270),
(239, 256, 301, 288),
(473, 255, 512, 274),
(340, 254, 420, 290),
(171, 254, 261, 290),
(181, 252, 216, 268),
(24, 252, 44, 313),
(35, 251, 90, 306),
(10, 251, 26, 329),
(221, 250, 256, 264),
(191, 248, 230, 263),
(144, 248, 204, 284),
(222, 247, 257, 260),
(442, 246, 491, 282),
(393, 243, 416, 260),
(413, 241, 424, 269),
(110, 241, 127, 266),
(383, 240, 405, 264),
(314, 240, 344, 294),
(9, 239, 34, 277),
(412, 237, 425, 276),
(363, 237, 378, 248),
(279, 237, 336, 372),
(431, 234, 458, 268),
(295, 234, 341, 283),
(287, 234, 305, 280),
(213, 233, 262, 260),
(159, 233, 202, 255),
(435, 232, 460, 274),
(191, 231, 227, 252),
(136, 230, 181, 270),
(429, 227, 456, 236),
(420, 227, 447, 273),
(221, 227, 240, 246),
(138, 226, 167, 249),
(0, 226, 19, 302),
(414, 224, 429, 269),
(258, 223, 276, 258),
(20, 223, 84, 278),
(259, 221, 287, 268),
(358, 220, 386, 238),
(302, 219, 329, 251),
(362, 218, 396, 272),
(275, 217, 289, 248),
(231, 217, 247, 235),
(384, 215, 404, 243),
(133, 215, 223, 271),
(123, 214, 139, 244),
(114, 214, 163, 284),
(0, 214, 39, 268),
(424, 209, 452, 224),
(367, 208, 381, 218),
(323, 206, 382, 264),
(242, 206, 263, 249),
(56, 205, 179, 344),
(150, 204, 171, 236),
(317, 201, 347, 238),
(287, 199, 325, 231),
(87, 199, 122, 239),
(215, 198, 246, 235),
(175, 198, 187, 230),
(180, 196, 229, 246),
(183, 196, 195, 227),
(175, 194, 190, 243),
(412, 191, 424, 222),
(267, 191, 295, 242),
(229, 191, 249, 217),
(125, 190, 143, 232),
(147, 188, 177, 236),
(419, 187, 451, 224),
(243, 186, 262, 241),
(71, 185, 79, 215),
(11, 184, 22, 238),
(318, 183, 336, 214),
(281, 182, 317, 211),
(151, 182, 180, 198),
(409, 180, 420, 208),
(301, 180, 327, 220),
(254, 180, 282, 234),
(113, 180, 128, 221),
(37, 180, 72, 257),
(186, 179, 243, 217),
(323, 178, 349, 210),
(419, 177, 453, 223),
(137, 177, 174, 244),
(30, 177, 49, 202),
(163, 175, 182, 192),
(18, 174, 77, 275),
(154, 171, 164, 190),
(138, 168, 162, 195),
(133, 168, 158, 203),
(226, 164, 304, 277),
(200, 164, 238, 180),
(455, 163, 467, 272),
(87, 158, 137, 218),
(348, 155, 381, 225),
(257, 154, 314, 196),
(140, 154, 180, 177),
(64, 154, 80, 211),
(156, 153, 188, 219),
(97, 153, 114, 169),
(0, 153, 19, 237),
(116, 152, 136, 167),
(125, 149, 184, 227),
(63, 147, 102, 210),
(10, 147, 37, 185),
(0, 147, 14, 172),
(275, 144, 295, 164),
(185, 144, 217, 182),
(92, 144, 126, 164),
(243, 142, 254, 158),
(113, 141, 136, 156),
(260, 140, 290, 162),
(413, 139, 432, 181),
(202, 139, 234, 155),
(132, 139, 153, 157),
(132, 132, 182, 167),
(15, 132, 31, 166),
(27, 130, 78, 237),
(152, 126, 175, 142),
(170, 124, 184, 148),
(0, 124, 13, 158),
(399, 123, 408, 183),
(492, 122, 512, 162),
(446, 122, 457, 235),
(185, 121, 251, 174),
(358, 119, 398, 224),
(40, 118, 95, 230),
(411, 117, 418, 156),
(122, 116, 150, 139),
(40, 116, 74, 158),
(427, 114, 456, 228),
(457, 110, 512, 265),
(487, 109, 508, 137),
(187, 108, 294, 191),
(377, 104, 405, 222),
(273, 104, 300, 138),
(129, 104, 167, 144),
(235, 102, 266, 150),
(220, 101, 251, 144),
(380, 100, 401, 113),
(156, 100, 181, 130),
(0, 100, 9, 124),
(243, 99, 259, 108),
(456, 96, 484, 125),
(194, 96, 216, 106),
(410, 91, 435, 186),
(496, 88, 512, 123),
(466, 88, 486, 103),
(229, 88, 270, 113),
(396, 85, 429, 252),
(134, 80, 157, 109),
(437, 78, 454, 205),
(197, 78, 252, 88),
(157, 78, 183, 115),
(393, 76, 419, 135),
(453, 75, 474, 122),
(41, 75, 85, 174),
(236, 68, 263, 85),
(177, 66, 189, 85),
(454, 63, 482, 96),
(188, 61, 262, 86),
(427, 59, 433, 89),
(399, 55, 409, 82),
(197, 55, 221, 69),
(27, 54, 48, 78),
(453, 52, 475, 75),
(262, 51, 280, 68),
(28, 50, 64, 121),
(250, 49, 303, 130),
(0, 48, 40, 160),
(296, 45, 307, 68),
(379, 43, 402, 104),
(280, 43, 297, 73),
(23, 43, 37, 129),
(436, 40, 449, 137),
(475, 39, 494, 57),
(189, 33, 200, 52),
(411, 31, 417, 57),
(402, 31, 422, 83),
(425, 28, 437, 188),
(438, 27, 462, 91),
(267, 27, 301, 54),
(153, 24, 196, 64),
(0, 21, 5, 51),
(190, 20, 221, 47),
(22, 20, 31, 55),
(20, 20, 25, 73),
(458, 19, 512, 56),
(153, 15, 286, 104),
(269, 12, 301, 32),
(22, 11, 54, 72),
(262, 10, 279, 29),
(44, 9, 74, 86),
(75, 3, 96, 26),
(26, 2, 49, 29),
(423, 0, 512, 55),
(411, 0, 436, 185),
(400, 0, 424, 58),
(382, 0, 406, 74),
(344, 0, 370, 14),
(311, 0, 369, 33),
(266, 0, 396, 211),
(267, 0, 311, 21),
(234, 0, 279, 22),
(230, 0, 263, 16),
(191, 0, 235, 31),
(206, 0, 227, 19),
(158, 0, 206, 19),
(135, 0, 167, 16),
(89, 0, 116, 30),
(81, 0, 103, 17),
(54, 0, 82, 23),
(30, 0, 66, 24),
(22, 0, 31, 14),
(10, 0, 23, 67),
(0, 0, 16, 69),
(0, 0, 13, 17)]
ss = selective_search.selective_search(data, mode='fast')
len(ss)
3056
box = selective_search.box_filter(ss, topN=50)
import matplotlib.patches as pt
fig, ax = plt.subplots(figsize=(6,6))
ax.imshow(data)
for x1, y1, x2, y2 in box:
ax.add_patch(pt.Rectangle((x1,y1), (x2-x1), (y2-y1), fill=None, edgecolor='red', linewidth=2))
import selectivesearch
t = selectivesearch.selective_search(data)
len(t)
2
#[i['rect'] for i in t[1]]
# %pip install -U seaborn
import seaborn as sns
tips = sns.load_dataset('tips')
tips # 머신러닝 데이터는 모든 샘플의 크기(컬럼)가 일정 해야 함
# R-CNN 은 SVM 에 넣기 위해 크기 맞추기 위해 warped region을 하고 CNN features 를 함
total_bill | tip | sex | smoker | day | time | size | |
---|---|---|---|---|---|---|---|
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
... | ... | ... | ... | ... | ... | ... | ... |
239 | 29.03 | 5.92 | Male | No | Sat | Dinner | 3 |
240 | 27.18 | 2.00 | Female | Yes | Sat | Dinner | 2 |
241 | 22.67 | 2.00 | Male | Yes | Sat | Dinner | 2 |
242 | 17.82 | 1.75 | Male | No | Sat | Dinner | 2 |
243 | 18.78 | 3.00 | Female | No | Thur | Dinner | 2 |
244 rows × 7 columns
R-CNN
- 2013
- region proposals with CNNs
- selective search
- extract region proposals(~2000개)
- Supervised pre-training : Caffe CNN library
- Domain-specific fine-tuning : VOC, N=20 / ILSVRC2013, N=200
SPP-net
- 2014
- spatial pyramid pooing
- convolutional features -> R-CNN : 2000번 / SPP-net : 1번 (R-CNN 보다 24~102배 더 빠름)
- image -> crop/wrap 을 안한다.
- image -> conv layers -> spatial pyramid pooing
- spatial pyramid pooing : 예) 1+4+16=21개
- SPP은 항상 고정된 사이즈가 나온다.
- Bag-of-words => spatial pyramid Matching(SPM) ==> SPP 로 확장(크기고정)