def adsh_eval(code_length): os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpu model_save_path = opt.model_save_path #model = torch.load(model_save_path) inf = pickle.load(open('./log/log-ADSH-cifar10-18-07-30-10-15-54')) V = inf['rB'] #model.eval() ''' dataset preprocessing ''' nums, dsets, labels = _dataset(10) num_database, num_test = nums dset_database, dset_test = dsets database_labels, test_labels = labels ''' testloader = DataLoader( dset_test, batch_size=1, shuffle=False, num_workers=4)''' print "here!" #qB = encode(model, testloader, num_test, num_testing, code_length) #print "query shape",qB.shape qB = V[0:1000] rB = V #计算有序性 map = calc_hr.calc_map(qB, rB, database_labels.numpy()[0:1000], database_labels.numpy()) print map
def test(model, nums, dsets, labels, code_length): model.eval() num_database, num_test = nums dset_database, dset_test = dsets database_labels, test_labels = labels retrievalloader = DataLoader(dset_database, batch_size=opt.batch_size, shuffle=False, num_workers=4) testloader = DataLoader(dset_test, batch_size=opt.batch_size, shuffle=False, num_workers=4) qB = get_codes(model, testloader, num_test, code_length, opt.sparsity) rB_sy = get_codes(model, retrievalloader, num_database, code_length, opt.sparsity) topKs = np.arange(1, 500, 50) top_ndcg = 100 Pres_sy = calc_hr.calc_topk_pres(qB, rB_sy, test_labels.numpy(), database_labels.numpy(), topKs) ndcg_sy = calc_hr.cal_ndcg_k(qB, rB_sy, test_labels.numpy(), database_labels.numpy(), top_ndcg) map_sy = calc_hr.calc_map(qB, rB_sy, test_labels.numpy(), database_labels.numpy()) top_map_sy = calc_hr.calc_topMap(qB, rB_sy, test_labels.numpy(), database_labels.numpy(), 2000) logger.info ('[Evaluation: mAP_sy: %.4f]', map_sy) logger.info ('[Evaluation: topK_mAP_sy: %.4f]', top_map_sy) logger.info ('[Evaluation: Pres_sy: %.4f]', Pres_sy[0]) print Pres_sy logger.info ('[Test Evaluation: topK_ndcg_sy: %.4f]', ndcg_sy) return qB, rB_sy, map_sy, top_map_sy, Pres_sy, ndcg_sy
def DAGH_algo(code_length, dataname): os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpu torch.manual_seed(0) torch.cuda.manual_seed(0) ''' parameter setting ''' max_iter = opt.max_iter epochs = opt.epochs batch_size = opt.batch_size learning_rate = opt.learning_rate weight_decay = 5 * 10**-4 num_anchor = opt.num_anchor lambda_1 = opt.lambda_1 lambda_2 = opt.lambda_2 lambda_3 = opt.lambda_3 record['param']['opt'] = opt record['param']['description'] = '[Comment: learning rate decay]' logger.info(opt) logger.info(code_length) logger.info(record['param']['description']) ''' dataset preprocessing ''' nums, dsets, labels = _dataset(dataname) num_database, num_test = nums dset_database, dset_test = dsets database_labels, test_labels = labels ''' model construction ''' beta = 2 model = cnn_model.CNNNet(opt.arch, code_length) model.cuda() cudnn.benchmark = True DAGH_loss = dl.DAGHLoss(lambda_1, lambda_2, lambda_3, code_length) L1_criterion = nn.L1Loss() L2_criterion = nn.MSELoss() optimizer = optim.SGD(model.parameters(), lr=learning_rate, weight_decay=weight_decay) B = np.sign(np.random.randn(code_length, num_database * 2)) model.train() for iter in range(max_iter): iter_time = time.time() trainloader = DataLoader(dset_database, batch_size=batch_size, shuffle=True, num_workers=4) F = np.zeros((num_database * 2, code_length), dtype=np.float) if iter == 0: ''' initialize the feature of all images to build dist graph ''' ini_Features = np.zeros((num_database * 2, 4096), dtype=np.float) ini_F = np.zeros((num_database * 2, 12), dtype=np.float) for iteration, (train_input, train_input2, train_label, batch_ind) in enumerate(trainloader): train_all = torch.cat((train_input, train_input2), dim=0) train_all = Variable(train_all.cuda()) output = model(train_all) batch_ind2 = batch_ind + num_database batch_all = torch.cat((batch_ind, batch_ind2), dim=0) ini_Features[batch_all, :] = output[0].cpu().data.numpy() ini_F[batch_all, :] = output[1].cpu().data.numpy() print('initialization dist graph forward done!') dist_graph = get_dist_graph(ini_Features, num_anchor) # dist_graph = np.random.rand(num_database,num_anchor) # bf = np.sign(ini_F) Z = calc_Z(dist_graph, s=3) elif (iter % 3) == 0: dist_graph = get_dist_graph(Features, num_anchor) Z = calc_Z(dist_graph, s=3) print('calculate dist graph forward done!') inv_A = inv(np.diag(Z.sum(0))) # m X m Z_T = Z.transpose() # m X n left = np.dot(B, np.dot(Z, inv_A)) # k X m if iter == 0: loss_ini = calc_loss(B, ini_F, Z, inv_A, lambda_1, lambda_2, lambda_3, code_length) # loss_ini2 = calc_all_loss(B,F,Z,inv_A,Z1,Z2,Y1,Y2,rho1,rho2,lambda_1,lambda_2) print(loss_ini) ''' learning deep neural network: feature learning ''' Features = np.zeros((num_database * 2, 4096), dtype=np.float) for epoch in range(epochs): for iteration, (train_input, train_input2, train_label, batch_ind) in enumerate(trainloader): train_all = torch.cat((train_input, train_input2), dim=0) train_all = Variable(train_all.cuda()) output = model(train_all) batch_ind2 = batch_ind + num_database batch_all = torch.cat((batch_ind, batch_ind2), dim=0) Features[batch_all, :] = output[0].cpu().data.numpy() F[batch_all, :] = output[1].cpu().data.numpy() batch_grad = get_batch_gard(B, left, Z_T, batch_all) / (1 * batch_size) batch_grad = Variable( torch.from_numpy(batch_grad).type( torch.FloatTensor).cuda()) optimizer.zero_grad() output[1].backward(batch_grad, retain_graph=True) B_cuda = Variable( torch.from_numpy(B[:, batch_all]).type( torch.FloatTensor).cuda()) # optimizer.zero_grad () other_loss = DAGH_loss(output[1].t(), B_cuda) one_vectors = Variable(torch.ones(output[1].size()).cuda()) L1_loss = L1_criterion(torch.abs(output[1]), one_vectors) # L2_loss = L2_criterion (output[1],B_cuda.t()) All_loss = other_loss + lambda_3 * L1_loss All_loss.backward() optimizer.step() if (iteration % 500) == 0: print('iteration:' + str(iteration)) #print (model.features[0].weight.data[1, 1, :, :]) #print (model.features[18].weight.data[1, 1, :, :]) #print (model.classifier[6].weight.data[:, 1]) adjusting_learning_rate(optimizer, iter) ''' learning binary codes: discrete coding ''' # bf = np.sign (F) # F = np.random.randn (num_database, 12) loss_before = calc_loss(B, F, Z, inv_A, lambda_1, lambda_2, lambda_3, code_length) B = B_step(F, Z, inv_A) iter_time = time.time() - iter_time loss_ = calc_loss(B, F, Z, inv_A, lambda_1, lambda_2, lambda_3, code_length) logger.info( '[Iteration: %3d/%3d][Train Loss: before:%.4f, after:%.4f]', iter, max_iter, loss_before, loss_) record['train loss'].append(loss_) record['iter time'].append(iter_time) ''' training procedure finishes, evaluation ''' model.eval() testloader = DataLoader(dset_test, batch_size=batch_size, shuffle=False, num_workers=4) trainloader2 = DataLoader(dset_database, batch_size=batch_size, shuffle=False, num_workers=4) qB = encode(model, testloader, num_test, code_length) rB_sy = encode(model, trainloader2, num_database, code_length) rB_asy = B[:, 0:num_database].transpose() map_sy = calc_hr.calc_map(qB, rB_sy, test_labels.numpy(), database_labels.numpy()) map_asy = calc_hr.calc_map(qB, rB_asy, test_labels.numpy(), database_labels.numpy()) top_map_sy = calc_hr.calc_topMap(qB, rB_sy, test_labels.numpy(), database_labels.numpy(), 2000) top_map_asy = calc_hr.calc_topMap(qB, rB_asy, test_labels.numpy(), database_labels.numpy(), 2000) logger.info('[Evaluation: mAP_sy: %.4f]', map_sy) logger.info('[Evaluation: topK_mAP_sy: %.4f]', top_map_sy) logger.info('[Evaluation: mAP_asy: %.4f]', map_asy) logger.info('[Evaluation: topK_mAP_asy: %.4f]', top_map_asy) record['rB_sy'] = rB_sy record['rB_asy'] = rB_asy record['qB'] = qB record['map_sy'] = map_sy record['map_asy'] = map_asy record['topK_map_sy'] = top_map_sy record['topK_map_asy'] = top_map_asy record['F'] = F filename = os.path.join(logdir, str(code_length) + 'bits-record.pkl') _save_record(record, filename)
def DAGH_algo(code_length, dataname): os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpu torch.manual_seed(0) torch.cuda.manual_seed(0) # code_length=8 ''' parameter setting ''' max_iter = opt.max_iter epochs = opt.epochs batch_size = opt.batch_size learning_rate = opt.learning_rate weight_decay = 5 * 10**-4 num_anchor = opt.num_anchor lambda_1 = float(opt.lambda_1) lambda_2 = float(opt.lambda_2) lambda_3 = float(opt.lambda_3) lambda_ba = float(opt.lambda_ba) record['param']['opt'] = opt record['param']['description'] = '[Comment: learning rate decay]' logger.info(opt) logger.info(code_length) logger.info(record['param']['description']) ''' dataset preprocessing ''' nums, dsets, labels = _dataset(dataname) num_database, num_test = nums dset_database, dset_test = dsets database_labels, test_labels = labels ''' model construction ''' model = cnn_model.CNNNet(opt.arch, code_length) model.cuda() cudnn.benchmark = True DAGH_sample_loss = dl.DAGH_sample_loss(lambda_1, lambda_2, lambda_3, code_length) L1_criterion = nn.L1Loss() optimizer = optim.SGD(model.parameters(), lr=learning_rate, weight_decay=weight_decay) B = np.sign(np.random.randn(code_length, num_database)) model.train() for iter in range(max_iter): iter_time = time.time() trainloader = DataLoader(dset_database, batch_size=batch_size, shuffle=True, num_workers=4) F = np.zeros((num_database, code_length), dtype=np.float) Features = np.zeros((num_database, 4096), dtype=np.float) # W = np.random.randn (num_database, num_database) if (iter % 3) == 0: for iteration, (train_input, train_label, batch_ind) in enumerate(trainloader): train_input = Variable(train_input.cuda(), volatile=True) output = model(train_input) Features[batch_ind, :] = output[0].cpu().data.numpy() F[batch_ind, :] = output[1].cpu().data.numpy() print('initialization dist graph forward done!') dist_graph = get_dist_graph(Features, num_anchor) Z = calc_Z(dist_graph) inv_A = inv(np.diag(Z.sum(0))) # m X m W = calc_WZ(Z, inv_A) print('calculate W done!') if iter == 0: loss_ini = calc_loss(B, F, W, lambda_1, lambda_2, lambda_3, code_length) print(loss_ini) ''' learning deep neural network: feature learning ''' for epoch in range(epochs): for iteration, (train_input, train_label, batch_ind) in enumerate(trainloader): train_input = Variable(train_input.cuda()) output = model(train_input) F[batch_ind, :] = output[1].cpu().data.numpy() optimizer.zero_grad() B_cuda = Variable( torch.from_numpy(B[:, batch_ind]).type( torch.FloatTensor).cuda()) w_batch = Variable( torch.from_numpy(W[batch_ind][:, batch_ind]).type( torch.FloatTensor).cuda()) other_loss = DAGH_sample_loss(w_batch, output[1].t(), B_cuda) one_vectors = Variable(torch.ones(output[1].size()).cuda()) L1_loss = L1_criterion(torch.abs(output[1]), one_vectors) # L2_loss = L2_criterion (output[1],B_cuda.t()) All_loss = other_loss + lambda_3 * L1_loss All_loss.backward() optimizer.step() if (iteration % 200) == 0: print('iteration:' + str(iteration)) #print (model.features[0].weight.data[1, 1, :, :]) #print (model.features[18].weight.data[1, 1, :, :]) #print (model.classifier[6].weight.data[:, 1]) adjusting_learning_rate(optimizer, iter) ''' learning binary codes: discrete coding ''' # bf = np.sign (F) # F = np.random.randn (num_database, 12) loss_before = calc_loss(B, F, W, lambda_1, lambda_2, lambda_3, code_length) B = B_step(F, W) iter_time = time.time() - iter_time loss_ = calc_loss(B, F, W, lambda_1, lambda_2, lambda_3, code_length) logger.info( '[Iteration: %3d/%3d][Train Loss: before:%.4f, after:%.4f]', iter, max_iter, loss_before, loss_) record['train loss'].append(loss_) record['iter time'].append(iter_time) ''' training procedure finishes, evaluation ''' model.eval() testloader = DataLoader(dset_test, batch_size=batch_size, shuffle=False, num_workers=4) qB = encode(model, testloader, num_test, code_length) rB = B.transpose() map = calc_hr.calc_map(qB, rB, test_labels.numpy(), database_labels.numpy()) top_map = calc_hr.calc_topMap(qB, rB, test_labels.numpy(), database_labels.numpy(), 2000) logger.info('[lambda_1: %.4f]', lambda_1) logger.info('[lambda_2: %.4f]', lambda_2) logger.info('[lambda_3: %.4f]', lambda_3) logger.info('[Evaluation: mAP: %.4f]', map) logger.info('[Evaluation: topK_mAP: %.4f]', top_map) record['rB'] = rB record['qB'] = qB record['map'] = map record['topK_map'] = top_map record['F'] = F filename = os.path.join(logdir, str(code_length) + 'bits-record.pkl') _save_record(record, filename) return top_map
def DAGH_algo(code_length, dataname): os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpu torch.manual_seed(0) torch.cuda.manual_seed(0) # code_length=8 ''' parameter setting ''' max_iter = opt.max_iter epochs = opt.epochs batch_size = opt.batch_size learning_rate = opt.learning_rate weight_decay = 5 * 10**-4 num_anchor = opt.num_anchor lambda_1 = float(opt.lambda_1) lambda_2 = float(opt.lambda_2) lambda_3 = float(opt.lambda_3) rho1 = opt.rho1 rho2 = opt.rho2 gamma = opt.gamma record['param']['opt'] = opt record['param']['description'] = '[Comment: learning rate decay]' logger.info(opt) logger.info(code_length) logger.info(record['param']['description']) ''' dataset preprocessing ''' nums, dsets, labels = load_dataset(dataname) num_database, num_test = nums dset_database, dset_test = dsets database_labels, test_labels = labels ''' model construction ''' model = cnn_model.CNNNet(opt.arch, code_length) model.cuda() cudnn.benchmark = True L2_criterion = nn.MSELoss() # optimizer = optim.SGD(model.parameters(), lr=learning_rate, weight_decay=weight_decay) optimizer = optim.SGD(model.parameters(), lr=learning_rate, weight_decay=weight_decay, momentum=0.9) #### # optimizer = optim.RMSprop (model.parameters (), lr=learning_rate, weight_decay=weight_decay) # optimizer = optim.RMSprop (model.parameters (), lr=learning_rate, weight_decay=weight_decay, momentum=0.9) # optimizer = optim.Adadelta (model.parameters (), weight_decay=weight_decay) # optimizer = optim.Adam (model.parameters ()) B = np.sign(np.random.randn(code_length, num_database)) model.train() for iter in range(max_iter): iter_time = time.time() adjusting_learning_rate(optimizer, learning_rate, False) trainloader = DataLoader(dset_database, batch_size=batch_size, shuffle=True, num_workers=4) if iter == 0: ''' initialize the feature of all images to build dist graph ''' ini_Features = np.zeros((num_database, 4096), dtype=np.float) ini_F = np.zeros((num_database, code_length), dtype=np.float) for iteration, (train_input, train_label, batch_ind) in enumerate(trainloader): train_input = Variable(train_input.cuda()) output = model(train_input) ini_Features[batch_ind, :] = output[0].cpu().data.numpy() ini_F[batch_ind, :] = output[1].cpu().data.numpy() print('initialization dist graph forward done!') dist_graph = get_dist_graph(ini_Features, num_anchor) # dist_graph = np.random.rand(num_database,num_anchor) # bf = np.sign(ini_F) Z = calc_Z(dist_graph) # scipy.io.savemat ('Z_STL10.mat', {'Z': Z}) elif (iter != 0): dist_graph = get_dist_graph(Features, num_anchor) Z = calc_Z(dist_graph) print('calculate dist graph forward done!') inv_A = inv(np.diag(Z.sum(0))) # m X m if iter == 0: loss_ini = calc_loss(B, ini_F, Z, inv_A, lambda_1, lambda_2, lambda_3, code_length) print(loss_ini) ''' learning deep neural network: feature learning ''' B = B4_step(B, Z, inv_A, lambda_1, lambda_2, rho1, rho2, gamma) for epoch in range(epochs): running_loss = 0.0 for iteration, (train_input, train_label, batch_ind) in enumerate(trainloader): train_input = Variable(train_input.cuda()) output = model(train_input) optimizer.zero_grad() B_cuda = Variable( torch.from_numpy(B[:, batch_ind]).type( torch.FloatTensor).cuda()) L2_loss = L2_criterion(output[1], B_cuda.t()) L2_loss.backward() optimizer.step() running_loss += L2_loss.data[0] if (iteration % 50) == 49: # print ('iteration:' + str (iteration)) print('[%d, %5d] loss: %.5f' % (epoch + 1, iteration + 1, running_loss / 50)) running_loss = 0.0 if epoch == (epochs / 2) - 1: adjusting_learning_rate(optimizer, learning_rate) trainloader2 = DataLoader(dset_database, batch_size=batch_size, shuffle=False, num_workers=4) F = get_F(model, trainloader2, num_database, code_length) Features = get_fearture(model, trainloader2, num_database, code_length) ''' learning binary codes: discrete coding ''' iter_time = time.time() - iter_time loss_ = calc_loss(B, F, Z, inv_A, lambda_1, lambda_2, lambda_3, code_length) logger.info('[Iteration: %3d/%3d][Train Loss:%.4f]', iter, max_iter, loss_) record['train loss'].append(loss_) record['iter time'].append(iter_time) ''' training procedure finishes, evaluation ''' model.eval() retrievalloader = DataLoader(dset_database, batch_size=batch_size, shuffle=False, num_workers=4) testloader = DataLoader(dset_test, batch_size=batch_size, shuffle=False, num_workers=4) qB = encode(model, testloader, num_test, code_length) rB_sy = encode(model, retrievalloader, num_database, code_length) rB_asy = B.transpose() topKs = np.arange(1, 500, 50) top_ndcg = 100 # map = calc_hr.calc_map (qB, rB, test_labels.numpy (), database_labels.numpy ()) # top_map = calc_hr.calc_topMap (qB, rB, test_labels.numpy (), database_labels.numpy (), 2000) Pres_asy = calc_hr.calc_topk_pres(qB, rB_asy, test_labels.numpy(), database_labels.numpy(), topKs) ndcg_asy = calc_hr.cal_ndcg_k(qB, rB_asy, test_labels.numpy(), database_labels.numpy(), top_ndcg) Pres_sy = calc_hr.calc_topk_pres(qB, rB_sy, test_labels.numpy(), database_labels.numpy(), topKs) ndcg_sy = calc_hr.cal_ndcg_k(qB, rB_sy, test_labels.numpy(), database_labels.numpy(), top_ndcg) map_sy = calc_hr.calc_map(qB, rB_sy, test_labels.numpy(), database_labels.numpy()) map_asy = calc_hr.calc_map(qB, rB_asy, test_labels.numpy(), database_labels.numpy()) top_map_sy = calc_hr.calc_topMap(qB, rB_sy, test_labels.numpy(), database_labels.numpy(), 2000) top_map_asy = calc_hr.calc_topMap(qB, rB_asy, test_labels.numpy(), database_labels.numpy(), 2000) logger.info('[lambda_1: %.4f]', lambda_1) logger.info('[lambda_2: %.4f]', lambda_2) logger.info('[lambda_3: %.4f]', lambda_3) logger.info('[Evaluation: mAP_sy: %.4f]', map_sy) logger.info('[Evaluation: mAP_asy: %.4f]', map_asy) logger.info('[Evaluation: topK_mAP_sy: %.4f]', top_map_sy) logger.info('[Evaluation: topK_mAP_asy: %.4f]', top_map_asy) logger.info('[Evaluation: Pres_sy: %.4f]', Pres_sy[0]) logger.info('[Evaluation: Pres_asy: %.4f]', Pres_asy[0]) logger.info('[Evaluation: topK_ndcg_sy: %.4f]', ndcg_sy) logger.info('[Evaluation: topK_ndcg_asy: %.4f]', ndcg_asy) record['rB_sy'] = rB_sy record['rB_asy'] = rB_asy record['qB'] = qB record['map_sy'] = map_sy record['map_asy'] = map_asy record['topK_map_sy'] = top_map_sy record['topK_map_asy'] = top_map_asy record['topK_ndcg_sy'] = ndcg_sy record['topK_ndcg_asy'] = ndcg_asy record['Pres_sy'] = Pres_sy record['Pres_asy'] = Pres_asy record['F'] = F filename = os.path.join(logdir, str(code_length) + 'bits-record.pkl') _save_record(record, filename) return top_map_sy
def DAGH_algo(code_length, dataname): os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpu torch.manual_seed(0) torch.cuda.manual_seed(0) # code_length=8 ''' parameter setting ''' max_iter = opt.max_iter epochs = opt.epochs batch_size = opt.batch_size learning_rate = opt.learning_rate weight_decay = 5 * 10**-4 num_anchor = opt.num_anchor lambda_1 = float(opt.lambda_1) lambda_2 = float(opt.lambda_2) lambda_3 = float(opt.lambda_3) rho1 = opt.rho1 rho2 = opt.rho2 beta1 = float(opt.beta_1) beta2 = float(opt.beta_2) gamma = opt.gamma record['param']['opt'] = opt record['param']['description'] = '[Comment: learning rate decay]' logger.info(opt) logger.info(code_length) logger.info(record['param']['description']) ''' dataset preprocessing ''' nums, dsets, labels = load_dataset(dataname) num_database, num_test = nums dset_database, dset_test = dsets database_labels, test_labels = labels ''' model construction ''' model = cnn_model.CNNNet(opt.arch, code_length) model.cuda() cudnn.benchmark = True DAGH_loss = dl.DAGHLoss(lambda_1, lambda_2, lambda_3, code_length) L1_criterion = nn.L1Loss() # optimizer = optim.SGD(model.parameters(), lr=learning_rate, weight_decay=weight_decay) # optimizer = optim.SGD (model.parameters (), lr=learning_rate, weight_decay=weight_decay, momentum=0.9) #### # optimizer = optim.RMSprop (model.parameters (), lr=learning_rate, weight_decay=weight_decay) # optimizer = optim.RMSprop (model.parameters (), lr=learning_rate, weight_decay=weight_decay, momentum=0.9) # optimizer = optim.Adadelta (model.parameters (), weight_decay=weight_decay) # optimizer = optim.Adam (model.parameters ()) hash_params = list(map(id, model.hash.parameters())) base_params = filter(lambda p: id(p) not in hash_params, model.parameters()) params = [ { "params": model.hash.parameters(), "lr": learning_rate * 10 }, { "params": base_params, "lr": learning_rate }, ] optimizer = optim.SGD(params, weight_decay=weight_decay, momentum=0.9) B = np.sign(np.random.randn(code_length, num_database)) model.train() for iter in range(max_iter): iter_time = time.time() trainloader = DataLoader(dset_database, batch_size=batch_size, shuffle=True, num_workers=4) if iter == 0: ''' initialize the feature of all images to build dist graph ''' ini_Features = np.zeros((num_database, 4096), dtype=np.float) ini_F = np.zeros((num_database, code_length), dtype=np.float) for iteration, (train_input, train_label, batch_ind) in enumerate(trainloader): train_input = Variable(train_input.cuda()) output = model(train_input) ini_Features[batch_ind, :] = output[0].cpu().data.numpy() ini_F[batch_ind, :] = output[1].cpu().data.numpy() print('initialization dist graph forward done!') s1_time = time.time() dist_graph = get_dist_graph(ini_Features, num_anchor) # dist_graph = np.random.rand(num_database,num_anchor) # bf = np.sign(ini_F) Z = calc_Z(dist_graph) S_time = time.time() - s1_time # B = np.sign (ini_F.transpose ()) # elif ((iter % 3) == 0) | (iter == max_iter - 1): else: s1_time = time.time() dist_graph = get_dist_graph(Features, num_anchor) Z = calc_Z(dist_graph) print('reset dist graph!') S_time = time.time() - s1_time inv_A = inv(np.diag(Z.sum(0))) # m X m Z_T = Z.transpose() # m X n left = np.dot(B, np.dot(Z, inv_A)) # k X m if iter == 0: loss_ini = calc_loss(B, ini_F, Z, inv_A, lambda_1, lambda_2, lambda_3, code_length) print('ini_loss' + str(loss_ini)) ''' learning deep neural network: feature learning ''' # B = B4_step (B, ini_F, Z, inv_A, lambda_1, lambda_2, rho1, rho2, gamma) n1_time = time.time() for epoch in range(epochs): F = np.zeros((num_database, code_length), dtype=np.float) running_loss = 0.0 adjusting_learning_rate2(optimizer, iter, epoch) print(optimizer.param_groups[0]['lr']) print(optimizer.param_groups[1]['lr']) for iteration, (train_input, train_label, batch_ind) in enumerate(trainloader): train_input = Variable(train_input.cuda()) output = model(train_input) F[batch_ind, :] = output[1].cpu().data.numpy() batch_grad = get_batch_gard( B, left, Z_T, batch_ind) / (code_length * batch_size) batch_grad = Variable( torch.from_numpy(batch_grad).type( torch.FloatTensor).cuda()) optimizer.zero_grad() output[1].backward(batch_grad, retain_graph=True) # output[1].backward (batch_grad) B_cuda = Variable( torch.from_numpy(B[:, batch_ind]).type( torch.FloatTensor).cuda()) other_loss = DAGH_loss(output[1].t(), B_cuda) one_vectors = Variable(torch.ones(output[1].size()).cuda()) L1_loss = L1_criterion(torch.abs(output[1]), one_vectors) # L2_loss = L2_criterion (output[1],B_cuda.t()) All_loss = other_loss + lambda_3 * L1_loss / code_length All_loss.backward() optimizer.step() running_loss += All_loss.data[0] if (iteration % 50) == 49: # print ('iteration:' + str (iteration)) print('[%d, %5d] loss: %.5f' % (epoch + 1, iteration + 1, running_loss / 50)) running_loss = 0.0 Tr_BLF = cal_BLF(B, Z, inv_A, F) print('[%d] Tr_BLF loss: %.5f' % (epoch + 1, Tr_BLF)) # adjusting_learning_rate (optimizer, iter) n_time = time.time() - n1_time trainloader2 = DataLoader(dset_database, batch_size=batch_size, shuffle=False, num_workers=4) F = get_F(model, trainloader2, num_database, code_length) Features = get_fearture(model, trainloader2, num_database, code_length) ''' learning binary codes: discrete coding ''' B_new = np.sign(F).transpose() B_random = np.sign(np.random.randn(code_length, num_database)) # F = np.random.randn (num_datasbase, 12) loss_before = calc_loss(B, F, Z, inv_A, lambda_1, lambda_2, lambda_3, code_length) # B = B_step (F, Z, inv_A) b1_time = time.time() B = B4_step(B_random, np.sign(F), Z, inv_A, beta1, beta2, rho1, rho2, gamma) b_time = time.time() - b1_time iter_time = time.time() - iter_time loss_ = calc_loss(B, F, Z, inv_A, lambda_1, lambda_2, lambda_3, code_length) # loss_ = calc_R1_loss (B, F, Z, inv_A, lambda_1, lambda_2, lambda_3, beta1, beta2, code_length) logger.info( '[Iteration: %3d/%3d][Train Loss: before:%.4f, after:%.4f]', iter, max_iter, loss_before, loss_) record['train loss'].append(loss_) record['iter time'].append(iter_time) print(S_time) record['stime'].append(S_time) record['btime'].append(b_time) record['ntime'].append(n_time) ''' training procedure finishes, evaluation ''' model.eval() retrievalloader = DataLoader(dset_database, batch_size=batch_size, shuffle=False, num_workers=4) testloader = DataLoader(dset_test, batch_size=batch_size, shuffle=False, num_workers=4) qB = encode(model, testloader, num_test, code_length) rB_sy = encode(model, retrievalloader, num_database, code_length) rB_asy = B.transpose() topKs = np.arange(1, 500, 50) top_ndcg = 100 # map = calc_hr.calc_map (qB, rB, test_labels.numpy (), database_labels.numpy ()) # top_map = calc_hr.calc_topMap (qB, rB, test_labels.numpy (), database_labels.numpy (), 2000) Pres_asy = calc_hr.calc_topk_pres(qB, rB_asy, test_labels.numpy(), database_labels.numpy(), topKs) ndcg_asy = calc_hr.cal_ndcg_k(qB, rB_asy, test_labels.numpy(), database_labels.numpy(), top_ndcg) Pres_sy = calc_hr.calc_topk_pres(qB, rB_sy, test_labels.numpy(), database_labels.numpy(), topKs) ndcg_sy = calc_hr.cal_ndcg_k(qB, rB_sy, test_labels.numpy(), database_labels.numpy(), top_ndcg) map_sy = calc_hr.calc_map(qB, rB_sy, test_labels.numpy(), database_labels.numpy()) map_asy = calc_hr.calc_map(qB, rB_asy, test_labels.numpy(), database_labels.numpy()) top_map_sy = calc_hr.calc_topMap(qB, rB_sy, test_labels.numpy(), database_labels.numpy(), 2000) top_map_asy = calc_hr.calc_topMap(qB, rB_asy, test_labels.numpy(), database_labels.numpy(), 2000) logger.info('[lambda_1: %.4f]', lambda_1) logger.info('[lambda_2: %.4f]', lambda_2) logger.info('[lambda_3: %.4f]', lambda_3) logger.info('[Evaluation: mAP_sy: %.4f]', map_sy) logger.info('[Evaluation: mAP_asy: %.4f]', map_asy) logger.info('[Evaluation: topK_mAP_sy: %.4f]', top_map_sy) logger.info('[Evaluation: topK_mAP_asy: %.4f]', top_map_asy) logger.info('[Evaluation: Pres_sy: %.4f]', Pres_sy[0]) print Pres_sy logger.info('[Evaluation: Pres_asy: %.4f]', Pres_asy[0]) print Pres_asy logger.info('[Evaluation: topK_ndcg_sy: %.4f]', ndcg_sy) logger.info('[Evaluation: topK_ndcg_asy: %.4f]', ndcg_asy) record['rB_sy'] = rB_sy record['rB_asy'] = rB_asy record['qB'] = qB record['map_sy'] = map_sy record['map_asy'] = map_asy record['topK_map_sy'] = top_map_sy record['topK_map_asy'] = top_map_asy record['topK_ndcg_sy'] = ndcg_sy record['topK_ndcg_asy'] = ndcg_asy record['Pres_sy'] = Pres_sy record['Pres_asy'] = Pres_asy record['F'] = F filename = os.path.join(logdir, str(code_length) + 'bits-record.pkl') _save_record(record, filename) return top_map_sy
def SRLH_algo(code_length, dataname): os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpu torch.manual_seed(0) torch.cuda.manual_seed(0) # code_length=8 ''' parameter setting ''' epochs = opt.epochs batch_size = opt.batch_size learning_rate = opt.learning_rate weight_decay = 5 * 10**-4 num_samples = opt.num_samples sparsity = opt.sparsity gamma = opt.gamma EPSILON = opt.EPSILON eps = np.finfo(np.float32).eps.item() record['param']['opt'] = opt record['param']['description'] = '[Comment: learning rate decay]' logger.info(opt) logger.info(code_length) logger.info(record['param']['description']) ''' dataset preprocessing ''' nums, dsets, labels = load_dataset(dataname, False, 20) num_database, num_test = nums dset_database, dset_test = dsets database_labels, test_labels = labels ''' model construction ''' model = PolicyNet.Policy_net(opt.arch, code_length) model.cuda() cudnn.benchmark = True # optimizer = optim.SGD(model.parameters(), lr=learning_rate, weight_decay=weight_decay) # optimizer = optim.SGD (model.parameters (), lr=learning_rate, weight_decay=weight_decay, momentum=0.9) #### # optimizer = optim.RMSprop (model.parameters (), lr=learning_rate, weight_decay=weight_decay) # optimizer = optim.RMSprop (model.parameters (), lr=learning_rate, weight_decay=weight_decay, momentum=0.9) # optimizer = optim.Adadelta (model.parameters (), weight_decay=weight_decay) # optimizer = optim.Adam (model.parameters ()) hash_params = list(map(id, model.hash.parameters())) base_params = filter(lambda p: id(p) not in hash_params, model.parameters()) params = [ { "params": model.hash.parameters(), "lr": learning_rate * 10 }, { "params": base_params, "lr": learning_rate }, ] optimizer = optim.SGD(params, weight_decay=weight_decay, momentum=0.9) #optimizer = optim.Adam (params) evalmodel_PATH = '/data/dacheng/SRLH/model.pkl' torch.save(model, evalmodel_PATH) ''' learning deep neural network: feature learning ''' trainloader = DataLoader(dset_database, batch_size=batch_size, shuffle=True, num_workers=4) agent = Agent.Agent(eps, EPSILON) model.train() for epoch in range(epochs): iter_time = time.time() eval_model = torch.load(evalmodel_PATH) eval_index = get_index(eval_model, trainloader, num_database, code_length) # adjusting_learning_rate2 (optimizer, iter, epoch) print(optimizer.param_groups[0]['lr']) print(optimizer.param_groups[1]['lr']) running_loss = 0 for iteration, (train_input, train_label, batch_ind) in enumerate(trainloader): train_input = Variable(train_input.cuda()) z_scores = model(train_input) #print train_label.shape optimizer.zero_grad() for batch_i in range(len(batch_ind)): per_env = Environment.Env(z_scores[batch_i, :], train_label[batch_i, :], eval_index, sparsity, num_samples, database_labels, code_length) # running_loss = 0 actions, log_probs, rewards = agent.sample_episode(per_env) # print log_probs # print rewards policy_loss = agent.cal_loss(log_probs, rewards, gamma) policy_loss = torch.cat(policy_loss).sum() / batch_size running_loss += policy_loss.data[0] policy_loss.backward(retain_graph=True) optimizer.step() # print('[%d, %5d] loss: %.5f' % (epoch + 1, iteration + 1, running_loss )) if (iteration % 50) == 49: # print ('iteration:' + str (iteration)) print('[%d, %5d] loss: %.5f' % (epoch + 1, iteration + 1, running_loss / 50)) running_loss = 0.0 torch.save(model, evalmodel_PATH) # adjusting_learning_rate (optimizer, iter) iter_time = time.time() - iter_time # logger.info ('[Iteration: %3d/%3d][Train Loss: before:%.4f, after:%.4f]', iter, max_iter, loss_before, loss_) # record['train loss'].append (loss_) record['iter time'].append(iter_time) ''' training procedure finishes, evaluation ''' model.eval() retrievalloader = DataLoader(dset_database, batch_size=batch_size, shuffle=False, num_workers=4) testloader = DataLoader(dset_test, batch_size=batch_size, shuffle=False, num_workers=4) qB = get_codes(model, testloader, num_test, code_length, sparsity) rB_sy = get_codes(model, retrievalloader, num_database, code_length, sparsity) topKs = np.arange(1, 500, 50) top_ndcg = 100 # Pres_asy = calc_hr.calc_topk_pres (qB, rB_asy, test_labels.numpy (), database_labels.numpy (), topKs) # ndcg_asy = calc_hr.cal_ndcg_k (qB, rB_asy, test_labels.numpy (), database_labels.numpy (), top_ndcg) Pres_sy = calc_hr.calc_topk_pres(qB, rB_sy, test_labels.numpy(), database_labels.numpy(), topKs) ndcg_sy = calc_hr.cal_ndcg_k(qB, rB_sy, test_labels.numpy(), database_labels.numpy(), top_ndcg) map_sy = calc_hr.calc_map(qB, rB_sy, test_labels.numpy(), database_labels.numpy()) # map_asy = calc_hr.calc_map (qB, rB_asy, test_labels.numpy (), database_labels.numpy ()) top_map_sy = calc_hr.calc_topMap(qB, rB_sy, test_labels.numpy(), database_labels.numpy(), 2000) # top_map_asy = calc_hr.calc_topMap (qB, rB_asy, test_labels.numpy (), database_labels.numpy (), 2000) logger.info('[Evaluation: mAP_sy: %.4f]', map_sy) # logger.info ('[Evaluation: mAP_asy: %.4f]', map_asy) logger.info('[Evaluation: topK_mAP_sy: %.4f]', top_map_sy) # logger.info ('[Evaluation: topK_mAP_asy: %.4f]', top_map_asy) logger.info('[Evaluation: Pres_sy: %.4f]', Pres_sy[0]) print Pres_sy # logger.info ('[Evaluation: Pres_asy: %.4f]', Pres_asy[0]) # print Pres_asy logger.info('[Evaluation: topK_ndcg_sy: %.4f]', ndcg_sy) # logger.info ('[Evaluation: topK_ndcg_asy: %.4f]', ndcg_asy) record['rB_sy'] = rB_sy record['qB'] = qB record['map_sy'] = map_sy # record['map_asy'] = map_asy record['topK_map_sy'] = top_map_sy # record['topK_map_asy'] = top_map_asy record['topK_ndcg_sy'] = ndcg_sy # record['topK_ndcg_asy'] = ndcg_asy record['Pres_sy'] = Pres_sy # record['Pres_asy'] = Pres_asy filename = os.path.join(logdir, str(code_length) + 'bits-record.pkl') _save_record(record, filename) return top_map_sy
def DCDH_algo(code_length): os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpu torch.manual_seed(0) torch.cuda.manual_seed(0) ''' parameter setting ''' max_iter = opt.max_iter epochs = opt.epochs batch_size = opt.batch_size learning_rate = opt.learning_rate weight_decay = 5 * 10**-4 num_samples = opt.num_samples gamma = opt.gamma record['param']['topk'] = 5000 record['param']['opt'] = opt record['param']['description'] = '[Comment: learning rate decay]' logger.info(opt) logger.info(code_length) logger.info(record['param']['description']) ''' dataset preprocessing ''' nums, dsets, labels = _dataset() num_database, num_test = nums dset_database, dset_test = dsets database_labels, test_labels = labels ''' model construction ''' model = cnn_model.CNNNet(opt.arch, code_length) model.cuda() DCDH_loss = al.DCDHLoss(gamma, code_length, num_database) optimizer = optim.SGD(model.parameters(), lr=learning_rate, weight_decay=weight_decay) labelNet = cnn_model.MLP(code_length, 38) labelNet.cuda() label_loss = al.DCDHLoss(gamma, code_length, num_database) optimizer2 = optim.SGD(model.parameters(), lr=learning_rate, weight_decay=weight_decay) product_model = cnn_model.ConcatMLP(code_length, 38) product_model.cuda() product_loss = al.ProductLoss(gamma, code_length, num_samples) optimizer3 = optim.Adam(product_model.parameters(), lr=learning_rate, weight_decay=weight_decay) V = np.zeros((num_database, code_length)) model.train() labelNet.train() product_model.train() for iter in range(max_iter): iter_time = time.time() ''' sampling and construct similarity matrix ''' select_index = list(np.random.permutation( range(num_database)))[0:num_samples] _sampler = subsetsampler.SubsetSampler(select_index) trainloader = DataLoader(dset_database, batch_size=batch_size, sampler=_sampler, shuffle=False, num_workers=4) ''' learning deep neural network: feature learning ''' sample_label = database_labels.index_select( 0, torch.from_numpy(np.array(select_index))) Sim = calc_sim(sample_label) U = np.zeros((num_samples, code_length), dtype=np.float) L = np.zeros((num_samples, code_length), dtype=np.float) I = np.zeros((num_samples, code_length), dtype=np.float) for epoch in range(epochs): for iteration, (train_input, train_label, batch_ind) in enumerate(trainloader): batch_size_ = train_label.size(0) u_ind = np.linspace(iteration * batch_size, np.min((num_samples, (iteration + 1) * batch_size)) - 1, batch_size_, dtype=int) train_input = Variable(train_input.cuda()) output = model(train_input) outputL = labelNet(train_label.type(torch.FloatTensor).cuda()) S = calc_sim(train_label) U[u_ind, :] = output.cpu().data.numpy() L[u_ind, :] = outputL.cpu().data.numpy() # semanCode = outputL.clone().detach().requires_grad_(True) imgCode = output.clone().detach().requires_grad_(True) product = torch.einsum('bi,bj->bij', semanCode, imgCode) product = product.reshape(batch_size_, code_length * code_length) hashcode, classify = product_model(product.cuda()) I[u_ind, :] = hashcode.cpu().data.numpy() model.zero_grad() labelNet.zero_grad() product_model.zero_grad() loss3 = product_loss(hashcode, V, S, V[batch_ind.cpu().numpy(), :], classify, train_label, imgCode, semanCode) loss2 = label_loss(output, V, S, V[batch_ind.cpu().numpy(), :]) loss = DCDH_loss(output, V, S, V[batch_ind.cpu().numpy(), :] ) + opt.lamda * loss2 + opt.mu * loss3 loss.backward() optimizer.step() optimizer2.step() optimizer3.step() adjusting_learning_rate(optimizer, iter) ''' learning binary codes: discrete coding ''' Q = -2 * code_length * Sim.cpu().numpy().transpose().dot( U) - 2 * gamma * U for k in range(code_length): sel_ind = np.setdiff1d([ii for ii in range(code_length)], k) V_ = V[:, sel_ind] V_ = V_[select_index, :] Uk = U[:, k] U_ = U[:, sel_ind] V[select_index, k] = -np.sign((Q[:, k] + 2 * V_.dot(U_.transpose().dot(Uk))) + opt.lamda * L[:, k] + opt.mu * I[:, k]) iter_time = time.time() - iter_time loss_ = calc_loss(V, U, Sim.cpu().numpy(), code_length, select_index, gamma) logger.info('[Iteration: %3d/%3d][Train Loss: %.4f]', iter, max_iter, loss_) record['train loss'].append(loss_) record['iter time'].append(iter_time) ''' training procedure finishes, evaluation ''' if iter % 10 == 9: model.eval() testloader = DataLoader(dset_test, batch_size=1, shuffle=False, num_workers=4) qB = encode(model, testloader, num_test, code_length) rB = V map = calc_hr.calc_map(qB, rB, test_labels.numpy(), database_labels.numpy()) topkmap = calc_hr.calc_topMap(qB, rB, test_labels.numpy(), database_labels.numpy(), record['param']['topk']) logger.info('[Evaluation: mAP: %.4f, top-%d mAP: %.4f]', map, record['param']['topk'], topkmap) record['rB'] = rB record['qB'] = qB record['map'] = map record['topkmap'] = topkmap filename = os.path.join(logdir, str(code_length) + 'bits-record.pkl') _save_record(record, filename)
def DAGH_algo(code_length): os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpu torch.manual_seed(0) torch.cuda.manual_seed(0) ''' parameter setting ''' max_iter = opt.max_iter epochs = opt.epochs batch_size = opt.batch_size learning_rate = opt.learning_rate weight_decay = 5 * 10**-4 num_anchor = opt.num_anchor lambda_1 = opt.lambda_1 lambda_2 = opt.lambda_2 record['param']['opt'] = opt record['param']['description'] = '[Comment: learning rate decay]' logger.info(opt) logger.info(code_length) logger.info(record['param']['description']) ''' dataset preprocessing ''' nums, dsets, labels = _dataset() num_database, num_test = nums dset_database, dset_test = dsets database_labels, test_labels = labels ''' model construction ''' model = cnn_model.CNNNet(opt.arch, code_length) model.cuda() # DAGH_loss = dl.DAGHLoss (lambda_1, lambda_2, code_length) optimizer = optim.SGD(model.parameters(), lr=learning_rate, weight_decay=weight_decay) B = np.sign(np.random.randn(code_length, num_database)) model.train() for iter in range(max_iter): iter_time = time.time() trainloader = DataLoader( dset_database, batch_size=batch_size, shuffle=True, ) F = np.zeros((num_database, code_length), dtype=np.float) if iter == 0: ''' initialize the feature of all images to build dist graph ''' ini_Features = np.zeros((num_database, 4096), dtype=np.float) ini_F = np.zeros((num_database, 12), dtype=np.float) for iteration, (train_input, train_label, batch_ind) in enumerate(trainloader): train_input = Variable(train_input.cuda()) output = model(train_input) ini_Features[batch_ind, :] = output[0].cpu().data.numpy() ini_F[batch_ind, :] = output[1].cpu().data.numpy() print('initialization dist graph forward done!') P, Q = calc_W(ini_Features) bf = np.sign(ini_F) elif (iter % 3) == 0: P, Q = calc_W(Features) print('calculate dist graph forward done!') if iter == 0: loss_ini = calc_loss(B, ini_F, P, Q, lambda_1, lambda_2, code_length) # #loss_ini2 = calc_all_loss(B,F,Z,inv_A,Z1,Z2,Y1,Y2,rho1,rho2,lambda_1,lambda_2) print(loss_ini) ''' learning deep neural network: feature learning ''' Features = np.zeros((num_database, 4096), dtype=np.float) for epoch in range(epochs): for iteration, (train_input, train_label, batch_ind) in enumerate(trainloader): train_input = Variable(train_input.cuda()) output = model(train_input) Features[batch_ind, :] = output[0].cpu().data.numpy() F[batch_ind, :] = output[1].cpu().data.numpy() batch_grad = get_batch_gard(B, P, Q, batch_ind) / (0.5 * batch_size) batch_grad = Variable( torch.from_numpy(batch_grad).type( torch.FloatTensor).cuda()) optimizer.zero_grad() output[1].backward(batch_grad, retain_graph=True) B_cuda = Variable( torch.from_numpy(B[:, batch_ind]).type( torch.FloatTensor).cuda()) # optimizer.zero_grad () loss2 = DAGH_loss(output[1].t(), B_cuda) #loss = criterion (output[1],B_cuda.t()) loss2.backward() optimizer.step() if (iteration % 200) == 0: print('iteration:' + str(iteration)) #print (model.features[0].weight.data[1, 1, :, :]) #print (model.features[18].weight.data[1, 1, :, :]) #print (model.classifier[6].weight.data[:, 1]) adjusting_learning_rate(optimizer, iter) ''' learning binary codes: discrete coding ''' # bf = np.sign (F) # F = np.random.rand (num_database, 12) loss_before = calc_loss(B, F, P, Q, lambda_1, lambda_2, code_length) B = B_step(F, P, Q) iter_time = time.time() - iter_time loss_ = calc_loss(B, F, P, Q, lambda_1, lambda_2, code_length) logger.info( '[Iteration: %3d/%3d][Train Loss: before:%.4f, after:%.4f]', iter, max_iter, loss_before, loss_) record['train loss'].append(loss_) record['iter time'].append(iter_time) ''' training procedure finishes, evaluation ''' model.eval() testloader = DataLoader(dset_test, batch_size=batch_size, shuffle=False, num_workers=4) qB = encode(model, testloader, num_test, code_length) rB = B.transpose() map = calc_hr.calc_map(qB, rB, test_labels.numpy(), database_labels.numpy()) top_map = calc_hr.calc_topMap(qB, rB, test_labels.numpy(), database_labels.numpy(), 2000) logger.info('[Evaluation: mAP: %.4f]', map) logger.info('[Evaluation: topK_mAP: %.4f]', top_map) record['rB'] = rB record['qB'] = qB record['map'] = map record['topK_map'] = top_map filename = os.path.join(logdir, str(code_length) + 'bits-record.pkl') _save_record(record, filename)
def adsh_algo(code_length): os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpu torch.manual_seed(0) torch.cuda.manual_seed(0) ''' parameter setting ''' max_iter = opt.max_iter epochs = opt.epochs batch_size = opt.batch_size learning_rate = opt.learning_rate weight_decay = 5 * 10**-4 num_samples = opt.num_samples alpha = opt.alpha gamma = opt.gamma lamda = opt.lamda model_save_path = opt.model_save_path record['param']['opt'] = opt record['param']['description'] = '[Comment: learning rate decay]' logger.info(opt) logger.info(code_length) logger.info(record['param']['description']) ''' dataset preprocessing ''' nums, dsets, labels = _dataset() num_database, num_test = nums dset_database, dset_test = dsets database_labels, test_labels = labels ''' model construction ''' model = cnn_model.CNNNet(opt.arch, code_length) model.cuda() adsh_loss = al.ADSHLoss(alpha, gamma, lamda, code_length, num_database) optimizer = optim.SGD(model.parameters(), lr=learning_rate, weight_decay=weight_decay) V = np.zeros((num_database, code_length)) model.train() for iter in range(max_iter): iter_time = time.time() ''' sampling and construct similarity matrix ''' select_index = list(np.random.permutation( range(num_database)))[0:num_samples] _sampler = subsetsampler.SubsetSampler(select_index) trainloader = DataLoader(dset_database, batch_size=batch_size, sampler=_sampler, shuffle=False, num_workers=4) ''' learning deep neural network: feature learning ''' sample_label = database_labels.index_select( 0, torch.from_numpy(np.array(select_index))) Sim = calc_sim(sample_label, database_labels) U = np.zeros((num_samples, code_length), dtype=np.float) for epoch in range(epochs): for iteration, (train_input, train_label, batch_ind) in enumerate(trainloader): batch_size_ = train_label.size(0) u_ind = np.linspace(iteration * batch_size, np.min((num_samples, (iteration + 1) * batch_size)) - 1, batch_size_, dtype=int) #是一个batch_size train_input = Variable(train_input.cuda()) output = model(train_input) S = Sim.index_select(0, torch.from_numpy(u_ind)) S_query = calc_sim(sample_label[u_ind, :], sample_label[u_ind, :]) U[u_ind, :] = output.cpu().data.numpy() model.zero_grad() loss = adsh_loss(output, V, S, S_query, V[batch_ind.cpu().numpy(), :], 1) loss.backward() optimizer.step() #print optimizer.state_dict() adjusting_learning_rate(optimizer, iter) ''' learning binary codes: discrete coding ''' barU = np.zeros((num_database, code_length)) barU[select_index, :] = U Q = -2 * code_length * Sim.cpu().numpy().transpose().dot( U) - 2 * gamma * barU for k in range(code_length): sel_ind = np.setdiff1d([ii for ii in range(code_length)], k) V_ = V[:, sel_ind] Uk = U[:, k] U_ = U[:, sel_ind] V[:, k] = -np.sign(Q[:, k] + 2 * V_.dot(U_.transpose().dot(Uk))) iter_time = time.time() - iter_time S_query = calc_sim(sample_label, sample_label) square_loss_, quanty_loss_, triplet_loss_, loss_ = calc_loss( V, U, Sim.cpu().numpy(), code_length, select_index, gamma, lamda) logger.info( '[Iteration: %3d/%3d][square Loss: %.4f][quanty Loss: %.4f][triplet Loss: %.4f][train Loss: %.4f]', iter, max_iter, square_loss_, quanty_loss_, triplet_loss_, loss_) record['train loss'].append(loss_) record['iter time'].append(iter_time) ''' training procedure finishes, evaluation ''' torch.save(model, model_save_path) print "model saved!" model.eval() testloader = DataLoader(dset_test, batch_size=1, shuffle=False, num_workers=4) qB = encode(model, testloader, num_test, code_length) rB = V #计算有序性 map = calc_hr.calc_map(qB, rB, test_labels.numpy(), database_labels.numpy()) logger.info('[Evaluation: mAP: %.4f]', map) record['rB'] = rB record['qB'] = qB record['map'] = map filename = os.path.join(logdir, str(code_length) + 'bits-record.pkl') _save_record(record, filename)
dataname = 'SUN20' nums, dsets, labels = load_dataset(dataname) num_database, num_test = nums dset_database, dset_test = dsets database_labels, test_labels = labels topKs = np.arange(1, 500, 50) top_ndcg = 100 Pres_sy = calc_hr.calc_topk_pres(qB, rB_sy, test_labels.numpy(), database_labels.numpy(), topKs) ndcg_sy = calc_hr.cal_ndcg_k(qB, rB_sy, test_labels.numpy(), database_labels.numpy(), top_ndcg) map_sy = calc_hr.calc_map(qB, rB_sy, test_labels.numpy(), database_labels.numpy()) top_map_sy = calc_hr.calc_topMap(qB, rB_sy, test_labels.numpy(), database_labels.numpy(), 2000) scipy.io.savemat( 'SUN_64_data5.mat', { 'qb': inf['qB'], 'b_sy': inf['rB_sy'], 'ind_qb': inf['ind_qb'], 'ind_rb': inf['ind_rb'] }) # #scipy.io.savemat('Z_mean_B.mat',{'b_asy':inf['rB_asy'], 'b_sy':inf['rB_sy']}) dataname = 'CIFAR100' rootpath = os.path.join('/data/dacheng/Datasets/', dataname)
dsets = (dset_database, dset_test) nums = (num_database, num_test) labels = (databaselabels, testlabels) return nums, dsets, labels filename = '/home/dacheng/PycharmProjects/ADSH_pytorch/log/noInk-MirFlickr-18-06-05-21-03-36//8bits-record.pkl' #/home/dacheng/PycharmProjects/ADSH_pytorch/log/log-DAGH-FF-cifar10-18-05-07-11-12-49/ inf = pickle.load(open(filename)) qB = inf['qB'] rB = inf['rB'] dataname = 'MirFlickr' topKs = np.arange(1, 500, 50) top_ndcg = 100 nums, dsets, labels = _dataset(dataname) database_labels, test_labels = labels ndcg = calc_hr.cal_ndcg_k(qB, rB, test_labels.numpy(), database_labels.numpy(), top_ndcg) map = calc_hr.calc_map(qB, rB, test_labels.numpy(), database_labels.numpy()) top_map = calc_hr.calc_topMap(qB, rB, test_labels.numpy(), database_labels.numpy(), 2000) Pres = calc_hr.calc_topk_pres(qB, rB, test_labels.numpy(), database_labels.numpy(), topKs) print(map) print(top_map)
def dchuc_algo(code_length): os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpu torch.manual_seed(0) torch.cuda.manual_seed(0) ''' parameter setting ''' max_iter = opt.max_iter epochs = opt.epochs batch_size = opt.batch_size learning_rate = opt.learning_rate learning_rate_txt = opt.learning_rate_txt weight_decay = 5 * 10 ** -4 num_samples = opt.num_samples gamma = opt.gamma alpha = opt.alpha beta = opt.beta yita = opt.yita mu = opt.mu y_dim =opt.y_dim path = '/data/nus' ''' dataset preprocessing ''' nums, dsets, labels = _dataset(path) num_database, num_test = nums dset_database, dset_database_txt, dset_test, dset_test_txt = dsets database_labels, test_labels = labels n_class = test_labels.size()[1] testloader = DataLoader(dset_test, batch_size=1, shuffle=False, num_workers=4) testloader_txt = DataLoader(dset_test_txt, batch_size=1, shuffle=False, num_workers=4) ''' model construction ''' model = cnn_model.CNNNet(opt.arch, code_length) model.cuda() model_txt = txt_model.TxtModule(y_dim, code_length) model_txt.cuda() adsh_loss = dl.DCHUCLoss(gamma, code_length, num_database, alpha, mu) adsh_loss_txt = dl.DCHUCLoss(gamma, code_length, num_database, alpha, mu) optimizer = optim.SGD(model.parameters(), lr=learning_rate, weight_decay=weight_decay) optimizer_txt = optim.SGD(model_txt.parameters(), lr=learning_rate_txt, weight_decay=weight_decay) V = np.zeros((num_database, code_length)) W = np.random.normal(loc=0.0, scale=0.01, size=(code_length, n_class)) model.train() for iter in range(max_iter): ''' sampling and construct similarity matrix ''' select_index = list(np.random.permutation(range(num_database)))[0: num_samples] _sampler = subsetsampler.SubsetSampler(select_index) trainloader = DataLoader(dset_database, batch_size=batch_size, sampler=_sampler, shuffle=False, num_workers=4) trainloader_txt = DataLoader(dset_database_txt, batch_size=batch_size, sampler=_sampler, shuffle=False, num_workers=4) ''' learning deep neural network: feature learning ''' sample_label = database_labels.index_select(0, torch.from_numpy(np.array(select_index))) Sim = calc_sim(sample_label, database_labels) S1 = calc_sim(sample_label, sample_label) U = np.zeros((num_samples, code_length), dtype=np.float) G = np.zeros((num_samples, code_length), dtype=np.float) for epoch in range(epochs): for zz in range(1): for iteration, (train_input, train_label, batch_ind) in enumerate(trainloader): batch_size_ = train_label.size(0) u_ind = np.linspace(iteration * batch_size, np.min((num_samples, (iteration + 1) * batch_size)) - 1, batch_size_, dtype=int) train_input = Variable(train_input.cuda()) output = model(train_input) S = Sim.index_select(0, torch.from_numpy(u_ind)) S_1 = S.index_select(1, torch.from_numpy(u_ind)) U[u_ind, :] = output.cpu().data.numpy() model.zero_grad() loss = adsh_loss(output, G[u_ind, :], V, S, S_1, V[batch_ind.cpu().numpy(), :], Variable(torch.from_numpy(W).type(torch.FloatTensor).cuda()), Variable(train_label.type(torch.FloatTensor).cuda())) loss.backward() optimizer.step() for zz in range(1): for iteration, (train_input, train_label, batch_ind) in enumerate(trainloader_txt): batch_size_ = train_label.size(0) u_ind = np.linspace(iteration * batch_size, np.min((num_samples, (iteration + 1) * batch_size)) - 1, batch_size_, dtype=int) train_input = train_input.unsqueeze(1).unsqueeze(-1).type(torch.FloatTensor) train_input = Variable(train_input.cuda()) output = model_txt(train_input) S = Sim.index_select(0, torch.from_numpy(u_ind)) S_1 = S.index_select(1, torch.from_numpy(u_ind)) G[u_ind, :] = output.cpu().data.numpy() model_txt.zero_grad() loss = adsh_loss_txt(output, U[u_ind, :], V, S, S_1, V[batch_ind.cpu().numpy(), :], Variable(torch.from_numpy(W).type(torch.FloatTensor).cuda()), Variable(train_label.type(torch.FloatTensor).cuda())) loss.backward() optimizer_txt.step() adjusting_learning_rate(optimizer, iter) adjusting_learning_rate(optimizer_txt, iter) ''' learning binary codes: discrete coding ''' barU = np.zeros((num_database, code_length)) barG = np.zeros((num_database, code_length)) barU[select_index, :] = U barG[select_index, :] = G Q = -2 * code_length * Sim.cpu().numpy().transpose().dot(U + G) - gamma * (barU + barG)\ - 2 * beta * database_labels.numpy().dot(W.transpose()) for k in range(code_length): sel_ind = np.setdiff1d([ii for ii in range(code_length)], k) V_ = V[:, sel_ind] W_ = W.transpose()[:, sel_ind] Wk = W.transpose()[:, k] Uk = U[:, k] Gk = G[:, k] U_ = U[:, sel_ind] G_ = G[:, sel_ind] V[:, k] = -np.sign(Q[:, k] + 2 * V_.dot(U_.transpose().dot(Uk) + 2 * G_.transpose().dot(Gk) + beta * 2 * W_.transpose().dot(Wk))) I = np.eye(code_length) P = np.matrix(U.transpose().dot(U)) * alpha + alpha * np.matrix(G.transpose().dot(G))\ + beta * np.matrix(V.transpose().dot(V)) + yita * I PN = np.linalg.pinv(P) BL = (alpha * barU + alpha * barG + beta * V).transpose().dot(database_labels.numpy()) W = np.asarray(PN.dot(BL)) lossx = calc_loss(W, sample_label, V, U, G, Sim.cpu().numpy(), S1.cpu().numpy(), code_length, select_index, gamma, yita, alpha, mu) print('[Iteration: %3d/%3d][Train Loss: %.4f]' % (iter, max_iter, lossx)) ''' training procedure finishes, evaluation ''' model.eval() model_txt.eval() qB = encode(model, testloader, num_test, code_length) qB_txt = encode(model_txt, testloader_txt, num_test, code_length, istxt=True) rB = V map = calc_hr.calc_map(qB, rB, test_labels.numpy(), database_labels.numpy()) map_txt2img = calc_hr.calc_map(qB_txt, rB, test_labels.numpy(), database_labels.numpy()) print('[Evaluation: mAP_i2t: %.4f, mAP_txt2img: %.4f]' % (map, map_txt2img))