import pandas as pd
import csv
import os
import sys
import torch
import shutil
import pickle
import numpy as np
from skimage.measure import compare_ssim, compare_psnr


def load_state_dict(model, fname):
    """
    Set parameters converted from Caffe models authors of VGGFace2 provide.
    See https://www.robots.ox.ac.uk/~vgg/data/vgg_face2/.

    Arguments:
        model: model
        fname: file name of parameters converted from a Caffe model, assuming the file format is Pickle.
    """
    with open(fname, 'rb') as f:
        weights = pickle.load(f, encoding='latin1')

    own_state = model.state_dict()
    for name, param in weights.items():
        if name in own_state:
            try:
                own_state[name].copy_(torch.from_numpy(param))
            except Exception:
                raise RuntimeError(
                    'While copying the parameter named {}, whose dimensions in the model are {} and whose ' \
                    'dimensions in the checkpoint are {}.'.format(name, own_state[name].size(), param.size()))
        else:
            raise KeyError('unexpected key "{}" in state_dict'.format(name))


class AverageMeter(object):
    """Computes and stores the average and current value"""

    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count


def get_psnr(im1, im2):
    h, w, _ = im1.shape
    return compare_psnr(im1, im2[:h, :w, :], data_range=255)


def get_ssim(im1, im2):
    h, w, _ = im1.shape
    return compare_ssim(im1, im2[:h, :w, :], data_range=255, gaussian_weights=True, use_sample_covariance=False,
                        multichannel=True)

import pandas as pd
import csv
import os
import sys
import torch
import shutil
import pickle
import numpy as np
from skimage.measure import compare_ssim, compare_psnr


def load_state_dict(model, fname):
    """
    Set parameters converted from Caffe models authors of VGGFace2 provide.
    See https://www.robots.ox.ac.uk/~vgg/data/vgg_face2/.

    Arguments:
        model: model
        fname: file name of parameters converted from a Caffe model, assuming the file format is Pickle.
    """
    with open(fname, 'rb') as f:
        weights = pickle.load(f, encoding='latin1')

    own_state = model.state_dict()
    for name, param in weights.items():
        if name in own_state:
            try:
                own_state[name].copy_(torch.from_numpy(param))
            except Exception:
                raise RuntimeError(
                    'While copying the parameter named {}, whose dimensions in the model are {} and whose ' \
                    'dimensions in the checkpoint are {}.'.format(name, own_state[name].size(), param.size()))
        else:
            raise KeyError('unexpected key "{}" in state_dict'.format(name))


class AverageMeter(object):
    """Computes and stores the average and current value"""

    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count


def get_psnr(im1, im2):
    h, w, _ = im1.shape
    return compare_psnr(im1, im2[:h, :w, :], data_range=255)


def get_ssim(im1, im2):
    h, w, _ = im1.shape
    return compare_ssim(im1, im2[:h, :w, :], data_range=255, gaussian_weights=True, use_sample_covariance=False,
                        multichannel=True)

Set parameters converted from Caffe models authors of VGGFace2 provide.     See https://www.robots.ox.ac.uk/~vgg/data/vgg_face2/.      Arguments:         model: model         fname: file name of parameters converted from a Caffe model, assuming the file format is Pickle. Computes and stores the average and current value