Пример #1
0
 def __init__(self, net_path, board_size=15, n=5):
     # 游戏棋盘大小
     self.board_size = board_size
     # 连五子胜利
     self.n = n
     # 环境实例化
     self.env = Game(board_size, n)
     self.device = "cuda" if torch.cuda.is_available() else "cpu"
     self.number_playout = args.n_playout
     # 记忆库大小
     self.buffer_size = args.buffer_size
     self.buffer = deque(maxlen=self.buffer_size)
     self.batch_size = args.batch_size
     # 自我对局1次后进行训练
     self.n_games = args.n_games
     # 自我对局后进行5次训练
     self.epochs = args.epochs
     # 打印保存模型间隔
     self.check_freq = args.check_freq
     # 总共游戏次数
     self.game_num = args.game_num
     self.net_path = net_path
     self.net = MyNet().to(self.device)
     self.MSELoss = nn.MSELoss()
     self.optimizer = torch.optim.Adam(self.net.parameters(),
                                       weight_decay=1e-4)
     # 实例化蒙特卡洛玩家,参数:游戏策略,探索常数,模拟次数,是否自我对弈(测试时为False)
     self.mcts_player = Player(policy=self.policy,
                               number_playout=self.number_playout,
                               is_self_play=True)
     self.writer = SummaryWriter()
     if os.path.exists(net_path):
         self.net.load_state_dict(torch.load(net_path))
     else:
         self.net.apply(self.weight_init)
Пример #2
0
 def __init__(self, net_path, board_size, n):
     self.device = "cuda" if torch.cuda.is_available() else "cpu"
     self.net = MyNet().to(self.device)
     self.net.load_state_dict(torch.load(net_path))
     # self.board_size = args.board_size
     # self.n = args.number
     self.number_playout = args.n_playout
     self.env = GameState(board_size, n)
     self.net.eval()
     self.mcts_player = Player(policy=self.policy,
                               number_playout=1000,
                               is_self_play=False,
                               print_detail=True)
Пример #3
0
def start_test():
    '''
    测试
    '''
    # 初始化网络并加载预训练模型
    myNet = MyNet().to("cuda:0")
    myNet.eval()
    with torch.no_grad():
        input = input.to("cuda:0")

        # 若想推理加速,在精度接受范围内img\model手动half()为FP16,然后只能GPU推理
        # input=input.half()
        # myNet=myNet.half()
        feature = myNet(input)
Пример #4
0
def start_train():
    '''
    训练
    '''
    use_amp = True
    # 前向反传N次,再更新参数  目的:增大batch(理论batch= batch_size * N)
    iter_size = 8

    myNet = MyNet(use_amp).to("cuda:0")
    myNet = torch.nn.DataParallel(myNet, device_ids=[0, 1])  # 数据并行
    myNet.train()
    # 训练开始前初始化 梯度缩放器
    scaler = GradScaler() if use_amp else None

    # 加载预训练权重
    if resume_train:
        scaler.load_state_dict(checkpoint['scaler'])  # amp自动混合精度用到
        optimizer.load_state_dict(checkpoint['optimizer'])
        myNet.load_state_dict(checkpoint["model"])

    for epoch in range(1, 100):
        for batch_idx, (input, target) in enumerate(dataloader_train):

            # 数据 转到每个并行模型的主卡上
            input = input.to("cuda:0")
            target = target.to("cuda:0")

            # 自动混合精度训练
            if use_amp:
                # 自动广播 将支持半精度操作自动转为FP16
                with autocast():
                    # 提取特征
                    feature = myNet(input)
                    losses = loss_function(target, feature)
                    loss = losses / iter_size
                scaler.scale(loss).backward()
            else:
                feature = myNet(input, target)
                losses = loss_function(target, feature)
                loss = losses / iter_size
                loss.backward()

            # 梯度累积,再更新参数
            if (batch_idx + 1) % iter_size == 0:
                # 梯度更新
                if use_amp:
                    scaler.step(optimizer)
                    scaler.update()
                else:
                    optimizer.step()
                # 梯度清零
                optimizer.zero_grad()
        # scaler 具有状态。恢复训练时需要加载
        state = {
            'net': myNet.state_dict(),
            'optimizer': optimizer.state_dict(),
            'scaler': scaler.state_dict()
        }
        torch.save(state, "filename.pth")
