from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import _init_paths
import os
import sys
import numpy as np
import argparse
import pprint
import pdb
import time
import cv2
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.optim as optim
import pickle
from roi_data_layer.roidb import combined_roidb
from roi_data_layer.roibatchLoader import roibatchLoader
from model.utils.config import cfg, cfg_from_file, cfg_from_list, get_output_dir
from model.rpn.bbox_transform import clip_boxes
from model.nms.nms_wrapper import nms
from model.rpn.bbox_transform import bbox_transform_inv
from model.utils.net_utils import save_net, load_net, vis_detections
from model.faster_rcnn.vgg16 import vgg16
from model.faster_rcnn.resnet import resnet
from model.faster_rcnn.alexnet import alexnet
from torchvision.transforms import ToTensor, ToPILImage, Resize
import torchvision.transforms as transforms
import pdb

try:
    xrange  # Python 2
except NameError:
    xrange = range  # Python 3


def evaluate(student_net, dataset):
    imdb, roidb, ratio_list, ratio_index = combined_roidb(dataset, False)

    im_data = torch.FloatTensor(1)
    im_info = torch.FloatTensor(1)
    num_boxes = torch.LongTensor(1)
    gt_boxes = torch.FloatTensor(1)

    # ship to cuda

    im_data = im_data.cuda()
    im_info = im_info.cuda()
    num_boxes = num_boxes.cuda()
    gt_boxes = gt_boxes.cuda()

    # make variable
    im_data = Variable(im_data)
    im_info = Variable(im_info)
    num_boxes = Variable(num_boxes)
    gt_boxes = Variable(gt_boxes)

    cfg.CUDA = True

    start = time.time()
    max_per_image = 100

    num_images = len(imdb.image_index)
    all_boxes = [[[] for _ in xrange(num_images)]
                 for _ in xrange(imdb.num_classes)]

    dataset = roibatchLoader(roidb, ratio_list, ratio_index, 1, \
                             imdb.num_classes, training=False, normalize=False)

    dataloader = torch.utils.data.DataLoader(dataset, batch_size=1,
                                             shuffle=False, num_workers=0,
                                             pin_memory=True)

    data_iter = iter(dataloader)
    class_agnostic = False
    vis = False
    thresh = 0.0
    # calcolare i bbbox
    student_net.eval()
    empty_array = np.transpose(np.array([[], [], [], [], []]), (1, 0))

    for i in range(num_images):
        data = next(data_iter)

        im_data.data.resize_(data[0].size()).copy_(data[0])
        im_info.data.resize_(data[1].size()).copy_(data[1])
        gt_boxes.data.resize_(data[2].size()).copy_(data[2])
        num_boxes.data.resize_(data[3].size()).copy_(data[3])

        det_tic = time.time()
        s_RPN_mask, s_RPN_reg, s_RPN_cls, s_RCN_mask, s_RCN_reg, s_RCN_cls = student_net(im_data, im_info, gt_boxes,
                                                                                         num_boxes)

        # Region Proposal Network Classification
        Z_s = s_RPN_cls[0]
        fg_bg_label = s_RPN_cls[1]
        rpn_loss_cls = s_RPN_cls[2]
        # Region Proposal Network Regression
        y_reg = s_RPN_reg[0]
        R_s = s_RPN_reg[1]
        rpn_loss_box = s_RPN_reg[2]
        # Region Proposal Network Mask
        iw = s_RPN_mask[0]
        ow = s_RPN_mask[1]

        # Region Classification Network Classification
        rcn_cls_score = s_RCN_cls[0]
        cls_prob = s_RCN_cls[1]
        RCNN_loss_cls = s_RCN_cls[2]
        # Region Classification Network Regression
        rois = s_RCN_reg[0]
        rois_label = s_RCN_reg[1]
        rois_target = s_RCN_reg[2]
        bbox_pred = s_RCN_reg[3]
        RCNN_loss_bbox = s_RCN_reg[4]
        # Region Classification Network Mask
        rois_inside_ws = s_RCN_mask[0]
        rois_outside_ws = s_RCN_mask[1]


        scores = cls_prob.data
        boxes = rois.data[:, :, 1:5]

        if cfg.TEST.BBOX_REG:
            # Apply bounding-box regression deltas
            box_deltas = bbox_pred.data
            if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED:
                # Optionally normalize targets by a precomputed mean and stdev
                if class_agnostic:
                    box_deltas = box_deltas.view(-1, 4) * torch.FloatTensor(cfg.TRAIN.BBOX_NORMALIZE_STDS).cuda() \
                                 + torch.FloatTensor(cfg.TRAIN.BBOX_NORMALIZE_MEANS).cuda()
                    box_deltas = box_deltas.view(1, -1, 4)
                else:
                    box_deltas = box_deltas.view(-1, 4) * torch.FloatTensor(cfg.TRAIN.BBOX_NORMALIZE_STDS).cuda() \
                                 + torch.FloatTensor(cfg.TRAIN.BBOX_NORMALIZE_MEANS).cuda()
                    box_deltas = box_deltas.view(1, -1, 4 * len(imdb.classes))

            pred_boxes = bbox_transform_inv(boxes, box_deltas, 1)
            pred_boxes = clip_boxes(pred_boxes, im_info.data, 1)
        else:
            # Simply repeat the boxes, once for each class
            pred_boxes = np.tile(boxes, (1, scores.shape[1]))

        pred_boxes /= data[1][0][2].item()

        scores = scores.squeeze()
        pred_boxes = pred_boxes.squeeze()
        det_toc = time.time()
        detect_time = det_toc - det_tic
        misc_tic = time.time()
        if vis:
            im = cv2.imread(imdb.image_path_at(i))
            im2show = np.copy(im)
        for j in xrange(1, imdb.num_classes):
            inds = torch.nonzero(scores[:, j] > thresh).view(-1)
            # if there is det
            if inds.numel() > 0:
                cls_scores = scores[:, j][inds]
                _, order = torch.sort(cls_scores, 0, True)
                if class_agnostic:
                    cls_boxes = pred_boxes[inds, :]
                else:
                    cls_boxes = pred_boxes[inds][:, j * 4:(j + 1) * 4]

                cls_dets = torch.cat((cls_boxes, cls_scores.unsqueeze(1)), 1)
                # cls_dets = torch.cat((cls_boxes, cls_scores), 1)
                cls_dets = cls_dets[order]
                keep = nms(cls_dets, cfg.TEST.NMS)
                cls_dets = cls_dets[keep.view(-1).long()]
                if vis:
                    im2show = vis_detections(im2show, imdb.classes[j], cls_dets.cpu().numpy(), 0.3)
                all_boxes[j][i] = cls_dets.cpu().numpy()
            else:
                all_boxes[j][i] = empty_array

        # Limit to max_per_image detections *over all classes*
        if max_per_image > 0:
            image_scores = np.hstack([all_boxes[j][i][:, -1]
                                      for j in xrange(1, imdb.num_classes)])
            if len(image_scores) > max_per_image:
                image_thresh = np.sort(image_scores)[-max_per_image]
                for j in xrange(1, imdb.num_classes):
                    keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
                    all_boxes[j][i] = all_boxes[j][i][keep, :]

        misc_toc = time.time()
        nms_time = misc_toc - misc_tic

        sys.stdout.write('im_detect: {:d}/{:d} {:.3f}s {:.3f}s   \r' \
                         .format(i + 1, num_images, detect_time, nms_time))
        sys.stdout.flush()
    map = imdb.evaluate_detections_MAP(all_boxes)
    return map



