<jupyter_start><jupyter_text>Dataset Loading<jupyter_code>root = 'D:\\datasets\\TargetBoxDatasets\\YoutubeBoundingBoxes\\videos\\boudingboxes_detection\\frames'
dataset = TrainDataLoader(root,out_feature=25)<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>weight_dir = 'checkpoint'

#Hyperparameters 
lr = 0.001
momentum = 0.9
weight_decay = 5e-5
max_epochs = 2
max_batches = 10
steps = 1

start = 0 # in case we're resuming training 
steps_save = 5 # nb of epochs to save model
steps_report = 100 # nb of steps to report loss
torch.cuda.empty_cache()
# Initialize model with pretrained weights : 
model = SiameseRPNpp(anchor=5)
model = model.cuda()
cudnn.benchmark = True
# Loss and optimizer
criterion= MultiBoxLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=lr, momentum=momentum, weight_decay = weight_decay)

def save(it):
    model_path = os.path.join('checkpoint', f'model_{str(it).zfill(5)}.pth')
    
    opt_path = os.path.join('checkpoint', f'opt_{str(it).zfill(5)}.pth')
    print(f'\nsaving model to {model_path} ...')
        
    torch.save({'model': model.state_dict()}, model_path)
    torch.save({'epoch': epoch,
                'iteration': it,
                'optimG': optimizer.state_dict()}, opt_path)
                    
                    
    os.system('echo {} > {}'.format(str(it).zfill(5),
                                            os.path.join('checkpoint', 'latest.ckpt')))
    if it > 0 : 
        print('Removing files :', os.path.join('checkpoint', f'model_{str(it-steps_save).zfill(5)}.pth'))
        
        os.remove(os.path.join('checkpoint', f'model_{str(it-steps_save).zfill(5)}.pth'))
        os.remove(os.path.join('checkpoint', f'opt_{str(it-steps_save).zfill(5)}.pth'))
%%time
closses, rlosses, tlosses = AverageMeter(), AverageMeter(), AverageMeter()
summary_loss = {'closs':[], 'rloss':[], 'tloss':[]}
if start == 0 : 
    previous_epoch = -1
else : 
    previous_epoch = start
for epoch in range(start, max_epochs):
    cur_lr = adjust_learning_rate(lr, optimizer, epoch, gamma=0.1) # adjust learning rate
    epoch_closs = []
    epoch_rloss = []
    epoch_tloss = []
    for idx in range(dataset.__len__()): 
        ret = dataset.__get__(idx)
        template = ret['template_tensor'].cuda()
        detection= ret['detection_tensor'].cuda()
        pos_neg_diff = ret['pos_neg_diff_tensor'].cuda()
        
        rout, cout = model(template, detection)
        # reshape model output to fit the loss
        cout = cout.squeeze().permute(1,2,0).reshape(-1, 2)
        rout = rout.squeeze().permute(1,2,0).reshape(-1, 4)
        
        # get labels
        predictions, targets = (cout, rout), pos_neg_diff
        
        # get multi box loss
        closs, rloss, loss, reg_pred, reg_target, pos_index, neg_index = criterion(predictions, targets)
       
        closs_ = closs.cpu().item() # transfer to cpu
        rloss_ = rloss.cpu().item()
        loss_ = loss.cpu().item()
        
        epoch_closs.append(closs_)
        epoch_rloss.append(rloss_)
        epoch_tloss.append(loss_)
        
        if np.isnan(closs_):
            print('Error, loss is nan')
            break
        
        closses.update(closs_)
        rlosses.update(rloss_)
        tlosses.update(loss_)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        steps += 1
        
        if epoch % steps_save == 0 and epoch != previous_epoch:
#             file_path = os.path.join(weight_dir, 'weights-{:07d}.pth.tar'.format(steps))
#             state = {
#             'epoch' :epoch+1,
#             'state_dict' :model.state_dict(),
#             'optimizer' : optimizer.state_dict(),
#             }
#             torch.save(state, file_path)
            save(epoch)
            previous_epoch = epoch 
        if steps % steps_report == 0: 
            print(f'Epoch {epoch}/{max_epochs}, example {idx}/{dataset.__len__()} :  closs={closs_}, rloss={rloss_}, tloss={loss_}')
    summary_loss['closs'].append(epoch_closs)
    summary_loss['rloss'].append(epoch_rloss)
    summary_loss['tloss'].append(epoch_tloss)
closs_mean = np.array(summary_loss['closs']).mean(axis=1)
rloss_mean = np.array(summary_loss['rloss']).mean(axis=1)
tloss_mean = np.array(summary_loss['tloss']).mean(axis=1)