Пример #5
0
def do_eval(data_path, model_name='mymodel', use_gpu=False):
    place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
    with fluid.dygraph.guard(place):
        model = MyNet()
        model_state_dict, _ = fluid.load_dygraph(
            os.path.join(MODEL_PATH, model_name))
        model.load_dict(model_state_dict)

        model.eval()
        eval_loader = load_data(data_path, mode='eval')

        avg_acc_set = []
        avg_loss_set = []
        for _, data in enumerate(eval_loader()):
            x_data, y_data = data
            img = fluid.dygraph.to_variable(x_data)
            label = fluid.dygraph.to_variable(y_data)
            predict, avg_acc = model(img, label)
            loss = fluid.layers.cross_entropy(input=predict, label=label)
            avg_loss = fluid.layers.mean(loss)
            avg_acc_set.append(float(avg_acc.numpy()))
            avg_loss_set.append(float(avg_loss.numpy()))

        #计算多个batch的平均损失和准确率
        avg_acc_val_mean = np.array(avg_acc_set).mean()
        avg_loss_val_mean = np.array(avg_loss_set).mean()

        print('loss={}, acc={}'.format(avg_loss_val_mean, avg_acc_val_mean))
Пример #6
0
class Detector:
    def __init__(self, net_path, board_size, n):
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
        self.net = MyNet().to(self.device)
        self.net.load_state_dict(torch.load(net_path))
        # self.board_size = args.board_size
        # self.n = args.number
        self.number_playout = args.n_playout
        self.env = GameState(board_size, n)
        self.net.eval()
        self.mcts_player = Player(policy=self.policy,
                                  number_playout=1000,
                                  is_self_play=False,
                                  print_detail=True)

    def policy(self, env):
        # 获取可用动作 15*15=225
        action_avail = env.action_avail
        # 获得当前状态
        state = torch.from_numpy(env.get_state).unsqueeze(0).to(self.device)

        # 放入神经网络得到预测的log动作概率以及当前状态的胜率
        log_action_probs, value = self.net(state)

        # 把 log 动作概率转换为动作概率并过滤不可用动作
        act_probs = torch.exp(
            log_action_probs).detach().cpu().numpy().flatten()
        act_probs = zip(action_avail, act_probs[action_avail])
        value = value.item()

        # 返回动作概率,当前局面价值
        return act_probs, value

    def detect(self):
        while True:
            action = None
            # 当玩家切换到人机以及游戏未结束时,人机使用MCTS算法得到最优动作
            if self.env.current_player == 1 and not self.env.pause:
                action = self.mcts_player.get_action(self.env.game)
            self.env.step(action)
Пример #7
0
def test_net():
    data_set = TestDataset()
    data_loader = DataLoader(data_set,
                             batch_size=1,
                             shuffle=True,
                             drop_last=False)

    classes = data_set.classes
    net = MyNet(classes)
    _, _, last_time_model = get_check_point()
    # assign directly
    # last_time_model='./weights/weights_21_110242'

    if os.path.exists(last_time_model):
        model = torch.load(last_time_model)
        if cfg.test_use_offline_feat:
            net.load_state_dict(model)
        else:
            net.load_state_dict(model)
        print("Using the model from the last check point:`%s`" %
              (last_time_model))
    else:
        raise ValueError("no model existed...")
    net.eval()
    is_cuda = cfg.use_cuda
    did = cfg.device_id
    # img_src=cv2.imread("/root/workspace/data/VOC2007_2012/VOCdevkit/VOC2007/JPEGImages/000012.jpg")
    # img_src=cv2.imread('./example.jpg')
    img_src = cv2.imread('./dog.jpg')  # BGR
    img = img_src[:, :, ::-1]  # RGB
    h, w, _ = img.shape
    img = img.transpose(2, 0, 1)  # [c,h,w]

    img = preprocess(img)
    img = img[None]
    img = torch.tensor(img)
    if is_cuda:
        net.cuda(did)
        img = img.cuda(did)
    boxes, labels, probs = net(img, torch.tensor([[w, h]]).type_as(img))[0]

    prob_mask = probs > cfg.out_thruth_thresh
    boxes = boxes[prob_mask]
    labels = labels[prob_mask].long()
    probs = probs[prob_mask]
    draw_box(img_src,
             boxes,
             color='pred',
             text_list=[
                 classes[_] + '[%.3f]' % (__) for _, __ in zip(labels, probs)
             ])
    show_img(img_src, -1)
