def __init__(self):
        self.model = get_net()
        state_dict = torch.load(
            r'..\static\model\model.pkl', map_location=device)

        self.model.load_state_dict(state_dict)
        self.model.eval()
Exemple #2
0
def calc_pos(val_path="../data/cub/CUB200/images/val/", log_path="./loc.txt"):
    model = net.get_net(classes_num=200, channel_size=2048)
    model.load_state_dict(
        torch.load("./weights/cub/resnet50_cub_best_acc.pth"), strict=True)
    transform = transforms.Compose([
        transforms.Resize(size=(224, 224)),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])
    model.eval()
    dir_list = os.listdir(val_path)
    log_list = []
    for dir_ in dir_list:
        imgae_list = os.listdir(val_path + "/" + dir_)
        for image in imgae_list:
            image_path = val_path + "/" + dir_ + "/" + image
            p1, p2 = model.get_boundbox_image(image_path, transform)
            log_list.append([image, p1, p2])
            print("{} finished".format(image_path))
    with open(log_path, "w", encoding="utf-8") as file:
        for item in log_list:
            image, p1, p2 = item
            content = "{},{},{},{},{}".format(image, p1[0], p1[1], p2[0],
                                              p2[1]) + "\n"
            file.write(content)
Exemple #3
0
 def build_qnet(self):
     net_func = get_net(self._net_name)
     hidden_units = self._layer_size + [self._action_size]
     self._q = net_func(self._state,
                        hidden_units=hidden_units,
                        name='Online')
     self._q_next = net_func(self._state_next,
                             hidden_units=hidden_units,
                             name='Target')
Exemple #4
0
def get_dogs_bounding_box():
	model=net.get_net(classes_num=120,channel_size=2048)
	model.load_state_dict(torch.load("./weights/dogs/resnet50_dogs_best_acc.pth"),strict=True)
	transform=transforms.Compose([transforms.Resize(size=(224,224)),
		transforms.ToTensor(),
		transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])])
	model.eval()
	img_name_list=os.listdir("./utils/ori/dogs/")

	for img_name in img_name_list:
		old_img_path="./utils/ori/dogs/"+img_name
		new_image_path="./utils/bnd/dogs/"+img_name
		model.get_boundbox_image(old_img_path,transform,new_image_path)
Exemple #5
0
def vis_cub():
	model=net.get_net(classes_num=200,channel_size=2048)
	model.load_state_dict(torch.load("./weights/cub/resnet50_cub_best_acc.pth"),strict=True)
	transform=transforms.Compose([transforms.Resize(size=(224,224)),
		transforms.ToTensor(),
		transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])])
	model.eval()
	img_name_list=os.listdir("./utils/ori/cub/")

	for img_name in img_name_list:
		old_img_path="./utils/ori/cub/"+img_name
		attention_img_path="./utils/new/cub/"+img_name
		model.get_attention_map(old_img_path,transform,attention_img_path)
		print("{} finished".format(old_img_path))
Exemple #6
0
def count_cub():
	model=net.get_net(classes_num=200,channel_size=2048)
	model.load_state_dict(torch.load("./weights/cub/resnet50_cub_best_acc.pth"),strict=True)
	transform=transforms.Compose([transforms.Resize(size=(224,224)),
		transforms.ToTensor(),
		transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])])
	model.eval()
	img_name_list=os.listdir("./utils/ori/cub/")
	feature_vec=[]
	for img_name in img_name_list:
		old_img_path="./utils/ori/cub/"+img_name
		vec=model.get_count_result(old_img_path,transform)
		feature_vec.append(vec)
	feature_vec=np.concatenate(feature_vec,axis=0)
	print(feature_vec.shape)
	draw_hist(feature_vec)
    def predict_all(self, datas):
        my_model = get_net()
        my_model.load_state_dict(
            torch.load(os.path.join(MODEL_PATH, TORCH_MODEL_NAME)))
        my_model.to(device)
        for param in my_model.parameters():
            param.requires_grad = False
        my_model.eval()
        labels = []
        step = 0
        for data in datas:
            # print('predict step : ', step)
            step += 1
            x_data = self.data.predict_data(**data)
            img = cv2.imread(os.path.join(DATA_PATH, x_data[0]))
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            img = torchvision.transforms.ToTensor()(img)
            pred = my_model([img.to(device)])[0]
            for (k, v) in pred.items():
                pred[k] = v.cpu().numpy()

            # print('labels len is ',len(pred['labels']))
            for j in range(len(pred['labels'])):
                labels.append([
                    x_data[0], pred['scores'][j], pred['boxes'][j],
                    pred['labels'][j] - 1
                ])  # 这里pred['labels'][j]-1 用于与标签对应 0-没有佩戴,1-有佩戴
        ''' 关于返回的说明: 
        返回labels为一个list集合,其中每一项样例为['img/000278.jpg', 0.99910635, [117.190445, 108.4803, 163.91493, 222.29749], 1],包括:
            x_data[0]:图片的相对路径
            pred['scores'][j]:预测框的置信度
            pred['boxes'][j]:预测框的坐标 [x_min, y_min, x_max, y_max]
            pred['labels'][j]:预测框所属类别
        评估指标为map。
        '''
        return labels