def load_data(dataset):
    if dataset == "pascal_voc":
        imdb_name = "voc_2007_trainval"
        imdbval_name = "voc_2007_test"
        set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS', '[0.5,1,2]']
    elif dataset == "pascal_voc_0712":
        imdb_name = "voc_2007_trainval+voc_2012_trainval"
        imdbval_name = "voc_2007_test"
        set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS', '[0.5,1,2]']
    elif dataset == "coco":
        imdb_name = "coco_2014_train+coco_2014_valminusminival"
        imdbval_name = "coco_2014_minival"
        set_cfgs = ['ANCHOR_SCALES', '[4, 8, 16, 32]', 'ANCHOR_RATIOS', '[0.5,1,2]']
    elif dataset == "imagenet":
        imdb_name = "imagenet_train"
        imdbval_name = "imagenet_val"
        set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS', '[0.5,1,2]']
    elif dataset == "vg":
        imdb_name = "vg_150-50-50_minitrain"
        imdbval_name = "vg_150-50-50_minival"
        set_cfgs = ['ANCHOR_SCALES', '[4, 8, 16, 32]', 'ANCHOR_RATIOS', '[0.5,1,2]']
    cfg.TRAIN.USE_FLIPPED = False
    imdb, roidb, ratio_list, ratio_index = combined_roidb(imdbval_name, False)
    imdb.competition_mode(on=True)
    print('{:d} roidb entries'.format(len(roidb)))
    return imdb, roidb, ratio_list, ratio_index


a388cbd0a58295cada18c7906186a4ebf51655b5
<mask token>


