예제 #1
0
    def Train_and_Save(self, training_data, training_truth, validation_data, validation_truth, epoch_num=16):

        for n in range(self.ensemble_num):

            model = FFN(str(n), self.networkConfig, self.para_num)
            model.Train(training_data, training_truth, validation_data, validation_truth, epochs=epoch_num)
            model.Save("Models/" + self.name + "/" + str(n))
            tf.reset_default_graph()
예제 #2
0
    def __init__(self, Sfilename, model='Net1_FFN_v7', verbose=False):
        self.index = 0
        self.settingfuncs = [self.setting1, self.setting2, self.setting3,
                             self.setting4, self.setting5, self.setting6,
                             self.setting7, self.setting8]
        if isinstance(model, str):
            self.modelname = model

            self.model = FFN(model)
            self.model.Load(verbose=verbose)

        elif isinstance(model, FFN):
            self.model = model
            self.modelname = self.model.name

        mask1, pmask1 = self.model.apply_mask(Sfilename)

        rgb, self.TitleStr = Vis.FalseColour(Sfilename, False)

        scn = DL.scene_loader(Sfilename)
        scn.load(['bayes_in', 'probability_cloud_single_in'])
        bmask = DL.upscale_repeat(scn['bayes_in'].values).astype('int')
        bmask = 1 - ((bmask & 2) / 2)
        bpmask = DL.upscale_repeat(scn['probability_cloud_single_in'].values)

        self.im1 = plt.imshow(rgb)
        plt.title('False colour image\n' + self.TitleStr)

        self.im2 = plt.imshow(mask1, cmap='Blues')
        self.im2.set_visible(False)

        bmask = DL.extract_mask(Sfilename, 'bayes_in', 2)
        self.im3 = plt.imshow(bmask, cmap='Reds')
        self.im3.set_visible(False)

        mask1 = mask1.astype('bool')
        temp = np.copy(rgb)
        temp[~mask1, :] = 254 / 255, 253 / 255, 185 / 255
        self.im4 = plt.imshow(temp)
        self.im4.set_visible(False)

        rgb[mask1, :] = 74 / 255, 117 / 255, 50 / 255
        self.im5 = plt.imshow(rgb)
        self.im5.set_visible(False)

        self.im6 = plt.imshow(1 - pmask1, cmap='Oranges')
        self.im6.set_visible(False)

        self.im7 = plt.imshow(1 - bpmask, cmap='Reds')
        self.im7.set_visible(False)

        maskdiff = bmask - mask1
        self.im8 = plt.imshow(maskdiff, cmap='bwr')
        self.im8.set_visible(False)

        self.cbset = False
        self.cb = None