Пример #8
0
    if args.subset_len:
        data_loader = get_subnet_dataloader(args.data_dir,
                                            batch_size,
                                            args.subset_len,
                                            num_workers=args.num_workers,
                                            shuffle=False,
                                            train=False,
                                            download=download)
    else:
        data_loader = get_dataloader(args.data_dir,
                                     args.batch_size,
                                     num_workers=args.num_workers,
                                     shuffle=False,
                                     train=False,
                                     download=download)

    model = MyNet()
    model = load_weights(model, args.pretrained)
    input_signature = torch.randn([1, 3, 32, 32], dtype=torch.float32)
    input_signature = input_signature.to(device)
    model = model.to(device)
    pruning_runner = get_pruning_runner(model, input_signature, 'one_step')

    pruning_runner.search(gpus=gpus,
                          calibration_fn=calibration_fn,
                          calib_args=(data_loader, ),
                          num_subnet=args.num_subnet,
                          removal_ratio=args.sparsity,
                          eval_fn=eval_fn,
                          eval_args=(data_loader, ))
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--batchsize',
                        '-b',
                        type=int,
                        default=32,
                        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')
    args = parser.parse_args()

    # Set up a neural network to train
    # Classifier reports softmax cross entropy loss and accuracy at every
    # iteration, which will be used by the PrintReport extension below.
    model = MyNet()
    model.to_gpu()  # Copy the model to the GPU

    # Setup an optimizer
    optimizer = chainer.optimizers.SGD(lr=0.0001)
    optimizer.setup(model)

    # 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)

    # Learning loop
    while train_iter.epoch < args.epoch:
        sum_loss = 0
        itr = 0

        train_batch = train_iter.next()
        x_data = []
        t_data = []
        for data in train_batch:
            x_data.append(data[0].reshape((1, 28, 28)))
            if data[1] % 2 == 0:
                t_data.append([1])
            else:
                t_data.append([0])

        x = Variable(cuda.to_gpu(np.array(x_data, dtype=np.float32)))
        t = Variable(cuda.to_gpu(np.array(t_data, dtype=np.int32)))

        y = model(x)

        model.cleargrads()
        loss = F.sigmoid_cross_entropy(y, t)
        loss.backward()
        optimizer.update()

        sum_loss += loss.data
        itr += 1

        if train_iter.is_new_epoch:
            print('epoch:{} train_loss:{} '.format(train_iter.epoch,
                                                   sum_loss / itr),
                  end='')

            sum_test_loss = 0
            sum_test_accuracy = 0
            test_itr = 0
            while True:
                test_batch = test_iter.next()
                x_test_data = []
                t_test_data = []
                for test_data in test_batch:
                    x_test_data.append(test_data[0].reshape((1, 28, 28)))
                    if test_data[1] % 2 == 0:
                        t_test_data.append([1])
                    else:
                        t_test_data.append([0])

                x_test = Variable(
                    cuda.to_gpu(np.array(x_test_data, dtype=np.float32)))
                t_test = Variable(
                    cuda.to_gpu(np.array(t_test_data, dtype=np.int32)))

                y_test = model(x_test)
                sum_test_loss += F.sigmoid_cross_entropy(y_test, t_test).data
                sum_test_accuracy += F.binary_accuracy(y_test, t_test).data
                test_itr += 1

                if test_iter.is_new_epoch:
                    test_iter.epoch = 0
                    test_iter.current_position = 0
                    test_iter.is_new_epoch = False
                    test_iter._pushed_position = None
                    break

            print('val_loss:{} val_accuracy:{}'.format(
                sum_test_loss / test_itr, sum_test_accuracy / test_itr))
Пример #10
0
parser.add_argument('--weight_decay',
                    type=float,
                    default=1e-4,
                    help='Weight decay')
parser.add_argument('--momentum', type=float, default=0.9, help='Momentum')

args, _ = parser.parse_known_args()

device = 'cuda'
gpus = get_gpus(args.gpus)