def evaluate(student_net, dataset):
    imdb, roidb, ratio_list, ratio_index = combined_roidb(dataset, False)
    im_data = torch.FloatTensor(1)
    im_info = torch.FloatTensor(1)
    num_boxes = torch.LongTensor(1)
    gt_boxes = torch.FloatTensor(1)
    im_data = im_data.cuda()
    im_info = im_info.cuda()
    num_boxes = num_boxes.cuda()
    gt_boxes = gt_boxes.cuda()
    im_data = Variable(im_data)
    im_info = Variable(im_info)
    num_boxes = Variable(num_boxes)
    gt_boxes = Variable(gt_boxes)
    cfg.CUDA = True
    start = time.time()
    max_per_image = 100
    num_images = len(imdb.image_index)
    all_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(imdb.
        num_classes)]
    dataset = roibatchLoader(roidb, ratio_list, ratio_index, 1, imdb.
        num_classes, training=False, normalize=False)
    dataloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle
        =False, num_workers=0, pin_memory=True)
    data_iter = iter(dataloader)
    class_agnostic = False
    vis = False
    thresh = 0.0
    student_net.eval()
    empty_array = np.transpose(np.array([[], [], [], [], []]), (1, 0))
    for i in range(num_images):
        data = next(data_iter)
        im_data.data.resize_(data[0].size()).copy_(data[0])
        im_info.data.resize_(data[1].size()).copy_(data[1])
        gt_boxes.data.resize_(data[2].size()).copy_(data[2])
        num_boxes.data.resize_(data[3].size()).copy_(data[3])
        det_tic = time.time()
        (s_RPN_mask, s_RPN_reg, s_RPN_cls, s_RCN_mask, s_RCN_reg, s_RCN_cls
            ) = student_net(im_data, im_info, gt_boxes, num_boxes)
        Z_s = s_RPN_cls[0]
        fg_bg_label = s_RPN_cls[1]
        rpn_loss_cls = s_RPN_cls[2]
        y_reg = s_RPN_reg[0]
        R_s = s_RPN_reg[1]
        rpn_loss_box = s_RPN_reg[2]
        iw = s_RPN_mask[0]
        ow = s_RPN_mask[1]
        rcn_cls_score = s_RCN_cls[0]
        cls_prob = s_RCN_cls[1]
        RCNN_loss_cls = s_RCN_cls[2]
        rois = s_RCN_reg[0]
        rois_label = s_RCN_reg[1]
        rois_target = s_RCN_reg[2]
        bbox_pred = s_RCN_reg[3]
        RCNN_loss_bbox = s_RCN_reg[4]
        rois_inside_ws = s_RCN_mask[0]
        rois_outside_ws = s_RCN_mask[1]
        scores = cls_prob.data
        boxes = rois.data[:, :, 1:5]
        if cfg.TEST.BBOX_REG:
            box_deltas = bbox_pred.data
            if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED:
                if class_agnostic:
                    box_deltas = box_deltas.view(-1, 4) * torch.FloatTensor(cfg
                        .TRAIN.BBOX_NORMALIZE_STDS).cuda() + torch.FloatTensor(
                        cfg.TRAIN.BBOX_NORMALIZE_MEANS).cuda()
                    box_deltas = box_deltas.view(1, -1, 4)
                else:
                    box_deltas = box_deltas.view(-1, 4) * torch.FloatTensor(cfg
                        .TRAIN.BBOX_NORMALIZE_STDS).cuda() + torch.FloatTensor(
                        cfg.TRAIN.BBOX_NORMALIZE_MEANS).cuda()
                    box_deltas = box_deltas.view(1, -1, 4 * len(imdb.classes))
            pred_boxes = bbox_transform_inv(boxes, box_deltas, 1)
            pred_boxes = clip_boxes(pred_boxes, im_info.data, 1)
        else:
            pred_boxes = np.tile(boxes, (1, scores.shape[1]))
        pred_boxes /= data[1][0][2].item()
        scores = scores.squeeze()
        pred_boxes = pred_boxes.squeeze()
        det_toc = time.time()
        detect_time = det_toc - det_tic
        misc_tic = time.time()
        if vis:
            im = cv2.imread(imdb.image_path_at(i))
            im2show = np.copy(im)
        for j in xrange(1, imdb.num_classes):
            inds = torch.nonzero(scores[:, j] > thresh).view(-1)
            if inds.numel() > 0:
                cls_scores = scores[:, j][inds]
                _, order = torch.sort(cls_scores, 0, True)
                if class_agnostic:
                    cls_boxes = pred_boxes[inds, :]
                else:
                    cls_boxes = pred_boxes[inds][:, j * 4:(j + 1) * 4]
                cls_dets = torch.cat((cls_boxes, cls_scores.unsqueeze(1)), 1)
                cls_dets = cls_dets[order]
                keep = nms(cls_dets, cfg.TEST.NMS)
                cls_dets = cls_dets[keep.view(-1).long()]
                if vis:
                    im2show = vis_detections(im2show, imdb.classes[j],
                        cls_dets.cpu().numpy(), 0.3)
                all_boxes[j][i] = cls_dets.cpu().numpy()
            else:
                all_boxes[j][i] = empty_array
        if max_per_image > 0:
            image_scores = np.hstack([all_boxes[j][i][:, -1] for j in
                xrange(1, imdb.num_classes)])
            if len(image_scores) > max_per_image:
                image_thresh = np.sort(image_scores)[-max_per_image]
                for j in xrange(1, imdb.num_classes):
                    keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
                    all_boxes[j][i] = all_boxes[j][i][keep, :]
        misc_toc = time.time()
        nms_time = misc_toc - misc_tic
        sys.stdout.write('im_detect: {:d}/{:d} {:.3f}s {:.3f}s   \r'.format
            (i + 1, num_images, detect_time, nms_time))
        sys.stdout.flush()
    map = imdb.evaluate_detections_MAP(all_boxes)
    return map


<mask token>

<mask token>


