import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt

def create_dir(_path):
	if not os.path.exists(_path):
		os.makedirs(_path)

def accuracy(output, target, topk=(1,)):
	"""Computes the precision@k for the specified values of k"""
	maxk = max(topk)
	batch_size = target.size(0)

	_, pred = output.topk(maxk, 1, True, True)
	pred = pred.t()
	correct = pred.eq(target.view(1, -1).expand_as(pred))

	res = []
	for k in topk:
		correct_k = correct[:k].view(-1).float().sum(0)
		res.append(correct_k.mul_(100.0 / batch_size))
	return res

class AverageMeter:
	"""Computes and stores the average and current value"""

	def __init__(self):
		self.reset()

	def reset(self):
		self.values = []
		self.counter = 0

	def append(self, val):
		self.values.append(val)
		self.counter += 1

	@property
	def val(self):
		return self.values[-1]

	@property
	def avg(self):
		return sum(self.values) / len(self.values)

	@property
	def sum(self):
		return sum(self.values)

	@property
	def last_avg(self):
		if self.counter == 0:
			return self.latest_avg
		else:
			self.latest_avg = sum(self.values[-self.counter:]) / self.counter
			self.counter = 0
			return self.latest_avg


class TwoCropTransform:
	"""Create two crops of the same image"""
	def __init__(self, transform):
		self.transform = transform

	def __call__(self, x):
		return [self.transform(x), self.transform(x)]
		