text:
import numpy as np
import os
import torch
import torch.optim as optim
from torch.nn import functional as F
from torch.utils.data import DataLoader, TensorDataset, random_split
from torchvision.utils import save_image
from model import SLF_Estimator, SLF_VAE
import sys
sys.path.append('..')
import random
import scipy.io as sio
import cv2

# runner for CNN-attention estimator
class SLF_Estimator_runner():
    def __init__(self, args):
        self.args = args
        cuda_flag = not self.args.no_cuda and torch.cuda.is_available()
        self.device = torch.device("cuda" if cuda_flag else "cpu")
        torch.manual_seed(self.args.seed)
        random.seed(self.args.seed)
        self.path = self.args.run + 'SLF_Estimator/'
        if not os.path.exists(self.path):
            os.makedirs(self.path)

    def get_optimizer(self, parameters):
        if self.args.optimizer == 'Adam':
            return optim.Adam(parameters, lr=self.args.lr, weight_decay=self.args.weight_decay, betas=(0.9, 0.999))
        elif self.args.optimizer == 'RMSProp':
            return optim.RMSprop(parameters, lr=self.args.lr, weight_decay=self.args.weight_decay)
        elif self.args.optimizer == 'SGD':
            return optim.SGD(parameters, lr=self.args.lr, momentum=0.9)
        else:
            raise NotImplementedError('Optimizer {} not understood.'.format(self.args.optimizer))

    def load_data(self, noise_level='all'):
        # Load training data
        training_data = sio.loadmat('./data/training_data.mat')
        # normalized noisy RSS measurement input (shape: [num_sample, NxPxP])
        train_RSS = training_data['RSS']
        RSS_min = np.amin(train_RSS, axis=0, keepdims=True)
        RSS_max = np.amax(train_RSS, axis=0, keepdims=True)
        train_RSS = (train_RSS - RSS_min) / (RSS_max - RSS_min)
        train_RSS = train_RSS.reshape(-1, self.args.N, self.args.P, self.args.P)
        # SLF image (shape: [num_sample, K0, K1])
        train_slf_img = training_data['slf_img']
        train_slf_img = train_slf_img.reshape(-1, 1, self.args.K0, self.args.K1)

        train_RSS = torch.from_numpy(train_RSS).float()
        train_slf_img = torch.from_numpy(train_slf_img).float()

        dataset = TensorDataset(train_RSS, train_slf_img)
        train_size = int(len(dataset) * 0.9)
        val_size = int(len(dataset) * 0.1)
        dataset_train, dataset_val = random_split(dataset, [train_size, val_size],
                                                  generator=torch.Generator().manual_seed(self.args.seed))

        # Load testing data
        testing_data = sio.loadmat('./data/testing_data.mat')
        # noise level classes (shape: [num_sample, 1]) (classes: 0, 1, 2)
        noise_class = testing_data['sig_epsilon_class']
        noise_class = np.squeeze(noise_class)
        idx = (noise_class == 0) | (noise_class == 1) | (noise_class == 2)
        if noise_level == 'all':
            pass
        elif noise_level == 'low':
            idx = (noise_class == 0)
        elif noise_level == 'mid':
            idx = (noise_class == 1)
        elif noise_level == 'high':
            idx = (noise_class == 2)
        else:
            raise NotImplementedError('Noise level {} not understood.'.format(noise_level))
        # normalized noisy RSS measurement input (shape: [num_sample, NxPxP])
        test_RSS = testing_data['RSS'][idx]
        test_RSS = (test_RSS - RSS_min) / (RSS_max - RSS_min)
        test_RSS = test_RSS.reshape(-1, self.args.N, self.args.P, self.args.P)
        # SLF image (shape: [num_sample, K0, K1])
        test_slf_img = testing_data['slf_img'][idx]
        test_slf_img = test_slf_img.reshape(-1, 1, self.args.K0, self.args.K1)

        # SLF image ENR (shape: [num_sample, K0, K1])
        test_slf_img_ENR = testing_data['slf_img_ENR'][idx]
        test_slf_img_ENR = test_slf_img_ENR.reshape(-1, 1, self.args.K0, self.args.K1)

        test_RSS = torch.from_numpy(test_RSS).float()
        test_slf_img = torch.from_numpy(test_slf_img).float()
        test_slf_img_ENR = torch.from_numpy(test_slf_img_ENR).float()

        dataset_test = TensorDataset(test_RSS, test_slf_img_ENR, test_slf_img)

        train_loader = DataLoader(
            dataset_train,
            batch_size=self.args.batch_size, shuffle=True, num_workers=2)
        val_loader = DataLoader(
            dataset_val,
            batch_size=self.args.batch_size, shuffle=True, num_workers=2)
        test_loader = DataLoader(
            dataset_test,
            batch_size=self.args.batch_size, shuffle=False, num_workers=2)
        print("Data Loaded!")
        return train_loader, val_loader, test_loader

    def train(self, model, train_loader, optimizer, epoch):
        model.train()
        train_recon_loss = 0                  # total loss
        train_rmse = 0                  # rmse loss
        for batch_idx,  (input_rss, target_slf) in enumerate(train_loader):
            input_rss = input_rss.to(self.device)
            target_slf = target_slf.to(self.device)
            optimizer.zero_grad()
            slf_recon = model(input_rss)
            loss = F.binary_cross_entropy(slf_recon, target_slf, reduction='sum')/(self.args.K0*self.args.K1)
            train_recon_loss += loss.item()

            mse = F.mse_loss(slf_recon, target_slf, reduction='sum') / (self.args.K0 * self.args.K1)
            train_rmse += mse.item()

            loss /= input_rss.size(0)
            loss.backward()
            optimizer.step()
            if batch_idx % self.args.log_interval == 0:
                print(
                    'Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                        epoch, batch_idx * len(input_rss), len(train_loader.dataset),
                               100. * batch_idx / len(train_loader),
                               loss.item()))

        train_recon_loss /= len(train_loader.dataset)
        train_rmse /= len(train_loader.dataset)
        train_rmse = np.sqrt(train_rmse)
        print('====> Epoch: {} Average loss: {:.6f}'.format(epoch, train_recon_loss))
        print('====> RMSE: {:.6f}'.format(train_rmse))
        return train_recon_loss, train_rmse

    def validate(self, model, val_loader):
        model.eval()
        val_recon_loss = 0           # recon loss
        val_rmse = 0           # rmse
        for batch_idx,  (input_rss, target_slf) in enumerate(val_loader):
            input_rss = input_rss.to(self.device)
            target_slf = target_slf.to(self.device)
            slf_recon = model(input_rss)
            recon_loss = F.binary_cross_entropy(slf_recon, target_slf, reduction='sum')/(self.args.K0*self.args.K1)
            val_recon_loss += recon_loss.item()
            mse = F.mse_loss(slf_recon, target_slf, reduction='sum')/(self.args.K0*self.args.K1)
            val_rmse += mse.item()
        val_recon_loss /= len(val_loader.dataset)
        val_rmse /= len(val_loader.dataset)
        val_rmse = np.sqrt(val_rmse)
        print('====> Loss: {:.6f}'.format(val_recon_loss))
        print('====> RMSE: {:.6f}'.format(val_rmse))
        return val_recon_loss, val_rmse

    def test(self, model, test_loader):
        model.eval()
        test_recon_loss = 0  # recon loss
        test_rmse1 = 0  # rmse for ENR
        test_rmse2 = 0  # rmse for our model
        for batch_idx, (input_rss, slf_enr, target_slf) in enumerate(test_loader):
            input_rss = input_rss.to(self.device)
            slf_enr = slf_enr.to(self.device)
            target_slf = target_slf.to(self.device)
            slf_recon = model(input_rss)
            recon_loss = F.binary_cross_entropy(slf_recon, target_slf, reduction='sum')/(self.args.K0*self.args.K1)
            test_recon_loss += recon_loss.item()
            mse1 = F.mse_loss(slf_enr, target_slf, reduction='sum') / (self.args.K0 * self.args.K1)
            mse2 = F.mse_loss(slf_recon, target_slf, reduction='sum') / (self.args.K0 * self.args.K1)
            test_rmse1 += mse1.item()
            test_rmse2 += mse2.item()
        test_recon_loss /= len(test_loader.dataset)
        test_rmse1 /= len(test_loader.dataset)
        test_rmse1 = np.sqrt(test_rmse1)
        test_rmse2 /= len(test_loader.dataset)
        test_rmse2 = np.sqrt(test_rmse2)
        print('====> Loss: {:.6f}'.format(test_recon_loss))
        print('====> RMSE for ENR: {:.6f}'.format(test_rmse1))
        print('====> RMSE for our model: {:.6f}'.format(test_rmse2))
        return test_recon_loss, test_rmse1, test_rmse2

    def train_save(self):
        model = SLF_Estimator(M=self.args.M, P=self.args.P, K=(self.args.K0, self.args.K1))
        model = model.to(self.device)
        train_loader, val_loader, test_loader = self.load_data()
        optimizer = self.get_optimizer(model.parameters())
        scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', factor=0.2, patience=8, min_lr=1e-5)
        train_set = []
        val_set = []
        test_set = []
        train_loss_path = self.path + 'train_loss_' + str(self.args.n_epochs) + '.npy'
        val_loss_path = self.path + 'val_loss_' + str(self.args.n_epochs) + '.npy'
        test_loss_path = self.path + 'test_loss_' + str(self.args.n_epochs) + '.npy'
        model_path = self.path + 'model_' + str(self.args.n_epochs) + '.pth'
        current_val_slf_loss = np.inf
        for epoch in range(1, self.args.n_epochs+1):
            print("Epoch %d learning rate：%f" % (epoch, optimizer.param_groups[0]['lr']))
            train_recon_loss, train_rmse = self.train(model, train_loader, optimizer, epoch)
            train_set.append([train_recon_loss, train_rmse])
            print('====> Validation Loss for Epoch {:d}'.format(epoch))
            val_recon_loss, val_rmse = self.validate(model, val_loader)
            val_set.append([val_recon_loss, val_rmse])
            scheduler.step(val_recon_loss)
            print('====> Test Loss for Epoch {:d}'.format(epoch))
            test_recon_loss, test_rmse1, test_rmse2 = self.test(model, test_loader)
            test_set.append([test_recon_loss, test_rmse1, test_rmse2])
            if epoch % self.args.save_freq == 0:
                ckpt_path = self.path + '/model_' + str(epoch) + '.pth'
                torch.save(model.state_dict(), ckpt_path)
                print('checkpoint{}.pth saved!'.format(epoch))

            if val_rmse < current_val_slf_loss:
                torch.save(model.state_dict(), model_path)
                current_val_slf_loss = val_rmse

        train_loss = np.asarray(train_set).reshape(-1, 2)
        val_loss = np.asarray(val_set).reshape(-1, 2)
        test_loss = np.asarray(test_set).reshape(-1, 3)
        np.save(train_loss_path, train_loss)
        np.save(val_loss_path, val_loss)
        np.save(test_loss_path, test_loss)

    def test_model(self, noise_level='all'):
        model = SLF_Estimator(M=self.args.M, P=self.args.P, K=(self.args.K0, self.args.K1))
        model = model.to(self.device)
        model_path = self.path + 'model_' + str(self.args.n_epochs) + '.pth'
        model.load_state_dict(torch.load(model_path, map_location=self.device))
        model.eval()
        *_, test_loader = self.load_data(noise_level)
        # test model
        slf_rmse1 = 0        # rmse for stage 1
        slf_mae1 = 0         # mae for stage 1
        slf_rmse2 = 0        # rmse for stage 2
        slf_mae2 = 0         # mae for stage 2
        for batch_idx, (input_rss, slf_enr, target_slf) in enumerate(test_loader):
            input_rss = input_rss.to(self.device)
            slf_enr = slf_enr.to(self.device)
            target_slf = target_slf.to(self.device)
            slf_recon = model(input_rss)
            # plot SLF reconstruction image for image1
            n = min(input_rss.size(0), 8)
            comparison = torch.cat([target_slf.view(target_slf.size(0), 1, self.args.K0, self.args.K1)[:n],
                                    slf_enr.view(slf_enr.size(0), 1, self.args.K0, self.args.K1)[:n],
                                    slf_recon.view(slf_recon.size(0), 1, self.args.K0, self.args.K1)[:n]])
            save_image(comparison.cpu(),
                       self.path + 'reconSLF_' + noise_level + '_' + str(batch_idx) + '.png',
                       nrow=n)
            # test SLF image RMSE
            slf_rmse1 += F.mse_loss(slf_enr, target_slf, reduction='sum').item() / (self.args.K0 * self.args.K1)
            slf_rmse2 += F.mse_loss(slf_recon, target_slf, reduction='sum').item() / (self.args.K0 * self.args.K1)
            # test SLF image MAE
            slf_mae1 += F.l1_loss(slf_enr, target_slf, reduction='sum').item() / (self.args.K0 * self.args.K1)
            slf_mae2 += F.l1_loss(slf_recon, target_slf, reduction='sum').item() / (self.args.K0 * self.args.K1)

        slf_rmse1 /= len(test_loader.dataset)
        slf_rmse1 = np.sqrt(slf_rmse1)
        slf_rmse2 /= len(test_loader.dataset)
        slf_rmse2 = np.sqrt(slf_rmse2)
        slf_mae1 /= len(test_loader.dataset)
        slf_mae2 /= len(test_loader.dataset)
        print('====> Test Model:')
        print('====> Noise Level: ' + noise_level)
        print('====> ENR SLF rmse: {:.6f}'.format(slf_rmse1))
        print('====> Recon SLF rmse: {:.6f}'.format(slf_rmse2))
        print('====> ENR SLF mae: {:.6f}'.format(slf_mae1))
        print('====> Recon SLF mae: {:.6f}'.format(slf_mae2))

    def load_data_metrics(self, noise_level='all'):
        # Load training data
        training_data = sio.loadmat('./data/training_data.mat')
        # normalized noisy RSS measurement input (shape: [num_sample, NxPxP])
        train_RSS = training_data['RSS']
        RSS_min = np.amin(train_RSS, axis=0, keepdims=True)
        RSS_max = np.amax(train_RSS, axis=0, keepdims=True)

        # Load testing data for statistical results simulation (ROC and mAP)
        testing_data = sio.loadmat('./data/testing_samples1.mat') # sio.loadmat('./data/testing_data_YOLO/mat_file2/testing_YOLO_data.mat')
        # noise level classes (shape: [num_sample, 1]) (classes: 0, 1, 2)
        noise_class = testing_data['sig_epsilon_class']
        noise_class = np.squeeze(noise_class)
        idx = (noise_class == 0) | (noise_class == 1) | (noise_class == 2)
        if noise_level == 'all':
            pass
        elif noise_level == 'low':
            idx = (noise_class == 0)
        elif noise_level == 'mid':
            idx = (noise_class == 1)
        elif noise_level == 'high':
            idx = (noise_class == 2)
        else:
            raise NotImplementedError('Noise level {} not understood.'.format(noise_level))
        # normalized noisy RSS measurement input (shape: [num_sample, NxPxP])
        test_RSS = testing_data['RSS'][idx]
        test_RSS = (test_RSS - RSS_min) / (RSS_max - RSS_min)
        test_RSS = test_RSS.reshape(-1, self.args.N, self.args.P, self.args.P)
        # SLF image (shape: [num_sample, K0, K1])
        test_slf_img = testing_data['slf_img'][idx]
        test_slf_img = test_slf_img.reshape(-1, 1, self.args.K0, self.args.K1)

        # SLF image ENR (shape: [num_sample, K0, K1])
        test_slf_img_ENR = testing_data['slf_img_ENR'][idx]
        test_slf_img_ENR = test_slf_img_ENR.reshape(-1, 1, self.args.K0, self.args.K1)

        test_RSS = torch.from_numpy(test_RSS).float()
        test_slf_img = torch.from_numpy(test_slf_img).float()
        test_slf_img_ENR = torch.from_numpy(test_slf_img_ENR).float()

        dataset_test = TensorDataset(test_RSS, test_slf_img_ENR, test_slf_img)

        test_loader = DataLoader(
            dataset_test,
            batch_size=self.args.batch_size, shuffle=False, num_workers=2)
        print("Data Loaded for testing metrics!")
        return test_loader

    def test_model_ROC(self, noise_level='all'):
        model = SLF_Estimator(M=self.args.M, P=self.args.P, K=(self.args.K0, self.args.K1))
        model = model.to(self.device)
        model_path = self.path + 'model_' + str(self.args.n_epochs) + '.pth'
        model.load_state_dict(torch.load(model_path, map_location=self.device))
        model.eval()
        test_loader = self.load_data_metrics(noise_level)
        # test model
        slf_rmse_model = 0
        slf_rmse_enr = 0
        slf_mae_model = 0
        slf_mae_enr = 0
        labels = list()
        preds_model = list()
        preds_enr = list()
        for batch_idx, (input_rss, slf_enr, target_slf) in enumerate(test_loader):
            input_rss = input_rss.to(self.device)
            slf_enr = slf_enr.to(self.device)
            target_slf = target_slf.to(self.device)
            slf_recon = model(input_rss)
            # test SLF image RMSE
            slf_rmse_model += F.mse_loss(slf_recon, target_slf, reduction='sum').item() / (self.args.K0 * self.args.K1)
            slf_rmse_enr += F.mse_loss(slf_enr, target_slf, reduction='sum').item() / (self.args.K0 * self.args.K1)
            # test SLF image MAE
            slf_mae_model += F.l1_loss(slf_recon, target_slf, reduction='sum').item() / (self.args.K0 * self.args.K1)
            slf_mae_enr += F.l1_loss(slf_enr, target_slf, reduction='sum').item() / (self.args.K0 * self.args.K1)
            labels.append(target_slf.detach().cpu().numpy())
            preds_model.append(slf_recon.detach().cpu().numpy())
            preds_enr.append(slf_enr.detach().cpu().numpy())

        slf_rmse_model /= len(test_loader.dataset)
        slf_rmse_model = np.sqrt(slf_rmse_model)
        slf_mae_model /= len(test_loader.dataset)
        slf_rmse_enr /= len(test_loader.dataset)
        slf_rmse_enr = np.sqrt(slf_rmse_enr)
        slf_mae_enr /= len(test_loader.dataset)
        print('====> Test Model:')
        print('====> Noise Level: ' + noise_level)
        print('====> Recon SLF rmse: {:.6f}'.format(slf_rmse_model))
        print('====> ENR SLF rmse: {:.6f}'.format(slf_rmse_enr))
        print('====> Recon SLF mae: {:.6f}'.format(slf_mae_model))
        print('====> ENR SLF mae: {:.6f}'.format(slf_mae_enr))

        ## revise part for manual ploting roc curve
        labels = np.concatenate(labels, axis=0)
        np.save('results/compare/ROC_data/labels_' + noise_level + '.npy', labels)
        preds_model = np.concatenate(preds_model, axis=0)
        np.save('results/compare/ROC_data/slf_estimator_preds_'+noise_level+'.npy', preds_model)
        preds_enr = np.concatenate(preds_enr, axis=0)
        np.save('results/compare/ROC_data/enr_preds_' + noise_level + '.npy', preds_enr)

        # # code for auto ploting roc curve
        # labels = np.concatenate(labels, axis=0).reshape(-1)
        # preds1 = np.concatenate(preds1, axis=0).reshape(-1)
        # preds2 = np.concatenate(preds2, axis=0).reshape(-1)
        # labels[labels > 0] = 1
        # save_path1 = self.args.run + 'ENR/' + 'roc_' + noise_level + '.npz'
        # save_path2 = self.path + 'roc_' + noise_level + '.npz'
        # fpr, tpr, thresholds = roc_curve(labels, preds1, pos_label=1)
        # np.savez(save_path1, fpr=fpr, tpr=tpr, thresholds=thresholds)
        # fpr, tpr, thresholds = roc_curve(labels, preds2, pos_label=1)
        # np.savez(save_path2, fpr=fpr, tpr=tpr, thresholds=thresholds)

    def estimate(self, noise_level='low'):
        if noise_level != 'low' and noise_level != 'mid' and noise_level != 'high' :
            raise NotImplementedError('Noise level {} not understood.'.format(noise_level))
        model = SLF_Estimator(M=self.args.M, P=self.args.P, K=(self.args.K0, self.args.K1))
        model = model.to(self.device)
        model_path = self.path + 'model_' + str(self.args.n_epochs) + '.pth'
        model.load_state_dict(torch.load(model_path, map_location=self.device))
        model.eval()
        test_loader = self.load_data_metrics(noise_level)
        path_clean = 'results/compare/YOLO/Origin/images/clean/'+noise_level+'/'
        path_enr = 'results/compare/YOLO/Origin/images/enr/' + noise_level + '/'
        path_model = 'results/compare/YOLO/Origin/images/estimator/' + noise_level + '/'
        if not os.path.exists(path_clean):
            os.makedirs(path_clean)
        if not os.path.exists(path_enr):
            os.makedirs(path_enr)
        if not os.path.exists(path_model):
            os.makedirs(path_model)
        print('====> Test Model with estimation:')
        print('====> Noise Level: ' + noise_level)
        thr = 0.4
        # test model
        for batch_idx, (input_rss, slf_enr, target_slf) in enumerate(test_loader):
            input_rss = input_rss.to(self.device)
            slf_enr = slf_enr.to(self.device)
            target_slf = target_slf.to(self.device)
            slf_recon = model(input_rss)
            target_slf = target_slf.detach().cpu().numpy()
            slf_enr = slf_enr.detach().cpu().numpy()
            slf_recon = slf_recon.detach().cpu().numpy()
            for idx in range(len(target_slf)):
                clean_img = target_slf[idx, 0, :, :]
                enr_img = slf_enr[idx, 0, :, :]
                # enr_img[enr_img > thr] = 1
                # enr_img[enr_img <= thr] = 0
                estimator_img = slf_recon[idx, 0, :, :]
                # estimator_img[estimator_img > thr] = 1
                # estimator_img[estimator_img <= thr] = 0
                clean_img = cv2.resize(clean_img, (320, 320), interpolation=cv2.INTER_NEAREST)*255
                enr_img = cv2.resize(enr_img, (320, 320), interpolation=cv2.INTER_NEAREST)*255
                estimator_img = cv2.resize(estimator_img, (320, 320), interpolation=cv2.INTER_NEAREST)*255
                cv2.imwrite(path_clean + 'im'+str(idx+batch_idx*self.args.batch_size+1) + '.jpg', clean_img)
                cv2.imwrite(path_enr + 'im' + str(idx+batch_idx*self.args.batch_size+1) +'.jpg', enr_img)
                cv2.imwrite(path_model + 'im' + str(idx+batch_idx*self.args.batch_size+1) +'.jpg', estimator_img)
            # image = cv2.imread(path_enr + 'im' + str(1) +'.jpg', cv2.IMREAD_GRAYSCALE)
            # print(np.amax(image))
            # print(np.shape(image))
            # break

