@@ -210,7 +210,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 219,
"metadata": {
"collapsed": false
},
@@ -258,12 +258,10 @@
" h = F.leaky_relu(self.fc1(h), slope=0.1)\n",
" h = F.dropout(h, train=self.train, ratio=0.5)\n",
" h = F.leaky_relu(self.fc2(h), slope=0.1)\n",
" h = self.fc3(h) # (batch_size, ((5*N_BOXES)+N_CLASSES)*N_GRID*N_GRID)\n",
"\n",
" # extract result tensors\n",
" h = self.fc3(h)\n",
" # reshape predicted tensor\n",
" h = F.reshape(h, (batch_size, (5*N_BOXES)+N_CLASSES, N_GRID, N_GRID))\n",
" x, y, w, h, conf, prob = F.split_axis(h, indices_or_sections=(1,2,3,4,5), axis=1)\n",
" return F.sigmoid(x), F.sigmoid(y), F.sigmoid(w), F.sigmoid(h), F.sigmoid(conf), F.sigmoid(prob)\n",
" return h\n",
"\n",
" def __call__(self, x, t):\n",
" # 推論を実行\n",
@@ -870,7 +868,7 @@
},
{
"cell_type": "code",
"execution_count": 96,
"execution_count": 220,
"metadata": {
"collapsed": false
},
@@ -888,6 +886,215 @@
"## 推論を実行"
]
},
{
"cell_type": "code",
"execution_count": 221,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"h = detector_model.forward(x)"
]
},
{
"cell_type": "code",
"execution_count": 261,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"h2 = F.sigmoid(h)"
]
},
{
"cell_type": "code",
"execution_count": 234,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"_px, _py, _pw, _ph, _pconf, _pprob = F.split_axis(h.data, indices_or_sections=(1,2,3,4,5), axis=1)\n",
"_px = F.sigmoid(_px).data\n",
"_py = F.sigmoid(_py).data\n",
"_pw = F.sigmoid(_pw).data\n",
"_ph = F.sigmoid(_ph).data\n",
"_pconf = F.sigmoid(_pconf).data\n",
"_pprob = F.sigmoid(_pprob).data"
]
},
{
"cell_type": "code",
"execution_count": 267,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"_px2, _py2, _pw2, _ph2, _pconf2, _pprob2 = np.array_split(h2.data, indices_or_sections=(1,2,3,4,5), axis=1)"
]
},
{
"cell_type": "code",
"execution_count": 277,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[-1. , 0.13344583, 0.91010243, 0.1460737 , 0.40128991,\n",
" 0.51702583, 0.57517201],\n",
" [ 0.04876372, 0.29020485, 0.99930859, 0.93551981, 0.05357945,\n",
" 0.86496329, 0.99785376],\n",
" [ 0.82082754, 0.27134973, 0.2106505 , 0.42414856, 0.97827983,\n",
" 0.52681172, 0.92448193],\n",
" [ 0.74471402, 0.22758949, 0.07767206, 0.99378127, 0.43946666,\n",
" 0.63642144, 0.89852059],\n",
" [ 0.11715493, 0.37505376, 0.03614116, 0.00101459, 0.74491358,\n",
" 0.23284879, 0.54585719],\n",
" [ 0.17654884, 0.34766054, 0.34777841, 0.18494397, 0.03876951,\n",
" 0.955625 , 0.42072499],\n",
" [ 0.65734005, 0.7649737 , 0.98942077, 0.39563155, 0.43352816,\n",
" 0.07697165, 0.74792385]], dtype=float32)"
]
},
"execution_count": 277,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"_px2[0][0][0][0] = -1\n",
"_px2[0][0]"
]
},
{
"cell_type": "code",
"execution_count": 275,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(10, 31, 7, 7)"
]
},
"execution_count": 275,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"h3 = chainer.cuda.to_cpu(h2.data.copy())\n",
"h3.shape"
]
},
{
"cell_type": "code",
"execution_count": 276,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ -2.00000000e+00, 1.33445829e-01, 9.10102427e-01,\n",
" 1.46073699e-01, 4.01289910e-01, 5.17025828e-01,\n",
" 5.75172007e-01],\n",
" [ 4.87637222e-02, 2.90204853e-01, 9.99308586e-01,\n",
" 9.35519814e-01, 5.35794497e-02, 8.64963293e-01,\n",
" 9.97853756e-01],\n",
" [ 8.20827544e-01, 2.71349728e-01, 2.10650504e-01,\n",
" 4.24148560e-01, 9.78279829e-01, 5.26811719e-01,\n",
" 9.24481928e-01],\n",
" [ 7.44714022e-01, 2.27589488e-01, 7.76720643e-02,\n",
" 9.93781269e-01, 4.39466655e-01, 6.36421442e-01,\n",
" 8.98520589e-01],\n",
" [ 1.17154926e-01, 3.75053763e-01, 3.61411572e-02,\n",
" 1.01459026e-03, 7.44913578e-01, 2.32848793e-01,\n",
" 5.45857191e-01],\n",
" [ 1.76548839e-01, 3.47660542e-01, 3.47778410e-01,\n",
" 1.84943974e-01, 3.87695134e-02, 9.55624998e-01,\n",
" 4.20724988e-01],\n",
" [ 6.57340050e-01, 7.64973700e-01, 9.89420772e-01,\n",
" 3.95631552e-01, 4.33528155e-01, 7.69716501e-02,\n",
" 7.47923851e-01]], dtype=float32)"
]
},
"execution_count": 276,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"h3[0][0][0][0] = -2.0\n",
"h3[0][0]"
]
},
{
"cell_type": "code",
"execution_count": 264,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.66239929, 0.13344583, 0.91010243, 0.1460737 , 0.40128991,\n",
" 0.51702583, 0.57517201],\n",
" [ 0.04876372, 0.29020485, 0.99930859, 0.93551981, 0.05357945,\n",
" 0.86496329, 0.99785376],\n",
" [ 0.82082754, 0.27134973, 0.2106505 , 0.42414856, 0.97827983,\n",
" 0.52681172, 0.92448193],\n",
" [ 0.74471402, 0.22758949, 0.07767206, 0.99378127, 0.43946666,\n",
" 0.63642144, 0.89852059],\n",
" [ 0.11715493, 0.37505376, 0.03614116, 0.00101459, 0.74491358,\n",
" 0.23284879, 0.54585719],\n",
" [ 0.17654884, 0.34766054, 0.34777841, 0.18494397, 0.03876951,\n",
" 0.955625 , 0.42072499],\n",
" [ 0.65734005, 0.7649737 , 0.98942077, 0.39563155, 0.43352816,\n",
" 0.07697165, 0.74792385]], dtype=float32)"
]
},
"execution_count": 264,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"h2.data[0][0]"
]
},
{
"cell_type": "code",
"execution_count": 246,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(12740, (10, 26, 7, 7))"
]
},
"execution_count": 246,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(_pprob == _pprob2).sum(), _pprob.shape"
]
},
{
"cell_type": "code",
"execution_count": 97,
@@ -1397,25 +1604,67 @@
},
{
"cell_type": "code",
"execution_count": 70,
"execution_count": 214,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(26, 7, 7)"
"array([ 0.43308464, 0.11953532, 0.29049623, 0.44944125, 0.25558978,\n",
" 0.3585023 , 0.61695206, 0.02689682, 0.11112458, 0.61580402,\n",
" 0.56997931, 0.36351889, 0.38775748, 0.39617181, 0.33856669,\n",
" 0.36801514, 0.23804198, 0.40024301, 0.09726732, 0.30708119,\n",
" 0.60552824, 0.6390807 , 0.49021432, 0.46792892, 0.08237838,\n",
" 0.62170118], dtype=float32)"
]
},
"execution_count": 70,
"execution_count": 214,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"_pobj = (_pprob[0] * _pconf[0]).transpose(1,2,0)\n",
"_pobj[1,3]"
]
},
{
"cell_type": "code",
"execution_count": 286,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.5436852 , 0.87299973, 0.54632992, 0.85493296, 0.79055774,\n",
" 0.04883667, 0.82923347],\n",
" [ 0.97029114, 0.99650782, 0.85720515, 0.29600522, 0.2879642 ,\n",
" 0.67986673, 0.996961 ],\n",
" [ 0.26799804, 0.19504192, 0.28671658, 0.86294001, 0.85752749,\n",
" 0.93175137, 0.04307476],\n",
" [ 0.01909542, 0.21990483, 0.97203565, 0.97640979, 0.07439857,\n",
" 0.90765405, 0.97738111],\n",
" [ 0.04907788, 0.57511681, 0.99103165, 0.30767789, 0.16018122,\n",
" 0.14995499, 0.72690445],\n",
" [ 0.89716494, 0.43719345, 0.94334656, 0.80119491, 0.32274631,\n",
" 0.61169893, 0.61864322],\n",
" [ 0.45193967, 0.77233005, 0.51528579, 0.10030524, 0.13502592,\n",
" 0.06036985, 0.98146945]], dtype=float32)"
]
},
"execution_count": 286,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"_class_probs = _pprob[0] * _pconf[0]\n",
"_class_probs.shape"
"_max_class_probs_map = _class_probs.max(axis=0)\n",
"_max_class_probs_map"
]
},
{
@@ -2036,6 +2285,260 @@
" for x in np.arange(0,5)]"
]
},
{
"cell_type": "code",
"execution_count": 178,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "TypeError",
"evalue": "an integer is required",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-178-35a1f25e62ce>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrand\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32mmtrand.pyx\u001b[0m in \u001b[0;36mmtrand.RandomState.rand (numpy/random/mtrand/mtrand.c:18928)\u001b[0;34m()\u001b[0m\n",
"\u001b[0;32mmtrand.pyx\u001b[0m in \u001b[0;36mmtrand.RandomState.random_sample (numpy/random/mtrand/mtrand.c:14895)\u001b[0;34m()\u001b[0m\n",
"\u001b[0;32mmtrand.pyx\u001b[0m in \u001b[0;36mmtrand.cont0_array (numpy/random/mtrand/mtrand.c:2383)\u001b[0;34m()\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: an integer is required"
]
}
],
"source": [
"np.random.rand((1,3,5,5)).shape"
]
},
{
"cell_type": "code",
"execution_count": 306,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 3, 4, 5])"
]
},
"execution_count": 306,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.arange(1, 6)\n",
"a[1:]"
]
},
{
"cell_type": "code",
"execution_count": 217,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2\n",
"2 3\n",
"3 4\n",
"4 5\n"
]
}
],
"source": [
"for i, item in enumerate(a[1:], 1):\n",
" print(i, item)"
]
},
{
"cell_type": "code",
"execution_count": 299,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([11, 12, 13, 14, 15])"
]
},
"execution_count": 299,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = np.arange(11, 16)\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 296,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 1 11\n",
"2 2 12\n",
"3 3 13\n",
"4 4 14\n",
"5 5 15\n"
]
}
],
"source": [
"for i, (a, b) in enumerate(zip(a, b), 1):\n",
" print (i, a, b)"
]
},
{
"cell_type": "code",
"execution_count": 304,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([15, 14, 13, 12, 11])"
]
},
"execution_count": 304,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.sort(b)[::-1]"
]
},
{
"cell_type": "code",
"execution_count": 309,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 11\n",
"1 12\n",
"1 13\n",
"1 14\n",
"1 15\n",
"2 11\n",
"2 12\n",
"2 13\n",
"2 14\n",
"2 15\n",
"3 11\n",
"3 12\n",
"3 13\n",
"3 14\n",
"3 15\n",
"4 11\n",
"4 12\n",
"4 13\n",
"4 14\n",
"4 15\n",
"5 11\n",
"5 12\n",
"5 13\n",
"5 14\n",
"5 15\n"
]
}
],
"source": [
"import itertools\n",
"for _a, _b in itertools.product(a, b):\n",
" print(_a, _b)"
]
},
{
"cell_type": "code",
"execution_count": 313,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 313,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [1,2,3,4,5]\n",
"type(a)"
]
},
{
"cell_type": "code",
"execution_count": 320,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.5"
]
},
"execution_count": 320,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [0.2, 0.1, 0.5, 0.9, 0.8]\n",
"np.asarray(a).mean()"
]
},
{
"cell_type": "code",
"execution_count": 321,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"5250.0"
]
},
"execution_count": 321,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"750 * (3.5+3.5)"
]
},
{
"cell_type": "code",
"execution_count": null,