def main():
    start_time = time()
    last_step = get_last_ckpt_step()
    assert last_step >= 0
    my_log(f'Checkpoint found: {last_step}\n')
    print_args()

    net_init, net_apply, net_init_cache, net_apply_fast = get_net()

    params = load_ckpt(last_step)
    in_shape = (args.batch_size, args.L, args.L, 1)
    _, cache_init = net_init_cache(params, jnp.zeros(in_shape), (-1, -1))

    # sample_raw_fun = get_sample_fun(net_apply, None)
    sample_raw_fun = get_sample_fun(net_apply_fast, cache_init)
    # sample_k_fun = get_sample_k_fun(net_apply, None)
    sample_k_fun = get_sample_k_fun(net_apply_fast, net_init_cache)
    log_q_fun = get_log_q_fun(net_apply)

    @jit
    def update(spins_old, log_q_old, energy_old, step, accept_count,
               energy_mean, energy_var_sum, rng):
        rng, rng_k, rng_sample, rng_accept = jrand.split(rng, 4)
        k = get_k(rng_k)
        spins = sample_k_fun(k, params, spins_old, rng_sample)
        log_q = log_q_fun(params, spins)
        energy = energy_fun(spins)

        log_uniform = jnp.log(jrand.uniform(rng_accept, (args.batch_size, )))
        accept = log_uniform < (log_q_old - log_q + args.beta *
                                (energy_old - energy))

        spins = jnp.where(jnp.expand_dims(accept, axis=(1, 2, 3)), spins,
                          spins_old)
        log_q = jnp.where(accept, log_q, log_q_old)
        energy = jnp.where(accept, energy, energy_old)
        mag = spins.mean(axis=(1, 2, 3))

        step += 1
        accept_count += accept.sum()
        energy_per_spin = energy / args.L**2
        energy_mean, energy_var_sum = welford_update(energy_per_spin.mean(),
                                                     step, energy_mean,
                                                     energy_var_sum)

        return (spins, log_q, energy, mag, accept, k, step, accept_count,
                energy_mean, energy_var_sum, rng)

    rng, rng_init = jrand.split(jrand.PRNGKey(args.seed))
    # Sample initial configurations from the network
    spins = sample_raw_fun(args.batch_size, params, rng_init)
    log_q = log_q_fun(params, spins)
    energy = energy_fun(spins)

    step = 0
    accept_count = 0
    energy_mean = 0
    energy_var_sum = 0

    data_filename = args.log_filename.replace('.log', '.hdf5')
    writer_proto = [
        # Uncomment to save all the sampled spins
        # ('spins', bool, (args.batch_size, args.L, args.L)),
        ('log_q', np.float32, (args.batch_size, )),
        ('energy', np.int32, (args.batch_size, )),
        ('mag', np.float32, (args.batch_size, )),
        ('accept', bool, (args.batch_size, )),
        ('k', np.int32, None),
    ]
    ensure_dir(data_filename)
    with ChunkedDataWriter(data_filename, writer_proto,
                           args.save_step) as writer:
        my_log('Sampling...')
        while step < args.max_step:
            (spins, log_q, energy, mag, accept, k, step, accept_count,
             energy_mean, energy_var_sum,
             rng) = update(spins, log_q, energy, step, accept_count,
                           energy_mean, energy_var_sum, rng)
            # Uncomment to save all the sampled spins
            # writer.write(spins[:, :, :, 0] > 0, log_q, energy, mag, accept, k)
            writer.write(log_q, energy, mag, accept, k)

            if args.print_step and step % args.print_step == 0:
                accept_rate = accept_count / (step * args.batch_size)
                energy_std = jnp.sqrt(energy_var_sum / step)
                my_log(', '.join([
                    f'step = {step}',
                    f'P = {accept_rate:.8g}',
                    f'E = {energy_mean:.8g}',
                    f'E_std = {energy_std:.8g}',
                    f'time = {time() - start_time:.3f}',
                ]))