def evaluate(student_net, dataset):
    imdb, roidb, ratio_list, ratio_index = combined_roidb(dataset, False)
    im_data = torch.FloatTensor(1)
    im_info = torch.FloatTensor(1)
    num_boxes = torch.LongTensor(1)
    gt_boxes = torch.FloatTensor(1)
    im_data = im_data.cuda()
    im_info = im_info.cuda()
    num_boxes = num_boxes.cuda()
    gt_boxes = gt_boxes.cuda()
    im_data = Variable(im_data)
    im_info = Variable(im_info)
    num_boxes = Variable(num_boxes)
    gt_boxes = Variable(gt_boxes)
    cfg.CUDA = True
    start = time.time()
    max_per_image = 100
    num_images = len(imdb.image_index)
    all_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(imdb.
        num_classes)]
    dataset = roibatchLoader(roidb, ratio_list, ratio_index, 1, imdb.
        num_classes, training=False, normalize=False)
    dataloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle
        =False, num_workers=0, pin_memory=True)
    data_iter = iter(dataloader)
    class_agnostic = False
    vis = False
    thresh = 0.0
    student_net.eval()
    empty_array = np.transpose(np.array([[], [], [], [], []]), (1, 0))
    for i in range(num_images):
        data = next(data_iter)
        im_data.data.resize_(data[0].size()).copy_(data[0])
        im_info.data.resize_(data[1].size()).copy_(data[1])
        gt_boxes.data.resize_(data[2].size()).copy_(data[2])
        num_boxes.data.resize_(data[3].size()).copy_(data[3])
        det_tic = time.time()
        (s_RPN_mask, s_RPN_reg, s_RPN_cls, s_RCN_mask, s_RCN_reg, s_RCN_cls
            ) = student_net(im_data, im_info, gt_boxes, num_boxes)
        Z_s = s_RPN_cls[0]
        fg_bg_label = s_RPN_cls[1]
        rpn_loss_cls = s_RPN_cls[2]
        y_reg = s_RPN_reg[0]
        R_s = s_RPN_reg[1]
        rpn_loss_box = s_RPN_reg[2]
        iw = s_RPN_mask[0]
        ow = s_RPN_mask[1]
        rcn_cls_score = s_RCN_cls[0]
        cls_prob = s_RCN_cls[1]
        RCNN_loss_cls = s_RCN_cls[2]
        rois = s_RCN_reg[0]
        rois_label = s_RCN_reg[1]
        rois_target = s_RCN_reg[2]
        bbox_pred = s_RCN_reg[3]
        RCNN_loss_bbox = s_RCN_reg[4]
        rois_inside_ws = s_RCN_mask[0]
        rois_outside_ws = s_RCN_mask[1]
        scores = cls_prob.data
        boxes = rois.data[:, :, 1:5]
        if cfg.TEST.BBOX_REG:
            box_deltas = bbox_pred.data
            if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED:
                if class_agnostic:
                    box_deltas = box_deltas.view(-1, 4) * torch.FloatTensor(cfg
                        .TRAIN.BBOX_NORMALIZE_STDS).cuda() + torch.FloatTensor(
                        cfg.TRAIN.BBOX_NORMALIZE_MEANS).cuda()
                    box_deltas = box_deltas.view(1, -1, 4)
                else:
                    box_deltas = box_deltas.view(-1, 4) * torch.FloatTensor(cfg
                        .TRAIN.BBOX_NORMALIZE_STDS).cuda() + torch.FloatTensor(
                        cfg.TRAIN.BBOX_NORMALIZE_MEANS).cuda()
                    box_deltas = box_deltas.view(1, -1, 4 * len(imdb.classes))
            pred_boxes = bbox_transform_inv(boxes, box_deltas, 1)
            pred_boxes = clip_boxes(pred_boxes, im_info.data, 1)
        else:
            pred_boxes = np.tile(boxes, (1, scores.shape[1]))
        pred_boxes /= data[1][0][2].item()
        scores = scores.squeeze()
        pred_boxes = pred_boxes.squeeze()
        det_toc = time.time()
        detect_time = det_toc - det_tic
        misc_tic = time.time()
        if vis:
            im = cv2.imread(imdb.image_path_at(i))
            im2show = np.copy(im)
        for j in xrange(1, imdb.num_classes):
            inds = torch.nonzero(scores[:, j] > thresh).view(-1)
            if inds.numel() > 0:
                cls_scores = scores[:, j][inds]
                _, order = torch.sort(cls_scores, 0, True)
                if class_agnostic:
                    cls_boxes = pred_boxes[inds, :]
                else:
                    cls_boxes = pred_boxes[inds][:, j * 4:(j + 1) * 4]
                cls_dets = torch.cat((cls_boxes, cls_scores.unsqueeze(1)), 1)
                cls_dets = cls_dets[order]
                keep = nms(cls_dets, cfg.TEST.NMS)
                cls_dets = cls_dets[keep.view(-1).long()]
                if vis:
                    im2show = vis_detections(im2show, imdb.classes[j],
                        cls_dets.cpu().numpy(), 0.3)
                all_boxes[j][i] = cls_dets.cpu().numpy()
            else:
                all_boxes[j][i] = empty_array
        if max_per_image > 0:
            image_scores = np.hstack([all_boxes[j][i][:, -1] for j in
                xrange(1, imdb.num_classes)])
            if len(image_scores) > max_per_image:
                image_thresh = np.sort(image_scores)[-max_per_image]
                for j in xrange(1, imdb.num_classes):
                    keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
                    all_boxes[j][i] = all_boxes[j][i][keep, :]
        misc_toc = time.time()
        nms_time = misc_toc - misc_tic
        sys.stdout.write('im_detect: {:d}/{:d} {:.3f}s {:.3f}s   \r'.format
            (i + 1, num_images, detect_time, nms_time))
        sys.stdout.flush()
    map = imdb.evaluate_detections_MAP(all_boxes)
    return map


def load_data(dataset):
    if dataset == 'pascal_voc':
        imdb_name = 'voc_2007_trainval'
        imdbval_name = 'voc_2007_test'
        set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS',
            '[0.5,1,2]']
    elif dataset == 'pascal_voc_0712':
        imdb_name = 'voc_2007_trainval+voc_2012_trainval'
        imdbval_name = 'voc_2007_test'
        set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS',
            '[0.5,1,2]']
    elif dataset == 'coco':
        imdb_name = 'coco_2014_train+coco_2014_valminusminival'
        imdbval_name = 'coco_2014_minival'
        set_cfgs = ['ANCHOR_SCALES', '[4, 8, 16, 32]', 'ANCHOR_RATIOS',
            '[0.5,1,2]']
    elif dataset == 'imagenet':
        imdb_name = 'imagenet_train'
        imdbval_name = 'imagenet_val'
        set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS',
            '[0.5,1,2]']
    elif dataset == 'vg':
        imdb_name = 'vg_150-50-50_minitrain'
        imdbval_name = 'vg_150-50-50_minival'
        set_cfgs = ['ANCHOR_SCALES', '[4, 8, 16, 32]', 'ANCHOR_RATIOS',
            '[0.5,1,2]']
    cfg.TRAIN.USE_FLIPPED = False
    imdb, roidb, ratio_list, ratio_index = combined_roidb(imdbval_name, False)
    imdb.competition_mode(on=True)
    print('{:d} roidb entries'.format(len(roidb)))
    return imdb, roidb, ratio_list, ratio_index

<mask token>
try:
    xrange
except NameError:
    xrange = range


