def _gen_model(self): N = self.batch input_shape = self.arch['input_shape'] output_shape = self.arch['output_shape'] if 'debug' in self.arch.keys(): debug = self.arch['debug'] else: debug = False self.batch_input_shape = self.get_shape(N, input_shape) self.batch_output_shape = self.get_shape(N, output_shape) depth = self.arch['depth'] unit = self.arch['unit'] units = np.ones(depth + 1) * unit _unit = np.prod(output_shape) units[-1] = _unit units = units.astype('int') layer = [rm.Flatten()] for _unit in units: layer.append(rm.BatchNormalize()) layer.append(rm.Relu()) layer.append(rm.Dense(_unit)) #layer = layer[:-1] + [rm.Dropout()] + [layer[-1]] self.fcnn = rm.Sequential(layer) if debug: x = np.zeros(self.batch_input_shape) for _layer in layer: x = _layer(x) print(x.shape, str(_layer.__class__).split('.')[-1]) x = rm.reshape(x, self.batch_output_shape) print(x.shape)
def forward(self, x): h = self.pool1(rm.leaky_relu(self.bn1(self.conv1(x)), slope=0.1)) h = self.pool2(rm.leaky_relu(self.bn2(self.conv2(h)), slope=0.1)) h = rm.leaky_relu(self.bn3(self.conv3(h)), slope=0.1) h = rm.leaky_relu(self.bn4(self.conv4(h)), slope=0.1) h = self.pool3(rm.leaky_relu(self.bn5(self.conv5(h)), slope=0.1)) h = rm.leaky_relu(self.bn6(self.conv6(h)), slope=0.1) h = rm.leaky_relu(self.bn7(self.conv7(h)), slope=0.1) h = self.pool4(rm.leaky_relu(self.bn8(self.conv8(h)), slope=0.1)) h = rm.leaky_relu(self.bn9(self.conv9(h)), slope=0.1) h = rm.leaky_relu(self.bn10(self.conv10(h)), slope=0.1) h = rm.leaky_relu(self.bn11(self.conv11(h)), slope=0.1) h = rm.leaky_relu(self.bn12(self.conv12(h)), slope=0.1) h = rm.leaky_relu(self.bn13(self.conv13(h)), slope=0.1) h = self.pool5(h) h = rm.leaky_relu(self.bn14(self.conv14(h)), slope=0.1) h = rm.leaky_relu(self.bn15(self.conv15(h)), slope=0.1) h = rm.leaky_relu(self.bn16(self.conv16(h)), slope=0.1) h = rm.leaky_relu(self.bn17(self.conv17(h)), slope=0.1) h = rm.leaky_relu(self.bn18(self.conv18(h)), slope=0.1) ##### pretraining layer h = self.conv23(h) h = rm.average_pool2d(h, filter=(h.shape[-1], h.shape[-1]), stride=(1, 1), padding=(0, 0)) y = rm.reshape(h, (x.shape[0], -1)) return y
def test_gpu_node_reshape(a): set_cuda_active(True) g1 = Variable(a) g3 = rm.sum(rm.reshape(g1, shape=(-1, 1))) g = g3.grad() g_g1 = g.get(g1) g3.to_cpu() set_cuda_active(False) c3 = rm.sum(rm.reshape(g1, shape=(-1, 1))) c = c3.grad() c_g1 = c.get(g1) close(g3, c3) close(c_g1, g_g1)
def forward(self, x): h = self.transform(x) #print(h.shape) h = rm.reshape(h, (len(x), self.channels, self.dim, self.dim)) #print(h.shape) layers = self.hidden._layers for i in range(len(layers)): if self.batch_normal: h = layers[2 * i](h) h = rm.relu(layers[2 * i + 1](h)) else: h = rm.relu(layers[i](h)) #print(h.shape) h = rm.sigmoid(self.output(h)) return h
def forward(self, x, print_parameter=False): hidden = x if print_parameter: print('^ {}'.format(hidden.shape)) if self.trans: hidden = self.trans(hidden) if print_parameter: print('^ {}'.format(hidden.shape)) hidden = rm.reshape(hidden, self.first_shape) if print_parameter: print('^ {}'.format(hidden.shape)) layers = self.parameters for layer in layers: hidden = layer(hidden) if print_parameter: print('^ {}'.format(hidden.shape)) if self.act: hidden = self.last_act(hidden) return hidden
def func(node): return sum(rm.reshape(node, shape)) + sum(node.reshape(shape)) + sum(node.reshape(*shape))
def _forward(self, x): x = self.fcnn(x) return rm.reshape(x, self.batch_output_shape)
def _train(self, x, idx, y): with self.fcnn.train(): x = self.fcnn(x) z = rm.reshape(x, self.batch_output_shape) return z, rm.softmax_cross_entropy(z[:len(idx)], y)
def _train(self, x, idx, y): with self.fcnn.train(): x = self.fcnn(x) z = rm.reshape(x, self.batch_output_shape) return z, rm.mean_squared_error(z[:len(idx)], y)
def _oper_cpu(cls, output, t, bbox, classes, init_anchors): batch_size, _, grid_h, grid_w = output.shape output_reshape = rm.reshape( output, (batch_size, bbox, classes + 5, grid_h, grid_w)) x, y, w, h, conf, prob = output_reshape[:, :, 0: 1, :, :], output_reshape[:, :, 1: 2, :, :], output_reshape[:, :, 2: 3, :, :], output_reshape[:, :, 3: 4, :, :], output_reshape[:, :, 4: 5, :, :], output_reshape[:, :, 5:, :, :] x = rm.sigmoid(x) y = rm.sigmoid(y) conf = rm.sigmoid(conf) prob = rm.transpose(prob, (0, 2, 1, 3, 4)).reshape(batch_size, classes, -1) prob = rm.softmax(prob) # prob_exp = np.exp(prob) # prob = prob_exp / np.sum(prob_exp, axis=1, keepdims=True) prob = rm.reshape(prob, (batch_size, classes, bbox, grid_h, grid_w)) deltas = np.zeros(output_reshape.shape, dtype=np.float32) #x.to_cpu() #y.to_cpu() #conf.to_cpu() #prob.to_cpu() #anchor if init_anchors is None: anchors = [[5.375, 5.03125], [5.40625, 4.6875], [2.96875, 2.53125], [2.59375, 2.78125], [1.9375, 3.25]] else: anchors = init_anchors thresh = 0.7 # 教師データ tw = np.ones(w.shape, dtype=np.float32) th = np.ones(h.shape, dtype=np.float32) tx = np.tile(0.5, x.shape).astype(np.float32) ty = np.tile(0.5, y.shape).astype(np.float32) box_learning_scale = np.tile(0.1, x.shape).astype(np.float32) tconf = np.zeros(conf.shape, dtype=np.float32) conf_learning_scale = np.tile(0.1, conf.shape).astype(np.float32) tprob = prob.as_ndarray() #print("output") #print(output_reshape[1,1, :,1,1]) x_shift = np.broadcast_to(np.arange(grid_w, dtype=np.float32), x.shape[1:]) y_shift = np.broadcast_to( np.arange(grid_h, dtype=np.float32).reshape(grid_h, 1), y.shape[1:]) w_anchor = np.broadcast_to( np.reshape( np.array(anchors, dtype=np.float32)[:, 0], (bbox, 1, 1, 1)), w.shape[1:]) h_anchor = np.broadcast_to( np.reshape( np.array(anchors, dtype=np.float32)[:, 1], (bbox, 1, 1, 1)), h.shape[1:]) #x_shift.to_gpu(), y_shift.to_gpu(), w_anchor.to_gpu(), h_anchor.to_gpu() best_ious = [] for batch in range(batch_size): truth_bbox = len(t[batch]) box_x = (x[batch] + x_shift) / grid_w box_y = (y[batch] + y_shift) / grid_h box_w = np.exp(w[batch]) * w_anchor / grid_w box_h = np.exp(h[batch]) * h_anchor / grid_h ious = [] for truth_index in range(truth_bbox): truth_box_x = np.broadcast_to( np.array(t[batch][truth_index]["x"], dtype=np.float32), box_x.shape) truth_box_y = np.broadcast_to( np.array(t[batch][truth_index]["y"], dtype=np.float32), box_y.shape) truth_box_w = np.broadcast_to( np.array(t[batch][truth_index]["w"], dtype=np.float32), box_w.shape) truth_box_h = np.broadcast_to( np.array(t[batch][truth_index]["h"], dtype=np.float32), box_h.shape) #truth_box_x.to_gpu(), truth_box_y.to_gpu(), truth_box_w.to_gpu(), truth_box_h.to_gpu() ious.append( multi_box_iou( Box(box_x, box_y, box_w, box_h), Box(truth_box_x, truth_box_y, truth_box_w, truth_box_h))) ious = np.array(ious) best_ious.append(np.max(ious, axis=0)) best_ious = np.array(best_ious) tconf[best_ious > thresh] = conf[best_ious > thresh] conf_learning_scale[best_ious > thresh] = 0 abs_anchors = anchors / np.array([grid_w, grid_h]) for batch in range(batch_size): for truth_box in t[batch]: truth_h = int(float(truth_box["x"]) * grid_w) truth_w = int(float(truth_box["y"]) * grid_h) truth_n = 0 best_iou = 0.0 for anchor_index, abs_anchor in enumerate(abs_anchors): iou = box_iou( Box(0, 0, float(truth_box["w"]), float(truth_box["h"])), Box(0, 0, abs_anchor[0], abs_anchor[1])) if best_iou < iou: best_iou = iou truth_n = anchor_index box_learning_scale[batch, truth_n, :, truth_h, truth_w] = 1.0 tx[batch, truth_n, :, truth_h, truth_w] = float(truth_box["x"]) * grid_w - truth_w ty[batch, truth_n, :, truth_h, truth_w] = float(truth_box["y"]) * grid_h - truth_h tw[batch, truth_n, :, truth_h, truth_w] = float(truth_box["w"]) / abs_anchors[truth_n][0] th[batch, truth_n, :, truth_h, truth_w] = float(truth_box["h"]) / abs_anchors[truth_n][1] tprob[batch, :, truth_n, truth_h, truth_w] = 0 tprob[batch, int(truth_box["label"]), truth_n, truth_h, truth_w] = 1 full_truth_box = Box(float(truth_box["x"]), float(truth_box["y"]), float(truth_box["w"]), float(truth_box["h"])) predicted_box = Box( (x[batch, truth_n, 0, truth_h, truth_w] + truth_w) / grid_w, (y[batch, truth_n, 0, truth_h, truth_w] + truth_h) / grid_h, np.exp(w[batch, truth_n, 0, truth_h, truth_w]) * abs_anchors[truth_n][0], np.exp(h[batch, truth_n, 0, truth_h, truth_w]) * abs_anchors[truth_n][1]) predicted_iou = box_iou(full_truth_box, predicted_box) tconf[batch, truth_n, :, truth_h, truth_w] = predicted_iou conf_learning_scale[batch, truth_n, :, truth_h, truth_w] = 5.0 #box_learning_scale *= 100 #loss #print(np.where(box_learning_scale==1)) x_loss = np.sum((tx - x)**2 * box_learning_scale) / 2 #print(deltas[:,:,0:1,:,:]) deltas[:, :, 0:1, :, :] = ((x - tx) * box_learning_scale * (1 - x) * x).as_ndarray() * 10 #print(deltas.dtype()) #print((x - tx).dtype()) #print(deltas[:,:,0:1,:,:] - ((x - tx) *box_learning_scale * (1 - x) * x)) #print(x-tx) #print(deltas[:,:,0,:,:]) y_loss = np.sum((ty - y)**2 * box_learning_scale) / 2 deltas[:, :, 1:2, :, :] = ((y - ty) * box_learning_scale * (1 - y) * y).as_ndarray() * 10 w_loss = np.sum((tw - np.exp(w))**2 * box_learning_scale) / 2 deltas[:, :, 2:3, :, :] = ((np.exp(w) - tw) * box_learning_scale * np.exp(w)) h_loss = np.sum((th - np.exp(h))**2 * box_learning_scale) / 2 deltas[:, :, 3:4, :, :] = ((np.exp(h) - th) * box_learning_scale * np.exp(h)) c_loss = np.sum((tconf - conf)**2 * conf_learning_scale) / 2 deltas[:, :, 4:5, :, :] = ((conf - tconf) * conf_learning_scale * (1 - conf) * conf).as_ndarray() #print(deltas[:,:,4:5,:,:]) #print(deltas[:,:,4:5,:,:] - (conf - tconf) * conf_learning_scale * (1 - conf) * conf) p_loss = np.sum((tprob - prob)**2) / 2 deltas[:, :, 5:, :, :] = (( ((prob - tprob) * (1 - prob) * prob)).as_ndarray()).transpose(0, 2, 1, 3, 4) * 10 #print(deltas[:,:,5:,:,:] - ((prob - tprob) * (1 - prob) * prob).transpose(0, 2, 1, 3, 4)) if np.isnan(p_loss): p_loss = 0 print( "x_loss: %f y_loss: %f w_loss: %f h_loss: %f c_loss: %f p_loss: %f" % (x_loss, y_loss, w_loss, h_loss, c_loss, p_loss)) loss = x_loss + y_loss + w_loss + h_loss + c_loss + p_loss #loss = p_loss ret = cls._create_node(loss) ret.attrs._output = output ret.attrs._deltas = deltas.reshape(batch_size, bbox * (classes + 5), grid_h, grid_w) # ret.attrs._cells = cells # ret.attrs._bbox = bbox # ret.attrs._classes = classes return ret