def loadPredictions(self, path_model):
        print("============== loading model =============")
        model = load_model(path_model, compile=False)
        print("Model", model)
        print("Loading in data: ", self.path_test + 'patches_in.npy')
        test_in = np.load(self.path_test + 'patches_in.npy', mmap_mode='r')
        #		test_label=np.load(self.path_test+'patches_label.npy')
        test_label = np.load(self.path_test + 'patches_label.npy')[:,
                                                                   -1]  # may18

        # add doty

        #	if dataset=='lm':
        ds = LEM()
        dataSource = SARSource()
        ds.addDataSource(dataSource)
        dotys, dotys_sin_cos = ds.getDayOfTheYear()

        def addDoty(input_):

            input_ = [input_, dotys_sin_cos]
            return input_

        dotys_sin_cos = np.expand_dims(dotys_sin_cos,
                                       axis=0)  # add batch dimension
        dotys_sin_cos = np.repeat(dotys_sin_cos, test_in.shape[0], axis=0)

        #test_in = addDoty(test_in)
        # Here do N to 1 prediction for last timestep at first...
        test_predictions = model.predict(test_in)
        print(test_in[0].shape, test_label.shape, test_predictions.shape)
        print("Test predictions dtype", test_predictions.dtype)
        #pdb.set_trace()
        del test_in
        return test_predictions, test_label
Esempio n. 2
0
full_label_test[full_label_test == 255] = class_n

print(full_ims_test.shape)
print(full_label_test.shape)

# add doty
mim = MIMFixed()

data = {'labeled_dates': 12}
data['labeled_dates'] = 12

if dataset == 'lm':
    ds = LEM()
elif dataset == 'l2':
    ds = LEM2()
dataSource = SARSource()
ds.addDataSource(dataSource)

time_delta = ds.getTimeDelta(delta=True, format='days')
ds.setDotyFlag(True)
dotys, dotys_sin_cos = ds.getDayOfTheYear()
ds.dotyReplicateSamples(sample_n=1)

# Reconstruct the image
print("Reconstructing the labes and predictions...")

patch_size = 32
add_padding_flag = False
if add_padding_flag == True:
    full_ims_test, stride, step_row, step_col, overlap = seq_add_padding(
        full_ims_test, patch_size, 0)
    def loadPredictions(self, path_model):
        print("============== loading model =============")
        model = load_model(path_model, compile=False)
        print("Model", model)
        print("Loading in data: ", self.path_test + 'patches_in.npy')
        batch = {}
        batch['in'] = np.load(self.path_test + 'patches_in.npy',
                              mmap_mode='r')  # len is 21
        #		test_label=np.load(self.path_test+'patches_label.npy')
        self.labeled_dates = 12
        batch['label'] = np.load(self.path_test + 'patches_label.npy')
        deb.prints(batch['label'].shape)
        #pdb.set_trace()
        batch['label'] = batch['label'][:, -self.labeled_dates:]  # may18
        deb.prints(batch['in'].shape)
        deb.prints(batch['label'].shape)
        #pdb.set_trace()

        self.mim = MIMVarLabel_PaddedSeq()
        #		self.mim = MIMFixed()

        data = {'labeled_dates': 12}
        data['labeled_dates'] = 12

        #batch = {'in': test_in, 'label': test_label}

        # add doty

        if self.dataset == 'lm':
            ds = LEM()
        elif self.dataset == 'l2':
            ds = LEM2()
        dataSource = SARSource()
        ds.addDataSource(dataSource)

        time_delta = ds.getTimeDelta(delta=True, format='days')
        ds.setDotyFlag(True)
        dotys, dotys_sin_cos = ds.getDayOfTheYear()
        ds.dotyReplicateSamples(sample_n=batch['label'].shape[0])

        prediction_dtype = np.float16

        model_t_len = 12
        batch['shape'] = (batch['in'].shape[0],
                          model_t_len) + batch['in'].shape[2:]

        # get model class n
        model_shape = model.layers[-1].output_shape
        model_class_n = model_shape[-1]
        deb.prints(model_shape)
        deb.prints(model_class_n)
        test_predictions = np.zeros_like(batch['label'][..., :-1],
                                         dtype=prediction_dtype)
        #		test_predictions = np.zeros((batch['label'].shape[:-1])+(model_class_n,), dtype = prediction_dtype)
        deb.prints(test_predictions.shape)

        #pdb.set_trace()
        for t_step in range(data['labeled_dates']):  # 0 to 11
            ###batch_val_label = batch['label'][:, t_step]
            #data.patches['test']['label'] = data.patches['test']['label'][:, label_id]
            ##deb.prints(batch_val_label.shape)
            ##deb.prints(t_step-data['labeled_dates'])
            #pdb.set_trace()
            input_ = self.mim.batchTrainPreprocess(
                batch, ds, label_date_id=t_step -
                data['labeled_dates'])  # tstep is -12 to -1
            deb.prints(input_[0].shape)
            #pdb.set_trace()
            #deb.prints(data.patches['test']['label'].shape)

            test_predictions[:, t_step] = (
                model.predict(input_)).astype(prediction_dtype)
            #pdb.set_trace()
        print(" shapes", test_predictions, batch['label'])

        print(
            "batch['in'][0].shape, batch['label'].shape, test_predictions.shape",
            batch['in'][0].shape, batch['label'].shape, test_predictions.shape)
        print("Test predictions dtype", test_predictions.dtype)
        deb.prints(
            np.unique(test_predictions.argmax(axis=-1), return_counts=True))
        deb.prints(
            np.unique(batch['label'].argmax(axis=-1), return_counts=True))

        #test_predictions = test_predictions.argmax(axis=-1)
        #batch['label'] = batch['label'].argmax(axis=-1)
        print("uniques",
              np.unique(test_predictions.argmax(axis=-1), return_counts=True),
              np.unique(batch['label'].argmax(axis=-1), return_counts=True))

        #test_predictions = self.newLabel2labelTranslate(test_predictions, 'new_labels2labels_lm_20171209_S1.pkl')
        #batch['label'] = self.newLabel2labelTranslate(batch['label'], 'new_labels2labels_l2_20191223_S1.pkl')
        print("uniques",
              np.unique(test_predictions.argmax(axis=-1), return_counts=True),
              np.unique(batch['label'].argmax(axis=-1), return_counts=True))
        #pdb.set_trace()
        del batch['in']
        return test_predictions, batch['label']