repo
stringlengths
1
99
file
stringlengths
13
239
code
stringlengths
0
59.2M
file_length
int64
0
59.2M
avg_line_length
float64
0
3.34M
max_line_length
int64
0
26.7M
extension_type
stringclasses
1 value
pke
pke-master/docs/source/conf.py
# -*- coding: utf-8 -*- # # Configuration file for the Sphinx documentation builder. # # This file does only contain a selection of the most common options. For a # full list see the documentation: # http://www.sphinx-doc.org/en/stable/config # -- Path setup -------------------------------------------------------------- # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. import os import sys sys.path.insert(0, os.path.abspath('../../')) # -- Project information ----------------------------------------------------- project = 'pke' copyright = '2018-2022, pke Contributors' author = 'pke Contributors' # The short X.Y version version = '2.0' # The full version, including alpha/beta/rc tags release = '2.0' # -- General configuration --------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. # # needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.todo', 'sphinx.ext.coverage', 'sphinx.ext.githubpages', 'sphinx.ext.napoleon', ] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix(es) of source filenames. # You can specify multiple suffix as a list of string: # source_suffix = '.rst' # The master toctree document. master_doc = 'index' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. # # This is also used if you do content translation via gettext catalogs. # Usually you set "language" from the command line for these cases. language = None # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This pattern also affects html_static_path and html_extra_path . exclude_patterns = [] # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # -- Options for HTML output ------------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. # html_theme = 'classic' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. # # html_theme_options = {} # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] # Custom sidebar templates, must be a dictionary that maps document names # to template names. # # The default sidebars (for documents that don't match any pattern) are # defined by theme itself. Builtin themes are using these templates by # default: ``['localtoc.html', 'relations.html', 'sourcelink.html', # 'searchbox.html']``. # # html_sidebars = {} # -- Options for HTMLHelp output --------------------------------------------- # Output file base name for HTML help builder. htmlhelp_basename = 'pkedoc' # -- Extension configuration ------------------------------------------------- # -- Options for todo extension ---------------------------------------------- # If true, `todo` and `todoList` produce output, else they produce nothing. todo_include_todos = True
3,678
30.715517
79
py
vadesc
vadesc-main/main.py
""" Runs the VaDeSC model. """ import argparse from pathlib import Path import yaml import logging import tensorflow as tf import tensorflow_probability as tfp import os from models.losses import Losses from train import run_experiment tfd = tfp.distributions tfkl = tf.keras.layers tfpl = tfp.layers tfk = tf.keras # Project-wide constants: ROOT_LOGGER_STR = "GMM_Survival" LOGGER_RESULT_FILE = "logs.txt" CHECKPOINT_PATH = 'models/Ours' logger = logging.getLogger(ROOT_LOGGER_STR + '.' + __name__) tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR) physical_devices = tf.config.experimental.list_physical_devices('GPU') assert len(physical_devices) > 0, "Not enough GPU hardware devices available" tf.config.experimental.set_memory_growth(physical_devices[0], True) def main(): project_dir = Path(__file__).absolute().parent print(project_dir) parser = argparse.ArgumentParser() # Model parameters parser.add_argument('--data', default='mnist', type=str, choices=['mnist', 'sim', 'support', 'flchain', 'hgg', 'hemo', 'lung1', 'nsclc', 'nsclc_features', 'basel'], help='the dataset (mnist, sim, support, flchain, hgg, hemo, lung1, nsclc, basel)') parser.add_argument('--num_epochs', default=1000, type=int, help='the number of training epochs') parser.add_argument('--batch_size', default=256, type=int, help='the mini-batch size') parser.add_argument('--lr', default=0.001, type=float, help='the learning rate') parser.add_argument('--decay', default=0.00001, type=float, help='the decay') parser.add_argument('--weibull_shape', default=1.0, type=float, help='the Weibull shape parameter (global)') parser.add_argument('--no-survival', dest='survival', action='store_false', help='specifies if the survival model should not be included') parser.add_argument('--dsa', dest='dsa', action='store_true', help='specifies if the deep survival analysis with k-means shuld be run') parser.add_argument('--dsa_k', default=1, type=int, help='number of clusters in deep survival analysis with k-means') parser.add_argument('--eval-cal', default=False, type=bool, help='specifies if the calibration needs to be evaluated') parser.set_defaults(survival=True) # Other parameters parser.add_argument('--runs', default=1, type=int, help='the number of runs, the results will be averaged') parser.add_argument('--results_dir', default=os.path.join(project_dir, 'models/experiments'), type=lambda p: Path(p).absolute(), help='the directory where the results will be saved') parser.add_argument('--results_fname', default='', type=str, help='the name of the .txt file with the results') parser.add_argument('--pretrain', default=False, type=bool, help='specifies if the autoencoder should be pretrained') parser.add_argument('--epochs_pretrain', default=10, type=int, help='the number of pretraining epochs') parser.add_argument('--save_model', default=False, type=bool, help='specifies if the model should be saved') parser.add_argument('--ex_name', default='', type=str, help='the experiment name') parser.add_argument('--config_override', default='', type=str, help='the override file name for config.yml') parser.add_argument('--seed', default=42, type=int, help='random number generator seed') parser.add_argument('--eager', default=False, type=bool, help='specifies if the TF functions should be run eagerly') args = parser.parse_args() data_name = args.data +'.yml' config_path = project_dir / 'configs' / data_name # Check for config override if args.config_override is not "": config_path = Path(args.config_override) with config_path.open(mode='r') as yamlfile: configs = yaml.safe_load(yamlfile) losses = Losses(configs) if args.data == "MNIST": loss = losses.loss_reconstruction_binary else: loss = losses.loss_reconstruction_mse run_experiment(args, configs, loss) if __name__ == "__main__": main()
5,142
37.380597
112
py
vadesc
vadesc-main/train.py
import time from pathlib import Path import tensorflow as tf import tensorflow_probability as tfp import numpy as np from sklearn.mixture import GaussianMixture from sklearn.cluster import KMeans from sklearn.metrics.cluster import normalized_mutual_info_score, adjusted_rand_score import uuid import math from utils.eval_utils import cindex, calibration, accuracy_metric, cindex_metric from utils.eval_utils import rae as RAE import os import utils.utils as utils from models.model import GMM_Survival from utils.plotting import plot_group_kaplan_meier, plot_bigroup_kaplan_meier, plot_tsne_by_cluster, \ plot_tsne_by_survival from utils.data_utils import get_data, get_gen tfd = tfp.distributions tfkl = tf.keras.layers tfpl = tfp.layers tfk = tf.keras def pretrain(model, args, ex_name, configs): input_shape = configs['training']['inp_shape'] num_clusters = configs['training']['num_clusters'] learn_prior = configs['training']['learn_prior'] if isinstance(input_shape, list): input_shape = [input_shape[0], input_shape[1], 1] # Get the AE from the model input = tfkl.Input(shape=input_shape) z, _ = model.encoder(input) if isinstance(input_shape, list): z_dec = tf.expand_dims(z, 0) else: z_dec = z dec = model.decoder(z_dec) if isinstance(input_shape, list): dec = tf.reshape(dec, [-1, input_shape[0], input_shape[1],1]) dec = tfkl.Lambda(lambda x: x, name="dec")(dec) autoencoder = tfk.Model(inputs=input, outputs=dec) optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)#, decay=args.decay) autoencoder.compile(optimizer=optimizer, loss={"dec":"mse"}) autoencoder.summary() s = tfkl.Dense(4, activation='softmax', name="classifier")(z) autoencoder_classifier = tfk.Model(inputs=input, outputs=[dec, s]) losses = {"dec": "mse", "classifier": "categorical_crossentropy"} lossWeights = {'dec': 10.0, "classifier": 1.0} opt = tf.keras.optimizers.Adam(learning_rate=0.001) autoencoder_classifier.compile(optimizer=opt, loss=losses, loss_weights=lossWeights, metrics={"classifier": "accuracy"}) autoencoder_classifier.summary() x_train, x_valid, x_test, y_train, y_valid, y_test = get_data(args, configs) gen_train = get_gen(x_train, y_train, configs, args.batch_size, ae_class=True) gen_test = get_gen(x_test, y_test, configs, args.batch_size, validation=True, ae_class=True) X = np.concatenate((x_train, x_test)) Y = np.concatenate((y_train[:, 2], y_test[:, 2])) project_dir = Path(__file__).absolute().parent pretrain_dir = os.path.join(project_dir, 'models/pretrain/' + args.data + "/input_" + str(input_shape[0]) + 'x' + str(input_shape[1])\ + '_ldim_' + str(configs['training']['latent_dim']) + '_pretrain_'+ str(args.epochs_pretrain)) print('\n******************** Pretraining **************************') inp_enc = X autoencoder_classifier.fit(gen_train, validation_data=gen_test, epochs=args.epochs_pretrain)#, callbacks=cp_callback) encoder = model.encoder input = tfkl.Input(shape=input_shape) z, _ = encoder(input) z_model = tf.keras.models.Model(inputs=input, outputs=z) z = z_model.predict(X) estimator = GaussianMixture(n_components=num_clusters, covariance_type='diag', n_init=3) estimator.fit(z) print('\n******************** Pretraining Done**************************') encoder = model.encoder input = tfkl.Input(shape=input_shape) z, _ = encoder(input) z_model = tf.keras.models.Model(inputs=input, outputs=z) # Assign weights to GMM mixtures of VaDE prior_samples = estimator.weights_ mu_samples = estimator.means_ prior_samples = prior_samples.reshape((num_clusters)) model.c_mu.assign(mu_samples) if learn_prior: model.prior_logits.assign(prior_samples) yy = estimator.predict(z_model.predict(X)) acc = utils.cluster_acc(yy, Y) pretrain_acc = acc print('\nPretrain accuracy: ' + str(acc)) return model, pretrain_acc def run_experiment(args, configs, loss): # Reproducibility np.random.seed(args.seed) tf.random.set_seed(args.seed) if args.eager: tf.config.run_functions_eagerly(True) # Set paths project_dir = Path(__file__).absolute().parent timestr = time.strftime("%Y%m%d-%H%M%S") ex_name = "{}_{}".format(str(timestr), uuid.uuid4().hex[:5]) experiment_path = args.results_dir / configs['data']['data_name'] / ex_name experiment_path.mkdir(parents=True) os.makedirs(os.path.join(project_dir, 'models/logs', ex_name)) print(experiment_path) # Override the survival argument configs['training']['survival'] = args.survival # Generate a new dataset each run x_train, x_valid, x_test, y_train, y_valid, y_test = get_data(args, configs) gen_train = get_gen(x_train, y_train, configs, args.batch_size) gen_test = get_gen(x_test, y_test, configs, args.batch_size, validation=True) # Override configs if the baseline DSA should be run configs['training']['dsa'] = args.dsa # Define model & optimizer model = GMM_Survival(**configs['training']) optimizer = tf.keras.optimizers.Adam(learning_rate=args.lr, decay=args.decay) cp_callback = [tf.keras.callbacks.TensorBoard(log_dir=os.path.join(project_dir, 'models/logs', ex_name))] model.compile(optimizer, loss={"output_1": loss}, metrics={"output_4": accuracy_metric, "output_5": cindex_metric}) # The survival time is used for training tf.keras.backend.set_value(model.use_t, np.array([1.0])) # Pretrain model: the model gets stuck in a local minimum, pretraining can prevent this. if args.pretrain: model, pretrain_acc = pretrain(model, args, ex_name, configs) # Fit model model.fit(gen_train, validation_data=gen_test, callbacks=cp_callback, epochs=args.num_epochs) # Save model if args.save_model: checkpoint_path = experiment_path print("\nSaving weights at ", experiment_path) model.save_weights(checkpoint_path) print("\n" * 2) print("Evaluation") print("\n" * 2) # NB: don't use MC samples to predict survival at evaluation model.sample_surv = False # Training set performance tf.keras.backend.set_value(model.use_t, np.array([1.0])) rec, z_sample, p_z_c, p_c_z, risk_scores, lambdas = model.predict((x_train, y_train), batch_size=args.batch_size) risk_scores = np.squeeze(risk_scores) if args.save_model: with open(experiment_path / 'c_train.npy', 'wb') as save_file: np.save(save_file, p_c_z) yy = np.argmax(p_c_z, axis=-1) if args.dsa: km_dsa = KMeans(n_clusters=args.dsa_k, random_state=args.seed) km_dsa = km_dsa.fit(z_sample[:, 0, :]) yy = km_dsa.predict(z_sample[:, 0, :]) acc = utils.cluster_acc(y_train[:, 2], yy) nmi = normalized_mutual_info_score(y_train[:, 2], yy) ari = adjusted_rand_score(y_train[:, 2], yy) ci = cindex(t=y_train[:, 0], d=y_train[:, 1], scores_pred=risk_scores) t_pred_med = risk_scores * np.log(2) ** (1 / model.weibull_shape) rae_nc = RAE(t_pred=t_pred_med[y_train[:, 1] == 1], t_true=y_train[y_train[:, 1] == 1, 0], cens_t=1 - y_train[y_train[:, 1] == 1, 1]) rae_c = RAE(t_pred=t_pred_med[y_train[:, 1] == 0], t_true=y_train[y_train[:, 1] == 0, 0], cens_t=1 - y_train[y_train[:, 1] == 0, 1]) if args.results_fname is '': file_results = "results_" + args.data + ".txt" else: file_results = args.results_fname + ".txt" f = open(file_results, "a+") f.write( "Epochs= %d, batch_size= %d, latent_dim= %d, K= %d, mc samples= %d, weibull_shape= %d, learning_rate= %f, pretrain_e= %d, decay= %f, name= %s, survival= %s, " "sample_surv= %s, seed= %d.\n" % (args.num_epochs, args.batch_size, configs['training']['latent_dim'], configs['training']['num_clusters'], configs['training']['monte_carlo'], configs['training']['weibull_shape'], args.lr, args.epochs_pretrain, args.decay, ex_name, args.survival, configs['training']['sample_surv'], args.seed)) if args.pretrain: f.write("epochs_pretrain: %d. Pretrain accuracy: %f , " % (args.epochs_pretrain, pretrain_acc)) f.write("Train (w t) | Accuracy: %.3f, NMI: %.3f, ARI: %.3f. CI: %.3f, RAE (nc.): %.3f, RAE (c.): %.3f.\n" % ( acc, nmi, ari, ci, rae_nc, rae_c)) plot_bigroup_kaplan_meier(t=y_train[:, 0], d=y_train[:, 1], c=y_train[:, 2], c_=yy, dir='./', postfix=args.data + '_' + str(args.seed)) plot_tsne_by_cluster(X=z_sample[:, 0], c=y_train[:, 2], font_size=12, seed=42, dir='./', postfix=args.data + '_' + str(args.seed) + '_z_wt') plot_tsne_by_survival(X=z_sample[:, 0], t=y_train[:, 0], d=y_train[:, 1], seed=42, dir='./', postfix=args.data + '_' + str(args.seed) + '_z_wt', plot_censored=True) if args.data != 'nsclc' and args.data != 'lung1' and args.data != 'basel': plot_tsne_by_cluster(X=x_train, c=yy, font_size=12, seed=42, dir='./', postfix=args.data + '_' + str(args.seed) + '_x_wt') plot_tsne_by_cluster(X=x_train, c=y_train[:, 2], font_size=12, seed=42, dir='./', postfix=args.data + '_' + str(args.seed) + '_x_true_labels') # Some extra logging if args.data == 'nsclc': np.savetxt(fname="c_hat_nsclc_" + str(args.seed) + ".csv", X=yy) plot_group_kaplan_meier(t=y_train[y_train[:, 0] > 0.001, 0], d=y_train[y_train[:, 0] > 0.001, 1], c=yy[y_train[:, 0] > 0.001], dir='', experiment_name='nsclc_' + str(args.seed)) elif args.data == 'lung1': np.savetxt(fname="c_hat_lung1_" + str(args.seed) + ".csv", X=yy) plot_group_kaplan_meier(t=y_train[:, 0], d=y_train[:, 1], c=yy, dir='', experiment_name='lung1_' + str(args.seed)) elif args.data == 'basel': np.savetxt(fname="c_hat_basel_" + str(args.seed) + ".csv", X=yy) plot_group_kaplan_meier(t=y_train[:, 0], d=y_train[:, 1], c=yy, dir='', experiment_name='basel_' + str(args.seed)) # Test set performance tf.keras.backend.set_value(model.use_t, np.array([0.0])) rec, z_sample, p_z_c, p_c_z, risk_scores, lambdas = model.predict((x_train, y_train), batch_size=args.batch_size) risk_scores = np.squeeze(risk_scores) yy = np.argmax(p_c_z, axis=-1) if args.dsa: yy = km_dsa.predict(z_sample[:, 0, :]) acc = utils.cluster_acc(y_train[:, 2], yy) nmi = normalized_mutual_info_score(y_train[:, 2], yy) ari = adjusted_rand_score(y_train[:, 2], yy) ci = cindex(t=y_train[:, 0], d=y_train[:, 1], scores_pred=risk_scores) t_pred_med = risk_scores * np.log(2) ** (1 / model.weibull_shape) rae_nc = RAE(t_pred=t_pred_med[y_train[:, 1] == 1], t_true=y_train[y_train[:, 1] == 1, 0], cens_t=1 - y_train[y_train[:, 1] == 1, 1]) rae_c = RAE(t_pred=t_pred_med[y_train[:, 1] == 0], t_true=y_train[y_train[:, 1] == 0, 0], cens_t=1 - y_train[y_train[:, 1] == 0, 1]) f.write("Train (w/o t) | Accuracy: %.3f, NMI: %.3f, ARI: %.3f. CI: %.3f, RAE (nc.): %.3f, RAE (c.): %.3f.\n" % ( acc, nmi, ari, ci, rae_nc, rae_c)) plot_tsne_by_cluster(X=z_sample[:, 0], c=y_train[:, 2], font_size=12, seed=42, dir='./', postfix=args.data + '_' + str(args.seed) + '_z_wot') plot_tsne_by_survival(X=z_sample[:, 0], t=y_train[:, 0], d=y_train[:, 1], seed=42, dir='./', postfix=args.data + '_' + str(args.seed) + '_z_wot', plot_censored=True) if args.data != 'nsclc' and args.data != 'lung1' and args.data != 'basel': plot_tsne_by_cluster(X=x_train, c=yy, font_size=12, seed=42, dir='./', postfix=args.data + '_' + str(args.seed) + '_x_wot') # Test set performance tf.keras.backend.set_value(model.use_t, np.array([1.0])) rec, z_sample, p_z_c, p_c_z, risk_scores, lambdas = model.predict((x_test, y_test), batch_size=args.batch_size) risk_scores = np.squeeze(risk_scores) if args.save_model: with open(experiment_path / 'c_test.npy', 'wb') as save_file: np.save(save_file, p_c_z) yy = np.argmax(p_c_z, axis=-1) if args.dsa: yy = km_dsa.predict(z_sample[:, 0, :]) acc = utils.cluster_acc(y_test[:, 2], yy) nmi = normalized_mutual_info_score(y_test[:, 2], yy) ari = adjusted_rand_score(y_test[:, 2], yy) ci = cindex(t=y_test[:, 0], d=y_test[:, 1], scores_pred=risk_scores) t_pred_med = risk_scores * np.log(2) ** (1 / model.weibull_shape) rae_nc = RAE(t_pred=t_pred_med[y_test[:, 1] == 1], t_true=y_test[y_test[:, 1] == 1, 0], cens_t=1 - y_test[y_test[:, 1] == 1, 1]) rae_c = RAE(t_pred=t_pred_med[y_test[:, 1] == 0], t_true=y_test[y_test[:, 1] == 0, 0], cens_t=1 - y_test[y_test[:, 1] == 0, 1]) if args.data == 'nsclc': np.savetxt(fname="c_hat_test_nsclc_" + str(args.seed) + ".csv", X=yy) if args.data == 'basel': np.savetxt(fname="c_hat_test_basel_" + str(args.seed) + ".csv", X=yy) f.write("Test (w t) | Accuracy: %.3f, NMI: %.3f, ARI: %.3f. CI: %.3f, RAE (nc.): %.3f, RAE (c.): %.3f.\n" % ( acc, nmi, ari, ci, rae_nc, rae_c)) # Plot generated samples.. if args.data == 'lung1' or args.data == 'nsclc' or args.data == 'basel': utils.save_generated_samples(model=model, inp_size=[64, 64], grid_size=10, cmap='bone', postfix='nsclc_' + str(args.seed) + '_K_' + str(model.num_clusters)) tf.keras.backend.set_value(model.use_t, np.array([0.0])) rec, z_sample, p_z_c, p_c_z, risk_scores, lambdas = model.predict((x_test, y_test), batch_size=args.batch_size) risk_scores = np.squeeze(risk_scores) yy = np.argmax(p_c_z, axis=-1) if args.dsa: yy = km_dsa.predict(z_sample[:, 0, :]) acc = utils.cluster_acc(y_test[:, 2], yy) nmi = normalized_mutual_info_score(y_test[:, 2], yy) ari = adjusted_rand_score(y_test[:, 2], yy) ci = cindex(t=y_test[:, 0], d=y_test[:, 1], scores_pred=risk_scores) t_pred_med = risk_scores * np.log(2) ** (1 / model.weibull_shape) rae_nc = RAE(t_pred=t_pred_med[y_test[:, 1] == 1], t_true=y_test[y_test[:, 1] == 1, 0], cens_t=1 - y_test[y_test[:, 1] == 1, 1]) rae_c = RAE(t_pred=t_pred_med[y_test[:, 1] == 0], t_true=y_test[y_test[:, 1] == 0, 0], cens_t=1 - y_test[y_test[:, 1] == 0, 1]) # NOTE: this can be slow, comment it out unless really necessary! if args.eval_cal: t_sample = utils.sample_weibull(scales=risk_scores, shape=model.weibull_shape) cal = calibration(predicted_samples=t_sample, t=y_test[:, 0], d=y_test[:, 1]) else: cal = np.nan f.write( "Test (w/o t) | Accuracy: %.3f, NMI: %.3f, ARI: %.3f. CI: %.3f, RAE (nc.): %.3f, RAE (c.): %.3f, CAL: %.3f.\n" % ( acc, nmi, ari, ci, rae_nc, rae_c, cal)) tf.keras.backend.set_value(model.use_t, np.array([1.0])) if args.data == 'lung1': np.savetxt(fname="preds_lung1_" + str(args.seed) + ".csv", X=np.stack((t_pred_med, y_test[:, 0], y_test[:, 1]), axis=1)) elif args.data == 'nsclc': np.savetxt(fname="preds_nsclc_" + str(args.seed) + ".csv", X=np.stack((t_pred_med, y_test[:, 0], y_test[:, 1]), axis=1)) elif args.data == 'basel': np.savetxt(fname="preds_basel_" + str(args.seed) + ".csv", X=np.stack((t_pred_med, y_test[:, 0], y_test[:, 1]), axis=1)) f.close() print(str(acc)) print(str(nmi)) print(str(ari)) print(str(ci)) print("(" + str(rae_nc) + "; " + str(rae_c) + ")")
16,068
46.54142
166
py
vadesc
vadesc-main/baselines/aft/main_aft.py
""" Runs Weibull AFT model. """ import argparse import os import numpy as np import pandas as pd import time import uuid from lifelines import WeibullAFTFitter import sys sys.path.insert(0, '../../') from datasets.support.support_data import generate_support from datasets.hgg.hgg_data import generate_hgg from datasets.nsclc_lung.nsclc_lung_data import generate_radiomic_features from datasets.simulations import simulate_nonlin_profile_surv from utils.data_utils import construct_surv_df from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from utils.eval_utils import cindex, calibration from utils.eval_utils import rae as RAE from utils import utils def get_data(args, val=False): if args.data == 'support': x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = \ generate_support(seed=args.seed) elif args.data == "flchain": data = pd.read_csv('../DCM/data/flchain.csv') feats = ['age', 'sex', 'sample.yr', 'kappa', 'lambda', 'flc.grp', 'creatinine', 'mgus'] prot = 'sex' feats = set(feats) feats = list(feats - set([prot])) t = data['futime'].values + 1 d = data['death'].values x = data[feats].values c = data[prot].values X = StandardScaler().fit_transform(x) t = t / np.max(t) + 0.001 x_train, x_test, t_train, t_test, d_train, d_test, c_train, c_test = train_test_split(X, t, d, c, test_size=.3, random_state=args.seed) elif args.data == 'hgg': x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = \ generate_hgg(seed=args.seed) elif args.data == 'nsclc': x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = \ generate_radiomic_features(n_slices=11, dsize=[256, 256], seed=args.seed) elif args.data == "sim": X, t, d, c, Z, mus, sigmas, betas, betas_0, mlp_dec = simulate_nonlin_profile_surv(p=1000, n=60000, latent_dim=16, k=args.num_clusters, p_cens=.9, seed=args.seed, clust_mean=True, clust_cov=True, clust_coeffs=True, clust_intercepts=True, balanced=True, weibull_k=1, brange=[-10.0, 10.0], isotropic=True, xrange=[-.5, .5]) # Normalisation t = t / np.max(t) + 0.001 scaler = StandardScaler() scaler.fit(X) X = scaler.transform(X) x_train, x_test, t_train, t_test, d_train, d_test, c_train, c_test = train_test_split(X, t, d, c, test_size=.3, random_state=args.seed) else: NotImplementedError('This dataset is not supported!') # Wrap t, d, and c together y_train = np.stack([t_train, d_train, c_train], axis=1) if val: y_valid = np.stack([t_valid, d_valid, c_valid], axis=1) y_test = np.stack([t_test, d_test, c_test], axis=1) if val: return x_train, x_valid, x_test, y_train, y_valid, y_test else: return x_train, x_test, x_test, y_train, y_test, y_test def run_experiment(args): os.chdir('../../bin/') timestr = time.strftime("%Y%m%d-%H%M%S") ex_name = "{}_{}".format(str(timestr), uuid.uuid4().hex[:5]) x_train, x_valid, x_test, y_train, y_valid, y_test = get_data(args) # Check variances of columns feat_var = np.var(x_train, axis=0) # Filter out features with low variance x_train = x_train[:, feat_var > 0.0001] x_valid = x_valid[:, feat_var > 0.0001] x_test = x_test[:, feat_var > 0.0001] print("Remaining dimensions: " + str(x_train.shape)) aft = WeibullAFTFitter(penalizer=args.penalty_weight) df = construct_surv_df(x_train, y_train[:, 0], y_train[:, 1]) aft = aft.fit(df, duration_col='time_to_event', event_col='failure', show_progress=True) # Training set performance ci = cindex(t=y_train[:, 0], d=y_train[:, 1], scores_pred=aft.predict_median(df)) rae_nc = RAE(t_pred=aft.predict_median(df)[y_train[:, 1] == 1], t_true=y_train[y_train[:, 1] == 1, 0], cens_t=1 - y_train[y_train[:, 1] == 1, 1]) rae_c = RAE(t_pred=aft.predict_median(df)[y_train[:, 1] == 0], t_true=y_train[y_train[:, 1] == 0, 0], cens_t=1 - y_train[y_train[:, 1] == 0, 1]) if args.data == 'support': f = open("results_SUPPORT_AFT.txt", "a+") elif args.data == 'flchain': f = open("results_FLChain_AFT.txt", "a+") elif args.data == 'nki': f = open("results_NKI_AFT.txt", "a+") elif args.data == 'hgg': f = open("results_HGG_AFT.txt", "a+") elif args.data == 'nsclc': f = open("results_nsclc_AFT.txt", "a+") elif args.data == 'sim': f = open("results_sim_AFT.txt", "a+") f.write("weight_penalty= %f, name= %s, seed= %d.\n" % (args.penalty_weight, ex_name, args.seed)) f.write("Train | CI: %f, RAE (nc.): %f, RAE (c.): %f.\n" % (ci, rae_nc, rae_c)) # Test set performance df = construct_surv_df(x_test, y_test[:, 0], y_test[:, 1]) ci = cindex(t=y_test[:, 0], d=y_test[:, 1], scores_pred=aft.predict_median(df)) rae_nc = RAE(t_pred=aft.predict_median(df)[y_test[:, 1] == 1], t_true=y_test[y_test[:, 1] == 1, 0], cens_t=1 - y_test[y_test[:, 1] == 1, 1]) rae_c = RAE(t_pred=aft.predict_median(df)[y_test[:, 1] == 0], t_true=y_test[y_test[:, 1] == 0, 0], cens_t=1 - y_test[y_test[:, 1] == 0, 1]) lambda_, rho_ = aft._prep_inputs_for_prediction_and_return_scores(df, ancillary_X=None) t_sample = utils.sample_weibull(scales=lambda_, shape=rho_) if args.data != 'sim': cal = calibration(predicted_samples=t_sample, t=y_test[:, 0], d=y_test[:, 1]) if args.data != 'sim': f.write("Test | CI: %f, RAE (nc.): %f, RAE (c.): %f, CAL: %f.\n" % (ci, rae_nc, rae_c, cal)) else: f.write("Test | CI: %f, RAE (nc.): %f, RAE (c.): %f.\n" % (ci, rae_nc, rae_c)) f.close() print(str(ci)) def main(): parser = argparse.ArgumentParser() parser.add_argument('--data', default='support', type=str, choices=['support', 'flchain', 'hgg', 'nsclc', 'sim'], help='specify the data (support, flchain, hgg, nsclc, sim)') parser.add_argument('--num_clusters', default=5, type=int, help='specify the number of clusters') parser.add_argument('--seed', default=42, type=int, help='specify the random generator seed') parser.add_argument('--penalty_weight', default=0.0, type=float, help='specify the penalty weight in the Cox PH model') args = parser.parse_args() run_experiment(args) if __name__ == "__main__": main()
8,240
41.699482
119
py
vadesc
vadesc-main/baselines/km/main_km.py
""" Runs k-means clustering. """ import argparse import numpy as np import time import uuid from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split from sklearn.metrics import normalized_mutual_info_score, adjusted_rand_score from sklearn.cluster import KMeans import sys from datasets.survivalMNIST.survivalMNIST_data import generate_surv_MNIST from datasets.simulations import simulate_nonlin_profile_surv from datasets.hemodialysis.hemo_data import generate_hemo from utils import utils sys.path.insert(0, '../../') def get_data(args, val=False): if args.data == 'MNIST': valid_perc = .15 if not val: valid_perc = .0 if val: x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = \ generate_surv_MNIST(n_groups=args.num_clusters, seed=args.seed, p_cens=.3, valid_perc=valid_perc) else: x_train, x_test, t_train, t_test, d_train, d_test, c_train, c_test = generate_surv_MNIST(n_groups=args.num_clusters, seed=args.seed, p_cens=.3, valid_perc=valid_perc) # Normalisation x_test = x_test / 255. if val: x_valid = x_valid / 255. x_train = x_train / 255. elif args.data == "sim": X, t, d, c, Z, mus, sigmas, betas, betas_0, mlp_dec = simulate_nonlin_profile_surv(p=1000, n=60000, latent_dim=16, k=args.num_clusters, p_cens=.3, seed=args.seed, clust_mean=True, clust_cov=True, clust_coeffs=True, clust_intercepts=True, balanced=True, weibull_k=1, brange=[-10.0, 10.0], isotropic=True, xrange=[-.5, .5]) # Normalisation t = t / np.max(t) + 0.001 scaler = StandardScaler() scaler.fit(X) X = scaler.transform(X) x_train, x_test, t_train, t_test, d_train, d_test, c_train, c_test = train_test_split(X, t, d, c, test_size=.3, random_state=args.seed) elif args.data == 'hemo': c = args.num_clusters x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = generate_hemo(seed=args.seed, label=c) else: NotImplementedError('This dataset is not supported!') # Wrap t, d, and c together y_train = np.stack([t_train, d_train, c_train], axis=1) if val: y_valid = np.stack([t_valid, d_valid, c_valid], axis=1) y_test = np.stack([t_test, d_test, c_test], axis=1) if val: return x_train, x_valid, x_test, y_train, y_valid, y_test else: return x_train, x_test, x_test, y_train, y_test, y_test def run_experiment(args): timestr = time.strftime("%Y%m%d-%H%M%S") ex_name = "{}_{}".format(str(timestr), uuid.uuid4().hex[:5]) x_train, x_valid, x_test, y_train, y_valid, y_test = get_data(args) km = KMeans(n_clusters=args.num_clusters) km = km.fit(X=x_train) # Training set performance yy = km.predict(X=x_train) acc = utils.cluster_acc(y_train[:, 2], yy) nmi = normalized_mutual_info_score(y_train[:, 2], yy) ari = adjusted_rand_score(y_train[:, 2], yy) if args.data == 'MNIST': f = open("results_MNIST_KM.txt", "a+") elif args.data == 'sim': f = open("results_sim_KM.txt", "a+") elif args.data == 'liverani': f = open("results_liverani_KM.txt", "a+") elif args.data == 'hemo': f = open("results_hemo_KM.txt", "a+") f.write("Accuracy train: %f, NMI: %f, ARI: %f.\n" % (acc, nmi, ari)) # Test set performance yy = km.predict(X=x_test) acc = utils.cluster_acc(y_test[:, 2], yy) nmi = normalized_mutual_info_score(y_test[:, 2], yy) ari = adjusted_rand_score(y_test[:, 2], yy) f.write("Accuracy test: %f, NMI: %f, ARI: %f.\n" % (acc, nmi, ari)) f.close() print(str(acc)) print(str(nmi)) print(str(ari)) def main(): parser = argparse.ArgumentParser() parser.add_argument('--data', default='MNIST', type=str, choices=['MNIST', 'sim', 'hemo'], help='specify the data (MNIST, sim, hemo)') parser.add_argument('--num_clusters', default=5, type=int, help='specify the number of clusters') parser.add_argument('--seed', default=42, type=int, help='specify the random generator seed') args = parser.parse_args() run_experiment(args) if __name__ == "__main__": main()
6,062
40.813793
151
py
vadesc
vadesc-main/baselines/coxph/main_coxph.py
""" Runs Cox PH regression. """ import argparse import os import numpy as np import time import uuid from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split from lifelines import CoxPHFitter import sys sys.path.insert(0, '../../') from datasets.survivalMNIST.survivalMNIST_data import generate_surv_MNIST from datasets.simulations import simulate_nonlin_profile_surv from datasets.support.support_data import generate_support from datasets.hemodialysis.hemo_data import generate_hemo from datasets.nsclc_lung.nsclc_lung_data import generate_radiomic_features from utils.data_utils import construct_surv_df from utils.eval_utils import cindex from utils.eval_utils import rae as RAE, calibration def get_data(args, val=False): if args.data == 'MNIST': valid_perc = .15 if not val: valid_perc = .0 if val: x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = \ generate_surv_MNIST(n_groups=args.num_clusters, seed=args.seed, p_cens=.3, valid_perc=valid_perc) else: x_train, x_test, t_train, t_test, d_train, d_test, c_train, c_test = generate_surv_MNIST(n_groups=args.num_clusters, seed=args.seed, p_cens=.3, valid_perc=valid_perc) # Normalisation x_test = x_test / 255. if val: x_valid = x_valid / 255. x_train = x_train / 255. elif args.data == "sim": X, t, d, c, Z, mus, sigmas, betas, betas_0, mlp_dec = simulate_nonlin_profile_surv(p=1000, n=60000, latent_dim=16, k=args.num_clusters, p_cens=.3, seed=args.seed, clust_mean=True, clust_cov=True, clust_coeffs=True, clust_intercepts=True, balanced=True, weibull_k=1, brange=[-10.0, 10.0], isotropic=True, xrange=[-.5, .5]) # Normalisation t = t / np.max(t) + 0.001 scaler = StandardScaler() scaler.fit(X) X = scaler.transform(X) x_train, x_test, t_train, t_test, d_train, d_test, c_train, c_test = train_test_split(X, t, d, c, test_size=.3, random_state=args.seed) elif args.data == 'support': x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = \ generate_support(seed=args.seed) elif args.data == 'hemo': c = args.num_clusters x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = generate_hemo(seed=args.seed, label=c) elif args.data == 'nsclc': x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = \ generate_radiomic_features(n_slices=11, dsize=[256, 256], seed=args.seed) else: NotImplementedError('This dataset is not supported!') # Wrap t, d, and c together y_train = np.stack([t_train, d_train, c_train], axis=1) if val: y_valid = np.stack([t_valid, d_valid, c_valid], axis=1) y_test = np.stack([t_test, d_test, c_test], axis=1) if val: return x_train, x_valid, x_test, y_train, y_valid, y_test else: return x_train, x_test, x_test, y_train, y_test, y_test def run_experiment(args): os.chdir('../../bin/') timestr = time.strftime("%Y%m%d-%H%M%S") ex_name = "{}_{}".format(str(timestr), uuid.uuid4().hex[:5]) x_train, x_valid, x_test, y_train, y_valid, y_test = get_data(args) # Check variances of columns feat_var = np.var(x_train, axis=0) # Filter out features with low variance x_train = x_train[:, feat_var > 0.0001] x_valid = x_valid[:, feat_var > 0.0001] x_test = x_test[:, feat_var > 0.0001] print("Remaining dimensions: " + str(x_train.shape)) cph = CoxPHFitter(penalizer=args.penalty_weight) df = construct_surv_df(x_train, y_train[:, 0], y_train[:, 1]) cph = cph.fit(df, duration_col='time_to_event', event_col='failure', show_progress=True) # Training set performance risk_scores = np.exp(-np.squeeze(np.matmul(x_train, np.expand_dims(cph.params_, 1)))) ci = cindex(t=y_train[:, 0], d=y_train[:, 1], scores_pred=risk_scores) if args.data == 'MNIST': f = open("results_MNIST_Cox.txt", "a+") elif args.data == 'sim': f = open("results_sim_Cox.txt", "a+") elif args.data == 'liverani': f = open("results_liverani_Cox.txt", "a+") elif args.data == 'nki': f = open("results_NKI_Cox.txt", "a+") elif args.data == 'support': f = open("results_SUPPORT_Cox.txt", "a+") elif args.data == 'hemo': f = open("results_hemo_Cox.txt", "a+") elif args.data == 'nsclc': f = open("results_nsclc_Cox.txt", "a+") f.write("weight_penalty= %f, name= %s, seed= %d.\n" % (args.penalty_weight, ex_name, args.seed)) f.write("Train | CI: %f.\n" % (ci)) # Test set performance risk_scores = np.exp(-np.squeeze(np.matmul(x_test, np.expand_dims(cph.params_, 1)))) ci = cindex(t=y_test[:, 0], d=y_test[:, 1], scores_pred=risk_scores) rae_nc = RAE(t_pred=cph.predict_median(x_test)[y_test[:, 1] == 1], t_true=y_test[y_test[:, 1] == 1, 0], cens_t=1 - y_test[y_test[:, 1] == 1, 1]) rae_c = RAE(t_pred=cph.predict_median(x_test)[y_test[:, 1] == 0], t_true=y_test[y_test[:, 1] == 0, 0], cens_t=1 - y_test[y_test[:, 1] == 0, 1]) times_sorted = np.sort(np.unique(y_train[y_train[:, 1] == 1, 0])) cdfs = np.transpose(1 - cph.predict_survival_function(X=x_test, times=times_sorted)) cdfs = np.concatenate((np.zeros((cdfs.shape[0], 1)), cdfs), axis=1) pdfs = np.diff(cdfs) t_sample = np.zeros((cdfs.shape[0], 200)) for i in range(cdfs.shape[0]): pdf = pdfs[i] probs = pdf / np.sum(pdf) t_sample[i, :] = np.random.choice(a=times_sorted, p=probs, size=(200,)) cal = calibration(predicted_samples=t_sample, t=y_test[:, 0], d=y_test[:, 1]) f.write("Test | CI: %f, RAE (nc.): %f, RAE (c.): %f, CAL: %f.\n" % (ci, rae_nc, rae_c, cal)) f.close() print(str(ci)) def main(): parser = argparse.ArgumentParser() parser.add_argument('--data', default='MNIST', type=str, choices=['MNIST', 'sim', 'support', 'hemo', 'nsclc'], help='specify the data (MNIST, sim, support, hemo, nsclc)') parser.add_argument('--num_clusters', default=5, type=int, help='specify the number of clusters') parser.add_argument('--seed', default=42, type=int, help='specify the random generator seed') parser.add_argument('--penalty_weight', default=0.0, type=float, help='specify the penalty weight in the Cox PH model') args = parser.parse_args() run_experiment(args) if __name__ == "__main__": main()
8,516
44.063492
151
py
vadesc
vadesc-main/baselines/coxph/coxph.py
""" Wrapper for Cox PH model as implemented by lifelines. """ import numpy as np from lifelines import CoxPHFitter import sys sys.path.insert(0, '../../') from utils.data_utils import construct_surv_df from utils.eval_utils import cindex def fit_coxph(X: np.ndarray, t: np.ndarray, d: np.ndarray, penalty_weight=0.0, X_test=None, t_test=None, d_test=None): """ Fits and evaluates a Cox proportional hazards (PH) model on the provided data. A wrapper function for the lifelines CoxPHFitter. :param X: predictor variables [n_samples, n_features]. :param t: time-to-event. :param d: labels of the type of event observed. d[i] == 1, if the i-th event is failure (death); d[i] == 0 otherwise. :param penalty_weight: weight of the penalty term in the Cox regression, 0.0 by default. Hint: use a non-zero penalty weight for strongly correlated features. :param X_test: test set predictor variables. :param t_test: test set time-to-event. :param d_test: test set labels of the type of event observed. :return: returns the fitted Cox PH model, predicted hazard function values, and the concordance index on the train set. If applicable, returns hazard scores and the concordance index on the test data as well. """ df = construct_surv_df(X, t, d) cph = CoxPHFitter(penalizer=penalty_weight) cph.fit(df, duration_col='time_to_event', event_col='failure') hazard_train = np.exp(-np.squeeze(np.matmul(X, np.expand_dims(cph.params_, 1)))) ci_train = cindex(t=t, d=d, scores_pred=hazard_train) if X_test is not None: assert t_test is not None and d_test is not None hazard_test = np.exp(-np.squeeze(np.matmul(X_test, np.expand_dims(cph.params_, 1)))) ci_test = cindex(t=t_test, d=d_test, scores_pred=hazard_test) return cph, hazard_train, ci_train, hazard_test, ci_test return cph, hazard_train, ci_train
1,914
43.534884
121
py
vadesc
vadesc-main/baselines/ssc/sscBair.py
""" A Python implementation of the semi-supervised survival data clustering described by Bair & Tibshirani in https://journals.plos.org/plosbiology/article?id=10.1371/journal.pbio.0020108 """ import numpy as np from lifelines import CoxPHFitter from sklearn.cluster import KMeans from sklearn.metrics import normalized_mutual_info_score from utils.data_utils import construct_surv_df class SSC_Bair(): def __init__(self, n_clusters: int, input_dim: int, clustering_dim: int, random_state: int, penalty_weight=0.0): self.cph = [] self.X_train = None self.t_train = None self.d_train = None self.hazard_ratios = [] self.clustering_features = None assert n_clusters >= 2 assert 0 < clustering_dim <= input_dim self.n_clusters = n_clusters self.input_dim = input_dim self.clustering_dim = clustering_dim self.penalty_weight = penalty_weight self.km = KMeans(n_clusters=self.n_clusters, random_state=random_state) self.random_state = random_state def fit(self, X: np.ndarray, t: np.ndarray, d: np.ndarray): self.X_train = X self.t_train = t self.d_train = d for j in range(self.X_train.shape[1]): print("Fitting Cox PH model " + str(j) + "/" + str(self.X_train.shape[1]), end="\r") # Fit a univariate Cox PH model cph_j = CoxPHFitter(penalizer=self.penalty_weight) df = construct_surv_df(np.expand_dims(X[:, j], 1), t, d) cph_j.fit(df, duration_col='time_to_event', event_col='failure', show_progress=False) self.cph.append(cph_j) # Retrieve the hazard ratio self.hazard_ratios.append(cph_j.hazard_ratios_.array[0]) print() self.hazard_ratios = np.array(self.hazard_ratios) # Choose top significant features self.clustering_features = np.argsort(-self.hazard_ratios)[:self.clustering_dim] # Perform k-means self.km = self.km.fit(X[:, self.clustering_features]) return self def re_fit(self, new_clustering_dim: int): # Re-fits with a new dimensionality assert self.X_train is not None and self.t_train is not None and self.d_train is not None self.clustering_dim = new_clustering_dim self.km = KMeans(n_clusters=self.n_clusters, random_state=self.random_state) # Choose top significant features self.clustering_features = np.argsort(-self.hazard_ratios)[:self.clustering_dim] # Perform k-means self.km = self.km.fit(self.X_train[:, self.clustering_features]) return self def predict(self, X: np.ndarray): assert self.clustering_features is not None c_pred = self.km.predict(X=X[:, self.clustering_features]) return c_pred def find_best_dim(model: SSC_Bair, c: np.ndarray, step=30): dims = np.arange(1, model.input_dim, step) d_best = -1 nmi_best = 0.0 for d in dims: model_d = model.re_fit(new_clustering_dim=d) c_pred_d = model_d.predict(X=model.X_train) nmi_d = normalized_mutual_info_score(labels_true=c, labels_pred=c_pred_d) if nmi_d > nmi_best: nmi_best = nmi_d d_best = d model_best = model.re_fit(new_clustering_dim=d_best) return model_best, d_best, nmi_best
3,382
32.49505
116
py
vadesc
vadesc-main/baselines/ssc/main_ssc_bair.py
""" Runs semi-supervised clustering of survival data as described by Bair & Tibshirani. """ import argparse import numpy as np import time import uuid from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split from sklearn.metrics import normalized_mutual_info_score, adjusted_rand_score import sys sys.path.insert(0, '../../') from datasets.survivalMNIST.survivalMNIST_data import generate_surv_MNIST from datasets.hemodialysis.hemo_data import generate_hemo from datasets.simulations import simulate_nonlin_profile_surv from sscBair import SSC_Bair, find_best_dim from utils import utils def get_data(args, val=False): if args.data == 'MNIST': valid_perc = .15 if not val: valid_perc = .0 if val: x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = \ generate_surv_MNIST(n_groups=args.num_clusters, seed=args.seed, p_cens=.3, valid_perc=valid_perc) else: x_train, x_test, t_train, t_test, d_train, d_test, c_train, c_test = generate_surv_MNIST(n_groups=args.num_clusters, seed=args.seed, p_cens=.3, valid_perc=valid_perc) # Normalisation x_test = x_test / 255. if val: x_valid = x_valid / 255. x_train = x_train / 255. elif args.data == "sim": X, t, d, c, Z, mus, sigmas, betas, betas_0, mlp_dec = simulate_nonlin_profile_surv(p=1000, n=60000, latent_dim=16, k=args.num_clusters, p_cens=.3, seed=args.seed, clust_mean=True, clust_cov=True, clust_coeffs=True, clust_intercepts=True, balanced=True, weibull_k=1, brange=[-10.0, 10.0], isotropic=True, xrange=[-.5, .5]) # Normalisation t = t / np.max(t) + 0.001 scaler = StandardScaler() scaler.fit(X) X = scaler.transform(X) x_train, x_test, t_train, t_test, d_train, d_test, c_train, c_test = train_test_split(X, t, d, c, test_size=.3, random_state=args.seed) elif args.data == 'hemo': c = args.num_clusters x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, \ c_test = generate_hemo(seed=args.seed, label=c) else: NotImplementedError('This dataset is not supported!') # Wrap t, d, and c together y_train = np.stack([t_train, d_train, c_train], axis=1) if val: y_valid = np.stack([t_valid, d_valid, c_valid], axis=1) y_test = np.stack([t_test, d_test, c_test], axis=1) if val: return x_train, x_valid, x_test, y_train, y_valid, y_test else: return x_train, x_test, x_test, y_train, y_test, y_test def run_experiment(args): timestr = time.strftime("%Y%m%d-%H%M%S") ex_name = "{}_{}".format(str(timestr), uuid.uuid4().hex[:5]) x_train, x_valid, x_test, y_train, y_valid, y_test = get_data(args) # Check variances of columns feat_var = np.var(x_train, axis=0) # Filter out features with low variance x_train = x_train[:, feat_var > 0.0001] x_valid = x_valid[:, feat_var > 0.0001] x_test = x_test[:, feat_var > 0.0001] print("Remaining dimensions: " + str(x_train.shape)) ssc = SSC_Bair(n_clusters=args.num_clusters, input_dim=x_train.shape[1], clustering_dim=args.clustering_dim, random_state=args.seed, penalty_weight=.1) ssc = ssc.fit(X=x_train, t=y_train[:, 0], d=y_train[:, 1]) # Look for the best dimensionality for clustering and return the best result # NOTE: this is way too optimistic ssc, d_best, nmi_best = find_best_dim(ssc, c=y_train[:, 2], step=50) print("Best clustering dim.: " + str(d_best)) # Training set performance yy = ssc.predict(X=x_train) acc = utils.cluster_acc(y_train[:, 2], yy) nmi = normalized_mutual_info_score(y_train[:, 2], yy) ari = adjusted_rand_score(y_train[:, 2], yy) ci = 0.5 #cindex(t=y_train[:, 0], d=y_train[:, 1], scores_pred=risk_scores) if args.data == 'MNIST': f = open("results_MNIST_SSC.txt", "a+") elif args.data == 'sim': f = open("results_sim_SSC.txt", "a+") elif args.data == 'liverani': f = open("results_liverani_SSC.txt", "a+") elif args.data == 'hemo': f = open("results_hemo_SSC.txt", "a+") f.write("Accuracy train: %f, NMI: %f, ARI: %f. CI train: %f.\n" % (acc, nmi, ari, ci)) # Test set performance yy = ssc.predict(X=x_test) acc = utils.cluster_acc(y_test[:, 2], yy) nmi = normalized_mutual_info_score(y_test[:, 2], yy) ari = adjusted_rand_score(y_test[:, 2], yy) ci = 0.5 f.write("Accuracy test: %f, NMI: %f, ARI: %f. CI test: %f.\n" % (acc, nmi, ari, ci)) f.close() print(str(acc)) print(str(nmi)) print(str(ari)) print(str(ci)) def main(): parser = argparse.ArgumentParser() parser.add_argument('--data', default='MNIST', type=str, choices=['MNIST', 'sim', 'hemo'], help='specify the data (MNIST, sim, hemo)') parser.add_argument('--num_clusters', default=5, type=int, help='specify the number of clusters') parser.add_argument('--clustering_dim', default=50, type=int, help='specify the number of features to use for clustering') parser.add_argument('--penalty', default=.0, type=float, help='specify the penalty weight for the Cox PH regression (default: 0.0)') parser.add_argument('--seed', default=42, type=int, help='specify the random generator seed') args = parser.parse_args() run_experiment(args) if __name__ == "__main__": main()
7,423
40.943503
128
py
vadesc
vadesc-main/baselines/sca/sca_utils/pre_processing.py
""" Some utility functions for data preprocessing taken from Chapfuwa et al.: https://github.com/paidamoyo/survival_cluster_analysis """ import numpy as np import pandas def one_hot_encoder(data, encode): print("Encoding data:{}".format(data.shape)) data_encoded = data.copy() encoded = pandas.get_dummies(data_encoded, prefix=encode, columns=encode) print("head of data:{}, data shape:{}".format(data_encoded.head(), data_encoded.shape)) print("Encoded:{}, one_hot:{}{}".format(encode, encoded.shape, encoded[0:5])) return encoded def log_transform(data, transform_ls): dataframe_update = data def transform(x): constant = 1e-8 transformed_data = np.log(x + constant) # print("max:{}, min:{}".format(np.max(transformed_data), np.min(transformed_data))) return np.abs(transformed_data) for column in transform_ls: df_column = dataframe_update[column] print(" before log transform: column:{}{}".format(column, df_column.head())) print("stats:max: {}, min:{}".format(df_column.max(), df_column.min())) dataframe_update[column] = dataframe_update[column].apply(transform) print(" after log transform: column:{}{}".format(column, dataframe_update[column].head())) return dataframe_update def formatted_data(x, t, e, idx, imputation_values=None): death_time = np.array(t[idx], dtype=float) censoring = np.array(e[idx], dtype=float) covariates = np.array(x[idx]) if imputation_values is not None: impute_covariates = impute_missing(data=covariates, imputation_values=imputation_values) else: impute_covariates = x survival_data = {'x': impute_covariates, 't': death_time, 'e': censoring} assert np.sum(np.isnan(impute_covariates)) == 0 return survival_data def get_train_median_mode(x, categorial): categorical_flat = flatten_nested(categorial) print("categorical_flat:{}".format(categorical_flat)) imputation_values = [] print("len covariates:{}, categorical:{}".format(x.shape[1], len(categorical_flat))) median = np.nanmedian(x, axis=0) mode = [] for idx in np.arange(x.shape[1]): a = x[:, idx] (_, idx, counts) = np.unique(a, return_index=True, return_counts=True) index = idx[np.argmax(counts)] mode_idx = a[index] mode.append(mode_idx) for i in np.arange(x.shape[1]): if i in categorical_flat: imputation_values.append(mode[i]) else: imputation_values.append(median[i]) print("imputation_values:{}".format(imputation_values)) return imputation_values def missing_proportion(dataset): missing = 0 columns = np.array(dataset.columns.values) for column in columns: missing += dataset[column].isnull().sum() return 100 * (missing / (dataset.shape[0] * dataset.shape[1])) def one_hot_indices(dataset, one_hot_encoder_list): indices_by_category = [] for colunm in one_hot_encoder_list: values = dataset.filter(regex="{}_.*".format(colunm)).columns.values # print("values:{}".format(values, len(values))) indices_one_hot = [] for value in values: indice = dataset.columns.get_loc(value) # print("column:{}, indice:{}".format(colunm, indice)) indices_one_hot.append(indice) indices_by_category.append(indices_one_hot) # print("one_hot_indices:{}".format(indices_by_category)) return indices_by_category def flatten_nested(list_of_lists): flattened = [val for sublist in list_of_lists for val in sublist] return flattened def print_missing_prop(covariates): missing = np.array(np.isnan(covariates), dtype=float) shape = np.shape(covariates) proportion = np.sum(missing) / (shape[0] * shape[1]) print("missing_proportion:{}".format(proportion)) def impute_missing(data, imputation_values): copy = data for i in np.arange(len(data)): row = data[i] indices = np.isnan(row) for idx in np.arange(len(indices)): if indices[idx]: # print("idx:{}, imputation_values:{}".format(idx, np.array(imputation_values)[idx])) copy[i][idx] = imputation_values[idx] # print("copy;{}".format(copy)) return copy
4,311
35.542373
101
py
vadesc
vadesc-main/models/losses.py
""" Loss functions for the reconstruction term of the ELBO. """ import tensorflow as tf class Losses: def __init__(self, configs): self.input_dim = configs['training']['inp_shape'] self.tuple = False if isinstance(self.input_dim, list): print("\nData is tuple!\n") self.tuple = True self.input_dim = self.input_dim[0] * self.input_dim[1] def loss_reconstruction_binary(self, inp, x_decoded_mean): x = inp # NB: transpose to make the first dimension correspond to MC samples if self.tuple: x_decoded_mean = tf.transpose(x_decoded_mean, perm=[1, 0, 2, 3]) else: x_decoded_mean = tf.transpose(x_decoded_mean, perm=[1, 0, 2]) loss = self.input_dim * tf.math.reduce_mean(tf.stack([tf.keras.losses.BinaryCrossentropy()(x, x_decoded_mean[i]) for i in range(x_decoded_mean.shape[0])], axis=-1), axis=-1) return loss def loss_reconstruction_mse(self, inp, x_decoded_mean): x = inp # NB: transpose to make the first dimension correspond to MC samples if self.tuple: x_decoded_mean = tf.transpose(x_decoded_mean, perm=[1, 0, 2, 3]) else: x_decoded_mean = tf.transpose(x_decoded_mean, perm=[1, 0, 2]) loss = self.input_dim * tf.math.reduce_mean(tf.stack([tf.keras.losses.MeanSquaredError()(x, x_decoded_mean[i]) for i in range(x_decoded_mean.shape[0])], axis=-1), axis=-1) return loss
1,721
43.153846
120
py
vadesc
vadesc-main/models/model.py
""" VaDeSC model. """ import tensorflow as tf import tensorflow_probability as tfp import os from models.networks import (VGGEncoder, VGGDecoder, Encoder, Decoder, Encoder_small, Decoder_small) from utils.utils import weibull_scale, weibull_log_pdf, tensor_slice # Pretrain autoencoder checkpoint_path = "autoencoder/cp.ckpt" checkpoint_dir = os.path.dirname(checkpoint_path) tfd = tfp.distributions tfkl = tf.keras.layers tfpl = tfp.layers tfk = tf.keras class GMM_Survival(tf.keras.Model): def __init__(self, **kwargs): super(GMM_Survival, self).__init__(name="GMM_Survival") self.encoded_size = kwargs['latent_dim'] self.num_clusters = kwargs['num_clusters'] self.inp_shape = kwargs['inp_shape'] self.activation = kwargs['activation'] self.survival = kwargs['survival'] self.s = kwargs['monte_carlo'] self.sample_surv = kwargs['sample_surv'] self.learn_prior = kwargs['learn_prior'] if isinstance(self.inp_shape, list): self.encoder = VGGEncoder(encoded_size=self.encoded_size) self.decoder = VGGDecoder(input_shape=[256, 256, 1], activation='none') elif self.inp_shape <= 100: self.encoder = Encoder_small(self.encoded_size) self.decoder = Decoder_small(self.inp_shape, self.activation) else: self.encoder = Encoder(self.encoded_size) self.decoder = Decoder(self.inp_shape, self.activation) self.c_mu = tf.Variable(tf.initializers.GlorotNormal()(shape=[self.num_clusters, self.encoded_size]), name='mu') self.log_c_sigma = tf.Variable(tf.initializers.GlorotNormal()([self.num_clusters, self.encoded_size]), name="sigma") # Cluster-specific survival model parameters self.c_beta = tf.Variable(tf.initializers.GlorotNormal()(shape=[self.num_clusters, self.encoded_size + 1]), name='beta') # Weibull distribution shape parameter self.weibull_shape = kwargs['weibull_shape'] if self.learn_prior: self.prior_logits = tf.Variable(tf.ones([self.num_clusters]), name="prior") else: self.prior = tf.constant(tf.ones([self.num_clusters]) * (1 / self.num_clusters)) self.use_t = tf.Variable([1.0], trainable=False) def call(self, inputs, training=True): # NB: inputs have to include predictors/covariates/features (x), time-to-event (t), and # event indicators (d). d[i] == 1 if the i-th event is a death, and d[i] == 0 otherwise. x, y = inputs t = y[:, 0] d = y[:, 1] enc_input = x z_mu, log_z_sigma = self.encoder(enc_input) tf.debugging.check_numerics(z_mu, message="z_mu") z = tfd.MultivariateNormalDiag(loc=z_mu, scale_diag=tf.math.sqrt(tf.math.exp(log_z_sigma))) if training: z_sample = z.sample(self.s) else: z_sample = tf.expand_dims(z_mu, 0) tf.debugging.check_numerics(self.c_mu, message="c_mu") tf.debugging.check_numerics(self.log_c_sigma, message="c_sigma") c_sigma = tf.math.exp(self.log_c_sigma) # p(z|c) p_z_c = tf.stack([tf.math.log( tfd.MultivariateNormalDiag(loc=tf.cast(self.c_mu[i, :], tf.float64), scale_diag=tf.math.sqrt(tf.cast(c_sigma[i, :], tf.float64))).prob( tf.cast(z_sample, tf.float64)) + 1e-60) for i in range(self.num_clusters)], axis=-1) tf.debugging.check_numerics(p_z_c, message="p_z_c") # prior p(c) if self.learn_prior: prior_logits = tf.math.abs(self.prior_logits) norm = tf.math.reduce_sum(prior_logits, keepdims=True) prior = prior_logits / (norm + 1e-60) else: prior = self.prior tf.debugging.check_numerics(prior, message="prior") if self.survival: # Compute Weibull distribution's scale parameter, given z and c tf.debugging.check_numerics(self.c_beta, message="c_beta") if self.sample_surv: lambda_z_c = tf.stack([weibull_scale(x=z_sample, beta=self.c_beta[i, :]) for i in range(self.num_clusters)], axis=-1) else: lambda_z_c = tf.stack([weibull_scale(x=tf.stack([z_mu for i in range(self.s)], axis=0), beta=self.c_beta[i, :]) for i in range(self.num_clusters)], axis=-1) tf.debugging.check_numerics(lambda_z_c, message="lambda_z_c") # Evaluate p(t|z,c), assuming t|z,c ~ Weibull(lambda_z_c, self.weibull_shape) p_t_z_c = tf.stack([weibull_log_pdf(t=t, d=d, lmbd=lambda_z_c[:, :, i], k=self.weibull_shape) for i in range(self.num_clusters)], axis=-1) p_t_z_c = tf.clip_by_value(p_t_z_c, -1e+64, 1e+64) tf.debugging.check_numerics(p_t_z_c, message="p_t_z_c") p_c_z = tf.math.log(tf.cast(prior, tf.float64) + 1e-60) + tf.cast(p_z_c, tf.float64) + p_t_z_c else: p_c_z = tf.math.log(tf.cast(prior, tf.float64) + 1e-60) + tf.cast(p_z_c, tf.float64) p_c_z = tf.nn.log_softmax(p_c_z, axis=-1) p_c_z = tf.math.exp(p_c_z) tf.debugging.check_numerics(p_c_z, message="p_c_z") if self.survival: loss_survival = -tf.reduce_sum(tf.multiply(p_t_z_c, tf.cast(p_c_z, tf.float64)), axis=-1) tf.debugging.check_numerics(loss_survival, message="loss_survival") loss_clustering = - tf.reduce_sum(tf.multiply(tf.cast(p_c_z, tf.float64), tf.cast(p_z_c, tf.float64)), axis=-1) loss_prior = - tf.math.reduce_sum(tf.math.xlogy(tf.cast(p_c_z, tf.float64), 1e-60 + tf.cast(prior, tf.float64)), axis=-1) loss_variational_1 = - 1 / 2 * tf.reduce_sum(log_z_sigma + 1, axis=-1) loss_variational_2 = tf.math.reduce_sum(tf.math.xlogy(tf.cast(p_c_z, tf.float64), 1e-60 + tf.cast(p_c_z, tf.float64)), axis=-1) tf.debugging.check_numerics(loss_clustering, message="loss_clustering") tf.debugging.check_numerics(loss_prior, message="loss_prior") tf.debugging.check_numerics(loss_variational_1, message="loss_variational_1") tf.debugging.check_numerics(loss_variational_2, message="loss_variational_2") if self.survival: self.add_loss(tf.math.reduce_mean(loss_survival)) self.add_loss(tf.math.reduce_mean(loss_clustering)) self.add_loss(tf.math.reduce_mean(loss_prior)) self.add_loss(tf.math.reduce_mean(loss_variational_1)) self.add_loss(tf.math.reduce_mean(loss_variational_2)) # Logging metrics in TensorBoard self.add_metric(loss_clustering, name='loss_clustering', aggregation="mean") self.add_metric(loss_prior, name='loss_prior', aggregation="mean") self.add_metric(loss_variational_1, name='loss_variational_1', aggregation="mean") self.add_metric(loss_variational_2, name='loss_variational_2', aggregation="mean") if self.survival: self.add_metric(loss_survival, name='loss_survival', aggregation="mean") dec = self.decoder(z_sample) # Evaluate risk scores based on hard clustering assignments # Survival time may ba unobserved, so a special procedure is needed when time is not observed... p_z_c = p_z_c[0] # take the first sample p_c_z = p_c_z[0] if self.survival: lambda_z_c = lambda_z_c[0] # Take the first sample # Use Bayes rule to compute p(c|z) instead of p(c|z,t), since t is unknown p_c_z_nt = tf.math.log(tf.cast(prior, tf.float64) + 1e-60) + tf.cast(p_z_c, tf.float64) p_c_z_nt = tf.nn.log_softmax(p_c_z_nt, axis=-1) p_c_z_nt = tf.math.exp(p_c_z_nt) inds_nt = tf.dtypes.cast(tf.argmax(p_c_z_nt, axis=-1), tf.int32) risk_scores_nt = tensor_slice(target_tensor=tf.cast(lambda_z_c, tf.float64), index_tensor=inds_nt) inds = tf.dtypes.cast(tf.argmax(p_c_z, axis=-1), tf.int32) risk_scores_t = tensor_slice(target_tensor=lambda_z_c, index_tensor=inds) p_c_z = tf.cond(self.use_t[0] < 0.5, lambda: p_c_z_nt, lambda: p_c_z) risk_scores = tf.cond(self.use_t[0] < 0.5, lambda: risk_scores_nt, lambda: risk_scores_t) else: inds = tf.dtypes.cast(tf.argmax(p_c_z, axis=-1), tf.int32) risk_scores = tensor_slice(target_tensor=p_c_z, index_tensor=inds) lambda_z_c = risk_scores p_z_c = tf.cast(p_z_c, tf.float64) if isinstance(self.inp_shape, list): dec = tf.transpose(dec, [1, 0, 2, 3, 4]) else: dec = tf.transpose(dec, [1, 0, 2]) z_sample = tf.transpose(z_sample, [1, 0, 2]) risk_scores = tf.expand_dims(risk_scores, -1) return dec, z_sample, p_z_c, p_c_z, risk_scores, lambda_z_c def generate_samples(self, j, n_samples): z = tfd.MultivariateNormalDiag(loc=self.c_mu[j, :], scale_diag=tf.math.sqrt(tf.math.exp(self.log_c_sigma[j, :]))) z_sample = z.sample(n_samples) dec = self.decoder(tf.expand_dims(z_sample, 0)) return dec
9,434
48.657895
124
py
vadesc
vadesc-main/models/networks.py
""" Encoder and decoder architectures used by VaDeSC. """ import tensorflow as tf import tensorflow_probability as tfp from tensorflow.keras import layers tfd = tfp.distributions tfkl = tf.keras.layers tfpl = tfp.layers tfk = tf.keras # Wide MLP encoder and decoder architectures class Encoder(layers.Layer): def __init__(self, encoded_size): super(Encoder, self).__init__(name='encoder') self.dense1 = tfkl.Dense(500, activation='relu') self.dense2 = tfkl.Dense(500, activation='relu') self.dense3 = tfkl.Dense(2000, activation='relu') self.mu = tfkl.Dense(encoded_size, activation=None) self.sigma = tfkl.Dense(encoded_size, activation=None) def call(self, inputs, **kwargs): x = tfkl.Flatten()(inputs) x = self.dense1(x) x = self.dense2(x) x = self.dense3(x) mu = self.mu(x) sigma = self.sigma(x) return mu, sigma class Decoder(layers.Layer): def __init__(self, input_shape, activation): super(Decoder, self).__init__(name='dec') self.inp_shape = input_shape self.dense1 = tfkl.Dense(2000, activation='relu') self.dense2 = tfkl.Dense(500, activation='relu') self.dense3 = tfkl.Dense(500, activation='relu') if activation == "sigmoid": self.dense4 = tfkl.Dense(self.inp_shape, activation="sigmoid") else: self.dense4 = tfkl.Dense(self.inp_shape) def call(self, inputs, **kwargs): x = self.dense1(inputs) x = self.dense2(x) x = self.dense3(x) x = self.dense4(x) return x # VGG-based architectures class VGGConvBlock(layers.Layer): def __init__(self, num_filters, block_id): super(VGGConvBlock, self).__init__(name="VGGConvBlock{}".format(block_id)) self.conv1 = tfkl.Conv2D(filters=num_filters, kernel_size=(3, 3), activation='relu') self.conv2 = tfkl.Conv2D(filters=num_filters, kernel_size=(3, 3), activation='relu') self.maxpool = tfkl.MaxPooling2D((2, 2)) def call(self, inputs, **kwargs): out = self.conv1(inputs) out = self.conv2(out) out = self.maxpool(out) return out class VGGDeConvBlock(layers.Layer): def __init__(self, num_filters, block_id): super(VGGDeConvBlock, self).__init__(name="VGGDeConvBlock{}".format(block_id)) self.upsample = tfkl.UpSampling2D((2, 2), interpolation='bilinear') self.convT1 = tfkl.Conv2DTranspose(filters=num_filters, kernel_size=(3, 3), padding='valid', activation='relu') self.convT2 = tfkl.Conv2DTranspose(filters=num_filters, kernel_size=(3, 3), padding='valid', activation='relu') def call(self, inputs, **kwargs): out = self.upsample(inputs) out = self.convT1(out) out = self.convT2(out) return out class VGGEncoder(layers.Layer): def __init__(self, encoded_size): super(VGGEncoder, self).__init__(name='VGGEncoder') self.layers = [VGGConvBlock(32, 1), VGGConvBlock(64, 2)] self.mu = tfkl.Dense(encoded_size, activation=None) self.sigma = tfkl.Dense(encoded_size, activation=None) def call(self, inputs, **kwargs): out = inputs # Iterate through blocks for block in self.layers: out = block(out) out_flat = tfkl.Flatten()(out) mu = self.mu(out_flat) sigma = self.sigma(out_flat) return mu, sigma class VGGDecoder(layers.Layer): def __init__(self, input_shape, activation): super(VGGDecoder, self).__init__(name='VGGDecoder') target_shape = (13, 13, 64) # 64 x 64 self.activation = activation self.dense = tfkl.Dense(target_shape[0] * target_shape[1] * target_shape[2]) self.reshape = tfkl.Reshape(target_shape=target_shape) self.layers = [VGGDeConvBlock(64, 1), VGGDeConvBlock(32, 2)] self.convT = tfkl.Conv2DTranspose(filters=input_shape[2], kernel_size=3, padding='same') def call(self, inputs, **kwargs): out = self.dense(inputs[0]) out = self.reshape(out) # Iterate through blocks for block in self.layers: out = block(out) # Last convolution out = self.convT(out) if self.activation == "sigmoid": out = tf.sigmoid(out) return tf.expand_dims(out, 0) # Smaller encoder and decoder architectures for low-dimensional datasets class Encoder_small(layers.Layer): def __init__(self, encoded_size): super(Encoder_small, self).__init__(name='encoder') self.dense1 = tfkl.Dense(50, activation='relu') self.dense2 = tfkl.Dense(100, activation='relu') self.mu = tfkl.Dense(encoded_size, activation=None) self.sigma = tfkl.Dense(encoded_size, activation=None) def call(self, inputs): x = tfkl.Flatten()(inputs) x = self.dense1(x) x = self.dense2(x) mu = self.mu(x) sigma = self.sigma(x) return mu, sigma class Decoder_small(layers.Layer): def __init__(self, input_shape, activation): super(Decoder_small, self).__init__(name='dec') self.inp_shape = input_shape self.dense1 = tfkl.Dense(100, activation='relu') self.dense2 = tfkl.Dense(50, activation='relu') if activation == "sigmoid": print("yeah") self.dense4 = tfkl.Dense(self.inp_shape, activation="sigmoid") else: self.dense4 = tfkl.Dense(self.inp_shape) def call(self, inputs): x = self.dense1(inputs) x = self.dense2(x) x = self.dense4(x) return x
5,648
32.229412
119
py
vadesc
vadesc-main/datasets/simulated_data.py
""" Returns the synthetic data. """ from datasets.simulations import format_profile_surv_data_tf def generate_data(): preproc = format_profile_surv_data_tf(p=100, n=1000, k=5, p_cens=0.2, seed=42, clust_mean=False, clust_cov=False, clust_intercepts=False, density=0.2, weibull_k=1, xrange=[-5, 5], brange=[-2.5, 2.5]) return preproc
427
34.666667
117
py
vadesc
vadesc-main/datasets/simulations.py
""" Numerical simulations and utility functions for constructing the synthetic dataset. """ import numpy as np from numpy.random import multivariate_normal, uniform, choice from sklearn.datasets import make_spd_matrix from scipy.stats import weibull_min from utils.sim_utils import random_nonlin_map from baselines.sca.sca_utils.pre_processing import formatted_data def simulate_profile_surv(p: int, n: int, k: int, p_cens: float, seed: int, p_c=None, balanced=False, clust_mean=True, clust_cov=True, isotropic=False, clust_coeffs=True, clust_intercepts=True, density=0.2, weibull_k=1, xrange=[-5, 5], brange=[-1, 1]): """ Simulates data with heterogeneous survival profiles. :param p: number of predictor variables. :param n: number of data points. :param k: nmber of clusters. :param p_cens: probability of censoring. :param seed: random generator seed. :param p_c: prior probabilities of clusters. :param balanced: if p_c is not specified, should cluster sizes be balanced? :param clust_mean: should predictors have clusterwise means? :param clust_cov: should predictors have clusterwise covariance matrices? :param isotropic: should predictor covariance matrices be isotropic? :param clust_coeffs: should survival time predictor coefficients be cluster-specific? :param clust_intercepts: should survival time intercept be cluster-specific? :param density: proportion of predictor variables contributing to survival time. :param weibull_k: the shape parameter of the Weibull distribution for survival time (> 0) :param xrange: range for the mean of predictors. :param brange: range for the survival coefficients. :return: """ # Replicability np.random.seed(seed) # Sanity checks assert p > 0 and n > 0 and k > 0 assert 1 < k < n assert len(xrange) == 2 and xrange[0] < xrange[1] assert len(brange) == 2 and brange[0] < brange[1] assert 0 < density <= 1.0 and int((1 - density) * p) >= 1 assert weibull_k > 0 # Cluster prior prob-s if p_c is not None: assert len(p_c) == k and sum(p_c) == 1 else: if balanced: p_c = np.ones((k, )) / k else: p_c = uniform(0, 1, (k, )) p_c = p_c / np.sum(p_c) # Cluster assignments c = choice(a=np.arange(k), size=(n, ), replace=True, p=p_c) # Cluster-specific means means = np.zeros((k, p)) mu = uniform(xrange[0], xrange[1], (1, p)) for l in range(k): if clust_mean: mu_l = uniform(xrange[0], xrange[1], (1, p)) means[l, :] = mu_l else: means[l, :] = mu # Cluster-specific covariances cov_mats = [] sigma = make_spd_matrix(p, random_state=seed) if isotropic: sigma = sigma * np.eye(p) for l in range(k): if clust_cov: sigma_l = make_spd_matrix(p, random_state=(seed + l)) if isotropic: sigma_l = sigma_l * np.eye(p) cov_mats.append(sigma_l) else: cov_mats.append(sigma) # Predictors X = np.zeros((n, p)) for l in range(k): n_l = np.sum(c == l) X_l = multivariate_normal(mean=means[l, :], cov=cov_mats[l], size=n_l) X[c == l, :] = X_l # Cluster-specific coefficients for the survival model coeffs = np.zeros((k, p)) intercepts = np.zeros((k, )) beta = uniform(brange[0], brange[1], (1, p)) beta0 = uniform(brange[0], brange[1], (1, 1)) n_zeros = int((1 - density) * p) zero_coeffs = choice(np.arange(p), (n_zeros, ), replace=False) beta[:, zero_coeffs] = 0.0 for l in range(k): if clust_coeffs: beta_l = uniform(brange[0], brange[1], (1, p)) zero_coeffs_l = choice(np.arange(p), (n_zeros, ), replace=False) beta_l[:, zero_coeffs_l] = 0.0 coeffs[l, :] = beta_l else: coeffs[l, :] = beta if clust_intercepts: beta0_l = uniform(brange[0], brange[1], (1, 1)) intercepts[l] = beta0_l else: intercepts[l] = beta0 # Survival times t = np.zeros((n, )) for l in range(k): n_l = np.sum(c == l) X_l = X[c == l, :] coeffs_l = np.expand_dims(coeffs[l, :], 1) intercept_l = intercepts[l] logexps_l = np.log(1 + np.exp(intercept_l + np.squeeze(np.matmul(X_l, coeffs_l)))) t_l = weibull_min.rvs(weibull_k, loc=0, scale=logexps_l, size=n_l) t[c == l] = t_l # Censoring # NB: d == 1 if failure; 0 if censored d = (uniform(0, 1, (n, )) >= p_cens) * 1.0 t_cens = uniform(0, t, (n, )) t[d == 0] = t_cens[d == 0] return X, t, d, c, means, cov_mats, coeffs, intercepts def simulate_nonlin_profile_surv(p: int, n: int, k: int, latent_dim: int, p_cens: float, seed: int, p_c=None, balanced=False, clust_mean=True, clust_cov=True, isotropic=False, clust_coeffs=True, clust_intercepts=True, weibull_k=1, xrange=[-5, 5], brange=[-1, 1]): """ Simulates data with heterogeneous survival profiles and nonlinear (!) relationships (covariates are generated from latent features using an MLP decoder). """ # Replicability np.random.seed(seed) # Sanity checks assert p > 0 and latent_dim > 0 and n > 0 and k > 0 assert 1 < k < n assert latent_dim < p assert len(xrange) == 2 and xrange[0] < xrange[1] assert len(brange) == 2 and brange[0] < brange[1] assert weibull_k > 0 # Cluster prior prob-s if p_c is not None: assert len(p_c) == k and sum(p_c) == 1 else: if balanced: p_c = np.ones((k, )) / k else: p_c = uniform(0, 1, (k, )) p_c = p_c / np.sum(p_c) # Cluster assignments c = choice(a=np.arange(k), size=(n, ), replace=True, p=p_c) # Cluster-specific means means = np.zeros((k, latent_dim)) mu = uniform(xrange[0], xrange[1], (1, latent_dim)) for l in range(k): if clust_mean: mu_l = uniform(xrange[0], xrange[1], (1, latent_dim)) means[l, :] = mu_l else: means[l, :] = mu # Cluster-specific covariances cov_mats = [] sigma = make_spd_matrix(latent_dim, random_state=seed) if isotropic: sigma = sigma * np.eye(latent_dim) for l in range(k): if clust_cov: sigma_l = make_spd_matrix(latent_dim, random_state=(seed + l)) if isotropic: sigma_l = sigma_l * np.eye(latent_dim) cov_mats.append(sigma_l) else: cov_mats.append(sigma) # Latent features Z = np.zeros((n, latent_dim)) for l in range(k): n_l = np.sum(c == l) Z_l = multivariate_normal(mean=means[l, :], cov=cov_mats[l], size=n_l) Z[c == l, :] = Z_l # Predictors mlp_dec = random_nonlin_map(n_in=latent_dim, n_out=p, n_hidden=int((latent_dim + p) / 2)) X = mlp_dec(Z) # Cluster-specific coefficients for the survival model coeffs = np.zeros((k, latent_dim)) intercepts = np.zeros((k, )) beta = uniform(brange[0], brange[1], (1, latent_dim)) beta0 = uniform(brange[0], brange[1], (1, 1)) for l in range(k): if clust_coeffs: beta_l = uniform(brange[0], brange[1], (1, latent_dim)) coeffs[l, :] = beta_l else: coeffs[l, :] = beta if clust_intercepts: beta0_l = uniform(brange[0], brange[1], (1, 1)) intercepts[l] = beta0_l else: intercepts[l] = beta0 # Survival times t = np.zeros((n, )) for l in range(k): n_l = np.sum(c == l) Z_l = Z[c == l, :] coeffs_l = np.expand_dims(coeffs[l, :], 1) intercept_l = intercepts[l] logexps_l = np.log(1 + np.exp(intercept_l + np.squeeze(np.matmul(Z_l, coeffs_l)))) t_l = weibull_min.rvs(weibull_k, loc=0, scale=logexps_l, size=n_l) t[c == l] = t_l # Censoring # NB: d == 1 if failure; 0 if censored d = (uniform(0, 1, (n, )) >= p_cens) * 1.0 t_cens = uniform(0, t, (n, )) t[d == 0] = t_cens[d == 0] return X, t, d, c, Z, mlp_dec, means, cov_mats, coeffs, intercepts def format_profile_surv_data_tf(p: int, n: int, k: int, p_cens: float, seed: int, p_c=None, balanced=False, clust_mean=True, clust_cov=True, isotropic=False, clust_coeffs=True, clust_intercepts=True, density=0.2, weibull_k=1, xrange=[-5, 5], brange=[-1, 1]): # Generates data with heterogeneous survival profiles, performs train-validation-test split, and returns data in the # same format as in the code of SCA by Chapfuwa et al. np.random.seed(seed) # Simulate the data X, t, d, c, means, cov_mats, coeffs, intercepts = simulate_profile_surv(p, n, k, p_cens, seed, p_c, balanced, clust_mean, clust_cov, isotropic, clust_coeffs, clust_intercepts, density, weibull_k, xrange, brange) # Renaming x = X e = d print("x:{}, t:{}, e:{}, len:{}".format(x[0], t[0], e[0], len(t))) idx = np.arange(0, x.shape[0]) print("x_shape:{}".format(x.shape)) np.random.shuffle(idx) x = x[idx] t = t[idx] e = e[idx] end_time = max(t) print("end_time:{}".format(end_time)) print("observed percent:{}".format(sum(e) / len(e))) num_examples = int(0.80 * len(e)) print("num_examples:{}".format(num_examples)) train_idx = idx[0: num_examples] split = int((len(t) - num_examples) / 2) test_idx = idx[num_examples: num_examples + split] valid_idx = idx[num_examples + split: len(t)] print("test:{}, valid:{}, train:{}, all: {}".format(len(test_idx), len(valid_idx), num_examples, len(test_idx) + len(valid_idx) + num_examples)) preprocessed = { 'train': formatted_data(x=x, t=t, e=e, idx=train_idx, imputation_values=None), 'test': formatted_data(x=x, t=t, e=e, idx=test_idx, imputation_values=None), 'valid': formatted_data(x=x, t=t, e=e, idx=valid_idx, imputation_values=None) } return preprocessed def format_nonlin_profile_surv_data_tf(p: int, n: int, k: int, latent_dim: int, p_cens: float, seed: int, p_c=None, balanced=False, clust_mean=True, clust_cov=True, isotropic=False, clust_coeffs=True, clust_intercepts=True, weibull_k=1, xrange=[-5, 5], brange=[-1, 1]): # Generates data with heterogeneous survival profiles and nonlinear relationships, # performs train-validation-test split, and returns data in the same format as in the code of SCA by Chapfuwa et al. np.random.seed(seed) # Simulate the data X, t, d, c, Z, mus, sigmas, betas, betas_0, mlp_dec = simulate_nonlin_profile_surv(p=p, n=n, latent_dim=latent_dim, k=k, p_cens=p_cens, seed=seed, clust_mean=clust_mean, clust_cov=clust_cov, clust_coeffs=clust_coeffs, clust_intercepts=clust_intercepts, balanced=balanced, weibull_k=weibull_k, brange=brange, isotropic=isotropic, xrange=xrange) # Renaming x = X e = d print("x:{}, t:{}, e:{}, len:{}".format(x[0], t[0], e[0], len(t))) idx = np.arange(0, x.shape[0]) print("x_shape:{}".format(x.shape)) np.random.shuffle(idx) x = x[idx] t = t[idx] e = e[idx] end_time = max(t) print("end_time:{}".format(end_time)) print("observed percent:{}".format(sum(e) / len(e))) num_examples = int(0.80 * len(e)) print("num_examples:{}".format(num_examples)) train_idx = idx[0: num_examples] split = int((len(t) - num_examples) / 2) test_idx = idx[num_examples: num_examples + split] valid_idx = idx[num_examples + split: len(t)] print("test:{}, valid:{}, train:{}, all: {}".format(len(test_idx), len(valid_idx), num_examples, len(test_idx) + len(valid_idx) + num_examples)) preprocessed = { 'train': formatted_data(x=x, t=t, e=e, idx=train_idx, imputation_values=None), 'test': formatted_data(x=x, t=t, e=e, idx=test_idx, imputation_values=None), 'valid': formatted_data(x=x, t=t, e=e, idx=valid_idx, imputation_values=None) } return preprocessed
13,134
37.632353
120
py
vadesc
vadesc-main/datasets/flchain/flchain_data.py
""" FLChain dataset. Based on the code from Chapfuwa et al.: https://github.com/paidamoyo/survival_cluster_analysis """ # age: age in years # sex: F=female, M=male # sample.yr: the calendar year in which a blood sample was obtained # kappa: serum free light chain, kappa portion # lambda: serum free light chain, lambda portion # flc.grp: the FLC group for the subject, as used in the original analysis # creatinine: serum creatinine # mgus: 1 if the subject had been diagnosed with monoclonal gammapothy (MGUS) # futime: days from enrollment until death. There are 3 subjects whose sample was obtained on their death date. # death 0=alive at last contact date, 1=dead # chapter: for those who died, a grouping of their primary cause of death by chapter headings of # the International Code of Diseases ICD-9 import os import numpy as np import pandas from baselines.sca.sca_utils.pre_processing import one_hot_encoder, formatted_data, missing_proportion, \ one_hot_indices, get_train_median_mode from sklearn.preprocessing import StandardScaler def generate_data(seed): np.random.seed(seed) dir_path = os.path.dirname(os.path.realpath(__file__)) path = os.path.abspath(os.path.join(dir_path, '', 'flchain.csv')) print("path:{}".format(path)) data_frame = pandas.read_csv(path, index_col=0) print("head of data:{}, data shape:{}".format(data_frame.head(), data_frame.shape)) # x_data = data_frame[['age', 'sex', 'kappa', 'lambda', 'flc.grp', 'creatinine', 'mgus']] # Preprocess to_drop = ['futime', 'death', 'chapter'] print("missing:{}".format(missing_proportion(data_frame.drop(labels=to_drop, axis=1)))) one_hot_encoder_list = ['sex', 'flc.grp', 'sample.yr'] data_frame = one_hot_encoder(data_frame, encode=one_hot_encoder_list) t_data = data_frame[['futime']] e_data = data_frame[['death']] c_data = data_frame[['death']] c_data['death'] = c_data['death'].astype('category') c_data['death'] = c_data['death'].cat.codes dataset = data_frame.drop(labels=to_drop, axis=1) print("head of dataset data:{}, data shape:{}".format(dataset.head(), dataset.shape)) encoded_indices = one_hot_indices(dataset, one_hot_encoder_list) include_idx = set(np.array(sum(encoded_indices, []))) mask = np.array([(i in include_idx) for i in np.arange(dataset.shape[1])]) print("data description:{}".format(dataset.describe())) covariates = np.array(dataset.columns.values) print("columns:{}".format(covariates)) x = np.array(dataset).reshape(dataset.shape) t = np.array(t_data).reshape(len(t_data)) e = np.array(e_data).reshape(len(e_data)) c = np.array(c_data).reshape(len(c_data)) print("x:{}, t:{}, e:{}, len:{}".format(x[0], t[0], e[0], len(t))) idx = np.arange(0, x.shape[0]) print("x_shape:{}".format(x.shape)) np.random.shuffle(idx) x = x[idx] t = t[idx] e = e[idx] c = c[idx] # Normalization t = t / np.max(t) + 0.001 scaler = StandardScaler() scaler.fit(x[:, ~mask]) x[:, ~mask] = scaler.transform(x[:, ~mask]) end_time = max(t) print("end_time:{}".format(end_time)) print("observed percent:{}".format(sum(e) / len(e))) print("shuffled x:{}, t:{}, e:{}, len:{}".format(x[0], t[0], e[0], len(t))) num_examples = int(0.80 * len(e)) print("num_examples:{}".format(num_examples)) train_idx = idx[0: num_examples] split = int((len(t) - num_examples) / 2) test_idx = idx[num_examples: num_examples + split] valid_idx = idx[num_examples + split: len(t)] print("test:{}, valid:{}, train:{}, all: {}".format(len(test_idx), len(valid_idx), num_examples, len(test_idx) + len(valid_idx) + num_examples)) imputation_values = get_train_median_mode(x=np.array(x[train_idx]), categorial=encoded_indices) preprocessed = { 'train': formatted_data(x=x, t=t, e=e, idx=train_idx, imputation_values=imputation_values), 'test': formatted_data(x=x, t=t, e=e, idx=test_idx, imputation_values=imputation_values), 'valid': formatted_data(x=x, t=t, e=e, idx=valid_idx, imputation_values=imputation_values) } preprocessed['train']['c'] = c[train_idx] preprocessed['valid']['c'] = c[valid_idx] preprocessed['test']['c'] = c[test_idx] return preprocessed def generate_flchain(seed=42): preproc = generate_data(seed) x_train = preproc['train']['x'] x_valid = preproc['valid']['x'] x_test = preproc['test']['x'] t_train = preproc['train']['t'] t_valid = preproc['valid']['t'] t_test = preproc['test']['t'] d_train = preproc['train']['e'] d_valid = preproc['valid']['e'] d_test = preproc['test']['e'] c_train = preproc['train']['c'] c_valid = preproc['valid']['c'] c_test = preproc['test']['c'] return x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test
4,967
38.744
113
py
vadesc
vadesc-main/datasets/hemodialysis/hemo_data.py
""" Dataset of children undergoing hemodialysis. """ import numpy as np import pandas as pd import os from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler def generate_hemo(seed=42, label=3): dir_path = os.path.dirname(os.path.realpath(__file__)) path = os.path.abspath(os.path.join(dir_path, '', 'data.csv')) df = pd.read_csv(path) df["cause.of.death"].loc[df['death'] == 0] = 'alive' df["fcensor.reason"] = df["fcensor.reason"].fillna(value='unkown') df["PatientRace4"] = df["PatientRace4"].fillna(value='unkown') df = df.interpolate(method="nearest") df["mean_rdw"][0] = df["mean_rdw"].mean() t = df['TIME'].to_numpy() t.astype(np.float64) del df['TIME'] d = df['death'].to_numpy() d = np.array(d, dtype=bool) del df['death'] del df['cause.of.death'] del df['fcensor.reason'] del df['fspktv3'] del df['raceB'] #clusters c_2 = df['fage2'].to_numpy() del df['fage2'] c_3 = df['fage3'].to_numpy() del df['fage3'] c_2[c_2 == '0-12 years'] = 0 c_2[c_2 == '>12 years'] = 1 c_3[c_3 == '<6 years'] = 0 c_3[c_3 == '6-12 years'] = 1 c_3[c_3 == '>12 years'] = 2 if label == 2: c = c_2 else: c = c_3 c = np.array(c, dtype=np.int64) df = pd.get_dummies(df) # Covariates to exclude (repetition) no_list = ['PatientRace4_unkown', 'raceB_African', 'fspktv4_(1.56,1.73]', #'fspktv4_[0.784,1.39]', 'USRDS_class_Etiology uncertain ', 'other', 'tidwg_day', 'tUFR_mLkgh', 'raceB_other', 'cDeath', 'cTIME', 'PatientIdentifier', 'PatientGender_Male', 'etiology2_other', 'PatientRace4_Other', 'etiology2_sec_glomerulonephritis_vasculitis'] for col in df.columns: if col in no_list: del df[col] data = df.to_numpy() X = StandardScaler().fit_transform(data) X = X.astype(np.float64) t = t / np.max(t) + 0.001 x_train, x_test, t_train, t_test, d_train, d_test, c_train, c_test = train_test_split(X, t, d, c, test_size=.3, random_state=seed) x_valid = x_test t_valid = t_test d_valid = d_test c_valid = c_test return x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test
2,403
32.859155
115
py
vadesc
vadesc-main/datasets/support/support_data.py
""" SUPPORT dataset. Based on the code from Chapfuwa et al.: https://github.com/paidamoyo/survival_cluster_analysis """ import os import numpy as np import pandas from baselines.sca.sca_utils.pre_processing import one_hot_encoder, formatted_data, missing_proportion, \ one_hot_indices, get_train_median_mode, log_transform from sklearn.preprocessing import StandardScaler def generate_data(seed=42): np.random.seed(seed) dir_path = os.path.dirname(os.path.realpath(__file__)) path = os.path.abspath(os.path.join(dir_path, '', 'support2.csv')) print("path:{}".format(path)) data_frame = pandas.read_csv(path, index_col=0) to_drop = ['hospdead', 'death', 'prg2m', 'prg6m', 'dnr', 'dnrday', 'd.time', 'aps', 'sps', 'surv2m', 'surv6m', 'totmcst'] print("head of data:{}, data shape:{}".format(data_frame.head(), data_frame.shape)) print("missing:{}".format(missing_proportion(data_frame.drop(labels=to_drop, axis=1)))) # Preprocess one_hot_encoder_list = ['sex', 'dzgroup', 'dzclass', 'income', 'race', 'ca', 'sfdm2'] data_frame = one_hot_encoder(data=data_frame, encode=one_hot_encoder_list) data_frame = log_transform(data_frame, transform_ls=['totmcst', 'totcst', 'charges', 'pafi', 'sod']) print("na columns:{}".format(data_frame.columns[data_frame.isnull().any()].tolist())) t_data = data_frame[['d.time']] e_data = data_frame[['death']] # dzgroup roughly corresponds to the diagnosis; more fine-grained than dzclass c_data = data_frame[['death']] c_data['death'] = c_data['death'].astype('category') c_data['death'] = c_data['death'].cat.codes x_data = data_frame.drop(labels=to_drop, axis=1) encoded_indices = one_hot_indices(x_data, one_hot_encoder_list) include_idx = set(np.array(sum(encoded_indices, []))) mask = np.array([(i in include_idx) for i in np.arange(x_data.shape[1])]) print("head of x data:{}, data shape:{}".format(x_data.head(), x_data.shape)) print("data description:{}".format(x_data.describe())) covariates = np.array(x_data.columns.values) print("columns:{}".format(covariates)) x = np.array(x_data).reshape(x_data.shape) t = np.array(t_data).reshape(len(t_data)) e = np.array(e_data).reshape(len(e_data)) c = np.array(c_data).reshape(len(c_data)) print("x:{}, t:{}, e:{}, len:{}".format(x[0], t[0], e[0], len(t))) idx = np.arange(0, x.shape[0]) print("x_shape:{}".format(x.shape)) np.random.shuffle(idx) x = x[idx] t = t[idx] e = e[idx] c = c[idx] # Normalization t = t / np.max(t) + 0.001 scaler = StandardScaler() scaler.fit(x[:, ~mask]) x[:, ~mask] = scaler.transform(x[:, ~mask]) end_time = max(t) print("end_time:{}".format(end_time)) print("observed percent:{}".format(sum(e) / len(e))) print("shuffled x:{}, t:{}, e:{}, len:{}".format(x[0], t[0], e[0], len(t))) num_examples = int(0.80 * len(e)) print("num_examples:{}".format(num_examples)) train_idx = idx[0: num_examples] split = int((len(t) - num_examples) / 2) test_idx = idx[num_examples: num_examples + split] valid_idx = idx[num_examples + split: len(t)] print("test:{}, valid:{}, train:{}, all: {}".format(len(test_idx), len(valid_idx), num_examples, len(test_idx) + len(valid_idx) + num_examples)) imputation_values = get_train_median_mode(x=x[train_idx], categorial=encoded_indices) preprocessed = { 'train': formatted_data(x=x, t=t, e=e, idx=train_idx, imputation_values=imputation_values), 'test': formatted_data(x=x, t=t, e=e, idx=test_idx, imputation_values=imputation_values), 'valid': formatted_data(x=x, t=t, e=e, idx=valid_idx, imputation_values=imputation_values) } preprocessed['train']['c'] = c[train_idx] preprocessed['valid']['c'] = c[valid_idx] preprocessed['test']['c'] = c[test_idx] return preprocessed def generate_support(seed=42): preproc = generate_data(seed) x_train = preproc['train']['x'] x_valid = preproc['valid']['x'] x_test = preproc['test']['x'] t_train = preproc['train']['t'] t_valid = preproc['valid']['t'] t_test = preproc['test']['t'] d_train = preproc['train']['e'] d_valid = preproc['valid']['e'] d_test = preproc['test']['e'] c_train = preproc['train']['c'] c_valid = preproc['valid']['c'] c_test = preproc['test']['c'] return x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test
4,591
37.588235
114
py
vadesc
vadesc-main/datasets/nsclc_lung/CT_preproc_utils.py
""" Utility functions for CT scan preprocessing. """ import numpy as np import pandas as pd import os import glob import cv2 import progressbar import re from PIL import Image, ImageOps # Libraries for DICOM data handling import pydicom import pydicom_seg LUNG1_N_PATIENTS = 422 RADIOGENOMICS_N_PATIENTS = 96 IGNORED_PATIENTS = np.unique([61, 179, 251, 352]) IGNORED_RADIOGENOMICS_PATIENTS = [7, 20, 21, 24, 36, 57, 74, 82, 87] IGNORED_LUNG3_PATIENTS = [12, 13, 16, 24, 26, 27, 28, 34, 37, 38, 40, 44, 53, 56, 57, 63, 64, 66, 68, 72] IGNORED_BASEL_PATIENTS = [3, 4, 5, 19, 32, 38, 41, 70, 76, 88, 107, 116, 119, 136, 153, 160, 164, 167, 178, 183, 199, 226, 237, 298, 302, 304, 306, 307, 310, 318, 337, 339, 347, 382, 385] REFERENCE_SLICE_THICKNESS = 3.0 DAYS = [f'day{i}/' for i in np.arange(2, 31)] # Loads a CT scan def load_scan(path, f1=True): slices = [pydicom.dcmread(os.path.join(path, s)) for s in os.listdir(path)] if f1: slices = [s for s in slices if 'SliceLocation' in s] slices.sort(key=lambda x: int(x.SliceLocation), reverse=True) try: slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2]) except: slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation) for s in slices: s.SliceThickness = slice_thickness pixel_spacing = slices[0].PixelSpacing return {'slices': slices, 'slice_thickness': slice_thickness, 'pixel_spacing': pixel_spacing} # Transforms DICOM data to a pixel array def get_pixels(scans, returnList=False): if not returnList: image = np.stack([s.pixel_array for s in scans]) image = image.astype(np.int16) return np.array(image, dtype=np.int16) else: return [s.pixel_array for s in scans] # Performs histogram equalisation def histogram_equalization(image, n_bins=256): # get image histogram image_histogram, bins = np.histogram(image.flatten(), n_bins, density=True) cdf = image_histogram.cumsum() # cumulative distribution function cdf = 255 * cdf / cdf[-1] # normalize # use linear interpolation of cdf to find new pixel values image_equalized = np.interp(image.flatten(), bins[:-1], cdf) return image_equalized.reshape(image.shape), cdf # Performs histogram equalisation on a batch of images def equalise_histograms(images, n_bins=256): images_eq = np.copy(images) for i in range(images.shape[0]): img_eq, cdf = histogram_equalization(images[i], n_bins=n_bins) images_eq[i, :, :] = img_eq return images_eq # Performs minmax normalisation on a batch of images def normalise_images(images): images_n = np.zeros_like(images) for i in range(images.shape[0]): img_n = cv2.normalize(images[i], None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX) images_n[i, :, :] = img_n return images_n # Downscales an image batch to the specified size def downscale_images(images, desired_size): downscaled = np.zeros((images.shape[0], desired_size[0], desired_size[1])) for j in range(images.shape[0]): downscaled[j] = cv2.resize(images[j], dsize=desired_size, interpolation=cv2.INTER_CUBIC) return downscaled # Crops a CT slice around lungs, lung segmentation is optional def crop_image(image, lung_segmentation=None, p_min=0.15): if lung_segmentation is None: th, threshed = cv2.threshold(image, p_min, 1, cv2.THRESH_BINARY) kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11)) morphed1 = cv2.morphologyEx(threshed, cv2.MORPH_OPEN, kernel) morphed = cv2.morphologyEx(morphed1, cv2.MORPH_CLOSE, kernel) morphed_int = np.array(morphed, dtype=np.uint8) cnts = cv2.findContours(morphed_int, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] cnt = sorted(cnts, key=cv2.contourArea)[-1] x, y, w, h = cv2.boundingRect(cnt) dst = image[y:y + h, x:x + w] else: cnts = \ cv2.findContours(np.array(lung_segmentation, dtype=np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] cnt = np.concatenate((sorted(cnts, key=cv2.contourArea)[0], sorted(cnts, key=cv2.contourArea)[1]), axis=0) x, y, w, h = cv2.boundingRect(cnt) dst = image[y:y + h, x:x + w] return dst # Rescales the image to the specified size def resize_image(image, desired_size): img = Image.fromarray(image) old_size = img.size ratio = float(desired_size) / max(old_size) new_size = tuple([int(x * ratio) for x in old_size]) delta_w = desired_size - new_size[0] delta_h = desired_size - new_size[1] padding = (delta_w // 2, delta_h // 2, delta_w - (delta_w // 2), delta_h - (delta_h // 2)) im = img.resize(new_size, Image.ANTIALIAS) new_im = ImageOps.expand(im, padding, fill=0) new_im = np.array(new_im) return new_im # Crops CT scans and subsequently performs histogram equalisation def crop_equalize_images(images, shape, n_bins, lung_segmentations=None, p_min=0.15): images_n = np.zeros([len(images), shape, shape]) for i in range(images.shape[0]): if lung_segmentations is None: img = crop_image(images[i], p_min=p_min) else: img = crop_image(images[i], lung_segmentation=lung_segmentations[i], p_min=p_min) img, cdf = histogram_equalization(img, n_bins=n_bins) img = cv2.normalize(img, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX) img = resize_image(img, shape) images_n[i, :, :] = img return images_n def load_lung1_images_max_tumour_volume_ave(lung1_dir, n_slices, dsize, verbose=1): """ Loads Lung1 dataset, takes an average 15 mm around the slice with the maximum transversal tumour area. https://wiki.cancerimagingarchive.net/display/Public/NSCLC-Radiomics """ assert n_slices % 2 == 1 lung1_best_slices = np.zeros((LUNG1_N_PATIENTS, 1, dsize[0], dsize[1])) lung1_tumor_volumes = np.zeros((LUNG1_N_PATIENTS, 6)) lung1_lung_volumes = np.zeros((LUNG1_N_PATIENTS,)) lung1_lung_volumes_tot = np.zeros((LUNG1_N_PATIENTS,)) lung1_best_slice_segmentations = np.zeros((LUNG1_N_PATIENTS, 1, dsize[0], dsize[1])) if verbose > 0: print('Loading CT data:') print() if os.path.exists('../datasets/nsclc_lung/lung1_best_slices_raw_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str( dsize[1]) + str('.npy')): if verbose > 0: print('Loading from a pre-saved file...') lung1_best_slices = np.load( file='../datasets/nsclc_lung/lung1_best_slices_raw_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str( dsize[1]) + str('.npy'), allow_pickle=True) else: if verbose > 0: bar = progressbar.ProgressBar(maxval=LUNG1_N_PATIENTS) bar.start() for i in range(1, LUNG1_N_PATIENTS + 1): patient_dir = os.path.join(lung1_dir, 'LUNG1-' + "{:03d}".format(i)) patient_dir = os.path.join(patient_dir, os.listdir(patient_dir)[0]) patient_modalities = os.listdir(patient_dir) list.sort(patient_modalities) seg_modalities = [f for f in patient_modalities if re.search('Segmentation', f)] patient_seg_dir = None if len(seg_modalities) > 0: patient_seg_dir = os.path.join(patient_dir, seg_modalities[0]) elif verbose > 0: print('WARNING: No segmentation for patient ' + str(i)) patient_ct_dir = os.path.join(patient_dir, patient_modalities[0]) results_dict = load_scan(patient_ct_dir) patient_ct_slices = results_dict['slices'] slice_thickness = results_dict['slice_thickness'] pixel_spacing = results_dict['pixel_spacing'] n_slices_scaled = int(REFERENCE_SLICE_THICKNESS / slice_thickness * n_slices) patient_ct_pix = get_pixels(patient_ct_slices) patient_ct_pix_d = downscale_images(patient_ct_pix, (dsize[0], dsize[1])) if patient_seg_dir is not None: lung_seg_dcm = pydicom.dcmread(patient_seg_dir + str('/1-1.dcm')) seg_reader = pydicom_seg.SegmentReader() seg_result = seg_reader.read(lung_seg_dcm) seg_infos = seg_result.segment_infos n_segments = len(seg_infos) lung_left_seg = None lung_right_seg = None lung_tot_seg = None neoplasm_seg = None for s in range(1, n_segments + 1): s_info = seg_infos[s] if re.search('Neoplasm', str(s_info)): neoplasm_seg = np.flip(seg_result._segment_data[s], 0) elif re.search('Lung-Left', str(s_info)): lung_left_seg = np.flip(seg_result._segment_data[s], 0) elif re.search('Lung-Right', str(s_info)): lung_right_seg = np.flip(seg_result._segment_data[s], 0) elif re.search('Lungs-Total', str(s_info)): lung_tot_seg = np.flip(seg_result._segment_data[s], 0) if neoplasm_seg is None and verbose > 0: print('WARNING: No neoplasm segment for patient ' + str(i)) if (lung_left_seg is None and lung_right_seg is None and lung_tot_seg is None) and verbose > 0: print('WARNING: No lung segment for patient ' + str(i)) tumour_vols = np.sum(neoplasm_seg, axis=(1, 2)) tumour_vols_mm = np.sum(neoplasm_seg, axis=(1, 2)) * pixel_spacing[0] * pixel_spacing[ 1] * slice_thickness lung_vols = None # lung_vols_mm = None if lung_left_seg is not None and lung_right_seg is not None: lung_vols = np.sum(lung_left_seg, axis=(1, 2)) + np.sum(lung_right_seg, axis=(1, 2)) elif lung_tot_seg is not None: lung_vols = np.sum(lung_tot_seg, axis=(1, 2)) best_slice_ind = np.argmax(tumour_vols) range_slices = np.arange((best_slice_ind - (n_slices_scaled - 1) // 2), (best_slice_ind + (n_slices_scaled - 1) // 2 + 1)) if len(range_slices) > 0: range_slices = range_slices[np.round(np.linspace(0, len(range_slices) - 1, n_slices)).astype(int)] if len(range_slices) == 0 or range_slices[0] >= patient_ct_pix.shape[0]: best_slices = patient_ct_pix_d[0:2] lung1_tumor_volumes[i - 1, 0] = np.sum(tumour_vols[0:2]) lung1_tumor_volumes[i - 1, 1] = np.sum(tumour_vols_mm[0:2]) if lung_vols is not None: lung1_lung_volumes[i - 1] = np.sum(lung_vols[0:2]) else: best_slices = patient_ct_pix_d[range_slices] lung1_tumor_volumes[i - 1, 0] = np.sum(tumour_vols[range_slices]) lung1_tumor_volumes[i - 1, 1] = np.sum(tumour_vols_mm[range_slices]) if lung_vols is not None: lung1_lung_volumes[i - 1] = np.sum(lung_vols[range_slices]) if lung_vols is not None: lung1_lung_volumes_tot[i - 1] = np.sum(lung_vols) lung1_tumor_volumes[i - 1, 3] = np.sum(tumour_vols) lung1_tumor_volumes[i - 1, 4] = np.sum(tumour_vols_mm) lung1_best_slices[i - 1, 0, :, :] = np.mean(best_slices, axis=0) lung1_best_slice_segmentations[i - 1, 0, :, :] = (downscale_images(np.expand_dims( neoplasm_seg[np.argmax(tumour_vols)], 0), (dsize[0], dsize[1]))[0] > 0) * 1. if verbose > 0: bar.update(i - 1) lung1_best_slices = lung1_best_slices.astype('float32') lung1_tumor_volumes[:, 2] = lung1_tumor_volumes[:, 0] / lung1_lung_volumes lung1_tumor_volumes[:, 5] = lung1_tumor_volumes[:, 3] / lung1_lung_volumes_tot if not os.path.exists( '../datasets/nsclc_lung/lung1_best_slices_raw_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str( dsize[1]) + str('.npy')): if verbose > 0: print('Saving as a file...') np.save(file='../datasets/nsclc_lung/lung1_best_slices_raw_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str( dsize[1]) + str('.npy'), arr=lung1_best_slices, allow_pickle=True) np.save(file='../datasets/nsclc_lung/lung1_tumor_volumes.npy', arr=lung1_tumor_volumes, allow_pickle=True) np.save(file='../datasets/nsclc_lung/lung1_segmentations_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str( dsize[1]) + str('.npy'), arr=lung1_best_slice_segmentations, allow_pickle=True) lung1_best_slices = np.delete(lung1_best_slices, [127], axis=0) # empty scan lung1_best_slices = np.expand_dims(lung1_best_slices, -1) return lung1_best_slices def load_lung3_images_max_tumour_volume_ave(lung3_dir, n_slices, dsize, verbose=1): """ Loads Lung3 dataset, takes an average 15 mm around the slice with the maximum transversal tumour area. https://wiki.cancerimagingarchive.net/display/Public/NSCLC-Radiomics-Genomics """ assert n_slices % 2 == 1 master_table = pd.read_csv(os.path.join(lung3_dir, 'Lung3_master.csv')) lung3_n_patients = len(master_table['Case ID'].values) lung3_best_slices = np.zeros((lung3_n_patients, 1, dsize[0], dsize[1])) if verbose > 0: print('Loading CT data:') print() if os.path.exists('../datasets/nsclc_lung/lung3_best_slices_raw_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str( dsize[1]) + str('.npy')): if verbose > 0: print('Loading from a pre-saved file...') lung3_best_slices = np.load( file='../datasets/nsclc_lung/lung3_best_slices_raw_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str( dsize[1]) + str('.npy'), allow_pickle=True) else: if verbose > 0: bar = progressbar.ProgressBar(maxval=lung3_n_patients) bar.start() for i in range(lung3_n_patients): patient_ct_dir = os.path.join(lung3_dir, master_table['CT directory'].values[i]) results_dict = load_scan(patient_ct_dir) patient_ct_slices = results_dict['slices'] slice_thickness = results_dict['slice_thickness'] n_slices_scaled = int(REFERENCE_SLICE_THICKNESS / slice_thickness * n_slices) patient_ct_pix = get_pixels(patient_ct_slices) patient_ct_pix_d = downscale_images(patient_ct_pix, (dsize[0], dsize[1])) best_slice_ind = master_table['Tumor slice index'].values[i] range_slices = np.arange((best_slice_ind - (n_slices_scaled - 1) // 2), (best_slice_ind + (n_slices_scaled - 1) // 2 + 1)) range_slices = range_slices[np.round(np.linspace(0, len(range_slices) - 1, n_slices)).astype(int)] if len(range_slices) == 0 or range_slices[0] >= patient_ct_pix.shape[0]: best_slices = patient_ct_pix_d[0:2] else: best_slices = patient_ct_pix_d[range_slices] lung3_best_slices[i, 0, :, :] = np.mean(best_slices, axis=0) if verbose > 0: bar.update(i) lung3_best_slices = lung3_best_slices.astype('float32') if not os.path.exists( '../datasets/nsclc_lung/lung3_best_slices_raw_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str( dsize[1]) + str('.npy')): if verbose > 0: print('Saving as a file...') np.save(file='../datasets/nsclc_lung/lung3_best_slices_raw_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str( dsize[1]) + str('.npy'), arr=lung3_best_slices, allow_pickle=True) lung3_best_slices = np.expand_dims(lung3_best_slices, -1) return lung3_best_slices def load_radiogenomics_images_max_tumour_volume_ave(radiogenomics_dir, n_slices, dsize, verbose=1): """ Loads a subset of the NSCLC Radiogenomics dataset, takes an average 15 mm around the slice with the maximum transversal tumour area. https://wiki.cancerimagingarchive.net/display/Public/NSCLC+Radiogenomics """ assert n_slices % 2 == 1 radiogenomics_best_slices = np.zeros((RADIOGENOMICS_N_PATIENTS, 1, dsize[0], dsize[1])) radiogenomics_best_slice_segmentations = np.zeros((RADIOGENOMICS_N_PATIENTS, 1, dsize[0], dsize[1])) if verbose > 0: print('Loading CT data:') print() if os.path.exists( '../datasets/nsclc_lung/radiogenomics_best_slices_raw_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str( dsize[1]) + str('.npy')): if verbose > 0: print('Loading from a pre-saved file...') radiogenomics_best_slices = np.load( file='../datasets/nsclc_lung/radiogenomics_best_slices_raw_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str(dsize[1]) + str('.npy'), allow_pickle=True) else: if verbose > 0: bar = progressbar.ProgressBar(maxval=RADIOGENOMICS_N_PATIENTS) bar.start() # Load technical metadata meta_dat = pd.read_csv(os.path.join(radiogenomics_dir, 'metadata.csv')) # Find segmentations segmentation_directories = meta_dat['File Location'].values[ meta_dat['Series Description'].values == '3D Slicer segmentation result'] # Sanity check assert len(segmentation_directories) == RADIOGENOMICS_N_PATIENTS for i in range(0, RADIOGENOMICS_N_PATIENTS): # Construct segmentation directory patient_seg_dir = segmentation_directories[i] patient_seg_dir = os.path.join(radiogenomics_dir, patient_seg_dir.replace('./', '')) # Patient's data directory patient_dir = os.path.dirname(patient_seg_dir) patient_modalities = os.listdir(patient_dir) list.sort(patient_modalities) # CT data directory ct_modalities = [f for f in patient_modalities if not (re.search('segmentation result', f))] patient_ct_dir = os.path.join(patient_dir, ct_modalities[0]) # Load CT results_dict = load_scan(patient_ct_dir) patient_ct_slices = results_dict['slices'] slice_thickness = results_dict['slice_thickness'] n_slices_scaled = int(REFERENCE_SLICE_THICKNESS / slice_thickness * n_slices) patient_ct_pix = get_pixels(patient_ct_slices) patient_ct_pix_d = downscale_images(patient_ct_pix, (dsize[0], dsize[1])) # Load segmentation lung_seg_dcm = pydicom.dcmread(patient_seg_dir + str('/1-1.dcm')) seg_reader = pydicom_seg.SegmentReader() seg_result = seg_reader.read(lung_seg_dcm) neoplasm_seg = np.flip(seg_result._segment_data[1], 0) # Find maximum tumour volume sice tumour_vols = np.sum(neoplasm_seg, axis=(1, 2)) best_slice_ind = np.argmax(tumour_vols) range_slices = np.arange((best_slice_ind - (n_slices_scaled - 1) // 2), (best_slice_ind + (n_slices_scaled - 1) // 2 + 1)) range_slices = range_slices[np.round(np.linspace(0, len(range_slices) - 1, n_slices)).astype(int)] if len(range_slices) == 0 or range_slices[0] >= patient_ct_pix.shape[0]: best_slices = patient_ct_pix_d[0:2] else: best_slices = patient_ct_pix_d[range_slices] radiogenomics_best_slices[i, 0, :, :] = np.mean(best_slices, axis=0) radiogenomics_best_slice_segmentations[i, 0, :, :] = (downscale_images(np.expand_dims( neoplasm_seg[np.argmax(tumour_vols)], 0), (dsize[0], dsize[1]))[0] > 0) * 1. if verbose > 0: bar.update(i) if not os.path.exists('../datasets/nsclc_lung/radiogenomics_best_slices_raw_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str(dsize[1]) + str('.npy')): if verbose > 0: print('Saving as a file...') np.save(file='../datasets/nsclc_lung/radiogenomics_best_slices_raw_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str(dsize[1]) + str('.npy'), arr=radiogenomics_best_slices, allow_pickle=True) np.save(file='../datasets/nsclc_lung/radiogenomics_segmentations_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str(dsize[1]) + str('.npy'), arr=radiogenomics_best_slice_segmentations, allow_pickle=True) radiogenomics_best_slices = np.expand_dims(radiogenomics_best_slices, -1) return radiogenomics_best_slices def load_radiogenomics_amc_images_max_tumour_volume_ave(radiogenomics_dir, n_slices, dsize, verbose=1): """ Loads a subset of the NSCLC Radiogenomics dataset, takes an average 15 mm around the slice with the maximum transversal tumour area. https://wiki.cancerimagingarchive.net/display/Public/NSCLC+Radiogenomics """ assert n_slices % 2 == 1 master_file = pd.read_csv(os.path.join(radiogenomics_dir, 'master_file_amc.csv')) radiogenomics_best_slices = np.zeros((len(master_file['Case ID'].values), 1, dsize[0], dsize[1])) if verbose > 0: print('Loading CT data:') print() if os.path.exists( '../datasets/nsclc_lung/radiogenomics_amc_best_slices_raw_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str( dsize[1]) + str('.npy')): if verbose > 0: print('Loading from a pre-saved file...') radiogenomics_best_slices = np.load( file='../datasets/nsclc_lung/radiogenomics_amc_best_slices_raw_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str(dsize[1]) + str('.npy'), allow_pickle=True) else: if verbose > 0: bar = progressbar.ProgressBar(maxval=len(master_file['Case ID'].values)) bar.start() for i in range(len(master_file['Case ID'].values)): patient_ct_dir = os.path.join(radiogenomics_dir, master_file['CT directory'].values[i]) # Load CT results_dict = load_scan(patient_ct_dir) patient_ct_slices = results_dict['slices'] slice_thickness = results_dict['slice_thickness'] n_slices_scaled = int(REFERENCE_SLICE_THICKNESS / slice_thickness * n_slices) patient_ct_pix = get_pixels(patient_ct_slices) patient_ct_pix_d = downscale_images(patient_ct_pix, (dsize[0], dsize[1])) best_slice_ind = int(master_file['Tumor slice'].values[i]) range_slices = np.arange((best_slice_ind - (n_slices_scaled - 1) // 2), (best_slice_ind + (n_slices_scaled - 1) // 2 + 1)) range_slices = range_slices[np.round(np.linspace(0, len(range_slices) - 1, n_slices)).astype(int)] if len(range_slices) == 0 or range_slices[0] >= patient_ct_pix.shape[0]: best_slices = patient_ct_pix_d[0:2] else: best_slices = patient_ct_pix_d[range_slices] radiogenomics_best_slices[i, 0, :, :] = np.mean(best_slices, axis=0) if verbose > 0: bar.update(i) if not os.path.exists('../datasets/nsclc_lung/radiogenomics_amc_best_slices_raw_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str(dsize[1]) + str('.npy')): if verbose > 0: print('Saving as a file...') np.save(file='../datasets/nsclc_lung/radiogenomics_amc_best_slices_raw_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str(dsize[1]) + str('.npy'), arr=radiogenomics_best_slices, allow_pickle=True) radiogenomics_best_slices = np.expand_dims(radiogenomics_best_slices, -1) return radiogenomics_best_slices def load_basel_images_max_tumour_volume_ave(basel_dir, n_slices, dsize, verbose=1): """ Loads the dataset from the Basel University Hospital. Code adapted from Pattisapu et al.: https://github.com/pvk95/PAG """ # assert n_slices % 2 == 1 if verbose: print('Loading CT data:') print() if os.path.exists('../datasets/nsclc_lung/basel_best_slices_raw_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str(dsize[1]) + str('.npy')): if verbose > 0: print('Loading from a pre-saved file...') basel_best_slices = np.load(file='../datasets/nsclc_lung/basel_best_slices_raw_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str(dsize[1]) + str('.npy'), allow_pickle=True) else: scandates = pd.read_csv(os.path.join(os.path.dirname(basel_dir), 'TableS4.csv')) deathdates = pd.read_csv(os.path.join(os.path.dirname(basel_dir), 'deaths_lungstage_Basel.csv')) stages = pd.read_csv(os.path.join(os.path.dirname(basel_dir), 'lungstage-data/TNM_labels.csv')) metadata = pd.read_excel(os.path.join(os.path.dirname(basel_dir), 'lungstage-data/190418_Rohdaten_UICC_7.xlsx'), sheet_name=None, engine='openpyxl') metadata = metadata['SPSS total'] nmissing_date = np.ones_like(deathdates['pat_death_date'].values).astype(bool) acc_codes = deathdates['accession'].values.astype('U32') scandates_first_saving = scandates['first saving'].values.astype('U32') scandates_id = scandates['ID'].values.astype('U32') for acode in acc_codes: if len(scandates_first_saving[scandates_id == acode]) == 0 or \ scandates_first_saving[scandates_id == acode] == 'nan': nmissing_date[acc_codes == acode] = False basel_n_patients = np.sum(nmissing_date) basel_best_slices = np.zeros((basel_n_patients, 1, dsize[0], dsize[1])) basel_segmentations = np.zeros((basel_n_patients, 1, dsize[0], dsize[1])) basel_tumor_volumes = np.zeros((basel_n_patients, 6)) clinical_data = {'Scan date': np.repeat(pd.to_datetime('190821'), basel_n_patients), 'Death date': np.repeat(pd.to_datetime('190821'), basel_n_patients), 'Survival time': np.zeros((basel_n_patients,)), 'Event': np.ones((basel_n_patients,)), 'T': np.repeat('nan', basel_n_patients), 'N': np.repeat('nan', basel_n_patients), 'M': np.repeat('nan', basel_n_patients), 'Sex': np.zeros((basel_n_patients,)), 'Age': np.zeros((basel_n_patients,)), 'Real_lesions': np.zeros((basel_n_patients,)), 'UICC_I_IV': np.zeros((basel_n_patients,))} clinical_data = pd.DataFrame(data=clinical_data) if verbose > 0: bar = progressbar.ProgressBar(maxval=basel_n_patients) bar.start() # Folders with images scan_dirs = [] for d in DAYS: path = os.path.join(basel_dir, d) scan_dirs = scan_dirs + glob.glob(path + '*/') i = 0 loaded_acc_codes = [] for ct_dir in scan_dirs: # Patient's ID acc_code = ct_dir.split('ACC', 1)[1].replace('/', '') sav_date = pd.to_datetime(ct_dir.split('StagingD', 1)[1].split('T', 1)[0]).date() # Date of CT scan scan_date = scandates['first saving'].values.astype('U32')[scandates['ID'].values.astype( 'U32') == acc_code] if len(scan_date) == 0 or scan_date[0] == 'nan': scan_date = None else: scan_date = pd.to_datetime(scan_date[0]) # Date of death death_date = deathdates['pat_death_date'].values.astype('U32')[deathdates['accession'].values.astype( 'U32') == acc_code] if len(death_date) == 0: death_date = None elif death_date[0] == 'nan' and scan_date is not None: death_date = pd.to_datetime('2021-08-23') clinical_data.at[i, 'Event'] = 0 else: death_date = pd.to_datetime(death_date[0]) sav_date_2 = scandates['saving for second reading'].values.astype('U32')[scandates['ID'].values.astype( 'U32') == acc_code] if len(sav_date_2) == 0 or sav_date_2[0] == 'nan': sav_date_2 = None else: sav_date_2 = pd.to_datetime(sav_date_2[0]).date() # Only load CTs for patients with available survival data if scan_date is not None and death_date is not None and acc_code not in loaded_acc_codes: # Load the .npy file with the images. d = np.load(os.path.join(ct_dir, 'lsa.npz')) spacing = d['spacing'] label_names = d['label_names'] # Retrieve relevant slices of CT patient_ct_pix = d['CT'].astype(float) patient_ct_pix_d = downscale_images(patient_ct_pix, (dsize[0], dsize[1])) seg = d['Labels'] tumour_vols = np.sum((seg > 0) * 1., axis=(1, 2)) best_slice_ind = np.argmax(tumour_vols) best_slices = patient_ct_pix_d[(best_slice_ind - (n_slices - 1) // 2):(best_slice_ind + (n_slices - 1) // 2 + 1)] basel_best_slices[i, 0] = np.mean(best_slices, axis=0) best_slice_seg = (seg[best_slice_ind] > 0) * 1. basel_segmentations[i, 0] = downscale_images(np.expand_dims(best_slice_seg, 0), (dsize[0], dsize[1]))[0] basel_tumor_volumes[i, 0] = np.sum(tumour_vols[(best_slice_ind - (n_slices - 1) // 2):( best_slice_ind + (n_slices - 1) // 2 + 1)]) basel_tumor_volumes[i, 1] = np.sum(tumour_vols[(best_slice_ind - (n_slices - 1) // 2):( best_slice_ind + (n_slices - 1) // 2 + 1)] * spacing[0] * spacing[1] * spacing[2]) basel_tumor_volumes[i, 3] = np.sum(tumour_vols) basel_tumor_volumes[i, 4] = np.sum(tumour_vols) * spacing[0] * spacing[1] * spacing[2] # Find relevant metadata sex = metadata['sex'].values[metadata['id'].values == int(acc_code)] if len(sex) == 0: sex = 'nan' age = 'nan' uicc = 'nan' lesions = 'nan' else: sex = sex[0] age = metadata['age'].values[metadata['id'].values == int(acc_code)][0] uicc = metadata['UICC_I_IV'].values[metadata['id'].values == int(acc_code)][0] lesions = metadata['real_lesions'].values[metadata['id'].values == int(acc_code)][0] T = stages['T'].values[stages['Accession#'] == int(acc_code)] if len(T) == 0: T = 'nan' M = 'nan' N = 'nan' else: T = T[0] M = stages['M'].values[stages['Accession#'] == int(acc_code)][0] N = stages['N'].values[stages['Accession#'] == int(acc_code)][0] # Save clinical data clinical_data.at[i, 'Scan date'] = scan_date clinical_data.at[i, 'Death date'] = death_date clinical_data.at[i, 'Survival time'] = (death_date - scan_date).days clinical_data.at[i, 'Sex'] = sex clinical_data.at[i, 'Age'] = age clinical_data.at[i, 'UICC_I_IV'] = uicc clinical_data.at[i, 'Real_lesions'] = lesions clinical_data.at[i, 'T'] = T clinical_data.at[i, 'M'] = M clinical_data.at[i, 'N'] = N loaded_acc_codes.append(acc_code) if verbose: bar.update(i) i = i + 1 basel_best_slices = basel_best_slices[0:i] basel_segmentations = basel_segmentations[0:i] basel_tumor_volumes = basel_tumor_volumes[0:i] clinical_data = clinical_data[0:i] if not os.path.exists('../datasets/nsclc_lung/basel_best_slices_raw_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str(dsize[1]) + str('.npy')): if verbose: print('Saving as a file...') np.save(file='../datasets/nsclc_lung/basel_best_slices_raw_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str(dsize[1]) + str('.npy'), arr=basel_best_slices, allow_pickle=True) # Save segmentations np.save(file='../datasets/nsclc_lung/basel_segmentations_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str(dsize[1]) + str('.npy'), arr=basel_segmentations, allow_pickle=True) # Save segmentations np.save(file='../datasets/nsclc_lung/basel_tumor_volumes.npy', arr=basel_tumor_volumes, allow_pickle=True) # Save clinical data clinical_data.to_csv('../datasets/nsclc_lung/clinical_data_basel.csv', index=False) basel_best_slices = np.expand_dims(basel_best_slices, -1) return basel_best_slices def preprocess_lung1_images(lung1_dir, n_slices, dsize, n_bins=40, verbose=1): """ Preprocesses Lung1 CT images. """ if os.path.exists( '../datasets/nsclc_lung/lung1_best_slices_preprocessed_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str( dsize[1]) + str('.npy')): if verbose > 0: print('Loading preprocessed data from a pre-saved file...') X = np.load(file='../datasets/nsclc_lung/lung1_best_slices_preprocessed_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str(dsize[1]) + str('.npy'), allow_pickle=True) else: # load data X = load_lung1_images_max_tumour_volume_ave(lung1_dir, n_slices, dsize) X = np.reshape(X, (-1, X.shape[1], dsize[0], dsize[1])) # preprocess if verbose > 0: print('Preprocess data...') bar = progressbar.ProgressBar(maxval=len(X)) bar.start() of_30 = [21, 147] p_10 = [50, 251, 304] for i in range(len(X)): offset = 50 p_min = 0.15 if i in of_30: offset = 30 if i in p_10: p_min = 0.10 elif i == 314: p_min = 0.2 temp = np.copy(X[i]) temp = normalise_images(temp) if i == 28: temp = temp[:, offset + 45:(X.shape[2] - offset - 25), offset + 20:(X.shape[3] - offset - 5)] elif i == 303: temp = temp[:, offset + 20:(X.shape[2] - offset - 30), offset + 20:(X.shape[3] - offset - 5)] elif i in [10, 36, 106, 292, 354]: temp = temp[:, offset:(X.shape[2] - offset), offset + 20:(X.shape[3] - offset + 10)] elif i == 351: temp = temp[:, 40 + offset:(X.shape[2] - offset - 10), offset + 30:(X.shape[3] - offset)] elif i in [9, 46, 57, 67, 78, 132, 142, 146, 257, 302]: temp = temp[:, offset:(X.shape[2] - offset), offset:(X.shape[3] - offset + 30)] elif i in [4, 26, 51, 129, 159, 199, 292, 137]: temp = temp[:, offset:(X.shape[2] - offset), offset - 30:(X.shape[3] - offset)] elif i in [168, 281]: temp = temp[:, -20 + offset:(X.shape[2] - offset), offset - 30:(X.shape[3] - offset) + 20] else: temp = temp[:, offset:(X.shape[2] - offset), offset:(X.shape[3] - offset)] X[i] = crop_equalize_images(temp, dsize[0], n_bins=n_bins, lung_segmentations=None, p_min=p_min) bar.update(i) X = np.delete(X, IGNORED_PATIENTS, axis=0) if verbose > 0: print('Saving as a file...') np.save( file='../datasets/nsclc_lung/lung1_best_slices_preprocessed_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str( dsize[1]) + str('.npy'), arr=X, allow_pickle=True) X = np.expand_dims(X, -1) return X def preprocess_lung3_images(lung3_dir, n_slices, dsize, n_bins=40, verbose=1): """ Preprocesses Lung3 CT images. """ if os.path.exists( '../datasets/nsclc_lung/lung3_best_slices_preprocessed_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str( dsize[1]) + str('.npy')): if verbose > 0: print('Loading preprocessed data from a pre-saved file...') X = np.load(file='../datasets/nsclc_lung/lung3_best_slices_preprocessed_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str(dsize[1]) + str('.npy'), allow_pickle=True) else: # load data X = load_lung3_images_max_tumour_volume_ave(lung3_dir, n_slices, dsize) X = np.reshape(X, (-1, X.shape[1], dsize[0], dsize[1])) # preprocess if verbose > 0: print('Preprocess data...') bar = progressbar.ProgressBar(maxval=len(X)) bar.start() of_20 = [1, 6, 31, 33, 62] of_0 = [21, 50, 54, 60, 69, 10] for i in range(len(X)): offset = 30 if i in of_20: offset = 20 elif i in of_0: offset = 0 temp = np.copy(X[i]) temp = normalise_images(temp) if i == 10: temp = temp[:, offset + 70:(X.shape[2] - offset), offset:(X.shape[3] - offset)] elif i == 21: temp = temp[:, offset + 110:(X.shape[2] - offset), offset:(X.shape[3] - offset)] else: temp = temp[:, offset:(X.shape[2] - offset), offset:(X.shape[3] - offset)] X[i] = crop_equalize_images(temp, dsize[0], n_bins=n_bins, lung_segmentations=None) bar.update(i) if verbose > 0: print('Saving as a file...') np.save( file='../datasets/nsclc_lung/lung3_best_slices_preprocessed_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str( dsize[1]) + str('.npy'), arr=X, allow_pickle=True) X = np.delete(X, IGNORED_LUNG3_PATIENTS, axis=0) X = np.expand_dims(X, -1) return X def preprocess_radiogenomics_images(radiogenomics_dir, n_slices, dsize, n_bins=40, verbose=1): """ Preprocesses a subset of NSCLC Radiogenomics CT images. """ if os.path.exists('../datasets/nsclc_lung/radiogenomics_best_slices_preprocessed_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str(dsize[1]) + str('.npy')): if verbose > 0: print('Loading preprocessed data from a pre-saved file...') X = np.load(file='../datasets/nsclc_lung/radiogenomics_best_slices_preprocessed_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str(dsize[1]) + str('.npy'), allow_pickle=True) else: # load data X = load_radiogenomics_images_max_tumour_volume_ave(radiogenomics_dir, n_slices, dsize) X = np.reshape(X, (-1, X.shape[1], dsize[0], dsize[1])) lung_segmentations = None if verbose > 0: print('Preprocess data...') bar = progressbar.ProgressBar(maxval=len(X)) bar.start() of_30 = [6, 14, 16, 26, 29, 71, 81, 90, 95] of_15 = [63, 77] of_25 = [91, 92, 35, 70] p_10 = [6, 14, 71] for i in range(len(X)): offset = 35 p_min = 0.15 if i in of_30: offset = 30 elif i in of_25: offset = 25 elif i in of_15: offset = 15 if i in p_10: p_min = 0.10 temp = np.copy(X[i]) if np.sum(temp <= 0) >= 300: # Remove the circle pattern if i in [6, 14, 71, 63]: temp = temp[:, offset + 35:(temp.shape[0] - offset - 20), offset:(temp.shape[1] - offset)] elif i in [81]: temp = temp[:, offset + 10:(temp.shape[0] - offset - 40), offset:(temp.shape[1] - offset)] elif i in [77, 91, 92]: temp = temp[:, offset + 30:(temp.shape[0] - offset - 40), offset:(temp.shape[1] - offset)] elif i in [16, 29, 95]: temp = temp[:, offset + 15:(temp.shape[0] - offset - 20), offset:(temp.shape[1] - offset)] elif i in [26, 90]: temp = temp[:, offset + 20:(temp.shape[0] - offset - 20), offset - 5:(temp.shape[1] - offset - 10)] elif i in [35]: temp = temp[:, offset:(temp.shape[0] - offset - 30), offset:(temp.shape[1] - offset)] elif i in [70]: temp = temp[:, offset + 35:(temp.shape[0] - offset - 20), offset - 10:(temp.shape[1] - offset)] else: temp = temp[:, offset + 10:(temp.shape[0] - offset - 10), offset:(temp.shape[1] - offset)] temp = normalise_images(temp) X[i] = crop_equalize_images(temp, dsize[0], n_bins=n_bins, lung_segmentations=lung_segmentations) bar.update(i) if verbose > 0: print('Saving as a file...') X = np.delete(X, IGNORED_RADIOGENOMICS_PATIENTS, axis=0) np.save( file='../datasets/nsclc_lung/radiogenomics_best_slices_preprocessed_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str( dsize[1]) + str('.npy'), arr=X, allow_pickle=True) X = np.expand_dims(X, -1) return X def preprocess_radiogenomics_images_amc(radiogenomics_dir, n_slices, dsize, n_bins=40, verbose=1): """ Preprocesses a subset of NSCLC Radiogenomics CT images. """ if os.path.exists('../datasets/nsclc_lung/radiogenomics_amc_best_slices_preprocessed_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str(dsize[1]) + str('.npy')): if verbose > 0: print('Loading preprocessed data from a pre-saved file...') X = np.load( file='../datasets/nsclc_lung/radiogenomics_amc_best_slices_preprocessed_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str(dsize[1]) + str('.npy'), allow_pickle=True) else: # load data X = load_radiogenomics_amc_images_max_tumour_volume_ave(radiogenomics_dir, n_slices, dsize) X = np.reshape(X, (-1, X.shape[1], dsize[0], dsize[1])) lung_segmentations = None if verbose > 0: print('Preprocess data...') bar = progressbar.ProgressBar(maxval=len(X)) bar.start() of_25 = [3, 8, 10, 15, 16, 17, 28, 39, 38, 36] of_40 = [29, 40] for i in range(len(X)): offset = 45 if i in of_40: offset = 40 elif i in of_25: offset = 25 temp = np.copy(X[i]) if np.sum(temp <= 0) >= 300: # Remove the circle pattern if i == 0: temp = temp[:, offset:(temp.shape[0] - offset), offset - 10:(temp.shape[1] - offset)] elif i == 29: temp = temp[:, offset + 35:(temp.shape[0] - offset), offset:(temp.shape[1] - offset + 20)] else: temp = temp[:, offset:(temp.shape[0] - offset), offset:(temp.shape[1] - offset)] temp = normalise_images(temp) X[i] = crop_equalize_images(temp, dsize[0], n_bins=n_bins, lung_segmentations=lung_segmentations) bar.update(i) if verbose > 0: print('Saving as a file...') np.save( file='../datasets/nsclc_lung/radiogenomics_amc_best_slices_preprocessed_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str( dsize[1]) + str('.npy'), arr=X, allow_pickle=True) X = np.expand_dims(X, -1) return X def preprocess_basel_images(basel_dir, n_slices, dsize, n_bins=40, verbose=1): """ Preprocesses Basel University Hospital CT images. """ if os.path.exists( '../datasets/nsclc_lung/basel_best_slices_preprocessed_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str( dsize[1]) + str('.npy')): if verbose > 0: print('Loading preprocessed data from a pre-saved file...') X = np.load(file='../datasets/nsclc_lung/basel_best_slices_preprocessed_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str(dsize[1]) + str('.npy'), allow_pickle=True) else: # load data X = load_basel_images_max_tumour_volume_ave(basel_dir, n_slices, dsize) X = np.reshape(X, (-1, X.shape[1], dsize[0], dsize[1])) lung_segmentations = None if verbose: print('Preprocess data...') bar = progressbar.ProgressBar(maxval=len(X)) bar.start() of_25 = [36, 72, 80, 85, 104, 108, 137, 344, 351] of_60 = [125, 202, 203, 357, 360, 320] of_30 = [200] p_06 = [125] p_09 = [301] p_14 = [71] p_78 = [200] plus_20 = [202, 203, 357, 360] plus_10 = [320] minus_20 = [186] for i in range(len(X)): p_min = 0.15 offset = 45 if i in of_25: offset = 25 elif i in of_60: offset = 60 elif i in of_30: offset = 30 if i in p_06: p_min = 0.06 elif i in p_09: p_min = 0.09 elif i in p_14: p_min = 0.14 elif i in p_78: p_min = 0.78 temp = np.copy(X[i]) if np.sum(temp <= 0) >= 300: # Remove the circle pattern if i in plus_20: temp = temp[:, offset:(temp.shape[0] - offset), offset:(temp.shape[1] - offset + 20)] elif i in plus_10: temp = temp[:, offset:(temp.shape[0] - offset), offset:(temp.shape[1] - offset + 10)] elif i in minus_20: temp = temp[:, offset:(temp.shape[0] - offset), offset:(temp.shape[1] - offset - 20)] else: temp = temp[:, offset:(temp.shape[0] - offset), offset:(temp.shape[1] - offset)] temp = normalise_images(temp) X[i] = crop_equalize_images(temp, dsize[0], n_bins=n_bins, lung_segmentations=lung_segmentations, p_min=p_min) bar.update(i) if verbose: print('Saving as a file...') X = np.delete(X, IGNORED_BASEL_PATIENTS, axis=0) np.save( file='../datasets/nsclc_lung/basel_best_slices_preprocessed_' + str(n_slices) + '_' + str( dsize[0]) + 'x' + str( dsize[1]) + str('.npy'), arr=X, allow_pickle=True) X = np.expand_dims(X, -1) return X def augment_images(images): """ Augments a batch of CT images. """ images = np.squeeze(images) images_augmented = np.zeros(images.shape) for i in range(images.shape[0]): image = np.squeeze(images[i]) image = augment_brightness(image, value_min=-0.1, value_max=0.1) o = np.random.rand() if o < 0.5: image = augment_noise(image) o = np.random.rand() if o < 0.5: image = np.flip(image, axis=1) o = np.random.rand() if o < 0.5: image = augment_rotate(image, angle_min=-4, angle_max=4) o = np.random.rand() if o < 0.5: image = augment_blur(image, width_min=1, width_max=3) image = augment_zoom(image, ratio_min=0.9, ratio_max=1.1) o = np.random.rand() if o > 0.5: image = augment_stretch_horizontal(image, ratio_min=1.0, ratio_max=1.2) else: image = augment_stretch_vertical(image, ratio_min=1.0, ratio_max=1.1) image = augment_shift(image, shift_h_min=-0.1, shift_h_max=0.1, shift_v_min=-0.1, shift_v_max=0.1) images_augmented[i] = np.squeeze(image) images_augmented = np.expand_dims(images_augmented, -1) return images_augmented # Atomic augmentations for CT scans def augment_rotate(image, angle_min=-30, angle_max=30): theta = np.random.uniform(angle_min, angle_max) (h, w) = image.shape[:2] (cX, cY) = (w // 2, h // 2) # rotate our image by 45 degrees around the center of the image M = cv2.getRotationMatrix2D((cX, cY), theta, 1.0) rotated = cv2.warpAffine(image, M, (w, h)) return rotated def augment_blur(image, width_min=2, width_max=10): w = np.random.randint(width_min, width_max + 1) blurred = cv2.blur(image, (w, w)) return blurred def augment_sharpen(image): sh_filter = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]]) image_sharpened = cv2.filter2D(image, -1, sh_filter) image_sharpened = image_sharpened - np.min(image_sharpened) image_sharpened = image_sharpened / np.max(image_sharpened) return image_sharpened def augment_noise(image): noise_mask = np.random.poisson(np.abs(image) * 255 / np.max(image)) image_noisy = image + noise_mask image_noisy = cv2.normalize(image_noisy, dst=None, alpha=0.0, beta=1.0, norm_type=cv2.NORM_MINMAX) return image_noisy def augment_invert(image): image_inverted = 1. - image return image_inverted def augment_zoom(image, ratio_min=0.8, ratio_max=1.2, pad_zeros=True): ratio = np.random.uniform(ratio_min, ratio_max) image_rescaled = cv2.resize(image, dsize=(int(image.shape[0] * ratio), int(image.shape[1] * ratio)), interpolation=cv2.INTER_CUBIC) outer_brim = np.concatenate((image[:, -1], image[-1, :], image[0, :], image[:, 0])) if pad_zeros: image_zoomed = np.zeros_like(image) else: image_zoomed = np.ones_like(image) * np.median(outer_brim) if ratio < 1.0: # Pad ht = image_rescaled.shape[0] wd = image_rescaled.shape[1] # Compute center offset xx = (image.shape[0] - wd) // 2 yy = (image.shape[1] - ht) // 2 image_zoomed[yy:yy + ht, xx:xx + wd] = image_rescaled else: # Crop center = [image_rescaled.shape[0] / 2, image_rescaled.shape[1] / 2] x = center[1] - image.shape[1] / 2 y = center[0] - image.shape[0] / 2 image_zoomed = image_rescaled[int(y):int(y + image.shape[0]), int(x):int(x + image.shape[1])] return image_zoomed def augment_shift(image, shift_h_min=-0.1, shift_h_max=0.1, shift_v_min=-0.1, shift_v_max=0.1, pad_zeros=True): shift_vertical = np.random.uniform(shift_v_min, shift_v_max) shift_horizontal = np.random.uniform(shift_h_min, shift_h_max) outer_brim = np.concatenate((image[:, -1], image[-1, :], image[0, :], image[:, 0])) if pad_zeros: image_shifted = np.zeros_like(image) else: image_shifted = np.ones_like(image) * np.median(outer_brim) if shift_vertical < 0: x0 = int(-shift_vertical * image.shape[0]) x1 = image.shape[0] - 1 x0_dest = 0 x1_dest = int(image.shape[0] + shift_vertical * image.shape[0]) else: x0 = 0 x1 = int(image.shape[0] - shift_vertical * image.shape[0]) x0_dest = int(shift_vertical * image.shape[0]) x1_dest = image.shape[0] - 1 if shift_horizontal < 0: y0 = int(-shift_horizontal * image.shape[1]) y1 = image.shape[1] - 1 y0_dest = 0 y1_dest = int(image.shape[1] + shift_horizontal * image.shape[1]) else: y0 = 0 y1 = int(image.shape[1] - shift_horizontal * image.shape[1]) y0_dest = int(shift_horizontal * image.shape[1]) y1_dest = image.shape[1] - 1 image_shifted[x0_dest:x1_dest, y0_dest:y1_dest] = image[x0:x1, y0:y1] return image_shifted def augment_stretch_horizontal(image, ratio_min=0.8, ratio_max=1.2, pad_zeros=True): ratio = np.random.uniform(ratio_min, ratio_max) outer_brim = np.concatenate((image[:, -1], image[-1, :], image[0, :], image[:, 0])) if pad_zeros: image_stretched = np.zeros_like(image) else: image_stretched = np.ones_like(image) * np.median(outer_brim) image_rescaled = cv2.resize(image, dsize=(int(image.shape[0] * ratio), image.shape[1]), interpolation=cv2.INTER_CUBIC) if ratio < 1.0: # Pad ht = image_rescaled.shape[0] wd = image_rescaled.shape[1] # Compute center offset xx = (image.shape[0] - wd) // 2 yy = (image.shape[1] - ht) // 2 image_stretched[:, xx:xx + wd] = image_rescaled else: # Crop center = [image_rescaled.shape[0] / 2, image_rescaled.shape[1] / 2] x = center[1] - image.shape[1] / 2 y = center[0] - image.shape[0] / 2 image_stretched = image_rescaled[:, int(x):int(x + image.shape[1])] return image_stretched def augment_stretch_vertical(image, ratio_min=0.8, ratio_max=1.2, pad_zeros=True): ratio = np.random.uniform(ratio_min, ratio_max) outer_brim = np.concatenate((image[:, -1], image[-1, :], image[0, :], image[:, 0])) if pad_zeros: image_stretched = np.zeros_like(image) else: image_stretched = np.ones_like(image) * np.median(outer_brim) image_rescaled = cv2.resize(image, dsize=(image.shape[0], int(image.shape[1] * ratio)), interpolation=cv2.INTER_CUBIC) if ratio < 1.0: # Pad ht = image_rescaled.shape[0] wd = image_rescaled.shape[1] # Compute center offset xx = (image.shape[0] - wd) // 2 yy = (image.shape[1] - ht) // 2 image_stretched[yy:yy + ht, :] = image_rescaled else: # Crop center = [image_rescaled.shape[0] / 2, image_rescaled.shape[1] / 2] x = center[1] - image.shape[1] / 2 y = center[0] - image.shape[0] / 2 image_stretched = image_rescaled[int(y):int(y + image.shape[0]), :] return image_stretched def augment_brightness(image, value_min=-0.1, value_max=0.1): u = (np.random.uniform(0, 1) >= 0.5) * 1.0 value = u * np.random.uniform(value_min, value_min / 2.0) + (1 - u) * np.random.uniform(value_max / 2.0, value_max) if value >= 0: image_augmented = np.where((1.0 - image) < value, 1.0, image + value) else: image_augmented = np.where(image < value, 0.0, image + value) return image_augmented
55,312
45.132611
120
py
vadesc
vadesc-main/datasets/nsclc_lung/nsclc_lung_data.py
""" Data loaders for NSCLC datasets. """ import os import re import numpy as np import progressbar import pandas as pd from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split from datasets.nsclc_lung.CT_preproc_utils import (preprocess_lung1_images, preprocess_radiogenomics_images, preprocess_radiogenomics_images_amc, preprocess_lung3_images, preprocess_basel_images, downscale_images, IGNORED_PATIENTS, IGNORED_RADIOGENOMICS_PATIENTS, IGNORED_LUNG3_PATIENTS, IGNORED_BASEL_PATIENTS) from utils.radiomics_utils import extract_radiomics_features # TODO: insert directories with CT scans and clinical data for NSCLC datasets LUNG1_CT_DIR = '...' RADIOGENOMICS_DIR = '...' LUNG3_DIR = '...' BASEL_DIR = '...' def generate_lung1_images(n_slices: int, dsize, seed=42, verbose=1, normalise_t=True): """ Loads Lung1 CT and survival data. """ np.random.seed(seed) # Load CT data X = preprocess_lung1_images(lung1_dir=LUNG1_CT_DIR, n_slices=n_slices, dsize=[256, 256], n_bins=40) # Downscale if dsize[0] < 256: X_d = np.zeros([X.shape[0], X.shape[1], dsize[0], dsize[1]]) if verbose > 0: bar = progressbar.ProgressBar(maxval=X.shape[0]) bar.start() print("Downsizing data...") for i in range(len(X)): X_d[i] = downscale_images(X[i], dsize) if verbose > 0: bar.update(i) X = np.expand_dims(X_d, axis=-1) print(X.shape) clinical_data = pd.read_csv('../datasets/nsclc_lung/clinical_data.csv') t = clinical_data['Survival.time'].values.astype('float32') d = clinical_data['deadstatus.event'].values.astype('float32') stages = clinical_data['clinical.T.Stage'].values stages[np.isnan(stages)] = 3 stages[stages == 5] = 4 c = stages - 1 c = c.astype('int32') # Normalisation if normalise_t: t = t / np.max(t) + 0.001 t = np.delete(t, 127) d = np.delete(d, 127) c = np.delete(c, 127) t = np.delete(t, IGNORED_PATIENTS) d = np.delete(d, IGNORED_PATIENTS) c = np.delete(c, IGNORED_PATIENTS) # Train-test split X_train, X_test, t_train, t_test, d_train, d_test, c_train, c_test = train_test_split(X, t, d, c, test_size=0.25, random_state=seed, stratify=np.digitize(t, np.quantile(t, np.array([0.3, 0.5, 0.75, 0.9])))) X_train = np.reshape(X_train, newshape=(X_train.shape[0] * X_train.shape[1], X_train.shape[2], X_train.shape[3], 1)) X_test = X_test[:, 0] X_test = np.reshape(X_test, newshape=(X_test.shape[0], X_test.shape[1], X_test.shape[2], 1)) return X_train, X_test, X_test, t_train, t_test, t_test, d_train, d_test, d_test, c_train, c_test, c_test def generate_radiogenomics_images(n_slices: int, dsize, seed=42, verbose=1, normalise_t=True): """ Loads a subset of NSCLC Radiogenomics CT and survival data. """ np.random.seed(seed) # Load CT data X = preprocess_radiogenomics_images(radiogenomics_dir=RADIOGENOMICS_DIR, n_slices=n_slices, dsize=[256, 256], n_bins=40) # Downscale if dsize[0] < 256: X_d = np.zeros([X.shape[0], X.shape[1], dsize[0], dsize[1]]) if verbose > 0: bar = progressbar.ProgressBar(maxval=X.shape[0]) bar.start() print("Downsizing data...") for i in range(len(X)): X_d[i] = downscale_images(X[i], dsize) if verbose > 0: bar.update(i) X = np.expand_dims(X_d, axis=-1) print(X.shape) clinical_data = pd.read_csv(os.path.join(RADIOGENOMICS_DIR, 'clinical_data.csv')) subj_ids = np.array([re.search('R01-0', str(clinical_data['Case ID'].values[i])) and not(re.search('R01-097', str(clinical_data['Case ID'].values[i])) or re.search('R01-098', str(clinical_data['Case ID'].values[i])) or re.search('R01-099', str(clinical_data['Case ID'].values[i]))) for i in range(len(clinical_data['Case ID'].values))]) subj_ids[subj_ids == None] = False subj_ids = subj_ids.astype(bool) t = (pd.to_datetime(clinical_data['Date of Last Known Alive']) - pd.to_datetime(clinical_data['CT Date'])).dt.days.values.astype('float32') t = t[subj_ids] d = clinical_data['Survival Status'].values d[d == 'Alive'] = 0 d[d == 'Dead'] = 1 d = d[subj_ids].astype('float32') stages = clinical_data['Pathological T stage'].values c = np.zeros_like(stages) c[np.logical_or(stages == 'T1a', stages == 'T1b')] = 0 c[np.logical_or(stages == 'T2a', stages == 'T2b')] = 1 c[stages == 'T3'] = 2 c[stages == 'T4'] = 3 c[stages == 'Tis'] = 0 c = c.astype('int32') c = c[subj_ids] # Normalisation if normalise_t: t = t / np.max(t) + 0.001 t = np.delete(t, IGNORED_RADIOGENOMICS_PATIENTS) d = np.delete(d, IGNORED_RADIOGENOMICS_PATIENTS) c = np.delete(c, IGNORED_RADIOGENOMICS_PATIENTS) # Train-test split X_train, X_test, t_train, t_test, d_train, d_test, c_train, c_test = train_test_split(X, t, d, c, test_size=0.25, random_state=seed, stratify=np.digitize(t, np.quantile(t, np.array([0.3, 0.5, 0.75, 0.9])))) X_train = np.reshape(X_train, newshape=(X_train.shape[0] * X_train.shape[1], X_train.shape[2], X_train.shape[3], 1)) X_test = X_test[:, 0] X_test = np.reshape(X_test, newshape=(X_test.shape[0], X_test.shape[1], X_test.shape[2], 1)) return X_train, X_test, X_test, t_train, t_test, t_test, d_train, d_test, d_test, c_train, c_test, c_test def generate_radiogenomics_images_amc(n_slices: int, dsize, seed=42, verbose=1, normalise_t=True): """ Loads a subset of NSCLC Radiogenomics CT and survival data. """ np.random.seed(seed) # Load CT data X = preprocess_radiogenomics_images_amc(radiogenomics_dir=RADIOGENOMICS_DIR, n_slices=n_slices, dsize=[256, 256], n_bins=40) # Downscale if dsize[0] < 256: X_d = np.zeros([X.shape[0], X.shape[1], dsize[0], dsize[1]]) if verbose > 0: bar = progressbar.ProgressBar(maxval=X.shape[0]) bar.start() print("Downsizing data...") for i in range(len(X)): X_d[i] = downscale_images(X[i], dsize) if verbose > 0: bar.update(i) X = np.expand_dims(X_d, axis=-1) print(X.shape) master_file = pd.read_csv(os.path.join(RADIOGENOMICS_DIR, 'master_file_amc.csv')) t = (pd.to_datetime(master_file['Date of last known alive']) - pd.to_datetime(master_file['CT date'])).dt.days.values.astype('float32') d = master_file['Survival status'].values d[d == 'Alive'] = 0 d[d == 'Dead'] = 1 d = d.astype('float32') # NB: no stage information in AMC subjects c = np.zeros_like(d) c = c.astype('int32') # Normalisation if normalise_t: t = t / np.max(t) + 0.001 # Train-test split X_train, X_test, t_train, t_test, d_train, d_test, c_train, c_test = train_test_split(X, t, d, c, test_size=0.25, random_state=seed, stratify=np.digitize(t, np.quantile(t, np.array([0.3, 0.5, 0.75, 0.9])))) X_train = np.reshape(X_train, newshape=(X_train.shape[0] * X_train.shape[1], X_train.shape[2], X_train.shape[3], 1)) X_test = X_test[:, 0] X_test = np.reshape(X_test, newshape=(X_test.shape[0], X_test.shape[1], X_test.shape[2], 1)) return X_train, X_test, X_test, t_train, t_test, t_test, d_train, d_test, d_test, c_train, c_test, c_test def generate_lung3_images(n_slices: int, dsize, seed=42, verbose=1, normalise_t=True): """ Loads Lung3 CT and survival data. """ np.random.seed(seed) # Load CT data X = preprocess_lung3_images(lung3_dir=LUNG3_DIR, n_slices=n_slices, dsize=[256, 256], n_bins=40) # Downscale if dsize[0] < 256: X_d = np.zeros([X.shape[0], X.shape[1], dsize[0], dsize[1]]) if verbose > 0: bar = progressbar.ProgressBar(maxval=X.shape[0]) bar.start() print("Downsizing data...") for i in range(len(X)): X_d[i] = downscale_images(X[i], dsize) if verbose > 0: bar.update(i) X = np.expand_dims(X_d, axis=-1) print(X.shape) master_table = pd.read_csv(os.path.join(LUNG3_DIR, 'Lung3_master.csv')) t = np.zeros((len(master_table['Case ID'].values), )) d = np.zeros((len(master_table['Case ID'].values),)) c = master_table['Tumor stage'].values - 1 t = np.delete(t, IGNORED_LUNG3_PATIENTS) d = np.delete(d, IGNORED_LUNG3_PATIENTS) c = np.delete(c, IGNORED_LUNG3_PATIENTS) # Normalisation if normalise_t: t = t / np.max(t) + 0.001 # Train-test split X_train, X_test, t_train, t_test, d_train, d_test, c_train, c_test = train_test_split(X, t, d, c, test_size=0.25, random_state=seed) X_train = np.reshape(X_train, newshape=(X_train.shape[0] * X_train.shape[1], X_train.shape[2], X_train.shape[3], 1)) X_test = X_test[:, 0] X_test = np.reshape(X_test, newshape=(X_test.shape[0], X_test.shape[1], X_test.shape[2], 1)) return X_train, X_test, X_test, t_train, t_test, t_test, d_train, d_test, d_test, c_train, c_test, c_test def generate_basel_images(n_slices: int, dsize, seed=42, verbose=1, normalise_t=True): """ Loads Basel University Hospital CT and survival data. """ np.random.seed(seed) # Load CT data X = preprocess_basel_images(basel_dir=BASEL_DIR, n_slices=n_slices, dsize=[256, 256], n_bins=40) # Downscale if dsize[0] < 256: X_d = np.zeros([X.shape[0], X.shape[1], dsize[0], dsize[1]]) if verbose > 0: bar = progressbar.ProgressBar(maxval=X.shape[0]) bar.start() print("Downsizing data...") for i in range(len(X)): X_d[i] = downscale_images(X[i], dsize) if verbose > 0: bar.update(i) X = np.expand_dims(X_d, axis=-1) print(X.shape) clinical_data = pd.read_csv('../datasets/nsclc_lung/clinical_data_basel.csv') t = clinical_data['Survival time'].values.astype('float32') d = clinical_data['Event'].values.astype('float32') c = np.zeros_like(d) # Normalisation if normalise_t: t = t / np.max(t) + 0.001 t = np.delete(t, IGNORED_BASEL_PATIENTS) d = np.delete(d, IGNORED_BASEL_PATIENTS) c = np.delete(c, IGNORED_BASEL_PATIENTS) # Train-test split X_train, X_test, t_train, t_test, d_train, d_test, c_train, c_test = \ train_test_split(X, t, d, c, test_size=0.25, random_state=seed, stratify=np.digitize(t, np.quantile(t, np.array([0.3, 0.5, 0.75, 0.9])))) X_train = np.reshape(X_train, newshape=(X_train.shape[0] * X_train.shape[1], X_train.shape[2], X_train.shape[3], 1)) X_test = X_test[:, 0] X_test = np.reshape(X_test, newshape=(X_test.shape[0], X_test.shape[1], X_test.shape[2], 1)) return X_train, X_test, X_test, t_train, t_test, t_test, d_train, d_test, d_test, c_train, c_test, c_test def generate_radiomic_features(n_slices: int, seed: int, dsize): """ Loads radiomic features from all NSCLC datasets with segmentations available. """ _ = preprocess_lung1_images(lung1_dir=LUNG1_CT_DIR, n_slices=n_slices, dsize=dsize, n_bins=40) _ = preprocess_radiogenomics_images(radiogenomics_dir=RADIOGENOMICS_DIR, n_slices=n_slices, dsize=dsize, n_bins=40) _ = preprocess_basel_images(basel_dir=BASEL_DIR, n_slices=n_slices, dsize=dsize, n_bins=40) seg_file = '../datasets/nsclc_lung/lung1_segmentations_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + \ str(dsize[1]) + '.npy' masks = np.load(file=seg_file, allow_pickle=True) masks = np.delete(masks, np.concatenate([IGNORED_PATIENTS, [127]]), axis=0) radiomic_features_lung1 = extract_radiomics_features( data_file='../datasets/nsclc_lung/lung1_best_slices_preprocessed_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str(dsize[1]) + '.npy', masks=masks) seg_file = '../datasets/nsclc_lung/radiogenomics_segmentations_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + \ str(dsize[1]) + '.npy' masks = np.load(file=seg_file, allow_pickle=True) masks = np.delete(masks, IGNORED_RADIOGENOMICS_PATIENTS, axis = 0) radiomic_features_radiogenomics = extract_radiomics_features( data_file='../datasets/nsclc_lung/radiogenomics_best_slices_preprocessed_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str(dsize[1]) + '.npy', masks=masks) seg_file = '../datasets/nsclc_lung/basel_segmentations_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + \ str(dsize[1]) + '.npy' masks = np.load(file=seg_file, allow_pickle=True) masks = np.delete(masks, IGNORED_BASEL_PATIENTS, axis = 0) basel_features_radiogenomics = extract_radiomics_features( data_file='../datasets/nsclc_lung/basel_best_slices_preprocessed_' + str(n_slices) + '_' + str(dsize[0]) + 'x' + str(dsize[1]) + '.npy', masks = masks) clinical_data_lung1 = pd.read_csv('../datasets/nsclc_lung/clinical_data.csv') t_lung1 = clinical_data_lung1['Survival.time'].values.astype('float32') d_lung1 = clinical_data_lung1['deadstatus.event'].values.astype('float32') stages_lung1 = clinical_data_lung1['clinical.T.Stage'].values stages_lung1[np.isnan(stages_lung1)] = 3 stages_lung1[stages_lung1 == 5] = 4 c_lung1 = stages_lung1 - 1 c_lung1 = c_lung1.astype('int32') t_lung1 = np.delete(t_lung1, np.concatenate([IGNORED_PATIENTS, [127]])) d_lung1 = np.delete(d_lung1, np.concatenate([IGNORED_PATIENTS, [127]])) c_lung1 = np.delete(c_lung1, np.concatenate([IGNORED_PATIENTS, [127]])) clinical_data_radiogenomics = pd.read_csv(os.path.join(RADIOGENOMICS_DIR, 'clinical_data.csv')) subj_ids = np.array([re.search('R01-0', str(clinical_data_radiogenomics['Case ID'].values[i])) and not ( re.search('R01-097', str(clinical_data_radiogenomics['Case ID'].values[i])) or re.search('R01-098', str( clinical_data_radiogenomics['Case ID'].values[i])) or re.search('R01-099', str(clinical_data_radiogenomics['Case ID'].values[i]))) for i in range(len(clinical_data_radiogenomics['Case ID'].values))]) subj_ids[subj_ids == None] = False subj_ids = subj_ids.astype(bool) t_radiogenomics = (pd.to_datetime(clinical_data_radiogenomics['Date of Last Known Alive']) - pd.to_datetime( clinical_data_radiogenomics['CT Date'])).dt.days.values.astype('float32') t_radiogenomics = t_radiogenomics[subj_ids] d_radiogenomics = clinical_data_radiogenomics['Survival Status'].values d_radiogenomics[d_radiogenomics == 'Alive'] = 0 d_radiogenomics[d_radiogenomics == 'Dead'] = 1 d_radiogenomics = d_radiogenomics[subj_ids].astype('float32') # d = d * 0 # Just use for AE stages_radiogenomics = clinical_data_radiogenomics['Pathological T stage'].values c_radiogenomics = np.zeros_like(stages_radiogenomics) c_radiogenomics[np.logical_or(stages_radiogenomics == 'T1a', stages_radiogenomics == 'T1b')] = 0 c_radiogenomics[np.logical_or(stages_radiogenomics == 'T2a', stages_radiogenomics == 'T2b')] = 1 c_radiogenomics[stages_radiogenomics == 'T3'] = 2 c_radiogenomics[stages_radiogenomics == 'T4'] = 3 c_radiogenomics[stages_radiogenomics == 'Tis'] = 0 c_radiogenomics = c_radiogenomics.astype('int32') c_radiogenomics = c_radiogenomics[subj_ids] t_radiogenomics = np.delete(t_radiogenomics, IGNORED_RADIOGENOMICS_PATIENTS) d_radiogenomics = np.delete(d_radiogenomics, IGNORED_RADIOGENOMICS_PATIENTS) c_radiogenomics = np.delete(c_radiogenomics, IGNORED_RADIOGENOMICS_PATIENTS) clinical_data_basel = pd.read_csv('../datasets/nsclc_lung/clinical_data_basel.csv') t_basel = clinical_data_basel['Survival time'].values.astype('float32') d_basel = clinical_data_basel['Event'].values.astype('float32') c_basel = np.zeros_like(d_basel) t_basel = np.delete(t_basel, IGNORED_BASEL_PATIENTS) d_basel = np.delete(d_basel, IGNORED_BASEL_PATIENTS) c_basel = np.delete(c_basel, IGNORED_BASEL_PATIENTS) X = np.concatenate((radiomic_features_lung1, radiomic_features_radiogenomics, basel_features_radiogenomics), axis=0) X = StandardScaler().fit_transform(X) X = X.astype(np.float64) t = np.concatenate((t_lung1, t_radiogenomics, t_basel)) d = np.concatenate((d_lung1, d_radiogenomics, d_basel)) c = np.concatenate((c_lung1, c_radiogenomics, c_basel)) t = t / np.max(t) + 0.001 # Train-test split X_train, X_test, t_train, t_test, d_train, d_test, c_train, c_test = train_test_split(X, t, d, c, test_size=0.25, random_state=seed, stratify=np.digitize(t, np.quantile(t, np.array([0.3, 0.5, 0.75, 0.9])))) return X_train, X_test, X_test, t_train, t_test, t_test, d_train, d_test, d_test, c_train, c_test, c_test
17,556
43.002506
120
py
vadesc
vadesc-main/datasets/survivalMNIST/survivalMNIST_data.py
""" Survival MNIST dataset. Based on Pölsterl's tutorial: https://k-d-w.org/blog/2019/07/survival-analysis-for-deep-learning/ https://github.com/sebp/survival-cnn-estimator """ import numpy as np from numpy.random import choice, uniform, normal import tensorflow as tf import tensorflow.keras.datasets.mnist as mnist def load_MNIST(split: str, flatten=True): (train_X, train_y), (test_X, test_y) = mnist.load_data() assert split == "train" or split == "test" # Flatten if flatten: train_X = train_X.reshape((train_X.shape[0], train_X.shape[1] * train_X.shape[2])) test_X = test_X.reshape((test_X.shape[0], test_X.shape[1] * test_X.shape[2])) if split == "train": return train_X, train_y else: return test_X, test_y def generate_surv_MNIST(n_groups: int, seed: int, p_cens: float, risk_range=[0.5, 15.0], risk_stdev=0.00, valid_perc=.05): assert 2 <= n_groups <= 10 assert risk_range[0] < risk_range[1] # Replicability np.random.seed(seed) tf.random.set_seed(seed) train_X, labels_train = load_MNIST(split="train") test_X, labels_test = load_MNIST(split="test") # Cluster assignments of digits c0 = choice(np.arange(n_groups), replace=False, size=(n_groups,)) c1 = np.array([]) if 10 - n_groups > 0: c1 = choice(np.arange(n_groups), replace=True, size=(10 - n_groups,)) c = np.concatenate((c0, c1)) np.random.shuffle(c) # Risk scores r_scores = uniform(risk_range[0], risk_range[1], size=(n_groups,)) r_scores = normal(r_scores[c], risk_stdev) print("-" * 50) print("Cluster Assignments & Risk Scores:") print("Digit: " + str(np.arange(10))) print("Risk group: " + str(c)) print("Risk score: " + str(r_scores)) print("-" * 50) print() print() r_scores_train = r_scores[labels_train] r_scores_test = r_scores[labels_test] stg_train = SurvivalTimeGenerator(num_samples=train_X.shape[0], mean_survival_time=150., prob_censored=p_cens) t_train, d_train = stg_train.gen_censored_time(r_scores_train) stg_test = SurvivalTimeGenerator(num_samples=test_X.shape[0], mean_survival_time=150., prob_censored=p_cens) t_test, d_test = stg_test.gen_censored_time(r_scores_test) c_train = c[labels_train] c_test = c[labels_test] t_train = t_train / max([np.max(t_train), np.max(t_test)]) + 0.001 t_test = t_test / max([np.max(t_train), np.max(t_test)]) + 0.001 if valid_perc > 0: n_valid = int(valid_perc * (train_X.shape[0] + test_X.shape[0])) shuffled_idx = np.arange(0, train_X.shape[0]) np.random.shuffle(shuffled_idx) train_idx = shuffled_idx[0:(shuffled_idx.shape[0] - n_valid)] valid_idx = shuffled_idx[(shuffled_idx.shape[0] - n_valid):] c_train_ = c_train[train_idx] c_valid = c_train[valid_idx] c_train = c_train_ return train_X[train_idx, :], train_X[valid_idx, :], test_X, \ t_train[train_idx], t_train[valid_idx], t_test, \ d_train[train_idx], d_train[valid_idx], d_test, \ c_train, c_valid, c_test else: return train_X, test_X, t_train, t_test, d_train, d_test, c_train, c_test class SurvivalTimeGenerator: def __init__(self, num_samples: int, mean_survival_time: float, prob_censored: float): self.num_samples = num_samples self.mean_survival_time = mean_survival_time self.prob_censored = prob_censored def gen_censored_time(self, risk_score: np.ndarray, seed: int = 89): rnd = np.random.RandomState(seed) # generate survival time baseline_hazard = 1. / self.mean_survival_time scale = baseline_hazard * np.exp(risk_score) u = rnd.uniform(low=0, high=1, size=risk_score.shape[0]) t = -np.log(u) / scale # generate time of censoring qt = np.quantile(t, 1.0 - self.prob_censored) c = rnd.uniform(low=t.min(), high=qt) # apply censoring observed_event = t <= c observed_time = np.where(observed_event, t, c) return observed_time, observed_event
4,151
34.487179
122
py
vadesc
vadesc-main/datasets/hgg/hgg_data.py
""" Dataset of high-grade glioma patients (HGG). Based on the code from Chapfuwa et al.: https://github.com/paidamoyo/survival_cluster_analysis """ import os import numpy as np import pandas from baselines.sca.sca_utils.pre_processing import one_hot_encoder, formatted_data, missing_proportion, \ one_hot_indices, get_train_median_mode, log_transform, impute_missing from sklearn.preprocessing import StandardScaler def generate_data(seed=42): np.random.seed(seed) dir_path = os.path.dirname(os.path.realpath(__file__)) path = os.path.abspath(os.path.join(dir_path, '', 'hgg.csv')) print("path:{}".format(path)) data_frame = pandas.read_csv(path, index_col=None) print("head of data:{}, data shape:{}".format(data_frame.head(), data_frame.shape)) print("missing:{}".format(missing_proportion(data_frame))) one_hot_encoder_list = ['sex', 'sy_group', 'pre_cognition', 'pre_motor_re_arm', 'pre_motor_li_arm', 'pre_motor_re_leg', 'pre_motor_li_leg', 'pre_sensibility', 'pre_language', 'pre_visualfield', 'pre_seizure', 'pre_headache', 'pre_nausea', 'post_cognition', 'post_motor_re_arm', 'post_motor_li_arm', 'post_motor_re_leg', 'post_motor_li_leg', 'post_sensibility', 'post_language', 'post_visualfield', 'post_seizure', 'post_headache', 'adjuvant_therapy', 'op_type', 'ultrasound', 'io_mri', 'ala', 'io_mapping', 'histology', 'antibody', 'idh1_seq', 'idh2_seq', 'mgmt', 'idh_status', 'tumor_side', 'frontal', 'central', 'parietal', 'occipital', 'temporal', 'insular', 'limbic', 'central_gray_matter', 't1_t2_pre_solid'] data_frame = one_hot_encoder(data=data_frame, encode=one_hot_encoder_list) print("na columns:{}".format(data_frame.columns[data_frame.isnull().any()].tolist())) t_data = data_frame[['loss_or_death_d']] e_data = 1 - data_frame[['censored']] c_data = 1 - data_frame[['censored']] c_data['censored'] = c_data['censored'].astype('category') c_data['censored'] = c_data['censored'].cat.codes to_drop = ['loss_or_death_d', 'censored'] x_data = data_frame.drop(labels=to_drop, axis=1) encoded_indices = one_hot_indices(x_data, one_hot_encoder_list) include_idx = set(np.array(sum(encoded_indices, []))) mask = np.array([(i in include_idx) for i in np.arange(x_data.shape[1])]) print("head of x data:{}, data shape:{}".format(x_data.head(), x_data.shape)) print("data description:{}".format(x_data.describe())) covariates = np.array(x_data.columns.values) print("columns:{}".format(covariates)) x = np.array(x_data).reshape(x_data.shape) t = np.array(t_data).reshape(len(t_data)) e = np.array(e_data).reshape(len(e_data)) c = np.array(c_data).reshape(len(c_data)) print("x:{}, t:{}, e:{}, len:{}".format(x[0], t[0], e[0], len(t))) idx = np.arange(0, x.shape[0]) print("x_shape:{}".format(x.shape)) np.random.shuffle(idx) x = x[idx] t = t[idx] e = e[idx] c = c[idx] # Normalization t = t / np.max(t) + 0.001 scaler = StandardScaler() scaler.fit(x[:, ~mask]) x[:, ~mask] = scaler.transform(x[:, ~mask]) end_time = max(t) print("end_time:{}".format(end_time)) print("observed percent:{}".format(sum(e) / len(e))) print("shuffled x:{}, t:{}, e:{}, len:{}".format(x[0], t[0], e[0], len(t))) num_examples = int(0.80 * len(e)) print("num_examples:{}".format(num_examples)) train_idx = idx[0: num_examples] split = int((len(t) - num_examples) / 2) test_idx = idx[num_examples: num_examples + split] valid_idx = idx[num_examples + split: len(t)] print("test:{}, valid:{}, train:{}, all: {}".format(len(test_idx), len(valid_idx), num_examples, len(test_idx) + len(valid_idx) + num_examples)) imputation_values = get_train_median_mode(x=x[train_idx], categorial=encoded_indices) preprocessed = { 'train': formatted_data(x=x, t=t, e=e, idx=train_idx, imputation_values=imputation_values), 'test': formatted_data(x=x, t=t, e=e, idx=test_idx, imputation_values=imputation_values), 'valid': formatted_data(x=x, t=t, e=e, idx=valid_idx, imputation_values=imputation_values) } preprocessed['train']['c'] = c[train_idx] preprocessed['valid']['c'] = c[valid_idx] preprocessed['test']['c'] = c[test_idx] return preprocessed def generate_hgg(seed=42): preproc = generate_data(seed) x_train = preproc['train']['x'] x_valid = preproc['valid']['x'] x_test = preproc['test']['x'] t_train = preproc['train']['t'] t_valid = preproc['valid']['t'] t_test = preproc['test']['t'] d_train = preproc['train']['e'] d_valid = preproc['valid']['e'] d_test = preproc['test']['e'] c_train = preproc['train']['c'] c_valid = preproc['valid']['c'] c_test = preproc['test']['c'] return x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test def generate_hgg_full(): dir_path = os.path.dirname(os.path.realpath(__file__)) path = os.path.abspath(os.path.join(dir_path, '', 'hgg.csv')) print("path:{}".format(path)) data_frame = pandas.read_csv(path, index_col=None) print("head of data:{}, data shape:{}".format(data_frame.head(), data_frame.shape)) print("missing:{}".format(missing_proportion(data_frame))) one_hot_encoder_list = ['sex', 'sy_group', 'pre_cognition', 'pre_motor_re_arm', 'pre_motor_li_arm', 'pre_motor_re_leg', 'pre_motor_li_leg', 'pre_sensibility', 'pre_language', 'pre_visualfield', 'pre_seizure', 'pre_headache', 'pre_nausea', 'post_cognition', 'post_motor_re_arm', 'post_motor_li_arm', 'post_motor_re_leg', 'post_motor_li_leg', 'post_sensibility', 'post_language', 'post_visualfield', 'post_seizure', 'post_headache', 'adjuvant_therapy', 'op_type', 'ultrasound', 'io_mri', 'ala', 'io_mapping', 'histology', 'antibody', 'idh1_seq', 'idh2_seq', 'mgmt', 'idh_status', 'tumor_side', 'frontal', 'central', 'parietal', 'occipital', 'temporal', 'insular', 'limbic', 'central_gray_matter', 't1_t2_pre_solid'] data_frame = one_hot_encoder(data=data_frame, encode=one_hot_encoder_list) print("na columns:{}".format(data_frame.columns[data_frame.isnull().any()].tolist())) t_data = data_frame[['loss_or_death_d']] e_data = 1 - data_frame[['censored']] c_data = 1 - data_frame[['censored']] c_data['censored'] = c_data['censored'].astype('category') c_data['censored'] = c_data['censored'].cat.codes to_drop = ['loss_or_death_d', 'censored'] x_data = data_frame.drop(labels=to_drop, axis=1) encoded_indices = one_hot_indices(x_data, one_hot_encoder_list) include_idx = set(np.array(sum(encoded_indices, []))) mask = np.array([(i in include_idx) for i in np.arange(x_data.shape[1])]) print("head of x data:{}, data shape:{}".format(x_data.head(), x_data.shape)) print("data description:{}".format(x_data.describe())) covariates = np.array(x_data.columns.values) print("columns:{}".format(covariates)) x = np.array(x_data).reshape(x_data.shape) t = np.array(t_data).reshape(len(t_data)) e = np.array(e_data).reshape(len(e_data)) c = np.array(c_data).reshape(len(c_data)) # Normalization t = t / np.max(t) + 0.001 scaler = StandardScaler() scaler.fit(x[:, ~mask]) x[:, ~mask] = scaler.transform(x[:, ~mask]) imputation_values = get_train_median_mode(x=x, categorial=encoded_indices) impute_covariates = impute_missing(data=x, imputation_values=imputation_values) return impute_covariates, t, e, c
8,022
44.327684
119
py
vadesc
vadesc-main/utils/sim_utils.py
""" Utility functions for numerical simulations. """ import numpy as np from sklearn.datasets import make_low_rank_matrix import pandas as pd def random_nonlin_map(n_in, n_out, n_hidden, rank=1000): # Random MLP mapping W_0 = make_low_rank_matrix(n_in, n_hidden, effective_rank=rank) W_1 = make_low_rank_matrix(n_hidden, n_hidden, effective_rank=rank) W_2 = make_low_rank_matrix(n_hidden, n_out, effective_rank=rank) # Disabled biases for now... b_0 = np.random.uniform(0, 0, (1, n_hidden)) b_1 = np.random.uniform(0, 0, (1, n_hidden)) b_2 = np.random.uniform(0, 0, (1, n_out)) nlin_map = lambda x: np.matmul(ReLU(np.matmul(ReLU(np.matmul(x, W_0) + np.tile(b_0, (x.shape[0], 1))), W_1) + np.tile(b_1, (x.shape[0], 1))), W_2) + \ np.tile(b_2, (x.shape[0], 1)) return nlin_map def ReLU(x): return x * (x > 0) def pp(start, end, n): start_u = start.value//10**9 end_u = end.value//10**9 return pd.DatetimeIndex((10**9*np.random.randint(start_u, end_u, n, dtype=np.int64)).view('M8[ns]'))
1,132
29.621622
106
py
vadesc
vadesc-main/utils/constants.py
# Project-wide constants: ROOT_LOGGER_STR = "VaDeSC" LOGGER_RESULT_FILE = "logs.txt"
84
27.333333
31
py
vadesc
vadesc-main/utils/plotting.py
""" Utility functions for plotting. """ import os import numpy as np from lifelines import KaplanMeierFitter import matplotlib.pyplot as plt from matplotlib import rc from openTSNE import TSNE as fastTSNE import sys sys.path.insert(0, '../') CB_COLOR_CYCLE = ['#377eb8', '#ff7f00', '#4daf4a', '#f781bf', '#a65628', '#984ea3', '#999999', '#e41a1c', '#dede00'] GRAY_COLOR_CYCLE = ['black', 'dimgray', 'darkgray', 'gainsboro', 'whitesmoke'] LINE_TYPES = ['solid', 'dashed', 'dashdot', 'dotted', 'dashed'] MARKER_STYLES = ['', '', '', '', ''] DASH_STYLES = [[], [4, 4], [4, 1], [1, 1, 1], [2, 1, 2]] def plotting_setup(font_size=12): # plot settings plt.style.use("seaborn-colorblind") plt.rcParams['font.size'] = font_size rc('text', usetex=False) def plot_overall_kaplan_meier(t, d, dir=None): kmf = KaplanMeierFitter() kmf.fit(t, d, label="Overall KM estimate") kmf.plot(ci_show=True) if dir is not None: plt.savefig(fname=os.path.join(dir, "km_plot.png"), dpi=300, pad_inches=0.2) plt.show() def plot_group_kaplan_meier(t, d, c, dir=None, experiment_name=''): fig = plt.figure() labels = np.unique(c) for l in labels: kmf = KaplanMeierFitter() kmf.fit(t[c == l], d[c == l], label="Cluster " + str(int(l + 1))) kmf.plot(ci_show=True, color=CB_COLOR_CYCLE[int(l)]) plt.xlabel("Time") plt.ylabel("Survival Probability") if dir is not None: plt.savefig(fname=os.path.join(dir, "km_group_plot_" + experiment_name +".png"), dpi=300, bbox_inches="tight") else: plt.show() def plot_bigroup_kaplan_meier(t, d, c, c_, dir=None, postfix=None, legend=False, legend_outside=False): fig = plt.figure() # Plot true clusters labels = np.unique(c) for l in labels: kmf = KaplanMeierFitter() if legend: kmf.fit(t[c == l], d[c == l], label="Cluster " + str(int(l + 1))) else: kmf.fit(t[c == l], d[c == l]) kmf.plot(ci_show=True, alpha=0.75, color=CB_COLOR_CYCLE[int(l)], linewidth=5) # Plot assigned clusters labels = np.unique(c_) for l in labels: kmf = KaplanMeierFitter() if legend: kmf.fit(t[c_ == l], d[c_ == l], label="Ass. cluster " + str(int(l + 1))) else: kmf.fit(t[c_ == l], d[c_ == l]) kmf.plot(ci_show=True, color='black', alpha=0.25, linestyle=LINE_TYPES[int(l)], dashes=DASH_STYLES[int(l)], linewidth=5) plt.xlabel("Time") plt.ylabel("Survival Probability") if legend: if legend_outside: leg = plt.legend(loc='upper right', frameon=False, bbox_to_anchor=(-0.15, 1)) else: leg = plt.legend(loc='lower right', frameon=False) else: leg = plt.legend('', frameon=False) if dir is not None: fname = 'km_bigroup_plot' if postfix is not None: fname += '_' + postfix fname += '.png' plt.savefig(fname=os.path.join(dir, fname), dpi=300, bbox_inches="tight") else: plt.show() def plot_dataset(X, t, d, c, font_size=12, seed=42, dir=None, postfix=None): plotting_setup(font_size=font_size) plot_group_kaplan_meier(t=t, d=d, c=c, dir=dir) if X.shape[0] > 10000: inds = np.random.choice(a=np.arange(0, X.shape[0]), size=(10000, )) c_ = c[inds] X_ = X[inds] else: c_ = c X_ = X X_embedded = fastTSNE(n_components=2, n_jobs=8, random_state=seed).fit(X_) fig = plt.figure() for l in np.unique(c_): plt.scatter(X_embedded[c_ == l, 0], X_embedded[c_ == l, 1], s=1.5, c=CB_COLOR_CYCLE[int(l)], label=("Cluster " + str(int(l + 1)))) plt.xlabel("t-SNE Dimension 1") plt.ylabel("t-SNE Dimension 2") plt.legend(markerscale=3.0) if dir is not None: fname = 'tsne' if postfix is not None: fname += '_' + postfix fname += '.png' plt.savefig(fname=os.path.join(dir, fname), dpi=300) else: plt.show() def plot_tsne_by_cluster(X, c, font_size=12, seed=42, dir=None, postfix=None): np.random.seed(seed) plotting_setup(font_size=font_size) if X.shape[0] > 10000: inds = np.random.choice(a=np.arange(0, X.shape[0]), size=(10000,)) c_ = c[inds] X_ = X[inds] else: c_ = c X_ = X X_embedded = fastTSNE(n_components=2, n_jobs=8, random_state=seed).fit(X_) fig = plt.figure() for l in np.unique(c_): plt.scatter(X_embedded[c_ == l, 0], X_embedded[c_ == l, 1], s=1.5, c=CB_COLOR_CYCLE[int(l)], label=("Cluster " + str(int(l + 1)))) plt.xlabel(r'$t$-SNE Dimension 1') plt.ylabel(r'$t$-SNE Dimension 2') plt.legend(markerscale=3.0) if dir is not None: fname = 'tsne_vs_c' if postfix is not None: fname += '_' + postfix fname += '.png' plt.savefig(fname=os.path.join(dir, fname), dpi=300) else: plt.show() def plot_tsne_by_survival(X, t, d, font_size=16, seed=42, dir=None, postfix=None, plot_censored=True): np.random.seed(seed) plotting_setup(font_size=font_size) if X.shape[0] > 10000: inds = np.random.choice(a=np.arange(0, X.shape[0]), size=(10000,)) t_ = t[inds] d_ = d[inds] X_ = X[inds] else: t_ = t d_ = d X_ = X X_embedded = fastTSNE(n_components=2, n_jobs=8, random_state=seed).fit(X_) fig = plt.figure() plt.scatter(X_embedded[d_ == 1, 0], X_embedded[d_ == 1, 1], s=1.5, c=np.log(t_[d_ == 1]), cmap='cividis', alpha=0.5) if plot_censored: plt.scatter(X_embedded[d_ == 0, 0], X_embedded[d_ == 0, 1], s=1.5, c=np.log(t_[d_ == 0]), cmap='cividis', alpha=0.5, marker='s') clb = plt.colorbar() clb.ax.set_title(r'$\log(T)$') plt.xlabel(r'$t$-SNE Dimension 1') plt.ylabel(r'$t$-SNE Dimension 2') plt.axis('off') if dir is not None: fname = 'tsne_vs_t' if postfix is not None: fname += '_' + postfix fname += '.png' plt.savefig(fname=os.path.join(dir, fname), dpi=300) else: plt.show() def plot_elbow(ks, avg, sd, xlab, ylab, dir=None): plotting_setup(16) plt.errorbar(ks, avg, yerr=sd, color=CB_COLOR_CYCLE[0], ecolor=CB_COLOR_CYCLE[0], barsabove=True, marker='D') plt.xlabel(xlab) plt.ylabel(ylab) if dir is not None: plt.savefig(fname=os.path.join(dir, "elbow_plot.png"), dpi=300, bbox_inches="tight") plt.show()
6,572
30.151659
120
py
vadesc
vadesc-main/utils/utils.py
""" miscellaneous utility functions. """ import matplotlib import matplotlib.pyplot as plt import logging from sklearn.utils.linear_assignment_ import linear_assignment import numpy as np from scipy.stats import weibull_min, fisk import sys from utils.constants import ROOT_LOGGER_STR import tensorflow as tf import tensorflow_probability as tfp tfd = tfp.distributions tfkl = tf.keras.layers tfpl = tfp.layers tfk = tf.keras matplotlib.use('Agg') sys.path.insert(0, '../../') logger = logging.getLogger(ROOT_LOGGER_STR + '.' + __name__) def setup_logger(results_path, create_stdlog): """Setup a general logger which saves all logs in the experiment folder""" f_format = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') f_handler = logging.FileHandler(str(results_path)) f_handler.setLevel(logging.DEBUG) f_handler.setFormatter(f_format) root_logger = logging.getLogger(ROOT_LOGGER_STR) root_logger.handlers = [] root_logger.setLevel(logging.DEBUG) root_logger.addHandler(f_handler) if create_stdlog: handler = logging.StreamHandler(sys.stdout) handler.setLevel(logging.INFO) root_logger.addHandler(handler) def cluster_acc(y_true, y_pred): """ Calculate clustering accuracy. Require scikit-learn installed # Arguments y: true labels, numpy.array with shape `(n_samples,)` y_pred: predicted labels, numpy.array with shape `(n_samples,)` # Return accuracy, in [0,1] """ y_true = y_true.astype(np.int64) assert y_pred.size == y_true.size D = max(y_pred.astype(int).max(), y_true.astype(int).max()) + 1 w = np.zeros((int(D), (D)), dtype=np.int64) for i in range(y_pred.size): w[int(y_pred[i]), int(y_true[i])] += 1 ind = linear_assignment(w.max() - w) return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size def sample_weibull(scales, shape, n_samples=200): return np.transpose(weibull_min.rvs(shape, loc=0, scale=scales, size=(n_samples, scales.shape[0]))) def save_mnist_reconstructions(recs, x, y): labels = y[:, 2] unique_labels = np.unique(labels) imgs_sampled = [] recs_sampled = [] for l in unique_labels: recs_l = recs[labels == l, :, :] x_l = x[labels == l, :] y_l = y[labels == l] j = np.random.randint(0, len(y_l)) imgs_sampled.append(np.reshape(x_l[j, :], (28, 28))) recs_sampled.append(np.reshape(recs_l[j, 0, :], (28, 28))) imgs_cat = np.concatenate(imgs_sampled, axis=1) recs_cat = np.concatenate(recs_sampled, axis=1) img_final = np.concatenate([imgs_cat, recs_cat], axis=0) plt.imsave("recs.png", img_final) def save_mnist_generated_samples(model, grid_size=4): for j in range(model.num_clusters): samples = model.generate_samples(j=j, n_samples=grid_size**2) cnt = 0 img = None for k in range(grid_size): row_k = [] for l in range(grid_size): row_k.append(np.reshape(samples[cnt, :], (28, 28))) cnt = cnt + 1 if img is None: img = np.concatenate(row_k, axis=1) else: img = np.concatenate([img, np.concatenate(row_k, axis=1)], axis=0) plt.imsave("generated_" + str(j) + ".png", img) def save_generated_samples(model, inp_size, grid_size=4, cmap='viridis', postfix=None): for j in range(model.num_clusters): samples = model.generate_samples(j=j, n_samples=grid_size**2) cnt = 0 img = None for k in range(grid_size): row_k = [] for l in range(grid_size): row_k.append(np.reshape(samples[0, cnt, :], (inp_size[0], inp_size[1]))) cnt = cnt + 1 if img is None: img = np.concatenate(row_k, axis=1) else: img = np.concatenate([img, np.concatenate(row_k, axis=1)], axis=0) if postfix is not None: plt.imsave("generated_" + str(j) + "_" + postfix + ".png", img, cmap=cmap) else: plt.imsave("generated_" + str(j) + ".png", img, cmap=cmap) # Weibull(lmbd, k) log-pdf def weibull_log_pdf(t, d, lmbd, k): t_ = tf.ones_like(lmbd) * tf.cast(t, tf.float64) d_ = tf.ones_like(lmbd) * tf.cast(d, tf.float64) k = tf.cast(k, tf.float64) a = t_ / (1e-60 + tf.cast(lmbd, tf.float64)) tf.debugging.check_numerics(a, message="weibull_log_pdf") return tf.cast(d_, tf.float64) * (tf.math.log(1e-60 + k) - tf.math.log(1e-60 + tf.cast(lmbd, tf.float64)) + (k - 1) * tf.math.log(1e-60 + tf.cast(t_, tf.float64)) - (k - 1) * tf.math.log(1e-60 + tf.cast(lmbd, tf.float64))) - (a) ** k def weibull_scale(x, beta): beta_ = tf.cast(beta, tf.float64) beta_ = tf.cast(tf.ones([tf.shape(x)[0], tf.shape(x)[1], beta.shape[0]]), tf.float64) * beta_ return tf.clip_by_value(tf.math.log(1e-60 + 1.0 + tf.math.exp(tf.reduce_sum(-tf.cast(x, tf.float64) * beta_[:, :, :-1], axis=2) - tf.cast(beta[-1], tf.float64))), -1e+64, 1e+64) def sample_weibull_mixture(scales, shape, p_c, n_samples=200): scales_ = np.zeros((scales.shape[0], n_samples)) cs = np.zeros((scales.shape[0], n_samples)).astype(int) for i in range(scales.shape[0]): cs[i] = np.random.choice(a=np.arange(0, p_c.shape[1]), p=p_c[i], size=(n_samples,)) scales_[i] = scales[i, cs[i]] return scales_ * np.random.weibull(shape, size=(scales.shape[0], n_samples)) def tensor_slice(target_tensor, index_tensor): indices = tf.stack([tf.range(tf.shape(index_tensor)[0]), index_tensor], 1) return tf.gather_nd(target_tensor, indices)
5,806
34.408537
133
py
vadesc
vadesc-main/utils/data_utils.py
""" Utility functions for data loading. """ import tensorflow as tf import tensorflow_probability as tfp import numpy as np from sklearn.model_selection import train_test_split import pandas as pd from sklearn.preprocessing import StandardScaler from tensorflow.keras.utils import to_categorical from datasets.survivalMNIST.survivalMNIST_data import generate_surv_MNIST from datasets.simulations import simulate_nonlin_profile_surv from datasets.support.support_data import generate_support from datasets.hgg.hgg_data import generate_hgg, generate_hgg_full from datasets.hemodialysis.hemo_data import generate_hemo from datasets.nsclc_lung.nsclc_lung_data import generate_lung1_images, generate_radiogenomics_images, \ generate_radiogenomics_images_amc, generate_lung3_images, generate_basel_images, generate_radiomic_features from datasets.nsclc_lung.CT_preproc_utils import augment_images tfd = tfp.distributions tfkl = tf.keras.layers tfpl = tfp.layers tfk = tf.keras class DataGen(tf.keras.utils.Sequence): def __init__(self, X, y, num_classes, ae=False, ae_class=False, batch_size=32, shuffle=True, augment=False): self.batch_size = batch_size self.X = X self.y = y self.ae = ae self.ae_class = ae_class self.num_classes = num_classes self.augment = augment self.shuffle = shuffle self.on_epoch_end() def on_epoch_end(self): if self.shuffle: inds = np.arange(len(self.X)) np.random.shuffle(inds) self.X = self.X[inds] self.y = self.y[inds] def __getitem__(self, index): X = self.X[index * self.batch_size:(index + 1) * self.batch_size] y = self.y[index * self.batch_size:(index + 1) * self.batch_size] # augmentation if self.augment: X = augment_images(X) if self.ae: return X, {'dec': X} elif self.ae_class: c = to_categorical(y[:, 2], self.num_classes) return X, {'dec': X, 'classifier': c} else: return (X, y), {"output_1": X, "output_4": y, "output_5": y} def __len__(self): return len(self.X) // self.batch_size def get_gen(X, y, configs, batch_size, validation=False, ae=False, ae_class=False): num_clusters = configs['training']['num_clusters'] input_dim = configs['training']['inp_shape'] if isinstance(input_dim, list) and validation==False: if ae_class: data_gen = DataGen(X, y, 4, augment=True, ae=ae, ae_class=ae_class, batch_size=batch_size) else: data_gen = DataGen(X, y, num_clusters, augment=True, ae=ae, ae_class=ae_class, batch_size=batch_size) else: if ae_class: data_gen = DataGen(X, y, 4, ae=ae, ae_class=ae_class, batch_size=batch_size) else: data_gen = DataGen(X, y, num_clusters, ae=ae, ae_class=ae_class, batch_size=batch_size) return data_gen def get_data(args, configs, val=False): if args.data == 'mnist': valid_perc = .15 if not val: valid_perc = .0 if val: x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = \ generate_surv_MNIST(n_groups=5, seed=args.seed, p_cens=.3, valid_perc=valid_perc) else: x_train, x_test, t_train, t_test, d_train, d_test, c_train, c_test = generate_surv_MNIST(n_groups=5, seed=args.seed, p_cens=.3, valid_perc=valid_perc) x_valid = x_test t_valid = t_test c_valid = c_test # Normalisation x_test = x_test / 255. if val: x_valid = x_valid / 255. x_train = x_train / 255. dat_label_train = np.zeros_like(t_train) dat_label_valid = np.zeros_like(t_valid) dat_label_test = np.zeros_like(t_test) elif args.data == "sim": X, t, d, c, Z, mus, sigmas, betas, betas_0, mlp_dec = simulate_nonlin_profile_surv(p=1000, n=60000, latent_dim=16, k=3, p_cens=.3, seed=args.seed, clust_mean=True, clust_cov=True, clust_coeffs=True, clust_intercepts=True, balanced=True, weibull_k=1, brange=[-10.0, 10.0], isotropic=True, xrange=[-.5, .5]) # Normalisation t = t / np.max(t) + 0.001 scaler = StandardScaler() scaler.fit(X) X = scaler.transform(X) x_train, x_test, t_train, t_test, d_train, d_test, c_train, c_test = train_test_split(X, t, d, c, test_size=.3, random_state=args.seed) dat_label_train = np.zeros_like(t_train) dat_label_valid = np.zeros_like(t_test) dat_label_test = np.zeros_like(t_test) elif args.data == "support": x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = \ generate_support(seed=args.seed) dat_label_train = np.zeros_like(t_train) dat_label_valid = np.zeros_like(t_valid) dat_label_test = np.zeros_like(t_test) elif args.data == "flchain": data = pd.read_csv('../baselines/DCM/data/flchain.csv') feats = ['age', 'sex', 'sample.yr', 'kappa', 'lambda', 'flc.grp', 'creatinine', 'mgus'] prot = 'sex' feats = set(feats) feats = list(feats) # - set([prot])) t = data['futime'].values + 1 d = data['death'].values x = data[feats].values c = data[prot].values X = StandardScaler().fit_transform(x) t = t / np.max(t) + 0.001 x_train, x_test, t_train, t_test, d_train, d_test, c_train, c_test = train_test_split(X, t, d, c, test_size=.3, random_state=args.seed) dat_label_train = np.zeros_like(t_train) dat_label_valid = np.zeros_like(t_train) dat_label_test = np.zeros_like(t_test) elif args.data == "hgg": x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = \ generate_hgg(seed=args.seed) dat_label_train = np.zeros_like(t_train) dat_label_valid = np.zeros_like(t_valid) dat_label_test = np.zeros_like(t_test) elif args.data == 'hemo': c = configs['training']['num_clusters'] x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = \ generate_hemo(seed=args.seed, label=c) dat_label_train = np.zeros_like(t_train) dat_label_valid = np.zeros_like(t_valid) dat_label_test = np.zeros_like(t_test) elif args.data == 'nsclc_features': x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = \ generate_radiomic_features(n_slices=11, dsize=[256, 256], seed=args.seed) dat_label_train = np.zeros_like(t_train) dat_label_valid = np.zeros_like(t_valid) dat_label_test = np.zeros_like(t_test) elif args.data == 'lung1': x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = \ generate_lung1_images(dsize=(configs['training']['inp_shape'][0], configs['training']['inp_shape'][1]), n_slices=configs['training']['n_slices'], seed=args.seed) dat_label_train = np.zeros_like(t_train) dat_label_valid = np.zeros_like(t_valid) dat_label_test = np.zeros_like(t_test) elif args.data == 'basel': x_train, x_valid, x_test, t_train, t_valid, t_test, d_train, d_valid, d_test, c_train, c_valid, c_test = \ generate_basel_images(dsize=(configs['training']['inp_shape'][0], configs['training']['inp_shape'][1]), n_slices=configs['training']['n_slices'], seed=args.seed, normalise_t=False) dat_label_train = np.zeros_like(t_train) dat_label_valid = np.zeros_like(t_valid) dat_label_test = np.zeros_like(t_test) elif args.data == 'nsclc': x_train_l, x_valid_l, x_test_l, t_train_l, t_valid_l, t_test_l, d_train_l, d_valid_l, d_test_l, c_train_l, c_valid_l, c_test_l = \ generate_lung1_images(dsize=(configs['training']['inp_shape'][0], configs['training']['inp_shape'][1]), n_slices=configs['training']['n_slices'], seed=args.seed, normalise_t=False) x_train_r, x_valid_r, x_test_r, t_train_r, t_valid_r, t_test_r, d_train_r, d_valid_r, d_test_r, c_train_r, c_valid_r, c_test_r = \ generate_radiogenomics_images(dsize=(configs['training']['inp_shape'][0], configs['training']['inp_shape'][1]), n_slices=configs['training']['n_slices'], seed=args.seed, normalise_t=False) x_train_ra, x_valid_ra, x_test_ra, t_train_ra, t_valid_ra, t_test_ra, d_train_ra, d_valid_ra, d_test_ra, c_train_ra, c_valid_ra, c_test_ra = \ generate_radiogenomics_images_amc(dsize=(configs['training']['inp_shape'][0], configs['training']['inp_shape'][1]), n_slices=configs['training']['n_slices'], seed=args.seed, normalise_t=False) x_train_l3, x_valid_l3, x_test_l3, t_train_l3, t_valid_l3, t_test_l3, d_train_l3, d_valid_l3, d_test_l3, c_train_l3, c_valid_l3, c_test_l3 = \ generate_lung3_images(dsize=(configs['training']['inp_shape'][0], configs['training']['inp_shape'][1]), n_slices=configs['training']['n_slices'], seed=args.seed, normalise_t=False) x_train_b, x_valid_b, x_test_b, t_train_b, t_valid_b, t_test_b, d_train_b, d_valid_b, d_test_b, c_train_b, c_valid_b, c_test_b = \ generate_basel_images(dsize=(configs['training']['inp_shape'][0], configs['training']['inp_shape'][1]), n_slices=configs['training']['n_slices'], seed=args.seed, normalise_t=False) x_train = np.concatenate((x_train_l, x_train_r, x_train_ra, x_train_l3, x_test_l3, x_train_b), axis=0) x_valid = np.concatenate((x_test_l, x_test_r, x_test_ra, x_test_b), axis=0) x_test = np.concatenate((x_test_l, x_test_r, x_test_ra, x_test_b), axis=0) dat_label_train = np.concatenate((np.zeros_like(t_train_l), np.ones_like(t_train_r), 2 * np.ones_like(t_train_ra), 3 * np.ones_like(t_train_l3), 3 * np.ones_like(t_test_l3), 4 * np.ones_like(t_train_b))) dat_label_valid = np.concatenate((np.zeros_like(t_test_l), np.ones_like(t_test_r), 2 * np.ones_like(t_test_ra), 4 * np.ones_like(t_test_b))) dat_label_test = np.concatenate((np.zeros_like(t_test_l), np.ones_like(t_test_r), 2 * np.ones_like(t_test_ra), 4 * np.ones_like(t_test_b))) t_train = np.concatenate((t_train_l, t_train_r, t_train_ra, t_train_l3, t_test_l3, t_train_b), axis=0) t_valid = np.concatenate((t_test_l, t_test_r, t_test_ra, t_test_b), axis=0) t_test = np.concatenate((t_test_l, t_test_r, t_test_ra, t_test_b), axis=0) d_train = np.concatenate((d_train_l, d_train_r, d_train_ra, d_train_l3, d_test_l3, d_train_b), axis=0) d_valid = np.concatenate((d_test_l, d_test_r, d_test_ra, d_test_b), axis=0) d_test = np.concatenate((d_test_l, d_test_r, d_test_ra, d_test_b), axis=0) c_train = np.concatenate((c_train_l, c_train_r, c_train_ra, c_train_l3, c_test_l3, c_train_b), axis=0) c_valid = np.concatenate((c_test_l, c_test_r, c_test_ra, c_test_b), axis=0) c_test = np.concatenate((c_test_l, c_test_r, c_test_ra, c_test_b), axis=0) t_max = np.max(np.concatenate((t_train, t_test))) t_train = t_train / t_max + 0.001 t_valid = t_valid / t_max + 0.001 t_test = t_test / t_max + 0.001 else: NotImplementedError('This dataset is not supported!') # Wrap t, d, and c together y_train = np.stack([t_train, d_train, c_train, dat_label_train], axis=1) if val: y_valid = np.stack([t_valid, d_valid, c_valid, dat_label_valid], axis=1) y_test = np.stack([t_test, d_test, c_test, dat_label_test], axis=1) np.savetxt(fname='y_train_nsclc_' + str(args.seed) + '.csv', X=y_train) np.savetxt(fname='y_test_nsclc_' + str(args.seed) + '.csv', X=y_test) if val: return x_train, x_valid, x_test, y_train, y_valid, y_test else: return x_train, x_test, x_test, y_train, y_test, y_test def construct_surv_df(X, t, d): p = X.shape[1] df = pd.DataFrame(X, columns=["X_" + str(i) for i in range(p)]) df["time_to_event"] = t df["failure"] = d return df
14,038
54.710317
150
py
vadesc
vadesc-main/utils/radiomics_utils.py
""" Utility functions for extracting radiomics features. """ import os import shutil import numpy as np import cv2 import logging import progressbar from radiomics import featureextractor def extract_radiomics_features(data_file, masks, verbose=1): # Set logging for the radiomics library logger = logging.getLogger("radiomics") logger.setLevel(logging.ERROR) # Load images and segmentation masks images = np.load(file=data_file, allow_pickle=True) print(images.shape, masks.shape) assert images.shape == masks.shape # Create a temporary directory for images and masks if os.path.exists('./radiomics_features_temp'): shutil.rmtree('./radiomics_features_temp') else: os.makedirs('./radiomics_features_temp') n_images = images.shape[0] if verbose: print('Extracting radiomics features...') bar = progressbar.ProgressBar(maxval=n_images) bar.start() # Feature extraction by PyRadiomics extractor = featureextractor.RadiomicsFeatureExtractor() extractor.enableAllFeatures() radiomics_features = None for i in range(n_images): # Create a directory for each image os.makedirs('./radiomics_features_temp/' + str(i)) imageName = './radiomics_features_temp/' + str(i) + '/image.png' maskName = './radiomics_features_temp/' + str(i) + '/mask.png' cv2.imwrite(filename=imageName, img=images[i, 0]) cv2.imwrite(filename=maskName, img=masks[i, 0]) # Provide mask and image files to the extractor result = extractor.execute(imageFilepath=imageName, maskFilepath=maskName) result_features = [val for key, val in result.items() if 'original_' in key and 'diagnostics_' not in key] result_features = [float(r) for r in result_features] if radiomics_features is None: radiomics_features = np.zeros((n_images, len(result_features))) radiomics_features[i] = result_features if verbose > 0: bar.update(i) shutil.rmtree('./radiomics_features_temp') return radiomics_features
2,116
29.242857
114
py
vadesc
vadesc-main/utils/eval_utils.py
""" Utility functions for model evaluation. """ import numpy as np from lifelines.utils import concordance_index import sys from sklearn.utils.linear_assignment_ import linear_assignment from sklearn.metrics.cluster import normalized_mutual_info_score import tensorflow as tf from lifelines import KaplanMeierFitter from scipy import stats from scipy.stats import linregress sys.path.insert(0, '../') def accuracy_metric(inp, p_c_z): y = inp[:, 2] y_pred = tf.math.argmax(p_c_z, axis=-1) return tf.numpy_function(normalized_mutual_info_score, [y, y_pred], tf.float64) def cindex_metric(inp, risk_scores): # Evaluates the concordance index based on provided predicted risk scores, computed using hard clustering # assignments. t = inp[:, 0] d = inp[:, 1] risk_scores = tf.squeeze(risk_scores) return tf.cond(tf.reduce_any(tf.math.is_nan(risk_scores)), lambda: tf.numpy_function(cindex, [t, d, tf.zeros_like(risk_scores)], tf.float64), lambda: tf.numpy_function(cindex, [t, d, risk_scores], tf.float64)) def cindex(t: np.ndarray, d: np.ndarray, scores_pred: np.ndarray): """ Evaluates concordance index based on the given predicted risk scores. :param t: observed time-to-event. :param d: labels of the type of even observed. d[i] == 1, if the i-th event is failure (death); d[i] == 0 otherwise. :param scores_pred: predicted risk/hazard scores. :return: return the concordance index. """ try: ci = concordance_index(event_times=t, event_observed=d, predicted_scores=scores_pred) except ZeroDivisionError: print('Cannot devide by zero.') ci = float(0.5) return ci def rae(t_pred, t_true, cens_t): # Relative absolute error as implemented by Chapfuwa et al. abs_error_i = np.abs(t_pred - t_true) pred_great_empirical = t_pred > t_true min_rea_i = np.minimum(np.divide(abs_error_i, t_true + 1e-8), 1.0) idx_cond = np.logical_and(cens_t, pred_great_empirical) min_rea_i[idx_cond] = 0.0 return np.sum(min_rea_i) / len(t_true) def calibration(predicted_samples, t, d): kmf = KaplanMeierFitter() kmf.fit(t, event_observed=d) range_quant = np.arange(start=0, stop=1.010, step=0.010) t_empirical_range = np.unique(np.sort(np.append(t, [0]))) km_pred_alive_prob = [kmf.predict(i) for i in t_empirical_range] empirical_dead = 1 - np.array(km_pred_alive_prob) km_dead_dist, km_var_dist, km_dist_ci = compute_km_dist(predicted_samples, t_empirical_range=t_empirical_range, event=d) slope, intercept, r_value, p_value, std_err = linregress(x=km_dead_dist, y=empirical_dead) return slope # Bounds def ci_bounds(surv_t, cumulative_sq_, alpha=0.95): # print("surv_t: ", surv_t, "cumulative_sq_: ", cumulative_sq_) # This method calculates confidence intervals using the exponential Greenwood formula. # See https://www.math.wustl.edu/%7Esawyer/handouts/greenwood.pdf # alpha = 0.95 if surv_t > 0.999: surv_t = 1 cumulative_sq_ = 0 alpha = 0.95 constant = 1e-8 alpha2 = stats.norm.ppf((1. + alpha) / 2.) v = np.log(surv_t) left_ci = np.log(-v) right_ci = alpha2 * np.sqrt(cumulative_sq_) * 1 / v c_plus = left_ci + right_ci c_neg = left_ci - right_ci ci_lower = np.exp(-np.exp(c_plus)) ci_upper = np.exp(-np.exp(c_neg)) return [ci_lower, ci_upper] # Population wise cdf def compute_km_dist(predicted_samples, t_empirical_range, event): km_dead = [] km_surv = 1 km_var = [] km_ci = [] km_sum = 0 kernel = [] e_event = event for j in np.arange(len(t_empirical_range)): r = t_empirical_range[j] low = 0 if j == 0 else t_empirical_range[j - 1] area = 0 censored = 0 dead = 0 at_risk = len(predicted_samples) count_death = 0 for i in np.arange(len(predicted_samples)): e = e_event[i] if len(kernel) != len(predicted_samples): kernel_i = stats.gaussian_kde(predicted_samples[i]) kernel.append(kernel_i) else: kernel_i = kernel[i] at_risk = at_risk - kernel_i.integrate_box_1d(low=0, high=low) if e == 1: count_death += kernel_i.integrate_box_1d(low=low, high=r) if at_risk == 0: break km_int_surv = 1 - count_death / at_risk km_int_sum = count_death / (at_risk * (at_risk - count_death)) km_surv = km_surv * km_int_surv km_sum = km_sum + km_int_sum km_ci.append(ci_bounds(cumulative_sq_=km_sum, surv_t=km_surv)) km_dead.append(1 - km_surv) km_var.append(km_surv * km_surv * km_sum) return np.array(km_dead), np.array(km_var), np.array(km_ci) def cluster_acc(y_true, y_pred): """ Calculate clustering accuracy. # Arguments y: true labels, numpy.array with shape `(n_samples,)` y_pred: predicted labels, numpy.array with shape `(n_samples,)` # Return accuracy, in [0,1] """ y_true = y_true.astype(np.int64) assert y_pred.size == y_true.size D = max(y_pred.astype(int).max(), y_true.astype(int).max()) + 1 w = np.zeros((int(D), (D)), dtype=np.int64) for i in range(y_pred.size): w[int(y_pred[i]), int(y_true[i])] += 1 ind = linear_assignment(w.max() - w) return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size
5,546
31.063584
120
py
vadesc
vadesc-main/posthoc_explanations/explainer_utils.py
import pandas as pd import numpy as np from sklearn.preprocessing import MinMaxScaler, StandardScaler import keras import math import seaborn as sns import matplotlib.pyplot as plt import matplotlib ############### PROTOTYPES SAMPLING UTILITY FUNCTIONS ##################################### def Prototypes_sampler(cluster, X, pcz, sample_size, p_threshold): #X = pd.DataFrame(X) # Function to extract prototypes from X assigned to cluster c with high probability (>= pcz_threshold) High_p_c_df = pd.DataFrame(pcz.loc[(pcz.iloc[:,cluster] > p_threshold), cluster]) # make sure we sample always the same prototypes for each cluster np.random.seed(seed=42) # Check if there are enough observations with high probability to sample for the given cluster if len(High_p_c_df) <= sample_size: id_X = High_p_c_df.index else: id_X = High_p_c_df.sample(n=sample_size).index Prototypes_c = X.iloc[id_X] return Prototypes_c, id_X def extract_prototypes_list(X, clusters_labs, pcz, n_prototypes, p_threshold): proto_id_list = [] for cluster in clusters_labs: df, proto_id = Prototypes_sampler(cluster, X, pcz, sample_size = n_prototypes, p_threshold = p_threshold) proto_id_list.append(proto_id) return proto_id_list def build_prototypes_ds(X, num_clusters, proto_id_list): Prototypes_ds = pd.DataFrame() proto_labels = [] for i in range(0,num_clusters): df = X.iloc[proto_id_list[i],:] lab = np.full((np.shape(df)[0],), i) Prototypes_ds = pd.concat([Prototypes_ds, df], axis=0) proto_labels = np.append(proto_labels, lab) return Prototypes_ds, proto_labels ############### HEMO DATA UTILS ################# def import_hemo_covnames(): cov_names = ['ageStart', 'myspKtV', 'myektv', 'UFR_mLkgh', 'zwtpost', 'CharlsonScore', 'diabetes', 'cardiovascular', 'ctd', 'mean_albumin', 'mean_nPCR', 'mean_ldh', 'mean_creatinine', 'mean_hematocrit', 'mean_iron', 'mean_neutrophils', 'mean_lymphocytes', 'mean_rdw', 'mean_rbc', 'mean_ag_ratio', 'mean_caxphos_c', 'mean_hemoglobin', 'mean_pth', 'mean_uf', 'mean_uf_percent', 'mean_idwg_day', 'mean_preSBP', 'mean_postSBP', 'mean_lowestSBP', 'TBWchild', 'TBWadult', 'BSA', 'cTargetDryWeightKg', 'WeightPostKg', 'spktv_cheek_BSA', 'spktv_cheek_W067', 'spktv_cheek_W075', 'spktv_watson_BSA', 'spktv_watson_W067', 'spktv_watson_W075', 'tidwg2', 'tuf_percent', 'PatientGender_F', 'PatientRace4_African', 'PatientRace4_Caucasian', 'PatientRace4_Hispanic', 'USRDS_class_Cystic/hereditary/congenital diseases', 'USRDS_class_Diabetes', 'USRDS_class_Glomerulonephritis', 'USRDS_class_Hypertensive/large vessel disease', 'USRDS_class_Interstitial nephritis/pyelonephritis', 'USRDS_class_Miscellaneous conditions ', 'USRDS_class_Neoplasms/tumors', 'USRDS_class_Secondary glomerulonephritis/vasculitis', 'fspktv4_(1.39,1.56]', 'fspktv4_(1.56,1.73]', 'fspktv4_(1.73,3.63]', 'fspktv4_[0.784,1.39]'] return cov_names def HemoData_preparation(X): cov_names = import_hemo_covnames() X = pd.DataFrame(X) X.columns = cov_names cov_to_eliminate = ['UFR_mLkgh', 'mean_uf', 'mean_idwg_day', 'mean_postSBP', 'mean_lowestSBP', 'TBWchild', 'TBWadult', 'spktv_watson_W067', 'spktv_watson_W075', 'spktv_watson_BSA', 'spktv_cheek_BSA', 'spktv_cheek_W075', 'tidwg2', 'tuf_percent', 'fspktv4_(1.39,1.56]', 'fspktv4_(1.56,1.73]', 'fspktv4_(1.73,3.63]', 'fspktv4_[0.784,1.39]'] X = X.drop(cov_to_eliminate, axis=1) cov_names = X.columns.values return X.values, cov_names ########## PLOTTING UTILS ############################################ def prepare_summary_plot_data(global_shaps, top_n, prototypes_ds_original, cluster_labels, feature_names): most_rel_shaps_ds = global_shaps.nlargest(top_n) # We extract the id of the most relevant features to retrieve the columns from the raw input data. # This passage is needed to plot the original features distribution in the two clusters of prototypes. id_most_rel = most_rel_shaps_ds.index Proto_mostRel_f_ds = prototypes_ds_original.iloc[:,id_most_rel] Plot_df = pd.concat([Proto_mostRel_f_ds, pd.DataFrame(cluster_labels, columns=["c"])], axis=1) top_feature_names = feature_names[id_most_rel] shap_bar_values = most_rel_shaps_ds.tolist() return top_feature_names, shap_bar_values, Plot_df def plot_topN_features(Plot_df, top_n, top_feature_names, shap_bar_values, unit_measures): CB_COLOR_CYCLE = ['#377eb8', '#ff7f00', '#4daf4a', '#f781bf', '#a65628', '#984ea3', '#999999', '#e41a1c', '#dede00'] number_gp = top_n def ax_settings(ax, var_name, unit_measure): ax.set_yticks([]) ax.spines['left'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.spines['bottom'].set_edgecolor('#444444') ax.spines['bottom'].set_linewidth(2) ax.set_xlabel(unit_measure, fontsize=16) ax.tick_params(axis='x', labelsize=14) #ax.set_xticklabels(ax.get_xticklabels(), fontsize=4) ax.text(-0.2, 0.1, var_name, fontsize=17, transform = ax.transAxes) return None # Manipulate each axes object in the left. fig = plt.figure(figsize=(18,21)) gs = matplotlib.gridspec.GridSpec(nrows=number_gp, ncols=2, figure=fig, width_ratios= [3, 1], height_ratios= [1]*number_gp, wspace=0.05, hspace=0.6 ) ax = [None]*(number_gp) # Create a figure, partition the figure into boxes, set up an ax array to store axes objects, and create a list of features. for i in range(number_gp): ax[i] = fig.add_subplot(gs[i, 0]) ax_settings(ax[i], str(top_feature_names[i]), str(unit_measures[i])) sns.histplot(data=Plot_df[(Plot_df['c'] == 0)].iloc[:,i], ax=ax[i], stat = 'density', color=CB_COLOR_CYCLE[1], legend=False, alpha=0.6, linewidth=0.1) sns.histplot(data=Plot_df[(Plot_df['c'] == 1)].iloc[:,i], ax=ax[i], stat = 'density', color=CB_COLOR_CYCLE[0], legend=False, alpha=0.6, linewidth=0.1) #if i < (number_gp - 1): # ax[i].set_xticks([]) if i == (number_gp-1): ax[i].text(0.2, -1, 'Covariates Distribution across Clusters', fontsize=18, transform = ax[i].transAxes) ax[0].legend(['Cluster 1', 'Cluster 2'], facecolor='w', loc='upper left', fontsize=15) for i in range(number_gp): ax[i] = fig.add_subplot(gs[i, 1]) ax[i].spines['right'].set_visible(False) ax[i].spines['top'].set_visible(False) ax[i].barh(0, shap_bar_values[i], color=CB_COLOR_CYCLE[-3], height=0.8, align = 'center') ax[i].set_xlim(0 , 0.015) ax[i].set_yticks([]) ax[i].set_ylim(-1,1) if i < (number_gp - 1): ax[i].set_xticks([]) ax[i].spines['bottom'].set_visible(False) if i == (number_gp-1): ax[i].spines['bottom'].set_visible(True) ax[i].tick_params(axis='x', labelrotation= 45, labelsize=13) ax[i].text(-0.01, -1, 'Mean(|Shapley Value|)', fontsize=18, transform = ax[i].transAxes) return fig
7,993
32.033058
158
py
sdmgrad
sdmgrad-main/toy/toy.py
from copy import deepcopy from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm, ticker from matplotlib.colors import LogNorm from tqdm import tqdm from scipy.optimize import minimize, Bounds, minimize_scalar import matplotlib.pyplot as plt import numpy as np import time import torch import torch.nn as nn from torch.optim.lr_scheduler import ExponentialLR import seaborn as sns import sys ################################################################################ # # Define the Optimization Problem # ################################################################################ LOWER = 0.000005 class Toy(nn.Module): def __init__(self): super(Toy, self).__init__() self.centers = torch.Tensor([[-3.0, 0], [3.0, 0]]) def forward(self, x, compute_grad=False): x1 = x[0] x2 = x[1] f1 = torch.clamp((0.5 * (-x1 - 7) - torch.tanh(-x2)).abs(), LOWER).log() + 6 f2 = torch.clamp((0.5 * (-x1 + 3) + torch.tanh(-x2) + 2).abs(), LOWER).log() + 6 c1 = torch.clamp(torch.tanh(x2 * 0.5), 0) f1_sq = ((-x1 + 7).pow(2) + 0.1 * (-x2 - 8).pow(2)) / 10 - 20 f2_sq = ((-x1 - 7).pow(2) + 0.1 * (-x2 - 8).pow(2)) / 10 - 20 c2 = torch.clamp(torch.tanh(-x2 * 0.5), 0) f1 = f1 * c1 + f1_sq * c2 f2 = f2 * c1 + f2_sq * c2 f = torch.tensor([f1, f2]) if compute_grad: g11 = torch.autograd.grad(f1, x1, retain_graph=True)[0].item() g12 = torch.autograd.grad(f1, x2, retain_graph=True)[0].item() g21 = torch.autograd.grad(f2, x1, retain_graph=True)[0].item() g22 = torch.autograd.grad(f2, x2, retain_graph=True)[0].item() g = torch.Tensor([[g11, g21], [g12, g22]]) return f, g else: return f def batch_forward(self, x): x1 = x[:, 0] x2 = x[:, 1] f1 = torch.clamp((0.5 * (-x1 - 7) - torch.tanh(-x2)).abs(), LOWER).log() + 6 f2 = torch.clamp((0.5 * (-x1 + 3) + torch.tanh(-x2) + 2).abs(), LOWER).log() + 6 c1 = torch.clamp(torch.tanh(x2 * 0.5), 0) f1_sq = ((-x1 + 7).pow(2) + 0.1 * (-x2 - 8).pow(2)) / 10 - 20 f2_sq = ((-x1 - 7).pow(2) + 0.1 * (-x2 - 8).pow(2)) / 10 - 20 c2 = torch.clamp(torch.tanh(-x2 * 0.5), 0) f1 = f1 * c1 + f1_sq * c2 f2 = f2 * c1 + f2_sq * c2 f = torch.cat([f1.view(-1, 1), f2.view(-1, 1)], -1) return f ################################################################################ # # Plot Utils # ################################################################################ def plotme(F, all_traj=None, xl=11): n = 500 x = np.linspace(-xl, xl, n) y = np.linspace(-xl, xl, n) X, Y = np.meshgrid(x, y) Xs = torch.Tensor(np.transpose(np.array([list(X.flat), list(Y.flat)]))).double() Ys = F.batch_forward(Xs) colormaps = { "sgd": "tab:blue", "pcgrad": "tab:orange", "mgd": "tab:cyan", "cagrad": "tab:red", "sdmgrad": "tab:green" } plt.figure(figsize=(12, 5)) plt.subplot(131) c = plt.contour(X, Y, Ys[:, 0].view(n, n)) if all_traj is not None: for i, (k, v) in enumerate(all_traj.items()): plt.plot(all_traj[k][:, 0], all_traj[k][:, 1], '--', c=colormaps[k], label=k) plt.title("L1(x)") plt.subplot(132) c = plt.contour(X, Y, Ys[:, 1].view(n, n)) if all_traj is not None: for i, (k, v) in enumerate(all_traj.items()): plt.plot(all_traj[k][:, 0], all_traj[k][:, 1], '--', c=colormaps[k], label=k) plt.title("L2(x)") plt.subplot(133) c = plt.contour(X, Y, Ys.mean(1).view(n, n)) if all_traj is not None: for i, (k, v) in enumerate(all_traj.items()): plt.plot(all_traj[k][:, 0], all_traj[k][:, 1], '--', c=colormaps[k], label=k) plt.legend() plt.title("0.5*(L1(x)+L2(x))") plt.tight_layout() plt.savefig(f"toy_ct.png") def plot3d(F, xl=11): n = 500 x = np.linspace(-xl, xl, n) y = np.linspace(-xl, xl, n) X, Y = np.meshgrid(x, y) Xs = torch.Tensor(np.transpose(np.array([list(X.flat), list(Y.flat)]))).double() Ys = F.batch_forward(Xs) fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) ax.grid(False) Yv = Ys.mean(1).view(n, n) surf = ax.plot_surface(X, Y, Yv.numpy(), cmap=cm.viridis) print(Ys.mean(1).min(), Ys.mean(1).max()) ax.set_zticks([-16, -8, 0, 8]) ax.set_zlim(-20, 10) ax.set_xticks([-10, 0, 10]) ax.set_yticks([-10, 0, 10]) for tick in ax.xaxis.get_major_ticks(): tick.label.set_fontsize(15) for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(15) for tick in ax.zaxis.get_major_ticks(): tick.label.set_fontsize(15) ax.view_init(25) plt.tight_layout() plt.savefig(f"3d-obj.png", dpi=1000) def plot_contour(F, task=1, traj=None, xl=11, plotbar=False, name="tmp"): n = 500 x = np.linspace(-xl, xl, n) y = np.linspace(-xl, xl, n) X, Y = np.meshgrid(x, y) fig = plt.figure() ax = fig.add_subplot(111) Xs = torch.Tensor(np.transpose(np.array([list(X.flat), list(Y.flat)]))).double() Ys = F.batch_forward(Xs) cmap = cm.get_cmap('viridis') yy = -8.3552 if task == 0: Yv = Ys.mean(1) plt.plot(-8.5, 7.5, marker='o', markersize=10, zorder=5, color='k') plt.plot(-8.5, -5, marker='o', markersize=10, zorder=5, color='k') plt.plot(9, 9, marker='o', markersize=10, zorder=5, color='k') plt.plot([-7, 7], [yy, yy], linewidth=8.0, zorder=0, color='gray') plt.plot(0, yy, marker='*', markersize=15, zorder=5, color='k') elif task == 1: Yv = Ys[:, 0] plt.plot(7, yy, marker='*', markersize=15, zorder=5, color='k') else: Yv = Ys[:, 1] plt.plot(-7, yy, marker='*', markersize=15, zorder=5, color='k') c = plt.contour(X, Y, Yv.view(n, n), cmap=cm.viridis, linewidths=4.0) if traj is not None: for tt in traj: l = tt.shape[0] color_list = np.zeros((l, 3)) color_list[:, 0] = 1. color_list[:, 1] = np.linspace(0, 1, l) #color_list[:,2] = 1-np.linspace(0, 1, l) ax.scatter(tt[:, 0], tt[:, 1], color=color_list, s=6, zorder=10) if plotbar: cbar = fig.colorbar(c, ticks=[-15, -10, -5, 0, 5]) cbar.ax.tick_params(labelsize=15) ax.set_aspect(1.0 / ax.get_data_ratio(), adjustable='box') plt.xticks([-10, -5, 0, 5, 10], fontsize=15) plt.yticks([-10, -5, 0, 5, 10], fontsize=15) plt.tight_layout() plt.savefig(f"{name}.png", dpi=100) plt.close() def smooth(x, n=20): l = len(x) y = [] for i in range(l): ii = max(0, i - n) jj = min(i + n, l - 1) v = np.array(x[ii:jj]).astype(np.float64) if i < 3: y.append(x[i]) else: y.append(v.mean()) return y def plot_loss(trajs, name="tmp"): fig = plt.figure() ax = fig.add_subplot(111) colormaps = { "sgd": "tab:blue", "pcgrad": "tab:orange", "mgd": "tab:purple", "cagrad": "tab:red", "sdmgrad": "tab:cyan" } maps = {"sgd": "Adam", "pcgrad": "PCGrad", "mgd": "MGDA", "cagrad": "CAGrad", "sdmgrad": "SDMGrad (Ours)"} for method in ["sgd", "mgd", "pcgrad", "cagrad", "sdmgrad"]: traj = trajs[method][::100] Ys = F.batch_forward(traj) x = np.arange(traj.shape[0]) #y = torch.cummin(Ys.mean(1), 0)[0] y = Ys.mean(1) ax.plot(x, smooth(list(y)), color=colormaps[method], linestyle='-', label=maps[method], linewidth=4.) plt.xticks([0, 200, 400, 600, 800, 1000], ["0", "20K", "40K", "60K", "80K", "100K"], fontsize=15) plt.yticks(fontsize=15) ax.grid() plt.legend(fontsize=15) ax.set_aspect(1.0 / ax.get_data_ratio(), adjustable='box') plt.tight_layout() plt.savefig(f"{name}.png", dpi=100) plt.close() ################################################################################ # # Multi-Objective Optimization Solver # ################################################################################ def mean_grad(grads): return grads.mean(1) def pcgrad(grads): g1 = grads[:, 0] g2 = grads[:, 1] g11 = g1.dot(g1).item() g12 = g1.dot(g2).item() g22 = g2.dot(g2).item() if g12 < 0: return ((1 - g12 / g11) * g1 + (1 - g12 / g22) * g2) / 2 else: return (g1 + g2) / 2 def mgd(grads): g1 = grads[:, 0] g2 = grads[:, 1] g11 = g1.dot(g1).item() g12 = g1.dot(g2).item() g22 = g2.dot(g2).item() if g12 < min(g11, g22): x = (g22 - g12) / (g11 + g22 - 2 * g12 + 1e-8) elif g11 < g22: x = 1 else: x = 0 g_mgd = x * g1 + (1 - x) * g2 # mgd gradient g_mgd return g_mgd def cagrad(grads, c=0.5): g1 = grads[:, 0] g2 = grads[:, 1] g0 = (g1 + g2) / 2 g11 = g1.dot(g1).item() g12 = g1.dot(g2).item() g22 = g2.dot(g2).item() g0_norm = 0.5 * np.sqrt(g11 + g22 + 2 * g12 + 1e-4) # want to minimize g_w^Tg_0 + c*||g_0||*||g_w|| coef = c * g0_norm def obj(x): # g_w^T g_0: x*0.5*(g11+g22-2g12)+(0.5+x)*(g12-g22)+g22 # g_w^T g_w: x^2*(g11+g22-2g12)+2*x*(g12-g22)+g22 return coef * np.sqrt(x**2*(g11+g22-2*g12)+2*x*(g12-g22)+g22+1e-4) + \ 0.5*x*(g11+g22-2*g12)+(0.5+x)*(g12-g22)+g22 res = minimize_scalar(obj, bounds=(0, 1), method='bounded') x = res.x gw = x * g1 + (1 - x) * g2 gw_norm = np.sqrt(x**2 * g11 + (1 - x)**2 * g22 + 2 * x * (1 - x) * g12 + 1e-4) lmbda = coef / (gw_norm + 1e-4) g = g0 + lmbda * gw return g / (1 + c) ### Our SDMGrad ### def sdmgrad(grads, lmbda): g1 = grads[:, 0] g2 = grads[:, 1] g0 = (g1 + g2) / 2 g11 = g1.dot(g1).item() g12 = g1.dot(g2).item() g22 = g2.dot(g2).item() def obj(x): # g_w^T g_0: x*0.5*(g11+g22-2g12)+(0.5+x)*(g12-g22)+g22 # g_w^T g_w: x^2*(g11+g22-2g12)+2*x*(g12-g22)+g22 return (x**2*(g11+g22-2*g12)+2*x*(g12-g22)+g22+1e-4) + \ 2 * lmbda * (0.5*x*(g11+g22-2*g12)+(0.5+x)*(g12-g22)+g22) + \ lmbda**2 * 0.25 * (g11+g22+2*g12+1e-4) res = minimize_scalar(obj, bounds=(0, 1), method='bounded') x = res.x gw = x * g1 + (1 - x) * g2 g = lmbda * g0 + gw return g / (1 + lmbda) ### Add noise ### def add_noise(grads, coef=0.2): grads_ = grads + coef * torch.randn_like(grads) return grads_ ### Define the problem ### F = Toy() maps = {"sgd": mean_grad, "cagrad": cagrad, "mgd": mgd, "pcgrad": pcgrad, "sdmgrad": sdmgrad} ### Start experiments ### def run_all(): all_traj = {} # the initial positions inits = [ torch.Tensor([-8.5, 7.5]), torch.Tensor([-8.5, -5.]), torch.Tensor([9., 9.]), ] for i, init in enumerate(inits): for m in tqdm(["sgd", "mgd", "pcgrad", "cagrad", "sdmgrad"]): all_traj[m] = None traj = [] solver = maps[m] x = init.clone() x.requires_grad = True n_iter = 70000 opt = torch.optim.Adam([x], lr=0.002) # scheduler = ExponentialLR(opt, gamma = 0.9999) for it in range(n_iter): traj.append(x.detach().numpy().copy()) # if it % 1000 == 0: # print(f'\niteration {it}, before update x: ', x.detach().numpy().copy()) f, grads = F(x, True) grads = add_noise(grads, coef=0.2) # grads = add_element_noise(grads, coef=1.0, it=it) if m == "cagrad": g = solver(grads, c=0.5) elif m == "sdmgrad": g = solver(grads, lmbda=0.01) else: g = solver(grads) opt.zero_grad() x.grad = g opt.step() # scheduler.step() all_traj[m] = torch.tensor(np.array(traj)) torch.save(all_traj, f"toy{i}.pt") plot_loss(all_traj) plot_results() def plot_results(): plot3d(F) plot_contour(F, 1, name="toy_task_1") plot_contour(F, 2, name="toy_task_2") t1 = torch.load(f"toy0.pt") t2 = torch.load(f"toy1.pt") t3 = torch.load(f"toy2.pt") length = t1["sdmgrad"].shape[0] for method in ["sgd", "mgd", "pcgrad", "cagrad", "sdmgrad"]: ranges = list(range(10, length, 1000)) ranges.append(length - 1) for t in tqdm(ranges): plot_contour( F, task=0, # task == 0 meeas plot for both tasks traj=[t1[method][:t], t2[method][:t], t3[method][:t]], plotbar=(method == "sdmgrad"), name=f"./imgs/toy_{method}_{t}") if __name__ == "__main__": run_all()
13,100
28.308725
110
py
sdmgrad
sdmgrad-main/mtrl/mtrl_files/sdmgrad.py
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved from copy import deepcopy from typing import Iterable, List, Optional, Tuple import numpy as np import time import torch from omegaconf import OmegaConf from mtrl.agent import grad_manipulation as grad_manipulation_agent from mtrl.utils.types import ConfigType, TensorType #from mtrl.agent.mgda import MinNormSolver def euclidean_proj_simplex(v, s=1): """ Compute the Euclidean projection on a positive simplex Solves the optimisation problem (using the algorithm from [1]): min_w 0.5 * || w - v ||_2^2 , s.t. \sum_i w_i = s, w_i >= 0 Parameters ---------- v: (n,) numpy array, n-dimensional vector to project s: int, optional, default: 1, radius of the simplex Returns ------- w: (n,) numpy array, Euclidean projection of v on the simplex Notes ----- The complexity of this algorithm is in O(n log(n)) as it involves sorting v. Better alternatives exist for high-dimensional sparse vectors (cf. [1]) However, this implementation still easily scales to millions of dimensions. References ---------- [1] Efficient Projections onto the .1-Ball for Learning in High Dimensions John Duchi, Shai Shalev-Shwartz, Yoram Singer, and Tushar Chandra. International Conference on Machine Learning (ICML 2008) http://www.cs.berkeley.edu/~jduchi/projects/DuchiSiShCh08.pdf [2] Projection onto the probability simplex: An efficient algorithm with a simple proof, and an application Weiran Wang, Miguel Á. Carreira-Perpiñán. arXiv:1309.1541 https://arxiv.org/pdf/1309.1541.pdf [3] https://gist.github.com/daien/1272551/edd95a6154106f8e28209a1c7964623ef8397246#file-simplex_projection-py """ assert s > 0, "Radius s must be strictly positive (%d <= 0)" % s v = v.astype(np.float64) n, = v.shape # will raise ValueError if v is not 1-D # check if we are already on the simplex if v.sum() == s and np.alltrue(v >= 0): # best projection: itself! return v # get the array of cumulative sums of a sorted (decreasing) copy of v u = np.sort(v)[::-1] cssv = np.cumsum(u) # get the number of > 0 components of the optimal solution rho = np.nonzero(u * np.arange(1, n + 1) > (cssv - s))[0][-1] # compute the Lagrange multiplier associated to the simplex constraint theta = float(cssv[rho] - s) / (rho + 1) # compute the projection by thresholding v using theta w = (v - theta).clip(min=0) return w def _check_param_device(param: TensorType, old_param_device: Optional[int]) -> int: """This helper function is to check if the parameters are located in the same device. Currently, the conversion between model parameters and single vector form is not supported for multiple allocations, e.g. parameters in different GPUs, or mixture of CPU/GPU. The implementation is taken from: https://github.com/pytorch/pytorch/blob/22a34bcf4e5eaa348f0117c414c3dd760ec64b13/torch/nn/utils/convert_parameters.py#L57 Args: param ([TensorType]): a Tensor of a parameter of a model. old_param_device ([int]): the device where the first parameter of a model is allocated. Returns: old_param_device (int): report device for the first time """ # Meet the first parameter if old_param_device is None: old_param_device = param.get_device() if param.is_cuda else -1 else: warn = False if param.is_cuda: # Check if in same GPU warn = param.get_device() != old_param_device else: # Check if in CPU warn = old_param_device != -1 if warn: raise TypeError("Found two parameters on different devices, " "this is currently not supported.") return old_param_device def apply_vector_grad_to_parameters(vec: TensorType, parameters: Iterable[TensorType], accumulate: bool = False): """Apply vector gradients to the parameters Args: vec (TensorType): a single vector represents the gradients of a model. parameters (Iterable[TensorType]): an iterator of Tensors that are the parameters of a model. """ # Ensure vec of type Tensor if not isinstance(vec, torch.Tensor): raise TypeError("expected torch.Tensor, but got: {}".format(torch.typename(vec))) # Flag for the device where the parameter is located param_device = None # Pointer for slicing the vector for each parameter pointer = 0 for param in parameters: # Ensure the parameters are located in the same device param_device = _check_param_device(param, param_device) # The length of the parameter num_param = param.numel() # Slice the vector, reshape it, and replace the old grad of the parameter if accumulate: param.grad = (param.grad + vec[pointer:pointer + num_param].view_as(param).data) else: param.grad = vec[pointer:pointer + num_param].view_as(param).data # Increment the pointer pointer += num_param class Agent(grad_manipulation_agent.Agent): def __init__( self, env_obs_shape: List[int], action_shape: List[int], action_range: Tuple[int, int], device: torch.device, agent_cfg: ConfigType, multitask_cfg: ConfigType, cfg_to_load_model: Optional[ConfigType] = None, should_complete_init: bool = True, ): """Regularized gradient algorithm.""" agent_cfg_copy = deepcopy(agent_cfg) del agent_cfg_copy['sdmgrad_lmbda'] del agent_cfg_copy['sdmgrad_method'] OmegaConf.set_struct(agent_cfg_copy, False) agent_cfg_copy.cfg_to_load_model = None agent_cfg_copy.should_complete_init = False agent_cfg_copy.loss_reduction = "none" OmegaConf.set_struct(agent_cfg_copy, True) super().__init__( env_obs_shape=env_obs_shape, action_shape=action_shape, action_range=action_range, multitask_cfg=multitask_cfg, agent_cfg=agent_cfg_copy, device=device, ) self.agent._compute_gradient = self._compute_gradient self._rng = np.random.default_rng() self.sdmgrad_lmbda = agent_cfg['sdmgrad_lmbda'] self.sdmgrad_method = agent_cfg['sdmgrad_method'] fn_maps = { "sdmgrad": self.sdmgrad, } for k in range(2, 50): fn_maps[f"sdmgrad_os{k}"] = self.sdmgrad_os fn_names = ", ".join(fn_maps.keys()) assert self.sdmgrad_method in fn_maps, \ f"[error] unrealized fn {self.sdmgrad_method}, currently we have {fn_names}" self.sdmgrad_fn = fn_maps[self.sdmgrad_method] self.wi_map = {} self.num_param_block = -1 self.conflicts = [] self.last_w = None self.save_target = 500000 if "os" in self.sdmgrad_method: num_tasks = multitask_cfg['num_envs'] self.os_n = int(self.sdmgrad_method[self.sdmgrad_method.find("os") + 2:]) if should_complete_init: self.complete_init(cfg_to_load_model=cfg_to_load_model) def _compute_gradient( self, loss: TensorType, # batch x 1 parameters: List[TensorType], step: int, component_names: List[str], env_metadata: grad_manipulation_agent.EnvMetadata, retain_graph: bool = False, allow_unused: bool = False, ) -> None: #t0 = time.time() task_loss = self._convert_loss_into_task_loss(loss=loss, env_metadata=env_metadata) num_tasks = task_loss.shape[0] grad = [] if "os" in self.sdmgrad_method: n = self.os_n while True: idx = np.random.binomial(1, n / num_tasks, num_tasks) sample_idx = np.where(idx == 1)[0] n_sample = sample_idx.shape[0] if n_sample: break losses = [0] * n_sample for j in range(n_sample): losses[j] = task_loss[sample_idx[j]] for loss in losses: grad.append( tuple(_grad.contiguous() for _grad in torch.autograd.grad( loss, parameters, retain_graph=True, allow_unused=allow_unused, ))) else: for index in range(num_tasks): grad.append( tuple(_grad.contiguous() for _grad in torch.autograd.grad( task_loss[index], parameters, retain_graph=(retain_graph or index != num_tasks - 1), allow_unused=allow_unused, ))) grad_vec = torch.cat( list(map(lambda x: torch.nn.utils.parameters_to_vector(x).unsqueeze(0), grad)), dim=0, ) # num_tasks x dim regularized_grad = self.sdmgrad_fn(grad_vec, num_tasks) apply_vector_grad_to_parameters(regularized_grad, parameters) def sdmgrad(self, grad_vec, num_tasks): """ grad_vec: [num_tasks, dim] """ grads = grad_vec GG = torch.mm(grads, grads.t()).cpu() scale = torch.mean(torch.sqrt(torch.diag(GG) + 1e-4)) GG = GG / scale.pow(2) Gg = torch.mean(GG, dim=1) gg = torch.mean(Gg) w = torch.ones(num_tasks) / num_tasks w.requires_grad = True if num_tasks == 50: w_opt = torch.optim.SGD([w], lr=50, momentum=0.5) else: w_opt = torch.optim.SGD([w], lr=25, momentum=0.5) lmbda = self.sdmgrad_lmbda w_best = None obj_best = np.inf for i in range(21): w_opt.zero_grad() obj = torch.dot(w, torch.mv(GG, w)) + 2 * lmbda * torch.dot(w, Gg) + lmbda**2 * gg if obj.item() < obj_best: obj_best = obj.item() w_best = w.clone() if i < 20: obj.backward() w_opt.step() proj = euclidean_proj_simplex(w.data.cpu().numpy()) w.data.copy_(torch.from_numpy(proj).data) g0 = torch.mean(grads, dim=0) gw = torch.mv(grads.t(), w_best.to(grads.device)) g = (gw + lmbda * g0) / (1 + lmbda) return g def sdmgrad_os(self, grad_vec, num_tasks): """ objective sampling grad_vec: [num_tasks, dim] """ grads = grad_vec n = grads.size(0) GG = torch.mm(grads, grads.t()).cpu() scale = (torch.diag(GG) + 1e-4).sqrt().mean() GG = GG / scale.pow(2) Gg = torch.mean(GG, dim=1) gg = torch.mean(Gg) w = torch.ones(n) / n w.requires_grad = True w_opt = torch.optim.SGD([w], lr=50, momentum=0.5) lmbda = self.sdmgrad_lmbda w_best = None obj_best = np.inf for i in range(21): w_opt.zero_grad() obj = torch.dot(w, torch.mv(GG, w)) + 2 * lmbda * torch.dot(w, Gg) + lmbda**2 * gg if obj.item() < obj_best: obj_best = obj.item() w_best = w.clone() if i < 20: obj.backward() w_opt.step() proj = euclidean_proj_simplex(w.data.cpu().numpy()) w.data.copy_(torch.from_numpy(proj).data) g0 = torch.mean(grads, dim=0) gw = torch.mv(grads.t(), w_best.to(grads.device)) g = (gw + lmbda * g0) / (1 + lmbda) return g
11,791
35.965517
163
py
sdmgrad
sdmgrad-main/mtrl/mtrl_files/config.py
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved """Code to interface with the config.""" import datetime import hashlib import os from copy import deepcopy from typing import Any, Dict, cast import hydra from omegaconf import OmegaConf from mtrl.utils import utils from mtrl.utils.types import ConfigType def dict_to_config(dictionary: Dict) -> ConfigType: """Convert the dictionary to a config. Args: dictionary (Dict): dictionary to convert. Returns: ConfigType: config made from the dictionary. """ return OmegaConf.create(dictionary) def make_config_mutable(config: ConfigType) -> ConfigType: """Set the config to be mutable. Args: config (ConfigType): Returns: ConfigType: """ OmegaConf.set_readonly(config, False) return config def make_config_immutable(config: ConfigType) -> ConfigType: """Set the config to be immutable. Args: config (ConfigType): Returns: ConfigType: """ OmegaConf.set_readonly(config, True) return config def set_struct(config: ConfigType) -> ConfigType: """Set the struct flag in the config. Args: config (ConfigType): Returns: ConfigType: """ OmegaConf.set_struct(config, True) return config def unset_struct(config: ConfigType) -> ConfigType: """Unset the struct flag in the config. Args: config (ConfigType): Returns: ConfigType: """ OmegaConf.set_struct(config, False) return config def to_dict(config: ConfigType) -> Dict[str, Any]: """Convert config to a dictionary. Args: config (ConfigType): Returns: Dict: """ dict_config = cast(Dict[str, Any], OmegaConf.to_container(deepcopy(config), resolve=False)) return dict_config def process_config(config: ConfigType, should_make_dir: bool = True) -> ConfigType: """Process the config. Args: config (ConfigType): config object to process. should_make_dir (bool, optional): should make dir for saving logs, models etc? Defaults to True. Returns: ConfigType: processed config. """ config = _process_setup_config(config=config) config = _process_experiment_config(config=config, should_make_dir=should_make_dir) return set_struct(make_config_immutable(config)) def read_config_from_file(config_path: str) -> ConfigType: """Read the config from filesystem. Args: config_path (str): path to read config from. Returns: ConfigType: """ config = OmegaConf.load(config_path) assert isinstance(config, ConfigType) return set_struct(make_config_immutable(config)) def _process_setup_config(config: ConfigType) -> ConfigType: """Process the `setup` node of the config. Args: config (ConfigType): config object. Returns: [ConfigType]: processed config. """ setup_config = config.setup if setup_config.base_path is None: setup_config.base_path = hydra.utils.get_original_cwd() if not setup_config.debug.should_enable: #setup_config.id = f"{hashlib.sha224(setup_config.description.encode()).hexdigest()}_issue_{setup_config.git.issue_id}_seed_{setup_config.seed}" if "sdmgrad" in config.agent.name: setup_config.id = f"{config.env.name}_{config.agent.name}_"+\ f"{config.agent.builder.agent_cfg.sdmgrad_method}_"+\ f"c{config.agent.builder.agent_cfg.sdmgrad_lmbda}_seed_{setup_config.seed}" else: setup_config.id = f"{config.env.name}_{config.agent.name}_seed_{setup_config.seed}" current_commit_id = utils.get_current_commit_id() if not setup_config.git.commit_id: setup_config.git.commit_id = current_commit_id else: # if the commit id is already set, assert that the commit id (in the # config) is the same as the current commit id. if setup_config.git.commit_id != current_commit_id: raise RuntimeError(f"""The current commit id ({current_commit_id}) does not match the commit id from the config ({setup_config.git.commit_id})""") if setup_config.git.has_uncommitted_changes == "": setup_config.git.has_uncommitted_changes = utils.has_uncommitted_changes() if not setup_config.date: setup_config.date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") slurm_id = [] env_var_names = ["SLURM_JOB_ID", "SLURM_STEP_ID"] for var_name in env_var_names: if var_name in os.environ: slurm_id.append(str(os.environ[var_name])) if slurm_id: setup_config.slurm_id = "-".join(slurm_id) else: setup_config.slurm_id = "-1" return config def _process_experiment_config(config: ConfigType, should_make_dir: bool) -> ConfigType: """Process the `experiment` section of the config. Args: config (ConfigType): config object. should_make_dir (bool): should make dir. Returns: ConfigType: Processed config """ if should_make_dir: utils.make_dir(path=config.experiment.save_dir) return config def pretty_print(config, resolve: bool = True): """Prettyprint the config. Args: config ([type]): resolve (bool, optional): should resolve the config before printing. Defaults to True. """ print(OmegaConf.to_yaml(config, resolve=resolve)) def get_env_params_from_config(config: ConfigType) -> ConfigType: """Get the params needed for building the environment from a config. Args: config (ConfigType): Returns: ConfigType: params for building the environment, encoded as a config. """ env_params = deepcopy(config.env.builder) env_params = make_config_mutable(env_params) env_params = unset_struct(env_params) env_params.pop("_target_") return env_params
5,961
26.99061
152
py
sdmgrad
sdmgrad-main/nyuv2/model_segnet_single.py
import torch.nn as nn import torch.optim as optim import torch.nn.functional as F import argparse from create_dataset import * from utils import * parser = argparse.ArgumentParser(description='Single-task: One Task') parser.add_argument('--task', default='semantic', type=str, help='choose task: semantic, depth, normal') parser.add_argument('--dataroot', default='nyuv2', type=str, help='dataset root') parser.add_argument('--seed', default=0, type=int, help='the seed') parser.add_argument('--apply_augmentation', action='store_true', help='toggle to apply data augmentation on NYUv2') opt = parser.parse_args() class SegNet(nn.Module): def __init__(self): super(SegNet, self).__init__() # initialise network parameters filter = [64, 128, 256, 512, 512] self.class_nb = 13 # define encoder decoder layers self.encoder_block = nn.ModuleList([self.conv_layer([3, filter[0]])]) self.decoder_block = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): self.encoder_block.append(self.conv_layer([filter[i], filter[i + 1]])) self.decoder_block.append(self.conv_layer([filter[i + 1], filter[i]])) # define convolution layer self.conv_block_enc = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) self.conv_block_dec = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): if i == 0: self.conv_block_enc.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.conv_block_dec.append(self.conv_layer([filter[i], filter[i]])) else: self.conv_block_enc.append( nn.Sequential(self.conv_layer([filter[i + 1], filter[i + 1]]), self.conv_layer([filter[i + 1], filter[i + 1]]))) self.conv_block_dec.append( nn.Sequential(self.conv_layer([filter[i], filter[i]]), self.conv_layer([filter[i], filter[i]]))) if opt.task == 'semantic': self.pred_task = self.conv_layer([filter[0], self.class_nb], pred=True) if opt.task == 'depth': self.pred_task = self.conv_layer([filter[0], 1], pred=True) if opt.task == 'normal': self.pred_task = self.conv_layer([filter[0], 3], pred=True) # define pooling and unpooling functions self.down_sampling = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True) self.up_sampling = nn.MaxUnpool2d(kernel_size=2, stride=2) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) def conv_layer(self, channel, pred=False): if not pred: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(num_features=channel[1]), nn.ReLU(inplace=True), ) else: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), ) return conv_block def forward(self, x): g_encoder, g_decoder, g_maxpool, g_upsampl, indices = ([0] * 5 for _ in range(5)) for i in range(5): g_encoder[i], g_decoder[-i - 1] = ([0] * 2 for _ in range(2)) # define global shared network for i in range(5): if i == 0: g_encoder[i][0] = self.encoder_block[i](x) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) else: g_encoder[i][0] = self.encoder_block[i](g_maxpool[i - 1]) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) for i in range(5): if i == 0: g_upsampl[i] = self.up_sampling(g_maxpool[-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) else: g_upsampl[i] = self.up_sampling(g_decoder[i - 1][-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) # define task prediction layers if opt.task == 'semantic': pred = F.log_softmax(self.pred_task(g_decoder[-1][-1]), dim=1) if opt.task == 'depth': pred = self.pred_task(g_decoder[-1][-1]) if opt.task == 'normal': pred = self.pred_task(g_decoder[-1][-1]) pred = pred / torch.norm(pred, p=2, dim=1, keepdim=True) return pred # control seed torch.backends.cudnn.enabled = False torch.manual_seed(opt.seed) np.random.seed(opt.seed) random.seed(opt.seed) torch.cuda.manual_seed_all(opt.seed) # define model, optimiser and scheduler device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") SegNet = SegNet().to(device) optimizer = optim.Adam(SegNet.parameters(), lr=1e-4) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.5) print('Parameter Space: ABS: {:.1f}, REL: {:.4f}'.format(count_parameters(SegNet), count_parameters(SegNet) / 24981069)) print( 'LOSS FORMAT: SEMANTIC_LOSS MEAN_IOU PIX_ACC | DEPTH_LOSS ABS_ERR REL_ERR | NORMAL_LOSS MEAN MED <11.25 <22.5 <30') # define dataset dataset_path = opt.dataroot if opt.apply_augmentation: nyuv2_train_set = NYUv2(root=dataset_path, train=True, augmentation=True) print('Applying data augmentation on NYUv2.') else: nyuv2_train_set = NYUv2(root=dataset_path, train=True) print('Standard training strategy without data augmentation.') nyuv2_test_set = NYUv2(root=dataset_path, train=False) batch_size = 2 nyuv2_train_loader = torch.utils.data.DataLoader(dataset=nyuv2_train_set, batch_size=batch_size, shuffle=True) nyuv2_test_loader = torch.utils.data.DataLoader(dataset=nyuv2_test_set, batch_size=batch_size, shuffle=False) # Train and evaluate single-task network single_task_trainer(nyuv2_train_loader, nyuv2_test_loader, SegNet, device, optimizer, scheduler, opt, 200)
6,820
43.292208
120
py
sdmgrad
sdmgrad-main/nyuv2/evaluate.py
import matplotlib import matplotlib.pyplot as plt import seaborn as sns import numpy as np import torch import itertools methods = [ "sdmgrad-1e-1", "sdmgrad-2e-1", "sdmgrad-3e-1", "sdmgrad-4e-1", "sdmgrad-5e-1", "sdmgrad-6e-1", "sdmgrad-7e-1", "sdmgrad-8e-1", "sdmgrad-9e-1", "sdmgrad-1e0" ] colors = ["C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "tab:green", "tab:cyan", "tab:blue", "tab:red"] stats = [ "semantic loss", "mean iou", "pix acc", "depth loss", "abs err", "rel err", "normal loss", "mean", "median", "<11.25", "<22.5", "<30" ] delta_stats = ["mean iou", "pix acc", "abs err", "rel err", "mean", "median", "<11.25", "<22.5", "<30"] stats_idx_map = [4, 5, 6, 8, 9, 10, 12, 13, 14, 15, 16, 17] time_idx = 34 # change random seeds used in the experiments here seeds = [0, 1, 2] logs = {} min_epoch = 100000 for m in methods: logs[m] = {"train": [None for _ in range(3)], "test": [None for _ in range(3)]} for seed in seeds: logs[m]["train"][seed] = {} logs[m]["test"][seed] = {} for stat in stats: for seed in seeds: logs[m]["train"][seed][stat] = [] logs[m]["test"][seed][stat] = [] for seed in seeds: logs[m]["train"][seed]["time"] = [] for seed in seeds: fname = f"logs/{m}-sd{seed}.log" with open(fname, "r") as f: lines = f.readlines() for line in lines: if line.startswith("Epoch"): ws = line.split(" ") for i, stat in enumerate(stats): logs[m]["train"][seed][stat].append(float(ws[stats_idx_map[i]])) logs[m]["test"][seed][stat].append(float(ws[stats_idx_map[i] + 15])) logs[m]["train"][seed]["time"].append(float(ws[time_idx])) min_epoch = min(min(min_epoch, len(logs[m]["train"][seed]["semantic loss"])), len(logs[m]["test"][seed]["semantic loss"])) test_stats = {} train_stats = {} learning_time = {} print(" " * 25 + " | ".join([f"{s:5s}" for s in stats])) for mi, mode in enumerate(["train", "test"]): if mi == 1: print(mode) for mmi, m in enumerate(methods): if m not in test_stats: test_stats[m] = {} train_stats[m] = {} string = f"{m:30s} " for stat in stats: x = [] for seed in seeds: x.append(np.array(logs[m][mode][seed][stat][min_epoch - 10:min_epoch]).mean()) x = np.array(x) if mode == "test": test_stats[m][stat] = x.copy() else: train_stats[m][stat] = x.copy() mu = x.mean() std = x.std() / np.sqrt(3) string += f" | {mu:5.4f}" if mode == "test": print(string) for m in methods: learning_time[m] = np.array([np.array(logs[m]["train"][sd]["time"]).mean() for sd in seeds]) ### print average training loss for method in methods: average_loss = np.mean([ train_stats[method]["semantic loss"].mean(), train_stats[method]["depth loss"].mean(), train_stats[method]["normal loss"].mean() ]) print(f"{method} average training loss {average_loss}") ### print delta M base = np.array([0.3830, 0.6376, 0.6754, 0.2780, 25.01, 19.21, 0.3014, 0.5720, 0.6915]) sign = np.array([1, 1, 0, 0, 0, 0, 1, 1, 1]) kk = np.ones(9) * -1 def delta_fn(a): return (kk**sign * (a - base) / base).mean() * 100. # *100 for percentage deltas = {} for method in methods: tmp = np.zeros(9) for i, stat in enumerate(delta_stats): tmp[i] = test_stats[method][stat].mean() deltas[method] = delta_fn(tmp) print(f"{method:30s} delta: {deltas[method]:4.3f}")
3,777
30.747899
117
py
sdmgrad
sdmgrad-main/nyuv2/model_segnet_stan.py
import torch.nn as nn import torch.optim as optim import torch.nn.functional as F import argparse from create_dataset import * from utils import * parser = argparse.ArgumentParser(description='Single-task: Attention Network') parser.add_argument('--task', default='semantic', type=str, help='choose task: semantic, depth, normal') parser.add_argument('--dataroot', default='nyuv2', type=str, help='dataset root') parser.add_argument('--apply_augmentation', action='store_true', help='toggle to apply data augmentation on NYUv2') opt = parser.parse_args() class SegNet(nn.Module): def __init__(self): super(SegNet, self).__init__() # initialise network parameters filter = [64, 128, 256, 512, 512] self.class_nb = 13 # define encoder decoder layers self.encoder_block = nn.ModuleList([self.conv_layer([3, filter[0]])]) self.decoder_block = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): self.encoder_block.append(self.conv_layer([filter[i], filter[i + 1]])) self.decoder_block.append(self.conv_layer([filter[i + 1], filter[i]])) # define convolution layer self.conv_block_enc = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) self.conv_block_dec = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): if i == 0: self.conv_block_enc.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.conv_block_dec.append(self.conv_layer([filter[i], filter[i]])) else: self.conv_block_enc.append( nn.Sequential(self.conv_layer([filter[i + 1], filter[i + 1]]), self.conv_layer([filter[i + 1], filter[i + 1]]))) self.conv_block_dec.append( nn.Sequential(self.conv_layer([filter[i], filter[i]]), self.conv_layer([filter[i], filter[i]]))) self.encoder_att = nn.ModuleList([nn.ModuleList([self.att_layer([filter[0], filter[0], filter[0]])])]) self.decoder_att = nn.ModuleList([nn.ModuleList([self.att_layer([2 * filter[0], filter[0], filter[0]])])]) self.encoder_block_att = nn.ModuleList([self.conv_layer([filter[0], filter[1]])]) self.decoder_block_att = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for j in range(1): for i in range(4): self.encoder_att[j].append(self.att_layer([2 * filter[i + 1], filter[i + 1], filter[i + 1]])) self.decoder_att[j].append(self.att_layer([filter[i + 1] + filter[i], filter[i], filter[i]])) for i in range(4): if i < 3: self.encoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 2]])) self.decoder_block_att.append(self.conv_layer([filter[i + 1], filter[i]])) else: self.encoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.decoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 1]])) if opt.task == 'semantic': self.pred_task = self.conv_layer([filter[0], self.class_nb], pred=True) if opt.task == 'depth': self.pred_task = self.conv_layer([filter[0], 1], pred=True) if opt.task == 'normal': self.pred_task = self.conv_layer([filter[0], 3], pred=True) # define pooling and unpooling functions self.down_sampling = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True) self.up_sampling = nn.MaxUnpool2d(kernel_size=2, stride=2) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) def conv_layer(self, channel, pred=False): if not pred: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(num_features=channel[1]), nn.ReLU(inplace=True), ) else: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), ) return conv_block def att_layer(self, channel): att_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), nn.BatchNorm2d(channel[1]), nn.ReLU(inplace=True), nn.Conv2d(in_channels=channel[1], out_channels=channel[2], kernel_size=1, padding=0), nn.BatchNorm2d(channel[2]), nn.Sigmoid(), ) return att_block def forward(self, x): g_encoder, g_decoder, g_maxpool, g_upsampl, indices = ([0] * 5 for _ in range(5)) for i in range(5): g_encoder[i], g_decoder[-i - 1] = ([0] * 2 for _ in range(2)) # define attention list for two tasks atten_encoder, atten_decoder = ([0] * 3 for _ in range(2)) for i in range(3): atten_encoder[i], atten_decoder[i] = ([0] * 5 for _ in range(2)) for i in range(3): for j in range(5): atten_encoder[i][j], atten_decoder[i][j] = ([0] * 3 for _ in range(2)) # define global shared network for i in range(5): if i == 0: g_encoder[i][0] = self.encoder_block[i](x) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) else: g_encoder[i][0] = self.encoder_block[i](g_maxpool[i - 1]) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) for i in range(5): if i == 0: g_upsampl[i] = self.up_sampling(g_maxpool[-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) else: g_upsampl[i] = self.up_sampling(g_decoder[i - 1][-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) # define task dependent attention module for i in range(1): for j in range(5): if j == 0: atten_encoder[i][j][0] = self.encoder_att[i][j](g_encoder[j][0]) atten_encoder[i][j][1] = (atten_encoder[i][j][0]) * g_encoder[j][1] atten_encoder[i][j][2] = self.encoder_block_att[j](atten_encoder[i][j][1]) atten_encoder[i][j][2] = F.max_pool2d(atten_encoder[i][j][2], kernel_size=2, stride=2) else: atten_encoder[i][j][0] = self.encoder_att[i][j](torch.cat( (g_encoder[j][0], atten_encoder[i][j - 1][2]), dim=1)) atten_encoder[i][j][1] = (atten_encoder[i][j][0]) * g_encoder[j][1] atten_encoder[i][j][2] = self.encoder_block_att[j](atten_encoder[i][j][1]) atten_encoder[i][j][2] = F.max_pool2d(atten_encoder[i][j][2], kernel_size=2, stride=2) for j in range(5): if j == 0: atten_decoder[i][j][0] = F.interpolate(atten_encoder[i][-1][-1], scale_factor=2, mode='bilinear', align_corners=True) atten_decoder[i][j][0] = self.decoder_block_att[-j - 1](atten_decoder[i][j][0]) atten_decoder[i][j][1] = self.decoder_att[i][-j - 1](torch.cat( (g_upsampl[j], atten_decoder[i][j][0]), dim=1)) atten_decoder[i][j][2] = (atten_decoder[i][j][1]) * g_decoder[j][-1] else: atten_decoder[i][j][0] = F.interpolate(atten_decoder[i][j - 1][2], scale_factor=2, mode='bilinear', align_corners=True) atten_decoder[i][j][0] = self.decoder_block_att[-j - 1](atten_decoder[i][j][0]) atten_decoder[i][j][1] = self.decoder_att[i][-j - 1](torch.cat( (g_upsampl[j], atten_decoder[i][j][0]), dim=1)) atten_decoder[i][j][2] = (atten_decoder[i][j][1]) * g_decoder[j][-1] # define task prediction layers if opt.task == 'semantic': pred = F.log_softmax(self.pred_task(atten_decoder[0][-1][-1]), dim=1) if opt.task == 'depth': pred = self.pred_task(atten_decoder[0][-1][-1]) if opt.task == 'normal': pred = self.pred_task(atten_decoder[0][-1][-1]) pred = pred / torch.norm(pred, p=2, dim=1, keepdim=True) return pred # define model, optimiser and scheduler device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") SegNet_STAN = SegNet().to(device) optimizer = optim.Adam(SegNet_STAN.parameters(), lr=1e-4) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.5) print('Parameter Space: ABS: {:.1f}, REL: {:.4f}'.format(count_parameters(SegNet_STAN), count_parameters(SegNet_STAN) / 24981069)) print( 'LOSS FORMAT: SEMANTIC_LOSS MEAN_IOU PIX_ACC | DEPTH_LOSS ABS_ERR REL_ERR | NORMAL_LOSS MEAN MED <11.25 <22.5 <30') # define dataset dataset_path = opt.dataroot if opt.apply_augmentation: nyuv2_train_set = NYUv2(root=dataset_path, train=True, augmentation=True) print('Applying data augmentation on NYUv2.') else: nyuv2_train_set = NYUv2(root=dataset_path, train=True) print('Standard training strategy without data augmentation.') nyuv2_test_set = NYUv2(root=dataset_path, train=False) batch_size = 2 nyuv2_train_loader = torch.utils.data.DataLoader(dataset=nyuv2_train_set, batch_size=batch_size, shuffle=True) nyuv2_test_loader = torch.utils.data.DataLoader(dataset=nyuv2_test_set, batch_size=batch_size, shuffle=False) # Train and evaluate single-task network single_task_trainer(nyuv2_train_loader, nyuv2_test_loader, SegNet_STAN, device, optimizer, scheduler, opt, 200)
11,017
49.310502
119
py
sdmgrad
sdmgrad-main/nyuv2/utils.py
import numpy as np import time import torch import torch.nn.functional as F from copy import deepcopy from min_norm_solvers import MinNormSolver from scipy.optimize import minimize, Bounds, minimize_scalar def euclidean_proj_simplex(v, s=1): """ Compute the Euclidean projection on a positive simplex Solves the optimisation problem (using the algorithm from [1]): min_w 0.5 * || w - v ||_2^2 , s.t. \sum_i w_i = s, w_i >= 0 Parameters ---------- v: (n,) numpy array, n-dimensional vector to project s: int, optional, default: 1, radius of the simplex Returns ------- w: (n,) numpy array, Euclidean projection of v on the simplex Notes ----- The complexity of this algorithm is in O(n log(n)) as it involves sorting v. Better alternatives exist for high-dimensional sparse vectors (cf. [1]) However, this implementation still easily scales to millions of dimensions. References ---------- [1] Efficient Projections onto the .1-Ball for Learning in High Dimensions John Duchi, Shai Shalev-Shwartz, Yoram Singer, and Tushar Chandra. International Conference on Machine Learning (ICML 2008) http://www.cs.berkeley.edu/~jduchi/projects/DuchiSiShCh08.pdf [2] Projection onto the probability simplex: An efficient algorithm with a simple proof, and an application Weiran Wang, Miguel Á. Carreira-Perpiñán. arXiv:1309.1541 https://arxiv.org/pdf/1309.1541.pdf [3] https://gist.github.com/daien/1272551/edd95a6154106f8e28209a1c7964623ef8397246#file-simplex_projection-py """ assert s > 0, "Radius s must be strictly positive (%d <= 0)" % s v = v.astype(np.float64) n, = v.shape # will raise ValueError if v is not 1-D # check if we are already on the simplex if v.sum() == s and np.alltrue(v >= 0): # best projection: itself! return v # get the array of cumulative sums of a sorted (decreasing) copy of v u = np.sort(v)[::-1] cssv = np.cumsum(u) # get the number of > 0 components of the optimal solution rho = np.nonzero(u * np.arange(1, n + 1) > (cssv - s))[0][-1] # compute the Lagrange multiplier associated to the simplex constraint theta = float(cssv[rho] - s) / (rho + 1) # compute the projection by thresholding v using theta w = (v - theta).clip(min=0) return w """ Define task metrics, loss functions and model trainer here. """ def count_parameters(model): return sum(p.numel() for p in model.parameters() if p.requires_grad) def model_fit(x_pred, x_output, task_type): device = x_pred.device # binary mark to mask out undefined pixel space binary_mask = (torch.sum(x_output, dim=1) != 0).float().unsqueeze(1).to(device) if task_type == 'semantic': # semantic loss: depth-wise cross entropy loss = F.nll_loss(x_pred, x_output, ignore_index=-1) if task_type == 'depth': # depth loss: l1 norm loss = torch.sum(torch.abs(x_pred - x_output) * binary_mask) / torch.nonzero(binary_mask, as_tuple=False).size(0) if task_type == 'normal': # normal loss: dot product loss = 1 - torch.sum((x_pred * x_output) * binary_mask) / torch.nonzero(binary_mask, as_tuple=False).size(0) return loss # Legacy: compute mIoU and Acc. for each image and average across all images. # def compute_miou(x_pred, x_output): # _, x_pred_label = torch.max(x_pred, dim=1) # x_output_label = x_output # batch_size = x_pred.size(0) # class_nb = x_pred.size(1) # device = x_pred.device # for i in range(batch_size): # true_class = 0 # first_switch = True # invalid_mask = (x_output[i] >= 0).float() # for j in range(class_nb): # pred_mask = torch.eq(x_pred_label[i], j * torch.ones(x_pred_label[i].shape).long().to(device)) # true_mask = torch.eq(x_output_label[i], j * torch.ones(x_output_label[i].shape).long().to(device)) # mask_comb = pred_mask.float() + true_mask.float() # union = torch.sum((mask_comb > 0).float() * invalid_mask) # remove non-defined pixel predictions # intsec = torch.sum((mask_comb > 1).float()) # if union == 0: # continue # if first_switch: # class_prob = intsec / union # first_switch = False # else: # class_prob = intsec / union + class_prob # true_class += 1 # if i == 0: # batch_avg = class_prob / true_class # else: # batch_avg = class_prob / true_class + batch_avg # return batch_avg / batch_size # # # def compute_iou(x_pred, x_output): # _, x_pred_label = torch.max(x_pred, dim=1) # x_output_label = x_output # batch_size = x_pred.size(0) # for i in range(batch_size): # if i == 0: # pixel_acc = torch.div( # torch.sum(torch.eq(x_pred_label[i], x_output_label[i]).float()), # torch.sum((x_output_label[i] >= 0).float())) # else: # pixel_acc = pixel_acc + torch.div( # torch.sum(torch.eq(x_pred_label[i], x_output_label[i]).float()), # torch.sum((x_output_label[i] >= 0).float())) # return pixel_acc / batch_size # New mIoU and Acc. formula: accumulate every pixel and average across all pixels in all images class ConfMatrix(object): def __init__(self, num_classes): self.num_classes = num_classes self.mat = None def update(self, pred, target): n = self.num_classes if self.mat is None: self.mat = torch.zeros((n, n), dtype=torch.int64, device=pred.device) with torch.no_grad(): k = (target >= 0) & (target < n) inds = n * target[k].to(torch.int64) + pred[k] self.mat += torch.bincount(inds, minlength=n**2).reshape(n, n) def get_metrics(self): h = self.mat.float() acc = torch.diag(h).sum() / h.sum() iu = torch.diag(h) / (h.sum(1) + h.sum(0) - torch.diag(h)) return torch.mean(iu).item(), acc.item() def depth_error(x_pred, x_output): device = x_pred.device binary_mask = (torch.sum(x_output, dim=1) != 0).unsqueeze(1).to(device) x_pred_true = x_pred.masked_select(binary_mask) x_output_true = x_output.masked_select(binary_mask) abs_err = torch.abs(x_pred_true - x_output_true) rel_err = torch.abs(x_pred_true - x_output_true) / x_output_true return (torch.sum(abs_err) / torch.nonzero(binary_mask, as_tuple=False).size(0)).item(), \ (torch.sum(rel_err) / torch.nonzero(binary_mask, as_tuple=False).size(0)).item() def normal_error(x_pred, x_output): binary_mask = (torch.sum(x_output, dim=1) != 0) error = torch.acos(torch.clamp(torch.sum(x_pred * x_output, 1).masked_select(binary_mask), -1, 1)).detach().cpu().numpy() error = np.degrees(error) return np.mean(error), np.median(error), np.mean(error < 11.25), np.mean(error < 22.5), np.mean(error < 30) """ =========== Universal Multi-task Trainer =========== """ def multi_task_trainer(train_loader, test_loader, multi_task_model, device, optimizer, scheduler, opt, total_epoch=200): start_time = time.time() train_batch = len(train_loader) test_batch = len(test_loader) T = opt.temp avg_cost = np.zeros([total_epoch, 24], dtype=np.float32) lambda_weight = np.ones([3, total_epoch]) for index in range(total_epoch): epoch_start_time = time.time() cost = np.zeros(24, dtype=np.float32) # apply Dynamic Weight Average if opt.weight == 'dwa': if index == 0 or index == 1: lambda_weight[:, index] = 1.0 else: w_1 = avg_cost[index - 1, 0] / avg_cost[index - 2, 0] w_2 = avg_cost[index - 1, 3] / avg_cost[index - 2, 3] w_3 = avg_cost[index - 1, 6] / avg_cost[index - 2, 6] lambda_weight[0, index] = 3 * np.exp(w_1 / T) / (np.exp(w_1 / T) + np.exp(w_2 / T) + np.exp(w_3 / T)) lambda_weight[1, index] = 3 * np.exp(w_2 / T) / (np.exp(w_1 / T) + np.exp(w_2 / T) + np.exp(w_3 / T)) lambda_weight[2, index] = 3 * np.exp(w_3 / T) / (np.exp(w_1 / T) + np.exp(w_2 / T) + np.exp(w_3 / T)) # iteration for all batches multi_task_model.train() train_dataset = iter(train_loader) conf_mat = ConfMatrix(multi_task_model.class_nb) for k in range(train_batch): train_data, train_label, train_depth, train_normal = train_dataset.next() train_data, train_label = train_data.to(device), train_label.long().to(device) train_depth, train_normal = train_depth.to(device), train_normal.to(device) train_pred, logsigma = multi_task_model(train_data) optimizer.zero_grad() train_loss = [ model_fit(train_pred[0], train_label, 'semantic'), model_fit(train_pred[1], train_depth, 'depth'), model_fit(train_pred[2], train_normal, 'normal') ] if opt.weight == 'equal' or opt.weight == 'dwa': loss = sum([lambda_weight[i, index] * train_loss[i] for i in range(3)]) #loss = sum([w[i] * train_loss[i] for i in range(3)]) else: loss = sum(1 / (2 * torch.exp(logsigma[i])) * train_loss[i] + logsigma[i] / 2 for i in range(3)) loss.backward() optimizer.step() # accumulate label prediction for every pixel in training images conf_mat.update(train_pred[0].argmax(1).flatten(), train_label.flatten()) cost[0] = train_loss[0].item() cost[3] = train_loss[1].item() cost[4], cost[5] = depth_error(train_pred[1], train_depth) cost[6] = train_loss[2].item() cost[7], cost[8], cost[9], cost[10], cost[11] = normal_error(train_pred[2], train_normal) avg_cost[index, :12] += cost[:12] / train_batch # compute mIoU and acc avg_cost[index, 1:3] = conf_mat.get_metrics() # evaluating test data multi_task_model.eval() conf_mat = ConfMatrix(multi_task_model.class_nb) with torch.no_grad(): # operations inside don't track history test_dataset = iter(test_loader) for k in range(test_batch): test_data, test_label, test_depth, test_normal = test_dataset.next() test_data, test_label = test_data.to(device), test_label.long().to(device) test_depth, test_normal = test_depth.to(device), test_normal.to(device) test_pred, _ = multi_task_model(test_data) test_loss = [ model_fit(test_pred[0], test_label, 'semantic'), model_fit(test_pred[1], test_depth, 'depth'), model_fit(test_pred[2], test_normal, 'normal') ] conf_mat.update(test_pred[0].argmax(1).flatten(), test_label.flatten()) cost[12] = test_loss[0].item() cost[15] = test_loss[1].item() cost[16], cost[17] = depth_error(test_pred[1], test_depth) cost[18] = test_loss[2].item() cost[19], cost[20], cost[21], cost[22], cost[23] = normal_error(test_pred[2], test_normal) avg_cost[index, 12:] += cost[12:] / test_batch # compute mIoU and acc avg_cost[index, 13:15] = conf_mat.get_metrics() scheduler.step() epoch_end_time = time.time() print( 'Epoch: {:04d} | TRAIN: {:.4f} {:.4f} {:.4f} | {:.4f} {:.4f} {:.4f} | {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} ||' 'TEST: {:.4f} {:.4f} {:.4f} | {:.4f} {:.4f} {:.4f} | {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} | {:.4f}'. format(index, avg_cost[index, 0], avg_cost[index, 1], avg_cost[index, 2], avg_cost[index, 3], avg_cost[index, 4], avg_cost[index, 5], avg_cost[index, 6], avg_cost[index, 7], avg_cost[index, 8], avg_cost[index, 9], avg_cost[index, 10], avg_cost[index, 11], avg_cost[index, 12], avg_cost[index, 13], avg_cost[index, 14], avg_cost[index, 15], avg_cost[index, 16], avg_cost[index, 17], avg_cost[index, 18], avg_cost[index, 19], avg_cost[index, 20], avg_cost[index, 21], avg_cost[index, 22], avg_cost[index, 23], epoch_end_time - epoch_start_time)) end_time = time.time() print("Training time: ", end_time - start_time) """ =========== Universal Single-task Trainer =========== """ def single_task_trainer(train_loader, test_loader, single_task_model, device, optimizer, scheduler, opt, total_epoch=200): train_batch = len(train_loader) test_batch = len(test_loader) avg_cost = np.zeros([total_epoch, 24], dtype=np.float32) for index in range(total_epoch): cost = np.zeros(24, dtype=np.float32) # iteration for all batches single_task_model.train() train_dataset = iter(train_loader) conf_mat = ConfMatrix(single_task_model.class_nb) for k in range(train_batch): train_data, train_label, train_depth, train_normal = train_dataset.next() train_data, train_label = train_data.to(device), train_label.long().to(device) train_depth, train_normal = train_depth.to(device), train_normal.to(device) train_pred = single_task_model(train_data) optimizer.zero_grad() if opt.task == 'semantic': train_loss = model_fit(train_pred, train_label, opt.task) train_loss.backward() optimizer.step() conf_mat.update(train_pred.argmax(1).flatten(), train_label.flatten()) cost[0] = train_loss.item() if opt.task == 'depth': train_loss = model_fit(train_pred, train_depth, opt.task) train_loss.backward() optimizer.step() cost[3] = train_loss.item() cost[4], cost[5] = depth_error(train_pred, train_depth) if opt.task == 'normal': train_loss = model_fit(train_pred, train_normal, opt.task) train_loss.backward() optimizer.step() cost[6] = train_loss.item() cost[7], cost[8], cost[9], cost[10], cost[11] = normal_error(train_pred, train_normal) avg_cost[index, :12] += cost[:12] / train_batch if opt.task == 'semantic': avg_cost[index, 1:3] = conf_mat.get_metrics() # evaluating test data single_task_model.eval() conf_mat = ConfMatrix(single_task_model.class_nb) with torch.no_grad(): # operations inside don't track history test_dataset = iter(test_loader) for k in range(test_batch): test_data, test_label, test_depth, test_normal = test_dataset.next() test_data, test_label = test_data.to(device), test_label.long().to(device) test_depth, test_normal = test_depth.to(device), test_normal.to(device) test_pred = single_task_model(test_data) if opt.task == 'semantic': test_loss = model_fit(test_pred, test_label, opt.task) conf_mat.update(test_pred.argmax(1).flatten(), test_label.flatten()) cost[12] = test_loss.item() if opt.task == 'depth': test_loss = model_fit(test_pred, test_depth, opt.task) cost[15] = test_loss.item() cost[16], cost[17] = depth_error(test_pred, test_depth) if opt.task == 'normal': test_loss = model_fit(test_pred, test_normal, opt.task) cost[18] = test_loss.item() cost[19], cost[20], cost[21], cost[22], cost[23] = normal_error(test_pred, test_normal) avg_cost[index, 12:] += cost[12:] / test_batch if opt.task == 'semantic': avg_cost[index, 13:15] = conf_mat.get_metrics() scheduler.step() if opt.task == 'semantic': print('Epoch: {:04d} | TRAIN: {:.4f} {:.4f} {:.4f} TEST: {:.4f} {:.4f} {:.4f}'.format( index, avg_cost[index, 0], avg_cost[index, 1], avg_cost[index, 2], avg_cost[index, 12], avg_cost[index, 13], avg_cost[index, 14])) if opt.task == 'depth': print('Epoch: {:04d} | TRAIN: {:.4f} {:.4f} {:.4f} TEST: {:.4f} {:.4f} {:.4f}'.format( index, avg_cost[index, 3], avg_cost[index, 4], avg_cost[index, 5], avg_cost[index, 15], avg_cost[index, 16], avg_cost[index, 17])) if opt.task == 'normal': print( 'Epoch: {:04d} | TRAIN: {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} TEST: {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} {:.4f}' .format(index, avg_cost[index, 6], avg_cost[index, 7], avg_cost[index, 8], avg_cost[index, 9], avg_cost[index, 10], avg_cost[index, 11], avg_cost[index, 18], avg_cost[index, 19], avg_cost[index, 20], avg_cost[index, 21], avg_cost[index, 22], avg_cost[index, 23])) ''' ===== multi task MGD trainer ==== ''' def multi_task_mgd_trainer(train_loader, test_loader, multi_task_model, device, optimizer, scheduler, opt, total_epoch=200, method='sumloss', alpha=0.5, seed=0): start_time = time.time() niter = opt.niter def graddrop(grads): P = 0.5 * (1. + grads.sum(1) / (grads.abs().sum(1) + 1e-8)) U = torch.rand_like(grads[:, 0]) M = P.gt(U).view(-1, 1) * grads.gt(0) + P.lt(U).view(-1, 1) * grads.lt(0) g = (grads * M.float()).mean(1) return g def mgd(grads): grads_cpu = grads.t().cpu() sol, min_norm = MinNormSolver.find_min_norm_element([grads_cpu[t] for t in range(grads.shape[-1])]) w = torch.FloatTensor(sol).to(grads.device) g = grads.mm(w.view(-1, 1)).view(-1) return g def pcgrad(grads, rng): grad_vec = grads.t() num_tasks = 3 shuffled_task_indices = np.zeros((num_tasks, num_tasks - 1), dtype=int) for i in range(num_tasks): task_indices = np.arange(num_tasks) task_indices[i] = task_indices[-1] shuffled_task_indices[i] = task_indices[:-1] rng.shuffle(shuffled_task_indices[i]) shuffled_task_indices = shuffled_task_indices.T normalized_grad_vec = grad_vec / (grad_vec.norm(dim=1, keepdim=True) + 1e-8) # num_tasks x dim modified_grad_vec = deepcopy(grad_vec) for task_indices in shuffled_task_indices: normalized_shuffled_grad = normalized_grad_vec[task_indices] # num_tasks x dim dot = (modified_grad_vec * normalized_shuffled_grad).sum(dim=1, keepdim=True) # num_tasks x dim modified_grad_vec -= torch.clamp_max(dot, 0) * normalized_shuffled_grad g = modified_grad_vec.mean(dim=0) return g def cagrad(grads, alpha=0.5, rescale=1): GG = grads.t().mm(grads).cpu() # [num_tasks, num_tasks] g0_norm = (GG.mean() + 1e-8).sqrt() # norm of the average gradient x_start = np.ones(3) / 3 bnds = tuple((0, 1) for x in x_start) cons = ({'type': 'eq', 'fun': lambda x: 1 - sum(x)}) A = GG.numpy() b = x_start.copy() c = (alpha * g0_norm + 1e-8).item() def objfn(x): return (x.reshape(1, 3).dot(A).dot(b.reshape(3, 1)) + c * np.sqrt(x.reshape(1, 3).dot(A).dot(x.reshape(3, 1)) + 1e-8)).sum() res = minimize(objfn, x_start, bounds=bnds, constraints=cons) w_cpu = res.x ww = torch.Tensor(w_cpu).to(grads.device) gw = (grads * ww.view(1, -1)).sum(1) gw_norm = gw.norm() lmbda = c / (gw_norm + 1e-8) g = grads.mean(1) + lmbda * gw if rescale == 0: return g elif rescale == 1: return g / (1 + alpha**2) else: return g / (1 + alpha) def sdmgrad(w, grads, alpha, niter=20): GG = torch.mm(grads.t(), grads) scale = torch.mean(torch.sqrt(torch.diag(GG) + 1e-4)) GG = GG / scale.pow(2) Gg = torch.mean(GG, dim=1) gg = torch.mean(Gg) w.requires_grad = True optimizer = torch.optim.SGD([w], lr=10, momentum=0.5) for i in range(niter): optimizer.zero_grad() obj = torch.dot(w, torch.mv(GG, w)) + 2 * alpha * torch.dot(w, Gg) + alpha**2 * gg obj.backward() optimizer.step() proj = euclidean_proj_simplex(w.data.cpu().numpy()) w.data.copy_(torch.from_numpy(proj).data) w.requires_grad = False g0 = torch.mean(grads, dim=1) gw = torch.mv(grads, w) g = (gw + alpha * g0) / (1 + alpha) return g def grad2vec(m, grads, grad_dims, task): # store the gradients grads[:, task].fill_(0.0) cnt = 0 for mm in m.shared_modules(): for p in mm.parameters(): grad = p.grad if grad is not None: grad_cur = grad.data.detach().clone() beg = 0 if cnt == 0 else sum(grad_dims[:cnt]) en = sum(grad_dims[:cnt + 1]) grads[beg:en, task].copy_(grad_cur.data.view(-1)) cnt += 1 def overwrite_grad(m, newgrad, grad_dims): newgrad = newgrad * 3 # to match the sum loss cnt = 0 for mm in m.shared_modules(): for param in mm.parameters(): beg = 0 if cnt == 0 else sum(grad_dims[:cnt]) en = sum(grad_dims[:cnt + 1]) this_grad = newgrad[beg:en].contiguous().view(param.data.size()) param.grad = this_grad.data.clone() cnt += 1 rng = np.random.default_rng() grad_dims = [] for mm in multi_task_model.shared_modules(): for param in mm.parameters(): grad_dims.append(param.data.numel()) grads = torch.Tensor(sum(grad_dims), 3).cuda() w = 1 / 3 * torch.ones(3).cuda() train_batch = len(train_loader) test_batch = len(test_loader) T = opt.temp avg_cost = np.zeros([total_epoch, 24], dtype=np.float32) lambda_weight = np.ones([3, total_epoch]) neg_trace = [] obj_trace = [] for index in range(total_epoch): epoch_start_time = time.time() cost = np.zeros(24, dtype=np.float32) # apply Dynamic Weight Average if opt.weight == 'dwa': if index == 0 or index == 1: lambda_weight[:, index] = 1.0 else: w_1 = avg_cost[index - 1, 0] / avg_cost[index - 2, 0] w_2 = avg_cost[index - 1, 3] / avg_cost[index - 2, 3] w_3 = avg_cost[index - 1, 6] / avg_cost[index - 2, 6] lambda_weight[0, index] = 3 * np.exp(w_1 / T) / (np.exp(w_1 / T) + np.exp(w_2 / T) + np.exp(w_3 / T)) lambda_weight[1, index] = 3 * np.exp(w_2 / T) / (np.exp(w_1 / T) + np.exp(w_2 / T) + np.exp(w_3 / T)) lambda_weight[2, index] = 3 * np.exp(w_3 / T) / (np.exp(w_1 / T) + np.exp(w_2 / T) + np.exp(w_3 / T)) # iteration for all batches multi_task_model.train() train_dataset = iter(train_loader) conf_mat = ConfMatrix(multi_task_model.class_nb) for k in range(train_batch): train_data, train_label, train_depth, train_normal = train_dataset.next() train_data, train_label = train_data.to(device), train_label.long().to(device) train_depth, train_normal = train_depth.to(device), train_normal.to(device) train_pred, logsigma = multi_task_model(train_data) train_loss = [ model_fit(train_pred[0], train_label, 'semantic'), model_fit(train_pred[1], train_depth, 'depth'), model_fit(train_pred[2], train_normal, 'normal') ] train_loss_tmp = [0, 0, 0] if opt.weight == 'equal' or opt.weight == 'dwa': for i in range(3): train_loss_tmp[i] = train_loss[i] * lambda_weight[i, index] else: for i in range(3): train_loss_tmp[i] = 1 / (2 * torch.exp(logsigma[i])) * train_loss[i] + logsigma[i] / 2 optimizer.zero_grad() if method == "graddrop": for i in range(3): if i < 3: train_loss_tmp[i].backward(retain_graph=True) else: train_loss_tmp[i].backward() grad2vec(multi_task_model, grads, grad_dims, i) multi_task_model.zero_grad_shared_modules() g = graddrop(grads) overwrite_grad(multi_task_model, g, grad_dims) optimizer.step() elif method == "mgd": for i in range(3): if i < 3: train_loss_tmp[i].backward(retain_graph=True) else: train_loss_tmp[i].backward() grad2vec(multi_task_model, grads, grad_dims, i) multi_task_model.zero_grad_shared_modules() g = mgd(grads) overwrite_grad(multi_task_model, g, grad_dims) optimizer.step() elif method == "pcgrad": for i in range(3): if i < 3: train_loss_tmp[i].backward(retain_graph=True) else: train_loss_tmp[i].backward() grad2vec(multi_task_model, grads, grad_dims, i) multi_task_model.zero_grad_shared_modules() g = pcgrad(grads, rng) overwrite_grad(multi_task_model, g, grad_dims) optimizer.step() elif method == "cagrad": for i in range(3): if i < 3: train_loss_tmp[i].backward(retain_graph=True) else: train_loss_tmp[i].backward() grad2vec(multi_task_model, grads, grad_dims, i) multi_task_model.zero_grad_shared_modules() g = cagrad(grads, alpha, rescale=1) overwrite_grad(multi_task_model, g, grad_dims) optimizer.step() elif method == "sdmgrad": for i in range(3): if i < 3: train_loss_tmp[i].backward(retain_graph=True) else: train_loss_tmp[i].backward() grad2vec(multi_task_model, grads, grad_dims, i) multi_task_model.zero_grad_shared_modules() g = sdmgrad(w, grads, alpha, niter=niter) overwrite_grad(multi_task_model, g, grad_dims) optimizer.step() # accumulate label prediction for every pixel in training images conf_mat.update(train_pred[0].argmax(1).flatten(), train_label.flatten()) cost[0] = train_loss[0].item() cost[3] = train_loss[1].item() cost[4], cost[5] = depth_error(train_pred[1], train_depth) cost[6] = train_loss[2].item() cost[7], cost[8], cost[9], cost[10], cost[11] = normal_error(train_pred[2], train_normal) avg_cost[index, :12] += cost[:12] / train_batch # compute mIoU and acc avg_cost[index, 1:3] = conf_mat.get_metrics() # evaluating test data multi_task_model.eval() conf_mat = ConfMatrix(multi_task_model.class_nb) with torch.no_grad(): # operations inside don't track history test_dataset = iter(test_loader) for k in range(test_batch): test_data, test_label, test_depth, test_normal = test_dataset.next() test_data, test_label = test_data.to(device), test_label.long().to(device) test_depth, test_normal = test_depth.to(device), test_normal.to(device) test_pred, _ = multi_task_model(test_data) test_loss = [ model_fit(test_pred[0], test_label, 'semantic'), model_fit(test_pred[1], test_depth, 'depth'), model_fit(test_pred[2], test_normal, 'normal') ] conf_mat.update(test_pred[0].argmax(1).flatten(), test_label.flatten()) cost[12] = test_loss[0].item() cost[15] = test_loss[1].item() cost[16], cost[17] = depth_error(test_pred[1], test_depth) cost[18] = test_loss[2].item() cost[19], cost[20], cost[21], cost[22], cost[23] = normal_error(test_pred[2], test_normal) avg_cost[index, 12:] += cost[12:] / test_batch # compute mIoU and acc avg_cost[index, 13:15] = conf_mat.get_metrics() scheduler.step() if method == "mean": torch.save(torch.Tensor(neg_trace), "trace.pt") if "debug" in method: torch.save(torch.Tensor(obj_trace), f"{method}_obj.pt") epoch_end_time = time.time() print( 'Epoch: {:04d} | TRAIN: {:.4f} {:.4f} {:.4f} | {:.4f} {:.4f} {:.4f} | {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} ||' 'TEST: {:.4f} {:.4f} {:.4f} | {:.4f} {:.4f} {:.4f} | {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} | {:.4f}'. format(index, avg_cost[index, 0], avg_cost[index, 1], avg_cost[index, 2], avg_cost[index, 3], avg_cost[index, 4], avg_cost[index, 5], avg_cost[index, 6], avg_cost[index, 7], avg_cost[index, 8], avg_cost[index, 9], avg_cost[index, 10], avg_cost[index, 11], avg_cost[index, 12], avg_cost[index, 13], avg_cost[index, 14], avg_cost[index, 15], avg_cost[index, 16], avg_cost[index, 17], avg_cost[index, 18], avg_cost[index, 19], avg_cost[index, 20], avg_cost[index, 21], avg_cost[index, 22], avg_cost[index, 23], epoch_end_time - epoch_start_time)) if "cagrad" in method: torch.save(multi_task_model.state_dict(), f"models/{method}-{opt.weight}-{alpha}-{seed}.pt") elif "sdmgrad" in method: torch.save(multi_task_model.state_dict(), f"models/{method}-{opt.weight}-{alpha}-{seed}-{niter}.pt") else: torch.save(multi_task_model.state_dict(), f"models/{method}-{opt.weight}-{seed}.pt") end_time = time.time() print("Training time: ", end_time - start_time)
31,500
43.242978
130
py
sdmgrad
sdmgrad-main/nyuv2/model_segnet_split.py
import torch.nn as nn import torch.optim as optim import torch.nn.functional as F import argparse import torch.utils.data.sampler as sampler from create_dataset import * from utils import * parser = argparse.ArgumentParser(description='Multi-task: Split') parser.add_argument('--type', default='standard', type=str, help='split type: standard, wide, deep') parser.add_argument('--weight', default='equal', type=str, help='multi-task weighting: equal, uncert, dwa') parser.add_argument('--dataroot', default='nyuv2', type=str, help='dataset root') parser.add_argument('--temp', default=2.0, type=float, help='temperature for DWA (must be positive)') parser.add_argument('--seed', default=0, type=int, help='the seed') parser.add_argument('--apply_augmentation', action='store_true', help='toggle to apply data augmentation on NYUv2') opt = parser.parse_args() class SegNet(nn.Module): def __init__(self): super(SegNet, self).__init__() # initialise network parameters if opt.type == 'wide': filter = [64, 128, 256, 512, 1024] else: filter = [64, 128, 256, 512, 512] self.class_nb = 13 # define encoder decoder layers self.encoder_block = nn.ModuleList([self.conv_layer([3, filter[0]])]) self.decoder_block = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): self.encoder_block.append(self.conv_layer([filter[i], filter[i + 1]])) self.decoder_block.append(self.conv_layer([filter[i + 1], filter[i]])) # define convolution layer self.conv_block_enc = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) self.conv_block_dec = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): if i == 0: self.conv_block_enc.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.conv_block_dec.append(self.conv_layer([filter[i], filter[i]])) else: self.conv_block_enc.append( nn.Sequential(self.conv_layer([filter[i + 1], filter[i + 1]]), self.conv_layer([filter[i + 1], filter[i + 1]]))) self.conv_block_dec.append( nn.Sequential(self.conv_layer([filter[i], filter[i]]), self.conv_layer([filter[i], filter[i]]))) # define task specific layers self.pred_task1 = nn.Sequential( nn.Conv2d(in_channels=filter[0], out_channels=filter[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=filter[0], out_channels=self.class_nb, kernel_size=1, padding=0)) self.pred_task2 = nn.Sequential( nn.Conv2d(in_channels=filter[0], out_channels=filter[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=filter[0], out_channels=1, kernel_size=1, padding=0)) self.pred_task3 = nn.Sequential( nn.Conv2d(in_channels=filter[0], out_channels=filter[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=filter[0], out_channels=3, kernel_size=1, padding=0)) # define pooling and unpooling functions self.down_sampling = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True) self.up_sampling = nn.MaxUnpool2d(kernel_size=2, stride=2) self.logsigma = nn.Parameter(torch.FloatTensor([-0.5, -0.5, -0.5])) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) # define convolutional block def conv_layer(self, channel): if opt.type == 'deep': conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(num_features=channel[1]), nn.ReLU(inplace=True), nn.Conv2d(in_channels=channel[1], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(num_features=channel[1]), nn.ReLU(inplace=True), ) else: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(num_features=channel[1]), nn.ReLU(inplace=True)) return conv_block def forward(self, x): import pdb pdb.set_trace() g_encoder, g_decoder, g_maxpool, g_upsampl, indices = ([0] * 5 for _ in range(5)) for i in range(5): g_encoder[i], g_decoder[-i - 1] = ([0] * 2 for _ in range(2)) # global shared encoder-decoder network for i in range(5): if i == 0: g_encoder[i][0] = self.encoder_block[i](x) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) else: g_encoder[i][0] = self.encoder_block[i](g_maxpool[i - 1]) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) for i in range(5): if i == 0: g_upsampl[i] = self.up_sampling(g_maxpool[-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) else: g_upsampl[i] = self.up_sampling(g_decoder[i - 1][-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) # define task prediction layers t1_pred = F.log_softmax(self.pred_task1(g_decoder[i][1]), dim=1) t2_pred = self.pred_task2(g_decoder[i][1]) t3_pred = self.pred_task3(g_decoder[i][1]) t3_pred = t3_pred / torch.norm(t3_pred, p=2, dim=1, keepdim=True) return [t1_pred, t2_pred, t3_pred], self.logsigma # control seed torch.backends.cudnn.enabled = False torch.manual_seed(opt.seed) np.random.seed(opt.seed) random.seed(opt.seed) torch.cuda.manual_seed_all(opt.seed) # define model, optimiser and scheduler device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") SegNet_SPLIT = SegNet().to(device) optimizer = optim.Adam(SegNet_SPLIT.parameters(), lr=1e-4) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.5) print('Parameter Space: ABS: {:.1f}, REL: {:.4f}'.format(count_parameters(SegNet_SPLIT), count_parameters(SegNet_SPLIT) / 24981069)) print( 'LOSS FORMAT: SEMANTIC_LOSS MEAN_IOU PIX_ACC | DEPTH_LOSS ABS_ERR REL_ERR | NORMAL_LOSS MEAN MED <11.25 <22.5 <30') # define dataset dataset_path = opt.dataroot if opt.apply_augmentation: nyuv2_train_set = NYUv2(root=dataset_path, train=True, augmentation=True) print('Applying data augmentation on NYUv2.') else: nyuv2_train_set = NYUv2(root=dataset_path, train=True) print('Standard training strategy without data augmentation.') nyuv2_test_set = NYUv2(root=dataset_path, train=False) batch_size = 2 ###org 2 nyuv2_train_loader = torch.utils.data.DataLoader(dataset=nyuv2_train_set, batch_size=batch_size, shuffle=True) nyuv2_test_loader = torch.utils.data.DataLoader(dataset=nyuv2_test_set, batch_size=batch_size, shuffle=False) import pdb pdb.set_trace() # Train and evaluate multi-task network multi_task_trainer(nyuv2_train_loader, nyuv2_test_loader, SegNet_SPLIT, device, optimizer, scheduler, opt, 200)
7,942
44.649425
119
py
sdmgrad
sdmgrad-main/nyuv2/min_norm_solvers.py
# This code is from # Multi-Task Learning as Multi-Objective Optimization # Ozan Sener, Vladlen Koltun # Neural Information Processing Systems (NeurIPS) 2018 # https://github.com/intel-isl/MultiObjectiveOptimization import numpy as np import torch class MinNormSolver: MAX_ITER = 20 STOP_CRIT = 1e-5 def _min_norm_element_from2(v1v1, v1v2, v2v2): """ Analytical solution for min_{c} |cx_1 + (1-c)x_2|_2^2 d is the distance (objective) optimzed v1v1 = <x1,x1> v1v2 = <x1,x2> v2v2 = <x2,x2> """ if v1v2 >= v1v1: # Case: Fig 1, third column gamma = 0.999 cost = v1v1 return gamma, cost if v1v2 >= v2v2: # Case: Fig 1, first column gamma = 0.001 cost = v2v2 return gamma, cost # Case: Fig 1, second column gamma = -1.0 * ((v1v2 - v2v2) / (v1v1 + v2v2 - 2 * v1v2)) cost = v2v2 + gamma * (v1v2 - v2v2) return gamma, cost def _min_norm_2d(vecs, dps): """ Find the minimum norm solution as combination of two points This is correct only in 2D ie. min_c |\sum c_i x_i|_2^2 st. \sum c_i = 1 , 1 >= c_1 >= 0 for all i, c_i + c_j = 1.0 for some i, j """ dmin = np.inf for i in range(len(vecs)): for j in range(i + 1, len(vecs)): if (i, j) not in dps: dps[(i, j)] = (vecs[i] * vecs[j]).sum().item() dps[(j, i)] = dps[(i, j)] if (i, i) not in dps: dps[(i, i)] = (vecs[i] * vecs[i]).sum().item() if (j, j) not in dps: dps[(j, j)] = (vecs[j] * vecs[j]).sum().item() c, d = MinNormSolver._min_norm_element_from2(dps[(i, i)], dps[(i, j)], dps[(j, j)]) if d < dmin: dmin = d sol = [(i, j), c, d] return sol, dps def _projection2simplex(y): """ Given y, it solves argmin_z |y-z|_2 st \sum z = 1 , 1 >= z_i >= 0 for all i """ m = len(y) sorted_y = np.flip(np.sort(y), axis=0) tmpsum = 0.0 tmax_f = (np.sum(y) - 1.0) / m for i in range(m - 1): tmpsum += sorted_y[i] tmax = (tmpsum - 1) / (i + 1.0) if tmax > sorted_y[i + 1]: tmax_f = tmax break return np.maximum(y - tmax_f, np.zeros(y.shape)) def _next_point(cur_val, grad, n): proj_grad = grad - (np.sum(grad) / n) tm1 = -1.0 * cur_val[proj_grad < 0] / proj_grad[proj_grad < 0] tm2 = (1.0 - cur_val[proj_grad > 0]) / (proj_grad[proj_grad > 0]) skippers = np.sum(tm1 < 1e-7) + np.sum(tm2 < 1e-7) t = 1 if len(tm1[tm1 > 1e-7]) > 0: t = np.min(tm1[tm1 > 1e-7]) if len(tm2[tm2 > 1e-7]) > 0: t = min(t, np.min(tm2[tm2 > 1e-7])) next_point = proj_grad * t + cur_val next_point = MinNormSolver._projection2simplex(next_point) return next_point def find_min_norm_element(vecs): """ Given a list of vectors (vecs), this method finds the minimum norm element in the convex hull as min |u|_2 st. u = \sum c_i vecs[i] and \sum c_i = 1. It is quite geometric, and the main idea is the fact that if d_{ij} = min |u|_2 st u = c x_i + (1-c) x_j; the solution lies in (0, d_{i,j}) Hence, we find the best 2-task solution, and then run the projected gradient descent until convergence """ # Solution lying at the combination of two points dps = {} init_sol, dps = MinNormSolver._min_norm_2d(vecs, dps) n = len(vecs) sol_vec = np.zeros(n) sol_vec[init_sol[0][0]] = init_sol[1] sol_vec[init_sol[0][1]] = 1 - init_sol[1] if n < 3: # This is optimal for n=2, so return the solution return sol_vec, init_sol[2] iter_count = 0 grad_mat = np.zeros((n, n)) for i in range(n): for j in range(n): grad_mat[i, j] = dps[(i, j)] while iter_count < MinNormSolver.MAX_ITER: grad_dir = -1.0 * np.dot(grad_mat, sol_vec) new_point = MinNormSolver._next_point(sol_vec, grad_dir, n) # Re-compute the inner products for line search v1v1 = 0.0 v1v2 = 0.0 v2v2 = 0.0 for i in range(n): for j in range(n): v1v1 += sol_vec[i] * sol_vec[j] * dps[(i, j)] v1v2 += sol_vec[i] * new_point[j] * dps[(i, j)] v2v2 += new_point[i] * new_point[j] * dps[(i, j)] nc, nd = MinNormSolver._min_norm_element_from2(v1v1, v1v2, v2v2) new_sol_vec = nc * sol_vec + (1 - nc) * new_point change = new_sol_vec - sol_vec if np.sum(np.abs(change)) < MinNormSolver.STOP_CRIT: return sol_vec, nd sol_vec = new_sol_vec def find_min_norm_element_FW(vecs): """ Given a list of vectors (vecs), this method finds the minimum norm element in the convex hull as min |u|_2 st. u = \sum c_i vecs[i] and \sum c_i = 1. It is quite geometric, and the main idea is the fact that if d_{ij} = min |u|_2 st u = c x_i + (1-c) x_j; the solution lies in (0, d_{i,j}) Hence, we find the best 2-task solution, and then run the Frank Wolfe until convergence """ # Solution lying at the combination of two points dps = {} init_sol, dps = MinNormSolver._min_norm_2d(vecs, dps) n = len(vecs) sol_vec = np.zeros(n) sol_vec[init_sol[0][0]] = init_sol[1] sol_vec[init_sol[0][1]] = 1 - init_sol[1] if n < 3: # This is optimal for n=2, so return the solution return sol_vec, init_sol[2] iter_count = 0 grad_mat = np.zeros((n, n)) for i in range(n): for j in range(n): grad_mat[i, j] = dps[(i, j)] while iter_count < MinNormSolver.MAX_ITER: t_iter = np.argmin(np.dot(grad_mat, sol_vec)) v1v1 = np.dot(sol_vec, np.dot(grad_mat, sol_vec)) v1v2 = np.dot(sol_vec, grad_mat[:, t_iter]) v2v2 = grad_mat[t_iter, t_iter] nc, nd = MinNormSolver._min_norm_element_from2(v1v1, v1v2, v2v2) new_sol_vec = nc * sol_vec new_sol_vec[t_iter] += 1 - nc change = new_sol_vec - sol_vec if np.sum(np.abs(change)) < MinNormSolver.STOP_CRIT: return sol_vec, nd sol_vec = new_sol_vec def gradient_normalizers(grads, losses, normalization_type): gn = {} if normalization_type == 'l2': for t in grads: gn[t] = np.sqrt(np.sum([gr.pow(2).sum().data[0] for gr in grads[t]])) elif normalization_type == 'loss': for t in grads: gn[t] = losses[t] elif normalization_type == 'loss+': for t in grads: gn[t] = losses[t] * np.sqrt(np.sum([gr.pow(2).sum().data[0] for gr in grads[t]])) elif normalization_type == 'none': for t in grads: gn[t] = 1.0 else: print('ERROR: Invalid Normalization Type') return gn
7,358
35.979899
147
py
sdmgrad
sdmgrad-main/nyuv2/model_segnet_mtan.py
import numpy as np import random import torch.nn as nn import torch.optim as optim import torch.nn.functional as F import argparse import torch.utils.data.sampler as sampler from create_dataset import * from utils import * parser = argparse.ArgumentParser(description='Multi-task: Attention Network') parser.add_argument('--weight', default='equal', type=str, help='multi-task weighting: equal, uncert, dwa') parser.add_argument('--dataroot', default='nyuv2', type=str, help='dataset root') parser.add_argument('--temp', default=2.0, type=float, help='temperature for DWA (must be positive)') parser.add_argument('--seed', default=0, type=int, help='the seed') parser.add_argument('--apply_augmentation', action='store_true', help='toggle to apply data augmentation on NYUv2') opt = parser.parse_args() class SegNet(nn.Module): def __init__(self): super(SegNet, self).__init__() # initialise network parameters filter = [64, 128, 256, 512, 512] self.class_nb = 13 # define encoder decoder layers self.encoder_block = nn.ModuleList([self.conv_layer([3, filter[0]])]) self.decoder_block = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): self.encoder_block.append(self.conv_layer([filter[i], filter[i + 1]])) self.decoder_block.append(self.conv_layer([filter[i + 1], filter[i]])) # define convolution layer self.conv_block_enc = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) self.conv_block_dec = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): if i == 0: self.conv_block_enc.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.conv_block_dec.append(self.conv_layer([filter[i], filter[i]])) else: self.conv_block_enc.append( nn.Sequential(self.conv_layer([filter[i + 1], filter[i + 1]]), self.conv_layer([filter[i + 1], filter[i + 1]]))) self.conv_block_dec.append( nn.Sequential(self.conv_layer([filter[i], filter[i]]), self.conv_layer([filter[i], filter[i]]))) # define task attention layers self.encoder_att = nn.ModuleList([nn.ModuleList([self.att_layer([filter[0], filter[0], filter[0]])])]) self.decoder_att = nn.ModuleList([nn.ModuleList([self.att_layer([2 * filter[0], filter[0], filter[0]])])]) self.encoder_block_att = nn.ModuleList([self.conv_layer([filter[0], filter[1]])]) self.decoder_block_att = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for j in range(3): if j < 2: self.encoder_att.append(nn.ModuleList([self.att_layer([filter[0], filter[0], filter[0]])])) self.decoder_att.append(nn.ModuleList([self.att_layer([2 * filter[0], filter[0], filter[0]])])) for i in range(4): self.encoder_att[j].append(self.att_layer([2 * filter[i + 1], filter[i + 1], filter[i + 1]])) self.decoder_att[j].append(self.att_layer([filter[i + 1] + filter[i], filter[i], filter[i]])) for i in range(4): if i < 3: self.encoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 2]])) self.decoder_block_att.append(self.conv_layer([filter[i + 1], filter[i]])) else: self.encoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.decoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.pred_task1 = self.conv_layer([filter[0], self.class_nb], pred=True) self.pred_task2 = self.conv_layer([filter[0], 1], pred=True) self.pred_task3 = self.conv_layer([filter[0], 3], pred=True) # define pooling and unpooling functions self.down_sampling = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True) self.up_sampling = nn.MaxUnpool2d(kernel_size=2, stride=2) self.logsigma = nn.Parameter(torch.FloatTensor([-0.5, -0.5, -0.5])) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) def conv_layer(self, channel, pred=False): if not pred: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(num_features=channel[1]), nn.ReLU(inplace=True), ) else: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), ) return conv_block def att_layer(self, channel): att_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), nn.BatchNorm2d(channel[1]), nn.ReLU(inplace=True), nn.Conv2d(in_channels=channel[1], out_channels=channel[2], kernel_size=1, padding=0), nn.BatchNorm2d(channel[2]), nn.Sigmoid(), ) return att_block def forward(self, x): g_encoder, g_decoder, g_maxpool, g_upsampl, indices = ([0] * 5 for _ in range(5)) for i in range(5): g_encoder[i], g_decoder[-i - 1] = ([0] * 2 for _ in range(2)) # define attention list for tasks atten_encoder, atten_decoder = ([0] * 3 for _ in range(2)) for i in range(3): atten_encoder[i], atten_decoder[i] = ([0] * 5 for _ in range(2)) for i in range(3): for j in range(5): atten_encoder[i][j], atten_decoder[i][j] = ([0] * 3 for _ in range(2)) # define global shared network for i in range(5): if i == 0: g_encoder[i][0] = self.encoder_block[i](x) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) else: g_encoder[i][0] = self.encoder_block[i](g_maxpool[i - 1]) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) for i in range(5): if i == 0: g_upsampl[i] = self.up_sampling(g_maxpool[-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) else: g_upsampl[i] = self.up_sampling(g_decoder[i - 1][-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) # define task dependent attention module for i in range(3): for j in range(5): if j == 0: atten_encoder[i][j][0] = self.encoder_att[i][j](g_encoder[j][0]) atten_encoder[i][j][1] = (atten_encoder[i][j][0]) * g_encoder[j][1] atten_encoder[i][j][2] = self.encoder_block_att[j](atten_encoder[i][j][1]) atten_encoder[i][j][2] = F.max_pool2d(atten_encoder[i][j][2], kernel_size=2, stride=2) else: atten_encoder[i][j][0] = self.encoder_att[i][j](torch.cat( (g_encoder[j][0], atten_encoder[i][j - 1][2]), dim=1)) atten_encoder[i][j][1] = (atten_encoder[i][j][0]) * g_encoder[j][1] atten_encoder[i][j][2] = self.encoder_block_att[j](atten_encoder[i][j][1]) atten_encoder[i][j][2] = F.max_pool2d(atten_encoder[i][j][2], kernel_size=2, stride=2) for j in range(5): if j == 0: atten_decoder[i][j][0] = F.interpolate(atten_encoder[i][-1][-1], scale_factor=2, mode='bilinear', align_corners=True) atten_decoder[i][j][0] = self.decoder_block_att[-j - 1](atten_decoder[i][j][0]) atten_decoder[i][j][1] = self.decoder_att[i][-j - 1](torch.cat( (g_upsampl[j], atten_decoder[i][j][0]), dim=1)) atten_decoder[i][j][2] = (atten_decoder[i][j][1]) * g_decoder[j][-1] else: atten_decoder[i][j][0] = F.interpolate(atten_decoder[i][j - 1][2], scale_factor=2, mode='bilinear', align_corners=True) atten_decoder[i][j][0] = self.decoder_block_att[-j - 1](atten_decoder[i][j][0]) atten_decoder[i][j][1] = self.decoder_att[i][-j - 1](torch.cat( (g_upsampl[j], atten_decoder[i][j][0]), dim=1)) atten_decoder[i][j][2] = (atten_decoder[i][j][1]) * g_decoder[j][-1] # define task prediction layers t1_pred = F.log_softmax(self.pred_task1(atten_decoder[0][-1][-1]), dim=1) t2_pred = self.pred_task2(atten_decoder[1][-1][-1]) t3_pred = self.pred_task3(atten_decoder[2][-1][-1]) t3_pred = t3_pred / torch.norm(t3_pred, p=2, dim=1, keepdim=True) return [t1_pred, t2_pred, t3_pred], self.logsigma # control seed torch.backends.cudnn.enabled = False torch.manual_seed(opt.seed) np.random.seed(opt.seed) random.seed(opt.seed) torch.cuda.manual_seed_all(opt.seed) # define model, optimiser and scheduler device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") SegNet_MTAN = SegNet().to(device) optimizer = optim.Adam(SegNet_MTAN.parameters(), lr=1e-4) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.5) print('Parameter Space: ABS: {:.1f}, REL: {:.4f}'.format(count_parameters(SegNet_MTAN), count_parameters(SegNet_MTAN) / 24981069)) print( 'LOSS FORMAT: SEMANTIC_LOSS MEAN_IOU PIX_ACC | DEPTH_LOSS ABS_ERR REL_ERR | NORMAL_LOSS MEAN MED <11.25 <22.5 <30') # define dataset dataset_path = opt.dataroot if opt.apply_augmentation: nyuv2_train_set = NYUv2(root=dataset_path, train=True, augmentation=True) print('Applying data augmentation on NYUv2.') else: nyuv2_train_set = NYUv2(root=dataset_path, train=True) print('Standard training strategy without data augmentation.') nyuv2_test_set = NYUv2(root=dataset_path, train=False) batch_size = 2 nyuv2_train_loader = torch.utils.data.DataLoader(dataset=nyuv2_train_set, batch_size=batch_size, shuffle=True) nyuv2_test_loader = torch.utils.data.DataLoader(dataset=nyuv2_test_set, batch_size=batch_size, shuffle=False) # Train and evaluate multi-task network multi_task_trainer(nyuv2_train_loader, nyuv2_test_loader, SegNet_MTAN, device, optimizer, scheduler, opt, 200)
11,617
49.077586
119
py
sdmgrad
sdmgrad-main/nyuv2/create_dataset.py
from torch.utils.data.dataset import Dataset import os import torch import torch.nn.functional as F import fnmatch import numpy as np import random class RandomScaleCrop(object): """ Credit to Jialong Wu from https://github.com/lorenmt/mtan/issues/34. """ def __init__(self, scale=[1.0, 1.2, 1.5]): self.scale = scale def __call__(self, img, label, depth, normal): height, width = img.shape[-2:] sc = self.scale[random.randint(0, len(self.scale) - 1)] h, w = int(height / sc), int(width / sc) i = random.randint(0, height - h) j = random.randint(0, width - w) img_ = F.interpolate(img[None, :, i:i + h, j:j + w], size=(height, width), mode='bilinear', align_corners=True).squeeze(0) label_ = F.interpolate(label[None, None, i:i + h, j:j + w], size=(height, width), mode='nearest').squeeze(0).squeeze(0) depth_ = F.interpolate(depth[None, :, i:i + h, j:j + w], size=(height, width), mode='nearest').squeeze(0) normal_ = F.interpolate(normal[None, :, i:i + h, j:j + w], size=(height, width), mode='bilinear', align_corners=True).squeeze(0) return img_, label_, depth_ / sc, normal_ class NYUv2(Dataset): """ We could further improve the performance with the data augmentation of NYUv2 defined in: [1] PAD-Net: Multi-Tasks Guided Prediction-and-Distillation Network for Simultaneous Depth Estimation and Scene Parsing [2] Pattern affinitive propagation across depth, surface normal and semantic segmentation [3] Mti-net: Multiscale task interaction networks for multi-task learning 1. Random scale in a selected raio 1.0, 1.2, and 1.5. 2. Random horizontal flip. Please note that: all baselines and MTAN did NOT apply data augmentation in the original paper. """ def __init__(self, root, train=True, augmentation=False): self.train = train self.root = os.path.expanduser(root) self.augmentation = augmentation # read the data file if train: self.data_path = root + '/train' else: self.data_path = root + '/val' # calculate data length self.data_len = len(fnmatch.filter(os.listdir(self.data_path + '/image'), '*.npy')) def __getitem__(self, index): # load data from the pre-processed npy files image = torch.from_numpy(np.moveaxis(np.load(self.data_path + '/image/{:d}.npy'.format(index)), -1, 0)) semantic = torch.from_numpy(np.load(self.data_path + '/label/{:d}.npy'.format(index))) depth = torch.from_numpy(np.moveaxis(np.load(self.data_path + '/depth/{:d}.npy'.format(index)), -1, 0)) normal = torch.from_numpy(np.moveaxis(np.load(self.data_path + '/normal/{:d}.npy'.format(index)), -1, 0)) # apply data augmentation if required if self.augmentation: image, semantic, depth, normal = RandomScaleCrop()(image, semantic, depth, normal) if torch.rand(1) < 0.5: image = torch.flip(image, dims=[2]) semantic = torch.flip(semantic, dims=[1]) depth = torch.flip(depth, dims=[2]) normal = torch.flip(normal, dims=[2]) normal[0, :, :] = -normal[0, :, :] return image.float(), semantic.float(), depth.float(), normal.float() def __len__(self): return self.data_len
3,568
40.988235
127
py
sdmgrad
sdmgrad-main/nyuv2/model_segnet_cross.py
import numpy as np import random import torch.nn as nn import torch.optim as optim import torch.nn.functional as F import argparse import torch.utils.data.sampler as sampler from create_dataset import * from utils import * parser = argparse.ArgumentParser(description='Multi-task: Cross') parser.add_argument('--weight', default='equal', type=str, help='multi-task weighting: equal, uncert, dwa') parser.add_argument('--dataroot', default='nyuv2', type=str, help='dataset root') parser.add_argument('--temp', default=2.0, type=float, help='temperature for DWA (must be positive)') parser.add_argument('--seed', default=0, type=int, help='the seed') parser.add_argument('--apply_augmentation', action='store_true', help='toggle to apply data augmentation on NYUv2') opt = parser.parse_args() class SegNet(nn.Module): def __init__(self): super(SegNet, self).__init__() # initialise network parameters filter = [64, 128, 256, 512, 512] self.class_nb = 13 # define encoder decoder layers self.encoder_block_t = nn.ModuleList( [nn.ModuleList([self.conv_layer([3, filter[0], filter[0]], bottle_neck=True)])]) self.decoder_block_t = nn.ModuleList( [nn.ModuleList([self.conv_layer([filter[0], filter[0], filter[0]], bottle_neck=True)])]) for j in range(3): if j < 2: self.encoder_block_t.append( nn.ModuleList([self.conv_layer([3, filter[0], filter[0]], bottle_neck=True)])) self.decoder_block_t.append( nn.ModuleList([self.conv_layer([filter[0], filter[0], filter[0]], bottle_neck=True)])) for i in range(4): if i == 0: self.encoder_block_t[j].append( self.conv_layer([filter[i], filter[i + 1], filter[i + 1]], bottle_neck=True)) self.decoder_block_t[j].append( self.conv_layer([filter[i + 1], filter[i], filter[i]], bottle_neck=True)) else: self.encoder_block_t[j].append( self.conv_layer([filter[i], filter[i + 1], filter[i + 1]], bottle_neck=False)) self.decoder_block_t[j].append( self.conv_layer([filter[i + 1], filter[i], filter[i]], bottle_neck=False)) # define cross-stitch units self.cs_unit_encoder = nn.Parameter(data=torch.ones(4, 3)) self.cs_unit_decoder = nn.Parameter(data=torch.ones(5, 3)) # define task specific layers self.pred_task1 = self.conv_layer([filter[0], self.class_nb], bottle_neck=True, pred_layer=True) self.pred_task2 = self.conv_layer([filter[0], 1], bottle_neck=True, pred_layer=True) self.pred_task3 = self.conv_layer([filter[0], 3], bottle_neck=True, pred_layer=True) # define pooling and unpooling functions self.down_sampling = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True) self.up_sampling = nn.MaxUnpool2d(kernel_size=2, stride=2) self.logsigma = nn.Parameter(torch.FloatTensor([-0.5, -0.5, -0.5])) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.xavier_uniform_(m.weight) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.xavier_uniform_(m.weight) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Parameter): nn.init.constant(m.weight, 1) def conv_layer(self, channel, bottle_neck, pred_layer=False): if bottle_neck: if not pred_layer: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(channel[1]), nn.ReLU(inplace=True), nn.Conv2d(in_channels=channel[1], out_channels=channel[2], kernel_size=3, padding=1), nn.BatchNorm2d(channel[2]), nn.ReLU(inplace=True), ) else: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), ) else: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(channel[1]), nn.ReLU(inplace=True), nn.Conv2d(in_channels=channel[1], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(channel[1]), nn.ReLU(inplace=True), nn.Conv2d(in_channels=channel[1], out_channels=channel[2], kernel_size=3, padding=1), nn.BatchNorm2d(channel[2]), nn.ReLU(inplace=True), ) return conv_block def forward(self, x): encoder_conv_t, decoder_conv_t, encoder_samp_t, decoder_samp_t, indices_t = ([0] * 3 for _ in range(5)) for i in range(3): encoder_conv_t[i], decoder_conv_t[i], encoder_samp_t[i], decoder_samp_t[i], indices_t[i] = ( [0] * 5 for _ in range(5)) # task branch 1 for i in range(5): for j in range(3): if i == 0: encoder_conv_t[j][i] = self.encoder_block_t[j][i](x) encoder_samp_t[j][i], indices_t[j][i] = self.down_sampling(encoder_conv_t[j][i]) else: encoder_cross_stitch = self.cs_unit_encoder[i - 1][0] * encoder_samp_t[0][i - 1] + \ self.cs_unit_encoder[i - 1][1] * encoder_samp_t[1][i - 1] + \ self.cs_unit_encoder[i - 1][2] * encoder_samp_t[2][i - 1] encoder_conv_t[j][i] = self.encoder_block_t[j][i](encoder_cross_stitch) encoder_samp_t[j][i], indices_t[j][i] = self.down_sampling(encoder_conv_t[j][i]) for i in range(5): for j in range(3): if i == 0: decoder_cross_stitch = self.cs_unit_decoder[i][0] * encoder_samp_t[0][-1] + \ self.cs_unit_decoder[i][1] * encoder_samp_t[1][-1] + \ self.cs_unit_decoder[i][2] * encoder_samp_t[2][-1] decoder_samp_t[j][i] = self.up_sampling(decoder_cross_stitch, indices_t[j][-i - 1]) decoder_conv_t[j][i] = self.decoder_block_t[j][-i - 1](decoder_samp_t[j][i]) else: decoder_cross_stitch = self.cs_unit_decoder[i][0] * decoder_conv_t[0][i - 1] + \ self.cs_unit_decoder[i][1] * decoder_conv_t[1][i - 1] + \ self.cs_unit_decoder[i][2] * decoder_conv_t[2][i - 1] decoder_samp_t[j][i] = self.up_sampling(decoder_cross_stitch, indices_t[j][-i - 1]) decoder_conv_t[j][i] = self.decoder_block_t[j][-i - 1](decoder_samp_t[j][i]) # define task prediction layers t1_pred = F.log_softmax(self.pred_task1(decoder_conv_t[0][-1]), dim=1) t2_pred = self.pred_task2(decoder_conv_t[1][-1]) t3_pred = self.pred_task3(decoder_conv_t[2][-1]) t3_pred = t3_pred / torch.norm(t3_pred, p=2, dim=1, keepdim=True) return [t1_pred, t2_pred, t3_pred], self.logsigma # control seed torch.backends.cudnn.enabled = False torch.manual_seed(opt.seed) np.random.seed(opt.seed) random.seed(opt.seed) torch.cuda.manual_seed_all(opt.seed) # define model, optimiser and scheduler device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") SegNet_CROSS = SegNet().to(device) optimizer = optim.Adam(SegNet_CROSS.parameters(), lr=1e-4) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.5) print('Parameter Space: ABS: {:.1f}, REL: {:.4f}'.format(count_parameters(SegNet_CROSS), count_parameters(SegNet_CROSS) / 24981069)) print( 'LOSS FORMAT: SEMANTIC_LOSS MEAN_IOU PIX_ACC | DEPTH_LOSS ABS_ERR REL_ERR | NORMAL_LOSS MEAN MED <11.25 <22.5 <30') # define dataset dataset_path = opt.dataroot if opt.apply_augmentation: nyuv2_train_set = NYUv2(root=dataset_path, train=True, augmentation=True) print('Applying data augmentation on NYUv2.') else: nyuv2_train_set = NYUv2(root=dataset_path, train=True) print('Standard training strategy without data augmentation.') nyuv2_test_set = NYUv2(root=dataset_path, train=False) batch_size = 2 nyuv2_train_loader = torch.utils.data.DataLoader(dataset=nyuv2_train_set, batch_size=batch_size, shuffle=True) nyuv2_test_loader = torch.utils.data.DataLoader(dataset=nyuv2_test_set, batch_size=batch_size, shuffle=False) # Train and evaluate multi-task network multi_task_trainer(nyuv2_train_loader, nyuv2_test_loader, SegNet_CROSS, device, optimizer, scheduler, opt, 200)
9,335
47.879581
119
py
sdmgrad
sdmgrad-main/nyuv2/model_segnet_mt.py
import numpy as np import random import torch.nn as nn import torch.optim as optim import torch.nn.functional as F import argparse import torch.utils.data.sampler as sampler from create_dataset import * from utils import * parser = argparse.ArgumentParser(description='Multi-task: Split') parser.add_argument('--type', default='standard', type=str, help='split type: standard, wide, deep') parser.add_argument('--weight', default='equal', type=str, help='multi-task weighting: equal, uncert, dwa') parser.add_argument('--dataroot', default='nyuv2', type=str, help='dataset root') parser.add_argument('--method', default='sdmgrad', type=str, help='optimization method') parser.add_argument('--temp', default=2.0, type=float, help='temperature for DWA (must be positive)') parser.add_argument('--alpha', default=0.3, type=float, help='the alpha') parser.add_argument('--lr', default=1e-4, type=float, help='the learning rate') parser.add_argument('--seed', default=1, type=int, help='the seed') parser.add_argument('--niter', default=20, type=int, help='number of inner iteration') parser.add_argument('--apply_augmentation', action='store_true', help='toggle to apply data augmentation on NYUv2') opt = parser.parse_args() class SegNet(nn.Module): def __init__(self): super(SegNet, self).__init__() # initialise network parameters filter = [64, 128, 256, 512, 512] self.class_nb = 13 # define encoder decoder layers self.encoder_block = nn.ModuleList([self.conv_layer([3, filter[0]])]) self.decoder_block = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): self.encoder_block.append(self.conv_layer([filter[i], filter[i + 1]])) self.decoder_block.append(self.conv_layer([filter[i + 1], filter[i]])) # define convolution layer self.conv_block_enc = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) self.conv_block_dec = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): if i == 0: self.conv_block_enc.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.conv_block_dec.append(self.conv_layer([filter[i], filter[i]])) else: self.conv_block_enc.append( nn.Sequential(self.conv_layer([filter[i + 1], filter[i + 1]]), self.conv_layer([filter[i + 1], filter[i + 1]]))) self.conv_block_dec.append( nn.Sequential(self.conv_layer([filter[i], filter[i]]), self.conv_layer([filter[i], filter[i]]))) # define task attention layers self.encoder_att = nn.ModuleList([nn.ModuleList([self.att_layer([filter[0], filter[0], filter[0]])])]) self.decoder_att = nn.ModuleList([nn.ModuleList([self.att_layer([2 * filter[0], filter[0], filter[0]])])]) self.encoder_block_att = nn.ModuleList([self.conv_layer([filter[0], filter[1]])]) self.decoder_block_att = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for j in range(3): if j < 2: self.encoder_att.append(nn.ModuleList([self.att_layer([filter[0], filter[0], filter[0]])])) self.decoder_att.append(nn.ModuleList([self.att_layer([2 * filter[0], filter[0], filter[0]])])) for i in range(4): self.encoder_att[j].append(self.att_layer([2 * filter[i + 1], filter[i + 1], filter[i + 1]])) self.decoder_att[j].append(self.att_layer([filter[i + 1] + filter[i], filter[i], filter[i]])) for i in range(4): if i < 3: self.encoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 2]])) self.decoder_block_att.append(self.conv_layer([filter[i + 1], filter[i]])) else: self.encoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.decoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.pred_task1 = self.conv_layer([filter[0], self.class_nb], pred=True) self.pred_task2 = self.conv_layer([filter[0], 1], pred=True) self.pred_task3 = self.conv_layer([filter[0], 3], pred=True) # define pooling and unpooling functions self.down_sampling = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True) self.up_sampling = nn.MaxUnpool2d(kernel_size=2, stride=2) self.logsigma = nn.Parameter(torch.FloatTensor([-0.5, -0.5, -0.5])) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) def shared_modules(self): return [ self.encoder_block, self.decoder_block, self.conv_block_enc, self.conv_block_dec, #self.encoder_att, self.decoder_att, self.encoder_block_att, self.decoder_block_att, self.down_sampling, self.up_sampling ] def zero_grad_shared_modules(self): for mm in self.shared_modules(): mm.zero_grad() def conv_layer(self, channel, pred=False): if not pred: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(num_features=channel[1]), nn.ReLU(inplace=True), ) else: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), ) return conv_block def att_layer(self, channel): att_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), nn.BatchNorm2d(channel[1]), nn.ReLU(inplace=True), nn.Conv2d(in_channels=channel[1], out_channels=channel[2], kernel_size=1, padding=0), nn.BatchNorm2d(channel[2]), nn.Sigmoid(), ) return att_block def forward(self, x): g_encoder, g_decoder, g_maxpool, g_upsampl, indices = ([0] * 5 for _ in range(5)) for i in range(5): g_encoder[i], g_decoder[-i - 1] = ([0] * 2 for _ in range(2)) # define attention list for tasks atten_encoder, atten_decoder = ([0] * 3 for _ in range(2)) for i in range(3): atten_encoder[i], atten_decoder[i] = ([0] * 5 for _ in range(2)) for i in range(3): for j in range(5): atten_encoder[i][j], atten_decoder[i][j] = ([0] * 3 for _ in range(2)) # define global shared network for i in range(5): if i == 0: g_encoder[i][0] = self.encoder_block[i](x) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) else: g_encoder[i][0] = self.encoder_block[i](g_maxpool[i - 1]) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) for i in range(5): if i == 0: g_upsampl[i] = self.up_sampling(g_maxpool[-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) else: g_upsampl[i] = self.up_sampling(g_decoder[i - 1][-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) # define task dependent attention module for i in range(3): for j in range(5): if j == 0: atten_encoder[i][j][0] = self.encoder_att[i][j](g_encoder[j][0]) atten_encoder[i][j][1] = (atten_encoder[i][j][0]) * g_encoder[j][1] atten_encoder[i][j][2] = self.encoder_block_att[j](atten_encoder[i][j][1]) atten_encoder[i][j][2] = F.max_pool2d(atten_encoder[i][j][2], kernel_size=2, stride=2) else: atten_encoder[i][j][0] = self.encoder_att[i][j](torch.cat( (g_encoder[j][0], atten_encoder[i][j - 1][2]), dim=1)) atten_encoder[i][j][1] = (atten_encoder[i][j][0]) * g_encoder[j][1] atten_encoder[i][j][2] = self.encoder_block_att[j](atten_encoder[i][j][1]) atten_encoder[i][j][2] = F.max_pool2d(atten_encoder[i][j][2], kernel_size=2, stride=2) for j in range(5): if j == 0: atten_decoder[i][j][0] = F.interpolate(atten_encoder[i][-1][-1], scale_factor=2, mode='bilinear', align_corners=True) atten_decoder[i][j][0] = self.decoder_block_att[-j - 1](atten_decoder[i][j][0]) atten_decoder[i][j][1] = self.decoder_att[i][-j - 1](torch.cat( (g_upsampl[j], atten_decoder[i][j][0]), dim=1)) atten_decoder[i][j][2] = (atten_decoder[i][j][1]) * g_decoder[j][-1] else: atten_decoder[i][j][0] = F.interpolate(atten_decoder[i][j - 1][2], scale_factor=2, mode='bilinear', align_corners=True) atten_decoder[i][j][0] = self.decoder_block_att[-j - 1](atten_decoder[i][j][0]) atten_decoder[i][j][1] = self.decoder_att[i][-j - 1](torch.cat( (g_upsampl[j], atten_decoder[i][j][0]), dim=1)) atten_decoder[i][j][2] = (atten_decoder[i][j][1]) * g_decoder[j][-1] # define task prediction layers t1_pred = F.log_softmax(self.pred_task1(atten_decoder[0][-1][-1]), dim=1) t2_pred = self.pred_task2(atten_decoder[1][-1][-1]) t3_pred = self.pred_task3(atten_decoder[2][-1][-1]) t3_pred = t3_pred / torch.norm(t3_pred, p=2, dim=1, keepdim=True) return [t1_pred, t2_pred, t3_pred], self.logsigma class SegNetSplit(nn.Module): def __init__(self): super(SegNetSplit, self).__init__() # initialise network parameters if opt.type == 'wide': filter = [64, 128, 256, 512, 1024] else: filter = [64, 128, 256, 512, 512] self.class_nb = 13 # define encoder decoder layers self.encoder_block = nn.ModuleList([self.conv_layer([3, filter[0]])]) self.decoder_block = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): self.encoder_block.append(self.conv_layer([filter[i], filter[i + 1]])) self.decoder_block.append(self.conv_layer([filter[i + 1], filter[i]])) # define convolution layer self.conv_block_enc = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) self.conv_block_dec = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): if i == 0: self.conv_block_enc.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.conv_block_dec.append(self.conv_layer([filter[i], filter[i]])) else: self.conv_block_enc.append( nn.Sequential(self.conv_layer([filter[i + 1], filter[i + 1]]), self.conv_layer([filter[i + 1], filter[i + 1]]))) self.conv_block_dec.append( nn.Sequential(self.conv_layer([filter[i], filter[i]]), self.conv_layer([filter[i], filter[i]]))) # define task specific layers self.pred_task1 = nn.Sequential( nn.Conv2d(in_channels=filter[0], out_channels=filter[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=filter[0], out_channels=self.class_nb, kernel_size=1, padding=0)) self.pred_task2 = nn.Sequential( nn.Conv2d(in_channels=filter[0], out_channels=filter[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=filter[0], out_channels=1, kernel_size=1, padding=0)) self.pred_task3 = nn.Sequential( nn.Conv2d(in_channels=filter[0], out_channels=filter[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=filter[0], out_channels=3, kernel_size=1, padding=0)) # define pooling and unpooling functions self.down_sampling = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True) self.up_sampling = nn.MaxUnpool2d(kernel_size=2, stride=2) self.logsigma = nn.Parameter(torch.FloatTensor([-0.5, -0.5, -0.5])) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) # define convolutional block def conv_layer(self, channel): if opt.type == 'deep': conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(num_features=channel[1]), nn.ReLU(inplace=True), nn.Conv2d(in_channels=channel[1], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(num_features=channel[1]), nn.ReLU(inplace=True), ) else: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(num_features=channel[1]), nn.ReLU(inplace=True)) return conv_block def forward(self, x): g_encoder, g_decoder, g_maxpool, g_upsampl, indices = ([0] * 5 for _ in range(5)) for i in range(5): g_encoder[i], g_decoder[-i - 1] = ([0] * 2 for _ in range(2)) # global shared encoder-decoder network for i in range(5): if i == 0: g_encoder[i][0] = self.encoder_block[i](x) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) else: g_encoder[i][0] = self.encoder_block[i](g_maxpool[i - 1]) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) for i in range(5): if i == 0: g_upsampl[i] = self.up_sampling(g_maxpool[-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) else: g_upsampl[i] = self.up_sampling(g_decoder[i - 1][-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) # define task prediction layers t1_pred = F.log_softmax(self.pred_task1(g_decoder[i][1]), dim=1) t2_pred = self.pred_task2(g_decoder[i][1]) t3_pred = self.pred_task3(g_decoder[i][1]) t3_pred = t3_pred / torch.norm(t3_pred, p=2, dim=1, keepdim=True) return [t1_pred, t2_pred, t3_pred], self.logsigma # control seed torch.backends.cudnn.enabled = False torch.manual_seed(opt.seed) np.random.seed(opt.seed) random.seed(opt.seed) torch.cuda.manual_seed_all(opt.seed) # define model, optimiser and scheduler device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") SegNet_MTAN = SegNet().to(device) optimizer = optim.Adam(SegNet_MTAN.parameters(), lr=opt.lr) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.5) print('Parameter Space: ABS: {:.1f}, REL: {:.4f}'.format(count_parameters(SegNet_MTAN), count_parameters(SegNet_MTAN) / 24981069)) print( 'LOSS FORMAT: SEMANTIC_LOSS MEAN_IOU PIX_ACC | DEPTH_LOSS ABS_ERR REL_ERR | NORMAL_LOSS MEAN MED <11.25 <22.5 <30') # define dataset dataset_path = opt.dataroot if opt.apply_augmentation: nyuv2_train_set = NYUv2(root=dataset_path, train=True, augmentation=True) print('Applying data augmentation on NYUv2.') else: nyuv2_train_set = NYUv2(root=dataset_path, train=True) print('Standard training strategy without data augmentation.') nyuv2_test_set = NYUv2(root=dataset_path, train=False) batch_size = 2 nyuv2_train_loader = torch.utils.data.DataLoader(dataset=nyuv2_train_set, batch_size=batch_size, shuffle=True) nyuv2_test_loader = torch.utils.data.DataLoader(dataset=nyuv2_test_set, batch_size=batch_size, shuffle=False) # Train and evaluate multi-task network multi_task_mgd_trainer(nyuv2_train_loader, nyuv2_test_loader, SegNet_MTAN, device, optimizer, scheduler, opt, 200, opt.method, opt.alpha, opt.seed)
18,041
48.027174
119
py
sdmgrad
sdmgrad-main/consistency/model_resnet.py
# resnet18 base model for Pareto MTL import torch import torch.nn as nn import torch.nn.functional as F from torch.nn.modules.loss import CrossEntropyLoss from torchvision import models class RegressionTrainResNet(torch.nn.Module): def __init__(self, model, init_weight): super(RegressionTrainResNet, self).__init__() self.model = model self.weights = torch.nn.Parameter(torch.from_numpy(init_weight).float()) self.ce_loss = CrossEntropyLoss() def forward(self, x, ts): n_tasks = 2 ys = self.model(x) task_loss = [] for i in range(n_tasks): task_loss.append(self.ce_loss(ys[:, i], ts[:, i])) task_loss = torch.stack(task_loss) return task_loss class MnistResNet(torch.nn.Module): def __init__(self, n_tasks): super(MnistResNet, self).__init__() self.n_tasks = n_tasks self.feature_extractor = models.resnet18(pretrained=False) self.feature_extractor.conv1 = torch.nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False) fc_in_features = self.feature_extractor.fc.in_features self.feature_extractor.fc = torch.nn.Linear(fc_in_features, 100) self.ce_loss = CrossEntropyLoss() for i in range(self.n_tasks): setattr(self, 'task_{}'.format(i), nn.Linear(100, 10)) def shared_modules(self): return [self.feature_extractor] def zero_grad_shared_modules(self): for mm in self.shared_modules(): mm.zero_grad() def forward(self, x): x = F.relu(self.feature_extractor(x)) outs = [] for i in range(self.n_tasks): layer = getattr(self, 'task_{}'.format(i)) outs.append(layer(x)) return torch.stack(outs, dim=1) def forward_loss(self, x, ts): ys = self.forward(x) task_loss = [] for i in range(self.n_tasks): task_loss.append(self.ce_loss(ys[:, i], ts[:, i])) task_loss = torch.stack(task_loss) return task_loss
2,346
31.150685
80
py
sdmgrad
sdmgrad-main/consistency/utils.py
import numpy as np from min_norm_solvers import MinNormSolver from scipy.optimize import minimize, Bounds, minimize_scalar import torch from torch import linalg as LA from torch.nn import functional as F def euclidean_proj_simplex(v, s=1): """ Compute the Euclidean projection on a positive simplex Solves the optimisation problem (using the algorithm from [1]): min_w 0.5 * || w - v ||_2^2 , s.t. \sum_i w_i = s, w_i >= 0 Parameters ---------- v: (n,) numpy array, n-dimensional vector to project s: int, optional, default: 1, radius of the simplex Returns ------- w: (n,) numpy array, Euclidean projection of v on the simplex Notes ----- The complexity of this algorithm is in O(n log(n)) as it involves sorting v. Better alternatives exist for high-dimensional sparse vectors (cf. [1]) However, this implementation still easily scales to millions of dimensions. References ---------- [1] Efficient Projections onto the .1-Ball for Learning in High Dimensions John Duchi, Shai Shalev-Shwartz, Yoram Singer, and Tushar Chandra. International Conference on Machine Learning (ICML 2008) http://www.cs.berkeley.edu/~jduchi/projects/DuchiSiShCh08.pdf [2] Projection onto the probability simplex: An efficient algorithm with a simple proof, and an application Weiran Wang, Miguel Á. Carreira-Perpiñán. arXiv:1309.1541 https://arxiv.org/pdf/1309.1541.pdf [3] https://gist.github.com/daien/1272551/edd95a6154106f8e28209a1c7964623ef8397246#file-simplex_projection-py """ assert s > 0, "Radius s must be strictly positive (%d <= 0)" % s v = v.astype(np.float64) n, = v.shape # will raise ValueError if v is not 1-D # check if we are already on the simplex if v.sum() == s and np.alltrue(v >= 0): # best projection: itself! return v # get the array of cumulative sums of a sorted (decreasing) copy of v u = np.sort(v)[::-1] cssv = np.cumsum(u) # get the number of > 0 components of the optimal solution rho = np.nonzero(u * np.arange(1, n + 1) > (cssv - s))[0][-1] # compute the Lagrange multiplier associated to the simplex constraint theta = float(cssv[rho] - s) / (rho + 1) # compute the projection by thresholding v using theta w = (v - theta).clip(min=0) return w def grad2vec(m, grads, grad_dims, task): # store the gradients grads[:, task].fill_(0.0) cnt = 0 for mm in m.shared_modules(): for p in mm.parameters(): grad = p.grad if grad is not None: grad_cur = grad.data.detach().clone() beg = 0 if cnt == 0 else sum(grad_dims[:cnt]) en = sum(grad_dims[:cnt + 1]) grads[beg:en, task].copy_(grad_cur.data.view(-1)) cnt += 1 def overwrite_grad(m, newgrad, grad_dims): # newgrad = newgrad * 2 # to match the sum loss cnt = 0 for mm in m.shared_modules(): for param in mm.parameters(): beg = 0 if cnt == 0 else sum(grad_dims[:cnt]) en = sum(grad_dims[:cnt + 1]) this_grad = newgrad[beg:en].contiguous().view(param.data.size()) param.grad = this_grad.data.clone() cnt += 1 def mean_grad(grads): return grads.mean(1) def mgd(grads): grads_cpu = grads.t().cpu() sol, min_norm = MinNormSolver.find_min_norm_element([grads_cpu[t] for t in range(grads.shape[-1])]) w = torch.FloatTensor(sol).to(grads.device) g = grads.mm(w.view(-1, 1)).view(-1) return g def cagrad(grads, alpha=0.5, rescale=0): g1 = grads[:, 0] g2 = grads[:, 1] g11 = g1.dot(g1).item() g12 = g1.dot(g2).item() g22 = g2.dot(g2).item() g0_norm = 0.5 * np.sqrt(g11 + g22 + 2 * g12) # want to minimize g_w^Tg_0 + c*||g_0||*||g_w|| coef = alpha * g0_norm def obj(x): # g_w^T g_0: x*0.5*(g11+g22-2g12)+(0.5+x)*(g12-g22)+g22 # g_w^T g_w: x^2*(g11+g22-2g12)+2*x*(g12-g22)+g22 return coef * np.sqrt(x**2 * (g11 + g22 - 2 * g12) + 2 * x * (g12 - g22) + g22 + 1e-8) + 0.5 * x * (g11 + g22 - 2 * g12) + (0.5 + x) * (g12 - g22) + g22 res = minimize_scalar(obj, bounds=(0, 1), method='bounded') x = res.x gw_norm = np.sqrt(x**2 * g11 + (1 - x)**2 * g22 + 2 * x * (1 - x) * g12 + 1e-8) lmbda = coef / (gw_norm + 1e-8) g = (0.5 + lmbda * x) * g1 + (0.5 + lmbda * (1 - x)) * g2 # g0 + lmbda*gw if rescale == 0: return g elif rescale == 1: return g / (1 + alpha**2) else: return g / (1 + alpha) def sdmgrad(w, grads, lmbda, niter=20): """ our proposed sdmgrad """ GG = torch.mm(grads.t(), grads) scale = torch.mean(torch.sqrt(torch.diag(GG) + 1e-4)) GG = GG / scale.pow(2) Gg = torch.mean(GG, dim=1) gg = torch.mean(Gg) w.requires_grad = True optimizer = torch.optim.SGD([w], lr=10, momentum=0.5) for i in range(niter): optimizer.zero_grad() obj = torch.dot(w, torch.mv(GG, w)) + 2 * lmbda * torch.dot(w, Gg) + lmbda**2 * gg obj.backward() optimizer.step() proj = euclidean_proj_simplex(w.data.cpu().numpy()) w.data.copy_(torch.from_numpy(proj).data) w.requires_grad = False g0 = torch.mean(grads, dim=1) gw = torch.mv(grads, w) g = (gw + lmbda * g0) / (1 + lmbda)
5,435
34.070968
113
py
sdmgrad
sdmgrad-main/consistency/model_lenet.py
# lenet base model for Pareto MTL import torch import torch.nn as nn import torch.nn.functional as F from torch.nn.modules.loss import CrossEntropyLoss class RegressionTrain(torch.nn.Module): def __init__(self, model, init_weight): super(RegressionTrain, self).__init__() self.model = model self.weights = torch.nn.Parameter(torch.from_numpy(init_weight).float()) self.ce_loss = CrossEntropyLoss() def forward(self, x, ts): n_tasks = 2 ys = self.model(x) task_loss = [] for i in range(n_tasks): task_loss.append(self.ce_loss(ys[:, i], ts[:, i])) task_loss = torch.stack(task_loss) return task_loss class RegressionModel(torch.nn.Module): def __init__(self, n_tasks): super(RegressionModel, self).__init__() self.n_tasks = n_tasks self.conv1 = nn.Conv2d(1, 10, 9, 1) self.conv2 = nn.Conv2d(10, 20, 5, 1) self.fc1 = nn.Linear(5 * 5 * 20, 50) self.ce_loss = CrossEntropyLoss() for i in range(self.n_tasks): setattr(self, 'task_{}'.format(i), nn.Linear(50, 10)) def shared_modules(self): return [self.conv1, self.conv2, self.fc1] def zero_grad_shared_modules(self): for mm in self.shared_modules(): mm.zero_grad() def forward(self, x): x = F.relu(self.conv1(x)) x = F.max_pool2d(x, 2, 2) x = F.relu(self.conv2(x)) x = F.max_pool2d(x, 2, 2) x = x.view(-1, 5 * 5 * 20) x = F.relu(self.fc1(x)) outs = [] for i in range(self.n_tasks): layer = getattr(self, 'task_{}'.format(i)) outs.append(layer(x)) return torch.stack(outs, dim=1) def forward_loss(self, x, ts): ys = self.forward(x) task_loss = [] for i in range(self.n_tasks): task_loss.append(self.ce_loss(ys[:, i], ts[:, i])) task_loss = torch.stack(task_loss) return task_loss
2,006
26.875
80
py
sdmgrad
sdmgrad-main/consistency/min_norm_solvers.py
# This code is from # Multi-Task Learning as Multi-Objective Optimization # Ozan Sener, Vladlen Koltun # Neural Information Processing Systems (NeurIPS) 2018 # https://github.com/intel-isl/MultiObjectiveOptimization import numpy as np import torch class MinNormSolver: MAX_ITER = 20 STOP_CRIT = 1e-5 def _min_norm_element_from2(v1v1, v1v2, v2v2): """ Analytical solution for min_{c} |cx_1 + (1-c)x_2|_2^2 d is the distance (objective) optimzed v1v1 = <x1,x1> v1v2 = <x1,x2> v2v2 = <x2,x2> """ if v1v2 >= v1v1: # Case: Fig 1, third column gamma = 0.999 cost = v1v1 return gamma, cost if v1v2 >= v2v2: # Case: Fig 1, first column gamma = 0.001 cost = v2v2 return gamma, cost # Case: Fig 1, second column gamma = -1.0 * ((v1v2 - v2v2) / (v1v1 + v2v2 - 2 * v1v2)) cost = v2v2 + gamma * (v1v2 - v2v2) return gamma, cost def _min_norm_2d(vecs, dps): """ Find the minimum norm solution as combination of two points This is correct only in 2D ie. min_c |\sum c_i x_i|_2^2 st. \sum c_i = 1 , 1 >= c_1 >= 0 for all i, c_i + c_j = 1.0 for some i, j """ dmin = np.inf for i in range(len(vecs)): for j in range(i + 1, len(vecs)): if (i, j) not in dps: dps[(i, j)] = (vecs[i] * vecs[j]).sum().item() dps[(j, i)] = dps[(i, j)] if (i, i) not in dps: dps[(i, i)] = (vecs[i] * vecs[i]).sum().item() if (j, j) not in dps: dps[(j, j)] = (vecs[j] * vecs[j]).sum().item() c, d = MinNormSolver._min_norm_element_from2(dps[(i, i)], dps[(i, j)], dps[(j, j)]) if d < dmin: dmin = d sol = [(i, j), c, d] return sol, dps def _projection2simplex(y): """ Given y, it solves argmin_z |y-z|_2 st \sum z = 1 , 1 >= z_i >= 0 for all i """ m = len(y) sorted_y = np.flip(np.sort(y), axis=0) tmpsum = 0.0 tmax_f = (np.sum(y) - 1.0) / m for i in range(m - 1): tmpsum += sorted_y[i] tmax = (tmpsum - 1) / (i + 1.0) if tmax > sorted_y[i + 1]: tmax_f = tmax break return np.maximum(y - tmax_f, np.zeros(y.shape)) def _next_point(cur_val, grad, n): proj_grad = grad - (np.sum(grad) / n) tm1 = -1.0 * cur_val[proj_grad < 0] / proj_grad[proj_grad < 0] tm2 = (1.0 - cur_val[proj_grad > 0]) / (proj_grad[proj_grad > 0]) skippers = np.sum(tm1 < 1e-7) + np.sum(tm2 < 1e-7) t = 1 if len(tm1[tm1 > 1e-7]) > 0: t = np.min(tm1[tm1 > 1e-7]) if len(tm2[tm2 > 1e-7]) > 0: t = min(t, np.min(tm2[tm2 > 1e-7])) next_point = proj_grad * t + cur_val next_point = MinNormSolver._projection2simplex(next_point) return next_point def find_min_norm_element(vecs): """ Given a list of vectors (vecs), this method finds the minimum norm element in the convex hull as min |u|_2 st. u = \sum c_i vecs[i] and \sum c_i = 1. It is quite geometric, and the main idea is the fact that if d_{ij} = min |u|_2 st u = c x_i + (1-c) x_j; the solution lies in (0, d_{i,j}) Hence, we find the best 2-task solution, and then run the projected gradient descent until convergence """ # Solution lying at the combination of two points dps = {} init_sol, dps = MinNormSolver._min_norm_2d(vecs, dps) n = len(vecs) sol_vec = np.zeros(n) sol_vec[init_sol[0][0]] = init_sol[1] sol_vec[init_sol[0][1]] = 1 - init_sol[1] if n < 3: # This is optimal for n=2, so return the solution return sol_vec, init_sol[2] iter_count = 0 grad_mat = np.zeros((n, n)) for i in range(n): for j in range(n): grad_mat[i, j] = dps[(i, j)] while iter_count < MinNormSolver.MAX_ITER: grad_dir = -1.0 * np.dot(grad_mat, sol_vec) new_point = MinNormSolver._next_point(sol_vec, grad_dir, n) # Re-compute the inner products for line search v1v1 = 0.0 v1v2 = 0.0 v2v2 = 0.0 for i in range(n): for j in range(n): v1v1 += sol_vec[i] * sol_vec[j] * dps[(i, j)] v1v2 += sol_vec[i] * new_point[j] * dps[(i, j)] v2v2 += new_point[i] * new_point[j] * dps[(i, j)] nc, nd = MinNormSolver._min_norm_element_from2(v1v1, v1v2, v2v2) new_sol_vec = nc * sol_vec + (1 - nc) * new_point change = new_sol_vec - sol_vec if np.sum(np.abs(change)) < MinNormSolver.STOP_CRIT: return sol_vec, nd sol_vec = new_sol_vec def find_min_norm_element_FW(vecs): """ Given a list of vectors (vecs), this method finds the minimum norm element in the convex hull as min |u|_2 st. u = \sum c_i vecs[i] and \sum c_i = 1. It is quite geometric, and the main idea is the fact that if d_{ij} = min |u|_2 st u = c x_i + (1-c) x_j; the solution lies in (0, d_{i,j}) Hence, we find the best 2-task solution, and then run the Frank Wolfe until convergence """ # Solution lying at the combination of two points dps = {} init_sol, dps = MinNormSolver._min_norm_2d(vecs, dps) n = len(vecs) sol_vec = np.zeros(n) sol_vec[init_sol[0][0]] = init_sol[1] sol_vec[init_sol[0][1]] = 1 - init_sol[1] if n < 3: # This is optimal for n=2, so return the solution return sol_vec, init_sol[2] iter_count = 0 grad_mat = np.zeros((n, n)) for i in range(n): for j in range(n): grad_mat[i, j] = dps[(i, j)] while iter_count < MinNormSolver.MAX_ITER: t_iter = np.argmin(np.dot(grad_mat, sol_vec)) v1v1 = np.dot(sol_vec, np.dot(grad_mat, sol_vec)) v1v2 = np.dot(sol_vec, grad_mat[:, t_iter]) v2v2 = grad_mat[t_iter, t_iter] nc, nd = MinNormSolver._min_norm_element_from2(v1v1, v1v2, v2v2) new_sol_vec = nc * sol_vec new_sol_vec[t_iter] += 1 - nc change = new_sol_vec - sol_vec if np.sum(np.abs(change)) < MinNormSolver.STOP_CRIT: return sol_vec, nd sol_vec = new_sol_vec def gradient_normalizers(grads, losses, normalization_type): gn = {} if normalization_type == 'l2': for t in grads: gn[t] = np.sqrt(np.sum([gr.pow(2).sum().data[0] for gr in grads[t]])) elif normalization_type == 'loss': for t in grads: gn[t] = losses[t] elif normalization_type == 'loss+': for t in grads: gn[t] = losses[t] * np.sqrt(np.sum([gr.pow(2).sum().data[0] for gr in grads[t]])) elif normalization_type == 'none': for t in grads: gn[t] = 1.0 else: print('ERROR: Invalid Normalization Type') return gn(base)
7,364
36.01005
147
py
sdmgrad
sdmgrad-main/consistency/train.py
import numpy as np import torch import torch.utils.data from torch import linalg as LA from torch.autograd import Variable from model_lenet import RegressionModel, RegressionTrain from model_resnet import MnistResNet, RegressionTrainResNet from utils import * import pickle import argparse parser = argparse.ArgumentParser(description='Multi-Fashion-MNIST') parser.add_argument('--base', default='lenet', type=str, help='base model') parser.add_argument('--solver', default='sdmgrad', type=str, help='which optimization algorithm to use') parser.add_argument('--alpha', default=0.5, type=float, help='the alpha used in cagrad') parser.add_argument('--lmbda', default=0.5, type=float, help='the lmbda used in sdmgrad') parser.add_argument('--seed', default=0, type=int, help='the seed') parser.add_argument('--niter', default=100, type=int, help='step of (outer) iteration') parser.add_argument('--initer', default=20, type=int, help='step of inner itration') args = parser.parse_args() torch.manual_seed(args.seed) np.random.seed(args.seed) torch.cuda.manual_seed_all(args.seed) def train(dataset, base_model, solver, alpha, lmbda, niter, initer): # generate #npref preference vectors n_tasks = 2 device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu') # load dataset # MultiMNIST: multi_mnist.pickle if dataset == 'mnist': with open('./data/multi_mnist.pickle', 'rb') as f: trainX, trainLabel, testX, testLabel = pickle.load(f) # MultiFashionMNIST: multi_fashion.pickle if dataset == 'fashion': with open('./data/multi_fashion.pickle', 'rb') as f: trainX, trainLabel, testX, testLabel = pickle.load(f) # Multi-(Fashion+MNIST): multi_fashion_and_mnist.pickle if dataset == 'fashion_and_mnist': with open('./data/multi_fashion_and_mnist.pickle', 'rb') as f: trainX, trainLabel, testX, testLabel = pickle.load(f) trainX = torch.from_numpy(trainX.reshape(120000, 1, 36, 36)).float() trainLabel = torch.from_numpy(trainLabel).long() testX = torch.from_numpy(testX.reshape(20000, 1, 36, 36)).float() testLabel = torch.from_numpy(testLabel).long() train_set = torch.utils.data.TensorDataset(trainX, trainLabel) test_set = torch.utils.data.TensorDataset(testX, testLabel) batch_size = 256 train_loader = torch.utils.data.DataLoader(dataset=train_set, batch_size=batch_size, shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=test_set, batch_size=batch_size, shuffle=False) print('==>>> total trainning batch number: {}'.format(len(train_loader))) print('==>>> total testing batch number: {}'.format(len(test_loader))) # define the base model for ParetoMTL if base_model == 'lenet': model = RegressionModel(n_tasks).to(device) if base_model == 'resnet18': model = MnistResNet(n_tasks).to(device) # choose different optimizer for different base model if base_model == 'lenet': optimizer = torch.optim.SGD(model.parameters(), lr=1e-2, momentum=0.9) scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[15, 30, 45, 60, 75, 90], gamma=0.5) if base_model == 'resnet18': optimizer = torch.optim.Adam(model.parameters(), lr=1e-3) scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[10, 20], gamma=0.1) # store infomation during optimization task_train_losses = [] train_accs = [] # grad grad_dims = [] for mm in model.shared_modules(): for param in mm.parameters(): grad_dims.append(param.data.numel()) grads = torch.Tensor(sum(grad_dims), n_tasks).to(device) w = torch.ones(n_tasks).to(device) / n_tasks # run niter epochs for t in range(niter): model.train() for it, (X, ts) in enumerate(train_loader): X, ts = X.to(device), ts.to(device) optimizer.zero_grad() # compute stochastic gradient task_loss = model.forward_loss(X, ts) # \nabla F, grads [n_model, n_tasks] for i in range(n_tasks): if i == 0: task_loss[i].backward(retain_graph=True) else: task_loss[i].backward() grad2vec(model, grads, grad_dims, i) model.zero_grad_shared_modules() if solver == 'cagrad': g = cagrad(grads, alpha, rescale=1) elif solver == 'mgd': g = mgd(grads) elif solver == 'sgd': g = mean_grad(grads) elif solver == 'sdmgrad': g = sdmgrad(w, grads, lmbda, initer) else: raise ValueError('Not supported solver.') overwrite_grad(model, g, grad_dims) # optimization step optimizer.step() scheduler.step() # calculate and record performance if t == 0 or (t + 1) % 2 == 0: model.eval() with torch.no_grad(): total_train_loss = [] train_acc = [] correct1_train = 0 correct2_train = 0 for it, (X, ts) in enumerate(train_loader): X, ts = X.to(device), ts.to(device) valid_train_loss = model.forward_loss(X, ts) total_train_loss.append(valid_train_loss) output1 = model(X).max(2, keepdim=True)[1][:, 0] output2 = model(X).max(2, keepdim=True)[1][:, 1] correct1_train += output1.eq(ts[:, 0].view_as(output1)).sum().item() correct2_train += output2.eq(ts[:, 1].view_as(output2)).sum().item() train_acc = np.stack([ 1.0 * correct1_train / len(train_loader.dataset), 1.0 * correct2_train / len(train_loader.dataset) ]) total_train_loss = torch.stack(total_train_loss) average_train_loss = torch.mean(total_train_loss, dim=0) # record and print task_train_losses.append(average_train_loss.data.cpu().numpy()) train_accs.append(train_acc) print('{}/{}: train_loss={}, train_acc={}'.format(t + 1, niter, task_train_losses[-1], train_accs[-1])) save_path = './saved_model/%s_%s_solver_%s_niter_%d_seed_%d.pickle' % (dataset, base_model, solver, niter, args.seed) torch.save(model.state_dict(), save_path) def run(dataset='mnist', base_model='lenet', solver='sdmgrad', alpha=0.5, lmbda=0.5, niter=100, initer=20): """ run stochatic moo algorithms """ train(dataset, base_model, solver, alpha, lmbda, niter, initer) run(dataset='fashion_and_mnist', base_model=args.base, solver=args.solver, alpha=args.alpha, lmbda=args.lmbda, niter=args.niter, initer=args.initer)
7,010
36.292553
118
py
sdmgrad
sdmgrad-main/cityscapes/model_segnet_single.py
import torch.nn as nn import torch.optim as optim import torch.nn.functional as F import argparse from create_dataset import * from utils import * parser = argparse.ArgumentParser(description='Single-task: One Task') parser.add_argument('--task', default='semantic', type=str, help='choose task: semantic, depth') parser.add_argument('--dataroot', default='cityscapes', type=str, help='dataset root') parser.add_argument('--seed', default=0, type=int, help='control seed') parser.add_argument('--apply_augmentation', action='store_true', help='toggle to apply data augmentation on NYUv2') opt = parser.parse_args() class SegNet(nn.Module): def __init__(self): super(SegNet, self).__init__() # initialise network parameters filter = [64, 128, 256, 512, 512] self.class_nb = 7 # define encoder decoder layers self.encoder_block = nn.ModuleList([self.conv_layer([3, filter[0]])]) self.decoder_block = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): self.encoder_block.append(self.conv_layer([filter[i], filter[i + 1]])) self.decoder_block.append(self.conv_layer([filter[i + 1], filter[i]])) # define convolution layer self.conv_block_enc = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) self.conv_block_dec = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): if i == 0: self.conv_block_enc.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.conv_block_dec.append(self.conv_layer([filter[i], filter[i]])) else: self.conv_block_enc.append( nn.Sequential(self.conv_layer([filter[i + 1], filter[i + 1]]), self.conv_layer([filter[i + 1], filter[i + 1]]))) self.conv_block_dec.append( nn.Sequential(self.conv_layer([filter[i], filter[i]]), self.conv_layer([filter[i], filter[i]]))) if opt.task == 'semantic': self.pred_task = self.conv_layer([filter[0], self.class_nb], pred=True) if opt.task == 'depth': self.pred_task = self.conv_layer([filter[0], 1], pred=True) # define pooling and unpooling functions self.down_sampling = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True) self.up_sampling = nn.MaxUnpool2d(kernel_size=2, stride=2) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) def conv_layer(self, channel, pred=False): if not pred: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(num_features=channel[1]), nn.ReLU(inplace=True), ) else: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), ) return conv_block def forward(self, x): g_encoder, g_decoder, g_maxpool, g_upsampl, indices = ([0] * 5 for _ in range(5)) for i in range(5): g_encoder[i], g_decoder[-i - 1] = ([0] * 2 for _ in range(2)) # define global shared network for i in range(5): if i == 0: g_encoder[i][0] = self.encoder_block[i](x) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) else: g_encoder[i][0] = self.encoder_block[i](g_maxpool[i - 1]) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) for i in range(5): if i == 0: g_upsampl[i] = self.up_sampling(g_maxpool[-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) else: g_upsampl[i] = self.up_sampling(g_decoder[i - 1][-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) # define task prediction layers if opt.task == 'semantic': pred = F.log_softmax(self.pred_task(g_decoder[-1][-1]), dim=1) if opt.task == 'depth': pred = self.pred_task(g_decoder[-1][-1]) return pred control_seed(opt.seed) # define model, optimiser and scheduler device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") SegNet = SegNet().to(device) optimizer = optim.Adam(SegNet.parameters(), lr=1e-4) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.5) print('Parameter Space: ABS: {:.1f}, REL: {:.4f}'.format(count_parameters(SegNet), count_parameters(SegNet) / 24981069)) print( 'LOSS FORMAT: SEMANTIC_LOSS MEAN_IOU PIX_ACC | DEPTH_LOSS ABS_ERR REL_ERR | NORMAL_LOSS MEAN MED <11.25 <22.5 <30') # define dataset dataset_path = opt.dataroot if opt.apply_augmentation: train_set = CityScapes(root=dataset_path, train=True, augmentation=True) print('Applying data augmentation.') else: train_set = CityScapes(root=dataset_path, train=True) print('Standard training strategy without data augmentation.') test_set = CityScapes(root=dataset_path, train=False) batch_size = 8 train_loader = torch.utils.data.DataLoader(dataset=train_set, batch_size=batch_size, shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=test_set, batch_size=batch_size, shuffle=False) # Train and evaluate single-task network single_task_trainer(train_loader, test_loader, SegNet, device, optimizer, scheduler, opt, 200)
6,370
43.552448
120
py
sdmgrad
sdmgrad-main/cityscapes/evaluate.py
import matplotlib.pyplot as plt import seaborn as sns import numpy as np import torch methods = [ "sdmgrad-1e-1", "sdmgrad-2e-1", "sdmgrad-3e-1", "sdmgrad-4e-1", "sdmgrad-5e-1", "sdmgrad-6e-1", "sdmgrad-7e-1", "sdmgrad-8e-1", "sdmgrad-9e-1", "sdmgrad-1e0" ] colors = ["C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "tab:green", "tab:cyan", "tab:blue", "tab:red"] stats = ["semantic loss", "mean iou", "pix acc", "depth loss", "abs err", "rel err"] stats_idx_map = [4, 5, 6, 8, 9, 10] delta_stats = ["mean iou", "pix acc", "abs err", "rel err"] time_idx = 22 # change random seeds used in the experiments here seeds = [0, 1, 2] logs = {} min_epoch = 100000 for m in methods: logs[m] = {"train": [None for _ in range(3)], "test": [None for _ in range(3)]} for seed in seeds: logs[m]["train"][seed] = {} logs[m]["test"][seed] = {} for stat in stats: for seed in seeds: logs[m]["train"][seed][stat] = [] logs[m]["test"][seed][stat] = [] for seed in seeds: logs[m]["train"][seed]["time"] = [] for seed in seeds: fname = f"logs/{m}-sd{seed}.log" with open(fname, "r") as f: lines = f.readlines() for line in lines: if line.startswith("Epoch"): ws = line.split(" ") for i, stat in enumerate(stats): logs[m]["train"][seed][stat].append(float(ws[stats_idx_map[i]])) logs[m]["test"][seed][stat].append(float(ws[stats_idx_map[i] + 9])) logs[m]["train"][seed]["time"].append(float(ws[time_idx])) n_epoch = min(len(logs[m]["train"][seed]["semantic loss"]), len(logs[m]["test"][seed]["semantic loss"])) if n_epoch < min_epoch: min_epoch = n_epoch print(m, n_epoch) test_stats = {} train_stats = {} learning_time = {} print(" " * 25 + " | ".join([f"{s:5s}" for s in stats])) for mi, mode in enumerate(["train", "test"]): if mi == 1: print(mode) for mmi, m in enumerate(methods): if m not in test_stats: test_stats[m] = {} train_stats[m] = {} string = f"{m:30s} " for stat in stats: x = [] for seed in seeds: x.append(np.array(logs[m][mode][seed][stat][min_epoch - 10:min_epoch]).mean()) x = np.array(x) if mode == "test": test_stats[m][stat] = x.copy() else: train_stats[m][stat] = x.copy() mu = x.mean() std = x.std() / np.sqrt(3) string += f" | {mu:5.4f}" if mode == "test": print(string) for m in methods: learning_time[m] = np.array([np.array(logs[m]["train"][sd]["time"]).mean() for sd in seeds]) ### print average training loss for method in methods: average_loss = np.mean([train_stats[method]["semantic loss"].mean(), train_stats[method]["depth loss"].mean()]) print(f"{method} average training loss {average_loss}") ### print delta M base = np.array([0.7401, 0.9316, 0.0125, 27.77]) sign = np.array([1, 1, 0, 0]) kk = np.ones(4) * -1 def delta_fn(a): return (kk**sign * (a - base) / base).mean() * 100. # *100 for percentage deltas = {} for method in methods: tmp = np.zeros(4) for i, stat in enumerate(delta_stats): tmp[i] = test_stats[method][stat].mean() deltas[method] = delta_fn(tmp) print(f"{method:30s} delta: {deltas[method]:4.3f}")
3,545
30.380531
117
py
sdmgrad
sdmgrad-main/cityscapes/model_segnet_stan.py
import torch.nn as nn import torch.optim as optim import torch.nn.functional as F import argparse from create_dataset import * from utils import * parser = argparse.ArgumentParser(description='Single-task: Attention Network') parser.add_argument('--task', default='semantic', type=str, help='choose task: semantic, depth, normal') parser.add_argument('--dataroot', default='cityscapes', type=str, help='dataset root') parser.add_argument('--apply_augmentation', action='store_true', help='toggle to apply data augmentation on NYUv2') opt = parser.parse_args() class SegNet(nn.Module): def __init__(self): super(SegNet, self).__init__() # initialise network parameters filter = [64, 128, 256, 512, 512] self.class_nb = 7 # define encoder decoder layers self.encoder_block = nn.ModuleList([self.conv_layer([3, filter[0]])]) self.decoder_block = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): self.encoder_block.append(self.conv_layer([filter[i], filter[i + 1]])) self.decoder_block.append(self.conv_layer([filter[i + 1], filter[i]])) # define convolution layer self.conv_block_enc = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) self.conv_block_dec = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): if i == 0: self.conv_block_enc.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.conv_block_dec.append(self.conv_layer([filter[i], filter[i]])) else: self.conv_block_enc.append( nn.Sequential(self.conv_layer([filter[i + 1], filter[i + 1]]), self.conv_layer([filter[i + 1], filter[i + 1]]))) self.conv_block_dec.append( nn.Sequential(self.conv_layer([filter[i], filter[i]]), self.conv_layer([filter[i], filter[i]]))) # define task attention layers self.encoder_att = nn.ModuleList([nn.ModuleList([self.att_layer([filter[0], filter[0], filter[0]])])]) self.decoder_att = nn.ModuleList([nn.ModuleList([self.att_layer([2 * filter[0], filter[0], filter[0]])])]) self.encoder_block_att = nn.ModuleList([self.conv_layer([filter[0], filter[1]])]) self.decoder_block_att = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for j in range(2): if j < 1: self.encoder_att.append(nn.ModuleList([self.att_layer([filter[0], filter[0], filter[0]])])) self.decoder_att.append(nn.ModuleList([self.att_layer([2 * filter[0], filter[0], filter[0]])])) for i in range(4): self.encoder_att[j].append(self.att_layer([2 * filter[i + 1], filter[i + 1], filter[i + 1]])) self.decoder_att[j].append(self.att_layer([filter[i + 1] + filter[i], filter[i], filter[i]])) for i in range(4): if i < 3: self.encoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 2]])) self.decoder_block_att.append(self.conv_layer([filter[i + 1], filter[i]])) else: self.encoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.decoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.pred_task1 = self.conv_layer([filter[0], self.class_nb], pred=True) self.pred_task2 = self.conv_layer([filter[0], 1], pred=True) #self.pred_task3 = self.conv_layer([filter[0], 3], pred=True) # define pooling and unpooling functions self.down_sampling = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True) self.up_sampling = nn.MaxUnpool2d(kernel_size=2, stride=2) self.logsigma = nn.Parameter(torch.FloatTensor([-0.5, -0.5, -0.5])) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) def conv_layer(self, channel, pred=False): if not pred: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(num_features=channel[1]), nn.ReLU(inplace=True), ) else: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), ) return conv_block def att_layer(self, channel): att_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), nn.BatchNorm2d(channel[1]), nn.ReLU(inplace=True), nn.Conv2d(in_channels=channel[1], out_channels=channel[2], kernel_size=1, padding=0), nn.BatchNorm2d(channel[2]), nn.Sigmoid(), ) return att_block def forward(self, x): g_encoder, g_decoder, g_maxpool, g_upsampl, indices = ([0] * 5 for _ in range(5)) for i in range(5): g_encoder[i], g_decoder[-i - 1] = ([0] * 2 for _ in range(2)) # define attention list for tasks atten_encoder, atten_decoder = ([0] * 2 for _ in range(2)) for i in range(2): atten_encoder[i], atten_decoder[i] = ([0] * 5 for _ in range(2)) for i in range(2): for j in range(5): atten_encoder[i][j], atten_decoder[i][j] = ([0] * 3 for _ in range(2)) # define global shared network for i in range(5): if i == 0: g_encoder[i][0] = self.encoder_block[i](x) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) else: g_encoder[i][0] = self.encoder_block[i](g_maxpool[i - 1]) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) for i in range(5): if i == 0: g_upsampl[i] = self.up_sampling(g_maxpool[-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) else: g_upsampl[i] = self.up_sampling(g_decoder[i - 1][-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) # define task dependent attention module for i in range(2): for j in range(5): if j == 0: atten_encoder[i][j][0] = self.encoder_att[i][j](g_encoder[j][0]) atten_encoder[i][j][1] = (atten_encoder[i][j][0]) * g_encoder[j][1] atten_encoder[i][j][2] = self.encoder_block_att[j](atten_encoder[i][j][1]) atten_encoder[i][j][2] = F.max_pool2d(atten_encoder[i][j][2], kernel_size=2, stride=2) else: atten_encoder[i][j][0] = self.encoder_att[i][j](torch.cat( (g_encoder[j][0], atten_encoder[i][j - 1][2]), dim=1)) atten_encoder[i][j][1] = (atten_encoder[i][j][0]) * g_encoder[j][1] atten_encoder[i][j][2] = self.encoder_block_att[j](atten_encoder[i][j][1]) atten_encoder[i][j][2] = F.max_pool2d(atten_encoder[i][j][2], kernel_size=2, stride=2) for j in range(5): if j == 0: atten_decoder[i][j][0] = F.interpolate(atten_encoder[i][-1][-1], scale_factor=2, mode='bilinear', align_corners=True) atten_decoder[i][j][0] = self.decoder_block_att[-j - 1](atten_decoder[i][j][0]) atten_decoder[i][j][1] = self.decoder_att[i][-j - 1](torch.cat( (g_upsampl[j], atten_decoder[i][j][0]), dim=1)) atten_decoder[i][j][2] = (atten_decoder[i][j][1]) * g_decoder[j][-1] else: atten_decoder[i][j][0] = F.interpolate(atten_decoder[i][j - 1][2], scale_factor=2, mode='bilinear', align_corners=True) atten_decoder[i][j][0] = self.decoder_block_att[-j - 1](atten_decoder[i][j][0]) atten_decoder[i][j][1] = self.decoder_att[i][-j - 1](torch.cat( (g_upsampl[j], atten_decoder[i][j][0]), dim=1)) atten_decoder[i][j][2] = (atten_decoder[i][j][1]) * g_decoder[j][-1] # define task prediction layers t1_pred = F.log_softmax(self.pred_task1(atten_decoder[0][-1][-1]), dim=1) t2_pred = self.pred_task2(atten_decoder[1][-1][-1]) #t3_pred = self.pred_task3(atten_decoder[2][-1][-1]) #t3_pred = t3_pred / torch.norm(t3_pred, p=2, dim=1, keepdim=True) return [t1_pred, t2_pred], self.logsigma # define model, optimiser and scheduler device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") SegNet_STAN = SegNet().to(device) optimizer = optim.Adam(SegNet_STAN.parameters(), lr=1e-4) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.5) print('Parameter Space: ABS: {:.1f}, REL: {:.4f}'.format(count_parameters(SegNet_STAN), count_parameters(SegNet_STAN) / 24981069)) print( 'LOSS FORMAT: SEMANTIC_LOSS MEAN_IOU PIX_ACC | DEPTH_LOSS ABS_ERR REL_ERR | NORMAL_LOSS MEAN MED <11.25 <22.5 <30') # define dataset dataset_path = opt.dataroot if opt.apply_augmentation: train_set = CityScapes(root=dataset_path, train=True, augmentation=True) print('Applying data augmentation.') else: train_set = CityScapes(root=dataset_path, train=True) print('Standard training strategy without data augmentation.') test_set = CityScapes(root=dataset_path, train=False) batch_size = 8 train_loader = torch.utils.data.DataLoader(dataset=train_set, batch_size=batch_size, shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=test_set, batch_size=batch_size, shuffle=False) # Train and evaluate single-task network single_task_trainer(train_loader, test_loader, SegNet_STAN, device, optimizer, scheduler, opt, 200)
11,156
49.713636
119
py
sdmgrad
sdmgrad-main/cityscapes/utils.py
import torch import torch.nn.functional as F import numpy as np import random import time from copy import deepcopy from min_norm_solvers import MinNormSolver from scipy.optimize import minimize, Bounds, minimize_scalar def euclidean_proj_simplex(v, s=1): """ Compute the Euclidean projection on a positive simplex Solves the optimisation problem (using the algorithm from [1]): min_w 0.5 * || w - v ||_2^2 , s.t. \sum_i w_i = s, w_i >= 0 Parameters ---------- v: (n,) numpy array, n-dimensional vector to project s: int, optional, default: 1, radius of the simplex Returns ------- w: (n,) numpy array, Euclidean projection of v on the simplex Notes ----- The complexity of this algorithm is in O(n log(n)) as it involves sorting v. Better alternatives exist for high-dimensional sparse vectors (cf. [1]) However, this implementation still easily scales to millions of dimensions. References ---------- [1] Efficient Projections onto the .1-Ball for Learning in High Dimensions John Duchi, Shai Shalev-Shwartz, Yoram Singer, and Tushar Chandra. International Conference on Machine Learning (ICML 2008) http://www.cs.berkeley.edu/~jduchi/projects/DuchiSiShCh08.pdf [2] Projection onto the probability simplex: An efficient algorithm with a simple proof, and an application Weiran Wang, Miguel Á. Carreira-Perpiñán. arXiv:1309.1541 https://arxiv.org/pdf/1309.1541.pdf [3] https://gist.github.com/daien/1272551/edd95a6154106f8e28209a1c7964623ef8397246#file-simplex_projection-py """ assert s > 0, "Radius s must be strictly positive (%d <= 0)" % s v = v.astype(np.float64) n, = v.shape # will raise ValueError if v is not 1-D # check if we are already on the simplex if v.sum() == s and np.alltrue(v >= 0): # best projection: itself! return v # get the array of cumulative sums of a sorted (decreasing) copy of v u = np.sort(v)[::-1] cssv = np.cumsum(u) # get the number of > 0 components of the optimal solution rho = np.nonzero(u * np.arange(1, n + 1) > (cssv - s))[0][-1] # compute the Lagrange multiplier associated to the simplex constraint theta = float(cssv[rho] - s) / (rho + 1) # compute the projection by thresholding v using theta w = (v - theta).clip(min=0) return w """ Define task metrics, loss functions and model trainer here. """ def control_seed(seed): torch.backends.cudnn.enabled = False torch.manual_seed(seed) np.random.seed(seed) random.seed(seed) torch.cuda.manual_seed_all(seed) def count_parameters(model): return sum(p.numel() for p in model.parameters() if p.requires_grad) def model_fit(x_pred, x_output, task_type): device = x_pred.device # binary mark to mask out undefined pixel space binary_mask = (torch.sum(x_output, dim=1) != 0).float().unsqueeze(1).to(device) if task_type == 'semantic': # semantic loss: depth-wise cross entropy loss = F.nll_loss(x_pred, x_output, ignore_index=-1) if task_type == 'depth': # depth loss: l1 norm loss = torch.sum(torch.abs(x_pred - x_output) * binary_mask) / torch.nonzero(binary_mask, as_tuple=False).size(0) if task_type == 'normal': # normal loss: dot product loss = 1 - torch.sum((x_pred * x_output) * binary_mask) / torch.nonzero(binary_mask, as_tuple=False).size(0) return loss # Legacy: compute mIoU and Acc. for each image and average across all images. # def compute_miou(x_pred, x_output): # _, x_pred_label = torch.max(x_pred, dim=1) # x_output_label = x_output # batch_size = x_pred.size(0) # class_nb = x_pred.size(1) # device = x_pred.device # for i in range(batch_size): # true_class = 0 # first_switch = True # invalid_mask = (x_output[i] >= 0).float() # for j in range(class_nb): # pred_mask = torch.eq(x_pred_label[i], j * torch.ones(x_pred_label[i].shape).long().to(device)) # true_mask = torch.eq(x_output_label[i], j * torch.ones(x_output_label[i].shape).long().to(device)) # mask_comb = pred_mask.float() + true_mask.float() # union = torch.sum((mask_comb > 0).float() * invalid_mask) # remove non-defined pixel predictions # intsec = torch.sum((mask_comb > 1).float()) # if union == 0: # continue # if first_switch: # class_prob = intsec / union # first_switch = False # else: # class_prob = intsec / union + class_prob # true_class += 1 # if i == 0: # batch_avg = class_prob / true_class # else: # batch_avg = class_prob / true_class + batch_avg # return batch_avg / batch_size # # # def compute_iou(x_pred, x_output): # _, x_pred_label = torch.max(x_pred, dim=1) # x_output_label = x_output # batch_size = x_pred.size(0) # for i in range(batch_size): # if i == 0: # pixel_acc = torch.div( # torch.sum(torch.eq(x_pred_label[i], x_output_label[i]).float()), # torch.sum((x_output_label[i] >= 0).float())) # else: # pixel_acc = pixel_acc + torch.div( # torch.sum(torch.eq(x_pred_label[i], x_output_label[i]).float()), # torch.sum((x_output_label[i] >= 0).float())) # return pixel_acc / batch_size # New mIoU and Acc. formula: accumulate every pixel and average across all pixels in all images class ConfMatrix(object): def __init__(self, num_classes): self.num_classes = num_classes self.mat = None def update(self, pred, target): n = self.num_classes if self.mat is None: self.mat = torch.zeros((n, n), dtype=torch.int64, device=pred.device) with torch.no_grad(): k = (target >= 0) & (target < n) inds = n * target[k].to(torch.int64) + pred[k] self.mat += torch.bincount(inds, minlength=n**2).reshape(n, n) def get_metrics(self): h = self.mat.float() acc = torch.diag(h).sum() / h.sum() iu = torch.diag(h) / (h.sum(1) + h.sum(0) - torch.diag(h)) return torch.mean(iu).item(), acc.item() def depth_error(x_pred, x_output): device = x_pred.device binary_mask = (torch.sum(x_output, dim=1) != 0).unsqueeze(1).to(device) x_pred_true = x_pred.masked_select(binary_mask) x_output_true = x_output.masked_select(binary_mask) abs_err = torch.abs(x_pred_true - x_output_true) rel_err = torch.abs(x_pred_true - x_output_true) / x_output_true return (torch.sum(abs_err) / torch.nonzero(binary_mask, as_tuple=False).size(0)).item(), \ (torch.sum(rel_err) / torch.nonzero(binary_mask, as_tuple=False).size(0)).item() def normal_error(x_pred, x_output): binary_mask = (torch.sum(x_output, dim=1) != 0) error = torch.acos(torch.clamp(torch.sum(x_pred * x_output, 1).masked_select(binary_mask), -1, 1)).detach().cpu().numpy() error = np.degrees(error) return np.mean(error), np.median(error), np.mean(error < 11.25), np.mean(error < 22.5), np.mean(error < 30) """ =========== Universal Multi-task Trainer =========== """ def multi_task_trainer(train_loader, test_loader, multi_task_model, device, optimizer, scheduler, opt, total_epoch=200): train_batch = len(train_loader) test_batch = len(test_loader) T = opt.temp avg_cost = np.zeros([total_epoch, 12], dtype=np.float32) lambda_weight = np.ones([2, total_epoch]) for index in range(total_epoch): t0 = time.time() cost = np.zeros(12, dtype=np.float32) # apply Dynamic Weight Average if opt.weight == 'dwa': if index == 0 or index == 1: lambda_weight[:, index] = 1.0 else: w_1 = avg_cost[index - 1, 0] / avg_cost[index - 2, 0] w_2 = avg_cost[index - 1, 3] / avg_cost[index - 2, 3] lambda_weight[0, index] = 2 * np.exp(w_1 / T) / (np.exp(w_1 / T) + np.exp(w_2 / T)) lambda_weight[1, index] = 2 * np.exp(w_2 / T) / (np.exp(w_1 / T) + np.exp(w_2 / T)) # iteration for all batches multi_task_model.train() train_dataset = iter(train_loader) conf_mat = ConfMatrix(multi_task_model.class_nb) for k in range(train_batch): train_data, train_label, train_depth = train_dataset.next() train_data, train_label = train_data.to(device), train_label.long().to(device) train_depth = train_depth.to(device) train_pred, logsigma = multi_task_model(train_data) optimizer.zero_grad() train_loss = [ model_fit(train_pred[0], train_label, 'semantic'), model_fit(train_pred[1], train_depth, 'depth') ] if opt.weight == 'equal' or opt.weight == 'dwa': loss = sum([lambda_weight[i, index] * train_loss[i] for i in range(2)]) else: loss = sum(1 / (2 * torch.exp(logsigma[i])) * train_loss[i] + logsigma[i] / 2 for i in range(2)) loss.backward() optimizer.step() # accumulate label prediction for every pixel in training images conf_mat.update(train_pred[0].argmax(1).flatten(), train_label.flatten()) cost[0] = train_loss[0].item() cost[3] = train_loss[1].item() cost[4], cost[5] = depth_error(train_pred[1], train_depth) avg_cost[index, :6] += cost[:6] / train_batch # compute mIoU and acc avg_cost[index, 1:3] = conf_mat.get_metrics() # evaluating test data multi_task_model.eval() conf_mat = ConfMatrix(multi_task_model.class_nb) with torch.no_grad(): # operations inside don't track history test_dataset = iter(test_loader) for k in range(test_batch): test_data, test_label, test_depth = test_dataset.next() test_data, test_label = test_data.to(device), test_label.long().to(device) test_depth = test_depth.to(device) test_pred, _ = multi_task_model(test_data) test_loss = [ model_fit(test_pred[0], test_label, 'semantic'), model_fit(test_pred[1], test_depth, 'depth') ] conf_mat.update(test_pred[0].argmax(1).flatten(), test_label.flatten()) cost[6] = test_loss[0].item() cost[9] = test_loss[1].item() cost[10], cost[11] = depth_error(test_pred[1], test_depth) avg_cost[index, 6:] += cost[6:] / test_batch # compute mIoU and acc avg_cost[index, 7:9] = conf_mat.get_metrics() scheduler.step() t1 = time.time() print( 'Epoch: {:04d} | TRAIN: {:.4f} {:.4f} {:.4f} | {:.4f} {:.4f} {:.4f} || TEST: {:.4f} {:.4f} {:.4f} | {:.4f} {:.4f} {:.4f} | TIME: {:.4f}' .format(index, avg_cost[index, 0], avg_cost[index, 1], avg_cost[index, 2], avg_cost[index, 3], avg_cost[index, 4], avg_cost[index, 5], avg_cost[index, 6], avg_cost[index, 7], avg_cost[index, 8], avg_cost[index, 9], avg_cost[index, 10], avg_cost[index, 11], t1 - t0)) """ =========== Universal Single-task Trainer =========== """ def single_task_trainer(train_loader, test_loader, single_task_model, device, optimizer, scheduler, opt, total_epoch=200): train_batch = len(train_loader) test_batch = len(test_loader) avg_cost = np.zeros([total_epoch, 12], dtype=np.float32) for index in range(total_epoch): cost = np.zeros(12, dtype=np.float32) # iteration for all batches single_task_model.train() train_dataset = iter(train_loader) conf_mat = ConfMatrix(single_task_model.class_nb) for k in range(train_batch): train_data, train_label, train_depth = train_dataset.next() train_data, train_label = train_data.to(device), train_label.long().to(device) train_depth = train_depth.to(device) train_pred = single_task_model(train_data) optimizer.zero_grad() if opt.task == 'semantic': train_loss = model_fit(train_pred, train_label, opt.task) train_loss.backward() optimizer.step() conf_mat.update(train_pred.argmax(1).flatten(), train_label.flatten()) cost[0] = train_loss.item() if opt.task == 'depth': train_loss = model_fit(train_pred, train_depth, opt.task) train_loss.backward() optimizer.step() cost[3] = train_loss.item() cost[4], cost[5] = depth_error(train_pred, train_depth) avg_cost[index, :6] += cost[:6] / train_batch if opt.task == 'semantic': avg_cost[index, 1:3] = conf_mat.get_metrics() # evaluating test data single_task_model.eval() conf_mat = ConfMatrix(single_task_model.class_nb) with torch.no_grad(): # operations inside don't track history test_dataset = iter(test_loader) for k in range(test_batch): test_data, test_label, test_depth = test_dataset.next() test_data, test_label = test_data.to(device), test_label.long().to(device) test_depth = test_depth.to(device) test_pred = single_task_model(test_data) if opt.task == 'semantic': test_loss = model_fit(test_pred, test_label, opt.task) conf_mat.update(test_pred.argmax(1).flatten(), test_label.flatten()) cost[6] = test_loss.item() if opt.task == 'depth': test_loss = model_fit(test_pred, test_depth, opt.task) cost[9] = test_loss.item() cost[10], cost[11] = depth_error(test_pred, test_depth) avg_cost[index, 6:] += cost[6:] / test_batch if opt.task == 'semantic': avg_cost[index, 7:9] = conf_mat.get_metrics() scheduler.step() if opt.task == 'semantic': print('Epoch: {:04d} | TRAIN: {:.4f} {:.4f} {:.4f} TEST: {:.4f} {:.4f} {:.4f}'.format( index, avg_cost[index, 0], avg_cost[index, 1], avg_cost[index, 2], avg_cost[index, 6], avg_cost[index, 7], avg_cost[index, 8])) if opt.task == 'depth': print('Epoch: {:04d} | TRAIN: {:.4f} {:.4f} {:.4f} TEST: {:.4f} {:.4f} {:.4f}'.format( index, avg_cost[index, 3], avg_cost[index, 4], avg_cost[index, 5], avg_cost[index, 9], avg_cost[index, 10], avg_cost[index, 11])) torch.save(single_task_model.state_dict(), f"models/single-{opt.task}-{opt.seed}.pt") """ =========== Universal Gradient Manipulation Multi-task Trainer =========== """ def multi_task_rg_trainer(train_loader, test_loader, multi_task_model, device, optimizer, scheduler, opt, total_epoch=200): method = opt.method alpha = opt.alpha niter = opt.niter # warm_niter = opt.warm_niter def graddrop(grads): P = 0.5 * (1. + grads.sum(1) / (grads.abs().sum(1) + 1e-8)) U = torch.rand_like(grads[:, 0]) M = P.gt(U).view(-1, 1) * grads.gt(0) + P.lt(U).view(-1, 1) * grads.lt(0) g = (grads * M.float()).mean(1) return g def mgd(grads): grads_cpu = grads.t().cpu() sol, min_norm = MinNormSolver.find_min_norm_element([grads_cpu[t] for t in range(grads.shape[-1])]) w = torch.FloatTensor(sol).to(grads.device) g = grads.mm(w.view(-1, 1)).view(-1) return g def pcgrad(grads, rng): grad_vec = grads.t() num_tasks = 2 shuffled_task_indices = np.zeros((num_tasks, num_tasks - 1), dtype=int) for i in range(num_tasks): task_indices = np.arange(num_tasks) task_indices[i] = task_indices[-1] shuffled_task_indices[i] = task_indices[:-1] rng.shuffle(shuffled_task_indices[i]) shuffled_task_indices = shuffled_task_indices.T normalized_grad_vec = grad_vec / (grad_vec.norm(dim=1, keepdim=True) + 1e-8) # num_tasks x dim modified_grad_vec = deepcopy(grad_vec) for task_indices in shuffled_task_indices: normalized_shuffled_grad = normalized_grad_vec[task_indices] # num_tasks x dim dot = (modified_grad_vec * normalized_shuffled_grad).sum(dim=1, keepdim=True) # num_tasks x dim modified_grad_vec -= torch.clamp_max(dot, 0) * normalized_shuffled_grad g = modified_grad_vec.mean(dim=0) return g def cagrad(grads, alpha=0.5, rescale=0): g1 = grads[:, 0] g2 = grads[:, 1] g11 = g1.dot(g1).item() g12 = g1.dot(g2).item() g22 = g2.dot(g2).item() g0_norm = 0.5 * np.sqrt(g11 + g22 + 2 * g12) # want to minimize g_w^Tg_0 + c*||g_0||*||g_w|| coef = alpha * g0_norm def obj(x): # g_w^T g_0: x*0.5*(g11+g22-2g12)+(0.5+x)*(g12-g22)+g22 # g_w^T g_w: x^2*(g11+g22-2g12)+2*x*(g12-g22)+g22 return coef * np.sqrt(x**2 * (g11 + g22 - 2 * g12) + 2 * x * (g12 - g22) + g22 + 1e-8) + 0.5 * x * (g11 + g22 - 2 * g12) + (0.5 + x) * (g12 - g22) + g22 res = minimize_scalar(obj, bounds=(0, 1), method='bounded') x = res.x gw_norm = np.sqrt(x**2 * g11 + (1 - x)**2 * g22 + 2 * x * (1 - x) * g12 + 1e-8) lmbda = coef / (gw_norm + 1e-8) g = (0.5 + lmbda * x) * g1 + (0.5 + lmbda * (1 - x)) * g2 # g0 + lmbda*gw if rescale == 0: return g elif rescale == 1: return g / (1 + alpha**2) else: return g / (1 + alpha) def sdmgrad(w, grads, alpha, niter=20): GG = torch.mm(grads.t(), grads) scale = torch.mean(torch.sqrt(torch.diag(GG) + 1e-4)) GG = GG / scale.pow(2) Gg = torch.mean(GG, dim=1) gg = torch.mean(Gg) w.requires_grad = True optimizer = torch.optim.SGD([w], lr=10, momentum=0.5) for i in range(niter): optimizer.zero_grad() obj = torch.dot(w, torch.mv(GG, w)) + 2 * alpha * torch.dot(w, Gg) + alpha**2 * gg obj.backward() optimizer.step() proj = euclidean_proj_simplex(w.data.cpu().numpy()) w.data.copy_(torch.from_numpy(proj).data) w.requires_grad = False g0 = torch.mean(grads, dim=1) gw = torch.mv(grads, w) g = (gw + alpha * g0) / (1 + alpha) return g def grad2vec(m, grads, grad_dims, task): # store the gradients grads[:, task].fill_(0.0) cnt = 0 for mm in m.shared_modules(): for p in mm.parameters(): grad = p.grad if grad is not None: grad_cur = grad.data.detach().clone() beg = 0 if cnt == 0 else sum(grad_dims[:cnt]) en = sum(grad_dims[:cnt + 1]) grads[beg:en, task].copy_(grad_cur.data.view(-1)) cnt += 1 def overwrite_grad(m, newgrad, grad_dims): newgrad = newgrad * 2 # to match the sum loss cnt = 0 for mm in m.shared_modules(): for param in mm.parameters(): beg = 0 if cnt == 0 else sum(grad_dims[:cnt]) en = sum(grad_dims[:cnt + 1]) this_grad = newgrad[beg:en].contiguous().view(param.data.size()) param.grad = this_grad.data.clone() cnt += 1 rng = np.random.default_rng() grad_dims = [] for mm in multi_task_model.shared_modules(): for param in mm.parameters(): grad_dims.append(param.data.numel()) grads = torch.Tensor(sum(grad_dims), 2).cuda() w = 1 / 2 * torch.ones(2).cuda() train_batch = len(train_loader) test_batch = len(test_loader) T = opt.temp avg_cost = np.zeros([total_epoch, 12], dtype=np.float32) lambda_weight = np.ones([2, total_epoch]) for index in range(total_epoch): t0 = time.time() cost = np.zeros(12, dtype=np.float32) # apply Dynamic Weight Average if opt.weight == 'dwa': if index == 0 or index == 1: lambda_weight[:, index] = 1.0 else: w_1 = avg_cost[index - 1, 0] / avg_cost[index - 2, 0] w_2 = avg_cost[index - 1, 3] / avg_cost[index - 2, 3] lambda_weight[0, index] = 2 * np.exp(w_1 / T) / (np.exp(w_1 / T) + np.exp(w_2 / T)) lambda_weight[1, index] = 2 * np.exp(w_2 / T) / (np.exp(w_1 / T) + np.exp(w_2 / T)) # iteration for all batches multi_task_model.train() train_dataset = iter(train_loader) conf_mat = ConfMatrix(multi_task_model.class_nb) for k in range(train_batch): train_data, train_label, train_depth = train_dataset.next() train_data, train_label = train_data.to(device), train_label.long().to(device) train_depth = train_depth.to(device) train_pred, logsigma = multi_task_model(train_data) train_loss = [ model_fit(train_pred[0], train_label, 'semantic'), model_fit(train_pred[1], train_depth, 'depth') ] train_loss_tmp = [0, 0] if opt.weight == 'equal' or opt.weight == 'dwa': for i in range(2): train_loss_tmp[i] = train_loss[i] * lambda_weight[i, index] else: for i in range(2): train_loss_tmp[i] = 1 / (2 * torch.exp(logsigma[i])) * train_loss[i] + logsigma[i] / 2 optimizer.zero_grad() if method == "graddrop": for i in range(2): if i == 0: train_loss_tmp[i].backward(retain_graph=True) else: train_loss_tmp[i].backward() grad2vec(multi_task_model, grads, grad_dims, i) multi_task_model.zero_grad_shared_modules() g = graddrop(grads) overwrite_grad(multi_task_model, g, grad_dims) optimizer.step() elif method == "pcgrad": for i in range(2): if i == 0: train_loss_tmp[i].backward(retain_graph=True) else: train_loss_tmp[i].backward() grad2vec(multi_task_model, grads, grad_dims, i) multi_task_model.zero_grad_shared_modules() g = pcgrad(grads, rng) overwrite_grad(multi_task_model, g, grad_dims) optimizer.step() elif method == "mgd": for i in range(2): if i == 0: train_loss_tmp[i].backward(retain_graph=True) else: train_loss_tmp[i].backward() grad2vec(multi_task_model, grads, grad_dims, i) multi_task_model.zero_grad_shared_modules() g = mgd(grads) overwrite_grad(multi_task_model, g, grad_dims) optimizer.step() elif method == "cagrad": for i in range(2): if i == 0: train_loss_tmp[i].backward(retain_graph=True) else: train_loss_tmp[i].backward() grad2vec(multi_task_model, grads, grad_dims, i) multi_task_model.zero_grad_shared_modules() g = cagrad(grads, alpha, rescale=1) overwrite_grad(multi_task_model, g, grad_dims) optimizer.step() elif method == "sdmgrad": for i in range(2): if i == 0: train_loss_tmp[i].backward(retain_graph=True) else: train_loss_tmp[i].backward() grad2vec(multi_task_model, grads, grad_dims, i) multi_task_model.zero_grad_shared_modules() g = sdmgrad(w, grads, alpha, niter=niter) overwrite_grad(multi_task_model, g, grad_dims) optimizer.step() # accumulate label prediction for every pixel in training images conf_mat.update(train_pred[0].argmax(1).flatten(), train_label.flatten()) cost[0] = train_loss[0].item() cost[3] = train_loss[1].item() cost[4], cost[5] = depth_error(train_pred[1], train_depth) avg_cost[index, :6] += cost[:6] / train_batch # compute mIoU and acc avg_cost[index, 1:3] = conf_mat.get_metrics() # evaluating test data multi_task_model.eval() conf_mat = ConfMatrix(multi_task_model.class_nb) with torch.no_grad(): # operations inside don't track history test_dataset = iter(test_loader) for k in range(test_batch): test_data, test_label, test_depth = test_dataset.next() test_data, test_label = test_data.to(device), test_label.long().to(device) test_depth = test_depth.to(device) test_pred, _ = multi_task_model(test_data) test_loss = [ model_fit(test_pred[0], test_label, 'semantic'), model_fit(test_pred[1], test_depth, 'depth') ] conf_mat.update(test_pred[0].argmax(1).flatten(), test_label.flatten()) cost[6] = test_loss[0].item() cost[9] = test_loss[1].item() cost[10], cost[11] = depth_error(test_pred[1], test_depth) avg_cost[index, 6:] += cost[6:] / test_batch # compute mIoU and acc avg_cost[index, 7:9] = conf_mat.get_metrics() scheduler.step() t1 = time.time() print( 'Epoch: {:04d} | TRAIN: {:.4f} {:.4f} {:.4f} | {:.4f} {:.4f} {:.4f} || TEST: {:.4f} {:.4f} {:.4f} | {:.4f} {:.4f} {:.4f} | TIME: {:.4f}' .format(index, avg_cost[index, 0], avg_cost[index, 1], avg_cost[index, 2], avg_cost[index, 3], avg_cost[index, 4], avg_cost[index, 5], avg_cost[index, 6], avg_cost[index, 7], avg_cost[index, 8], avg_cost[index, 9], avg_cost[index, 10], avg_cost[index, 11], t1 - t0)) torch.save(multi_task_model.state_dict(), f"models/{method}-{opt.weight}-{alpha}-{opt.seed}.pt")
27,394
40.25753
148
py
sdmgrad
sdmgrad-main/cityscapes/model_segnet_split.py
import torch.nn as nn import torch.optim as optim import torch.nn.functional as F import argparse import torch.utils.data.sampler as sampler from create_dataset import * from utils import * parser = argparse.ArgumentParser(description='Multi-task: Split') parser.add_argument('--type', default='standard', type=str, help='split type: standard, wide, deep') parser.add_argument('--weight', default='equal', type=str, help='multi-task weighting: equal, uncert, dwa') parser.add_argument('--dataroot', default='cityscapes', type=str, help='dataset root') parser.add_argument('--temp', default=2.0, type=float, help='temperature for DWA (must be positive)') parser.add_argument('--apply_augmentation', action='store_true', help='toggle to apply data augmentation on NYUv2') opt = parser.parse_args() class SegNet(nn.Module): def __init__(self): super(SegNet, self).__init__() # initialise network parameters filter = [64, 128, 256, 512, 512] self.class_nb = 7 # define encoder decoder layers self.encoder_block = nn.ModuleList([self.conv_layer([3, filter[0]])]) self.decoder_block = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): self.encoder_block.append(self.conv_layer([filter[i], filter[i + 1]])) self.decoder_block.append(self.conv_layer([filter[i + 1], filter[i]])) # define convolution layer self.conv_block_enc = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) self.conv_block_dec = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): if i == 0: self.conv_block_enc.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.conv_block_dec.append(self.conv_layer([filter[i], filter[i]])) else: self.conv_block_enc.append( nn.Sequential(self.conv_layer([filter[i + 1], filter[i + 1]]), self.conv_layer([filter[i + 1], filter[i + 1]]))) self.conv_block_dec.append( nn.Sequential(self.conv_layer([filter[i], filter[i]]), self.conv_layer([filter[i], filter[i]]))) # define task attention layers self.encoder_att = nn.ModuleList([nn.ModuleList([self.att_layer([filter[0], filter[0], filter[0]])])]) self.decoder_att = nn.ModuleList([nn.ModuleList([self.att_layer([2 * filter[0], filter[0], filter[0]])])]) self.encoder_block_att = nn.ModuleList([self.conv_layer([filter[0], filter[1]])]) self.decoder_block_att = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for j in range(2): if j < 1: self.encoder_att.append(nn.ModuleList([self.att_layer([filter[0], filter[0], filter[0]])])) self.decoder_att.append(nn.ModuleList([self.att_layer([2 * filter[0], filter[0], filter[0]])])) for i in range(4): self.encoder_att[j].append(self.att_layer([2 * filter[i + 1], filter[i + 1], filter[i + 1]])) self.decoder_att[j].append(self.att_layer([filter[i + 1] + filter[i], filter[i], filter[i]])) for i in range(4): if i < 3: self.encoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 2]])) self.decoder_block_att.append(self.conv_layer([filter[i + 1], filter[i]])) else: self.encoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.decoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.pred_task1 = self.conv_layer([filter[0], self.class_nb], pred=True) self.pred_task2 = self.conv_layer([filter[0], 1], pred=True) #self.pred_task3 = self.conv_layer([filter[0], 3], pred=True) # define pooling and unpooling functions self.down_sampling = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True) self.up_sampling = nn.MaxUnpool2d(kernel_size=2, stride=2) self.logsigma = nn.Parameter(torch.FloatTensor([-0.5, -0.5, -0.5])) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) def conv_layer(self, channel, pred=False): if not pred: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(num_features=channel[1]), nn.ReLU(inplace=True), ) else: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), ) return conv_block def att_layer(self, channel): att_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), nn.BatchNorm2d(channel[1]), nn.ReLU(inplace=True), nn.Conv2d(in_channels=channel[1], out_channels=channel[2], kernel_size=1, padding=0), nn.BatchNorm2d(channel[2]), nn.Sigmoid(), ) return att_block def forward(self, x): g_encoder, g_decoder, g_maxpool, g_upsampl, indices = ([0] * 5 for _ in range(5)) for i in range(5): g_encoder[i], g_decoder[-i - 1] = ([0] * 2 for _ in range(2)) # define attention list for tasks atten_encoder, atten_decoder = ([0] * 2 for _ in range(2)) for i in range(2): atten_encoder[i], atten_decoder[i] = ([0] * 5 for _ in range(2)) for i in range(2): for j in range(5): atten_encoder[i][j], atten_decoder[i][j] = ([0] * 3 for _ in range(2)) # define global shared network for i in range(5): if i == 0: g_encoder[i][0] = self.encoder_block[i](x) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) else: g_encoder[i][0] = self.encoder_block[i](g_maxpool[i - 1]) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) for i in range(5): if i == 0: g_upsampl[i] = self.up_sampling(g_maxpool[-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) else: g_upsampl[i] = self.up_sampling(g_decoder[i - 1][-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) # define task dependent attention module for i in range(2): for j in range(5): if j == 0: atten_encoder[i][j][0] = self.encoder_att[i][j](g_encoder[j][0]) atten_encoder[i][j][1] = (atten_encoder[i][j][0]) * g_encoder[j][1] atten_encoder[i][j][2] = self.encoder_block_att[j](atten_encoder[i][j][1]) atten_encoder[i][j][2] = F.max_pool2d(atten_encoder[i][j][2], kernel_size=2, stride=2) else: atten_encoder[i][j][0] = self.encoder_att[i][j](torch.cat( (g_encoder[j][0], atten_encoder[i][j - 1][2]), dim=1)) atten_encoder[i][j][1] = (atten_encoder[i][j][0]) * g_encoder[j][1] atten_encoder[i][j][2] = self.encoder_block_att[j](atten_encoder[i][j][1]) atten_encoder[i][j][2] = F.max_pool2d(atten_encoder[i][j][2], kernel_size=2, stride=2) for j in range(5): if j == 0: atten_decoder[i][j][0] = F.interpolate(atten_encoder[i][-1][-1], scale_factor=2, mode='bilinear', align_corners=True) atten_decoder[i][j][0] = self.decoder_block_att[-j - 1](atten_decoder[i][j][0]) atten_decoder[i][j][1] = self.decoder_att[i][-j - 1](torch.cat( (g_upsampl[j], atten_decoder[i][j][0]), dim=1)) atten_decoder[i][j][2] = (atten_decoder[i][j][1]) * g_decoder[j][-1] else: atten_decoder[i][j][0] = F.interpolate(atten_decoder[i][j - 1][2], scale_factor=2, mode='bilinear', align_corners=True) atten_decoder[i][j][0] = self.decoder_block_att[-j - 1](atten_decoder[i][j][0]) atten_decoder[i][j][1] = self.decoder_att[i][-j - 1](torch.cat( (g_upsampl[j], atten_decoder[i][j][0]), dim=1)) atten_decoder[i][j][2] = (atten_decoder[i][j][1]) * g_decoder[j][-1] # define task prediction layers t1_pred = F.log_softmax(self.pred_task1(atten_decoder[0][-1][-1]), dim=1) t2_pred = self.pred_task2(atten_decoder[1][-1][-1]) #t3_pred = self.pred_task3(atten_decoder[2][-1][-1]) #t3_pred = t3_pred / torch.norm(t3_pred, p=2, dim=1, keepdim=True) return [t1_pred, t2_pred], self.logsigma # define model, optimiser and scheduler device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") SegNet_SPLIT = SegNet().to(device) optimizer = optim.Adam(SegNet_SPLIT.parameters(), lr=1e-4) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.5) print('Parameter Space: ABS: {:.1f}, REL: {:.4f}'.format(count_parameters(SegNet_SPLIT), count_parameters(SegNet_SPLIT) / 24981069)) print( 'LOSS FORMAT: SEMANTIC_LOSS MEAN_IOU PIX_ACC | DEPTH_LOSS ABS_ERR REL_ERR | NORMAL_LOSS MEAN MED <11.25 <22.5 <30') # define dataset dataset_path = opt.dataroot if opt.apply_augmentation: train_set = CityScapes(root=dataset_path, train=True, augmentation=True) print('Applying data augmentation.') else: train_set = CityScapes(root=dataset_path, train=True) print('Standard training strategy without data augmentation.') test_set = CityScapes(root=dataset_path, train=False) batch_size = 8 train_loader = torch.utils.data.DataLoader(dataset=train_set, batch_size=batch_size, shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=test_set, batch_size=batch_size, shuffle=False) # Train and evaluate multi-task network multi_task_trainer(train_loader, test_loader, SegNet_SPLIT, device, optimizer, scheduler, opt, 200)
11,395
50.103139
119
py
sdmgrad
sdmgrad-main/cityscapes/min_norm_solvers.py
# This code is from # Multi-Task Learning as Multi-Objective Optimization # Ozan Sener, Vladlen Koltun # Neural Information Processing Systems (NeurIPS) 2018 # https://github.com/intel-isl/MultiObjectiveOptimization import numpy as np import torch class MinNormSolver: MAX_ITER = 20 STOP_CRIT = 1e-5 def _min_norm_element_from2(v1v1, v1v2, v2v2): """ Analytical solution for min_{c} |cx_1 + (1-c)x_2|_2^2 d is the distance (objective) optimzed v1v1 = <x1,x1> v1v2 = <x1,x2> v2v2 = <x2,x2> """ if v1v2 >= v1v1: # Case: Fig 1, third column gamma = 0.999 cost = v1v1 return gamma, cost if v1v2 >= v2v2: # Case: Fig 1, first column gamma = 0.001 cost = v2v2 return gamma, cost # Case: Fig 1, second column gamma = -1.0 * ((v1v2 - v2v2) / (v1v1 + v2v2 - 2 * v1v2)) cost = v2v2 + gamma * (v1v2 - v2v2) return gamma, cost def _min_norm_2d(vecs, dps): """ Find the minimum norm solution as combination of two points This is correct only in 2D ie. min_c |\sum c_i x_i|_2^2 st. \sum c_i = 1 , 1 >= c_1 >= 0 for all i, c_i + c_j = 1.0 for some i, j """ dmin = np.inf for i in range(len(vecs)): for j in range(i + 1, len(vecs)): if (i, j) not in dps: dps[(i, j)] = (vecs[i] * vecs[j]).sum().item() dps[(j, i)] = dps[(i, j)] if (i, i) not in dps: dps[(i, i)] = (vecs[i] * vecs[i]).sum().item() if (j, j) not in dps: dps[(j, j)] = (vecs[j] * vecs[j]).sum().item() c, d = MinNormSolver._min_norm_element_from2(dps[(i, i)], dps[(i, j)], dps[(j, j)]) if d < dmin: dmin = d sol = [(i, j), c, d] return sol, dps def _projection2simplex(y): """ Given y, it solves argmin_z |y-z|_2 st \sum z = 1 , 1 >= z_i >= 0 for all i """ m = len(y) sorted_y = np.flip(np.sort(y), axis=0) tmpsum = 0.0 tmax_f = (np.sum(y) - 1.0) / m for i in range(m - 1): tmpsum += sorted_y[i] tmax = (tmpsum - 1) / (i + 1.0) if tmax > sorted_y[i + 1]: tmax_f = tmax break return np.maximum(y - tmax_f, np.zeros(y.shape)) def _next_point(cur_val, grad, n): proj_grad = grad - (np.sum(grad) / n) tm1 = -1.0 * cur_val[proj_grad < 0] / proj_grad[proj_grad < 0] tm2 = (1.0 - cur_val[proj_grad > 0]) / (proj_grad[proj_grad > 0]) skippers = np.sum(tm1 < 1e-7) + np.sum(tm2 < 1e-7) t = 1 if len(tm1[tm1 > 1e-7]) > 0: t = np.min(tm1[tm1 > 1e-7]) if len(tm2[tm2 > 1e-7]) > 0: t = min(t, np.min(tm2[tm2 > 1e-7])) next_point = proj_grad * t + cur_val next_point = MinNormSolver._projection2simplex(next_point) return next_point def find_min_norm_element(vecs): """ Given a list of vectors (vecs), this method finds the minimum norm element in the convex hull as min |u|_2 st. u = \sum c_i vecs[i] and \sum c_i = 1. It is quite geometric, and the main idea is the fact that if d_{ij} = min |u|_2 st u = c x_i + (1-c) x_j; the solution lies in (0, d_{i,j}) Hence, we find the best 2-task solution, and then run the projected gradient descent until convergence """ # Solution lying at the combination of two points dps = {} init_sol, dps = MinNormSolver._min_norm_2d(vecs, dps) n = len(vecs) sol_vec = np.zeros(n) sol_vec[init_sol[0][0]] = init_sol[1] sol_vec[init_sol[0][1]] = 1 - init_sol[1] if n < 3: # This is optimal for n=2, so return the solution return sol_vec, init_sol[2] iter_count = 0 grad_mat = np.zeros((n, n)) for i in range(n): for j in range(n): grad_mat[i, j] = dps[(i, j)] while iter_count < MinNormSolver.MAX_ITER: grad_dir = -1.0 * np.dot(grad_mat, sol_vec) new_point = MinNormSolver._next_point(sol_vec, grad_dir, n) # Re-compute the inner products for line search v1v1 = 0.0 v1v2 = 0.0 v2v2 = 0.0 for i in range(n): for j in range(n): v1v1 += sol_vec[i] * sol_vec[j] * dps[(i, j)] v1v2 += sol_vec[i] * new_point[j] * dps[(i, j)] v2v2 += new_point[i] * new_point[j] * dps[(i, j)] nc, nd = MinNormSolver._min_norm_element_from2(v1v1, v1v2, v2v2) new_sol_vec = nc * sol_vec + (1 - nc) * new_point change = new_sol_vec - sol_vec if np.sum(np.abs(change)) < MinNormSolver.STOP_CRIT: return sol_vec, nd sol_vec = new_sol_vec def find_min_norm_element_FW(vecs): """ Given a list of vectors (vecs), this method finds the minimum norm element in the convex hull as min |u|_2 st. u = \sum c_i vecs[i] and \sum c_i = 1. It is quite geometric, and the main idea is the fact that if d_{ij} = min |u|_2 st u = c x_i + (1-c) x_j; the solution lies in (0, d_{i,j}) Hence, we find the best 2-task solution, and then run the Frank Wolfe until convergence """ # Solution lying at the combination of two points dps = {} init_sol, dps = MinNormSolver._min_norm_2d(vecs, dps) n = len(vecs) sol_vec = np.zeros(n) sol_vec[init_sol[0][0]] = init_sol[1] sol_vec[init_sol[0][1]] = 1 - init_sol[1] if n < 3: # This is optimal for n=2, so return the solution return sol_vec, init_sol[2] iter_count = 0 grad_mat = np.zeros((n, n)) for i in range(n): for j in range(n): grad_mat[i, j] = dps[(i, j)] while iter_count < MinNormSolver.MAX_ITER: t_iter = np.argmin(np.dot(grad_mat, sol_vec)) v1v1 = np.dot(sol_vec, np.dot(grad_mat, sol_vec)) v1v2 = np.dot(sol_vec, grad_mat[:, t_iter]) v2v2 = grad_mat[t_iter, t_iter] nc, nd = MinNormSolver._min_norm_element_from2(v1v1, v1v2, v2v2) new_sol_vec = nc * sol_vec new_sol_vec[t_iter] += 1 - nc change = new_sol_vec - sol_vec if np.sum(np.abs(change)) < MinNormSolver.STOP_CRIT: return sol_vec, nd sol_vec = new_sol_vec def gradient_normalizers(grads, losses, normalization_type): gn = {} if normalization_type == 'l2': for t in grads: gn[t] = np.sqrt(np.sum([gr.pow(2).sum().data[0] for gr in grads[t]])) elif normalization_type == 'loss': for t in grads: gn[t] = losses[t] elif normalization_type == 'loss+': for t in grads: gn[t] = losses[t] * np.sqrt(np.sum([gr.pow(2).sum().data[0] for gr in grads[t]])) elif normalization_type == 'none': for t in grads: gn[t] = 1.0 else: print('ERROR: Invalid Normalization Type') return gn
7,358
35.979899
147
py
sdmgrad
sdmgrad-main/cityscapes/model_segnet_mtan.py
import torch.nn as nn import torch.optim as optim import torch.nn.functional as F import argparse import torch.utils.data.sampler as sampler from create_dataset import * from utils import * parser = argparse.ArgumentParser(description='Multi-task: Attention Network') parser.add_argument('--weight', default='equal', type=str, help='multi-task weighting: equal, uncert, dwa') parser.add_argument('--dataroot', default='cityscapes', type=str, help='dataset root') parser.add_argument('--temp', default=2.0, type=float, help='temperature for DWA (must be positive)') parser.add_argument('--seed', default=0, type=int, help='control seed') parser.add_argument('--apply_augmentation', action='store_true', help='toggle to apply data augmentation on NYUv2') opt = parser.parse_args() class SegNet(nn.Module): def __init__(self): super(SegNet, self).__init__() # initialise network parameters filter = [64, 128, 256, 512, 512] self.class_nb = 7 # define encoder decoder layers self.encoder_block = nn.ModuleList([self.conv_layer([3, filter[0]])]) self.decoder_block = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): self.encoder_block.append(self.conv_layer([filter[i], filter[i + 1]])) self.decoder_block.append(self.conv_layer([filter[i + 1], filter[i]])) # define convolution layer self.conv_block_enc = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) self.conv_block_dec = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): if i == 0: self.conv_block_enc.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.conv_block_dec.append(self.conv_layer([filter[i], filter[i]])) else: self.conv_block_enc.append( nn.Sequential(self.conv_layer([filter[i + 1], filter[i + 1]]), self.conv_layer([filter[i + 1], filter[i + 1]]))) self.conv_block_dec.append( nn.Sequential(self.conv_layer([filter[i], filter[i]]), self.conv_layer([filter[i], filter[i]]))) # define task attention layers self.encoder_att = nn.ModuleList([nn.ModuleList([self.att_layer([filter[0], filter[0], filter[0]])])]) self.decoder_att = nn.ModuleList([nn.ModuleList([self.att_layer([2 * filter[0], filter[0], filter[0]])])]) self.encoder_block_att = nn.ModuleList([self.conv_layer([filter[0], filter[1]])]) self.decoder_block_att = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for j in range(2): if j < 1: self.encoder_att.append(nn.ModuleList([self.att_layer([filter[0], filter[0], filter[0]])])) self.decoder_att.append(nn.ModuleList([self.att_layer([2 * filter[0], filter[0], filter[0]])])) for i in range(4): self.encoder_att[j].append(self.att_layer([2 * filter[i + 1], filter[i + 1], filter[i + 1]])) self.decoder_att[j].append(self.att_layer([filter[i + 1] + filter[i], filter[i], filter[i]])) for i in range(4): if i < 3: self.encoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 2]])) self.decoder_block_att.append(self.conv_layer([filter[i + 1], filter[i]])) else: self.encoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.decoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.pred_task1 = self.conv_layer([filter[0], self.class_nb], pred=True) self.pred_task2 = self.conv_layer([filter[0], 1], pred=True) #self.pred_task3 = self.conv_layer([filter[0], 3], pred=True) # define pooling and unpooling functions self.down_sampling = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True) self.up_sampling = nn.MaxUnpool2d(kernel_size=2, stride=2) self.logsigma = nn.Parameter(torch.FloatTensor([-0.5, -0.5, -0.5])) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) def conv_layer(self, channel, pred=False): if not pred: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(num_features=channel[1]), nn.ReLU(inplace=True), ) else: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), ) return conv_block def att_layer(self, channel): att_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), nn.BatchNorm2d(channel[1]), nn.ReLU(inplace=True), nn.Conv2d(in_channels=channel[1], out_channels=channel[2], kernel_size=1, padding=0), nn.BatchNorm2d(channel[2]), nn.Sigmoid(), ) return att_block def forward(self, x): g_encoder, g_decoder, g_maxpool, g_upsampl, indices = ([0] * 5 for _ in range(5)) for i in range(5): g_encoder[i], g_decoder[-i - 1] = ([0] * 2 for _ in range(2)) # define attention list for tasks atten_encoder, atten_decoder = ([0] * 2 for _ in range(2)) for i in range(2): atten_encoder[i], atten_decoder[i] = ([0] * 5 for _ in range(2)) for i in range(2): for j in range(5): atten_encoder[i][j], atten_decoder[i][j] = ([0] * 3 for _ in range(2)) # define global shared network for i in range(5): if i == 0: g_encoder[i][0] = self.encoder_block[i](x) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) else: g_encoder[i][0] = self.encoder_block[i](g_maxpool[i - 1]) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) for i in range(5): if i == 0: g_upsampl[i] = self.up_sampling(g_maxpool[-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) else: g_upsampl[i] = self.up_sampling(g_decoder[i - 1][-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) # define task dependent attention module for i in range(2): for j in range(5): if j == 0: atten_encoder[i][j][0] = self.encoder_att[i][j](g_encoder[j][0]) atten_encoder[i][j][1] = (atten_encoder[i][j][0]) * g_encoder[j][1] atten_encoder[i][j][2] = self.encoder_block_att[j](atten_encoder[i][j][1]) atten_encoder[i][j][2] = F.max_pool2d(atten_encoder[i][j][2], kernel_size=2, stride=2) else: atten_encoder[i][j][0] = self.encoder_att[i][j](torch.cat( (g_encoder[j][0], atten_encoder[i][j - 1][2]), dim=1)) atten_encoder[i][j][1] = (atten_encoder[i][j][0]) * g_encoder[j][1] atten_encoder[i][j][2] = self.encoder_block_att[j](atten_encoder[i][j][1]) atten_encoder[i][j][2] = F.max_pool2d(atten_encoder[i][j][2], kernel_size=2, stride=2) for j in range(5): if j == 0: atten_decoder[i][j][0] = F.interpolate(atten_encoder[i][-1][-1], scale_factor=2, mode='bilinear', align_corners=True) atten_decoder[i][j][0] = self.decoder_block_att[-j - 1](atten_decoder[i][j][0]) atten_decoder[i][j][1] = self.decoder_att[i][-j - 1](torch.cat( (g_upsampl[j], atten_decoder[i][j][0]), dim=1)) atten_decoder[i][j][2] = (atten_decoder[i][j][1]) * g_decoder[j][-1] else: atten_decoder[i][j][0] = F.interpolate(atten_decoder[i][j - 1][2], scale_factor=2, mode='bilinear', align_corners=True) atten_decoder[i][j][0] = self.decoder_block_att[-j - 1](atten_decoder[i][j][0]) atten_decoder[i][j][1] = self.decoder_att[i][-j - 1](torch.cat( (g_upsampl[j], atten_decoder[i][j][0]), dim=1)) atten_decoder[i][j][2] = (atten_decoder[i][j][1]) * g_decoder[j][-1] # define task prediction layers t1_pred = F.log_softmax(self.pred_task1(atten_decoder[0][-1][-1]), dim=1) t2_pred = self.pred_task2(atten_decoder[1][-1][-1]) #t3_pred = self.pred_task3(atten_decoder[2][-1][-1]) #t3_pred = t3_pred / torch.norm(t3_pred, p=2, dim=1, keepdim=True) return [t1_pred, t2_pred], self.logsigma control_seed(opt.seed) # define model, optimiser and scheduler device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") SegNet_MTAN = SegNet().to(device) optimizer = optim.Adam(SegNet_MTAN.parameters(), lr=1e-4) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.5) print('Parameter Space: ABS: {:.1f}, REL: {:.4f}'.format(count_parameters(SegNet_MTAN), count_parameters(SegNet_MTAN) / 24981069)) print( 'LOSS FORMAT: SEMANTIC_LOSS MEAN_IOU PIX_ACC | DEPTH_LOSS ABS_ERR REL_ERR | NORMAL_LOSS MEAN MED <11.25 <22.5 <30') # define dataset dataset_path = opt.dataroot if opt.apply_augmentation: train_set = CityScapes(root=dataset_path, train=True, augmentation=True) print('Applying data augmentation.') else: train_set = CityScapes(root=dataset_path, train=True) print('Standard training strategy without data augmentation.') test_set = CityScapes(root=dataset_path, train=False) batch_size = 8 train_loader = torch.utils.data.DataLoader(dataset=train_set, batch_size=batch_size, shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=test_set, batch_size=batch_size, shuffle=False) # Train and evaluate multi-task network multi_task_trainer(train_loader, test_loader, SegNet_MTAN, device, optimizer, scheduler, opt, 200)
11,396
49.879464
119
py
sdmgrad
sdmgrad-main/cityscapes/create_dataset.py
from torch.utils.data.dataset import Dataset import os import torch import torch.nn.functional as F import fnmatch import numpy as np import random class RandomScaleCrop(object): """ Credit to Jialong Wu from https://github.com/lorenmt/mtan/issues/34. """ def __init__(self, scale=[1.0, 1.2, 1.5]): self.scale = scale def __call__(self, img, label, depth, normal): height, width = img.shape[-2:] sc = self.scale[random.randint(0, len(self.scale) - 1)] h, w = int(height / sc), int(width / sc) i = random.randint(0, height - h) j = random.randint(0, width - w) img_ = F.interpolate(img[None, :, i:i + h, j:j + w], size=(height, width), mode='bilinear', align_corners=True).squeeze(0) label_ = F.interpolate(label[None, None, i:i + h, j:j + w], size=(height, width), mode='nearest').squeeze(0).squeeze(0) depth_ = F.interpolate(depth[None, :, i:i + h, j:j + w], size=(height, width), mode='nearest').squeeze(0) normal_ = F.interpolate(normal[None, :, i:i + h, j:j + w], size=(height, width), mode='bilinear', align_corners=True).squeeze(0) return img_, label_, depth_ / sc, normal_ class RandomScaleCropCityScapes(object): """ Credit to Jialong Wu from https://github.com/lorenmt/mtan/issues/34. """ def __init__(self, scale=[1.0, 1.2, 1.5]): self.scale = scale def __call__(self, img, label, depth): height, width = img.shape[-2:] sc = self.scale[random.randint(0, len(self.scale) - 1)] h, w = int(height / sc), int(width / sc) i = random.randint(0, height - h) j = random.randint(0, width - w) img_ = F.interpolate(img[None, :, i:i + h, j:j + w], size=(height, width), mode='bilinear', align_corners=True).squeeze(0) label_ = F.interpolate(label[None, None, i:i + h, j:j + w], size=(height, width), mode='nearest').squeeze(0).squeeze(0) depth_ = F.interpolate(depth[None, :, i:i + h, j:j + w], size=(height, width), mode='nearest').squeeze(0) return img_, label_, depth_ / sc class NYUv2(Dataset): """ We could further improve the performance with the data augmentation of NYUv2 defined in: [1] PAD-Net: Multi-Tasks Guided Prediction-and-Distillation Network for Simultaneous Depth Estimation and Scene Parsing [2] Pattern affinitive propagation across depth, surface normal and semantic segmentation [3] Mti-net: Multiscale task interaction networks for multi-task learning 1. Random scale in a selected raio 1.0, 1.2, and 1.5. 2. Random horizontal flip. Please note that: all baselines and MTAN did NOT apply data augmentation in the original paper. """ def __init__(self, root, train=True, augmentation=False): self.train = train self.root = os.path.expanduser(root) self.augmentation = augmentation # read the data file if train: self.data_path = root + '/train' else: self.data_path = root + '/val' # calculate data length self.data_len = len(fnmatch.filter(os.listdir(self.data_path + '/image'), '*.npy')) def __getitem__(self, index): # load data from the pre-processed npy files image = torch.from_numpy(np.moveaxis(np.load(self.data_path + '/image/{:d}.npy'.format(index)), -1, 0)) semantic = torch.from_numpy(np.load(self.data_path + '/label/{:d}.npy'.format(index))) depth = torch.from_numpy(np.moveaxis(np.load(self.data_path + '/depth/{:d}.npy'.format(index)), -1, 0)) normal = torch.from_numpy(np.moveaxis(np.load(self.data_path + '/normal/{:d}.npy'.format(index)), -1, 0)) # apply data augmentation if required if self.augmentation: image, semantic, depth, normal = RandomScaleCrop()(image, semantic, depth, normal) if torch.rand(1) < 0.5: image = torch.flip(image, dims=[2]) semantic = torch.flip(semantic, dims=[1]) depth = torch.flip(depth, dims=[2]) normal = torch.flip(normal, dims=[2]) normal[0, :, :] = -normal[0, :, :] return image.float(), semantic.float(), depth.float(), normal.float() def __len__(self): return self.data_len class CityScapes(Dataset): """ We could further improve the performance with the data augmentation of NYUv2 defined in: [1] PAD-Net: Multi-Tasks Guided Prediction-and-Distillation Network for Simultaneous Depth Estimation and Scene Parsing [2] Pattern affinitive propagation across depth, surface normal and semantic segmentation [3] Mti-net: Multiscale task interaction networks for multi-task learning 1. Random scale in a selected raio 1.0, 1.2, and 1.5. 2. Random horizontal flip. Please note that: all baselines and MTAN did NOT apply data augmentation in the original paper. """ def __init__(self, root, train=True, augmentation=False): self.train = train self.root = os.path.expanduser(root) self.augmentation = augmentation # read the data file if train: self.data_path = root + '/train' else: self.data_path = root + '/val' # calculate data length self.data_len = len(fnmatch.filter(os.listdir(self.data_path + '/image'), '*.npy')) def __getitem__(self, index): # load data from the pre-processed npy files image = torch.from_numpy(np.moveaxis(np.load(self.data_path + '/image/{:d}.npy'.format(index)), -1, 0)) semantic = torch.from_numpy(np.load(self.data_path + '/label_7/{:d}.npy'.format(index))) depth = torch.from_numpy(np.moveaxis(np.load(self.data_path + '/depth/{:d}.npy'.format(index)), -1, 0)) # apply data augmentation if required if self.augmentation: image, semantic, depth = RandomScaleCropCityScapes()(image, semantic, depth) if torch.rand(1) < 0.5: image = torch.flip(image, dims=[2]) semantic = torch.flip(semantic, dims=[1]) depth = torch.flip(depth, dims=[2]) return image.float(), semantic.float(), depth.float() def __len__(self): return self.data_len
6,513
41.298701
127
py
sdmgrad
sdmgrad-main/cityscapes/model_segnet_cross.py
import torch.nn as nn import torch.optim as optim import torch.nn.functional as F import argparse import torch.utils.data.sampler as sampler from create_dataset import * from utils import * parser = argparse.ArgumentParser(description='Multi-task: Cross') parser.add_argument('--weight', default='equal', type=str, help='multi-task weighting: equal, uncert, dwa') parser.add_argument('--dataroot', default='cityscapes', type=str, help='dataset root') parser.add_argument('--temp', default=2.0, type=float, help='temperature for DWA (must be positive)') parser.add_argument('--seed', default=0, type=int, help='control seed') parser.add_argument('--apply_augmentation', action='store_true', help='toggle to apply data augmentation on NYUv2') opt = parser.parse_args() class SegNet(nn.Module): def __init__(self): super(SegNet, self).__init__() # initialise network parameters filter = [64, 128, 256, 512, 512] self.class_nb = 7 # define encoder decoder layers self.encoder_block_t = nn.ModuleList( [nn.ModuleList([self.conv_layer([3, filter[0], filter[0]], bottle_neck=True)])]) self.decoder_block_t = nn.ModuleList( [nn.ModuleList([self.conv_layer([filter[0], filter[0], filter[0]], bottle_neck=True)])]) for j in range(2): if j < 1: self.encoder_block_t.append( nn.ModuleList([self.conv_layer([3, filter[0], filter[0]], bottle_neck=True)])) self.decoder_block_t.append( nn.ModuleList([self.conv_layer([filter[0], filter[0], filter[0]], bottle_neck=True)])) for i in range(4): if i == 0: self.encoder_block_t[j].append( self.conv_layer([filter[i], filter[i + 1], filter[i + 1]], bottle_neck=True)) self.decoder_block_t[j].append( self.conv_layer([filter[i + 1], filter[i], filter[i]], bottle_neck=True)) else: self.encoder_block_t[j].append( self.conv_layer([filter[i], filter[i + 1], filter[i + 1]], bottle_neck=False)) self.decoder_block_t[j].append( self.conv_layer([filter[i + 1], filter[i], filter[i]], bottle_neck=False)) # define cross-stitch units self.cs_unit_encoder = nn.Parameter(data=torch.ones(4, 2)) self.cs_unit_decoder = nn.Parameter(data=torch.ones(5, 2)) # define task specific layers self.pred_task1 = self.conv_layer([filter[0], self.class_nb], bottle_neck=True, pred_layer=True) self.pred_task2 = self.conv_layer([filter[0], 1], bottle_neck=True, pred_layer=True) #self.pred_task3 = self.conv_layer([filter[0], 3], bottle_neck=True, pred_layer=True) # define pooling and unpooling functions self.down_sampling = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True) self.up_sampling = nn.MaxUnpool2d(kernel_size=2, stride=2) self.logsigma = nn.Parameter(torch.FloatTensor([-0.5, -0.5])) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.xavier_uniform_(m.weight) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.xavier_uniform_(m.weight) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Parameter): nn.init.constant(m.weight, 1) def conv_layer(self, channel, bottle_neck, pred_layer=False): if bottle_neck: if not pred_layer: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(channel[1]), nn.ReLU(inplace=True), nn.Conv2d(in_channels=channel[1], out_channels=channel[2], kernel_size=3, padding=1), nn.BatchNorm2d(channel[2]), nn.ReLU(inplace=True), ) else: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), ) else: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(channel[1]), nn.ReLU(inplace=True), nn.Conv2d(in_channels=channel[1], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(channel[1]), nn.ReLU(inplace=True), nn.Conv2d(in_channels=channel[1], out_channels=channel[2], kernel_size=3, padding=1), nn.BatchNorm2d(channel[2]), nn.ReLU(inplace=True), ) return conv_block def forward(self, x): encoder_conv_t, decoder_conv_t, encoder_samp_t, decoder_samp_t, indices_t = ([0] * 2 for _ in range(5)) for i in range(2): encoder_conv_t[i], decoder_conv_t[i], encoder_samp_t[i], decoder_samp_t[i], indices_t[i] = ( [0] * 5 for _ in range(5)) # task branch 1 for i in range(5): for j in range(2): if i == 0: encoder_conv_t[j][i] = self.encoder_block_t[j][i](x) encoder_samp_t[j][i], indices_t[j][i] = self.down_sampling(encoder_conv_t[j][i]) else: encoder_cross_stitch = self.cs_unit_encoder[i - 1][0] * encoder_samp_t[0][i - 1] + \ self.cs_unit_encoder[i - 1][1] * encoder_samp_t[1][i - 1] #self.cs_unit_encoder[i - 1][2] * encoder_samp_t[2][i - 1] encoder_conv_t[j][i] = self.encoder_block_t[j][i](encoder_cross_stitch) encoder_samp_t[j][i], indices_t[j][i] = self.down_sampling(encoder_conv_t[j][i]) for i in range(5): for j in range(2): if i == 0: decoder_cross_stitch = self.cs_unit_decoder[i][0] * encoder_samp_t[0][-1] + \ self.cs_unit_decoder[i][1] * encoder_samp_t[1][-1] #self.cs_unit_decoder[i][2] * encoder_samp_t[2][-1] decoder_samp_t[j][i] = self.up_sampling(decoder_cross_stitch, indices_t[j][-i - 1]) decoder_conv_t[j][i] = self.decoder_block_t[j][-i - 1](decoder_samp_t[j][i]) else: decoder_cross_stitch = self.cs_unit_decoder[i][0] * decoder_conv_t[0][i - 1] + \ self.cs_unit_decoder[i][1] * decoder_conv_t[1][i - 1] #self.cs_unit_decoder[i][2] * decoder_conv_t[2][i - 1] decoder_samp_t[j][i] = self.up_sampling(decoder_cross_stitch, indices_t[j][-i - 1]) decoder_conv_t[j][i] = self.decoder_block_t[j][-i - 1](decoder_samp_t[j][i]) # define task prediction layers t1_pred = F.log_softmax(self.pred_task1(decoder_conv_t[0][-1]), dim=1) t2_pred = self.pred_task2(decoder_conv_t[1][-1]) #t3_pred = self.pred_task3(decoder_conv_t[2][-1]) #t3_pred = t3_pred / torch.norm(t3_pred, p=2, dim=1, keepdim=True) return [t1_pred, t2_pred], self.logsigma control_seed(opt.seed) # define model, optimiser and scheduler device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") SegNet_CROSS = SegNet().to(device) optimizer = optim.Adam(SegNet_CROSS.parameters(), lr=1e-4) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.5) print('Parameter Space: ABS: {:.1f}, REL: {:.4f}'.format(count_parameters(SegNet_CROSS), count_parameters(SegNet_CROSS) / 24981069)) print( 'LOSS FORMAT: SEMANTIC_LOSS MEAN_IOU PIX_ACC | DEPTH_LOSS ABS_ERR REL_ERR | NORMAL_LOSS MEAN MED <11.25 <22.5 <30') # define dataset dataset_path = opt.dataroot if opt.apply_augmentation: train_set = CityScapes(root=dataset_path, train=True, augmentation=True) print('Applying data augmentation on CityScapes.') else: train_set = CityScapes(root=dataset_path, train=True) print('Standard training strategy without data augmentation.') test_set = CityScapes(root=dataset_path, train=False) batch_size = 8 train_loader = torch.utils.data.DataLoader(dataset=train_set, batch_size=batch_size, shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=test_set, batch_size=batch_size, shuffle=False) # Train and evaluate multi-task network multi_task_trainer(train_loader, test_loader, SegNet_CROSS, device, optimizer, scheduler, opt, 200)
9,044
48.42623
119
py
sdmgrad
sdmgrad-main/cityscapes/model_segnet_mt.py
import torch.nn as nn import torch.optim as optim import torch.nn.functional as F import argparse import torch.utils.data.sampler as sampler from create_dataset import * from utils import * parser = argparse.ArgumentParser(description='Multi-task: Attention Network') parser.add_argument('--method', default='sdmgrad', type=str, help='which optimization algorithm to use') parser.add_argument('--weight', default='equal', type=str, help='multi-task weighting: equal, uncert, dwa') parser.add_argument('--dataroot', default='cityscapes', type=str, help='dataset root') parser.add_argument('--temp', default=2.0, type=float, help='temperature for DWA (must be positive)') parser.add_argument('--alpha', default=0.3, type=float, help='the alpha') parser.add_argument('--lr', default=1e-4, type=float, help='the learning rate') parser.add_argument('--seed', default=1, type=int, help='control seed') parser.add_argument('--niter', default=20, type=int, help='number of inner iteration') parser.add_argument('--apply_augmentation', action='store_true', help='toggle to apply data augmentation on NYUv2') opt = parser.parse_args() class SegNet(nn.Module): def __init__(self): super(SegNet, self).__init__() # initialise network parameters filter = [64, 128, 256, 512, 512] self.class_nb = 7 # define encoder decoder layers self.encoder_block = nn.ModuleList([self.conv_layer([3, filter[0]])]) self.decoder_block = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): self.encoder_block.append(self.conv_layer([filter[i], filter[i + 1]])) self.decoder_block.append(self.conv_layer([filter[i + 1], filter[i]])) # define convolution layer self.conv_block_enc = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) self.conv_block_dec = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for i in range(4): if i == 0: self.conv_block_enc.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.conv_block_dec.append(self.conv_layer([filter[i], filter[i]])) else: self.conv_block_enc.append( nn.Sequential(self.conv_layer([filter[i + 1], filter[i + 1]]), self.conv_layer([filter[i + 1], filter[i + 1]]))) self.conv_block_dec.append( nn.Sequential(self.conv_layer([filter[i], filter[i]]), self.conv_layer([filter[i], filter[i]]))) # define task attention layers self.encoder_att = nn.ModuleList([nn.ModuleList([self.att_layer([filter[0], filter[0], filter[0]])])]) self.decoder_att = nn.ModuleList([nn.ModuleList([self.att_layer([2 * filter[0], filter[0], filter[0]])])]) self.encoder_block_att = nn.ModuleList([self.conv_layer([filter[0], filter[1]])]) self.decoder_block_att = nn.ModuleList([self.conv_layer([filter[0], filter[0]])]) for j in range(2): if j < 1: self.encoder_att.append(nn.ModuleList([self.att_layer([filter[0], filter[0], filter[0]])])) self.decoder_att.append(nn.ModuleList([self.att_layer([2 * filter[0], filter[0], filter[0]])])) for i in range(4): self.encoder_att[j].append(self.att_layer([2 * filter[i + 1], filter[i + 1], filter[i + 1]])) self.decoder_att[j].append(self.att_layer([filter[i + 1] + filter[i], filter[i], filter[i]])) for i in range(4): if i < 3: self.encoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 2]])) self.decoder_block_att.append(self.conv_layer([filter[i + 1], filter[i]])) else: self.encoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.decoder_block_att.append(self.conv_layer([filter[i + 1], filter[i + 1]])) self.pred_task1 = self.conv_layer([filter[0], self.class_nb], pred=True) self.pred_task2 = self.conv_layer([filter[0], 1], pred=True) #self.pred_task3 = self.conv_layer([filter[0], 3], pred=True) # define pooling and unpooling functions self.down_sampling = nn.MaxPool2d(kernel_size=2, stride=2, return_indices=True) self.up_sampling = nn.MaxUnpool2d(kernel_size=2, stride=2) self.logsigma = nn.Parameter(torch.FloatTensor([-0.5, -0.5, -0.5])) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.xavier_normal_(m.weight) nn.init.constant_(m.bias, 0) def shared_modules(self): return [ self.encoder_block, self.decoder_block, self.conv_block_enc, self.conv_block_dec, self.encoder_block_att, self.decoder_block_att, self.down_sampling, self.up_sampling ] def zero_grad_shared_modules(self): for mm in self.shared_modules(): mm.zero_grad() def conv_layer(self, channel, pred=False): if not pred: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=3, padding=1), nn.BatchNorm2d(num_features=channel[1]), nn.ReLU(inplace=True), ) else: conv_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[0], kernel_size=3, padding=1), nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), ) return conv_block def att_layer(self, channel): att_block = nn.Sequential( nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=1, padding=0), nn.BatchNorm2d(channel[1]), nn.ReLU(inplace=True), nn.Conv2d(in_channels=channel[1], out_channels=channel[2], kernel_size=1, padding=0), nn.BatchNorm2d(channel[2]), nn.Sigmoid(), ) return att_block def forward(self, x): g_encoder, g_decoder, g_maxpool, g_upsampl, indices = ([0] * 5 for _ in range(5)) for i in range(5): g_encoder[i], g_decoder[-i - 1] = ([0] * 2 for _ in range(2)) # define attention list for tasks atten_encoder, atten_decoder = ([0] * 2 for _ in range(2)) for i in range(2): atten_encoder[i], atten_decoder[i] = ([0] * 5 for _ in range(2)) for i in range(2): for j in range(5): atten_encoder[i][j], atten_decoder[i][j] = ([0] * 3 for _ in range(2)) # define global shared network for i in range(5): if i == 0: g_encoder[i][0] = self.encoder_block[i](x) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) else: g_encoder[i][0] = self.encoder_block[i](g_maxpool[i - 1]) g_encoder[i][1] = self.conv_block_enc[i](g_encoder[i][0]) g_maxpool[i], indices[i] = self.down_sampling(g_encoder[i][1]) for i in range(5): if i == 0: g_upsampl[i] = self.up_sampling(g_maxpool[-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) else: g_upsampl[i] = self.up_sampling(g_decoder[i - 1][-1], indices[-i - 1]) g_decoder[i][0] = self.decoder_block[-i - 1](g_upsampl[i]) g_decoder[i][1] = self.conv_block_dec[-i - 1](g_decoder[i][0]) # define task dependent attention module for i in range(2): for j in range(5): if j == 0: atten_encoder[i][j][0] = self.encoder_att[i][j](g_encoder[j][0]) atten_encoder[i][j][1] = (atten_encoder[i][j][0]) * g_encoder[j][1] atten_encoder[i][j][2] = self.encoder_block_att[j](atten_encoder[i][j][1]) atten_encoder[i][j][2] = F.max_pool2d(atten_encoder[i][j][2], kernel_size=2, stride=2) else: atten_encoder[i][j][0] = self.encoder_att[i][j](torch.cat( (g_encoder[j][0], atten_encoder[i][j - 1][2]), dim=1)) atten_encoder[i][j][1] = (atten_encoder[i][j][0]) * g_encoder[j][1] atten_encoder[i][j][2] = self.encoder_block_att[j](atten_encoder[i][j][1]) atten_encoder[i][j][2] = F.max_pool2d(atten_encoder[i][j][2], kernel_size=2, stride=2) for j in range(5): if j == 0: atten_decoder[i][j][0] = F.interpolate(atten_encoder[i][-1][-1], scale_factor=2, mode='bilinear', align_corners=True) atten_decoder[i][j][0] = self.decoder_block_att[-j - 1](atten_decoder[i][j][0]) atten_decoder[i][j][1] = self.decoder_att[i][-j - 1](torch.cat( (g_upsampl[j], atten_decoder[i][j][0]), dim=1)) atten_decoder[i][j][2] = (atten_decoder[i][j][1]) * g_decoder[j][-1] else: atten_decoder[i][j][0] = F.interpolate(atten_decoder[i][j - 1][2], scale_factor=2, mode='bilinear', align_corners=True) atten_decoder[i][j][0] = self.decoder_block_att[-j - 1](atten_decoder[i][j][0]) atten_decoder[i][j][1] = self.decoder_att[i][-j - 1](torch.cat( (g_upsampl[j], atten_decoder[i][j][0]), dim=1)) atten_decoder[i][j][2] = (atten_decoder[i][j][1]) * g_decoder[j][-1] # define task prediction layers t1_pred = F.log_softmax(self.pred_task1(atten_decoder[0][-1][-1]), dim=1) t2_pred = self.pred_task2(atten_decoder[1][-1][-1]) #t3_pred = self.pred_task3(atten_decoder[2][-1][-1]) #t3_pred = t3_pred / torch.norm(t3_pred, p=2, dim=1, keepdim=True) return [t1_pred, t2_pred], self.logsigma control_seed(opt.seed) # define model, optimiser and scheduler device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") SegNet_MTAN = SegNet().to(device) optimizer = optim.Adam(SegNet_MTAN.parameters(), lr=opt.lr) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.5) print('Parameter Space: ABS: {:.1f}, REL: {:.4f}'.format(count_parameters(SegNet_MTAN), count_parameters(SegNet_MTAN) / 24981069)) print( 'LOSS FORMAT: SEMANTIC_LOSS MEAN_IOU PIX_ACC | DEPTH_LOSS ABS_ERR REL_ERR | NORMAL_LOSS MEAN MED <11.25 <22.5 <30') # define dataset dataset_path = opt.dataroot if opt.apply_augmentation: train_set = CityScapes(root=dataset_path, train=True, augmentation=True) print('Applying data augmentation.') else: train_set = CityScapes(root=dataset_path, train=True) print('Standard training strategy without data augmentation.') test_set = CityScapes(root=dataset_path, train=False) batch_size = 8 train_loader = torch.utils.data.DataLoader(dataset=train_set, batch_size=batch_size, shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=test_set, batch_size=batch_size, shuffle=False) # Train and evaluate multi-task network multi_task_rg_trainer(train_loader, test_loader, SegNet_MTAN, device, optimizer, scheduler, opt, 200)
12,105
49.865546
119
py
SyNet
SyNet-master/CenterNet/src/main.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import _init_paths import os import torch import torch.utils.data from opts import opts from models.model import create_model, load_model, save_model from models.data_parallel import DataParallel from logger import Logger from datasets.dataset_factory import get_dataset from trains.train_factory import train_factory def main(opt): torch.manual_seed(opt.seed) torch.backends.cudnn.benchmark = not opt.not_cuda_benchmark and not opt.test Dataset = get_dataset(opt.dataset, opt.task) opt = opts().update_dataset_info_and_set_heads(opt, Dataset) print(opt) logger = Logger(opt) os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpus_str opt.device = torch.device('cuda' if opt.gpus[0] >= 0 else 'cpu') print('Creating model...') model = create_model(opt.arch, opt.heads, opt.head_conv) optimizer = torch.optim.Adam(model.parameters(), opt.lr) start_epoch = 0 if opt.load_model != '': model, optimizer, start_epoch = load_model( model, opt.load_model, optimizer, opt.resume, opt.lr, opt.lr_step) Trainer = train_factory[opt.task] trainer = Trainer(opt, model, optimizer) trainer.set_device(opt.gpus, opt.chunk_sizes, opt.device) print('Setting up data...') val_loader = torch.utils.data.DataLoader( Dataset(opt, 'val'), batch_size=1, shuffle=False, num_workers=1, pin_memory=True ) if opt.test: _, preds = trainer.val(0, val_loader) val_loader.dataset.run_eval(preds, opt.save_dir) return train_loader = torch.utils.data.DataLoader( Dataset(opt, 'train'), batch_size=opt.batch_size, shuffle=True, num_workers=opt.num_workers, pin_memory=True, drop_last=True ) print('Starting training...') best = 1e10 for epoch in range(start_epoch + 1, opt.num_epochs + 1): mark = epoch if opt.save_all else 'last' log_dict_train, _ = trainer.train(epoch, train_loader) logger.write('epoch: {} |'.format(epoch)) for k, v in log_dict_train.items(): logger.scalar_summary('train_{}'.format(k), v, epoch) logger.write('{} {:8f} | '.format(k, v)) if opt.val_intervals > 0 and epoch % opt.val_intervals == 0: save_model(os.path.join(opt.save_dir, 'model_{}.pth'.format(mark)), epoch, model, optimizer) with torch.no_grad(): log_dict_val, preds = trainer.val(epoch, val_loader) for k, v in log_dict_val.items(): logger.scalar_summary('val_{}'.format(k), v, epoch) logger.write('{} {:8f} | '.format(k, v)) if log_dict_val[opt.metric] < best: best = log_dict_val[opt.metric] save_model(os.path.join(opt.save_dir, 'model_best.pth'), epoch, model) else: save_model(os.path.join(opt.save_dir, 'model_last.pth'), epoch, model, optimizer) logger.write('\n') if epoch in opt.lr_step: save_model(os.path.join(opt.save_dir, 'model_{}.pth'.format(epoch)), epoch, model, optimizer) lr = opt.lr * (0.1 ** (opt.lr_step.index(epoch) + 1)) print('Drop LR to', lr) for param_group in optimizer.param_groups: param_group['lr'] = lr logger.close() if __name__ == '__main__': opt = opts().parse() main(opt)
3,348
31.833333
78
py
SyNet
SyNet-master/CenterNet/src/test.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import _init_paths import os import json import cv2 import numpy as np import time from progress.bar import Bar import torch from external.nms import soft_nms from opts import opts from logger import Logger from utils.utils import AverageMeter from datasets.dataset_factory import dataset_factory from detectors.detector_factory import detector_factory class PrefetchDataset(torch.utils.data.Dataset): def __init__(self, opt, dataset, pre_process_func): self.images = dataset.images self.load_image_func = dataset.coco.loadImgs self.img_dir = dataset.img_dir self.pre_process_func = pre_process_func self.opt = opt def __getitem__(self, index): img_id = self.images[index] img_info = self.load_image_func(ids=[img_id])[0] img_path = os.path.join(self.img_dir, img_info['file_name']) image = cv2.imread(img_path) images, meta = {}, {} for scale in opt.test_scales: if opt.task == 'ddd': images[scale], meta[scale] = self.pre_process_func( image, scale, img_info['calib']) else: images[scale], meta[scale] = self.pre_process_func(image, scale) return img_id, {'images': images, 'image': image, 'meta': meta} def __len__(self): return len(self.images) def prefetch_test(opt): os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpus_str Dataset = dataset_factory[opt.dataset] opt = opts().update_dataset_info_and_set_heads(opt, Dataset) print(opt) Logger(opt) Detector = detector_factory[opt.task] split = 'val' if not opt.trainval else 'test' dataset = Dataset(opt, split) detector = Detector(opt) data_loader = torch.utils.data.DataLoader( PrefetchDataset(opt, dataset, detector.pre_process), batch_size=1, shuffle=False, num_workers=1, pin_memory=True) results = {} num_iters = len(dataset) bar = Bar('{}'.format(opt.exp_id), max=num_iters) time_stats = ['tot', 'load', 'pre', 'net', 'dec', 'post', 'merge'] avg_time_stats = {t: AverageMeter() for t in time_stats} for ind, (img_id, pre_processed_images) in enumerate(data_loader): ret = detector.run(pre_processed_images) results[img_id.numpy().astype(np.int32)[0]] = ret['results'] Bar.suffix = '[{0}/{1}]|Tot: {total:} |ETA: {eta:} '.format( ind, num_iters, total=bar.elapsed_td, eta=bar.eta_td) for t in avg_time_stats: avg_time_stats[t].update(ret[t]) Bar.suffix = Bar.suffix + '|{} {tm.val:.3f}s ({tm.avg:.3f}s) '.format( t, tm = avg_time_stats[t]) bar.next() bar.finish() dataset.run_eval(results, opt.save_dir) def test(opt): os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpus_str Dataset = dataset_factory[opt.dataset] opt = opts().update_dataset_info_and_set_heads(opt, Dataset) print(opt) Logger(opt) Detector = detector_factory[opt.task] split = 'val' if not opt.trainval else 'test' dataset = Dataset(opt, split) detector = Detector(opt) results = {} num_iters = len(dataset) bar = Bar('{}'.format(opt.exp_id), max=num_iters) time_stats = ['tot', 'load', 'pre', 'net', 'dec', 'post', 'merge'] avg_time_stats = {t: AverageMeter() for t in time_stats} for ind in range(num_iters): img_id = dataset.images[ind] img_info = dataset.coco.loadImgs(ids=[img_id])[0] img_path = os.path.join(dataset.img_dir, img_info['file_name']) if opt.task == 'ddd': ret = detector.run(img_path, img_info['calib']) else: ret = detector.run(img_path) results[img_id] = ret['results'] Bar.suffix = '[{0}/{1}]|Tot: {total:} |ETA: {eta:} '.format( ind, num_iters, total=bar.elapsed_td, eta=bar.eta_td) for t in avg_time_stats: avg_time_stats[t].update(ret[t]) Bar.suffix = Bar.suffix + '|{} {:.3f} '.format(t, avg_time_stats[t].avg) bar.next() bar.finish() dataset.run_eval(results, opt.save_dir) if __name__ == '__main__': opt = opts().parse() if opt.not_prefetch_test: test(opt) else: prefetch_test(opt)
4,092
31.484127
78
py
SyNet
SyNet-master/CenterNet/src/_init_paths.py
import os.path as osp import sys def add_path(path): if path not in sys.path: sys.path.insert(0, path) this_dir = osp.dirname(__file__) # Add lib to PYTHONPATH lib_path = osp.join(this_dir, 'lib') add_path(lib_path)
231
16.846154
36
py
SyNet
SyNet-master/CenterNet/src/demo.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import _init_paths import os import cv2 from opts import opts from detectors.detector_factory import detector_factory image_ext = ['jpg', 'jpeg', 'png', 'webp'] video_ext = ['mp4', 'mov', 'avi', 'mkv'] time_stats = ['tot', 'load', 'pre', 'net', 'dec', 'post', 'merge'] def demo(opt): os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpus_str opt.debug = max(opt.debug, 1) Detector = detector_factory[opt.task] detector = Detector(opt) if opt.demo == 'webcam' or \ opt.demo[opt.demo.rfind('.') + 1:].lower() in video_ext: cam = cv2.VideoCapture(0 if opt.demo == 'webcam' else opt.demo) detector.pause = False while True: _, img = cam.read() cv2.imshow('input', img) ret = detector.run(img) time_str = '' for stat in time_stats: time_str = time_str + '{} {:.3f}s |'.format(stat, ret[stat]) print(time_str) if cv2.waitKey(1) == 27: return # esc to quit else: if os.path.isdir(opt.demo): image_names = [] ls = os.listdir(opt.demo) for file_name in sorted(ls): ext = file_name[file_name.rfind('.') + 1:].lower() if ext in image_ext: image_names.append(os.path.join(opt.demo, file_name)) else: image_names = [opt.demo] for (image_name) in image_names: ret = detector.run(image_name) time_str = '' for stat in time_stats: time_str = time_str + '{} {:.3f}s |'.format(stat, ret[stat]) print(time_str) if __name__ == '__main__': opt = opts().init() demo(opt)
1,674
28.385965
70
py
SyNet
SyNet-master/CenterNet/src/tools/merge_pascal_json.py
import json # ANNOT_PATH = '/home/zxy/Datasets/VOC/annotations/' ANNOT_PATH = 'voc/annotations/' OUT_PATH = ANNOT_PATH INPUT_FILES = ['pascal_train2012.json', 'pascal_val2012.json', 'pascal_train2007.json', 'pascal_val2007.json'] OUTPUT_FILE = 'pascal_trainval0712.json' KEYS = ['images', 'type', 'annotations', 'categories'] MERGE_KEYS = ['images', 'annotations'] out = {} tot_anns = 0 for i, file_name in enumerate(INPUT_FILES): data = json.load(open(ANNOT_PATH + file_name, 'r')) print('keys', data.keys()) if i == 0: for key in KEYS: out[key] = data[key] print(file_name, key, len(data[key])) else: out['images'] += data['images'] for j in range(len(data['annotations'])): data['annotations'][j]['id'] += tot_anns out['annotations'] += data['annotations'] print(file_name, 'images', len(data['images'])) print(file_name, 'annotations', len(data['annotations'])) tot_anns = len(out['annotations']) print('tot', len(out['annotations'])) json.dump(out, open(OUT_PATH + OUTPUT_FILE, 'w'))
1,058
33.16129
62
py
SyNet
SyNet-master/CenterNet/src/tools/eval_coco_hp.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import pycocotools.coco as coco from pycocotools.cocoeval import COCOeval import sys import cv2 import numpy as np import pickle import os this_dir = os.path.dirname(__file__) ANN_PATH = this_dir + '../../data/coco/annotations/person_keypoints_val2017.json' print(ANN_PATH) if __name__ == '__main__': pred_path = sys.argv[1] coco = coco.COCO(ANN_PATH) dets = coco.loadRes(pred_path) img_ids = coco.getImgIds() num_images = len(img_ids) coco_eval = COCOeval(coco, dets, "keypoints") coco_eval.evaluate() coco_eval.accumulate() coco_eval.summarize() coco_eval = COCOeval(coco, dets, "bbox") coco_eval.evaluate() coco_eval.accumulate() coco_eval.summarize()
795
24.677419
81
py
SyNet
SyNet-master/CenterNet/src/tools/reval.py
#!/usr/bin/env python # -------------------------------------------------------- # Fast R-CNN # Copyright (c) 2015 Microsoft # Licensed under The MIT License [see LICENSE for details] # Written by Ross Girshick # Modified by Xingyi Zhou # -------------------------------------------------------- # Reval = re-eval. Re-evaluate saved detections. from __future__ import absolute_import from __future__ import division from __future__ import print_function import sys import os.path as osp sys.path.insert(0, osp.join(osp.dirname(__file__), 'voc_eval_lib')) from model.test import apply_nms from datasets.pascal_voc import pascal_voc import pickle import os, argparse import numpy as np import json def parse_args(): """ Parse input arguments """ parser = argparse.ArgumentParser(description='Re-evaluate results') parser.add_argument('detection_file', type=str) parser.add_argument('--output_dir', help='results directory', type=str) parser.add_argument('--imdb', dest='imdb_name', help='dataset to re-evaluate', default='voc_2007_test', type=str) parser.add_argument('--matlab', dest='matlab_eval', help='use matlab for evaluation', action='store_true') parser.add_argument('--comp', dest='comp_mode', help='competition mode', action='store_true') parser.add_argument('--nms', dest='apply_nms', help='apply nms', action='store_true') if len(sys.argv) == 1: parser.print_help() sys.exit(1) args = parser.parse_args() return args def from_dets(imdb_name, detection_file, args): imdb = pascal_voc('test', '2007') imdb.competition_mode(args.comp_mode) imdb.config['matlab_eval'] = args.matlab_eval with open(os.path.join(detection_file), 'rb') as f: if 'json' in detection_file: dets = json.load(f) else: dets = pickle.load(f, encoding='latin1') # import pdb; pdb.set_trace() if args.apply_nms: print('Applying NMS to all detections') test_nms = 0.3 nms_dets = apply_nms(dets, test_nms) else: nms_dets = dets print('Evaluating detections') imdb.evaluate_detections(nms_dets) if __name__ == '__main__': args = parse_args() imdb_name = args.imdb_name from_dets(imdb_name, args.detection_file, args)
2,331
28.518987
74
py
SyNet
SyNet-master/CenterNet/src/tools/convert_kitti_to_coco.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import pickle import json import numpy as np import cv2 DATA_PATH = '../../data/kitti/' DEBUG = False # VAL_PATH = DATA_PATH + 'training/label_val/' import os SPLITS = ['3dop', 'subcnn'] import _init_paths from utils.ddd_utils import compute_box_3d, project_to_image, alpha2rot_y from utils.ddd_utils import draw_box_3d, unproject_2d_to_3d ''' #Values Name Description ---------------------------------------------------------------------------- 1 type Describes the type of object: 'Car', 'Van', 'Truck', 'Pedestrian', 'Person_sitting', 'Cyclist', 'Tram', 'Misc' or 'DontCare' 1 truncated Float from 0 (non-truncated) to 1 (truncated), where truncated refers to the object leaving image boundaries 1 occluded Integer (0,1,2,3) indicating occlusion state: 0 = fully visible, 1 = partly occluded 2 = largely occluded, 3 = unknown 1 alpha Observation angle of object, ranging [-pi..pi] 4 bbox 2D bounding box of object in the image (0-based index): contains left, top, right, bottom pixel coordinates 3 dimensions 3D object dimensions: height, width, length (in meters) 3 location 3D object location x,y,z in camera coordinates (in meters) 1 rotation_y Rotation ry around Y-axis in camera coordinates [-pi..pi] 1 score Only for results: Float, indicating confidence in detection, needed for p/r curves, higher is better. ''' def _bbox_to_coco_bbox(bbox): return [(bbox[0]), (bbox[1]), (bbox[2] - bbox[0]), (bbox[3] - bbox[1])] def read_clib(calib_path): f = open(calib_path, 'r') for i, line in enumerate(f): if i == 2: calib = np.array(line[:-1].split(' ')[1:], dtype=np.float32) calib = calib.reshape(3, 4) return calib cats = ['Pedestrian', 'Car', 'Cyclist', 'Van', 'Truck', 'Person_sitting', 'Tram', 'Misc', 'DontCare'] cat_ids = {cat: i + 1 for i, cat in enumerate(cats)} # cat_info = [{"name": "pedestrian", "id": 1}, {"name": "vehicle", "id": 2}] F = 721 H = 384 # 375 W = 1248 # 1242 EXT = [45.75, -0.34, 0.005] CALIB = np.array([[F, 0, W / 2, EXT[0]], [0, F, H / 2, EXT[1]], [0, 0, 1, EXT[2]]], dtype=np.float32) cat_info = [] for i, cat in enumerate(cats): cat_info.append({'name': cat, 'id': i + 1}) for SPLIT in SPLITS: image_set_path = DATA_PATH + 'ImageSets_{}/'.format(SPLIT) ann_dir = DATA_PATH + 'training/label_2/' calib_dir = DATA_PATH + '{}/calib/' splits = ['train', 'val'] # splits = ['trainval', 'test'] calib_type = {'train': 'training', 'val': 'training', 'trainval': 'training', 'test': 'testing'} for split in splits: ret = {'images': [], 'annotations': [], "categories": cat_info} image_set = open(image_set_path + '{}.txt'.format(split), 'r') image_to_id = {} for line in image_set: if line[-1] == '\n': line = line[:-1] image_id = int(line) calib_path = calib_dir.format(calib_type[split]) + '{}.txt'.format(line) calib = read_clib(calib_path) image_info = {'file_name': '{}.png'.format(line), 'id': int(image_id), 'calib': calib.tolist()} ret['images'].append(image_info) if split == 'test': continue ann_path = ann_dir + '{}.txt'.format(line) # if split == 'val': # os.system('cp {} {}/'.format(ann_path, VAL_PATH)) anns = open(ann_path, 'r') if DEBUG: image = cv2.imread( DATA_PATH + 'images/trainval/' + image_info['file_name']) for ann_ind, txt in enumerate(anns): tmp = txt[:-1].split(' ') cat_id = cat_ids[tmp[0]] truncated = int(float(tmp[1])) occluded = int(tmp[2]) alpha = float(tmp[3]) bbox = [float(tmp[4]), float(tmp[5]), float(tmp[6]), float(tmp[7])] dim = [float(tmp[8]), float(tmp[9]), float(tmp[10])] location = [float(tmp[11]), float(tmp[12]), float(tmp[13])] rotation_y = float(tmp[14]) ann = {'image_id': image_id, 'id': int(len(ret['annotations']) + 1), 'category_id': cat_id, 'dim': dim, 'bbox': _bbox_to_coco_bbox(bbox), 'depth': location[2], 'alpha': alpha, 'truncated': truncated, 'occluded': occluded, 'location': location, 'rotation_y': rotation_y} ret['annotations'].append(ann) if DEBUG and tmp[0] != 'DontCare': box_3d = compute_box_3d(dim, location, rotation_y) box_2d = project_to_image(box_3d, calib) # print('box_2d', box_2d) image = draw_box_3d(image, box_2d) x = (bbox[0] + bbox[2]) / 2 ''' print('rot_y, alpha2rot_y, dlt', tmp[0], rotation_y, alpha2rot_y(alpha, x, calib[0, 2], calib[0, 0]), np.cos( rotation_y - alpha2rot_y(alpha, x, calib[0, 2], calib[0, 0]))) ''' depth = np.array([location[2]], dtype=np.float32) pt_2d = np.array([(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2], dtype=np.float32) pt_3d = unproject_2d_to_3d(pt_2d, depth, calib) pt_3d[1] += dim[0] / 2 print('pt_3d', pt_3d) print('location', location) if DEBUG: cv2.imshow('image', image) cv2.waitKey() print("# images: ", len(ret['images'])) print("# annotations: ", len(ret['annotations'])) # import pdb; pdb.set_trace() out_path = '{}/annotations/kitti_{}_{}.json'.format(DATA_PATH, SPLIT, split) json.dump(ret, open(out_path, 'w'))
5,935
37.797386
80
py
SyNet
SyNet-master/CenterNet/src/tools/_init_paths.py
import os.path as osp import sys def add_path(path): if path not in sys.path: sys.path.insert(0, path) this_dir = osp.dirname(__file__) # Add lib to PYTHONPATH lib_path = osp.join(this_dir, '../lib') add_path(lib_path)
234
17.076923
39
py
SyNet
SyNet-master/CenterNet/src/tools/calc_coco_overlap.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import pycocotools.coco as COCO import cv2 import numpy as np from pycocotools import mask as maskUtils ANN_PATH = '../../data/coco/annotations/' IMG_PATH = '../../data/coco/' ANN_FILES = {'train': 'instances_train2017.json', 'val': 'instances_val2017.json'} DEBUG = False RESIZE = True class_name = [ '__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush' ] def iou(box1, box2): area1 = (box1[2] - box1[0] + 1) * (box1[3] - box1[1] + 1) area2 = (box2[2] - box2[0] + 1) * (box2[3] - box2[1] + 1) inter = max(min(box1[2], box2[2]) - max(box1[0], box2[0]) + 1, 0) * \ max(min(box1[3], box2[3]) - max(box1[1], box2[1]) + 1, 0) iou = 1.0 * inter / (area1 + area2 - inter) return iou def generate_anchors( stride=16, sizes=(32, 64, 128, 256, 512), aspect_ratios=(0.5, 1, 2) ): """Generates a matrix of anchor boxes in (x1, y1, x2, y2) format. Anchors are centered on stride / 2, have (approximate) sqrt areas of the specified sizes, and aspect ratios as given. """ return _generate_anchors( stride, np.array(sizes, dtype=np.float) / stride, np.array(aspect_ratios, dtype=np.float) ) def _generate_anchors(base_size, scales, aspect_ratios): """Generate anchor (reference) windows by enumerating aspect ratios X scales wrt a reference (0, 0, base_size - 1, base_size - 1) window. """ anchor = np.array([1, 1, base_size, base_size], dtype=np.float) - 1 anchors = _ratio_enum(anchor, aspect_ratios) anchors = np.vstack( [_scale_enum(anchors[i, :], scales) for i in range(anchors.shape[0])] ) return anchors def _whctrs(anchor): """Return width, height, x center, and y center for an anchor (window).""" w = anchor[2] - anchor[0] + 1 h = anchor[3] - anchor[1] + 1 x_ctr = anchor[0] + 0.5 * (w - 1) y_ctr = anchor[1] + 0.5 * (h - 1) return w, h, x_ctr, y_ctr def _mkanchors(ws, hs, x_ctr, y_ctr): """Given a vector of widths (ws) and heights (hs) around a center (x_ctr, y_ctr), output a set of anchors (windows). """ ws = ws[:, np.newaxis] hs = hs[:, np.newaxis] anchors = np.hstack( ( x_ctr - 0.5 * (ws - 1), y_ctr - 0.5 * (hs - 1), x_ctr + 0.5 * (ws - 1), y_ctr + 0.5 * (hs - 1) ) ) return anchors def _ratio_enum(anchor, ratios): """Enumerate a set of anchors for each aspect ratio wrt an anchor.""" w, h, x_ctr, y_ctr = _whctrs(anchor) size = w * h size_ratios = size / ratios ws = np.round(np.sqrt(size_ratios)) hs = np.round(ws * ratios) anchors = _mkanchors(ws, hs, x_ctr, y_ctr) return anchors def _scale_enum(anchor, scales): """Enumerate a set of anchors for each scale wrt an anchor.""" w, h, x_ctr, y_ctr = _whctrs(anchor) ws = w * scales hs = h * scales anchors = _mkanchors(ws, hs, x_ctr, y_ctr) return anchors def _coco_box_to_bbox(box): bbox = np.array([box[0], box[1], box[0] + box[2], box[1] + box[3]], dtype=np.float32) return bbox def count_agnostic(split): coco = COCO.COCO(ANN_PATH + ANN_FILES[split]) images = coco.getImgIds() cnt = 0 for img_id in images: ann_ids = coco.getAnnIds(imgIds=[img_id]) anns = coco.loadAnns(ids=ann_ids) centers = [] for ann in anns: bbox = ann['bbox'] center = ((bbox[0] + bbox[2] / 2) // 4, (bbox[1] + bbox[3] / 2) // 4) for c in centers: if center[0] == c[0] and center[1] == c[1]: cnt += 1 centers.append(center) print('find {} collisions!'.format(cnt)) def count(split): coco = COCO.COCO(ANN_PATH + ANN_FILES[split]) images = coco.getImgIds() cnt = 0 obj = 0 for img_id in images: ann_ids = coco.getAnnIds(imgIds=[img_id]) anns = coco.loadAnns(ids=ann_ids) centers = [] obj += len(anns) for ann in anns: if ann['iscrowd'] > 0: continue bbox = ann['bbox'] center = ((bbox[0] + bbox[2] / 2) // 4, (bbox[1] + bbox[3] / 2) // 4, ann['category_id'], bbox) for c in centers: if center[0] == c[0] and center[1] == c[1] and center[2] == c[2] and \ iou(_coco_box_to_bbox(bbox), _coco_box_to_bbox(c[3])) < 2:# 0.5: cnt += 1 if DEBUG: file_name = coco.loadImgs(ids=[img_id])[0]['file_name'] img = cv2.imread('{}/{}2017/{}'.format(IMG_PATH, split, file_name)) x1, y1 = int(c[3][0]), int(c[3][1]), x2, y2 = int(c[3][0] + c[3][2]), int(c[3][1] + c[3][3]) cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2, cv2.LINE_AA) x1, y1 = int(center[3][0]), int(center[3][1]), x2, y2 = int(center[3][0] + center[3][2]), int(center[3][1] + center[3][3]) cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2, cv2.LINE_AA) cv2.imshow('img', img) cv2.waitKey() centers.append(center) print('find {} collisions of {} objects!'.format(cnt, obj)) def count_iou(split): coco = COCO.COCO(ANN_PATH + ANN_FILES[split]) images = coco.getImgIds() cnt = 0 obj = 0 for img_id in images: ann_ids = coco.getAnnIds(imgIds=[img_id]) anns = coco.loadAnns(ids=ann_ids) bboxes = [] obj += len(anns) for ann in anns: if ann['iscrowd'] > 0: continue bbox = _coco_box_to_bbox(ann['bbox']).tolist() + [ann['category_id']] for b in bboxes: if iou(b, bbox) > 0.5 and b[4] == bbox[4]: cnt += 1 if DEBUG: file_name = coco.loadImgs(ids=[img_id])[0]['file_name'] img = cv2.imread('{}/{}2017/{}'.format(IMG_PATH, split, file_name)) x1, y1 = int(b[0]), int(b[1]), x2, y2 = int(b[2]), int(b[3]) cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2, cv2.LINE_AA) x1, y1 = int(bbox[0]), int(bbox[1]), x2, y2 = int(bbox[2]), int(bbox[3]) cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2, cv2.LINE_AA) cv2.imshow('img', img) print('cats', class_name[b[4]], class_name[bbox[4]]) cv2.waitKey() bboxes.append(bbox) print('find {} collisions of {} objects!'.format(cnt, obj)) def count_anchor(split): coco = COCO.COCO(ANN_PATH + ANN_FILES[split]) images = coco.getImgIds() cnt = 0 obj = 0 stride = 16 anchor = generate_anchors().reshape(15, 2, 2) miss_s, miss_m, miss_l = 0, 0, 0 N = len(images) print(N, 'images') for ind, img_id in enumerate(images): if ind % 1000 == 0: print(ind, N) anchors = [] ann_ids = coco.getAnnIds(imgIds=[img_id]) anns = coco.loadAnns(ids=ann_ids) obj += len(anns) img_info = coco.loadImgs(ids=[img_id])[0] h, w = img_info['height'], img_info['width'] if RESIZE: if h > w: for i in range(len(anns)): anns[i]['bbox'][0] *= 800 / w anns[i]['bbox'][1] *= 800 / w anns[i]['bbox'][2] *= 800 / w anns[i]['bbox'][3] *= 800 / w h = h * 800 // w w = 800 else: for i in range(len(anns)): anns[i]['bbox'][0] *= 800 / h anns[i]['bbox'][1] *= 800 / h anns[i]['bbox'][2] *= 800 / h anns[i]['bbox'][3] *= 800 / h w = w * 800 // h h = 800 for i in range(w // stride): for j in range(h // stride): ct = np.array([i * stride, j * stride], dtype=np.float32).reshape(1, 1, 2) anchors.append(anchor + ct) anchors = np.concatenate(anchors, axis=0).reshape(-1, 4) anchors[:, 2:4] = anchors[:, 2:4] - anchors[:, 0:2] anchors = anchors.tolist() # import pdb; pdb.set_trace() g = [g['bbox'] for g in anns] iscrowd = [int(o['iscrowd']) for o in anns] ious = maskUtils.iou(anchors,g,iscrowd) for t in range(len(g)): if ious[:, t].max() < 0.5: s = anns[t]['area'] if s < 32 ** 2: miss_s += 1 elif s < 96 ** 2: miss_m += 1 else: miss_l += 1 if DEBUG: file_name = coco.loadImgs(ids=[img_id])[0]['file_name'] img = cv2.imread('{}/{}2017/{}'.format(IMG_PATH, split, file_name)) if RESIZE: img = cv2.resize(img, (w, h)) for t, gt in enumerate(g): if anns[t]['iscrowd'] > 0: continue x1, y1, x2, y2 = _coco_box_to_bbox(gt) cl = (0, 0, 255) if ious[:, t].max() < 0.5 else (0, 255, 0) cv2.rectangle(img, (x1, y1), (x2, y2), cl, 2, cv2.LINE_AA) for k in range(len(anchors)): if ious[k, t] > 0.5: x1, y1, x2, y2 = _coco_box_to_bbox(anchors[k]) cl = (np.array([255, 0, 0]) * ious[k, t]).astype(np.int32).tolist() cv2.rectangle(img, (x1, y1), (x2, y2), cl, 1, cv2.LINE_AA) cv2.imshow('img', img) cv2.waitKey() miss = 0 if len(ious) > 0: miss = (ious.max(axis=0) < 0.5).sum() cnt += miss print('cnt, obj, ratio ', cnt, obj, cnt / obj) print('s, m, l ', miss_s, miss_m, miss_l) # import pdb; pdb.set_trace() def count_size(split): coco = COCO.COCO(ANN_PATH + ANN_FILES[split]) images = coco.getImgIds() cnt = 0 obj = 0 stride = 16 anchor = generate_anchors().reshape(15, 2, 2) cnt_s, cnt_m, cnt_l = 0, 0, 0 N = len(images) print(N, 'images') for ind, img_id in enumerate(images): anchors = [] ann_ids = coco.getAnnIds(imgIds=[img_id]) anns = coco.loadAnns(ids=ann_ids) obj += len(anns) img_info = coco.loadImgs(ids=[img_id])[0] for t in range(len(anns)): if 1: s = anns[t]['area'] if s < 32 ** 2: cnt_s += 1 elif s < 96 ** 2: cnt_m += 1 else: cnt_l += 1 cnt += 1 print('cnt', cnt) print('s, m, l ', cnt_s, cnt_m, cnt_l) # count_iou('train') # count_anchor('train') # count('train') count_size('train')
10,869
32.653251
101
py
SyNet
SyNet-master/CenterNet/src/tools/convert_hourglass_weight.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function MODEL_PATH = '../../models/ExtremeNet_500000.pkl' OUT_PATH = '../../models/ExtremeNet_500000.pth' import torch state_dict = torch.load(MODEL_PATH) key_map = {'t_heats': 'hm_t', 'l_heats': 'hm_l', 'b_heats': 'hm_b', \ 'r_heats': 'hm_r', 'ct_heats': 'hm_c', \ 't_regrs': 'reg_t', 'l_regrs': 'reg_l', \ 'b_regrs': 'reg_b', 'r_regrs': 'reg_r'} out = {} for k in state_dict.keys(): changed = False for m in key_map.keys(): if m in k: if 'ct_heats' in k and m == 't_heats': continue new_k = k.replace(m, key_map[m]) out[new_k] = state_dict[k] changed = True print('replace {} to {}'.format(k, new_k)) if not changed: out[k] = state_dict[k] data = {'epoch': 0, 'state_dict': out} torch.save(data, OUT_PATH)
905
28.225806
69
py
SyNet
SyNet-master/CenterNet/src/tools/voc_eval_lib/datasets/ds_utils.py
# -------------------------------------------------------- # Fast/er R-CNN # Licensed under The MIT License [see LICENSE for details] # Written by Ross Girshick # -------------------------------------------------------- from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np def unique_boxes(boxes, scale=1.0): """Return indices of unique boxes.""" v = np.array([1, 1e3, 1e6, 1e9]) hashes = np.round(boxes * scale).dot(v) _, index = np.unique(hashes, return_index=True) return np.sort(index) def xywh_to_xyxy(boxes): """Convert [x y w h] box format to [x1 y1 x2 y2] format.""" return np.hstack((boxes[:, 0:2], boxes[:, 0:2] + boxes[:, 2:4] - 1)) def xyxy_to_xywh(boxes): """Convert [x1 y1 x2 y2] box format to [x y w h] format.""" return np.hstack((boxes[:, 0:2], boxes[:, 2:4] - boxes[:, 0:2] + 1)) def validate_boxes(boxes, width=0, height=0): """Check that a set of boxes are valid.""" x1 = boxes[:, 0] y1 = boxes[:, 1] x2 = boxes[:, 2] y2 = boxes[:, 3] assert (x1 >= 0).all() assert (y1 >= 0).all() assert (x2 >= x1).all() assert (y2 >= y1).all() assert (x2 < width).all() assert (y2 < height).all() def filter_small_boxes(boxes, min_size): w = boxes[:, 2] - boxes[:, 0] h = boxes[:, 3] - boxes[:, 1] keep = np.where((w >= min_size) & (h > min_size))[0] return keep
1,402
27.06
70
py
SyNet
SyNet-master/CenterNet/src/lib/opts.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import argparse import os import sys class opts(object): def __init__(self): self.parser = argparse.ArgumentParser() # basic experiment setting self.parser.add_argument('task', default='ctdet', help='ctdet | ddd | multi_pose | exdet') self.parser.add_argument('--dataset', default='visdrone', help='coco | kitti | coco_hp | pascal') self.parser.add_argument('--exp_id', default='default') self.parser.add_argument('--test', action='store_true') self.parser.add_argument('--debug', type=int, default=0, help='level of visualization.' '1: only show the final detection results' '2: show the network output features' '3: use matplot to display' # useful when lunching training with ipython notebook '4: save all visualizations to disk') self.parser.add_argument('--demo', default='', help='path to image/ image folders/ video. ' 'or "webcam"') self.parser.add_argument('--load_model', default='', help='path to pretrained model') self.parser.add_argument('--resume', action='store_true', help='resume an experiment. ' 'Reloaded the optimizer parameter and ' 'set load_model to model_last.pth ' 'in the exp dir if load_model is empty.') # system self.parser.add_argument('--gpus', default='0', help='-1 for CPU, use comma for multiple gpus') self.parser.add_argument('--num_workers', type=int, default=4, help='dataloader threads. 0 for single-thread.') self.parser.add_argument('--not_cuda_benchmark', action='store_true', help='disable when the input size is not fixed.') self.parser.add_argument('--seed', type=int, default=317, help='random seed') # from CornerNet # log self.parser.add_argument('--print_iter', type=int, default=0, help='disable progress bar and print to screen.') self.parser.add_argument('--hide_data_time', action='store_true', help='not display time during training.') self.parser.add_argument('--save_all', action='store_true', help='save model to disk every 5 epochs.') self.parser.add_argument('--metric', default='loss', help='main metric to save best model') self.parser.add_argument('--vis_thresh', type=float, default=0.3, help='visualization threshold.') self.parser.add_argument('--debugger_theme', default='white', choices=['white', 'black']) # model self.parser.add_argument('--arch', default='dla_34', help='model architecture. Currently tested' 'res_18 | res_101 | resdcn_18 | resdcn_101 |' 'dlav0_34 | dla_34 | hourglass') self.parser.add_argument('--head_conv', type=int, default=-1, help='conv layer channels for output head' '0 for no conv layer' '-1 for default setting: ' '64 for resnets and 256 for dla.') self.parser.add_argument('--down_ratio', type=int, default=4, help='output stride. Currently only supports 4.') # input self.parser.add_argument('--input_res', type=int, default=-1, help='input height and width. -1 for default from ' 'dataset. Will be overriden by input_h | input_w') self.parser.add_argument('--input_h', type=int, default=-1, help='input height. -1 for default from dataset.') self.parser.add_argument('--input_w', type=int, default=-1, help='input width. -1 for default from dataset.') # train self.parser.add_argument('--lr', type=float, default=1.25e-4, help='learning rate for batch size 32.') self.parser.add_argument('--lr_step', type=str, default='90,120', help='drop learning rate by 10.') self.parser.add_argument('--num_epochs', type=int, default=140, help='total training epochs.') self.parser.add_argument('--batch_size', type=int, default=32, help='batch size') self.parser.add_argument('--master_batch_size', type=int, default=-1, help='batch size on the master gpu.') self.parser.add_argument('--num_iters', type=int, default=-1, help='default: #samples / batch_size.') self.parser.add_argument('--val_intervals', type=int, default=5, help='number of epochs to run validation.') self.parser.add_argument('--trainval', action='store_true', help='include validation in training and ' 'test on test set') # test self.parser.add_argument('--flip_test', action='store_true', help='flip data augmentation.') self.parser.add_argument('--test_scales', type=str, default='1', help='multi scale test augmentation.') self.parser.add_argument('--nms', action='store_true', help='run nms in testing.') self.parser.add_argument('--K', type=int, default=100, help='max number of output objects.') self.parser.add_argument('--not_prefetch_test', action='store_true', help='not use parallal data pre-processing.') self.parser.add_argument('--fix_res', action='store_true', help='fix testing resolution or keep ' 'the original resolution') self.parser.add_argument('--keep_res', action='store_true', help='keep the original resolution' ' during validation.') # dataset self.parser.add_argument('--not_rand_crop', action='store_true', help='not use the random crop data augmentation' 'from CornerNet.') self.parser.add_argument('--shift', type=float, default=0.1, help='when not using random crop' 'apply shift augmentation.') self.parser.add_argument('--scale', type=float, default=0.4, help='when not using random crop' 'apply scale augmentation.') self.parser.add_argument('--rotate', type=float, default=0, help='when not using random crop' 'apply rotation augmentation.') self.parser.add_argument('--flip', type = float, default=0.5, help='probability of applying flip augmentation.') self.parser.add_argument('--no_color_aug', action='store_true', help='not use the color augmenation ' 'from CornerNet') # multi_pose self.parser.add_argument('--aug_rot', type=float, default=0, help='probability of applying ' 'rotation augmentation.') # ddd self.parser.add_argument('--aug_ddd', type=float, default=0.5, help='probability of applying crop augmentation.') self.parser.add_argument('--rect_mask', action='store_true', help='for ignored object, apply mask on the ' 'rectangular region or just center point.') self.parser.add_argument('--kitti_split', default='3dop', help='different validation split for kitti: ' '3dop | subcnn') # loss self.parser.add_argument('--mse_loss', action='store_true', help='use mse loss or focal loss to train ' 'keypoint heatmaps.') # ctdet self.parser.add_argument('--reg_loss', default='l1', help='regression loss: sl1 | l1 | l2') self.parser.add_argument('--hm_weight', type=float, default=1, help='loss weight for keypoint heatmaps.') self.parser.add_argument('--off_weight', type=float, default=1, help='loss weight for keypoint local offsets.') self.parser.add_argument('--wh_weight', type=float, default=0.1, help='loss weight for bounding box size.') # multi_pose self.parser.add_argument('--hp_weight', type=float, default=1, help='loss weight for human pose offset.') self.parser.add_argument('--hm_hp_weight', type=float, default=1, help='loss weight for human keypoint heatmap.') # ddd self.parser.add_argument('--dep_weight', type=float, default=1, help='loss weight for depth.') self.parser.add_argument('--dim_weight', type=float, default=1, help='loss weight for 3d bounding box size.') self.parser.add_argument('--rot_weight', type=float, default=1, help='loss weight for orientation.') self.parser.add_argument('--peak_thresh', type=float, default=0.2) # task # ctdet self.parser.add_argument('--norm_wh', action='store_true', help='L1(\hat(y) / y, 1) or L1(\hat(y), y)') self.parser.add_argument('--dense_wh', action='store_true', help='apply weighted regression near center or ' 'just apply regression on center point.') self.parser.add_argument('--cat_spec_wh', action='store_true', help='category specific bounding box size.') self.parser.add_argument('--not_reg_offset', action='store_true', help='not regress local offset.') # exdet self.parser.add_argument('--agnostic_ex', action='store_true', help='use category agnostic extreme points.') self.parser.add_argument('--scores_thresh', type=float, default=0.1, help='threshold for extreme point heatmap.') self.parser.add_argument('--center_thresh', type=float, default=0.1, help='threshold for centermap.') self.parser.add_argument('--aggr_weight', type=float, default=0.0, help='edge aggregation weight.') # multi_pose self.parser.add_argument('--dense_hp', action='store_true', help='apply weighted pose regression near center ' 'or just apply regression on center point.') self.parser.add_argument('--not_hm_hp', action='store_true', help='not estimate human joint heatmap, ' 'directly use the joint offset from center.') self.parser.add_argument('--not_reg_hp_offset', action='store_true', help='not regress local offset for ' 'human joint heatmaps.') self.parser.add_argument('--not_reg_bbox', action='store_true', help='not regression bounding box size.') # ground truth validation self.parser.add_argument('--eval_oracle_hm', action='store_true', help='use ground center heatmap.') self.parser.add_argument('--eval_oracle_wh', action='store_true', help='use ground truth bounding box size.') self.parser.add_argument('--eval_oracle_offset', action='store_true', help='use ground truth local heatmap offset.') self.parser.add_argument('--eval_oracle_kps', action='store_true', help='use ground truth human pose offset.') self.parser.add_argument('--eval_oracle_hmhp', action='store_true', help='use ground truth human joint heatmaps.') self.parser.add_argument('--eval_oracle_hp_offset', action='store_true', help='use ground truth human joint local offset.') self.parser.add_argument('--eval_oracle_dep', action='store_true', help='use ground truth depth.') def parse(self, args=''): if args == '': opt = self.parser.parse_args() else: opt = self.parser.parse_args(args) opt.gpus_str = opt.gpus opt.gpus = [int(gpu) for gpu in opt.gpus.split(',')] opt.gpus = [i for i in range(len(opt.gpus))] if opt.gpus[0] >=0 else [-1] opt.lr_step = [int(i) for i in opt.lr_step.split(',')] opt.test_scales = [float(i) for i in opt.test_scales.split(',')] opt.fix_res = not opt.keep_res print('Fix size testing.' if opt.fix_res else 'Keep resolution testing.') opt.reg_offset = not opt.not_reg_offset opt.reg_bbox = not opt.not_reg_bbox opt.hm_hp = not opt.not_hm_hp opt.reg_hp_offset = (not opt.not_reg_hp_offset) and opt.hm_hp if opt.head_conv == -1: # init default head_conv opt.head_conv = 256 if 'dla' in opt.arch else 64 opt.pad = 127 if 'hourglass' in opt.arch else 31 opt.num_stacks = 2 if opt.arch == 'hourglass' else 1 if opt.trainval: opt.val_intervals = 100000000 if opt.debug > 0: opt.num_workers = 0 opt.batch_size = 1 opt.gpus = [opt.gpus[0]] opt.master_batch_size = -1 if opt.master_batch_size == -1: opt.master_batch_size = opt.batch_size // len(opt.gpus) rest_batch_size = (opt.batch_size - opt.master_batch_size) opt.chunk_sizes = [opt.master_batch_size] for i in range(len(opt.gpus) - 1): slave_chunk_size = rest_batch_size // (len(opt.gpus) - 1) if i < rest_batch_size % (len(opt.gpus) - 1): slave_chunk_size += 1 opt.chunk_sizes.append(slave_chunk_size) print('training chunk_sizes:', opt.chunk_sizes) opt.root_dir = os.path.join(os.path.dirname(__file__), '..', '..') opt.data_dir = os.path.join(opt.root_dir, 'data') opt.exp_dir = os.path.join(opt.root_dir, 'exp', opt.task) opt.save_dir = os.path.join(opt.exp_dir, opt.exp_id) opt.debug_dir = os.path.join(opt.save_dir, 'debug') print('The output will be saved to ', opt.save_dir) if opt.resume and opt.load_model == '': model_path = opt.save_dir[:-4] if opt.save_dir.endswith('TEST') \ else opt.save_dir opt.load_model = os.path.join(model_path, 'model_last.pth') return opt def update_dataset_info_and_set_heads(self, opt, dataset): input_h, input_w = dataset.default_resolution opt.mean, opt.std = dataset.mean, dataset.std opt.num_classes = dataset.num_classes # input_h(w): opt.input_h overrides opt.input_res overrides dataset default input_h = opt.input_res if opt.input_res > 0 else input_h input_w = opt.input_res if opt.input_res > 0 else input_w opt.input_h = opt.input_h if opt.input_h > 0 else input_h opt.input_w = opt.input_w if opt.input_w > 0 else input_w opt.output_h = opt.input_h // opt.down_ratio opt.output_w = opt.input_w // opt.down_ratio opt.input_res = max(opt.input_h, opt.input_w) opt.output_res = max(opt.output_h, opt.output_w) if opt.task == 'exdet': # assert opt.dataset in ['coco'] num_hm = 1 if opt.agnostic_ex else opt.num_classes opt.heads = {'hm_t': num_hm, 'hm_l': num_hm, 'hm_b': num_hm, 'hm_r': num_hm, 'hm_c': opt.num_classes} if opt.reg_offset: opt.heads.update({'reg_t': 2, 'reg_l': 2, 'reg_b': 2, 'reg_r': 2}) elif opt.task == 'ddd': # assert opt.dataset in ['gta', 'kitti', 'viper'] opt.heads = {'hm': opt.num_classes, 'dep': 1, 'rot': 8, 'dim': 3} if opt.reg_bbox: opt.heads.update( {'wh': 2}) if opt.reg_offset: opt.heads.update({'reg': 2}) elif opt.task == 'ctdet': # assert opt.dataset in ['pascal', 'coco'] opt.heads = {'hm': opt.num_classes, 'wh': 2 if not opt.cat_spec_wh else 2 * opt.num_classes} if opt.reg_offset: opt.heads.update({'reg': 2}) elif opt.task == 'multi_pose': # assert opt.dataset in ['coco_hp'] opt.flip_idx = dataset.flip_idx opt.heads = {'hm': opt.num_classes, 'wh': 2, 'hps': 34} if opt.reg_offset: opt.heads.update({'reg': 2}) if opt.hm_hp: opt.heads.update({'hm_hp': 17}) if opt.reg_hp_offset: opt.heads.update({'hp_offset': 2}) else: assert 0, 'task not defined!' print('heads', opt.heads) return opt def init(self, args=''): default_dataset_info = { 'ctdet': {'default_resolution': [512, 512], 'num_classes': 10, 'mean': [0.408, 0.447, 0.470], 'std': [0.289, 0.274, 0.278], 'dataset': 'visdrone'}, 'exdet': {'default_resolution': [512, 512], 'num_classes': 80, 'mean': [0.408, 0.447, 0.470], 'std': [0.289, 0.274, 0.278], 'dataset': 'coco'}, 'multi_pose': { 'default_resolution': [512, 512], 'num_classes': 1, 'mean': [0.408, 0.447, 0.470], 'std': [0.289, 0.274, 0.278], 'dataset': 'coco_hp', 'num_joints': 17, 'flip_idx': [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12], [13, 14], [15, 16]]}, 'ddd': {'default_resolution': [384, 1280], 'num_classes': 3, 'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225], 'dataset': 'kitti'}, } class Struct: def __init__(self, entries): for k, v in entries.items(): self.__setattr__(k, v) opt = self.parse(args) dataset = Struct(default_dataset_info[opt.task]) opt.dataset = dataset.dataset opt = self.update_dataset_info_and_set_heads(opt, dataset) return opt
18,703
50.526171
115
py
SyNet
SyNet-master/CenterNet/src/lib/logger.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function # Code referenced from https://gist.github.com/gyglim/1f8dfb1b5c82627ae3efcfbbadb9f514 import os import time import sys import torch USE_TENSORBOARD = True try: import tensorboardX print('Using tensorboardX') except: USE_TENSORBOARD = False class Logger(object): def __init__(self, opt): """Create a summary writer logging to log_dir.""" if not os.path.exists(opt.save_dir): os.makedirs(opt.save_dir) if not os.path.exists(opt.debug_dir): os.makedirs(opt.debug_dir) time_str = time.strftime('%Y-%m-%d-%H-%M') args = dict((name, getattr(opt, name)) for name in dir(opt) if not name.startswith('_')) file_name = os.path.join(opt.save_dir, 'opt.txt') with open(file_name, 'wt') as opt_file: opt_file.write('==> torch version: {}\n'.format(torch.__version__)) opt_file.write('==> cudnn version: {}\n'.format( torch.backends.cudnn.version())) opt_file.write('==> Cmd:\n') opt_file.write(str(sys.argv)) opt_file.write('\n==> Opt:\n') for k, v in sorted(args.items()): opt_file.write(' %s: %s\n' % (str(k), str(v))) log_dir = opt.save_dir + '/logs_{}'.format(time_str) if USE_TENSORBOARD: self.writer = tensorboardX.SummaryWriter(log_dir=log_dir) else: if not os.path.exists(os.path.dirname(log_dir)): os.mkdir(os.path.dirname(log_dir)) if not os.path.exists(log_dir): os.mkdir(log_dir) self.log = open(log_dir + '/log.txt', 'w') try: os.system('cp {}/opt.txt {}/'.format(opt.save_dir, log_dir)) except: pass self.start_line = True def write(self, txt): if self.start_line: time_str = time.strftime('%Y-%m-%d-%H-%M') self.log.write('{}: {}'.format(time_str, txt)) else: self.log.write(txt) self.start_line = False if '\n' in txt: self.start_line = True self.log.flush() def close(self): self.log.close() def scalar_summary(self, tag, value, step): """Log a scalar variable.""" if USE_TENSORBOARD: self.writer.add_scalar(tag, value, step)
2,228
29.534247
86
py
SyNet
SyNet-master/CenterNet/src/lib/external/setup.py
import numpy from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize extensions = [ Extension( "nms", ["nms.pyx"]) ] setup( name="coco", ext_modules=cythonize(extensions), include_dirs=[numpy.get_include()] )
298
16.588235
41
py
SyNet
SyNet-master/CenterNet/src/lib/detectors/exdet.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import _init_paths import os import cv2 import numpy as np from progress.bar import Bar import time import torch from models.decode import exct_decode, agnex_ct_decode from models.utils import flip_tensor from utils.image import get_affine_transform, transform_preds from utils.post_process import ctdet_post_process from utils.debugger import Debugger from .base_detector import BaseDetector class ExdetDetector(BaseDetector): def __init__(self, opt): super(ExdetDetector, self).__init__(opt) self.decode = agnex_ct_decode if opt.agnostic_ex else exct_decode def process(self, images, return_time=False): with torch.no_grad(): torch.cuda.synchronize() output = self.model(images)[-1] t_heat = output['hm_t'].sigmoid_() l_heat = output['hm_l'].sigmoid_() b_heat = output['hm_b'].sigmoid_() r_heat = output['hm_r'].sigmoid_() c_heat = output['hm_c'].sigmoid_() torch.cuda.synchronize() forward_time = time.time() if self.opt.reg_offset: dets = self.decode(t_heat, l_heat, b_heat, r_heat, c_heat, output['reg_t'], output['reg_l'], output['reg_b'], output['reg_r'], K=self.opt.K, scores_thresh=self.opt.scores_thresh, center_thresh=self.opt.center_thresh, aggr_weight=self.opt.aggr_weight) else: dets = self.decode(t_heat, l_heat, b_heat, r_heat, c_heat, K=self.opt.K, scores_thresh=self.opt.scores_thresh, center_thresh=self.opt.center_thresh, aggr_weight=self.opt.aggr_weight) if return_time: return output, dets, forward_time else: return output, dets def debug(self, debugger, images, dets, output, scale=1): detection = dets.detach().cpu().numpy().copy() detection[:, :, :4] *= self.opt.down_ratio for i in range(1): inp_height, inp_width = images.shape[2], images.shape[3] pred_hm = np.zeros((inp_height, inp_width, 3), dtype=np.uint8) img = images[i].detach().cpu().numpy().transpose(1, 2, 0) img = ((img * self.std + self.mean) * 255).astype(np.uint8) parts = ['t', 'l', 'b', 'r', 'c'] for p in parts: tag = 'hm_{}'.format(p) pred = debugger.gen_colormap( output[tag][i].detach().cpu().numpy(), (inp_height, inp_width)) if p != 'c': pred_hm = np.maximum(pred_hm, pred) else: debugger.add_blend_img( img, pred, 'pred_{}_{:.1f}'.format(p, scale)) debugger.add_blend_img(img, pred_hm, 'pred_{:.1f}'.format(scale)) debugger.add_img(img, img_id='out_{:.1f}'.format(scale)) for k in range(len(detection[i])): # print('detection', detection[i, k, 4], detection[i, k]) if detection[i, k, 4] > 0.01: # print('detection', detection[i, k, 4], detection[i, k]) debugger.add_coco_bbox(detection[i, k, :4], detection[i, k, -1], detection[i, k, 4], img_id='out_{:.1f}'.format(scale)) def post_process(self, dets, meta, scale=1): out_width, out_height = meta['out_width'], meta['out_height'] dets = dets.detach().cpu().numpy().reshape(2, -1, 14) dets[1, :, [0, 2]] = out_width - dets[1, :, [2, 0]] dets = dets.reshape(1, -1, 14) dets[0, :, 0:2] = transform_preds( dets[0, :, 0:2], meta['c'], meta['s'], (out_width, out_height)) dets[0, :, 2:4] = transform_preds( dets[0, :, 2:4], meta['c'], meta['s'], (out_width, out_height)) dets[:, :, 0:4] /= scale return dets[0] def merge_outputs(self, detections): detections = np.concatenate( [detection for detection in detections], axis=0).astype(np.float32) classes = detections[..., -1] keep_inds = (detections[:, 4] > 0) detections = detections[keep_inds] classes = classes[keep_inds] results = {} for j in range(self.num_classes): keep_inds = (classes == j) results[j + 1] = detections[keep_inds][:, 0:7].astype(np.float32) soft_nms(results[j + 1], Nt=0.5, method=2) results[j + 1] = results[j + 1][:, 0:5] scores = np.hstack([ results[j][:, -1] for j in range(1, self.num_classes + 1) ]) if len(scores) > self.max_per_image: kth = len(scores) - self.max_per_image thresh = np.partition(scores, kth)[kth] for j in range(1, self.num_classes + 1): keep_inds = (results[j][:, -1] >= thresh) results[j] = results[j][keep_inds] return results def show_results(self, debugger, image, results): debugger.add_img(image, img_id='exdet') for j in range(1, self.num_classes + 1): for bbox in results[j]: if bbox[4] > self.opt.vis_thresh: debugger.add_coco_bbox(bbox[:4], j - 1, bbox[4], img_id='exdet') debugger.show_all_imgs(pause=self.pause)
5,063
37.363636
80
py
SyNet
SyNet-master/CenterNet/src/lib/detectors/ctdet.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import cv2 import numpy as np from progress.bar import Bar import time import torch try: from external.nms import soft_nms except: print('NMS not imported! If you need it,' ' do \n cd $CenterNet_ROOT/src/lib/external \n make') from models.decode import ctdet_decode from models.utils import flip_tensor from utils.image import get_affine_transform from utils.post_process import ctdet_post_process from utils.debugger import Debugger from .base_detector import BaseDetector class CtdetDetector(BaseDetector): def __init__(self, opt): super(CtdetDetector, self).__init__(opt) def process(self, images, return_time=False): with torch.no_grad(): output = self.model(images)[-1] hm = output['hm'].sigmoid_() wh = output['wh'] reg = output['reg'] if self.opt.reg_offset else None if self.opt.flip_test: hm = (hm[0:1] + flip_tensor(hm[1:2])) / 2 wh = (wh[0:1] + flip_tensor(wh[1:2])) / 2 reg = reg[0:1] if reg is not None else None torch.cuda.synchronize() forward_time = time.time() dets = ctdet_decode(hm, wh, reg=reg, cat_spec_wh=self.opt.cat_spec_wh, K=self.opt.K) if return_time: return output, dets, forward_time else: return output, dets def post_process(self, dets, meta, scale=1): dets = dets.detach().cpu().numpy() dets = dets.reshape(1, -1, dets.shape[2]) dets = ctdet_post_process( dets.copy(), [meta['c']], [meta['s']], meta['out_height'], meta['out_width'], self.opt.num_classes) for j in range(1, self.num_classes + 1): dets[0][j] = np.array(dets[0][j], dtype=np.float32).reshape(-1, 5) dets[0][j][:, :4] /= scale return dets[0] def merge_outputs(self, detections): results = {} for j in range(1, self.num_classes + 1): results[j] = np.concatenate( [detection[j] for detection in detections], axis=0).astype(np.float32) if len(self.scales) > 1 or self.opt.nms: soft_nms(results[j], Nt=0.5, method=2) scores = np.hstack( [results[j][:, 4] for j in range(1, self.num_classes + 1)]) if len(scores) > self.max_per_image: kth = len(scores) - self.max_per_image thresh = np.partition(scores, kth)[kth] for j in range(1, self.num_classes + 1): keep_inds = (results[j][:, 4] >= thresh) results[j] = results[j][keep_inds] return results def debug(self, debugger, images, dets, output, scale=1): detection = dets.detach().cpu().numpy().copy() detection[:, :, :4] *= self.opt.down_ratio for i in range(1): img = images[i].detach().cpu().numpy().transpose(1, 2, 0) img = ((img * self.std + self.mean) * 255).astype(np.uint8) pred = debugger.gen_colormap(output['hm'][i].detach().cpu().numpy()) debugger.add_blend_img(img, pred, 'pred_hm_{:.1f}'.format(scale)) debugger.add_img(img, img_id='out_pred_{:.1f}'.format(scale)) for k in range(len(dets[i])): if detection[i, k, 4] > self.opt.center_thresh: debugger.add_coco_bbox(detection[i, k, :4], detection[i, k, -1], detection[i, k, 4], img_id='out_pred_{:.1f}'.format(scale)) def show_results(self, debugger, image, results): debugger.add_img(image, img_id='ctdet') for j in range(1, self.num_classes + 1): for bbox in results[j]: if bbox[4] > self.opt.vis_thresh: debugger.add_coco_bbox(bbox[:4], j - 1, bbox[4], img_id='ctdet') debugger.show_all_imgs(pause=self.pause)
3,674
36.886598
90
py
SyNet
SyNet-master/CenterNet/src/lib/detectors/ddd.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import cv2 import numpy as np from progress.bar import Bar import time import torch from models.decode import ddd_decode from models.utils import flip_tensor from utils.image import get_affine_transform from utils.post_process import ddd_post_process from utils.debugger import Debugger from utils.ddd_utils import compute_box_3d, project_to_image, alpha2rot_y from utils.ddd_utils import draw_box_3d, unproject_2d_to_3d from .base_detector import BaseDetector class DddDetector(BaseDetector): def __init__(self, opt): super(DddDetector, self).__init__(opt) self.calib = np.array([[707.0493, 0, 604.0814, 45.75831], [0, 707.0493, 180.5066, -0.3454157], [0, 0, 1., 0.004981016]], dtype=np.float32) def pre_process(self, image, scale, calib=None): height, width = image.shape[0:2] inp_height, inp_width = self.opt.input_h, self.opt.input_w c = np.array([width / 2, height / 2], dtype=np.float32) if self.opt.keep_res: s = np.array([inp_width, inp_height], dtype=np.int32) else: s = np.array([width, height], dtype=np.int32) trans_input = get_affine_transform(c, s, 0, [inp_width, inp_height]) resized_image = image #cv2.resize(image, (width, height)) inp_image = cv2.warpAffine( resized_image, trans_input, (inp_width, inp_height), flags=cv2.INTER_LINEAR) inp_image = (inp_image.astype(np.float32) / 255.) inp_image = (inp_image - self.mean) / self.std images = inp_image.transpose(2, 0, 1)[np.newaxis, ...] calib = np.array(calib, dtype=np.float32) if calib is not None \ else self.calib images = torch.from_numpy(images) meta = {'c': c, 's': s, 'out_height': inp_height // self.opt.down_ratio, 'out_width': inp_width // self.opt.down_ratio, 'calib': calib} return images, meta def process(self, images, return_time=False): with torch.no_grad(): torch.cuda.synchronize() output = self.model(images)[-1] output['hm'] = output['hm'].sigmoid_() output['dep'] = 1. / (output['dep'].sigmoid() + 1e-6) - 1. wh = output['wh'] if self.opt.reg_bbox else None reg = output['reg'] if self.opt.reg_offset else None torch.cuda.synchronize() forward_time = time.time() dets = ddd_decode(output['hm'], output['rot'], output['dep'], output['dim'], wh=wh, reg=reg, K=self.opt.K) if return_time: return output, dets, forward_time else: return output, dets def post_process(self, dets, meta, scale=1): dets = dets.detach().cpu().numpy() detections = ddd_post_process( dets.copy(), [meta['c']], [meta['s']], [meta['calib']], self.opt) self.this_calib = meta['calib'] return detections[0] def merge_outputs(self, detections): results = detections[0] for j in range(1, self.num_classes + 1): if len(results[j] > 0): keep_inds = (results[j][:, -1] > self.opt.peak_thresh) results[j] = results[j][keep_inds] return results def debug(self, debugger, images, dets, output, scale=1): dets = dets.detach().cpu().numpy() img = images[0].detach().cpu().numpy().transpose(1, 2, 0) img = ((img * self.std + self.mean) * 255).astype(np.uint8) pred = debugger.gen_colormap(output['hm'][0].detach().cpu().numpy()) debugger.add_blend_img(img, pred, 'pred_hm') debugger.add_ct_detection( img, dets[0], show_box=self.opt.reg_bbox, center_thresh=self.opt.vis_thresh, img_id='det_pred') def show_results(self, debugger, image, results): debugger.add_3d_detection( image, results, self.this_calib, center_thresh=self.opt.vis_thresh, img_id='add_pred') debugger.add_bird_view( results, center_thresh=self.opt.vis_thresh, img_id='bird_pred') debugger.show_all_imgs(pause=self.pause)
4,013
36.867925
73
py
SyNet
SyNet-master/CenterNet/src/lib/detectors/multi_pose.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import cv2 import numpy as np from progress.bar import Bar import time import torch try: from external.nms import soft_nms_39 except: print('NMS not imported! If you need it,' ' do \n cd $CenterNet_ROOT/src/lib/external \n make') from models.decode import multi_pose_decode from models.utils import flip_tensor, flip_lr_off, flip_lr from utils.image import get_affine_transform from utils.post_process import multi_pose_post_process from utils.debugger import Debugger from .base_detector import BaseDetector class MultiPoseDetector(BaseDetector): def __init__(self, opt): super(MultiPoseDetector, self).__init__(opt) self.flip_idx = opt.flip_idx def process(self, images, return_time=False): with torch.no_grad(): torch.cuda.synchronize() output = self.model(images)[-1] output['hm'] = output['hm'].sigmoid_() if self.opt.hm_hp and not self.opt.mse_loss: output['hm_hp'] = output['hm_hp'].sigmoid_() reg = output['reg'] if self.opt.reg_offset else None hm_hp = output['hm_hp'] if self.opt.hm_hp else None hp_offset = output['hp_offset'] if self.opt.reg_hp_offset else None torch.cuda.synchronize() forward_time = time.time() if self.opt.flip_test: output['hm'] = (output['hm'][0:1] + flip_tensor(output['hm'][1:2])) / 2 output['wh'] = (output['wh'][0:1] + flip_tensor(output['wh'][1:2])) / 2 output['hps'] = (output['hps'][0:1] + flip_lr_off(output['hps'][1:2], self.flip_idx)) / 2 hm_hp = (hm_hp[0:1] + flip_lr(hm_hp[1:2], self.flip_idx)) / 2 \ if hm_hp is not None else None reg = reg[0:1] if reg is not None else None hp_offset = hp_offset[0:1] if hp_offset is not None else None dets = multi_pose_decode( output['hm'], output['wh'], output['hps'], reg=reg, hm_hp=hm_hp, hp_offset=hp_offset, K=self.opt.K) if return_time: return output, dets, forward_time else: return output, dets def post_process(self, dets, meta, scale=1): dets = dets.detach().cpu().numpy().reshape(1, -1, dets.shape[2]) dets = multi_pose_post_process( dets.copy(), [meta['c']], [meta['s']], meta['out_height'], meta['out_width']) for j in range(1, self.num_classes + 1): dets[0][j] = np.array(dets[0][j], dtype=np.float32).reshape(-1, 39) # import pdb; pdb.set_trace() dets[0][j][:, :4] /= scale dets[0][j][:, 5:] /= scale return dets[0] def merge_outputs(self, detections): results = {} results[1] = np.concatenate( [detection[1] for detection in detections], axis=0).astype(np.float32) if self.opt.nms or len(self.opt.test_scales) > 1: soft_nms_39(results[1], Nt=0.5, method=2) results[1] = results[1].tolist() return results def debug(self, debugger, images, dets, output, scale=1): dets = dets.detach().cpu().numpy().copy() dets[:, :, :4] *= self.opt.down_ratio dets[:, :, 5:39] *= self.opt.down_ratio img = images[0].detach().cpu().numpy().transpose(1, 2, 0) img = np.clip((( img * self.std + self.mean) * 255.), 0, 255).astype(np.uint8) pred = debugger.gen_colormap(output['hm'][0].detach().cpu().numpy()) debugger.add_blend_img(img, pred, 'pred_hm') if self.opt.hm_hp: pred = debugger.gen_colormap_hp( output['hm_hp'][0].detach().cpu().numpy()) debugger.add_blend_img(img, pred, 'pred_hmhp') def show_results(self, debugger, image, results): debugger.add_img(image, img_id='multi_pose') for bbox in results[1]: if bbox[4] > self.opt.vis_thresh: debugger.add_coco_bbox(bbox[:4], 0, bbox[4], img_id='multi_pose') debugger.add_coco_hp(bbox[5:39], img_id='multi_pose') debugger.show_all_imgs(pause=self.pause)
3,923
37.097087
79
py
SyNet
SyNet-master/CenterNet/src/lib/detectors/detector_factory.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function from .exdet import ExdetDetector from .ddd import DddDetector from .ctdet import CtdetDetector from .multi_pose import MultiPoseDetector detector_factory = { 'exdet': ExdetDetector, 'ddd': DddDetector, 'ctdet': CtdetDetector, 'multi_pose': MultiPoseDetector, }
382
22.9375
41
py
SyNet
SyNet-master/CenterNet/src/lib/detectors/base_detector.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import cv2 import numpy as np from progress.bar import Bar import time import torch from models.model import create_model, load_model from utils.image import get_affine_transform from utils.debugger import Debugger class BaseDetector(object): def __init__(self, opt): if opt.gpus[0] >= 0: opt.device = torch.device('cuda') else: opt.device = torch.device('cpu') print('Creating model...') self.model = create_model(opt.arch, opt.heads, opt.head_conv) self.model = load_model(self.model, opt.load_model) self.model = self.model.to(opt.device) self.model.eval() self.mean = np.array(opt.mean, dtype=np.float32).reshape(1, 1, 3) self.std = np.array(opt.std, dtype=np.float32).reshape(1, 1, 3) self.max_per_image = 100 self.num_classes = opt.num_classes self.scales = opt.test_scales self.opt = opt self.pause = True def pre_process(self, image, scale, meta=None): height, width = image.shape[0:2] new_height = int(height * scale) new_width = int(width * scale) if self.opt.fix_res: inp_height, inp_width = self.opt.input_h, self.opt.input_w c = np.array([new_width / 2., new_height / 2.], dtype=np.float32) s = max(height, width) * 1.0 else: inp_height = (new_height | self.opt.pad) + 1 inp_width = (new_width | self.opt.pad) + 1 c = np.array([new_width // 2, new_height // 2], dtype=np.float32) s = np.array([inp_width, inp_height], dtype=np.float32) trans_input = get_affine_transform(c, s, 0, [inp_width, inp_height]) resized_image = cv2.resize(image, (new_width, new_height)) inp_image = cv2.warpAffine( resized_image, trans_input, (inp_width, inp_height), flags=cv2.INTER_LINEAR) inp_image = ((inp_image / 255. - self.mean) / self.std).astype(np.float32) images = inp_image.transpose(2, 0, 1).reshape(1, 3, inp_height, inp_width) if self.opt.flip_test: images = np.concatenate((images, images[:, :, :, ::-1]), axis=0) images = torch.from_numpy(images) meta = {'c': c, 's': s, 'out_height': inp_height // self.opt.down_ratio, 'out_width': inp_width // self.opt.down_ratio} return images, meta def process(self, images, return_time=False): raise NotImplementedError def post_process(self, dets, meta, scale=1): raise NotImplementedError def merge_outputs(self, detections): raise NotImplementedError def debug(self, debugger, images, dets, output, scale=1): raise NotImplementedError def show_results(self, debugger, image, results): raise NotImplementedError def run(self, image_or_path_or_tensor, meta=None): load_time, pre_time, net_time, dec_time, post_time = 0, 0, 0, 0, 0 merge_time, tot_time = 0, 0 debugger = Debugger(dataset=self.opt.dataset, ipynb=(self.opt.debug==3), theme=self.opt.debugger_theme) start_time = time.time() pre_processed = False if isinstance(image_or_path_or_tensor, np.ndarray): image = image_or_path_or_tensor elif type(image_or_path_or_tensor) == type (''): image = cv2.imread(image_or_path_or_tensor) else: image = image_or_path_or_tensor['image'][0].numpy() pre_processed_images = image_or_path_or_tensor pre_processed = True loaded_time = time.time() load_time += (loaded_time - start_time) detections = [] for scale in self.scales: scale_start_time = time.time() if not pre_processed: images, meta = self.pre_process(image, scale, meta) else: # import pdb; pdb.set_trace() images = pre_processed_images['images'][scale][0] meta = pre_processed_images['meta'][scale] meta = {k: v.numpy()[0] for k, v in meta.items()} images = images.to(self.opt.device) torch.cuda.synchronize() pre_process_time = time.time() pre_time += pre_process_time - scale_start_time output, dets, forward_time = self.process(images, return_time=True) torch.cuda.synchronize() net_time += forward_time - pre_process_time decode_time = time.time() dec_time += decode_time - forward_time if self.opt.debug >= 2: self.debug(debugger, images, dets, output, scale) dets = self.post_process(dets, meta, scale) torch.cuda.synchronize() post_process_time = time.time() post_time += post_process_time - decode_time detections.append(dets) results = self.merge_outputs(detections) torch.cuda.synchronize() end_time = time.time() merge_time += end_time - post_process_time tot_time += end_time - start_time if self.opt.debug >= 1: self.show_results(debugger, image, results) return {'results': results, 'tot': tot_time, 'load': load_time, 'pre': pre_time, 'net': net_time, 'dec': dec_time, 'post': post_time, 'merge': merge_time}
5,061
34.152778
78
py
SyNet
SyNet-master/CenterNet/src/lib/models/decode.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import torch import torch.nn as nn from .utils import _gather_feat, _transpose_and_gather_feat def _nms(heat, kernel=3): pad = (kernel - 1) // 2 hmax = nn.functional.max_pool2d( heat, (kernel, kernel), stride=1, padding=pad) keep = (hmax == heat).float() return heat * keep def _left_aggregate(heat): ''' heat: batchsize x channels x h x w ''' shape = heat.shape heat = heat.reshape(-1, heat.shape[3]) heat = heat.transpose(1, 0).contiguous() ret = heat.clone() for i in range(1, heat.shape[0]): inds = (heat[i] >= heat[i - 1]) ret[i] += ret[i - 1] * inds.float() return (ret - heat).transpose(1, 0).reshape(shape) def _right_aggregate(heat): ''' heat: batchsize x channels x h x w ''' shape = heat.shape heat = heat.reshape(-1, heat.shape[3]) heat = heat.transpose(1, 0).contiguous() ret = heat.clone() for i in range(heat.shape[0] - 2, -1, -1): inds = (heat[i] >= heat[i +1]) ret[i] += ret[i + 1] * inds.float() return (ret - heat).transpose(1, 0).reshape(shape) def _top_aggregate(heat): ''' heat: batchsize x channels x h x w ''' heat = heat.transpose(3, 2) shape = heat.shape heat = heat.reshape(-1, heat.shape[3]) heat = heat.transpose(1, 0).contiguous() ret = heat.clone() for i in range(1, heat.shape[0]): inds = (heat[i] >= heat[i - 1]) ret[i] += ret[i - 1] * inds.float() return (ret - heat).transpose(1, 0).reshape(shape).transpose(3, 2) def _bottom_aggregate(heat): ''' heat: batchsize x channels x h x w ''' heat = heat.transpose(3, 2) shape = heat.shape heat = heat.reshape(-1, heat.shape[3]) heat = heat.transpose(1, 0).contiguous() ret = heat.clone() for i in range(heat.shape[0] - 2, -1, -1): inds = (heat[i] >= heat[i + 1]) ret[i] += ret[i + 1] * inds.float() return (ret - heat).transpose(1, 0).reshape(shape).transpose(3, 2) def _h_aggregate(heat, aggr_weight=0.1): return aggr_weight * _left_aggregate(heat) + \ aggr_weight * _right_aggregate(heat) + heat def _v_aggregate(heat, aggr_weight=0.1): return aggr_weight * _top_aggregate(heat) + \ aggr_weight * _bottom_aggregate(heat) + heat ''' # Slow for large number of categories def _topk(scores, K=40): batch, cat, height, width = scores.size() topk_scores, topk_inds = torch.topk(scores.view(batch, -1), K) topk_clses = (topk_inds / (height * width)).int() topk_inds = topk_inds % (height * width) topk_ys = (topk_inds / width).int().float() topk_xs = (topk_inds % width).int().float() return topk_scores, topk_inds, topk_clses, topk_ys, topk_xs ''' def _topk_channel(scores, K=40): batch, cat, height, width = scores.size() topk_scores, topk_inds = torch.topk(scores.view(batch, cat, -1), K) topk_inds = topk_inds % (height * width) topk_ys = (topk_inds / width).int().float() topk_xs = (topk_inds % width).int().float() return topk_scores, topk_inds, topk_ys, topk_xs def _topk(scores, K=40): batch, cat, height, width = scores.size() topk_scores, topk_inds = torch.topk(scores.view(batch, cat, -1), K) topk_inds = topk_inds % (height * width) topk_ys = (topk_inds / width).int().float() topk_xs = (topk_inds % width).int().float() topk_score, topk_ind = torch.topk(topk_scores.view(batch, -1), K) topk_clses = (topk_ind / K).int() topk_inds = _gather_feat( topk_inds.view(batch, -1, 1), topk_ind).view(batch, K) topk_ys = _gather_feat(topk_ys.view(batch, -1, 1), topk_ind).view(batch, K) topk_xs = _gather_feat(topk_xs.view(batch, -1, 1), topk_ind).view(batch, K) return topk_score, topk_inds, topk_clses, topk_ys, topk_xs def agnex_ct_decode( t_heat, l_heat, b_heat, r_heat, ct_heat, t_regr=None, l_regr=None, b_regr=None, r_regr=None, K=40, scores_thresh=0.1, center_thresh=0.1, aggr_weight=0.0, num_dets=1000 ): batch, cat, height, width = t_heat.size() ''' t_heat = torch.sigmoid(t_heat) l_heat = torch.sigmoid(l_heat) b_heat = torch.sigmoid(b_heat) r_heat = torch.sigmoid(r_heat) ct_heat = torch.sigmoid(ct_heat) ''' if aggr_weight > 0: t_heat = _h_aggregate(t_heat, aggr_weight=aggr_weight) l_heat = _v_aggregate(l_heat, aggr_weight=aggr_weight) b_heat = _h_aggregate(b_heat, aggr_weight=aggr_weight) r_heat = _v_aggregate(r_heat, aggr_weight=aggr_weight) # perform nms on heatmaps t_heat = _nms(t_heat) l_heat = _nms(l_heat) b_heat = _nms(b_heat) r_heat = _nms(r_heat) t_heat[t_heat > 1] = 1 l_heat[l_heat > 1] = 1 b_heat[b_heat > 1] = 1 r_heat[r_heat > 1] = 1 t_scores, t_inds, _, t_ys, t_xs = _topk(t_heat, K=K) l_scores, l_inds, _, l_ys, l_xs = _topk(l_heat, K=K) b_scores, b_inds, _, b_ys, b_xs = _topk(b_heat, K=K) r_scores, r_inds, _, r_ys, r_xs = _topk(r_heat, K=K) ct_heat_agn, ct_clses = torch.max(ct_heat, dim=1, keepdim=True) # import pdb; pdb.set_trace() t_ys = t_ys.view(batch, K, 1, 1, 1).expand(batch, K, K, K, K) t_xs = t_xs.view(batch, K, 1, 1, 1).expand(batch, K, K, K, K) l_ys = l_ys.view(batch, 1, K, 1, 1).expand(batch, K, K, K, K) l_xs = l_xs.view(batch, 1, K, 1, 1).expand(batch, K, K, K, K) b_ys = b_ys.view(batch, 1, 1, K, 1).expand(batch, K, K, K, K) b_xs = b_xs.view(batch, 1, 1, K, 1).expand(batch, K, K, K, K) r_ys = r_ys.view(batch, 1, 1, 1, K).expand(batch, K, K, K, K) r_xs = r_xs.view(batch, 1, 1, 1, K).expand(batch, K, K, K, K) box_ct_xs = ((l_xs + r_xs + 0.5) / 2).long() box_ct_ys = ((t_ys + b_ys + 0.5) / 2).long() ct_inds = box_ct_ys * width + box_ct_xs ct_inds = ct_inds.view(batch, -1) ct_heat_agn = ct_heat_agn.view(batch, -1, 1) ct_clses = ct_clses.view(batch, -1, 1) ct_scores = _gather_feat(ct_heat_agn, ct_inds) clses = _gather_feat(ct_clses, ct_inds) t_scores = t_scores.view(batch, K, 1, 1, 1).expand(batch, K, K, K, K) l_scores = l_scores.view(batch, 1, K, 1, 1).expand(batch, K, K, K, K) b_scores = b_scores.view(batch, 1, 1, K, 1).expand(batch, K, K, K, K) r_scores = r_scores.view(batch, 1, 1, 1, K).expand(batch, K, K, K, K) ct_scores = ct_scores.view(batch, K, K, K, K) scores = (t_scores + l_scores + b_scores + r_scores + 2 * ct_scores) / 6 # reject boxes based on classes top_inds = (t_ys > l_ys) + (t_ys > b_ys) + (t_ys > r_ys) top_inds = (top_inds > 0) left_inds = (l_xs > t_xs) + (l_xs > b_xs) + (l_xs > r_xs) left_inds = (left_inds > 0) bottom_inds = (b_ys < t_ys) + (b_ys < l_ys) + (b_ys < r_ys) bottom_inds = (bottom_inds > 0) right_inds = (r_xs < t_xs) + (r_xs < l_xs) + (r_xs < b_xs) right_inds = (right_inds > 0) sc_inds = (t_scores < scores_thresh) + (l_scores < scores_thresh) + \ (b_scores < scores_thresh) + (r_scores < scores_thresh) + \ (ct_scores < center_thresh) sc_inds = (sc_inds > 0) scores = scores - sc_inds.float() scores = scores - top_inds.float() scores = scores - left_inds.float() scores = scores - bottom_inds.float() scores = scores - right_inds.float() scores = scores.view(batch, -1) scores, inds = torch.topk(scores, num_dets) scores = scores.unsqueeze(2) if t_regr is not None and l_regr is not None \ and b_regr is not None and r_regr is not None: t_regr = _transpose_and_gather_feat(t_regr, t_inds) t_regr = t_regr.view(batch, K, 1, 1, 1, 2) l_regr = _transpose_and_gather_feat(l_regr, l_inds) l_regr = l_regr.view(batch, 1, K, 1, 1, 2) b_regr = _transpose_and_gather_feat(b_regr, b_inds) b_regr = b_regr.view(batch, 1, 1, K, 1, 2) r_regr = _transpose_and_gather_feat(r_regr, r_inds) r_regr = r_regr.view(batch, 1, 1, 1, K, 2) t_xs = t_xs + t_regr[..., 0] t_ys = t_ys + t_regr[..., 1] l_xs = l_xs + l_regr[..., 0] l_ys = l_ys + l_regr[..., 1] b_xs = b_xs + b_regr[..., 0] b_ys = b_ys + b_regr[..., 1] r_xs = r_xs + r_regr[..., 0] r_ys = r_ys + r_regr[..., 1] else: t_xs = t_xs + 0.5 t_ys = t_ys + 0.5 l_xs = l_xs + 0.5 l_ys = l_ys + 0.5 b_xs = b_xs + 0.5 b_ys = b_ys + 0.5 r_xs = r_xs + 0.5 r_ys = r_ys + 0.5 bboxes = torch.stack((l_xs, t_ys, r_xs, b_ys), dim=5) bboxes = bboxes.view(batch, -1, 4) bboxes = _gather_feat(bboxes, inds) clses = clses.contiguous().view(batch, -1, 1) clses = _gather_feat(clses, inds).float() t_xs = t_xs.contiguous().view(batch, -1, 1) t_xs = _gather_feat(t_xs, inds).float() t_ys = t_ys.contiguous().view(batch, -1, 1) t_ys = _gather_feat(t_ys, inds).float() l_xs = l_xs.contiguous().view(batch, -1, 1) l_xs = _gather_feat(l_xs, inds).float() l_ys = l_ys.contiguous().view(batch, -1, 1) l_ys = _gather_feat(l_ys, inds).float() b_xs = b_xs.contiguous().view(batch, -1, 1) b_xs = _gather_feat(b_xs, inds).float() b_ys = b_ys.contiguous().view(batch, -1, 1) b_ys = _gather_feat(b_ys, inds).float() r_xs = r_xs.contiguous().view(batch, -1, 1) r_xs = _gather_feat(r_xs, inds).float() r_ys = r_ys.contiguous().view(batch, -1, 1) r_ys = _gather_feat(r_ys, inds).float() detections = torch.cat([bboxes, scores, t_xs, t_ys, l_xs, l_ys, b_xs, b_ys, r_xs, r_ys, clses], dim=2) return detections def exct_decode( t_heat, l_heat, b_heat, r_heat, ct_heat, t_regr=None, l_regr=None, b_regr=None, r_regr=None, K=40, scores_thresh=0.1, center_thresh=0.1, aggr_weight=0.0, num_dets=1000 ): batch, cat, height, width = t_heat.size() ''' t_heat = torch.sigmoid(t_heat) l_heat = torch.sigmoid(l_heat) b_heat = torch.sigmoid(b_heat) r_heat = torch.sigmoid(r_heat) ct_heat = torch.sigmoid(ct_heat) ''' if aggr_weight > 0: t_heat = _h_aggregate(t_heat, aggr_weight=aggr_weight) l_heat = _v_aggregate(l_heat, aggr_weight=aggr_weight) b_heat = _h_aggregate(b_heat, aggr_weight=aggr_weight) r_heat = _v_aggregate(r_heat, aggr_weight=aggr_weight) # perform nms on heatmaps t_heat = _nms(t_heat) l_heat = _nms(l_heat) b_heat = _nms(b_heat) r_heat = _nms(r_heat) t_heat[t_heat > 1] = 1 l_heat[l_heat > 1] = 1 b_heat[b_heat > 1] = 1 r_heat[r_heat > 1] = 1 t_scores, t_inds, t_clses, t_ys, t_xs = _topk(t_heat, K=K) l_scores, l_inds, l_clses, l_ys, l_xs = _topk(l_heat, K=K) b_scores, b_inds, b_clses, b_ys, b_xs = _topk(b_heat, K=K) r_scores, r_inds, r_clses, r_ys, r_xs = _topk(r_heat, K=K) t_ys = t_ys.view(batch, K, 1, 1, 1).expand(batch, K, K, K, K) t_xs = t_xs.view(batch, K, 1, 1, 1).expand(batch, K, K, K, K) l_ys = l_ys.view(batch, 1, K, 1, 1).expand(batch, K, K, K, K) l_xs = l_xs.view(batch, 1, K, 1, 1).expand(batch, K, K, K, K) b_ys = b_ys.view(batch, 1, 1, K, 1).expand(batch, K, K, K, K) b_xs = b_xs.view(batch, 1, 1, K, 1).expand(batch, K, K, K, K) r_ys = r_ys.view(batch, 1, 1, 1, K).expand(batch, K, K, K, K) r_xs = r_xs.view(batch, 1, 1, 1, K).expand(batch, K, K, K, K) t_clses = t_clses.view(batch, K, 1, 1, 1).expand(batch, K, K, K, K) l_clses = l_clses.view(batch, 1, K, 1, 1).expand(batch, K, K, K, K) b_clses = b_clses.view(batch, 1, 1, K, 1).expand(batch, K, K, K, K) r_clses = r_clses.view(batch, 1, 1, 1, K).expand(batch, K, K, K, K) box_ct_xs = ((l_xs + r_xs + 0.5) / 2).long() box_ct_ys = ((t_ys + b_ys + 0.5) / 2).long() ct_inds = t_clses.long() * (height * width) + box_ct_ys * width + box_ct_xs ct_inds = ct_inds.view(batch, -1) ct_heat = ct_heat.view(batch, -1, 1) ct_scores = _gather_feat(ct_heat, ct_inds) t_scores = t_scores.view(batch, K, 1, 1, 1).expand(batch, K, K, K, K) l_scores = l_scores.view(batch, 1, K, 1, 1).expand(batch, K, K, K, K) b_scores = b_scores.view(batch, 1, 1, K, 1).expand(batch, K, K, K, K) r_scores = r_scores.view(batch, 1, 1, 1, K).expand(batch, K, K, K, K) ct_scores = ct_scores.view(batch, K, K, K, K) scores = (t_scores + l_scores + b_scores + r_scores + 2 * ct_scores) / 6 # reject boxes based on classes cls_inds = (t_clses != l_clses) + (t_clses != b_clses) + \ (t_clses != r_clses) cls_inds = (cls_inds > 0) top_inds = (t_ys > l_ys) + (t_ys > b_ys) + (t_ys > r_ys) top_inds = (top_inds > 0) left_inds = (l_xs > t_xs) + (l_xs > b_xs) + (l_xs > r_xs) left_inds = (left_inds > 0) bottom_inds = (b_ys < t_ys) + (b_ys < l_ys) + (b_ys < r_ys) bottom_inds = (bottom_inds > 0) right_inds = (r_xs < t_xs) + (r_xs < l_xs) + (r_xs < b_xs) right_inds = (right_inds > 0) sc_inds = (t_scores < scores_thresh) + (l_scores < scores_thresh) + \ (b_scores < scores_thresh) + (r_scores < scores_thresh) + \ (ct_scores < center_thresh) sc_inds = (sc_inds > 0) scores = scores - sc_inds.float() scores = scores - cls_inds.float() scores = scores - top_inds.float() scores = scores - left_inds.float() scores = scores - bottom_inds.float() scores = scores - right_inds.float() scores = scores.view(batch, -1) scores, inds = torch.topk(scores, num_dets) scores = scores.unsqueeze(2) if t_regr is not None and l_regr is not None \ and b_regr is not None and r_regr is not None: t_regr = _transpose_and_gather_feat(t_regr, t_inds) t_regr = t_regr.view(batch, K, 1, 1, 1, 2) l_regr = _transpose_and_gather_feat(l_regr, l_inds) l_regr = l_regr.view(batch, 1, K, 1, 1, 2) b_regr = _transpose_and_gather_feat(b_regr, b_inds) b_regr = b_regr.view(batch, 1, 1, K, 1, 2) r_regr = _transpose_and_gather_feat(r_regr, r_inds) r_regr = r_regr.view(batch, 1, 1, 1, K, 2) t_xs = t_xs + t_regr[..., 0] t_ys = t_ys + t_regr[..., 1] l_xs = l_xs + l_regr[..., 0] l_ys = l_ys + l_regr[..., 1] b_xs = b_xs + b_regr[..., 0] b_ys = b_ys + b_regr[..., 1] r_xs = r_xs + r_regr[..., 0] r_ys = r_ys + r_regr[..., 1] else: t_xs = t_xs + 0.5 t_ys = t_ys + 0.5 l_xs = l_xs + 0.5 l_ys = l_ys + 0.5 b_xs = b_xs + 0.5 b_ys = b_ys + 0.5 r_xs = r_xs + 0.5 r_ys = r_ys + 0.5 bboxes = torch.stack((l_xs, t_ys, r_xs, b_ys), dim=5) bboxes = bboxes.view(batch, -1, 4) bboxes = _gather_feat(bboxes, inds) clses = t_clses.contiguous().view(batch, -1, 1) clses = _gather_feat(clses, inds).float() t_xs = t_xs.contiguous().view(batch, -1, 1) t_xs = _gather_feat(t_xs, inds).float() t_ys = t_ys.contiguous().view(batch, -1, 1) t_ys = _gather_feat(t_ys, inds).float() l_xs = l_xs.contiguous().view(batch, -1, 1) l_xs = _gather_feat(l_xs, inds).float() l_ys = l_ys.contiguous().view(batch, -1, 1) l_ys = _gather_feat(l_ys, inds).float() b_xs = b_xs.contiguous().view(batch, -1, 1) b_xs = _gather_feat(b_xs, inds).float() b_ys = b_ys.contiguous().view(batch, -1, 1) b_ys = _gather_feat(b_ys, inds).float() r_xs = r_xs.contiguous().view(batch, -1, 1) r_xs = _gather_feat(r_xs, inds).float() r_ys = r_ys.contiguous().view(batch, -1, 1) r_ys = _gather_feat(r_ys, inds).float() detections = torch.cat([bboxes, scores, t_xs, t_ys, l_xs, l_ys, b_xs, b_ys, r_xs, r_ys, clses], dim=2) return detections def ddd_decode(heat, rot, depth, dim, wh=None, reg=None, K=40): batch, cat, height, width = heat.size() # heat = torch.sigmoid(heat) # perform nms on heatmaps heat = _nms(heat) scores, inds, clses, ys, xs = _topk(heat, K=K) if reg is not None: reg = _transpose_and_gather_feat(reg, inds) reg = reg.view(batch, K, 2) xs = xs.view(batch, K, 1) + reg[:, :, 0:1] ys = ys.view(batch, K, 1) + reg[:, :, 1:2] else: xs = xs.view(batch, K, 1) + 0.5 ys = ys.view(batch, K, 1) + 0.5 rot = _transpose_and_gather_feat(rot, inds) rot = rot.view(batch, K, 8) depth = _transpose_and_gather_feat(depth, inds) depth = depth.view(batch, K, 1) dim = _transpose_and_gather_feat(dim, inds) dim = dim.view(batch, K, 3) clses = clses.view(batch, K, 1).float() scores = scores.view(batch, K, 1) xs = xs.view(batch, K, 1) ys = ys.view(batch, K, 1) if wh is not None: wh = _transpose_and_gather_feat(wh, inds) wh = wh.view(batch, K, 2) detections = torch.cat( [xs, ys, scores, rot, depth, dim, wh, clses], dim=2) else: detections = torch.cat( [xs, ys, scores, rot, depth, dim, clses], dim=2) return detections def ctdet_decode(heat, wh, reg=None, cat_spec_wh=False, K=100): batch, cat, height, width = heat.size() # heat = torch.sigmoid(heat) # perform nms on heatmaps heat = _nms(heat) scores, inds, clses, ys, xs = _topk(heat, K=K) if reg is not None: reg = _transpose_and_gather_feat(reg, inds) reg = reg.view(batch, K, 2) xs = xs.view(batch, K, 1) + reg[:, :, 0:1] ys = ys.view(batch, K, 1) + reg[:, :, 1:2] else: xs = xs.view(batch, K, 1) + 0.5 ys = ys.view(batch, K, 1) + 0.5 wh = _transpose_and_gather_feat(wh, inds) if cat_spec_wh: wh = wh.view(batch, K, cat, 2) clses_ind = clses.view(batch, K, 1, 1).expand(batch, K, 1, 2).long() wh = wh.gather(2, clses_ind).view(batch, K, 2) else: wh = wh.view(batch, K, 2) clses = clses.view(batch, K, 1).float() scores = scores.view(batch, K, 1) bboxes = torch.cat([xs - wh[..., 0:1] / 2, ys - wh[..., 1:2] / 2, xs + wh[..., 0:1] / 2, ys + wh[..., 1:2] / 2], dim=2) detections = torch.cat([bboxes, scores, clses], dim=2) return detections def multi_pose_decode( heat, wh, kps, reg=None, hm_hp=None, hp_offset=None, K=100): batch, cat, height, width = heat.size() num_joints = kps.shape[1] // 2 # heat = torch.sigmoid(heat) # perform nms on heatmaps heat = _nms(heat) scores, inds, clses, ys, xs = _topk(heat, K=K) kps = _transpose_and_gather_feat(kps, inds) kps = kps.view(batch, K, num_joints * 2) kps[..., ::2] += xs.view(batch, K, 1).expand(batch, K, num_joints) kps[..., 1::2] += ys.view(batch, K, 1).expand(batch, K, num_joints) if reg is not None: reg = _transpose_and_gather_feat(reg, inds) reg = reg.view(batch, K, 2) xs = xs.view(batch, K, 1) + reg[:, :, 0:1] ys = ys.view(batch, K, 1) + reg[:, :, 1:2] else: xs = xs.view(batch, K, 1) + 0.5 ys = ys.view(batch, K, 1) + 0.5 wh = _transpose_and_gather_feat(wh, inds) wh = wh.view(batch, K, 2) clses = clses.view(batch, K, 1).float() scores = scores.view(batch, K, 1) bboxes = torch.cat([xs - wh[..., 0:1] / 2, ys - wh[..., 1:2] / 2, xs + wh[..., 0:1] / 2, ys + wh[..., 1:2] / 2], dim=2) if hm_hp is not None: hm_hp = _nms(hm_hp) thresh = 0.1 kps = kps.view(batch, K, num_joints, 2).permute( 0, 2, 1, 3).contiguous() # b x J x K x 2 reg_kps = kps.unsqueeze(3).expand(batch, num_joints, K, K, 2) hm_score, hm_inds, hm_ys, hm_xs = _topk_channel(hm_hp, K=K) # b x J x K if hp_offset is not None: hp_offset = _transpose_and_gather_feat( hp_offset, hm_inds.view(batch, -1)) hp_offset = hp_offset.view(batch, num_joints, K, 2) hm_xs = hm_xs + hp_offset[:, :, :, 0] hm_ys = hm_ys + hp_offset[:, :, :, 1] else: hm_xs = hm_xs + 0.5 hm_ys = hm_ys + 0.5 mask = (hm_score > thresh).float() hm_score = (1 - mask) * -1 + mask * hm_score hm_ys = (1 - mask) * (-10000) + mask * hm_ys hm_xs = (1 - mask) * (-10000) + mask * hm_xs hm_kps = torch.stack([hm_xs, hm_ys], dim=-1).unsqueeze( 2).expand(batch, num_joints, K, K, 2) dist = (((reg_kps - hm_kps) ** 2).sum(dim=4) ** 0.5) min_dist, min_ind = dist.min(dim=3) # b x J x K hm_score = hm_score.gather(2, min_ind).unsqueeze(-1) # b x J x K x 1 min_dist = min_dist.unsqueeze(-1) min_ind = min_ind.view(batch, num_joints, K, 1, 1).expand( batch, num_joints, K, 1, 2) hm_kps = hm_kps.gather(3, min_ind) hm_kps = hm_kps.view(batch, num_joints, K, 2) l = bboxes[:, :, 0].view(batch, 1, K, 1).expand(batch, num_joints, K, 1) t = bboxes[:, :, 1].view(batch, 1, K, 1).expand(batch, num_joints, K, 1) r = bboxes[:, :, 2].view(batch, 1, K, 1).expand(batch, num_joints, K, 1) b = bboxes[:, :, 3].view(batch, 1, K, 1).expand(batch, num_joints, K, 1) mask = (hm_kps[..., 0:1] < l) + (hm_kps[..., 0:1] > r) + \ (hm_kps[..., 1:2] < t) + (hm_kps[..., 1:2] > b) + \ (hm_score < thresh) + (min_dist > (torch.max(b - t, r - l) * 0.3)) mask = (mask > 0).float().expand(batch, num_joints, K, 2) kps = (1 - mask) * hm_kps + mask * kps kps = kps.permute(0, 2, 1, 3).contiguous().view( batch, K, num_joints * 2) detections = torch.cat([bboxes, scores, kps, clses], dim=2) return detections
21,763
37.115587
79
py
SyNet
SyNet-master/CenterNet/src/lib/models/losses.py
# ------------------------------------------------------------------------------ # Portions of this code are from # CornerNet (https://github.com/princeton-vl/CornerNet) # Copyright (c) 2018, University of Michigan # Licensed under the BSD 3-Clause License # ------------------------------------------------------------------------------ from __future__ import absolute_import from __future__ import division from __future__ import print_function import torch import torch.nn as nn from .utils import _transpose_and_gather_feat import torch.nn.functional as F def _slow_neg_loss(pred, gt): '''focal loss from CornerNet''' pos_inds = gt.eq(1) neg_inds = gt.lt(1) neg_weights = torch.pow(1 - gt[neg_inds], 4) loss = 0 pos_pred = pred[pos_inds] neg_pred = pred[neg_inds] pos_loss = torch.log(pos_pred) * torch.pow(1 - pos_pred, 2) neg_loss = torch.log(1 - neg_pred) * torch.pow(neg_pred, 2) * neg_weights num_pos = pos_inds.float().sum() pos_loss = pos_loss.sum() neg_loss = neg_loss.sum() if pos_pred.nelement() == 0: loss = loss - neg_loss else: loss = loss - (pos_loss + neg_loss) / num_pos return loss def _neg_loss(pred, gt): ''' Modified focal loss. Exactly the same as CornerNet. Runs faster and costs a little bit more memory Arguments: pred (batch x c x h x w) gt_regr (batch x c x h x w) ''' pos_inds = gt.eq(1).float() neg_inds = gt.lt(1).float() neg_weights = torch.pow(1 - gt, 4) loss = 0 pos_loss = torch.log(pred) * torch.pow(1 - pred, 2) * pos_inds neg_loss = torch.log(1 - pred) * torch.pow(pred, 2) * neg_weights * neg_inds num_pos = pos_inds.float().sum() pos_loss = pos_loss.sum() neg_loss = neg_loss.sum() if num_pos == 0: loss = loss - neg_loss else: loss = loss - (pos_loss + neg_loss) / num_pos return loss def _not_faster_neg_loss(pred, gt): pos_inds = gt.eq(1).float() neg_inds = gt.lt(1).float() num_pos = pos_inds.float().sum() neg_weights = torch.pow(1 - gt, 4) loss = 0 trans_pred = pred * neg_inds + (1 - pred) * pos_inds weight = neg_weights * neg_inds + pos_inds all_loss = torch.log(1 - trans_pred) * torch.pow(trans_pred, 2) * weight all_loss = all_loss.sum() if num_pos > 0: all_loss /= num_pos loss -= all_loss return loss def _slow_reg_loss(regr, gt_regr, mask): num = mask.float().sum() mask = mask.unsqueeze(2).expand_as(gt_regr) regr = regr[mask] gt_regr = gt_regr[mask] regr_loss = nn.functional.smooth_l1_loss(regr, gt_regr, size_average=False) regr_loss = regr_loss / (num + 1e-4) return regr_loss def _reg_loss(regr, gt_regr, mask): ''' L1 regression loss Arguments: regr (batch x max_objects x dim) gt_regr (batch x max_objects x dim) mask (batch x max_objects) ''' num = mask.float().sum() mask = mask.unsqueeze(2).expand_as(gt_regr).float() regr = regr * mask gt_regr = gt_regr * mask regr_loss = nn.functional.smooth_l1_loss(regr, gt_regr, size_average=False) regr_loss = regr_loss / (num + 1e-4) return regr_loss class FocalLoss(nn.Module): '''nn.Module warpper for focal loss''' def __init__(self): super(FocalLoss, self).__init__() self.neg_loss = _neg_loss def forward(self, out, target): return self.neg_loss(out, target) class RegLoss(nn.Module): '''Regression loss for an output tensor Arguments: output (batch x dim x h x w) mask (batch x max_objects) ind (batch x max_objects) target (batch x max_objects x dim) ''' def __init__(self): super(RegLoss, self).__init__() def forward(self, output, mask, ind, target): pred = _transpose_and_gather_feat(output, ind) loss = _reg_loss(pred, target, mask) return loss class RegL1Loss(nn.Module): def __init__(self): super(RegL1Loss, self).__init__() def forward(self, output, mask, ind, target): pred = _transpose_and_gather_feat(output, ind) mask = mask.unsqueeze(2).expand_as(pred).float() # loss = F.l1_loss(pred * mask, target * mask, reduction='elementwise_mean') loss = F.l1_loss(pred * mask, target * mask, size_average=False) loss = loss / (mask.sum() + 1e-4) return loss class NormRegL1Loss(nn.Module): def __init__(self): super(NormRegL1Loss, self).__init__() def forward(self, output, mask, ind, target): pred = _transpose_and_gather_feat(output, ind) mask = mask.unsqueeze(2).expand_as(pred).float() # loss = F.l1_loss(pred * mask, target * mask, reduction='elementwise_mean') pred = pred / (target + 1e-4) target = target * 0 + 1 loss = F.l1_loss(pred * mask, target * mask, size_average=False) loss = loss / (mask.sum() + 1e-4) return loss class RegWeightedL1Loss(nn.Module): def __init__(self): super(RegWeightedL1Loss, self).__init__() def forward(self, output, mask, ind, target): pred = _transpose_and_gather_feat(output, ind) mask = mask.float() # loss = F.l1_loss(pred * mask, target * mask, reduction='elementwise_mean') loss = F.l1_loss(pred * mask, target * mask, size_average=False) loss = loss / (mask.sum() + 1e-4) return loss class L1Loss(nn.Module): def __init__(self): super(L1Loss, self).__init__() def forward(self, output, mask, ind, target): pred = _transpose_and_gather_feat(output, ind) mask = mask.unsqueeze(2).expand_as(pred).float() loss = F.l1_loss(pred * mask, target * mask, reduction='elementwise_mean') return loss class BinRotLoss(nn.Module): def __init__(self): super(BinRotLoss, self).__init__() def forward(self, output, mask, ind, rotbin, rotres): pred = _transpose_and_gather_feat(output, ind) loss = compute_rot_loss(pred, rotbin, rotres, mask) return loss def compute_res_loss(output, target): return F.smooth_l1_loss(output, target, reduction='elementwise_mean') # TODO: weight def compute_bin_loss(output, target, mask): mask = mask.expand_as(output) output = output * mask.float() return F.cross_entropy(output, target, reduction='elementwise_mean') def compute_rot_loss(output, target_bin, target_res, mask): # output: (B, 128, 8) [bin1_cls[0], bin1_cls[1], bin1_sin, bin1_cos, # bin2_cls[0], bin2_cls[1], bin2_sin, bin2_cos] # target_bin: (B, 128, 2) [bin1_cls, bin2_cls] # target_res: (B, 128, 2) [bin1_res, bin2_res] # mask: (B, 128, 1) # import pdb; pdb.set_trace() output = output.view(-1, 8) target_bin = target_bin.view(-1, 2) target_res = target_res.view(-1, 2) mask = mask.view(-1, 1) loss_bin1 = compute_bin_loss(output[:, 0:2], target_bin[:, 0], mask) loss_bin2 = compute_bin_loss(output[:, 4:6], target_bin[:, 1], mask) loss_res = torch.zeros_like(loss_bin1) if target_bin[:, 0].nonzero().shape[0] > 0: idx1 = target_bin[:, 0].nonzero()[:, 0] valid_output1 = torch.index_select(output, 0, idx1.long()) valid_target_res1 = torch.index_select(target_res, 0, idx1.long()) loss_sin1 = compute_res_loss( valid_output1[:, 2], torch.sin(valid_target_res1[:, 0])) loss_cos1 = compute_res_loss( valid_output1[:, 3], torch.cos(valid_target_res1[:, 0])) loss_res += loss_sin1 + loss_cos1 if target_bin[:, 1].nonzero().shape[0] > 0: idx2 = target_bin[:, 1].nonzero()[:, 0] valid_output2 = torch.index_select(output, 0, idx2.long()) valid_target_res2 = torch.index_select(target_res, 0, idx2.long()) loss_sin2 = compute_res_loss( valid_output2[:, 6], torch.sin(valid_target_res2[:, 1])) loss_cos2 = compute_res_loss( valid_output2[:, 7], torch.cos(valid_target_res2[:, 1])) loss_res += loss_sin2 + loss_cos2 return loss_bin1 + loss_bin2 + loss_res
7,843
31.957983
80
py
SyNet
SyNet-master/CenterNet/src/lib/models/data_parallel.py
import torch from torch.nn.modules import Module from torch.nn.parallel.scatter_gather import gather from torch.nn.parallel.replicate import replicate from torch.nn.parallel.parallel_apply import parallel_apply from .scatter_gather import scatter_kwargs class _DataParallel(Module): r"""Implements data parallelism at the module level. This container parallelizes the application of the given module by splitting the input across the specified devices by chunking in the batch dimension. In the forward pass, the module is replicated on each device, and each replica handles a portion of the input. During the backwards pass, gradients from each replica are summed into the original module. The batch size should be larger than the number of GPUs used. It should also be an integer multiple of the number of GPUs so that each chunk is the same size (so that each GPU processes the same number of samples). See also: :ref:`cuda-nn-dataparallel-instead` Arbitrary positional and keyword inputs are allowed to be passed into DataParallel EXCEPT Tensors. All variables will be scattered on dim specified (default 0). Primitive types will be broadcasted, but all other types will be a shallow copy and can be corrupted if written to in the model's forward pass. Args: module: module to be parallelized device_ids: CUDA devices (default: all devices) output_device: device location of output (default: device_ids[0]) Example:: >>> net = torch.nn.DataParallel(model, device_ids=[0, 1, 2]) >>> output = net(input_var) """ # TODO: update notes/cuda.rst when this class handles 8+ GPUs well def __init__(self, module, device_ids=None, output_device=None, dim=0, chunk_sizes=None): super(_DataParallel, self).__init__() if not torch.cuda.is_available(): self.module = module self.device_ids = [] return if device_ids is None: device_ids = list(range(torch.cuda.device_count())) if output_device is None: output_device = device_ids[0] self.dim = dim self.module = module self.device_ids = device_ids self.chunk_sizes = chunk_sizes self.output_device = output_device if len(self.device_ids) == 1: self.module.cuda(device_ids[0]) def forward(self, *inputs, **kwargs): if not self.device_ids: return self.module(*inputs, **kwargs) inputs, kwargs = self.scatter(inputs, kwargs, self.device_ids, self.chunk_sizes) if len(self.device_ids) == 1: return self.module(*inputs[0], **kwargs[0]) replicas = self.replicate(self.module, self.device_ids[:len(inputs)]) outputs = self.parallel_apply(replicas, inputs, kwargs) return self.gather(outputs, self.output_device) def replicate(self, module, device_ids): return replicate(module, device_ids) def scatter(self, inputs, kwargs, device_ids, chunk_sizes): return scatter_kwargs(inputs, kwargs, device_ids, dim=self.dim, chunk_sizes=self.chunk_sizes) def parallel_apply(self, replicas, inputs, kwargs): return parallel_apply(replicas, inputs, kwargs, self.device_ids[:len(replicas)]) def gather(self, outputs, output_device): return gather(outputs, output_device, dim=self.dim) def data_parallel(module, inputs, device_ids=None, output_device=None, dim=0, module_kwargs=None): r"""Evaluates module(input) in parallel across the GPUs given in device_ids. This is the functional version of the DataParallel module. Args: module: the module to evaluate in parallel inputs: inputs to the module device_ids: GPU ids on which to replicate module output_device: GPU location of the output Use -1 to indicate the CPU. (default: device_ids[0]) Returns: a Variable containing the result of module(input) located on output_device """ if not isinstance(inputs, tuple): inputs = (inputs,) if device_ids is None: device_ids = list(range(torch.cuda.device_count())) if output_device is None: output_device = device_ids[0] inputs, module_kwargs = scatter_kwargs(inputs, module_kwargs, device_ids, dim) if len(device_ids) == 1: return module(*inputs[0], **module_kwargs[0]) used_device_ids = device_ids[:len(inputs)] replicas = replicate(module, used_device_ids) outputs = parallel_apply(replicas, inputs, module_kwargs, used_device_ids) return gather(outputs, output_device, dim) def DataParallel(module, device_ids=None, output_device=None, dim=0, chunk_sizes=None): if chunk_sizes is None: return torch.nn.DataParallel(module, device_ids, output_device, dim) standard_size = True for i in range(1, len(chunk_sizes)): if chunk_sizes[i] != chunk_sizes[0]: standard_size = False if standard_size: return torch.nn.DataParallel(module, device_ids, output_device, dim) return _DataParallel(module, device_ids, output_device, dim, chunk_sizes)
5,176
39.445313
101
py
SyNet
SyNet-master/CenterNet/src/lib/models/utils.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import torch import torch.nn as nn def _sigmoid(x): y = torch.clamp(x.sigmoid_(), min=1e-4, max=1-1e-4) return y def _gather_feat(feat, ind, mask=None): dim = feat.size(2) ind = ind.unsqueeze(2).expand(ind.size(0), ind.size(1), dim) feat = feat.gather(1, ind) if mask is not None: mask = mask.unsqueeze(2).expand_as(feat) feat = feat[mask] feat = feat.view(-1, dim) return feat def _transpose_and_gather_feat(feat, ind): feat = feat.permute(0, 2, 3, 1).contiguous() feat = feat.view(feat.size(0), -1, feat.size(3)) feat = _gather_feat(feat, ind) return feat def flip_tensor(x): return torch.flip(x, [3]) # tmp = x.detach().cpu().numpy()[..., ::-1].copy() # return torch.from_numpy(tmp).to(x.device) def flip_lr(x, flip_idx): tmp = x.detach().cpu().numpy()[..., ::-1].copy() shape = tmp.shape for e in flip_idx: tmp[:, e[0], ...], tmp[:, e[1], ...] = \ tmp[:, e[1], ...].copy(), tmp[:, e[0], ...].copy() return torch.from_numpy(tmp.reshape(shape)).to(x.device) def flip_lr_off(x, flip_idx): tmp = x.detach().cpu().numpy()[..., ::-1].copy() shape = tmp.shape tmp = tmp.reshape(tmp.shape[0], 17, 2, tmp.shape[2], tmp.shape[3]) tmp[:, :, 0, :, :] *= -1 for e in flip_idx: tmp[:, e[0], ...], tmp[:, e[1], ...] = \ tmp[:, e[1], ...].copy(), tmp[:, e[0], ...].copy() return torch.from_numpy(tmp.reshape(shape)).to(x.device)
1,571
30.44
65
py
SyNet
SyNet-master/CenterNet/src/lib/models/model.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import torchvision.models as models import torch import torch.nn as nn import os from .networks.msra_resnet import get_pose_net from .networks.dlav0 import get_pose_net as get_dlav0 from .networks.pose_dla_dcn import get_pose_net as get_dla_dcn from .networks.resnet_dcn import get_pose_net as get_pose_net_dcn from .networks.large_hourglass import get_large_hourglass_net _model_factory = { 'res': get_pose_net, # default Resnet with deconv 'dlav0': get_dlav0, # default DLAup 'dla': get_dla_dcn, 'resdcn': get_pose_net_dcn, 'hourglass': get_large_hourglass_net, } def create_model(arch, heads, head_conv): num_layers = int(arch[arch.find('_') + 1:]) if '_' in arch else 0 arch = arch[:arch.find('_')] if '_' in arch else arch get_model = _model_factory[arch] model = get_model(num_layers=num_layers, heads=heads, head_conv=head_conv) return model def load_model(model, model_path, optimizer=None, resume=False, lr=None, lr_step=None): start_epoch = 0 checkpoint = torch.load(model_path, map_location=lambda storage, loc: storage) print('loaded {}, epoch {}'.format(model_path, checkpoint['epoch'])) state_dict_ = checkpoint['state_dict'] state_dict = {} # convert data_parallal to model for k in state_dict_: if k.startswith('module') and not k.startswith('module_list'): state_dict[k[7:]] = state_dict_[k] else: state_dict[k] = state_dict_[k] model_state_dict = model.state_dict() # check loaded parameters and created model parameters msg = 'If you see this, your model does not fully load the ' + \ 'pre-trained weight. Please make sure ' + \ 'you have correctly specified --arch xxx ' + \ 'or set the correct --num_classes for your own dataset.' for k in state_dict: if k in model_state_dict: if state_dict[k].shape != model_state_dict[k].shape: print('Skip loading parameter {}, required shape{}, '\ 'loaded shape{}. {}'.format( k, model_state_dict[k].shape, state_dict[k].shape, msg)) state_dict[k] = model_state_dict[k] else: print('Drop parameter {}.'.format(k) + msg) for k in model_state_dict: if not (k in state_dict): print('No param {}.'.format(k) + msg) state_dict[k] = model_state_dict[k] model.load_state_dict(state_dict, strict=False) # resume optimizer parameters if optimizer is not None and resume: if 'optimizer' in checkpoint: optimizer.load_state_dict(checkpoint['optimizer']) start_epoch = checkpoint['epoch'] start_lr = lr for step in lr_step: if start_epoch >= step: start_lr *= 0.1 for param_group in optimizer.param_groups: param_group['lr'] = start_lr print('Resumed optimizer with start lr', start_lr) else: print('No optimizer parameters in checkpoint.') if optimizer is not None: return model, optimizer, start_epoch else: return model def save_model(path, epoch, model, optimizer=None): if isinstance(model, torch.nn.DataParallel): state_dict = model.module.state_dict() else: state_dict = model.state_dict() data = {'epoch': epoch, 'state_dict': state_dict} if not (optimizer is None): data['optimizer'] = optimizer.state_dict() torch.save(data, path)
3,415
34.216495
80
py
SyNet
SyNet-master/CenterNet/src/lib/models/scatter_gather.py
import torch from torch.autograd import Variable from torch.nn.parallel._functions import Scatter, Gather def scatter(inputs, target_gpus, dim=0, chunk_sizes=None): r""" Slices variables into approximately equal chunks and distributes them across given GPUs. Duplicates references to objects that are not variables. Does not support Tensors. """ def scatter_map(obj): if isinstance(obj, Variable): return Scatter.apply(target_gpus, chunk_sizes, dim, obj) assert not torch.is_tensor(obj), "Tensors not supported in scatter." if isinstance(obj, tuple): return list(zip(*map(scatter_map, obj))) if isinstance(obj, list): return list(map(list, zip(*map(scatter_map, obj)))) if isinstance(obj, dict): return list(map(type(obj), zip(*map(scatter_map, obj.items())))) return [obj for targets in target_gpus] return scatter_map(inputs) def scatter_kwargs(inputs, kwargs, target_gpus, dim=0, chunk_sizes=None): r"""Scatter with support for kwargs dictionary""" inputs = scatter(inputs, target_gpus, dim, chunk_sizes) if inputs else [] kwargs = scatter(kwargs, target_gpus, dim, chunk_sizes) if kwargs else [] if len(inputs) < len(kwargs): inputs.extend([() for _ in range(len(kwargs) - len(inputs))]) elif len(kwargs) < len(inputs): kwargs.extend([{} for _ in range(len(inputs) - len(kwargs))]) inputs = tuple(inputs) kwargs = tuple(kwargs) return inputs, kwargs
1,535
38.384615
77
py
SyNet
SyNet-master/CenterNet/src/lib/models/networks/resnet_dcn.py
# ------------------------------------------------------------------------------ # Copyright (c) Microsoft # Licensed under the MIT License. # Written by Bin Xiao (Bin.Xiao@microsoft.com) # Modified by Dequan Wang and Xingyi Zhou # ------------------------------------------------------------------------------ from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import math import logging import torch import torch.nn as nn from .DCNv2.dcn_v2 import DCN import torch.utils.model_zoo as model_zoo BN_MOMENTUM = 0.1 logger = logging.getLogger(__name__) model_urls = { 'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth', 'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth', 'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth', 'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth', 'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth', } def conv3x3(in_planes, out_planes, stride=1): """3x3 convolution with padding""" return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False) class BasicBlock(nn.Module): expansion = 1 def __init__(self, inplanes, planes, stride=1, downsample=None): super(BasicBlock, self).__init__() self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM) self.relu = nn.ReLU(inplace=True) self.conv2 = conv3x3(planes, planes) self.bn2 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM) self.downsample = downsample self.stride = stride def forward(self, x): residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) if self.downsample is not None: residual = self.downsample(x) out += residual out = self.relu(out) return out class Bottleneck(nn.Module): expansion = 4 def __init__(self, inplanes, planes, stride=1, downsample=None): super(Bottleneck, self).__init__() self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM) self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM) self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1, bias=False) self.bn3 = nn.BatchNorm2d(planes * self.expansion, momentum=BN_MOMENTUM) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride def forward(self, x): residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) if self.downsample is not None: residual = self.downsample(x) out += residual out = self.relu(out) return out def fill_up_weights(up): w = up.weight.data f = math.ceil(w.size(2) / 2) c = (2 * f - 1 - f % 2) / (2. * f) for i in range(w.size(2)): for j in range(w.size(3)): w[0, 0, i, j] = \ (1 - math.fabs(i / f - c)) * (1 - math.fabs(j / f - c)) for c in range(1, w.size(0)): w[c, 0, :, :] = w[0, 0, :, :] def fill_fc_weights(layers): for m in layers.modules(): if isinstance(m, nn.Conv2d): nn.init.normal_(m.weight, std=0.001) # torch.nn.init.kaiming_normal_(m.weight.data, nonlinearity='relu') # torch.nn.init.xavier_normal_(m.weight.data) if m.bias is not None: nn.init.constant_(m.bias, 0) class PoseResNet(nn.Module): def __init__(self, block, layers, heads, head_conv): self.inplanes = 64 self.heads = heads self.deconv_with_bias = False super(PoseResNet, self).__init__() self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = nn.BatchNorm2d(64, momentum=BN_MOMENTUM) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(block, 64, layers[0]) self.layer2 = self._make_layer(block, 128, layers[1], stride=2) self.layer3 = self._make_layer(block, 256, layers[2], stride=2) self.layer4 = self._make_layer(block, 512, layers[3], stride=2) # used for deconv layers self.deconv_layers = self._make_deconv_layer( 3, [256, 128, 64], [4, 4, 4], ) for head in self.heads: classes = self.heads[head] if head_conv > 0: fc = nn.Sequential( nn.Conv2d(64, head_conv, kernel_size=3, padding=1, bias=True), nn.ReLU(inplace=True), nn.Conv2d(head_conv, classes, kernel_size=1, stride=1, padding=0, bias=True)) if 'hm' in head: fc[-1].bias.data.fill_(-2.19) else: fill_fc_weights(fc) else: fc = nn.Conv2d(64, classes, kernel_size=1, stride=1, padding=0, bias=True) if 'hm' in head: fc.bias.data.fill_(-2.19) else: fill_fc_weights(fc) self.__setattr__(head, fc) def _make_layer(self, block, planes, blocks, stride=1): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(planes * block.expansion, momentum=BN_MOMENTUM), ) layers = [] layers.append(block(self.inplanes, planes, stride, downsample)) self.inplanes = planes * block.expansion for i in range(1, blocks): layers.append(block(self.inplanes, planes)) return nn.Sequential(*layers) def _get_deconv_cfg(self, deconv_kernel, index): if deconv_kernel == 4: padding = 1 output_padding = 0 elif deconv_kernel == 3: padding = 1 output_padding = 1 elif deconv_kernel == 2: padding = 0 output_padding = 0 return deconv_kernel, padding, output_padding def _make_deconv_layer(self, num_layers, num_filters, num_kernels): assert num_layers == len(num_filters), \ 'ERROR: num_deconv_layers is different len(num_deconv_filters)' assert num_layers == len(num_kernels), \ 'ERROR: num_deconv_layers is different len(num_deconv_filters)' layers = [] for i in range(num_layers): kernel, padding, output_padding = \ self._get_deconv_cfg(num_kernels[i], i) planes = num_filters[i] fc = DCN(self.inplanes, planes, kernel_size=(3,3), stride=1, padding=1, dilation=1, deformable_groups=1) # fc = nn.Conv2d(self.inplanes, planes, # kernel_size=3, stride=1, # padding=1, dilation=1, bias=False) # fill_fc_weights(fc) up = nn.ConvTranspose2d( in_channels=planes, out_channels=planes, kernel_size=kernel, stride=2, padding=padding, output_padding=output_padding, bias=self.deconv_with_bias) fill_up_weights(up) layers.append(fc) layers.append(nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)) layers.append(nn.ReLU(inplace=True)) layers.append(up) layers.append(nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)) layers.append(nn.ReLU(inplace=True)) self.inplanes = planes return nn.Sequential(*layers) def forward(self, x): x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.maxpool(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) x = self.deconv_layers(x) ret = {} for head in self.heads: ret[head] = self.__getattr__(head)(x) return [ret] def init_weights(self, num_layers): if 1: url = model_urls['resnet{}'.format(num_layers)] pretrained_state_dict = model_zoo.load_url(url) print('=> loading pretrained model {}'.format(url)) self.load_state_dict(pretrained_state_dict, strict=False) print('=> init deconv weights from normal distribution') for name, m in self.deconv_layers.named_modules(): if isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) resnet_spec = {18: (BasicBlock, [2, 2, 2, 2]), 34: (BasicBlock, [3, 4, 6, 3]), 50: (Bottleneck, [3, 4, 6, 3]), 101: (Bottleneck, [3, 4, 23, 3]), 152: (Bottleneck, [3, 8, 36, 3])} def get_pose_net(num_layers, heads, head_conv=256): block_class, layers = resnet_spec[num_layers] model = PoseResNet(block_class, layers, heads, head_conv=head_conv) model.init_weights(num_layers) return model
10,054
33.553265
80
py
SyNet
SyNet-master/CenterNet/src/lib/models/networks/pose_dla_dcn.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import math import logging import numpy as np from os.path import join import torch from torch import nn import torch.nn.functional as F import torch.utils.model_zoo as model_zoo from .DCNv2.dcn_v2 import DCN BN_MOMENTUM = 0.1 logger = logging.getLogger(__name__) def get_model_url(data='imagenet', name='dla34', hash='ba72cf86'): return join('http://dl.yf.io/dla/models', data, '{}-{}.pth'.format(name, hash)) def conv3x3(in_planes, out_planes, stride=1): "3x3 convolution with padding" return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False) class BasicBlock(nn.Module): def __init__(self, inplanes, planes, stride=1, dilation=1): super(BasicBlock, self).__init__() self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=3, stride=stride, padding=dilation, bias=False, dilation=dilation) self.bn1 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM) self.relu = nn.ReLU(inplace=True) self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=dilation, bias=False, dilation=dilation) self.bn2 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM) self.stride = stride def forward(self, x, residual=None): if residual is None: residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out += residual out = self.relu(out) return out class Bottleneck(nn.Module): expansion = 2 def __init__(self, inplanes, planes, stride=1, dilation=1): super(Bottleneck, self).__init__() expansion = Bottleneck.expansion bottle_planes = planes // expansion self.conv1 = nn.Conv2d(inplanes, bottle_planes, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(bottle_planes, momentum=BN_MOMENTUM) self.conv2 = nn.Conv2d(bottle_planes, bottle_planes, kernel_size=3, stride=stride, padding=dilation, bias=False, dilation=dilation) self.bn2 = nn.BatchNorm2d(bottle_planes, momentum=BN_MOMENTUM) self.conv3 = nn.Conv2d(bottle_planes, planes, kernel_size=1, bias=False) self.bn3 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM) self.relu = nn.ReLU(inplace=True) self.stride = stride def forward(self, x, residual=None): if residual is None: residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) out += residual out = self.relu(out) return out class BottleneckX(nn.Module): expansion = 2 cardinality = 32 def __init__(self, inplanes, planes, stride=1, dilation=1): super(BottleneckX, self).__init__() cardinality = BottleneckX.cardinality # dim = int(math.floor(planes * (BottleneckV5.expansion / 64.0))) # bottle_planes = dim * cardinality bottle_planes = planes * cardinality // 32 self.conv1 = nn.Conv2d(inplanes, bottle_planes, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(bottle_planes, momentum=BN_MOMENTUM) self.conv2 = nn.Conv2d(bottle_planes, bottle_planes, kernel_size=3, stride=stride, padding=dilation, bias=False, dilation=dilation, groups=cardinality) self.bn2 = nn.BatchNorm2d(bottle_planes, momentum=BN_MOMENTUM) self.conv3 = nn.Conv2d(bottle_planes, planes, kernel_size=1, bias=False) self.bn3 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM) self.relu = nn.ReLU(inplace=True) self.stride = stride def forward(self, x, residual=None): if residual is None: residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) out += residual out = self.relu(out) return out class Root(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, residual): super(Root, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 1, stride=1, bias=False, padding=(kernel_size - 1) // 2) self.bn = nn.BatchNorm2d(out_channels, momentum=BN_MOMENTUM) self.relu = nn.ReLU(inplace=True) self.residual = residual def forward(self, *x): children = x x = self.conv(torch.cat(x, 1)) x = self.bn(x) if self.residual: x += children[0] x = self.relu(x) return x class Tree(nn.Module): def __init__(self, levels, block, in_channels, out_channels, stride=1, level_root=False, root_dim=0, root_kernel_size=1, dilation=1, root_residual=False): super(Tree, self).__init__() if root_dim == 0: root_dim = 2 * out_channels if level_root: root_dim += in_channels if levels == 1: self.tree1 = block(in_channels, out_channels, stride, dilation=dilation) self.tree2 = block(out_channels, out_channels, 1, dilation=dilation) else: self.tree1 = Tree(levels - 1, block, in_channels, out_channels, stride, root_dim=0, root_kernel_size=root_kernel_size, dilation=dilation, root_residual=root_residual) self.tree2 = Tree(levels - 1, block, out_channels, out_channels, root_dim=root_dim + out_channels, root_kernel_size=root_kernel_size, dilation=dilation, root_residual=root_residual) if levels == 1: self.root = Root(root_dim, out_channels, root_kernel_size, root_residual) self.level_root = level_root self.root_dim = root_dim self.downsample = None self.project = None self.levels = levels if stride > 1: self.downsample = nn.MaxPool2d(stride, stride=stride) if in_channels != out_channels: self.project = nn.Sequential( nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, bias=False), nn.BatchNorm2d(out_channels, momentum=BN_MOMENTUM) ) def forward(self, x, residual=None, children=None): children = [] if children is None else children bottom = self.downsample(x) if self.downsample else x residual = self.project(bottom) if self.project else bottom if self.level_root: children.append(bottom) x1 = self.tree1(x, residual) if self.levels == 1: x2 = self.tree2(x1) x = self.root(x2, x1, *children) else: children.append(x1) x = self.tree2(x1, children=children) return x class DLA(nn.Module): def __init__(self, levels, channels, num_classes=1000, block=BasicBlock, residual_root=False, linear_root=False): super(DLA, self).__init__() self.channels = channels self.num_classes = num_classes self.base_layer = nn.Sequential( nn.Conv2d(3, channels[0], kernel_size=7, stride=1, padding=3, bias=False), nn.BatchNorm2d(channels[0], momentum=BN_MOMENTUM), nn.ReLU(inplace=True)) self.level0 = self._make_conv_level( channels[0], channels[0], levels[0]) self.level1 = self._make_conv_level( channels[0], channels[1], levels[1], stride=2) self.level2 = Tree(levels[2], block, channels[1], channels[2], 2, level_root=False, root_residual=residual_root) self.level3 = Tree(levels[3], block, channels[2], channels[3], 2, level_root=True, root_residual=residual_root) self.level4 = Tree(levels[4], block, channels[3], channels[4], 2, level_root=True, root_residual=residual_root) self.level5 = Tree(levels[5], block, channels[4], channels[5], 2, level_root=True, root_residual=residual_root) # for m in self.modules(): # if isinstance(m, nn.Conv2d): # n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels # m.weight.data.normal_(0, math.sqrt(2. / n)) # elif isinstance(m, nn.BatchNorm2d): # m.weight.data.fill_(1) # m.bias.data.zero_() def _make_level(self, block, inplanes, planes, blocks, stride=1): downsample = None if stride != 1 or inplanes != planes: downsample = nn.Sequential( nn.MaxPool2d(stride, stride=stride), nn.Conv2d(inplanes, planes, kernel_size=1, stride=1, bias=False), nn.BatchNorm2d(planes, momentum=BN_MOMENTUM), ) layers = [] layers.append(block(inplanes, planes, stride, downsample=downsample)) for i in range(1, blocks): layers.append(block(inplanes, planes)) return nn.Sequential(*layers) def _make_conv_level(self, inplanes, planes, convs, stride=1, dilation=1): modules = [] for i in range(convs): modules.extend([ nn.Conv2d(inplanes, planes, kernel_size=3, stride=stride if i == 0 else 1, padding=dilation, bias=False, dilation=dilation), nn.BatchNorm2d(planes, momentum=BN_MOMENTUM), nn.ReLU(inplace=True)]) inplanes = planes return nn.Sequential(*modules) def forward(self, x): y = [] x = self.base_layer(x) for i in range(6): x = getattr(self, 'level{}'.format(i))(x) y.append(x) return y def load_pretrained_model(self, data='imagenet', name='dla34', hash='ba72cf86'): # fc = self.fc if name.endswith('.pth'): model_weights = torch.load(data + name) else: model_url = get_model_url(data, name, hash) model_weights = model_zoo.load_url(model_url) num_classes = len(model_weights[list(model_weights.keys())[-1]]) self.fc = nn.Conv2d( self.channels[-1], num_classes, kernel_size=1, stride=1, padding=0, bias=True) self.load_state_dict(model_weights) # self.fc = fc def dla34(pretrained=True, **kwargs): # DLA-34 model = DLA([1, 1, 1, 2, 2, 1], [16, 32, 64, 128, 256, 512], block=BasicBlock, **kwargs) if pretrained: model.load_pretrained_model(data='imagenet', name='dla34', hash='ba72cf86') return model class Identity(nn.Module): def __init__(self): super(Identity, self).__init__() def forward(self, x): return x def fill_fc_weights(layers): for m in layers.modules(): if isinstance(m, nn.Conv2d): if m.bias is not None: nn.init.constant_(m.bias, 0) def fill_up_weights(up): w = up.weight.data f = math.ceil(w.size(2) / 2) c = (2 * f - 1 - f % 2) / (2. * f) for i in range(w.size(2)): for j in range(w.size(3)): w[0, 0, i, j] = \ (1 - math.fabs(i / f - c)) * (1 - math.fabs(j / f - c)) for c in range(1, w.size(0)): w[c, 0, :, :] = w[0, 0, :, :] class DeformConv(nn.Module): def __init__(self, chi, cho): super(DeformConv, self).__init__() self.actf = nn.Sequential( nn.BatchNorm2d(cho, momentum=BN_MOMENTUM), nn.ReLU(inplace=True) ) self.conv = DCN(chi, cho, kernel_size=(3,3), stride=1, padding=1, dilation=1, deformable_groups=1) def forward(self, x): x = self.conv(x) x = self.actf(x) return x class IDAUp(nn.Module): def __init__(self, o, channels, up_f): super(IDAUp, self).__init__() for i in range(1, len(channels)): c = channels[i] f = int(up_f[i]) proj = DeformConv(c, o) node = DeformConv(o, o) up = nn.ConvTranspose2d(o, o, f * 2, stride=f, padding=f // 2, output_padding=0, groups=o, bias=False) fill_up_weights(up) setattr(self, 'proj_' + str(i), proj) setattr(self, 'up_' + str(i), up) setattr(self, 'node_' + str(i), node) def forward(self, layers, startp, endp): for i in range(startp + 1, endp): upsample = getattr(self, 'up_' + str(i - startp)) project = getattr(self, 'proj_' + str(i - startp)) layers[i] = upsample(project(layers[i])) node = getattr(self, 'node_' + str(i - startp)) layers[i] = node(layers[i] + layers[i - 1]) class DLAUp(nn.Module): def __init__(self, startp, channels, scales, in_channels=None): super(DLAUp, self).__init__() self.startp = startp if in_channels is None: in_channels = channels self.channels = channels channels = list(channels) scales = np.array(scales, dtype=int) for i in range(len(channels) - 1): j = -i - 2 setattr(self, 'ida_{}'.format(i), IDAUp(channels[j], in_channels[j:], scales[j:] // scales[j])) scales[j + 1:] = scales[j] in_channels[j + 1:] = [channels[j] for _ in channels[j + 1:]] def forward(self, layers): out = [layers[-1]] # start with 32 for i in range(len(layers) - self.startp - 1): ida = getattr(self, 'ida_{}'.format(i)) ida(layers, len(layers) -i - 2, len(layers)) out.insert(0, layers[-1]) return out class Interpolate(nn.Module): def __init__(self, scale, mode): super(Interpolate, self).__init__() self.scale = scale self.mode = mode def forward(self, x): x = F.interpolate(x, scale_factor=self.scale, mode=self.mode, align_corners=False) return x class DLASeg(nn.Module): def __init__(self, base_name, heads, pretrained, down_ratio, final_kernel, last_level, head_conv, out_channel=0): super(DLASeg, self).__init__() assert down_ratio in [2, 4, 8, 16] self.first_level = int(np.log2(down_ratio)) self.last_level = last_level self.base = globals()[base_name](pretrained=pretrained) channels = self.base.channels scales = [2 ** i for i in range(len(channels[self.first_level:]))] self.dla_up = DLAUp(self.first_level, channels[self.first_level:], scales) if out_channel == 0: out_channel = channels[self.first_level] self.ida_up = IDAUp(out_channel, channels[self.first_level:self.last_level], [2 ** i for i in range(self.last_level - self.first_level)]) self.heads = heads for head in self.heads: classes = self.heads[head] if head_conv > 0: fc = nn.Sequential( nn.Conv2d(channels[self.first_level], head_conv, kernel_size=3, padding=1, bias=True), nn.ReLU(inplace=True), nn.Conv2d(head_conv, classes, kernel_size=final_kernel, stride=1, padding=final_kernel // 2, bias=True)) if 'hm' in head: fc[-1].bias.data.fill_(-2.19) else: fill_fc_weights(fc) else: fc = nn.Conv2d(channels[self.first_level], classes, kernel_size=final_kernel, stride=1, padding=final_kernel // 2, bias=True) if 'hm' in head: fc.bias.data.fill_(-2.19) else: fill_fc_weights(fc) self.__setattr__(head, fc) def forward(self, x): x = self.base(x) x = self.dla_up(x) y = [] for i in range(self.last_level - self.first_level): y.append(x[i].clone()) self.ida_up(y, 0, len(y)) z = {} for head in self.heads: z[head] = self.__getattr__(head)(y[-1]) return [z] def get_pose_net(num_layers, heads, head_conv=256, down_ratio=4): model = DLASeg('dla{}'.format(num_layers), heads, pretrained=True, down_ratio=down_ratio, final_kernel=1, last_level=5, head_conv=head_conv) return model
17,594
34.617409
106
py
SyNet
SyNet-master/CenterNet/src/lib/models/networks/msra_resnet.py
# ------------------------------------------------------------------------------ # Copyright (c) Microsoft # Licensed under the MIT License. # Written by Bin Xiao (Bin.Xiao@microsoft.com) # Modified by Xingyi Zhou # ------------------------------------------------------------------------------ from __future__ import absolute_import from __future__ import division from __future__ import print_function import os import torch import torch.nn as nn import torch.utils.model_zoo as model_zoo BN_MOMENTUM = 0.1 model_urls = { 'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth', 'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth', 'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth', 'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth', 'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth', } def conv3x3(in_planes, out_planes, stride=1): """3x3 convolution with padding""" return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False) class BasicBlock(nn.Module): expansion = 1 def __init__(self, inplanes, planes, stride=1, downsample=None): super(BasicBlock, self).__init__() self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM) self.relu = nn.ReLU(inplace=True) self.conv2 = conv3x3(planes, planes) self.bn2 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM) self.downsample = downsample self.stride = stride def forward(self, x): residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) if self.downsample is not None: residual = self.downsample(x) out += residual out = self.relu(out) return out class Bottleneck(nn.Module): expansion = 4 def __init__(self, inplanes, planes, stride=1, downsample=None): super(Bottleneck, self).__init__() self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM) self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM) self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1, bias=False) self.bn3 = nn.BatchNorm2d(planes * self.expansion, momentum=BN_MOMENTUM) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride def forward(self, x): residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) if self.downsample is not None: residual = self.downsample(x) out += residual out = self.relu(out) return out class PoseResNet(nn.Module): def __init__(self, block, layers, heads, head_conv, **kwargs): self.inplanes = 64 self.deconv_with_bias = False self.heads = heads super(PoseResNet, self).__init__() self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = nn.BatchNorm2d(64, momentum=BN_MOMENTUM) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(block, 64, layers[0]) self.layer2 = self._make_layer(block, 128, layers[1], stride=2) self.layer3 = self._make_layer(block, 256, layers[2], stride=2) self.layer4 = self._make_layer(block, 512, layers[3], stride=2) # used for deconv layers self.deconv_layers = self._make_deconv_layer( 3, [256, 256, 256], [4, 4, 4], ) # self.final_layer = [] for head in sorted(self.heads): num_output = self.heads[head] if head_conv > 0: fc = nn.Sequential( nn.Conv2d(256, head_conv, kernel_size=3, padding=1, bias=True), nn.ReLU(inplace=True), nn.Conv2d(head_conv, num_output, kernel_size=1, stride=1, padding=0)) else: fc = nn.Conv2d( in_channels=256, out_channels=num_output, kernel_size=1, stride=1, padding=0 ) self.__setattr__(head, fc) # self.final_layer = nn.ModuleList(self.final_layer) def _make_layer(self, block, planes, blocks, stride=1): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(planes * block.expansion, momentum=BN_MOMENTUM), ) layers = [] layers.append(block(self.inplanes, planes, stride, downsample)) self.inplanes = planes * block.expansion for i in range(1, blocks): layers.append(block(self.inplanes, planes)) return nn.Sequential(*layers) def _get_deconv_cfg(self, deconv_kernel, index): if deconv_kernel == 4: padding = 1 output_padding = 0 elif deconv_kernel == 3: padding = 1 output_padding = 1 elif deconv_kernel == 2: padding = 0 output_padding = 0 return deconv_kernel, padding, output_padding def _make_deconv_layer(self, num_layers, num_filters, num_kernels): assert num_layers == len(num_filters), \ 'ERROR: num_deconv_layers is different len(num_deconv_filters)' assert num_layers == len(num_kernels), \ 'ERROR: num_deconv_layers is different len(num_deconv_filters)' layers = [] for i in range(num_layers): kernel, padding, output_padding = \ self._get_deconv_cfg(num_kernels[i], i) planes = num_filters[i] layers.append( nn.ConvTranspose2d( in_channels=self.inplanes, out_channels=planes, kernel_size=kernel, stride=2, padding=padding, output_padding=output_padding, bias=self.deconv_with_bias)) layers.append(nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)) layers.append(nn.ReLU(inplace=True)) self.inplanes = planes return nn.Sequential(*layers) def forward(self, x): x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.maxpool(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) x = self.deconv_layers(x) ret = {} for head in self.heads: ret[head] = self.__getattr__(head)(x) return [ret] def init_weights(self, num_layers, pretrained=True): if pretrained: # print('=> init resnet deconv weights from normal distribution') for _, m in self.deconv_layers.named_modules(): if isinstance(m, nn.ConvTranspose2d): # print('=> init {}.weight as normal(0, 0.001)'.format(name)) # print('=> init {}.bias as 0'.format(name)) nn.init.normal_(m.weight, std=0.001) if self.deconv_with_bias: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): # print('=> init {}.weight as 1'.format(name)) # print('=> init {}.bias as 0'.format(name)) nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) # print('=> init final conv weights from normal distribution') for head in self.heads: final_layer = self.__getattr__(head) for i, m in enumerate(final_layer.modules()): if isinstance(m, nn.Conv2d): # nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') # print('=> init {}.weight as normal(0, 0.001)'.format(name)) # print('=> init {}.bias as 0'.format(name)) if m.weight.shape[0] == self.heads[head]: if 'hm' in head: nn.init.constant_(m.bias, -2.19) else: nn.init.normal_(m.weight, std=0.001) nn.init.constant_(m.bias, 0) #pretrained_state_dict = torch.load(pretrained) url = model_urls['resnet{}'.format(num_layers)] pretrained_state_dict = model_zoo.load_url(url) print('=> loading pretrained model {}'.format(url)) self.load_state_dict(pretrained_state_dict, strict=False) else: print('=> imagenet pretrained model dose not exist') print('=> please download it first') raise ValueError('imagenet pretrained model does not exist') resnet_spec = {18: (BasicBlock, [2, 2, 2, 2]), 34: (BasicBlock, [3, 4, 6, 3]), 50: (Bottleneck, [3, 4, 6, 3]), 101: (Bottleneck, [3, 4, 23, 3]), 152: (Bottleneck, [3, 8, 36, 3])} def get_pose_net(num_layers, heads, head_conv): block_class, layers = resnet_spec[num_layers] model = PoseResNet(block_class, layers, heads, head_conv=head_conv) model.init_weights(num_layers, pretrained=True) return model
10,167
35.185053
94
py
SyNet
SyNet-master/CenterNet/src/lib/models/networks/large_hourglass.py
# ------------------------------------------------------------------------------ # This code is base on # CornerNet (https://github.com/princeton-vl/CornerNet) # Copyright (c) 2018, University of Michigan # Licensed under the BSD 3-Clause License # ------------------------------------------------------------------------------ from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np import torch import torch.nn as nn class convolution(nn.Module): def __init__(self, k, inp_dim, out_dim, stride=1, with_bn=True): super(convolution, self).__init__() pad = (k - 1) // 2 self.conv = nn.Conv2d(inp_dim, out_dim, (k, k), padding=(pad, pad), stride=(stride, stride), bias=not with_bn) self.bn = nn.BatchNorm2d(out_dim) if with_bn else nn.Sequential() self.relu = nn.ReLU(inplace=True) def forward(self, x): conv = self.conv(x) bn = self.bn(conv) relu = self.relu(bn) return relu class fully_connected(nn.Module): def __init__(self, inp_dim, out_dim, with_bn=True): super(fully_connected, self).__init__() self.with_bn = with_bn self.linear = nn.Linear(inp_dim, out_dim) if self.with_bn: self.bn = nn.BatchNorm1d(out_dim) self.relu = nn.ReLU(inplace=True) def forward(self, x): linear = self.linear(x) bn = self.bn(linear) if self.with_bn else linear relu = self.relu(bn) return relu class residual(nn.Module): def __init__(self, k, inp_dim, out_dim, stride=1, with_bn=True): super(residual, self).__init__() self.conv1 = nn.Conv2d(inp_dim, out_dim, (3, 3), padding=(1, 1), stride=(stride, stride), bias=False) self.bn1 = nn.BatchNorm2d(out_dim) self.relu1 = nn.ReLU(inplace=True) self.conv2 = nn.Conv2d(out_dim, out_dim, (3, 3), padding=(1, 1), bias=False) self.bn2 = nn.BatchNorm2d(out_dim) self.skip = nn.Sequential( nn.Conv2d(inp_dim, out_dim, (1, 1), stride=(stride, stride), bias=False), nn.BatchNorm2d(out_dim) ) if stride != 1 or inp_dim != out_dim else nn.Sequential() self.relu = nn.ReLU(inplace=True) def forward(self, x): conv1 = self.conv1(x) bn1 = self.bn1(conv1) relu1 = self.relu1(bn1) conv2 = self.conv2(relu1) bn2 = self.bn2(conv2) skip = self.skip(x) return self.relu(bn2 + skip) def make_layer(k, inp_dim, out_dim, modules, layer=convolution, **kwargs): layers = [layer(k, inp_dim, out_dim, **kwargs)] for _ in range(1, modules): layers.append(layer(k, out_dim, out_dim, **kwargs)) return nn.Sequential(*layers) def make_layer_revr(k, inp_dim, out_dim, modules, layer=convolution, **kwargs): layers = [] for _ in range(modules - 1): layers.append(layer(k, inp_dim, inp_dim, **kwargs)) layers.append(layer(k, inp_dim, out_dim, **kwargs)) return nn.Sequential(*layers) class MergeUp(nn.Module): def forward(self, up1, up2): return up1 + up2 def make_merge_layer(dim): return MergeUp() # def make_pool_layer(dim): # return nn.MaxPool2d(kernel_size=2, stride=2) def make_pool_layer(dim): return nn.Sequential() def make_unpool_layer(dim): return nn.Upsample(scale_factor=2) def make_kp_layer(cnv_dim, curr_dim, out_dim): return nn.Sequential( convolution(3, cnv_dim, curr_dim, with_bn=False), nn.Conv2d(curr_dim, out_dim, (1, 1)) ) def make_inter_layer(dim): return residual(3, dim, dim) def make_cnv_layer(inp_dim, out_dim): return convolution(3, inp_dim, out_dim) class kp_module(nn.Module): def __init__( self, n, dims, modules, layer=residual, make_up_layer=make_layer, make_low_layer=make_layer, make_hg_layer=make_layer, make_hg_layer_revr=make_layer_revr, make_pool_layer=make_pool_layer, make_unpool_layer=make_unpool_layer, make_merge_layer=make_merge_layer, **kwargs ): super(kp_module, self).__init__() self.n = n curr_mod = modules[0] next_mod = modules[1] curr_dim = dims[0] next_dim = dims[1] self.up1 = make_up_layer( 3, curr_dim, curr_dim, curr_mod, layer=layer, **kwargs ) self.max1 = make_pool_layer(curr_dim) self.low1 = make_hg_layer( 3, curr_dim, next_dim, curr_mod, layer=layer, **kwargs ) self.low2 = kp_module( n - 1, dims[1:], modules[1:], layer=layer, make_up_layer=make_up_layer, make_low_layer=make_low_layer, make_hg_layer=make_hg_layer, make_hg_layer_revr=make_hg_layer_revr, make_pool_layer=make_pool_layer, make_unpool_layer=make_unpool_layer, make_merge_layer=make_merge_layer, **kwargs ) if self.n > 1 else \ make_low_layer( 3, next_dim, next_dim, next_mod, layer=layer, **kwargs ) self.low3 = make_hg_layer_revr( 3, next_dim, curr_dim, curr_mod, layer=layer, **kwargs ) self.up2 = make_unpool_layer(curr_dim) self.merge = make_merge_layer(curr_dim) def forward(self, x): up1 = self.up1(x) max1 = self.max1(x) low1 = self.low1(max1) low2 = self.low2(low1) low3 = self.low3(low2) up2 = self.up2(low3) return self.merge(up1, up2) class exkp(nn.Module): def __init__( self, n, nstack, dims, modules, heads, pre=None, cnv_dim=256, make_tl_layer=None, make_br_layer=None, make_cnv_layer=make_cnv_layer, make_heat_layer=make_kp_layer, make_tag_layer=make_kp_layer, make_regr_layer=make_kp_layer, make_up_layer=make_layer, make_low_layer=make_layer, make_hg_layer=make_layer, make_hg_layer_revr=make_layer_revr, make_pool_layer=make_pool_layer, make_unpool_layer=make_unpool_layer, make_merge_layer=make_merge_layer, make_inter_layer=make_inter_layer, kp_layer=residual ): super(exkp, self).__init__() self.nstack = nstack self.heads = heads curr_dim = dims[0] self.pre = nn.Sequential( convolution(7, 3, 128, stride=2), residual(3, 128, 256, stride=2) ) if pre is None else pre self.kps = nn.ModuleList([ kp_module( n, dims, modules, layer=kp_layer, make_up_layer=make_up_layer, make_low_layer=make_low_layer, make_hg_layer=make_hg_layer, make_hg_layer_revr=make_hg_layer_revr, make_pool_layer=make_pool_layer, make_unpool_layer=make_unpool_layer, make_merge_layer=make_merge_layer ) for _ in range(nstack) ]) self.cnvs = nn.ModuleList([ make_cnv_layer(curr_dim, cnv_dim) for _ in range(nstack) ]) self.inters = nn.ModuleList([ make_inter_layer(curr_dim) for _ in range(nstack - 1) ]) self.inters_ = nn.ModuleList([ nn.Sequential( nn.Conv2d(curr_dim, curr_dim, (1, 1), bias=False), nn.BatchNorm2d(curr_dim) ) for _ in range(nstack - 1) ]) self.cnvs_ = nn.ModuleList([ nn.Sequential( nn.Conv2d(cnv_dim, curr_dim, (1, 1), bias=False), nn.BatchNorm2d(curr_dim) ) for _ in range(nstack - 1) ]) ## keypoint heatmaps for head in heads.keys(): if 'hm' in head: module = nn.ModuleList([ make_heat_layer( cnv_dim, curr_dim, heads[head]) for _ in range(nstack) ]) self.__setattr__(head, module) for heat in self.__getattr__(head): heat[-1].bias.data.fill_(-2.19) else: module = nn.ModuleList([ make_regr_layer( cnv_dim, curr_dim, heads[head]) for _ in range(nstack) ]) self.__setattr__(head, module) self.relu = nn.ReLU(inplace=True) def forward(self, image): # print('image shape', image.shape) inter = self.pre(image) outs = [] for ind in range(self.nstack): kp_, cnv_ = self.kps[ind], self.cnvs[ind] kp = kp_(inter) cnv = cnv_(kp) out = {} for head in self.heads: layer = self.__getattr__(head)[ind] y = layer(cnv) out[head] = y outs.append(out) if ind < self.nstack - 1: inter = self.inters_[ind](inter) + self.cnvs_[ind](cnv) inter = self.relu(inter) inter = self.inters[ind](inter) return outs def make_hg_layer(kernel, dim0, dim1, mod, layer=convolution, **kwargs): layers = [layer(kernel, dim0, dim1, stride=2)] layers += [layer(kernel, dim1, dim1) for _ in range(mod - 1)] return nn.Sequential(*layers) class HourglassNet(exkp): def __init__(self, heads, num_stacks=2): n = 5 dims = [256, 256, 384, 384, 384, 512] modules = [2, 2, 2, 2, 2, 4] super(HourglassNet, self).__init__( n, num_stacks, dims, modules, heads, make_tl_layer=None, make_br_layer=None, make_pool_layer=make_pool_layer, make_hg_layer=make_hg_layer, kp_layer=residual, cnv_dim=256 ) def get_large_hourglass_net(num_layers, heads, head_conv): model = HourglassNet(heads, 2) return model
9,942
32.033223
118
py
SyNet
SyNet-master/CenterNet/src/lib/models/networks/dlav0.py
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import math from os.path import join import torch from torch import nn import torch.utils.model_zoo as model_zoo import numpy as np BatchNorm = nn.BatchNorm2d def get_model_url(data='imagenet', name='dla34', hash='ba72cf86'): return join('http://dl.yf.io/dla/models', data, '{}-{}.pth'.format(name, hash)) def conv3x3(in_planes, out_planes, stride=1): "3x3 convolution with padding" return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False) class BasicBlock(nn.Module): def __init__(self, inplanes, planes, stride=1, dilation=1): super(BasicBlock, self).__init__() self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=3, stride=stride, padding=dilation, bias=False, dilation=dilation) self.bn1 = BatchNorm(planes) self.relu = nn.ReLU(inplace=True) self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=dilation, bias=False, dilation=dilation) self.bn2 = BatchNorm(planes) self.stride = stride def forward(self, x, residual=None): if residual is None: residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out += residual out = self.relu(out) return out class Bottleneck(nn.Module): expansion = 2 def __init__(self, inplanes, planes, stride=1, dilation=1): super(Bottleneck, self).__init__() expansion = Bottleneck.expansion bottle_planes = planes // expansion self.conv1 = nn.Conv2d(inplanes, bottle_planes, kernel_size=1, bias=False) self.bn1 = BatchNorm(bottle_planes) self.conv2 = nn.Conv2d(bottle_planes, bottle_planes, kernel_size=3, stride=stride, padding=dilation, bias=False, dilation=dilation) self.bn2 = BatchNorm(bottle_planes) self.conv3 = nn.Conv2d(bottle_planes, planes, kernel_size=1, bias=False) self.bn3 = BatchNorm(planes) self.relu = nn.ReLU(inplace=True) self.stride = stride def forward(self, x, residual=None): if residual is None: residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) out += residual out = self.relu(out) return out class BottleneckX(nn.Module): expansion = 2 cardinality = 32 def __init__(self, inplanes, planes, stride=1, dilation=1): super(BottleneckX, self).__init__() cardinality = BottleneckX.cardinality # dim = int(math.floor(planes * (BottleneckV5.expansion / 64.0))) # bottle_planes = dim * cardinality bottle_planes = planes * cardinality // 32 self.conv1 = nn.Conv2d(inplanes, bottle_planes, kernel_size=1, bias=False) self.bn1 = BatchNorm(bottle_planes) self.conv2 = nn.Conv2d(bottle_planes, bottle_planes, kernel_size=3, stride=stride, padding=dilation, bias=False, dilation=dilation, groups=cardinality) self.bn2 = BatchNorm(bottle_planes) self.conv3 = nn.Conv2d(bottle_planes, planes, kernel_size=1, bias=False) self.bn3 = BatchNorm(planes) self.relu = nn.ReLU(inplace=True) self.stride = stride def forward(self, x, residual=None): if residual is None: residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) out += residual out = self.relu(out) return out class Root(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, residual): super(Root, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 1, stride=1, bias=False, padding=(kernel_size - 1) // 2) self.bn = BatchNorm(out_channels) self.relu = nn.ReLU(inplace=True) self.residual = residual def forward(self, *x): children = x x = self.conv(torch.cat(x, 1)) x = self.bn(x) if self.residual: x += children[0] x = self.relu(x) return x class Tree(nn.Module): def __init__(self, levels, block, in_channels, out_channels, stride=1, level_root=False, root_dim=0, root_kernel_size=1, dilation=1, root_residual=False): super(Tree, self).__init__() if root_dim == 0: root_dim = 2 * out_channels if level_root: root_dim += in_channels if levels == 1: self.tree1 = block(in_channels, out_channels, stride, dilation=dilation) self.tree2 = block(out_channels, out_channels, 1, dilation=dilation) else: self.tree1 = Tree(levels - 1, block, in_channels, out_channels, stride, root_dim=0, root_kernel_size=root_kernel_size, dilation=dilation, root_residual=root_residual) self.tree2 = Tree(levels - 1, block, out_channels, out_channels, root_dim=root_dim + out_channels, root_kernel_size=root_kernel_size, dilation=dilation, root_residual=root_residual) if levels == 1: self.root = Root(root_dim, out_channels, root_kernel_size, root_residual) self.level_root = level_root self.root_dim = root_dim self.downsample = None self.project = None self.levels = levels if stride > 1: self.downsample = nn.MaxPool2d(stride, stride=stride) if in_channels != out_channels: self.project = nn.Sequential( nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, bias=False), BatchNorm(out_channels) ) def forward(self, x, residual=None, children=None): children = [] if children is None else children bottom = self.downsample(x) if self.downsample else x residual = self.project(bottom) if self.project else bottom if self.level_root: children.append(bottom) x1 = self.tree1(x, residual) if self.levels == 1: x2 = self.tree2(x1) x = self.root(x2, x1, *children) else: children.append(x1) x = self.tree2(x1, children=children) return x class DLA(nn.Module): def __init__(self, levels, channels, num_classes=1000, block=BasicBlock, residual_root=False, return_levels=False, pool_size=7, linear_root=False): super(DLA, self).__init__() self.channels = channels self.return_levels = return_levels self.num_classes = num_classes self.base_layer = nn.Sequential( nn.Conv2d(3, channels[0], kernel_size=7, stride=1, padding=3, bias=False), BatchNorm(channels[0]), nn.ReLU(inplace=True)) self.level0 = self._make_conv_level( channels[0], channels[0], levels[0]) self.level1 = self._make_conv_level( channels[0], channels[1], levels[1], stride=2) self.level2 = Tree(levels[2], block, channels[1], channels[2], 2, level_root=False, root_residual=residual_root) self.level3 = Tree(levels[3], block, channels[2], channels[3], 2, level_root=True, root_residual=residual_root) self.level4 = Tree(levels[4], block, channels[3], channels[4], 2, level_root=True, root_residual=residual_root) self.level5 = Tree(levels[5], block, channels[4], channels[5], 2, level_root=True, root_residual=residual_root) self.avgpool = nn.AvgPool2d(pool_size) self.fc = nn.Conv2d(channels[-1], num_classes, kernel_size=1, stride=1, padding=0, bias=True) for m in self.modules(): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) elif isinstance(m, BatchNorm): m.weight.data.fill_(1) m.bias.data.zero_() def _make_level(self, block, inplanes, planes, blocks, stride=1): downsample = None if stride != 1 or inplanes != planes: downsample = nn.Sequential( nn.MaxPool2d(stride, stride=stride), nn.Conv2d(inplanes, planes, kernel_size=1, stride=1, bias=False), BatchNorm(planes), ) layers = [] layers.append(block(inplanes, planes, stride, downsample=downsample)) for i in range(1, blocks): layers.append(block(inplanes, planes)) return nn.Sequential(*layers) def _make_conv_level(self, inplanes, planes, convs, stride=1, dilation=1): modules = [] for i in range(convs): modules.extend([ nn.Conv2d(inplanes, planes, kernel_size=3, stride=stride if i == 0 else 1, padding=dilation, bias=False, dilation=dilation), BatchNorm(planes), nn.ReLU(inplace=True)]) inplanes = planes return nn.Sequential(*modules) def forward(self, x): y = [] x = self.base_layer(x) for i in range(6): x = getattr(self, 'level{}'.format(i))(x) y.append(x) if self.return_levels: return y else: x = self.avgpool(x) x = self.fc(x) x = x.view(x.size(0), -1) return x def load_pretrained_model(self, data='imagenet', name='dla34', hash='ba72cf86'): fc = self.fc if name.endswith('.pth'): model_weights = torch.load(data + name) else: model_url = get_model_url(data, name, hash) model_weights = model_zoo.load_url(model_url) num_classes = len(model_weights[list(model_weights.keys())[-1]]) self.fc = nn.Conv2d( self.channels[-1], num_classes, kernel_size=1, stride=1, padding=0, bias=True) self.load_state_dict(model_weights) self.fc = fc def dla34(pretrained, **kwargs): # DLA-34 model = DLA([1, 1, 1, 2, 2, 1], [16, 32, 64, 128, 256, 512], block=BasicBlock, **kwargs) if pretrained: model.load_pretrained_model(data='imagenet', name='dla34', hash='ba72cf86') return model def dla46_c(pretrained=None, **kwargs): # DLA-46-C Bottleneck.expansion = 2 model = DLA([1, 1, 1, 2, 2, 1], [16, 32, 64, 64, 128, 256], block=Bottleneck, **kwargs) if pretrained is not None: model.load_pretrained_model(pretrained, 'dla46_c') return model def dla46x_c(pretrained=None, **kwargs): # DLA-X-46-C BottleneckX.expansion = 2 model = DLA([1, 1, 1, 2, 2, 1], [16, 32, 64, 64, 128, 256], block=BottleneckX, **kwargs) if pretrained is not None: model.load_pretrained_model(pretrained, 'dla46x_c') return model def dla60x_c(pretrained, **kwargs): # DLA-X-60-C BottleneckX.expansion = 2 model = DLA([1, 1, 1, 2, 3, 1], [16, 32, 64, 64, 128, 256], block=BottleneckX, **kwargs) if pretrained: model.load_pretrained_model(data='imagenet', name='dla60x_c', hash='b870c45c') return model def dla60(pretrained=None, **kwargs): # DLA-60 Bottleneck.expansion = 2 model = DLA([1, 1, 1, 2, 3, 1], [16, 32, 128, 256, 512, 1024], block=Bottleneck, **kwargs) if pretrained is not None: model.load_pretrained_model(pretrained, 'dla60') return model def dla60x(pretrained=None, **kwargs): # DLA-X-60 BottleneckX.expansion = 2 model = DLA([1, 1, 1, 2, 3, 1], [16, 32, 128, 256, 512, 1024], block=BottleneckX, **kwargs) if pretrained is not None: model.load_pretrained_model(pretrained, 'dla60x') return model def dla102(pretrained=None, **kwargs): # DLA-102 Bottleneck.expansion = 2 model = DLA([1, 1, 1, 3, 4, 1], [16, 32, 128, 256, 512, 1024], block=Bottleneck, residual_root=True, **kwargs) if pretrained is not None: model.load_pretrained_model(pretrained, 'dla102') return model def dla102x(pretrained=None, **kwargs): # DLA-X-102 BottleneckX.expansion = 2 model = DLA([1, 1, 1, 3, 4, 1], [16, 32, 128, 256, 512, 1024], block=BottleneckX, residual_root=True, **kwargs) if pretrained is not None: model.load_pretrained_model(pretrained, 'dla102x') return model def dla102x2(pretrained=None, **kwargs): # DLA-X-102 64 BottleneckX.cardinality = 64 model = DLA([1, 1, 1, 3, 4, 1], [16, 32, 128, 256, 512, 1024], block=BottleneckX, residual_root=True, **kwargs) if pretrained is not None: model.load_pretrained_model(pretrained, 'dla102x2') return model def dla169(pretrained=None, **kwargs): # DLA-169 Bottleneck.expansion = 2 model = DLA([1, 1, 2, 3, 5, 1], [16, 32, 128, 256, 512, 1024], block=Bottleneck, residual_root=True, **kwargs) if pretrained is not None: model.load_pretrained_model(pretrained, 'dla169') return model def set_bn(bn): global BatchNorm BatchNorm = bn dla.BatchNorm = bn class Identity(nn.Module): def __init__(self): super(Identity, self).__init__() def forward(self, x): return x def fill_up_weights(up): w = up.weight.data f = math.ceil(w.size(2) / 2) c = (2 * f - 1 - f % 2) / (2. * f) for i in range(w.size(2)): for j in range(w.size(3)): w[0, 0, i, j] = \ (1 - math.fabs(i / f - c)) * (1 - math.fabs(j / f - c)) for c in range(1, w.size(0)): w[c, 0, :, :] = w[0, 0, :, :] class IDAUp(nn.Module): def __init__(self, node_kernel, out_dim, channels, up_factors): super(IDAUp, self).__init__() self.channels = channels self.out_dim = out_dim for i, c in enumerate(channels): if c == out_dim: proj = Identity() else: proj = nn.Sequential( nn.Conv2d(c, out_dim, kernel_size=1, stride=1, bias=False), BatchNorm(out_dim), nn.ReLU(inplace=True)) f = int(up_factors[i]) if f == 1: up = Identity() else: up = nn.ConvTranspose2d( out_dim, out_dim, f * 2, stride=f, padding=f // 2, output_padding=0, groups=out_dim, bias=False) fill_up_weights(up) setattr(self, 'proj_' + str(i), proj) setattr(self, 'up_' + str(i), up) for i in range(1, len(channels)): node = nn.Sequential( nn.Conv2d(out_dim * 2, out_dim, kernel_size=node_kernel, stride=1, padding=node_kernel // 2, bias=False), BatchNorm(out_dim), nn.ReLU(inplace=True)) setattr(self, 'node_' + str(i), node) for m in self.modules(): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) elif isinstance(m, BatchNorm): m.weight.data.fill_(1) m.bias.data.zero_() def forward(self, layers): assert len(self.channels) == len(layers), \ '{} vs {} layers'.format(len(self.channels), len(layers)) layers = list(layers) for i, l in enumerate(layers): upsample = getattr(self, 'up_' + str(i)) project = getattr(self, 'proj_' + str(i)) layers[i] = upsample(project(l)) x = layers[0] y = [] for i in range(1, len(layers)): node = getattr(self, 'node_' + str(i)) x = node(torch.cat([x, layers[i]], 1)) y.append(x) return x, y class DLAUp(nn.Module): def __init__(self, channels, scales=(1, 2, 4, 8, 16), in_channels=None): super(DLAUp, self).__init__() if in_channels is None: in_channels = channels self.channels = channels channels = list(channels) scales = np.array(scales, dtype=int) for i in range(len(channels) - 1): j = -i - 2 setattr(self, 'ida_{}'.format(i), IDAUp(3, channels[j], in_channels[j:], scales[j:] // scales[j])) scales[j + 1:] = scales[j] in_channels[j + 1:] = [channels[j] for _ in channels[j + 1:]] def forward(self, layers): layers = list(layers) assert len(layers) > 1 for i in range(len(layers) - 1): ida = getattr(self, 'ida_{}'.format(i)) x, y = ida(layers[-i - 2:]) layers[-i - 1:] = y return x def fill_fc_weights(layers): for m in layers.modules(): if isinstance(m, nn.Conv2d): nn.init.normal_(m.weight, std=0.001) # torch.nn.init.kaiming_normal_(m.weight.data, nonlinearity='relu') # torch.nn.init.xavier_normal_(m.weight.data) if m.bias is not None: nn.init.constant_(m.bias, 0) class DLASeg(nn.Module): def __init__(self, base_name, heads, pretrained=True, down_ratio=4, head_conv=256): super(DLASeg, self).__init__() assert down_ratio in [2, 4, 8, 16] self.heads = heads self.first_level = int(np.log2(down_ratio)) self.base = globals()[base_name]( pretrained=pretrained, return_levels=True) channels = self.base.channels scales = [2 ** i for i in range(len(channels[self.first_level:]))] self.dla_up = DLAUp(channels[self.first_level:], scales=scales) ''' self.fc = nn.Sequential( nn.Conv2d(channels[self.first_level], classes, kernel_size=1, stride=1, padding=0, bias=True) ) ''' for head in self.heads: classes = self.heads[head] if head_conv > 0: fc = nn.Sequential( nn.Conv2d(channels[self.first_level], head_conv, kernel_size=3, padding=1, bias=True), nn.ReLU(inplace=True), nn.Conv2d(head_conv, classes, kernel_size=1, stride=1, padding=0, bias=True)) if 'hm' in head: fc[-1].bias.data.fill_(-2.19) else: fill_fc_weights(fc) else: fc = nn.Conv2d(channels[self.first_level], classes, kernel_size=1, stride=1, padding=0, bias=True) if 'hm' in head: fc.bias.data.fill_(-2.19) else: fill_fc_weights(fc) self.__setattr__(head, fc) ''' up_factor = 2 ** self.first_level if up_factor > 1: up = nn.ConvTranspose2d(classes, classes, up_factor * 2, stride=up_factor, padding=up_factor // 2, output_padding=0, groups=classes, bias=False) fill_up_weights(up) up.weight.requires_grad = False else: up = Identity() self.up = up self.softmax = nn.LogSoftmax(dim=1) for m in self.fc.modules(): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) elif isinstance(m, BatchNorm): m.weight.data.fill_(1) m.bias.data.zero_() ''' def forward(self, x): x = self.base(x) x = self.dla_up(x[self.first_level:]) # x = self.fc(x) # y = self.softmax(self.up(x)) ret = {} for head in self.heads: ret[head] = self.__getattr__(head)(x) return [ret] ''' def optim_parameters(self, memo=None): for param in self.base.parameters(): yield param for param in self.dla_up.parameters(): yield param for param in self.fc.parameters(): yield param ''' ''' def dla34up(classes, pretrained_base=None, **kwargs): model = DLASeg('dla34', classes, pretrained_base=pretrained_base, **kwargs) return model def dla60up(classes, pretrained_base=None, **kwargs): model = DLASeg('dla60', classes, pretrained_base=pretrained_base, **kwargs) return model def dla102up(classes, pretrained_base=None, **kwargs): model = DLASeg('dla102', classes, pretrained_base=pretrained_base, **kwargs) return model def dla169up(classes, pretrained_base=None, **kwargs): model = DLASeg('dla169', classes, pretrained_base=pretrained_base, **kwargs) return model ''' def get_pose_net(num_layers, heads, head_conv=256, down_ratio=4): model = DLASeg('dla{}'.format(num_layers), heads, pretrained=True, down_ratio=down_ratio, head_conv=head_conv) return model
22,682
34.00463
86
py
SyNet
SyNet-master/CenterNet/src/lib/models/networks/DCNv2/setup.py
#!/usr/bin/env python import os import glob import torch from torch.utils.cpp_extension import CUDA_HOME from torch.utils.cpp_extension import CppExtension from torch.utils.cpp_extension import CUDAExtension from setuptools import find_packages from setuptools import setup requirements = ["torch", "torchvision"] def get_extensions(): this_dir = os.path.dirname(os.path.abspath(__file__)) extensions_dir = os.path.join(this_dir, "src") main_file = glob.glob(os.path.join(extensions_dir, "*.cpp")) source_cpu = glob.glob(os.path.join(extensions_dir, "cpu", "*.cpp")) source_cuda = glob.glob(os.path.join(extensions_dir, "cuda", "*.cu")) os.environ["CC"] = "g++" sources = main_file + source_cpu extension = CppExtension extra_compile_args = {"cxx": []} define_macros = [] if torch.cuda.is_available() and CUDA_HOME is not None: extension = CUDAExtension sources += source_cuda define_macros += [("WITH_CUDA", None)] extra_compile_args["nvcc"] = [ "-DCUDA_HAS_FP16=1", "-D__CUDA_NO_HALF_OPERATORS__", "-D__CUDA_NO_HALF_CONVERSIONS__", "-D__CUDA_NO_HALF2_OPERATORS__", ] else: #raise NotImplementedError('Cuda is not available') pass sources = [os.path.join(extensions_dir, s) for s in sources] include_dirs = [extensions_dir] ext_modules = [ extension( "_ext", sources, include_dirs=include_dirs, define_macros=define_macros, extra_compile_args=extra_compile_args, ) ] return ext_modules setup( name="DCNv2", version="0.1", author="charlesshang", url="https://github.com/charlesshang/DCNv2", description="deformable convolutional networks", packages=find_packages(exclude=("configs", "tests",)), # install_requires=requirements, ext_modules=get_extensions(), cmdclass={"build_ext": torch.utils.cpp_extension.BuildExtension}, )
2,035
27.676056
73
py
SyNet
SyNet-master/CenterNet/src/lib/trains/train_factory.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function from .ctdet import CtdetTrainer from .ddd import DddTrainer from .exdet import ExdetTrainer from .multi_pose import MultiPoseTrainer train_factory = { 'exdet': ExdetTrainer, 'ddd': DddTrainer, 'ctdet': CtdetTrainer, 'multi_pose': MultiPoseTrainer, }
371
22.25
40
py
SyNet
SyNet-master/CenterNet/src/lib/trains/exdet.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import torch import numpy as np import cv2 import sys import time from utils.debugger import Debugger from models.data_parallel import DataParallel from models.losses import FocalLoss, RegL1Loss from models.decode import agnex_ct_decode, exct_decode from models.utils import _sigmoid from .base_trainer import BaseTrainer class ExdetLoss(torch.nn.Module): def __init__(self, opt): super(ExdetLoss, self).__init__() self.crit = torch.nn.MSELoss() if opt.mse_loss else FocalLoss() self.crit_reg = RegL1Loss() self.opt = opt self.parts = ['t', 'l', 'b', 'r', 'c'] def forward(self, outputs, batch): opt = self.opt hm_loss, reg_loss = 0, 0 for s in range(opt.num_stacks): output = outputs[s] for p in self.parts: tag = 'hm_{}'.format(p) output[tag] = _sigmoid(output[tag]) hm_loss += self.crit(output[tag], batch[tag]) / opt.num_stacks if p != 'c' and opt.reg_offset and opt.off_weight > 0: reg_loss += self.crit_reg(output['reg_{}'.format(p)], batch['reg_mask'], batch['ind_{}'.format(p)], batch['reg_{}'.format(p)]) / opt.num_stacks loss = opt.hm_weight * hm_loss + opt.off_weight * reg_loss loss_stats = {'loss': loss, 'off_loss': reg_loss, 'hm_loss': hm_loss} return loss, loss_stats class ExdetTrainer(BaseTrainer): def __init__(self, opt, model, optimizer=None): super(ExdetTrainer, self).__init__(opt, model, optimizer=optimizer) self.decode = agnex_ct_decode if opt.agnostic_ex else exct_decode def _get_losses(self, opt): loss_states = ['loss', 'hm_loss', 'off_loss'] loss = ExdetLoss(opt) return loss_states, loss def debug(self, batch, output, iter_id): opt = self.opt detections = self.decode(output['hm_t'], output['hm_l'], output['hm_b'], output['hm_r'], output['hm_c']).detach().cpu().numpy() detections[:, :, :4] *= opt.input_res / opt.output_res for i in range(1): debugger = Debugger( dataset=opt.dataset, ipynb=(opt.debug==3), theme=opt.debugger_theme) pred_hm = np.zeros((opt.input_res, opt.input_res, 3), dtype=np.uint8) gt_hm = np.zeros((opt.input_res, opt.input_res, 3), dtype=np.uint8) img = batch['input'][i].detach().cpu().numpy().transpose(1, 2, 0) img = ((img * self.opt.std + self.opt.mean) * 255.).astype(np.uint8) for p in self.parts: tag = 'hm_{}'.format(p) pred = debugger.gen_colormap(output[tag][i].detach().cpu().numpy()) gt = debugger.gen_colormap(batch[tag][i].detach().cpu().numpy()) if p != 'c': pred_hm = np.maximum(pred_hm, pred) gt_hm = np.maximum(gt_hm, gt) if p == 'c' or opt.debug > 2: debugger.add_blend_img(img, pred, 'pred_{}'.format(p)) debugger.add_blend_img(img, gt, 'gt_{}'.format(p)) debugger.add_blend_img(img, pred_hm, 'pred') debugger.add_blend_img(img, gt_hm, 'gt') debugger.add_img(img, img_id='out') for k in range(len(detections[i])): if detections[i, k, 4] > 0.1: debugger.add_coco_bbox(detections[i, k, :4], detections[i, k, -1], detections[i, k, 4], img_id='out') if opt.debug == 4: debugger.save_all_imgs(opt.debug_dir, prefix='{}'.format(iter_id)) else: debugger.show_all_imgs(pause=True)
3,605
40.930233
79
py
SyNet
SyNet-master/CenterNet/src/lib/trains/ctdet.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import torch import numpy as np from models.losses import FocalLoss from models.losses import RegL1Loss, RegLoss, NormRegL1Loss, RegWeightedL1Loss from models.decode import ctdet_decode from models.utils import _sigmoid from utils.debugger import Debugger from utils.post_process import ctdet_post_process from utils.oracle_utils import gen_oracle_map from .base_trainer import BaseTrainer class CtdetLoss(torch.nn.Module): def __init__(self, opt): super(CtdetLoss, self).__init__() self.crit = torch.nn.MSELoss() if opt.mse_loss else FocalLoss() self.crit_reg = RegL1Loss() if opt.reg_loss == 'l1' else \ RegLoss() if opt.reg_loss == 'sl1' else None self.crit_wh = torch.nn.L1Loss(reduction='sum') if opt.dense_wh else \ NormRegL1Loss() if opt.norm_wh else \ RegWeightedL1Loss() if opt.cat_spec_wh else self.crit_reg self.opt = opt def forward(self, outputs, batch): opt = self.opt hm_loss, wh_loss, off_loss = 0, 0, 0 for s in range(opt.num_stacks): output = outputs[s] if not opt.mse_loss: output['hm'] = _sigmoid(output['hm']) if opt.eval_oracle_hm: output['hm'] = batch['hm'] if opt.eval_oracle_wh: output['wh'] = torch.from_numpy(gen_oracle_map( batch['wh'].detach().cpu().numpy(), batch['ind'].detach().cpu().numpy(), output['wh'].shape[3], output['wh'].shape[2])).to(opt.device) if opt.eval_oracle_offset: output['reg'] = torch.from_numpy(gen_oracle_map( batch['reg'].detach().cpu().numpy(), batch['ind'].detach().cpu().numpy(), output['reg'].shape[3], output['reg'].shape[2])).to(opt.device) hm_loss += self.crit(output['hm'], batch['hm']) / opt.num_stacks if opt.wh_weight > 0: if opt.dense_wh: mask_weight = batch['dense_wh_mask'].sum() + 1e-4 wh_loss += ( self.crit_wh(output['wh'] * batch['dense_wh_mask'], batch['dense_wh'] * batch['dense_wh_mask']) / mask_weight) / opt.num_stacks elif opt.cat_spec_wh: wh_loss += self.crit_wh( output['wh'], batch['cat_spec_mask'], batch['ind'], batch['cat_spec_wh']) / opt.num_stacks else: wh_loss += self.crit_reg( output['wh'], batch['reg_mask'], batch['ind'], batch['wh']) / opt.num_stacks if opt.reg_offset and opt.off_weight > 0: off_loss += self.crit_reg(output['reg'], batch['reg_mask'], batch['ind'], batch['reg']) / opt.num_stacks loss = opt.hm_weight * hm_loss + opt.wh_weight * wh_loss + \ opt.off_weight * off_loss loss_stats = {'loss': loss, 'hm_loss': hm_loss, 'wh_loss': wh_loss, 'off_loss': off_loss} return loss, loss_stats class CtdetTrainer(BaseTrainer): def __init__(self, opt, model, optimizer=None): super(CtdetTrainer, self).__init__(opt, model, optimizer=optimizer) def _get_losses(self, opt): loss_states = ['loss', 'hm_loss', 'wh_loss', 'off_loss'] loss = CtdetLoss(opt) return loss_states, loss def debug(self, batch, output, iter_id): opt = self.opt reg = output['reg'] if opt.reg_offset else None dets = ctdet_decode( output['hm'], output['wh'], reg=reg, cat_spec_wh=opt.cat_spec_wh, K=opt.K) dets = dets.detach().cpu().numpy().reshape(1, -1, dets.shape[2]) dets[:, :, :4] *= opt.down_ratio dets_gt = batch['meta']['gt_det'].numpy().reshape(1, -1, dets.shape[2]) dets_gt[:, :, :4] *= opt.down_ratio for i in range(1): debugger = Debugger( dataset=opt.dataset, ipynb=(opt.debug==3), theme=opt.debugger_theme) img = batch['input'][i].detach().cpu().numpy().transpose(1, 2, 0) img = np.clip((( img * opt.std + opt.mean) * 255.), 0, 255).astype(np.uint8) pred = debugger.gen_colormap(output['hm'][i].detach().cpu().numpy()) gt = debugger.gen_colormap(batch['hm'][i].detach().cpu().numpy()) debugger.add_blend_img(img, pred, 'pred_hm') debugger.add_blend_img(img, gt, 'gt_hm') debugger.add_img(img, img_id='out_pred') for k in range(len(dets[i])): if dets[i, k, 4] > opt.center_thresh: debugger.add_coco_bbox(dets[i, k, :4], dets[i, k, -1], dets[i, k, 4], img_id='out_pred') debugger.add_img(img, img_id='out_gt') for k in range(len(dets_gt[i])): if dets_gt[i, k, 4] > opt.center_thresh: debugger.add_coco_bbox(dets_gt[i, k, :4], dets_gt[i, k, -1], dets_gt[i, k, 4], img_id='out_gt') if opt.debug == 4: debugger.save_all_imgs(opt.debug_dir, prefix='{}'.format(iter_id)) else: debugger.show_all_imgs(pause=True) def save_result(self, output, batch, results): reg = output['reg'] if self.opt.reg_offset else None dets = ctdet_decode( output['hm'], output['wh'], reg=reg, cat_spec_wh=self.opt.cat_spec_wh, K=self.opt.K) dets = dets.detach().cpu().numpy().reshape(1, -1, dets.shape[2]) dets_out = ctdet_post_process( dets.copy(), batch['meta']['c'].cpu().numpy(), batch['meta']['s'].cpu().numpy(), output['hm'].shape[2], output['hm'].shape[3], output['hm'].shape[1]) results[batch['meta']['img_id'].cpu().numpy()[0]] = dets_out[0]
5,518
40.810606
78
py
SyNet
SyNet-master/CenterNet/src/lib/trains/ddd.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import torch import numpy as np from models.losses import FocalLoss, L1Loss, BinRotLoss from models.decode import ddd_decode from models.utils import _sigmoid from utils.debugger import Debugger from utils.post_process import ddd_post_process from utils.oracle_utils import gen_oracle_map from .base_trainer import BaseTrainer class DddLoss(torch.nn.Module): def __init__(self, opt): super(DddLoss, self).__init__() self.crit = torch.nn.MSELoss() if opt.mse_loss else FocalLoss() self.crit_reg = L1Loss() self.crit_rot = BinRotLoss() self.opt = opt def forward(self, outputs, batch): opt = self.opt hm_loss, dep_loss, rot_loss, dim_loss = 0, 0, 0, 0 wh_loss, off_loss = 0, 0 for s in range(opt.num_stacks): output = outputs[s] output['hm'] = _sigmoid(output['hm']) output['dep'] = 1. / (output['dep'].sigmoid() + 1e-6) - 1. if opt.eval_oracle_dep: output['dep'] = torch.from_numpy(gen_oracle_map( batch['dep'].detach().cpu().numpy(), batch['ind'].detach().cpu().numpy(), opt.output_w, opt.output_h)).to(opt.device) hm_loss += self.crit(output['hm'], batch['hm']) / opt.num_stacks if opt.dep_weight > 0: dep_loss += self.crit_reg(output['dep'], batch['reg_mask'], batch['ind'], batch['dep']) / opt.num_stacks if opt.dim_weight > 0: dim_loss += self.crit_reg(output['dim'], batch['reg_mask'], batch['ind'], batch['dim']) / opt.num_stacks if opt.rot_weight > 0: rot_loss += self.crit_rot(output['rot'], batch['rot_mask'], batch['ind'], batch['rotbin'], batch['rotres']) / opt.num_stacks if opt.reg_bbox and opt.wh_weight > 0: wh_loss += self.crit_reg(output['wh'], batch['rot_mask'], batch['ind'], batch['wh']) / opt.num_stacks if opt.reg_offset and opt.off_weight > 0: off_loss += self.crit_reg(output['reg'], batch['rot_mask'], batch['ind'], batch['reg']) / opt.num_stacks loss = opt.hm_weight * hm_loss + opt.dep_weight * dep_loss + \ opt.dim_weight * dim_loss + opt.rot_weight * rot_loss + \ opt.wh_weight * wh_loss + opt.off_weight * off_loss loss_stats = {'loss': loss, 'hm_loss': hm_loss, 'dep_loss': dep_loss, 'dim_loss': dim_loss, 'rot_loss': rot_loss, 'wh_loss': wh_loss, 'off_loss': off_loss} return loss, loss_stats class DddTrainer(BaseTrainer): def __init__(self, opt, model, optimizer=None): super(DddTrainer, self).__init__(opt, model, optimizer=optimizer) def _get_losses(self, opt): loss_states = ['loss', 'hm_loss', 'dep_loss', 'dim_loss', 'rot_loss', 'wh_loss', 'off_loss'] loss = DddLoss(opt) return loss_states, loss def debug(self, batch, output, iter_id): opt = self.opt wh = output['wh'] if opt.reg_bbox else None reg = output['reg'] if opt.reg_offset else None dets = ddd_decode(output['hm'], output['rot'], output['dep'], output['dim'], wh=wh, reg=reg, K=opt.K) # x, y, score, r1-r8, depth, dim1-dim3, cls dets = dets.detach().cpu().numpy().reshape(1, -1, dets.shape[2]) calib = batch['meta']['calib'].detach().numpy() # x, y, score, rot, depth, dim1, dim2, dim3 # if opt.dataset == 'gta': # dets[:, 12:15] /= 3 dets_pred = ddd_post_process( dets.copy(), batch['meta']['c'].detach().numpy(), batch['meta']['s'].detach().numpy(), calib, opt) dets_gt = ddd_post_process( batch['meta']['gt_det'].detach().numpy().copy(), batch['meta']['c'].detach().numpy(), batch['meta']['s'].detach().numpy(), calib, opt) #for i in range(input.size(0)): for i in range(1): debugger = Debugger(dataset=opt.dataset, ipynb=(opt.debug==3), theme=opt.debugger_theme) img = batch['input'][i].detach().cpu().numpy().transpose(1, 2, 0) img = ((img * self.opt.std + self.opt.mean) * 255.).astype(np.uint8) pred = debugger.gen_colormap( output['hm'][i].detach().cpu().numpy()) gt = debugger.gen_colormap(batch['hm'][i].detach().cpu().numpy()) debugger.add_blend_img(img, pred, 'hm_pred') debugger.add_blend_img(img, gt, 'hm_gt') # decode debugger.add_ct_detection( img, dets[i], show_box=opt.reg_bbox, center_thresh=opt.center_thresh, img_id='det_pred') debugger.add_ct_detection( img, batch['meta']['gt_det'][i].cpu().numpy().copy(), show_box=opt.reg_bbox, img_id='det_gt') debugger.add_3d_detection( batch['meta']['image_path'][i], dets_pred[i], calib[i], center_thresh=opt.center_thresh, img_id='add_pred') debugger.add_3d_detection( batch['meta']['image_path'][i], dets_gt[i], calib[i], center_thresh=opt.center_thresh, img_id='add_gt') # debugger.add_bird_view( # dets_pred[i], center_thresh=opt.center_thresh, img_id='bird_pred') # debugger.add_bird_view(dets_gt[i], img_id='bird_gt') debugger.add_bird_views( dets_pred[i], dets_gt[i], center_thresh=opt.center_thresh, img_id='bird_pred_gt') # debugger.add_blend_img(img, pred, 'out', white=True) debugger.compose_vis_add( batch['meta']['image_path'][i], dets_pred[i], calib[i], opt.center_thresh, pred, 'bird_pred_gt', img_id='out') # debugger.add_img(img, img_id='out') if opt.debug ==4: debugger.save_all_imgs(opt.debug_dir, prefix='{}'.format(iter_id)) else: debugger.show_all_imgs(pause=True) def save_result(self, output, batch, results): opt = self.opt wh = output['wh'] if opt.reg_bbox else None reg = output['reg'] if opt.reg_offset else None dets = ddd_decode(output['hm'], output['rot'], output['dep'], output['dim'], wh=wh, reg=reg, K=opt.K) # x, y, score, r1-r8, depth, dim1-dim3, cls dets = dets.detach().cpu().numpy().reshape(1, -1, dets.shape[2]) calib = batch['meta']['calib'].detach().numpy() # x, y, score, rot, depth, dim1, dim2, dim3 dets_pred = ddd_post_process( dets.copy(), batch['meta']['c'].detach().numpy(), batch['meta']['s'].detach().numpy(), calib, opt) img_id = batch['meta']['img_id'].detach().numpy()[0] results[img_id] = dets_pred[0] for j in range(1, opt.num_classes + 1): keep_inds = (results[img_id][j][:, -1] > opt.center_thresh) results[img_id][j] = results[img_id][j][keep_inds]
6,919
43.645161
80
py
SyNet
SyNet-master/CenterNet/src/lib/trains/multi_pose.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import torch import numpy as np from models.losses import FocalLoss, RegL1Loss, RegLoss, RegWeightedL1Loss from models.decode import multi_pose_decode from models.utils import _sigmoid, flip_tensor, flip_lr_off, flip_lr from utils.debugger import Debugger from utils.post_process import multi_pose_post_process from utils.oracle_utils import gen_oracle_map from .base_trainer import BaseTrainer class MultiPoseLoss(torch.nn.Module): def __init__(self, opt): super(MultiPoseLoss, self).__init__() self.crit = FocalLoss() self.crit_hm_hp = torch.nn.MSELoss() if opt.mse_loss else FocalLoss() self.crit_kp = RegWeightedL1Loss() if not opt.dense_hp else \ torch.nn.L1Loss(reduction='sum') self.crit_reg = RegL1Loss() if opt.reg_loss == 'l1' else \ RegLoss() if opt.reg_loss == 'sl1' else None self.opt = opt def forward(self, outputs, batch): opt = self.opt hm_loss, wh_loss, off_loss = 0, 0, 0 hp_loss, off_loss, hm_hp_loss, hp_offset_loss = 0, 0, 0, 0 for s in range(opt.num_stacks): output = outputs[s] output['hm'] = _sigmoid(output['hm']) if opt.hm_hp and not opt.mse_loss: output['hm_hp'] = _sigmoid(output['hm_hp']) if opt.eval_oracle_hmhp: output['hm_hp'] = batch['hm_hp'] if opt.eval_oracle_hm: output['hm'] = batch['hm'] if opt.eval_oracle_kps: if opt.dense_hp: output['hps'] = batch['dense_hps'] else: output['hps'] = torch.from_numpy(gen_oracle_map( batch['hps'].detach().cpu().numpy(), batch['ind'].detach().cpu().numpy(), opt.output_res, opt.output_res)).to(opt.device) if opt.eval_oracle_hp_offset: output['hp_offset'] = torch.from_numpy(gen_oracle_map( batch['hp_offset'].detach().cpu().numpy(), batch['hp_ind'].detach().cpu().numpy(), opt.output_res, opt.output_res)).to(opt.device) hm_loss += self.crit(output['hm'], batch['hm']) / opt.num_stacks if opt.dense_hp: mask_weight = batch['dense_hps_mask'].sum() + 1e-4 hp_loss += (self.crit_kp(output['hps'] * batch['dense_hps_mask'], batch['dense_hps'] * batch['dense_hps_mask']) / mask_weight) / opt.num_stacks else: hp_loss += self.crit_kp(output['hps'], batch['hps_mask'], batch['ind'], batch['hps']) / opt.num_stacks if opt.wh_weight > 0: wh_loss += self.crit_reg(output['wh'], batch['reg_mask'], batch['ind'], batch['wh']) / opt.num_stacks if opt.reg_offset and opt.off_weight > 0: off_loss += self.crit_reg(output['reg'], batch['reg_mask'], batch['ind'], batch['reg']) / opt.num_stacks if opt.reg_hp_offset and opt.off_weight > 0: hp_offset_loss += self.crit_reg( output['hp_offset'], batch['hp_mask'], batch['hp_ind'], batch['hp_offset']) / opt.num_stacks if opt.hm_hp and opt.hm_hp_weight > 0: hm_hp_loss += self.crit_hm_hp( output['hm_hp'], batch['hm_hp']) / opt.num_stacks loss = opt.hm_weight * hm_loss + opt.wh_weight * wh_loss + \ opt.off_weight * off_loss + opt.hp_weight * hp_loss + \ opt.hm_hp_weight * hm_hp_loss + opt.off_weight * hp_offset_loss loss_stats = {'loss': loss, 'hm_loss': hm_loss, 'hp_loss': hp_loss, 'hm_hp_loss': hm_hp_loss, 'hp_offset_loss': hp_offset_loss, 'wh_loss': wh_loss, 'off_loss': off_loss} return loss, loss_stats class MultiPoseTrainer(BaseTrainer): def __init__(self, opt, model, optimizer=None): super(MultiPoseTrainer, self).__init__(opt, model, optimizer=optimizer) def _get_losses(self, opt): loss_states = ['loss', 'hm_loss', 'hp_loss', 'hm_hp_loss', 'hp_offset_loss', 'wh_loss', 'off_loss'] loss = MultiPoseLoss(opt) return loss_states, loss def debug(self, batch, output, iter_id): opt = self.opt reg = output['reg'] if opt.reg_offset else None hm_hp = output['hm_hp'] if opt.hm_hp else None hp_offset = output['hp_offset'] if opt.reg_hp_offset else None dets = multi_pose_decode( output['hm'], output['wh'], output['hps'], reg=reg, hm_hp=hm_hp, hp_offset=hp_offset, K=opt.K) dets = dets.detach().cpu().numpy().reshape(1, -1, dets.shape[2]) dets[:, :, :4] *= opt.input_res / opt.output_res dets[:, :, 5:39] *= opt.input_res / opt.output_res dets_gt = batch['meta']['gt_det'].numpy().reshape(1, -1, dets.shape[2]) dets_gt[:, :, :4] *= opt.input_res / opt.output_res dets_gt[:, :, 5:39] *= opt.input_res / opt.output_res for i in range(1): debugger = Debugger( dataset=opt.dataset, ipynb=(opt.debug==3), theme=opt.debugger_theme) img = batch['input'][i].detach().cpu().numpy().transpose(1, 2, 0) img = np.clip((( img * opt.std + opt.mean) * 255.), 0, 255).astype(np.uint8) pred = debugger.gen_colormap(output['hm'][i].detach().cpu().numpy()) gt = debugger.gen_colormap(batch['hm'][i].detach().cpu().numpy()) debugger.add_blend_img(img, pred, 'pred_hm') debugger.add_blend_img(img, gt, 'gt_hm') debugger.add_img(img, img_id='out_pred') for k in range(len(dets[i])): if dets[i, k, 4] > opt.center_thresh: debugger.add_coco_bbox(dets[i, k, :4], dets[i, k, -1], dets[i, k, 4], img_id='out_pred') debugger.add_coco_hp(dets[i, k, 5:39], img_id='out_pred') debugger.add_img(img, img_id='out_gt') for k in range(len(dets_gt[i])): if dets_gt[i, k, 4] > opt.center_thresh: debugger.add_coco_bbox(dets_gt[i, k, :4], dets_gt[i, k, -1], dets_gt[i, k, 4], img_id='out_gt') debugger.add_coco_hp(dets_gt[i, k, 5:39], img_id='out_gt') if opt.hm_hp: pred = debugger.gen_colormap_hp(output['hm_hp'][i].detach().cpu().numpy()) gt = debugger.gen_colormap_hp(batch['hm_hp'][i].detach().cpu().numpy()) debugger.add_blend_img(img, pred, 'pred_hmhp') debugger.add_blend_img(img, gt, 'gt_hmhp') if opt.debug == 4: debugger.save_all_imgs(opt.debug_dir, prefix='{}'.format(iter_id)) else: debugger.show_all_imgs(pause=True) def save_result(self, output, batch, results): reg = output['reg'] if self.opt.reg_offset else None hm_hp = output['hm_hp'] if self.opt.hm_hp else None hp_offset = output['hp_offset'] if self.opt.reg_hp_offset else None dets = multi_pose_decode( output['hm'], output['wh'], output['hps'], reg=reg, hm_hp=hm_hp, hp_offset=hp_offset, K=self.opt.K) dets = dets.detach().cpu().numpy().reshape(1, -1, dets.shape[2]) dets_out = multi_pose_post_process( dets.copy(), batch['meta']['c'].cpu().numpy(), batch['meta']['s'].cpu().numpy(), output['hm'].shape[2], output['hm'].shape[3]) results[batch['meta']['img_id'].cpu().numpy()[0]] = dets_out[0]
7,252
44.049689
82
py
SyNet
SyNet-master/CenterNet/src/lib/trains/base_trainer.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import time import torch from progress.bar import Bar from models.data_parallel import DataParallel from utils.utils import AverageMeter class ModelWithLoss(torch.nn.Module): def __init__(self, model, loss): super(ModelWithLoss, self).__init__() self.model = model self.loss = loss def forward(self, batch): outputs = self.model(batch['input']) loss, loss_stats = self.loss(outputs, batch) return outputs[-1], loss, loss_stats class BaseTrainer(object): def __init__( self, opt, model, optimizer=None): self.opt = opt self.optimizer = optimizer self.loss_stats, self.loss = self._get_losses(opt) self.model_with_loss = ModelWithLoss(model, self.loss) def set_device(self, gpus, chunk_sizes, device): if len(gpus) > 1: self.model_with_loss = DataParallel( self.model_with_loss, device_ids=gpus, chunk_sizes=chunk_sizes).to(device) else: self.model_with_loss = self.model_with_loss.to(device) for state in self.optimizer.state.values(): for k, v in state.items(): if isinstance(v, torch.Tensor): state[k] = v.to(device=device, non_blocking=True) def run_epoch(self, phase, epoch, data_loader): model_with_loss = self.model_with_loss if phase == 'train': model_with_loss.train() else: if len(self.opt.gpus) > 1: model_with_loss = self.model_with_loss.module model_with_loss.eval() torch.cuda.empty_cache() opt = self.opt results = {} data_time, batch_time = AverageMeter(), AverageMeter() avg_loss_stats = {l: AverageMeter() for l in self.loss_stats} num_iters = len(data_loader) if opt.num_iters < 0 else opt.num_iters bar = Bar('{}/{}'.format(opt.task, opt.exp_id), max=num_iters) end = time.time() for iter_id, batch in enumerate(data_loader): if iter_id >= num_iters: break data_time.update(time.time() - end) for k in batch: if k != 'meta': batch[k] = batch[k].to(device=opt.device, non_blocking=True) output, loss, loss_stats = model_with_loss(batch) loss = loss.mean() if phase == 'train': self.optimizer.zero_grad() loss.backward() self.optimizer.step() batch_time.update(time.time() - end) end = time.time() Bar.suffix = '{phase}: [{0}][{1}/{2}]|Tot: {total:} |ETA: {eta:} '.format( epoch, iter_id, num_iters, phase=phase, total=bar.elapsed_td, eta=bar.eta_td) for l in avg_loss_stats: avg_loss_stats[l].update( loss_stats[l].mean().item(), batch['input'].size(0)) Bar.suffix = Bar.suffix + '|{} {:.4f} '.format(l, avg_loss_stats[l].avg) if not opt.hide_data_time: Bar.suffix = Bar.suffix + '|Data {dt.val:.3f}s({dt.avg:.3f}s) ' \ '|Net {bt.avg:.3f}s'.format(dt=data_time, bt=batch_time) if opt.print_iter > 0: if iter_id % opt.print_iter == 0: print('{}/{}| {}'.format(opt.task, opt.exp_id, Bar.suffix)) else: bar.next() if opt.debug > 0: self.debug(batch, output, iter_id) if opt.test: self.save_result(output, batch, results) del output, loss, loss_stats bar.finish() ret = {k: v.avg for k, v in avg_loss_stats.items()} ret['time'] = bar.elapsed_td.total_seconds() / 60. return ret, results def debug(self, batch, output, iter_id): raise NotImplementedError def save_result(self, output, batch, results): raise NotImplementedError def _get_losses(self, opt): raise NotImplementedError def val(self, epoch, data_loader): return self.run_epoch('val', epoch, data_loader) def train(self, epoch, data_loader): return self.run_epoch('train', epoch, data_loader)
3,913
31.890756
80
py
SyNet
SyNet-master/CenterNet/src/lib/datasets/dataset_factory.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function from .sample.ddd import DddDataset from .sample.exdet import EXDetDataset from .sample.ctdet import CTDetDataset from .sample.multi_pose import MultiPoseDataset from .dataset.visdrone import Visdrone from .dataset.fashion import Fashion from .dataset.coco import COCO from .dataset.pascal import PascalVOC from .dataset.kitti import KITTI from .dataset.coco_hp import COCOHP dataset_factory = { 'visdrone': Visdrone, 'fashion': Fashion, 'coco': COCO, 'pascal': PascalVOC, 'kitti': KITTI, 'coco_hp': COCOHP } _sample_factory = { 'exdet': EXDetDataset, 'ctdet': CTDetDataset, 'ddd': DddDataset, 'multi_pose': MultiPoseDataset } def get_dataset(dataset, task): class Dataset(dataset_factory[dataset], _sample_factory[task]): pass return Dataset
885
22.315789
65
py
SyNet
SyNet-master/CenterNet/src/lib/datasets/sample/exdet.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import torch.utils.data as data import pycocotools.coco as coco import numpy as np import torch import json import cv2 import os from utils.image import flip, color_aug from utils.image import get_affine_transform, affine_transform from utils.image import gaussian_radius, draw_umich_gaussian, draw_msra_gaussian import pycocotools.coco as coco import math class EXDetDataset(data.Dataset): def _coco_box_to_bbox(self, box): bbox = np.array([box[0], box[1], box[0] + box[2], box[1] + box[3]], dtype=np.float32) return bbox def _get_border(self, border, size): i = 1 while size - border // i <= border // i: i *= 2 return border // i def __getitem__(self, index): img_id = self.images[index] img_info = self.coco.loadImgs(ids=[img_id])[0] img_path = os.path.join(self.img_dir, img_info['file_name']) img = cv2.imread(img_path) height, width = img.shape[0], img.shape[1] c = np.array([img.shape[1] / 2., img.shape[0] / 2.]) s = max(img.shape[0], img.shape[1]) * 1.0 flipped = False if self.split == 'train': if not self.opt.not_rand_crop: s = s * np.random.choice(np.arange(0.6, 1.4, 0.1)) w_border = self._get_border(128, img.shape[1]) h_border = self._get_border(128, img.shape[0]) c[0] = np.random.randint(low=w_border, high=img.shape[1] - w_border) c[1] = np.random.randint(low=h_border, high=img.shape[0] - h_border) else: sf = self.opt.scale cf = self.opt.shift s = s * np.clip(np.random.randn()*sf + 1, 1 - sf, 1 + sf) c[0] += img.shape[1] * np.clip(np.random.randn()*cf, -2*cf, 2*cf) c[1] += img.shape[0] * np.clip(np.random.randn()*cf, -2*cf, 2*cf) if np.random.random() < self.opt.flip: flipped = True img = img[:, ::-1, :] trans_input = get_affine_transform( c, s, 0, [self.opt.input_res, self.opt.input_res]) inp = cv2.warpAffine(img, trans_input, (self.opt.input_res, self.opt.input_res), flags=cv2.INTER_LINEAR) inp = (inp.astype(np.float32) / 255.) if self.split == 'train' and not self.opt.no_color_aug: color_aug(self._data_rng, inp, self._eig_val, self._eig_vec) inp = (inp - self.mean) / self.std inp = inp.transpose(2, 0, 1) output_res = self.opt.output_res num_classes = self.opt.num_classes trans_output = get_affine_transform(c, s, 0, [output_res, output_res]) num_hm = 1 if self.opt.agnostic_ex else num_classes hm_t = np.zeros((num_hm, output_res, output_res), dtype=np.float32) hm_l = np.zeros((num_hm, output_res, output_res), dtype=np.float32) hm_b = np.zeros((num_hm, output_res, output_res), dtype=np.float32) hm_r = np.zeros((num_hm, output_res, output_res), dtype=np.float32) hm_c = np.zeros((num_classes, output_res, output_res), dtype=np.float32) reg_t = np.zeros((self.max_objs, 2), dtype=np.float32) reg_l = np.zeros((self.max_objs, 2), dtype=np.float32) reg_b = np.zeros((self.max_objs, 2), dtype=np.float32) reg_r = np.zeros((self.max_objs, 2), dtype=np.float32) ind_t = np.zeros((self.max_objs), dtype=np.int64) ind_l = np.zeros((self.max_objs), dtype=np.int64) ind_b = np.zeros((self.max_objs), dtype=np.int64) ind_r = np.zeros((self.max_objs), dtype=np.int64) reg_mask = np.zeros((self.max_objs), dtype=np.uint8) ann_ids = self.coco.getAnnIds(imgIds=[img_id]) anns = self.coco.loadAnns(ids=ann_ids) num_objs = min(len(anns), self.max_objs) draw_gaussian = draw_msra_gaussian if self.opt.mse_loss else \ draw_umich_gaussian for k in range(num_objs): ann = anns[k] # bbox = self._coco_box_to_bbox(ann['bbox']) # tlbr pts = np.array(ann['extreme_points'], dtype=np.float32).reshape(4, 2) # cls_id = int(self.cat_ids[ann['category_id']] - 1) # bug cls_id = int(self.cat_ids[ann['category_id']]) hm_id = 0 if self.opt.agnostic_ex else cls_id if flipped: pts[:, 0] = width - pts[:, 0] - 1 pts[1], pts[3] = pts[3].copy(), pts[1].copy() for j in range(4): pts[j] = affine_transform(pts[j], trans_output) pts = np.clip(pts, 0, self.opt.output_res - 1) h, w = pts[2, 1] - pts[0, 1], pts[3, 0] - pts[1, 0] if h > 0 and w > 0: radius = gaussian_radius((math.ceil(h), math.ceil(w))) radius = max(0, int(radius)) pt_int = pts.astype(np.int32) draw_gaussian(hm_t[hm_id], pt_int[0], radius) draw_gaussian(hm_l[hm_id], pt_int[1], radius) draw_gaussian(hm_b[hm_id], pt_int[2], radius) draw_gaussian(hm_r[hm_id], pt_int[3], radius) reg_t[k] = pts[0] - pt_int[0] reg_l[k] = pts[1] - pt_int[1] reg_b[k] = pts[2] - pt_int[2] reg_r[k] = pts[3] - pt_int[3] ind_t[k] = pt_int[0, 1] * output_res + pt_int[0, 0] ind_l[k] = pt_int[1, 1] * output_res + pt_int[1, 0] ind_b[k] = pt_int[2, 1] * output_res + pt_int[2, 0] ind_r[k] = pt_int[3, 1] * output_res + pt_int[3, 0] ct = [int((pts[3, 0] + pts[1, 0]) / 2), int((pts[0, 1] + pts[2, 1]) / 2)] draw_gaussian(hm_c[cls_id], ct, radius) reg_mask[k] = 1 ret = {'input': inp, 'hm_t': hm_t, 'hm_l': hm_l, 'hm_b': hm_b, 'hm_r': hm_r, 'hm_c': hm_c} if self.opt.reg_offset: ret.update({'reg_mask': reg_mask, 'reg_t': reg_t, 'reg_l': reg_l, 'reg_b': reg_b, 'reg_r': reg_r, 'ind_t': ind_t, 'ind_l': ind_l, 'ind_b': ind_b, 'ind_r': ind_r}) return ret
5,722
40.773723
81
py
SyNet
SyNet-master/CenterNet/src/lib/datasets/sample/ctdet.py
from __future__ import absolute_import from __future__ import division from __future__ import print_function import torch.utils.data as data import numpy as np import torch import json import cv2 import os from utils.image import flip, color_aug from utils.image import get_affine_transform, affine_transform from utils.image import gaussian_radius, draw_umich_gaussian, draw_msra_gaussian from utils.image import draw_dense_reg import math class CTDetDataset(data.Dataset): def _coco_box_to_bbox(self, box): bbox = np.array([box[0], box[1], box[0] + box[2], box[1] + box[3]], dtype=np.float32) return bbox def _get_border(self, border, size): i = 1 while size - border // i <= border // i: i *= 2 return border // i def __getitem__(self, index): img_id = self.images[index] file_name = self.coco.loadImgs(ids=[img_id])[0]['file_name'] img_path = os.path.join(self.img_dir, file_name) ann_ids = self.coco.getAnnIds(imgIds=[img_id]) anns = self.coco.loadAnns(ids=ann_ids) num_objs = min(len(anns), self.max_objs) img = cv2.imread(img_path) height, width = img.shape[0], img.shape[1] c = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32) if self.opt.keep_res: input_h = (height | self.opt.pad) + 1 input_w = (width | self.opt.pad) + 1 s = np.array([input_w, input_h], dtype=np.float32) else: s = max(img.shape[0], img.shape[1]) * 1.0 input_h, input_w = self.opt.input_h, self.opt.input_w flipped = False if self.split == 'train': if not self.opt.not_rand_crop: s = s * np.random.choice(np.arange(0.6, 1.4, 0.1)) w_border = self._get_border(128, img.shape[1]) h_border = self._get_border(128, img.shape[0]) c[0] = np.random.randint(low=w_border, high=img.shape[1] - w_border) c[1] = np.random.randint(low=h_border, high=img.shape[0] - h_border) else: sf = self.opt.scale cf = self.opt.shift c[0] += s * np.clip(np.random.randn()*cf, -2*cf, 2*cf) c[1] += s * np.clip(np.random.randn()*cf, -2*cf, 2*cf) s = s * np.clip(np.random.randn()*sf + 1, 1 - sf, 1 + sf) if np.random.random() < self.opt.flip: flipped = True img = img[:, ::-1, :] c[0] = width - c[0] - 1 trans_input = get_affine_transform( c, s, 0, [input_w, input_h]) inp = cv2.warpAffine(img, trans_input, (input_w, input_h), flags=cv2.INTER_LINEAR) inp = (inp.astype(np.float32) / 255.) if self.split == 'train' and not self.opt.no_color_aug: color_aug(self._data_rng, inp, self._eig_val, self._eig_vec) inp = (inp - self.mean) / self.std inp = inp.transpose(2, 0, 1) output_h = input_h // self.opt.down_ratio output_w = input_w // self.opt.down_ratio num_classes = self.num_classes trans_output = get_affine_transform(c, s, 0, [output_w, output_h]) hm = np.zeros((num_classes, output_h, output_w), dtype=np.float32) wh = np.zeros((self.max_objs, 2), dtype=np.float32) dense_wh = np.zeros((2, output_h, output_w), dtype=np.float32) reg = np.zeros((self.max_objs, 2), dtype=np.float32) ind = np.zeros((self.max_objs), dtype=np.int64) reg_mask = np.zeros((self.max_objs), dtype=np.uint8) cat_spec_wh = np.zeros((self.max_objs, num_classes * 2), dtype=np.float32) cat_spec_mask = np.zeros((self.max_objs, num_classes * 2), dtype=np.uint8) draw_gaussian = draw_msra_gaussian if self.opt.mse_loss else \ draw_umich_gaussian gt_det = [] for k in range(num_objs): ann = anns[k] bbox = self._coco_box_to_bbox(ann['bbox']) cls_id = int(self.cat_ids[ann['category_id']]) if flipped: bbox[[0, 2]] = width - bbox[[2, 0]] - 1 bbox[:2] = affine_transform(bbox[:2], trans_output) bbox[2:] = affine_transform(bbox[2:], trans_output) bbox[[0, 2]] = np.clip(bbox[[0, 2]], 0, output_w - 1) bbox[[1, 3]] = np.clip(bbox[[1, 3]], 0, output_h - 1) h, w = bbox[3] - bbox[1], bbox[2] - bbox[0] if h > 0 and w > 0: radius = gaussian_radius((math.ceil(h), math.ceil(w))) radius = max(0, int(radius)) radius = self.opt.hm_gauss if self.opt.mse_loss else radius ct = np.array( [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2], dtype=np.float32) ct_int = ct.astype(np.int32) draw_gaussian(hm[cls_id], ct_int, radius) wh[k] = 1. * w, 1. * h ind[k] = ct_int[1] * output_w + ct_int[0] reg[k] = ct - ct_int reg_mask[k] = 1 cat_spec_wh[k, cls_id * 2: cls_id * 2 + 2] = wh[k] cat_spec_mask[k, cls_id * 2: cls_id * 2 + 2] = 1 if self.opt.dense_wh: draw_dense_reg(dense_wh, hm.max(axis=0), ct_int, wh[k], radius) gt_det.append([ct[0] - w / 2, ct[1] - h / 2, ct[0] + w / 2, ct[1] + h / 2, 1, cls_id]) ret = {'input': inp, 'hm': hm, 'reg_mask': reg_mask, 'ind': ind, 'wh': wh} if self.opt.dense_wh: hm_a = hm.max(axis=0, keepdims=True) dense_wh_mask = np.concatenate([hm_a, hm_a], axis=0) ret.update({'dense_wh': dense_wh, 'dense_wh_mask': dense_wh_mask}) del ret['wh'] elif self.opt.cat_spec_wh: ret.update({'cat_spec_wh': cat_spec_wh, 'cat_spec_mask': cat_spec_mask}) del ret['wh'] if self.opt.reg_offset: ret.update({'reg': reg}) if self.opt.debug > 0 or not self.split == 'train': gt_det = np.array(gt_det, dtype=np.float32) if len(gt_det) > 0 else \ np.zeros((1, 6), dtype=np.float32) meta = {'c': c, 's': s, 'gt_det': gt_det, 'img_id': img_id} ret['meta'] = meta return ret
5,803
39.027586
80
py