def evaluate(student_net, dataset):
    imdb, roidb, ratio_list, ratio_index = combined_roidb(dataset, False)
    im_data = torch.FloatTensor(1)
    im_info = torch.FloatTensor(1)
    num_boxes = torch.LongTensor(1)
    gt_boxes = torch.FloatTensor(1)
    im_data = im_data.cuda()
    im_info = im_info.cuda()
    num_boxes = num_boxes.cuda()
    gt_boxes = gt_boxes.cuda()
    im_data = Variable(im_data)
    im_info = Variable(im_info)
    num_boxes = Variable(num_boxes)
    gt_boxes = Variable(gt_boxes)
    cfg.CUDA = True
    start = time.time()
    max_per_image = 100
    num_images = len(imdb.image_index)
    all_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(imdb.
        num_classes)]
    dataset = roibatchLoader(roidb, ratio_list, ratio_index, 1, imdb.
        num_classes, training=False, normalize=False)
    dataloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle
        =False, num_workers=0, pin_memory=True)
    data_iter = iter(dataloader)
    class_agnostic = False
    vis = False
    thresh = 0.0
    student_net.eval()
    empty_array = np.transpose(np.array([[], [], [], [], []]), (1, 0))
    for i in range(num_images):
        data = next(data_iter)
        im_data.data.resize_(data[0].size()).copy_(data[0])
        im_info.data.resize_(data[1].size()).copy_(data[1])
        gt_boxes.data.resize_(data[2].size()).copy_(data[2])
        num_boxes.data.resize_(data[3].size()).copy_(data[3])
        det_tic = time.time()
        (s_RPN_mask, s_RPN_reg, s_RPN_cls, s_RCN_mask, s_RCN_reg, s_RCN_cls
            ) = student_net(im_data, im_info, gt_boxes, num_boxes)
        Z_s = s_RPN_cls[0]
        fg_bg_label = s_RPN_cls[1]
        rpn_loss_cls = s_RPN_cls[2]
        y_reg = s_RPN_reg[0]
        R_s = s_RPN_reg[1]
        rpn_loss_box = s_RPN_reg[2]
        iw = s_RPN_mask[0]
        ow = s_RPN_mask[1]
        rcn_cls_score = s_RCN_cls[0]
        cls_prob = s_RCN_cls[1]
        RCNN_loss_cls = s_RCN_cls[2]
        rois = s_RCN_reg[0]
        rois_label = s_RCN_reg[1]
        rois_target = s_RCN_reg[2]
        bbox_pred = s_RCN_reg[3]
        RCNN_loss_bbox = s_RCN_reg[4]
        rois_inside_ws = s_RCN_mask[0]
        rois_outside_ws = s_RCN_mask[1]
        scores = cls_prob.data
        boxes = rois.data[:, :, 1:5]
        if cfg.TEST.BBOX_REG:
            box_deltas = bbox_pred.data
            if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED:
                if class_agnostic:
                    box_deltas = box_deltas.view(-1, 4) * torch.FloatTensor(cfg
                        .TRAIN.BBOX_NORMALIZE_STDS).cuda() + torch.FloatTensor(
                        cfg.TRAIN.BBOX_NORMALIZE_MEANS).cuda()
                    box_deltas = box_deltas.view(1, -1, 4)
                else:
                    box_deltas = box_deltas.view(-1, 4) * torch.FloatTensor(cfg
                        .TRAIN.BBOX_NORMALIZE_STDS).cuda() + torch.FloatTensor(
                        cfg.TRAIN.BBOX_NORMALIZE_MEANS).cuda()
                    box_deltas = box_deltas.view(1, -1, 4 * len(imdb.classes))
            pred_boxes = bbox_transform_inv(boxes, box_deltas, 1)
            pred_boxes = clip_boxes(pred_boxes, im_info.data, 1)
        else:
            pred_boxes = np.tile(boxes, (1, scores.shape[1]))
        pred_boxes /= data[1][0][2].item()
        scores = scores.squeeze()
        pred_boxes = pred_boxes.squeeze()
        det_toc = time.time()
        detect_time = det_toc - det_tic
        misc_tic = time.time()
        if vis:
            im = cv2.imread(imdb.image_path_at(i))
            im2show = np.copy(im)
        for j in xrange(1, imdb.num_classes):
            inds = torch.nonzero(scores[:, j] > thresh).view(-1)
            if inds.numel() > 0:
                cls_scores = scores[:, j][inds]
                _, order = torch.sort(cls_scores, 0, True)
                if class_agnostic:
                    cls_boxes = pred_boxes[inds, :]
                else:
                    cls_boxes = pred_boxes[inds][:, j * 4:(j + 1) * 4]
                cls_dets = torch.cat((cls_boxes, cls_scores.unsqueeze(1)), 1)
                cls_dets = cls_dets[order]
                keep = nms(cls_dets, cfg.TEST.NMS)
                cls_dets = cls_dets[keep.view(-1).long()]
                if vis:
                    im2show = vis_detections(im2show, imdb.classes[j],
                        cls_dets.cpu().numpy(), 0.3)
                all_boxes[j][i] = cls_dets.cpu().numpy()
            else:
                all_boxes[j][i] = empty_array
        if max_per_image > 0:
            image_scores = np.hstack([all_boxes[j][i][:, -1] for j in
                xrange(1, imdb.num_classes)])
            if len(image_scores) > max_per_image:
                image_thresh = np.sort(image_scores)[-max_per_image]
                for j in xrange(1, imdb.num_classes):
                    keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
                    all_boxes[j][i] = all_boxes[j][i][keep, :]
        misc_toc = time.time()
        nms_time = misc_toc - misc_tic
        sys.stdout.write('im_detect: {:d}/{:d} {:.3f}s {:.3f}s   \r'.format
            (i + 1, num_images, detect_time, nms_time))
        sys.stdout.flush()
    map = imdb.evaluate_detections_MAP(all_boxes)
    return map