# runner for SLF-VAE
class SLF_VAE_runner():
    def __init__(self, args):
        self.args = args
        cuda_flag = not self.args.no_cuda and torch.cuda.is_available()
        self.device = torch.device("cuda" if cuda_flag else "cpu")
        torch.manual_seed(self.args.seed)
        random.seed(self.args.seed)
        self.path = self.args.run + 'SLF_VAE/'
        if not os.path.exists(self.path):
            os.makedirs(self.path)

    def get_optimizer(self, parameters):
        if self.args.optimizer == 'Adam':
            return optim.Adam(parameters, lr=self.args.lr, weight_decay=self.args.weight_decay, betas=(0.9, 0.999))
        elif self.args.optimizer == 'RMSProp':
            return optim.RMSprop(parameters, lr=self.args.lr, weight_decay=self.args.weight_decay)
        elif self.args.optimizer == 'SGD':
            return optim.SGD(parameters, lr=self.args.lr, momentum=0.9)
        else:
            raise NotImplementedError('Optimizer {} not understood.'.format(self.args.optimizer))

    def load_data(self, noise_level='all'):
        # Load training data
        training_data = sio.loadmat('./data/training_data.mat')
        # normalized noisy RSS measurement input (shape: [num_sample, NxPxP])
        train_RSS = training_data['RSS']
        RSS_min = np.amin(train_RSS, axis=0, keepdims=True)
        RSS_max = np.amax(train_RSS, axis=0, keepdims=True)
        train_RSS = (train_RSS - RSS_min) / (RSS_max - RSS_min)
        train_RSS = train_RSS.reshape(-1, self.args.N, self.args.P, self.args.P)
        # SLF image (shape: [num_sample, K0, K1])
        train_slf_img = training_data['slf_img']
        train_slf_img = train_slf_img.reshape(-1, 1, self.args.K0, self.args.K1)

        # SLF image ENR (shape: [num_sample, K0, K1])
        train_slf_img_ENR = training_data['slf_img_ENR']
        train_slf_img_ENR = train_slf_img_ENR.reshape(-1, 1, self.args.K0, self.args.K1)

        train_RSS = torch.from_numpy(train_RSS).float()
        train_slf_img = torch.from_numpy(train_slf_img).float()
        train_slf_img_ENR = torch.from_numpy(train_slf_img_ENR).float()

        dataset = TensorDataset(train_RSS, train_slf_img_ENR, train_slf_img)
        train_size = int(len(dataset) * 0.9)
        val_size = int(len(dataset) * 0.1)
        dataset_train, dataset_val = random_split(dataset, [train_size, val_size],
                                                  generator=torch.Generator().manual_seed(self.args.seed))

        # Load testing data
        testing_data = sio.loadmat('./data/testing_data.mat')
        # noise level classes (shape: [num_sample, 1]) (classes: 0, 1, 2)
        noise_class = testing_data['sig_epsilon_class']
        noise_class = np.squeeze(noise_class)
        idx = (noise_class == 0) | (noise_class == 1) | (noise_class == 2)
        if noise_level == 'all':
            pass
        elif noise_level == 'low':
            idx = (noise_class == 0)
        elif noise_level == 'mid':
            idx = (noise_class == 1)
        elif noise_level == 'high':
            idx = (noise_class == 2)
        else:
            raise NotImplementedError('Noise level {} not understood.'.format(noise_level))
        # normalized noisy RSS measurement input (shape: [num_sample, NxPxP])
        test_RSS = testing_data['RSS'][idx]
        test_RSS = (test_RSS - RSS_min) / (RSS_max - RSS_min)
        test_RSS = test_RSS.reshape(-1, self.args.N, self.args.P, self.args.P)
        # SLF image (shape: [num_sample, K0, K1])
        test_slf_img = testing_data['slf_img'][idx]
        test_slf_img = test_slf_img.reshape(-1, 1, self.args.K0, self.args.K1)

        # SLF image ENR (shape: [num_sample, K0, K1])
        test_slf_img_ENR = testing_data['slf_img_ENR'][idx]
        test_slf_img_ENR = test_slf_img_ENR.reshape(-1, 1, self.args.K0, self.args.K1)

        test_RSS = torch.from_numpy(test_RSS).float()
        test_slf_img = torch.from_numpy(test_slf_img).float()
        test_slf_img_ENR = torch.from_numpy(test_slf_img_ENR).float()

        dataset_test = TensorDataset(test_RSS, test_slf_img_ENR, test_slf_img)

        train_loader = DataLoader(
            dataset_train,
            batch_size=self.args.batch_size, shuffle=True, num_workers=2)
        val_loader = DataLoader(
            dataset_val,
            batch_size=self.args.batch_size, shuffle=True, num_workers=2)
        test_loader = DataLoader(
            dataset_test,
            batch_size=self.args.batch_size, shuffle=False, num_workers=2)
        print("Data Loaded!")
        return train_loader, val_loader, test_loader

    def train(self, model, train_loader, optimizer, epoch):
        model.train()
        train_loss = 0                  # total loss
        train_recon_loss = 0            # recon loss
        train_kld = 0                   # kld loss
        train_rmse1 = 0                 # rmse for enr
        train_rmse2 = 0                 # rmse for our model
        for batch_idx,  (input_rss, slf_coarse, target_slf) in enumerate(train_loader):
            input_rss = input_rss.to(self.device)
            slf_coarse = slf_coarse.to(self.device)
            target_slf = target_slf.to(self.device)
            optimizer.zero_grad()
            mu, logvar, slf_recon = model.train_model(input_rss, target_slf, slf_coarse)
            recon_loss, kld = model.train_loss(mu, logvar, slf_recon, target_slf)
            loss = self.args.lambda0 * kld + self.args.lambda1 * recon_loss
            train_loss += loss.item()
            train_recon_loss += recon_loss.item()
            train_kld += kld.item()

            mse1 = F.mse_loss(slf_coarse, target_slf, reduction='sum')/(self.args.K0*self.args.K1)
            mse2 = F.mse_loss(slf_recon, target_slf, reduction='sum') / (self.args.K0 * self.args.K1)
            train_rmse1 += mse1.item()
            train_rmse2 += mse2.item()

            loss /= input_rss.size(0)
            loss.backward()
            optimizer.step()
            if batch_idx % self.args.log_interval == 0:
                print(
                    'Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\tRecon_Loss: {:.6f}\tKLD: {:.6f}'.format(
                        epoch, batch_idx * len(input_rss), len(train_loader.dataset),
                               100. * batch_idx / len(train_loader),
                               loss.item(), recon_loss.item() / len(input_rss),
                               kld.item() / len(input_rss)))

        train_loss /= len(train_loader.dataset)
        train_recon_loss /= len(train_loader.dataset)
        train_kld /= len(train_loader.dataset)
        train_rmse1 /= len(train_loader.dataset)
        train_rmse1 = np.sqrt(train_rmse1)
        train_rmse2 /= len(train_loader.dataset)
        train_rmse2 = np.sqrt(train_rmse2)
        print('====> Epoch: {} Average loss: {:.6f}'.format(epoch, train_loss))
        print('====> Recon loss: {:.6f}'.format(train_recon_loss))
        print('====> KLD: {:.6f}'.format(train_kld))
        print('====> RMSE for ENR: {:.6f}'.format(train_rmse1))
        print('====> RMSE for our model: {:.6f}'.format(train_rmse2))
        return train_loss, train_kld, train_recon_loss, train_rmse1, train_rmse2

    def validate(self, model, val_loader):
        model.eval()
        val_recon_loss = 0           # recon loss
        val_rmse1 = 0                # rmse for ENR
        val_rmse2 = 0                # rmse for our model
        for batch_idx,  (input_rss, slf_coarse, target_slf) in enumerate(val_loader):
            input_rss = input_rss.to(self.device)
            slf_coarse = slf_coarse.to(self.device)
            target_slf = target_slf.to(self.device)
            slf_recon = model.test_model(input_rss, slf_coarse)
            recon_loss = model.test_loss(slf_recon, target_slf)
            val_recon_loss += recon_loss.item()
            mse1 = F.mse_loss(slf_coarse, target_slf, reduction='sum')/(self.args.K0*self.args.K1)
            mse2 = F.mse_loss(slf_recon, target_slf, reduction='sum')/(self.args.K0*self.args.K1)
            val_rmse1 += mse1.item()
            val_rmse2 += mse2.item()
        val_recon_loss /= len(val_loader.dataset)
        val_rmse1 /= len(val_loader.dataset)
        val_rmse1 = np.sqrt(val_rmse1)
        val_rmse2 /= len(val_loader.dataset)
        val_rmse2 = np.sqrt(val_rmse2)
        print('====> Recon loss: {:.6f}'.format(val_recon_loss))
        print('====> RMSE for ENR: {:.6f}'.format(val_rmse1))
        print('====> RMSE for our model: {:.6f}'.format(val_rmse2))
        return val_recon_loss, val_rmse1, val_rmse2

    def test(self, model, test_loader):
        model.eval()
        test_recon_loss = 0  # recon loss
        test_rmse1 = 0  # rmse for ENR
        test_rmse2 = 0  # rmse for our model
        for batch_idx, (input_rss, slf_coarse, target_slf) in enumerate(test_loader):
            input_rss = input_rss.to(self.device)
            slf_coarse = slf_coarse.to(self.device)
            target_slf = target_slf.to(self.device)
            slf_recon = model.test_model(input_rss, slf_coarse)
            recon_loss = model.test_loss(slf_recon, target_slf)
            test_recon_loss += recon_loss.item()
            mse1 = F.mse_loss(slf_coarse, target_slf, reduction='sum') / (self.args.K0 * self.args.K1)
            mse2 = F.mse_loss(slf_recon, target_slf, reduction='sum') / (self.args.K0 * self.args.K1)
            test_rmse1 += mse1.item()
            test_rmse2 += mse2.item()
        test_recon_loss /= len(test_loader.dataset)
        test_rmse1 /= len(test_loader.dataset)
        test_rmse1 = np.sqrt(test_rmse1)
        test_rmse2 /= len(test_loader.dataset)
        test_rmse2 = np.sqrt(test_rmse2)
        print('====> Recon loss: {:.6f}'.format(test_recon_loss))
        print('====> RMSE for ENR: {:.6f}'.format(test_rmse1))
        print('====> RMSE for our model: {:.6f}'.format(test_rmse2))
        return test_recon_loss, test_rmse1, test_rmse2

    def train_save(self):
        model = SLF_VAE(M=self.args.M, P=self.args.P, K=(self.args.K0, self.args.K1))
        model = model.to(self.device)
        train_loader, val_loader, test_loader = self.load_data()
        optimizer = self.get_optimizer(model.parameters())
        scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', factor=0.2, patience=8, min_lr=1e-5)
        train_set = []
        val_set = []
        test_set = []
        train_loss_path = self.path + 'train_loss_' + str(self.args.n_epochs) + '.npy'
        val_loss_path = self.path + 'val_loss_' + str(self.args.n_epochs) + '.npy'
        test_loss_path = self.path + 'test_loss_' + str(self.args.n_epochs) + '.npy'
        model_path = self.path + 'model_' + str(self.args.n_epochs) + '.pth'
        current_val_slf_loss = np.inf
        for epoch in range(1, self.args.n_epochs+1):
            print("Epoch %d learning rate：%f" % (epoch, optimizer.param_groups[0]['lr']))
            train_loss, train_kld, train_recon_loss, train_rmse1, train_rmse2 = self.train(model, train_loader,
                                                                                optimizer, epoch)
            train_set.append([train_loss, train_kld, train_recon_loss, train_rmse1, train_rmse2])
            print('====> Validation Loss for Epoch {:d}'.format(epoch))
            val_recon_loss, val_rmse1, val_rmse2 = self.validate(model, val_loader)
            val_set.append([val_recon_loss, val_rmse1, val_rmse2])
            scheduler.step(val_recon_loss)
            print('====> Test Loss for Epoch {:d}'.format(epoch))
            test_recon_loss, test_rmse1, test_rmse2 = self.test(model, test_loader)
            test_set.append([test_recon_loss, test_rmse1, test_rmse2])
            if epoch % self.args.save_freq == 0:
                ckpt_path = self.path + '/model_' + str(epoch) + '.pth'
                torch.save(model.state_dict(), ckpt_path)
                print('checkpoint{}.pth saved!'.format(epoch))

            if val_rmse2 < current_val_slf_loss:
                torch.save(model.state_dict(), model_path)
                current_val_slf_loss = val_rmse2

        train_loss = np.asarray(train_set).reshape(-1, 5)
        val_loss = np.asarray(val_set).reshape(-1, 3)
        test_loss = np.asarray(test_set).reshape(-1, 3)
        np.save(train_loss_path, train_loss)
        np.save(val_loss_path, val_loss)
        np.save(test_loss_path, test_loss)

    def test_model(self, noise_level='all'):
        model = SLF_VAE(M=self.args.M, P=self.args.P, K=(self.args.K0, self.args.K1))
        model = model.to(self.device)
        model_path = self.path + 'model_' + str(self.args.n_epochs) + '.pth'
        model.load_state_dict(torch.load(model_path, map_location=self.device))
        model.eval()
        *_, test_loader = self.load_data(noise_level)
        # test model
        slf_rmse1 = 0        # rmse for stage 1
        slf_mae1 = 0         # mae for stage 1
        slf_rmse2 = 0        # rmse for stage 2
        slf_mae2 = 0         # mae for stage 2
        for batch_idx, (input_rss, slf_coarse, target_slf) in enumerate(test_loader):
            input_rss = input_rss.to(self.device)
            slf_coarse = slf_coarse.to(self.device)
            target_slf = target_slf.to(self.device)
            slf_recon = model.test_model(input_rss, slf_coarse)
            # plot SLF reconstruction image for image1
            n = min(input_rss.size(0), 8)
            comparison = torch.cat([target_slf.view(target_slf.size(0), 1, self.args.K0, self.args.K1)[:n],
                                    slf_coarse.view(slf_coarse.size(0), 1, self.args.K0, self.args.K1)[:n],
                                    slf_recon.view(slf_recon.size(0), 1, self.args.K0, self.args.K1)[:n]])
            save_image(comparison.cpu(),
                       self.path + 'reconSLF_' + noise_level + '_' + str(batch_idx) + '.png',
                       nrow=n)
            # test SLF image RMSE
            slf_rmse1 += F.mse_loss(slf_coarse, target_slf, reduction='sum').item() / (self.args.K0 * self.args.K1)
            slf_rmse2 += F.mse_loss(slf_recon, target_slf, reduction='sum').item() / (self.args.K0 * self.args.K1)
            # test SLF image MAE
            slf_mae1 += F.l1_loss(slf_coarse, target_slf, reduction='sum').item() / (self.args.K0 * self.args.K1)
            slf_mae2 += F.l1_loss(slf_recon, target_slf, reduction='sum').item() / (self.args.K0 * self.args.K1)

        slf_rmse1 /= len(test_loader.dataset)
        slf_rmse1 = np.sqrt(slf_rmse1)
        slf_rmse2 /= len(test_loader.dataset)
        slf_rmse2 = np.sqrt(slf_rmse2)
        slf_mae1 /= len(test_loader.dataset)
        slf_mae2 /= len(test_loader.dataset)
        print('====> Test Model:')
        print('====> Noise Level: ' + noise_level)
        print('====> ENR SLF rmse: {:.6f}'.format(slf_rmse1))
        print('====> Recon SLF rmse: {:.6f}'.format(slf_rmse2))
        print('====> ENR SLF mae: {:.6f}'.format(slf_mae1))
        print('====> Recon SLF mae: {:.6f}'.format(slf_mae2))

    def load_data_metrics(self, noise_level='all'):
        # Load training data
        training_data = sio.loadmat('./data/training_data.mat')
        # normalized noisy RSS measurement input (shape: [num_sample, NxPxP])
        train_RSS = training_data['RSS']
        RSS_min = np.amin(train_RSS, axis=0, keepdims=True)
        RSS_max = np.amax(train_RSS, axis=0, keepdims=True)

        # Load testing data for statistical results simulation (ROC and mAP)
        testing_data = sio.loadmat(
            './data/testing_samples1.mat')  # sio.loadmat('./data/testing_data_YOLO/mat_file2/testing_YOLO_data.mat')
        # noise level classes (shape: [num_sample, 1]) (classes: 0, 1, 2)
        noise_class = testing_data['sig_epsilon_class']
        noise_class = np.squeeze(noise_class)
        idx = (noise_class == 0) | (noise_class == 1) | (noise_class == 2)
        if noise_level == 'all':
            pass
        elif noise_level == 'low':
            idx = (noise_class == 0)
        elif noise_level == 'mid':
            idx = (noise_class == 1)
        elif noise_level == 'high':
            idx = (noise_class == 2)
        else:
            raise NotImplementedError('Noise level {} not understood.'.format(noise_level))
        # normalized noisy RSS measurement input (shape: [num_sample, NxPxP])
        test_RSS = testing_data['RSS'][idx]
        test_RSS = (test_RSS - RSS_min) / (RSS_max - RSS_min)
        test_RSS = test_RSS.reshape(-1, self.args.N, self.args.P, self.args.P)
        # SLF image (shape: [num_sample, K0, K1])
        test_slf_img = testing_data['slf_img'][idx]
        test_slf_img = test_slf_img.reshape(-1, 1, self.args.K0, self.args.K1)

        # SLF image ENR (shape: [num_sample, K0, K1])
        test_slf_img_ENR = testing_data['slf_img_ENR'][idx]
        test_slf_img_ENR = test_slf_img_ENR.reshape(-1, 1, self.args.K0, self.args.K1)

        test_RSS = torch.from_numpy(test_RSS).float()
        test_slf_img = torch.from_numpy(test_slf_img).float()
        test_slf_img_ENR = torch.from_numpy(test_slf_img_ENR).float()

        dataset_test = TensorDataset(test_RSS, test_slf_img_ENR, test_slf_img)

        test_loader = DataLoader(
            dataset_test,
            batch_size=self.args.batch_size, shuffle=False, num_workers=2)
        print("Data Loaded for testing metrics!")
        return test_loader

    def test_model_ROC(self, noise_level='all'):
        model = SLF_VAE(M=self.args.M, P=self.args.P, K=(self.args.K0, self.args.K1))
        model = model.to(self.device)
        model_path = self.path + 'model_' + str(self.args.n_epochs) + '.pth'
        model.load_state_dict(torch.load(model_path, map_location=self.device))
        model.eval()
        test_loader = self.load_data_metrics(noise_level)
        # test model
        slf_rmse1 = 0        # rmse for stage 1
        slf_mae1 = 0         # mae for stage 1
        slf_rmse2 = 0        # rmse for stage 2
        slf_mae2 = 0         # mae for stage 2
        labels = list()
        preds1 = list()      # for ENR
        preds2 = list()      # for SLF_VAE
        for batch_idx, (input_rss, slf_coarse, target_slf) in enumerate(test_loader):
            input_rss = input_rss.to(self.device)
            slf_coarse = slf_coarse.to(self.device)
            target_slf = target_slf.to(self.device)
            slf_recon = model.test_model(input_rss, slf_coarse)
            # test SLF image RMSE
            slf_rmse1 += F.mse_loss(slf_coarse, target_slf, reduction='sum').item() / (self.args.K0 * self.args.K1)
            slf_rmse2 += F.mse_loss(slf_recon, target_slf, reduction='sum').item() / (self.args.K0 * self.args.K1)
            # test SLF image MAE
            slf_mae1 += F.l1_loss(slf_coarse, target_slf, reduction='sum').item() / (self.args.K0 * self.args.K1)
            slf_mae2 += F.l1_loss(slf_recon, target_slf, reduction='sum').item() / (self.args.K0 * self.args.K1)
            labels.append(target_slf.detach().cpu().numpy())
            preds1.append(slf_coarse.detach().cpu().numpy())
            preds2.append(slf_recon.detach().cpu().numpy())

        slf_rmse1 /= len(test_loader.dataset)
        slf_rmse1 = np.sqrt(slf_rmse1)
        slf_rmse2 /= len(test_loader.dataset)
        slf_rmse2 = np.sqrt(slf_rmse2)
        slf_mae1 /= len(test_loader.dataset)
        slf_mae2 /= len(test_loader.dataset)
        print('====> Test Model:')
        print('====> Noise Level: ' + noise_level)
        print('====> ENR SLF rmse: {:.6f}'.format(slf_rmse1))
        print('====> Recon SLF rmse: {:.6f}'.format(slf_rmse2))
        print('====> ENR SLF mae: {:.6f}'.format(slf_mae1))
        print('====> Recon SLF mae: {:.6f}'.format(slf_mae2))

        ## revise part for manual ploting roc curve
        preds_model = np.concatenate(preds2, axis=0)
        np.save('results/compare/ROC_data/slf_vae_preds_' + noise_level + '.npy', preds_model)

