def _predict_depth_chainer_backend(self, bgr, depth_bgr=None): bgr_data = np.array([bgr], dtype=np.float32) depth_bgr_data = np.array([depth_bgr], dtype=np.float32) if self.gpu != -1: bgr_data = cuda.to_gpu(bgr_data, device=self.gpu) depth_bgr_data = cuda.to_gpu(depth_bgr_data, device=self.gpu) if LooseVersion(chainer.__version__) < LooseVersion('2.0.0'): bgr = chainer.Variable(bgr_data, volatile=True) depth_bgr = chainer.Variable(depth_bgr_data, volatile=True) self.model(bgr, depth_bgr) else: with chainer.using_config('train', False): with chainer.no_backprop_mode(): bgr = chainer.Variable(bgr_data) depth_bgr = chainer.Variable(depth_bgr_data) self.model(bgr, depth_bgr) proba_img = F.softmax(self.model.mask_score) label_pred = F.argmax(self.model.mask_score, axis=1) depth_pred = F.sigmoid(self.model.depth_score) proba_img = F.transpose(proba_img, (0, 2, 3, 1)) max_proba_img = F.max(proba_img, axis=-1) # squeeze batch axis, gpu -> cpu proba_img = cuda.to_cpu(proba_img.data)[0] max_proba_img = cuda.to_cpu(max_proba_img.data)[0] label_pred = cuda.to_cpu(label_pred.data)[0] depth_pred = cuda.to_cpu(depth_pred.data)[0] # uncertain because the probability is low label_pred[max_proba_img < self.proba_threshold] = self.bg_label # get depth image depth_pred = depth_pred[0, :, :] depth_pred *= (self.model.max_depth - self.model.min_depth) depth_pred += self.model.min_depth return label_pred, proba_img, depth_pred
def test(self, x_l, y_l): y = F.softmax(self.mlp_enc(x_l, test=True)) y_argmax = F.argmax(y, axis=1) acc = F.accuracy(y, y_l) y_l_cpu = cuda.to_cpu(y_l.data) y_argmax_cpu = cuda.to_cpu(y_argmax.data) # Confuction Matrix cm = confusion_matrix(y_l_cpu, y_argmax_cpu) print(cm) # Wrong samples idx = np.where(y_l_cpu != y_argmax_cpu)[0] #print(idx.tolist()) # Generate and Save x_rec = self.mlp_dec(y, test=True) save_incorrect_info(x_rec.data[idx, ], x_l.data[idx, ], y.data[idx, ], y_l.data[idx, ]) # Save model serializers.save_hdf5("./model/mlp_encdec.h5py", self.model) loss = self.forward_for_losses(x_l, y_l, None, test=True) # only measure x_l supervised_loss = loss return acc, supervised_loss
def update(Q, target_Q, opt, samples, gamma=0.99, target_type='double_dqn'): """Update a Q-function with given samples and a target Q-function.""" dtype = chainer.get_dtype() xp = Q.xp obs = xp.asarray([sample[0] for sample in samples], dtype=dtype) action = xp.asarray([sample[1] for sample in samples], dtype=np.int32) reward = xp.asarray([sample[2] for sample in samples], dtype=dtype) done = xp.asarray([sample[3] for sample in samples], dtype=dtype) obs_next = xp.asarray([sample[4] for sample in samples], dtype=dtype) # Predicted values: Q(s,a) y = F.select_item(Q(obs), action) # Target values: r + gamma * max_b Q(s',b) with chainer.no_backprop_mode(): if target_type == 'dqn': next_q = F.max(target_Q(obs_next), axis=1) elif target_type == 'double_dqn': next_q = F.select_item(target_Q(obs_next), F.argmax(Q(obs_next), axis=1)) else: raise ValueError('Unsupported target_type: {}'.format(target_type)) target = reward + gamma * (1 - done) * next_q loss = mean_clipped_loss(y, target) Q.cleargrads() loss.backward() opt.update()
def eps_greedy(self, obs, epsilon): # Check Q function, do argmax. rnd = rng.rand() if rnd > epsilon: obs = self._obs_preprocessor(obs) q_values = self._q.forward(obs) return F.argmax(q_values, axis=1).data[0] else: return rng.randint(0, self._act_dim)
def __call__(self, xs, ts): ts = self._sequential_var(ts) hs = super(BLSTM, self).__call__(xs) loss = 0 ys = [] for h, t in zip(hs, ts): loss += F.softmax_cross_entropy(h, t) ys.append(F.reshape(F.argmax(h, axis=1), t.shape)) accuracy = self._accuracy(ys, ts) return loss, accuracy
def test(self, x_l, y_l): y, z = self.mlp_ae.mlp_encoder(x_l, test=True) y_argmax = F.argmax(y, axis=1) acc = F.accuracy(y, y_l) y_l_cpu = cuda.to_cpu(y_l.data) y_argmax_cpu = cuda.to_cpu(y_argmax.data) # Confuction Matrix cm = confusion_matrix(y_l_cpu, y_argmax_cpu) print(cm) # Wrong samples idx = np.where(y_l_cpu != y_argmax_cpu)[0] return acc
def generate_and_save_wrong_samples(self, x_l, y_l, y): y = F.softmax(y) y_argmax = F.argmax(y, axis=1) y_l_cpu = cuda.to_cpu(y_l.data) y_argmax_cpu = cuda.to_cpu(y_argmax.data) # Confuction Matrix cm = confusion_matrix(y_l_cpu, y_argmax_cpu) print(cm) # Wrong samples idx = np.where(y_l_cpu != y_argmax_cpu)[0] # Generate and Save x_rec = self.ae.decoder(y[idx, ], test=True) x_rec_cpu = cuda.to_cpu(x_rec.data) x_l_cpu = cuda.to_cpu(x_l.data) y_cpu = cuda.to_cpu(y.data) self.save_incorrect_info(x_rec_cpu, x_l_cpu[idx, ], y_cpu[idx, ], y_l_cpu[idx, ])
def compute_double_q_learning_loss(self, l_obs, l_act, l_rew, l_next_obs, l_done): """ :param l_obs: A chainer variable holding a list of observations. Should be of shape N * |S|. :param l_act: A chainer variable holding a list of actions. Should be of shape N. :param l_rew: A chainer variable holding a list of rewards. Should be of shape N. :param l_next_obs: A chainer variable holding a list of observations at the next time step. Should be of shape N * |S|. :param l_done: A chainer variable holding a list of binary values (indicating whether episode ended after this time step). Should be of shape N. :return: A chainer variable holding a scalar loss. """ # Hint: You may want to make use of the following fields: self._discount, self._q, self._qt # Hint2: Q-function can be called by self._q.forward(argument) # Hint3: You might also find https://docs.chainer.org/en/stable/reference/generated/chainer.functions.select_item.html useful "*** YOUR CODE HERE ***" tar_act = F.argmax(self._q.forward(l_next_obs), axis=1) y = l_rew + (1 - l_done) * self._discount * F.select_item(self._qt.forward(l_next_obs), tar_act) q = F.select_item(self._q.forward(l_obs), l_act) loss = F.mean_squared_error(y, q) return loss
def test(self, x_l, y_l): y = self.mlp_enc(x_l, test=True) y_argmax = F.argmax(y, axis=1) acc = F.accuracy(y, y_l) y_l_cpu = cuda.to_cpu(y_l.data) y_argmax_cpu = cuda.to_cpu(y_argmax.data) # Confuction Matrix cm = confusion_matrix(y_l_cpu, y_argmax_cpu) print(cm) # Wrong samples idx = np.where(y_l_cpu != y_argmax_cpu)[0] #print(idx.tolist()) ## Generate and Save #x_rec = self.mlp_dec(y, test=True) #self.save_generate_images(x_rec, idx) loss = self.forward_for_losses(x_l, y_l, None, test=True) # only measure x_l supervised_loss = loss return acc, supervised_loss
def test(self, x_l, y_l): y = F.softmax(self.mlp_ae.mlp_encoder(x_l, test=True)) y_argmax = F.argmax(y, axis=1) acc = F.accuracy(y, y_l) y_l_cpu = cuda.to_cpu(y_l.data) y_argmax_cpu = cuda.to_cpu(y_argmax.data) # Confuction Matrix cm = confusion_matrix(y_l_cpu, y_argmax_cpu) print(cm) # Wrong samples idx = np.where(y_l_cpu != y_argmax_cpu)[0] # Generate and Save x_rec = self.mlp_ae.mlp_decoder(y, self.mlp_encoder.hiddens, test=True) save_incorrect_info(x_rec.data[idx, ], x_l.data[idx, ], y.data[idx, ], y_l.data[idx, ]) # Save model serializers.save_hdf5("./model/mlp_encdec.h5py", self.mlp_ae) return acc
def _double_dqn(self, model, targets, terminals, post_states, rewards): max_a = F.argmax(model(post_states), axis=1) q_targets = F.select_item(targets(post_states), max_a) result = F.reshape(((self._discount**self._nstep) * (1 - terminals)) * q_targets + rewards, (-1, 1)) return result.data
def get_action(self, state): state = np.array([state], dtype=np.float32) with chainer.using_config('train', False): act = F.argmax(self(state)) return int(act.data)
def calc_treewidth_with_GNN(self, G, S, opt, model, prob_bound): """ This method judges whether G[S] has treewidth at most opt by using recursive algorithm. This method uses dp_S to reduce computational complexity. And this method also uses GNN to reduce the number of function calls. Parameters ---------- G : Graph object entire graph S : set a subset of vertices opt : int a value to judge model : GNN object GNN model used in this method prob_bound : float The threshold of probability used in GNN-pruning Returns ------- Result : boolean whether the given graph has treewidth at most opt """ self.func_call_num += 1 if len(S) == 0: return True if len(S) == 1: return self.Q(G.g, set(), S.pop()) <= opt if int_set(S) in self.dp_S: return self.dp_S[int_set(S)] # check whether we should branch from S using evaluation of tw(G[S]) ev_st = time.time() # make the induced graph tmp_G = nx.relabel.convert_node_labels_to_integers(G.g.subgraph(S)) induce_G = util.GraphData.nx_to_graph(tmp_G, "binary") if induce_G.g.number_of_edges() > 1: # predict the treewidth with chainer.using_config('train', False), chainer.using_config('enable_backprop', False): pred = model.predictor([induce_G]) prob = F.max(F.softmax(pred), axis=1).array[0] prediction = F.argmax(F.softmax(pred), axis=1).array[0] if prob > prob_bound: # use the predition if prediction == 0 and 2 * opt < induce_G.g.number_of_nodes() and (self.prune == "upper" or self.prune == "both"): # upper-prune self.prune_num += 1 self.dp_S[int_set(S)] = False self.eval_GNN_time += time.time() - ev_st return False if prediction == 1 and 2 * opt > induce_G.g.number_of_nodes() and (self.prune == "lower" or self.prune == "both"): # lower-prune self.prune_num += 1 self.dp_S[int_set(S)] = True self.eval_GNN_time += time.time() - ev_st return True # memorize GNN evaluation time self.eval_GNN_time += time.time() - ev_st # branch from this state res = False for vertex in S: Qval_check = self.Q(G.g, S - set([vertex]), vertex) <= opt if Qval_check: res = (res or (self.calc_treewidth_with_GNN(G, S - set([vertex]), opt, model, prob_bound))) if res: break self.dp_S[int_set(S)] = res return res
def forward(self, ws, ss, ps, ls, dep_ts=None): batchsize, length = ws.shape split = scanl(lambda x, y: x + y, 0, ls)[1:-1] xp = chainer.cuda.get_array_module(ws[0]) ws = self.emb_word(ws) # (batch, length, word_dim) ss = F.reshape(self.emb_suf(ss), (batchsize, length, -1)) ps = F.reshape(self.emb_prf(ps), (batchsize, length, -1)) hs = F.concat([ws, ss, ps], 2) hs = F.dropout(hs, self.dropout_ratio, train=self.train) fs = hs for qrnn_f in self.qrnn_fs: inp = fs fs = qrnn_f(inp) bs = hs[:, ::-1, :] for qrnn_b in self.qrnn_bs: inp = bs bs = qrnn_b(inp) # fs = [hs] # for qrnn_f in self.qrnn_fs: # inp = F.concat(fs, 2) # fs.append(F.dropout(qrnn_f(inp), 0.32, train=self.train)) # fs = fs[-1] # # bs = [hs[:, ::-1, :]] # for qrnn_b in self.qrnn_bs: # inp = F.concat(bs, 2) # bs.append(F.dropout(qrnn_b(inp), 0.32, train=self.train)) # bs = bs[-1] # hs = F.concat([fs, bs[:, ::-1, :]], 2) _, hs_len, hidden = hs.shape hs = [F.reshape(var, (hs_len, hidden))[:l] for l, var in \ zip(ls, F.split_axis(hs, batchsize, 0))] dep_ys = [ self.biaffine_arc( F.elu(F.dropout(self.arc_dep(h), 0.32, train=self.train)), F.elu(F.dropout(self.arc_head(h), 0.32, train=self.train))) for h in hs ] if dep_ts is not None: heads = dep_ts else: heads = [F.argmax(y, axis=1) for y in dep_ys] heads = F.elu(F.dropout( self.rel_head( F.vstack([F.embed_id(t, h, ignore_label=IGNORE) \ for h, t in zip(hs, heads)])), 0.32, train=self.train)) childs = F.elu( F.dropout(self.rel_dep(F.vstack(hs)), 0.32, train=self.train)) cat_ys = self.biaffine_tag(childs, heads) cat_ys = list(F.split_axis(cat_ys, split, 0)) return cat_ys, dep_ys
print("----------------------------------------------------------------------") print("epoch =", epoch , ", Loss =", sum_loss / len_data, ", Accuracy =", sum_accuracy / len_data) if epoch % 5 == 0: # outupt generated images img_input = [] img_output = [] seg_t = [] for i in range(OUT_PUT_IMG_NUM): images_np, segs_np = make_data.get_data_for_1_batch(i, 1) img_input.append(images_np[0]) images_ = Variable(cuda.to_gpu(images_np)) prob = model(images_) out_seg_argmax = F.argmax(prob, axis=1) img_output.append(out_seg_argmax.data[0]) seg_t.append(segs_np) img_in_np = np.asarray(img_input).transpose((0, 2, 3, 1)) img_out_cp = np.asarray(img_output) seg_t_np = np.asarray(seg_t).transpose((0, 2, 3, 1)) print('type(img_in_np)', type(img_in_np)) print('type(img_out_np)', type(img_out_cp)) seg_t_np_re = seg_t_np.reshape((seg_t_np.shape[0], seg_t_np.shape[1], seg_t_np.shape[2])) print('type(img_X2seg_np)', type(img_out_cp)) img_out_np = cuda.to_cpu(img_out_cp)
def run(self): xp = np if self.gpu < 0 else chainer.cuda.cupy sum_accuracy = 0 sum_loss = 0 while self.train_iter.epoch < self.n_epoch: # train phase batch = self.train_iter.next() if self.flag_train: # step by step update x, t = convert.concat_examples(batch, self.gpu) self.model.cleargrads() y, loss = self.model.loss(x, t) loss.backward() self.optimizer.update() sum_loss += float(loss.data) * len(t) sum_accuracy += float(self.model.accuracy.data) * len(t) # test phase if self.train_iter.is_new_epoch: print('epoch: ', self.train_iter.epoch) print('train mean loss: {}, accuracy: {}'.format( sum_loss / self.N_train, sum_accuracy / self.N_train)) sum_accuracy = 0 sum_loss = 0 batch_test = self.test_iter.next() x, _ = convert.concat_examples(batch_test, self.gpu) with chainer.using_config('train', False), chainer.no_backprop_mode(): f = self.model.predict(x) t = F.argmax(f).data img_org = (x[0] * 255).astype(xp.uint8) stdev = self.noise * (xp.max(x) - xp.min(x)) x_tile = xp.tile(x, (self.N_sample, 1, 1, 1)) noise = xp.random.normal(0, stdev, x_tile.shape).astype(xp.float32) x_tile = x_tile + noise t = xp.tile(t, self.N_sample) with chainer.using_config('train', False): x_tile = chainer.Variable(x_tile) y, loss = self.model.loss(x_tile, t) x_tile.zerograd() loss.backward() total_grad = xp.sum(xp.absolute(x_tile.grad), axis=(0, 1)) grad_max = xp.max(total_grad) grad = ((total_grad / grad_max) * 255).astype(xp.uint8) if self.gpu >= 0: img_org = chainer.cuda.to_cpu(img_org) grad = chainer.cuda.to_cpu(grad) img1 = cv2.cvtColor(img_org.transpose(1, 2, 0), cv2.COLOR_BGR2RGB) img1 = cv2.resize(img1, (320, 320)) img2 = cv2.applyColorMap(grad, cv2.COLORMAP_JET) img2 = cv2.GaussianBlur(img2, (3, 3), 0) img2 = cv2.resize(img2, (320, 320)) img_h = cv2.hconcat([img1, img2]) fname = 'img_sg.png' cv2.imwrite(fname, img_h) self.test_iter.reset() sum_accuracy = 0 sum_loss = 0 try: chainer.serializers.save_npz('net/net.model', self.model) chainer.serializers.save_npz('net/net.state', self.optimizer) print('Successfully saved model') except: print('ERROR: saving model ignored')
def compute_loss(self, mask_score, depth_pred, true_mask, true_depth): seg_loss = self.compute_loss_mask(mask_score, true_mask) reg_loss = self.compute_loss_depth(depth_pred, true_mask, true_depth) # Loss coef = [1, 1] loss = coef[0] * seg_loss + coef[1] * reg_loss if self.xp.isnan(float(loss.data)): raise ValueError('Loss is nan.') batch_size = len(mask_score) assert batch_size == 1 # GPU -> CPU # N, C, H, W -> C, H, W true_mask = cuda.to_cpu(true_mask)[0] mask_pred = cuda.to_cpu(F.argmax(self.score_label, axis=1).data)[0] true_depth = cuda.to_cpu(true_depth)[0] depth_pred = cuda.to_cpu(depth_pred.data)[0] # Evaluate Mask IU mask_iu = fcn.utils.label_accuracy_score([true_mask], [mask_pred], n_class=2)[2] # Evaluate Depth Accuracy depth_acc = {} for thresh in [ 0.01, 0.02, 0.03, 0.04, 0.05, 0.07, 0.10, 0.15, 0.20, 0.25, 0.30, 0.40, 0.50, 0.70, 1.00 ]: t_lbl_fg = true_mask > 0 p_lbl_fg = mask_pred > 0 if np.sum(t_lbl_fg) == 0 and np.sum(p_lbl_fg) == 0: acc = 1.0 elif np.sum(t_lbl_fg) == 0: acc = 0.0 else: # {TP and (|error| < thresh)} / (TP or FP or FN) true_depth_cp = np.copy(true_depth) true_depth_cp[np.isnan(true_depth_cp)] = np.inf numer = np.sum( np.logical_and( np.logical_and(t_lbl_fg, p_lbl_fg), np.abs(true_depth_cp - depth_pred) < thresh)) denom = np.sum(np.logical_or(t_lbl_fg, p_lbl_fg)) acc = 1. * numer / denom depth_acc['%.2f' % thresh] = acc chainer.reporter.report( { 'loss': loss, 'seg_loss': seg_loss, 'reg_loss': reg_loss, 'miou': mask_iu, 'depth_acc<0.01': depth_acc['0.01'], 'depth_acc<0.02': depth_acc['0.02'], 'depth_acc<0.03': depth_acc['0.03'], 'depth_acc<0.04': depth_acc['0.04'], 'depth_acc<0.05': depth_acc['0.05'], 'depth_acc<0.07': depth_acc['0.07'], 'depth_acc<0.10': depth_acc['0.10'], 'depth_acc<0.15': depth_acc['0.15'], 'depth_acc<0.20': depth_acc['0.20'], 'depth_acc<0.25': depth_acc['0.25'], 'depth_acc<0.30': depth_acc['0.30'], 'depth_acc<0.40': depth_acc['0.40'], 'depth_acc<0.50': depth_acc['0.50'], 'depth_acc<0.70': depth_acc['0.70'], 'depth_acc<1.00': depth_acc['1.00'], }, self) return loss
def compute_loss(self, s, a, r, new_s, done, loss_log=False): if self.net_type == "full": s = s.reshape(self.batch_size, self.input_slides*self.size*self.size) new_s = new_s.reshape(self.batch_size, self.input_slides*self.size*self.size) #gpu if self.gpu >= 0: s = cuda.to_gpu(s) new_s = cuda.to_gpu(new_s) if chainer.__version__ >= "2.0.0": s = Variable(s) new_s = Variable(new_s) else: s = Variable(s, volatile='auto') new_s = Variable(new_s, volatile='auto') q_value = self.q(s) with chainer.no_backprop_mode(): if self.mode == "regularize": tg_q_value = self.q(new_s) elif self.mode == "target_mix": tg_q_value = (1.0-self.mix_rate) * self.q(new_s) + self.mix_rate * self.fixed_q(new_s) elif self.mode == "default": tg_q_value = self.fixed_q(new_s) #print "tg_q_value[0]", tg_q_value[0].data if self.gpu >= 0: a = cuda.to_gpu(a) r = cuda.to_gpu(r) done = cuda.to_gpu(done) if chainer.__version__ >= "2.0.0": a = Variable(a) else: a = Variable(a, volatile='auto') argmax_a = F.argmax(tg_q_value, axis=1) #print a #print r q_action_value = F.select_item(q_value, a) #print "q_action_value", q_action_value.data target = r + self.discount * (1.0 - done) * F.select_item(tg_q_value, argmax_a) #print "target", target.data #target is float32 q_action_value = F.reshape(q_action_value, (-1, 1)) target = F.reshape(target, (-1, 1)) loss_sum = F.sum(F.huber_loss(q_action_value, target, delta=1.0)) loss = loss_sum / q_action_value.shape[0] #print "loss_a", loss.data if self.mode == "regularize" or loss_log == True: if self.penalty_function == "value": y = q_value with chainer.no_backprop_mode(): t = self.fixed_q(s) if self.penalty_function == "action_value": y = q_action_value with chainer.no_backprop_mode(): t = F.select_item(self.fixed_q(s), a) t = F.reshape(t, (-1, 1)) if self.penalty_function == "max_action_value": y = F.select_item(self.q(new_s), argmax_a) y = F.reshape(y, (-1, 1)) with chainer.no_backprop_mode(): t = F.select_item(self.fixed_q(new_s), argmax_a) t = F.reshape(t, (-1, 1)) if self.penalty_type == "huber": if self.final_penalty_cut == 1: penalty_sum = F.sum((1.0 - done)*F.huber_loss(y, t, delta=1.0)) else: penalty_sum = F.sum(F.huber_loss(y, t, delta=1.0)) penalty = penalty_sum / (y.shape[0]*y.shape[1]) if self.penalty_type == "mean_squared": penalty = F.mean_squared_error(y, t) if loss_log == True: #y_data = cuda.to_cpu(y.data) #t_data = cuda.to_cpu(t.data) return loss, penalty #return loss, penalty, np.average(y_data), np.std(y_data), np.average(t_data), np.std(t_data) if penalty.data > self.threshold: #print "-------------on----------------" loss = loss + self.penalty_weight * penalty #print "loss_b", loss.data return loss
def predict_depth(self, rgb, mask_score, depth_viz, rgb_pool5): # conv_depth_1 h = F.relu(self.conv_depth_1_1(depth_viz)) h = F.relu(self.conv_depth_1_2(h)) h = F.max_pooling_2d(h, 2, stride=2, pad=0) depth_pool1 = h # 1/2 # conv_depth_2 h = F.relu(self.conv_depth_2_1(depth_pool1)) h = F.relu(self.conv_depth_2_2(h)) h = F.max_pooling_2d(h, 2, stride=2, pad=0) depth_pool2 = h # 1/4 # conv_depth_3 h = F.relu(self.conv_depth_3_1(depth_pool2)) h = F.relu(self.conv_depth_3_2(h)) h = F.relu(self.conv_depth_3_3(h)) h = F.max_pooling_2d(h, 2, stride=2, pad=0) depth_pool3 = h # 1/8 # conv_depth_4 h = F.relu(self.conv_depth_4_1(depth_pool3)) h = F.relu(self.conv_depth_4_2(h)) h = F.relu(self.conv_depth_4_3(h)) h = F.max_pooling_2d(h, 2, stride=2, pad=0) depth_pool4 = h # 1/16 # conv_depth_5 h = F.relu(self.conv_depth_5_1(depth_pool4)) h = F.relu(self.conv_depth_5_2(h)) h = F.relu(self.conv_depth_5_3(h)) h = F.max_pooling_2d(h, 2, stride=2, pad=0) depth_pool5 = h # 1/32 if self.masking is True: # Apply negative_mask to depth_pool5 # (N, C, H, W) -> (N, H, W) mask_pred_tmp = F.argmax(self.mask_score, axis=1) # (N, H, W) -> (N, 1, H, W), float required for resizing mask_pred_tmp = mask_pred_tmp[:, None, :, :].data.astype( self.xp.float32) # 1/1 resized_mask_pred = F.resize_images( mask_pred_tmp, (depth_pool5.shape[2], depth_pool5.shape[3])) # 1/32 depth_pool5_cp = depth_pool5 masked_depth_pool5 = depth_pool5_cp * \ (resized_mask_pred.data == 0.0).astype(self.xp.float32) else: masked_depth_pool5 = depth_pool5 if self.concat is True: # concatenate rgb_pool5 and depth_pool5 concat_pool5 = F.concat((rgb_pool5, masked_depth_pool5), axis=1) # concat_fc6 h = F.relu(self.concat_fc6(concat_pool5)) h = F.dropout(h, ratio=.5) concat_fc6 = h # 1/32 else: # concat_fc6 h = F.relu(self.depth_fc6(masked_depth_pool5)) h = F.dropout(h, ratio=.5) concat_fc6 = h # 1/32 # concat_fc7 h = F.relu(self.concat_fc7(concat_fc6)) h = F.dropout(h, ratio=.5) concat_fc7 = h # 1/32 # depth_score_fr h = self.depth_score_fr(concat_fc7) depth_score_fr = h # 1/32 # depth_score_pool3 scale_depth_pool3 = 0.0001 * depth_pool3 h = self.depth_score_pool3(scale_depth_pool3) depth_score_pool3 = h # 1/8 # depth_score_pool4 scale_depth_pool4 = 0.01 * depth_pool4 h = self.depth_score_pool4(scale_depth_pool4) depth_score_pool4 = h # 1/16 # depth upscore2 h = self.depth_upscore2(depth_score_fr) depth_upscore2 = h # 1/16 # depth_score_pool4c h = depth_score_pool4[:, :, 5:5 + depth_upscore2.data.shape[2], 5:5 + depth_upscore2.data.shape[3]] depth_score_pool4c = h # 1/16 # depth_fuse_pool4 h = depth_upscore2 + depth_score_pool4c depth_fuse_pool4 = h # 1/16 # depth_upscore_pool4 h = self.depth_upscore_pool4(depth_fuse_pool4) depth_upscore_pool4 = h # 1/8 # depth_score_pool3c h = depth_score_pool3[:, :, 9:9 + depth_upscore_pool4.data.shape[2], 9:9 + depth_upscore_pool4.data.shape[3]] depth_score_pool3c = h # 1/8 # depth_fuse_pool3 h = depth_upscore_pool4 + depth_score_pool3c depth_fuse_pool3 = h # 1/8 # depth_upscore8 h = self.depth_upscore8(depth_fuse_pool3) depth_upscore8 = h # 1/1 # depth_score h = depth_upscore8[:, :, 31:31 + rgb.shape[2], 31:31 + rgb.shape[3]] depth_score = h # 1/1 return depth_score
def uniform(self, x): xp = cuda.get_array_module(x) result = xp.random.random((x.shape[0], x.shape[1])) return F.argmax(result, axis=1)
def main(): parser = ArgumentParser() parser.add_argument('--gpu', '-g', default=-1, type=int, help='GPU ID') parser.add_argument('--embedding_file', '-e', metavar="FILE", default=None) parser.add_argument('--training_file', metavar="FILE", default=None) parser.add_argument('--test_file', metavar="FILE", default=None) parser.add_argument('--voc_limit', metavar="INT", type=int, default=100000) parser.add_argument('--num_epoch', metavar="INT", type=int, default=10) args = parser.parse_args() ''' if args.embedding_file is None: args.embedding_file = "." print(args.gpu) cuda.check_cuda_available() if args.embedding_file is not None: print(args.embedding_file) if args.training_file is not None: print(args.training_file) if args.test_file is not None: print(args.test_file) print(args.voc_limit) print(args.num_epoch) ''' # train_data, train_label = Utils.load_data_seq(args.training_file) # test_data, test_label = Utils.load_data_seq(args.test_file) train_data, train_label = Utils.load_data_seq(TRAINING_FILE) test_data, test_label = Utils.load_data_seq(TEST_FILE) # embedding_loader = Embedding_Loader(embedding_file_path = args.embedding_file) embedding_loader = Embedding_Loader(embedding_file_path=EMBEDDING_FILE) embedding_l = embedding_loader.load_embedding(voc_limit=VOC_LIMIT) embedding_l.disable_update() print("1") train_mask = [] test_mask = [] train_max_seq_length, train_max_doc_length, train_data = embedding_loader.documents_to_ids( train_data) test_max_seq_length, test_max_doc_length, test_data = embedding_loader.documents_to_ids( test_data) print("2") # print(train_max_length) # print(test_max_length) # print(np.array(train_data).shape) # print(np.array(test_data).shape) max_seq_length = train_max_seq_length if train_max_seq_length > test_max_seq_length else test_max_seq_length max_doc_length = train_max_doc_length if train_max_doc_length > test_max_doc_length else test_max_doc_length print('max_doc_length:{}'.format(max_doc_length)) print('max_seq_length:{}'.format(max_seq_length)) print(len(train_data)) print(len(test_data)) train_data = Utils.zero_padding_doc_cut(train_data, max_seq_length, max_doc_length, 100, 40) test_data = Utils.zero_padding_doc_cut(test_data, max_seq_length, max_doc_length, 100, 40) print("3") print(train_data.shape) print(test_data.shape) ''' train_max_length, train_data = embedding_loader.seq_to_ids(train_data) test_max_length, test_data = embedding_loader.seq_to_ids(test_data) max_length = train_max_length if train_max_length > test_max_length else test_max_length train_data = Utils.zero_padding(train_data, max_length) test_data = Utils.zero_padding(test_data, max_length) ''' train_data = embedding_l(train_data) test_data = embedding_l(test_data) train_size = train_data.shape[0] validation_size = int(train_size * VALIDATION_RATIO) validation_data = train_data[:validation_size] train_data = train_data[validation_size:] validation_label = train_label[:validation_size] train_label = train_label[validation_size:] print("end embedding_l") print(train_data.shape) print(validation_data.shape) print(test_data.shape) _, doc_len, seq_len, emb_len = train_data.shape train_data = F.reshape( train_data, [-1, train_data.shape[2] * train_data.shape[1] * train_data.shape[3]]) # train_data = tuple_dataset.TupleDataset(train_data.array[:,:1000], train_label) train_data = tuple_dataset.TupleDataset(train_data.array, train_label) validation_data = F.reshape(validation_data, [ -1, validation_data.shape[2] * validation_data.shape[1] * validation_data.shape[3] ]) # train_data = tuple_dataset.TupleDataset(train_data.array[:,:1000], train_label) validation_data = tuple_dataset.TupleDataset(validation_data.array, validation_label) test_data = F.reshape( test_data, [-1, test_data.shape[2] * test_data.shape[1] * test_data.shape[3]]) ''' # test_data = tuple_dataset.TupleDataset(test_data.array[:,:1000], test_label) test_data = tuple_dataset.TupleDataset(test_data.array, test_label) ''' print("end tuple_dataset") batch_size = 128 train_iter = chainer.iterators.SerialIterator(train_data, batch_size) # test_iter = chainer.iterators.SerialIterator(test_data, batch_size, repeat=False, shuffle=False) validation_iter = chainer.iterators.SerialIterator(validation_data, batch_size, repeat=False, shuffle=False) # model = L.Classifier(MLP(100, 50, 1)) model = L.Classifier(Han(doc_len, seq_len, emb_len)) # model = MLP(100, 50, 1) # model.to_gpu(args.gpu) # optimizer = chainer.optimizers.SGD() optimizer = chainer.optimizers.Adam() optimizer.setup(model) # print("optimizer") # updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu) # trainer = training.Trainer(updater, (args.num_epoch, 'epoch'), out="result") # trainer.extend(extensions.Evaluator(test_iter, model, device=args.gpu)) updater = training.StandardUpdater(train_iter, optimizer, device=GPU) trainer = training.Trainer(updater, (NUM_EPOCH, 'epoch'), out="result") # trainer.extend(extensions.Evaluator(test_iter, model, device=GPU)) trainer.extend(extensions.Evaluator(validation_iter, model, device=GPU)) # trainer.extend(extensions.dump_graph('main/loss')) # trainer.extend(extensions.snapshot(), trigger=(args.num_epoch, 'epoch')) trainer.extend(extensions.LogReport()) trainer.extend( extensions.PrintReport([ 'epoch', 'main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy' ])) trainer.extend(extensions.ProgressBar()) trainer.extend(extensions.snapshot_object(model, 'best_model'), trigger=chainer.training.triggers.MinValueTrigger( 'validation/main/loss')) print("start running...") trainer.run() print("finished running...") print("start testing...") chainer.serializers.load_npz('result/best_model', model) model.to_cpu() result = model.predictor(test_data) pred = F.argmax(result, axis=1) print(f1_score(test_label.flatten(), pred.data.flatten(), average='macro')) FILE_NAME = 'tmp_output/tmp_output_' current_datetime = datetime.datetime.now() current_time_str = current_datetime.strftime('%Y_%m_%d_%H:%M:%S') filename = FILE_NAME + current_time_str + '.txt' with open(filename, 'w') as file: file.write( str( f1_score(test_label.flatten(), pred.data.flatten(), average='macro'))) file.write('\n') print("finished testing...")
def run(self): self.__debug(self.__config.get("episodes")) # Build OpenAI Gym environment environment = gym.make(self.__config.get("environment")) if hasattr(environment, 'env'): environment = environment.env obvSpace = environment.observation_space.low.size actSpace = environment.action_space.n # Get parameter configuration. replayStartThreshold = self.__config.get("replay_start_threshold") minimumEpsilon = self.__config.get("minimum_epsilon") epsilonDecayPeriod = self.__config.get( "epsilon_decay_period") # Iterations rewardScaling = self.__config.get("reward_scaling") minibatchSize = self.__config.get("minibatch_size") hiddenLayers = [self.__config.get("hidden_layers")] * 2 gamma = self.__config.get("gamma") tau = self.__config.get("tau") sigma = self.__config.get("sigma") networkUpdateFrequency = self.__config.get("network_update_frequency") maximumNumberOfSteps = self.__config.get("maximum_timesteps") # Initialise storage queues. replayBuffer = ReplayBuffer(capacity=10**6) episodeTotals = deque( maxlen=self.__config.get("episode_history_averaging")) variationalLosses = deque( maxlen=self.__config.get("episode_history_averaging")) bellmanLosses = deque( maxlen=self.__config.get("episode_history_averaging")) with tf.Session() as session: _q = self.VariationalQFunction(obvSpace, actSpace, hiddenLayers, session, optimiser=tf.train.AdamOptimizer( self.__config.get("loss_rate")), scope="primary") _qTarget = self.VariationalQFunction( obvSpace, actSpace, hiddenLayers, session, optimiser=tf.train.AdamOptimizer(1e-3), scope="target") _n = self.NormalSampler(*_q.get_shape()) self.__n = _n session.run(tf.global_variables_initializer()) _qTarget.update(_q) # Episode loop. iteration = 0 self.__debug("Starting episode loop...") for episode in range(self.__config.get("episodes")): self.__debug("\n\n--- EPISODE {} ---".format(episode)) currentState = environment.reset() episodeRewards = 0 running = True timestep = 0 start = time() # Run an iteration of the current episode. while running and timestep < maximumNumberOfSteps: self.__debug("Episode {}: Timestep {}".format( episode, timestep)) # Select and execute the next action. action = self.__generateAction(_q, currentState) nextState, reward, completed, _ = environment.step(action) environment.render() episodeRewards += reward # Save the experience. experience = ReplayBuffer.Experience( currentState, action, reward * rewardScaling, completed, nextState) replayBuffer.add(experience) currentState = nextState # Sample and replay minibatch if threshold reached. if (len(replayBuffer) >= replayStartThreshold): minibatch = replayBuffer.randomSample(minibatchSize) noise_W, noise_b = _n.sample(minibatchSize) _alpha = _qTarget.computeValue(minibatch["nextStates"], noise_W, noise_b) if self.__doubleVDQN: _beta = _q.computeValue(minibatch["nextStates"], noise_W, noise_b) _qNext = functions.select_item( _alpha, functions.argmax(_beta, axis=1)) else: _qNext = functions.max(_alpha, axis=1) # _qNext = np.max(_alpha, axis=1) # .array _qTargetValue = gamma * _qNext.array * ( 1 - minibatch["completes"]) + minibatch["rewards"] _loss = _q.train(minibatch["states"], minibatch["actions"], _qTargetValue) variationalLosses.append(_loss["loss"]) noise_W_dup, noise_b_dup = noise_W, noise_b _prediction = _q.computeValue(minibatch["states"], noise_W_dup, noise_b_dup) _predictedAction = _prediction[ np.arange(minibatchSize), minibatch["actions"]] _bellmanLoss = np.mean( (_predictedAction - _qTargetValue)**2) bellmanLosses.append(_bellmanLoss) # Update the target Q network. if iteration % networkUpdateFrequency == 0: _qTarget.update(_q) iteration += 1 timestep += 1 running = not completed # Run post episode event handler. episodeTotals.append(episodeRewards) self.__config.get("post_episode")({ "episode": episode, "iteration": iteration, "reward": episodeRewards, "meanPreviousRewards": np.mean(episodeTotals), "duration": time() - start, "variationalLosses": np.mean(variationalLosses), "bellmanLosses": np.mean(bellmanLosses), })
delta_ammo = new_ammo - old_ammo old_ammo = new_ammo # reward += 0.05 * delta_health # reward += 0.02 * delta_ammo train_image = cuda.to_gpu( (state.screen_buffer.astype(np.float32).transpose((2, 0, 1))), device=args.gpu) #reward, terminal = game.process(screen) train_image = Variable( train_image.reshape((1, ) + train_image.shape) / 127.5 - 1, volatile=True) score = action_q(train_image, train=False) best_idx = int(F.argmax(score).data) # action = game.randomize_action(best, random_probability) action = randomize_action(actions[best_idx], random_probability) state_pool[index] = cuda.to_cpu(train_image.data) action_pool[index] = actions.index(action) reward_pool[index - 1] = reward average_reward = average_reward * 0.9 + reward * 0.1 # print(reward) if frame % 100 == 0: print "average reward: ", average_reward terminal_pool[index - 1] = 0 frame += 1 save_iter -= 1
def compute_loss(self, score_label, depth_pred, label_gt, depth_gt): seg_loss = self.compute_loss_label(score_label, label_gt) reg_loss = self.compute_loss_depth(depth_pred, label_gt, depth_gt) # Loss # XXX: What is proper loss function? coef = [1, 1] loss = coef[0] * seg_loss + coef[1] * reg_loss if self.xp.isnan(float(loss.data)): raise ValueError('Loss is nan.') batch_size = len(score_label) assert batch_size == 1 # GPU -> CPU # N, C, H, W -> C, H, W label_gt = cuda.to_cpu(label_gt)[0] label_pred = cuda.to_cpu(F.argmax(score_label, axis=1).data)[0] depth_gt = cuda.to_cpu(depth_gt)[0] depth_pred = cuda.to_cpu(depth_pred.data)[0] # Evaluate Mean IU miou = fcn.utils.label_accuracy_score([label_gt], [label_pred], n_class=self.n_class)[2] # Evaluate Depth Accuracy depth_acc = {} for thresh in [ 0.01, 0.02, 0.03, 0.04, 0.05, 0.07, 0.10, 0.15, 0.20, 0.25, 0.30, 0.40, 0.50, 0.70, 1.00 ]: t_lbl_fg = label_gt > 0 p_lbl_fg = label_pred > 0 if np.sum(t_lbl_fg) == 0 and np.sum(p_lbl_fg) == 0: acc = 1.0 elif np.sum(t_lbl_fg) == 0: acc = np.nan else: # {TP and (|error| < thresh)} / (TP or FP or FN) depth_gt_cp = np.copy(depth_gt) depth_gt_cp[np.isnan(depth_gt_cp)] = np.inf numer = np.sum( np.logical_and(np.logical_and(t_lbl_fg, p_lbl_fg), np.abs(depth_gt_cp - depth_pred) < thresh)) denom = np.sum(np.logical_or(t_lbl_fg, p_lbl_fg)) acc = 1.0 * numer / denom depth_acc['%.2f' % thresh] = acc chainer.reporter.report( { 'loss': loss, 'seg_loss': seg_loss, 'reg_loss': reg_loss, 'miou': miou, 'depth_acc<0.01': depth_acc['0.01'], 'depth_acc<0.02': depth_acc['0.02'], 'depth_acc<0.03': depth_acc['0.03'], 'depth_acc<0.04': depth_acc['0.04'], 'depth_acc<0.05': depth_acc['0.05'], 'depth_acc<0.07': depth_acc['0.07'], 'depth_acc<0.10': depth_acc['0.10'], 'depth_acc<0.15': depth_acc['0.15'], 'depth_acc<0.20': depth_acc['0.20'], 'depth_acc<0.25': depth_acc['0.25'], 'depth_acc<0.30': depth_acc['0.30'], 'depth_acc<0.40': depth_acc['0.40'], 'depth_acc<0.50': depth_acc['0.50'], 'depth_acc<0.70': depth_acc['0.70'], 'depth_acc<1.00': depth_acc['1.00'], }, self) return loss
def predict(self,word,hx=None): test_vec = word_to_index(word) test_vec = one_hot_encoding(test_vec).astype(np.float32) res = self([test_vec],hx)[0] return F.argmax(res)
def predict_depth(self, rgb, mask_score, depth_viz, rgb_pool5): # conv_depth_1 h = F.relu(self.conv_depth_1_1(depth_viz)) h = F.relu(self.conv_depth_1_2(h)) h = F.max_pooling_2d(h, 2, stride=2, pad=0) depth_pool1 = h # 1/2 # conv_depth_2 h = F.relu(self.conv_depth_2_1(depth_pool1)) h = F.relu(self.conv_depth_2_2(h)) h = F.max_pooling_2d(h, 2, stride=2, pad=0) depth_pool2 = h # 1/4 # conv_depth_3 h = F.relu(self.conv_depth_3_1(depth_pool2)) h = F.relu(self.conv_depth_3_2(h)) h = F.relu(self.conv_depth_3_3(h)) h = F.max_pooling_2d(h, 2, stride=2, pad=0) depth_pool3 = h # 1/8 # conv_depth_4 h = F.relu(self.conv_depth_4_1(depth_pool3)) h = F.relu(self.conv_depth_4_2(h)) h = F.relu(self.conv_depth_4_3(h)) h = F.max_pooling_2d(h, 2, stride=2, pad=0) depth_pool4 = h # 1/16 # conv_depth_5 h = F.relu(self.conv_depth_5_1(depth_pool4)) h = F.relu(self.conv_depth_5_2(h)) h = F.relu(self.conv_depth_5_3(h)) h = F.max_pooling_2d(h, 2, stride=2, pad=0) depth_pool5 = h # 1/32 if self.masking is True: # Apply negative_mask to depth_pool5 # (N, C, H, W) -> (N, H, W) mask_pred_tmp = F.argmax(self.mask_score, axis=1) # (N, H, W) -> (N, 1, H, W), float required for resizing mask_pred_tmp = mask_pred_tmp[:, None, :, :].data.astype( self.xp.float32) # 1/1 resized_mask_pred = F.resize_images( mask_pred_tmp, (depth_pool5.shape[2], depth_pool5.shape[3])) # 1/32 depth_pool5_cp = depth_pool5 masked_depth_pool5 = depth_pool5_cp * \ (resized_mask_pred.data == 0.0).astype(self.xp.float32) else: masked_depth_pool5 = depth_pool5 if self.concat is True: # concatenate rgb_pool5 and depth_pool5 concat_pool5 = F.concat((rgb_pool5, masked_depth_pool5), axis=1) # concat_fc6 h = F.relu(self.concat_fc6(concat_pool5)) h = F.dropout(h, ratio=.5) concat_fc6 = h # 1/32 else: # concat_fc6 h = F.relu(self.depth_fc6(masked_depth_pool5)) h = F.dropout(h, ratio=.5) concat_fc6 = h # 1/32 # concat_fc7 h = F.relu(self.concat_fc7(concat_fc6)) h = F.dropout(h, ratio=.5) concat_fc7 = h # 1/32 # depth_score_fr h = self.depth_score_fr(concat_fc7) depth_score_fr = h # 1/32 # depth_score_pool3 scale_depth_pool3 = 0.0001 * depth_pool3 h = self.depth_score_pool3(scale_depth_pool3) depth_score_pool3 = h # 1/8 # depth_score_pool4 scale_depth_pool4 = 0.01 * depth_pool4 h = self.depth_score_pool4(scale_depth_pool4) depth_score_pool4 = h # 1/16 # depth upscore2 h = self.depth_upscore2(depth_score_fr) depth_upscore2 = h # 1/16 # depth_score_pool4c h = depth_score_pool4[:, :, 5:5 + depth_upscore2.data.shape[2], 5:5 + depth_upscore2.data.shape[3]] depth_score_pool4c = h # 1/16 # depth_fuse_pool4 h = depth_upscore2 + depth_score_pool4c depth_fuse_pool4 = h # 1/16 # depth_upscore_pool4 h = self.depth_upscore_pool4(depth_fuse_pool4) depth_upscore_pool4 = h # 1/8 # depth_score_pool3c h = depth_score_pool3[:, :, 9:9 + depth_upscore_pool4.data.shape[2], 9:9 + depth_upscore_pool4.data.shape[3]] depth_score_pool3c = h # 1/8 # depth_fuse_pool3 h = depth_upscore_pool4 + depth_score_pool3c depth_fuse_pool3 = h # 1/8 # depth_upscore8 h = self.depth_upscore8(depth_fuse_pool3) depth_upscore8 = h # 1/1 # depth_score h = depth_upscore8[:, :, 31:31 + rgb.shape[2], 31:31 + rgb.shape[3]] depth_score = h # 1/1 return depth_score
def max(self, x): x_r = F.reshape(x, (x.shape[0] * x.shape[1], x.shape[2])) out = self.regressor.predict(x_r) result = F.reshape(out, (x.shape[0], x.shape[1])) return F.argmax(result, axis=1)
def Test(model, embed): out = open(OUTPUT_PATH + 'output_haihun.csv', "w", encoding='UTF8') out.write('"id","after"\n') test = open(INPUT_PATH + "en_test.csv", encoding='UTF8') line = test.readline().strip() base = open(OUTPUT_PATH + 'baseline4_en.csv', "r", encoding='UTF8') line_b = base.readline().strip() sentenceID = '0' sentence = [] sentence_b = [] while 1: line = test.readline().strip() line_b = base.readline().strip() if line == '': break #test pos = line.find(',') i1 = line[:pos] line = line[pos + 1:] pos = line.find(',') i2 = line[:pos] line = line[pos + 1:] line = line[1:-1] #baseline pos_b = line_b.find(',') line_b = line_b[pos_b + 1:] pos_b = line_b.find(',') line_b = line_b[pos_b + 1:] line_b = line_b[1:-1] if line.isdigit(): if len(line) == 4: line = "<YEAR>" elif len(line) == 3: line = "<3_DIGIT>" elif len(line) == 2: line = "<2_DIGIT>" if i1 == sentenceID: sentence.append(line) sentence_b.append(line_b) else: for p, word in enumerate(sentence): if '-' == word or '—' == word: if word == '-' or word == '—': v, flg = getV(p, sentence, embed) if flg: source = F.reshape(v, (1, n_dim * 2)) pred = F.argmax(model.fwd(source)) print("pred:", pred.data) if pred.data == 1: sentence_b[p] = 'to' else: sentence_b[p] = word out.write('"' + sentenceID + '_' + str(p) + '",') out.write('"' + sentence_b[p] + '"') out.write('\n') sentence = [line] sentence_b = [line_b] sentenceID = i1 for p, word in enumerate(sentence): if '-' == word or '—' == word: if word == '-' or word == '—': v, flg = getV(p, sentence, embed) if flg: source = F.reshape(v, (1, n_dim * 2)) pred = F.argmax(model.fwd(source)) print("pred:", pred.data) if pred.data == 1: sentence_b[p] = 'to' else: sentence_b[p] = word out.write('"' + sentenceID + '_' + str(p) + '",') out.write('"' + sentence_b[p] + '"') out.write('\n') out.close() test.close() base.close()
def evaluate(self): def _do_one(inputs): t, action, result, length, hopeful, terminate = inputs if terminate or t >= length: return 0, 0, 0, 0, 0, 0, True reward, reward_hopeful = 0, 0 buzz, correct, rush, late = 0, 0, 0, 0 if action == 1: terminate = True buzz = 1 if result == 1: reward = 10 correct = 1 else: reward = -5 if hopeful: rush = 1 # reward -= 10 elif t == length - 1: if hopeful: late = 1 # reward = -10 reward_hopeful = reward if hopeful else 0 return reward, reward_hopeful, buzz, correct, rush, late, terminate progress_bar = ProgressBar(self.eval_iter.size, unit_iteration=True) epoch_stats = [0, 0, 0, 0, 0, 0] num_examples = 0 num_hopeful = 0 for i in range(self.eval_iter.size): batch = self.eval_iter.next_batch(self.model.xp) length, batch_size, _ = batch.vecs.shape qvalues = [self.model(vec) for vec in batch.vecs] # length * batch_size * 2 actions = F.argmax(F.stack(qvalues), axis=2).data # length * batch_size hopeful = [any(x == 1) for x in batch.results.T] # batch_size terminate = [False for _ in range(batch_size)] num_examples += batch_size num_hopeful += sum(hopeful) for t in range(length): tt = [t for _ in range(batch_size)] inputs = zip(tt, actions[t], batch.results[t], batch.mask[t], hopeful, terminate) returns = map(_do_one, inputs) returns = list(map(list, zip(*returns))) terminate = returns[-1] returns = returns[:-1] epoch_stats = list( map(lambda x, y: x + sum(y), epoch_stats, returns)) if all(terminate): break # end of batch progress_bar(*self.eval_iter.epoch_detail) # end of epoch self.eval_iter.finalize(reset=True) progress_bar.finalize() epoch_stats[0] /= num_examples # reward / num_total epoch_stats[1] /= num_hopeful # reward_hopeful / num_hopeful epoch_stats = [num_examples, num_hopeful] + epoch_stats epoch_stats = BuzzStats(*epoch_stats) return epoch_stats
def original(self, hs, ys): '''Decoder forward :param Variable hs: :param Variable ys: :return: ''' self.loss = None # prepare input and output word sequences with sos/eos IDs eos = self.xp.array([self.eos], 'i') sos = self.xp.array([self.sos], 'i') ys_in = [F.concat([sos, y], axis=0) for y in ys] ys_out = [F.concat([y, eos], axis=0) for y in ys] # padding for ys with -1 # pys: utt x olen pad_ys_in = F.pad_sequence(ys_in, padding=self.eos) pad_ys_out = F.pad_sequence(ys_out, padding=-1) # get dim, length info batch = pad_ys_out.shape[0] olength = pad_ys_out.shape[1] logging.info(self.__class__.__name__ + ' input lengths: ' + str(self.xp.array([h.shape[0] for h in hs]))) logging.info(self.__class__.__name__ + ' output lengths: ' + str(self.xp.array([y.shape[0] for y in ys_out]))) # initialization c_list = [None] # list of cell state of each layer z_list = [None] # list of hidden state of each layer for l in six.moves.range(1, self.dlayers): c_list.append(None) z_list.append(None) att_w = None z_all = [] self.att.reset() # reset pre-computation of h # pre-computation of embedding eys = self.embed(pad_ys_in) # utt x olen x zdim eys = F.separate(eys, axis=1) # loop for an output sequence for i in six.moves.range(olength): att_c, att_w = self.att(hs, z_list[0], att_w) if i > 0 and random.random() < self.sampling_probability: logging.info(' scheduled sampling ') z_out = self.output(z_all[-1]) z_out = F.argmax(F.log_softmax(z_out), axis=1) z_out = self.embed(z_out) ey = F.hstack((z_out, att_c)) # utt x (zdim + hdim) else: ey = F.hstack((eys[i], att_c)) # utt x (zdim + hdim) c_list[0], z_list[0] = self.lstm0(c_list[0], z_list[0], ey) for l in six.moves.range(1, self.dlayers): c_list[l], z_list[l] = self['lstm%d' % l](c_list[l], z_list[l], z_list[l - 1]) z_all.append(z_list[-1]) z_all = F.reshape(F.stack(z_all, axis=1), (batch * olength, self.dunits)) # compute loss y_all = self.output(z_all) self.loss = F.softmax_cross_entropy(y_all, F.flatten(pad_ys_out)) # -1: eos, which is removed in the loss computation self.loss *= (np.mean([len(x) for x in ys_in]) - 1) acc = F.accuracy(y_all, F.flatten(pad_ys_out), ignore_label=-1) logging.info('att loss:' + str(self.loss.data)) # show predicted character sequence for debug if self.verbose > 0 and self.char_list is not None: y_hat = F.reshape(y_all, (batch, olength, -1)) y_true = pad_ys_out for (i, y_hat_), y_true_ in zip(enumerate(y_hat.data), y_true.data): if i == MAX_DECODER_OUTPUT: break idx_hat = self.xp.argmax(y_hat_[y_true_ != -1], axis=1) idx_true = y_true_[y_true_ != -1] seq_hat = [self.char_list[int(idx)] for idx in idx_hat] seq_true = [self.char_list[int(idx)] for idx in idx_true] seq_hat = "".join(seq_hat).replace('<space>', ' ') seq_true = "".join(seq_true).replace('<space>', ' ') logging.info("groundtruth[%d]: " % i + seq_true) logging.info("prediction [%d]: " % i + seq_hat) if self.labeldist is not None: if self.vlabeldist is None: self.vlabeldist = chainer.Variable( self.xp.asarray(self.labeldist)) loss_reg = -F.sum( F.scale(F.log_softmax(y_all), self.vlabeldist, axis=1)) / len(ys_in) self.loss = ( 1. - self.lsm_weight) * self.loss + self.lsm_weight * loss_reg return self.loss, acc
def argmax_rnn(self, x): y = self.blstm.GetFeat([Variable(x)])[0] path = F.argmax(y, axis=1).data return cp.asnumpy(path).astype(np.int32)
def main(): parser = argparse.ArgumentParser(description='vgg16') parser.add_argument('--input', '-i', type=str, default='./images/cat.jpg', help='predict imagefile') parser.add_argument('--batchsize', '-b', type=int, default=100, help='Number of images in each mini-batch') parser.add_argument('--epoch', '-e', type=int, default=20, help='Number of sweeps over the dataset to train') parser.add_argument('--gpu', '-g', type=int, default=-1, help='GPU ID (negative value indicates CPU)') parser.add_argument('--out', '-o', default='result', help='Directory to output the result') parser.add_argument('--classes', '-c', type=int, default=5, help='Number of classes') args = parser.parse_args() print('# GPU: {}'.format(args.gpu)) print('# classes: {}'.format(args.classes)) print('# batch: {}'.format(args.batchsize)) print('# epoch: {}'.format(args.epoch)) print('') # prepare network model = L.Classifier(VGG16Model(out_size=args.classes)) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() optimizer = chainer.optimizers.Adam(alpha=0.0001) optimizer.setup(model) # training rate for func_name in model.predictor.base._children: for param in model.predictor.base[func_name].params(): param.update_rule.hyperparam.alpha *= 0.1 # Transfer learning img = Image.open(args.input) x = L.model.vision.vgg.prepare(img) x = x[np.newaxis] # batch size print('predict') starttime = time.time() result = model.predictor(x) print(result) predict = F.argmax(F.softmax(result, axis=1), axis=1) endtime = time.time() print(predict, (endtime - starttime), 'sec') # variable([0]) 45.696336031 sec exit() # Load the MNIST dataset train, test = chainer.datasets.get_mnist() train_iter = chainer.iterators.SerialIterator(train, args.batchsize) test_iter = chainer.iterators.SerialIterator(test, args.batchsize, repeat=False, shuffle=False) # Set up a trainer updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu) trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) # Evaluate the model with the test dataset for each epoch trainer.extend(extensions.Evaluator(test_iter, model, device=args.gpu)) # Dump a computational graph from 'loss' variable at the first iteration # The "main" refers to the target link of the "main" optimizer. trainer.extend(extensions.dump_graph('main/loss')) # Take a snapshot for each specified epoch frequency = args.epoch if args.frequency == -1 else max(1, args.frequency) # trainer.extend(extensions.snapshot(), trigger=(frequency, 'epoch')) # Write a log of evaluation statistics for each epoch trainer.extend(extensions.LogReport()) # Save two plot images to the result dir if extensions.PlotReport.available(): trainer.extend( extensions.PlotReport(['main/loss', 'validation/main/loss'], 'epoch', file_name='loss.png')) trainer.extend( extensions.PlotReport( ['main/accuracy', 'validation/main/accuracy'], 'epoch', file_name='accuracy.png')) # Print selected entries of the log to stdout # Here "main" refers to the target link of the "main" optimizer again, and # "validation" refers to the default name of the Evaluator extension. # Entries other than 'epoch' are reported by the Classifier link, called by # either the updater or the evaluator. trainer.extend( extensions.PrintReport([ 'epoch', 'main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy', 'elapsed_time' ])) # Print a progress bar to stdout trainer.extend(extensions.ProgressBar()) if args.resume: # Resume from a snapshot chainer.serializers.load_npz(args.resume, trainer) # Run the training trainer.run() # save model chainer.serializers.save_npz(args.out + '/pretrained_model', model_mlp)
def argmax_rnn(self, x_list): self.rnn.reset_state() y_list = self.rnn(x_list) path = [F.argmax(y).data for y in y_list] return np.array(path, dtype="int32").flatten()
def __call__(self, x, t=None, t_cls=None): n_batch = len(x) h = F.relu(self.bn1(self.conv1(x))) h = F.max_pooling_2d(h, 3, stride=2) h = F.relu(self.bn2(self.conv2(h))) h = F.max_pooling_2d(h, 3, stride=2) h = F.relu(self.conv3(h)) h = F.relu(self.conv4(h)) h = F.relu(self.bn5(self.conv5(h))) h = F.max_pooling_2d(h, 3, stride=3) conv4 = h if not self.train_conv: h.unchain_backward() # failure prediction h = F.dropout(F.relu(self.fc6_failure(conv4)), ratio=0.5) h = F.dropout(F.relu(self.fc7_failure(h)), ratio=0.5) h = self.fc8_failure(h) h = h.reshape((-1, 2, self.n_failure)) fc8_failure = h fail_prob = F.softmax(fc8_failure, axis=1)[:, 1, :] self.fail_prob = fail_prob # classification prediction h = F.dropout(F.relu(self.fc6_cls(conv4)), ratio=0.5) h = F.dropout(F.relu(self.fc7_cls(h)), ratio=0.5) h = self.fc8_cls(h) cls_score = h self.cls_score = cls_score if t is None: assert not chainer.config.train return # failure loss half_n = self.n_failure / 2 is_singlearm_mask = t[:, half_n] == -1 # loss for single arm h_single = fc8_failure[is_singlearm_mask][:, :, :half_n] t_single = t[is_singlearm_mask][:, :half_n] # Requires: https://github.com/chainer/chainer/pull/3310 if h_single.data.shape[0] > 0: loss_single = F.softmax_cross_entropy(h_single, t_single, normalize=False) else: loss_single = None # loss for dual arm h_dual = fc8_failure[~is_singlearm_mask][:, :, half_n:] t_dual = t[~is_singlearm_mask][:, half_n:] # Requires: https://github.com/chainer/chainer/pull/3310 if h_dual.data.shape[0] > 0: loss_dual = F.softmax_cross_entropy(h_dual, t_dual, normalize=False) else: loss_dual = None # classification loss cls_loss = F.softmax_cross_entropy(cls_score, t_cls) self.cls_loss = cls_loss if loss_single is None: self.fail_loss = loss_dual elif loss_dual is None: self.fail_loss = loss_single else: self.fail_loss = loss_single + loss_dual self.loss = self.fail_loss + self.cls_loss # calculate acc on CPU fail_prob_single = fail_prob[is_singlearm_mask][:, :half_n] fail_prob_single = chainer.cuda.to_cpu(fail_prob_single.data) t_single = chainer.cuda.to_cpu(t_single) fail_prob_dual = fail_prob[~is_singlearm_mask][:, half_n:] fail_prob_dual = chainer.cuda.to_cpu(fail_prob_dual.data) t_dual = chainer.cuda.to_cpu(t_dual) fail_label_single = fail_prob_single > self.threshold fail_label_single = fail_label_single.astype(self.xp.int32) fail_label_dual = fail_prob_dual > self.threshold fail_label_dual = fail_label_dual.astype(self.xp.int32) fail_acc_single = (t_single == fail_label_single).all(axis=1) fail_acc_single = fail_acc_single.astype(self.xp.int32).flatten() fail_acc_dual = (t_dual == fail_label_dual).all(axis=1) fail_acc_dual = fail_acc_dual.astype(self.xp.int32).flatten() self.fail_acc = self.xp.sum(fail_acc_single) self.fail_acc += self.xp.sum(fail_acc_dual) self.fail_acc /= float(len(fail_acc_single) + len(fail_acc_dual)) cls_pred = F.argmax(cls_score, axis=1) cls_pred = chainer.cuda.to_cpu(cls_pred.data) t_cls = chainer.cuda.to_cpu(t_cls) self.cls_acc = self.xp.sum(t_cls == cls_pred) self.cls_acc /= float(len(t_cls)) chainer.reporter.report( { 'loss': self.loss, 'cls/loss': self.cls_loss, 'cls/acc': self.cls_acc, 'fail/loss': self.fail_loss, 'fail/acc': self.fail_acc, }, self) if chainer.config.train: return self.loss
self).__init__(Wp_h=L.Linear(input_dim, n_units), Wa_h=L.Linear(n_units, n_units), W_v=L.Linear(n_units, 1), W_vQVQ=L.Linear(100, 37), W_f_gru=L.StatefulGRU(input_dim, n_units)) model_OL = OutputLayer(1, 200, u_qs[0].shape[1]) s_j = F.batch_matmul( F.tanh(WqUqj + F.stack([model_OL.W_vQVQ.W] * batch_size, axis=0)), F.broadcast_to(model_OL.W_v.W, [batch_size, model_OL.W_v.W.shape[1]]), ).reshape(batch_size, WqUqj.shape[1]) a_i = F.softmax(s_j) rQ = F.batch_matmul(F.stack(u_qs), a_i, transa=True).reshape(batch_size, u_qs[0].shape[1]) model_OL.W_f_gru.h = rQ WpHp = F.stack([model_OL.Wp_h(hp) for hp in hps]) hta_new_b_list = [] for index in range(2): s_tj = F.batch_matmul( F.tanh(WpHp + F.stack([model_OL.W_f_gru.h] * WpHp.shape[1], axis=1)), F.broadcast_to(model_OL.W_v.W, [batch_size, model_OL.W_v.W.shape[1]])).reshape( batch_size, WpHp.shape[1]) at = F.softmax(s_tj) pt = F.argmax(at, axis=1) ct = F.batch_matmul(hps, at).reshape(batch_size, hps.shape[1]) h_new = model_OL.W_f_gru(ct) hta_new_b_list.append(s_tj)
use_dropout = 0.25 label_size = 3 knowledge_size = 2 input_hidden = 3 kelic_hidden = 5 enrich_hidden = 5 mlp_hidden = 3 input_layers = 1 enrich_layer = 1 model = kim(word_embed, emb_dim, label_size, knowledge_size, input_hidden, kelic_hidden, enrich_hidden, mlp_hidden, input_layers, enrich_layer, use_dropout) optimizer = optimizers.AdaGrad() optimizer.use_cleargrads() optimizer.setup(model) optimizer.add_hook(WeightDecay(0.0001)) if args.gpu >= 0: model.to_gpu() word_embed.to_gpu() for i in range(1, args.epoch + 1): system_start, system_end = model(x_list, y_list, x_mask, y_mask, knowledge) loss = F.softmax_cross_entropy(system_start, gold) print(F.argmax(system_start, axis=1), "loss:", loss.data) model.cleargrads() loss.backward() optimizer.update()
def predict(self, X): y_raw = self.forward(X) y_pred = F.argmax(F.softmax(y_raw).data, axis=1) return y_pred.data