def load_data(dataset):
    if dataset == 'pascal_voc':
        imdb_name = 'voc_2007_trainval'
        imdbval_name = 'voc_2007_test'
        set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS',
            '[0.5,1,2]']
    elif dataset == 'pascal_voc_0712':
        imdb_name = 'voc_2007_trainval+voc_2012_trainval'
        imdbval_name = 'voc_2007_test'
        set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS',
            '[0.5,1,2]']
    elif dataset == 'coco':
        imdb_name = 'coco_2014_train+coco_2014_valminusminival'
        imdbval_name = 'coco_2014_minival'
        set_cfgs = ['ANCHOR_SCALES', '[4, 8, 16, 32]', 'ANCHOR_RATIOS',
            '[0.5,1,2]']
    elif dataset == 'imagenet':
        imdb_name = 'imagenet_train'
        imdbval_name = 'imagenet_val'
        set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS',
            '[0.5,1,2]']
    elif dataset == 'vg':
        imdb_name = 'vg_150-50-50_minitrain'
        imdbval_name = 'vg_150-50-50_minival'
        set_cfgs = ['ANCHOR_SCALES', '[4, 8, 16, 32]', 'ANCHOR_RATIOS',
            '[0.5,1,2]']
    cfg.TRAIN.USE_FLIPPED = False
    imdb, roidb, ratio_list, ratio_index = combined_roidb(imdbval_name, False)
    imdb.competition_mode(on=True)
    print('{:d} roidb entries'.format(len(roidb)))
    return imdb, roidb, ratio_list, ratio_index

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import _init_paths
import os
import sys
import numpy as np
import argparse
import pprint
import pdb
import time
import cv2
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.optim as optim
import pickle
from roi_data_layer.roidb import combined_roidb
from roi_data_layer.roibatchLoader import roibatchLoader
from model.utils.config import cfg, cfg_from_file, cfg_from_list, get_output_dir
from model.rpn.bbox_transform import clip_boxes
from model.nms.nms_wrapper import nms
from model.rpn.bbox_transform import bbox_transform_inv
from model.utils.net_utils import save_net, load_net, vis_detections
from model.faster_rcnn.vgg16 import vgg16
from model.faster_rcnn.resnet import resnet
from model.faster_rcnn.alexnet import alexnet
from torchvision.transforms import ToTensor, ToPILImage, Resize
import torchvision.transforms as transforms
import pdb
try:
    xrange
except NameError:
    xrange = range


def evaluate(student_net, dataset):
    imdb, roidb, ratio_list, ratio_index = combined_roidb(dataset, False)
    im_data = torch.FloatTensor(1)
    im_info = torch.FloatTensor(1)
    num_boxes = torch.LongTensor(1)
    gt_boxes = torch.FloatTensor(1)
    im_data = im_data.cuda()
    im_info = im_info.cuda()
    num_boxes = num_boxes.cuda()
    gt_boxes = gt_boxes.cuda()
    im_data = Variable(im_data)
    im_info = Variable(im_info)
    num_boxes = Variable(num_boxes)
    gt_boxes = Variable(gt_boxes)
    cfg.CUDA = True
    start = time.time()
    max_per_image = 100
    num_images = len(imdb.image_index)
    all_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(imdb.
        num_classes)]
    dataset = roibatchLoader(roidb, ratio_list, ratio_index, 1, imdb.
        num_classes, training=False, normalize=False)
    dataloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle
        =False, num_workers=0, pin_memory=True)
    data_iter = iter(dataloader)
    class_agnostic = False
    vis = False
    thresh = 0.0
    student_net.eval()
    empty_array = np.transpose(np.array([[], [], [], [], []]), (1, 0))
    for i in range(num_images):
        data = next(data_iter)
        im_data.data.resize_(data[0].size()).copy_(data[0])
        im_info.data.resize_(data[1].size()).copy_(data[1])
        gt_boxes.data.resize_(data[2].size()).copy_(data[2])
        num_boxes.data.resize_(data[3].size()).copy_(data[3])
        det_tic = time.time()
        (s_RPN_mask, s_RPN_reg, s_RPN_cls, s_RCN_mask, s_RCN_reg, s_RCN_cls
            ) = student_net(im_data, im_info, gt_boxes, num_boxes)
        Z_s = s_RPN_cls[0]
        fg_bg_label = s_RPN_cls[1]
        rpn_loss_cls = s_RPN_cls[2]
        y_reg = s_RPN_reg[0]
        R_s = s_RPN_reg[1]
        rpn_loss_box = s_RPN_reg[2]
        iw = s_RPN_mask[0]
        ow = s_RPN_mask[1]
        rcn_cls_score = s_RCN_cls[0]
        cls_prob = s_RCN_cls[1]
        RCNN_loss_cls = s_RCN_cls[2]
        rois = s_RCN_reg[0]
        rois_label = s_RCN_reg[1]
        rois_target = s_RCN_reg[2]
        bbox_pred = s_RCN_reg[3]
        RCNN_loss_bbox = s_RCN_reg[4]
        rois_inside_ws = s_RCN_mask[0]
        rois_outside_ws = s_RCN_mask[1]
        scores = cls_prob.data
        boxes = rois.data[:, :, 1:5]
        if cfg.TEST.BBOX_REG:
            box_deltas = bbox_pred.data
            if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED:
                if class_agnostic:
                    box_deltas = box_deltas.view(-1, 4) * torch.FloatTensor(cfg
                        .TRAIN.BBOX_NORMALIZE_STDS).cuda() + torch.FloatTensor(
                        cfg.TRAIN.BBOX_NORMALIZE_MEANS).cuda()
                    box_deltas = box_deltas.view(1, -1, 4)
                else:
                    box_deltas = box_deltas.view(-1, 4) * torch.FloatTensor(cfg
                        .TRAIN.BBOX_NORMALIZE_STDS).cuda() + torch.FloatTensor(
                        cfg.TRAIN.BBOX_NORMALIZE_MEANS).cuda()
                    box_deltas = box_deltas.view(1, -1, 4 * len(imdb.classes))
            pred_boxes = bbox_transform_inv(boxes, box_deltas, 1)
            pred_boxes = clip_boxes(pred_boxes, im_info.data, 1)
        else:
            pred_boxes = np.tile(boxes, (1, scores.shape[1]))
        pred_boxes /= data[1][0][2].item()
        scores = scores.squeeze()
        pred_boxes = pred_boxes.squeeze()
        det_toc = time.time()
        detect_time = det_toc - det_tic
        misc_tic = time.time()
        if vis:
            im = cv2.imread(imdb.image_path_at(i))
            im2show = np.copy(im)
        for j in xrange(1, imdb.num_classes):
            inds = torch.nonzero(scores[:, j] > thresh).view(-1)
            if inds.numel() > 0:
                cls_scores = scores[:, j][inds]
                _, order = torch.sort(cls_scores, 0, True)
                if class_agnostic:
                    cls_boxes = pred_boxes[inds, :]
                else:
                    cls_boxes = pred_boxes[inds][:, j * 4:(j + 1) * 4]
                cls_dets = torch.cat((cls_boxes, cls_scores.unsqueeze(1)), 1)
                cls_dets = cls_dets[order]
                keep = nms(cls_dets, cfg.TEST.NMS)
                cls_dets = cls_dets[keep.view(-1).long()]
                if vis:
                    im2show = vis_detections(im2show, imdb.classes[j],
                        cls_dets.cpu().numpy(), 0.3)
                all_boxes[j][i] = cls_dets.cpu().numpy()
            else:
                all_boxes[j][i] = empty_array
        if max_per_image > 0:
            image_scores = np.hstack([all_boxes[j][i][:, -1] for j in
                xrange(1, imdb.num_classes)])
            if len(image_scores) > max_per_image:
                image_thresh = np.sort(image_scores)[-max_per_image]
                for j in xrange(1, imdb.num_classes):
                    keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
                    all_boxes[j][i] = all_boxes[j][i][keep, :]
        misc_toc = time.time()
        nms_time = misc_toc - misc_tic
        sys.stdout.write('im_detect: {:d}/{:d} {:.3f}s {:.3f}s   \r'.format
            (i + 1, num_images, detect_time, nms_time))
        sys.stdout.flush()
    map = imdb.evaluate_detections_MAP(all_boxes)
    return map