if __name__ == '__main__':
    torch.distributed.init_process_group(backend="nccl")
    torch.cuda.set_device(args.local_rank)
    batch_size = args.batch_size * len(gpus)
    model = MyNet()
    model.cuda()
    model = torch.nn.parallel.DistributedDataParallel(
        model,
        device_ids=[args.local_rank],
        output_device=args.local_rank,
        find_unused_parameters=True)

    if os.path.exists(args.data_dir):
        download = False
    else:
        download = True

    train_loader = get_dataloader_ddp(args.data_dir,
                                      batch_size,
                                      num_workers=args.num_workers,
Пример #11
0
    mask_inds = np.delete(mask_inds, np.argwhere(mask_inds == 255))
    inds_sim = torch.from_numpy(np.where(mask == 255)[0])
    inds_scr = torch.from_numpy(np.where(mask != 255)[0])
    target_scr = torch.from_numpy(mask.astype(np.int))

    if use_cuda:
        inds_sim = inds_sim.cuda()
        inds_scr = inds_scr.cuda()
        target_scr = target_scr.cuda()

    target_scr = Variable(target_scr)
    # set minLabels
    args.minLabels = len(mask_inds)

# train
model = MyNet(profile["count"], args.nChannel, args.nConv)

if use_cuda:

    model.cuda()

model.train()

# similarity loss definition
loss_fn = torch.nn.CrossEntropyLoss()

# scribble loss definition
loss_fn_scr = torch.nn.CrossEntropyLoss()

# continuity loss definition
loss_hpy = torch.nn.L1Loss(size_average=True)
Пример #12
0
class Trainer:
    def __init__(self, net_path, board_size=15, n=5):
        # 游戏棋盘大小
        self.board_size = board_size
        # 连五子胜利
        self.n = n
        # 环境实例化
        self.env = Game(board_size, n)
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
        self.number_playout = args.n_playout
        # 记忆库大小
        self.buffer_size = args.buffer_size
        self.buffer = deque(maxlen=self.buffer_size)
        self.batch_size = args.batch_size
        # 自我对局1次后进行训练
        self.n_games = args.n_games
        # 自我对局后进行5次训练
        self.epochs = args.epochs
        # 打印保存模型间隔
        self.check_freq = args.check_freq
        # 总共游戏次数
        self.game_num = args.game_num
        self.net_path = net_path
        self.net = MyNet().to(self.device)
        self.MSELoss = nn.MSELoss()
        self.optimizer = torch.optim.Adam(self.net.parameters(),
                                          weight_decay=1e-4)
        # 实例化蒙特卡洛玩家,参数:游戏策略,探索常数,模拟次数,是否自我对弈(测试时为False)
        self.mcts_player = Player(policy=self.policy,
                                  number_playout=self.number_playout,
                                  is_self_play=True)
        self.writer = SummaryWriter()
        if os.path.exists(net_path):
            self.net.load_state_dict(torch.load(net_path))
        else:
            self.net.apply(self.weight_init)

    def weight_init(self, net):
        if isinstance(net, nn.Linear) or isinstance(net, nn.Conv2d):
            nn.init.normal_(net.weight, mean=0., std=0.1)
            nn.init.constant_(net.bias, 0.)

    def train(self):
        for i in range(self.game_num):
            # 环境先自我对弈获得棋局状态,动作概率以及玩家可以赢的概率值
            for _ in range(self.n_games):
                winner, data = self.env.self_play(self.mcts_player, temp=1.0)
                # 打印每局对局信息
                print(self.env, "\n", "------------------xx--------")
                # 将获得的数据多样化存入样本池
                self.extend_sample(data)

            # 取样训练
            batch = random.sample(self.buffer,
                                  min(len(self.buffer), self.batch_size))
            # 解包
            state_batch, mcts_probs_batch, winner_value_batch = zip(*batch)
            loss = 0.
            for _ in range(self.epochs):
                # 数据处理
                state_batch = torch.tensor(state_batch).to(self.device)
                mcts_probs_batch = torch.tensor(mcts_probs_batch).to(
                    self.device)
                winner_value_batch = torch.tensor(winner_value_batch).to(
                    self.device)

                # 通过神经网络输出动作概率,价值用于训练
                log_act_probs, value = self.net(state_batch)

                # 计算损失
                # 价值损失:输出价值与该状态所在对局最终胜负的值(-1/0/1)(均方差)
                # 策略损失:蒙特卡洛树模拟的概率值与神经网络模拟的概率值的相似度 (-log(pi) * p)(交叉熵)
                value_loss = self.MSELoss(value,
                                          winner_value_batch.view_as(value))
                policy_loss = -torch.mean(
                    torch.sum(mcts_probs_batch * log_act_probs, dim=-1))
                loss = value_loss + policy_loss

                # 反向传播
                self.optimizer.zero_grad()
                loss.backward()
                self.optimizer.step()
            print(f"epoch:{i},loss:{loss}")
            self.writer.add_scalar("loss", loss, i)
            self.net.add_histogram(self.writer, i)
            if (i + 1) % self.check_freq == 0:
                torch.save(self.net.state_dict(), self.net_path)

    # 多样化数据样本
    def extend_sample(self, data):
        extend_data = []
        for state, mcts_prob, winner_value in data:
            extend_data.append((state, mcts_prob, winner_value))
            # 分别旋转 90度/180度/270度,增加数据多样性
            for i in range(1, 4):
                # 同时旋转棋盘状态和概率值
                state_ = np.rot90(state, i, (1, 2))
                mcts_prob_ = np.rot90(
                    mcts_prob.reshape(self.env.height, self.env.width), i)
                extend_data.append(
                    (state_, mcts_prob_.flatten(), winner_value))

                # 翻转棋盘,将矩阵中的每一位玩家的状态进行翻转
                state_ = np.array([np.fliplr(s) for s in state_])
                mcts_prob_ = np.fliplr(mcts_prob_)
                extend_data.append(
                    (state_, mcts_prob_.flatten(), winner_value))
        # 将样本存入样本池
        self.buffer.extend(extend_data)

    # 用于player调用神经网络获得动作概率,当前局面价值
    def policy(self, env):
        # 获取可用动作 15*15=225
        action_avail = env.action_avail
        # 获得当前状态
        state = torch.from_numpy(env.get_state).unsqueeze(0).to(self.device)

        # 放入神经网络得到预测的log动作概率以及当前状态的胜率
        log_action_probs, value = self.net(state)

        # 把 log 动作概率转换为动作概率并过滤不可用动作
        act_probs = torch.exp(
            log_action_probs).detach().cpu().numpy().flatten()
        act_probs = zip(action_avail, act_probs[action_avail])
        value = value.item()

        # 返回动作概率,当前局面价值
        return act_probs, value
Пример #13
0
def train():
    print("my name is van")
    # let the random counld be the same

    if cfg.train_use_offline_feat:
        data_set = TrainSetExt()
    else:
        data_set = TrainDataset()
    data_loader = DataLoader(data_set,
                             batch_size=1,
                             shuffle=True,
                             drop_last=False)

    net = MyNet(data_set.classes)
    net.print_net_info()

    epoch, iteration, w_path = get_check_point()
    if w_path:
        model = torch.load(w_path)
        if cfg.train_use_offline_feat:
            net.load_state_dict(model)
        else:
            net.load_state_dict(model)
        print("Using the model from the last check point:%s" % (w_path),
              end=" ")
        epoch += 1

    net.train()
    is_cuda = cfg.use_cuda
    did = cfg.device_id
    if is_cuda:
        net.cuda(did)

    while epoch < cfg.epochs:

        # train the rpn
        print('******epoch %d*********' % (epoch))

        for i, (imgs, boxes, labels, scale,
                img_sizes) in tqdm(enumerate(data_loader)):
            if is_cuda:
                imgs = imgs.cuda(did)
                labels = labels.cuda(did)
                boxes = boxes.cuda(did)
                scale = scale.cuda(did).float()
                img_sizes = img_sizes.cuda(did).float()
            loss = net.train_once(imgs, boxes, labels, scale, img_sizes,
                                  cfg.train_use_offline_feat)
            tqdm.write('Epoch:%d, iter:%d, loss:%.5f' %
                       (epoch, iteration, loss))

            iteration += 1

        if cfg.train_use_offline_feat:
            torch.save(net.state_dict(),
                       '%sweights_%d_%d' % (cfg.weights_dir, epoch, iteration))
        else:
            torch.save(net.state_dict(),
                       '%sweights_%d_%d' % (cfg.weights_dir, epoch, iteration))

        _map = eval_net(net=net, num=100, shuffle=True)['map']
        print("map:", _map)
        epoch += 1
    print(eval_net(net=net))
Пример #14
0
def eval_net(net=None, num=cfg.eval_number, shuffle=False):
    if cfg.test_use_offline_feat:
        data_set = TestSetExt()
    else:
        data_set = TestDataset()
    data_loader = DataLoader(data_set,
                             batch_size=1,
                             shuffle=shuffle,
                             drop_last=False)

    is_cuda = cfg.use_cuda
    did = cfg.device_id

    if net is None:
        classes = data_set.classes
        net = MyNet(classes)
        _, _, last_time_model = get_check_point()
        # assign directly
        # last_time_model='./weights/weights_21_110242'

        if os.path.exists(last_time_model):
            model = torch.load(last_time_model)
            if cfg.test_use_offline_feat:
                net.load_state_dict(model)
            else:
                net.load_state_dict(model)
            print("Using the model from the last check point:`%s`" %
                  (last_time_model))

            if is_cuda:
                net.cuda(did)
        else:
            raise ValueError("no model existed...")

    net.eval()

    upper_bound = num

    gt_bboxes = []
    gt_labels = []
    gt_difficults = []
    pred_bboxes = []
    pred_classes = []
    pred_scores = []

    for i, (img, sr_im_size, cur_im_size, gt_box, label,
            diff) in tqdm(enumerate(data_loader)):
        if i > upper_bound:
            break

        sr_im_size = sr_im_size.float()
        cur_im_size = cur_im_size.float()
        if is_cuda:
            img = img.cuda(did)
            im_size = sr_im_size.cuda(did)
            cur_im_size = cur_im_size.cuda(did)

        pred_box, pred_class, pred_prob = net(
            img,
            im_size,
            offline_feat=cfg.test_use_offline_feat,
            cur_image_size=cur_im_size)[0]
        prob_mask = pred_prob > cfg.out_thruth_thresh
        pbox = pred_box[prob_mask]
        plabel = pred_class[prob_mask].long()
        pprob = pred_prob[prob_mask]

        gt_bboxes += list(gt_box.numpy())
        gt_labels += list(label.numpy())
        gt_difficults += list(diff.numpy().astype('bool'))

        pred_bboxes += [pbox.cpu().detach().numpy()]
        pred_classes += [plabel.cpu().numpy()]
        pred_scores += [pprob.cpu().detach().numpy()]

        # pred_bboxes+=[np.empty(0) ]
        # pred_classes+=[np.empty(0) ]
        # pred_scores+=[np.empty(0) ]

    res = voc_eval(pred_bboxes,
                   pred_classes,
                   pred_scores,
                   gt_bboxes,
                   gt_labels,
                   gt_difficults,
                   use_07_metric=True)
    # print(res)

    # avoid potential error
    net.train()

    return res
Пример #15
0
                    type=str,
                    default='./dataset/cifar10',
                    help='Dataset directory')