예제 #3
0
class TestMaskingMethod(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        self.model = FFN('Net1_FFN')
        self.model.Load()
        self.TestFile = "./SatelliteData/SLSTR/2018/08/S3A_SL_1_RBT____20180806T081914_20180806T082214_20180807T131253_0179_034_178_1620_LN2_O_NT_003.SEN3"

    def test_mask_FFNMethod(self):
        mask = self.model.apply_mask(self.TestFile)[0]

        self.assertEqual(mask.shape, (2400, 3000))
예제 #4
0
    def Predict(self, X):
        Y = []
        for n in range(self.ensemble_num):
            model = FFN("Models/" + self.name + "/" + str(n))
            model.Load()
            y = model.Predict(X)
            Y.append(y)
            tf.reset_default_graph()

        Y = np.array(Y)
        Y = Y.reshape(self.ensemble_num, -1, 2)
        YY = Y[:, :, 0]
        Ym = np.mean(YY, axis=0)
        Ys = np.std(YY, axis=0) 
        return(Ym, Ys)
예제 #5
0
    def model_agreement(self,
                        model,
                        verbose=False,
                        MaxDist=None,
                        MaxTime=None):
        """
        Apply a model to the dataframe and add model output to rows

        Adds the direct output of the model into the 'Labels' and
        'Label_Confidence' columns, in addition the 'Agree' column shows
        whether the model result agrees with the Calipso truth.

        Parameters
        ----------
        model: str
            Name of model to use. If using a model on disk, it should be saved in the Models folder.

        Returns
        ----------
        None
        """

        if MaxDist is not None:
            self._obj = self._obj[self._obj['Distance'] < MaxDist]
        if MaxTime is not None:
            self._obj = self._obj[abs(self._obj['TimeDiff']) < MaxTime]

        if isinstance(model, str):
            self.model = model
            model = FFN(model)
            model.Load(verbose=verbose)

        elif isinstance(model, FFN):
            pass

        num_inputs = model.para_num
        inputs = self._obj.dp.get_ffn_inputs(num_inputs)
        output_labels = model.model.predict_label(inputs)
        output_con = model.model.predict(inputs)

        self._obj['Labels'] = pd.Series(output_labels[:, 0],
                                        index=self._obj.index)
        self._obj['Label_Confidence'] = pd.Series(output_con[:, 0],
                                                  index=self._obj.index)

        self._obj = self._obj.dp.make_CTruth_col()

        self._obj['Agree'] = self._obj['CTruth'] != self._obj['Labels']
예제 #6
0
    def reproducibility(self,
                        modelname,
                        number_of_runs=15,
                        validation_frac=0.15,
                        para_num=22):
        """
        Return the average and standard deviation of a same model but different
        order of the data it is presented. These outputs quantify the
        reproducibilty  of the model.

        Parameters
        -----------
        modelname: str
            refers to model architecture to run

        number of runs: int
            number of times to run the model.

        validation_frac: float
            the fraction of data kept for validation when preparing the model's training data.

        para_num: int
            the number of inputs take by the model.

        Returns
        ---------
        average: float,
            average accuracy of the model.

        std: float
            standard deviation of the model.
        """

        accuracies = []

        for i in range(number_of_runs):
            model = FFN('Reproducibilty', modelname, 21)
            tdata, vdata, ttruth, vtruth = self._obj.dp.get_ffn_training_data(
                validation_frac=validation_frac, input_type=para_num)
            model.Train(tdata, ttruth, vdata, vtruth)
            acc = me.get_accuracy(model, vdata, vtruth, para_num=para_num)
            accuracies.append(acc)

        average = np.mean(accuracies)
        std = np.std(accuracies)

        return average, std
예제 #7
0
    def __init__(self, w, k, n, q, h, z, layers, activ_func):
        """
            FRNN:
                w = window length (number of periods)
                k = dimension of time (note: time2vec will make time-dim k+1)
                n = number of features at each point in time (without time features)
                q = number of queries (column in W)
                h = number of heads
                z = number of features to be extracted from the q*h results created by the attention heads
            FFN:
                layers    = list of integers representing the number of nodes in each layer
                activ     = the activation func between each layer
        """
        super().__init__()
        self.frnn = FRNN(w, k, n, q, h, z)
        self.ffn  = FFN(layers, activ_func)

        self.w, self.k, self.n, self.q, self.h, self.z, self.layer_dims, self.activ_func = \
            w, k, n, q, h, z, layers, activ_func
예제 #8
0
def mask_debug(Sfilename, model, verbose):
    if isinstance(model, str):
        modelname = model

        model = FFN(model)
        model.Load(verbose=verbose)

    elif isinstance(model, FFN):
        modelname = model.name

    mask1, pmask = model.apply_mask(Sfilename)

    rgb, TitleStr = Vis.FalseColour(Sfilename, False)

    plt.figure()
    plt.imshow(rgb)
    plt.title('False colour image\n' + TitleStr)
    plt.xlabel('km')
    plt.ylabel('km')
    plt.xticks([0, 500, 1000, 1500, 2000, 2500, 3000],
               [0, 250, 500, 750, 1000, 1250, 1500])
    plt.yticks([0, 500, 1000, 1500, 2000], [0, 250, 500, 750, 1000])

    plt.figure()
    plt.imshow(mask1, cmap='Blues')
    plt.title(modelname + ' mask\n' + TitleStr)
    plt.xlabel('km')
    plt.ylabel('km')
    plt.xticks([0, 500, 1000, 1500, 2000, 2500, 3000],
               [0, 250, 500, 750, 1000, 1250, 1500])
    plt.yticks([0, 500, 1000, 1500, 2000], [0, 250, 500, 750, 1000])

    plt.figure()
    bmask = DL.extract_mask(Sfilename, 'bayes_in', 2)
    plt.imshow(bmask, cmap='Reds')
    plt.title('Bayesian mask\n' + TitleStr)
    plt.xlabel('km')
    plt.ylabel('km')
    plt.xticks([0, 500, 1000, 1500, 2000, 2500, 3000],
               [0, 250, 500, 750, 1000, 1250, 1500])
    plt.yticks([0, 500, 1000, 1500, 2000], [0, 250, 500, 750, 1000])

    plt.figure()
    mask1 = mask1.astype('bool')
    rgb[~mask1, :] = 254 / 255, 253 / 255, 185 / 255
    plt.imshow(rgb)
    plt.title(modelname + ' masked false colour image\n' + TitleStr)
    plt.xlabel('km')
    plt.ylabel('km')
    plt.xticks([0, 500, 1000, 1500, 2000, 2500, 3000],
               [0, 250, 500, 750, 1000, 1250, 1500])
    plt.yticks([0, 500, 1000, 1500, 2000], [0, 250, 500, 750, 1000])

    plt.figure()
    ax = plt.gca()
    im5 = plt.imshow(pmask, cmap='Oranges_r')
    plt.title(modelname + ' model output\n' + TitleStr)
    plt.xlabel('km')
    plt.ylabel('km')
    plt.xticks([0, 500, 1000, 1500, 2000, 2500, 3000],
               [0, 250, 500, 750, 1000, 1250, 1500])
    plt.yticks([0, 500, 1000, 1500, 2000], [0, 250, 500, 750, 1000])
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="5%", pad=0.1)
    plt.colorbar(im5, cax=cax)

    plt.figure()
    maskdiff = mask1 - bmask
    ax = plt.gca()
    im6 = plt.imshow(maskdiff, cmap='bwr_r')
    plt.title(modelname + ' mask - Bayesian mask\n' + TitleStr)
    plt.xlabel('km')
    plt.ylabel('km')
    plt.xticks([0, 500, 1000, 1500, 2000, 2500, 3000],
               [0, 250, 500, 750, 1000, 1250, 1500])
    plt.yticks([0, 500, 1000, 1500, 2000], [0, 250, 500, 750, 1000])
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="5%", pad=0.1)
    plt.colorbar(im6, cax=cax)

    plt.show()