reason: import numpy,import scipy


text:
import numpy as np

aminoacids = 'ACDEFGHIKLMNPQRSTVWY'

def dist1_hamming(x, reference):
    """ Is the kmer x a Hamming distance 1 away from any of the kmers in the reference set"""
    for i in range(len(x)):
        for aa in aminoacids:
            if aa == x[i]:
                continue
            if x[:i]+aa+x[i+1:] in reference:
                return True
    return False

def dist1_levenshtein(x, reference):
    """ Is the kmer x a Levenshtein distance 1 away from any of the kmers in the reference set"""
    if not type(x) == type(''):
        return np.nan
    for i in range(len(x)):
        # deletion
        if x[:i]+x[i+1:] in reference:
            return True
        for aa in aminoacids:
            if aa == x[i]:
                continue
            # replacement
            if x[:i]+aa+x[i+1:] in reference:
                return True
            # insertion
            if x[:i]+aa+x[i:] in reference:
                return True
    return False

ext: py

max_stars_repo_name: andim/paper-tcellimprint

max_issues_repo_name: andim/paper-tcellimprint

max_forks_repo_name: andim/paper-tcellimprint

reason: import numpy


text:
import argparse
import json
import os
import time
from io import BytesIO

import cv2
import numpy as np
import torch
import torchvision.transforms as T
from detectron2.data import DatasetCatalog, MetadataCatalog
from detectron2.utils.visualizer import Visualizer
from panopticapi.utils import rgb2id
from PIL import Image