parser.add_argument('--num_workers',
                    type=int,
                    default=64,
                    help='Number of workers used in dataloading')
parser.add_argument('--batch_size', type=int, default=128, help='Batch size')

args, _ = parser.parse_known_args()

device = 'cuda'
gpus = get_gpus(args.gpus)

if __name__ == '__main__':
    model = MyNet()
    model = torch.nn.DataParallel(model, device_ids=gpus)
    batch_size = args.batch_size * len(gpus)

    if os.path.exists(args.data_dir):
        download = False
    else:
        download = True

    val_loader = get_dataloader(args.data_dir,
                                batch_size,
                                num_workers=args.num_workers,
                                shuffle=False,
                                train=False,
                                download=download)
Пример #16
0
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

trainset = torchvision.datasets.CIFAR10(root='./data',
                                        train=True,
                                        download=True,
                                        transform=transform)
trainloader = torch.utils.data.DataLoader(trainset,
                                          batch_size=batch_size,
                                          shuffle=True,
                                          num_workers=2)

### Initialize the neural net and begin the training loop.
net = MyNet()

# NOTE: Cross-entropy loss is the default for classification, but try looking
# into other loss functions that could be used.
criterion = nn.CrossEntropyLoss()

# NOTE: Check out the torch.optim library for other optimization algorithms that
# could be used instead.
optimizer = optim.SGD(net.parameters(), lr=learning_rate)