Exemple #9
0
def main():
    start_time = time()
    last_step = get_last_ckpt_step()
    assert last_step >= 0
    my_log(f'Checkpoint found: {last_step}\n')
    print_args()

    net_init, net_apply, net_init_cache, net_apply_fast = get_net()

    params = load_ckpt(last_step)
    in_shape = (args.batch_size, args.L, args.L, 1)
    _, cache_init = net_init_cache(params, jnp.zeros(in_shape), (-1, -1))

    # sample_fun = get_sample_fun(net_apply, None)
    sample_fun = get_sample_fun(net_apply_fast, cache_init)
    log_q_fun = get_log_q_fun(net_apply)

    def sample_energy_fun(rng):
        spins = sample_fun(args.batch_size, params, rng)
        log_q = log_q_fun(params, spins)
        energy = energy_fun(spins)
        return spins, log_q, energy

    @jit
    def update(spins_old, log_q_old, energy_old, step, energy_mean,
               energy_var_sum, rng):
        rng, rng_sample = jrand.split(rng)
        spins, log_q, energy = sample_energy_fun(rng_sample)
        mag = spins.mean(axis=(1, 2, 3))

        step += 1
        energy_per_spin = energy / args.L**2
        energy_mean, energy_var_sum = welford_update(energy_per_spin.mean(),
                                                     step, energy_mean,
                                                     energy_var_sum)

        return (spins, log_q, energy, mag, step, energy_mean, energy_var_sum,
                rng)

    rng, rng_init = jrand.split(jrand.PRNGKey(args.seed))
    spins, log_q, energy = sample_energy_fun(rng_init)

    step = 0
    energy_mean = 0
    energy_var_sum = 0

    data_filename = args.log_filename.replace('.log', '.hdf5')
    writer_proto = [
        # Uncomment to save all the sampled spins
        # ('spins', bool, (args.L, args.L)),
        ('log_q', np.float32, None),
        ('energy', np.int32, None),
        ('mag', np.float32, None),
    ]
    ensure_dir(data_filename)
    with ChunkedDataWriter(data_filename, writer_proto,
                           args.save_step * args.batch_size) as writer:
        my_log('Sampling...')
        while step < args.max_step:
            (spins, log_q, energy, mag, step, energy_mean, energy_var_sum,
             rng) = update(spins, log_q, energy, step, energy_mean,
                           energy_var_sum, rng)
            # Uncomment to save all the sampled spins
            # writer.write_batch(spins[:, :, :, 0] > 0, log_q, energy, mag)
            writer.write_batch(log_q, energy, mag)

            if args.print_step and step % args.print_step == 0:
                energy_std = jnp.sqrt(energy_var_sum / step)
                my_log(', '.join([
                    f'step = {step}',
                    f'E = {energy_mean:.8g}',
                    f'E_std = {energy_std:.8g}',
                    f'time = {time() - start_time:.3f}',
                ]))
train_batch_size = args.BATCH
valid_batch_size = 1
train_data_loader = torch.utils.data.DataLoader(train_dataset,
                                                batch_size=train_batch_size,
                                                shuffle=True,
                                                num_workers=0,
                                                collate_fn=collate_fn)
valid_data_loader = torch.utils.data.DataLoader(valid_dataset,
                                                batch_size=valid_batch_size,
                                                shuffle=False,
                                                num_workers=0,
                                                collate_fn=collate_fn)
'''
实现自己的网络机构
'''
my_model = get_net()
# 判断gpu是否可用
if torch.cuda.is_available():
    device = 'cuda'
    print('pytorch use cuda')
else:
    device = 'cpu'
    print('pytorch use cpu')

device = torch.device(device)
my_model.to(device)

TORCH_MODEL_NAME = "model.pkl"
lr = 1e-4
params = [p for p in my_model.parameters() if p.requires_grad]
optimizer = torch.optim.SGD(params, lr=lr, momentum=0.9, weight_decay=0.001)
Exemple #11
0
parser.add_argument('--model_choice', default=MODEL_CHOICE, type=int, help='MODEL_CHOICE')
parser.add_argument('--which_epoch',default='last', type=str, help='0,1,2,3...or last')
parser.add_argument('--batch_size', default=BATCH_SIZE, type=int, help='batchsize')
parser.add_argument('--num_workers', default=1, type=int, help='num_workers')
opt = parser.parse_args()