예제 #9
0
    def model_agreement(self,
                        model,
                        modeltype='FFN',
                        verbose=False,
                        MaxDist=None,
                        MaxTime=None):
        """
        Apply a model to the dataframe and add model output to rows

        Adds the direct output of the model into the 'Labels' and
        'Label_Confidence' columns, in addition the 'Agree' column shows
        whether the model result agrees with the Calipso truth.

        Parameters
        ----------
        model: str
            Name of model to use. If using a model on disk, it should be saved in the Models folder.

        Returns
        ----------
        None
        """

        if MaxDist is not None:
            self._obj = self._obj[self._obj['Distance'] < MaxDist]
        if MaxTime is not None:
            self._obj = self._obj[abs(self._obj['TimeDiff']) < MaxTime]

        if isinstance(model, str):
            self.model = model

            if modeltype == 'FFN':
                model = FFN(model)
            if modeltype == 'CNN':
                model = CNN(model)
            if modeltype == 'SuperModel':
                model = SuperModel(model)

            model.Load(verbose=verbose)

        elif isinstance(model, FFN):
            pass
        elif isinstance(model, CNN):
            pass
        elif isinstance(model, SuperModel):
            pass

        if modeltype == 'FFN':
            num_inputs = model.para_num
            inputs = self._obj.dp.get_ffn_inputs(num_inputs)
            output_labels = model.model.predict_label(inputs)
            output_con = model.model.predict(inputs)

        if modeltype == 'CNN':
            inputs = self._obj.dp.get_cnn_inputs()
            output_labels = model.model.predict_label(inputs)
            output_con = model.model.predict(inputs)

        if modeltype == 'SuperModel':
            num_inputs = model.para_num
            ffninputs = self._obj.dp.get_ffn_inputs(num_inputs)
            predictions1 = model.FFN.Predict(ffninputs)[:, 0]
            labels1 = model.FFN.model.predict_label(ffninputs)[:, 0]

            # boolean mask of bad predictions
            bad = abs(predictions1 - 0.5) < 0.25
            goodindices = np.where(bad == False)[0]
            badindices = np.where(bad == True)[0]
            cnninputs = self._obj[badindices].dp.get_cnn_inputs()
            cnninputs = dp.star_padding(cnninputs)

            # Feeding all of the inputs at once can cause a memory error
            # Instead split into chunks of 10,000
            chunkedcnninputs = [
                cnninputs[i:i + 10000] for i in range(0, len(cnninputs), 10000)
            ]

            predictions2 = []
            labels2 = []

            for i in range(len(chunkedcnninputs)):
                predictions2.extend(
                    model.CNN.model.predict(chunkedcnninputs[i])[:, 0])
                labels2.extend(
                    model.CNN.model.predict_label(chunkedcnninputs[i])[:, 0])

            finallabels = np.zeros(len(self._obj))
            finallabels[goodindices] = labels1[goodindices]
            finallabels[badindices] = labels2

            finalpredictions = np.zeros(len(self._obj))
            finalpredictions[goodindices] = predictions1[goodindices]
            finalpredictions[badindices] = predictions2

            output_labels = finallabels
            output_con = finalpredictions

        self._obj['Labels'] = pd.Series(output_labels[:, 0],
                                        index=self._obj.index)
        self._obj['Label_Confidence'] = pd.Series(output_con[:, 0],
                                                  index=self._obj.index)

        self._obj = self._obj.dp.make_CTruth_col()

        self._obj['Agree'] = self._obj['CTruth'] != self._obj['Labels']
