def __init__(self, n_states, n_actions, gamma=0.99, epsilon_start=0.9, epsilon_end=0.05, epsilon_decay=200, memory_capacity=10000, policy_lr=0.01, batch_size=128, device="cpu"): self.actions_count = 0 self.n_actions = n_actions # 总的动作个数 self.device = device # 设备,cpu或gpu等 self.gamma = gamma # e-greedy 策略相关参数 self.epsilon = 0 self.epsilon_start = epsilon_start self.epsilon_end = epsilon_end self.epsilon_decay = epsilon_decay self.batch_size = batch_size self.policy_net = FCN(n_states, n_actions).to(self.device) self.target_net = FCN(n_states, n_actions).to(self.device) # target_net的初始模型参数完全复制policy_net self.target_net.load_state_dict(self.policy_net.state_dict()) self.target_net.eval() # 不启用 BatchNormalization 和 Dropout # 可查parameters()与state_dict()的区别,前者require_grad=True self.optimizer = optim.Adam(self.policy_net.parameters(), lr=policy_lr) self.loss = 0 self.memory = ReplayBuffer(memory_capacity)
def __init__(self, n_states, n_actions, gamma=0.99, epsilon_start=0.9, epsilon_end=0.05, epsilon_decay=200, memory_capacity=10000, policy_lr=0.01, batch_size=128, device="cpu"): self.actions_count = 0 self.n_actions = n_actions self.device = device self.gamma = gamma self.epsilon = 0 self.epsilon_start = epsilon_start self.epsilon_end = epsilon_end self.epsilon_decay = epsilon_decay self.batch_size = batch_size self.policy_net = FCN(n_states, n_actions).to(self.device) self.target_net = FCN(n_states, n_actions).to(self.device) self.target_net.load_state_dict(self.policy_net.state_dict()) self.target_net.eval() # 不启用 BatchNormalization 和 Dropout self.optimizer = optim.Adam(self.policy_net.parameters(), lr=policy_lr) self.loss = 0 self.memory = ReplayBuffer(memory_capacity)
def __init__(self, seq_len=20, learning_rate=3e-4): device = torch.device( "cuda: 0" if torch.cuda.is_available() else "cpu") self.device = device self.seq_len = seq_len time_stamp = time.strftime("%m-%d-%Y_%H:%M:%S", time.localtime()) print("run on device", device, ",current time:", time_stamp) self.writer = SummaryWriter('runs/emb_graph' + time_stamp) # define layers self.categ_embedding = CategoricalEmbedding().to(device) self.r2s_embedding = Route2Stop(vertex_feature=105, edge_feature=112).to(device) self.encoder = Encoder(input_size=100, seq_len=seq_len).to(device) self.fcn = FCN(input_size=100).to(device) self.similarity = Similarity(input_size=30, device=device).to(device) # define training parameters self.criterion = nn.BCELoss() self.optimizer = optim.Adam( [{ 'params': self.categ_embedding.parameters() }, { 'params': self.r2s_embedding.parameters() }, { 'params': self.encoder.parameters() }, { 'params': self.fcn.parameters() }, { 'params': self.similarity.parameters() }], lr=learning_rate)
def __init__(self, state_dim, device='cpu', gamma=0.99, lr=0.01, batch_size=5): self.gamma = gamma self.policy_net = FCN(state_dim) self.optimizer = torch.optim.RMSprop(self.policy_net.parameters(), lr=lr) self.batch_size = batch_size
def train(): with tf.Session() as sess: # create and initialize all FCN variables fcn = FCN(n_classes) sess.run(tf.global_variables_initializer()) # replace VGG net variables with pretrained weights restorer = tf.train.Saver( slim.get_variables_to_restore(include=['vgg_16'])) restorer.restore(sess, vgg_model_path) # create optimizer only after weight restoration, # otherwise will try to restore optimizer variables for VGG optimizer = tf.train.AdamOptimizer(learning_rate) train_step = optimizer.minimize(fcn.loss) sess.run(tf.variables_initializer(optimizer.variables())) saver = tf.train.Saver() for i in trange(num_iterations): inputs, masks = dataset.next_batch(batch_size) # inputs: array of images, shape [batch, h, w, 3], values [0 - 255] # masks: array of masks, shape [batch, h, w], values [0 - n_classes) _, loss = sess.run([train_step, loss], feed_dict={ fcn.inputs: inputs, fcn.masks: masks, fcn.keep_prob: 0.5, }) tqdm.write(f'Loss: {loss}') saver.save(sess, fcn_model_path)
class Novelty: def __init__(self, state_dims, device): self.device = device self.model = FCN(state_dims, state_dims).to(device) self.model_optim = Adam(self.model.parameters(), lr=p.lr) def get_reward(self, state, next_state): with torch.no_grad(): pred_next_state = self.model(state) try: reward = ((pred_next_state - next_state)**2).mean(1) except IndexError: reward = ((pred_next_state - next_state)**2).mean() return reward def update(self, memory): state_batch, action_batch, reward_batch, next_state_batch, mask_batch = memory.sample( p.reward_model_batch_size) state_batch = torch.FloatTensor(state_batch).to(self.device) next_state_batch = torch.FloatTensor(next_state_batch).to(self.device) pred_next_states = self.model(state_batch) loss = F.mse_loss(next_state_batch, pred_next_states) self.model_optim.zero_grad() loss.backward() self.model_optim.step() return loss.item()
def test_three(): config = utils.load_config() dermis = inputs.SkinData(config['data_dir'], 'dermis') with tf.Graph().as_default(): global_step = tf.train.get_or_create_global_step() image_ph = tf.placeholder(dtype=tf.float32, shape=(None, None, 3)) images = tf.expand_dims(image_ph, axis=0) net = FCN(images, net_params=config['net_params']) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint(config['train_dir'])) logger.info('Model at step-%d restored successfully!' % sess.run(global_step)) utils.create_if_not_exists(config['save_path']) for i, (image, label) in enumerate(zip(dermis.images, dermis.labels)): if i % 5 == 0: logger.info('Processing image %d...' % i) prep_image = image_prep_for_test(image) pred = np.squeeze( sess.run(net.outputs, feed_dict={image_ph: prep_image})) path = os.path.join(config['save_path'], dermis.listing[i].split('/')[-1] + '.jpg') save_all(image, label, pred, path)
def main(): image_paths = glob.glob('images/*png') model_dir = 'Result' class_num = 40 size = (224, 224) gpu_id = 0 model_file = os.path.join(model_dir, 'model_100epoch') out_save_dir = os.path.join(model_dir, 'out') model = chainer.links.Classifier(FCN(class_num=class_num)) serializers.load_npz(model_file, model) if gpu_id >= 0: cuda.get_device_from_id(gpu_id).use() model.to_gpu() for i, f in enumerate(image_paths): # Predict Input Data x = data.read_image(f, size) x = data.image_norm(x) x = cuda.to_gpu(x[np.newaxis, :, :, :]) y = model.predictor(x).data.argmax(axis=1)[0] # Save Predict Image input_file_name = f.split('/')[-1] # get file name save_path = os.path.join(out_save_dir, 'pred{:>05}_'.format(i) + input_file_name) y = cuda.to_cpu(y.astype(np.uint8)) Image.fromarray(y).save(save_path) print(f, save_path)
def test_five(): config = utils.load_config() dermis = inputs.SkinData(config['data_dir'], 'dermis') with tf.Graph().as_default(): global_step = tf.train.get_or_create_global_step() image_ph = tf.placeholder(dtype=tf.float32, shape=(None, None, 3)) images = tf.expand_dims(image_ph, axis=0) net = FCN(images, net_params=config['net_params']) h, w = tf.shape(image_ph)[0], tf.shape(image_ph)[1] upscore = tf.image.resize_images(net.endpoints['conv4'], size=(h, w)) prob_one = tf.nn.sigmoid(upscore) prob_zero = 1 - prob_one probs = tf.concat([prob_zero, prob_one], axis=3) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint(config['train_dir'])) logger.info('Model at step-%d restored successfully!' % sess.run(global_step)) utils.create_if_not_exists(config['save_path']) result_before = {'TP': 0, 'TN': 0, 'FP': 0, 'FN': 0} result_after = result_before.copy() def update_dict(d, to_update): for key in to_update: d[key] += to_update[key] _, x_test, _, y_test = train_test_split( dermis.images, dermis.labels, random_state=config['split_seed']) for i, (image, label) in enumerate(zip(x_test, y_test)): prep_image = image_prep_for_test(image) probs_o = np.squeeze( sess.run(probs, feed_dict={image_ph: prep_image})) cnn_result = np.argmax(probs_o, axis=2) cnn_crf_result = crf_post_process(image, probs_o) result_bi = count_many(cnn_result, label) result_ai = count_many(cnn_crf_result, label) update_dict(result_before, result_bi) update_dict(result_after, result_ai) result_bi.update(metric_many_from_counter(result_bi)) result_ai.update(metric_many_from_counter(result_ai)) if i % 5 == 0: logger.info( 'Image-{}\nresult before\n{}\nresult after\n{}\n'. format(i, result_bi, result_ai)) result_before.update(metric_many_from_counter(result_before)) result_after.update(metric_many_from_counter(result_after)) logger.info('\nresult before\n{}\nresult after\n{}\n'.format( result_before, result_after))
def prediction(model_fp, input_fp, output_fp, limit): model = FCN() model.load_state_dict(tor.load(model_fp)) model.cuda() dir_size = len(os.listdir(input_fp)) limit = limit if limit else float("inf") for i in range(dir_size): if i < limit: file_name = os.path.join(input_fp, "{:0>4}_sat.jpg".format(i)) img = plt.imread(file_name) img = np.moveaxis(img, 2, 0) img = tor.FloatTensor(np.array([img])) img_var = Variable(img).type(tor.FloatTensor).cuda() pred_img = model(img_var) pred_img = tor.max(pred_img, 1)[1] pred_img = pred_img.cpu().data.numpy() pred_img = np.moveaxis(pred_img, 0, 2) output_img = img_recovery(pred_img) scipy.misc.imsave( os.path.join(output_fp, "{:0>4}_mask.png".format(i)), output_img) else: break
def test_four(): config = utils.load_config() dermis = inputs.SkinData(config['data_dir'], 'dermis') with tf.Graph().as_default(): global_step = tf.train.get_or_create_global_step() image_ph = tf.placeholder(dtype=tf.float32, shape=(None, None, 3)) images = tf.expand_dims(image_ph, axis=0) net = FCN(images, net_params=config['net_params']) h, w = tf.shape(image_ph)[0], tf.shape(image_ph)[1] upscore = tf.image.resize_images(net.endpoints['conv4'], size=(h, w)) prob_one = tf.nn.sigmoid(upscore) prob_zero = 1 - prob_one probs = tf.concat([prob_zero, prob_one], axis=3) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint(config['train_dir'])) logger.info('Model at step-%d restored successfully!' % sess.run(global_step)) utils.create_if_not_exists(config['save_path']) total_count = 0 true_count = 0 for i, (image, label) in enumerate(zip(dermis.images, dermis.labels)): prep_image = image_prep_for_test(image) probs_o = np.squeeze( sess.run(probs, feed_dict={image_ph: prep_image})) cnn_result = np.argmax(probs_o, axis=2) accuracy_before, _, _ = metric_accuracy(cnn_result, label) cnn_crf_result = crf_post_process(image, probs_o) accuracy_i, true_count_i, total_count_i = metric_accuracy( cnn_crf_result, label) true_count += true_count_i total_count += total_count_i ss = 'DOWN' if accuracy_before > accuracy_i else 'UP' path = os.path.join(config['save_path'], dermis.listing[i].split('/')[-1]) path = '{:<6} ({:.3f}) ({:.3f}) {}.jpg'.format( path, accuracy_before, accuracy_i, ss) save_all_two(image, label, cnn_result, cnn_crf_result, path) if i % 5 == 0: logger.info( 'Image-%d accuracy before(%.3f) after(%.3f) %s' % (i, accuracy_before, accuracy_i, ss)) accuracy = true_count * 1.0 / total_count logger.info('Accuracy after crf: %.3f' % accuracy)
class PolicyGradient: def __init__(self, n_states,device='cpu',gamma = 0.99,lr = 0.01,batch_size=5): self.gamma = gamma self.policy_net = FCN(n_states) self.optimizer = torch.optim.RMSprop(self.policy_net.parameters(), lr=lr) self.batch_size = batch_size def choose_action(self,state): state = torch.from_numpy(state).float() state = Variable(state) probs = self.policy_net(state) m = Bernoulli(probs) action = m.sample() action = action.data.numpy().astype(int)[0] # 转为标量 return action def update(self,reward_pool,state_pool,action_pool): # Discount reward running_add = 0 for i in reversed(range(len(reward_pool))): if reward_pool[i] == 0: running_add = 0 else: running_add = running_add * self.gamma + reward_pool[i] reward_pool[i] = running_add # Normalize reward reward_mean = np.mean(reward_pool) reward_std = np.std(reward_pool) for i in range(len(reward_pool)): reward_pool[i] = (reward_pool[i] - reward_mean) / reward_std # Gradient Desent self.optimizer.zero_grad() for i in range(len(reward_pool)): state = state_pool[i] action = Variable(torch.FloatTensor([action_pool[i]])) reward = reward_pool[i] state = Variable(torch.from_numpy(state).float()) probs = self.policy_net(state) m = Bernoulli(probs) loss = -m.log_prob(action) * reward # Negtive score function x reward # print(loss) loss.backward() self.optimizer.step()
def test(): config = utils.load_config() with tf.Graph().as_default(): with tf.device('/cpu'): dermis = inputs.SkinData(config['data_dir'], 'dermis') global_step = tf.train.get_or_create_global_step() image_ph = tf.placeholder(dtype=tf.float32, shape=(None, None, 3)) label_ph = tf.placeholder(dtype=tf.float32, shape=(None, None)) images = tf.expand_dims(image_ph, axis=0) labels = tf.expand_dims(label_ph, axis=0) labels = tf.expand_dims(labels, axis=-1) net = FCN(images, net_params=config['net_params']) h, w = tf.shape(label_ph)[0], tf.shape(label_ph)[1] output = tf.image.resize_nearest_neighbor(net.endpoints['out'], size=(h, w)) correct = tf.cast(tf.equal(labels, output), dtype=tf.float32) correct_count = tf.reduce_sum(correct) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint(config['train_dir'])) logger.info('Model at step-%d restored successfully!' % sess.run(global_step)) total_pixels = 0 correct_pixels = 0 for i, (image, label) in enumerate(zip(dermis.images, dermis.labels)): if i % 10 == 0: logger.info('Processing image %i...' % i) correct_pixels += sess.run(correct_count, feed_dict={ image_ph: image / 255.0, label_ph: label }) total_pixels += np.product(label.shape) accuracy = correct_pixels * 1.0 / total_pixels logger.info('Accuracy: %.3f' % accuracy)
def test_two(): config = utils.load_config() dermis = inputs.SkinData(config['data_dir'], 'dermis') tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default(): batch_images, batch_labels = dermis.test_batch( seed=config['split_seed']) net = FCN(batch_images, net_params=config['net_params']) name_to_values, name_to_updates = slim.metrics.aggregate_metric_map({ 'accuracy': slim.metrics.streaming_accuracy(predictions=net.outputs, labels=batch_labels) }) results = slim.evaluation.evaluate_once( '', tf.train.latest_checkpoint(config['train_dir']), '/tmp/model', num_evals=config['n_examples_for_test'], eval_op=list(name_to_updates.values()), final_op=list(name_to_values.values())) for key, value in zip(name_to_values.keys(), results): logger.info('{:<10} {}'.format(key, value))
logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) nplogger = NeptuneLogger(api_token=os.getenv('NEPTUNE_API_TOKEN'), project_name="vladimir.isakov/sandbox", experiment_name='pneumonia', upload_source_files='./train.py', params={ 'batch_size': batch_size, 'epochs': epochs, 'lr': lr, 'step_size': step_size, 'gamma': gamma, 'weight_decay': weight_decay }) net = FCN() net.cuda() data = ImageDataset('./train.zarr', transform=train_tr) val = ImageDataset('./test.zarr', transform=val_tr) loader = DataLoader(dataset=data, batch_size=batch_size, shuffle=True, num_workers=6) val_loader = DataLoader(dataset=val, batch_size=batch_size, shuffle=False) loss_fn = nn.CrossEntropyLoss() optimizer = Adam(params=net.parameters(), lr=lr, weight_decay=weight_decay) scaler = GradScaler() scheduler = StepLR(optimizer=optimizer, step_size=step_size, gamma=gamma)
import cv2 import torch import numpy as np import torch.nn as nn import torch.nn.functional as F from tensorboard import TensorBoard from model import FCN from bezier import * writer = TensorBoard('log/') import torch.optim as optim criterion = nn.MSELoss() Decoder = FCN(64) optimizer = optim.Adam(Decoder.parameters(), lr=3e-4) batch_size = 64 use_cuda = True step = 0 def save_model(): if use_cuda: Decoder.cpu() torch.save(Decoder.state_dict(), './Decoder.pkl') if use_cuda: Decoder.cuda() def load_weights(): Decoder.load_state_dict(torch.load('./Decoder.pkl'))
valloader = data.DataLoader( SSDataSet(root, split="val", img_transform=input_transform, label_transform=target_transform), #sampler=sampler, batch_size=1, shuffle=False, num_workers=2) numClass = 8 numPlanes = 32 levels = 5 levelDepth = 2 kernelSize = 3 model = FCN(numPlanes, levels, levelDepth, numClass, kernelSize, 0.1) indices = [] mapLoc = None if haveCuda else {'cuda:0': 'cpu'} if haveCuda: model = model.cuda() criterion = CrossEntropyLoss2d() epochs = 200 lr = 1e-1 weight_decay = 1e-3 momentum = 0.5 patience = 20 optimizer = torch.optim.SGD([
class DQN: def __init__(self, n_states, n_actions, gamma=0.99, epsilon_start=0.9, epsilon_end=0.05, epsilon_decay=200, memory_capacity=10000, policy_lr=0.01, batch_size=128, device="cpu"): self.actions_count = 0 self.n_actions = n_actions # 总的动作个数 self.device = device # 设备,cpu或gpu等 self.gamma = gamma # e-greedy 策略相关参数 self.epsilon = 0 self.epsilon_start = epsilon_start self.epsilon_end = epsilon_end self.epsilon_decay = epsilon_decay self.batch_size = batch_size self.policy_net = FCN(n_states, n_actions).to(self.device) self.target_net = FCN(n_states, n_actions).to(self.device) # target_net的初始模型参数完全复制policy_net self.target_net.load_state_dict(self.policy_net.state_dict()) self.target_net.eval() # 不启用 BatchNormalization 和 Dropout # 可查parameters()与state_dict()的区别,前者require_grad=True self.optimizer = optim.Adam(self.policy_net.parameters(), lr=policy_lr) self.loss = 0 self.memory = ReplayBuffer(memory_capacity) def select_action(self, state): '''选择动作 Args: state [array]: [description] Returns: action [array]: [description] ''' self.epsilon = self.epsilon_end + (self.epsilon_start - self.epsilon_end) * \ math.exp(-1. * self.actions_count / self.epsilon_decay) self.actions_count += 1 if random.random() > self.epsilon: with torch.no_grad(): # 先转为张量便于丢给神经网络,state元素数据原本为float64 # 注意state=torch.tensor(state).unsqueeze(0)跟state=torch.tensor([state])等价 state = torch.tensor([state], device=self.device, dtype=torch.float32) # 如tensor([[-0.0798, -0.0079]], grad_fn=<AddmmBackward>) q_value = self.policy_net(state) # tensor.max(1)返回每行的最大值以及对应的下标, # 如torch.return_types.max(values=tensor([10.3587]),indices=tensor([0])) # 所以tensor.max(1)[1]返回最大值对应的下标,即action action = q_value.max(1)[1].item() else: action = random.randrange(self.n_actions) return action def update(self): if len(self.memory) < self.batch_size: return # 从memory中随机采样transition state_batch, action_batch, reward_batch, next_state_batch, done_batch = self.memory.sample( self.batch_size) # 转为张量 # 例如tensor([[-4.5543e-02, -2.3910e-01, 1.8344e-02, 2.3158e-01],...,[-1.8615e-02, -2.3921e-01, -1.1791e-02, 2.3400e-01]]) state_batch = torch.tensor(state_batch, device=self.device, dtype=torch.float) action_batch = torch.tensor(action_batch, device=self.device).unsqueeze( 1) # 例如tensor([[1],...,[0]]) reward_batch = torch.tensor( reward_batch, device=self.device, dtype=torch.float) # tensor([1., 1.,...,1]) next_state_batch = torch.tensor(next_state_batch, device=self.device, dtype=torch.float) done_batch = torch.tensor(np.float32(done_batch), device=self.device).unsqueeze( 1) # 将bool转为float然后转为张量 # 计算当前(s_t,a)对应的Q(s_t, a) # 关于torch.gather,对于a=torch.Tensor([[1,2],[3,4]]) # 那么a.gather(1,torch.Tensor([[0],[1]]))=torch.Tensor([[1],[3]]) q_values = self.policy_net(state_batch).gather( dim=1, index=action_batch) # 等价于self.forward # 计算所有next states的V(s_{t+1}),即通过target_net中选取reward最大的对应states next_state_values = self.target_net(next_state_batch).max( 1)[0].detach() # 比如tensor([ 0.0060, -0.0171,...,]) # 计算 expected_q_value # 对于终止状态,此时done_batch[0]=1, 对应的expected_q_value等于reward expected_q_values = reward_batch + self.gamma * \ next_state_values * (1-done_batch[0]) # self.loss = F.smooth_l1_loss(q_values,expected_q_values.unsqueeze(1)) # 计算 Huber loss self.loss = nn.MSELoss()(q_values, expected_q_values.unsqueeze(1)) # 计算 均方误差loss # 优化模型 self.optimizer.zero_grad( ) # zero_grad清除上一步所有旧的gradients from the last step # loss.backward()使用backpropagation计算loss相对于所有parameters(需要gradients)的微分 self.loss.backward() for param in self.policy_net.parameters(): # clip防止梯度爆炸 param.grad.data.clamp_(-1, 1) self.optimizer.step() # 更新模型 def save_model(): pass def load_model(): pass
type=int, help='GPU ID (negative value indicates CPU)') parser.add_argument('--image_path', '-i', default=None, type=str) parser.add_argument('--weight', '-w', default="weight/chainer_fcn.weight", type=str) parser.add_argument('--classes', default=21, type=int) parser.add_argument('--clop', "-c", default=True, type=bool) parser.add_argument('--clopsize', "-s", default=256, type=int) args = parser.parse_args() img_name = args.image_path.split("/")[-1].split(".")[0] color_map = make_color_map() model = FCN(n_class=args.classes) serializers.load_npz('weight/chainer_fcn.weight', model) o = load_data(args.image_path, crop=args.clop, size=args.clopsize, mode="predict") x = load_data(args.image_path, crop=args.clop, size=args.clopsize, mode="data") x = np.expand_dims(x, axis=0) pred = model(x).data pred = pred[0].argmax(axis=0) row, col = pred.shape dst = np.ones((row, col, 3)) for i in range(21): dst[pred == i] = color_map[i]
]) target_transform = Compose([ Scale((512, 256), Image.NEAREST), ToLabel(), ReLabel(), ]) batch_size = 1 dst = CSTestSet("/root/group-incubation-bj", img_transform=image_transform, label_transform=target_transform) testloader = data.DataLoader(dst, batch_size=batch_size, num_workers=8) model = torch.nn.DataParallel(FCN(config.NUM_CLASSES)) model.cuda() model.load_state_dict(torch.load(config.TORCH_MODEL_PATH)) model.eval() valid_image_count = list() iou_score = list() line_count = 0 for i in range(config.NUM_CLASSES): valid_image_count.append(0) iou_score.append(0.0) for j, data in enumerate(testloader): imgs, labels, names, original_size = data width = list(original_size)[0][0] height = list(original_size)[1][0]
class Model: def __init__(self, seq_len=20, learning_rate=3e-4): device = torch.device( "cuda: 0" if torch.cuda.is_available() else "cpu") self.device = device self.seq_len = seq_len time_stamp = time.strftime("%m-%d-%Y_%H:%M:%S", time.localtime()) print("run on device", device, ",current time:", time_stamp) self.writer = SummaryWriter('runs/emb_graph' + time_stamp) # define layers self.categ_embedding = CategoricalEmbedding().to(device) self.r2s_embedding = Route2Stop(vertex_feature=105, edge_feature=112).to(device) self.encoder = Encoder(input_size=100, seq_len=seq_len).to(device) self.fcn = FCN(input_size=100).to(device) self.similarity = Similarity(input_size=30, device=device).to(device) # define training parameters self.criterion = nn.BCELoss() self.optimizer = optim.Adam( [{ 'params': self.categ_embedding.parameters() }, { 'params': self.r2s_embedding.parameters() }, { 'params': self.encoder.parameters() }, { 'params': self.fcn.parameters() }, { 'params': self.similarity.parameters() }], lr=learning_rate) def forward(self, old, real, fake, numer_list, categ_list): old = self.categ_embedding(old, numer_list, categ_list, self.device) real = self.categ_embedding(real, numer_list, categ_list, self.device) fake = self.categ_embedding(fake, numer_list, categ_list, self.device) old = self.r2s_embedding(old) real = self.r2s_embedding(real) fake = self.r2s_embedding(fake) old = self.encoder(old) real = self.fcn(real) fake = self.fcn(fake) score_real = self.similarity(old, real) score_fake = self.similarity(old, fake) return score_real, score_fake def metrics(self, score_real, score_fake, label_real_test, label_fake_test): y_true = np.concatenate( [label_real_test.cpu().numpy(), label_fake_test.cpu().numpy()], axis=0) y_pred = torch.cat([ torch.argmax(score_real, dim=1, keepdim=True), torch.argmax(score_fake, dim=1, keepdim=True) ], dim=0).cpu().numpy() acc = accuracy_score(y_true, y_pred) precision = precision_score(y_true, y_pred) recall = recall_score(y_true, y_pred) f1 = f1_score(y_true, y_pred) return acc, precision, recall, f1 def train_and_test(self, data, batch_size=64, num_epoch=50): #initialize labels before training label_real = torch.cat( [torch.zeros([batch_size, 1]), torch.ones([batch_size, 1])], dim=1).to(self.device) label_fake = torch.cat( [torch.ones([batch_size, 1]), torch.zeros([batch_size, 1])], dim=1).to(self.device) old_test, real_test, fake_test = data.test test_size = real_test.shape[0] label_real_test = torch.ones([test_size, 1]).type(torch.long).to(self.device) label_fake_test = torch.zeros([test_size, 1]).type(torch.long).to(self.device) for epoch in range(num_epoch): total_loss = [0] * len(data) total_loss_real = [0] * len(data) # training first for i, chunk in enumerate(data.train): old_chunk, real_chunk, fake_chunk = chunk num_batch = real_chunk.shape[0] // batch_size for batch in range(num_batch): # get a batch of data pair: (old, real, fake) old_batch = old_chunk.iloc[batch * self.seq_len * batch_size:(batch + 1) * self.seq_len * batch_size, :] real_batch = real_chunk.iloc[batch * batch_size:(batch + 1) * batch_size, :] fake_batch = fake_chunk.iloc[batch * batch_size:(batch + 1) * batch_size, :] score_real, score_fake = self.forward( old_batch, real_batch, fake_batch, data.numer_list, data.categ_list) loss_real = self.criterion(score_real, label_real) loss_fake = self.criterion(score_fake, label_fake) loss = loss_real + loss_fake total_loss[i] += loss.data total_loss_real[i] += loss_real.data self.optimizer.zero_grad() loss.backward() self.optimizer.step() if (batch + 1) % 100 == 0: print( "epoch: %d, chunk: %d, batch: %d, loss: %.3f, real: %.3f, fake: %.3f" % (epoch, i, batch + 1, loss.data, loss_real.data, loss_fake.data)) total_loss[i] = (total_loss[i] / batch).cpu().numpy() total_loss_real[i] = (total_loss_real[i] / batch).cpu().numpy() # testing score_real, score_fake = self.forward(old_test, real_test, fake_test, data.numer_list, data.categ_list) acc, precision, recall, f1 = self.metrics(score_real, score_fake, label_real_test, label_fake_test) print("test acc: %.4f" % acc) self.writer.add_scalar('testing accuracy', acc, epoch) self.writer.close() # print result and save loss in tensorboard print("epoch: %d, average loss: %.4f" % (epoch, np.mean(total_loss))) self.writer.add_scalars('training loss', { 'overall': np.mean(total_loss), 'good': np.mean(total_loss_real) }, epoch) self.writer.close() return acc, precision, recall, f1
class DQN: def __init__(self, n_states, n_actions, gamma=0.99, epsilon_start=0.9, epsilon_end=0.05, epsilon_decay=200, memory_capacity=10000, policy_lr=0.01, batch_size=128, device="cpu"): self.actions_count = 0 self.n_actions = n_actions self.device = device self.gamma = gamma self.epsilon = 0 self.epsilon_start = epsilon_start self.epsilon_end = epsilon_end self.epsilon_decay = epsilon_decay self.batch_size = batch_size self.policy_net = FCN(n_states, n_actions).to(self.device) self.target_net = FCN(n_states, n_actions).to(self.device) self.target_net.load_state_dict(self.policy_net.state_dict()) self.target_net.eval() # 不启用 BatchNormalization 和 Dropout self.optimizer = optim.Adam(self.policy_net.parameters(), lr=policy_lr) self.loss = 0 self.memory = ReplayBuffer(memory_capacity) def select_action(self, state): '''选择工作 Args: state [array]: 状态 Returns: [array]: 动作 ''' self.epsilon = self.epsilon_end + (self.epsilon_start - self.epsilon_end) * \ math.exp(-1. * self.actions_count / self.epsilon_decay) self.actions_count += 1 if random.random() > self.epsilon: with torch.no_grad(): state = torch.tensor( [state], device=self.device, dtype=torch.float32 ) # 先转为张量便于丢给神经网络,state元素数据原本为float64;注意state=torch.tensor(state).unsqueeze(0)跟state=torch.tensor([state])等价 q_value = self.policy_net( state ) # tensor([[-0.0798, -0.0079]], grad_fn=<AddmmBackward>) action = q_value.max(1)[1].item() else: action = random.randrange(self.n_actions) return action def update(self): if len(self.memory) < self.batch_size: return state_batch, action_batch, reward_batch, next_state_batch, done_batch = self.memory.sample( self.batch_size) state_batch = torch.tensor( state_batch, device=self.device, dtype=torch.float ) # 例如tensor([[-4.5543e-02, -2.3910e-01, 1.8344e-02, 2.3158e-01],...,[-1.8615e-02, -2.3921e-01, -1.1791e-02, 2.3400e-01]]) action_batch = torch.tensor(action_batch, device=self.device).unsqueeze( 1) # 例如tensor([[1],...,[0]]) reward_batch = torch.tensor( reward_batch, device=self.device, dtype=torch.float) # tensor([1., 1.,...,1]) next_state_batch = torch.tensor(next_state_batch, device=self.device, dtype=torch.float) done_batch = torch.tensor(np.float32(done_batch), device=self.device).unsqueeze( 1) # 将bool转为float然后转为张量 # Compute Q(s_t, a) - the model computes Q(s_t), then we select the # columns of actions taken. These are the actions which would've been taken # for each batch state according to policy_net q_values = self.policy_net(state_batch).gather( 1, action_batch) # 等价于self.forward # Compute V(s_{t+1}) for all next states. # Expected values of actions for non_final_next_states are computed based # on the "older" target_net; selecting their best reward with max(1)[0]. # This is merged based on the mask, such that we'll have either the expected # state value or 0 in case the state was final. next_state_values = self.target_net(next_state_batch).max( 1)[0].detach() # tensor([ 0.0060, -0.0171,...,]) # Compute the expected Q values expected_q_values = reward_batch + self.gamma * next_state_values * ( 1 - done_batch[0]) # Compute Huber loss # self.loss = nn.MSELoss(q_values, expected_q_values.unsqueeze(1)) self.loss = nn.MSELoss()(q_values, expected_q_values.unsqueeze(1)) # Optimize the model self.optimizer.zero_grad( ) # zero_grad clears old gradients from the last step (otherwise you’d just accumulate the gradients from all loss.backward() calls). self.loss.backward( ) # loss.backward() computes the derivative of the loss w.r.t. the parameters (or anything requiring gradients) using backpropagation. for param in self.policy_net.parameters(): # clip防止梯度爆炸 param.grad.data.clamp_(-1, 1) self.optimizer.step( ) # causes the optimizer to take a step based on the gradients of the parameters.
from tensorboard import TensorBoard from model import FCN from synth import Generator from bezier import * from vggnet import * Encoder = VGG(16, 36) writer = TensorBoard('log/') import torch.optim as optim criterion = nn.MSELoss() criterion2 = nn.CrossEntropyLoss() Decoder = FCN(64) optimizerE = optim.Adam(Encoder.parameters(), lr=3e-4) optimizerD = optim.Adam(Decoder.parameters(), lr=3e-4) batch_size = 64 data_size = 100000 generated_size = 0 val_data_size = 512 first_generate = True use_cuda = True step = 0 Train_batch = [None] * data_size Ground_truth = [None] * data_size Label_batch = [None] * data_size Val_train_batch = [None] * val_data_size Val_ground_truth = [None] * val_data_size
def main(config): ## ================ Load data ============================================================= dataloader_train, dataloader_test = get_dataloader(config, config.tforms) data, labels, fname, raw_audio = next(iter(dataloader_train)) tn = tforms_mine.ApplyReverb(config.resampling_rate) tmp = tn(data[0]) print("Data shape pre module = %s" % str(data.shape)) transformer = get_time_frequency_transform(config) out = do_time_frequency_transform(data, transformer, config) print("Data shape after transformer = %s" % str(out.shape)) # Visualize some of the data tmp = out tmp2 = {'spectrogram': tmp, 'labels': labels, 'fname': fname} AudioTaggingPreProcessing.show_spectrogram_batch( tmp2, config, filepath=config.result_path) tmp3 = {'audio': raw_audio, 'labels': labels, 'fname': fname} AudioTaggingPreProcessing.show_waveforms_from_audio_batch( tmp3, config, filepath=config.result_path, saveWavs=False) ## ================ Model ================================================================= device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print(device) # input shape (batch, 1, 128, 1366) ss model = FCN(ConvBlock, output_shape=dataloader_train.dataset.num_classes, max_pool=[(2, 4), (2, 4), (2, 4), (3, 5), (4, 4)]).to(device) summary(model, input_size=(1, 128, 1366)) # input shape (batch, 1, 128, 256) # model = FCN(ConvBlock, output_shape=dataloader_train.dataset.num_classes, # max_pool=[(2, 2), (2, 2), (2, 4), (3, 3), (4, 4)], # filters_num=[64, 128, 256, 512, 1024]).to(device) # # summary(model, input_size=(1, 128, 256)) # Loss and optimizer learning_rate = 0.001 criterion = model.loss() optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) # Another hacky way to get the iterator for the validation set def loopy(dl): while True: for x in iter(dl): yield x ## ================ Training loop ========================================================= t_sum = 0 t_epoch = 0 last_time = time.time() last_epoch = time.time() all_time = [] all_time_epoch = [] total_step = len(dataloader_train) loss_history_train = [] train_loss_step = 0.0 valid_loss_step = 0.0 train_loss_epoch = 0.0 valid_loss_epoch = 0.0 train_loss_history = [] valid_loss_history = [] train_steps = 0 valid_steps = 0 myIter = loopy(dataloader_test) for epoch in range(config.num_epochs): train_loss_epoch = 0.0 valid_loss_epoch = 0.0 train_loss_step = 0.0 valid_loss_step = 0.0 # Training loop pbar = tqdm(enumerate(dataloader_train)) for i, sample in pbar: t = time.time() t_diff = t - last_time t_sum += t_diff last_time = t all_time.append(t_diff) audio = sample[ 0] # Sample is (transformed_audio, labels, fname, raw_audio) labels = sample[1].to(device) # Forward pass if config.tforms == TformsSet.TorchAudio: # Torchaudio supports full GPU audio = audio.to(device) specs = do_time_frequency_transform( audio, transformer.to(device), config) # time-freq transform outputs = model(specs) else: # Audtorch uses numpy, so no support for GPU specs = do_time_frequency_transform( audio, transformer, config) # time-freq transform outputs = model(specs.to(device)) loss = criterion(outputs, labels) train_loss_epoch += loss.item() train_loss_step = loss.item() train_steps += 1 train_loss_history.append( train_loss_step) # Append losses for plotting # Backward and optimize optimizer.zero_grad() loss.backward() optimizer.step() if train_steps % config.print_every == 0: model.eval() with torch.no_grad(): # Hacky way to get a single validation batch, see above for comments. try: sample = next(myIter) except StopIteration: myIter = loopy(dataloader_test) sample = next(myIter) audio = sample[0] labels = sample[1].to(device) # Forward pass | time-freq transform if config.tforms == TformsSet.TorchAudio: # Torchaudio supports full GPU specs = do_time_frequency_transform( audio.to(device), transformer.to(device), config) else: # Audtorch uses numpy, so no support for GPU specs = do_time_frequency_transform( audio, transformer, config).to(device) outputs = model(specs) loss = criterion(outputs, labels) valid_loss_step = loss.item() valid_loss_epoch += valid_loss_step valid_steps += 1 valid_loss_history.append( valid_loss_step) # Append losses for plotting pbar.set_description( "Epoch [{}/{}], Step [{}/{}], T_diff {} , T_total {} , train_Loss: {:.4f} , valid_Loss {:.4f}" .format(epoch + 1, config.num_epochs, i + 1, total_step, t_diff, t_sum, train_loss_step, valid_loss_step)) t = time.time() t_epoch = t - last_epoch last_epoch = t all_time_epoch.append(t_epoch) print( "--------- Epoch [{}/{}] Summary , time per epoch {} , train_loss {} , valid_loss {}" .format(epoch + 1, config.num_epochs, t_epoch, train_loss_epoch / len(dataloader_train), valid_loss_epoch / max(valid_steps, 1))) ## ================ Plot training ========================================================= ## Plot time for each step t = np.linspace(0, config.num_epochs, num=len(all_time), endpoint=False) plt.figure(figsize=(16, 8)) plt.plot(t, all_time, 'r-+', label='Time per step') plt.legend() plt.savefig(os.path.join(config.result_path, 'Output_time_per_step.png')) plt.xlabel('Epochs') plt.ylabel('Time (s)') plt.title("Train data = %d" % (len(dataloader_train))) plt.show() ## Plot time per epoch t = np.linspace(0, config.num_epochs, num=len(all_time_epoch), endpoint=False) plt.figure(figsize=(16, 8)) plt.plot(t, all_time_epoch, 'r-+', label='Time per step') plt.legend() plt.savefig(os.path.join(config.result_path, 'Output_time_per_epoch.png')) plt.xlabel('Epochs') plt.ylabel('Time (s)') plt.title("Train data = %d" % (len(dataloader_train))) plt.show() ## Plot loss t = np.linspace(0, config.num_epochs, num=train_steps, endpoint=False) t_valid = np.linspace(0, config.num_epochs, num=valid_steps, endpoint=False) plt.figure(figsize=(20, 8)) plt.plot(t, train_loss_history, 'r-+', label='Train') plt.plot(t_valid, valid_loss_history, 'b--o', label='Valid') plt.legend() plt.yscale('log') plt.ylabel('Loss') plt.title("Train data = %d" % (len(dataloader_train))) plt.savefig(os.path.join(config.result_path, 'Output_loss.png')) plt.show() ## ================ Evaluation ============================================================ def get_auc(y_true, y_preds, labels_list): from sklearn import metrics score_accuracy = 0 score_lwlrap = 0 score_roc_auc_macro = 0 score_pr_auc_macro = 0 score_roc_auc_micro = 0 score_pr_auc_micro = 0 score_mse = 0 score_roc_auc_all = np.zeros((len(labels_list), 1)).squeeze() score_pr_auc_all = np.zeros((len(labels_list), 1)).squeeze() try: # for accuracy, lets pretend this is a single label problem, so assign only one label to every prediction score_accuracy = metrics.accuracy_score( y_true, indices_to_one_hot(y_preds.argmax(axis=1), dataloader_train.dataset.num_classes)) score_lwlrap = metrics.label_ranking_average_precision_score( y_true, y_preds) score_mse = math.sqrt(metrics.mean_squared_error(y_true, y_preds)) # Average precision is a single number used to approximate the integral of the PR curve # Macro is average over all clases, without considering class imbalances score_pr_auc_macro = metrics.average_precision_score( y_true, y_preds, average="macro") score_roc_auc_macro = metrics.roc_auc_score(y_true, y_preds, average="macro") # Micro, considers class imbalances score_pr_auc_micro = metrics.average_precision_score( y_true, y_preds, average="micro") score_roc_auc_micro = metrics.roc_auc_score(y_true, y_preds, average="micro") except ValueError as e: print("Soemthing wrong with evaluation") print(e) print("Accuracy = %f" % (score_accuracy)) print("Label ranking average precision = %f" % (score_lwlrap)) print("ROC_AUC macro score = %f" % (score_roc_auc_macro)) print("PR_AUC macro score = %f" % (score_pr_auc_macro)) print("ROC_AUC_micro score = %f" % (score_roc_auc_micro)) print("PR_AUC_micro score = %f" % (score_pr_auc_micro)) print("MSE score for train = %f" % (score_mse)) # These are per tag try: score_roc_auc_all = metrics.roc_auc_score(y_true, y_preds, average=None) score_pr_auc_all = metrics.average_precision_score(y_true, y_preds, average=None) except ValueError as e: print("Something wrong with evaluation") print(e) print("") print("Per tag, roc_auc, pr_auc") for i in range(len(labels_list)): print('%s \t\t\t\t\t\t %.4f \t%.4f' % (labels_list[i], score_roc_auc_all[i], score_pr_auc_all[i])) tmp = { 'accuracy': score_accuracy, 'lwlrap': score_lwlrap, 'mse': score_mse, 'roc_auc_macro': score_roc_auc_macro, 'pr_auc_macro': score_pr_auc_macro, 'roc_auc_micro': score_roc_auc_micro, 'pr_auc_micro': score_pr_auc_micro, 'roc_auc_all': score_roc_auc_all, 'pr_auc_all': score_pr_auc_all } scores = edict(tmp) try: plt.figure(figsize=(20, 8)) plt.bar(np.arange(len(labels_list)), scores.roc_auc_all[:]) plt.ylabel('ROC_AUC') plt.title("Train data = %d" % (len(dataloader_train))) plt.savefig( os.path.join(config.result_path, 'Output_scores_per_label.png')) plt.show() # Plot only a few of the scores aa = { 'accuracy': scores.accuracy, 'lwlrap': scores.lwlrap, 'mse': scores.mse, 'roc_auc_macro': scores.roc_auc_macro, 'roc_auc_micro': scores.roc_auc_micro, 'pr_auc_macro': scores.pr_auc_macro, 'pr_auc_micro': scores.pr_auc_micro } plt.figure(figsize=(10, 6)) plt.bar(range(len(aa)), list(aa.values()), align='center') plt.xticks(range(len(aa)), list(aa.keys())) plt.ylabel('Scores') plt.savefig(os.path.join(config.result_path, 'Outputs_scores.png')) plt.show() except IndexError as e: print(e) return scores del myIter model.eval() with torch.no_grad(): total = 0 numBatches = 0 allPreds = np.empty((0, dataloader_test.dataset.num_classes), float) allLabels = np.empty((0, dataloader_test.dataset.num_classes), int) for batch in dataloader_test: audio = batch[0] labels = batch[1].cpu().numpy() # Forward pass | time-freq transform if config.tforms == TformsSet.TorchAudio: # Torchaudio supports full GPU specs = do_time_frequency_transform(audio.to(device), transformer.to(device), config) else: # Audtorch uses numpy, so no support for GPU specs = do_time_frequency_transform(audio, transformer, config).to(device) outputs = model.forward_with_evaluation( specs.to(device)).cpu().numpy() if not check_outputs(outputs, True): warnings.warn( "Warning, the ouputs appear to have wrong values!!!!!") allPreds = np.append(allPreds, outputs, axis=0) allLabels = np.append(allLabels, labels, axis=0) total += labels.shape[0] numBatches += 1 print("Evaluated on {} validation batches".format(numBatches)) scores = get_auc(allLabels, allPreds, dataloader_test.dataset.taglist) # Save scores to disk import pickle with open(os.path.join(config.result_path, 'Scores.pkl')) as f: pickle.dump(scores, f, pickle.HIGHEST_PROTOCOL) return # Load the results like this: with open('obj/' + name + '.pkl', 'rb') as f: return pickle.load(f)
import torchvision.transforms as transforms from tensorboard import TensorBoard from model import FCN from synth import Generator # Used to add noise to images G = Generator() from bezier import * from vggnet import * Encoder = VGG(16, 36) #Initializing a VGGnet architecture with 16 depth and 39 (9*4) as the num_outputs. #Now we have to pass in the data writer = TensorBoard('log/') import torch.optim as optim criterion = nn.MSELoss() criterion2 = nn.CrossEntropyLoss() Decoder = FCN(64) #Initializing the FCN network with width 64 (which is useless as the entire thing is hardcoded) optimizerE = optim.Adam(Encoder.parameters(), lr=3e-4) optimizerD = optim.Adam(Decoder.parameters(), lr=3e-4) batch_size = 64 data_size = 100000 generated_size = 0 val_data_size = 512 first_generate = True use_cuda = True step = 0 Train_batch = [None] * data_size Ground_truth = [None] * data_size Label_batch = [None] * data_size Val_train_batch = [None] * val_data_size Val_ground_truth = [None] * val_data_size
def train_deep(): import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from torch.utils.data import Dataset from model import FCN from torch.optim import lr_scheduler def train(model, device, train_loader, optimizer): model.train() train_loss = 0 for batch_idx, (data, target) in enumerate(train_loader): data, target = data.to(device), target.to(device) optimizer.zero_grad() output = model(data) loss = F.nll_loss(output, target) train_loss += loss.item() loss.backward() optimizer.step() return train_loss / len(train_loader.dataset) def test(model, device, test_loader): model.eval() test_loss = 0 correct = 0 with torch.no_grad(): for data, target in test_loader: data, target = data.to(device), target.to(device) output = model(data) test_loss += F.nll_loss( output, target, reduction="sum").item() # sum up batch loss pred = output.argmax( dim=1, keepdim=True) # get the index of the max log-probability correct += pred.eq(target.view_as(pred)).sum().item() test_loss /= len(test_loader.dataset) test_acc = 100.0 * correct / len(test_loader.dataset) return test_loss, test_acc # training settings batch_size = 32 test_batch_size = 1000 epochs = 500 patience = 30 # for early stopping use_cuda = torch.cuda.is_available() torch.manual_seed(9) device = torch.device("cuda" if use_cuda else "cpu") kwargs = {"num_workers": 1, "pin_memory": True} if use_cuda else {} train_loader = torch.utils.data.DataLoader( PoseDataset([root_dir / d for d in train_data_dirs]), batch_size=batch_size, shuffle=True, **kwargs, ) test_loader = torch.utils.data.DataLoader( PoseDataset([root_dir / d for d in test_data_dirs], mode="test"), batch_size=test_batch_size, shuffle=True, **kwargs, ) model = FCN().to(device) optimizer = optim.Adam(model.parameters(), lr=1e-3, amsgrad=True) early_stopping = utils.EarlyStopping(patience, Path("results")) for epoch in range(1, epochs + 1): train_loss = train(model, device, train_loader, optimizer) test_loss, test_acc = test(model, device, test_loader) print(f"epoch: {epoch:>3}, train_loss: {train_loss:.4f}, ", end="") print(f"test_loss: {test_loss:.4f}, test_acc: {test_acc:.3f}") early_stopping(test_loss, test_acc, model) if early_stopping.early_stop: print("Early stopping activated") break print(f"deep model acc: {early_stopping.best_acc}")
logger.addHandler(handler) logger.addHandler(console) def weights_init(m): if isinstance(m, nn.Conv2d): torch.nn.init.normal(m.weight.data,mean=0,std=1) torch.nn.init.normal(m.bias.data,mean=0,std=1) with open("/home/yuchen/Programs/cancer-prognosis/best.txt", 'r') as file: best = float(file.readline()) epoch0 = int(file.readline()) print("Last Train: accu %f , epoch0 %d" % (best, epoch0)) try: net = torch.load('/home/yuchen/Programs/cancer-prognosis/seg_model.pkl') except: net = FCN() net.apply(weights_init) net.cuda() optimizer = torch.optim.Adam(net.parameters(),lr=LR, weight_decay=0.5) scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.9) loss_func = nn.NLLLoss2d(weight=torch.FloatTensor([1,8000]).cuda()) data_loder = DataLoader() zeros = np.zeros((512,512)) for epoch in range(epoch0, epoch0+EPOCH): scheduler.step() train_step = 0 test_step = 0
# 初始化训练验证数据集 train_ds = CamvidDataset(type="train", crop_size=cfg.CROP_SIZE) val_ds = CamvidDataset(type="val", crop_size=cfg.CROP_SIZE) test_ds = CamvidDataset(type="test", crop_size=cfg.CROP_SIZE) # 加载训练验证数据集 train_dl = DataLoader(train_ds, batch_size=cfg.BATCH_SIZE, shuffle=True, num_workers=cfg.WORKERS_NUM) val_dl = DataLoader(val_ds, batch_size=cfg.BATCH_SIZE, shuffle=True, num_workers=cfg.WORKERS_NUM) test_dl = DataLoader(test_ds, batch_size=cfg.BATCH_SIZE, shuffle=False, num_workers=cfg.WORKERS_NUM) predict_dl = DataLoader(test_ds, batch_size=1, shuffle=False, num_workers=cfg.WORKERS_NUM) # 加载模型并部署到设备上 fcn = FCN.FCN(cfg.DATASET[1]) # torchsummary.summary(model, (cfg.CHANNEL_NUM,) + cfg.CROP_SIZE, device="cpu") fcn.to(device) # 损失函数 criterion = nn.NLLLoss().to(device) # 优化器 optimizer = optim.Adam(fcn.parameters(), lr=cfg.LEARNING_RATE) # 学习率调整 scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer=optimizer, mode='min', factor=0.5, patience=2, min_lr=0.00001, threshold=1) # 训练 train(fcn, train_dl, val_dl, criterion, optimizer, scheduler, device, cfg.MODEL_SAVE_PATH)
class DQN: def __init__(self, n_states, n_actions, gamma=0.99, epsilon_start=0.9, epsilon_end=0.05, epsilon_decay=200, memory_capacity=10000, policy_lr=0.01, batch_size=128, device="cpu"): self.actions_count = 0 self.n_actions = n_actions # 总的动作个数 self.device = device # 设备,cpu或gpu等 self.gamma = gamma # e-greedy策略相关参数 self.epsilon = 0 self.epsilon_start = epsilon_start self.epsilon_end = epsilon_end self.epsilon_decay = epsilon_decay self.batch_size = batch_size self.policy_net = FCN(n_states, n_actions).to(self.device) self.target_net = FCN(n_states, n_actions).to(self.device) # target_net的初始模型参数完全复制policy_net self.target_net.load_state_dict(self.policy_net.state_dict()) self.target_net.eval() # 不启用 BatchNormalization 和 Dropout # 可查parameters()与state_dict()的区别,前者require_grad=True self.optimizer = optim.Adam(self.policy_net.parameters(), lr=policy_lr) self.loss = 0 self.memory = ReplayBuffer(memory_capacity) def choose_action(self, state, train=True): '''选择动作 ''' if train: self.epsilon = self.epsilon_end + (self.epsilon_start - self.epsilon_end) * \ math.exp(-1. * self.actions_count / self.epsilon_decay) self.actions_count += 1 if random.random() > self.epsilon: with torch.no_grad(): # 先转为张量便于丢给神经网络,state元素数据原本为float64 # 注意state=torch.tensor(state).unsqueeze(0)跟state=torch.tensor([state])等价 state = torch.tensor([state], device=self.device, dtype=torch.float32) # 如tensor([[-0.0798, -0.0079]], grad_fn=<AddmmBackward>) q_value = self.policy_net(state) # tensor.max(1)返回每行的最大值以及对应的下标, # 如torch.return_types.max(values=tensor([10.3587]),indices=tensor([0])) # 所以tensor.max(1)[1]返回最大值对应的下标,即action action = q_value.max(1)[1].item() else: action = random.randrange(self.n_actions) return action else: with torch.no_grad(): # 先转为张量便于丢给神经网络,state元素数据原本为float64 # 注意state=torch.tensor(state).unsqueeze(0)跟state=torch.tensor([state])等价 state = torch.tensor([state], device='cpu', dtype=torch.float32) # 如tensor([[-0.0798, -0.0079]], grad_fn=<AddmmBackward>) q_value = self.target_net(state) # tensor.max(1)返回每行的最大值以及对应的下标, # 如torch.return_types.max(values=tensor([10.3587]),indices=tensor([0])) # 所以tensor.max(1)[1]返回最大值对应的下标,即action action = q_value.max(1)[1].item() return action def update(self): if len(self.memory) < self.batch_size: return # 从memory中随机采样transition state_batch, action_batch, reward_batch, next_state_batch, done_batch = self.memory.sample( self.batch_size) # 转为张量 # 例如tensor([[-4.5543e-02, -2.3910e-01, 1.8344e-02, 2.3158e-01],...,[-1.8615e-02, -2.3921e-01, -1.1791e-02, 2.3400e-01]]) state_batch = torch.tensor(state_batch, device=self.device, dtype=torch.float) action_batch = torch.tensor(action_batch, device=self.device).unsqueeze( 1) # 例如tensor([[1],...,[0]]) reward_batch = torch.tensor( reward_batch, device=self.device, dtype=torch.float) # tensor([1., 1.,...,1]) next_state_batch = torch.tensor(next_state_batch, device=self.device, dtype=torch.float) done_batch = torch.tensor(np.float32(done_batch), device=self.device).unsqueeze( 1) # 将bool转为float然后转为张量 # 计算当前(s_t,a)对应的Q(s_t, a) q_values = self.policy_net(state_batch) next_q_values = self.policy_net(next_state_batch) # 代入当前选择的action,得到Q(s_t|a=a_t) q_value = q_values.gather(dim=1, index=action_batch) '''以下是Nature DQN的q_target计算方式 # 计算所有next states的Q'(s_{t+1})的最大值,Q'为目标网络的q函数 next_q_state_value = self.target_net( next_state_batch).max(1)[0].detach() # 比如tensor([ 0.0060, -0.0171,...,]) # 计算 q_target # 对于终止状态,此时done_batch[0]=1, 对应的expected_q_value等于reward q_target = reward_batch + self.gamma * next_q_state_value * (1-done_batch[0]) ''' '''以下是Double DQNq_target计算方式,与NatureDQN稍有不同''' next_target_values = self.target_net(next_state_batch) # 选出Q(s_t‘, a)对应的action,代入到next_target_values获得target net对应的next_q_value,即Q’(s_t|a=argmax Q(s_t‘, a)) next_target_q_value = next_target_values.gather( 1, torch.max(next_q_values, 1)[1].unsqueeze(1)).squeeze(1) q_target = reward_batch + self.gamma * next_target_q_value * ( 1 - done_batch[0]) self.loss = nn.MSELoss()(q_value, q_target.unsqueeze(1)) # 计算 均方误差loss # 优化模型 self.optimizer.zero_grad( ) # zero_grad清除上一步所有旧的gradients from the last step # loss.backward()使用backpropagation计算loss相对于所有parameters(需要gradients)的微分 self.loss.backward() for param in self.policy_net.parameters(): # clip防止梯度爆炸 param.grad.data.clamp_(-1, 1) self.optimizer.step() # 更新模型 def save_model(self, path): torch.save(self.target_net.state_dict(), path) def load_model(self, path): self.target_net.load_state_dict(torch.load(path))
def train(data_loader, model_index, x_eval_train, y_eval_train): ### Model Initiation fcn = FCN() #print (fcn.b_1_conv_1[0].weight.data) d = tor.load("./models/vgg16_pretrained.pkl") fcn.vgg16_load(d) #d = tor.load("./models/fcn_model_1_1.pkl") #fcn.load_state_dict(d) fcn.cuda() #loss_func = tor.nn.CrossEntropyLoss(weight=w) loss_func = tor.nn.CrossEntropyLoss() #loss_func = tor.nn.MSELoss() #optim = tor.optim.SGD(fcn.parameters(), lr=LR, momentum=MOMENTUM) optim1 = tor.optim.Adam(fcn.b_6_conv_1.parameters(), lr=LR) optim2 = tor.optim.Adam(fcn.b_6_conv_2.parameters(), lr=LR) optim3 = tor.optim.Adam(fcn.b_6_conv_3.parameters(), lr=LR) optim4 = tor.optim.Adam(fcn.b_7_trans_1.parameters(), lr=LR) optim = tor.optim.Adam(fcn.parameters(), lr=LR) ### Training for epoch in range(EPOCH): print("|Epoch: {:>4} |".format(epoch + 1), end="") ### Training for step, (x_batch, y_batch) in enumerate(data_loader): x = Variable(x_batch).type(tor.FloatTensor).cuda() y = Variable(y_batch).type(tor.LongTensor).cuda() pred = fcn(x) optim1.zero_grad() optim2.zero_grad() optim3.zero_grad() optim4.zero_grad() optim.zero_grad() loss = loss_func(pred, y) loss.backward() optim1.step() optim2.step() optim3.step() optim4.step() print(pred[:2]) print(tor.max(pred[:5], 1)[1]) ### Evaluation loss = float(loss.data) acc = evaluate(fcn, x_eval_train, y_eval_train) print("|Loss: {:<8} |Acc: {:<8}".format(loss, acc)) ### Save model if epoch % RECORD_MODEL_PERIOD == 0: tor.save( fcn.state_dict(), os.path.join(MODEL_ROOT, "fcn_model_{}_{}.pkl".format(model_index, epoch))) ### Record record_data = dict() if epoch == 0: record_data["model_name"] = "fcn_model_{}.pkl".format(model_index) record_data["data_size"] = AVAILABLA_SIZE record_data["batch_size"] = BATCHSIZE record_data["decay"] = str((LR_STEPSIZE, LR_GAMMA)) record_data["lr_init"] = float(optim1.param_groups[0]["lr"]) record_data["lr"] = float(optim1.param_groups[0]["lr"]) record_data["record_epoch"] = RECORD_MODEL_PERIOD record_data["loss"] = loss record_data["acc"] = acc else: record_data["model_name"] = "fcn_model_{}.pkl".format(model_index) record_data["lr"] = float(optim1.param_groups[0]["lr"]) record_data["loss"] = loss record_data["acc"] = acc record(RECORD_FP, record_data)