MODEL_NAME = MODEL_DICT[opt.model_choice]
DATASET_NAME = DATASET_DICT[opt.data_choice]

data_dir = os.path.join(DATA_ROOT, DATASET_NAME)
model_dir = os.path.join(MODEL_ROOT, DATASET_NAME, MODEL_NAME)
result_dir = os.path.join(RESULT_ROOT, DATASET_NAME, MODEL_NAME)
if not os.path.isdir(result_dir):
    os.makedirs(result_dir)

create_net = get_net(opt.model_choice)

######################################################################
# Load Data
# ---------
data_transforms = transforms.Compose([
        transforms.Resize((288,144), interpolation=3),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

image_datasets = {x: datasets.ImageFolder( os.path.join(data_dir,x) ,data_transforms) for x in ['gallery','query']}
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=opt.batch_size,
                shuffle=False, num_workers=opt.num_workers) for x in ['gallery','query']}

class_names = image_datasets['query'].classes
Exemple #12
0
def train():
    '''
	进行训练
	'''
    #定义记录部分
    log = Metrics.Logger(config.csv_path, config.tb_path)
    #定义模型
    if config.start_epoch == 1:
        model = net.get_net(classes_num=config.classes_num,
                            channel_size=config.channel_size,
                            cnn_weights_path=config.cnn_weights_path,
                            drop_rate=config.drop_rate)
    else:
        model = net.get_net(classes_num=config.classes_num,
                            channel_size=config.channel_size,
                            drop_rate=config.drop_rate)

    print("model load succeed")

    if config.use_cuda:
        model = model.cuda()  #将model转到cuda上
    if config.use_parallel:
        model = nn.DataParallel(model, device_ids=config.device_ids)
        cudnn.benchmark = True
    if config.start_epoch != 1:
        all_weights_path = config.save_weights_path.format(config.start_epoch -
                                                           1)
        model.load_state_dict(torch.load(all_weights_path))
        print("{} load succeed".format(all_weights_path))

    #加载数据集
    train_folder = config.train_img_path
    validate_folder = config.validate_img_path
    train_loader = ImageReader.get_loader("train", train_folder)
    validate_loader = ImageReader.get_loader("validate", validate_folder)

    #定义优化器和学习率调度器
    optimizer = SGD(params=model.parameters(),
                    lr=config.start_lr,
                    momentum=0.9,
                    weight_decay=config.weight_decay)
    scheduler = optim.lr_scheduler.MultiStepLR(optimizer,
                                               milestones=config.stones,
                                               gamma=0.1)

    #定义评估函数
    accuracy = Metrics.AccuracyList(num=1)
    l_train = Metrics.LossList(1)
    l_val = Metrics.LossList(1)

    #定义最好的准确率
    best_acc = 0
    for i in range(config.start_epoch, config.start_epoch + config.num_epoch):
        #分配学习率
        scheduler.step(epoch=i)
        lr = scheduler.get_lr()[0]

        print("{} epoch start , lr is {}".format(i, lr))

        #开始训练这一轮
        model.train()
        accuracy.set_zeros()
        l_train.set_zeros()
        train_step = 0
        for (x1, x2), y in train_loader:
            x1 = Variable(x1)
            x2 = Variable(x2)
            y = Variable(y)
            if config.use_cuda:
                x1 = x1.cuda()
                x2 = x2.cuda()
                y = y.cuda(async=True)
            optimizer.zero_grad()  #清空梯度值
            y_ = model(x1, x2)  #求y
            #求这一步的损失值和准确率
            step_loss, loss_list = model.get_loss(y_, y)
            step_acc = accuracy(y_, y)
            l_train.log(loss_list)

            #更新梯度值
            step_loss.backward()
            optimizer.step()

            train_step += 1  #训练步数+1

            #输出这一步的记录
            print("{} epoch,{} step,step loss is {:.6f},step acc is {:.4f}".
                  format(i, train_step, loss_list[0], max(step_acc)))
            del (step_loss, x1, x2, y, y_)
        #求这一轮训练情况
        train_acc_list = accuracy.get_acc_list()
        train_loss_list = l_train.get_loss_list()

        #保存模型
        weights_name = config.save_weights_path.format(i)
        torch.save(model.state_dict(), weights_name)
        del_weights_name = config.save_weights_path.format(i - 1)
        if os.path.exists(del_weights_name):
            os.remove(del_weights_name)
        print("{} save,{} delete".format(weights_name, del_weights_name))

        #开始验证步骤
        model.eval()
        accuracy.set_zeros()  #将accuracy中total_sample和total_correct清0
        l_val.set_zeros()
        for (x1, x2), y in validate_loader:
            x1 = Variable(x1, requires_grad=False)
            x2 = Variable(x2, requires_grad=False)
            y = Variable(y, requires_grad=False)
            if config.use_cuda:
                x1 = x1.cuda()
                x2 = x2.cuda()
                y = y.cuda(async=True)
            y_ = model(x1, x2)
            _, loss_list = model.get_valloss(y_, y)
            accuracy(y_, y)
            l_val.log(loss_list)
            del (x1, x2, y, y_, _)
        val_acc_list = accuracy.get_acc_list()
        val_loss_list = l_val.get_loss_list()
        print("validate end,log start")

        #保存最佳的模型
        if best_acc < max(val_acc_list):
            weights_name = config.save_weights_path.format("best_acc")
            torch.save(model.state_dict(), weights_name)
            best_acc = max(val_acc_list)

        #求model的正则化项
        l2_reg = 0.0
        for param in model.parameters():
            l2_reg += torch.norm(param).data[0]
        #开始记录
        log.log(i, train_acc_list, train_loss_list, val_acc_list,
                val_loss_list, l2_reg, lr)
        print("log end ...")

        print(
            "{} epoch end, train loss is {},train acc is {},val loss is {},val acc is {},weight l2 norm is {}"
            .format(i, train_loss_list[0], max(train_acc_list),
                    val_loss_list[0], max(val_acc_list), l2_reg))
    del (model)
    print("{} train end,best_acc is {}...".format(config.dataset, best_acc))
