def __call__(self, s): # sは入力文章の単語id配列 accum_loss = None # 入力単語をword2vecで縮約 v, k = self.embed.W.data.shape h = Variable(np.zeros((1, k), dtype=np.float32)) # 履歴入力 c = Variable(np.zeros((1, k), dtype=np.float32)) # 入力 # 注目単語の次の単語を訓練データとして学習 for i in range(len(s)): next_w_id = eos_is if (i == len(s) - 1) else s[i + 1] # 訓練データ tx = Variable(np.array([next_w_id], dtype=np.int32)) # 入力文のうちの単語[i] x_k = self.embed(Variable(np.array([s[i]], dtype=np.int32))) # RNN層の計算 z0 = self.Wz(x_k) + self.Rz(h) z1 = F.tan(z0) # Input層の計算 i0 = self.Wi(x_k) + self.Ri(F.dropout(h)) i1 = F.sigmoid(i0) # Forget層の計算 f0 = self.Wf(x_k) + slef.Rf(F.dropout(h)) f1 = F.sigmoid(f0) # MemoryCellに渡す値の計算 c = i1 * z1 + f1 * c # LSTM出力層の計算 o0 = slef.Wo(x, k) + self.Ro(h) o1 = F.sigmoid(o0) # 次の時刻に渡す履歴計算 h = o1 * F.tanh(c) loss = F.softmax_cross_entropy(self.W(h), tx) accum_loss = loss if accum_loss is None else accum_loss + loss return accum_loss
def perspective(vertices, angle=None): assert (vertices.ndim == 3) if angle is None: xp = chainer.cuda.get_array_module(vertices) angle = xp.array([xp.radians(60)] * vertices.shape[0], 'float32') width = cf.tan(angle / 2) width = cf.broadcast_to(width[:, None], vertices.shape[:2]) z = vertices[:, :, 2] x = vertices[:, :, 0] / z / width y = vertices[:, :, 1] / z / width vertices = cf.concat((x[:, :, None], y[:, :, None], z[:, :, None]), axis=2) return vertices
def get_sample(self, mu, sigma, number_samples): """ One line description Parameters ---------- Returns ------- """ mu, sigma = broadcast_and_squeeze(mu, sigma) sample = mu + sigma*F.tan(np.pi*np.random.uniform(0,1,size=mu.shape).astype(np.float32)) return sample
def perspective(vertices, angle=30.): assert (vertices.ndim == 3) xp = chainer.cuda.get_array_module(vertices) if isinstance(angle, float) or isinstance(angle, int): angle = chainer.Variable(xp.array(angle, 'float32')) angle = angle / 180. * 3.1416 angle = cf.broadcast_to(angle[None], (vertices.shape[0],)) width = cf.tan(angle) width = cf.broadcast_to(width[:, None], vertices.shape[:2]) z = vertices[:, :, 2] x = vertices[:, :, 0] / z / width y = vertices[:, :, 1] / z / width vertices = cf.concat((x[:, :, None], y[:, :, None], z[:, :, None]), axis=2) return vertices
def perspective(vertices, angle=30.): assert (vertices.ndim == 3) xp = chainer.cuda.get_array_module(vertices) if isinstance(angle, float) or isinstance(angle, int): angle = chainer.Variable(xp.array(angle, 'float32')) angle = angle / 180. * 3.1416 angle = cf.broadcast_to(angle[None], (vertices.shape[0], )) width = cf.tan(angle) width = cf.broadcast_to(width[:, None], vertices.shape[:2]) z = vertices[:, :, 2] x = vertices[:, :, 0] / z / width y = vertices[:, :, 1] / z / width vertices = cf.concat((x[:, :, None], y[:, :, None], z[:, :, None]), axis=2) return vertices
def __call__(self, x): xTan = self.dilatedConvTan(x) xTan = self.batchNormTan(x) xTan = F.tan(xTan) xSig = self.dilatedConvSig(x) xSig = self.batchNormSig(x) xSig = F.sigmoid(xSig) h = xTan * xSig skip = self.conv(h) skip = self.batchNormCnn(h) s = x.shape xp = chainer.cuda.cupy if self.useGPU else np zero = xp.zeros((s[0], s[1], s[2], s[3] - skip.shape[3]), dtype='f') skip = F.concat((skip, zero), axis=3) next = skip + x return next, skip
def shoot(self): W = self.width H = self.height origin = self.origin xaxis = self.xaxis zaxis = self.zaxis yaxis = self.yaxis angle = self.fov t0 = self.t0 t1 = self.t1 xp = chainer.backend.get_array_module(origin) #zaxis = zaxis.reshape((1, 3, 1, 1)) #yaxis = yaxis.reshape((1, 3, 1, 1)) #xaxis = vnorm(vcross(yaxis, zaxis)) #yaxis = vnorm(vcross(zaxis, xaxis)) xaxis = vnorm(xaxis.reshape((1, 3, 1, 1))).reshape((1, 1, 3)) yaxis = vnorm(yaxis.reshape((1, 3, 1, 1))).reshape((1, 1, 3)) zaxis = vnorm(zaxis.reshape((1, 3, 1, 1))).reshape((1, 1, 3)) #print(angle.shape) angle = (angle / 2) * math.pi / 180.0 HH = F.tan(angle) ro = origin ro = F.tile(ro, (H, W, 1)) ro = F.transpose(ro, (2, 0, 1)) yy = xp.tile(xp.arange(H, dtype=np.float32).reshape((H, 1, 1)), (1, W, 1)) #(H, W, 1) xx = xp.tile(xp.arange(W, dtype=np.float32).reshape((1, W, 1)), (H, 1, 1)) #(H, W, 1) yy = (1 - 2 * (yy + 0.5) / H) * HH xx = (2 * (xx + 0.5) / W - 1) * HH rd = xx * xaxis + yy * yaxis + F.broadcast_to(zaxis, (H, W, 3)) rd = F.transpose(rd, (2, 0, 1)) rd = rd.reshape((1, 3, H, W)) rd = vnorm(rd) rd = rd.reshape((3, H, W)) t0 = F.broadcast_to(t0.reshape((1, 1, 1)), (1, H, W)) t1 = F.broadcast_to(t1.reshape((1, 1, 1)), (1, H, W)) #print(ro.shape, ro.dtype) #print(rd.shape, rd.dtype) return ro, rd, t0, t1
def tan(self, x): return F.tan(x)
def forward(self, x): y1 = F.tan(x) return y1