예제 #10
0
    def shuffled_model_agreement(self,
                                 model,
                                 channel_name,
                                 verbose=False,
                                 MaxDist=None,
                                 MaxTime=None):
        """
        Apply a model to the dataframe and add model output to rows

        Adds the direct output of the model into the 'Labels' and
        'Label_Confidence' columns, in addition the 'Agree' column shows
        whether the model result agrees with the Calipso truth.

        Parameters
        ----------
        model: str
            Name of model to use. If using a model on disk, it should be saved in the Models folder.

        Returns
        ----------
        None
        """

        self.shuffled_channel = channel_name

        if MaxDist is not None:
            self._obj = self._obj[self._obj['Distance'] < MaxDist]
        if MaxTime is not None:
            self._obj = self._obj[abs(self._obj['TimeDiff']) < MaxTime]

        if isinstance(model, str):
            self.model = model

            model = FFN(model)
            model.Load(verbose=verbose)

        elif isinstance(model, FFN):
            pass

        channel_indices = {
            'S1_an': 0,
            'S2_an': 1,
            'S3_an': 2,
            'S4_an': 3,
            'S5_an': 4,
            'S6_an': 5,
            'S7_in': 6,
            'S8_in': 7,
            'S9_in': 8,
            'satellite_zenith_angle': 9,
            'solar_zenith_angle': 10,
            'latitude_an': 11,
            'longitude_an': 12
        }

        num_inputs = model.para_num
        inputs = self._obj.dp.get_ffn_inputs(num_inputs)
        shuffled_inputs = np.column_stack(
            (inputs[:, :channel_indices[channel_name]],
             np.random.permutation(inputs[:, channel_indices[channel_name]]),
             inputs[:, channel_indices[channel_name] + 1:]))
        output_labels = model.model.predict_label(inputs)
        output_con = model.model.predict(inputs)
        shuffled_output_con = model.model.predict(shuffled_inputs)

        self._obj['Labels'] = pd.Series(output_labels[:, 0],
                                        index=self._obj.index)
        self._obj['Label_Confidence'] = pd.Series(output_con[:, 0],
                                                  index=self._obj.index)
        self._obj['Shuffled_Confidence'] = pd.Series(shuffled_output_con[:, 0],
                                                     index=self._obj.index)

        self._obj = self._obj.dp.make_CTruth_col()

        self._obj['Agree'] = self._obj['CTruth'] != self._obj['Labels']
예제 #11
0
 def setUpClass(self):
     self.model = FFN('Net1_FFN')
     self.model.Load()
     self.TestFile = "./SatelliteData/SLSTR/2018/08/S3A_SL_1_RBT____20180806T081914_20180806T082214_20180807T131253_0179_034_178_1620_LN2_O_NT_003.SEN3"