from models import build_model

np.random.seed(111)

def get_args_parser():
    parser = argparse.ArgumentParser('Set transformer detector', add_help=False)
    parser.add_argument('--lr', default=1e-4, type=float)
    parser.add_argument('--lr_backbone', default=1e-5, type=float)
    parser.add_argument('--batch_size', default=6, type=int)
    parser.add_argument('--weight_decay', default=1e-4, type=float)
    parser.add_argument('--epochs', default=300, type=int)
    parser.add_argument('--lr_drop', default=200, type=int)
    parser.add_argument('--clip_max_norm', default=0.1, type=float,
                        help='gradient clipping max norm')

    # Model parameters
    parser.add_argument('--frozen_weights', type=str, default=None,
                        help="Path to the pretrained model. If set, only the mask head will be trained")
    # * Backbone
    parser.add_argument('--backbone', default='resnet50', type=str,
                        help="Name of the convolutional backbone to use")
    parser.add_argument('--dilation', action='store_true',
                        help="If true, we replace stride with dilation in the last convolutional block (DC5)")
    parser.add_argument('--position_embedding', default='sine', type=str, choices=('sine', 'learned'),
                        help="Type of positional embedding to use on top of the image features")

    # * Transformer
    parser.add_argument('--enc_layers', default=6, type=int,
                        help="Number of encoding layers in the transformer")
    parser.add_argument('--dec_layers', default=6, type=int,
                        help="Number of decoding layers in the transformer")
    parser.add_argument('--dim_feedforward', default=2048, type=int,
                        help="Intermediate size of the feedforward layers in the transformer blocks")
    parser.add_argument('--hidden_dim', default=256, type=int,
                        help="Size of the embeddings (dimension of the transformer)")
    parser.add_argument('--dropout', default=0.1, type=float,
                        help="Dropout applied in the transformer")
    parser.add_argument('--nheads', default=8, type=int,
                        help="Number of attention heads inside the transformer's attentions")
    parser.add_argument('--num_queries', default=25, type=int,
                        help="Number of query slots")
    parser.add_argument('--pre_norm', action='store_true')

    # * Segmentation
    parser.add_argument('--masks', action='store_true',
                        help="Train segmentation head if the flag is provided")

    # # Loss
    parser.add_argument('--no_aux_loss', dest='aux_loss', action='store_false',
                        help="Disables auxiliary decoding losses (loss at each layer)")
    # * Matcher
    parser.add_argument('--set_cost_class', default=1, type=float,
                        help="Class coefficient in the matching cost")
    parser.add_argument('--set_cost_bbox', default=5, type=float,
                        help="L1 box coefficient in the matching cost")
    parser.add_argument('--set_cost_giou', default=2, type=float,
                        help="giou box coefficient in the matching cost")
    # * Loss coefficients
    parser.add_argument('--mask_loss_coef', default=1, type=float)
    parser.add_argument('--dice_loss_coef', default=1, type=float)
    parser.add_argument('--bbox_loss_coef', default=5, type=float)
    parser.add_argument('--giou_loss_coef', default=2, type=float)
    parser.add_argument('--eos_coef', default=0.1, type=float,
                        help="Relative classification weight of the no-object class")

    # dataset parameters
    parser.add_argument('--dataset_file', default='coco')
    parser.add_argument('--data_path', default='data.test.json', type=str)
    parser.add_argument('--data_panoptic_path', type=str)
    parser.add_argument('--remove_difficult', action='store_true')

    parser.add_argument('--output_dir', default='',
                        help='path where to save the results, empty for no saving')
    parser.add_argument('--device', default='cuda',
                        help='device to use for training / testing')
    parser.add_argument('--resume', default='', help='resume from checkpoint')

    parser.add_argument('--thresh', default=0.85, type=float)
    parser.add_argument('--save_location', type=str,
                        help="the loacation to save to results of mdodel")
    parser.add_argument('--num_test', type=int,
                        help="number of images to be tested")
    return parser

