def __call__(self, x): h = cf.relu(self.linear1_bn(self.linear1(x))) h = cf.relu(self.linear2_bn(self.linear2(h))) bias = cf.reshape(self.linear_bias(h), (-1, self.num_vertices, 3)) bias *= self.scaling base = self.vertices_base base = cf.broadcast_to(base[None, :, :], bias.shape) vertices = base + bias if self.symmetric: xy = vertices[:, :, :2] # [bs, nv, 2] z = cf.absolute(vertices[:, :, 2:3]) # [bs, nv, 1] vertices = cf.concat((xy, z), axis=2) vertices = cf.transpose( cf.tensordot(vertices, self.symmetric_matrix, axes=(1, 0)), (0, 2, 1)) xy = vertices[:, :, :2] # [bs, nv, 2] z = vertices[:, :, 2:3] # [bs, nv, 1] z = z * self.z_sign[None, :, None] vertices = cf.concat((xy, z), axis=2) vertices = cf.tanh(vertices) * self.tanh_scale return vertices, self.faces
def create_2d_window(window_size, channel, xp): _1D_window = F.reshape(gaussian(window_size, 1.5, xp), (-1, 1)) _2D_window = F.reshape( F.tensordot(_1D_window, _1D_window.transpose(), axes=1), (1, 1, -1, window_size)) window = F.repeat(_2D_window, channel, axis=0) return window
def setUp(self): self.a = self._setup_tensor(.5, 1, self.a_shape, self.a_dtype) self.b = self._setup_tensor(.5, 1, self.b_shape, self.b_dtype) ret_dtype = numpy.result_type(self.a_dtype, self.b_dtype) self.gc = self._setup_tensor(-1, 1, self.gc_shape, ret_dtype) self.gga = self._setup_tensor(.5, 1, self.a_shape, self.a_dtype) self.ggb = self._setup_tensor(.5, 1, self.b_shape, self.b_dtype) self.op = lambda a, b: F.tensordot(a, b, axes=self.axes) self.forward_answer = numpy.tensordot(self.a, self.b, self.axes)
def __call__(self, x, is_prob=False): if is_prob: x_emb = F.tensordot(x, self.embedding.embed.W, [[2], [0]]) else: x_emb = self.embedding(x) x_emb = F.expand_dims(x_emb, 1) x_emb = normalizing(x_emb, 1) H = self.encoder(x_emb) logits = self.disc(H) return logits, H
def test_invalid_shape(self): a_data = numpy.zeros((4, 3, 2), dtype=numpy.float32) b_data = numpy.zeros((2, 3, 5), dtype=numpy.float32) a = chainer.Variable(a_data) b = chainer.Variable(b_data) with self.assertRaises(ValueError): F.tensordot(a, b) with self.assertRaises(ValueError): F.tensordot(a, b, axes=((1, 2), (0, 1))) with self.assertRaises(ValueError): F.tensordot(a, b, axes=((0), (0))) with self.assertRaises(ValueError): F.tensordot(a, b, axes=((2), (2)))
def __call__(self, x): batch_size = x.shape[0] hs = [] for i in range(6): linear1 = getattr(self, 'linear_p%d_1' % i) conv1 = getattr(self, 'conv_p%d_1' % i) conv2 = getattr(self, 'conv_p%d_2' % i) conv3 = getattr(self, 'conv_p%d_3' % i) conv4 = getattr(self, 'conv_p%d_4' % i) linear1_bn = getattr(self, 'linear_p%d_1_bn' % i) conv1_bn = getattr(self, 'conv_p%d_1_bn' % i) conv2_bn = getattr(self, 'conv_p%d_2_bn' % i) conv3_bn = getattr(self, 'conv_p%d_3_bn' % i) h = linear1(x) h = h.reshape((h.shape[0], -1, 2, 2)) h = cf.relu(linear1_bn(h)) if not self.use_up_sampling: h = cf.relu(conv1_bn(conv1(h))) h = cf.relu(conv2_bn(conv2(h))) h = cf.relu(conv3_bn(conv3(h))) else: h = cf.relu(conv1_bn(conv1(up_sample(h)))) h = cf.relu(conv2_bn(conv2(up_sample(h)))) h = cf.relu(conv3_bn(conv3(up_sample(h)))) h = conv4(h) # [bs, 3, 16, 16] h = cf.transpose(h, (0, 2, 3, 1)) # [bs, 16, 16, 3] h = cf.reshape(h, (batch_size, -1, 3)) # [bs, 16 ** 2, 3] hs.append(h) h = cf.concat(hs, axis=1) # [bs, 6 * 16 ** 2, 3] vm = self.xp.tile(self.vertices_matrix[None, :, :], (batch_size, 1, 1)) bias = cf.matmul(vm, h, transa=True) bias *= self.scaling base = self.vertices_base base = cf.broadcast_to(base[None, :, :], bias.shape) vertices = base + bias if self.symmetric: xy = vertices[:, :, :2] # [bs, nv, 2] z = cf.absolute(vertices[:, :, 2:3]) # [bs, nv, 1] vertices = cf.concat((xy, z), axis=2) vertices = cf.transpose( cf.tensordot(vertices, self.symmetric_matrix, axes=(1, 0)), (0, 2, 1)) xy = vertices[:, :, :2] # [bs, nv, 2] z = vertices[:, :, 2:3] # [bs, nv, 1] z = z * self.z_sign[None, :, None] vertices = cf.concat((xy, z), axis=2) vertices = cf.tanh(vertices) * self.tanh_scale return vertices, self.faces
def __call__(self, x_data, u_h): """ Perform one forward step computation given the input x_data, and the current states of the input neurons (u_io) and the hidden context layer neurons (u_h). If u_h is None, the initial states are used instead. """ from chainer.backends import cuda xp = cuda.get_array_module(self.initial_states.W) x = chainer.Variable(x_data) #.reshape((1,-1))) if not self.current_y.array is None: # get mean and variance for prior distribution pred_mean = self.current_y # prediction variance is taken from current variance estimation which expresses ability to predict the variance in the training data # hyp_prior multiplication of factor H pred_var = self.current_v * (self.hyp_prior) if not xp.any(pred_var.array): # It should not happen, but just in case check for zero prediction variance print("Predicted variance is zero!!!!") pred_var.array = xp.tile(np.float32(0.00001), pred_var.array.shape) # get mean and variance for input distribution input_mean = x # if external_signal_variance is not available, set equal to pred_var if self.external_signal_variance is None: input_var = pred_var else: input_var = chainer.Variable( xp.tile( xp.asarray(xp.float32([self.external_signal_variance ])), (x.shape[0], x.shape[1]))) ### Bayesian inference ### if xp.any(xp.isinf(input_var.array)): sigma_BI = xp.sqrt(pred_var.array) mu_BI = pred_mean.array else: # standard deviation of BI signal sigma_BI = xp.sqrt( xp.divide(xp.multiply(input_var.array, pred_var.array), (input_var.array + pred_var.array))) # mean of BI signal mu_BI = xp.power(sigma_BI, 2) * ( xp.divide(pred_mean.array, pred_var.array) + xp.divide(input_mean.array, input_var.array)) # sample from posterior distribution to get new input for the network if self.add_BI_variance: if cuda.get_array_module(self.initial_states.W) == np: x.array = mu_BI + sigma_BI * xp.float32( xp.random.randn(sigma_BI.shape[0], sigma_BI.shape[1])) else: x.array = mu_BI + sigma_BI * (xp.random.randn( sigma_BI.shape[0], sigma_BI.shape[1], dtype=xp.float32)) else: x.array = mu_BI self.current_x = x # in the first time step no u_h is given => set initial states if u_h is None: u_h = self.initial_states()[self.classes] # forward context mapping # update recurrent connections h = F.tanh(u_h) if self.bias_learning: recurrent_connections = F.transpose( F.tensordot(self.h_to_h.W, F.transpose(h), axes=1)) + F.tile( self.h_to_h.b, (h.shape[0], 1)) u_h = chainer.functions.scale( self.x_to_h(x) + recurrent_connections, 1 / self.tau_c()) + chainer.functions.scale( u_h, (1 - 1 / self.tau_c())) else: u_h = chainer.functions.scale( self.x_to_h_bias(self.x_to_h(x)) + self.h_to_h_bias(self.h_to_h(h)), 1 / self.tau_c()) + chainer.functions.scale( u_h, (1 - 1 / self.tau_c())) # forward output mapping u_out = self.h_to_y(h) y = F.tanh(u_out) # forward variance mapping u_v = self.h_to_v(h) v = F.exp(u_v + self.aberrant_sensory_precision ) + 0.00001 # [Idei et al. 2017] self.current_y = y self.current_v = v return u_h, y, v
'-m', type=str, default="model/ResNet-50-model.npz") parser.add_argument('--image', '-i', type=str, default="data/cat.jpeg") args = parser.parse_args() img = utils.read_image(args.image, color=True) img = F.expand_dims(img, 0) img = F.resize_images(img, (224, 224)) ax = vis_image(F.squeeze(img, 0).data) model = ResNetCAM() chainer.serializers.load_npz(args.model, model) with chainer.using_config('train', False): conv_outputs, preds = model(img) resized_conv_outputs = F.resize_images(conv_outputs, (224, 224)) to_multiply_tensor = model.fc6.W[2].reshape(1, 2048) heatmap = np.flip( F.tensordot(to_multiply_tensor, resized_conv_outputs).data, 1) ax.imshow(heatmap, cmap='jet', alpha=.4) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) ax.get_xaxis().set_ticks([]) ax.get_yaxis().set_ticks([]) plt.title("Predicted: " + CLASS_ID[int(F.argmax(preds).data)]) plt.savefig("predicted/result.png") plt.show()
def forward(self, inputs, device): a, b = inputs y = F.tensordot(a, b, axes=self.axes) return y,