def load_data(dataset):
    if dataset == 'pascal_voc':
        imdb_name = 'voc_2007_trainval'
        imdbval_name = 'voc_2007_test'
        set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS',
            '[0.5,1,2]']
    elif dataset == 'pascal_voc_0712':
        imdb_name = 'voc_2007_trainval+voc_2012_trainval'
        imdbval_name = 'voc_2007_test'
        set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS',
            '[0.5,1,2]']
    elif dataset == 'coco':
        imdb_name = 'coco_2014_train+coco_2014_valminusminival'
        imdbval_name = 'coco_2014_minival'
        set_cfgs = ['ANCHOR_SCALES', '[4, 8, 16, 32]', 'ANCHOR_RATIOS',
            '[0.5,1,2]']
    elif dataset == 'imagenet':
        imdb_name = 'imagenet_train'
        imdbval_name = 'imagenet_val'
        set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS',
            '[0.5,1,2]']
    elif dataset == 'vg':
        imdb_name = 'vg_150-50-50_minitrain'
        imdbval_name = 'vg_150-50-50_minival'
        set_cfgs = ['ANCHOR_SCALES', '[4, 8, 16, 32]', 'ANCHOR_RATIOS',
            '[0.5,1,2]']
    cfg.TRAIN.USE_FLIPPED = False
    imdb, roidb, ratio_list, ratio_index = combined_roidb(imdbval_name, False)
    imdb.competition_mode(on=True)
    print('{:d} roidb entries'.format(len(roidb)))
    return imdb, roidb, ratio_list, ratio_index

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import _init_paths
import os
import sys
import numpy as np
import argparse
import pprint
import pdb
import time
import cv2
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.optim as optim
import pickle
from roi_data_layer.roidb import combined_roidb
from roi_data_layer.roibatchLoader import roibatchLoader
from model.utils.config import cfg, cfg_from_file, cfg_from_list, get_output_dir
from model.rpn.bbox_transform import clip_boxes
from model.nms.nms_wrapper import nms
from model.rpn.bbox_transform import bbox_transform_inv
from model.utils.net_utils import save_net, load_net, vis_detections
from model.faster_rcnn.vgg16 import vgg16
from model.faster_rcnn.resnet import resnet
from model.faster_rcnn.alexnet import alexnet
from torchvision.transforms import ToTensor, ToPILImage, Resize
import torchvision.transforms as transforms
import pdb

try:
    xrange  # Python 2
except NameError:
    xrange = range  # Python 3