# Begin the training loop.
for epoch in range(num_epoch):
    # Helps keep track of where you are in training. Don't like this method?
    # Check out the tqdm module, which will print a loading bar for for loops.
    print("Epoch:", epoch + 1)
Пример #17
0
# Load the testing dataset
testset = torchvision.datasets.CIFAR10(root='./data',
                                       train=False,
                                       download=True,
                                       transform=transform)
testloader = torch.utils.data.DataLoader(testset,
                                         batch_size=10000,
                                         shuffle=False,
                                         num_workers=2)

# Define the classes in the dataset
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',
           'ship', 'truck')

# Reload the neural net
net = MyNet()
net.load_state_dict(torch.load("./" + net_name + ".pth"))

# Get accuracy of the trained net as a whole
correct = 0
total = 0
with torch.no_grad():
    for data in testloader:
        images, labels = data
        outputs = net(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print('Accuracy of the network on the 10000 test images: %d %%' %
      (100 * correct / total))
Пример #18
0
parser = argparse.ArgumentParser()
parser.add_argument('--sparsity',
                    type=float,
                    default=0.5,
                    help='Sparsity ratio')
parser.add_argument('--save_dir',
                    type=str,
                    default='./',
                    help='Where to save retrained model')
args, _ = parser.parse_known_args()

device = 'cuda'

if __name__ == '__main__':
    sparse_model_path = os.path.join(args.save_dir, 'mynet_sparse.pth')
    assert os.path.exists(sparse_model_path), "No sparse model!"

    slim_model_path = os.path.join(args.save_dir, 'mynet_slim.pth')
    sparse_model = MyNet()
    sparse_model = load_weights(sparse_model, sparse_model_path)
    sparse_model.to(device)
    input_signature = torch.randn([1, 3, 32, 32], dtype=torch.float32)
    input_signature = input_signature.to(device)

    pruning_runner = get_pruning_runner(sparse_model, input_signature,
                                        'iterative')
    slim_model = pruning_runner.prune(removal_ratio=args.sparsity, mode='slim')
    torch.save(slim_model.state_dict(), slim_model_path)
    print('Convert sparse model to slim model done!')
Пример #19
0
def do_train(data_path,
             model_name='mymodel',
             use_gpu=False,
             epoch_num=5,
             batch_size=100,
             learning_rate=0.01):
    place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
    with fluid.dygraph.guard(place):
        model = MyNet()
        model.train()
        train_loader = load_data(data_path, mode='train')

        optimizer = fluid.optimizer.SGDOptimizer(
            learning_rate=learning_rate, parameter_list=model.parameters())

        iter = 0
        for epoch_id in range(epoch_num):
            for batch_id, data in enumerate(train_loader()):
                #准备数据,格式需要转换成符合框架要求的
                image_data, label_data = data
                # 将数据转为飞桨动态图格式
                image = fluid.dygraph.to_variable(image_data)
                label = fluid.dygraph.to_variable(label_data)

                # #前向计算的过程
                # predict = model(image)
                #前向计算的过程,同时拿到模型输出值和分类准确率
                predict, avg_acc = model(image, label)

                #计算损失,取一个批次样本损失的平均值
                # loss = fluid.layers.square_error_cost(predict, label)
                loss = fluid.layers.cross_entropy(predict, label)
                avg_loss = fluid.layers.mean(loss)

                #每训练了1000批次的数据,打印下当前Loss的情况
                if batch_id != 0 and batch_id % 100 == 0:
                    print(
                        "epoch: {}, batch: {}, loss is: {}, acc is: {}".format(
                            epoch_id, batch_id, avg_loss.numpy(),
                            avg_acc.numpy()))
                    log_writer.add_scalar(tag='acc',
                                          step=iter,
                                          value=avg_acc.numpy())
                    log_writer.add_scalar(tag='loss',
                                          step=iter,
                                          value=avg_loss.numpy())
                    iter = iter + 100

                #后向传播,更新参数的过程
                avg_loss.backward()
                optimizer.minimize(avg_loss)
                model.clear_gradients()

            fluid.save_dygraph(
                model.state_dict(),
                os.path.join(CHECKPOINT_PATH,
                             f'{model_name}_epoch_{epoch_id}'))
            fluid.save_dygraph(
                optimizer.state_dict(),
                os.path.join(CHECKPOINT_PATH,
                             f'{model_name}_epoch_{epoch_id}'))

        # 保存模型
        fluid.save_dygraph(model.state_dict(),
                           os.path.join(MODEL_PATH, model_name))
Пример #20
0
    torch.cuda.set_device(args.local_rank)
    batch_size = args.batch_size * len(gpus)
    train_loader = get_dataloader_ddp(args.data_dir,
                                      batch_size,
                                      num_workers=args.num_workers,
                                      shuffle=True,
                                      train=True,
                                      download=download)
    val_loader = get_dataloader_ddp(args.data_dir,
                                    batch_size,
                                    num_workers=args.num_workers,
                                    shuffle=False,
                                    train=False,
                                    download=download)

    model = MyNet()
    model = load_weights(model, args.pretrained)
    input_signature = torch.randn([1, 3, 32, 32], dtype=torch.float32)
    input_signature = input_signature.to(device)
    model = model.to(device)
    pruning_runner = get_pruning_runner(model, input_signature, 'iterative')

    model = pruning_runner.prune(removal_ratio=args.sparsity, mode='sparse')
    model = torch.nn.parallel.DistributedDataParallel(
        model,
        device_ids=[args.local_rank],
        output_device=args.local_rank,
        find_unused_parameters=True)
    criterion = torch.nn.CrossEntropyLoss().cuda()
    optimizer = torch.optim.Adam(model.parameters(),
                                 args.lr,
Пример #21
0
def eval_net(net=None,num=cfg.eval_number,shuffle=False):
    data_set=TestDataset()
    data_loader=DataLoader(data_set,batch_size=1,shuffle=shuffle,drop_last=False)
    
    is_cuda=cfg.use_cuda
    did=cfg.device_id

    if net is None:
        classes=data_set.classes
        net=MyNet(len(classes)+1)
        _,_,last_time_model=get_check_point()

        if os.path.exists(last_time_model):
            model=torch.load(last_time_model)
            net.load_state_dict(model)
            print("Using the model from the last check point:`%s`"%(last_time_model))
            
            if is_cuda:
                net.cuda(did)
        else:
            raise ValueError("no model existed...")

    net.eval()
   
    upper_bound=num

    gt_bboxes=[]
    gt_labels=[]
    gt_difficults=[]
    pred_bboxes=[]
    pred_classes=[]
    pred_scores=[]

    for i,(img,sr_im_size,gt_box,label,diff) in tqdm(enumerate(data_loader)):
        assert img.shape[0]==1
        if i> upper_bound:
            break

        sr_im_size=sr_im_size.float()
        if is_cuda:
            img=img.cuda(did)
            im_size=sr_im_size.cuda(did)
            
        pred_box,pred_class,pred_prob=net.predict(img,im_size)[0]
        prob_mask=pred_prob>cfg.out_thruth_thresh
        pbox=pred_box[prob_mask ] 
        plabel=pred_class[prob_mask ].long()
        pprob=pred_prob[prob_mask]

        gt_box=gt_box.numpy()

        if len(gt_box)!=0:
            # print(gt_box.shape)
            gt_box=gt_box[:,:,[1,0,3,2]] # change `xyxy` to `yxyx` 
        gt_bboxes += list(gt_box )
        gt_labels += list(label.numpy())
        gt_difficults += list(diff.numpy().astype('bool'))

        pbox=pbox.cpu().detach().numpy()
        if len(pbox)!=0:
            pbox=pbox[:,[1,0,3,2]] # change `xyxy` to `yxyx`
        pred_bboxes+=[pbox]
        pred_classes+=[plabel.cpu().numpy()]
        pred_scores+=[pprob.cpu().detach().numpy()]

        # pred_bboxes+=[np.empty(0) ]
        # pred_classes+=[np.empty(0) ]
        # pred_scores+=[np.empty(0) ]

    res=voc_eval(pred_bboxes,pred_classes,pred_scores,
        gt_bboxes,gt_labels,gt_difficults,use_07_metric=True)
    # print(res)

    # avoid potential error
    net.train()

    return res