def _get_gauss_params(self, net):
        '''
        Returns the mean and covariance for the gaussian model on the whole
        patch (i.e., window to sample plus padding around it)
        '''

        means = np.zeros((3, self.patchSize * self.patchSize))
        covs = np.zeros((3, self.patchSize * self.patchSize,
                         self.patchSize * self.patchSize))

        path_mean = self.path_folder + '{}_{}_means{}_indep'.format(
            self.dataset, self.netname, self.patchSize)
        path_cov = self.path_folder + '{}_{}_covs{}_indep'.format(
            self.dataset, self.netname, self.patchSize)

        # check if values are already precomputed and saved; otherwise do so first
        if os.path.exists(path_mean + '.npy') and os.path.exists(path_cov +
                                                                 '.npy'):

            means = np.load(path_mean + '.npy')
            covs = np.load(path_cov + '.npy')

        else:

            for c in [0, 1, 2]:
                # get data
                X, _, _ = utlD.get_data(self.dataset, net,
                                        self.folder_est_name)

                # get samples for fitting the distribution
                patchesMat = np.empty((0, self.patchSize * self.patchSize),
                                      dtype=np.float)
                for i in xrange(int(self.num_samples_fit / X.shape[0]) + 1):
                    # get a random (upper left) position of the patch
                    idx = random.sample(
                        range((self.image_dims[0] - self.patchSize) *
                              (self.image_dims[1] - self.patchSize)), 1)[0]
                    idx = np.unravel_index(
                        idx, (self.image_dims[0] - self.patchSize,
                              self.image_dims[1] - self.patchSize))
                    idx = [idx[0], idx[1]]
                    # get the patch from all the images in X, from the given channel
                    patch = X[:, c, idx[0]:idx[0] + self.patchSize,
                              idx[1]:idx[1] + self.patchSize]
                    patchesMat = np.vstack(
                        (patchesMat,
                         patch.reshape(
                             (X.shape[0], self.patchSize * self.patchSize))))

                # compute the mean and covariance of the collected samples
                means[c] = np.mean(patchesMat, axis=0)
                covs[c] = np.cov(patchesMat.T)

            # save the mean and the covariance
            np.save(path_mean, means)
            np.save(path_cov, covs)

        return means, covs
 def _save_minmax_values(self, net):
     '''
     When X.npy is updated, this can be executed to also update the min/max
     values of the data (which is being used to cut off the values in the
     sampler so that we don't have overflowing values)
     '''
     X, _, _ = utlD.get_data(self.dataset, net, self.folder_est_name)
     minMaxVals = np.zeros((2, 3, X.shape[-2], X.shape[-1]))
     minMaxVals[0] = np.min(X, axis=0)
     minMaxVals[1] = np.max(X, axis=0)
     if not os.path.exists(self.path_folder):
         os.makedirs(self.path_folder)
     np.save(
         self.path_folder +
         '{}_{}_minMaxVals'.format(self.dataset, self.netname), minMaxVals)
Beispiel #3
0
import pandas as pd
from utils import Strategy, optimise_ma_strat
from utils_email import send_email
from utils_data import get_data, create_spread

months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
years = ['2015','2016']

df = get_data(months,years,'S')

#df = create_spread('S15','W15','S')

#final_pnl,sharpe,max_drawdown

#opt, matrix = optimise_ma_strat(slice(2,21,1),slice(0.1,0.5,0.05),100000,200000,df,'final_pnl')
#opt_strat = Strategy(opt[0][0],opt[0][1],100000,200000,df)
#opt_strat.run_strategy()
#opt_strat.graph()


#strat = Strategy(4,0.2,100000,200000,df)
#strat.run_strategy()
#strat.graph()

#bp,sp,pos,vwap = opt_strat.calc_todays_actions()

#send_email('nbpttf_reversion',bp,sp,pos,vwap)
 import utils_visualise_cv2 as utlV
 #
 args = parse_args()
 folder_name = args.folder_name
 dataset = args.dataset
 net_name = args.net_name
 gpu_id = args.gpu_id
 batch_size = args.batch_size
 segmentation_method = args.segmentation_method
 num_segments = args.num_segments
 num_samples = args.num_samples
 #
 utlC.set_caffe_mode(gpu_id)
 netP, netD, blobnames = utlC.get_caffenet(dataset, net_name)
 # get the data
 blL, gtL, fnL = utlD.get_data(dataset, netP, folder_name)
 #
 test_indices = range(len(fnL))
 # get the label names
 classes = utlD.get_classnames(dataset)
 CC = utlD.get_classnums(dataset)  # num of classes
 C = CC - 1  # num of classes - background
 # make folder for saving the results if it doesn't exist
 path_results = './examples/e2x/intgrads/results_{}/{}/{}_{}_{}_{}'.format(
     method, dataset, net_name, segmentation_method, num_segments,
     num_samples)
 if not os.path.exists(path_results):
     os.makedirs(path_results)
 # ------------------------ EXPERIMENTS ------------------------
 # target function (mapping input features to output confidences)
 [TB, TC, TH, TW] = netD.blobs['data'].data.shape
batch_size = 64


# ------------------------ SET-UP ------------------------

net = utlC.get_net(netpath)

utlC.set_torch_mode(net, gpu=gpu)

# get the data
transformation = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.1307,), (0.3081,))
])

X_test, X_test_im, X_filenames = utlD.get_data(path_data, transformation)

image_dims = X_test_im[0].shape

if not test_indices:
    test_indices = [i for i in range(len(X_test))]

# make folder for saving the results if it doesn't exist
path_results = './results/'
if not os.path.exists(path_results):
    os.makedirs(path_results)          
          
# ------------------------ EXPERIMENTS ------------------------

# target function (mapping input features to output probabilities)
target_func = lambda x: utlC.forward_pass(net, x, layer_numbers, gpu)