plt.plot(closs_mean, label='classification loss')
plt.plot(rloss_mean, label='regression loss')
plt.plot(tloss_mean, label='total loss')
plt.title('loss per epoch')
plt.xlabel('epochs')
plt.legend()
_ = model.eval()
# for c in range(20):
#     channel_1 = rout[0][c].cpu().detach()
#     ax = sns.heatmap(channel_1, linewidth=0, cmap="jet")
#     plt.show()<jupyter_output><empty_output><jupyter_text>Test<jupyter_code># dataset = TrainDataLoader(root,out_feature=25)
%%time
import time
times = []
start = time.time()


index_list = range(dataset.__len__())
ret = dataset.__get__(random.choice(index_list)) 
template = ret['template_tensor'].cuda()
detection= ret['detection_tensor'].cuda()
pos_neg_diff = ret['pos_neg_diff_tensor'].cuda()

rout, cout  = model(template, detection) #[1, 10, 17, 17], [1, 20, 17, 17]

cout = cout.reshape(-1, 2)
rout = rout.reshape(-1, 4)
cout_ = cout.cpu().detach().numpy()
score = 1/(1 + np.exp(cout_[:,1]-cout_[:,0]))
diff   = rout.cpu().detach().numpy() #1445

num_proposals = 10


end_time = time.time()-start
times.append(end_time)
    
# best_labels = np.argsort(pos_neg_diff.cpu().detach().numpy()[:,0])[::-1][:10]
score = F.softmax(cout).cpu().detach().numpy()[:,1]

score_64_index = np.argsort(score)[::-1][:num_proposals]
score64 = score[score_64_index]
diffs64 = diff[score_64_index, :]#pos_neg_diff.cpu().detach().numpy()[best_labels,1:] #diff[score_64_index, :]
anchors64 = ret['anchors'][score_64_index]


proposals_x = (anchors64[:, 0] + anchors64[:, 2] * diffs64[:, 0]).reshape(-1, 1)
proposals_y = (anchors64[:, 1] + anchors64[:, 3] * diffs64[:, 1]).reshape(-1, 1)
proposals_w = (anchors64[:, 2] * np.exp(diffs64[:, 2])).reshape(-1, 1)
proposals_h = (anchors64[:, 3] * np.exp(diffs64[:, 3])).reshape(-1, 1)

# proposals_x = anchors64[:,0].reshape(-1, 1)
# proposals_y = anchors64[:,1].reshape(-1, 1)
# proposals_w = anchors64[:,2].reshape(-1, 1)
# proposals_h = anchors64[:,3].reshape(-1, 1)

proposals = np.hstack((proposals_x, proposals_y, proposals_w, proposals_h))


detection = ret['detection_cropped_resized']
template = ret['template_cropped_resized']
detection = ret['detection_tensor'].cpu().detach().numpy()
detection = Image.open(ret['detection_img_path'])
# pos_neg_diff_cpu = pos_neg_diff.cpu().numpy()
# num_proposals = 50
# score_64_index =  np.argsort(score)[::-1][:num_proposals]#np.array([1492])

# diffs64 = pos_neg_diff_cpu[score_64_index,1:]
# score64 = score[score_64_index]

# anchors64 = ret['anchors'][score_64_index]
# proposals_x = (anchors64[:, 0] + anchors64[:, 2] * diffs64[:, 0]).reshape(-1, 1)
# proposals_y = (anchors64[:, 1] + anchors64[:, 3] * diffs64[:, 1]).reshape(-1, 1)
# proposals_w = (anchors64[:, 2] * np.exp(diffs64[:, 2])).reshape(-1, 1)
# proposals_h = (anchors64[:, 3] * np.exp(diffs64[:, 3])).reshape(-1, 1)
# proposals = np.hstack((proposals_x, proposals_y, proposals_w, proposals_h))

from PIL import Image, ImageOps, ImageStat, ImageDraw

draw = ImageDraw.Draw(detection)
for i in range(num_proposals):
    x, y, w, h = proposals_x[i], proposals_y[i], proposals_w[i], proposals_h[i]
    print(x, y, w, h)
    x1, y1, x2, y2 = x-w//2, y-h//2, x+w//2, y+h//2
    draw.line([(x1, y1), (x2, y1), (x2, y2), (x1, y2), (x1, y1)], width=1, fill='red')
""" save detection template proposals"""
x, y, w, h = ret['detection_target_xywh']
print(x, y, w, h)
x1, y1, x2, y2 = x-w//2, y-h//2, x+w//2, y+h//2
draw.line([(x1, y1), (x2, y1), (x2, y2), (x1, y2), (x1, y1)], width=1, fill='green')

import matplotlib.pyplot as plt
plt.imshow(detection)
plt.axis('off')

plt.imshow(template)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1766864953803316, 'fraction_numerical': 0.03354005822047842, 'mean_word_length': 3.723251643753736, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '558244', 'license': 'MIT', 'n_tokens_mistral': 3106, 'n_tokens_neox': 2977, 'n_words': 527, 'path': 'train.ipynb', 'repo_name': 'ybendou/SiameseRPNpp', 'size': '7901'}