예제 #12
0
class MaskToggler():
    def __init__(self, Sfilename, model='Net1_FFN_v7', verbose=False):
        self.index = 0
        self.settingfuncs = [self.setting1, self.setting2, self.setting3,
                             self.setting4, self.setting5, self.setting6,
                             self.setting7, self.setting8]
        if isinstance(model, str):
            self.modelname = model

            self.model = FFN(model)
            self.model.Load(verbose=verbose)

        elif isinstance(model, FFN):
            self.model = model
            self.modelname = self.model.name

        mask1, pmask1 = self.model.apply_mask(Sfilename)

        rgb, self.TitleStr = Vis.FalseColour(Sfilename, False)

        scn = DL.scene_loader(Sfilename)
        scn.load(['bayes_in', 'probability_cloud_single_in'])
        bmask = DL.upscale_repeat(scn['bayes_in'].values).astype('int')
        bmask = 1 - ((bmask & 2) / 2)
        bpmask = DL.upscale_repeat(scn['probability_cloud_single_in'].values)

        self.im1 = plt.imshow(rgb)
        plt.title('False colour image\n' + self.TitleStr)

        self.im2 = plt.imshow(mask1, cmap='Blues')
        self.im2.set_visible(False)

        bmask = DL.extract_mask(Sfilename, 'bayes_in', 2)
        self.im3 = plt.imshow(bmask, cmap='Reds')
        self.im3.set_visible(False)

        mask1 = mask1.astype('bool')
        temp = np.copy(rgb)
        temp[~mask1, :] = 254 / 255, 253 / 255, 185 / 255
        self.im4 = plt.imshow(temp)
        self.im4.set_visible(False)

        rgb[mask1, :] = 74 / 255, 117 / 255, 50 / 255
        self.im5 = plt.imshow(rgb)
        self.im5.set_visible(False)

        self.im6 = plt.imshow(1 - pmask1, cmap='Oranges')
        self.im6.set_visible(False)

        self.im7 = plt.imshow(1 - bpmask, cmap='Reds')
        self.im7.set_visible(False)

        maskdiff = bmask - mask1
        self.im8 = plt.imshow(maskdiff, cmap='bwr')
        self.im8.set_visible(False)

        self.cbset = False
        self.cb = None

    def toggle_images(self, event):
        """Toggle between different images to display"""
        if event.key == '1':
            self._clearframe()
            self.setting1()
        elif event.key == '2':
            self._clearframe()
            self.setting2()
        elif event.key == '3':
            self._clearframe()
            self.setting3()
        elif event.key == '4':
            self._clearframe()
            self.setting4()
        elif event.key == '5':
            self._clearframe()
            self.setting5()
        elif event.key == '6':
            self._clearframe()
            self.setting6()
        elif event.key == '7':
            self._clearframe()
            self.setting7()
        elif event.key == '8':
            self._clearframe()
            self.setting8()
        elif event.key == 'm':
            self._clearframe()
            self.cycleforward()
        elif event.key == 'n':
            self._clearframe()
            self.cyclebackward()
        else:
            return

    def _clearframe(self):
        self.im1.set_visible(False)
        self.im2.set_visible(False)
        self.im3.set_visible(False)
        self.im4.set_visible(False)
        self.im5.set_visible(False)
        self.im6.set_visible(False)
        self.im7.set_visible(False)
        self.im8.set_visible(False)
        if self.cbset:
            self.cb.remove()
            self.cbset = False

    def cycleforward(self):
        self.index = (self.index + 1) % 8
        self.settingfuncs[self.index]()

    def cyclebackward(self):
        self.index = (self.index - 1) % 8
        self.settingfuncs[self.index]()

    def setting1(self):
        plt.title('False colour image\n' + self.TitleStr)
        self.im1.set_visible(True)
        self.index = 0
        plt.draw()

    def setting2(self):
        plt.title(self.modelname + ' mask\n' + self.TitleStr)
        self.im2.set_visible(True)
        self.index = 1
        plt.draw()

    def setting3(self):
        plt.title('Bayesian mask\n' + self.TitleStr)
        self.im3.set_visible(True)
        self.index = 2
        plt.draw()

    def setting4(self):
        plt.title(self.modelname
                  + ' masked false colour image\n' + self.TitleStr)
        self.im4.set_visible(True)
        self.index = 3
        plt.draw()

    def setting5(self):
        plt.title(self.modelname
                  + ' reverse masked false colour image\n' + self.TitleStr)
        self.im5.set_visible(True)
        self.index = 4
        plt.draw()

    def setting6(self):
        plt.title(self.modelname + ' raw model output\n' + self.TitleStr)
        self.im6.set_visible(True)
        self.index = 5
        self.cb = plt.colorbar(self.im6)
        self.cbset = True
        plt.draw()

    def setting7(self):
        plt.title('Bayesian mask raw output\n' + self.TitleStr)
        self.im7.set_visible(True)
        self.index = 6
        self.cb = plt.colorbar(self.im7)
        self.cbset = True
        plt.draw()

    def setting8(self):
        plt.title(self.modelname + ' mask - Bayesian mask\n' + self.TitleStr)
        self.im8.set_visible(True)
        self.index = 7
        self.cb = plt.colorbar(self.im8)
        self.cbset = True
        plt.draw()