Exemple #13
0
def main():
    start_time = time()
    init_out_dir()
    last_step = get_last_ckpt_step()
    if last_step >= 0:
        my_log(f'\nCheckpoint found: {last_step}\n')
    else:
        clear_log()
    print_args()

    net_init, net_apply, net_init_cache, net_apply_fast = get_net()

    rng, rng_net = jrand.split(jrand.PRNGKey(args.seed))
    in_shape = (args.batch_size, args.L, args.L, 1)
    out_shape, params_init = net_init(rng_net, in_shape)

    _, cache_init = net_init_cache(params_init, jnp.zeros(in_shape), (-1, -1))

    # sample_fun = get_sample_fun(net_apply, None)
    sample_fun = get_sample_fun(net_apply_fast, cache_init)
    log_q_fun = get_log_q_fun(net_apply)

    need_beta_anneal = args.beta_anneal_step > 0

    opt_init, opt_update, get_params = optimizers.adam(args.lr)

    @jit
    def update(step, opt_state, rng):
        params = get_params(opt_state)
        rng, rng_sample = jrand.split(rng)
        spins = sample_fun(args.batch_size, params, rng_sample)
        log_q = log_q_fun(params, spins) / args.L**2
        energy = energy_fun(spins) / args.L**2

        def neg_log_Z_fun(params, spins):
            log_q = log_q_fun(params, spins) / args.L**2
            energy = energy_fun(spins) / args.L**2
            beta = args.beta
            if need_beta_anneal:
                beta *= jnp.minimum(step / args.beta_anneal_step, 1)
            neg_log_Z = log_q + beta * energy
            return neg_log_Z

        loss_fun = partial(expect,
                           log_q_fun,
                           neg_log_Z_fun,
                           mean_grad_expected_is_zero=True)
        grads = grad(loss_fun)(params, spins, spins)
        opt_state = opt_update(step, grads, opt_state)

        return spins, log_q, energy, opt_state, rng

    if last_step >= 0:
        params_init = load_ckpt(last_step)

    opt_state = opt_init(params_init)

    my_log('Training...')
    for step in range(last_step + 1, args.max_step + 1):
        spins, log_q, energy, opt_state, rng = update(step, opt_state, rng)

        if args.print_step and step % args.print_step == 0:
            # Use the final beta, not the annealed beta
            free_energy = log_q / args.beta + energy
            my_log(', '.join([
                f'step = {step}',
                f'F = {free_energy.mean():.8g}',
                f'F_std = {free_energy.std():.8g}',
                f'S = {-log_q.mean():.8g}',
                f'E = {energy.mean():.8g}',
                f'time = {time() - start_time:.3f}',
            ]))

        if args.save_step and step % args.save_step == 0:
            params = get_params(opt_state)
            save_ckpt(params, step)