def evaluate(student_net, dataset):
    imdb, roidb, ratio_list, ratio_index = combined_roidb(dataset, False)

    im_data = torch.FloatTensor(1)
    im_info = torch.FloatTensor(1)
    num_boxes = torch.LongTensor(1)
    gt_boxes = torch.FloatTensor(1)

    # ship to cuda

    im_data = im_data.cuda()
    im_info = im_info.cuda()
    num_boxes = num_boxes.cuda()
    gt_boxes = gt_boxes.cuda()

    # make variable
    im_data = Variable(im_data)
    im_info = Variable(im_info)
    num_boxes = Variable(num_boxes)
    gt_boxes = Variable(gt_boxes)

    cfg.CUDA = True

    start = time.time()
    max_per_image = 100

    num_images = len(imdb.image_index)
    all_boxes = [[[] for _ in xrange(num_images)]
                 for _ in xrange(imdb.num_classes)]

    dataset = roibatchLoader(roidb, ratio_list, ratio_index, 1, \
                             imdb.num_classes, training=False, normalize=False)

    dataloader = torch.utils.data.DataLoader(dataset, batch_size=1,
                                             shuffle=False, num_workers=0,
                                             pin_memory=True)

    data_iter = iter(dataloader)
    class_agnostic = False
    vis = False
    thresh = 0.0
    # calcolare i bbbox
    student_net.eval()
    empty_array = np.transpose(np.array([[], [], [], [], []]), (1, 0))

    for i in range(num_images):
        data = next(data_iter)

        im_data.data.resize_(data[0].size()).copy_(data[0])
        im_info.data.resize_(data[1].size()).copy_(data[1])
        gt_boxes.data.resize_(data[2].size()).copy_(data[2])
        num_boxes.data.resize_(data[3].size()).copy_(data[3])

        det_tic = time.time()
        s_RPN_mask, s_RPN_reg, s_RPN_cls, s_RCN_mask, s_RCN_reg, s_RCN_cls = student_net(im_data, im_info, gt_boxes,
                                                                                         num_boxes)

        # Region Proposal Network Classification
        Z_s = s_RPN_cls[0]
        fg_bg_label = s_RPN_cls[1]
        rpn_loss_cls = s_RPN_cls[2]
        # Region Proposal Network Regression
        y_reg = s_RPN_reg[0]
        R_s = s_RPN_reg[1]
        rpn_loss_box = s_RPN_reg[2]
        # Region Proposal Network Mask
        iw = s_RPN_mask[0]
        ow = s_RPN_mask[1]

        # Region Classification Network Classification
        rcn_cls_score = s_RCN_cls[0]
        cls_prob = s_RCN_cls[1]
        RCNN_loss_cls = s_RCN_cls[2]
        # Region Classification Network Regression
        rois = s_RCN_reg[0]
        rois_label = s_RCN_reg[1]
        rois_target = s_RCN_reg[2]
        bbox_pred = s_RCN_reg[3]
        RCNN_loss_bbox = s_RCN_reg[4]
        # Region Classification Network Mask
        rois_inside_ws = s_RCN_mask[0]
        rois_outside_ws = s_RCN_mask[1]


        scores = cls_prob.data
        boxes = rois.data[:, :, 1:5]

        if cfg.TEST.BBOX_REG:
            # Apply bounding-box regression deltas
            box_deltas = bbox_pred.data
            if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED:
                # Optionally normalize targets by a precomputed mean and stdev
                if class_agnostic:
                    box_deltas = box_deltas.view(-1, 4) * torch.FloatTensor(cfg.TRAIN.BBOX_NORMALIZE_STDS).cuda() \
                                 + torch.FloatTensor(cfg.TRAIN.BBOX_NORMALIZE_MEANS).cuda()
                    box_deltas = box_deltas.view(1, -1, 4)
                else:
                    box_deltas = box_deltas.view(-1, 4) * torch.FloatTensor(cfg.TRAIN.BBOX_NORMALIZE_STDS).cuda() \
                                 + torch.FloatTensor(cfg.TRAIN.BBOX_NORMALIZE_MEANS).cuda()
                    box_deltas = box_deltas.view(1, -1, 4 * len(imdb.classes))

            pred_boxes = bbox_transform_inv(boxes, box_deltas, 1)
            pred_boxes = clip_boxes(pred_boxes, im_info.data, 1)
        else:
            # Simply repeat the boxes, once for each class
            pred_boxes = np.tile(boxes, (1, scores.shape[1]))

        pred_boxes /= data[1][0][2].item()

        scores = scores.squeeze()
        pred_boxes = pred_boxes.squeeze()
        det_toc = time.time()
        detect_time = det_toc - det_tic
        misc_tic = time.time()
        if vis:
            im = cv2.imread(imdb.image_path_at(i))
            im2show = np.copy(im)
        for j in xrange(1, imdb.num_classes):
            inds = torch.nonzero(scores[:, j] > thresh).view(-1)
            # if there is det
            if inds.numel() > 0:
                cls_scores = scores[:, j][inds]
                _, order = torch.sort(cls_scores, 0, True)
                if class_agnostic:
                    cls_boxes = pred_boxes[inds, :]
                else:
                    cls_boxes = pred_boxes[inds][:, j * 4:(j + 1) * 4]

                cls_dets = torch.cat((cls_boxes, cls_scores.unsqueeze(1)), 1)
                # cls_dets = torch.cat((cls_boxes, cls_scores), 1)
                cls_dets = cls_dets[order]
                keep = nms(cls_dets, cfg.TEST.NMS)
                cls_dets = cls_dets[keep.view(-1).long()]
                if vis:
                    im2show = vis_detections(im2show, imdb.classes[j], cls_dets.cpu().numpy(), 0.3)
                all_boxes[j][i] = cls_dets.cpu().numpy()
            else:
                all_boxes[j][i] = empty_array

        # Limit to max_per_image detections *over all classes*
        if max_per_image > 0:
            image_scores = np.hstack([all_boxes[j][i][:, -1]
                                      for j in xrange(1, imdb.num_classes)])
            if len(image_scores) > max_per_image:
                image_thresh = np.sort(image_scores)[-max_per_image]
                for j in xrange(1, imdb.num_classes):
                    keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
                    all_boxes[j][i] = all_boxes[j][i][keep, :]

        misc_toc = time.time()
        nms_time = misc_toc - misc_tic

        sys.stdout.write('im_detect: {:d}/{:d} {:.3f}s {:.3f}s   \r' \
                         .format(i + 1, num_images, detect_time, nms_time))
        sys.stdout.flush()
    map = imdb.evaluate_detections_MAP(all_boxes)
    return map



def load_data(dataset):
    if dataset == "pascal_voc":
        imdb_name = "voc_2007_trainval"
        imdbval_name = "voc_2007_test"
        set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS', '[0.5,1,2]']
    elif dataset == "pascal_voc_0712":
        imdb_name = "voc_2007_trainval+voc_2012_trainval"
        imdbval_name = "voc_2007_test"
        set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS', '[0.5,1,2]']
    elif dataset == "coco":
        imdb_name = "coco_2014_train+coco_2014_valminusminival"
        imdbval_name = "coco_2014_minival"
        set_cfgs = ['ANCHOR_SCALES', '[4, 8, 16, 32]', 'ANCHOR_RATIOS', '[0.5,1,2]']
    elif dataset == "imagenet":
        imdb_name = "imagenet_train"
        imdbval_name = "imagenet_val"
        set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS', '[0.5,1,2]']
    elif dataset == "vg":
        imdb_name = "vg_150-50-50_minitrain"
        imdbval_name = "vg_150-50-50_minival"
        set_cfgs = ['ANCHOR_SCALES', '[4, 8, 16, 32]', 'ANCHOR_RATIOS', '[0.5,1,2]']
    cfg.TRAIN.USE_FLIPPED = False
    imdb, roidb, ratio_list, ratio_index = combined_roidb(imdbval_name, False)
    imdb.competition_mode(on=True)
    print('{:d} roidb entries'.format(len(roidb)))
    return imdb, roidb, ratio_list, ratio_index