def box_cxcywh_to_xyxy(x):
    x_c, y_c, w, h = x.unbind(1)
    b = [(x_c - 0.5 * w), (y_c - 0.5 * h),
         (x_c + 0.5 * w), (y_c + 0.5 * h)]
    return torch.stack(b, dim=1)

def rescale_bboxes(out_bbox, size):
    img_w, img_h = size
    b = box_cxcywh_to_xyxy(out_bbox)
    b = b * torch.tensor([img_w, img_h,
                          img_w, img_h
                          ], dtype=torch.float32)
    return b

transform = T.Compose([
    T.Resize(800),
    T.ToTensor(),
    T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

@torch.no_grad()
def infer(orig_image, model, postprocessors, classes, device, meta, colors):
    model.eval()
    w, h = orig_image.size
    image = transform(orig_image).unsqueeze(0).to(device)

    out = model(image)

    out["pred_logits"] = out["pred_logits"].cpu()
    out["pred_boxes"] = out["pred_boxes"].cpu()

    probas = out['pred_logits'].softmax(-1)[0, :, :-1]
    # keep = probas.max(-1).values > 0.85
    keep = probas.max(-1).values > args.thresh
    probas = probas[keep].cpu().data.numpy()

    bboxes_scaled = rescale_bboxes(out['pred_boxes'][0, keep], orig_image.size)

    result = postprocessors['panoptic'](out, torch.as_tensor(image.shape[-2:]).unsqueeze(0))[0]
    segments_info = result["segments_info"]
    # Panoptic predictions are stored in a special format png
    panoptic_seg = Image.open(BytesIO(result['png_string']))
    final_w, final_h = panoptic_seg.size
    # We convert the png into an segment id map
    panoptic_seg = np.array(panoptic_seg, dtype=np.uint8)
    panoptic_seg = torch.from_numpy(rgb2id(panoptic_seg))

    if len(bboxes_scaled) == 0:
        return None

    # plotting boxes
    img_obj = np.array(orig_image)
    for p, box in zip(probas, bboxes_scaled):
        bbox = box.cpu().data.numpy()
        bbox = bbox.astype(np.int32)
        x, y = bbox[0], bbox[1]
        bbox = np.array([
            [bbox[0], bbox[1]],
            [bbox[2], bbox[1]],
            [bbox[2], bbox[3]],
            [bbox[0], bbox[3]],
        ])
        cl = p.argmax()
        c = tuple(map(int, colors[cl]))
        text = f"{classes[cl]}: {p[cl]:.2f}"
        cv2.putText(img_obj, text, (x + 2, y + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, c, 2)
        bbox = bbox.reshape((4, 2))
        cv2.polylines(img_obj, [bbox], True, c, 2)

    # plotting segmentation
    for i in range(len(segments_info)):
        c = segments_info[i]["category_id"]
        segments_info[i]["category_id"] = \
            meta.thing_dataset_id_to_contiguous_id[c] if segments_info[i]["isthing"] else meta.stuff_dataset_id_to_contiguous_id[c]

    # Finally visualize the prediction
    v = Visualizer(np.array(orig_image.copy().resize((final_w, final_h)))[:, :, ::-1], meta, scale=1.0)
    v._default_font_size = 18
    v = v.draw_panoptic_seg_predictions(panoptic_seg, segments_info, area_threshold=0, alpha=0.4)
    img_pan = cv2.resize(v.get_image(), (w, h))

    return (img_obj, img_pan)

if __name__ == '__main__':
    parser = argparse.ArgumentParser('DETR evaluation script', parents=[get_args_parser()])
    args = parser.parse_args()

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    print("Using: ", device)

    # loading model
    model, _, postprocessors = build_model(args)
    if args.frozen_weights is not None:
        checkpoint = torch.load(args.frozen_weights, map_location='cpu')
        model.detr.load_state_dict(checkpoint['model'])

    if args.resume:
        checkpoint = torch.load(args.resume, map_location='cpu')
        model.load_state_dict(checkpoint['model'])
    model.to(device)

    data = json.load(open("data/test_panoptic.json"))
    im_info = data['images']
    id_to_class = {}
    for cat in data['categories']:
        id_to_class[cat['id']] = cat['name']

    colors = np.random.randint(0, 255, size=(len(id_to_class), 3), dtype=np.int)

    # generating META for custom dataset for visualiztion with detectron
    thing_classes = []
    stuff_classes = []
    thing_dataset_id_to_contiguous_id = {}
    stuff_dataset_id_to_contiguous_id = {}
    thing_count = 0
    stuff_count = 0
    for cat in data['categories']:
        if cat['isthing']:
            thing_classes.append(cat['name'])
            thing_dataset_id_to_contiguous_id[cat['id']] = thing_count
            thing_count += 1
        else:
            stuff_classes.append(cat['name'])
            stuff_dataset_id_to_contiguous_id[cat['id']] = stuff_count
            stuff_count += 1

    DatasetCatalog.register("construction_data", lambda: data['annotations'])
    MetadataCatalog.get("construction_data").set(thing_classes=thing_classes, stuff_classes=stuff_classes,
                                                 thing_dataset_id_to_contiguous_id=thing_dataset_id_to_contiguous_id,
                                                 stuff_dataset_id_to_contiguous_id=stuff_dataset_id_to_contiguous_id)
    construction_meta = MetadataCatalog.get("construction_data")

    if not os.path.exists(args.save_location):
        os.makedirs(args.save_location)

    i, k = 0, 0
    while True:
        im_path = im_info[k]['file_name']
        im_org = Image.open(im_path)

        start_t = time.perf_counter()
        preds = infer(im_org, model, postprocessors, id_to_class, device, construction_meta, colors)
        end_t = time.perf_counter()

        k += 1

        if preds is not None:
            print("Processed...{} ({:.3f}s) Done:{}..".format(im_path, end_t - start_t, i))

            obj_pred, pan_pred = preds
            im_org = cv2.copyMakeBorder(np.float32(im_org), top=15, bottom=15, left=15, right=15,
                                        borderType=cv2.BORDER_CONSTANT, value=(255, 255, 255))
            obj_pred = cv2.copyMakeBorder(np.float32(obj_pred), top=15, bottom=15, left=15, right=15,
                                          borderType=cv2.BORDER_CONSTANT, value=(255, 255, 255))
            pan_pred = cv2.copyMakeBorder(np.float32(pan_pred), top=15, bottom=15, left=15, right=15,
                                          borderType=cv2.BORDER_CONSTANT, value=(255, 255, 255))

            cv2.imwrite(f"{args.save_location}/pred_{i}.jpeg", cv2.hconcat([im_org, obj_pred, pan_pred]))
            i += 1

        if i == args.num_test:
            break


text:
from argparse import ArgumentParser
from glob import glob
from os import path

import numpy as np

from parcels import ParticleFile

def convert_npydir_to_netcdf(tempwritedir_base, delete_tempfiles=False):
    """Convert npy files in tempwritedir to a NetCDF file
    :param tempwritedir_base: directory where the directories for temporary npy files
            are stored (can be obtained from ParticleFile.tempwritedir_base attribute)
    """

    tempwritedir = sorted(glob(path.join("%s" % tempwritedir_base, "*")),
                          key=lambda x: int(path.basename(x)))[0]
    pyset_file = path.join(tempwritedir, 'pset_info.npy')
    if not path.isdir(tempwritedir):
        raise ValueError('Output directory "%s" does not exist' % tempwritedir)
    if not path.isfile(pyset_file):
        raise ValueError('Output directory "%s" does not contain a pset_info.npy file' % tempwritedir)

    pset_info = np.load(pyset_file, allow_pickle=True).item()
    pfile = ParticleFile(None, None, pset_info=pset_info, tempwritedir=tempwritedir_base, convert_at_end=False)
    pfile.close(delete_tempfiles)

def main(tempwritedir_base=None, delete_tempfiles=False):
    if tempwritedir_base is None:
        p = ArgumentParser(description="""Script to convert temporary npy output files to NetCDF""")
        p.add_argument('tempwritedir', help='Name of directory where temporary npy files are stored '
                                            '(not including numbered subdirectories)')
        p.add_argument('-d', '--delete_tempfiles', default=False,
                       help='Flag to delete temporary files at end of call (default False)')
        args = p.parse_args()
        tempwritedir_base = args.tempwritedir
        if hasattr(args, 'delete_tempfiles'):
            delete_tempfiles = args.delete_tempfiles

    convert_npydir_to_netcdf(tempwritedir_base, delete_tempfiles)

if __name__ == "__main__":
    main()

max_stars_repo_name: jelletreep/parcels

max_issues_repo_name: jelletreep/parcels

max_forks_repo_name: jelletreep/parcels


text:
# univariate cnn lstm example
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import TimeDistributed
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D

# split a univariate sequence into samples
def split_sequence(sequence, n_steps):
	X, y = list(), list()
	for i in range(len(sequence)):
		# find the end of this pattern
		end_ix = i + n_steps
		# check if we are beyond the sequence
		if end_ix > len(sequence)-1:
			break
		# gather input and output parts of the pattern
		seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
		X.append(seq_x)
		y.append(seq_y)
	return array(X), array(y)

# define input sequence
raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90]
# choose a number of time steps
n_steps = 4
# split into samples
X, y = split_sequence(raw_seq, n_steps)
# reshape from [samples, timesteps] into [samples, subsequences, timesteps, features]
n_features = 1
n_seq = 2
n_steps = 2
X = X.reshape((X.shape[0], n_seq, n_steps, n_features))
# define model
model = Sequential()
model.add(TimeDistributed(Conv1D(64, 1, activation='relu'), input_shape=(None, n_steps, n_features)))
model.add(TimeDistributed(MaxPooling1D()))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(50, activation='relu'))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit(X, y, epochs=500, verbose=0)
# demonstrate prediction
x_input = array([60, 70, 80, 90])
x_input = x_input.reshape((1, n_seq, n_steps, n_features))
yhat = model.predict(x_input, verbose=0)
print(yhat)

reason: from numpy


text:
import json
from urllib.request import urlopen

import dash
import dash_daq as daq
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
from dash_html_components.Div import Div
from dash_html_components.Label import Label
import dash_table
import numpy as np
import os
import pandas as pd
import plotly.express as px
import plotly.graph_objs as go
from dash.dependencies import Input, Output
from flask.helpers import get_root_path
print(get_root_path(__name__))

# Get relative data paths
this_directory = os.path.dirname(__file__)
src_zip_zones = os.path.join(this_directory, 'data', 'zip_zones.csv')
src_usda_plants = os.path.join(this_directory, 'data', 'usda_plants_filtered.csv')

# Load data
df = pd.read_csv(src_zip_zones)
df_plants = pd.read_csv(src_usda_plants, index_col=0)

# Create minimum temperature column from trange
df['min_temp'] = df['trange'].apply(lambda x: x.split(' ')[0])
df['min_temp'] = pd.to_numeric(df['min_temp'])

# Fix column names
df_plants.columns = df_plants.columns.str.lower()

# Sort dataframe
df_plants = df_plants.sort_values('common_name')
df_plants = df_plants.reset_index(drop=True)

# Insert row id to df_plants
df_plants.insert(loc=0, column='id', value=np.arange(len(df_plants)))

# Set mapbox access token
px.set_mapbox_access_token("pk.eyJ1IjoiYmlnZG9nZGF0YSIsImEiOiJja3FiYnd2MWcwaDF1Mm9rZDhpNGVqc2gzIn0.XeBuYQMlGHgu4ml4R4RtRQ")

# Style settings
colors = {
    'background': 'rgba(0,0,0,0)',
    'text': 'white'
}

app = dash.Dash(
    __name__,
    meta_tags=[
            {
                "name": "viewport",
                "content": "width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no",
            }
    ],
)
app.title = 'Plant Viewer'
server = app.server

# App layout
app.layout = html.Div(
    [
        html.Div(
            [
                html.Div(
                    [
                        html.H1("Plant Viewer"),
                    ],
                    className='eight columns',
                    id='title'
                ),
                html.Div(
                    [
                        html.A(
                            html.Button('Source Code'),
                            href='https://github.com/azhangd/dash-plants',
                            target="_blank"
                        )
                    ],
                    className='one column'
                )
            ],
            id='header',
            className='center-flex-display',
            style={"margin-bottom": "25px"}
        ),

        html.Div(
            [
                html.Div(
                    [
                        html.Div(
                            [
                                html.Label('Filter by Zip Code', className="control_label"),
                                dcc.Dropdown(
                                    id='zip-dropdown',
                                    options=[{'label': i, 'value': i} for i in df['zipcode']],
                                    value=92620,
                                    multi=False,
                                    placeholder="Zip Code",
                                    className="dcc_control"
                                ),

                                html.Div(id='slider-temperature-output', className="control_label"),
                                dcc.Slider(
                                    id='slider-temperature',
                                    min=-70,
                                    max=52,
                                    value=-70,
                                    marks={
                                        -70: {'label': '-70°F'},
                                        52: {'label': '52°F'}
                                    },
                                    className="dcc_control"
                                ),

                                html.Label('Filter by Duration', className="control_label"),
                                dcc.Checklist(
                                    id='checklist-duration',
                                    options=[
                                        {'label': 'Biennial', 'value': 'Biennial'},
                                        {'label': 'Annual', 'value': 'Annual'},
                                        {'label': 'Perennial', 'value': 'Perennial'}
                                    ],
                                    labelStyle={'display': 'inline-block'},
                                    className="dcc_control"
                                ),

                                html.Label('Filter by Growth Habit', className="control_label"),
                                dcc.Dropdown(
                                    id='dropdown-growth-habit',
                                    options=[
                                        {'label': 'Tree', 'value': 'Tree'},
                                        {'label': 'Shrub', 'value': 'Shrub'},
                                        {'label': 'Forb', 'value': 'Forb'},
                                        {'label': 'Herb', 'value': 'Herb'},
                                        {'label': 'Graminoid', 'value': 'Graminoid'},
                                        {'label': 'Vine', 'value': 'Vine'}
                                    ],
                                    multi=True,
                                    placeholder="Growth Habit",
                                    className="dcc_control"
                                ),
                                html.Div(
                                    [
                                        html.Label('Has image:', style={'marginRight': '7px'}),
                                        daq.BooleanSwitch(
                                            id='checklist-image',
                                            on=True,
                                            color='#80FCFF',
                                            style={'marginRight':'20px'}
                                        ),
                                    ],
                                    className='control_label row flex-display'
                                ),
                                dash_table.DataTable(
                                    id='table-paging-and-sorting',
                                    columns=[
                                        {'name': 'Common Name', 'id': 'common_name'},
                                    ],
                                    page_size=25,
                                    style_table={'height': '500px', 'overflowY': 'auto', 'marginTop': '15px'},
                                    style_header={'backgroundColor': '#525F89'},
                                    style_data_conditional=[
                                        {
                                            'if': {'state': 'active'},
                                            'backgroundColor': '#525F89',
                                            'border': '#FFFFF'
                                        },
                                        {
                                            'if': {'column_id': 'common_name'},
                                            'textAlign': 'center'
                                        }
                                    ],
                                    style_header_conditional=[
                                        {
                                            'if': {'column_id': 'common_name'},
                                            'textAlign': 'center'
                                        }
                                    ],
                                    style_cell={
                                        'backgroundColor': colors['background'],
                                        'color': colors['text'],
                                        'textAlign': 'left'
                                    },
                                    style_as_list_view=True,
                                    active_cell={'row': 6, 'column': 0, 'column_id': 'common_name', 'row_id': 52},
                                    css=[
                                        {
                                            'selector': 'tr:hover',
                                            'rule': 'background-color: #525F89;'
                                        }
                                    ],
                                ),
                            ],
                        ),
                    ],
                    className="pretty_container two columns"
                ),
                html.Div(
                    [
                        html.Div(
                            id='common_scientific_div',
                            children=[
                                html.Div(
                                    [
                                        html.Label('Common Name', className='control_label'),
                                        dcc.Dropdown(
                                            id='common-dropdown',
                                            options=[{'label': i, 'value': i} for i in df_plants['common_name']],
                                            value='California blackberry',
                                            multi=False
                                        ),
                                    ],
                                    style={'width': '100%'}
                                ),
                                html.Div(
                                    [
                                        html.Label('Scientific Name', className='control_label'),
                                        dcc.Dropdown(
                                            id='scientific-dropdown',
                                            options=[{'label': i, 'value': i} for i in df_plants['scientific_name_x']],
                                            value='Rubus ursinus Cham. & Schltdl.',
                                            multi=False
                                        )
                                    ],
                                    style={'width': '100%'}
                                ),
                            ],
                            className='center-flex-display'
                        ),
                        html.Div(
                            [
                                html.Img(
                                    id='image-url',
                                    src='https://plants.sc.egov.usda.gov/ImageLibrary/standard/RUUR_001_svp.jpg',
                                ),
                            ],
                            className='imagebox'
                        ),
                        dcc.Loading(
                            html.Div(id='table-characteristics-div'),
                            type="cube"
                        ),
                    ],
                    id='middle-column',
                    className='pretty_container two columns'
                ),
                html.Div(
                    [
                        dcc.Loading(
                            children=[dcc.Graph(id='plants-map')],
                            type="cube"
                        ),
                    ],
                    id='right-column',
                    className='pretty_container five columns'
                ),
            ],
            className='center-flex-display'
        ),
    ],
    id='mainContainer'
)

# Helper Functions
def filter_df_plants(selected_plant):
    df_plants_filtered = df_plants[df_plants['common_name']==selected_plant]
    selected_temp = df_plants_filtered['temperature_minimum_f'].max()
    filtered_df = df[df['min_temp'] >= selected_temp]
    return filtered_df

def get_image_url(selected_plant):
    url_1 = 'https://plants.sc.egov.usda.gov/ImageLibrary/standard/'
    url_2 = '_001_svp.jpg'
    symbol = df_plants[df_plants['common_name']==selected_plant]['symbol'].to_string(index=False)
    return (url_1 + symbol + url_2)

def filter_by_zip(selected_df, selected_zip):
    min_temp = df[df['zipcode'] == selected_zip]['min_temp'].item()
    return selected_df[selected_df['temperature_minimum_f'] <= min_temp]

def filter_by_duration(selected_df, duration_list):
    for duration in duration_list:
        selected_df = selected_df[selected_df['duration'].str.contains(duration, case=False, na=False)]
    return selected_df

def filter_by_image(selected_df):
    return selected_df[selected_df['has_image'] > 0]

def filter_by_temperature(selected_df, selected_temp):
    return selected_df[selected_df['temperature_minimum_f'] >= selected_temp]

def filter_by_growth_habit(selected_df, growth_habit_list):
    for growth_habit in growth_habit_list:
        selected_df = selected_df[selected_df['growth_habit'].str.contains(growth_habit, case=False, na=False)]
    return selected_df

def transpose_classification(selected_df, selected_plant):
    df_transposed = selected_df
    plant_id = df_transposed[df_transposed['common_name'] == selected_plant]['id']
    df_transposed = df_transposed.drop('common_name', 1)
    df_transposed = df_transposed.set_index('id').transpose()
    df_transposed = df_transposed.reset_index()
    df_transposed = df_transposed[['index', int(plant_id)]]
    return df_transposed

# Update map with dropdown
@app.callback(
    Output('plants-map', 'figure'),
    Input('common-dropdown', 'value')
)
def update_graph(selected_plant):
    if selected_plant is None:
        return dash.no_update
    if not selected_plant:
        filtered_df = df
    else:
        filtered_df = filter_df_plants(selected_plant)

    fig = px.scatter_mapbox(
        filtered_df,
        lat="latitude",
        lon="longitude",
        hover_name="city",
        hover_data=["zone", "zipcode"],
        color="min_temp",
        color_continuous_scale=px.colors.sequential.haline,
        range_color=[-55, 65],
        center={"lat": 39.5, "lon": -110},
        zoom=4,
        opacity=0.8
    )

    fig.update_layout(
        legend_title_text='Min. Temp',
        mapbox_style="mapbox://styles/bigdogdata/ckqreebqs3s2b17rygkmnnyhy/draft",
        autosize=True,
        uirevision='no reset of zoom',
        margin=dict(l=30, r=30, b=20, t=0),
        height=1000,
        plot_bgcolor=colors['background'],
        paper_bgcolor=colors['background'],
        legend_bgcolor=colors['background'],
        font_color=colors['text']
    )
    return fig

# Show zip code on map click
@app.callback(
    # Output('led-zipcode', 'value'),
    Output('zip-dropdown', 'value'),
    Input('plants-map', 'clickData')
    )
def display_click_data(clickData):
    if clickData is None:
        return dash.no_update
    else:
        return clickData['points'][0]['customdata'][1]

# Zipcode dropdown updates temperature slider
@app.callback(
    Output('slider-temperature', 'value'),
    Input('zip-dropdown', 'value'),
    Input('slider-temperature', 'value')
)
def update_slider(zipcode, selected_temp):
    trigger_id = dash.callback_context.triggered[0]["prop_id"]
    if trigger_id == 'zip-dropdown.value':
        if zipcode is None:
            return dash.no_update
        zipcode_temp = df[df['zipcode']==zipcode]['min_temp'].item()
        if zipcode_temp <= selected_temp:
            return zipcode_temp
    return dash.no_update

# Update table using zip dropdown
@app.callback(
    Output('table-paging-and-sorting', 'data'),
    Input('zip-dropdown', 'value'),
    Input("checklist-duration", "value"),
    Input("checklist-image", "on"),
    Input("slider-temperature", "drag_value"),
    Input("dropdown-growth-habit", "value")
)
def update_table(selected_zip, duration_list, image_selected_list, selected_temperature, growth_habit_list):
    data = df_plants
    if selected_zip is not None:
        data = filter_by_zip(data, selected_zip)
    if duration_list is not None:
        data = filter_by_duration(data, duration_list)
    if image_selected_list:
        data = filter_by_image(data)
    if selected_temperature > df_plants['temperature_minimum_f'].min():
        data = filter_by_temperature(data, selected_temperature)
    if growth_habit_list is not None:
        data = filter_by_growth_habit(data, growth_habit_list)
    return data.to_dict('records')

# Print temperature slider value
@app.callback(
    Output('slider-temperature-output', 'children'),
    Input('slider-temperature', 'drag_value')
)
def display_value(drag_value):
    return 'Filter by Minimum Temperature: {}°F'.format(drag_value)

# Show image on datatable click
@app.callback(
    Output("image-url", "src"),
    Input("common-dropdown", "value"),
    prevent_initial_call=True
)
def cell_clicked(selected_plant):
    trigger_id = dash.callback_context.triggered[0]["prop_id"]
    if trigger_id == 'common-dropdown.value':
        if selected_plant is None:
            return dash.no_update
        if df_plants[df_plants['common_name']==selected_plant]['has_image'].item() > 0:
            return str(get_image_url(selected_plant))
        return 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/65/No-Image-Placeholder.svg/1200px-No-Image-Placeholder.svg.png'

# Update characteristics
@app.callback(
    Output('table-characteristics-div', 'children'),
    Input("common-dropdown", "value")
)
def update_table(selected_plant):
    if selected_plant is None:
        return dash.no_update
    df_characteristics = df_plants[['id', 'common_name', 'family_common_name', 'category', 'species']]
    data_characteristics = transpose_classification(df_characteristics, selected_plant)
    dict_chracteristics = data_characteristics.to_dict('records')

    df_growth = df_plants[['id', 'common_name', 'temperature_minimum_f', 'growth_habit', 'growth_rate', 'height_mature_feet', 'lifespan', 'toxicity']]
    data_growth = transpose_classification(df_growth, selected_plant)
    dict_growth = data_growth.to_dict('records')

    df_reproduction = df_plants[['id', 'common_name', 'bloom_period', 'fruit_seed_period_begin', 'fruit_seed_period_end', 'fruit_seed_abundance']]
    data_reproduction = transpose_classification(df_reproduction, selected_plant)
    dict_reproduction = data_reproduction.to_dict('records')

    return [
        # Update characteristics table
        html.Div(
            [
                dash_table.DataTable(
                    id='table-characteristics',
                    columns=[{'name': ['Characteristics', str(i)], 'id': str(i)} for i in data_characteristics.columns],
                    data=dict_chracteristics,
                    style_header={
                        'backgroundColor': 'rgb(30, 30, 30)',
                        'border': 'none',
                        'textAlign': 'center'
                        },
                    style_header_conditional=[
                        {
                            'if': {'header_index': 1},
                            'display': 'none'
                        }
                    ],
                    style_cell={
                        'backgroundColor': colors['background'],
                        'color': colors['text'],
                        'textAlign': 'left',
                        'overflow': 'hidden',
                        'textOverflow': 'ellipsis',
                        'width': '50%'
                    },
                    style_data_conditional=[
                        {
                            'if': {'state': 'active'},
                            'backgroundColor': '#525F89',
                            'border': '#FFFFF'
                        },
                        {
                            'if': {'column_id': 'index'},
                            'textAlign': 'left',
                        }
                    ],
                    merge_duplicate_headers=True,
                    style_as_list_view=True,
                    css=[
                        {
                            'selector': 'tr:hover',
                            'rule': 'background-color: #525F89;'
                        }
                    ],
                ),
            ],
        ),
        # Update growth Table
        html.Div(
            [
                dash_table.DataTable(
                    id='table-growth',
                    columns=[{'name': ['Growth Requirements', str(i)], 'id': str(i)} for i in data_growth.columns],
                    data=dict_growth,
                    style_header={
                        'backgroundColor': 'rgb(30, 30, 30)',
                        'border': 'none',
                        'textAlign': 'center'
                        },
                    style_header_conditional=[
                        {
                            'if': {'header_index': 1},
                            'display': 'none'
                        }
                    ],
                    style_cell={
                        'backgroundColor': colors['background'],
                        'color': colors['text'],
                        'textAlign': 'left',
                        'overflow': 'hidden',
                        'textOverflow': 'ellipsis',
                        'width': '50%'
                    },
                    style_data_conditional=[
                        {
                            'if': {'state': 'active'},
                            'backgroundColor': '#525F89',
                            'border': '#FFFFF'
                        },
                        {
                            'if': {'column_id': 'index'},
                            'textAlign': 'left',
                        }
                    ],
                    merge_duplicate_headers=True,
                    style_as_list_view=True,
                    css=[
                        {
                            'selector': 'tr:hover',
                            'rule': 'background-color: #525F89'
                        },
                    ],
                ),
            ],
        ),
        # Update reproduction Table
        html.Div(
            [
                dash_table.DataTable(
                    id='table-reproduction',
                    columns=[{'name': ['Reproduction', str(i)], 'id': str(i)} for i in data_reproduction.columns],
                    data=dict_reproduction,
                    style_table={'width': '100%'},
                    style_header={
                        'backgroundColor': 'rgb(30, 30, 30)',
                        'border': 'none',
                        'textAlign': 'center'
                        },
                    style_header_conditional=[
                        {
                            'if': {'header_index': 1},
                            'display': 'none'
                        }
                    ],
                    style_cell={
                        'backgroundColor': colors['background'],
                        'color': colors['text'],
                        'textAlign': 'left',
                        'overflow': 'hidden',
                        'textOverflow': 'ellipsis',
                        'width': '50%'
                    },
                    style_data_conditional=[
                        {
                            'if': {'state': 'active'},
                            'backgroundColor': '#525F89',
                            'border': '#FFFFF'
                        },
                        {
                            'if': {'column_id': 'index'},
                            'textAlign': 'left',
                        }
                    ],
                    merge_duplicate_headers=True,
                    style_as_list_view=True,
                    css=[
                        {
                            'selector': 'tr:hover',
                            'rule': 'background-color: #525F89'
                        },
                    ],
                ),
            ],
        ),
    ]

# Add value to common/scientific dropdown when datatable is clicked
@app.callback(
    Output('common-dropdown', 'value'),
    Input("table-paging-and-sorting", "active_cell"),
    prevent_initial_call=True
)
def update_dropdown(active_cell):
    if active_cell is None:
        return dash.no_update
    row_id = active_cell["row_id"]
    selected_plant = df_plants.at[row_id, 'common_name']
    return selected_plant

# Sync scientific and common dropdowns
@app.callback(
    Output('common_scientific_div', 'children'),
    [
        Input('common-dropdown', 'value'),
        Input('scientific-dropdown', "value")
    ],
    prevent_initial_call=True
)
def input_update(common, scientific):
    trigger_id = dash.callback_context.triggered[0]["prop_id"]
    if trigger_id == "common-dropdown.value":
        scientific = df_plants[df_plants['common_name'] == common]['scientific_name_x'].to_string(index=False)
        return [
            html.Div(
                [
                    html.Label('Common Name', className='control_label'),
                    dcc.Dropdown(
                        id='common-dropdown',
                        options=[{'label': i, 'value': i} for i in df_plants['common_name']],
                        value=common,
                        multi=False
                    ),
                ],
                style={'width': '100%'}
            ),
            html.Div(
                [
                    html.Label('Scientific Name', className='control_label'),
                    dcc.Dropdown(
                        id='scientific-dropdown',
                        options=[{'label': i, 'value': i} for i in df_plants['scientific_name_x']],
                        value=scientific                    )
                ],
                style={'width': '100%'}
            ),
        ]
    if trigger_id == "scientific-dropdown.value":
        common = df_plants[df_plants['scientific_name_x'] == scientific]['common_name'].to_string(index=False)
        return [
            html.Div(
                [
                    html.Label('Common Name:', className='control_label'),
                    dcc.Dropdown(
                        id='common-dropdown',
                        options=[{'label': i, 'value': i} for i in df_plants['common_name']],
                        value=common,
                        multi=False
                    ),
                ],
                style={'width': '100%'}
            ),
            html.Div(
                [
                    html.Label('Scientific Name:', className='control_label'),
                    dcc.Dropdown(
                        id='scientific-dropdown',
                        options=[{'label': i, 'value': i} for i in df_plants['scientific_name_x']],
                        value=scientific,
                        multi=False
                    )
                ],
                style={'width': '100%'}
            ),
        ]
    return dash.no_update

if __name__ == "__main__":
    app.run_server(debug=True)

max_stars_repo_name: azhangd/dash-plants

max_issues_repo_name: azhangd/dash-plants

max_forks_repo_name: azhangd/dash-plants


text:
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Conv2D, Input
from tensorflow.keras import Model
import keras
from tensorflow.keras.models import load_model
from keras_multi_head import MultiHead

def calc_euclidian_dists(x, y):
    """
    Calculate euclidian distance between two 3D tensors.
    Args:
        x (tf.Tensor):
        y (tf.Tensor):
    Returns (tf.Tensor): 2-dim tensor with distances.
    """
    n = x.shape[0]
    m = y.shape[0]
    x = tf.tile(tf.expand_dims(x, 1), [1, m, 1])
    y = tf.tile(tf.expand_dims(y, 0), [n, 1, 1])
    return tf.reduce_mean(tf.math.pow(x - y, 2), 2)

def hw_flatten(x):
    h=tf.shape(x)[0]
    w=tf.shape(x)[-1]
    return tf.reshape(x, [h, -1, w])

class SelfAttention(tf.keras.layers.Layer):
    def __init__(self, units=32, input_dim=32):
        super(SelfAttention, self).__init__()
        self.gamma = tf.Variable(tf.random.truncated_normal([1]),
                             name="gamma")
        self.f1 = Conv2D(filters=4, kernel_size=3, padding='same',name='fir')
        self.g1 = Conv2D(filters=4, kernel_size=3, padding='same',name='sec')
        self.h1 = Conv2D(filters=16, kernel_size=3, padding='same',name='thir')
        self.o1 = Conv2D(filters=16, kernel_size=3, padding='same',name='four')

    def call(self, inputs):
        f = self.f1(inputs) # [bs, h, w, c']
        g = self.g1(inputs) # [bs, h, w, c']
        h = self.h1(inputs) # [bs, h, w, c]

            # N = h * w
        s = tf.matmul(hw_flatten(g), hw_flatten(f), transpose_b=True) # # [bs, N, N]

        beta = tf.nn.softmax(s)  # attention map

        o = tf.matmul(beta, hw_flatten(h)) # [bs, N, C]

        o = tf.reshape(o, shape=tf.shape(inputs)) # [bs, h, w, C]
        #o = tf.reshape(o, shape=tf.shape(inputs.shape)) # [bs, h, w, C]

        o = self.o1(o)

        inputs = self.gamma * o + inputs
        return inputs

    def get_config(self):

        config = super().get_config().copy()
        config.update({
            'f1': self.f1,
            'g1': self.g1,
            'h1': self.h1,
            'o1': self.o1
        })
        return config

class Prototypical(Model):
    """
    Implemenation of Prototypical Network.
    """

    def __init__(self, n_support, n_query, w, h, c):
        """
        Args:
            n_support (int): number of support examples.
            n_query (int): number of query examples.
            w (int): image width .
            h (int): image height.
            c (int): number of channels.
        """
        super(Prototypical, self).__init__()
        self.w, self.h, self.c = w, h, c
        #self.W = tf.Variable(tf.random.truncated_normal([3136]),
        #                     name="W")
        self.W = tf.Variable(tf.random.truncated_normal([1,16]), name="W")
        # Encoder as ResNet like CNN with 4 blocks
        inputs1 = tf.keras.layers.Input(shape=(self.w, self.h, self.c))
        '''
        meta_enc1 = tf.keras.layers.Conv2D(filters=16, kernel_size=3, padding='same')(inputs1)
        meta_enc1 = tf.keras.layers.BatchNormalization()(meta_enc1)
        meta_enc1 = tf.keras.layers.ReLU()(meta_enc1)
        meta_enc1 = tf.keras.layers.MaxPool2D((2, 2))(meta_enc1)
        meta_enc1 = tf.keras.layers.GlobalAveragePooling2D()(meta_enc1)
        print(meta_enc1)
        '''

        self.l1= tf.keras.layers.Conv2D(filters=16, kernel_size=3, padding='same')
        self.l2= tf.keras.layers.Conv2D(filters=16, kernel_size=3, padding='same')
        self.l3= tf.keras.layers.Conv2D(filters=16, kernel_size=3, padding='same')
        self.l4= tf.keras.layers.Conv2D(filters=16, kernel_size=3, padding='same')
        self.l5= tf.keras.layers.BatchNormalization()
        self.l6= tf.keras.layers.ReLU()
        self.l7= tf.keras.layers.MaxPool2D((2, 2))

        self.l8=  tf.keras.layers.Conv2D(filters=16, kernel_size=3, padding='same')
        self.l9=  tf.keras.layers.Conv2D(filters=16, kernel_size=3, padding='same')
        #self.l10= SelfAttention()

        self.l11= tf.keras.layers.BatchNormalization()
        self.l12= tf.keras.layers.ReLU()
        self.l13= tf.keras.layers.MaxPool2D((2, 2))
        self.l14= Flatten()

        encoder=self.l1(inputs1)
        encoder1=self.l2(encoder)
        encoder2=self.l3(encoder1)
        encoder3=self.l4(encoder2)
        encoder4=self.l5(encoder3)
        encoder6=self.l6(encoder4)
        encoder7=self.l7(encoder6)
        encoder8=self.l8(encoder7)
        encoder9=self.l9(encoder8)
        #encoder10=self.l10(encoder9)
        encoder11=self.l11(encoder9)
        encoder12=self.l12(encoder11)
        encoder13=self.l13(encoder12)
        print(encoder13)
        encoder14=tf.keras.layers.Attention()([self.W, encoder13, encoder13])
        print(encoder14)
        encoder15=self.l14(encoder14)

        #meta_att = tf.keras.layers.Attention()([meta_enc1, encoder])
        self.encoder = Model(inputs = inputs1, outputs = encoder15)
        #self.meta_encoder = Model(inputs = inputs1, outputs = meta_enc1)

        '''
        self.decoder = tf.keras.Sequential()
        #self.decoder.add(tf.keras.layers.Reshape(tf.shape(encoder14))
        self.decoder.add(tf.keras.layers.UpSampling2D((2, 2)))
        self.decoder.add(tf.keras.layers.Conv2D(filters=16, kernel_size=3, padding='same'))
        self.decoder.add(tf.keras.layers.Conv2D(filters=16, kernel_size=3, padding='same'))
        self.decoder.add(tf.keras.layers.UpSampling2D((2, 2)))
        self.decoder.add(tf.keras.layers.Conv2D(filters=16, kernel_size=3, padding='same'))
        self.decoder.add(tf.keras.layers.Conv2D(filters=16, kernel_size=3, padding='same'))
        self.decoder.add(tf.keras.layers.Conv2D(filters=16, kernel_size=3, padding='same'))
        self.decoder.add(tf.keras.layers.Conv2D(filters=16, kernel_size=3, padding='same'))
        self.decoder.add(tf.keras.layers.Conv2D(filters=1, kernel_size=1, padding='same'))
        '''
        '''
        self.meta_encoder = tf.keras.Sequential([
            tf.keras.layers.Conv2D(filters=16, kernel_size=3, padding='same'),
            tf.keras.layers.BatchNormalization(),
            tf.keras.layers.ReLU(),
            tf.keras.layers.MaxPool2D((2, 2)), Flatten(), Dense(128)]
        )
        '''

    '''
    def generator(self,input, prototype):
        combined = input + self.W*prototype
        return self.decoder(combined)
    '''

    def call(self, support, query):
        n_class = support.shape[0]
        n_support = support.shape[1]
        n_query = query.shape[1]
        y = np.tile(np.arange(n_class)[:, np.newaxis], (1, n_query))
        y_onehot = tf.cast(tf.one_hot(y, n_class), tf.float32)
        uns_loss = 0.0
        # correct indices of support samples (just natural order)
        target_inds = tf.reshape(tf.range(n_class), [n_class, 1])
        target_inds = tf.tile(target_inds, [1, n_query])

        # merge support and query to forward through encoder
        cat = tf.concat([
            tf.reshape(support, [n_class * n_support,
                                 self.w, self.h, self.c]),
            tf.reshape(query, [n_class * n_query,
                               self.w, self.h, self.c])], axis=0)

        z = self.encoder(cat)

        '''
        z_uns = self.uns_enc(z)
        print("zshape")
        print(z.shape)

        z1 = tf.reshape(z_uns[:n_class*n_support],[n_class, n_support, z_uns.shape[-1]])

        for clss in range(n_class):
          for img1 in range(n_support):
            cnt=0
            enc1 = tf.expand_dims(z1[clss, img1,:],axis=0)
            tot_loss = 0
            enc_aug = self.uns_enc(self.encoder(tf.expand_dims(tf.image.flip_left_right(support[clss][img1]),axis=0)))
            cnt+=1
            tot_loss = tot_loss + (calc_euclidian_dists(enc1,enc_aug)**2)
            for img2 in range(n_support):
              enc2 = tf.expand_dims(z1[clss, img2,:],axis=0)
              tot_loss = tot_loss + (calc_euclidian_dists(enc1,enc2)**2)
              cnt+=1
              if(cnt>10):
                break

            for img3 in range(n_support):
              adv_cls = (clss+1)%n_class
              enc3 = tf.expand_dims(z1[adv_cls, img3,:],axis=0)
              tot_loss = tot_loss - (calc_euclidian_dists(enc1,enc3)**2)
              cnt+=1
              if(cnt>20):
                break

            uns_loss = uns_loss + tot_loss/(cnt*1.0)
            uns_loss = uns_loss + 0.5
        '''

        #z_meta = self.encoder(cat)

        #z_feat1 = self.meta_enc1(cat)

        '''
        z_att1 = tf.keras.layers.Attention()(
            [z_feat1, z_meta]
        )
        '''

        #z_fin = tf.concat([z],axis=1)
        #W_prototypes = self.meta_encoder(z)

        '''
        for ind,layer in enumerate(self.encoder.layers):
            print(layer)
            cat = self.W[ind]*layer(cat)
            cnt+=1
        '''
        #z = self.encoder(cat)
        # Divide embedding into support and query
        #z_prototypes = self.meta_encoder(cat)
        z_prototypes = tf.reshape(z[:n_class * n_support],
                                  [n_class, n_support, z.shape[-1]])

        z_prototypes = tf.math.reduce_mean(z_prototypes, axis=1)
        #z1 = self.encoder(support[0][0])
        #z2 = generative(z1, z_prototypes[0])
        #z2 = self.decoder(z1)
        #uns_loss = uns_loss + tf.reduce_sum(z2-z1)
        z_query = z[n_class * n_support:]

        # Calculate distances between query and prototypes
        dists = calc_euclidian_dists(z_query, z_prototypes)

        # log softmax of calculated distances
        log_p_y = tf.nn.log_softmax(-dists, axis=-1)
        log_p_y = tf.reshape(log_p_y, [n_class, n_query, -1])

        loss = -tf.reduce_mean(tf.reshape(tf.reduce_sum(tf.multiply(y_onehot, log_p_y), axis=-1), [-1])) + uns_loss
        eq = tf.cast(tf.equal(
            tf.cast(tf.argmax(log_p_y, axis=-1), tf.int32),
            tf.cast(y, tf.int32)), tf.float32)
        acc = tf.reduce_mean(eq)
        return loss, acc

    def save(self, model_path):
        """
        Save encoder to the file.
        Args:
            model_path (str): path to the .h5 file.
        Returns: None
        """
        self.encoder.save(model_path)

    def load(self, model_path):
        """
        Load encoder from the file.
        Args:
            model_path (str): path to the .h5 file.
        Returns: None
        """
        self.encoder(tf.zeros([1, self.w, self.h, self.c]))
        self.encoder.load_weights(model_path)