Exemple #14
0
def train():
    '''
	进行训练
	'''
    #定义记录部分
    csv_path = config.csv_path
    tb_path = config.tb_path
    writer = SummaryWriter(log_dir=tb_path)

    #定义模型

    if config.start_epoch == 1:
        model = net.get_net(classes_num=config.classes_num,
                            channel_size=config.channel_size,
                            cnn_weights_path=config.cnn_weights_path,
                            drop_rate=config.drop_rate)
    else:
        model = net.get_net(classes_num=config.classes_num,
                            channel_size=config.channel_size,
                            drop_rate=config.drop_rate)

    print("model load succeed")

    if config.use_cuda:
        model = model.cuda()  #将model转到cuda上
    if config.use_parallel:
        model = nn.DataParallel(model, device_ids=config.device_ids)
        cudnn.benchmark = True
    if config.start_epoch != 1:
        all_weights_path = config.save_weights_path.format(config.start_epoch -
                                                           1)
        model.load_state_dict(torch.load(all_weights_path))
        print("{} load succeed".format(all_weights_path))

    #加载数据集
    train_folder = config.train_img_path
    validate_folder = config.validate_img_path
    train_loader = ImageReader.getLoader(config.dataset, "train", train_folder)
    validate_loader = ImageReader.getLoader(config.dataset, "validate",
                                            validate_folder)

    #定义优化器和学习率调度器
    optimizer = SGD(params=model.parameters(),
                    lr=config.start_lr,
                    momentum=0.9,
                    weight_decay=config.weight_decay)
    scheduler = optim.lr_scheduler.MultiStepLR(optimizer,
                                               milestones=config.stones,
                                               gamma=0.1)

    #定义评估函数
    criterion = nn.CrossEntropyLoss()
    accuracy = Metrics.Accuracy()

    #定义最好的准确率
    best_acc = 0
    for i in range(config.start_epoch, config.start_epoch + config.num_epoch):
        #分配学习率
        scheduler.step(epoch=i)
        lr = scheduler.get_lr()[0]

        print("{} epoch start , lr is {}".format(i, lr))

        #开始训练这一轮
        model.train()
        accuracy.__init__()
        train_loss = 0
        train_step = 0

        for x, y in train_loader:
            x = Variable(x)
            y = Variable(y)
            if config.use_cuda:
                x = x.cuda()
                y = y.cuda(async=True)
            optimizer.zero_grad()  #清空梯度值
            y_ = model(x)  #求y
            #求这一步的损失值和准确率
            step_loss = criterion(y_, y)
            step_acc = accuracy(y_, y)
            train_loss += step_loss.data[0]

            #更新梯度值
            step_loss.backward()
            optimizer.step()

            train_step += 1  #训练步数+1

            #输出这一步的记录
            print("{} epoch,{} step,step loss is {:.6f},step acc is {:.4f}".
                  format(i, train_step, step_loss.data[0], step_acc))
            del (step_loss, x, y, y_)
        #求这一轮训练情况
        train_acc = accuracy.total_correct / (accuracy.total_sample + 1e-5)
        train_loss = train_loss / (train_step + 1e-5)

        #保存模型
        weights_name = config.save_weights_path.format(i)
        torch.save(model.state_dict(), weights_name)
        del_weights_name = config.save_weights_path.format(i - 3)
        if os.path.exists(del_weights_name):
            os.remove(del_weights_name)
        print("{} save,{} delete".format(weights_name, del_weights_name))

        #开始验证步骤
        model.eval()
        accuracy.__init__()  #将accuracy中total_sample和total_correct清0
        val_loss = 0
        val_step = 0
        for x, y in validate_loader:
            x = Variable(x, requires_grad=False)
            y = Variable(y, requires_grad=False)
            if config.use_cuda:
                x = x.cuda()
                y = y.cuda(async=True)
            y_ = model(x)
            step_loss = criterion(y_, y)
            step_acc = accuracy(y_, y)
            val_loss += step_loss.data[0]
            val_step += 1
            del (x, y, y_, step_loss)
        val_acc = accuracy.total_correct / (accuracy.total_sample + 1e-5)
        val_loss = val_loss / (val_step + 1e-5)
        print("validate end,log start")

        #保存最佳的模型
        if best_acc < val_acc:
            weights_name = config.save_weights_path.format("best_acc")
            torch.save(model.state_dict(), weights_name)
            best_acc = val_acc

        #求model的正则化项
        l2_reg = 0.0
        for param in model.parameters():
            l2_reg += torch.norm(param).data[0]
        #开始记录
        with open(csv_path, "a", encoding="utf-8") as file:
            t = get_ctime()
            content = "{},{:.6f},{:.4f},{:.6f},{:.4f},{:.6f},{}".format(
                i, train_loss, train_acc, val_loss, val_acc, l2_reg, t) + "\n"
            file.write(content)
        writer.add_scalar("Train/acc", train_acc, i)
        writer.add_scalar("Train/loss", train_loss, i)
        writer.add_scalar("Val/acc", val_acc, i)
        writer.add_scalar("Val/loss", val_loss, i)
        writer.add_scalar("lr", lr, i)
        writer.add_scalar("l2_reg", l2_reg, i)
        print("log end ...")

        print(
            "{} epoch end, train loss is {:.6f},train acc is {:.4f},val loss is \
{:.6f},val acc is {:.4f},weight l2 norm is {:.6f}".format(
                i, train_loss, train_acc, val_loss, val_acc, l2_reg))
    del (model)
    print("{} train end,best_acc is {}...".format(config.dataset, best_acc))
Exemple #15
0
import net,config,os
import ImageReader
import torch
from torchvision import transforms
import numpy as np

model=net.get_net(classes_num=200,channel_size=2048)

model.load_state_dict(
	torch.load("./weights/cub/resnet50_cub_best_acc.pth"),strict=True)
transform=transforms.Compose([transforms.Resize([224,224]),
	transforms.ToTensor(),
	transforms.Normalize(mean=(0.485, 0.456, 0.406),std=(0.229, 0.224, 0.225))])
model.eval()
#model.get_max_pos_count("./utils/ori/101.jpg",transform)
#model.get_middle_attention_map("./utils/ori/18.jpg",transform,"./utils/new/res18_middle.jpg")
img_list=os.listdir("./utils/ori/")
for img in img_list:
	classes_num=int(img.split(".")[0])
	#model.get_attention_map("./utils/ori/"+img,transform,classes_num,
	#	"./utils/new/"+img)
	model.get_res("./utils/ori/"+img,transform,classes_num,
		"./utils/new/"+img)	
Exemple #16
0
    # context
    ctx = utils.try_gpu()
    # ctx = mx.cpu()

    # neural style network
    pretrained_net = models.vgg19(pretrained=False)
    # Utlize class VGG + layers(11, 13, 16, 19. The number of conv layer) +
    #     filters(num_kernels, e.g. [64, 128, 256, 512, 512]) to define a net.
    pretrained_net.load_params(
        filename="/home/ly/mxnet/models/vgg19-f7134366.params", ctx=ctx)
    # Utlize get_model_file(name, root) to download the trained model.
    # If you set pretrained=True, Every time you run the code, it will redownload the trained model.
    # So, in here set pretrained=False, load trained parameters directly!
    # content_layers + style_layers is: [25,0,5,10,19,28]. max(content_layers + style_layers) is 28.
    net = net.get_net(pretrained_net, args.content_layers, args.style_layers)
    # Use trained model to define our neural-style network. It contains 29 layer, e.g. conv + relu + bn....
    # Every layer of neural-style network has owned parameters, so don't initialize.
    net.collect_params().reset_ctx(ctx)
    # parameters in ctx. This operate must do, ensure the parameters of network is in appointed ctx.
    # Otherwise, it maight have a error.
    # There not define trainer(the first argument is net.collect_params()), so we don't
    # train this net. We only train the content image.
    # Therefore, Neural Style's efficient is very high! Time is so short.

    # style image and content image.
    # There, we only load a pair of images. one is style image, the other is content image.
    content_img = image.imread(os.path.join(args.image_root, "tubingen.jpg"))
    style_img = image.imread(os.path.join(args.image_root, "the_scream.jpg"))
    # image.imread(): load a image, return NDArray. data format is BGR, w.t. HWC.
    # imshow