diff --git a/.gitattributes b/.gitattributes index a6344aac8c09253b3b630fb776ae94478aa0275b..a43b0d00f374daa278f37d5500c92fb6e53d5b07 100644 --- a/.gitattributes +++ b/.gitattributes @@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text *.zip filter=lfs diff=lfs merge=lfs -text *.zst filter=lfs diff=lfs merge=lfs -text *tfevents* filter=lfs diff=lfs merge=lfs -text +pics/benchmark.png filter=lfs diff=lfs merge=lfs -text diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..0a197900e25d259ab4af2e31e78501787d7a6daa --- /dev/null +++ b/.gitignore @@ -0,0 +1,174 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# UV +# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +#uv.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/latest/usage/project/#working-with-version-control +.pdm.toml +.pdm-python +.pdm-build/ + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +# Ruff stuff: +.ruff_cache/ + +# PyPI configuration file +.pypirc diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..74aa98fa5cc9828be147b0e0d87ea083d66d045b --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +# AMPRecognitionBenchmark + +The official code for the paper "A Benchmark for Antimicrobial Peptide Recognition Based on Structure and Sequence Representation" + +Antimicrobial peptides (AMPs) serve as potent therapeutic agents against drug-resistant (DR) microbes; however, their clinical application is constrained by limitations in activity. Recently, machine learning has shown significant promise in recognizing high-activity AMPs. Nevertheless, these activity datasets about AMPs are aggregated from thousands of publications, which employ varying wet-lab experimental setups and focus on only one or a few types of DR bacteria. This heterogeneity restricts the advancement of AI methods for fair evaluation of AMP recognition. Additionally, while AlphaFold has revolutionized drug discovery through accurate protein structure predictions, the integration of these predicted structures into AMP discovery remains unexplored. + +To address these challenges, we present two key contributions: + +**(a) DRAMPAtlas 1.0**: We introduce **DRAMPAtlas 1.0**, comprising a training set sourced from public databases and a testing set derived from our wet-lab experiments. Each AMP sequence in the atlas is annotated with its 3D structure, activity data against six types of DR bacteria, and toxicity profiles. + +**(b) Comprehensive AMP recognition Experiments**: We perform extensive experiments on AMP recognition by modeling the 3D structures as voxels or graphs, either in combination with sequence information or using structure or sequence data exclusively. Our experiments reveal several insightful findings that enhance our understanding of AMP activity prediction. + +We anticipate that our benchmark and findings will aid the research community in designing more effective algorithms for discovering high-activity AMPs. + +![main](https://github.com/EricwanAR/AMPRecognitionBenchmark/blob/main/pics/benchmark.png) + +## DRAMPAtlas 1.0 Dataset +[Harvard Dataverse](https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi%3A10.7910%2FDVN%2FE9A88D)(wet-lab data not included for now, reveal after publication) + +## Instruction +### Download the code +```bash +git clone https://github.com/EricwanAR/AMPRecognitionBenchmark.git +cd AMPRecognitionBenchmark +``` + +### Dataset Preparation +1. Put the Downloaded `dataverse_files.zip` in the root directory of this project `AMPRecognitionBenchmark/`. +2. Run `bash dataproc.sh` in terminal +3. Check if there are 2 new folders `metadata/` and `pdb/` +```bash +metadata/ +├── data_simi.csv +├── data_0920_i.csv +└── ... + +pdb/ +├── pdb_af/ #AlphaFold Predicts +│ └── (pdb files ...) +└── pdb_dbassp/ #HelixFold Predicts + └── (pdb files ...) +``` + +### Training and Inferencing +1. `cd` to any unit folder, eg `cd t3.1` or `cd voxabl` +2. `python main.py [args]`, full arguments can be found in `main.py`, refer to `runthro.sh` for experiments in our paper. +3. Checkpoints will be saved in `run/` +4. For inferencing, simply replace `main.py` with `infer.py`, eg `python infer.py [training args]` \ No newline at end of file diff --git a/dataproc.sh b/dataproc.sh new file mode 100644 index 0000000000000000000000000000000000000000..bc2a816baf09b02705755c5e8a5076faa5850b25 --- /dev/null +++ b/dataproc.sh @@ -0,0 +1,5 @@ +#!/bin/bash +unzip -o -q dataverse_files.zip +mkdir pdb +unzip -q pdb.zip -d pdb +rm pdb.zip \ No newline at end of file diff --git a/graphabl/dataset.py b/graphabl/dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..a501f426606ed0218f59f43f90a2c2c555dd7bd3 --- /dev/null +++ b/graphabl/dataset.py @@ -0,0 +1,404 @@ +import os +import networkx as nx +import torch +from Bio.PDB import PDBParser, is_aa +from Bio.PDB.Polypeptide import three_to_one +from torch.utils.data import Dataset +import pandas as pd +import numpy as np +from tqdm import tqdm +from Bio.Align import PairwiseAligner +from torch_geometric.data import Data +from torch_geometric.utils import from_networkx +from Bio.SeqUtils.ProtParam import ProteinAnalysis + + +aligner = PairwiseAligner() +# Define the scoring system +aligner.mode = 'global' # Global alignment +aligner.match_score = 1 +aligner.mismatch_score = -1 +aligner.open_gap_score = -0.5 +aligner.extend_gap_score = -0.1 + + +def calculate_similarity(seq_gen, seq_tem): + + # Perform the alignment + score = aligner.score(seq_gen, seq_tem) + return score / len(seq_gen) + + +def parse_ll37_testset(path='metadata/LL37_v0.csv'): + files = open(path, 'r').readlines() + seqs = [] + for file in files: + seqs.append(str(file.strip().split(',')[-1].strip().upper())) + return seqs + + +def gmean(labels): + # Assuming 'labels' is a NumPy array of shape (n, m) + # Calculate the geometric mean across axis 1 + + # Take the product of elements along axis=1 + product = np.prod(labels, axis=1) + + # nth root of product, where n is the number of elements along axis=1 + geometric_means = product ** (1.0 / labels.shape[1]) + return geometric_means + + +def clamp(n, smallest, largest): + return sorted([smallest, n, largest])[1] + + +ATOMS = {'H': 1, 'C': 12, 'N': 14, 'O': 16, 'S': 30} +ATOMS_R = {'H': 1, 'C': 1.5, 'N': 1.5, 'O': 1.5, 'S': 2} +AMINO_ACID_WATER = {'A': 255, 'V': 255, 'P': 255, 'F': 255, 'W': 255, 'I': 255, 'L': 255, 'G': 155, 'M': 155, + 'Y': 55, 'S': 55, 'T': 55, 'C': 55, 'N': 55, 'Q': 55, 'D': 55, 'E': 55, 'K': 55, 'R': 55, 'H': 55} +AMINO_ACID_CHARGE = {'D': 55, 'E': 55, 'A': 155, 'V': 155, 'P': 155, 'F': 155, 'W': 155, 'I': 155, 'L': 155, 'G': 155, + 'M': 155, 'Y': 155, 'S': 155, 'T': 155, 'C': 155, 'N': 155, 'Q': 155, 'K': 255, 'R': 255, 'H': 255} +# AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, +# 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19} +AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, + 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19, 'X': 21} + + +def pdb_parser(structure): + """ + + """ + voxel = np.zeros((4, 64, 64, 64), dtype=np.int8) + id = '' + seq_str = '' + for i in structure[0]: + id = i.id + chain = structure[0][id] + for res in chain: + if is_aa(res.get_resname(), standard=True): + resname = res.get_resname() + amino = three_to_one(resname) + seq_str += str(amino) + ATOM_WATER = AMINO_ACID_WATER[amino] + ATOM_CHARGE = AMINO_ACID_CHARGE[amino] + ATOM_CATEGORY = AMAs[amino] * 20 + + for i in res: + if i.id not in ATOMS.keys(): + continue + x, y, z = i.get_coord() + if abs(x) > 32: + x = clamp(x, -31, 31) + if abs(y) > 32: + y = clamp(x, -31, 31) + if abs(z) > 32: + z = clamp(x, -31, 31) + x_i, y_i, z_i = int(x) + 32, int(y) + 32, int(z) + 32 + ATOM_WEIGHT = ATOMS[i.id] + ATOM_R = ATOMS_R[i.id] + + if ATOM_R <= 1.5: + voxel[0, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WEIGHT + voxel[1, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WATER + voxel[2, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CHARGE + voxel[3, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CATEGORY + else: + voxel[0, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WEIGHT + voxel[1, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WATER + voxel[2, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CHARGE + voxel[3, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CATEGORY + return voxel + + +def read_scoring_functions(pdb): + scoring = False + profile = [] + for line in open(pdb): + if line.startswith("#END_POSE_ENERGIES_TABLE"): + scoring = False + if scoring: + data = [float(v) for v in line.split()[1:]] + profile.append(data) + if line.startswith("pose"): + scoring = True + return profile + + +def load_aa_features(feature_path): + aa_features = {} + for line in open(feature_path): + line = line.strip().split() + aa, features = line[0], [float(feature) for feature in line[1:]] + aa_features[aa] = features + return aa_features + + +class PairData(Data): + def __init__(self, edge_index_s, x_s): + super(PairData, self).__init__() + self.edge_index_s = edge_index_s + self.x_s = x_s + + def __inc__(self, key, value, *args): + if key == 'edge_index_s': + return self.x_s.size(0) + if key == 'wide_nodes': + return self.x_s.num_nodes + else: + return super().__inc__(key, value, *args) + + +def calculate_property(seq): + # print(seq) + analysed_seq = ProteinAnalysis(seq) + aa_counts = analysed_seq.count_amino_acids() + aliphatic_index = ((aa_counts['A'] + 2.9 * aa_counts['V'] + 3.9 * (aa_counts['I'] + aa_counts['L'])) / len(seq)) + + charged_amino_acids = ['D', 'E', 'R', 'K', 'H'] + total_charged = sum(aa_counts[aa] for aa in charged_amino_acids) + charge_density = total_charged / len(seq) + alpha_helix, beta_helix, turn_helix = analysed_seq.secondary_structure_fraction() + + return list( + [round(analysed_seq.gravy(), 3) * 10, round(aliphatic_index, 3) * 10, round(analysed_seq.aromaticity(), 3) * 10, + round(analysed_seq.instability_index(), 3), round(alpha_helix * 10, 3), round(beta_helix * 10, 3), + round(turn_helix * 10, 3), round(analysed_seq.charge_at_pH(7), 3), round(analysed_seq.isoelectric_point(), 3), + round(charge_density, 3) * 10]) + + +def construct_graph(structure, seq, gt, pdb_path, threshold, max_length): + # Initialize an empty graph + G = nx.Graph() + + # Check if chain 'A' is present in the structure, return None if not + if 'A' not in structure[0]: + return None + chain = structure[0]['A'] + + # Add nodes to the graph for each amino acid in the chain with their 3D positions + for res in chain: + if is_aa(res.get_resname(), standard=True): + resname = res.get_resname() + alpha_c_atom = res["CA"] + x, y, z = alpha_c_atom.get_coord() # Get 3D coordinates + G.add_node(res.id[1], name=resname, position=(x, y, z)) + + # Add edges between nodes if the distance between alpha carbons is <= 5Å + for i, m in enumerate(G.nodes): + for n in list(G.nodes)[i + 1:]: + distance = chain[m]["CA"] - chain[n]["CA"] + if distance <= 5: + G.add_edge(m, n, weight=5 / distance) + + # Load scoring functions and features + scoring = read_scoring_functions(pdb_path) + # print(pdb_path) + aa_features = load_aa_features('metadata/features.txt') + + # Assign features to nodes in the graph, including the 3D position + for node_id, node_data in G.nodes(data=True): + res = chain[node_id] + aa_feature = aa_features[three_to_one(res.get_resname())] + energy_feature = scoring[node_id - 1] + position_feature = list(node_data['position']) # Convert position tuple to list + node_data['x'] = aa_feature + energy_feature + position_feature + # node_data['x'][20:40]=[0 for i in range(20)] + # node_data['x'][40:43]=[0 for i in range(3)] + + # Convert graph to data format used in ML models + G = nx.convert_node_labels_to_integers(G) + data_wt = from_networkx(G) + data_graph = PairData(data_wt.edge_index, data_wt.x) + data_graph.gt = (gt < threshold).astype(int) + global_properties = calculate_property(seq) + + # Encode the sequence and pad it to a fixed length + seq_emb = [AMAs[char] for char in seq] + [0] * (max_length - len(seq)) + + # print(seq_emb) + data_graph.seq = seq_emb + data_graph.global_f = global_properties + return data_graph + + +def remove_features(x, features_to_remove): + # 创建一个掩码来保留特征 + num_features = x.size(1) + mask = torch.ones(num_features, dtype=torch.bool) + mask[features_to_remove] = False + + # 更新节点特征矩阵 + x = x[:, mask] + + return x + + +class MDataset(Dataset): + ''' + x: Features. + y: Targets, if none, do prediction. + ''' + + def __init__(self, threshold=32, mode='train', max_length=50, pdb_src='af', data_ver='0922', exclude_feature=None): + self.num_classes = 6 + self.num_features = 43 + exclude_flag = True + if exclude_feature == 0: + self.num_features = 23 + exclude_index = torch.arange(0, 20) + elif exclude_feature == 1: + self.num_features = 23 + exclude_index = torch.arange(20, 40) + elif exclude_feature == 2: + self.num_features = 40 + exclude_index = torch.arange(40, 43) + else: + exclude_flag = False + exclude_list = pd.read_csv('../metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() + exclude_filter = False + + p = PDBParser(QUIET=True) + + if mode == 'train': + all_data = pd.read_csv(f'../metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values + exclude_filter = True + elif mode == 'qlx': + all_data = pd.read_csv('../metadata/data_qlx.csv', encoding="unicode_escape").values + elif mode == 'saap': + all_data = pd.read_csv('../metadata/data_saap.csv', encoding="unicode_escape").values + else: + raise NotImplementedError + idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) + + filter_idx_list = [] + seq_new_list = [] + label_list = [] + for idx in range(len(idx_list)): + seq = seq_list[idx].upper().strip() + if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: + continue + if exclude_filter: + if seq in exclude_list: + continue + + filter_idx_list.append(idx) + seq_new_list.append(seq) + label_list.append(labels[idx]) + + if pdb_src == 'af': + pdb_root = '../pdb/pdb_af/' + elif pdb_src == 'hf': + pdb_root = '../pdb/pdb_dbassp/' + else: + raise NotImplementedError + self.data_list = [] + for i in tqdm(range(len(filter_idx_list))): + idx = filter_idx_list[i] + seq = seq_new_list[i] + label = label_list[i] + + # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"): + # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb" + # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"): + # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb" + if os.path.exists(pdb_root + seq + ".pdb"): + pdb_path = pdb_root + seq + ".pdb" + else: + continue + # raise FileNotFoundError + + structure = p.get_structure(idx, pdb_path) + voxel1 = construct_graph(structure, seq, label, pdb_path, threshold, max_length) + if exclude_flag: + voxel1.x_s = remove_features(voxel1.x_s, exclude_index) + self.data_list.append(voxel1) + + def __getitem__(self, idx): + voxel1 = self.data_list[idx] + + return voxel1 + + def __len__(self): + return len(self.data_list) + + +# class SDataset(Dataset): +# ''' +# x: Features. +# y: Targets, if none, do prediction. +# ''' + +# def __init__(self, threshold=32, mode='train', task='0', max_length=50, pdb_src='af', data_ver='0922'): +# self.num_classes = 1 +# self.task = int(task) +# exclude_list = pd.read_csv('metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() +# exclude_filter = False + +# p = PDBParser(QUIET=True) + +# if mode == 'train': +# all_data = pd.read_csv(f'metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values +# exclude_filter = True +# elif mode == 'qlx': +# all_data = pd.read_csv('metadata/data_qlx.csv', encoding="unicode_escape").values +# elif mode == 'saap': +# all_data = pd.read_csv('metadata/data_saap.csv', encoding="unicode_escape").values +# else: +# raise NotImplementedError +# idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) +# labels = labels[:, self.task:self.task+1] + +# filter_idx_list = [] +# seq_new_list = [] +# label_list = [] +# for idx in range(len(idx_list)): +# seq = seq_list[idx].upper().strip() +# if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: +# continue +# if exclude_filter: +# if seq in exclude_list: +# continue + +# filter_idx_list.append(idx) +# seq_new_list.append(seq) +# label_list.append(labels[idx]) + +# if pdb_src == 'af': +# pdb_root = './pdb/pdb_af/' +# elif pdb_src == 'hf': +# pdb_root = './pdb/pdb_dbassp/' +# else: +# raise NotImplementedError +# self.data_list = [] +# for i in tqdm(range(len(filter_idx_list))): +# idx = filter_idx_list[i] +# seq = seq_new_list[i] +# label = label_list[i] + +# # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"): +# # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb" +# # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"): +# # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb" +# if os.path.exists(pdb_root + seq + ".pdb"): +# pdb_path = pdb_root + seq + ".pdb" +# else: +# continue +# # raise FileNotFoundError + +# structure = p.get_structure(idx, pdb_path) +# voxel1 = construct_graph(structure, seq, label, pdb_path, threshold, max_length) +# self.data_list.append(voxel1) + +# def __getitem__(self, idx): +# voxel1 = self.data_list[idx] + +# return voxel1 + +# def __len__(self): +# return len(self.data_list) + diff --git a/graphabl/eval.py b/graphabl/eval.py new file mode 100644 index 0000000000000000000000000000000000000000..035c4d57f4a93fb51a813ea83410b38e12a80b03 --- /dev/null +++ b/graphabl/eval.py @@ -0,0 +1,209 @@ +import argparse +import json +import logging +import os +import time + +from dataset import SDataset, MDataset +from network import MMGraph +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset +from torch_geometric.loader import DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=64, + help="MIC threshold for determine labels (default: 64)") +parser.add_argument('--num-layer', type=int, dest='num_layer', default=2, + help='number of GNN message passing layers (default: 2)') +parser.add_argument('--emb-dim', type=int, dest='emb_dim', default=90, + help='embedding dimensions (default: 200)') +parser.add_argument('--dropout-ratio', type=float, dest='dropout_ratio', default=0.5, + help='dropout ratio (default: 0.5)') +parser.add_argument('--graph-pooling', type=str, dest='graph_pooling', default="attention", + help='graph level pooling (sum, mean, max, attention)') +parser.add_argument('--gnn-type', type=str, dest='gnn_type', default="gatv2", + help='gnn type (gin, gcn, gat, graphsage)') + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=110, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.002, + help='learning rate (default: 0.002)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='micro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='mlce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + weight_dir = "./run/" + args.task_type + '-' + args.task + "-m-" + args.model + '-' + args.loss + str(args.batch_size) + str(args.lr) + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + raise ValueError + + logging.basicConfig(handlers=[ + # logging.FileHandler(filename=os.path.join(weight_dir, "eval.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + # with open(os.path.join(weight_dir, "config.json"), "w") as f: + # f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src) + else: + set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src) + else: + qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + # train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + + # train_loader = DataLoader(train_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=1, follow_batch=['x_s'], shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=1, follow_batch=['x_s'], shuffle=False) + + if args.model == 'mm': + model = MMGraph(num_layer=args.num_layer, input_dim=43, emb_dim=args.emb_dim, out_dim=set_all.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type=args.gnn_type, max_length=args.max_length) + + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + model.load_state_dict(torch.load(weights_path)) + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + + + if args.task_type in ('mlc', 'slc') : + _, ap, f1, acc, auc = train(args, None, model, None, valid_loader, device, None, None) + logging.info(f'ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, None, model, None, qlx_loader, device, None, None) + logging.info(f'QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, None, model, None, saap_loader, device, None, None) + logging.info(f'SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/eval.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/graphabl/loss.py b/graphabl/loss.py new file mode 100644 index 0000000000000000000000000000000000000000..d2c1cabdc7bb5df53b8513344372ea557b89aa29 --- /dev/null +++ b/graphabl/loss.py @@ -0,0 +1,123 @@ +import torch +from torch import nn +import torch.nn.functional as F +from math import cos, pi, sin +import math +import numpy as np +from scipy.special import lambertw + + +def mixup_criterion(criterion, pred, y_a, y_b, lam, pow=2): + y = lam ** pow * y_a + (1 - lam) ** pow * y_b + return criterion(pred, y) + + +def mixup_data(v, q, a): + '''Returns mixed inputs, pairs of targets, and lambda without organ constraint''' + lam = np.random.beta(1, 1) + + batch_size = v.shape[0] + index = torch.randperm(batch_size) + + mixed_v = lam * v + (1 - lam) * v[index, :] + mixed_q = lam * q + (1 - lam) * q[index, :] + + a_1, a_2 = a, a[index] + return mixed_v, mixed_q, a_1, a_2, lam + + +def linear(epoch, nepoch): + return 1 - epoch / nepoch + + +def convex(epoch, nepoch): + return epoch / (2 - nepoch) + + +def concave(epoch, nepoch): + return 1 - sin((epoch / nepoch) * (pi / 2)) + + +def composite(epoch, nepoch): + return 0.5 * cos((epoch / nepoch) * pi) + 0.5 + + +class LogCoshLoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y_t, y_prime_t): + ey_t = y_t - y_prime_t + return torch.mean(torch.log(torch.cosh(ey_t + 1e-12)))+F.mse_loss(y_t, y_prime_t) + + +class MLCE(nn.Module): + def __init__(self): + super(MLCE, self).__init__() + + def _mlcce(self, y_pred, y_true): + y_pred = (1 - 2 * y_true) * y_pred + y_pred_neg = y_pred - y_true * 1e12 + y_pred_pos = y_pred - (1 - y_true) * 1e12 + zeros = torch.zeros_like(y_pred[..., :1]) + y_pred_neg = torch.cat([y_pred_neg, zeros], dim=-1) + y_pred_pos = torch.cat([y_pred_pos, zeros], dim=-1) + neg_loss = torch.logsumexp(y_pred_neg, dim=-1) + pos_loss = torch.logsumexp(y_pred_pos, dim=-1) + loss = torch.mean(neg_loss + pos_loss) + return loss + + def __call__(self, y_pred, y_true): + return self._mlcce(y_pred, y_true) + + +class SuperLoss(nn.Module): + def __init__(self, C=10, lam=1, batch_size=128): + super(SuperLoss, self).__init__() + self.tau = math.log(C) + self.lam = lam # set to 1 for CIFAR10 and 0.25 for CIFAR100 + self.batch_size = batch_size + + def forward(self, logits, targets): + l_i = F.mse_loss(logits, targets, reduction='none').detach() + sigma = self.sigma(l_i) + loss = (F.mse_loss(logits, targets, reduction='none') - self.tau) * sigma + self.lam * ( + torch.log(sigma) ** 2) + loss = loss.sum() / self.batch_size + return loss + + def sigma(self, l_i): + x = torch.ones(l_i.size()) * (-2 / math.exp(1.)) + x = x.cuda() + y = 0.5 * torch.max(x, (l_i - self.tau) / self.lam) + y = y.cpu().numpy() + sigma = np.exp(-lambertw(y)) + sigma = sigma.real.astype(np.float32) + sigma = torch.from_numpy(sigma).cuda() + return sigma + + +def unbiased_curriculum_loss(out, data, args, epoch, epochs, scheduler='linear'): + losses = [] + scheduler = linear if scheduler == 'linear' else concave + + # calculate difficulty measurement function + adjusted_losses = [] + for idx in range(out.shape[0]): + ground_truth = max(1, abs(data[idx].item())) + loss = F.mse_loss(out[idx], data[idx]) + losses.append(loss) + adjusted_losses.append(loss.item() / ground_truth) + + mean_loss, std_loss = np.mean(adjusted_losses), np.std(adjusted_losses) + + # re-weight losses + total_loss = 0 + for i, loss in enumerate(losses): + if adjusted_losses[i] > mean_loss + 1 * std_loss: + schedule_factor = scheduler(epoch, args.epochs) + total_loss += schedule_factor * loss + else: + total_loss += loss + + return total_loss diff --git a/graphabl/main.py b/graphabl/main.py new file mode 100644 index 0000000000000000000000000000000000000000..66f81e82451b2d65b40ae8f9a55b8ae283b6ef01 --- /dev/null +++ b/graphabl/main.py @@ -0,0 +1,245 @@ +import argparse +import json +import logging +import os +import time + +from dataset import MDataset +from network import GraphGNN +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset +from torch_geometric.loader import DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") +parser.add_argument('--num-layer', type=int, dest='num_layer', default=2, + help='number of GNN message passing layers (default: 2)') +parser.add_argument('--emb-dim', type=int, dest='emb_dim', default=128, + help='embedding dimensions (default: 128)') +parser.add_argument('--dropout-ratio', type=float, dest='dropout_ratio', default=0.3, + help='dropout ratio (default: 0.3)') +parser.add_argument('--graph-pooling', type=str, dest='graph_pooling', default="attention", + help='graph level pooling (sum, mean, max, attention)') +parser.add_argument('--gnn-type', type=str, dest='gnn_type', default="gat", + help='gnn type (gin, gcn, gat, graphsage)') + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") +parser.add_argument('--exclude-feature', dest='exclude_feature', type=int, default=None) + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + if args.loss == 'mlce' and args.task_type != 'slc': + criterion = MLCE() + elif args.loss == "bce" or args.task_type == 'slc': + args.loss = "bce" + criterion = nn.BCEWithLogitsLoss() + else: + criterion = 0 + pass + weight_dir = "./run/" + str(args.exclude_feature) + '-' + args.loss + str(args.batch_size) + '-' + str(args.lr) + '-' + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + os.makedirs(weight_dir) + + logging.basicConfig(handlers=[ + logging.FileHandler(filename=os.path.join(weight_dir, "training.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + with open(os.path.join(weight_dir, "config.json"), "w") as f: + f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src, data_ver=args.data_ver, exclude_feature=args.exclude_feature) + # else: + # set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src, data_ver=args.data_ver) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src, exclude_feature=args.exclude_feature) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src, exclude_feature=args.exclude_feature) + # else: + # qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + # saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + # if args.loss == 'bce': + # weight = torch.Tensor([0] * args.classes) + # for i in train_set: + # weight += i[2] + # weight = (len(train_set) - weight) / weight + # criterion.register_buffer('pos_weight', weight.to(device)) + + train_loader = DataLoader(train_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + + if args.model == 'mm': + model = GraphGNN(num_layer=args.num_layer, input_dim=set_all.num_features, emb_dim=args.emb_dim, out_dim=set_all.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type=args.gnn_type) + + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + # optimizer = torch.optim.Adam(model.parameters()) + optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr) + # optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=True, weight_decay=5e-5) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Train set:{len(train_set)}, Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + best_metric = 0 + best_qlx = 0 + best_saap = 0 + start_time = time.time() + + for epoch in range(1, args.epochs + 1): + if args.task_type in ('mlc', 'slc') : + train_loss, ap, f1, acc, auc = train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer) + logging.info(f'Epoch: {epoch:03d}, Train Loss: {train_loss:.3f}, ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + avg_metric = ap + f1 + acc + auc + if avg_metric > best_metric: + logging.info(f'Epoch: {epoch:03d} New Best validation metrics, running test session') + torch.save(model.state_dict(), weights_path) + best_metric = avg_metric + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, epoch, model, None, qlx_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_metric = qlx_ap + qlx_f1 + qlx_acc + qlx_auc + if qlx_metric > best_qlx: + best_qlx = qlx_metric + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, epoch, model, None, saap_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_metric = saap_ap + saap_f1 + saap_acc + saap_auc + if saap_metric > best_saap: + best_saap = saap_metric + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'used time {(time.time()-start_time)/3600:.2f}h') + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/result.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/graphabl/network.py b/graphabl/network.py new file mode 100644 index 0000000000000000000000000000000000000000..65dbdc7bbdeb55e1031f4c397a3cd2e09c1b719b --- /dev/null +++ b/graphabl/network.py @@ -0,0 +1,367 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F +import numpy as np +from torch.nn import Sequential, Linear, ReLU +from torch_geometric.nn import GraphConv, GINConv, GATConv, SAGEConv, GPSConv, GINEConv, GATv2Conv +from torch_geometric.nn import global_mean_pool, global_add_pool, global_max_pool, GlobalAttention, Set2Set, MulAggregation +from torch_geometric.nn import aggr +from torch_geometric.nn import GraphNorm + + +import torch +import torch.nn as nn +import torch.nn.functional as F + +class SimpleSelfAttention(nn.Module): + def __init__(self, embedding_dim, num_heads=1): + super(SimpleSelfAttention, self).__init__() + self.embedding_dim = embedding_dim + self.num_heads = num_heads + + # Assuming key_channels = value_channels = embedding_dim + self.key_channels = self.embedding_dim + self.value_channels = self.embedding_dim + + # Linear layers for queries, keys, and values + self.query = nn.Linear(embedding_dim, self.key_channels * num_heads) + self.key = nn.Linear(embedding_dim, self.key_channels * num_heads) + self.value = nn.Linear(embedding_dim, self.value_channels * num_heads) + + # Output projection layer + self.proj = nn.Linear(self.value_channels * num_heads, embedding_dim) + + # Scaling for dot-product attention + self.scale = nn.Parameter(torch.sqrt(torch.FloatTensor([self.key_channels // num_heads]))) + + def forward(self, x1, x2, x3): + # x1, x2, x3 shapes: [Batch_size, Embedding_dim] + # Stack the inputs along a new dimension (sequence dimension) + batch_size = x1.shape[0] + x = torch.stack((x1, x2, x3), dim=1) # [Batch_size, 3, Embedding_dim] + + # Compute queries, keys, values for all three inputs + Q = self.query(x) # [Batch_size, 3, num_heads * embedding_dim] + K = self.key(x) # [Batch_size, 3, num_heads * embedding_dim] + V = self.value(x) # [Batch_size, 3, num_heads * embedding_dim] + + # Reshape for multi-head attention + Q = Q.view(batch_size, -1, self.num_heads, self.embedding_dim).transpose(1, 2) # [Batch_size, num_heads, 3, embedding_dim] + K = K.view(batch_size, -1, self.num_heads, self.embedding_dim).transpose(1, 2) # [Batch_size, num_heads, 3, embedding_dim] + V = V.view(batch_size, -1, self.num_heads, self.embedding_dim).transpose(1, 2) # [Batch_size, num_heads, 3, embedding_dim] + + # Calculate dot product attention + attention_scores = torch.matmul(Q, K.transpose(-2, -1)) / self.scale + attention = F.softmax(attention_scores, dim=-1) + + # Apply attention to V + x = torch.matmul(attention, V) # [Batch_size, num_heads, 3, embedding_dim] + + # Concatenate heads and put through final linear layer + x = x.transpose(1, 2).reshape(batch_size, -1, self.num_heads * self.embedding_dim) + x = self.proj(x) # [Batch_size, 3, embedding_dim] + + # Sum the outputs from the three inputs + out = x.sum(dim=1) # [Batch_size, embedding_dim] + return out + +def cosine_similarity(x,y): + num = x.dot(y.T) + denom = np.linalg.norm(x) * np.linalg.norm(y) + return num / denom + +class LogCoshLoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y_t, y_prime_t): + ey_t = y_t - y_prime_t + return torch.mean(torch.log(torch.cosh(ey_t + 1e-12))) + + +class WeightedMSELoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y, y_t, weights=None): + loss = (y - y_t) ** 2 + if weights is not None: + loss *= weights.expand_as(loss) + return torch.mean(loss) + + +class GNN(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, JK="last", drop_ratio=0, gnn_type="gin"): + super(GNN, self).__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + # self.fc2 = nn.Linear(200, 200) + self.gnns = torch.nn.ModuleList() + for layer in range(num_layer): + in_dim = input_dim if layer == 0 else emb_dim + if gnn_type == "gin": + # self.gnns.append(GINConv(nn.Sequential(nn.Linear(in_dim, emb_dim), nn.BatchNorm1d(emb_dim), nn.ReLU(), + # nn.Linear(emb_dim, emb_dim)))) + self.gnns.append(GINConv(nn.Sequential(nn.Linear(in_dim, emb_dim), GraphNorm(emb_dim), nn.ReLU(), + nn.Linear(emb_dim, emb_dim), nn.ReLU()))) + elif gnn_type == "gps": + nn_ = Sequential( + Linear(in_dim, emb_dim), + ReLU(), + Linear(emb_dim, emb_dim), + ) + conv = GPSConv(emb_dim, GINEConv(nn_), heads=4) + self.gnns.append(conv) + elif gnn_type == "gcn": + self.gnns.append(GraphConv(in_dim, emb_dim)) + elif gnn_type == "gat": + self.gnns.append(GATConv(in_dim, emb_dim)) + elif gnn_type == "gatv2": + self.gnns.append(GATv2Conv(in_dim, emb_dim)) + elif gnn_type == "graphsage": + self.gnns.append(SAGEConv(in_dim, emb_dim)) + else: + raise ValueError("Invalid GNN type.") + + def forward(self, x, edge_index, edge_attr=None): + h_list = [x] + mut_site = [] + for layer in range(self.num_layer): + + h = self.gnns[layer](h_list[layer], edge_index, edge_attr) + # if layer == self.num_layer - 1: + # # remove relu from the last layer + # h = F.dropout(h, self.drop_ratio, training=self.training) + # else: + # h = F.dropout(F.relu(h), self.drop_ratio, training=self.training) # F.relu() + h_list.append(h) + # if len(h_list) == 2: + # previous_mut_site_feature = h_list[-2][mut_res_idx] + # current_mut_site_feature = h_list[-1][mut_res_idx] + # # print(previous_mut_site_feature.shape, current_mut_site_feature.shape) + # h_feature = self.global_encoder(previous_mut_site_feature) + # h_list[-1][mut_res_idx] = h_feature + current_mut_site_feature + # if len(h_list) == 3: + # previous_mut_site_feature = h_list[-2][mut_res_idx].squeeze(0) + # current_mut_site_feature = h_list[-1][mut_res_idx].squeeze(0) + + # h_feature = self.fc2(previous_mut_site_feature) + current_mut_site_feature + # h_list[-1][mut_res_idx] = h_feature.unsqueeze(0) + # mut_site.append() + # print(len(h_list)) + if self.JK == "last": + node_representation = h_list[-1] + elif self.JK == "sum": + h_list = [h.unsqueeze_(0) for h in h_list] + node_representation = torch.sum(torch.cat(h_list[1:], dim=0), dim=0) + # print('node_rep', node_representation.shape) + return h_list[-1] + + +# orthogonal initialization +def init_gru_orth(model, gain=1): + model.reset_parameters() + # orthogonal initialization of gru weights + for _, hh, _, _ in model.all_weights: + for i in range(0, hh.size(0), model.hidden_size): + torch.nn.init.orthogonal_(hh[i:i + model.hidden_size], gain=gain) + + +def init_lstm_orth(model, gain=1): + init_gru_orth(model, gain) + + # positive forget gate bias (Jozefowicz es at. 2015) + for _, _, ih_b, hh_b in model.all_weights: + l = len(ih_b) + ih_b[l // 4: l // 2].data.fill_(1.0) + hh_b[l // 4: l // 2].data.fill_(1.0) + + +class GraphGNN(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, out_dim, JK="last", drop_ratio=0.5, graph_pooling="attention", + gnn_type="gat", concat_type=None, fds=False, feature_level='both', contrast_curri=False) -> object: + super(GraphGNN, self).__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + self.input_dim = input_dim + self.emb_dim = emb_dim + self.out_dim = out_dim + self.concat_type = concat_type + self.feature_level = feature_level + self.contrast_curri = contrast_curri + + self.fc = nn.Sequential( + nn.Linear(self.emb_dim, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), nn.Linear(self.emb_dim, self.out_dim)) + + self.gnn = GNN(num_layer, input_dim, emb_dim, JK, drop_ratio, gnn_type=gnn_type) + + if graph_pooling == "sum": + self.pool = global_add_pool + elif graph_pooling == "mean": + self.pool = global_mean_pool + elif graph_pooling == "max": + self.pool = global_max_pool + elif graph_pooling == "mul": + self.pool = MulAggregation() + elif graph_pooling == "attention": + self.pool = GlobalAttention(gate_nn=torch.nn.Linear(emb_dim, 1)) + elif graph_pooling == "set2set": + self.pool = Set2Set(emb_dim, processing_steps=2) + elif graph_pooling == "lstm": + self.pool = aggr.LSTMAggregation(emb_dim, emb_dim) + else: + raise ValueError("Invalid graph pooling type.") + + def forward_once(self, x, edge_index, batch): + node_representation = self.gnn(x, edge_index) + graph_rep = self.pool(node_representation, batch) + return graph_rep + + def forward(self, data): + graph_rep_be = self.forward_once(data.x_s, data.edge_index_s, data.x_s_batch) + x = self.fc(graph_rep_be) + return x + + + +class MMGraph(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, out_dim, JK="last", drop_ratio=0.5, graph_pooling="attention", + gnn_type="gat", concat_type=None, fds=False, feature_level='both', contrast_curri=False, max_length=50) -> object: + super(MMGraph, self).__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + self.input_dim = input_dim + self.emb_dim = emb_dim + self.out_dim = out_dim + self.concat_type = concat_type + self.feature_level = feature_level + self.contrast_curri = contrast_curri + + self.graph_pool = nn.Linear(self.emb_dim, 1) + + self.fc = nn.Sequential( + nn.Linear(self.emb_dim, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + nn.Linear(self.emb_dim, self.out_dim)) + + if fds: + self.dir = True + else: + self.dir = False + self.global_encoder = nn.Sequential(nn.Linear(10, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio),) + self.seq_encoder = nn.Sequential( + nn.Linear(max_length, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + ) + self.gnn = GNN(num_layer, input_dim, emb_dim, JK, drop_ratio, gnn_type=gnn_type) + + if graph_pooling == "sum": + self.pool = global_add_pool + elif graph_pooling == "mean": + self.pool = global_mean_pool + elif graph_pooling == "max": + self.pool = global_max_pool + elif graph_pooling == "mul": + self.pool = MulAggregation() + elif graph_pooling == "attention": + self.pool = GlobalAttention(gate_nn=torch.nn.Linear(emb_dim, 1)) + elif graph_pooling == "set2set": + self.pool = Set2Set(emb_dim, processing_steps=2) + elif graph_pooling == "lstm": + self.pool = aggr.LSTMAggregation(emb_dim, emb_dim) + else: + raise ValueError("Invalid graph pooling type.") + self.att = SimpleSelfAttention(emb_dim, num_heads=4) + + def forward_once(self, x, edge_index, batch): + node_representation = self.gnn(x, edge_index) + graph_rep = self.pool(node_representation, batch) + return graph_rep + + def forward(self, data): + seq1, global_1 = data.seq, data.global_f + device = self.graph_pool.bias.device + seq1 = torch.tensor(seq1, dtype=torch.float, device=device) + global_1 = torch.tensor(global_1, dtype=torch.float, device=device) + + graph_rep_be = self.forward_once(data.x_s, data.edge_index_s, data.x_s_batch) + seq1_rep_be = self.seq_encoder(seq1) + global1 = self.global_encoder(global_1) + + a1 = self.att(graph_rep_be, seq1_rep_be, global1) + return self.fc(a1) + + + +class PMMGraph(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, out_dim, JK="last", drop_ratio=0.5, graph_pooling="attention", + gnn_type="gat", concat_type=None, fds=False, feature_level='both', contrast_curri=False): + super(PMMGraph, self).__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + self.input_dim = input_dim + self.emb_dim = emb_dim + self.out_dim = out_dim + self.concat_type = concat_type + self.feature_level = feature_level + self.contrast_curri = contrast_curri + + # Define the learnable prompt token + self.prompt_token = nn.Parameter(torch.randn(1, 10)) + + self.graph_pool = nn.Linear(self.emb_dim, 1) + + self.fc = nn.Sequential( + nn.Linear(self.emb_dim + 10, self.emb_dim), # Adjust input size to include the prompt token + nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + nn.Linear(self.emb_dim, self.out_dim)) + + self.dir = fds + self.global_encoder = nn.Sequential(nn.Linear(10, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio)) + self.seq_encoder = nn.Sequential( + nn.Linear(30, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + ) + self.gnn = GNN(num_layer, input_dim, emb_dim, JK, drop_ratio, gnn_type=gnn_type) + + # Initialize pooling based on the specified type + if graph_pooling in ["sum", "mean", "max", "mul", "attention", "set2set", "lstm"]: + pooling_classes = { + "sum": global_add_pool, + "mean": global_mean_pool, + "max": global_max_pool, + "mul": aggr.MulAggregation(), + "attention": GlobalAttention(gate_nn=torch.nn.Linear(emb_dim, 1)), + "set2set": Set2Set(emb_dim, processing_steps=2), + "lstm": aggr.LSTMAggregation(emb_dim, emb_dim) + } + self.pool = pooling_classes[graph_pooling] + else: + raise ValueError("Invalid graph pooling type.") + + self.att = SimpleSelfAttention(emb_dim + 10, num_heads=4) # Adjust for prompt dimension + + def forward_once(self, x, edge_index, batch): + node_representation = self.gnn(x, edge_index) + graph_rep = self.pool(node_representation, batch) + # Concatenate the prompt token + graph_rep = torch.cat([graph_rep, self.prompt_token.expand(graph_rep.size(0), -1)], dim=1) + return graph_rep + + def forward(self, data): + seq1 = torch.tensor(np.array(data.seq, dtype=np.float32)).to(device='cuda') + global_1 = torch.tensor(np.array(data.global_f, dtype=np.float32)).to(device='cuda') + + graph_rep_be = self.forward_once(data.x_s, data.edge_index_s, data.x_s_batch) + seq1_rep_be = self.seq_encoder(seq1) + global1_rep = self.global_encoder(global_1) + # Concatenate the prompt token to other representations as well + seq1_rep_be = torch.cat([seq1_rep_be, self.prompt_token.expand(seq1_rep_be.size(0), -1)], dim=1) + global1_rep = torch.cat([global1_rep, self.prompt_token.expand(global1_rep.size(0), -1)], dim=1) + + # Process combined representations + a1 = self.att(graph_rep_be, seq1_rep_be, global1_rep) + return self.fc(a1) + + diff --git a/graphabl/run/0-bce256-0.001-50af/config.json b/graphabl/run/0-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..6b10990197be152914b6669b840c310d0e4873ec --- /dev/null +++ b/graphabl/run/0-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "mm", "fusion": "1", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gat", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "exclude_feature": 0, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/graphabl/run/0-bce256-0.001-50af/model_1.pth b/graphabl/run/0-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..0a41471bbf0add7ab05ab0010dc6e27c874c362f --- /dev/null +++ b/graphabl/run/0-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dad7f2bb88a59092b90fca5321988f9238c36346ea1be00f08c636e87f5fa6ce +size 154796 diff --git a/graphabl/run/0-bce256-0.001-50af/model_2.pth b/graphabl/run/0-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..02acdcb8c8f67d5e0598fe75a493cae8bb6df592 --- /dev/null +++ b/graphabl/run/0-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:740b0e562b26bd5f080c6f0f02f123c75b34a8ce745daba51fbb9a99c3023a4f +size 154796 diff --git a/graphabl/run/0-bce256-0.001-50af/model_3.pth b/graphabl/run/0-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..a2ce9e2a317b8b1686bc15cb12dc85cc1afd6f23 --- /dev/null +++ b/graphabl/run/0-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:848cc853139d38d15b4d84d171d75619ff1dada05448445d22e4de77e6de6d7b +size 154796 diff --git a/graphabl/run/0-bce256-0.001-50af/model_4.pth b/graphabl/run/0-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..2a719dc44344cd0c6775fda581ff159e901396ff --- /dev/null +++ b/graphabl/run/0-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:985f68a84bb7262181f3c13c9fe2bccf093dc2b09433b102bb7535c228e0a98e +size 154796 diff --git a/graphabl/run/0-bce256-0.001-50af/model_5.pth b/graphabl/run/0-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..2763c89b02325b3f50ff9a10ecc724e103b062cf --- /dev/null +++ b/graphabl/run/0-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f36416aeba32de0bf3ac631298a20089fb8e1cce4828ba7efa7d47337aaea425 +size 154796 diff --git a/graphabl/run/0-bce256-0.001-50af/result.txt b/graphabl/run/0-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..a123b15f49703d8c9173d94653d405ab5229b07b --- /dev/null +++ b/graphabl/run/0-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8311869859695434,0.7992442131042481,0.7261005401611328,0.7632609367370605 +0.01089897049504037,0.006706284424244486,0.008409664915215989,0.009405898938088679 +qlx +0.6402872443199158,0.708274245262146,0.5708609342575073,0.6535321354866028 +0.012465302588486486,0.003057944969321764,0.006488706331848968,0.008808348470467832 +saap +0.599161159992218,0.5113322138786316,0.40466665625572207,0.6856127262115479 +0.027291274786404338,0.0011573248233569527,0.00985450937591801,0.027237991254309084 diff --git a/graphabl/run/1-bce256-0.001-50af/config.json b/graphabl/run/1-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..b4669d5bea4afcc9d83aa2b77c30cfb2d5d78c5d --- /dev/null +++ b/graphabl/run/1-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "mm", "fusion": "1", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gat", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "exclude_feature": 1, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/graphabl/run/1-bce256-0.001-50af/model_1.pth b/graphabl/run/1-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..1268239e9012131c24b8805b05f6ec7ce368cf50 --- /dev/null +++ b/graphabl/run/1-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:983cb35e257c0cd54583a802d4be899b066d3aac69916879d8521fab756f7011 +size 154796 diff --git a/graphabl/run/1-bce256-0.001-50af/model_2.pth b/graphabl/run/1-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..0844953b194b2465604f0b1df15663066693ea62 --- /dev/null +++ b/graphabl/run/1-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c4e0885e5114ab118f98dc452f691753a1f1570c1bf11eff43b7605391d77ed5 +size 154796 diff --git a/graphabl/run/1-bce256-0.001-50af/model_3.pth b/graphabl/run/1-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..6f5c043e4b5539cfaf9f7b809ce49f44b7f7012d --- /dev/null +++ b/graphabl/run/1-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c6314642ce4a6f7b63adeeb1b4c7e985add18565cb7648afc172c7044d57a8bc +size 154796 diff --git a/graphabl/run/1-bce256-0.001-50af/model_4.pth b/graphabl/run/1-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..be6e048ab97c80c8497a39878c6f96f1c3e27665 --- /dev/null +++ b/graphabl/run/1-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a26e91e9415a4ca7dadb1372331f7f79258b64bdcd4d7a1d62ef94fc21c50f04 +size 154796 diff --git a/graphabl/run/1-bce256-0.001-50af/model_5.pth b/graphabl/run/1-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..04eed5c48cfb847bc90fd914ab5361177d020e2b --- /dev/null +++ b/graphabl/run/1-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7331bbf91e11dd08e8409ef0cf5064be381bbcfb7a611529afb65a04c08c7a6e +size 154796 diff --git a/graphabl/run/1-bce256-0.001-50af/result.txt b/graphabl/run/1-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..7156d32e7988dcbf4395cd22883352a30accb135 --- /dev/null +++ b/graphabl/run/1-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8527601003646851,0.8259660601615906,0.7641097426414489,0.7995261788368225 +0.0059770813086121736,0.005005334075867127,0.0075221584213969106,0.00905588109790848 +qlx +0.8474146246910095,0.7568915486335754,0.6677704215049743,0.882286548614502 +0.006088750075017117,0.005074444981365584,0.009391623656924189,0.00546507473652238 +saap +0.5873079895973206,0.5063873946666717,0.46200000047683715,0.6355118036270142 +0.01626200352905642,0.014330853382268484,0.03196179334254133,0.01651862039748566 diff --git a/graphabl/run/2-bce256-0.001-50af/config.json b/graphabl/run/2-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..39d15cfcd4c138b9192688282ff3e2c43ce249cf --- /dev/null +++ b/graphabl/run/2-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "mm", "fusion": "1", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gat", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "exclude_feature": 2, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/graphabl/run/2-bce256-0.001-50af/model_1.pth b/graphabl/run/2-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..75730d7edc72eb2ecd6417b9a9b8ddb19ec97dfb --- /dev/null +++ b/graphabl/run/2-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ae523c84d47ceb99f0d6a262f66d165b2611348e079a93cbc0fea6561fba589b +size 163500 diff --git a/graphabl/run/2-bce256-0.001-50af/model_2.pth b/graphabl/run/2-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..ed87ee7e7c864c7a20410c857685afe00f6e6c62 --- /dev/null +++ b/graphabl/run/2-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:365f99c2d2553b2913ee8919da1bd8143619680a074ab9bfac16c2af9aabdeeb +size 163500 diff --git a/graphabl/run/2-bce256-0.001-50af/model_3.pth b/graphabl/run/2-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..1e220478617eb8177403dc83730ec5f5a1e2f211 --- /dev/null +++ b/graphabl/run/2-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:de1fea2b6cba7c3ec405f94e0a01b62da2aa55677e81b88e6d6c94804c0d8fc3 +size 163500 diff --git a/graphabl/run/2-bce256-0.001-50af/model_4.pth b/graphabl/run/2-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..6dd390cfab285b601af7630cf07748f67ade0870 --- /dev/null +++ b/graphabl/run/2-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:617a27e7d4a36795a9d53e0d55f57d21ff41e94fc2d7c4453471b6e0e2fcd201 +size 163500 diff --git a/graphabl/run/2-bce256-0.001-50af/model_5.pth b/graphabl/run/2-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..8756e8a32465f242ab6d20cf916c346d43eb6aa8 --- /dev/null +++ b/graphabl/run/2-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ba996cfae42f644da78337dcea5c716f549244f3370c6f85257899c8eade4bad +size 163500 diff --git a/graphabl/run/2-bce256-0.001-50af/result.txt b/graphabl/run/2-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..56183ef98d858b4c2799c03149f833e8cecd3000 --- /dev/null +++ b/graphabl/run/2-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8713407397270203,0.8358925104141235,0.7774726510047912,0.8197148442268372 +0.009872133162932156,0.008417747308414294,0.010920918060023394,0.012654893910484355 +qlx +0.808427631855011,0.7580655455589295,0.6690949201583862,0.8328594923019409 +0.01449267758309804,0.03268245468668244,0.061416284002013204,0.019205095683731136 +saap +0.5958592414855957,0.5154798030853271,0.43899999260902406,0.6548543810844422 +0.026156405618067657,0.007699140763235296,0.040573662765624754,0.02752765483558861 diff --git a/graphabl/summary.py b/graphabl/summary.py new file mode 100644 index 0000000000000000000000000000000000000000..420cc50d5a8330d07342a07c2f5bfc482023480a --- /dev/null +++ b/graphabl/summary.py @@ -0,0 +1,59 @@ +import os +import csv +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument('--dir', type=str, default='./run') +parser.add_argument('--output', type=str, default='summary.csv') +args = parser.parse_args() + +# 设置目录路径和输出CSV文件名 +directory_path = args.dir +output_csv = args.output + +# 创建CSV文件并写入数据 +with open(output_csv, mode='w', newline='') as file: + writer = csv.writer(file) + + # 写入第一个子表的标题和表头 + writer.writerow(['QLX']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 6: + means_5_6 = lines[4].strip().split(',') + std_devs_5_6 = lines[5].strip().split(',') + if len(means_5_6) == 4 and len(std_devs_5_6) == 4: + values_5_6 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_5_6, std_devs_5_6)] + writer.writerow([subdirectory] + values_5_6) + + # 加入空行以分隔两个子表 + writer.writerow([]) + + # 写入第二个子表的标题和表头 + writer.writerow(['SAAP']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 再次遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 9: + means_8_9 = lines[7].strip().split(',') + std_devs_8_9 = lines[8].strip().split(',') + if len(means_8_9) == 4 and len(std_devs_8_9) == 4: + values_8_9 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_8_9, std_devs_8_9)] + writer.writerow([subdirectory] + values_8_9) \ No newline at end of file diff --git a/graphabl/train.py b/graphabl/train.py new file mode 100644 index 0000000000000000000000000000000000000000..de2a4067e435ff9c1ead1fe0d33496f726d6c3ff --- /dev/null +++ b/graphabl/train.py @@ -0,0 +1,56 @@ +import torch +from torchmetrics import F1Score, Accuracy, AveragePrecision, AUROC + + +def train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer): + train_loss = 0 + num_labels = model.out_dim + avg = args.metric_avg + if num_labels == 1: + task = 'binary' + else: + task = 'multilabel' + metric_macro_acc = Accuracy(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_f1 = F1Score(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_ap = AveragePrecision(average=avg, task=task, num_labels=num_labels).to(device) + metric_auc = AUROC(average=avg, task=task, num_labels=num_labels).to(device) + + if train_loader is not None: + model.train() + for data in train_loader: + data = data.to(device) + # print(seq_lengths) + out = model(data) + # print(out[0]) + # print(gt[0]) + loss = criterion(out, torch.tensor(data.gt, dtype=torch.float, device=device)) + # loss_0 = criterion(out[0], gt.to(device).float()) + # loss_1 = criterion(out[1], gt.to(device).float()) + # loss_2 = criterion(out[2], gt.to(device).float()) + # loss = loss_0 + loss_1 + loss_2 + loss.backward() + optimizer.step() + optimizer.zero_grad() + train_loss += loss.item() + + model.eval() + preds = [] + gt_list_valid = [] + with torch.no_grad(): + for data in valid_loader: + data = data.to(device) + gt_list_valid.append(torch.tensor(data.gt, device=device)) + out = model(data) + preds.append(out) + + # calculate metrics + preds = torch.nn.functional.sigmoid(torch.cat(preds, dim=0)) + gt_list_valid = torch.cat(gt_list_valid, dim=0).int() + + macro_ap = metric_macro_ap(preds, gt_list_valid).item() + # class_ap = [round(i.item(), 5) for i in metric_class_ap(preds, gt_list_valid)] + macro_auc = metric_auc(preds, gt_list_valid).item() + macro_f1 = metric_macro_f1(preds, gt_list_valid).item() + macro_acc = metric_macro_acc(preds, gt_list_valid).item() + return train_loss, macro_ap, macro_f1, macro_acc, macro_auc + diff --git a/graphabl/utils.py b/graphabl/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..7fd5de2ae11e3a64de85ddc8c0ce0f777a738a48 --- /dev/null +++ b/graphabl/utils.py @@ -0,0 +1,32 @@ +import random +import torch +import numpy as np + + +def set_seed(seed): + random.seed(seed) + np.random.seed(seed) + torch.manual_seed(seed) + torch.cuda.manual_seed(seed) + torch.cuda.manual_seed_all(seed) + torch.backends.cudnn.deterministic = True + torch.backends.cudnn.benchmark = False + + +def load_pretrain_model(net, weights): + net_keys = list(net.state_dict().keys()) + weights_keys = list(weights.keys()) + # assert(len(net_keys) <= len(weights_keys)) + i = 0 + j = 0 + while i < len(net_keys) and j < len(weights_keys): + name_i = net_keys[i] + name_j = weights_keys[j] + if net.state_dict()[name_i].shape == weights[name_j].shape: + net.state_dict()[name_i].copy_(weights[name_j].cpu()) + i += 1 + j += 1 + else: + i += 1 + # print i, len(net_keys), j, len(weights_keys) + return net diff --git a/pics/benchmark.png b/pics/benchmark.png new file mode 100644 index 0000000000000000000000000000000000000000..333d2924cb1509917a25ce85d1396609fd63b17f --- /dev/null +++ b/pics/benchmark.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:deed205e26fca89247ae4a68c3fb9ff3d4a41bc43c7af8d4deed96ec34a68a65 +size 110525 diff --git a/t3.1/dataset.py b/t3.1/dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..f8bb46d260b22523d5ca4c5d38465d5393bb016b --- /dev/null +++ b/t3.1/dataset.py @@ -0,0 +1,260 @@ +import os +import torch +from Bio.PDB import PDBParser, is_aa +from Bio.PDB.Polypeptide import three_to_one +from torch.utils.data import Dataset +import pandas as pd +import numpy as np +from tqdm import tqdm +from Bio.Align import PairwiseAligner + + +aligner = PairwiseAligner() +# Define the scoring system +aligner.mode = 'global' # Global alignment +aligner.match_score = 1 +aligner.mismatch_score = -1 +aligner.open_gap_score = -0.5 +aligner.extend_gap_score = -0.1 + + +def calculate_similarity(seq_gen, seq_tem): + + # Perform the alignment + score = aligner.score(seq_gen, seq_tem) + return score / len(seq_gen) + + +def parse_ll37_testset(path='metadata/LL37_v0.csv'): + files = open(path, 'r').readlines() + seqs = [] + for file in files: + seqs.append(str(file.strip().split(',')[-1].strip().upper())) + return seqs + + +def gmean(labels): + # Assuming 'labels' is a NumPy array of shape (n, m) + # Calculate the geometric mean across axis 1 + + # Take the product of elements along axis=1 + product = np.prod(labels, axis=1) + + # nth root of product, where n is the number of elements along axis=1 + geometric_means = product ** (1.0 / labels.shape[1]) + return geometric_means + + +def clamp(n, smallest, largest): + return sorted([smallest, n, largest])[1] + + +ATOMS = {'H': 1, 'C': 12, 'N': 14, 'O': 16, 'S': 30} +ATOMS_R = {'H': 1, 'C': 1.5, 'N': 1.5, 'O': 1.5, 'S': 2} +AMINO_ACID_WATER = {'A': 255, 'V': 255, 'P': 255, 'F': 255, 'W': 255, 'I': 255, 'L': 255, 'G': 155, 'M': 155, + 'Y': 55, 'S': 55, 'T': 55, 'C': 55, 'N': 55, 'Q': 55, 'D': 55, 'E': 55, 'K': 55, 'R': 55, 'H': 55} +AMINO_ACID_CHARGE = {'D': 55, 'E': 55, 'A': 155, 'V': 155, 'P': 155, 'F': 155, 'W': 155, 'I': 155, 'L': 155, 'G': 155, + 'M': 155, 'Y': 155, 'S': 155, 'T': 155, 'C': 155, 'N': 155, 'Q': 155, 'K': 255, 'R': 255, 'H': 255} +# AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, +# 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19} +AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, + 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19, 'X': 21} + + +def pdb_parser(structure): + """ + + """ + voxel = np.zeros((4, 64, 64, 64), dtype=np.int8) + id = '' + seq_str = '' + for i in structure[0]: + id = i.id + chain = structure[0][id] + for res in chain: + if is_aa(res.get_resname(), standard=True): + resname = res.get_resname() + amino = three_to_one(resname) + seq_str += str(amino) + ATOM_WATER = AMINO_ACID_WATER[amino] + ATOM_CHARGE = AMINO_ACID_CHARGE[amino] + ATOM_CATEGORY = AMAs[amino] * 20 + + for i in res: + if i.id not in ATOMS.keys(): + continue + x, y, z = i.get_coord() + if abs(x) > 32: + x = clamp(x, -31, 31) + if abs(y) > 32: + y = clamp(x, -31, 31) + if abs(z) > 32: + z = clamp(x, -31, 31) + x_i, y_i, z_i = int(x) + 32, int(y) + 32, int(z) + 32 + ATOM_WEIGHT = ATOMS[i.id] + ATOM_R = ATOMS_R[i.id] + + if ATOM_R <= 1.5: + voxel[0, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WEIGHT + voxel[1, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WATER + voxel[2, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CHARGE + voxel[3, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CATEGORY + else: + voxel[0, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WEIGHT + voxel[1, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WATER + voxel[2, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CHARGE + voxel[3, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CATEGORY + return voxel + + +class MDataset(Dataset): + def __init__(self, threshold=32, mode='train', max_length=50, pdb_src='af', data_ver='0922'): + self.num_classes = 6 + exclude_list = pd.read_csv('../metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() + exclude_filter = False + + p = PDBParser(QUIET=True) + + if mode == 'train': + all_data = pd.read_csv(f'../metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values + exclude_filter = True + elif mode == 'qlx': + all_data = pd.read_csv('../metadata/data_qlx.csv', encoding="unicode_escape").values + elif mode == 'saap': + all_data = pd.read_csv('../metadata/data_saap.csv', encoding="unicode_escape").values + else: + raise NotImplementedError + idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) + labels = (labels < threshold).astype(int) + + filter_idx_list = [] + seq_new_list = [] + label_list = [] + for idx in range(len(idx_list)): + seq = seq_list[idx].upper().strip() + if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: + continue + if exclude_filter: + if seq in exclude_list: + continue + + filter_idx_list.append(idx) + seq_new_list.append(seq) + label_list.append(labels[idx]) + + # if pdb_src == 'af': + # pdb_root = './pdb/pdb_af/' + # elif pdb_src == 'hf': + # pdb_root = './pdb/pdb_dbassp/' + # else: + # raise NotImplementedError + self.data_list = [] + for i in tqdm(range(len(filter_idx_list))): + idx = filter_idx_list[i] + seq = seq_new_list[i] + label = label_list[i] + + # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"): + # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb" + # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"): + # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb" + # if os.path.exists(pdb_root + seq + ".pdb"): + # pdb_path = pdb_root + seq + ".pdb" + # else: + # print(f'lacking pdb file {seq}') + # continue + # # raise FileNotFoundError + + # structure = p.get_structure(idx, pdb_path) + # voxel = pdb_parser(structure) + seq_emb = [AMAs[char] for char in seq] + [0] * (max_length - len(seq)) + self.data_list.append((0, seq_emb, label)) + + def __getitem__(self, idx): + voxel, seq_emb, gt = self.data_list[idx] + + return torch.Tensor(voxel).float(), torch.Tensor(seq_emb), torch.Tensor(gt) + + def __len__(self): + return len(self.data_list) + + +# class SDataset(Dataset): +# ''' +# x: Features. +# y: Targets, if none, do prediction. +# ''' + +# def __init__(self, threshold=32, mode='train', task='0', max_length=50, pdb_src='af'): +# self.num_classes = 1 +# self.task = int(task) +# exclude_list = pd.read_csv('metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() +# exclude_filter = False + +# p = PDBParser(QUIET=True) + +# if mode == 'train': +# all_data = pd.read_csv('metadata/data_0922_i.csv', encoding="unicode_escape").values +# exclude_filter = True +# elif mode == 'qlx': +# all_data = pd.read_csv('metadata/data_qlx.csv', encoding="unicode_escape").values +# elif mode == 'saap': +# all_data = pd.read_csv('metadata/data_saap.csv', encoding="unicode_escape").values +# else: +# raise NotImplementedError +# idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) +# labels = (labels[:, self.task:self.task+1] <= threshold).astype(int) + +# filter_idx_list = [] +# seq_new_list = [] +# label_list = [] +# for idx in range(len(idx_list)): +# seq = seq_list[idx].upper().strip() +# if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: +# continue +# if exclude_filter: +# if seq in exclude_list: +# continue + +# filter_idx_list.append(idx) +# seq_new_list.append(seq) +# label_list.append(labels[idx]) + +# if pdb_src == 'af': +# pdb_root = './pdb/pdb_af/' +# elif pdb_src == 'hf': +# pdb_root = './pdb/pdb_dbassp/' +# else: +# raise NotImplementedError +# self.data_list = [] +# for i in tqdm(range(len(filter_idx_list))): +# idx = filter_idx_list[i] +# seq = seq_new_list[i] +# label = label_list[i] + +# # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"): +# # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb" +# # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"): +# # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb" +# if os.path.exists(pdb_root + seq + ".pdb"): +# pdb_path = pdb_root + seq + ".pdb" +# else: +# continue +# # raise FileNotFoundError + +# structure = p.get_structure(idx, pdb_path) +# voxel = pdb_parser(structure) +# seq_emb = [AMAs[char] for char in seq] + [0] * (max_length - len(seq)) +# self.data_list.append((voxel, seq_emb, label)) + +# def __getitem__(self, idx): +# voxel, seq_emb, gt = self.data_list[idx] + +# return torch.Tensor(voxel).float(), torch.Tensor(seq_emb), torch.Tensor(gt) + +# def __len__(self): +# return len(self.data_list) \ No newline at end of file diff --git a/t3.1/eval.py b/t3.1/eval.py new file mode 100644 index 0000000000000000000000000000000000000000..18f38bfff4c7b7f9bf2fef9834b96c07a96d947d --- /dev/null +++ b/t3.1/eval.py @@ -0,0 +1,203 @@ +import argparse +import json +import logging +import os +import time + +from dataset import SDataset, MDataset +from network import MMPeptide, SEQPeptide, VoxPeptide, MMFPeptide, SMPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=64, + help="MIC threshold for determine labels (default: 64)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=128, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.02, + help='learning rate (default: 0.002)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='micro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='mlce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + weight_dir = "./run/" + args.task_type + '-' + args.task + "-m-" + args.model + '-' + args.loss + str(args.batch_size) + str(args.lr) + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + raise ValueError + + logging.basicConfig(handlers=[ + # logging.FileHandler(filename=os.path.join(weight_dir, "eval.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + # with open(os.path.join(weight_dir, "config.json"), "w") as f: + # f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src) + else: + set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src) + else: + qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + # train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + + # train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=1, shuffle=False) + if args.model == 'seq': + model = SEQPeptide(classes=set_all.num_classes, q_encoder='mlp') + elif args.model == 'voxel': + model = VoxPeptide(classes=set_all.num_classes) + elif args.model == 'mm': + # model = MMPeptide(classes=train_set.num_classes, q_encoder='tf', ) # attention='hamburger' + # model = SMPeptide(classes=train_set.num_classes, q_encoder='mlp', max_length=30) # attention='hamburger' + model = MMPeptide(classes=set_all.num_classes, q_encoder='mlp', max_length=args.max_length)# , attention='hamburger' + elif args.model == 'mmf': + model = MMFPeptide(classes=set_all.num_classes, q_encoder='mlp', max_length=args.max_length) # attention='hamburger' + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + model.load_state_dict(torch.load(weights_path)) + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + + + if args.task_type in ('mlc', 'slc') : + _, ap, f1, acc, auc = train(args, None, model, None, valid_loader, device, None, None) + logging.info(f'ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, None, model, None, qlx_loader, device, None, None) + logging.info(f'QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, None, model, None, saap_loader, device, None, None) + logging.info(f'SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/eval.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/t3.1/gen_simi.py b/t3.1/gen_simi.py new file mode 100644 index 0000000000000000000000000000000000000000..c350b6cbccbe8e4884ea3bbbfd150eb1a66b348b --- /dev/null +++ b/t3.1/gen_simi.py @@ -0,0 +1,38 @@ +import pandas as pd +from dataset import calculate_similarity + +# 读取模板 CSV 文件和目标 CSV 文件 +template_file = './metadata/data_test.csv' # 模板CSV文件路径 +target_file = './metadata/data_0920_i.csv' # 目标CSV文件路径 + +template_df = pd.read_csv(template_file) +target_df = pd.read_csv(target_file) + +# 假设我们要使用 'template_column' 列作为模板中的列,'target_column' 列作为目标表格中的列 +template_column = 'Seq' # 替换为你的模板表格列名 +target_column = 'Seq' # 替换为你的目标表格列名 + +# 创建一个空的 DataFrame,用于存储符合条件的行,并复制目标表格的表头 +extracted_rows = pd.DataFrame(columns=target_df.columns) + +# 遍历目标表格的每一行 +for index, target_row in target_df.iterrows(): + target_value = target_row[target_column] + + # 遍历模板表格的每个值 + for template_value in template_df[template_column]: + + # 计算指标 + metric = calculate_similarity(target_value.strip().upper(), template_value.strip().upper()) + + # 如果指标大于0.3,则提取目标表格的该行 + if metric > 0.3: + extracted_rows = pd.concat([extracted_rows, pd.DataFrame([target_row])], ignore_index=True) + # 只要找到一个匹配值,就可以继续检查下一行 + break + +# 将提取的行保存到一个新的CSV文件中,确保表头与目标表格一致 +output_file = './metadata/data_simi.csv' +extracted_rows.to_csv(output_file, index=False) + +print(f"符合条件的行已保存到 {output_file}") \ No newline at end of file diff --git a/t3.1/infer.py b/t3.1/infer.py new file mode 100644 index 0000000000000000000000000000000000000000..b84ad6455d44618f55ce9cabb2d83f6d5ae2dd29 --- /dev/null +++ b/t3.1/infer.py @@ -0,0 +1,133 @@ +import argparse +import json +import logging +import os +import time +import pandas as pd + +from dataset import MDataset +from network import SEQPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mlp', + help='mlp gru lstm rnn tf mamba') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--mlp', type=str, default='./run/mlp-bce256-0.001-50', + help='path of the pretrain model') +parser.add_argument('--gru', type=str, default='./run/gru-bce256-0.001-50', + help='path of the pretrain model') +parser.add_argument('--lstm', type=str, default='./run/lstm-bce256-0.001-50', + help='path of the pretrain model') +parser.add_argument('--rnn', type=str, default='./run/rnn-bce256-0.001-50', + help='path of the pretrain model') +parser.add_argument('--tf', type=str, default='./run/tf-bce256-0.001-50', + help='path of the pretrain model') +parser.add_argument('--mamba', type=str, default='./run/mamba-bce256-0.001-50', + help='path of the pretrain model') +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + +model_path = {'mlp': args.mlp, 'gru': args.gru, 'lstm': args.lstm, 'rnn': args.rnn, 'tf': args.tf, 'mamba': args.mamba} + + +def main(): + set_seed(args.seed) + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + results = pd.DataFrame() + + logging.info('Loading Test Dataset') + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + + models = {'mlp': SEQPeptide(classes=args.classes, q_encoder='mlp'), + 'gru': SEQPeptide(classes=args.classes, q_encoder='gru'), + 'lstm': SEQPeptide(classes=args.classes, q_encoder='lstm'), + 'rnn': SEQPeptide(classes=args.classes, q_encoder='rnn'), + 'tf': SEQPeptide(classes=args.classes, q_encoder='tf'), + 'mamba': SEQPeptide(classes=args.classes, q_encoder='mamba')} + + for model_name in model_path.keys(): + model = models[model_name] + pred_all = [] + gt_all = [] + for i in range(1,6): + model.load_state_dict(torch.load(os.path.join(model_path[model_name], f'model_{i}.pth'))) + model.to(device).eval() + with torch.no_grad(): + for data in qlx_loader: + voxel, seq, gt = data + gt_all.append(gt.to(device)) + out = model((voxel.to(device), seq.to(device))) + pred_all.append(out) + pred_all = torch.nn.functional.sigmoid(torch.cat(pred_all, dim=0)).squeeze().cpu().numpy() + gt_all = torch.cat(gt_all, dim=0).int().squeeze().cpu().numpy() + results[model_name] = pred_all.ravel(order='F') + + results['gt'] = gt_all.ravel(order='F') + + results.to_csv("preds.csv", index=False) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/t3.1/loss.py b/t3.1/loss.py new file mode 100644 index 0000000000000000000000000000000000000000..d2c1cabdc7bb5df53b8513344372ea557b89aa29 --- /dev/null +++ b/t3.1/loss.py @@ -0,0 +1,123 @@ +import torch +from torch import nn +import torch.nn.functional as F +from math import cos, pi, sin +import math +import numpy as np +from scipy.special import lambertw + + +def mixup_criterion(criterion, pred, y_a, y_b, lam, pow=2): + y = lam ** pow * y_a + (1 - lam) ** pow * y_b + return criterion(pred, y) + + +def mixup_data(v, q, a): + '''Returns mixed inputs, pairs of targets, and lambda without organ constraint''' + lam = np.random.beta(1, 1) + + batch_size = v.shape[0] + index = torch.randperm(batch_size) + + mixed_v = lam * v + (1 - lam) * v[index, :] + mixed_q = lam * q + (1 - lam) * q[index, :] + + a_1, a_2 = a, a[index] + return mixed_v, mixed_q, a_1, a_2, lam + + +def linear(epoch, nepoch): + return 1 - epoch / nepoch + + +def convex(epoch, nepoch): + return epoch / (2 - nepoch) + + +def concave(epoch, nepoch): + return 1 - sin((epoch / nepoch) * (pi / 2)) + + +def composite(epoch, nepoch): + return 0.5 * cos((epoch / nepoch) * pi) + 0.5 + + +class LogCoshLoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y_t, y_prime_t): + ey_t = y_t - y_prime_t + return torch.mean(torch.log(torch.cosh(ey_t + 1e-12)))+F.mse_loss(y_t, y_prime_t) + + +class MLCE(nn.Module): + def __init__(self): + super(MLCE, self).__init__() + + def _mlcce(self, y_pred, y_true): + y_pred = (1 - 2 * y_true) * y_pred + y_pred_neg = y_pred - y_true * 1e12 + y_pred_pos = y_pred - (1 - y_true) * 1e12 + zeros = torch.zeros_like(y_pred[..., :1]) + y_pred_neg = torch.cat([y_pred_neg, zeros], dim=-1) + y_pred_pos = torch.cat([y_pred_pos, zeros], dim=-1) + neg_loss = torch.logsumexp(y_pred_neg, dim=-1) + pos_loss = torch.logsumexp(y_pred_pos, dim=-1) + loss = torch.mean(neg_loss + pos_loss) + return loss + + def __call__(self, y_pred, y_true): + return self._mlcce(y_pred, y_true) + + +class SuperLoss(nn.Module): + def __init__(self, C=10, lam=1, batch_size=128): + super(SuperLoss, self).__init__() + self.tau = math.log(C) + self.lam = lam # set to 1 for CIFAR10 and 0.25 for CIFAR100 + self.batch_size = batch_size + + def forward(self, logits, targets): + l_i = F.mse_loss(logits, targets, reduction='none').detach() + sigma = self.sigma(l_i) + loss = (F.mse_loss(logits, targets, reduction='none') - self.tau) * sigma + self.lam * ( + torch.log(sigma) ** 2) + loss = loss.sum() / self.batch_size + return loss + + def sigma(self, l_i): + x = torch.ones(l_i.size()) * (-2 / math.exp(1.)) + x = x.cuda() + y = 0.5 * torch.max(x, (l_i - self.tau) / self.lam) + y = y.cpu().numpy() + sigma = np.exp(-lambertw(y)) + sigma = sigma.real.astype(np.float32) + sigma = torch.from_numpy(sigma).cuda() + return sigma + + +def unbiased_curriculum_loss(out, data, args, epoch, epochs, scheduler='linear'): + losses = [] + scheduler = linear if scheduler == 'linear' else concave + + # calculate difficulty measurement function + adjusted_losses = [] + for idx in range(out.shape[0]): + ground_truth = max(1, abs(data[idx].item())) + loss = F.mse_loss(out[idx], data[idx]) + losses.append(loss) + adjusted_losses.append(loss.item() / ground_truth) + + mean_loss, std_loss = np.mean(adjusted_losses), np.std(adjusted_losses) + + # re-weight losses + total_loss = 0 + for i, loss in enumerate(losses): + if adjusted_losses[i] > mean_loss + 1 * std_loss: + schedule_factor = scheduler(epoch, args.epochs) + total_loss += schedule_factor * loss + else: + total_loss += loss + + return total_loss diff --git a/t3.1/main.py b/t3.1/main.py new file mode 100644 index 0000000000000000000000000000000000000000..695c354a0323b12bb1d11c0c8050d0ebc06d09fa --- /dev/null +++ b/t3.1/main.py @@ -0,0 +1,229 @@ +import argparse +import json +import logging +import os +import time + +from dataset import MDataset +from network import SEQPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mlp', + help='mlp gru lstm rnn tf mamba') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + if args.loss == 'mlce' and args.task_type != 'slc': + criterion = MLCE() + elif args.loss == "bce" or args.task_type == 'slc': + args.loss = "bce" + criterion = nn.BCEWithLogitsLoss() + else: + criterion = 0 + pass + weight_dir = "./run/" + args.model + '-' + args.loss + str(args.batch_size) + '-' + str(args.lr) + '-' + str(args.epochs) + if not os.path.exists(weight_dir): + os.makedirs(weight_dir) + + logging.basicConfig(handlers=[ + logging.FileHandler(filename=os.path.join(weight_dir, "training.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + with open(os.path.join(weight_dir, "config.json"), "w") as f: + f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src, data_ver=args.data_ver) + # else: + # set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src) + # else: + # qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + # saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + # if args.loss == 'bce': + # weight = torch.Tensor([0] * args.classes) + # for i in train_set: + # weight += i[2] + # weight = (len(train_set) - weight) / weight + # criterion.register_buffer('pos_weight', weight.to(device)) + + train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=1, shuffle=False) + model = SEQPeptide(classes=set_all.num_classes, q_encoder=args.model) + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + # optimizer = torch.optim.Adam(model.parameters()) + optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr) + # optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=True, weight_decay=5e-5) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Train set:{len(train_set)}, Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + best_metric = 0 + best_qlx = 0 + best_saap = 0 + start_time = time.time() + + for epoch in range(1, args.epochs + 1): + if args.task_type in ('mlc', 'slc') : + train_loss, ap, f1, acc, auc = train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer) + logging.info(f'Epoch: {epoch:03d}, Train Loss: {train_loss:.3f}, ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + avg_metric = ap + f1 + acc + auc + if avg_metric > best_metric: + logging.info(f'Epoch: {epoch:03d} New Best validation metrics, running test session') + torch.save(model.state_dict(), weights_path) + best_metric = avg_metric + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, epoch, model, None, qlx_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_metric = qlx_ap + qlx_f1 + qlx_acc + qlx_auc + if qlx_metric > best_qlx: + best_qlx = qlx_metric + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, epoch, model, None, saap_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_metric = saap_ap + saap_f1 + saap_acc + saap_auc + if saap_metric > best_saap: + best_saap = saap_metric + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'used time {(time.time()-start_time)/3600:.2f}h') + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/result.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/t3.1/network.py b/t3.1/network.py new file mode 100644 index 0000000000000000000000000000000000000000..1c423411243727e05e267be33a6f7d742ff450fc --- /dev/null +++ b/t3.1/network.py @@ -0,0 +1,703 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F +from mamba_ssm import Mamba + + +class Hamburger(nn.Module): + def __init__(self, inp, oup, reduction=32): + super(Hamburger, self).__init__() + self.pool_h = nn.AdaptiveAvgPool3d((1, None, None)) + self.pool_w = nn.AdaptiveAvgPool3d((None, 1, None)) + self.pool_d = nn.AdaptiveAvgPool3d((None, None, 1)) + + mip = max(8, inp // reduction) + self.conv1 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.conv2 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.conv3 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.gn1 = nn.GroupNorm(8, mip) + self.gn2 = nn.GroupNorm(8, mip) + self.gn3 = nn.GroupNorm(8, mip) + + self.act = nn.LeakyReLU(0.2) + self.conv_h = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + self.conv_w = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + self.conv_d = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + + def forward(self, x): + n, c, h, w, d = x.size() + x_h = self.pool_h(x) + # print(x_h.shape) + x_w = self.pool_w(x).permute(0, 1, 3, 2, 4) + # print(x_w.shape) + x_d = self.pool_d(x).permute(0, 1, 4, 2, 3) + # print(x_d.shape) + y_hwd = torch.cat([x_h, x_w, x_d], dim=2) + # y_hd = torch.cat([x_h, x_d], dim=2) + # y_dw = torch.cat([x_d, x_w], dim=2) + y_hwd = self.conv1(y_hwd) + # y_hd = self.conv2(y_hd) + # y_dw = self.conv3(y_dw) + y_hwd = self.gn1(y_hwd) + # y_hd = self.gn2(y_hd) + # y_dw = self.gn3(y_dw) + y_hwd = self.act(y_hwd) + # y_hd = self.act(y_hd) + # y_dw = self.act(y_dw) + # print(y_hwd.shape) + x_h, x_w, x_d = torch.split(y_hwd, [1, 1, 1], dim=2) + x_w = x_w + x_h = x_h.permute(0, 1, 3, 2, 4) + x_d = x_d.permute(0, 1, 3, 4, 2) + a_h = self.conv_h(x_h).sigmoid() + a_w = self.conv_w(x_w).sigmoid() + a_d = self.conv_d(x_d).sigmoid() + a_hw = a_w * a_h + out = a_hw * a_d + return out + x + + +class BasicBlock(nn.Module): + expansion = 1 + + def __init__(self, in_channels, channels, stride=1): + super().__init__() + + self.conv1 = nn.Conv3d(in_channels, channels, kernel_size=3, stride=stride, padding=1, bias=False) + self.bn1 = nn.BatchNorm3d(channels) + self.relu = nn.ReLU() + + self.conv2 = nn.Conv3d(channels, channels, kernel_size=3, stride=1, padding=1, bias=False) + self.bn2 = nn.BatchNorm3d(channels) + + self.downsample = nn.Sequential() + if stride != 1 or in_channels != channels * self.expansion: + self.downsample = nn.Sequential( + nn.Conv3d(in_channels, channels * self.expansion, kernel_size=1, stride=stride, bias=False), + nn.BatchNorm3d(channels * self.expansion) + ) + + self.stride = stride + + def forward(self, x): + shortcut = self.downsample(x) + + out = self.conv1(x) + out = self.bn1(out) + out = self.relu(out) + + out = self.conv2(out) + out = self.bn2(out) + + out += shortcut + out = self.relu(out) + + return out + + +class Bottleneck(nn.Module): + expansion = 4 + + def __init__(self, in_channels, channels, stride=1): + super().__init__() + + self.conv1 = nn.Conv3d(in_channels, channels, kernel_size=1, bias=False) + self.bn1 = nn.BatchNorm3d(channels) + self.relu = nn.ReLU() + + self.conv2 = nn.Conv3d(channels, channels, kernel_size=3, stride=stride, padding=1, bias=False) + self.bn2 = nn.BatchNorm3d(channels) + + self.conv3 = nn.Conv3d(channels, channels * self.expansion, kernel_size=1, bias=False) + self.bn3 = nn.BatchNorm3d(channels * self.expansion) + + self.downsample = nn.Sequential() + if stride != 1 or in_channels != channels * self.expansion: + self.downsample = nn.Sequential( + nn.Conv3d(in_channels, channels * self.expansion, kernel_size=1, stride=stride, bias=False), + nn.BatchNorm3d(channels * self.expansion) + ) + + def forward(self, x): + shortcut = self.downsample(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 += shortcut + out = self.relu(out) + + return out + + +class ResNet3D(nn.Module): + def __init__(self, block, layers, attention_module): + super().__init__() + if attention_module is not None: + self.attention_flag = True + self.attention_module = attention_module + else: + self.attention_flag = False + + self.in_channels = 64 + + self.conv1 = nn.Conv3d(4, 64, kernel_size=(7, 7, 7), stride=(2, 2, 2), padding=(3, 3, 3), bias=False) + self.bn1 = nn.BatchNorm3d(64) + self.relu = nn.ReLU() + self.max_pool = nn.MaxPool3d(kernel_size=(3, 3, 3), stride=(2, 2, 2), padding=(1, 1, 1)) + + self.layer1 = self._make_layer(block, 64, layers[0], stride=1) + 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) + self.avg_pool = nn.AdaptiveAvgPool3d(1) + + # initialize + for m in self.modules(): + if isinstance(m, nn.Conv3d): + nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') + elif isinstance(m, nn.BatchNorm3d): + m.weight.data.fill_(1) + m.bias.data.zero_() + + def _make_layer(self, block, channels, n_blocks, stride=1): + assert n_blocks > 0, "number of blocks should be greater than zero" + layers = [block(self.in_channels, channels, stride)] + self.in_channels = channels * block.expansion + for i in range(1, n_blocks): + layers.append(block(self.in_channels, channels)) + return nn.Sequential(*layers) + + def forward(self, x, debug=False): + out = self.conv1(x) + out = self.bn1(out) + out = self.relu(out) + if debug: + print("shape1:", out.shape) + out = self.max_pool(out) + if debug: + print("shape2:", out.shape) + out = self.layer1(out) + if debug: + print("shape3:", out.shape) + out = self.layer2(out) + if debug: + print("shape4:", out.shape) + out = self.layer3(out) + if debug: + print("shape5:", out.shape) + out = self.layer4(out) + if self.attention_flag: + out = self.attention_module(out) + if debug: + print("shape6:", out.shape) + out = self.avg_pool(out) + if debug: + print("shape7:", out.shape) + + out = out.view(out.size(0), -1) + return out + + +class FusionModule(nn.Module): + def __init__(self): + super().__init__() + self.f_value = nn.Sequential( + nn.Conv1d(32, 1, kernel_size=1, stride=1), + ) + + def forward(self, x, x_seq): + x = self.f_value(x) + print(x.shape) + + +class ResNet3DFusion(nn.Module): + def __init__(self, block, layers, attention_module): + super().__init__() + if attention_module is not None: + self.attention_flag = True + self.attention_module = attention_module + else: + self.attention_flag = False + + self.in_channels = 64 + + self.conv1 = nn.Conv3d(3, 64, kernel_size=(7, 7, 7), stride=(2, 2, 2), padding=(3, 3, 3), bias=False) + self.bn1 = nn.BatchNorm3d(64) + self.relu = nn.ReLU() + self.max_pool = nn.MaxPool3d(kernel_size=(3, 3, 3), stride=(2, 2, 2), padding=(1, 1, 1)) + + self.layer1 = self._make_layer(block, 64, layers[0], stride=1) + 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) + self.avg_pool = nn.AdaptiveAvgPool3d(1) + self.fc1 = nn.Linear(256, 1) + self.fc2 = nn.Linear(256, 1) + self.fc3 = nn.Linear(256, 1) + self.fc4 = nn.Linear(256, 1) + self.fc5 = nn.Linear(256, 1) + self.fc6 = nn.Linear(256, 1) + + # initialize + for m in self.modules(): + if isinstance(m, nn.Conv3d): + nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') + elif isinstance(m, nn.BatchNorm3d): + m.weight.data.fill_(1) + m.bias.data.zero_() + + def _make_layer(self, block, channels, n_blocks, stride=1): + assert n_blocks > 0, "number of blocks should be greater than zero" + layers = [block(self.in_channels, channels, stride)] + self.in_channels = channels * block.expansion + for i in range(1, n_blocks): + layers.append(block(self.in_channels, channels)) + return nn.Sequential(*layers) + + def forward(self, x, x_seq, debug=False): + seq1 = self.fc1(x_seq) + seq2 = self.fc2(x_seq) + seq3 = self.fc3(x_seq) + seq4 = self.fc4(x_seq) + seq5 = self.fc5(x_seq) + out = self.conv1(x) + + out = out * (seq1.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) + 1) + + out = self.bn1(out) + out = self.relu(out) + if debug: + print("shape1:", out.shape) + out = self.max_pool(out) + if debug: + print("shape2:", out.shape) + out = self.layer1(out) + out = out * (seq2.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) + 1) + + if debug: + print("shape3:", out.shape) + out = self.layer2(out) + out = out * (seq3.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) + 1) + + if debug: + print("shape4:", out.shape) + out = self.layer3(out) + out = out * (seq4.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) + 1) + + if debug: + print("shape5:", out.shape) + out = self.layer4(out) + out = out * (seq5.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) + 1) + + if self.attention_flag: + out = self.attention_module(out) + if debug: + print("shape6:", out.shape) + out = self.avg_pool(out) + if debug: + print("shape7:", out.shape) + + out = out.view(out.size(0), -1) + return out + + +class MLP(nn.Module): + def __init__(self, input_dim, hidden_dim, output_dim, num_layers, dropout_rate): + super(MLP, self).__init__() + + layers = [] + layers.append(nn.Linear(input_dim, hidden_dim)) + layers.append(nn.ReLU()) + layers.append(nn.Dropout(dropout_rate)) + + for _ in range(num_layers - 1): + layers.append(nn.Linear(hidden_dim, hidden_dim)) + layers.append(nn.ReLU()) + layers.append(nn.Dropout(dropout_rate)) + + layers.append(nn.Linear(hidden_dim, output_dim)) + + self.network = nn.Sequential(*layers) + + def forward(self, x): + return self.network(x) + + +class PositionalEncoding(nn.Module): + def __init__(self, d_model, max_len=50): + super(PositionalEncoding, self).__init__() + + pe = torch.zeros(max_len, d_model) # (max_len, d_model) + position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1) # (max_len, 1) + div_term = torch.exp(torch.arange(0, d_model, 2).float() * + (-torch.log(torch.FloatTensor([10000.0])) / d_model)) # (d_model/2,) + pe[:, 0::2] = torch.sin(position * div_term) # 偶数维 + pe[:, 1::2] = torch.cos(position * div_term) # 奇数维 + pe = pe.unsqueeze(0) # (1, max_len, d_model) + self.register_buffer('pe', pe) + + def forward(self, x): + """ + x: (B, N, d_model) + """ + x = x + self.pe[:, :x.size(1), :] + return x + + +class TransformerModel(nn.Module): + def __init__(self, nheads, d_model, num_layers, out_dim, max_length=50): + super(TransformerModel, self).__init__() + + # 嵌入层,将输入从 (B, N) 转换到 (B, N, embed_dim) + self.embedding = nn.Linear(1, d_model) + + # 位置编码 + self.pos_encoder = PositionalEncoding(d_model, max_length) + + # Transformer 编码器层 + encoder_layers = nn.TransformerEncoderLayer(d_model=d_model, + nhead=nheads, + activation='relu') + self.transformer_encoder = nn.TransformerEncoder(encoder_layers, num_layers=num_layers) + + # 全局池化(可以根据任务选择不同的聚合方式) + self.global_pool = nn.AdaptiveAvgPool1d(1) + + # 输出层 + self.fc = nn.Linear(d_model, out_dim) + + def forward(self, src): + """ + src: (B, N) + """ + # 嵌入 + embedded = self.embedding(src.unsqueeze(-1)) # (B, N, embed_dim) + embedded = self.pos_encoder(embedded) # 添加位置编码 + + # 转置以适应 Transformer (N, B, embed_dim) + embedded = embedded.permute(1, 0, 2) + + # Transformer 编码 + transformer_out = self.transformer_encoder(embedded) # (N, B, embed_dim) + + # 转置回 (B, N, embed_dim) + transformer_out = transformer_out.permute(1, 0, 2) + + # 全局池化,将 (B, N, embed_dim) 转换为 (B, embed_dim) + pooled = self.global_pool(transformer_out.permute(0, 2, 1)).squeeze(-1) + + # 输出层 + output = self.fc(pooled) # (B, output_dim) + + return output + + +class MambaModel(nn.Module): + def __init__(self, d_model, out_dim, max_length=30): + super(MambaModel, self).__init__() + self.linear = nn.Linear(in_features=1, out_features=d_model) + self.pos_encoder = PositionalEncoding(d_model, max_length) + self.mamba = Mamba(d_model=d_model) + self.global_pool = nn.AdaptiveAvgPool1d(1) + self.fc = nn.Linear(d_model * 2, out_dim) + + def forward(self, x: torch.Tensor): + x = self.pos_encoder(self.linear(x.unsqueeze(-1))) + y = self.mamba(x) + y_flip = self.mamba(x.flip([-2])).flip([-2]) + y = torch.cat((y, y_flip), dim=-1) + y = self.fc(self.global_pool(y.permute(0, 2, 1)).squeeze(-1)) + return y + + +class SEQ(nn.Module): + def __init__(self, seq_type='mlp', input_dim=21, hidden_dim=128, out_dim=128, num_layers=2, max_length=30): + super(SEQ, self).__init__() + self.seq_type = seq_type + if seq_type == 'rnn': + self.rnn = nn.RNN( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'gru': + self.rnn = nn.GRU( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'lstm': + self.rnn = nn.LSTM( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'tf': + self.transformer = TransformerModel(nheads=4, d_model=hidden_dim, num_layers=2, out_dim=out_dim, max_length=max_length) + elif seq_type == 'mamba': + self.mamba = MambaModel(d_model=hidden_dim, out_dim=out_dim, max_length=max_length) + elif self.seq_type == 'mlp': + self.rnn = MLP(30, hidden_dim, out_dim, 3, 0.3) + else: + raise NotImplementedError(f'\'{seq_type}\' not implemented') + + self.rnn_fc = nn.Sequential( + nn.Linear(hidden_dim * 2, hidden_dim), + nn.ReLU(), + nn.Linear(hidden_dim, out_dim) + ) + + def forward(self, seq): + if self.seq_type == 'mlp': + return self.rnn(seq) + elif self.seq_type == 'tf': + return self.transformer(seq) + elif self.seq_type == 'mamba': + return self.mamba(seq) + else: + one_hot_seq = F.one_hot(seq.to(torch.int64), num_classes=21).float() + r_out = self.rnn(one_hot_seq, None)[0] # None represents zero initial hidden state + out = self.rnn_fc(r_out[:, -1, :]) + return out + # def forward(self, x, seq_lengths): + # + + +class VoxPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None): + super().__init__() + + if attention == 'hamburger': + self.attention = Hamburger(2048, 2048) + else: + self.attention = None + # v_encoder could be resnet26 or resnet50 + if v_encoder == 'resnet26': + self.v_encoder = ResNet3D(Bottleneck, [1, 2, 4, 1], self.attention) + + if fusion == 'mlp': + self.fusion = nn.Linear(512 * 4 + 256, classes) + elif fusion == 'att': + self.fusion = nn.Linear(512 * 4 + 256, classes) + else: + raise NotImplementedError + self.vox_fc = nn.Linear(2048, classes) + + def forward(self, x, seq_lengths=None): + vox, seq = x + seq_emb = self.v_encoder(vox) + pred = self.vox_fc(seq_emb) + return pred + + +class SEQPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + self.classes = classes + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + + self.seq_fc = nn.Linear(128, classes) + + def forward(self, x, seq_lengths=None): + vox, seq = x + seq_emb = self.q_encoder(seq) + pred = self.seq_fc(seq_emb) + return pred + + +import torch +import torch.nn as nn +import torch.nn.functional as F + + +class ConvNet(nn.Module): + def __init__(self, num_classes: int = 2): + super(ConvNet, self).__init__() + self.conv1 = nn.Conv1d(3, 16, 3, padding=1) + self.conv2 = nn.Conv1d(16, 32, 3, padding=1) + self.fc1 = nn.Linear(32 * 7, 128) + # self.fc2 = nn.Linear(128, num_classes) + + def forward(self, x): + # print(x.shape) + x = x.permute(0, 2, 1) + x = F.relu(self.conv1(x)) + x = F.max_pool1d(x, 2) + x = F.relu(self.conv2(x)) + x = F.max_pool1d(x, 2) + x = x.view(x.shape[0], -1) + return self.fc1(x) + # x = F.relu(self.fc1(x)) + # x = self.fc2(x) + # return x + + +class ConvNet2D(nn.Module): + def __init__(self, num_classes: int = 2): + super(ConvNet2D, self).__init__() + self.conv1 = nn.Conv2d(1, 8, 3, padding=1) + self.conv2 = nn.Conv2d(8, 16, 3, padding=2, stride=2) + self.conv3 = nn.Conv2d(16, 32, 3, padding=2, stride=2) + # self.pool = nn.AdaptiveAvgPool2d(32) + self.fc1 = nn.Linear(32 * 3 * 9, 128) + # self.fc2 = nn.Linear(128, num_classes) + + def forward(self, x): + # print(x.shape) + x = x.unsqueeze(1) + x = F.relu(self.conv1(x)) + # print(x.shape) + x = F.relu(self.conv2(x)) + # print(x.shape) + x = F.relu(self.conv3(x)) + # x = self.pool(x) + # print(x.shape) + x = x.view(x.shape[0], -1) + return self.fc1(x) + + +# convnet = ConvNet() +# print(convnet) +class MMPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + if attention == 'hamburger': + self.attention = Hamburger(2048, 2048) + else: + self.attention = None + # v_encoder could be resnet26 or resnet50 + if v_encoder == 'resnet26': + self.v_encoder = ResNet3D(Bottleneck, [1, 2, 4, 1], self.attention) + # self.v_encoder = SwinUNETR(img_size=(64, 64, 64), in_channels=3, out_channels=1) + elif v_encoder == 'resnet50': + self.v_encoder = ResNet3D(Bottleneck, [3, 4, 6, 3], self.attention) + else: + raise NotImplementedError + + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + # self.ss_encoder = SEQ(seq_type=q_encoder) + if fusion == 'mlp': + self.fusion = nn.Linear(512 * 4 + 256, 256) + # self.fusion = nn.Linear(192 + 256, classes) + elif fusion == 'att': + self.fusion = nn.Linear(512 * 4 + 256, 256) + else: + raise NotImplementedError + + # self.vox_fc = nn.Linear(2048, classes) + # self.seq_fc = nn.Linear(256, classes) + self.out = nn.Sequential(nn.ReLU(inplace=True), nn.Linear(256, classes)) + self.classes = classes + + def forward(self, x, seq_lengths=None): + vox, seq = x + # print(vox.shape) + # print(seq.shape) + vox_emb = self.v_encoder(vox) + # print(vox_emb.shape) + seq_emb = self.q_encoder(seq, seq_lengths) + # print(seq_emb.shape) + # ss_emb = self.ss_encoder(second_s) + fused_feature = torch.cat((seq_emb, vox_emb), dim=1) + pred = self.fusion(fused_feature) + pred = self.out(pred) + # pred1 = self.vox_fc(vox_emb) + # pred2 = self.seq_fc(seq_emb) + # return pred, fused_feature + return pred + + +class SMPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, hidden_dim=256, max_length=30): + super().__init__() + self.siamese_encoder1 = MMPeptide(v_encoder, q_encoder, fusion, classes, attention, max_length) + # self.siamese_encoder2 = MMPeptide(v_encoder, q_encoder, fusion, classes, attention) + self.fc = nn.Sequential( + nn.Linear(hidden_dim * 2, hidden_dim), + nn.ReLU(), + nn.Linear(hidden_dim, 1) + ) + + def forward(self, x, seq_lengths=None): + f_mutated = self.siamese_encoder1(x[0]) + f_wide_type = self.siamese_encoder1(x[1]) + return self.fc(torch.cat((f_mutated, f_wide_type), dim=1)) + + +class MMFPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + if attention == 'hamburger': + self.attention = Hamburger(2048, 2048) + else: + self.attention = None + # v_encoder could be resnet26 or resnet50 + if v_encoder == 'resnet26': + self.v_encoder = ResNet3D(Bottleneck, [1, 2, 4, 1], self.attention) + # self.v_encoder = ResNet3DFusion(Bottleneck, [1, 2, 4, 1], self.attention) + elif v_encoder == 'resnet50': + self.v_encoder = ResNet3D(Bottleneck, [3, 4, 6, 3], self.attention) + else: + raise NotImplementedError + + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + + if fusion == 'mlp': + self.fusion = nn.Linear(512 * 4 + 256, classes) + elif fusion == 'att': + self.fusion = nn.Linear(512 * 4 + 256, classes) + else: + raise NotImplementedError + + self.vox_fc = nn.Linear(2048, classes) + self.seq_fc = nn.Linear(256, classes) + + def forward(self, x, seq_lengths=None): + vox, seq = x + # print(vox.shape) + # print(seq.shape) + seq_emb = self.q_encoder(seq, seq_lengths) + + vox_emb = self.v_encoder(vox, seq_emb) + # print(vox_emb.shape) + # print(seq_emb.shape) + fused_feature = torch.cat((seq_emb, vox_emb), dim=1) + pred = self.fusion(fused_feature) + # pred1 = self.vox_fc(vox_emb) + # pred2 = self.seq_fc(seq_emb) + return pred + + +if __name__ == "__main__": + # model = MMFPeptide() + # voxel = torch.zeros((4, 3, 64, 64, 64)) + # # # h_in = torch.zeros((2, 2048, 2, 2, 2)) + # # # h = Hamburger(2048, 2048) + # # # h(h_in) + # seq = torch.ones((4, 50)) + # res = model.forward((voxel, seq)) + # out = model((voxel, seq)) + # print(out.shape) + # model = ConvNet2D() + input_seq = torch.ones((4, 1, 30)) + # model(input_seq) + transformer = TransformerModel(nhead=4, d_model=32, num_layers=2) + print(transformer(input_seq).shape) diff --git a/t3.1/run/gru-bce256-0.001-50/config.json b/t3.1/run/gru-bce256-0.001-50/config.json new file mode 100644 index 0000000000000000000000000000000000000000..444398d7944d7be6deeb7ed758da919d48a8bdb1 --- /dev/null +++ b/t3.1/run/gru-bce256-0.001-50/config.json @@ -0,0 +1 @@ +{"model": "gru", "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.1/run/gru-bce256-0.001-50/model_1.pth b/t3.1/run/gru-bce256-0.001-50/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..e082346260152013d14c500c01121cf86964a6c3 --- /dev/null +++ b/t3.1/run/gru-bce256-0.001-50/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:44a0a6f59469bd945e6ab7336a04da049f63e8fe61edea1e8206747da4fba871 +size 1854596 diff --git a/t3.1/run/gru-bce256-0.001-50/model_2.pth b/t3.1/run/gru-bce256-0.001-50/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..d8c2628bf0319f836b80a0825bf90c9a7b2c385a --- /dev/null +++ b/t3.1/run/gru-bce256-0.001-50/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:792b01df7874d7680ab772d874e789376080f24814ef1774dcfac3520342c39f +size 1854596 diff --git a/t3.1/run/gru-bce256-0.001-50/model_3.pth b/t3.1/run/gru-bce256-0.001-50/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..4f6d4186de1b99dc3d2c18eada613ad41e32b3d8 --- /dev/null +++ b/t3.1/run/gru-bce256-0.001-50/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c7fbec6fe9eb8b38fdfeb78fc848e1a8169e8f5df510d2b5b3b9d3c69315df10 +size 1854596 diff --git a/t3.1/run/gru-bce256-0.001-50/model_4.pth b/t3.1/run/gru-bce256-0.001-50/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..37360aab8a0db325dbf2db9db72f856457c7cf4a --- /dev/null +++ b/t3.1/run/gru-bce256-0.001-50/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ca22553d99c46dd857f653f3b055aadd1c53045c4a6c118c01d422f65917c178 +size 1854596 diff --git a/t3.1/run/gru-bce256-0.001-50/model_5.pth b/t3.1/run/gru-bce256-0.001-50/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..5ea20f8a2c2e1c14bcb508b7f5c1276ed442032a --- /dev/null +++ b/t3.1/run/gru-bce256-0.001-50/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:69b590f53b23c804cd9dc4efc21b7bdfa66f42a811aca6e82600f465d7f7d45e +size 1854596 diff --git a/t3.1/run/gru-bce256-0.001-50/result.txt b/t3.1/run/gru-bce256-0.001-50/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..58b56898a6dfb8328a84a1f02a7d52654b8004f9 --- /dev/null +++ b/t3.1/run/gru-bce256-0.001-50/result.txt @@ -0,0 +1,9 @@ +Valid +0.8988269329071045,0.8484792709350586,0.8013780236244201,0.8573168516159058 +0.007939289205324655,0.0059540444194092395,0.0067192045478746375,0.005264435899967903 +qlx +0.8493218064308167,0.7297939658164978,0.6150110244750977,0.874067509174347 +0.0029503253825771893,0.012699909074253446,0.02536615358364534,0.006737497012955599 +saap +0.5456169724464417,0.5129449367523193,0.38499999046325684,0.6531985998153687 +0.016126342152109306,0.0,0.0,0.013445912126869347 diff --git a/t3.1/run/lstm-bce256-0.001-50/config.json b/t3.1/run/lstm-bce256-0.001-50/config.json new file mode 100644 index 0000000000000000000000000000000000000000..7ef1bcc3d556ffeaa6ae4ce01c2cb0ffbf891f99 --- /dev/null +++ b/t3.1/run/lstm-bce256-0.001-50/config.json @@ -0,0 +1 @@ +{"model": "lstm", "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.1/run/lstm-bce256-0.001-50/model_1.pth b/t3.1/run/lstm-bce256-0.001-50/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..6dc930d2bd5c4d53315cabea01aab94042b0cd79 --- /dev/null +++ b/t3.1/run/lstm-bce256-0.001-50/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b1e44b7386d60d1b1903626529e7c892d650229ddfc752f93155d36a6f515eda +size 2404484 diff --git a/t3.1/run/lstm-bce256-0.001-50/model_2.pth b/t3.1/run/lstm-bce256-0.001-50/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..e9faf189d83b1802bfeb267d0ccd086612bcfb54 --- /dev/null +++ b/t3.1/run/lstm-bce256-0.001-50/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8e78e5ee2e404f7f8b200b84569b23f0db60df62ab8b9f064511f9914db07b2b +size 2404484 diff --git a/t3.1/run/lstm-bce256-0.001-50/model_3.pth b/t3.1/run/lstm-bce256-0.001-50/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..97ad0906a7c7f08de8ecba6aa728dba0206f8ac4 --- /dev/null +++ b/t3.1/run/lstm-bce256-0.001-50/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ea8e10857507062d7a1fe5c6e7d2f8cd355a686928856399db895a658ae8e50c +size 2404484 diff --git a/t3.1/run/lstm-bce256-0.001-50/model_4.pth b/t3.1/run/lstm-bce256-0.001-50/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..ec9da33bfe5a92ce16e7971500317e4b365d3a89 --- /dev/null +++ b/t3.1/run/lstm-bce256-0.001-50/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cdf99d7dbfd6f1eb3895b6d8f23864f0ca0c0952bdc9b7294b5ca1618af11ad7 +size 2404484 diff --git a/t3.1/run/lstm-bce256-0.001-50/model_5.pth b/t3.1/run/lstm-bce256-0.001-50/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..43ffa07635b37678b23a37f463f4ecf478438079 --- /dev/null +++ b/t3.1/run/lstm-bce256-0.001-50/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5b548faace86adf66d6deefd13fab8af1cfb2cd6ae872654c14b48edaa37c2e6 +size 2404484 diff --git a/t3.1/run/lstm-bce256-0.001-50/result.txt b/t3.1/run/lstm-bce256-0.001-50/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..490037de566e0fda97d19f3c081dea7fafd226fd --- /dev/null +++ b/t3.1/run/lstm-bce256-0.001-50/result.txt @@ -0,0 +1,9 @@ +Valid +0.8937485814094543,0.8488885283470153,0.798557448387146,0.8518829464912414 +0.007305721316349519,0.006658241734526482,0.008083102929079536,0.007285823002735515 +qlx +0.8555017709732056,0.7273223996162415,0.6099337816238404,0.8849706411361694 +0.0024824259853227413,0.013888210525234338,0.027893287343044654,0.004315002625547045 +saap +0.6014903664588929,0.5140300154685974,0.3923333168029785,0.6707973122596741 +0.029397268273480225,0.0022126333830570625,0.009580062772531414,0.009874603952021692 diff --git a/t3.1/run/mamba-bce256-0.001-50/config.json b/t3.1/run/mamba-bce256-0.001-50/config.json new file mode 100644 index 0000000000000000000000000000000000000000..b148bb017838898ce40518b0c1c05335003d5fb5 --- /dev/null +++ b/t3.1/run/mamba-bce256-0.001-50/config.json @@ -0,0 +1 @@ +{"model": "mamba", "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.1/run/mamba-bce256-0.001-50/model_1.pth b/t3.1/run/mamba-bce256-0.001-50/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..0d7e41df551b24d473c9b5007e764e609244b063 --- /dev/null +++ b/t3.1/run/mamba-bce256-0.001-50/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fb9346f8ce824dd8f8adba5dba9dc2df877b21839f5d72b8759b7d1f276fcbb8 +size 821466 diff --git a/t3.1/run/mamba-bce256-0.001-50/model_2.pth b/t3.1/run/mamba-bce256-0.001-50/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..83bbfc77e1d7b52a0b5fd310401be1971eae299d --- /dev/null +++ b/t3.1/run/mamba-bce256-0.001-50/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0d7c84912f3e187ba283b3d183d31bc40c52a54db5c2d0c90058df67a3f96129 +size 821466 diff --git a/t3.1/run/mamba-bce256-0.001-50/model_3.pth b/t3.1/run/mamba-bce256-0.001-50/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..003399cad8d464f5930a72bd84584ac4d3e2ada1 --- /dev/null +++ b/t3.1/run/mamba-bce256-0.001-50/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7b2e56e3716b2441602c3677f4553c78ca1ad126c244d5474a71559b7af04b2f +size 821466 diff --git a/t3.1/run/mamba-bce256-0.001-50/model_4.pth b/t3.1/run/mamba-bce256-0.001-50/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..c214c9f20747cabcb8150b009495a34fa48413fe --- /dev/null +++ b/t3.1/run/mamba-bce256-0.001-50/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:49730d641accc50087c3529100012ef98b02e9b9204c50cf0c80eaa7d7f48b49 +size 821466 diff --git a/t3.1/run/mamba-bce256-0.001-50/model_5.pth b/t3.1/run/mamba-bce256-0.001-50/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..cbc31eec63b605134530f8c5f733708372e1ef2c --- /dev/null +++ b/t3.1/run/mamba-bce256-0.001-50/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dd3e033afeee9e06372725b9e968e51e1d82ac92e887c8d9b74ec0e1a11fc240 +size 821466 diff --git a/t3.1/run/mamba-bce256-0.001-50/result.txt b/t3.1/run/mamba-bce256-0.001-50/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..899a7b835eb0d5ffdbef98f688979c6c1344b1ea --- /dev/null +++ b/t3.1/run/mamba-bce256-0.001-50/result.txt @@ -0,0 +1,9 @@ +Valid +0.8659572720527648,0.8170834898948669,0.7579811692237854,0.8059861421585083 +0.0068057588821083355,0.009066688693069138,0.010303703721356172,0.010411869403007968 +qlx +0.7881160736083984,0.7599167704582215,0.7116997718811036,0.7934251070022583 +0.015644019899357025,0.026361853883693283,0.025709647354186186,0.022902887511244762 +saap +0.4370036065578461,0.5107917368412018,0.3886666536331177,0.5112276196479797 +0.020066347960116748,0.007785707798989145,0.008717789573141406,0.011946772321041702 diff --git a/t3.1/run/mlp-bce256-0.001-50/config.json b/t3.1/run/mlp-bce256-0.001-50/config.json new file mode 100644 index 0000000000000000000000000000000000000000..8afd020845be7ebf67f52f2f9e7174309bcf324d --- /dev/null +++ b/t3.1/run/mlp-bce256-0.001-50/config.json @@ -0,0 +1 @@ +{"model": "mlp", "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.1/run/mlp-bce256-0.001-50/model_1.pth b/t3.1/run/mlp-bce256-0.001-50/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..a8369d08dbe7f809e14b4a77b666e093c8c41d1b --- /dev/null +++ b/t3.1/run/mlp-bce256-0.001-50/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e2cc36166bc5bc027ca62c59456a1e97f72d7b9a06e383b28cf62dc7494c5347 +size 419756 diff --git a/t3.1/run/mlp-bce256-0.001-50/model_2.pth b/t3.1/run/mlp-bce256-0.001-50/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..87f2fc866580463932b15f290f5917826e6727cd --- /dev/null +++ b/t3.1/run/mlp-bce256-0.001-50/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8f08c3dde2101d4c0b287b3442abd3342bee7394fc86ac37f64ef7336180b504 +size 419756 diff --git a/t3.1/run/mlp-bce256-0.001-50/model_3.pth b/t3.1/run/mlp-bce256-0.001-50/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..0de6232b08b434128ccffbd3371c247f635cb9dd --- /dev/null +++ b/t3.1/run/mlp-bce256-0.001-50/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:445afefebd60c53a8950e9d657cb41102a8ba902fd5a65f2a4af5e463dc752ef +size 419756 diff --git a/t3.1/run/mlp-bce256-0.001-50/model_4.pth b/t3.1/run/mlp-bce256-0.001-50/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..e62d3409610bbc138946909a92337506cf97d26f --- /dev/null +++ b/t3.1/run/mlp-bce256-0.001-50/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bc6108ddd5c348a9e1abfcfb796810c0f1e86400e083d8248b19cfd9dbb7671b +size 419756 diff --git a/t3.1/run/mlp-bce256-0.001-50/model_5.pth b/t3.1/run/mlp-bce256-0.001-50/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..c66204c63dd34e3ad1ccc44640fcb52abbbad4a2 --- /dev/null +++ b/t3.1/run/mlp-bce256-0.001-50/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cab1711eab68b64e791323f25c73a054c94f63247779deb33372603b156f8eef +size 419756 diff --git a/t3.1/run/mlp-bce256-0.001-50/result.txt b/t3.1/run/mlp-bce256-0.001-50/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..1bf1e29e6c28e2aa17539adba2bfe89fe4685ca2 --- /dev/null +++ b/t3.1/run/mlp-bce256-0.001-50/result.txt @@ -0,0 +1,9 @@ +Valid +0.8461740136146545,0.7761861443519592,0.708870804309845,0.7606322407722473 +0.00852592182527828,0.005755604810441352,0.0063862498372552045,0.01057700696109496 +qlx +0.7094735503196716,0.686418604850769,0.5505518555641175,0.6533601999282836 +0.04528414701451461,0.009817155475167583,0.027165804762294396,0.07340266361342862 +saap +0.49114673137664794,0.5129449367523193,0.38499999046325684,0.5837281346321106 +0.04236579947084209,0.0,0.0,0.048177624029287915 diff --git a/t3.1/run/rnn-bce256-0.001-50/config.json b/t3.1/run/rnn-bce256-0.001-50/config.json new file mode 100644 index 0000000000000000000000000000000000000000..0f36855dd82dd158cb10973acdcba52f436268f6 --- /dev/null +++ b/t3.1/run/rnn-bce256-0.001-50/config.json @@ -0,0 +1 @@ +{"model": "rnn", "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.1/run/rnn-bce256-0.001-50/model_1.pth b/t3.1/run/rnn-bce256-0.001-50/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..615139895b2616c7f71fd2cc850d468aa98e833c --- /dev/null +++ b/t3.1/run/rnn-bce256-0.001-50/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ff47345dc6c0f0dcce03e11cc8588c02cbe19abb05e40fd62ff5fc2b4389f356 +size 754820 diff --git a/t3.1/run/rnn-bce256-0.001-50/model_2.pth b/t3.1/run/rnn-bce256-0.001-50/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..90b2ff02a9f9ec55fd47401abf5bcc000460e321 --- /dev/null +++ b/t3.1/run/rnn-bce256-0.001-50/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c0a9e7e830d54d1d61f3256447358e16990852fd8c8e232cf657a2b0443e1500 +size 754820 diff --git a/t3.1/run/rnn-bce256-0.001-50/model_3.pth b/t3.1/run/rnn-bce256-0.001-50/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..15dc63a6e41eb80f15f8b8fb019b649261117978 --- /dev/null +++ b/t3.1/run/rnn-bce256-0.001-50/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2486d9bb76c1b91c6fd9d5c16890275cbaa7fec0da9ef5e89310ecd694120ee8 +size 754820 diff --git a/t3.1/run/rnn-bce256-0.001-50/model_4.pth b/t3.1/run/rnn-bce256-0.001-50/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..f0c75994fb9e333650e808f9cb3cadaf98c7be8d --- /dev/null +++ b/t3.1/run/rnn-bce256-0.001-50/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4228b8e45ebc2620d58abb8248a9fd9fc5bf2d787944c53cd3be35a2513ecd54 +size 754820 diff --git a/t3.1/run/rnn-bce256-0.001-50/model_5.pth b/t3.1/run/rnn-bce256-0.001-50/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..6510d00a4a2c9aebae728584e0cdf74420c9c9c6 --- /dev/null +++ b/t3.1/run/rnn-bce256-0.001-50/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d474dbd6150a8e217b7fa916de90e246112b12326f8bbdc8de427c62bc55374a +size 754820 diff --git a/t3.1/run/rnn-bce256-0.001-50/result.txt b/t3.1/run/rnn-bce256-0.001-50/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..c231af55b4c632b2db3f96272debf4adecfaef9f --- /dev/null +++ b/t3.1/run/rnn-bce256-0.001-50/result.txt @@ -0,0 +1,9 @@ +Valid +0.8522108435630799,0.8332502365112304,0.775079095363617,0.8058153629302979 +0.02726555469402754,0.008308712861283348,0.01685464868999949,0.030622741226795383 +qlx +0.7879756212234497,0.7154163002967835,0.585871958732605,0.825910222530365 +0.044353001884581765,0.005957653796830326,0.012397451599285689,0.025735939663436354 +saap +0.5283541440963745,0.5133676171302796,0.3943333148956299,0.6201598405838012 +0.033016723824901915,0.0023645049183349783,0.008069137929269865,0.026673070821244173 diff --git a/t3.1/run/tf-bce256-0.001-50/config.json b/t3.1/run/tf-bce256-0.001-50/config.json new file mode 100644 index 0000000000000000000000000000000000000000..850e7a99e6540a615e7df51d6b15e2c52e1d1c52 --- /dev/null +++ b/t3.1/run/tf-bce256-0.001-50/config.json @@ -0,0 +1 @@ +{"model": "tf", "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.1/run/tf-bce256-0.001-50/model_1.pth b/t3.1/run/tf-bce256-0.001-50/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..7a885117fd3356421aab1e257808a3344f5e0514 --- /dev/null +++ b/t3.1/run/tf-bce256-0.001-50/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c23c4d7db6728c25940eb007f8877ebc411c37b6c3a4adec7cd22d031541372d +size 5041261 diff --git a/t3.1/run/tf-bce256-0.001-50/model_2.pth b/t3.1/run/tf-bce256-0.001-50/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..34a6a3ed69a2cbf8ed4cf1fa6a385bf47aa2183b --- /dev/null +++ b/t3.1/run/tf-bce256-0.001-50/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6f7e058d1eda2c8cf59dd909df463d8b708099a6cc61efc10b9e1221f698975b +size 5041261 diff --git a/t3.1/run/tf-bce256-0.001-50/model_3.pth b/t3.1/run/tf-bce256-0.001-50/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..3c39665e229ad238142e8eda4122add9cd8f07c0 --- /dev/null +++ b/t3.1/run/tf-bce256-0.001-50/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8e21b0c57638fd7dfcf324b3b331f0cea14c5effb3e1204c20237430e41ac5fb +size 5041261 diff --git a/t3.1/run/tf-bce256-0.001-50/model_4.pth b/t3.1/run/tf-bce256-0.001-50/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..c8b8e76fb9926240b5a3e8477fb689604ef33b9f --- /dev/null +++ b/t3.1/run/tf-bce256-0.001-50/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4db7dd1e4d8036b085d1e77b75f3b0c91d55cc6f31215275713374395f5fe6dd +size 5041261 diff --git a/t3.1/run/tf-bce256-0.001-50/model_5.pth b/t3.1/run/tf-bce256-0.001-50/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..09816705ef4f37ce573d81440696139727852235 --- /dev/null +++ b/t3.1/run/tf-bce256-0.001-50/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f209a41164c80ec1c79c5035447e5fbc3bb641d7ad8cdf077ca50291d532557e +size 5041261 diff --git a/t3.1/run/tf-bce256-0.001-50/result.txt b/t3.1/run/tf-bce256-0.001-50/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..ade66d56d5a3a3300b3365d144b21dd018c7b064 --- /dev/null +++ b/t3.1/run/tf-bce256-0.001-50/result.txt @@ -0,0 +1,9 @@ +Valid +0.817381489276886,0.7911657094955444,0.7021342277526855,0.7453438997268677 +0.0184298827349803,0.010762381845951085,0.007959060878621579,0.019508872553489143 +qlx +0.8181260824203491,0.7464347839355469,0.695364236831665,0.840061628818512 +0.017779600272379754,0.022063019251556383,0.06643087301229963,0.025109580813734992 +saap +0.534603762626648,0.5118828415870667,0.3856666564941406,0.597984516620636 +0.048084463424957036,0.0014294604871296225,0.001333332061767578,0.04864767292773974 diff --git a/t3.1/runthro.sh b/t3.1/runthro.sh new file mode 100644 index 0000000000000000000000000000000000000000..05afe5bf562bd5a3bb0bc2991a3672d27ccc2325 --- /dev/null +++ b/t3.1/runthro.sh @@ -0,0 +1,7 @@ +#!/bin/bash +python main.py --model mlp +python main.py --model gru +python main.py --model lstm +python main.py --model rnn +python main.py --model tf +python main.py --model mamba diff --git a/t3.1/summary.py b/t3.1/summary.py new file mode 100644 index 0000000000000000000000000000000000000000..420cc50d5a8330d07342a07c2f5bfc482023480a --- /dev/null +++ b/t3.1/summary.py @@ -0,0 +1,59 @@ +import os +import csv +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument('--dir', type=str, default='./run') +parser.add_argument('--output', type=str, default='summary.csv') +args = parser.parse_args() + +# 设置目录路径和输出CSV文件名 +directory_path = args.dir +output_csv = args.output + +# 创建CSV文件并写入数据 +with open(output_csv, mode='w', newline='') as file: + writer = csv.writer(file) + + # 写入第一个子表的标题和表头 + writer.writerow(['QLX']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 6: + means_5_6 = lines[4].strip().split(',') + std_devs_5_6 = lines[5].strip().split(',') + if len(means_5_6) == 4 and len(std_devs_5_6) == 4: + values_5_6 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_5_6, std_devs_5_6)] + writer.writerow([subdirectory] + values_5_6) + + # 加入空行以分隔两个子表 + writer.writerow([]) + + # 写入第二个子表的标题和表头 + writer.writerow(['SAAP']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 再次遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 9: + means_8_9 = lines[7].strip().split(',') + std_devs_8_9 = lines[8].strip().split(',') + if len(means_8_9) == 4 and len(std_devs_8_9) == 4: + values_8_9 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_8_9, std_devs_8_9)] + writer.writerow([subdirectory] + values_8_9) \ No newline at end of file diff --git a/t3.1/train.py b/t3.1/train.py new file mode 100644 index 0000000000000000000000000000000000000000..5f0f3ff0c2dc32000009493d29fe02c1cc5124ac --- /dev/null +++ b/t3.1/train.py @@ -0,0 +1,56 @@ +import torch +from torchmetrics import F1Score, Accuracy, AveragePrecision, AUROC + + +def train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer): + train_loss = 0 + num_labels = model.classes + avg = args.metric_avg + if num_labels == 1: + task = 'binary' + else: + task = 'multilabel' + metric_macro_acc = Accuracy(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_f1 = F1Score(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_ap = AveragePrecision(average=avg, task=task, num_labels=num_labels).to(device) + metric_auc = AUROC(average=avg, task=task, num_labels=num_labels).to(device) + + if train_loader is not None: + model.train() + for data in train_loader: + voxel, seq, gt = data + # print(seq_lengths) + out = model((voxel.to(device), seq.to(device))) + # print(out[0]) + # print(gt[0]) + loss = criterion(out, gt.to(device).float()) + # loss_0 = criterion(out[0], gt.to(device).float()) + # loss_1 = criterion(out[1], gt.to(device).float()) + # loss_2 = criterion(out[2], gt.to(device).float()) + # loss = loss_0 + loss_1 + loss_2 + loss.backward() + optimizer.step() + optimizer.zero_grad() + train_loss += loss.item() + + model.eval() + preds = [] + gt_list_valid = [] + with torch.no_grad(): + for data in valid_loader: + voxel, seq, gt = data + gt_list_valid.append(gt.to(device)) + out = model((voxel.to(device), seq.to(device))) + preds.append(out) + + # calculate metrics + preds = torch.nn.functional.sigmoid(torch.cat(preds, dim=0)) + gt_list_valid = torch.cat(gt_list_valid, dim=0).int() + + macro_ap = metric_macro_ap(preds, gt_list_valid).item() + # class_ap = [round(i.item(), 5) for i in metric_class_ap(preds, gt_list_valid)] + macro_auc = metric_auc(preds, gt_list_valid).item() + macro_f1 = metric_macro_f1(preds, gt_list_valid).item() + macro_acc = metric_macro_acc(preds, gt_list_valid).item() + return train_loss, macro_ap, macro_f1, macro_acc, macro_auc + diff --git a/t3.1/utils.py b/t3.1/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..7fd5de2ae11e3a64de85ddc8c0ce0f777a738a48 --- /dev/null +++ b/t3.1/utils.py @@ -0,0 +1,32 @@ +import random +import torch +import numpy as np + + +def set_seed(seed): + random.seed(seed) + np.random.seed(seed) + torch.manual_seed(seed) + torch.cuda.manual_seed(seed) + torch.cuda.manual_seed_all(seed) + torch.backends.cudnn.deterministic = True + torch.backends.cudnn.benchmark = False + + +def load_pretrain_model(net, weights): + net_keys = list(net.state_dict().keys()) + weights_keys = list(weights.keys()) + # assert(len(net_keys) <= len(weights_keys)) + i = 0 + j = 0 + while i < len(net_keys) and j < len(weights_keys): + name_i = net_keys[i] + name_j = weights_keys[j] + if net.state_dict()[name_i].shape == weights[name_j].shape: + net.state_dict()[name_i].copy_(weights[name_j].cpu()) + i += 1 + j += 1 + else: + i += 1 + # print i, len(net_keys), j, len(weights_keys) + return net diff --git a/t3.2.1/dataset.py b/t3.2.1/dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..0f19a4efb65400dffe5fd21af04db48fc674adfe --- /dev/null +++ b/t3.2.1/dataset.py @@ -0,0 +1,256 @@ +import os +import torch +from Bio.PDB import PDBParser, is_aa +from Bio.PDB.Polypeptide import three_to_one +from torch.utils.data import Dataset +import pandas as pd +import numpy as np +from tqdm import tqdm +from Bio.Align import PairwiseAligner + + +aligner = PairwiseAligner() +# Define the scoring system +aligner.mode = 'global' # Global alignment +aligner.match_score = 1 +aligner.mismatch_score = -1 +aligner.open_gap_score = -0.5 +aligner.extend_gap_score = -0.1 + + +def calculate_similarity(seq_gen, seq_tem): + + # Perform the alignment + score = aligner.score(seq_gen, seq_tem) + return score / len(seq_gen) + + +def parse_ll37_testset(path='metadata/LL37_v0.csv'): + files = open(path, 'r').readlines() + seqs = [] + for file in files: + seqs.append(str(file.strip().split(',')[-1].strip().upper())) + return seqs + + +def gmean(labels): + # Assuming 'labels' is a NumPy array of shape (n, m) + # Calculate the geometric mean across axis 1 + + # Take the product of elements along axis=1 + product = np.prod(labels, axis=1) + + # nth root of product, where n is the number of elements along axis=1 + geometric_means = product ** (1.0 / labels.shape[1]) + return geometric_means + + +def clamp(n, smallest, largest): + return sorted([smallest, n, largest])[1] + + +ATOMS = {'H': 1, 'C': 12, 'N': 14, 'O': 16, 'S': 30} +ATOMS_R = {'H': 1, 'C': 1.5, 'N': 1.5, 'O': 1.5, 'S': 2} +AMINO_ACID_WATER = {'A': 255, 'V': 255, 'P': 255, 'F': 255, 'W': 255, 'I': 255, 'L': 255, 'G': 155, 'M': 155, + 'Y': 55, 'S': 55, 'T': 55, 'C': 55, 'N': 55, 'Q': 55, 'D': 55, 'E': 55, 'K': 55, 'R': 55, 'H': 55} +AMINO_ACID_CHARGE = {'D': 55, 'E': 55, 'A': 155, 'V': 155, 'P': 155, 'F': 155, 'W': 155, 'I': 155, 'L': 155, 'G': 155, + 'M': 155, 'Y': 155, 'S': 155, 'T': 155, 'C': 155, 'N': 155, 'Q': 155, 'K': 255, 'R': 255, 'H': 255} +# AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, +# 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19} +AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, + 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19, 'X': 21} + + +def pdb_parser(structure): + """ + + """ + voxel = np.zeros((4, 64, 64, 64), dtype=np.int8) + id = '' + seq_str = '' + for i in structure[0]: + id = i.id + chain = structure[0][id] + for res in chain: + if is_aa(res.get_resname(), standard=True): + resname = res.get_resname() + amino = three_to_one(resname) + seq_str += str(amino) + ATOM_WATER = AMINO_ACID_WATER[amino] + ATOM_CHARGE = AMINO_ACID_CHARGE[amino] + ATOM_CATEGORY = AMAs[amino] * 20 + + for i in res: + if i.id not in ATOMS.keys(): + continue + x, y, z = i.get_coord() + if abs(x) > 32: + x = clamp(x, -31, 31) + if abs(y) > 32: + y = clamp(x, -31, 31) + if abs(z) > 32: + z = clamp(x, -31, 31) + x_i, y_i, z_i = int(x) + 32, int(y) + 32, int(z) + 32 + ATOM_WEIGHT = ATOMS[i.id] + ATOM_R = ATOMS_R[i.id] + + if ATOM_R <= 1.5: + voxel[0, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WEIGHT + voxel[1, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WATER + voxel[2, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CHARGE + voxel[3, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CATEGORY + else: + voxel[0, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WEIGHT + voxel[1, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WATER + voxel[2, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CHARGE + voxel[3, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CATEGORY + return voxel + + +class MDataset(Dataset): + def __init__(self, threshold=32, mode='train', max_length=50, pdb_src='af', data_ver='0922'): + self.num_classes = 6 + exclude_list = pd.read_csv('../metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() + exclude_filter = False + + p = PDBParser(QUIET=True) + + if mode == 'train': + all_data = pd.read_csv(f'../metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values + exclude_filter = True + elif mode == 'qlx': + all_data = pd.read_csv('../metadata/data_qlx.csv', encoding="unicode_escape").values + elif mode == 'saap': + all_data = pd.read_csv('../metadata/data_saap.csv', encoding="unicode_escape").values + else: + raise NotImplementedError + idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) + labels = (labels < threshold).astype(int) + + filter_idx_list = [] + seq_new_list = [] + label_list = [] + for idx in range(len(idx_list)): + seq = seq_list[idx].upper().strip() + if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: + continue + if exclude_filter: + if seq in exclude_list: + continue + + filter_idx_list.append(idx) + seq_new_list.append(seq) + label_list.append(labels[idx]) + + if pdb_src == 'af': + pdb_root = '../pdb/pdb_af/' + elif pdb_src == 'hf': + pdb_root = '../pdb/pdb_dbassp/' + else: + raise NotImplementedError + self.data_list = [] + for i in tqdm(range(len(filter_idx_list))): + idx = filter_idx_list[i] + seq = seq_new_list[i] + label = label_list[i] + + if os.path.exists(pdb_root + seq + ".pdb"): + pdb_path = pdb_root + seq + ".pdb" + else: + print(f'lacking pdb file {seq}') + continue + # raise FileNotFoundError + + structure = p.get_structure(idx, pdb_path) + voxel = pdb_parser(structure) + seq_emb = [AMAs[char] for char in seq] + [0] * (max_length - len(seq)) + self.data_list.append((voxel, seq_emb, label)) + + def __getitem__(self, idx): + voxel, seq_emb, gt = self.data_list[idx] + + return torch.Tensor(voxel).float(), torch.Tensor(seq_emb), torch.Tensor(gt) + + def __len__(self): + return len(self.data_list) + + +# class SDataset(Dataset): +# ''' +# x: Features. +# y: Targets, if none, do prediction. +# ''' + +# def __init__(self, threshold=32, mode='train', task='0', max_length=50, pdb_src='af'): +# self.num_classes = 1 +# self.task = int(task) +# exclude_list = pd.read_csv('metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() +# exclude_filter = False + +# p = PDBParser(QUIET=True) + +# if mode == 'train': +# all_data = pd.read_csv('metadata/data_0922_i.csv', encoding="unicode_escape").values +# exclude_filter = True +# elif mode == 'qlx': +# all_data = pd.read_csv('metadata/data_qlx.csv', encoding="unicode_escape").values +# elif mode == 'saap': +# all_data = pd.read_csv('metadata/data_saap.csv', encoding="unicode_escape").values +# else: +# raise NotImplementedError +# idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) +# labels = (labels[:, self.task:self.task+1] <= threshold).astype(int) + +# filter_idx_list = [] +# seq_new_list = [] +# label_list = [] +# for idx in range(len(idx_list)): +# seq = seq_list[idx].upper().strip() +# if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: +# continue +# if exclude_filter: +# if seq in exclude_list: +# continue + +# filter_idx_list.append(idx) +# seq_new_list.append(seq) +# label_list.append(labels[idx]) + +# if pdb_src == 'af': +# pdb_root = './pdb/pdb_af/' +# elif pdb_src == 'hf': +# pdb_root = './pdb/pdb_dbassp/' +# else: +# raise NotImplementedError +# self.data_list = [] +# for i in tqdm(range(len(filter_idx_list))): +# idx = filter_idx_list[i] +# seq = seq_new_list[i] +# label = label_list[i] + +# # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"): +# # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb" +# # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"): +# # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb" +# if os.path.exists(pdb_root + seq + ".pdb"): +# pdb_path = pdb_root + seq + ".pdb" +# else: +# continue +# # raise FileNotFoundError + +# structure = p.get_structure(idx, pdb_path) +# voxel = pdb_parser(structure) +# seq_emb = [AMAs[char] for char in seq] + [0] * (max_length - len(seq)) +# self.data_list.append((voxel, seq_emb, label)) + +# def __getitem__(self, idx): +# voxel, seq_emb, gt = self.data_list[idx] + +# return torch.Tensor(voxel).float(), torch.Tensor(seq_emb), torch.Tensor(gt) + +# def __len__(self): +# return len(self.data_list) \ No newline at end of file diff --git a/t3.2.1/eval.py b/t3.2.1/eval.py new file mode 100644 index 0000000000000000000000000000000000000000..18f38bfff4c7b7f9bf2fef9834b96c07a96d947d --- /dev/null +++ b/t3.2.1/eval.py @@ -0,0 +1,203 @@ +import argparse +import json +import logging +import os +import time + +from dataset import SDataset, MDataset +from network import MMPeptide, SEQPeptide, VoxPeptide, MMFPeptide, SMPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=64, + help="MIC threshold for determine labels (default: 64)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=128, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.02, + help='learning rate (default: 0.002)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='micro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='mlce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + weight_dir = "./run/" + args.task_type + '-' + args.task + "-m-" + args.model + '-' + args.loss + str(args.batch_size) + str(args.lr) + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + raise ValueError + + logging.basicConfig(handlers=[ + # logging.FileHandler(filename=os.path.join(weight_dir, "eval.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + # with open(os.path.join(weight_dir, "config.json"), "w") as f: + # f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src) + else: + set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src) + else: + qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + # train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + + # train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=1, shuffle=False) + if args.model == 'seq': + model = SEQPeptide(classes=set_all.num_classes, q_encoder='mlp') + elif args.model == 'voxel': + model = VoxPeptide(classes=set_all.num_classes) + elif args.model == 'mm': + # model = MMPeptide(classes=train_set.num_classes, q_encoder='tf', ) # attention='hamburger' + # model = SMPeptide(classes=train_set.num_classes, q_encoder='mlp', max_length=30) # attention='hamburger' + model = MMPeptide(classes=set_all.num_classes, q_encoder='mlp', max_length=args.max_length)# , attention='hamburger' + elif args.model == 'mmf': + model = MMFPeptide(classes=set_all.num_classes, q_encoder='mlp', max_length=args.max_length) # attention='hamburger' + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + model.load_state_dict(torch.load(weights_path)) + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + + + if args.task_type in ('mlc', 'slc') : + _, ap, f1, acc, auc = train(args, None, model, None, valid_loader, device, None, None) + logging.info(f'ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, None, model, None, qlx_loader, device, None, None) + logging.info(f'QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, None, model, None, saap_loader, device, None, None) + logging.info(f'SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/eval.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/t3.2.1/gen_simi.py b/t3.2.1/gen_simi.py new file mode 100644 index 0000000000000000000000000000000000000000..c350b6cbccbe8e4884ea3bbbfd150eb1a66b348b --- /dev/null +++ b/t3.2.1/gen_simi.py @@ -0,0 +1,38 @@ +import pandas as pd +from dataset import calculate_similarity + +# 读取模板 CSV 文件和目标 CSV 文件 +template_file = './metadata/data_test.csv' # 模板CSV文件路径 +target_file = './metadata/data_0920_i.csv' # 目标CSV文件路径 + +template_df = pd.read_csv(template_file) +target_df = pd.read_csv(target_file) + +# 假设我们要使用 'template_column' 列作为模板中的列,'target_column' 列作为目标表格中的列 +template_column = 'Seq' # 替换为你的模板表格列名 +target_column = 'Seq' # 替换为你的目标表格列名 + +# 创建一个空的 DataFrame,用于存储符合条件的行,并复制目标表格的表头 +extracted_rows = pd.DataFrame(columns=target_df.columns) + +# 遍历目标表格的每一行 +for index, target_row in target_df.iterrows(): + target_value = target_row[target_column] + + # 遍历模板表格的每个值 + for template_value in template_df[template_column]: + + # 计算指标 + metric = calculate_similarity(target_value.strip().upper(), template_value.strip().upper()) + + # 如果指标大于0.3,则提取目标表格的该行 + if metric > 0.3: + extracted_rows = pd.concat([extracted_rows, pd.DataFrame([target_row])], ignore_index=True) + # 只要找到一个匹配值,就可以继续检查下一行 + break + +# 将提取的行保存到一个新的CSV文件中,确保表头与目标表格一致 +output_file = './metadata/data_simi.csv' +extracted_rows.to_csv(output_file, index=False) + +print(f"符合条件的行已保存到 {output_file}") \ No newline at end of file diff --git a/t3.2.1/infer.py b/t3.2.1/infer.py new file mode 100644 index 0000000000000000000000000000000000000000..a68399ba45be5657b2aa7a7c68982fba2a5660c9 --- /dev/null +++ b/t3.2.1/infer.py @@ -0,0 +1,132 @@ +import argparse +import json +import logging +import os +import time +import pandas as pd + +from dataset import MDataset +from network import VoxPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mlp', + help='resnet34 resnet50 densenet convnext vit swintf') +parser.add_argument('--in-channels', dest='in_channels', type=int, default=4) +parser.add_argument('--channels', type=int, default=16) +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--resnet', type=str, default='./run/resnet3416-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--densenet', type=str, default='./run/densenet16-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--convnext', type=str, default='./run/convnext16-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--vit', type=str, default='./run/vit16-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--swintf', type=str, default='./run/swintf16-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + +model_path = {'resnet': args.resnet, 'densenet': args.densenet, 'convnext': args.convnext, 'vit': args.vit, 'swintf': args.swintf} + + +def main(): + set_seed(args.seed) + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + results = pd.DataFrame() + + logging.info('Loading Test Dataset') + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + + models = {'resnet': VoxPeptide(classes=qlx_set.num_classes, v_encoder='resnet34', channels=args.channels, in_channels=args.in_channels), + 'densenet': VoxPeptide(classes=qlx_set.num_classes, v_encoder='densenet', channels=args.channels, in_channels=args.in_channels), + 'convnext': VoxPeptide(classes=qlx_set.num_classes, v_encoder='convnext', channels=args.channels, in_channels=args.in_channels), + 'vit': VoxPeptide(classes=qlx_set.num_classes, v_encoder='vit', channels=args.channels, in_channels=args.in_channels), + 'swintf': VoxPeptide(classes=qlx_set.num_classes, v_encoder='swintf', channels=args.channels, in_channels=args.in_channels)} + + for model_name in model_path.keys(): + model = models[model_name] + pred_all = [] + gt_all = [] + for i in range(1,6): + model.load_state_dict(torch.load(os.path.join(model_path[model_name], f'model_{i}.pth'))) + model.to(device).eval() + with torch.no_grad(): + for data in qlx_loader: + voxel, seq, gt = data + gt_all.append(gt.to(device)) + out = model((voxel.to(device), seq.to(device))) + pred_all.append(out) + pred_all = torch.nn.functional.sigmoid(torch.cat(pred_all, dim=0)).squeeze().cpu().numpy() + gt_all = torch.cat(gt_all, dim=0).int().squeeze().cpu().numpy() + results[model_name] = pred_all.ravel(order='F') + + results['gt'] = gt_all.ravel(order='F') + + results.to_csv("preds.csv", index=False) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/t3.2.1/infert4.3.py b/t3.2.1/infert4.3.py new file mode 100644 index 0000000000000000000000000000000000000000..4baa226cc4518f970963678a1f8d2bd65291e37b --- /dev/null +++ b/t3.2.1/infert4.3.py @@ -0,0 +1,129 @@ +import argparse +import json +import logging +import os +import time +import pandas as pd + +from dataset import MDataset +from network import VoxPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mlp', + help='resnet34 resnet50 densenet convnext vit swintf') +parser.add_argument('--in-channels', dest='in_channels', type=int, default=4) +parser.add_argument('--channels', type=int, default=16) +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--resnet34_16', type=str, default='./run/resnet3416-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--resnet34_32', type=str, default='./run/resnet3432-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--resnet50_16', type=str, default='./run/resnet5016-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--resnet50_32', type=str, default='./run/resnet5032-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + +model_path = {'resnet34@16': args.resnet34_16, 'resnet34@32': args.resnet34_32, 'resnet50@16': args.resnet50_16, 'resnet50@32': args.resnet50_32} + + +def main(): + set_seed(args.seed) + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + results = pd.DataFrame() + + logging.info('Loading Test Dataset') + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + + models = {'resnet34@16': VoxPeptide(classes=qlx_set.num_classes, v_encoder='resnet34', channels=16, in_channels=args.in_channels), + 'resnet34@32': VoxPeptide(classes=qlx_set.num_classes, v_encoder='resnet34', channels=32, in_channels=args.in_channels), + 'resnet50@16': VoxPeptide(classes=qlx_set.num_classes, v_encoder='resnet50', channels=16, in_channels=args.in_channels), + 'resnet50@32': VoxPeptide(classes=qlx_set.num_classes, v_encoder='resnet50', channels=32, in_channels=args.in_channels)} + + for model_name in model_path.keys(): + model = models[model_name] + pred_all = [] + gt_all = [] + for i in range(1,6): + model.load_state_dict(torch.load(os.path.join(model_path[model_name], f'model_{i}.pth'))) + model.to(device).eval() + with torch.no_grad(): + for data in qlx_loader: + voxel, seq, gt = data + gt_all.append(gt.to(device)) + out = model((voxel.to(device), seq.to(device))) + pred_all.append(out) + pred_all = torch.nn.functional.sigmoid(torch.cat(pred_all, dim=0)).squeeze().cpu().numpy() + gt_all = torch.cat(gt_all, dim=0).int().squeeze().cpu().numpy() + results[model_name] = pred_all.ravel(order='F') + + results['gt'] = gt_all.ravel(order='F') + + results.to_csv("preds.csv", index=False) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/t3.2.1/loss.py b/t3.2.1/loss.py new file mode 100644 index 0000000000000000000000000000000000000000..d2c1cabdc7bb5df53b8513344372ea557b89aa29 --- /dev/null +++ b/t3.2.1/loss.py @@ -0,0 +1,123 @@ +import torch +from torch import nn +import torch.nn.functional as F +from math import cos, pi, sin +import math +import numpy as np +from scipy.special import lambertw + + +def mixup_criterion(criterion, pred, y_a, y_b, lam, pow=2): + y = lam ** pow * y_a + (1 - lam) ** pow * y_b + return criterion(pred, y) + + +def mixup_data(v, q, a): + '''Returns mixed inputs, pairs of targets, and lambda without organ constraint''' + lam = np.random.beta(1, 1) + + batch_size = v.shape[0] + index = torch.randperm(batch_size) + + mixed_v = lam * v + (1 - lam) * v[index, :] + mixed_q = lam * q + (1 - lam) * q[index, :] + + a_1, a_2 = a, a[index] + return mixed_v, mixed_q, a_1, a_2, lam + + +def linear(epoch, nepoch): + return 1 - epoch / nepoch + + +def convex(epoch, nepoch): + return epoch / (2 - nepoch) + + +def concave(epoch, nepoch): + return 1 - sin((epoch / nepoch) * (pi / 2)) + + +def composite(epoch, nepoch): + return 0.5 * cos((epoch / nepoch) * pi) + 0.5 + + +class LogCoshLoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y_t, y_prime_t): + ey_t = y_t - y_prime_t + return torch.mean(torch.log(torch.cosh(ey_t + 1e-12)))+F.mse_loss(y_t, y_prime_t) + + +class MLCE(nn.Module): + def __init__(self): + super(MLCE, self).__init__() + + def _mlcce(self, y_pred, y_true): + y_pred = (1 - 2 * y_true) * y_pred + y_pred_neg = y_pred - y_true * 1e12 + y_pred_pos = y_pred - (1 - y_true) * 1e12 + zeros = torch.zeros_like(y_pred[..., :1]) + y_pred_neg = torch.cat([y_pred_neg, zeros], dim=-1) + y_pred_pos = torch.cat([y_pred_pos, zeros], dim=-1) + neg_loss = torch.logsumexp(y_pred_neg, dim=-1) + pos_loss = torch.logsumexp(y_pred_pos, dim=-1) + loss = torch.mean(neg_loss + pos_loss) + return loss + + def __call__(self, y_pred, y_true): + return self._mlcce(y_pred, y_true) + + +class SuperLoss(nn.Module): + def __init__(self, C=10, lam=1, batch_size=128): + super(SuperLoss, self).__init__() + self.tau = math.log(C) + self.lam = lam # set to 1 for CIFAR10 and 0.25 for CIFAR100 + self.batch_size = batch_size + + def forward(self, logits, targets): + l_i = F.mse_loss(logits, targets, reduction='none').detach() + sigma = self.sigma(l_i) + loss = (F.mse_loss(logits, targets, reduction='none') - self.tau) * sigma + self.lam * ( + torch.log(sigma) ** 2) + loss = loss.sum() / self.batch_size + return loss + + def sigma(self, l_i): + x = torch.ones(l_i.size()) * (-2 / math.exp(1.)) + x = x.cuda() + y = 0.5 * torch.max(x, (l_i - self.tau) / self.lam) + y = y.cpu().numpy() + sigma = np.exp(-lambertw(y)) + sigma = sigma.real.astype(np.float32) + sigma = torch.from_numpy(sigma).cuda() + return sigma + + +def unbiased_curriculum_loss(out, data, args, epoch, epochs, scheduler='linear'): + losses = [] + scheduler = linear if scheduler == 'linear' else concave + + # calculate difficulty measurement function + adjusted_losses = [] + for idx in range(out.shape[0]): + ground_truth = max(1, abs(data[idx].item())) + loss = F.mse_loss(out[idx], data[idx]) + losses.append(loss) + adjusted_losses.append(loss.item() / ground_truth) + + mean_loss, std_loss = np.mean(adjusted_losses), np.std(adjusted_losses) + + # re-weight losses + total_loss = 0 + for i, loss in enumerate(losses): + if adjusted_losses[i] > mean_loss + 1 * std_loss: + schedule_factor = scheduler(epoch, args.epochs) + total_loss += schedule_factor * loss + else: + total_loss += loss + + return total_loss diff --git a/t3.2.1/main.py b/t3.2.1/main.py new file mode 100644 index 0000000000000000000000000000000000000000..21bab214ec6630af9a711eedf80fb5670aa6b7bd --- /dev/null +++ b/t3.2.1/main.py @@ -0,0 +1,232 @@ +import argparse +import json +import logging +import os +import time + +from dataset import MDataset +from network import VoxPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mlp', + help='resnet34 resnet50 densenet convnext vit swintf') +parser.add_argument('--in-channels', dest='in_channels', type=int, default=4) +parser.add_argument('--channels', type=int, default=16) +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + if args.loss == 'mlce' and args.task_type != 'slc': + criterion = MLCE() + elif args.loss == "bce" or args.task_type == 'slc': + args.loss = "bce" + criterion = nn.BCEWithLogitsLoss() + else: + criterion = 0 + pass + weight_dir = "./run/" + args.model + str(args.channels) + '-' + args.loss + str(args.batch_size) + '-' + str(args.lr) + '-' + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + os.makedirs(weight_dir) + + logging.basicConfig(handlers=[ + logging.FileHandler(filename=os.path.join(weight_dir, "training.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + with open(os.path.join(weight_dir, "config.json"), "w") as f: + f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src, data_ver=args.data_ver) + # else: + # set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src) + # else: + # qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + # saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + # if args.loss == 'bce': + # weight = torch.Tensor([0] * args.classes) + # for i in train_set: + # weight += i[2] + # weight = (len(train_set) - weight) / weight + # criterion.register_buffer('pos_weight', weight.to(device)) + + train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=1, shuffle=False) + + model = VoxPeptide(classes=set_all.num_classes, v_encoder=args.model, channels=args.channels, in_channels=args.in_channels) + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + # optimizer = torch.optim.Adam(model.parameters()) + optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr) + # optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=True, weight_decay=5e-5) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Train set:{len(train_set)}, Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + best_metric = 0 + best_qlx = 0 + best_saap = 0 + start_time = time.time() + + for epoch in range(1, args.epochs + 1): + if args.task_type in ('mlc', 'slc') : + train_loss, ap, f1, acc, auc = train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer) + logging.info(f'Epoch: {epoch:03d}, Train Loss: {train_loss:.3f}, ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + avg_metric = ap + f1 + acc + auc + if avg_metric > best_metric: + logging.info(f'Epoch: {epoch:03d} New Best validation metrics, running test session') + torch.save(model.state_dict(), weights_path) + best_metric = avg_metric + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, epoch, model, None, qlx_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_metric = qlx_ap + qlx_f1 + qlx_acc + qlx_auc + if qlx_metric > best_qlx: + best_qlx = qlx_metric + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, epoch, model, None, saap_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_metric = saap_ap + saap_f1 + saap_acc + saap_auc + if saap_metric > best_saap: + best_saap = saap_metric + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'used time {(time.time()-start_time)/3600:.2f}h') + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/result.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/t3.2.1/network.py b/t3.2.1/network.py new file mode 100644 index 0000000000000000000000000000000000000000..c643a89208689e9aad367f813c7c4bd5f4c4d9cc --- /dev/null +++ b/t3.2.1/network.py @@ -0,0 +1,1338 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F +from math import ceil +from mamba_ssm import Mamba + + +class Hamburger(nn.Module): + def __init__(self, inp, oup, reduction=32): + super(Hamburger, self).__init__() + self.pool_h = nn.AdaptiveAvgPool3d((1, None, None)) + self.pool_w = nn.AdaptiveAvgPool3d((None, 1, None)) + self.pool_d = nn.AdaptiveAvgPool3d((None, None, 1)) + + mip = max(8, inp // reduction) + self.conv1 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.conv2 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.conv3 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.gn1 = nn.GroupNorm(8, mip) + self.gn2 = nn.GroupNorm(8, mip) + self.gn3 = nn.GroupNorm(8, mip) + + self.act = nn.LeakyReLU(0.2) + self.conv_h = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + self.conv_w = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + self.conv_d = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + + def forward(self, x): + n, c, h, w, d = x.size() + x_h = self.pool_h(x) + # print(x_h.shape) + x_w = self.pool_w(x).permute(0, 1, 3, 2, 4) + # print(x_w.shape) + x_d = self.pool_d(x).permute(0, 1, 4, 2, 3) + # print(x_d.shape) + y_hwd = torch.cat([x_h, x_w, x_d], dim=2) + # y_hd = torch.cat([x_h, x_d], dim=2) + # y_dw = torch.cat([x_d, x_w], dim=2) + y_hwd = self.conv1(y_hwd) + # y_hd = self.conv2(y_hd) + # y_dw = self.conv3(y_dw) + y_hwd = self.gn1(y_hwd) + # y_hd = self.gn2(y_hd) + # y_dw = self.gn3(y_dw) + y_hwd = self.act(y_hwd) + # y_hd = self.act(y_hd) + # y_dw = self.act(y_dw) + # print(y_hwd.shape) + x_h, x_w, x_d = torch.split(y_hwd, [1, 1, 1], dim=2) + x_w = x_w + x_h = x_h.permute(0, 1, 3, 2, 4) + x_d = x_d.permute(0, 1, 3, 4, 2) + a_h = self.conv_h(x_h).sigmoid() + a_w = self.conv_w(x_w).sigmoid() + a_d = self.conv_d(x_d).sigmoid() + a_hw = a_w * a_h + out = a_hw * a_d + return out + x + + +class BasicBlock3D(nn.Module): + expansion = 1 + + def __init__(self, in_channels, out_channels, stride=1, downsample=None): + super(BasicBlock3D, self).__init__() + self.conv1 = nn.Conv3d(in_channels, out_channels, kernel_size=3, + stride=stride, padding=1, bias=False) + self.bn1 = nn.BatchNorm3d(out_channels) + self.relu = nn.ReLU(inplace=True) + self.conv2 = nn.Conv3d(out_channels, out_channels, kernel_size=3, + stride=1, padding=1, bias=False) + self.bn2 = nn.BatchNorm3d(out_channels) + self.downsample = downsample + self.stride = stride + + def forward(self, x): + identity = 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: + identity = self.downsample(x) + + out += identity + out = self.relu(out) + + return out + +class Bottleneck3D(nn.Module): + expansion = 4 + + def __init__(self, in_channels, out_channels, stride=1, downsample=None): + super(Bottleneck3D, self).__init__() + self.conv1 = nn.Conv3d(in_channels, out_channels, kernel_size=1, bias=False) + self.bn1 = nn.BatchNorm3d(out_channels) + self.conv2 = nn.Conv3d(out_channels, out_channels, kernel_size=3, + stride=stride, padding=1, bias=False) + self.bn2 = nn.BatchNorm3d(out_channels) + self.conv3 = nn.Conv3d(out_channels, out_channels * self.expansion, + kernel_size=1, bias=False) + self.bn3 = nn.BatchNorm3d(out_channels * self.expansion) + self.relu = nn.ReLU(inplace=True) + self.downsample = downsample + self.stride = stride + + def forward(self, x): + identity = 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: + identity = self.downsample(x) + + out += identity + out = self.relu(out) + + return out + +class ResNet3D(nn.Module): + def __init__(self, block, layers, input_channels=4, base_channels=16, feature_dim=512): + super(ResNet3D, self).__init__() + self.in_channels = base_channels + + self.conv1 = nn.Conv3d(input_channels, base_channels, kernel_size=7, + stride=2, padding=3, bias=False) + self.bn1 = nn.BatchNorm3d(base_channels) + self.relu = nn.ReLU(inplace=True) + self.maxpool = nn.MaxPool3d(kernel_size=3, stride=2, padding=1) + + # 每个layer的通道数是基于 base_channels 乘以扩展因子 + self.layer1 = self._make_layer(block, base_channels, layers[0]) + self.layer2 = self._make_layer(block, base_channels * 2, layers[1], stride=2) + self.layer3 = self._make_layer(block, base_channels * 4, layers[2], stride=2) + self.layer4 = self._make_layer(block, base_channels * 8, layers[3], stride=2) + + self.avgpool = nn.AdaptiveAvgPool3d((1, 1, 1)) + self.fc = nn.Linear(base_channels * 8 * block.expansion, feature_dim) + + # 初始化权重 + self._initialize_weights() + + def _make_layer(self, block, out_channels, blocks, stride=1): + downsample = None + if stride != 1 or self.in_channels != out_channels * block.expansion: + downsample = nn.Sequential( + nn.Conv3d(self.in_channels, out_channels * block.expansion, + kernel_size=1, stride=stride, bias=False), + nn.BatchNorm3d(out_channels * block.expansion), + ) + + layers = [] + layers.append(block(self.in_channels, out_channels, stride, downsample)) + self.in_channels = out_channels * block.expansion + for _ in range(1, blocks): + layers.append(block(self.in_channels, out_channels)) + + return nn.Sequential(*layers) + + def _initialize_weights(self): + for m in self.modules(): + if isinstance(m, nn.Conv3d): + nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') + elif isinstance(m, (nn.BatchNorm3d, nn.GroupNorm)): + nn.init.constant_(m.weight, 1) + nn.init.constant_(m.bias, 0) + elif isinstance(m, nn.Linear): + nn.init.normal_(m.weight, 0, 0.01) + nn.init.constant_(m.bias, 0) + + def forward(self, x): + # Input shape: (B, C, D, H, W) + x = self.conv1(x) + x = self.bn1(x) + x = self.relu(x) + x = self.maxpool(x) + + x = self.layer1(x) # -> (B, C, D/4, H/4, W/4) + x = self.layer2(x) # -> (B, 2C, D/8, H/8, W/8) + x = self.layer3(x) # -> (B, 4C, D/16, H/16, W/16) + x = self.layer4(x) # -> (B, 8C, D/32, H/32, W/32) + + x = self.avgpool(x) # -> (B, 8C, 1, 1, 1) + x = torch.flatten(x, 1) # -> (B, 8C) + x = self.fc(x) # -> (B, feature_dim) + + return x + +def ResNet3D34(input_channels=4, base_channels=16, feature_dim=512): + """Constructs a ResNet-34 3D model.""" + return ResNet3D(BasicBlock3D, [3, 4, 6, 3], + input_channels=input_channels, + base_channels=base_channels, + feature_dim=feature_dim) + +def ResNet3D50(input_channels=4, base_channels=16, feature_dim=512): + """Constructs a ResNet-50 3D model.""" + return ResNet3D(Bottleneck3D, [3, 4, 6, 3], + input_channels=input_channels, + base_channels=base_channels, + feature_dim=feature_dim) + + +class DenseLayer3D(nn.Module): + """DenseNet3D 的基本层,包括批归一化、激活和卷积操作""" + def __init__(self, in_channels, growth_rate, bn_size=4, drop_rate=0.0): + super(DenseLayer3D, self).__init__() + self.bn1 = nn.BatchNorm3d(in_channels) + self.relu = nn.ReLU(inplace=True) + self.conv1 = nn.Conv3d(in_channels, bn_size * growth_rate, kernel_size=1, stride=1, bias=False) + + self.bn2 = nn.BatchNorm3d(bn_size * growth_rate) + self.conv2 = nn.Conv3d(bn_size * growth_rate, growth_rate, kernel_size=3, stride=1, padding=1, bias=False) + + self.drop_rate = drop_rate + + def forward(self, x): + out = self.bn1(x) + out = self.relu(out) + out = self.conv1(out) + + out = self.bn2(out) + out = self.relu(out) + out = self.conv2(out) + + if self.drop_rate > 0: + out = F.dropout3d(out, p=self.drop_rate, training=self.training) + + # 将输入和输出在通道维度上拼接 + out = torch.cat([x, out], 1) + return out + + +class DenseBlock3D(nn.Module): + """由多个 DenseLayer3D 组成的 DenseBlock""" + def __init__(self, num_layers, in_channels, growth_rate, bn_size=4, drop_rate=0.0): + super(DenseBlock3D, self).__init__() + layers = [] + for i in range(num_layers): + layers.append(DenseLayer3D( + in_channels + i * growth_rate, + growth_rate, + bn_size=bn_size, + drop_rate=drop_rate + )) + self.layer = nn.Sequential(*layers) + + def forward(self, x): + return self.layer(x) + + +class Transition3D(nn.Module): + """用于减少特征图的尺寸和通道数的过渡层""" + def __init__(self, in_channels, out_channels): + super(Transition3D, self).__init__() + self.bn = nn.BatchNorm3d(in_channels) + self.relu = nn.ReLU(inplace=True) + self.conv = nn.Conv3d(in_channels, out_channels, kernel_size=1, stride=1, bias=False) + self.pool = nn.AvgPool3d(kernel_size=2, stride=2) + + def forward(self, x): + out = self.bn(x) + out = self.relu(out) + out = self.conv(out) + out = self.pool(out) + return out + + +class DenseNet3D(nn.Module): + """DenseNet3D 模型""" + def __init__(self, input_channels=1, base_channels=64, growth_rate=32, block_layers=[3, 6, 12, 8], + bn_size=4, drop_rate=0.0, feature_dim=1024): + super(DenseNet3D, self).__init__() + self.growth_rate = growth_rate + + # 初始卷积层 + self.features = nn.Sequential( + nn.Conv3d(input_channels, base_channels, kernel_size=7, stride=2, padding=3, bias=False), + nn.BatchNorm3d(base_channels), + nn.ReLU(inplace=True), + nn.MaxPool3d(kernel_size=3, stride=2, padding=1) + ) + + # Dense Blocks 和 Transition Layers + num_features = base_channels + self.block_layers = [] + self.num_blocks = len(block_layers) + self.dense_blocks = nn.ModuleList() + self.trans_blocks = nn.ModuleList() + + for i, num_layers in enumerate(block_layers): + dense_block = DenseBlock3D( + num_layers=num_layers, + in_channels=num_features, + growth_rate=growth_rate, + bn_size=bn_size, + drop_rate=drop_rate + ) + self.dense_blocks.append(dense_block) + num_features = num_features + num_layers * growth_rate + + if i != self.num_blocks - 1: + trans_block = Transition3D( + in_channels=num_features, + out_channels=num_features // 2 + ) + self.trans_blocks.append(trans_block) + num_features = num_features // 2 + + # 最后一个 batch norm + self.bn_final = nn.BatchNorm3d(num_features) + self.relu_final = nn.ReLU(inplace=True) + + # 全局平均池化 + self.global_pool = nn.AdaptiveAvgPool3d((1, 1, 1)) + + # 分类头 + self.classifier = nn.Sequential( + nn.Flatten(), + nn.Linear(num_features, feature_dim) + ) + + # 初始化权重 + self._initialize_weights() + + def forward(self, x): + out = self.features(x) + + for i in range(self.num_blocks): + out = self.dense_blocks[i](out) + if i < self.num_blocks - 1: + out = self.trans_blocks[i](out) + + out = self.bn_final(out) + out = self.relu_final(out) + + out = self.global_pool(out) # (B, C, 1, 1, 1) + out = out.view(out.size(0), -1) # (B, C) + out = self.classifier(out) # (B, feature_dim) + return out + + def _initialize_weights(self): + """初始化权重""" + for m in self.modules(): + if isinstance(m, nn.Conv3d): + nn.init.kaiming_normal_(m.weight) + elif isinstance(m, nn.BatchNorm3d): + nn.init.constant_(m.weight, 1) + nn.init.constant_(m.bias, 0) + elif isinstance(m, nn.Linear): + nn.init.normal_(m.weight, 0, 0.01) + nn.init.constant_(m.bias, 0) + + +class DepthwiseConv3D(nn.Module): + """深度可分离卷积(3D)""" + def __init__(self, dim, kernel_size=7, padding=3): + super(DepthwiseConv3D, self).__init__() + self.dw_conv = nn.Conv3d(dim, dim, kernel_size=kernel_size, padding=padding, groups=dim, bias=False) + + def forward(self, x): + return self.dw_conv(x) + + +class ConvNeXtBlock3D(nn.Module): + """ConvNeXt 基本块(3D)""" + def __init__(self, dim, drop_path=0.0): + super(ConvNeXtBlock3D, self).__init__() + self.depthwise_conv = DepthwiseConv3D(dim) + self.norm = nn.LayerNorm(dim, eps=1e-6) + self.pointwise_conv1 = nn.Linear(dim, 4 * dim) + self.act = nn.GELU() + self.pointwise_conv2 = nn.Linear(4 * dim, dim) + self.drop_path = nn.Identity() if drop_path == 0.0 else nn.Dropout(drop_path) + + def forward(self, x): + # 输入x形状: (B, C, D, H, W) + residual = x + x = self.depthwise_conv(x) + # 转换为 (B, D, H, W, C) 以应用 LayerNorm 和 Linear + x = x.permute(0, 2, 3, 4, 1) + x = self.norm(x) + x = self.pointwise_conv1(x) + x = self.act(x) + x = self.pointwise_conv2(x) + x = self.drop_path(x) + # 转换回 (B, C, D, H, W) + x = x.permute(0, 4, 1, 2, 3) + x = residual + x # 残差连接 + return x + + +class DownSample3D(nn.Module): + """下采样层(3D)""" + def __init__(self, in_channels, out_channels): + super(DownSample3D, self).__init__() + self.layer = nn.Sequential( + nn.LayerNorm(in_channels, eps=1e-6), + nn.Conv3d(in_channels, out_channels, kernel_size=2, stride=2) + ) + + def forward(self, x): + # 输入x形状: (B, C, D, H, W) + # 需要在 LayerNorm 之前转换为 (B, D, H, W, C) + x = x.permute(0, 2, 3, 4, 1) + x = self.layer[0](x) # LayerNorm + x = x.permute(0, 4, 1, 2, 3) # 转回 (B, C, D, H, W) + x = self.layer[1](x) # Conv3d 下采样 + return x + + +class ConvNeXt3D(nn.Module): + """ConvNeXt 模型(3D)""" + def __init__(self, input_channels=3, base_channels=96, feature_dim=1024, + depths=[3, 3, 9, 3], drop_path_rate=0.1, layer_scale_init_value=1e-6): + super(ConvNeXt3D, self).__init__() + self.num_stages = len(depths) + self.drop_path_rate = drop_path_rate + + # 计算每个阶段的通道数 + self.dims = [base_channels * (2 ** i) for i in range(self.num_stages)] + + # Stem 层:卷积下采样 + self.stem = nn.Sequential( + nn.Conv3d(input_channels, self.dims[0], kernel_size=4, stride=4), + nn.LayerNorm(self.dims[0], eps=1e-6) + ) + + # 将每个阶段的块和下采样层组合 + self.stages = nn.ModuleList() + self.downsamples = nn.ModuleList() + total_blocks = sum(depths) + # 为 DropPath 计算每个块的丢弃概率 + dpr = [x.item() for x in torch.linspace(0, drop_path_rate, total_blocks)] + + block_idx = 0 + for i in range(self.num_stages): + # ConvNeXt 块 + stage = nn.Sequential( + *[ConvNeXtBlock3D(dim=self.dims[i], drop_path=dpr[block_idx + j]) for j in range(depths[i])] + ) + self.stages.append(stage) + block_idx += depths[i] + # 下采样层(除最后一个阶段外) + if i < self.num_stages - 1: + self.downsamples.append(DownSample3D(self.dims[i], self.dims[i+1])) + + # 全局池化和分类头 + self.global_pool = nn.AdaptiveAvgPool3d(1) + self.norm_head = nn.LayerNorm(self.dims[-1], eps=1e-6) + self.flatten = nn.Flatten() + self.fc = nn.Linear(self.dims[-1], feature_dim) + + def forward(self, x): + # Stem 层 + x = self.stem(x) + + # 各个阶段 + for i in range(self.num_stages): + x = self.stages[i](x) + if i < self.num_stages - 1: + x = self.downsamples[i](x) + + # 全局池化 + x = self.global_pool(x) # (B, C, 1, 1, 1) + x = x.view(x.shape[0], x.shape[1]) # (B, C) + x = self.norm_head(x) # LayerNorm + x = self.flatten(x) # (B, C) + x = self.fc(x) # (B, feature_dim) + return x + + +class PatchEmbed3D(nn.Module): + """3D Patch Embedding Layer""" + def __init__(self, input_channels=4, embed_dim=16, patch_size=(4, 8, 8)): + super(PatchEmbed3D, self).__init__() + self.patch_size = patch_size # (D, H, W) + self.proj = nn.Conv3d( + input_channels, + embed_dim, + kernel_size=patch_size, + stride=patch_size + ) + self.norm = nn.LayerNorm(embed_dim) + + def forward(self, x): + # x: (B, C, D, H, W) + x = self.proj(x) # (B, embed_dim, D', H', W') + B, C, D, H, W = x.shape + x = x.permute(0, 2, 3, 4, 1) # (B, D', H', W', C) + x = x.reshape(B, D * H * W, C) # (B, N, C), N = D'*H'*W' = (64/4)*(64/8)*(64/8)=16*8*8=1024 + x = self.norm(x) + return x # (B, N, C) + + +class TransformerEncoderLayer3D(nn.Module): + """Standard Transformer Encoder Layer for 3D ViT""" + def __init__(self, embed_dim, num_heads, mlp_ratio=4.0, drop=0.0, attn_drop=0.0): + super(TransformerEncoderLayer3D, self).__init__() + self.norm1 = nn.LayerNorm(embed_dim) + self.attn = nn.MultiheadAttention(embed_dim, num_heads, dropout=attn_drop, batch_first=True) + self.drop1 = nn.Dropout(drop) + self.norm2 = nn.LayerNorm(embed_dim) + hidden_dim = int(embed_dim * mlp_ratio) + self.mlp = nn.Sequential( + nn.Linear(embed_dim, hidden_dim), + nn.GELU(), + nn.Dropout(drop), + nn.Linear(hidden_dim, embed_dim), + nn.Dropout(drop) + ) + + def forward(self, x): + # x: (B, N, C) + x2 = self.norm1(x) + attn_output, _ = self.attn(x2, x2, x2) # attn_output: (B, N, C) + x = x + self.drop1(attn_output) + x2 = self.norm2(x) + x = x + self.mlp(x2) + return x # (B, N, C) + + +class TransformerEncoder3D(nn.Module): + """Transformer Encoder consisting of multiple TransformerEncoderLayer3D""" + def __init__(self, depth, embed_dim, num_heads, mlp_ratio=4.0, drop=0.0, attn_drop=0.0): + super(TransformerEncoder3D, self).__init__() + self.layers = nn.ModuleList([ + TransformerEncoderLayer3D(embed_dim, num_heads, mlp_ratio, drop, attn_drop) + for _ in range(depth) + ]) + self.norm = nn.LayerNorm(embed_dim) + + def forward(self, x): + for layer in self.layers: + x = layer(x) + x = self.norm(x) + return x # (B, N, C) + + +class VisionTransformer3D(nn.Module): + """3D Vision Transformer (ViT)""" + def __init__(self, input_channels=4, base_channels=16, feature_dim=512, + patch_size=(8, 8, 8), depth=4, num_heads=4, + mlp_ratio=4.0, drop_rate=0.1, attn_drop_rate=0.1): + super(VisionTransformer3D, self).__init__() + self.patch_embed = PatchEmbed3D(input_channels, base_channels, patch_size) + # Calculate number of patches + D_patch, H_patch, W_patch = patch_size + self.num_patches = (64 // D_patch) * (64 // H_patch) * (64 // W_patch) # 8*8*8=512 + + # CLS token + self.cls_token = nn.Parameter(torch.zeros(1, 1, base_channels)) + # Position Embedding + self.pos_embed = nn.Parameter(torch.zeros(1, self.num_patches + 1, base_channels)) + self.pos_drop = nn.Dropout(p=drop_rate) + + # Transformer Encoder + self.encoder = TransformerEncoder3D(depth, base_channels, num_heads, mlp_ratio, drop_rate, attn_drop_rate) + + # Classification head + self.norm_head = nn.LayerNorm(base_channels) + self.fc = nn.Linear(base_channels, feature_dim) + + self._init_weights() + + def _init_weights(self): + nn.init.trunc_normal_(self.pos_embed, std=0.02) + nn.init.trunc_normal_(self.cls_token, std=0.02) + nn.init.xavier_uniform_(self.fc.weight) + nn.init.constant_(self.fc.bias, 0) + + def forward(self, x): + # x: (B, C, D, H, W) + B = x.shape[0] + x = self.patch_embed(x) # (B, N, C) + + # Expand CLS token + cls_tokens = self.cls_token.expand(B, -1, -1) # (B, 1, C) + x = torch.cat((cls_tokens, x), dim=1) # (B, N+1, C) + x = x + self.pos_embed # (B, N+1, C) + x = self.pos_drop(x) + + x = self.encoder(x) # (B, N+1, C) + + cls_token_final = x[:, 0] # (B, C) + cls_token_final = self.norm_head(cls_token_final) # (B, C) + features = self.fc(cls_token_final) # (B, feature_dim) + return features # (B, feature_dim) + + +class SwinPatchEmbed3D(nn.Module): + """将3D输入分割成patches并进行嵌入""" + + def __init__(self, patch_size=(4, 4, 4), in_channels=4, embed_dim=16): + super(SwinPatchEmbed3D, self).__init__() + self.patch_size = patch_size + self.proj = nn.Conv3d(in_channels, embed_dim, kernel_size=patch_size, stride=patch_size) + + def forward(self, x): + # x shape: (B, C, D, H, W) + B, C, D, H, W = x.shape + x = self.proj(x) # (B, embed_dim, D/p, H/p, W/p) + x = x.flatten(2).transpose(1, 2) # (B, num_patches, embed_dim) + D_p, H_p, W_p = x.shape[1] // (H // self.patch_size[1] * W // self.patch_size[2]), H // self.patch_size[1], W // self.patch_size[2] + return x, (D_p, H_p, W_p) + + +class WindowAttention3D(nn.Module): + """3D窗口多头自注意力""" + + def __init__(self, dim, window_size=(7, 7, 7), num_heads=8, qkv_bias=True, attn_drop=0., proj_drop=0.): + super(WindowAttention3D, self).__init__() + self.dim = dim + self.window_size = window_size # (Wd, Wh, Ww) + self.num_heads = num_heads + head_dim = dim // num_heads + self.scale = head_dim ** -0.5 + + # 相对位置编码 + self.relative_position_bias_table = nn.Parameter( + torch.zeros( + (2 * window_size[0] - 1) * (2 * window_size[1] - 1) * (2 * window_size[2] - 1), + num_heads + ) + ) # 每个相对位置一个bias + + # 生成相对位置的index + coords_d = torch.arange(window_size[0]) + coords_h = torch.arange(window_size[1]) + coords_w = torch.arange(window_size[2]) + coords = torch.stack(torch.meshgrid(coords_d, coords_h, coords_w, indexing='ij')) # 3, Wd, Wh, Ww + coords_flatten = torch.flatten(coords, 1) # 3, Wd*Wh*Ww + relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :] # 3, Wd*Wh*Ww, Wd*Wh*Ww + relative_coords = relative_coords.permute(1, 2, 0).contiguous() # Wd*Wh*Ww, Wd*Wh*Ww, 3 + relative_coords += torch.tensor(self.window_size) - 1 # shift to start from 0 + relative_coords[:, :, 0] *= (2 * window_size[1] - 1) * (2 * window_size[2] - 1) + relative_coords[:, :, 1] *= (2 * window_size[2] - 1) + relative_position_index = relative_coords.sum(-1) # Wd*Wh*Ww, Wd*Wh*Ww + self.register_buffer("relative_position_index", relative_position_index) + + self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias) + self.attn_drop = nn.Dropout(attn_drop) + self.proj = nn.Linear(dim, dim) + self.proj_drop = nn.Dropout(proj_drop) + + nn.init.trunc_normal_(self.relative_position_bias_table, std=0.02) + + def forward(self, x, mask=None): + """ + x: (num_windows*B, N, C) + mask: (num_windows, N, N) or None + """ + B_, N, C = x.shape + qkv = self.qkv(x).reshape(B_, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4) + # qkv shape: (3, B_, num_heads, N, head_dim) + q, k, v = qkv[0], qkv[1], qkv[2] # each: (B_, num_heads, N, head_dim) + + q = q * self.scale + attn = (q @ k.transpose(-2, -1)) # (B_, num_heads, N, N) + + # 添加相对位置编码 + relative_position_bias = self.relative_position_bias_table[self.relative_position_index.view(-1)].view( + N, N, -1 + ) # (N, N, num_heads) + relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # (num_heads, N, N) + attn = attn + relative_position_bias.unsqueeze(0) + + if mask is not None: + nW = mask.shape[0] + attn = attn.view(B_ // nW, nW, self.num_heads, N, N) + mask.unsqueeze(1).unsqueeze(0) + attn = attn.view(-1, self.num_heads, N, N) + attn = F.softmax(attn, dim=-1) + else: + attn = F.softmax(attn, dim=-1) + + attn = self.attn_drop(attn) + out = (attn @ v) # (B_, num_heads, N, head_dim) + out = out.transpose(1, 2).reshape(B_, N, C) # (B_, N, C) + out = self.proj(out) + out = self.proj_drop(out) + return out + + +class SwinTransformerBlock3D(nn.Module): + """3D Swin Transformer Block""" + + def __init__(self, dim, num_heads, window_size=(7, 7, 7), shift_size=(0, 0, 0), + mlp_ratio=4., qkv_bias=True, drop=0., attn_drop=0., drop_path=0.): + super(SwinTransformerBlock3D, self).__init__() + self.dim = dim + self.num_heads = num_heads + self.window_size = window_size # Wd, Wh, Ww + self.shift_size = shift_size # Wd_shift, Wh_shift, Ww_shift + self.mlp_ratio = mlp_ratio + + self.norm1 = nn.LayerNorm(dim) + self.attn = WindowAttention3D(dim, window_size, num_heads, qkv_bias, attn_drop, drop) + + self.drop_path = nn.Identity() if drop_path == 0 else nn.Dropout(drop_path) + self.norm2 = nn.LayerNorm(dim) + mlp_hidden_dim = int(dim * mlp_ratio) + self.mlp = nn.Sequential( + nn.Linear(dim, mlp_hidden_dim), + nn.GELU(), + nn.Dropout(drop), + nn.Linear(mlp_hidden_dim, dim), + nn.Dropout(drop) + ) + + @staticmethod + def window_partition(x, window_size): + """ + 将输入x (B, D, H, W, C) 分割成窗口 + 返回: windows (num_windows*B, window_size D, window_size H, window_size W, C) + """ + B, D, H, W, C = x.shape + x = x.view( + B, + D // window_size[0], window_size[0], + H // window_size[1], window_size[1], + W // window_size[2], window_size[2], + C + ) + windows = x.permute(0, 1, 3, 5, 2, 4, 6, 7).contiguous().view(-1, *window_size, C) + return windows + + @staticmethod + def window_reverse(windows, window_size, B, D, H, W): + """ + 将窗口逆操作合并回原始特征图 + Input: + windows: (num_windows*B, window_size D, window_size H, window_size W, C) + window_size: tuple (Wd, Wh, Ww) + B, D, H, W: 原始体数据的维度 + Output: + x: (B, D, H, W, C) + """ + C = windows.shape[-1] + x = windows.view( + B, + D // window_size[0], + H // window_size[1], + W // window_size[2], + window_size[0], + window_size[1], + window_size[2], + C + ) + x = x.permute(0, 1, 2, 3, 4, 5, 6, 7).contiguous().view(B, D, H, W, C) + return x + + def create_attn_mask(self, input_resolution): + """创建注意力掩码,用于处理移动窗口边界""" + D, H, W = input_resolution + img_mask = torch.zeros((1, D, H, W, 1), device=self.attn.relative_position_bias_table.device) # (1, D, H, W, 1) + Wd, Wh, Ww = self.window_size + sd, sh, sw = self.shift_size + + # 计算每个区块的位置标记 + cnt = 0 + for d in (slice(0, -Wd), slice(-Wd, -sd) if sd > 0 else slice(0, None), slice(-sd, None) if sd > 0 else slice(0, None)): + for h in (slice(0, -Wh), slice(-Wh, -sh) if sh > 0 else slice(0, None), slice(-sh, None) if sh > 0 else slice(0, None)): + for w in (slice(0, -Ww), slice(-Ww, -sw) if sw > 0 else slice(0, None), slice(-sw, None) if sw > 0 else slice(0, None)): + img_mask[:, d, h, w, :] = cnt + cnt += 1 + + # 分割为窗口 + mask_windows = self.window_partition(img_mask, self.window_size) # (num_windows, Wd, Wh, Ww, 1) + mask_windows = mask_windows.view(-1, Wd * Wh * Ww) + attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2) + attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0)) + return attn_mask + + def forward(self, x, input_resolution): + """ + x: (B, N, C), N = D*H*W + input_resolution: tuple (D, H, W) + """ + D, H, W = input_resolution + B, N, C = x.shape + assert N == D * H * W, "Input has incorrect size" + + shortcut = x + x = self.norm1(x) + x = x.view(B, D, H, W, C) + + # 1. Shift + if any(s > 0 for s in self.shift_size): + shifted_x = torch.roll(x, shifts=(-self.shift_size[0], -self.shift_size[1], -self.shift_size[2]), dims=(1, 2, 3)) + else: + shifted_x = x + + # 2. Window partition + x_windows = self.window_partition(shifted_x, self.window_size) # (num_windows*B, Wd, Wh, Ww, C) + x_windows = x_windows.view(-1, self.window_size[0] * self.window_size[1] * self.window_size[2], C) # (num_windows*B, Nw, C) + + # 3. Window Multi-Head Self-Attention + if any(s > 0 for s in self.shift_size): + attn_mask = self.create_attn_mask(input_resolution) + else: + attn_mask = None + + attn_windows = self.attn(x_windows, mask=attn_mask) # (num_windows*B, Nw, C) + + # 4. Merge windows + attn_windows = attn_windows.view(-1, *self.window_size, C) + shifted_x = self.window_reverse(attn_windows, self.window_size, B, D, H, W) # (B, D, H, W, C) + + # 5. Reverse shift + if any(s > 0 for s in self.shift_size): + x = torch.roll(shifted_x, shifts=(self.shift_size[0], self.shift_size[1], self.shift_size[2]), dims=(1, 2, 3)) + else: + x = shifted_x + + x = x.view(B, D * H * W, C) + + # 6. MLP + x = shortcut + self.drop_path(x) + x = x + self.drop_path(self.mlp(self.norm2(x))) + + return x + + +class PatchMerging3D(nn.Module): + """3D Patch Merging""" + + def __init__(self, dim): + super(PatchMerging3D, self).__init__() + self.dim = dim + self.reduction = nn.Linear(dim * 8, 2 * dim, bias=False) # 2x reduction in resolution + self.norm = nn.LayerNorm(dim * 8) + + def forward(self, x, input_resolution): + # x: (B, N, C), N = D*H*W + B, N, C = x.shape + D, H, W = input_resolution + assert N == D * H * W, "Input has incorrect size" + + x = x.view(B, D, H, W, C) + + # 2x2x2_merge for D, H, W + x0 = x[:, 0::2, 0::2, 0::2, :] # (B, D/2, H/2, W/2, C) + x1 = x[:, 1::2, 0::2, 0::2, :] + x2 = x[:, 0::2, 1::2, 0::2, :] + x3 = x[:, 0::2, 0::2, 1::2, :] + x4 = x[:, 1::2, 1::2, 0::2, :] + x5 = x[:, 1::2, 0::2, 1::2, :] + x6 = x[:, 0::2, 1::2, 1::2, :] + x7 = x[:, 1::2, 1::2, 1::2, :] + x = torch.cat([x0, x1, x2, x3, x4, x5, x6, x7], -1) # (B, D/2, H/2, W/2, 8*C) + x = x.view(B, -1, 8 * C) # (B, D/2 * H/2 * W/2, 8*C) + + x = self.norm(x) + x = self.reduction(x) # (B, D/2 * H/2 * W/2, 2*C) + + return x + + +class SwinTransformerStage3D(nn.Module): + """一个Swin Transformer阶段,包括多个Block和Patch Merging""" + + def __init__(self, dim, depth, num_heads, window_size=(7, 7, 7), mlp_ratio=4., + qkv_bias=True, drop=0., attn_drop=0., drop_path=None, downsample=True): + super(SwinTransformerStage3D, self).__init__() + self.blocks = nn.ModuleList() + for i in range(depth): + shift_size = ( + window_size[0] // 2 if (i % 2 == 1 and window_size[0] > 1) else 0, + window_size[1] // 2 if (i % 2 == 1 and window_size[1] > 1) else 0, + window_size[2] // 2 if (i % 2 == 1 and window_size[2] > 1) else 0, + ) + block = SwinTransformerBlock3D( + dim=dim, + num_heads=num_heads, + window_size=window_size, + shift_size=shift_size, + mlp_ratio=mlp_ratio, + qkv_bias=qkv_bias, + drop=drop, + attn_drop=attn_drop, + drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path + ) + self.blocks.append(block) + + self.downsample = PatchMerging3D(dim) if downsample else None + + def forward(self, x, input_resolution): + """ + x: (B, N, C) + input_resolution: tuple (D, H, W) + """ + for blk in self.blocks: + x = blk(x, input_resolution) + if self.downsample: + x = self.downsample(x, input_resolution) + D, H, W = input_resolution + input_resolution = (D // 2, H // 2, W // 2) + return x, input_resolution + + +class SwinTransformer3D(nn.Module): + """3D Swin Transformer模型""" + + def __init__(self, input_channels=4, base_channels=16, feature_dim=512, + patch_size=(4, 4, 4), + depths=[2, 2, 6, 2], num_heads=[2, 4, 8, 16], + window_size=(2, 2, 2), mlp_ratio=4., qkv_bias=True, + drop_rate=0., attn_drop_rate=0., drop_path_rate=0.1): + super(SwinTransformer3D, self).__init__() + self.num_layers = len(depths) + self.embed_dim = base_channels + self.patch_size = patch_size + self.patch_embed = SwinPatchEmbed3D(patch_size, in_channels=input_channels, embed_dim=self.embed_dim) + + # 计算drop path率 + total_depth = sum(depths) + dpr = [x.item() for x in torch.linspace(0, drop_path_rate, total_depth)] + + self.layers = nn.ModuleList() + current_depth = 0 + for i_layer in range(self.num_layers): + layer = SwinTransformerStage3D( + dim=base_channels * 2**i_layer, + depth=depths[i_layer], + num_heads=num_heads[i_layer], + window_size=window_size, + mlp_ratio=mlp_ratio, + qkv_bias=qkv_bias, + drop=drop_rate, + attn_drop=attn_drop_rate, + drop_path=dpr[current_depth:current_depth + depths[i_layer]], + downsample=(i_layer < self.num_layers - 1) + ) + self.layers.append(layer) + current_depth += depths[i_layer] + + self.norm = nn.LayerNorm(base_channels * 2**(self.num_layers - 1)) + self.avgpool = nn.AdaptiveAvgPool1d(1) + self.head = nn.Linear(base_channels * 2**(self.num_layers - 1), feature_dim) + + def forward_features(self, x): + """ + x: (B, C, D, H, W) + """ + x, input_resolution = self.patch_embed(x) # (B, num_patches, embed_dim), (D_p, H_p, W_p) + for layer in self.layers: + x, input_resolution = layer(x, input_resolution) + x = self.norm(x) # (B, num_patches, dim) + return x + + def forward(self, x): + """ + x: (B, C, D, H, W) + """ + x = self.forward_features(x) # (B, num_patches, dim) + x = x.transpose(1, 2) # (B, dim, num_patches) + x = self.avgpool(x).squeeze(-1) # (B, dim) + x = self.head(x) # (B, feature_dim) + return x + + +class VoxPeptide(nn.Module): + def __init__(self, v_encoder='resnet34', classes=6, channels=16, in_channels=4): + super().__init__() + self.classes = classes + + if v_encoder == 'resnet34': + self.v_encoder = ResNet3D34(input_channels=in_channels, base_channels=channels, feature_dim=512) + elif v_encoder == 'resnet50': + self.v_encoder = ResNet3D50(input_channels=in_channels, base_channels=channels, feature_dim=512) + elif v_encoder == 'densenet': + self.v_encoder = DenseNet3D(input_channels=in_channels, base_channels=channels, growth_rate=16 if channels < 48 else 32, feature_dim=512) + elif v_encoder == 'convnext': + self.v_encoder = ConvNeXt3D(input_channels=in_channels, base_channels=channels, feature_dim=512) + elif v_encoder == 'vit': + self.v_encoder = VisionTransformer3D(input_channels=in_channels, base_channels=channels, feature_dim=512) + elif v_encoder == 'swintf': + self.v_encoder = SwinTransformer3D(input_channels=in_channels, base_channels=channels, feature_dim=512) + else: + raise NotImplementedError(f'\'{v_encoder}\' not implemented') + + self.vox_fc = nn.Linear(512, classes) + + def forward(self, x): + vox, seq = x + seq_emb = self.v_encoder(vox) + pred = self.vox_fc(seq_emb) + return pred + + +class PositionalEncoding(nn.Module): + def __init__(self, d_model, max_len=50): + super(PositionalEncoding, self).__init__() + + pe = torch.zeros(max_len, d_model) # (max_len, d_model) + position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1) # (max_len, 1) + div_term = torch.exp(torch.arange(0, d_model, 2).float() * + (-torch.log(torch.FloatTensor([10000.0])) / d_model)) # (d_model/2,) + pe[:, 0::2] = torch.sin(position * div_term) # 偶数维 + pe[:, 1::2] = torch.cos(position * div_term) # 奇数维 + pe = pe.unsqueeze(0) # (1, max_len, d_model) + self.register_buffer('pe', pe) + + def forward(self, x): + """ + x: (B, N, d_model) + """ + x = x + self.pe[:, :x.size(1), :] + return x + + +class TransformerModel(nn.Module): + def __init__(self, nheads, d_model, num_layers, out_dim, max_length=50): + super(TransformerModel, self).__init__() + + # 嵌入层,将输入从 (B, N) 转换到 (B, N, embed_dim) + self.embedding = nn.Linear(1, d_model) + + # 位置编码 + self.pos_encoder = PositionalEncoding(d_model, max_length) + + # Transformer 编码器层 + encoder_layers = nn.TransformerEncoderLayer(d_model=d_model, + nhead=nheads, + activation='relu') + self.transformer_encoder = nn.TransformerEncoder(encoder_layers, num_layers=num_layers) + + # 全局池化(可以根据任务选择不同的聚合方式) + self.global_pool = nn.AdaptiveAvgPool1d(1) + + # 输出层 + self.fc = nn.Linear(d_model, out_dim) + + def forward(self, src): + """ + src: (B, N) + """ + # 嵌入 + embedded = self.embedding(src.unsqueeze(-1)) # (B, N, embed_dim) + embedded = self.pos_encoder(embedded) # 添加位置编码 + + # 转置以适应 Transformer (N, B, embed_dim) + embedded = embedded.permute(1, 0, 2) + + # Transformer 编码 + transformer_out = self.transformer_encoder(embedded) # (N, B, embed_dim) + + # 转置回 (B, N, embed_dim) + transformer_out = transformer_out.permute(1, 0, 2) + + # 全局池化,将 (B, N, embed_dim) 转换为 (B, embed_dim) + pooled = self.global_pool(transformer_out.permute(0, 2, 1)).squeeze(-1) + + # 输出层 + output = self.fc(pooled) # (B, output_dim) + + return output + + +class MambaModel(nn.Module): + def __init__(self, d_model, out_dim, max_length=30): + super(MambaModel, self).__init__() + self.linear = nn.Linear(in_features=1, out_features=d_model) + self.pos_encoder = PositionalEncoding(d_model, max_length) + self.mamba = Mamba(d_model=d_model) + self.global_pool = nn.AdaptiveAvgPool1d(1) + self.fc = nn.Linear(d_model * 2, out_dim) + + def forward(self, x: torch.Tensor): + x = self.pos_encoder(self.linear(x.unsqueeze(-1))) + y = self.mamba(x) + y_flip = self.mamba(x.flip([-2])).flip([-2]) + y = torch.cat((y, y_flip), dim=-1) + y = self.fc(self.global_pool(y.permute(0, 2, 1)).squeeze(-1)) + return y + + +class SEQ(nn.Module): + def __init__(self, seq_type='mlp', input_dim=21, hidden_dim=128, out_dim=128, num_layers=2, max_length=30): + super(SEQ, self).__init__() + self.seq_type = seq_type + if seq_type == 'rnn': + self.rnn = nn.RNN( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'gru': + self.rnn = nn.GRU( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'lstm': + self.rnn = nn.LSTM( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'tf': + self.transformer = TransformerModel(nheads=4, d_model=hidden_dim, num_layers=2, out_dim=out_dim, max_length=max_length) + elif seq_type == 'mamba': + self.mamba = MambaModel(d_model=hidden_dim, out_dim=out_dim, max_length=max_length) + else: + # nn.Linear(50, 50, bias=False), nn.ReLU(), + self.rnn = nn.Sequential(nn.Linear(max_length, hidden_dim * 4), nn.ReLU(), nn.Linear(hidden_dim * 4, out_dim)) + self.rnn_fc = nn.Sequential( + nn.Linear(hidden_dim * 2, hidden_dim), + nn.ReLU(), + nn.Linear(hidden_dim, out_dim) + ) + + def forward(self, seq): + if self.seq_type == 'mlp': + return self.rnn(seq.squeeze(1)) + elif self.seq_type == 'tf': + return self.transformer(seq) + elif self.seq_type == 'mamba': + return self.mamba(seq) + else: + one_hot_seq = F.one_hot(seq.to(torch.int64), num_classes=21).float() + r_out = self.rnn(one_hot_seq, None)[0] # None represents zero initial hidden state + out = self.rnn_fc(r_out[:, -1, :]) + return out + # def forward(self, x, seq_lengths): + # + + +class SEQPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + self.classes = classes + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + + self.seq_fc = nn.Linear(128, classes) + + def forward(self, x, seq_lengths=None): + vox, seq = x + seq_emb = self.q_encoder(seq) + pred = self.seq_fc(seq_emb) + return pred + + +class ConvNet(nn.Module): + def __init__(self, num_classes: int = 2): + super(ConvNet, self).__init__() + self.conv1 = nn.Conv1d(3, 16, 3, padding=1) + self.conv2 = nn.Conv1d(16, 32, 3, padding=1) + self.fc1 = nn.Linear(32 * 7, 128) + # self.fc2 = nn.Linear(128, num_classes) + + def forward(self, x): + # print(x.shape) + x = x.permute(0, 2, 1) + x = F.relu(self.conv1(x)) + x = F.max_pool1d(x, 2) + x = F.relu(self.conv2(x)) + x = F.max_pool1d(x, 2) + x = x.view(x.shape[0], -1) + return self.fc1(x) + # x = F.relu(self.fc1(x)) + # x = self.fc2(x) + # return x + + +class ConvNet2D(nn.Module): + def __init__(self, num_classes: int = 2): + super(ConvNet2D, self).__init__() + self.conv1 = nn.Conv2d(1, 8, 3, padding=1) + self.conv2 = nn.Conv2d(8, 16, 3, padding=2, stride=2) + self.conv3 = nn.Conv2d(16, 32, 3, padding=2, stride=2) + # self.pool = nn.AdaptiveAvgPool2d(32) + self.fc1 = nn.Linear(32 * 3 * 9, 128) + # self.fc2 = nn.Linear(128, num_classes) + + def forward(self, x): + # print(x.shape) + x = x.unsqueeze(1) + x = F.relu(self.conv1(x)) + # print(x.shape) + x = F.relu(self.conv2(x)) + # print(x.shape) + x = F.relu(self.conv3(x)) + # x = self.pool(x) + # print(x.shape) + x = x.view(x.shape[0], -1) + return self.fc1(x) + + +# convnet = ConvNet() +# print(convnet) +class MMPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + if attention == 'hamburger': + self.attention = Hamburger(2048, 2048) + else: + self.attention = None + # v_encoder could be resnet26 or resnet50 + if v_encoder == 'resnet26': + self.v_encoder = ResNet3D(Bottleneck3D, [1, 2, 4, 1], self.attention) + # self.v_encoder = SwinUNETR(img_size=(64, 64, 64), in_channels=3, out_channels=1) + elif v_encoder == 'resnet50': + self.v_encoder = ResNet3D(Bottleneck3D, [3, 4, 6, 3], self.attention) + else: + raise NotImplementedError + + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + # self.ss_encoder = SEQ(seq_type=q_encoder) + if fusion == 'mlp': + self.fusion = nn.Linear(512 * 4 + 256, 256) + # self.fusion = nn.Linear(192 + 256, classes) + elif fusion == 'att': + self.fusion = nn.Linear(512 * 4 + 256, 256) + else: + raise NotImplementedError + + # self.vox_fc = nn.Linear(2048, classes) + # self.seq_fc = nn.Linear(256, classes) + self.out = nn.Sequential(nn.ReLU(inplace=True), nn.Linear(256, classes)) + self.classes = classes + + def forward(self, x, seq_lengths=None): + vox, seq = x + # print(vox.shape) + # print(seq.shape) + vox_emb = self.v_encoder(vox) + # print(vox_emb.shape) + seq_emb = self.q_encoder(seq, seq_lengths) + # print(seq_emb.shape) + # ss_emb = self.ss_encoder(second_s) + fused_feature = torch.cat((seq_emb, vox_emb), dim=1) + pred = self.fusion(fused_feature) + pred = self.out(pred) + # pred1 = self.vox_fc(vox_emb) + # pred2 = self.seq_fc(seq_emb) + # return pred, fused_feature + return pred + + +class SMPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, hidden_dim=256, max_length=30): + super().__init__() + self.siamese_encoder1 = MMPeptide(v_encoder, q_encoder, fusion, classes, attention, max_length) + # self.siamese_encoder2 = MMPeptide(v_encoder, q_encoder, fusion, classes, attention) + self.fc = nn.Sequential( + nn.Linear(hidden_dim * 2, hidden_dim), + nn.ReLU(), + nn.Linear(hidden_dim, 1) + ) + + def forward(self, x, seq_lengths=None): + f_mutated = self.siamese_encoder1(x[0]) + f_wide_type = self.siamese_encoder1(x[1]) + return self.fc(torch.cat((f_mutated, f_wide_type), dim=1)) + + +class MMFPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + if attention == 'hamburger': + self.attention = Hamburger(2048, 2048) + else: + self.attention = None + # v_encoder could be resnet26 or resnet50 + if v_encoder == 'resnet26': + self.v_encoder = ResNet3D(Bottleneck3D, [1, 2, 4, 1], self.attention) + # self.v_encoder = ResNet3DFusion(Bottleneck, [1, 2, 4, 1], self.attention) + elif v_encoder == 'resnet50': + self.v_encoder = ResNet3D(Bottleneck3D, [3, 4, 6, 3], self.attention) + else: + raise NotImplementedError + + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + + if fusion == 'mlp': + self.fusion = nn.Linear(512 * 4 + 256, classes) + elif fusion == 'att': + self.fusion = nn.Linear(512 * 4 + 256, classes) + else: + raise NotImplementedError + + self.vox_fc = nn.Linear(2048, classes) + self.seq_fc = nn.Linear(256, classes) + + def forward(self, x, seq_lengths=None): + vox, seq = x + # print(vox.shape) + # print(seq.shape) + seq_emb = self.q_encoder(seq, seq_lengths) + + vox_emb = self.v_encoder(vox, seq_emb) + # print(vox_emb.shape) + # print(seq_emb.shape) + fused_feature = torch.cat((seq_emb, vox_emb), dim=1) + pred = self.fusion(fused_feature) + # pred1 = self.vox_fc(vox_emb) + # pred2 = self.seq_fc(seq_emb) + return pred + + +if __name__ == "__main__": + # model = MMFPeptide() + # voxel = torch.zeros((4, 3, 64, 64, 64)) + # # # h_in = torch.zeros((2, 2048, 2, 2, 2)) + # # # h = Hamburger(2048, 2048) + # # # h(h_in) + # seq = torch.ones((4, 50)) + # res = model.forward((voxel, seq)) + # out = model((voxel, seq)) + # print(out.shape) + # model = ConvNet2D() + input_seq = torch.ones((4, 1, 30)) + # model(input_seq) + transformer = TransformerModel(nhead=4, d_model=32, num_layers=2) + print(transformer(input_seq).shape) diff --git a/t3.2.1/run/convnext16-bce256-0.001-50af/config.json b/t3.2.1/run/convnext16-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..fa755f55ec53101887cc53ac117d129e2dc64be3 --- /dev/null +++ b/t3.2.1/run/convnext16-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "convnext", "channels": 16, "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.2.1/run/convnext16-bce256-0.001-50af/model_1.pth b/t3.2.1/run/convnext16-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..7f1dd253f6888cb02973aa50ac9ce81e3395db21 --- /dev/null +++ b/t3.2.1/run/convnext16-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:53dae88bac23355d9d63a1fb3270c35079b01689e4368d905ddc053de5408cdb +size 5118282 diff --git a/t3.2.1/run/convnext16-bce256-0.001-50af/model_2.pth b/t3.2.1/run/convnext16-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..c2ad5d8718d7a72eae66075f29f1433b67972871 --- /dev/null +++ b/t3.2.1/run/convnext16-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b1c214cdabdee6f7f4070579c05a1a487dc4596a387c70375c68a0817a2c2b0b +size 5118282 diff --git a/t3.2.1/run/convnext16-bce256-0.001-50af/model_3.pth b/t3.2.1/run/convnext16-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..f6a65b0477e72d117b5cd9e5b6bce2412e41b19a --- /dev/null +++ b/t3.2.1/run/convnext16-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cd34e76b639de507ed69ad852548fb459277f4eee9bf749859458939b883cb1c +size 5118282 diff --git a/t3.2.1/run/convnext16-bce256-0.001-50af/model_4.pth b/t3.2.1/run/convnext16-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..fb225e92bb5b6941955c1eaa7586d48d7373d32f --- /dev/null +++ b/t3.2.1/run/convnext16-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7c1402a8ae4377951e19eda5f3cbd449cfd1de09220bde65f0c2e41286165c8d +size 5118282 diff --git a/t3.2.1/run/convnext16-bce256-0.001-50af/model_5.pth b/t3.2.1/run/convnext16-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..6d51113e8bae3194d903504e9008c4219159d0fe --- /dev/null +++ b/t3.2.1/run/convnext16-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ab6ac2b18e74345caff1467994148e9f06ff3c4dcdf0a6cff3b968903bd2d994 +size 5118282 diff --git a/t3.2.1/run/convnext16-bce256-0.001-50af/result.txt b/t3.2.1/run/convnext16-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..ee7f9201c6fd0a102da38737d91532d75fe20625 --- /dev/null +++ b/t3.2.1/run/convnext16-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.694989800453186,0.7819282174110412,0.6507226705551148,0.5785039901733399 +0.05072218546457726,0.007210807064022936,0.02619578013074793,0.07632904146177551 +qlx +0.6121723055839539,0.6973407864570618,0.5505518436431884,0.5642228484153747 +0.04626211742648071,0.00977027416229248,0.02825608253479004,0.052436869260199005 +saap +0.44698960781097413,0.512074327468872,0.3873333215713501,0.5525253534317016 +0.02223170408075914,0.0017412185668945312,0.004666662216186524,0.04158899878083978 diff --git a/t3.2.1/run/densenet16-bce256-0.001-50af/config.json b/t3.2.1/run/densenet16-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..ce1665c26a93c5cf239d0f4f5fe46f11cddaf95a --- /dev/null +++ b/t3.2.1/run/densenet16-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "densenet", "channels": 16, "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.2.1/run/densenet16-bce256-0.001-50af/model_1.pth b/t3.2.1/run/densenet16-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..3ccd704078896198ba7a282315ffd39311e3cdd3 --- /dev/null +++ b/t3.2.1/run/densenet16-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:261591602f72af231dd2e4b52290f91482bc9b701b828a86104e9bc3e0a9ef5c +size 5225592 diff --git a/t3.2.1/run/densenet16-bce256-0.001-50af/model_2.pth b/t3.2.1/run/densenet16-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..592057110cc621b5f8e15fb05a27c73e2c30ee6a --- /dev/null +++ b/t3.2.1/run/densenet16-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:51e56329533772bcf6810a05d1a3173999f2f45be3175c62e2d62dc58b997f73 +size 5225592 diff --git a/t3.2.1/run/densenet16-bce256-0.001-50af/model_3.pth b/t3.2.1/run/densenet16-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..c32c632d81ae9e0960e18ea95fd63da2beb476b2 --- /dev/null +++ b/t3.2.1/run/densenet16-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:881e524508d59dcc1057ce4b141e6dc86732cf22e942a43df1abcf62b7294983 +size 5225592 diff --git a/t3.2.1/run/densenet16-bce256-0.001-50af/model_4.pth b/t3.2.1/run/densenet16-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..c4437b88de83de6c2611f65ed4d4813611b18722 --- /dev/null +++ b/t3.2.1/run/densenet16-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:aa84623eaaa898cd1439a76b8bf5ce893c58de11e15fcafa90c23d4d50dcc523 +size 5225592 diff --git a/t3.2.1/run/densenet16-bce256-0.001-50af/model_5.pth b/t3.2.1/run/densenet16-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..acec59dbba9079ca8180d9f36055cfee66b797de --- /dev/null +++ b/t3.2.1/run/densenet16-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bb3747ad0ebc35aedc2ab9a121ec5202a9bf939dbd7adab52ce223bc576139fc +size 5225592 diff --git a/t3.2.1/run/densenet16-bce256-0.001-50af/result.txt b/t3.2.1/run/densenet16-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..cd2d0e469e418dee66c93e934646277735500f35 --- /dev/null +++ b/t3.2.1/run/densenet16-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8206434369087219,0.8027147889137268,0.728118646144867,0.7601370334625244 +0.014040154050613722,0.013483532591558227,0.011515973441915591,0.010880879794137847 +qlx +0.7648542881011963,0.7061002612113952,0.6441501021385193,0.7459516644477844 +0.010990198673586998,0.04380993872843276,0.01624578726518622,0.013774303512510163 +saap +0.437517786026001,0.5146240949630737,0.39099998474121095,0.5253238320350647 +0.04197228932499725,0.0019389045047049326,0.007195670852641275,0.0475296191856597 diff --git a/t3.2.1/run/resnet3416-bce256-0.001-50af/config.json b/t3.2.1/run/resnet3416-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..c4180d0cfc2f479140a96d44bfb642af9e1abaec --- /dev/null +++ b/t3.2.1/run/resnet3416-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "channels": 16, "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.2.1/run/resnet3416-bce256-0.001-50af/model_1.pth b/t3.2.1/run/resnet3416-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..cb366d69f3bb8ad1dae371e05822ef681f64ac2d --- /dev/null +++ b/t3.2.1/run/resnet3416-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a91cf56d19d50725aac42be7201ee0911f41047ec9f354e75013380ea1822d84 +size 16331514 diff --git a/t3.2.1/run/resnet3416-bce256-0.001-50af/model_2.pth b/t3.2.1/run/resnet3416-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..a3c32802c3aa0d767ade676b081d29e321c7dd7f --- /dev/null +++ b/t3.2.1/run/resnet3416-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:579b3ecaf5c79aabcd2861be371cbe25d5eb6cd1da031bee84a0ea064bbe93a8 +size 16331514 diff --git a/t3.2.1/run/resnet3416-bce256-0.001-50af/model_3.pth b/t3.2.1/run/resnet3416-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..667dec00f0da5341679888cd4075bc2faf4ce8ba --- /dev/null +++ b/t3.2.1/run/resnet3416-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ffe1c524c37ec0e84f07ac838a192ba856b4ca3164e89e2ebc2190b7d6c73ff6 +size 16331514 diff --git a/t3.2.1/run/resnet3416-bce256-0.001-50af/model_4.pth b/t3.2.1/run/resnet3416-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..a5e6880543a6324db1bed2e7ddc56f8f40a103d7 --- /dev/null +++ b/t3.2.1/run/resnet3416-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3ba18fa9527f3fca85464a29715994f2f558796767336767c94e3a25dc84077b +size 16331514 diff --git a/t3.2.1/run/resnet3416-bce256-0.001-50af/model_5.pth b/t3.2.1/run/resnet3416-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..f415bee7e3e105c7b710ae92b87b34851dd2fd7c --- /dev/null +++ b/t3.2.1/run/resnet3416-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:245bda9efe48fdb6ceadb69122d892ca9d3ff125cdbcd0b73aeda73164fac671 +size 16331514 diff --git a/t3.2.1/run/resnet3416-bce256-0.001-50af/result.txt b/t3.2.1/run/resnet3416-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..87e180f08e2a25ee9fa35f11d6b49413d9730c3f --- /dev/null +++ b/t3.2.1/run/resnet3416-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8142587661743164,0.7818215250968933,0.6963970422744751,0.7428945064544678 +0.009441299141151136,0.022269939172797126,0.018502286297546886,0.013476376604275031 +qlx +0.7353579640388489,0.6800113558769226,0.612582778930664,0.7305090427398682 +0.021229618983948935,0.020103967585270957,0.052369548771971836,0.018961690170217378 +saap +0.4504553318023682,0.5095038294792176,0.4056666612625122,0.5338208317756653 +0.028594701723130708,0.004684147610775225,0.023584370254439498,0.030469552370707087 diff --git a/t3.2.1/run/resnet3416-bce256-0.001-50hf/config.json b/t3.2.1/run/resnet3416-bce256-0.001-50hf/config.json new file mode 100644 index 0000000000000000000000000000000000000000..08e6d8df12eb8d2fb4187dba12867d2620fab105 --- /dev/null +++ b/t3.2.1/run/resnet3416-bce256-0.001-50hf/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "channels": 16, "fusion": "1", "pdb_src": "hf", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.2.1/run/resnet3416-bce256-0.001-50hf/model_1.pth b/t3.2.1/run/resnet3416-bce256-0.001-50hf/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..d3960b9ef67cd3bff6d4509d65edab0985231416 --- /dev/null +++ b/t3.2.1/run/resnet3416-bce256-0.001-50hf/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8cddeab0dbb19017b1559474ddb73200bad959877a22ee6b88dcf7c74c48c931 +size 16331514 diff --git a/t3.2.1/run/resnet3416-bce256-0.001-50hf/model_2.pth b/t3.2.1/run/resnet3416-bce256-0.001-50hf/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..1a5ee11340e3b11b23db40ea5ec4249c0a8f94ff --- /dev/null +++ b/t3.2.1/run/resnet3416-bce256-0.001-50hf/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:de23480537e131e373fdcff989b8374c1723e0760b8dbf90ff70dd09065ddc15 +size 16331514 diff --git a/t3.2.1/run/resnet3416-bce256-0.001-50hf/model_3.pth b/t3.2.1/run/resnet3416-bce256-0.001-50hf/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..ca5fca03e6111f2ac00aadf2f2ba37e826206a2b --- /dev/null +++ b/t3.2.1/run/resnet3416-bce256-0.001-50hf/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0fdeb44804e70995d36e3a0b6dd59741bf1e6989c24d38681b5a545428774f1c +size 16331514 diff --git a/t3.2.1/run/resnet3416-bce256-0.001-50hf/model_4.pth b/t3.2.1/run/resnet3416-bce256-0.001-50hf/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..577af62d495a92db27c2bdf3d7920ec7fce78909 --- /dev/null +++ b/t3.2.1/run/resnet3416-bce256-0.001-50hf/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7c649246b8de25b3bc85a30f1138f19ba377ada154bd63c098a7e7f5d7312c8a +size 16331514 diff --git a/t3.2.1/run/resnet3416-bce256-0.001-50hf/model_5.pth b/t3.2.1/run/resnet3416-bce256-0.001-50hf/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..1ef6f7679ff49e8ad6a238b3ea01299a633f2d01 --- /dev/null +++ b/t3.2.1/run/resnet3416-bce256-0.001-50hf/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fb4ea9b083a78fb3e57facd359ed9737cdb13f315cce42c03e6de6b0faa1a506 +size 16331514 diff --git a/t3.2.1/run/resnet3416-bce256-0.001-50hf/result.txt b/t3.2.1/run/resnet3416-bce256-0.001-50hf/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..e17d055bc17cff9681f4dd8cf7d6cf330b977138 --- /dev/null +++ b/t3.2.1/run/resnet3416-bce256-0.001-50hf/result.txt @@ -0,0 +1,9 @@ +Valid +0.8360827088356018,0.792181134223938,0.7188670396804809,0.769420611858368 +0.013570479834021291,0.01323250406935312,0.007290165839185682,0.010633843931735066 +qlx +0.7338361740112305,0.6921789765357971,0.5922737121582031,0.7001710295677185 +0.00886090918007702,0.018574255722406865,0.03031282793395665,0.013880467138797897 +saap +0.4400079011917114,0.5128284454345703,0.387666654586792,0.5654788255691529 +0.014772665942581711,0.00019698557463228568,0.003887297556010825,0.01901689985883802 diff --git a/t3.2.1/run/resnet3432-bce256-0.001-50af/config.json b/t3.2.1/run/resnet3432-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..5c6ac063e6f389e597455307f33d0b20e20f0ad8 --- /dev/null +++ b/t3.2.1/run/resnet3432-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "channels": 32, "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.2.1/run/resnet3432-bce256-0.001-50af/model_1.pth b/t3.2.1/run/resnet3432-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..6520ce9f3186179a095c3ce8a63822bdee7c52e5 --- /dev/null +++ b/t3.2.1/run/resnet3432-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5947b45c771f0a4fb3ae9ada039e8902d6f733f828507f56a8d565eb87738ed7 +size 64288634 diff --git a/t3.2.1/run/resnet3432-bce256-0.001-50af/model_2.pth b/t3.2.1/run/resnet3432-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..42063e7f0e0cfd4b02aa0837c5b08bc1d1a37a9a --- /dev/null +++ b/t3.2.1/run/resnet3432-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f22e7d404d9f1750784c31027c5c25730619f12143e9eeeedae5fa54fe8bb878 +size 64288634 diff --git a/t3.2.1/run/resnet3432-bce256-0.001-50af/model_3.pth b/t3.2.1/run/resnet3432-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..5aeaf65f3d37612796c2656a520ed88efe9b331f --- /dev/null +++ b/t3.2.1/run/resnet3432-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a5344ecc667153c211bb9ef77749f304141cd35d80a79c1fe0e5819569a03c18 +size 64288634 diff --git a/t3.2.1/run/resnet3432-bce256-0.001-50af/model_4.pth b/t3.2.1/run/resnet3432-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..c5996b1a0d81bb850c7cbec1683383096b002298 --- /dev/null +++ b/t3.2.1/run/resnet3432-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c166bd55e81fc390801657448444b0c3302e0652531467695e1f2fc2c7345278 +size 64288634 diff --git a/t3.2.1/run/resnet3432-bce256-0.001-50af/model_5.pth b/t3.2.1/run/resnet3432-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..d1584aa9ae30ad8ef99442e4f2acf2748bd1bfd2 --- /dev/null +++ b/t3.2.1/run/resnet3432-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:087605515bdc4ed74a2e3f386238f79fd39a58d3916b8c41cb8c8300a677e57f +size 64288634 diff --git a/t3.2.1/run/resnet3432-bce256-0.001-50af/result.txt b/t3.2.1/run/resnet3432-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..7293d1dc0feba7b8afafab154b60b1b80bbf6451 --- /dev/null +++ b/t3.2.1/run/resnet3432-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8297885775566101,0.7991637110710144,0.7236441612243653,0.7673058032989502 +0.013502856354554468,0.007033118512803595,0.011577684646964521,0.012467367821274836 +qlx +0.7670576333999634,0.7203718423843384,0.6306843280792236,0.7504899740219116 +0.017551423978692738,0.005259278502693183,0.020287432137502347,0.021230903336630653 +saap +0.45671294927597045,0.5100360691547394,0.41333332657814026,0.5562495708465576 +0.04392915597562726,0.006747311525532205,0.043256346768550814,0.021778191219362496 diff --git a/t3.2.1/run/resnet5016-bce256-0.001-50af/config.json b/t3.2.1/run/resnet5016-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..4a4ac0f68d5375cbacdd4c1b11683e6483d32b7f --- /dev/null +++ b/t3.2.1/run/resnet5016-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "resnet50", "channels": 16, "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.2.1/run/resnet5016-bce256-0.001-50af/model_1.pth b/t3.2.1/run/resnet5016-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..31de4de03718553af2adb1dfbdc4f97fa357f814 --- /dev/null +++ b/t3.2.1/run/resnet5016-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e9138812c6c65624e249b701794d796d94e46d6012f852ed3eda44e4d4beeaec +size 12886958 diff --git a/t3.2.1/run/resnet5016-bce256-0.001-50af/model_2.pth b/t3.2.1/run/resnet5016-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..cff669886b680698a544d78400673ba577946a94 --- /dev/null +++ b/t3.2.1/run/resnet5016-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6de6f3786cfbbb9b678b85b0086580397e151e0e39c87811503bdd2ab9ef54f9 +size 12886958 diff --git a/t3.2.1/run/resnet5016-bce256-0.001-50af/model_3.pth b/t3.2.1/run/resnet5016-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..bbae43f0fd4f0d157b27487fedf2b6bbbe246967 --- /dev/null +++ b/t3.2.1/run/resnet5016-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5f542e1c60440ff2684b611ad9aa8610ecb03621c61a43da30d8052e296b0aba +size 12886958 diff --git a/t3.2.1/run/resnet5016-bce256-0.001-50af/model_4.pth b/t3.2.1/run/resnet5016-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..da0b664016015d8f19217ea0264f4a629cd56d7e --- /dev/null +++ b/t3.2.1/run/resnet5016-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2e5916f6dc9a18e84e4fcaaecaf2f2f72c3b318e11833ccda9ab83ff875a7f5d +size 12886958 diff --git a/t3.2.1/run/resnet5016-bce256-0.001-50af/model_5.pth b/t3.2.1/run/resnet5016-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..cee3b5978dea766e8f901c9f29ade4f7e16b9baa --- /dev/null +++ b/t3.2.1/run/resnet5016-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:de5d74c9fda073a1bc6bb31bfe8dba7802c63b49e4db29f5fb25efb5246f02b3 +size 12886958 diff --git a/t3.2.1/run/resnet5016-bce256-0.001-50af/result.txt b/t3.2.1/run/resnet5016-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..3c99542d8fdc6a5ea65592b085f183217bc7abc2 --- /dev/null +++ b/t3.2.1/run/resnet5016-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8130381345748902,0.7975062251091003,0.7043828368186951,0.7435903310775757 +0.008287002081757484,0.005954895000298649,0.01621208006540676,0.006671031081776789 +qlx +0.7415489315986633,0.7091193556785583,0.5964680075645447,0.7298561930656433 +0.04657356712495086,0.014072393360698711,0.040922895645105165,0.051361052795268966 +saap +0.44514244198799136,0.511269998550415,0.39066665172576903,0.5590876936912537 +0.03428558760967686,0.0033397741735696905,0.010198029301577672,0.06529812974210072 diff --git a/t3.2.1/run/resnet5032-bce256-0.001-50af/config.json b/t3.2.1/run/resnet5032-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..cc920377a8fdc4ee744f13b04ee3f3c4efdab142 --- /dev/null +++ b/t3.2.1/run/resnet5032-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "resnet50", "channels": 32, "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.2.1/run/resnet5032-bce256-0.001-50af/model_1.pth b/t3.2.1/run/resnet5032-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..c7115f2089723e68a8a6b7c349c1a1d3d964b873 --- /dev/null +++ b/t3.2.1/run/resnet5032-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c459781389b063ab2ea600f7f7d3f89ff95407dfb80e4e1c54d6dc3cef1aa114 +size 48689710 diff --git a/t3.2.1/run/resnet5032-bce256-0.001-50af/model_2.pth b/t3.2.1/run/resnet5032-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..6a929a321aeb9205521be698221b94405223018c --- /dev/null +++ b/t3.2.1/run/resnet5032-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:32a4331c2bc52880de40f379080278ac281456d44b12ece15209ce6ced74a9f9 +size 48689710 diff --git a/t3.2.1/run/resnet5032-bce256-0.001-50af/model_3.pth b/t3.2.1/run/resnet5032-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..7e3e80453f0d340c507a612e2b7cdfbd28b25269 --- /dev/null +++ b/t3.2.1/run/resnet5032-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ac112196daf2675420fa2c4a953cc2796925a0446eb251965b9a80526428e614 +size 48689710 diff --git a/t3.2.1/run/resnet5032-bce256-0.001-50af/model_4.pth b/t3.2.1/run/resnet5032-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..90c510a91c824197240a61f05936237141eef2b5 --- /dev/null +++ b/t3.2.1/run/resnet5032-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:961068cd54e7bb000d59097cdc9ec4c34ef9c625b50abc4e1e5c8b9d3c58235d +size 48689710 diff --git a/t3.2.1/run/resnet5032-bce256-0.001-50af/model_5.pth b/t3.2.1/run/resnet5032-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..12d5a120d372cd924e7e615994edc29bb2bd045a --- /dev/null +++ b/t3.2.1/run/resnet5032-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:11badc95da3bcff40ff86caa516dc9a55db456708de7c5110d01d8c57bd36418 +size 48689710 diff --git a/t3.2.1/run/resnet5032-bce256-0.001-50af/result.txt b/t3.2.1/run/resnet5032-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..dec1afac54b306f426c83b6b2bbf066f3cf434f2 --- /dev/null +++ b/t3.2.1/run/resnet5032-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8105508208274841,0.794320547580719,0.7186717629432678,0.7449751257896423 +0.008970725187226483,0.010613968351152874,0.01637079644952252,0.012670480755836305 +qlx +0.7387563586235046,0.7137936115264892,0.6238410830497741,0.726553988456726 +0.04231720450002604,0.019692288353109936,0.023030100225287484,0.04764634281957402 +saap +0.4473957002162933,0.5133458137512207,0.39466664791107176,0.5439144015312195 +0.014932161266855393,0.0035319464843679097,0.008589390959660721,0.02406517604141864 diff --git a/t3.2.1/run/swintf16-bce256-0.001-50af/config.json b/t3.2.1/run/swintf16-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..eecdd8771d5d8078c812f2a5099bc779db6fa005 --- /dev/null +++ b/t3.2.1/run/swintf16-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "swintf", "channels": 16, "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.2.1/run/swintf16-bce256-0.001-50af/model_1.pth b/t3.2.1/run/swintf16-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..91e129e44af066404c2896e20a7182fb7f5b247a --- /dev/null +++ b/t3.2.1/run/swintf16-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:be889ce7595b5d5f419c31eaf47eaba5543f769fad12c64b91c36d2eb6d30f75 +size 3646912 diff --git a/t3.2.1/run/swintf16-bce256-0.001-50af/model_2.pth b/t3.2.1/run/swintf16-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..93329bbc0a53b7077c8ff5c8f2de2aa536000757 --- /dev/null +++ b/t3.2.1/run/swintf16-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:67801f02f22a583c1b90bde7b8b7abe4c9032f2782d8efbdfebb215bbf4364fb +size 3646912 diff --git a/t3.2.1/run/swintf16-bce256-0.001-50af/model_3.pth b/t3.2.1/run/swintf16-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..0da0da8c3bb2c6c21c16e31dec212f10d8fe4c9d --- /dev/null +++ b/t3.2.1/run/swintf16-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:45ef832d8fae0f8aa76d49824c650c1c0e8f41bb29dd854fc4092947551de120 +size 3646912 diff --git a/t3.2.1/run/swintf16-bce256-0.001-50af/model_4.pth b/t3.2.1/run/swintf16-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..c16c250ac796abafa55dbf046dcc1faace603b04 --- /dev/null +++ b/t3.2.1/run/swintf16-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:56c974190e38b1d09fcc1d4e581bcf16c6dc1b8956228ab4041c693c9ee24bbf +size 3646912 diff --git a/t3.2.1/run/swintf16-bce256-0.001-50af/model_5.pth b/t3.2.1/run/swintf16-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..40e24504464478f689eb7f9a9f97a64f7407c68f --- /dev/null +++ b/t3.2.1/run/swintf16-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:34318c51d51c189c622067d972cf7c25483b9fc6ba6d36db11f85c917219ef68 +size 3646912 diff --git a/t3.2.1/run/swintf16-bce256-0.001-50af/result.txt b/t3.2.1/run/swintf16-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..ec7390435f07ff29379f16ac50f8d27b9c1b240d --- /dev/null +++ b/t3.2.1/run/swintf16-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.6510078549385071,0.7783253788948059,0.6371437788009644,0.5230368733406067 +0.00447152530397592,0.003972139967400592,0.005327861659643588,0.007292977625048757 +qlx +0.5675087690353393,0.6924556493759155,0.5364238023757935,0.5183290481567383 +0.00935965342198592,0.0,0.0,0.010232664060513062 +saap +0.42946860790252683,0.5129449367523193,0.38499999046325684,0.5248277306556701 +0.020707839435866102,0.0,0.0,0.02577624185586192 diff --git a/t3.2.1/run/vit16-bce256-0.001-50af/config.json b/t3.2.1/run/vit16-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..fa156a0835c2bc17a9d19eccc0b7120ac3c2a5f7 --- /dev/null +++ b/t3.2.1/run/vit16-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "vit", "channels": 16, "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.2.1/run/vit16-bce256-0.001-50af/model_1.pth b/t3.2.1/run/vit16-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..918968130881828ab3c44509c9a0c2d511dd7044 --- /dev/null +++ b/t3.2.1/run/vit16-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:638bd4237bf9ded9e96be6b8273584d363d390262f25cc426380471abf21eafd +size 286876 diff --git a/t3.2.1/run/vit16-bce256-0.001-50af/model_2.pth b/t3.2.1/run/vit16-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..eadef6722d5d976902c92fd8edce5ae0e4e6df12 --- /dev/null +++ b/t3.2.1/run/vit16-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3de7c231b2df3c3df6b71d2484ce0af5425734fc3dc12c9e8fe94357963c5d2f +size 286876 diff --git a/t3.2.1/run/vit16-bce256-0.001-50af/model_3.pth b/t3.2.1/run/vit16-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..c7e45907af32d50727d882e679530e8a9c73b6da --- /dev/null +++ b/t3.2.1/run/vit16-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9c079bc3bc94d26357da069d233011127df43fe5ce99039cb1218b853afabb01 +size 286876 diff --git a/t3.2.1/run/vit16-bce256-0.001-50af/model_4.pth b/t3.2.1/run/vit16-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..0e68f397d4a817fce3a5fdb6671469b48759a2cc --- /dev/null +++ b/t3.2.1/run/vit16-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5830e48bd92d83efbb8e6a413e4cd22f0845015ec4b6e862e85d0730c0ca9026 +size 286876 diff --git a/t3.2.1/run/vit16-bce256-0.001-50af/model_5.pth b/t3.2.1/run/vit16-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..631cfddd45d98ae1d8d89b9b311f34a9f3a0a444 --- /dev/null +++ b/t3.2.1/run/vit16-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a4e7991c85d01b3dc2043f7d008ed61b59157e21d464254dd62c34dc631e8501 +size 286876 diff --git a/t3.2.1/run/vit16-bce256-0.001-50af/result.txt b/t3.2.1/run/vit16-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..592c62ac31c92b6f49cf10c97cb6817ec1803b4b --- /dev/null +++ b/t3.2.1/run/vit16-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.7357846617698669,0.7629601001739502,0.6456422209739685,0.6328354597091674 +0.019352567605907384,0.01686275234196609,0.012022577170150654,0.026581526381583176 +qlx +0.7031257271766662,0.6949156165122986,0.5565121173858643,0.7093217492103576 +0.013078302558822827,0.004919934272766113,0.0401766300201416,0.019196581650958916 +saap +0.42891687750816343,0.5103235125541687,0.39199999570846555,0.5410133123397827 +0.023067406481162938,0.0034936596241858012,0.012400728711219064,0.02164330956231532 diff --git a/t3.2.1/runthro.sh b/t3.2.1/runthro.sh new file mode 100644 index 0000000000000000000000000000000000000000..43343384c38fee40bfeec189f580677a8cc24d54 --- /dev/null +++ b/t3.2.1/runthro.sh @@ -0,0 +1,7 @@ +#!/bin/bash +python main.py --model resnet34 +python main.py --model resnet50 +python main.py --model densenet +python main.py --model convnext +python main.py --model vit +python main.py --model swintf diff --git a/t3.2.1/summary.py b/t3.2.1/summary.py new file mode 100644 index 0000000000000000000000000000000000000000..420cc50d5a8330d07342a07c2f5bfc482023480a --- /dev/null +++ b/t3.2.1/summary.py @@ -0,0 +1,59 @@ +import os +import csv +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument('--dir', type=str, default='./run') +parser.add_argument('--output', type=str, default='summary.csv') +args = parser.parse_args() + +# 设置目录路径和输出CSV文件名 +directory_path = args.dir +output_csv = args.output + +# 创建CSV文件并写入数据 +with open(output_csv, mode='w', newline='') as file: + writer = csv.writer(file) + + # 写入第一个子表的标题和表头 + writer.writerow(['QLX']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 6: + means_5_6 = lines[4].strip().split(',') + std_devs_5_6 = lines[5].strip().split(',') + if len(means_5_6) == 4 and len(std_devs_5_6) == 4: + values_5_6 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_5_6, std_devs_5_6)] + writer.writerow([subdirectory] + values_5_6) + + # 加入空行以分隔两个子表 + writer.writerow([]) + + # 写入第二个子表的标题和表头 + writer.writerow(['SAAP']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 再次遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 9: + means_8_9 = lines[7].strip().split(',') + std_devs_8_9 = lines[8].strip().split(',') + if len(means_8_9) == 4 and len(std_devs_8_9) == 4: + values_8_9 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_8_9, std_devs_8_9)] + writer.writerow([subdirectory] + values_8_9) \ No newline at end of file diff --git a/t3.2.1/train.py b/t3.2.1/train.py new file mode 100644 index 0000000000000000000000000000000000000000..0204d2cc203fcf064ff5307d7c35ba1ff1cfaec3 --- /dev/null +++ b/t3.2.1/train.py @@ -0,0 +1,61 @@ +import torch +from torchmetrics import F1Score, Accuracy, AveragePrecision, AUROC + + +def train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer): + train_loss = 0 + num_labels = model.classes + avg = args.metric_avg + if num_labels == 1: + task = 'binary' + else: + task = 'multilabel' + metric_macro_acc = Accuracy(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_f1 = F1Score(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_ap = AveragePrecision(average=avg, task=task, num_labels=num_labels).to(device) + metric_auc = AUROC(average=avg, task=task, num_labels=num_labels).to(device) + + if train_loader is not None: + model.train() + for data in train_loader: + voxel, seq, gt = data + # print(seq_lengths) + out = model((voxel.to(device), seq.to(device))) + # print(out[0]) + # print(gt[0]) + loss = criterion(out, gt.to(device).float()) + # loss_0 = criterion(out[0], gt.to(device).float()) + # loss_1 = criterion(out[1], gt.to(device).float()) + # loss_2 = criterion(out[2], gt.to(device).float()) + # loss = loss_0 + loss_1 + loss_2 + loss.backward() + optimizer.step() + optimizer.zero_grad() + train_loss += loss.item() + + model.eval() + preds = [] + gt_list_valid = [] + with torch.no_grad(): + for data in valid_loader: + voxel, seq, gt = data + gt_list_valid.append(gt.to(device)) + out = model((voxel.to(device), seq.to(device))) + preds.append(out) + + # calculate metrics + preds = torch.nn.functional.sigmoid(torch.cat(preds, dim=0)) + gt_list_valid = torch.cat(gt_list_valid, dim=0).int() + # if train_loader is None: + # print(preds) + # print(preds.shape) + # print(gt_list_valid) + # print(gt_list_valid.shape) + + macro_ap = metric_macro_ap(preds, gt_list_valid).item() + # class_ap = [round(i.item(), 5) for i in metric_class_ap(preds, gt_list_valid)] + macro_auc = metric_auc(preds, gt_list_valid).item() + macro_f1 = metric_macro_f1(preds, gt_list_valid).item() + macro_acc = metric_macro_acc(preds, gt_list_valid).item() + return train_loss, macro_ap, macro_f1, macro_acc, macro_auc + diff --git a/t3.2.1/utils.py b/t3.2.1/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..7fd5de2ae11e3a64de85ddc8c0ce0f777a738a48 --- /dev/null +++ b/t3.2.1/utils.py @@ -0,0 +1,32 @@ +import random +import torch +import numpy as np + + +def set_seed(seed): + random.seed(seed) + np.random.seed(seed) + torch.manual_seed(seed) + torch.cuda.manual_seed(seed) + torch.cuda.manual_seed_all(seed) + torch.backends.cudnn.deterministic = True + torch.backends.cudnn.benchmark = False + + +def load_pretrain_model(net, weights): + net_keys = list(net.state_dict().keys()) + weights_keys = list(weights.keys()) + # assert(len(net_keys) <= len(weights_keys)) + i = 0 + j = 0 + while i < len(net_keys) and j < len(weights_keys): + name_i = net_keys[i] + name_j = weights_keys[j] + if net.state_dict()[name_i].shape == weights[name_j].shape: + net.state_dict()[name_i].copy_(weights[name_j].cpu()) + i += 1 + j += 1 + else: + i += 1 + # print i, len(net_keys), j, len(weights_keys) + return net diff --git a/t3.2.2/dataset.py b/t3.2.2/dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..ef68f6e9c9b24c36586f5f1a222be18e2f341a51 --- /dev/null +++ b/t3.2.2/dataset.py @@ -0,0 +1,378 @@ +import os +import networkx as nx +import torch +from Bio.PDB import PDBParser, is_aa +from Bio.PDB.Polypeptide import three_to_one +from torch.utils.data import Dataset +import pandas as pd +import numpy as np +from tqdm import tqdm +from Bio.Align import PairwiseAligner +from torch_geometric.data import Data +from torch_geometric.utils import from_networkx +from Bio.SeqUtils.ProtParam import ProteinAnalysis + + +aligner = PairwiseAligner() +# Define the scoring system +aligner.mode = 'global' # Global alignment +aligner.match_score = 1 +aligner.mismatch_score = -1 +aligner.open_gap_score = -0.5 +aligner.extend_gap_score = -0.1 + + +def calculate_similarity(seq_gen, seq_tem): + + # Perform the alignment + score = aligner.score(seq_gen, seq_tem) + return score / len(seq_gen) + + +def parse_ll37_testset(path='metadata/LL37_v0.csv'): + files = open(path, 'r').readlines() + seqs = [] + for file in files: + seqs.append(str(file.strip().split(',')[-1].strip().upper())) + return seqs + + +def gmean(labels): + # Assuming 'labels' is a NumPy array of shape (n, m) + # Calculate the geometric mean across axis 1 + + # Take the product of elements along axis=1 + product = np.prod(labels, axis=1) + + # nth root of product, where n is the number of elements along axis=1 + geometric_means = product ** (1.0 / labels.shape[1]) + return geometric_means + + +def clamp(n, smallest, largest): + return sorted([smallest, n, largest])[1] + + +ATOMS = {'H': 1, 'C': 12, 'N': 14, 'O': 16, 'S': 30} +ATOMS_R = {'H': 1, 'C': 1.5, 'N': 1.5, 'O': 1.5, 'S': 2} +AMINO_ACID_WATER = {'A': 255, 'V': 255, 'P': 255, 'F': 255, 'W': 255, 'I': 255, 'L': 255, 'G': 155, 'M': 155, + 'Y': 55, 'S': 55, 'T': 55, 'C': 55, 'N': 55, 'Q': 55, 'D': 55, 'E': 55, 'K': 55, 'R': 55, 'H': 55} +AMINO_ACID_CHARGE = {'D': 55, 'E': 55, 'A': 155, 'V': 155, 'P': 155, 'F': 155, 'W': 155, 'I': 155, 'L': 155, 'G': 155, + 'M': 155, 'Y': 155, 'S': 155, 'T': 155, 'C': 155, 'N': 155, 'Q': 155, 'K': 255, 'R': 255, 'H': 255} +# AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, +# 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19} +AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, + 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19, 'X': 21} + + +def pdb_parser(structure): + """ + + """ + voxel = np.zeros((4, 64, 64, 64), dtype=np.int8) + id = '' + seq_str = '' + for i in structure[0]: + id = i.id + chain = structure[0][id] + for res in chain: + if is_aa(res.get_resname(), standard=True): + resname = res.get_resname() + amino = three_to_one(resname) + seq_str += str(amino) + ATOM_WATER = AMINO_ACID_WATER[amino] + ATOM_CHARGE = AMINO_ACID_CHARGE[amino] + ATOM_CATEGORY = AMAs[amino] * 20 + + for i in res: + if i.id not in ATOMS.keys(): + continue + x, y, z = i.get_coord() + if abs(x) > 32: + x = clamp(x, -31, 31) + if abs(y) > 32: + y = clamp(x, -31, 31) + if abs(z) > 32: + z = clamp(x, -31, 31) + x_i, y_i, z_i = int(x) + 32, int(y) + 32, int(z) + 32 + ATOM_WEIGHT = ATOMS[i.id] + ATOM_R = ATOMS_R[i.id] + + if ATOM_R <= 1.5: + voxel[0, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WEIGHT + voxel[1, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WATER + voxel[2, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CHARGE + voxel[3, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CATEGORY + else: + voxel[0, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WEIGHT + voxel[1, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WATER + voxel[2, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CHARGE + voxel[3, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CATEGORY + return voxel + + +def read_scoring_functions(pdb): + scoring = False + profile = [] + for line in open(pdb): + if line.startswith("#END_POSE_ENERGIES_TABLE"): + scoring = False + if scoring: + data = [float(v) for v in line.split()[1:]] + profile.append(data) + if line.startswith("pose"): + scoring = True + return profile + + +def load_aa_features(feature_path): + aa_features = {} + for line in open(feature_path): + line = line.strip().split() + aa, features = line[0], [float(feature) for feature in line[1:]] + aa_features[aa] = features + return aa_features + + +class PairData(Data): + def __init__(self, edge_index_s, x_s): + super(PairData, self).__init__() + self.edge_index_s = edge_index_s + self.x_s = x_s + + def __inc__(self, key, value, *args): + if key == 'edge_index_s': + return self.x_s.size(0) + if key == 'wide_nodes': + return self.x_s.num_nodes + else: + return super().__inc__(key, value, *args) + + +def calculate_property(seq): + # print(seq) + analysed_seq = ProteinAnalysis(seq) + aa_counts = analysed_seq.count_amino_acids() + aliphatic_index = ((aa_counts['A'] + 2.9 * aa_counts['V'] + 3.9 * (aa_counts['I'] + aa_counts['L'])) / len(seq)) + + charged_amino_acids = ['D', 'E', 'R', 'K', 'H'] + total_charged = sum(aa_counts[aa] for aa in charged_amino_acids) + charge_density = total_charged / len(seq) + alpha_helix, beta_helix, turn_helix = analysed_seq.secondary_structure_fraction() + + return list( + [round(analysed_seq.gravy(), 3) * 10, round(aliphatic_index, 3) * 10, round(analysed_seq.aromaticity(), 3) * 10, + round(analysed_seq.instability_index(), 3), round(alpha_helix * 10, 3), round(beta_helix * 10, 3), + round(turn_helix * 10, 3), round(analysed_seq.charge_at_pH(7), 3), round(analysed_seq.isoelectric_point(), 3), + round(charge_density, 3) * 10]) + + +def construct_graph(structure, seq, gt, pdb_path, threshold, max_length): + # Initialize an empty graph + G = nx.Graph() + + # Check if chain 'A' is present in the structure, return None if not + if 'A' not in structure[0]: + return None + chain = structure[0]['A'] + + # Add nodes to the graph for each amino acid in the chain with their 3D positions + for res in chain: + if is_aa(res.get_resname(), standard=True): + resname = res.get_resname() + alpha_c_atom = res["CA"] + x, y, z = alpha_c_atom.get_coord() # Get 3D coordinates + G.add_node(res.id[1], name=resname, position=(x, y, z)) + + # Add edges between nodes if the distance between alpha carbons is <= 5Å + for i, m in enumerate(G.nodes): + for n in list(G.nodes)[i + 1:]: + distance = chain[m]["CA"] - chain[n]["CA"] + if distance <= 5: + G.add_edge(m, n, weight=5 / distance) + + # Load scoring functions and features + scoring = read_scoring_functions(pdb_path) + # print(pdb_path) + aa_features = load_aa_features('metadata/features.txt') + + # Assign features to nodes in the graph, including the 3D position + for node_id, node_data in G.nodes(data=True): + res = chain[node_id] + aa_feature = aa_features[three_to_one(res.get_resname())] + energy_feature = scoring[node_id - 1] + position_feature = list(node_data['position']) # Convert position tuple to list + node_data['x'] = aa_feature + energy_feature + position_feature + # node_data['x'][20:40]=[0 for i in range(20)] + # node_data['x'][40:43]=[0 for i in range(3)] + + # Convert graph to data format used in ML models + G = nx.convert_node_labels_to_integers(G) + data_wt = from_networkx(G) + data_graph = PairData(data_wt.edge_index, data_wt.x) + data_graph.gt = (gt < threshold).astype(int) + global_properties = calculate_property(seq) + + # Encode the sequence and pad it to a fixed length + seq_emb = [AMAs[char] for char in seq] + [0] * (max_length - len(seq)) + + # print(seq_emb) + data_graph.seq = seq_emb + data_graph.global_f = global_properties + return data_graph + + +class MDataset(Dataset): + ''' + x: Features. + y: Targets, if none, do prediction. + ''' + + def __init__(self, threshold=32, mode='train', max_length=50, pdb_src='af', data_ver='0922'): + self.num_classes = 6 + self.num_features = 43 + exclude_list = pd.read_csv('../metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() + exclude_filter = False + + p = PDBParser(QUIET=True) + + if mode == 'train': + all_data = pd.read_csv(f'../metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values + exclude_filter = True + elif mode == 'qlx': + all_data = pd.read_csv('../metadata/data_qlx.csv', encoding="unicode_escape").values + elif mode == 'saap': + all_data = pd.read_csv('../metadata/data_saap.csv', encoding="unicode_escape").values + else: + raise NotImplementedError + idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) + + filter_idx_list = [] + seq_new_list = [] + label_list = [] + for idx in range(len(idx_list)): + seq = seq_list[idx].upper().strip() + if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: + continue + if exclude_filter: + if seq in exclude_list: + continue + + filter_idx_list.append(idx) + seq_new_list.append(seq) + label_list.append(labels[idx]) + + if pdb_src == 'af': + pdb_root = '../pdb/pdb_af/' + elif pdb_src == 'hf': + pdb_root = '../pdb/pdb_dbassp/' + else: + raise NotImplementedError + self.data_list = [] + for i in tqdm(range(len(filter_idx_list))): + idx = filter_idx_list[i] + seq = seq_new_list[i] + label = label_list[i] + + # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"): + # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb" + # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"): + # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb" + if os.path.exists(pdb_root + seq + ".pdb"): + pdb_path = pdb_root + seq + ".pdb" + else: + continue + # raise FileNotFoundError + + structure = p.get_structure(idx, pdb_path) + voxel1 = construct_graph(structure, seq, label, pdb_path, threshold, max_length) + self.data_list.append(voxel1) + + def __getitem__(self, idx): + voxel1 = self.data_list[idx] + + return voxel1 + + def __len__(self): + return len(self.data_list) + + +# class SDataset(Dataset): +# ''' +# x: Features. +# y: Targets, if none, do prediction. +# ''' + +# def __init__(self, threshold=32, mode='train', task='0', max_length=50, pdb_src='af', data_ver='0922'): +# self.num_classes = 1 +# self.task = int(task) +# exclude_list = pd.read_csv('metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() +# exclude_filter = False + +# p = PDBParser(QUIET=True) + +# if mode == 'train': +# all_data = pd.read_csv(f'metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values +# exclude_filter = True +# elif mode == 'qlx': +# all_data = pd.read_csv('metadata/data_qlx.csv', encoding="unicode_escape").values +# elif mode == 'saap': +# all_data = pd.read_csv('metadata/data_saap.csv', encoding="unicode_escape").values +# else: +# raise NotImplementedError +# idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) +# labels = labels[:, self.task:self.task+1] + +# filter_idx_list = [] +# seq_new_list = [] +# label_list = [] +# for idx in range(len(idx_list)): +# seq = seq_list[idx].upper().strip() +# if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: +# continue +# if exclude_filter: +# if seq in exclude_list: +# continue + +# filter_idx_list.append(idx) +# seq_new_list.append(seq) +# label_list.append(labels[idx]) + +# if pdb_src == 'af': +# pdb_root = './pdb/pdb_af/' +# elif pdb_src == 'hf': +# pdb_root = './pdb/pdb_dbassp/' +# else: +# raise NotImplementedError +# self.data_list = [] +# for i in tqdm(range(len(filter_idx_list))): +# idx = filter_idx_list[i] +# seq = seq_new_list[i] +# label = label_list[i] + +# # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"): +# # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb" +# # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"): +# # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb" +# if os.path.exists(pdb_root + seq + ".pdb"): +# pdb_path = pdb_root + seq + ".pdb" +# else: +# continue +# # raise FileNotFoundError + +# structure = p.get_structure(idx, pdb_path) +# voxel1 = construct_graph(structure, seq, label, pdb_path, threshold, max_length) +# self.data_list.append(voxel1) + +# def __getitem__(self, idx): +# voxel1 = self.data_list[idx] + +# return voxel1 + +# def __len__(self): +# return len(self.data_list) + diff --git a/t3.2.2/eval.py b/t3.2.2/eval.py new file mode 100644 index 0000000000000000000000000000000000000000..035c4d57f4a93fb51a813ea83410b38e12a80b03 --- /dev/null +++ b/t3.2.2/eval.py @@ -0,0 +1,209 @@ +import argparse +import json +import logging +import os +import time + +from dataset import SDataset, MDataset +from network import MMGraph +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset +from torch_geometric.loader import DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=64, + help="MIC threshold for determine labels (default: 64)") +parser.add_argument('--num-layer', type=int, dest='num_layer', default=2, + help='number of GNN message passing layers (default: 2)') +parser.add_argument('--emb-dim', type=int, dest='emb_dim', default=90, + help='embedding dimensions (default: 200)') +parser.add_argument('--dropout-ratio', type=float, dest='dropout_ratio', default=0.5, + help='dropout ratio (default: 0.5)') +parser.add_argument('--graph-pooling', type=str, dest='graph_pooling', default="attention", + help='graph level pooling (sum, mean, max, attention)') +parser.add_argument('--gnn-type', type=str, dest='gnn_type', default="gatv2", + help='gnn type (gin, gcn, gat, graphsage)') + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=110, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.002, + help='learning rate (default: 0.002)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='micro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='mlce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + weight_dir = "./run/" + args.task_type + '-' + args.task + "-m-" + args.model + '-' + args.loss + str(args.batch_size) + str(args.lr) + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + raise ValueError + + logging.basicConfig(handlers=[ + # logging.FileHandler(filename=os.path.join(weight_dir, "eval.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + # with open(os.path.join(weight_dir, "config.json"), "w") as f: + # f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src) + else: + set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src) + else: + qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + # train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + + # train_loader = DataLoader(train_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=1, follow_batch=['x_s'], shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=1, follow_batch=['x_s'], shuffle=False) + + if args.model == 'mm': + model = MMGraph(num_layer=args.num_layer, input_dim=43, emb_dim=args.emb_dim, out_dim=set_all.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type=args.gnn_type, max_length=args.max_length) + + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + model.load_state_dict(torch.load(weights_path)) + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + + + if args.task_type in ('mlc', 'slc') : + _, ap, f1, acc, auc = train(args, None, model, None, valid_loader, device, None, None) + logging.info(f'ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, None, model, None, qlx_loader, device, None, None) + logging.info(f'QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, None, model, None, saap_loader, device, None, None) + logging.info(f'SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/eval.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/t3.2.2/infer.py b/t3.2.2/infer.py new file mode 100644 index 0000000000000000000000000000000000000000..5532225df5615a59942bb28b88ad23facdc4fb37 --- /dev/null +++ b/t3.2.2/infer.py @@ -0,0 +1,143 @@ +import argparse +import json +import logging +import os +import time + +from dataset import MDataset +from network import GraphGNN +from sklearn.model_selection import KFold +import pandas as pd +import torch +import torch.nn as nn +from torch_geometric.loader import DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") +parser.add_argument('--num-layer', type=int, dest='num_layer', default=2, + help='number of GNN message passing layers (default: 2)') +parser.add_argument('--emb-dim', type=int, dest='emb_dim', default=128, + help='embedding dimensions (default: 128)') +parser.add_argument('--dropout-ratio', type=float, dest='dropout_ratio', default=0.3, + help='dropout ratio (default: 0.3)') +parser.add_argument('--graph-pooling', type=str, dest='graph_pooling', default="attention", + help='graph level pooling (sum, mean, max, attention)') +parser.add_argument('--gnn-type', type=str, dest='gnn_type', default="gatv2", + help='gnn type (gin, gcn, gat, graphsage)') + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--gcn', type=str, default='./run/gcn-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--gat', type=str, default='./run/gat-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--graphsage', type=str, default='./run/graphsage-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--gin', type=str, default='./run/gin-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--gatv2', type=str, default='./run/gatv2-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +parser.add_argument('--loss', type=str, default='bce', + help='loss function') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + +model_path = {'gcn': args.gcn ,'gat': args.gat, 'graphsage': args.graphsage, 'gin': args.gin, 'gatv2': args.gatv2} + + +def main(): + set_seed(args.seed) + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + results = pd.DataFrame() + + logging.info('Loading Test Dataset') + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + qlx_loader = DataLoader(qlx_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + + models = {'gcn': GraphGNN(num_layer=args.num_layer, input_dim=qlx_set.num_features, emb_dim=args.emb_dim, out_dim=qlx_set.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type='gcn'), + 'gat': GraphGNN(num_layer=args.num_layer, input_dim=qlx_set.num_features, emb_dim=args.emb_dim, out_dim=qlx_set.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type='gat'), + 'graphsage': GraphGNN(num_layer=args.num_layer, input_dim=qlx_set.num_features, emb_dim=args.emb_dim, out_dim=qlx_set.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type='graphsage'), + 'gin': GraphGNN(num_layer=args.num_layer, input_dim=qlx_set.num_features, emb_dim=args.emb_dim, out_dim=qlx_set.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type='gin'), + 'gatv2': GraphGNN(num_layer=args.num_layer, input_dim=qlx_set.num_features, emb_dim=args.emb_dim, out_dim=qlx_set.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type='gatv2')} + + for model_name in model_path.keys(): + model = models[model_name] + pred_all = [] + gt_all = [] + for i in range(1,6): + model.load_state_dict(torch.load(os.path.join(model_path[model_name], f'model_{i}.pth'))) + model.to(device).eval() + with torch.no_grad(): + for data in qlx_loader: + data = data.to(device) + gt_all.append(torch.tensor(data.gt, device=device)) + out = model(data) + pred_all.append(out) + pred_all = torch.nn.functional.sigmoid(torch.cat(pred_all, dim=0)).squeeze().cpu().numpy() + gt_all = torch.cat(gt_all, dim=0).int().squeeze().cpu().numpy() + results[model_name] = pred_all.ravel(order='F') + + results['gt'] = gt_all.ravel(order='F') + + results.to_csv("preds.csv", index=False) + + +if __name__ == "__main__": + main() diff --git a/t3.2.2/loss.py b/t3.2.2/loss.py new file mode 100644 index 0000000000000000000000000000000000000000..d2c1cabdc7bb5df53b8513344372ea557b89aa29 --- /dev/null +++ b/t3.2.2/loss.py @@ -0,0 +1,123 @@ +import torch +from torch import nn +import torch.nn.functional as F +from math import cos, pi, sin +import math +import numpy as np +from scipy.special import lambertw + + +def mixup_criterion(criterion, pred, y_a, y_b, lam, pow=2): + y = lam ** pow * y_a + (1 - lam) ** pow * y_b + return criterion(pred, y) + + +def mixup_data(v, q, a): + '''Returns mixed inputs, pairs of targets, and lambda without organ constraint''' + lam = np.random.beta(1, 1) + + batch_size = v.shape[0] + index = torch.randperm(batch_size) + + mixed_v = lam * v + (1 - lam) * v[index, :] + mixed_q = lam * q + (1 - lam) * q[index, :] + + a_1, a_2 = a, a[index] + return mixed_v, mixed_q, a_1, a_2, lam + + +def linear(epoch, nepoch): + return 1 - epoch / nepoch + + +def convex(epoch, nepoch): + return epoch / (2 - nepoch) + + +def concave(epoch, nepoch): + return 1 - sin((epoch / nepoch) * (pi / 2)) + + +def composite(epoch, nepoch): + return 0.5 * cos((epoch / nepoch) * pi) + 0.5 + + +class LogCoshLoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y_t, y_prime_t): + ey_t = y_t - y_prime_t + return torch.mean(torch.log(torch.cosh(ey_t + 1e-12)))+F.mse_loss(y_t, y_prime_t) + + +class MLCE(nn.Module): + def __init__(self): + super(MLCE, self).__init__() + + def _mlcce(self, y_pred, y_true): + y_pred = (1 - 2 * y_true) * y_pred + y_pred_neg = y_pred - y_true * 1e12 + y_pred_pos = y_pred - (1 - y_true) * 1e12 + zeros = torch.zeros_like(y_pred[..., :1]) + y_pred_neg = torch.cat([y_pred_neg, zeros], dim=-1) + y_pred_pos = torch.cat([y_pred_pos, zeros], dim=-1) + neg_loss = torch.logsumexp(y_pred_neg, dim=-1) + pos_loss = torch.logsumexp(y_pred_pos, dim=-1) + loss = torch.mean(neg_loss + pos_loss) + return loss + + def __call__(self, y_pred, y_true): + return self._mlcce(y_pred, y_true) + + +class SuperLoss(nn.Module): + def __init__(self, C=10, lam=1, batch_size=128): + super(SuperLoss, self).__init__() + self.tau = math.log(C) + self.lam = lam # set to 1 for CIFAR10 and 0.25 for CIFAR100 + self.batch_size = batch_size + + def forward(self, logits, targets): + l_i = F.mse_loss(logits, targets, reduction='none').detach() + sigma = self.sigma(l_i) + loss = (F.mse_loss(logits, targets, reduction='none') - self.tau) * sigma + self.lam * ( + torch.log(sigma) ** 2) + loss = loss.sum() / self.batch_size + return loss + + def sigma(self, l_i): + x = torch.ones(l_i.size()) * (-2 / math.exp(1.)) + x = x.cuda() + y = 0.5 * torch.max(x, (l_i - self.tau) / self.lam) + y = y.cpu().numpy() + sigma = np.exp(-lambertw(y)) + sigma = sigma.real.astype(np.float32) + sigma = torch.from_numpy(sigma).cuda() + return sigma + + +def unbiased_curriculum_loss(out, data, args, epoch, epochs, scheduler='linear'): + losses = [] + scheduler = linear if scheduler == 'linear' else concave + + # calculate difficulty measurement function + adjusted_losses = [] + for idx in range(out.shape[0]): + ground_truth = max(1, abs(data[idx].item())) + loss = F.mse_loss(out[idx], data[idx]) + losses.append(loss) + adjusted_losses.append(loss.item() / ground_truth) + + mean_loss, std_loss = np.mean(adjusted_losses), np.std(adjusted_losses) + + # re-weight losses + total_loss = 0 + for i, loss in enumerate(losses): + if adjusted_losses[i] > mean_loss + 1 * std_loss: + schedule_factor = scheduler(epoch, args.epochs) + total_loss += schedule_factor * loss + else: + total_loss += loss + + return total_loss diff --git a/t3.2.2/main.py b/t3.2.2/main.py new file mode 100644 index 0000000000000000000000000000000000000000..636653cd2f472a27ca92cb245dc3f301d3f2218e --- /dev/null +++ b/t3.2.2/main.py @@ -0,0 +1,244 @@ +import argparse +import json +import logging +import os +import time + +from dataset import SDataset, MDataset +from network import GraphGNN +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset +from torch_geometric.loader import DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") +parser.add_argument('--num-layer', type=int, dest='num_layer', default=2, + help='number of GNN message passing layers (default: 2)') +parser.add_argument('--emb-dim', type=int, dest='emb_dim', default=128, + help='embedding dimensions (default: 128)') +parser.add_argument('--dropout-ratio', type=float, dest='dropout_ratio', default=0.3, + help='dropout ratio (default: 0.3)') +parser.add_argument('--graph-pooling', type=str, dest='graph_pooling', default="attention", + help='graph level pooling (sum, mean, max, attention)') +parser.add_argument('--gnn-type', type=str, dest='gnn_type', default="gatv2", + help='gnn type (gin, gcn, gat, graphsage)') + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + if args.loss == 'mlce' and args.task_type != 'slc': + criterion = MLCE() + elif args.loss == "bce" or args.task_type == 'slc': + args.loss = "bce" + criterion = nn.BCEWithLogitsLoss() + else: + criterion = 0 + pass + weight_dir = "./run/" + args.gnn_type + '-' + args.loss + str(args.batch_size) + '-' + str(args.lr) + '-' + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + os.makedirs(weight_dir) + + logging.basicConfig(handlers=[ + logging.FileHandler(filename=os.path.join(weight_dir, "training.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + with open(os.path.join(weight_dir, "config.json"), "w") as f: + f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src, data_ver=args.data_ver) + # else: + # set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src, data_ver=args.data_ver) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src) + # else: + # qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + # saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + # if args.loss == 'bce': + # weight = torch.Tensor([0] * args.classes) + # for i in train_set: + # weight += i[2] + # weight = (len(train_set) - weight) / weight + # criterion.register_buffer('pos_weight', weight.to(device)) + + train_loader = DataLoader(train_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + + if args.model == 'mm': + model = GraphGNN(num_layer=args.num_layer, input_dim=set_all.num_features, emb_dim=args.emb_dim, out_dim=set_all.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type=args.gnn_type) + + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + # optimizer = torch.optim.Adam(model.parameters()) + optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr) + # optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=True, weight_decay=5e-5) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Train set:{len(train_set)}, Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + best_metric = 0 + best_qlx = 0 + best_saap = 0 + start_time = time.time() + + for epoch in range(1, args.epochs + 1): + if args.task_type in ('mlc', 'slc') : + train_loss, ap, f1, acc, auc = train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer) + logging.info(f'Epoch: {epoch:03d}, Train Loss: {train_loss:.3f}, ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + avg_metric = ap + f1 + acc + auc + if avg_metric > best_metric: + logging.info(f'Epoch: {epoch:03d} New Best validation metrics, running test session') + torch.save(model.state_dict(), weights_path) + best_metric = avg_metric + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, epoch, model, None, qlx_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_metric = qlx_ap + qlx_f1 + qlx_acc + qlx_auc + if qlx_metric > best_qlx: + best_qlx = qlx_metric + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, epoch, model, None, saap_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_metric = saap_ap + saap_f1 + saap_acc + saap_auc + if saap_metric > best_saap: + best_saap = saap_metric + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'used time {(time.time()-start_time)/3600:.2f}h') + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/result.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/t3.2.2/network.py b/t3.2.2/network.py new file mode 100644 index 0000000000000000000000000000000000000000..65dbdc7bbdeb55e1031f4c397a3cd2e09c1b719b --- /dev/null +++ b/t3.2.2/network.py @@ -0,0 +1,367 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F +import numpy as np +from torch.nn import Sequential, Linear, ReLU +from torch_geometric.nn import GraphConv, GINConv, GATConv, SAGEConv, GPSConv, GINEConv, GATv2Conv +from torch_geometric.nn import global_mean_pool, global_add_pool, global_max_pool, GlobalAttention, Set2Set, MulAggregation +from torch_geometric.nn import aggr +from torch_geometric.nn import GraphNorm + + +import torch +import torch.nn as nn +import torch.nn.functional as F + +class SimpleSelfAttention(nn.Module): + def __init__(self, embedding_dim, num_heads=1): + super(SimpleSelfAttention, self).__init__() + self.embedding_dim = embedding_dim + self.num_heads = num_heads + + # Assuming key_channels = value_channels = embedding_dim + self.key_channels = self.embedding_dim + self.value_channels = self.embedding_dim + + # Linear layers for queries, keys, and values + self.query = nn.Linear(embedding_dim, self.key_channels * num_heads) + self.key = nn.Linear(embedding_dim, self.key_channels * num_heads) + self.value = nn.Linear(embedding_dim, self.value_channels * num_heads) + + # Output projection layer + self.proj = nn.Linear(self.value_channels * num_heads, embedding_dim) + + # Scaling for dot-product attention + self.scale = nn.Parameter(torch.sqrt(torch.FloatTensor([self.key_channels // num_heads]))) + + def forward(self, x1, x2, x3): + # x1, x2, x3 shapes: [Batch_size, Embedding_dim] + # Stack the inputs along a new dimension (sequence dimension) + batch_size = x1.shape[0] + x = torch.stack((x1, x2, x3), dim=1) # [Batch_size, 3, Embedding_dim] + + # Compute queries, keys, values for all three inputs + Q = self.query(x) # [Batch_size, 3, num_heads * embedding_dim] + K = self.key(x) # [Batch_size, 3, num_heads * embedding_dim] + V = self.value(x) # [Batch_size, 3, num_heads * embedding_dim] + + # Reshape for multi-head attention + Q = Q.view(batch_size, -1, self.num_heads, self.embedding_dim).transpose(1, 2) # [Batch_size, num_heads, 3, embedding_dim] + K = K.view(batch_size, -1, self.num_heads, self.embedding_dim).transpose(1, 2) # [Batch_size, num_heads, 3, embedding_dim] + V = V.view(batch_size, -1, self.num_heads, self.embedding_dim).transpose(1, 2) # [Batch_size, num_heads, 3, embedding_dim] + + # Calculate dot product attention + attention_scores = torch.matmul(Q, K.transpose(-2, -1)) / self.scale + attention = F.softmax(attention_scores, dim=-1) + + # Apply attention to V + x = torch.matmul(attention, V) # [Batch_size, num_heads, 3, embedding_dim] + + # Concatenate heads and put through final linear layer + x = x.transpose(1, 2).reshape(batch_size, -1, self.num_heads * self.embedding_dim) + x = self.proj(x) # [Batch_size, 3, embedding_dim] + + # Sum the outputs from the three inputs + out = x.sum(dim=1) # [Batch_size, embedding_dim] + return out + +def cosine_similarity(x,y): + num = x.dot(y.T) + denom = np.linalg.norm(x) * np.linalg.norm(y) + return num / denom + +class LogCoshLoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y_t, y_prime_t): + ey_t = y_t - y_prime_t + return torch.mean(torch.log(torch.cosh(ey_t + 1e-12))) + + +class WeightedMSELoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y, y_t, weights=None): + loss = (y - y_t) ** 2 + if weights is not None: + loss *= weights.expand_as(loss) + return torch.mean(loss) + + +class GNN(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, JK="last", drop_ratio=0, gnn_type="gin"): + super(GNN, self).__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + # self.fc2 = nn.Linear(200, 200) + self.gnns = torch.nn.ModuleList() + for layer in range(num_layer): + in_dim = input_dim if layer == 0 else emb_dim + if gnn_type == "gin": + # self.gnns.append(GINConv(nn.Sequential(nn.Linear(in_dim, emb_dim), nn.BatchNorm1d(emb_dim), nn.ReLU(), + # nn.Linear(emb_dim, emb_dim)))) + self.gnns.append(GINConv(nn.Sequential(nn.Linear(in_dim, emb_dim), GraphNorm(emb_dim), nn.ReLU(), + nn.Linear(emb_dim, emb_dim), nn.ReLU()))) + elif gnn_type == "gps": + nn_ = Sequential( + Linear(in_dim, emb_dim), + ReLU(), + Linear(emb_dim, emb_dim), + ) + conv = GPSConv(emb_dim, GINEConv(nn_), heads=4) + self.gnns.append(conv) + elif gnn_type == "gcn": + self.gnns.append(GraphConv(in_dim, emb_dim)) + elif gnn_type == "gat": + self.gnns.append(GATConv(in_dim, emb_dim)) + elif gnn_type == "gatv2": + self.gnns.append(GATv2Conv(in_dim, emb_dim)) + elif gnn_type == "graphsage": + self.gnns.append(SAGEConv(in_dim, emb_dim)) + else: + raise ValueError("Invalid GNN type.") + + def forward(self, x, edge_index, edge_attr=None): + h_list = [x] + mut_site = [] + for layer in range(self.num_layer): + + h = self.gnns[layer](h_list[layer], edge_index, edge_attr) + # if layer == self.num_layer - 1: + # # remove relu from the last layer + # h = F.dropout(h, self.drop_ratio, training=self.training) + # else: + # h = F.dropout(F.relu(h), self.drop_ratio, training=self.training) # F.relu() + h_list.append(h) + # if len(h_list) == 2: + # previous_mut_site_feature = h_list[-2][mut_res_idx] + # current_mut_site_feature = h_list[-1][mut_res_idx] + # # print(previous_mut_site_feature.shape, current_mut_site_feature.shape) + # h_feature = self.global_encoder(previous_mut_site_feature) + # h_list[-1][mut_res_idx] = h_feature + current_mut_site_feature + # if len(h_list) == 3: + # previous_mut_site_feature = h_list[-2][mut_res_idx].squeeze(0) + # current_mut_site_feature = h_list[-1][mut_res_idx].squeeze(0) + + # h_feature = self.fc2(previous_mut_site_feature) + current_mut_site_feature + # h_list[-1][mut_res_idx] = h_feature.unsqueeze(0) + # mut_site.append() + # print(len(h_list)) + if self.JK == "last": + node_representation = h_list[-1] + elif self.JK == "sum": + h_list = [h.unsqueeze_(0) for h in h_list] + node_representation = torch.sum(torch.cat(h_list[1:], dim=0), dim=0) + # print('node_rep', node_representation.shape) + return h_list[-1] + + +# orthogonal initialization +def init_gru_orth(model, gain=1): + model.reset_parameters() + # orthogonal initialization of gru weights + for _, hh, _, _ in model.all_weights: + for i in range(0, hh.size(0), model.hidden_size): + torch.nn.init.orthogonal_(hh[i:i + model.hidden_size], gain=gain) + + +def init_lstm_orth(model, gain=1): + init_gru_orth(model, gain) + + # positive forget gate bias (Jozefowicz es at. 2015) + for _, _, ih_b, hh_b in model.all_weights: + l = len(ih_b) + ih_b[l // 4: l // 2].data.fill_(1.0) + hh_b[l // 4: l // 2].data.fill_(1.0) + + +class GraphGNN(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, out_dim, JK="last", drop_ratio=0.5, graph_pooling="attention", + gnn_type="gat", concat_type=None, fds=False, feature_level='both', contrast_curri=False) -> object: + super(GraphGNN, self).__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + self.input_dim = input_dim + self.emb_dim = emb_dim + self.out_dim = out_dim + self.concat_type = concat_type + self.feature_level = feature_level + self.contrast_curri = contrast_curri + + self.fc = nn.Sequential( + nn.Linear(self.emb_dim, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), nn.Linear(self.emb_dim, self.out_dim)) + + self.gnn = GNN(num_layer, input_dim, emb_dim, JK, drop_ratio, gnn_type=gnn_type) + + if graph_pooling == "sum": + self.pool = global_add_pool + elif graph_pooling == "mean": + self.pool = global_mean_pool + elif graph_pooling == "max": + self.pool = global_max_pool + elif graph_pooling == "mul": + self.pool = MulAggregation() + elif graph_pooling == "attention": + self.pool = GlobalAttention(gate_nn=torch.nn.Linear(emb_dim, 1)) + elif graph_pooling == "set2set": + self.pool = Set2Set(emb_dim, processing_steps=2) + elif graph_pooling == "lstm": + self.pool = aggr.LSTMAggregation(emb_dim, emb_dim) + else: + raise ValueError("Invalid graph pooling type.") + + def forward_once(self, x, edge_index, batch): + node_representation = self.gnn(x, edge_index) + graph_rep = self.pool(node_representation, batch) + return graph_rep + + def forward(self, data): + graph_rep_be = self.forward_once(data.x_s, data.edge_index_s, data.x_s_batch) + x = self.fc(graph_rep_be) + return x + + + +class MMGraph(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, out_dim, JK="last", drop_ratio=0.5, graph_pooling="attention", + gnn_type="gat", concat_type=None, fds=False, feature_level='both', contrast_curri=False, max_length=50) -> object: + super(MMGraph, self).__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + self.input_dim = input_dim + self.emb_dim = emb_dim + self.out_dim = out_dim + self.concat_type = concat_type + self.feature_level = feature_level + self.contrast_curri = contrast_curri + + self.graph_pool = nn.Linear(self.emb_dim, 1) + + self.fc = nn.Sequential( + nn.Linear(self.emb_dim, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + nn.Linear(self.emb_dim, self.out_dim)) + + if fds: + self.dir = True + else: + self.dir = False + self.global_encoder = nn.Sequential(nn.Linear(10, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio),) + self.seq_encoder = nn.Sequential( + nn.Linear(max_length, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + ) + self.gnn = GNN(num_layer, input_dim, emb_dim, JK, drop_ratio, gnn_type=gnn_type) + + if graph_pooling == "sum": + self.pool = global_add_pool + elif graph_pooling == "mean": + self.pool = global_mean_pool + elif graph_pooling == "max": + self.pool = global_max_pool + elif graph_pooling == "mul": + self.pool = MulAggregation() + elif graph_pooling == "attention": + self.pool = GlobalAttention(gate_nn=torch.nn.Linear(emb_dim, 1)) + elif graph_pooling == "set2set": + self.pool = Set2Set(emb_dim, processing_steps=2) + elif graph_pooling == "lstm": + self.pool = aggr.LSTMAggregation(emb_dim, emb_dim) + else: + raise ValueError("Invalid graph pooling type.") + self.att = SimpleSelfAttention(emb_dim, num_heads=4) + + def forward_once(self, x, edge_index, batch): + node_representation = self.gnn(x, edge_index) + graph_rep = self.pool(node_representation, batch) + return graph_rep + + def forward(self, data): + seq1, global_1 = data.seq, data.global_f + device = self.graph_pool.bias.device + seq1 = torch.tensor(seq1, dtype=torch.float, device=device) + global_1 = torch.tensor(global_1, dtype=torch.float, device=device) + + graph_rep_be = self.forward_once(data.x_s, data.edge_index_s, data.x_s_batch) + seq1_rep_be = self.seq_encoder(seq1) + global1 = self.global_encoder(global_1) + + a1 = self.att(graph_rep_be, seq1_rep_be, global1) + return self.fc(a1) + + + +class PMMGraph(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, out_dim, JK="last", drop_ratio=0.5, graph_pooling="attention", + gnn_type="gat", concat_type=None, fds=False, feature_level='both', contrast_curri=False): + super(PMMGraph, self).__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + self.input_dim = input_dim + self.emb_dim = emb_dim + self.out_dim = out_dim + self.concat_type = concat_type + self.feature_level = feature_level + self.contrast_curri = contrast_curri + + # Define the learnable prompt token + self.prompt_token = nn.Parameter(torch.randn(1, 10)) + + self.graph_pool = nn.Linear(self.emb_dim, 1) + + self.fc = nn.Sequential( + nn.Linear(self.emb_dim + 10, self.emb_dim), # Adjust input size to include the prompt token + nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + nn.Linear(self.emb_dim, self.out_dim)) + + self.dir = fds + self.global_encoder = nn.Sequential(nn.Linear(10, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio)) + self.seq_encoder = nn.Sequential( + nn.Linear(30, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + ) + self.gnn = GNN(num_layer, input_dim, emb_dim, JK, drop_ratio, gnn_type=gnn_type) + + # Initialize pooling based on the specified type + if graph_pooling in ["sum", "mean", "max", "mul", "attention", "set2set", "lstm"]: + pooling_classes = { + "sum": global_add_pool, + "mean": global_mean_pool, + "max": global_max_pool, + "mul": aggr.MulAggregation(), + "attention": GlobalAttention(gate_nn=torch.nn.Linear(emb_dim, 1)), + "set2set": Set2Set(emb_dim, processing_steps=2), + "lstm": aggr.LSTMAggregation(emb_dim, emb_dim) + } + self.pool = pooling_classes[graph_pooling] + else: + raise ValueError("Invalid graph pooling type.") + + self.att = SimpleSelfAttention(emb_dim + 10, num_heads=4) # Adjust for prompt dimension + + def forward_once(self, x, edge_index, batch): + node_representation = self.gnn(x, edge_index) + graph_rep = self.pool(node_representation, batch) + # Concatenate the prompt token + graph_rep = torch.cat([graph_rep, self.prompt_token.expand(graph_rep.size(0), -1)], dim=1) + return graph_rep + + def forward(self, data): + seq1 = torch.tensor(np.array(data.seq, dtype=np.float32)).to(device='cuda') + global_1 = torch.tensor(np.array(data.global_f, dtype=np.float32)).to(device='cuda') + + graph_rep_be = self.forward_once(data.x_s, data.edge_index_s, data.x_s_batch) + seq1_rep_be = self.seq_encoder(seq1) + global1_rep = self.global_encoder(global_1) + # Concatenate the prompt token to other representations as well + seq1_rep_be = torch.cat([seq1_rep_be, self.prompt_token.expand(seq1_rep_be.size(0), -1)], dim=1) + global1_rep = torch.cat([global1_rep, self.prompt_token.expand(global1_rep.size(0), -1)], dim=1) + + # Process combined representations + a1 = self.att(graph_rep_be, seq1_rep_be, global1_rep) + return self.fc(a1) + + diff --git a/t3.2.2/run/gat-bce256-0.001-50af/config.json b/t3.2.2/run/gat-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..2823ae1aa7ea9c7dd0586bb5446d39fa4390d9e3 --- /dev/null +++ b/t3.2.2/run/gat-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "mm", "fusion": "1", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gat", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.2.2/run/gat-bce256-0.001-50af/model_1.pth b/t3.2.2/run/gat-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..1ec087e5258da444e2ce1d58127ab63ca2304cb7 --- /dev/null +++ b/t3.2.2/run/gat-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1c7775fcd745afa4f7c284eb05edfab6220c2d9777a07e025b63da6cc4f16dc4 +size 165036 diff --git a/t3.2.2/run/gat-bce256-0.001-50af/model_2.pth b/t3.2.2/run/gat-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..40ba031344fd38910f884d9c583374fc0c21bde6 --- /dev/null +++ b/t3.2.2/run/gat-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cb02b5ceccaac5b3f92f39b3c1c3036caa824d67b590a35b622e7a6aa9d21d9f +size 165036 diff --git a/t3.2.2/run/gat-bce256-0.001-50af/model_3.pth b/t3.2.2/run/gat-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..b65ead1e08808b68a29bb2f27293f33f78c26e4b --- /dev/null +++ b/t3.2.2/run/gat-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0a57f94f968358c76343addc2b9e9c915d230852b5a562284298db5fbf915d38 +size 165036 diff --git a/t3.2.2/run/gat-bce256-0.001-50af/model_4.pth b/t3.2.2/run/gat-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..343e1b312da26a30f144ae4a93b815034650b9ec --- /dev/null +++ b/t3.2.2/run/gat-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ecd6f57b3b53a60c22faba93d853253aa152f331a31fa0c52292d72d1bff27dd +size 165036 diff --git a/t3.2.2/run/gat-bce256-0.001-50af/model_5.pth b/t3.2.2/run/gat-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..405cecc31a043c942b32290a7947e7e2c36aae01 --- /dev/null +++ b/t3.2.2/run/gat-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a9844e2d78a8c1f1ba3a2d57676e44cd58489ffdd4e2937c68b057a2fda1d29e +size 165036 diff --git a/t3.2.2/run/gat-bce256-0.001-50af/result.txt b/t3.2.2/run/gat-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..ecfa69c75d8debf3a88e32d7422776c4241d70fd --- /dev/null +++ b/t3.2.2/run/gat-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8628148436546326,0.8265277981758118,0.7665179371833801,0.8089367270469665 +0.00864809930586221,0.007544044214974885,0.011198959111616837,0.011721431762246015 +qlx +0.803301191329956,0.7617238879203796,0.6790287017822265,0.8287477850914001 +0.019255763527499217,0.009075946088895562,0.0190588604122177,0.013275452710174936 +saap +0.5845708608627319,0.5198760032653809,0.437666654586792,0.647720468044281 +0.026648692018761452,0.005629440220328842,0.03432849079536525,0.021330601739710745 diff --git a/t3.2.2/run/gat-bce256-0.001-50hf/config.json b/t3.2.2/run/gat-bce256-0.001-50hf/config.json new file mode 100644 index 0000000000000000000000000000000000000000..91f9c127b1ec176886c09a0d2e10afca1fa8b13e --- /dev/null +++ b/t3.2.2/run/gat-bce256-0.001-50hf/config.json @@ -0,0 +1 @@ +{"model": "mm", "fusion": "1", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gat", "pdb_src": "hf", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.2.2/run/gat-bce256-0.001-50hf/model_1.pth b/t3.2.2/run/gat-bce256-0.001-50hf/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..5cc4325fd81e16b3da95317af0d6e8a6fec53612 --- /dev/null +++ b/t3.2.2/run/gat-bce256-0.001-50hf/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1dbfd1c0c1bc68b078502129df52ebe6b87bfbf5cb097b0ccbdf41e4dc6a9a9f +size 165036 diff --git a/t3.2.2/run/gat-bce256-0.001-50hf/model_2.pth b/t3.2.2/run/gat-bce256-0.001-50hf/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..a7b443fd8b0adfee98ddf0cb18bd55875f22b6a2 --- /dev/null +++ b/t3.2.2/run/gat-bce256-0.001-50hf/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1500415d3545542e34dea67ffbb04a927a522f95672c69a08bc785b3b50ac566 +size 165036 diff --git a/t3.2.2/run/gat-bce256-0.001-50hf/model_3.pth b/t3.2.2/run/gat-bce256-0.001-50hf/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..f5c7ffd6055c52575a78e6b7a073533dbeb2b98c --- /dev/null +++ b/t3.2.2/run/gat-bce256-0.001-50hf/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:90094651ca2c74a05c0593f1da24a2ad9e374b886ff33a0c81f923902c1b6464 +size 165036 diff --git a/t3.2.2/run/gat-bce256-0.001-50hf/model_4.pth b/t3.2.2/run/gat-bce256-0.001-50hf/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..73b6c7dbb6b874249d04e4b29e6a4ea6fff8387f --- /dev/null +++ b/t3.2.2/run/gat-bce256-0.001-50hf/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d7b035f5a60a04f1d821758e5690ad7ae7f667f278fe7d97e4ab5019da8715a6 +size 165036 diff --git a/t3.2.2/run/gat-bce256-0.001-50hf/model_5.pth b/t3.2.2/run/gat-bce256-0.001-50hf/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..e95a3b129c057e16b87951c1e2c1a4ae857c0257 --- /dev/null +++ b/t3.2.2/run/gat-bce256-0.001-50hf/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ad022184864e8be6faa761265e0f3a16c21c7f33aef99dfb812be40145c52d3f +size 165036 diff --git a/t3.2.2/run/gat-bce256-0.001-50hf/result.txt b/t3.2.2/run/gat-bce256-0.001-50hf/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..965d2ae91dda6700ae8ca807989ed7ce15deaf5e --- /dev/null +++ b/t3.2.2/run/gat-bce256-0.001-50hf/result.txt @@ -0,0 +1,9 @@ +Valid +0.8678158640861511,0.8276983141899109,0.7667606115341187,0.8145498037338257 +0.007307591799000196,0.007563696917747784,0.008260366815825496,0.010376896964692763 +qlx +0.8106412410736084,0.7361466646194458,0.6346578240394593,0.7895317077636719 +0.01895400237348519,0.0077296457094694045,0.012642671366490566,0.02859959774827979 +saap +0.5732253432273865,0.5141594171524048,0.39199998378753664,0.6489122390747071 +0.019402079492265772,0.0010933037788179202,0.009333324432373048,0.011192892666658398 diff --git a/t3.2.2/run/gatv2-bce256-0.001-50af/config.json b/t3.2.2/run/gatv2-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..1622649599801296c1a481a56cb93cb751ed0018 --- /dev/null +++ b/t3.2.2/run/gatv2-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "mm", "fusion": "1", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gatv2", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.2.2/run/gatv2-bce256-0.001-50af/model_1.pth b/t3.2.2/run/gatv2-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..a3c091f40bb1fdd0bc74bdae4df45c33b588dbaf --- /dev/null +++ b/t3.2.2/run/gatv2-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0b88a161cef3c3520a686b3ed3bc54e29d2b424e6898f9e71ac9732b2392fe03 +size 254752 diff --git a/t3.2.2/run/gatv2-bce256-0.001-50af/model_2.pth b/t3.2.2/run/gatv2-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..defe15ed5a8e40ecbb4e3aa50274e74cb157d906 --- /dev/null +++ b/t3.2.2/run/gatv2-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e46cf7dd53b2d415d3eba239b17dbf2ad350a9924e2c483d539f9ce25616a09f +size 254752 diff --git a/t3.2.2/run/gatv2-bce256-0.001-50af/model_3.pth b/t3.2.2/run/gatv2-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..05bc3c60d07028e9d253286d07ab9595de5724f7 --- /dev/null +++ b/t3.2.2/run/gatv2-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f6e4fdc26f4bd4e542c7252faa1d44b07864e60338aa5ae02a804b5037479334 +size 254752 diff --git a/t3.2.2/run/gatv2-bce256-0.001-50af/model_4.pth b/t3.2.2/run/gatv2-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..b9631c2105468ad2e6660e827edac1c466cd3fec --- /dev/null +++ b/t3.2.2/run/gatv2-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:31c45c9591cb198bb12956d51d073d4d3f28da92e106be1e9beffba667b6e232 +size 254752 diff --git a/t3.2.2/run/gatv2-bce256-0.001-50af/model_5.pth b/t3.2.2/run/gatv2-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..6b9b6cfcf62b26cd24641c0492ec99325f57029d --- /dev/null +++ b/t3.2.2/run/gatv2-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:702a69863645dacd4da317956db365ee18cea575b77fc758bbd476458f5b664f +size 254752 diff --git a/t3.2.2/run/gatv2-bce256-0.001-50af/result.txt b/t3.2.2/run/gatv2-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..6365046670af71fa91792aa1bf1f92c958a5bc70 --- /dev/null +++ b/t3.2.2/run/gatv2-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8614276885986328,0.8246758222579956,0.7673438429832459,0.8068701028823853 +0.009870437944478403,0.009641075802452027,0.012371011823285963,0.013054548257387303 +qlx +0.7868559718132019,0.7362469911575318,0.6284768104553222,0.8151044726371766 +0.01567121031245636,0.012889898788913261,0.02478508285780525,0.02204428042917696 +saap +0.5876142024993897,0.5170042514801025,0.4226666510105133,0.6513442993164062 +0.01733999684148333,0.00545090175922666,0.036660608262893316,0.020795634384317602 diff --git a/t3.2.2/run/gcn-bce256-0.001-50af/config.json b/t3.2.2/run/gcn-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..314775da4537cd703bc44fcf61ff830cc358cd8c --- /dev/null +++ b/t3.2.2/run/gcn-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "mm", "fusion": "1", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gcn", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.2.2/run/gcn-bce256-0.001-50af/model_1.pth b/t3.2.2/run/gcn-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..c1ff4395850e6e3bc852aede6259dcfc42e77398 --- /dev/null +++ b/t3.2.2/run/gcn-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ac63ed7ce6716656f3b0f2f2e80caa94538d566dea6ba5c57971fc0ad90146f5 +size 250034 diff --git a/t3.2.2/run/gcn-bce256-0.001-50af/model_2.pth b/t3.2.2/run/gcn-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..8916a541df19801a62a5a3128a40b77d7b2b3818 --- /dev/null +++ b/t3.2.2/run/gcn-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:495b2c22d886361dd28360c4e52e7a41eec224564acd578bd3987949e692598f +size 250034 diff --git a/t3.2.2/run/gcn-bce256-0.001-50af/model_3.pth b/t3.2.2/run/gcn-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..c1cabf87b4d800f0786de1e317a3b4de2a402cb0 --- /dev/null +++ b/t3.2.2/run/gcn-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8cf4d3bb94290069bce74043f94e36fdd032b841d6e8616858a4e2886dd186ec +size 250034 diff --git a/t3.2.2/run/gcn-bce256-0.001-50af/model_4.pth b/t3.2.2/run/gcn-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..178851524904d33b0464190660ad921738211858 --- /dev/null +++ b/t3.2.2/run/gcn-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9d77c9d24f8e4c8c3d43b4810b242a0ea1424a2e187ad847ebaf8b0998e0a30b +size 250034 diff --git a/t3.2.2/run/gcn-bce256-0.001-50af/model_5.pth b/t3.2.2/run/gcn-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..77cf362697b17cf9fc9a884e4035dd2113d5c0f5 --- /dev/null +++ b/t3.2.2/run/gcn-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d8824c378518515304594001cfbbd64b115b2e017c202e9e590a677e1d12ef4c +size 250034 diff --git a/t3.2.2/run/gcn-bce256-0.001-50af/result.txt b/t3.2.2/run/gcn-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..4416b3b5f01ac6a043209c21ef6e1822fcc51261 --- /dev/null +++ b/t3.2.2/run/gcn-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8692162275314331,0.8306087255477905,0.77294921875,0.8185346484184265 +0.0071013511276380266,0.009455140392305609,0.011780157617866271,0.01048236433343682 +qlx +0.803939139842987,0.7336844801902771,0.6231788039207459,0.8219778776168823 +0.02327244672849884,0.004225297999966109,0.00850964706597026,0.02134693692108923 +saap +0.5538405001163482,0.5103731989860535,0.3926666498184204,0.6263194680213928 +0.03793485826853089,0.0020605992765583693,0.005333328247070312,0.03212643793691851 diff --git a/t3.2.2/run/gin-bce256-0.001-50af/config.json b/t3.2.2/run/gin-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..7010889c1c32dd714c7c186417bd107bd3888b1b --- /dev/null +++ b/t3.2.2/run/gin-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "mm", "fusion": "1", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gin", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.2.2/run/gin-bce256-0.001-50af/model_1.pth b/t3.2.2/run/gin-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..373c89cff09ed46584f1251544a736544c4318df --- /dev/null +++ b/t3.2.2/run/gin-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d84b20923a24541217537212c7395f8cd9e192083e7bdca39a8e4bf7c290fd7f +size 300756 diff --git a/t3.2.2/run/gin-bce256-0.001-50af/model_2.pth b/t3.2.2/run/gin-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..02b60f8414bc1da229ce44b5e1dc7d71ce8463e9 --- /dev/null +++ b/t3.2.2/run/gin-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0dd3db1bd2266903563f0867adcd284a4a3a5092f6efb7748363830d008f5710 +size 300756 diff --git a/t3.2.2/run/gin-bce256-0.001-50af/model_3.pth b/t3.2.2/run/gin-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..2760bf01bfc0a766d6055650c37316d745a5d2db --- /dev/null +++ b/t3.2.2/run/gin-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:683f9ddb198f4816dfc4e23d7a07997fe8614c3787dfdc97d2636925068ad2a0 +size 300756 diff --git a/t3.2.2/run/gin-bce256-0.001-50af/model_4.pth b/t3.2.2/run/gin-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..70b2f97b8c410b4362d177667f0db61981835781 --- /dev/null +++ b/t3.2.2/run/gin-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:725c3305802d6c52348489c90753cc4abea5c42d9611cbf7bbbf2be09fa26d7a +size 300756 diff --git a/t3.2.2/run/gin-bce256-0.001-50af/model_5.pth b/t3.2.2/run/gin-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..1b45175c184de1f18cdfb59ebc1d7270af4af74b --- /dev/null +++ b/t3.2.2/run/gin-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9fc76d32b4f0f2f0ca86e8ed0b69de2d479a264af716c6b97c4c26117ca8ea92 +size 300756 diff --git a/t3.2.2/run/gin-bce256-0.001-50af/result.txt b/t3.2.2/run/gin-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..b02bf27e14d5004feea6e283fa33a0deee8307d3 --- /dev/null +++ b/t3.2.2/run/gin-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.7594280362129211,0.7445878028869629,0.6573643207550048,0.6605328559875489 +0.011317264449661548,0.009502342190660705,0.009569612091640511,0.010772855382883783 +qlx +0.7920526027679443,0.7620222330093384,0.6971302270889282,0.8031510949134827 +0.016017084376019303,0.013526511671024359,0.027461930506924675,0.019919469234947924 +saap +0.5097687602043152,0.49837098121643064,0.5486666560173035,0.5965338945388794 +0.01746760051508408,0.008006418264884652,0.011372488729782212,0.006907071354443276 diff --git a/t3.2.2/run/graphsage-bce256-0.001-50af/config.json b/t3.2.2/run/graphsage-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..93359e0a4fde59f8896cd32b0532cc5e60f77a3f --- /dev/null +++ b/t3.2.2/run/graphsage-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "mm", "fusion": "1", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "graphsage", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t3.2.2/run/graphsage-bce256-0.001-50af/model_1.pth b/t3.2.2/run/graphsage-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..abc42ea734cd9d7f19476f31e2c0b5e5fc07c8e4 --- /dev/null +++ b/t3.2.2/run/graphsage-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5ec6037295d1150ecc3128c31f3819c86434f4c2e57f069dc6a9514480af2400 +size 250034 diff --git a/t3.2.2/run/graphsage-bce256-0.001-50af/model_2.pth b/t3.2.2/run/graphsage-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..49e1a175118c0fbbaf3ca2e9183387043cad0d8f --- /dev/null +++ b/t3.2.2/run/graphsage-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:86eff66ffd0a60a731c5061010b59e794f6433a14ed0a77f339f227d12f8d139 +size 250034 diff --git a/t3.2.2/run/graphsage-bce256-0.001-50af/model_3.pth b/t3.2.2/run/graphsage-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..a05badc1aa3ee4de25f6f130beefff1b20bc1a5b --- /dev/null +++ b/t3.2.2/run/graphsage-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:02d8d3233eef1436f9af07a3f48b00d4ad64729736ca1d75bdc6e8ff087c4395 +size 250034 diff --git a/t3.2.2/run/graphsage-bce256-0.001-50af/model_4.pth b/t3.2.2/run/graphsage-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..09ca25070fe0ee0347ea1a0f8e2fabbe4997bd60 --- /dev/null +++ b/t3.2.2/run/graphsage-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fc424171d53cbe3452e3103ff353276ec6519ccfd9d8e17aa40a439223e21344 +size 250034 diff --git a/t3.2.2/run/graphsage-bce256-0.001-50af/model_5.pth b/t3.2.2/run/graphsage-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..8b331956646c95c021e1474168639199ede9b049 --- /dev/null +++ b/t3.2.2/run/graphsage-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0b399319f943d6ade3641d9805a3f1e7386084652d26f3f885fef5e9c44e58ea +size 250034 diff --git a/t3.2.2/run/graphsage-bce256-0.001-50af/result.txt b/t3.2.2/run/graphsage-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..591eb8573965c7c0c5f3f06db0e9ca0d924c7677 --- /dev/null +++ b/t3.2.2/run/graphsage-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8723568916320801,0.8335093975067138,0.772597324848175,0.821173906326294 +0.00719067513225323,0.006400081938734053,0.008216006128742867,0.008518720396034078 +qlx +0.7737062931060791,0.7308491349220276,0.6178807973861694,0.7963312864303589 +0.009736574621750633,0.011413997962182312,0.02369754485729128,0.0031071077609289657 +saap +0.592711079120636,0.5125713109970093,0.3886666536331177,0.6461053013801574 +0.011740735558104457,0.002751984029667805,0.0038586086210706267,0.017750617109755236 diff --git a/t3.2.2/runthro.sh b/t3.2.2/runthro.sh new file mode 100644 index 0000000000000000000000000000000000000000..5e2f0d42f9fa70bd5c124c5696ea95d5816022bb --- /dev/null +++ b/t3.2.2/runthro.sh @@ -0,0 +1,8 @@ +#!/bin/bash +python main.py --gnn-type gin +python main.py --gnn-type gcn +python main.py --gnn-type gat +python main.py --gnn-type gatv2 +python main.py --gnn-type graphsage + + diff --git a/t3.2.2/summary.py b/t3.2.2/summary.py new file mode 100644 index 0000000000000000000000000000000000000000..420cc50d5a8330d07342a07c2f5bfc482023480a --- /dev/null +++ b/t3.2.2/summary.py @@ -0,0 +1,59 @@ +import os +import csv +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument('--dir', type=str, default='./run') +parser.add_argument('--output', type=str, default='summary.csv') +args = parser.parse_args() + +# 设置目录路径和输出CSV文件名 +directory_path = args.dir +output_csv = args.output + +# 创建CSV文件并写入数据 +with open(output_csv, mode='w', newline='') as file: + writer = csv.writer(file) + + # 写入第一个子表的标题和表头 + writer.writerow(['QLX']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 6: + means_5_6 = lines[4].strip().split(',') + std_devs_5_6 = lines[5].strip().split(',') + if len(means_5_6) == 4 and len(std_devs_5_6) == 4: + values_5_6 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_5_6, std_devs_5_6)] + writer.writerow([subdirectory] + values_5_6) + + # 加入空行以分隔两个子表 + writer.writerow([]) + + # 写入第二个子表的标题和表头 + writer.writerow(['SAAP']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 再次遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 9: + means_8_9 = lines[7].strip().split(',') + std_devs_8_9 = lines[8].strip().split(',') + if len(means_8_9) == 4 and len(std_devs_8_9) == 4: + values_8_9 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_8_9, std_devs_8_9)] + writer.writerow([subdirectory] + values_8_9) \ No newline at end of file diff --git a/t3.2.2/train.py b/t3.2.2/train.py new file mode 100644 index 0000000000000000000000000000000000000000..de2a4067e435ff9c1ead1fe0d33496f726d6c3ff --- /dev/null +++ b/t3.2.2/train.py @@ -0,0 +1,56 @@ +import torch +from torchmetrics import F1Score, Accuracy, AveragePrecision, AUROC + + +def train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer): + train_loss = 0 + num_labels = model.out_dim + avg = args.metric_avg + if num_labels == 1: + task = 'binary' + else: + task = 'multilabel' + metric_macro_acc = Accuracy(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_f1 = F1Score(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_ap = AveragePrecision(average=avg, task=task, num_labels=num_labels).to(device) + metric_auc = AUROC(average=avg, task=task, num_labels=num_labels).to(device) + + if train_loader is not None: + model.train() + for data in train_loader: + data = data.to(device) + # print(seq_lengths) + out = model(data) + # print(out[0]) + # print(gt[0]) + loss = criterion(out, torch.tensor(data.gt, dtype=torch.float, device=device)) + # loss_0 = criterion(out[0], gt.to(device).float()) + # loss_1 = criterion(out[1], gt.to(device).float()) + # loss_2 = criterion(out[2], gt.to(device).float()) + # loss = loss_0 + loss_1 + loss_2 + loss.backward() + optimizer.step() + optimizer.zero_grad() + train_loss += loss.item() + + model.eval() + preds = [] + gt_list_valid = [] + with torch.no_grad(): + for data in valid_loader: + data = data.to(device) + gt_list_valid.append(torch.tensor(data.gt, device=device)) + out = model(data) + preds.append(out) + + # calculate metrics + preds = torch.nn.functional.sigmoid(torch.cat(preds, dim=0)) + gt_list_valid = torch.cat(gt_list_valid, dim=0).int() + + macro_ap = metric_macro_ap(preds, gt_list_valid).item() + # class_ap = [round(i.item(), 5) for i in metric_class_ap(preds, gt_list_valid)] + macro_auc = metric_auc(preds, gt_list_valid).item() + macro_f1 = metric_macro_f1(preds, gt_list_valid).item() + macro_acc = metric_macro_acc(preds, gt_list_valid).item() + return train_loss, macro_ap, macro_f1, macro_acc, macro_auc + diff --git a/t3.2.2/utils.py b/t3.2.2/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..7fd5de2ae11e3a64de85ddc8c0ce0f777a738a48 --- /dev/null +++ b/t3.2.2/utils.py @@ -0,0 +1,32 @@ +import random +import torch +import numpy as np + + +def set_seed(seed): + random.seed(seed) + np.random.seed(seed) + torch.manual_seed(seed) + torch.cuda.manual_seed(seed) + torch.cuda.manual_seed_all(seed) + torch.backends.cudnn.deterministic = True + torch.backends.cudnn.benchmark = False + + +def load_pretrain_model(net, weights): + net_keys = list(net.state_dict().keys()) + weights_keys = list(weights.keys()) + # assert(len(net_keys) <= len(weights_keys)) + i = 0 + j = 0 + while i < len(net_keys) and j < len(weights_keys): + name_i = net_keys[i] + name_j = weights_keys[j] + if net.state_dict()[name_i].shape == weights[name_j].shape: + net.state_dict()[name_i].copy_(weights[name_j].cpu()) + i += 1 + j += 1 + else: + i += 1 + # print i, len(net_keys), j, len(weights_keys) + return net diff --git a/t4.2/dataset.py b/t4.2/dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..fa9b0a34a16ca33af7b40611bc7835a9deaa5acd --- /dev/null +++ b/t4.2/dataset.py @@ -0,0 +1,347 @@ +import os +import mdtraj as md +import torch +from Bio.PDB import PDBParser, is_aa +from Bio.PDB.Polypeptide import three_to_one +from Bio.SeqUtils.ProtParam import ProteinAnalysis +from torch.utils.data import Dataset +import pandas as pd +import numpy as np +from tqdm import tqdm +from Bio.Align import PairwiseAligner + + +aligner = PairwiseAligner() +# Define the scoring system +aligner.mode = 'global' # Global alignment +aligner.match_score = 1 +aligner.mismatch_score = -1 +aligner.open_gap_score = -0.5 +aligner.extend_gap_score = -0.1 + + +def calculate_similarity(seq_gen, seq_tem): + + # Perform the alignment + score = aligner.score(seq_gen, seq_tem) + return score / len(seq_gen) + + +def parse_ll37_testset(path='metadata/LL37_v0.csv'): + files = open(path, 'r').readlines() + seqs = [] + for file in files: + seqs.append(str(file.strip().split(',')[-1].strip().upper())) + return seqs + + +def gmean(labels): + # Assuming 'labels' is a NumPy array of shape (n, m) + # Calculate the geometric mean across axis 1 + + # Take the product of elements along axis=1 + product = np.prod(labels, axis=1) + + # nth root of product, where n is the number of elements along axis=1 + geometric_means = product ** (1.0 / labels.shape[1]) + return geometric_means + + +def clamp(n, smallest, largest): + return sorted([smallest, n, largest])[1] + + +ATOMS = {'H': 1, 'C': 12, 'N': 14, 'O': 16, 'S': 30} +ATOMS_R = {'H': 1, 'C': 1.5, 'N': 1.5, 'O': 1.5, 'S': 2} +AMINO_ACID_WATER = {'A': 255, 'V': 255, 'P': 255, 'F': 255, 'W': 255, 'I': 255, 'L': 255, 'G': 155, 'M': 155, + 'Y': 55, 'S': 55, 'T': 55, 'C': 55, 'N': 55, 'Q': 55, 'D': 55, 'E': 55, 'K': 55, 'R': 55, 'H': 55} +AMINO_ACID_CHARGE = {'D': 55, 'E': 55, 'A': 155, 'V': 155, 'P': 155, 'F': 155, 'W': 155, 'I': 155, 'L': 155, 'G': 155, + 'M': 155, 'Y': 155, 'S': 155, 'T': 155, 'C': 155, 'N': 155, 'Q': 155, 'K': 255, 'R': 255, 'H': 255} +# AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, +# 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19} +AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, + 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19, 'X': 21} + + +def pdb_parser(structure): + """ + + """ + voxel = np.zeros((4, 64, 64, 64), dtype=np.int8) + id = '' + seq_str = '' + for i in structure[0]: + id = i.id + chain = structure[0][id] + for res in chain: + if is_aa(res.get_resname(), standard=True): + resname = res.get_resname() + amino = three_to_one(resname) + seq_str += str(amino) + ATOM_WATER = AMINO_ACID_WATER[amino] + ATOM_CHARGE = AMINO_ACID_CHARGE[amino] + ATOM_CATEGORY = AMAs[amino] * 20 + + for i in res: + if i.id not in ATOMS.keys(): + continue + x, y, z = i.get_coord() + if abs(x) > 32: + x = clamp(x, -31, 31) + if abs(y) > 32: + y = clamp(x, -31, 31) + if abs(z) > 32: + z = clamp(x, -31, 31) + x_i, y_i, z_i = int(x) + 32, int(y) + 32, int(z) + 32 + ATOM_WEIGHT = ATOMS[i.id] + ATOM_R = ATOMS_R[i.id] + + if ATOM_R <= 1.5: + voxel[0, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WEIGHT + voxel[1, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WATER + voxel[2, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CHARGE + voxel[3, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CATEGORY + else: + voxel[0, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WEIGHT + voxel[1, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WATER + voxel[2, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CHARGE + voxel[3, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CATEGORY + return voxel + + +def calculate_property(seq): + analysed_seq = ProteinAnalysis(seq) + aa_counts = analysed_seq.count_amino_acids() + aliphatic_index = ((aa_counts['A'] + 2.9 * aa_counts['V'] + 3.9 * (aa_counts['I'] + aa_counts['L'])) / len(seq)) + + positive_charged_amino_acids = ['R', 'K', 'H'] + negative_charged_amino_acids = ['D', 'E'] + + total_positive_charged = sum(aa_counts.get(aa, 0) for aa in positive_charged_amino_acids) + total_negative_charged = sum(aa_counts.get(aa, 0) for aa in negative_charged_amino_acids) + + total_charge = total_positive_charged - total_negative_charged + charge_density = total_charge / len(seq) + alpha_helix, beta_helix, turn_helix = analysed_seq.secondary_structure_fraction() + + return list( + [round(analysed_seq.gravy(), 3) * 10, round(aliphatic_index, 3) * 10, round(analysed_seq.aromaticity(), 3) * 10, + round(analysed_seq.instability_index(), 3), round(alpha_helix * 10, 3), round(beta_helix * 10, 3), + round(turn_helix * 10, 3), round(analysed_seq.charge_at_pH(7), 3), round(analysed_seq.isoelectric_point(), 3), + round(charge_density, 3) * 10]) + + +def calculate_property_pdb(pdb_file): + # Load the structure + traj = md.load(pdb_file) + topology = traj.topology + + # Extract sequence from the structure + sequence = ''.join([residue.code for residue in topology.residues if residue.is_protein]) + analysed_seq = ProteinAnalysis(sequence) + + # Count amino acids directly from the structure + aa_counts = {residue.code: 0 for residue in topology.residues if residue.is_protein} + for residue in topology.residues: + if residue.is_protein: + aa_counts[residue.code] += 1 + + # Calculate aliphatic index based on the structure + aliphatic_index = ((aa_counts.get('A', 0) + 2.9 * aa_counts.get('V', 0) + 3.9 * (aa_counts.get('I', 0) + aa_counts.get('L', 0))) / len(sequence)) + + # Positive and negative charged amino acids + positive_charged_amino_acids = ['R', 'K', 'H'] + negative_charged_amino_acids = ['D', 'E'] + + total_positive_charged = sum(aa_counts.get(aa, 0) for aa in positive_charged_amino_acids) + total_negative_charged = sum(aa_counts.get(aa, 0) for aa in negative_charged_amino_acids) + + total_charge = total_positive_charged - total_negative_charged + charge_density = total_charge / len(sequence) + + # Secondary structure (using DSSP) + dssp = md.compute_dssp(traj) + total_residues = len(sequence) + alpha_helix = np.sum(dssp == 'H') / total_residues + beta_sheet = np.sum(dssp == 'E') / total_residues + turn_helix = np.sum(dssp == 'T') / total_residues + + # Calculate properties using ProteinAnalysis for those not readily available from structure alone + gravy = analysed_seq.gravy() + aromaticity = analysed_seq.aromaticity() + instability_index = analysed_seq.instability_index() + charge_at_pH7 = analysed_seq.charge_at_pH(7) + isoelectric_point = analysed_seq.isoelectric_point() + + return [ + round(gravy, 3) * 10, + round(aliphatic_index, 3) * 10, + round(aromaticity, 3) * 10, + round(instability_index, 3), + round(alpha_helix * 10, 3), + round(beta_sheet * 10, 3), + round(turn_helix * 10, 3), + round(charge_at_pH7, 3), + round(isoelectric_point, 3), + round(charge_density, 3) * 10 + ] + + +class MDataset(Dataset): + def __init__(self, threshold=32, mode='train', max_length=50, pdb_src='af', data_ver='0920'): + self.num_classes = 6 + exclude_list = pd.read_csv('../metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() + exclude_filter = False + + p = PDBParser(QUIET=True) + + if mode == 'train': + all_data = pd.read_csv(f'../metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values + exclude_filter = True + elif mode == 'qlx': + all_data = pd.read_csv('../metadata/data_qlx.csv', encoding="unicode_escape").values + elif mode == 'saap': + all_data = pd.read_csv('../metadata/data_saap.csv', encoding="unicode_escape").values + else: + raise NotImplementedError + idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) + labels = (labels < threshold).astype(int) + + filter_idx_list = [] + seq_new_list = [] + label_list = [] + for idx in range(len(idx_list)): + seq = seq_list[idx].upper().strip() + if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: + continue + if exclude_filter: + if seq in exclude_list: + continue + + filter_idx_list.append(idx) + seq_new_list.append(seq) + label_list.append(labels[idx]) + + read_pdb_flag = True + if pdb_src == 'af': + pdb_root = '../pdb/pdb_af/' + elif pdb_src == 'hf': + pdb_root = '../pdb/pdb_dbassp/' + elif pdb_src == 'no': + read_pdb_flag = False + else: + raise NotImplementedError + self.data_list = [] + for i in tqdm(range(len(filter_idx_list))): + idx = filter_idx_list[i] + seq = seq_new_list[i] + label = label_list[i] + + # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"): + # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb" + # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"): + # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb" + if read_pdb_flag: + if os.path.exists(pdb_root + seq + ".pdb"): + pdb_path = pdb_root + seq + ".pdb" + else: + print(f'lacking pdb file {seq}') + continue + # raise FileNotFoundError + globf = calculate_property_pdb(pdb_path) + else: + globf = calculate_property(seq) + + # structure = p.get_structure(idx, pdb_path) + # voxel = pdb_parser(structure) + # seq_emb = [AMAs[char] for char in seq] + [0] * (max_length - len(seq)) + self.data_list.append((0, globf, label)) + + def __getitem__(self, idx): + voxel, seq_emb, gt = self.data_list[idx] + + return torch.Tensor(voxel).float(), torch.Tensor(seq_emb), torch.Tensor(gt) + + def __len__(self): + return len(self.data_list) + + +# class SDataset(Dataset): +# ''' +# x: Features. +# y: Targets, if none, do prediction. +# ''' + +# def __init__(self, threshold=32, mode='train', task='0', max_length=50, pdb_src='af'): +# self.num_classes = 1 +# self.task = int(task) +# exclude_list = pd.read_csv('metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() +# exclude_filter = False + +# p = PDBParser(QUIET=True) + +# if mode == 'train': +# all_data = pd.read_csv('metadata/data_0922_i.csv', encoding="unicode_escape").values +# exclude_filter = True +# elif mode == 'qlx': +# all_data = pd.read_csv('metadata/data_qlx.csv', encoding="unicode_escape").values +# elif mode == 'saap': +# all_data = pd.read_csv('metadata/data_saap.csv', encoding="unicode_escape").values +# else: +# raise NotImplementedError +# idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) +# labels = (labels[:, self.task:self.task+1] <= threshold).astype(int) + +# filter_idx_list = [] +# seq_new_list = [] +# label_list = [] +# for idx in range(len(idx_list)): +# seq = seq_list[idx].upper().strip() +# if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: +# continue +# if exclude_filter: +# if seq in exclude_list: +# continue + +# filter_idx_list.append(idx) +# seq_new_list.append(seq) +# label_list.append(labels[idx]) + +# if pdb_src == 'af': +# pdb_root = './pdb/pdb_af/' +# elif pdb_src == 'hf': +# pdb_root = './pdb/pdb_dbassp/' +# else: +# raise NotImplementedError +# self.data_list = [] +# for i in tqdm(range(len(filter_idx_list))): +# idx = filter_idx_list[i] +# seq = seq_new_list[i] +# label = label_list[i] + +# # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"): +# # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb" +# # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"): +# # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb" +# if os.path.exists(pdb_root + seq + ".pdb"): +# pdb_path = pdb_root + seq + ".pdb" +# else: +# continue +# # raise FileNotFoundError + +# structure = p.get_structure(idx, pdb_path) +# voxel = pdb_parser(structure) +# seq_emb = [AMAs[char] for char in seq] + [0] * (max_length - len(seq)) +# self.data_list.append((voxel, seq_emb, label)) + +# def __getitem__(self, idx): +# voxel, seq_emb, gt = self.data_list[idx] + +# return torch.Tensor(voxel).float(), torch.Tensor(seq_emb), torch.Tensor(gt) + +# def __len__(self): +# return len(self.data_list) \ No newline at end of file diff --git a/t4.2/eval.py b/t4.2/eval.py new file mode 100644 index 0000000000000000000000000000000000000000..18f38bfff4c7b7f9bf2fef9834b96c07a96d947d --- /dev/null +++ b/t4.2/eval.py @@ -0,0 +1,203 @@ +import argparse +import json +import logging +import os +import time + +from dataset import SDataset, MDataset +from network import MMPeptide, SEQPeptide, VoxPeptide, MMFPeptide, SMPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=64, + help="MIC threshold for determine labels (default: 64)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=128, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.02, + help='learning rate (default: 0.002)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='micro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='mlce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + weight_dir = "./run/" + args.task_type + '-' + args.task + "-m-" + args.model + '-' + args.loss + str(args.batch_size) + str(args.lr) + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + raise ValueError + + logging.basicConfig(handlers=[ + # logging.FileHandler(filename=os.path.join(weight_dir, "eval.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + # with open(os.path.join(weight_dir, "config.json"), "w") as f: + # f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src) + else: + set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src) + else: + qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + # train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + + # train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=1, shuffle=False) + if args.model == 'seq': + model = SEQPeptide(classes=set_all.num_classes, q_encoder='mlp') + elif args.model == 'voxel': + model = VoxPeptide(classes=set_all.num_classes) + elif args.model == 'mm': + # model = MMPeptide(classes=train_set.num_classes, q_encoder='tf', ) # attention='hamburger' + # model = SMPeptide(classes=train_set.num_classes, q_encoder='mlp', max_length=30) # attention='hamburger' + model = MMPeptide(classes=set_all.num_classes, q_encoder='mlp', max_length=args.max_length)# , attention='hamburger' + elif args.model == 'mmf': + model = MMFPeptide(classes=set_all.num_classes, q_encoder='mlp', max_length=args.max_length) # attention='hamburger' + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + model.load_state_dict(torch.load(weights_path)) + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + + + if args.task_type in ('mlc', 'slc') : + _, ap, f1, acc, auc = train(args, None, model, None, valid_loader, device, None, None) + logging.info(f'ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, None, model, None, qlx_loader, device, None, None) + logging.info(f'QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, None, model, None, saap_loader, device, None, None) + logging.info(f'SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/eval.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/t4.2/gen_simi.py b/t4.2/gen_simi.py new file mode 100644 index 0000000000000000000000000000000000000000..c350b6cbccbe8e4884ea3bbbfd150eb1a66b348b --- /dev/null +++ b/t4.2/gen_simi.py @@ -0,0 +1,38 @@ +import pandas as pd +from dataset import calculate_similarity + +# 读取模板 CSV 文件和目标 CSV 文件 +template_file = './metadata/data_test.csv' # 模板CSV文件路径 +target_file = './metadata/data_0920_i.csv' # 目标CSV文件路径 + +template_df = pd.read_csv(template_file) +target_df = pd.read_csv(target_file) + +# 假设我们要使用 'template_column' 列作为模板中的列,'target_column' 列作为目标表格中的列 +template_column = 'Seq' # 替换为你的模板表格列名 +target_column = 'Seq' # 替换为你的目标表格列名 + +# 创建一个空的 DataFrame,用于存储符合条件的行,并复制目标表格的表头 +extracted_rows = pd.DataFrame(columns=target_df.columns) + +# 遍历目标表格的每一行 +for index, target_row in target_df.iterrows(): + target_value = target_row[target_column] + + # 遍历模板表格的每个值 + for template_value in template_df[template_column]: + + # 计算指标 + metric = calculate_similarity(target_value.strip().upper(), template_value.strip().upper()) + + # 如果指标大于0.3,则提取目标表格的该行 + if metric > 0.3: + extracted_rows = pd.concat([extracted_rows, pd.DataFrame([target_row])], ignore_index=True) + # 只要找到一个匹配值,就可以继续检查下一行 + break + +# 将提取的行保存到一个新的CSV文件中,确保表头与目标表格一致 +output_file = './metadata/data_simi.csv' +extracted_rows.to_csv(output_file, index=False) + +print(f"符合条件的行已保存到 {output_file}") \ No newline at end of file diff --git a/t4.2/infer.py b/t4.2/infer.py new file mode 100644 index 0000000000000000000000000000000000000000..f146bddeb0dbe1b237509ade778d98faeaa06a5d --- /dev/null +++ b/t4.2/infer.py @@ -0,0 +1,121 @@ +import argparse +import json +import logging +import os +import time +import pandas as pd + +from dataset import MDataset +from network import GLOBPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mlp', + help='mlp gru lstm rnn tf mamba') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='no', + help='no, af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--alphafold', type=str, default='./run/af-bce256-0.001-50', + help='path of the pretrain model') +parser.add_argument('--helixfold', type=str, default='./run/hf-bce256-0.001-50', + help='path of the pretrain model') +parser.add_argument('--seq', type=str, default='./run/no-bce256-0.001-50', + help='path of the pretrain model') +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + +model_path = {'alphafold': args.alphafold, 'helixfold': args.helixfold, 'seq': args.seq} + + +def main(): + set_seed(args.seed) + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + results = pd.DataFrame() + + logging.info('Loading Test Dataset') + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + + model = GLOBPeptide(classes=qlx_set.num_classes, q_encoder=args.model) + + for model_name in model_path.keys(): + pred_all = [] + gt_all = [] + for i in range(1,6): + model.load_state_dict(torch.load(os.path.join(model_path[model_name], f'model_{i}.pth'))) + model.to(device).eval() + with torch.no_grad(): + for data in qlx_loader: + voxel, seq, gt = data + gt_all.append(gt.to(device)) + out = model((voxel.to(device), seq.to(device))) + pred_all.append(out) + pred_all = torch.nn.functional.sigmoid(torch.cat(pred_all, dim=0)).squeeze().cpu().numpy() + gt_all = torch.cat(gt_all, dim=0).int().squeeze().cpu().numpy() + results[model_name] = pred_all.ravel(order='F') + + results['gt'] = gt_all.ravel(order='F') + + results.to_csv("preds.csv", index=False) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/t4.2/loss.py b/t4.2/loss.py new file mode 100644 index 0000000000000000000000000000000000000000..d2c1cabdc7bb5df53b8513344372ea557b89aa29 --- /dev/null +++ b/t4.2/loss.py @@ -0,0 +1,123 @@ +import torch +from torch import nn +import torch.nn.functional as F +from math import cos, pi, sin +import math +import numpy as np +from scipy.special import lambertw + + +def mixup_criterion(criterion, pred, y_a, y_b, lam, pow=2): + y = lam ** pow * y_a + (1 - lam) ** pow * y_b + return criterion(pred, y) + + +def mixup_data(v, q, a): + '''Returns mixed inputs, pairs of targets, and lambda without organ constraint''' + lam = np.random.beta(1, 1) + + batch_size = v.shape[0] + index = torch.randperm(batch_size) + + mixed_v = lam * v + (1 - lam) * v[index, :] + mixed_q = lam * q + (1 - lam) * q[index, :] + + a_1, a_2 = a, a[index] + return mixed_v, mixed_q, a_1, a_2, lam + + +def linear(epoch, nepoch): + return 1 - epoch / nepoch + + +def convex(epoch, nepoch): + return epoch / (2 - nepoch) + + +def concave(epoch, nepoch): + return 1 - sin((epoch / nepoch) * (pi / 2)) + + +def composite(epoch, nepoch): + return 0.5 * cos((epoch / nepoch) * pi) + 0.5 + + +class LogCoshLoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y_t, y_prime_t): + ey_t = y_t - y_prime_t + return torch.mean(torch.log(torch.cosh(ey_t + 1e-12)))+F.mse_loss(y_t, y_prime_t) + + +class MLCE(nn.Module): + def __init__(self): + super(MLCE, self).__init__() + + def _mlcce(self, y_pred, y_true): + y_pred = (1 - 2 * y_true) * y_pred + y_pred_neg = y_pred - y_true * 1e12 + y_pred_pos = y_pred - (1 - y_true) * 1e12 + zeros = torch.zeros_like(y_pred[..., :1]) + y_pred_neg = torch.cat([y_pred_neg, zeros], dim=-1) + y_pred_pos = torch.cat([y_pred_pos, zeros], dim=-1) + neg_loss = torch.logsumexp(y_pred_neg, dim=-1) + pos_loss = torch.logsumexp(y_pred_pos, dim=-1) + loss = torch.mean(neg_loss + pos_loss) + return loss + + def __call__(self, y_pred, y_true): + return self._mlcce(y_pred, y_true) + + +class SuperLoss(nn.Module): + def __init__(self, C=10, lam=1, batch_size=128): + super(SuperLoss, self).__init__() + self.tau = math.log(C) + self.lam = lam # set to 1 for CIFAR10 and 0.25 for CIFAR100 + self.batch_size = batch_size + + def forward(self, logits, targets): + l_i = F.mse_loss(logits, targets, reduction='none').detach() + sigma = self.sigma(l_i) + loss = (F.mse_loss(logits, targets, reduction='none') - self.tau) * sigma + self.lam * ( + torch.log(sigma) ** 2) + loss = loss.sum() / self.batch_size + return loss + + def sigma(self, l_i): + x = torch.ones(l_i.size()) * (-2 / math.exp(1.)) + x = x.cuda() + y = 0.5 * torch.max(x, (l_i - self.tau) / self.lam) + y = y.cpu().numpy() + sigma = np.exp(-lambertw(y)) + sigma = sigma.real.astype(np.float32) + sigma = torch.from_numpy(sigma).cuda() + return sigma + + +def unbiased_curriculum_loss(out, data, args, epoch, epochs, scheduler='linear'): + losses = [] + scheduler = linear if scheduler == 'linear' else concave + + # calculate difficulty measurement function + adjusted_losses = [] + for idx in range(out.shape[0]): + ground_truth = max(1, abs(data[idx].item())) + loss = F.mse_loss(out[idx], data[idx]) + losses.append(loss) + adjusted_losses.append(loss.item() / ground_truth) + + mean_loss, std_loss = np.mean(adjusted_losses), np.std(adjusted_losses) + + # re-weight losses + total_loss = 0 + for i, loss in enumerate(losses): + if adjusted_losses[i] > mean_loss + 1 * std_loss: + schedule_factor = scheduler(epoch, args.epochs) + total_loss += schedule_factor * loss + else: + total_loss += loss + + return total_loss diff --git a/t4.2/main.py b/t4.2/main.py new file mode 100644 index 0000000000000000000000000000000000000000..81a042c71b204d4c9d1e1d7c83872d98bb3b13fb --- /dev/null +++ b/t4.2/main.py @@ -0,0 +1,229 @@ +import argparse +import json +import logging +import os +import time + +from dataset import MDataset +from network import GLOBPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mlp', + help='mlp gru lstm rnn tf mamba') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='no', + help='no, af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + if args.loss == 'mlce' and args.task_type != 'slc': + criterion = MLCE() + elif args.loss == "bce" or args.task_type == 'slc': + args.loss = "bce" + criterion = nn.BCEWithLogitsLoss() + else: + criterion = 0 + pass + weight_dir = "./run/" + args.pdb_src + '-' + args.loss + str(args.batch_size) + '-' + str(args.lr) + '-' + str(args.epochs) + if not os.path.exists(weight_dir): + os.makedirs(weight_dir) + + logging.basicConfig(handlers=[ + logging.FileHandler(filename=os.path.join(weight_dir, "training.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + with open(os.path.join(weight_dir, "config.json"), "w") as f: + f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src, data_ver=args.data_ver) + # else: + # set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src) + # else: + # qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + # saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + # if args.loss == 'bce': + # weight = torch.Tensor([0] * args.classes) + # for i in train_set: + # weight += i[2] + # weight = (len(train_set) - weight) / weight + # criterion.register_buffer('pos_weight', weight.to(device)) + + train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=1, shuffle=False) + model = GLOBPeptide(classes=set_all.num_classes, q_encoder=args.model) + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + # optimizer = torch.optim.Adam(model.parameters()) + optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr) + # optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=True, weight_decay=5e-5) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Train set:{len(train_set)}, Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + best_metric = 0 + best_qlx = 0 + best_saap = 0 + start_time = time.time() + + for epoch in range(1, args.epochs + 1): + if args.task_type in ('mlc', 'slc') : + train_loss, ap, f1, acc, auc = train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer) + logging.info(f'Epoch: {epoch:03d}, Train Loss: {train_loss:.3f}, ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + avg_metric = ap + f1 + acc + auc + if avg_metric > best_metric: + logging.info(f'Epoch: {epoch:03d} New Best validation metrics, running test session') + torch.save(model.state_dict(), weights_path) + best_metric = avg_metric + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, epoch, model, None, qlx_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_metric = qlx_ap + qlx_f1 + qlx_acc + qlx_auc + if qlx_metric > best_qlx: + best_qlx = qlx_metric + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, epoch, model, None, saap_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_metric = saap_ap + saap_f1 + saap_acc + saap_auc + if saap_metric > best_saap: + best_saap = saap_metric + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'used time {(time.time()-start_time)/3600:.2f}h') + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/result.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/t4.2/network.py b/t4.2/network.py new file mode 100644 index 0000000000000000000000000000000000000000..51bcdab7674fea7afb574fd9253405b7e9191c8e --- /dev/null +++ b/t4.2/network.py @@ -0,0 +1,713 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F +from mamba_ssm import Mamba + + +class Hamburger(nn.Module): + def __init__(self, inp, oup, reduction=32): + super(Hamburger, self).__init__() + self.pool_h = nn.AdaptiveAvgPool3d((1, None, None)) + self.pool_w = nn.AdaptiveAvgPool3d((None, 1, None)) + self.pool_d = nn.AdaptiveAvgPool3d((None, None, 1)) + + mip = max(8, inp // reduction) + self.conv1 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.conv2 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.conv3 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.gn1 = nn.GroupNorm(8, mip) + self.gn2 = nn.GroupNorm(8, mip) + self.gn3 = nn.GroupNorm(8, mip) + + self.act = nn.LeakyReLU(0.2) + self.conv_h = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + self.conv_w = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + self.conv_d = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + + def forward(self, x): + n, c, h, w, d = x.size() + x_h = self.pool_h(x) + # print(x_h.shape) + x_w = self.pool_w(x).permute(0, 1, 3, 2, 4) + # print(x_w.shape) + x_d = self.pool_d(x).permute(0, 1, 4, 2, 3) + # print(x_d.shape) + y_hwd = torch.cat([x_h, x_w, x_d], dim=2) + # y_hd = torch.cat([x_h, x_d], dim=2) + # y_dw = torch.cat([x_d, x_w], dim=2) + y_hwd = self.conv1(y_hwd) + # y_hd = self.conv2(y_hd) + # y_dw = self.conv3(y_dw) + y_hwd = self.gn1(y_hwd) + # y_hd = self.gn2(y_hd) + # y_dw = self.gn3(y_dw) + y_hwd = self.act(y_hwd) + # y_hd = self.act(y_hd) + # y_dw = self.act(y_dw) + # print(y_hwd.shape) + x_h, x_w, x_d = torch.split(y_hwd, [1, 1, 1], dim=2) + x_w = x_w + x_h = x_h.permute(0, 1, 3, 2, 4) + x_d = x_d.permute(0, 1, 3, 4, 2) + a_h = self.conv_h(x_h).sigmoid() + a_w = self.conv_w(x_w).sigmoid() + a_d = self.conv_d(x_d).sigmoid() + a_hw = a_w * a_h + out = a_hw * a_d + return out + x + + +class BasicBlock(nn.Module): + expansion = 1 + + def __init__(self, in_channels, channels, stride=1): + super().__init__() + + self.conv1 = nn.Conv3d(in_channels, channels, kernel_size=3, stride=stride, padding=1, bias=False) + self.bn1 = nn.BatchNorm3d(channels) + self.relu = nn.ReLU() + + self.conv2 = nn.Conv3d(channels, channels, kernel_size=3, stride=1, padding=1, bias=False) + self.bn2 = nn.BatchNorm3d(channels) + + self.downsample = nn.Sequential() + if stride != 1 or in_channels != channels * self.expansion: + self.downsample = nn.Sequential( + nn.Conv3d(in_channels, channels * self.expansion, kernel_size=1, stride=stride, bias=False), + nn.BatchNorm3d(channels * self.expansion) + ) + + self.stride = stride + + def forward(self, x): + shortcut = self.downsample(x) + + out = self.conv1(x) + out = self.bn1(out) + out = self.relu(out) + + out = self.conv2(out) + out = self.bn2(out) + + out += shortcut + out = self.relu(out) + + return out + + +class Bottleneck(nn.Module): + expansion = 4 + + def __init__(self, in_channels, channels, stride=1): + super().__init__() + + self.conv1 = nn.Conv3d(in_channels, channels, kernel_size=1, bias=False) + self.bn1 = nn.BatchNorm3d(channels) + self.relu = nn.ReLU() + + self.conv2 = nn.Conv3d(channels, channels, kernel_size=3, stride=stride, padding=1, bias=False) + self.bn2 = nn.BatchNorm3d(channels) + + self.conv3 = nn.Conv3d(channels, channels * self.expansion, kernel_size=1, bias=False) + self.bn3 = nn.BatchNorm3d(channels * self.expansion) + + self.downsample = nn.Sequential() + if stride != 1 or in_channels != channels * self.expansion: + self.downsample = nn.Sequential( + nn.Conv3d(in_channels, channels * self.expansion, kernel_size=1, stride=stride, bias=False), + nn.BatchNorm3d(channels * self.expansion) + ) + + def forward(self, x): + shortcut = self.downsample(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 += shortcut + out = self.relu(out) + + return out + + +class ResNet3D(nn.Module): + def __init__(self, block, layers, attention_module): + super().__init__() + if attention_module is not None: + self.attention_flag = True + self.attention_module = attention_module + else: + self.attention_flag = False + + self.in_channels = 64 + + self.conv1 = nn.Conv3d(4, 64, kernel_size=(7, 7, 7), stride=(2, 2, 2), padding=(3, 3, 3), bias=False) + self.bn1 = nn.BatchNorm3d(64) + self.relu = nn.ReLU() + self.max_pool = nn.MaxPool3d(kernel_size=(3, 3, 3), stride=(2, 2, 2), padding=(1, 1, 1)) + + self.layer1 = self._make_layer(block, 64, layers[0], stride=1) + 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) + self.avg_pool = nn.AdaptiveAvgPool3d(1) + + # initialize + for m in self.modules(): + if isinstance(m, nn.Conv3d): + nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') + elif isinstance(m, nn.BatchNorm3d): + m.weight.data.fill_(1) + m.bias.data.zero_() + + def _make_layer(self, block, channels, n_blocks, stride=1): + assert n_blocks > 0, "number of blocks should be greater than zero" + layers = [block(self.in_channels, channels, stride)] + self.in_channels = channels * block.expansion + for i in range(1, n_blocks): + layers.append(block(self.in_channels, channels)) + return nn.Sequential(*layers) + + def forward(self, x, debug=False): + out = self.conv1(x) + out = self.bn1(out) + out = self.relu(out) + if debug: + print("shape1:", out.shape) + out = self.max_pool(out) + if debug: + print("shape2:", out.shape) + out = self.layer1(out) + if debug: + print("shape3:", out.shape) + out = self.layer2(out) + if debug: + print("shape4:", out.shape) + out = self.layer3(out) + if debug: + print("shape5:", out.shape) + out = self.layer4(out) + if self.attention_flag: + out = self.attention_module(out) + if debug: + print("shape6:", out.shape) + out = self.avg_pool(out) + if debug: + print("shape7:", out.shape) + + out = out.view(out.size(0), -1) + return out + + +class FusionModule(nn.Module): + def __init__(self): + super().__init__() + self.f_value = nn.Sequential( + nn.Conv1d(32, 1, kernel_size=1, stride=1), + ) + + def forward(self, x, x_seq): + x = self.f_value(x) + print(x.shape) + + +class ResNet3DFusion(nn.Module): + def __init__(self, block, layers, attention_module): + super().__init__() + if attention_module is not None: + self.attention_flag = True + self.attention_module = attention_module + else: + self.attention_flag = False + + self.in_channels = 64 + + self.conv1 = nn.Conv3d(3, 64, kernel_size=(7, 7, 7), stride=(2, 2, 2), padding=(3, 3, 3), bias=False) + self.bn1 = nn.BatchNorm3d(64) + self.relu = nn.ReLU() + self.max_pool = nn.MaxPool3d(kernel_size=(3, 3, 3), stride=(2, 2, 2), padding=(1, 1, 1)) + + self.layer1 = self._make_layer(block, 64, layers[0], stride=1) + 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) + self.avg_pool = nn.AdaptiveAvgPool3d(1) + self.fc1 = nn.Linear(256, 1) + self.fc2 = nn.Linear(256, 1) + self.fc3 = nn.Linear(256, 1) + self.fc4 = nn.Linear(256, 1) + self.fc5 = nn.Linear(256, 1) + self.fc6 = nn.Linear(256, 1) + + # initialize + for m in self.modules(): + if isinstance(m, nn.Conv3d): + nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') + elif isinstance(m, nn.BatchNorm3d): + m.weight.data.fill_(1) + m.bias.data.zero_() + + def _make_layer(self, block, channels, n_blocks, stride=1): + assert n_blocks > 0, "number of blocks should be greater than zero" + layers = [block(self.in_channels, channels, stride)] + self.in_channels = channels * block.expansion + for i in range(1, n_blocks): + layers.append(block(self.in_channels, channels)) + return nn.Sequential(*layers) + + def forward(self, x, x_seq, debug=False): + seq1 = self.fc1(x_seq) + seq2 = self.fc2(x_seq) + seq3 = self.fc3(x_seq) + seq4 = self.fc4(x_seq) + seq5 = self.fc5(x_seq) + out = self.conv1(x) + + out = out * (seq1.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) + 1) + + out = self.bn1(out) + out = self.relu(out) + if debug: + print("shape1:", out.shape) + out = self.max_pool(out) + if debug: + print("shape2:", out.shape) + out = self.layer1(out) + out = out * (seq2.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) + 1) + + if debug: + print("shape3:", out.shape) + out = self.layer2(out) + out = out * (seq3.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) + 1) + + if debug: + print("shape4:", out.shape) + out = self.layer3(out) + out = out * (seq4.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) + 1) + + if debug: + print("shape5:", out.shape) + out = self.layer4(out) + out = out * (seq5.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1) + 1) + + if self.attention_flag: + out = self.attention_module(out) + if debug: + print("shape6:", out.shape) + out = self.avg_pool(out) + if debug: + print("shape7:", out.shape) + + out = out.view(out.size(0), -1) + return out + + +class MLP(nn.Module): + def __init__(self, input_dim, hidden_dim, output_dim, num_layers, dropout_rate): + super(MLP, self).__init__() + + layers = [] + layers.append(nn.Linear(input_dim, hidden_dim)) + layers.append(nn.ReLU()) + layers.append(nn.Dropout(dropout_rate)) + + for _ in range(num_layers - 1): + layers.append(nn.Linear(hidden_dim, hidden_dim)) + layers.append(nn.ReLU()) + layers.append(nn.Dropout(dropout_rate)) + + layers.append(nn.Linear(hidden_dim, output_dim)) + + self.network = nn.Sequential(*layers) + + def forward(self, x): + return self.network(x) + + +class PositionalEncoding(nn.Module): + def __init__(self, d_model, max_len=50): + super(PositionalEncoding, self).__init__() + + pe = torch.zeros(max_len, d_model) # (max_len, d_model) + position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1) # (max_len, 1) + div_term = torch.exp(torch.arange(0, d_model, 2).float() * + (-torch.log(torch.FloatTensor([10000.0])) / d_model)) # (d_model/2,) + pe[:, 0::2] = torch.sin(position * div_term) # 偶数维 + pe[:, 1::2] = torch.cos(position * div_term) # 奇数维 + pe = pe.unsqueeze(0) # (1, max_len, d_model) + self.register_buffer('pe', pe) + + def forward(self, x): + """ + x: (B, N, d_model) + """ + x = x + self.pe[:, :x.size(1), :] + return x + + +class TransformerModel(nn.Module): + def __init__(self, nheads, d_model, num_layers, out_dim, max_length=50): + super(TransformerModel, self).__init__() + + # 嵌入层,将输入从 (B, N) 转换到 (B, N, embed_dim) + self.embedding = nn.Linear(1, d_model) + + # 位置编码 + self.pos_encoder = PositionalEncoding(d_model, max_length) + + # Transformer 编码器层 + encoder_layers = nn.TransformerEncoderLayer(d_model=d_model, + nhead=nheads, + activation='relu') + self.transformer_encoder = nn.TransformerEncoder(encoder_layers, num_layers=num_layers) + + # 全局池化(可以根据任务选择不同的聚合方式) + self.global_pool = nn.AdaptiveAvgPool1d(1) + + # 输出层 + self.fc = nn.Linear(d_model, out_dim) + + def forward(self, src): + """ + src: (B, N) + """ + # 嵌入 + embedded = self.embedding(src.unsqueeze(-1)) # (B, N, embed_dim) + embedded = self.pos_encoder(embedded) # 添加位置编码 + + # 转置以适应 Transformer (N, B, embed_dim) + embedded = embedded.permute(1, 0, 2) + + # Transformer 编码 + transformer_out = self.transformer_encoder(embedded) # (N, B, embed_dim) + + # 转置回 (B, N, embed_dim) + transformer_out = transformer_out.permute(1, 0, 2) + + # 全局池化,将 (B, N, embed_dim) 转换为 (B, embed_dim) + pooled = self.global_pool(transformer_out.permute(0, 2, 1)).squeeze(-1) + + # 输出层 + output = self.fc(pooled) # (B, output_dim) + + return output + + +class MambaModel(nn.Module): + def __init__(self, d_model, out_dim, max_length=30): + super(MambaModel, self).__init__() + self.linear = nn.Linear(in_features=1, out_features=d_model) + self.pos_encoder = PositionalEncoding(d_model, max_length) + self.mamba = Mamba(d_model=d_model) + self.global_pool = nn.AdaptiveAvgPool1d(1) + self.fc = nn.Linear(d_model * 2, out_dim) + + def forward(self, x: torch.Tensor): + x = self.pos_encoder(self.linear(x.unsqueeze(-1))) + y = self.mamba(x) + y_flip = self.mamba(x.flip([-2])).flip([-2]) + y = torch.cat((y, y_flip), dim=-1) + y = self.fc(self.global_pool(y.permute(0, 2, 1)).squeeze(-1)) + return y + + +class SEQ(nn.Module): + def __init__(self, seq_type='mlp', input_dim=21, hidden_dim=128, out_dim=128, num_layers=2, max_length=30): + super(SEQ, self).__init__() + self.seq_type = seq_type + if seq_type == 'rnn': + self.rnn = nn.RNN( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'gru': + self.rnn = nn.GRU( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'lstm': + self.rnn = nn.LSTM( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'tf': + self.transformer = TransformerModel(nheads=4, d_model=hidden_dim, num_layers=2, out_dim=out_dim, max_length=max_length) + elif seq_type == 'mamba': + self.mamba = MambaModel(d_model=hidden_dim, out_dim=out_dim, max_length=max_length) + elif self.seq_type == 'mlp': + self.rnn = MLP(30, hidden_dim, out_dim, 3, 0.3) + else: + raise NotImplementedError(f'\'{seq_type}\' not implemented') + + self.rnn_fc = nn.Sequential( + nn.Linear(hidden_dim * 2, hidden_dim), + nn.ReLU(), + nn.Linear(hidden_dim, out_dim) + ) + + def forward(self, seq): + if self.seq_type == 'mlp': + return self.rnn(seq) + elif self.seq_type == 'tf': + return self.transformer(seq) + elif self.seq_type == 'mamba': + return self.mamba(seq) + else: + one_hot_seq = F.one_hot(seq.to(torch.int64), num_classes=21).float() + r_out = self.rnn(one_hot_seq, None)[0] # None represents zero initial hidden state + out = self.rnn_fc(r_out[:, -1, :]) + return out + # def forward(self, x, seq_lengths): + # + + +class VoxPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None): + super().__init__() + + if attention == 'hamburger': + self.attention = Hamburger(2048, 2048) + else: + self.attention = None + # v_encoder could be resnet26 or resnet50 + if v_encoder == 'resnet26': + self.v_encoder = ResNet3D(Bottleneck, [1, 2, 4, 1], self.attention) + + if fusion == 'mlp': + self.fusion = nn.Linear(512 * 4 + 256, classes) + elif fusion == 'att': + self.fusion = nn.Linear(512 * 4 + 256, classes) + else: + raise NotImplementedError + self.vox_fc = nn.Linear(2048, classes) + + def forward(self, x, seq_lengths=None): + vox, seq = x + seq_emb = self.v_encoder(vox) + pred = self.vox_fc(seq_emb) + return pred + + +class SEQPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + self.classes = classes + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + + self.seq_fc = nn.Linear(128, classes) + + def forward(self, x, seq_lengths=None): + vox, seq = x + seq_emb = self.q_encoder(seq) + pred = self.seq_fc(seq_emb) + return pred + +class GLOBPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + self.classes = classes + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = MLP(input_dim=10, hidden_dim=128, output_dim=128, num_layers=3, dropout_rate=0.3) + + self.seq_fc = nn.Linear(128, classes) + + def forward(self, x, seq_lengths=None): + vox, seq = x + seq_emb = self.q_encoder(seq) + pred = self.seq_fc(seq_emb) + return pred + + +class ConvNet(nn.Module): + def __init__(self, num_classes: int = 2): + super(ConvNet, self).__init__() + self.conv1 = nn.Conv1d(3, 16, 3, padding=1) + self.conv2 = nn.Conv1d(16, 32, 3, padding=1) + self.fc1 = nn.Linear(32 * 7, 128) + # self.fc2 = nn.Linear(128, num_classes) + + def forward(self, x): + # print(x.shape) + x = x.permute(0, 2, 1) + x = F.relu(self.conv1(x)) + x = F.max_pool1d(x, 2) + x = F.relu(self.conv2(x)) + x = F.max_pool1d(x, 2) + x = x.view(x.shape[0], -1) + return self.fc1(x) + # x = F.relu(self.fc1(x)) + # x = self.fc2(x) + # return x + + +class ConvNet2D(nn.Module): + def __init__(self, num_classes: int = 2): + super(ConvNet2D, self).__init__() + self.conv1 = nn.Conv2d(1, 8, 3, padding=1) + self.conv2 = nn.Conv2d(8, 16, 3, padding=2, stride=2) + self.conv3 = nn.Conv2d(16, 32, 3, padding=2, stride=2) + # self.pool = nn.AdaptiveAvgPool2d(32) + self.fc1 = nn.Linear(32 * 3 * 9, 128) + # self.fc2 = nn.Linear(128, num_classes) + + def forward(self, x): + # print(x.shape) + x = x.unsqueeze(1) + x = F.relu(self.conv1(x)) + # print(x.shape) + x = F.relu(self.conv2(x)) + # print(x.shape) + x = F.relu(self.conv3(x)) + # x = self.pool(x) + # print(x.shape) + x = x.view(x.shape[0], -1) + return self.fc1(x) + + +# convnet = ConvNet() +# print(convnet) +class MMPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + if attention == 'hamburger': + self.attention = Hamburger(2048, 2048) + else: + self.attention = None + # v_encoder could be resnet26 or resnet50 + if v_encoder == 'resnet26': + self.v_encoder = ResNet3D(Bottleneck, [1, 2, 4, 1], self.attention) + # self.v_encoder = SwinUNETR(img_size=(64, 64, 64), in_channels=3, out_channels=1) + elif v_encoder == 'resnet50': + self.v_encoder = ResNet3D(Bottleneck, [3, 4, 6, 3], self.attention) + else: + raise NotImplementedError + + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + # self.ss_encoder = SEQ(seq_type=q_encoder) + if fusion == 'mlp': + self.fusion = nn.Linear(512 * 4 + 256, 256) + # self.fusion = nn.Linear(192 + 256, classes) + elif fusion == 'att': + self.fusion = nn.Linear(512 * 4 + 256, 256) + else: + raise NotImplementedError + + # self.vox_fc = nn.Linear(2048, classes) + # self.seq_fc = nn.Linear(256, classes) + self.out = nn.Sequential(nn.ReLU(inplace=True), nn.Linear(256, classes)) + self.classes = classes + + def forward(self, x, seq_lengths=None): + vox, seq = x + # print(vox.shape) + # print(seq.shape) + vox_emb = self.v_encoder(vox) + # print(vox_emb.shape) + seq_emb = self.q_encoder(seq, seq_lengths) + # print(seq_emb.shape) + # ss_emb = self.ss_encoder(second_s) + fused_feature = torch.cat((seq_emb, vox_emb), dim=1) + pred = self.fusion(fused_feature) + pred = self.out(pred) + # pred1 = self.vox_fc(vox_emb) + # pred2 = self.seq_fc(seq_emb) + # return pred, fused_feature + return pred + + +class SMPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, hidden_dim=256, max_length=30): + super().__init__() + self.siamese_encoder1 = MMPeptide(v_encoder, q_encoder, fusion, classes, attention, max_length) + # self.siamese_encoder2 = MMPeptide(v_encoder, q_encoder, fusion, classes, attention) + self.fc = nn.Sequential( + nn.Linear(hidden_dim * 2, hidden_dim), + nn.ReLU(), + nn.Linear(hidden_dim, 1) + ) + + def forward(self, x, seq_lengths=None): + f_mutated = self.siamese_encoder1(x[0]) + f_wide_type = self.siamese_encoder1(x[1]) + return self.fc(torch.cat((f_mutated, f_wide_type), dim=1)) + + +class MMFPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + if attention == 'hamburger': + self.attention = Hamburger(2048, 2048) + else: + self.attention = None + # v_encoder could be resnet26 or resnet50 + if v_encoder == 'resnet26': + self.v_encoder = ResNet3D(Bottleneck, [1, 2, 4, 1], self.attention) + # self.v_encoder = ResNet3DFusion(Bottleneck, [1, 2, 4, 1], self.attention) + elif v_encoder == 'resnet50': + self.v_encoder = ResNet3D(Bottleneck, [3, 4, 6, 3], self.attention) + else: + raise NotImplementedError + + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + + if fusion == 'mlp': + self.fusion = nn.Linear(512 * 4 + 256, classes) + elif fusion == 'att': + self.fusion = nn.Linear(512 * 4 + 256, classes) + else: + raise NotImplementedError + + self.vox_fc = nn.Linear(2048, classes) + self.seq_fc = nn.Linear(256, classes) + + def forward(self, x, seq_lengths=None): + vox, seq = x + # print(vox.shape) + # print(seq.shape) + seq_emb = self.q_encoder(seq, seq_lengths) + + vox_emb = self.v_encoder(vox, seq_emb) + # print(vox_emb.shape) + # print(seq_emb.shape) + fused_feature = torch.cat((seq_emb, vox_emb), dim=1) + pred = self.fusion(fused_feature) + # pred1 = self.vox_fc(vox_emb) + # pred2 = self.seq_fc(seq_emb) + return pred + + +if __name__ == "__main__": + # model = MMFPeptide() + # voxel = torch.zeros((4, 3, 64, 64, 64)) + # # # h_in = torch.zeros((2, 2048, 2, 2, 2)) + # # # h = Hamburger(2048, 2048) + # # # h(h_in) + # seq = torch.ones((4, 50)) + # res = model.forward((voxel, seq)) + # out = model((voxel, seq)) + # print(out.shape) + # model = ConvNet2D() + input_seq = torch.ones((4, 1, 30)) + # model(input_seq) + transformer = TransformerModel(nhead=4, d_model=32, num_layers=2) + print(transformer(input_seq).shape) diff --git a/t4.2/run/af-bce256-0.001-50/config.json b/t4.2/run/af-bce256-0.001-50/config.json new file mode 100644 index 0000000000000000000000000000000000000000..daf0e469b71db09076af6fe2123dded38e5d9f29 --- /dev/null +++ b/t4.2/run/af-bce256-0.001-50/config.json @@ -0,0 +1 @@ +{"model": "mlp", "fusion": "1", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t4.2/run/af-bce256-0.001-50/model_1.pth b/t4.2/run/af-bce256-0.001-50/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..878852ef0e652244476cb4650d708c6b492f1a1a --- /dev/null +++ b/t4.2/run/af-bce256-0.001-50/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:369466403bf826101cc73ae4235faa452274f8747cdc315b3c430281577fd685 +size 210488 diff --git a/t4.2/run/af-bce256-0.001-50/model_2.pth b/t4.2/run/af-bce256-0.001-50/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..fc09f1af9b34f1037923fc07b3be619120b3cb6e --- /dev/null +++ b/t4.2/run/af-bce256-0.001-50/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:da1b464da8a9bf71379d0c0b5bf215472f420930e1eff528a788b8a452d215f5 +size 210488 diff --git a/t4.2/run/af-bce256-0.001-50/model_3.pth b/t4.2/run/af-bce256-0.001-50/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..552c0a638bbb9e8d5cf200f5f7e879a04487e42b --- /dev/null +++ b/t4.2/run/af-bce256-0.001-50/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9e776d8e944a3cb88fd11aa6527fa46dc74c1dacd110d0107ab04b774428018d +size 210488 diff --git a/t4.2/run/af-bce256-0.001-50/model_4.pth b/t4.2/run/af-bce256-0.001-50/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..e8871b709fc966f733650a27e999e640ae95cce5 --- /dev/null +++ b/t4.2/run/af-bce256-0.001-50/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:603b07c1647c411e6aac23ff77028a4a47d1b2fbf70b3d0e9b2590f11532e266 +size 210488 diff --git a/t4.2/run/af-bce256-0.001-50/model_5.pth b/t4.2/run/af-bce256-0.001-50/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..77094503734beed8a85d725a8ee799f26ef673bf --- /dev/null +++ b/t4.2/run/af-bce256-0.001-50/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7af5aea76f2e19e2d5769b609cb24daf46f4e2e610bfddb55efacaee4caf88d1 +size 210488 diff --git a/t4.2/run/af-bce256-0.001-50/result.txt b/t4.2/run/af-bce256-0.001-50/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..c1510bb0152e64697fb1b3eb3271f28b51e6253c --- /dev/null +++ b/t4.2/run/af-bce256-0.001-50/result.txt @@ -0,0 +1,9 @@ +Valid +0.8700168848037719,0.829870069026947,0.7732164740562439,0.8170999646186828 +0.011645997999268924,0.007643625661103131,0.011208616882488724,0.013046811436483426 +qlx +0.8071112990379333,0.7529298901557923,0.6604856610298157,0.8309757351875305 +0.010022469291913673,0.00739588259595218,0.01417966077706339,0.013651444714501776 +saap +0.5006589710712432,0.5129449367523193,0.38499999046325684,0.5914555311203002 +0.01595218444465141,0.0,0.0,0.01585766743851582 diff --git a/t4.2/run/hf-bce256-0.001-50/config.json b/t4.2/run/hf-bce256-0.001-50/config.json new file mode 100644 index 0000000000000000000000000000000000000000..0424293395e4c2d9f8dfa806db28ac0bcf49246f --- /dev/null +++ b/t4.2/run/hf-bce256-0.001-50/config.json @@ -0,0 +1 @@ +{"model": "mlp", "fusion": "1", "pdb_src": "hf", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t4.2/run/hf-bce256-0.001-50/model_1.pth b/t4.2/run/hf-bce256-0.001-50/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..1456534246913d8373efa22235bec7ebac775c86 --- /dev/null +++ b/t4.2/run/hf-bce256-0.001-50/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9fb394c5e78edd9c033a97974fcd7b1c9c34ea037f181b55dfc6dc3c17720fe3 +size 210488 diff --git a/t4.2/run/hf-bce256-0.001-50/model_2.pth b/t4.2/run/hf-bce256-0.001-50/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..cecdddaae7530c7db8ac41ea7d21f9ad2eb71604 --- /dev/null +++ b/t4.2/run/hf-bce256-0.001-50/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:884636b9fc82e6eee643cc0f4f0fcf896b037a9de0158caad67cfbe28168c09c +size 210488 diff --git a/t4.2/run/hf-bce256-0.001-50/model_3.pth b/t4.2/run/hf-bce256-0.001-50/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..5b30b6b4f39ae2d5e72fa381e44d96db67fce327 --- /dev/null +++ b/t4.2/run/hf-bce256-0.001-50/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0f23288c94dfd40287f75f3c309e404940c0bc71a8a2f6b07cca3cb71a687554 +size 210488 diff --git a/t4.2/run/hf-bce256-0.001-50/model_4.pth b/t4.2/run/hf-bce256-0.001-50/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..35ac6e14ba6c2b563aa1656beab7ed3f9af0cf0a --- /dev/null +++ b/t4.2/run/hf-bce256-0.001-50/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:32077e1575aa6b23fabf4e520e8c9ec9d4c6ea52e92652a7ac55e357121a63c4 +size 210488 diff --git a/t4.2/run/hf-bce256-0.001-50/model_5.pth b/t4.2/run/hf-bce256-0.001-50/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..c21e4e3f8cd858d660bf3c76cd9806ec1966ef22 --- /dev/null +++ b/t4.2/run/hf-bce256-0.001-50/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d40701b56ba4b2016bc86845268ed14015d8e45edf0e2c5314b2b20fee1a0201 +size 210488 diff --git a/t4.2/run/hf-bce256-0.001-50/result.txt b/t4.2/run/hf-bce256-0.001-50/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..729a024604cc7ce50dc1a6a33e0655b663f6079a --- /dev/null +++ b/t4.2/run/hf-bce256-0.001-50/result.txt @@ -0,0 +1,9 @@ +Valid +0.8719751119613648,0.8315282702445984,0.7742257833480835,0.8188141345977783 +0.009939588843739668,0.009799635104017266,0.01210653989570696,0.012518670362233277 +qlx +0.8112729310989379,0.7503309369087219,0.6551876425743103,0.835211205482483 +0.012931660387732342,0.006138344566652785,0.011651768746506414,0.009700005692371248 +saap +0.5165261328220367,0.5150565862655639,0.39099998474121095,0.6172165036201477 +0.017591248254639376,0.0017241546076819363,0.004898974813535444,0.0076444053320058455 diff --git a/t4.2/run/no-bce256-0.001-50/config.json b/t4.2/run/no-bce256-0.001-50/config.json new file mode 100644 index 0000000000000000000000000000000000000000..886eb4c3782357048bb019f5ec0076f87b460a3f --- /dev/null +++ b/t4.2/run/no-bce256-0.001-50/config.json @@ -0,0 +1 @@ +{"model": "mlp", "fusion": "1", "pdb_src": "no", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t4.2/run/no-bce256-0.001-50/model_1.pth b/t4.2/run/no-bce256-0.001-50/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..8b902f564f523e6f5147b5fcf677f12471d880f1 --- /dev/null +++ b/t4.2/run/no-bce256-0.001-50/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e4d1b8215ad553312447f2f84d16fa703abb557cdcb2f22047e47705cd7cc909 +size 210488 diff --git a/t4.2/run/no-bce256-0.001-50/model_2.pth b/t4.2/run/no-bce256-0.001-50/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..be1288cd661c29565159003f9223d09a9fece1cb --- /dev/null +++ b/t4.2/run/no-bce256-0.001-50/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:606b1afaa472e0eadd421a2b23dca9f908f9e1ffdce324883e02572a5cdb5898 +size 210488 diff --git a/t4.2/run/no-bce256-0.001-50/model_3.pth b/t4.2/run/no-bce256-0.001-50/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..0057fb6f538aa013022cbfda1f53678547fa4f98 --- /dev/null +++ b/t4.2/run/no-bce256-0.001-50/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:492fc8df8e8938721f4776e2282376d53177f392ba0bad27ceed744ea8ae4684 +size 210488 diff --git a/t4.2/run/no-bce256-0.001-50/model_4.pth b/t4.2/run/no-bce256-0.001-50/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..1f5db62c0e1a8090c5dd86a92b3d6c6de69ad7c3 --- /dev/null +++ b/t4.2/run/no-bce256-0.001-50/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:355348f7454f046968bfbd26f826b609fbec80b5507b1bd8930f76c8b7c9d189 +size 210488 diff --git a/t4.2/run/no-bce256-0.001-50/model_5.pth b/t4.2/run/no-bce256-0.001-50/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..b18b746ba30b3cbaa63b645c14151dca87548558 --- /dev/null +++ b/t4.2/run/no-bce256-0.001-50/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:383c6cba22255f32900f2a2920716efad3f2714d7c8fe4cbaf01d5c8686a6b10 +size 210488 diff --git a/t4.2/run/no-bce256-0.001-50/result.txt b/t4.2/run/no-bce256-0.001-50/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..0829c6fb3dccc5c520c79493e3f2d61911dbbf24 --- /dev/null +++ b/t4.2/run/no-bce256-0.001-50/result.txt @@ -0,0 +1,9 @@ +Valid +0.8699991703033447,0.8277685403823852,0.7682315111160278,0.8144280552864075 +0.012043435816901439,0.01036246687814122,0.011150108634873715,0.01257664306349131 +qlx +0.8281428694725037,0.7502928256988526,0.6551876306533814,0.8571536302566528 +0.015224370835636056,0.00795591871735562,0.015111327712184115,0.01457044083866149 +saap +0.46939916014671323,0.516464352607727,0.39499998092651367,0.5808656334877014 +0.013406252253458345,0.0,0.0,0.016736609750311358 diff --git a/t4.2/runthro.sh b/t4.2/runthro.sh new file mode 100644 index 0000000000000000000000000000000000000000..0d608aff74ef24803e2a56a693cc639f3b65f4ee --- /dev/null +++ b/t4.2/runthro.sh @@ -0,0 +1,4 @@ +#!/bin/bash +python main.py --pdb-src no --gpu 1 +python main.py --pdb-src af --gpu 1 +python main.py --pdb-src hf --gpu 1 diff --git a/t4.2/summary.py b/t4.2/summary.py new file mode 100644 index 0000000000000000000000000000000000000000..420cc50d5a8330d07342a07c2f5bfc482023480a --- /dev/null +++ b/t4.2/summary.py @@ -0,0 +1,59 @@ +import os +import csv +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument('--dir', type=str, default='./run') +parser.add_argument('--output', type=str, default='summary.csv') +args = parser.parse_args() + +# 设置目录路径和输出CSV文件名 +directory_path = args.dir +output_csv = args.output + +# 创建CSV文件并写入数据 +with open(output_csv, mode='w', newline='') as file: + writer = csv.writer(file) + + # 写入第一个子表的标题和表头 + writer.writerow(['QLX']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 6: + means_5_6 = lines[4].strip().split(',') + std_devs_5_6 = lines[5].strip().split(',') + if len(means_5_6) == 4 and len(std_devs_5_6) == 4: + values_5_6 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_5_6, std_devs_5_6)] + writer.writerow([subdirectory] + values_5_6) + + # 加入空行以分隔两个子表 + writer.writerow([]) + + # 写入第二个子表的标题和表头 + writer.writerow(['SAAP']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 再次遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 9: + means_8_9 = lines[7].strip().split(',') + std_devs_8_9 = lines[8].strip().split(',') + if len(means_8_9) == 4 and len(std_devs_8_9) == 4: + values_8_9 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_8_9, std_devs_8_9)] + writer.writerow([subdirectory] + values_8_9) \ No newline at end of file diff --git a/t4.2/train.py b/t4.2/train.py new file mode 100644 index 0000000000000000000000000000000000000000..5f0f3ff0c2dc32000009493d29fe02c1cc5124ac --- /dev/null +++ b/t4.2/train.py @@ -0,0 +1,56 @@ +import torch +from torchmetrics import F1Score, Accuracy, AveragePrecision, AUROC + + +def train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer): + train_loss = 0 + num_labels = model.classes + avg = args.metric_avg + if num_labels == 1: + task = 'binary' + else: + task = 'multilabel' + metric_macro_acc = Accuracy(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_f1 = F1Score(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_ap = AveragePrecision(average=avg, task=task, num_labels=num_labels).to(device) + metric_auc = AUROC(average=avg, task=task, num_labels=num_labels).to(device) + + if train_loader is not None: + model.train() + for data in train_loader: + voxel, seq, gt = data + # print(seq_lengths) + out = model((voxel.to(device), seq.to(device))) + # print(out[0]) + # print(gt[0]) + loss = criterion(out, gt.to(device).float()) + # loss_0 = criterion(out[0], gt.to(device).float()) + # loss_1 = criterion(out[1], gt.to(device).float()) + # loss_2 = criterion(out[2], gt.to(device).float()) + # loss = loss_0 + loss_1 + loss_2 + loss.backward() + optimizer.step() + optimizer.zero_grad() + train_loss += loss.item() + + model.eval() + preds = [] + gt_list_valid = [] + with torch.no_grad(): + for data in valid_loader: + voxel, seq, gt = data + gt_list_valid.append(gt.to(device)) + out = model((voxel.to(device), seq.to(device))) + preds.append(out) + + # calculate metrics + preds = torch.nn.functional.sigmoid(torch.cat(preds, dim=0)) + gt_list_valid = torch.cat(gt_list_valid, dim=0).int() + + macro_ap = metric_macro_ap(preds, gt_list_valid).item() + # class_ap = [round(i.item(), 5) for i in metric_class_ap(preds, gt_list_valid)] + macro_auc = metric_auc(preds, gt_list_valid).item() + macro_f1 = metric_macro_f1(preds, gt_list_valid).item() + macro_acc = metric_macro_acc(preds, gt_list_valid).item() + return train_loss, macro_ap, macro_f1, macro_acc, macro_auc + diff --git a/t4.2/utils.py b/t4.2/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..7fd5de2ae11e3a64de85ddc8c0ce0f777a738a48 --- /dev/null +++ b/t4.2/utils.py @@ -0,0 +1,32 @@ +import random +import torch +import numpy as np + + +def set_seed(seed): + random.seed(seed) + np.random.seed(seed) + torch.manual_seed(seed) + torch.cuda.manual_seed(seed) + torch.cuda.manual_seed_all(seed) + torch.backends.cudnn.deterministic = True + torch.backends.cudnn.benchmark = False + + +def load_pretrain_model(net, weights): + net_keys = list(net.state_dict().keys()) + weights_keys = list(weights.keys()) + # assert(len(net_keys) <= len(weights_keys)) + i = 0 + j = 0 + while i < len(net_keys) and j < len(weights_keys): + name_i = net_keys[i] + name_j = weights_keys[j] + if net.state_dict()[name_i].shape == weights[name_j].shape: + net.state_dict()[name_i].copy_(weights[name_j].cpu()) + i += 1 + j += 1 + else: + i += 1 + # print i, len(net_keys), j, len(weights_keys) + return net diff --git a/t5/dataset.py b/t5/dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..8a23397480a6b827d6a48fd42654900c5da6ad9e --- /dev/null +++ b/t5/dataset.py @@ -0,0 +1,293 @@ +import os +import torch +from Bio.PDB import PDBParser, is_aa +from Bio.PDB.Polypeptide import three_to_one +from Bio.SeqUtils.ProtParam import ProteinAnalysis +from torch.utils.data import Dataset +import pandas as pd +import numpy as np +from tqdm import tqdm +from Bio.Align import PairwiseAligner + + +aligner = PairwiseAligner() +# Define the scoring system +aligner.mode = 'global' # Global alignment +aligner.match_score = 1 +aligner.mismatch_score = -1 +aligner.open_gap_score = -0.5 +aligner.extend_gap_score = -0.1 + + +def calculate_similarity(seq_gen, seq_tem): + + # Perform the alignment + score = aligner.score(seq_gen, seq_tem) + return score / len(seq_gen) + + +def parse_ll37_testset(path='metadata/LL37_v0.csv'): + files = open(path, 'r').readlines() + seqs = [] + for file in files: + seqs.append(str(file.strip().split(',')[-1].strip().upper())) + return seqs + + +def gmean(labels): + # Assuming 'labels' is a NumPy array of shape (n, m) + # Calculate the geometric mean across axis 1 + + # Take the product of elements along axis=1 + product = np.prod(labels, axis=1) + + # nth root of product, where n is the number of elements along axis=1 + geometric_means = product ** (1.0 / labels.shape[1]) + return geometric_means + + +def clamp(n, smallest, largest): + return sorted([smallest, n, largest])[1] + + +ATOMS = {'H': 1, 'C': 12, 'N': 14, 'O': 16, 'S': 30} +ATOMS_R = {'H': 1, 'C': 1.5, 'N': 1.5, 'O': 1.5, 'S': 2} +AMINO_ACID_WATER = {'A': 255, 'V': 255, 'P': 255, 'F': 255, 'W': 255, 'I': 255, 'L': 255, 'G': 155, 'M': 155, + 'Y': 55, 'S': 55, 'T': 55, 'C': 55, 'N': 55, 'Q': 55, 'D': 55, 'E': 55, 'K': 55, 'R': 55, 'H': 55} +AMINO_ACID_CHARGE = {'D': 55, 'E': 55, 'A': 155, 'V': 155, 'P': 155, 'F': 155, 'W': 155, 'I': 155, 'L': 155, 'G': 155, + 'M': 155, 'Y': 155, 'S': 155, 'T': 155, 'C': 155, 'N': 155, 'Q': 155, 'K': 255, 'R': 255, 'H': 255} +# AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, +# 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19} +AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, + 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19, 'X': 21} + + +def pdb_parser(structure): + """ + + """ + voxel = np.zeros((4, 64, 64, 64), dtype=np.int8) + id = '' + seq_str = '' + for i in structure[0]: + id = i.id + chain = structure[0][id] + for res in chain: + if is_aa(res.get_resname(), standard=True): + resname = res.get_resname() + amino = three_to_one(resname) + seq_str += str(amino) + ATOM_WATER = AMINO_ACID_WATER[amino] + ATOM_CHARGE = AMINO_ACID_CHARGE[amino] + ATOM_CATEGORY = AMAs[amino] * 20 + + for i in res: + if i.id not in ATOMS.keys(): + continue + x, y, z = i.get_coord() + if abs(x) > 32: + x = clamp(x, -31, 31) + if abs(y) > 32: + y = clamp(x, -31, 31) + if abs(z) > 32: + z = clamp(x, -31, 31) + x_i, y_i, z_i = int(x) + 32, int(y) + 32, int(z) + 32 + ATOM_WEIGHT = ATOMS[i.id] + ATOM_R = ATOMS_R[i.id] + + if ATOM_R <= 1.5: + voxel[0, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WEIGHT + voxel[1, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WATER + voxel[2, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CHARGE + voxel[3, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CATEGORY + else: + voxel[0, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WEIGHT + voxel[1, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WATER + voxel[2, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CHARGE + voxel[3, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CATEGORY + return voxel + + +def calculate_property(seq): + analysed_seq = ProteinAnalysis(seq) + aa_counts = analysed_seq.count_amino_acids() + aliphatic_index = ((aa_counts['A'] + 2.9 * aa_counts['V'] + 3.9 * (aa_counts['I'] + aa_counts['L'])) / len(seq)) + + positive_charged_amino_acids = ['R', 'K', 'H'] + negative_charged_amino_acids = ['D', 'E'] + + total_positive_charged = sum(aa_counts.get(aa, 0) for aa in positive_charged_amino_acids) + total_negative_charged = sum(aa_counts.get(aa, 0) for aa in negative_charged_amino_acids) + + total_charge = total_positive_charged - total_negative_charged + charge_density = total_charge / len(seq) + alpha_helix, beta_helix, turn_helix = analysed_seq.secondary_structure_fraction() + + return list( + [round(analysed_seq.gravy(), 3) * 10, round(aliphatic_index, 3) * 10, round(analysed_seq.aromaticity(), 3) * 10, + round(analysed_seq.instability_index(), 3), round(alpha_helix * 10, 3), round(beta_helix * 10, 3), + round(turn_helix * 10, 3), round(analysed_seq.charge_at_pH(7), 3), round(analysed_seq.isoelectric_point(), 3), + round(charge_density, 3) * 10]) + + +class MDataset(Dataset): + def __init__(self, threshold=32, mode='train', max_length=50, pdb_src='af', data_ver='0922', model_mode='111'): + self.num_classes = 6 + exclude_list = pd.read_csv('metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() + exclude_filter = False + + p = PDBParser(QUIET=True) + + if mode == 'train': + all_data = pd.read_csv(f'../metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values + exclude_filter = True + elif mode == 'qlx': + all_data = pd.read_csv('../metadata/data_qlx.csv', encoding="unicode_escape").values + elif mode == 'saap': + all_data = pd.read_csv('../metadata/data_saap.csv', encoding="unicode_escape").values + else: + raise NotImplementedError + idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) + labels = (labels < threshold).astype(int) + + filter_idx_list = [] + seq_new_list = [] + label_list = [] + for idx in range(len(idx_list)): + seq = seq_list[idx].upper().strip() + if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: + continue + if exclude_filter: + if seq in exclude_list: + continue + + filter_idx_list.append(idx) + seq_new_list.append(seq) + label_list.append(labels[idx]) + + if pdb_src == 'af': + pdb_root = '../pdb/pdb_af/' + elif pdb_src == 'hf': + pdb_root = '../pdb/pdb_dbassp/' + else: + raise NotImplementedError + + if model_mode[1] == '0': + read_pdb_flag = False + else: + read_pdb_flag = True + + self.data_list = [] + for i in tqdm(range(len(filter_idx_list))): + idx = filter_idx_list[i] + seq = seq_new_list[i] + label = label_list[i] + if read_pdb_flag: + if os.path.exists(pdb_root + seq + ".pdb"): + pdb_path = pdb_root + seq + ".pdb" + else: + print(f'lacking pdb file {seq}') + continue + # raise FileNotFoundError + + structure = p.get_structure(idx, pdb_path) + voxel = pdb_parser(structure) + else: + voxel = 0 + + seq_emb = np.zeros((max_length, 21), dtype=np.float32) + for pos in range(len(seq)): + seq_emb[pos, AMAs[seq[pos]]] = 1 + + globf = calculate_property(seq) + + self.data_list.append((voxel, seq_emb, globf, label)) + + def __getitem__(self, idx): + voxel, seq_emb, globf, gt = self.data_list[idx] + + return torch.Tensor(voxel).float(), torch.Tensor(seq_emb).float(), torch.tensor(globf).float(), torch.Tensor(gt) + + def __len__(self): + return len(self.data_list) + + +# class SDataset(Dataset): +# ''' +# x: Features. +# y: Targets, if none, do prediction. +# ''' + +# def __init__(self, threshold=32, mode='train', task='0', max_length=50, pdb_src='af'): +# self.num_classes = 1 +# self.task = int(task) +# exclude_list = pd.read_csv('metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() +# exclude_filter = False + +# p = PDBParser(QUIET=True) + +# if mode == 'train': +# all_data = pd.read_csv('metadata/data_0922_i.csv', encoding="unicode_escape").values +# exclude_filter = True +# elif mode == 'qlx': +# all_data = pd.read_csv('metadata/data_qlx.csv', encoding="unicode_escape").values +# elif mode == 'saap': +# all_data = pd.read_csv('metadata/data_saap.csv', encoding="unicode_escape").values +# else: +# raise NotImplementedError +# idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) +# labels = (labels[:, self.task:self.task+1] <= threshold).astype(int) + +# filter_idx_list = [] +# seq_new_list = [] +# label_list = [] +# for idx in range(len(idx_list)): +# seq = seq_list[idx].upper().strip() +# if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: +# continue +# if exclude_filter: +# if seq in exclude_list: +# continue + +# filter_idx_list.append(idx) +# seq_new_list.append(seq) +# label_list.append(labels[idx]) + +# if pdb_src == 'af': +# pdb_root = './pdb/pdb_af/' +# elif pdb_src == 'hf': +# pdb_root = './pdb/pdb_dbassp/' +# else: +# raise NotImplementedError +# self.data_list = [] +# for i in tqdm(range(len(filter_idx_list))): +# idx = filter_idx_list[i] +# seq = seq_new_list[i] +# label = label_list[i] + +# # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"): +# # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb" +# # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"): +# # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb" +# if os.path.exists(pdb_root + seq + ".pdb"): +# pdb_path = pdb_root + seq + ".pdb" +# else: +# continue +# # raise FileNotFoundError + +# structure = p.get_structure(idx, pdb_path) +# voxel = pdb_parser(structure) +# seq_emb = [AMAs[char] for char in seq] + [0] * (max_length - len(seq)) +# self.data_list.append((voxel, seq_emb, label)) + +# def __getitem__(self, idx): +# voxel, seq_emb, gt = self.data_list[idx] + +# return torch.Tensor(voxel).float(), torch.Tensor(seq_emb), torch.Tensor(gt) + +# def __len__(self): +# return len(self.data_list) \ No newline at end of file diff --git a/t5/eval.py b/t5/eval.py new file mode 100644 index 0000000000000000000000000000000000000000..18f38bfff4c7b7f9bf2fef9834b96c07a96d947d --- /dev/null +++ b/t5/eval.py @@ -0,0 +1,203 @@ +import argparse +import json +import logging +import os +import time + +from dataset import SDataset, MDataset +from network import MMPeptide, SEQPeptide, VoxPeptide, MMFPeptide, SMPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=64, + help="MIC threshold for determine labels (default: 64)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=128, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.02, + help='learning rate (default: 0.002)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='micro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='mlce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + weight_dir = "./run/" + args.task_type + '-' + args.task + "-m-" + args.model + '-' + args.loss + str(args.batch_size) + str(args.lr) + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + raise ValueError + + logging.basicConfig(handlers=[ + # logging.FileHandler(filename=os.path.join(weight_dir, "eval.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + # with open(os.path.join(weight_dir, "config.json"), "w") as f: + # f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src) + else: + set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src) + else: + qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + # train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + + # train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=1, shuffle=False) + if args.model == 'seq': + model = SEQPeptide(classes=set_all.num_classes, q_encoder='mlp') + elif args.model == 'voxel': + model = VoxPeptide(classes=set_all.num_classes) + elif args.model == 'mm': + # model = MMPeptide(classes=train_set.num_classes, q_encoder='tf', ) # attention='hamburger' + # model = SMPeptide(classes=train_set.num_classes, q_encoder='mlp', max_length=30) # attention='hamburger' + model = MMPeptide(classes=set_all.num_classes, q_encoder='mlp', max_length=args.max_length)# , attention='hamburger' + elif args.model == 'mmf': + model = MMFPeptide(classes=set_all.num_classes, q_encoder='mlp', max_length=args.max_length) # attention='hamburger' + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + model.load_state_dict(torch.load(weights_path)) + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + + + if args.task_type in ('mlc', 'slc') : + _, ap, f1, acc, auc = train(args, None, model, None, valid_loader, device, None, None) + logging.info(f'ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, None, model, None, qlx_loader, device, None, None) + logging.info(f'QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, None, model, None, saap_loader, device, None, None) + logging.info(f'SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/eval.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/t5/gen_simi.py b/t5/gen_simi.py new file mode 100644 index 0000000000000000000000000000000000000000..c350b6cbccbe8e4884ea3bbbfd150eb1a66b348b --- /dev/null +++ b/t5/gen_simi.py @@ -0,0 +1,38 @@ +import pandas as pd +from dataset import calculate_similarity + +# 读取模板 CSV 文件和目标 CSV 文件 +template_file = './metadata/data_test.csv' # 模板CSV文件路径 +target_file = './metadata/data_0920_i.csv' # 目标CSV文件路径 + +template_df = pd.read_csv(template_file) +target_df = pd.read_csv(target_file) + +# 假设我们要使用 'template_column' 列作为模板中的列,'target_column' 列作为目标表格中的列 +template_column = 'Seq' # 替换为你的模板表格列名 +target_column = 'Seq' # 替换为你的目标表格列名 + +# 创建一个空的 DataFrame,用于存储符合条件的行,并复制目标表格的表头 +extracted_rows = pd.DataFrame(columns=target_df.columns) + +# 遍历目标表格的每一行 +for index, target_row in target_df.iterrows(): + target_value = target_row[target_column] + + # 遍历模板表格的每个值 + for template_value in template_df[template_column]: + + # 计算指标 + metric = calculate_similarity(target_value.strip().upper(), template_value.strip().upper()) + + # 如果指标大于0.3,则提取目标表格的该行 + if metric > 0.3: + extracted_rows = pd.concat([extracted_rows, pd.DataFrame([target_row])], ignore_index=True) + # 只要找到一个匹配值,就可以继续检查下一行 + break + +# 将提取的行保存到一个新的CSV文件中,确保表头与目标表格一致 +output_file = './metadata/data_simi.csv' +extracted_rows.to_csv(output_file, index=False) + +print(f"符合条件的行已保存到 {output_file}") \ No newline at end of file diff --git a/t5/infer.py b/t5/infer.py new file mode 100644 index 0000000000000000000000000000000000000000..f7804b4eca2bbca5db401b6aa5b84c3e7f7af554 --- /dev/null +++ b/t5/infer.py @@ -0,0 +1,128 @@ +import argparse +import json +import logging +import os +import time +import pandas as pd + +from dataset import MDataset +from network import FusionPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='resnet34', + help='resnet34 resnet50 densenet') +parser.add_argument('--channels', type=int, default=32) +parser.add_argument('--mode', type=str, default='111', + help="0 for off and 1 for on. First digit for seq, second for voxel, third for globf") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--sd', type=str, default='./run/101-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--sv', type=str, default='./run/110-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--vd', type=str, default='./run/011-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--svd', type=str, default='./run/111-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + +model_path = {'Seq.+Disc.': args.sd, 'Seq.+Voxel': args.sv, 'Voxel+Disc.': args.vd, 'Seq.+Voxel+Disc.': args.svd} + + +def main(): + set_seed(args.seed) + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + results = pd.DataFrame() + + logging.info('Loading Test Dataset') + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + + models = {'Seq.+Disc.': FusionPeptide(classes=qlx_set.num_classes, v_encoder=args.model, channels=args.channels, mode='101'), + 'Seq.+Voxel': FusionPeptide(classes=qlx_set.num_classes, v_encoder=args.model, channels=args.channels, mode='110'), + 'Voxel+Disc.': FusionPeptide(classes=qlx_set.num_classes, v_encoder=args.model, channels=args.channels, mode='011'), + 'Seq.+Voxel+Disc.': FusionPeptide(classes=qlx_set.num_classes, v_encoder=args.model, channels=args.channels, mode='111')} + + for model_name in model_path.keys(): + model = models[model_name] + pred_all = [] + gt_all = [] + for i in range(1,6): + model.load_state_dict(torch.load(os.path.join(model_path[model_name], f'model_{i}.pth'))) + model.to(device).eval() + with torch.no_grad(): + for data in qlx_loader: + voxel, seq, globf, gt = data + gt_all.append(gt.to(device)) + out = model((voxel.to(device), seq.to(device), globf.to(device))) + pred_all.append(out) + pred_all = torch.nn.functional.sigmoid(torch.cat(pred_all, dim=0)).squeeze().cpu().numpy() + gt_all = torch.cat(gt_all, dim=0).int().squeeze().cpu().numpy() + results[model_name] = pred_all.ravel(order='F') + + results['gt'] = gt_all.ravel(order='F') + + results.to_csv("preds.csv", index=False) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/t5/loss.py b/t5/loss.py new file mode 100644 index 0000000000000000000000000000000000000000..d2c1cabdc7bb5df53b8513344372ea557b89aa29 --- /dev/null +++ b/t5/loss.py @@ -0,0 +1,123 @@ +import torch +from torch import nn +import torch.nn.functional as F +from math import cos, pi, sin +import math +import numpy as np +from scipy.special import lambertw + + +def mixup_criterion(criterion, pred, y_a, y_b, lam, pow=2): + y = lam ** pow * y_a + (1 - lam) ** pow * y_b + return criterion(pred, y) + + +def mixup_data(v, q, a): + '''Returns mixed inputs, pairs of targets, and lambda without organ constraint''' + lam = np.random.beta(1, 1) + + batch_size = v.shape[0] + index = torch.randperm(batch_size) + + mixed_v = lam * v + (1 - lam) * v[index, :] + mixed_q = lam * q + (1 - lam) * q[index, :] + + a_1, a_2 = a, a[index] + return mixed_v, mixed_q, a_1, a_2, lam + + +def linear(epoch, nepoch): + return 1 - epoch / nepoch + + +def convex(epoch, nepoch): + return epoch / (2 - nepoch) + + +def concave(epoch, nepoch): + return 1 - sin((epoch / nepoch) * (pi / 2)) + + +def composite(epoch, nepoch): + return 0.5 * cos((epoch / nepoch) * pi) + 0.5 + + +class LogCoshLoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y_t, y_prime_t): + ey_t = y_t - y_prime_t + return torch.mean(torch.log(torch.cosh(ey_t + 1e-12)))+F.mse_loss(y_t, y_prime_t) + + +class MLCE(nn.Module): + def __init__(self): + super(MLCE, self).__init__() + + def _mlcce(self, y_pred, y_true): + y_pred = (1 - 2 * y_true) * y_pred + y_pred_neg = y_pred - y_true * 1e12 + y_pred_pos = y_pred - (1 - y_true) * 1e12 + zeros = torch.zeros_like(y_pred[..., :1]) + y_pred_neg = torch.cat([y_pred_neg, zeros], dim=-1) + y_pred_pos = torch.cat([y_pred_pos, zeros], dim=-1) + neg_loss = torch.logsumexp(y_pred_neg, dim=-1) + pos_loss = torch.logsumexp(y_pred_pos, dim=-1) + loss = torch.mean(neg_loss + pos_loss) + return loss + + def __call__(self, y_pred, y_true): + return self._mlcce(y_pred, y_true) + + +class SuperLoss(nn.Module): + def __init__(self, C=10, lam=1, batch_size=128): + super(SuperLoss, self).__init__() + self.tau = math.log(C) + self.lam = lam # set to 1 for CIFAR10 and 0.25 for CIFAR100 + self.batch_size = batch_size + + def forward(self, logits, targets): + l_i = F.mse_loss(logits, targets, reduction='none').detach() + sigma = self.sigma(l_i) + loss = (F.mse_loss(logits, targets, reduction='none') - self.tau) * sigma + self.lam * ( + torch.log(sigma) ** 2) + loss = loss.sum() / self.batch_size + return loss + + def sigma(self, l_i): + x = torch.ones(l_i.size()) * (-2 / math.exp(1.)) + x = x.cuda() + y = 0.5 * torch.max(x, (l_i - self.tau) / self.lam) + y = y.cpu().numpy() + sigma = np.exp(-lambertw(y)) + sigma = sigma.real.astype(np.float32) + sigma = torch.from_numpy(sigma).cuda() + return sigma + + +def unbiased_curriculum_loss(out, data, args, epoch, epochs, scheduler='linear'): + losses = [] + scheduler = linear if scheduler == 'linear' else concave + + # calculate difficulty measurement function + adjusted_losses = [] + for idx in range(out.shape[0]): + ground_truth = max(1, abs(data[idx].item())) + loss = F.mse_loss(out[idx], data[idx]) + losses.append(loss) + adjusted_losses.append(loss.item() / ground_truth) + + mean_loss, std_loss = np.mean(adjusted_losses), np.std(adjusted_losses) + + # re-weight losses + total_loss = 0 + for i, loss in enumerate(losses): + if adjusted_losses[i] > mean_loss + 1 * std_loss: + schedule_factor = scheduler(epoch, args.epochs) + total_loss += schedule_factor * loss + else: + total_loss += loss + + return total_loss diff --git a/t5/main.py b/t5/main.py new file mode 100644 index 0000000000000000000000000000000000000000..a9832e07b143dfb0a0c2d404be8cf41f8c116549 --- /dev/null +++ b/t5/main.py @@ -0,0 +1,236 @@ +import argparse +import json +import logging +import os +import time + +from dataset import MDataset +from network import FusionPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='resnet34', + help='resnet34 resnet50 densenet') +parser.add_argument('--channels', type=int, default=32) +parser.add_argument('--mode', type=str, default='111', + help="0 for off and 1 for on. First digit for seq, second for voxel, third for globf") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + if args.loss == 'mlce' and args.task_type != 'slc': + criterion = MLCE() + elif args.loss == "bce" or args.task_type == 'slc': + args.loss = "bce" + criterion = nn.BCEWithLogitsLoss() + else: + criterion = 0 + pass + weight_dir = "./run/" + args.mode + '-' + args.loss + str(args.batch_size) + '-' + str(args.lr) + '-' + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + os.makedirs(weight_dir) + + logging.basicConfig(handlers=[ + logging.FileHandler(filename=os.path.join(weight_dir, "training.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + with open(os.path.join(weight_dir, "config.json"), "w") as f: + f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src, data_ver=args.data_ver, model_mode=args.mode) + # else: + # set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src, model_mode=args.mode) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src, model_mode=args.mode) + # else: + # qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + # saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + # if args.loss == 'bce': + # weight = torch.Tensor([0] * args.classes) + # for i in train_set: + # weight += i[2] + # weight = (len(train_set) - weight) / weight + # criterion.register_buffer('pos_weight', weight.to(device)) + + train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=1, shuffle=False) + + model = FusionPeptide(classes=set_all.num_classes, v_encoder=args.model, channels=args.channels, mode=args.mode) + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + # optimizer = torch.optim.Adam(model.parameters()) + optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr) + # optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=True, weight_decay=5e-5) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Train set:{len(train_set)}, Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + best_metric = 0 + best_qlx = 0 + best_saap = 0 + start_time = time.time() + + for epoch in range(1, args.epochs + 1): + if args.task_type in ('mlc', 'slc') : + train_loss, ap, f1, acc, auc = train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer) + logging.info(f'Epoch: {epoch:03d}, Train Loss: {train_loss:.3f}, ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + avg_metric = ap + f1 + acc + auc + if avg_metric > best_metric or epoch % 10 == 0: + if avg_metric > best_metric: + logging.info(f'Epoch: {epoch:03d} New Best validation metrics, running test session') + torch.save(model.state_dict(), weights_path) + best_metric = avg_metric + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + else: + logging.info(f'Per 10 epochs check, running test session') + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, epoch, model, None, qlx_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_metric = qlx_ap + qlx_f1 + qlx_acc + qlx_auc + if qlx_metric > best_qlx: + best_qlx = qlx_metric + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + if epoch % 10 == 0: + torch.save(model.state_dict(), weights_path) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, epoch, model, None, saap_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_metric = saap_ap + saap_f1 + saap_acc + saap_auc + if saap_metric > best_saap: + best_saap = saap_metric + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'used time {(time.time()-start_time)/3600:.2f}h') + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/result.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/t5/network.py b/t5/network.py new file mode 100644 index 0000000000000000000000000000000000000000..ea43653482c5a06686c9f54e9d42cb4cd1d03e15 --- /dev/null +++ b/t5/network.py @@ -0,0 +1,789 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F +from math import ceil +from mamba_ssm import Mamba + + +class Hamburger(nn.Module): + def __init__(self, inp, oup, reduction=32): + super(Hamburger, self).__init__() + self.pool_h = nn.AdaptiveAvgPool3d((1, None, None)) + self.pool_w = nn.AdaptiveAvgPool3d((None, 1, None)) + self.pool_d = nn.AdaptiveAvgPool3d((None, None, 1)) + + mip = max(8, inp // reduction) + self.conv1 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.conv2 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.conv3 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.gn1 = nn.GroupNorm(8, mip) + self.gn2 = nn.GroupNorm(8, mip) + self.gn3 = nn.GroupNorm(8, mip) + + self.act = nn.LeakyReLU(0.2) + self.conv_h = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + self.conv_w = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + self.conv_d = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + + def forward(self, x): + n, c, h, w, d = x.size() + x_h = self.pool_h(x) + # print(x_h.shape) + x_w = self.pool_w(x).permute(0, 1, 3, 2, 4) + # print(x_w.shape) + x_d = self.pool_d(x).permute(0, 1, 4, 2, 3) + # print(x_d.shape) + y_hwd = torch.cat([x_h, x_w, x_d], dim=2) + # y_hd = torch.cat([x_h, x_d], dim=2) + # y_dw = torch.cat([x_d, x_w], dim=2) + y_hwd = self.conv1(y_hwd) + # y_hd = self.conv2(y_hd) + # y_dw = self.conv3(y_dw) + y_hwd = self.gn1(y_hwd) + # y_hd = self.gn2(y_hd) + # y_dw = self.gn3(y_dw) + y_hwd = self.act(y_hwd) + # y_hd = self.act(y_hd) + # y_dw = self.act(y_dw) + # print(y_hwd.shape) + x_h, x_w, x_d = torch.split(y_hwd, [1, 1, 1], dim=2) + x_w = x_w + x_h = x_h.permute(0, 1, 3, 2, 4) + x_d = x_d.permute(0, 1, 3, 4, 2) + a_h = self.conv_h(x_h).sigmoid() + a_w = self.conv_w(x_w).sigmoid() + a_d = self.conv_d(x_d).sigmoid() + a_hw = a_w * a_h + out = a_hw * a_d + return out + x + + +class BasicBlock3D(nn.Module): + expansion = 1 + + def __init__(self, in_channels, out_channels, stride=1, downsample=None): + super(BasicBlock3D, self).__init__() + self.conv1 = nn.Conv3d(in_channels, out_channels, kernel_size=3, + stride=stride, padding=1, bias=False) + self.bn1 = nn.BatchNorm3d(out_channels) + self.relu = nn.ReLU(inplace=True) + self.conv2 = nn.Conv3d(out_channels, out_channels, kernel_size=3, + stride=1, padding=1, bias=False) + self.bn2 = nn.BatchNorm3d(out_channels) + self.downsample = downsample + self.stride = stride + + def forward(self, x): + identity = 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: + identity = self.downsample(x) + + out += identity + out = self.relu(out) + + return out + +class Bottleneck3D(nn.Module): + expansion = 4 + + def __init__(self, in_channels, out_channels, stride=1, downsample=None): + super(Bottleneck3D, self).__init__() + self.conv1 = nn.Conv3d(in_channels, out_channels, kernel_size=1, bias=False) + self.bn1 = nn.BatchNorm3d(out_channels) + self.conv2 = nn.Conv3d(out_channels, out_channels, kernel_size=3, + stride=stride, padding=1, bias=False) + self.bn2 = nn.BatchNorm3d(out_channels) + self.conv3 = nn.Conv3d(out_channels, out_channels * self.expansion, + kernel_size=1, bias=False) + self.bn3 = nn.BatchNorm3d(out_channels * self.expansion) + self.relu = nn.ReLU(inplace=True) + self.downsample = downsample + self.stride = stride + + def forward(self, x): + identity = 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: + identity = self.downsample(x) + + out += identity + out = self.relu(out) + + return out + +class ResNet3D(nn.Module): + def __init__(self, block, layers, input_channels=4, base_channels=16, feature_dim=512): + super(ResNet3D, self).__init__() + self.in_channels = base_channels + + self.conv1 = nn.Conv3d(input_channels, base_channels, kernel_size=7, + stride=2, padding=3, bias=False) + self.bn1 = nn.BatchNorm3d(base_channels) + self.relu = nn.ReLU(inplace=True) + self.maxpool = nn.MaxPool3d(kernel_size=3, stride=2, padding=1) + + # 每个layer的通道数是基于 base_channels 乘以扩展因子 + self.layer1 = self._make_layer(block, base_channels, layers[0]) + self.layer2 = self._make_layer(block, base_channels * 2, layers[1], stride=2) + self.layer3 = self._make_layer(block, base_channels * 4, layers[2], stride=2) + self.layer4 = self._make_layer(block, base_channels * 8, layers[3], stride=2) + + self.avgpool = nn.AdaptiveAvgPool3d((1, 1, 1)) + self.fc = nn.Linear(base_channels * 8 * block.expansion, feature_dim) + + # 初始化权重 + self._initialize_weights() + + def _make_layer(self, block, out_channels, blocks, stride=1): + downsample = None + if stride != 1 or self.in_channels != out_channels * block.expansion: + downsample = nn.Sequential( + nn.Conv3d(self.in_channels, out_channels * block.expansion, + kernel_size=1, stride=stride, bias=False), + nn.BatchNorm3d(out_channels * block.expansion), + ) + + layers = [] + layers.append(block(self.in_channels, out_channels, stride, downsample)) + self.in_channels = out_channels * block.expansion + for _ in range(1, blocks): + layers.append(block(self.in_channels, out_channels)) + + return nn.Sequential(*layers) + + def _initialize_weights(self): + for m in self.modules(): + if isinstance(m, nn.Conv3d): + nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') + elif isinstance(m, (nn.BatchNorm3d, nn.GroupNorm)): + nn.init.constant_(m.weight, 1) + nn.init.constant_(m.bias, 0) + elif isinstance(m, nn.Linear): + nn.init.normal_(m.weight, 0, 0.01) + nn.init.constant_(m.bias, 0) + + def forward(self, x): + # Input shape: (B, C, D, H, W) + x = self.conv1(x) + x = self.bn1(x) + x = self.relu(x) + x = self.maxpool(x) + + x = self.layer1(x) # -> (B, C, D/4, H/4, W/4) + x = self.layer2(x) # -> (B, 2C, D/8, H/8, W/8) + x = self.layer3(x) # -> (B, 4C, D/16, H/16, W/16) + x = self.layer4(x) # -> (B, 8C, D/32, H/32, W/32) + + x = self.avgpool(x) # -> (B, 8C, 1, 1, 1) + x = torch.flatten(x, 1) # -> (B, 8C) + x = self.fc(x) # -> (B, feature_dim) + + return x + +def ResNet3D34(input_channels=4, base_channels=16, feature_dim=512): + """Constructs a ResNet-34 3D model.""" + return ResNet3D(BasicBlock3D, [3, 4, 6, 3], + input_channels=input_channels, + base_channels=base_channels, + feature_dim=feature_dim) + +def ResNet3D50(input_channels=4, base_channels=16, feature_dim=512): + """Constructs a ResNet-50 3D model.""" + return ResNet3D(Bottleneck3D, [3, 4, 6, 3], + input_channels=input_channels, + base_channels=base_channels, + feature_dim=feature_dim) + + +class DenseLayer3D(nn.Module): + """DenseNet3D 的基本层,包括批归一化、激活和卷积操作""" + def __init__(self, in_channels, growth_rate, bn_size=4, drop_rate=0.0): + super(DenseLayer3D, self).__init__() + self.bn1 = nn.BatchNorm3d(in_channels) + self.relu = nn.ReLU(inplace=True) + self.conv1 = nn.Conv3d(in_channels, bn_size * growth_rate, kernel_size=1, stride=1, bias=False) + + self.bn2 = nn.BatchNorm3d(bn_size * growth_rate) + self.conv2 = nn.Conv3d(bn_size * growth_rate, growth_rate, kernel_size=3, stride=1, padding=1, bias=False) + + self.drop_rate = drop_rate + + def forward(self, x): + out = self.bn1(x) + out = self.relu(out) + out = self.conv1(out) + + out = self.bn2(out) + out = self.relu(out) + out = self.conv2(out) + + if self.drop_rate > 0: + out = F.dropout3d(out, p=self.drop_rate, training=self.training) + + # 将输入和输出在通道维度上拼接 + out = torch.cat([x, out], 1) + return out + + +class DenseBlock3D(nn.Module): + """由多个 DenseLayer3D 组成的 DenseBlock""" + def __init__(self, num_layers, in_channels, growth_rate, bn_size=4, drop_rate=0.0): + super(DenseBlock3D, self).__init__() + layers = [] + for i in range(num_layers): + layers.append(DenseLayer3D( + in_channels + i * growth_rate, + growth_rate, + bn_size=bn_size, + drop_rate=drop_rate + )) + self.layer = nn.Sequential(*layers) + + def forward(self, x): + return self.layer(x) + + +class Transition3D(nn.Module): + """用于减少特征图的尺寸和通道数的过渡层""" + def __init__(self, in_channels, out_channels): + super(Transition3D, self).__init__() + self.bn = nn.BatchNorm3d(in_channels) + self.relu = nn.ReLU(inplace=True) + self.conv = nn.Conv3d(in_channels, out_channels, kernel_size=1, stride=1, bias=False) + self.pool = nn.AvgPool3d(kernel_size=2, stride=2) + + def forward(self, x): + out = self.bn(x) + out = self.relu(out) + out = self.conv(out) + out = self.pool(out) + return out + + +class DenseNet3D(nn.Module): + """DenseNet3D 模型""" + def __init__(self, input_channels=1, base_channels=64, growth_rate=32, block_layers=[3, 6, 12, 8], + bn_size=4, drop_rate=0.0, feature_dim=1024): + super(DenseNet3D, self).__init__() + self.growth_rate = growth_rate + + # 初始卷积层 + self.features = nn.Sequential( + nn.Conv3d(input_channels, base_channels, kernel_size=7, stride=2, padding=3, bias=False), + nn.BatchNorm3d(base_channels), + nn.ReLU(inplace=True), + nn.MaxPool3d(kernel_size=3, stride=2, padding=1) + ) + + # Dense Blocks 和 Transition Layers + num_features = base_channels + self.block_layers = [] + self.num_blocks = len(block_layers) + self.dense_blocks = nn.ModuleList() + self.trans_blocks = nn.ModuleList() + + for i, num_layers in enumerate(block_layers): + dense_block = DenseBlock3D( + num_layers=num_layers, + in_channels=num_features, + growth_rate=growth_rate, + bn_size=bn_size, + drop_rate=drop_rate + ) + self.dense_blocks.append(dense_block) + num_features = num_features + num_layers * growth_rate + + if i != self.num_blocks - 1: + trans_block = Transition3D( + in_channels=num_features, + out_channels=num_features // 2 + ) + self.trans_blocks.append(trans_block) + num_features = num_features // 2 + + # 最后一个 batch norm + self.bn_final = nn.BatchNorm3d(num_features) + self.relu_final = nn.ReLU(inplace=True) + + # 全局平均池化 + self.global_pool = nn.AdaptiveAvgPool3d((1, 1, 1)) + + # 分类头 + self.classifier = nn.Sequential( + nn.Flatten(), + nn.Linear(num_features, feature_dim) + ) + + # 初始化权重 + self._initialize_weights() + + def forward(self, x): + out = self.features(x) + + for i in range(self.num_blocks): + out = self.dense_blocks[i](out) + if i < self.num_blocks - 1: + out = self.trans_blocks[i](out) + + out = self.bn_final(out) + out = self.relu_final(out) + + out = self.global_pool(out) # (B, C, 1, 1, 1) + out = out.view(out.size(0), -1) # (B, C) + out = self.classifier(out) # (B, feature_dim) + return out + + def _initialize_weights(self): + """初始化权重""" + for m in self.modules(): + if isinstance(m, nn.Conv3d): + nn.init.kaiming_normal_(m.weight) + elif isinstance(m, nn.BatchNorm3d): + nn.init.constant_(m.weight, 1) + nn.init.constant_(m.bias, 0) + elif isinstance(m, nn.Linear): + nn.init.normal_(m.weight, 0, 0.01) + nn.init.constant_(m.bias, 0) + + + + +class FusionPeptide(nn.Module): + def __init__(self, v_encoder='resnet34', q_encoder='lstm', g_encoder='mlp', mode='111', classes=6, channels=16): + super().__init__() + if mode == '000': + raise KeyError('None of the module acitvated') + self.classes = classes + self.mode = [False, False, False] + final_dim = 0 + + if mode[0] == '1': + final_dim += 128 + self.mode[0] = True + if q_encoder == 'lstm': + self.q_encoder = nn.LSTM( + input_size=21, + hidden_size=128, + num_layers=2, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + self.q_fc = nn.Linear(256, 128) + else: + raise NotImplementedError + + if mode[1] == '1': + final_dim += 512 + self.mode[1] = True + if v_encoder == 'resnet34': + self.v_encoder = ResNet3D34(input_channels=4, base_channels=channels, feature_dim=512) + elif v_encoder == 'resnet50': + self.v_encoder = ResNet3D50(input_channels=4, base_channels=channels, feature_dim=512) + elif v_encoder == 'densenet': + self.v_encoder = DenseNet3D(input_channels=4, base_channels=channels, growth_rate=16 if channels < 48 else 32, feature_dim=512) + else: + raise NotImplementedError(f'\'{v_encoder}\' not implemented') + + if mode[2] == '1': + final_dim += 128 + self.mode[2] = True + if g_encoder == 'mlp': + self.g_encoder = MLP(10, 128, 128, 3, 0.3) + else: + raise NotImplementedError + + self.fc = nn.Sequential( + nn.Linear(final_dim, 128), nn.LeakyReLU(0.1), nn.Dropout(0.3), + nn.Linear(128, 64), nn.LeakyReLU(0.1), nn.Dropout(0.3), + nn.Linear(64, self.classes)) + + def forward(self, x): + vox, seq, globf = x + fusion = [] + if self.mode[0]: + fusion.append(self.q_fc(self.q_encoder(seq)[0][:, -1, :])) + if self.mode[1]: + fusion.append(self.v_encoder(vox)) + if self.mode[2]: + fusion.append(self.g_encoder(globf)) + fusion = torch.cat(fusion, dim=-1) + pred = self.fc(fusion) + return pred + + +class MLP(nn.Module): + def __init__(self, input_dim, hidden_dim, output_dim, num_layers, dropout_rate): + super(MLP, self).__init__() + + layers = [] + layers.append(nn.Linear(input_dim, hidden_dim)) + layers.append(nn.ReLU()) + layers.append(nn.Dropout(dropout_rate)) + + for _ in range(num_layers - 1): + layers.append(nn.Linear(hidden_dim, hidden_dim)) + layers.append(nn.ReLU()) + layers.append(nn.Dropout(dropout_rate)) + + layers.append(nn.Linear(hidden_dim, output_dim)) + + self.network = nn.Sequential(*layers) + + def forward(self, x): + return self.network(x) + + +class PositionalEncoding(nn.Module): + def __init__(self, d_model, max_len=50): + super(PositionalEncoding, self).__init__() + + pe = torch.zeros(max_len, d_model) # (max_len, d_model) + position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1) # (max_len, 1) + div_term = torch.exp(torch.arange(0, d_model, 2).float() * + (-torch.log(torch.FloatTensor([10000.0])) / d_model)) # (d_model/2,) + pe[:, 0::2] = torch.sin(position * div_term) # 偶数维 + pe[:, 1::2] = torch.cos(position * div_term) # 奇数维 + pe = pe.unsqueeze(0) # (1, max_len, d_model) + self.register_buffer('pe', pe) + + def forward(self, x): + """ + x: (B, N, d_model) + """ + x = x + self.pe[:, :x.size(1), :] + return x + + +class TransformerModel(nn.Module): + def __init__(self, nheads, d_model, num_layers, out_dim, max_length=50): + super(TransformerModel, self).__init__() + + # 嵌入层,将输入从 (B, N) 转换到 (B, N, embed_dim) + self.embedding = nn.Linear(1, d_model) + + # 位置编码 + self.pos_encoder = PositionalEncoding(d_model, max_length) + + # Transformer 编码器层 + encoder_layers = nn.TransformerEncoderLayer(d_model=d_model, + nhead=nheads, + activation='relu') + self.transformer_encoder = nn.TransformerEncoder(encoder_layers, num_layers=num_layers) + + # 全局池化(可以根据任务选择不同的聚合方式) + self.global_pool = nn.AdaptiveAvgPool1d(1) + + # 输出层 + self.fc = nn.Linear(d_model, out_dim) + + def forward(self, src): + """ + src: (B, N) + """ + # 嵌入 + embedded = self.embedding(src.unsqueeze(-1)) # (B, N, embed_dim) + embedded = self.pos_encoder(embedded) # 添加位置编码 + + # 转置以适应 Transformer (N, B, embed_dim) + embedded = embedded.permute(1, 0, 2) + + # Transformer 编码 + transformer_out = self.transformer_encoder(embedded) # (N, B, embed_dim) + + # 转置回 (B, N, embed_dim) + transformer_out = transformer_out.permute(1, 0, 2) + + # 全局池化,将 (B, N, embed_dim) 转换为 (B, embed_dim) + pooled = self.global_pool(transformer_out.permute(0, 2, 1)).squeeze(-1) + + # 输出层 + output = self.fc(pooled) # (B, output_dim) + + return output + + +class MambaModel(nn.Module): + def __init__(self, d_model, out_dim, max_length=30): + super(MambaModel, self).__init__() + self.linear = nn.Linear(in_features=1, out_features=d_model) + self.pos_encoder = PositionalEncoding(d_model, max_length) + self.mamba = Mamba(d_model=d_model) + self.global_pool = nn.AdaptiveAvgPool1d(1) + self.fc = nn.Linear(d_model * 2, out_dim) + + def forward(self, x: torch.Tensor): + x = self.pos_encoder(self.linear(x.unsqueeze(-1))) + y = self.mamba(x) + y_flip = self.mamba(x.flip([-2])).flip([-2]) + y = torch.cat((y, y_flip), dim=-1) + y = self.fc(self.global_pool(y.permute(0, 2, 1)).squeeze(-1)) + return y + + +class SEQ(nn.Module): + def __init__(self, seq_type='mlp', input_dim=21, hidden_dim=128, out_dim=128, num_layers=2, max_length=30): + super(SEQ, self).__init__() + self.seq_type = seq_type + if seq_type == 'rnn': + self.rnn = nn.RNN( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'gru': + self.rnn = nn.GRU( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'lstm': + self.rnn = nn.LSTM( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'tf': + self.transformer = TransformerModel(nheads=4, d_model=hidden_dim, num_layers=2, out_dim=out_dim, max_length=max_length) + elif seq_type == 'mamba': + self.mamba = MambaModel(d_model=hidden_dim, out_dim=out_dim, max_length=max_length) + else: + # nn.Linear(50, 50, bias=False), nn.ReLU(), + self.rnn = nn.Sequential(nn.Linear(max_length, hidden_dim * 4), nn.ReLU(), nn.Linear(hidden_dim * 4, out_dim)) + self.rnn_fc = nn.Sequential( + nn.Linear(hidden_dim * 2, hidden_dim), + nn.ReLU(), + nn.Linear(hidden_dim, out_dim) + ) + + def forward(self, seq): + if self.seq_type == 'mlp': + return self.rnn(seq.squeeze(1)) + elif self.seq_type == 'tf': + return self.transformer(seq) + elif self.seq_type == 'mamba': + return self.mamba(seq) + else: + one_hot_seq = F.one_hot(seq.to(torch.int64), num_classes=21).float() + r_out = self.rnn(one_hot_seq, None)[0] # None represents zero initial hidden state + out = self.rnn_fc(r_out[:, -1, :]) + return out + # def forward(self, x, seq_lengths): + # + + +class SEQPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + self.classes = classes + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + + self.seq_fc = nn.Linear(128, classes) + + def forward(self, x, seq_lengths=None): + vox, seq = x + seq_emb = self.q_encoder(seq) + pred = self.seq_fc(seq_emb) + return pred + + +class ConvNet(nn.Module): + def __init__(self, num_classes: int = 2): + super(ConvNet, self).__init__() + self.conv1 = nn.Conv1d(3, 16, 3, padding=1) + self.conv2 = nn.Conv1d(16, 32, 3, padding=1) + self.fc1 = nn.Linear(32 * 7, 128) + # self.fc2 = nn.Linear(128, num_classes) + + def forward(self, x): + # print(x.shape) + x = x.permute(0, 2, 1) + x = F.relu(self.conv1(x)) + x = F.max_pool1d(x, 2) + x = F.relu(self.conv2(x)) + x = F.max_pool1d(x, 2) + x = x.view(x.shape[0], -1) + return self.fc1(x) + # x = F.relu(self.fc1(x)) + # x = self.fc2(x) + # return x + + +class ConvNet2D(nn.Module): + def __init__(self, num_classes: int = 2): + super(ConvNet2D, self).__init__() + self.conv1 = nn.Conv2d(1, 8, 3, padding=1) + self.conv2 = nn.Conv2d(8, 16, 3, padding=2, stride=2) + self.conv3 = nn.Conv2d(16, 32, 3, padding=2, stride=2) + # self.pool = nn.AdaptiveAvgPool2d(32) + self.fc1 = nn.Linear(32 * 3 * 9, 128) + # self.fc2 = nn.Linear(128, num_classes) + + def forward(self, x): + # print(x.shape) + x = x.unsqueeze(1) + x = F.relu(self.conv1(x)) + # print(x.shape) + x = F.relu(self.conv2(x)) + # print(x.shape) + x = F.relu(self.conv3(x)) + # x = self.pool(x) + # print(x.shape) + x = x.view(x.shape[0], -1) + return self.fc1(x) + + +# convnet = ConvNet() +# print(convnet) +class MMPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + if attention == 'hamburger': + self.attention = Hamburger(2048, 2048) + else: + self.attention = None + # v_encoder could be resnet26 or resnet50 + if v_encoder == 'resnet26': + self.v_encoder = ResNet3D(Bottleneck3D, [1, 2, 4, 1], self.attention) + # self.v_encoder = SwinUNETR(img_size=(64, 64, 64), in_channels=3, out_channels=1) + elif v_encoder == 'resnet50': + self.v_encoder = ResNet3D(Bottleneck3D, [3, 4, 6, 3], self.attention) + else: + raise NotImplementedError + + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + # self.ss_encoder = SEQ(seq_type=q_encoder) + if fusion == 'mlp': + self.fusion = nn.Linear(512 * 4 + 256, 256) + # self.fusion = nn.Linear(192 + 256, classes) + elif fusion == 'att': + self.fusion = nn.Linear(512 * 4 + 256, 256) + else: + raise NotImplementedError + + # self.vox_fc = nn.Linear(2048, classes) + # self.seq_fc = nn.Linear(256, classes) + self.out = nn.Sequential(nn.ReLU(inplace=True), nn.Linear(256, classes)) + self.classes = classes + + def forward(self, x, seq_lengths=None): + vox, seq = x + # print(vox.shape) + # print(seq.shape) + vox_emb = self.v_encoder(vox) + # print(vox_emb.shape) + seq_emb = self.q_encoder(seq, seq_lengths) + # print(seq_emb.shape) + # ss_emb = self.ss_encoder(second_s) + fused_feature = torch.cat((seq_emb, vox_emb), dim=1) + pred = self.fusion(fused_feature) + pred = self.out(pred) + # pred1 = self.vox_fc(vox_emb) + # pred2 = self.seq_fc(seq_emb) + # return pred, fused_feature + return pred + + +class SMPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, hidden_dim=256, max_length=30): + super().__init__() + self.siamese_encoder1 = MMPeptide(v_encoder, q_encoder, fusion, classes, attention, max_length) + # self.siamese_encoder2 = MMPeptide(v_encoder, q_encoder, fusion, classes, attention) + self.fc = nn.Sequential( + nn.Linear(hidden_dim * 2, hidden_dim), + nn.ReLU(), + nn.Linear(hidden_dim, 1) + ) + + def forward(self, x, seq_lengths=None): + f_mutated = self.siamese_encoder1(x[0]) + f_wide_type = self.siamese_encoder1(x[1]) + return self.fc(torch.cat((f_mutated, f_wide_type), dim=1)) + + +class MMFPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + if attention == 'hamburger': + self.attention = Hamburger(2048, 2048) + else: + self.attention = None + # v_encoder could be resnet26 or resnet50 + if v_encoder == 'resnet26': + self.v_encoder = ResNet3D(Bottleneck3D, [1, 2, 4, 1], self.attention) + # self.v_encoder = ResNet3DFusion(Bottleneck, [1, 2, 4, 1], self.attention) + elif v_encoder == 'resnet50': + self.v_encoder = ResNet3D(Bottleneck3D, [3, 4, 6, 3], self.attention) + else: + raise NotImplementedError + + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + + if fusion == 'mlp': + self.fusion = nn.Linear(512 * 4 + 256, classes) + elif fusion == 'att': + self.fusion = nn.Linear(512 * 4 + 256, classes) + else: + raise NotImplementedError + + self.vox_fc = nn.Linear(2048, classes) + self.seq_fc = nn.Linear(256, classes) + + def forward(self, x, seq_lengths=None): + vox, seq = x + # print(vox.shape) + # print(seq.shape) + seq_emb = self.q_encoder(seq, seq_lengths) + + vox_emb = self.v_encoder(vox, seq_emb) + # print(vox_emb.shape) + # print(seq_emb.shape) + fused_feature = torch.cat((seq_emb, vox_emb), dim=1) + pred = self.fusion(fused_feature) + # pred1 = self.vox_fc(vox_emb) + # pred2 = self.seq_fc(seq_emb) + return pred + + +if __name__ == "__main__": + # model = MMFPeptide() + # voxel = torch.zeros((4, 3, 64, 64, 64)) + # # # h_in = torch.zeros((2, 2048, 2, 2, 2)) + # # # h = Hamburger(2048, 2048) + # # # h(h_in) + # seq = torch.ones((4, 50)) + # res = model.forward((voxel, seq)) + # out = model((voxel, seq)) + # print(out.shape) + # model = ConvNet2D() + input_seq = torch.ones((4, 1, 30)) + # model(input_seq) + transformer = TransformerModel(nhead=4, d_model=32, num_layers=2) + print(transformer(input_seq).shape) diff --git a/t5/run/011-bce256-0.001-50af/config.json b/t5/run/011-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..50d4a1ab2a7146d6211b055a5ce66d7cfdf29b5e --- /dev/null +++ b/t5/run/011-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "channels": 32, "mode": "011", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t5/run/011-bce256-0.001-50af/model_1.pth b/t5/run/011-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..90870c23b94a9a60f92d23dad574c4be99862f06 --- /dev/null +++ b/t5/run/011-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b988336303e76df3381ca8672f170c686b9e38fd858e0f58b2d9b864845838df +size 64847266 diff --git a/t5/run/011-bce256-0.001-50af/model_2.pth b/t5/run/011-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..ba7c8a1415956e452c49b387ad8293566a306146 --- /dev/null +++ b/t5/run/011-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e75873c9d1f81e9a1a68c163c8f8ffa81c8c0ccdd49fa3e2437e12e46c6352ab +size 64847266 diff --git a/t5/run/011-bce256-0.001-50af/model_3.pth b/t5/run/011-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..38eb23c735852048da9da2390d1436e867730588 --- /dev/null +++ b/t5/run/011-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fc1b2dcc13d2d63a26285c44e3e65feb13597196f53723c93fc3e6d2da29bfc8 +size 64847266 diff --git a/t5/run/011-bce256-0.001-50af/model_4.pth b/t5/run/011-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..04cf79a40a3d546f59ad5025794533ff09900d9d --- /dev/null +++ b/t5/run/011-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9813e4157a6418ea3ab7a481933a6d24419a44fc50baeb608d788142d4404f8b +size 64847266 diff --git a/t5/run/011-bce256-0.001-50af/model_5.pth b/t5/run/011-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..4e5e00f5c86955dd53e18a1d799df86a590e8e92 --- /dev/null +++ b/t5/run/011-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f339643bf8d7e0ca0e71b6c4e9d85600de366685a7413d7fa3e9918d21b625ad +size 64847266 diff --git a/t5/run/011-bce256-0.001-50af/result.txt b/t5/run/011-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..183f6d6397330948da3bc110d7a0b0258f15d4b2 --- /dev/null +++ b/t5/run/011-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8377629160881043,0.8046760797500611,0.7387343287467957,0.778809106349945 +0.011419252197720555,0.008681232761677106,0.009972909981627085,0.008012996237645433 +qlx +0.7915665507316589,0.7379152297973632,0.6693156719207763,0.7839010715484619 +0.01996951545502594,0.02046348390391133,0.02250787747638132,0.020972048070935008 +saap +0.45398326516151427,0.5117629885673523,0.39199998378753664,0.5437707960605621 +0.030967586296975578,0.0034553748808734516,0.005999994277954101,0.03800926656128362 diff --git a/t5/run/101-bce256-0.001-50af/config.json b/t5/run/101-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..6ceea80c6db545b7a19cfd04609d28a9634dab60 --- /dev/null +++ b/t5/run/101-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "channels": 32, "mode": "101", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t5/run/101-bce256-0.001-50af/model_1.pth b/t5/run/101-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..dc70daa87fb375fe65a34f99e921a81eec2a942b --- /dev/null +++ b/t5/run/101-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7ca41bf3adf623596b1815f42d73f2fa5508dd480a925ba800d5cb0e83764fed +size 2708515 diff --git a/t5/run/101-bce256-0.001-50af/model_2.pth b/t5/run/101-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..dd03fe87315b2d2f91a1706b5fb8d0f924c74b44 --- /dev/null +++ b/t5/run/101-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d7f12ecc1288e10c82b9e9f48d889e95b5fa4b35674ddd05969161984c3ec246 +size 2708515 diff --git a/t5/run/101-bce256-0.001-50af/model_3.pth b/t5/run/101-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..0561c6c54281e24213acce891974f8a84749d074 --- /dev/null +++ b/t5/run/101-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:00ab55b5f68cfc37c2768dc646fa944b8af2d440cb7dddc077c91f76e3f4c337 +size 2708515 diff --git a/t5/run/101-bce256-0.001-50af/model_4.pth b/t5/run/101-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..8b736367d7886fac049879c76b6ccefb01e0f39c --- /dev/null +++ b/t5/run/101-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f2f8925bcf4b3b2175ec70f36aff9878c85959211ac43bdc465f38391dfdb2f7 +size 2708515 diff --git a/t5/run/101-bce256-0.001-50af/model_5.pth b/t5/run/101-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..7488fb29643dc616c30a87b277ed5603bdc6479d --- /dev/null +++ b/t5/run/101-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:82cc4cb491912d5ba8ee677b104bedec3bfbd3d6f81ce5e6677a05825d3b0223 +size 2708515 diff --git a/t5/run/101-bce256-0.001-50af/result.txt b/t5/run/101-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..debb62e7ea363829b808be443b87a9172134b6d2 --- /dev/null +++ b/t5/run/101-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8926330089569092,0.8463260293006897,0.7943743467330933,0.8478242635726929 +0.00680226074532931,0.007103850208925431,0.007880895940061104,0.008573066102051866 +qlx +0.8094740033149719,0.7590240240097046,0.6715231657028198,0.8285720705986023 +0.015118698463534284,0.012455559800983668,0.0233265382347594,0.02104578043594882 +saap +0.533372837305069,0.513932740688324,0.3936666488647461,0.6385525465011597 +0.03988184733425455,0.0021755433870076814,0.006863746881539309,0.0509698383630384 diff --git a/t5/run/110-bce256-0.001-50af/config.json b/t5/run/110-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..2f317500e7cb782e5a5751d7fdd35b6fdee56f62 --- /dev/null +++ b/t5/run/110-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "channels": 32, "mode": "110", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t5/run/110-bce256-0.001-50af/model_1.pth b/t5/run/110-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..3bb64312ad0d41793f68e3206c0c0953940c7257 --- /dev/null +++ b/t5/run/110-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f0d98fd47291ecdcccafaf8d45dece9aa5d7a5ffa5cf3e60b03d8df18b100981 +size 66974188 diff --git a/t5/run/110-bce256-0.001-50af/model_2.pth b/t5/run/110-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..2467d390c1561c77b0313743817277ad989f5dfd --- /dev/null +++ b/t5/run/110-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:52e91d9b1e010be193a3d8a2a1588c8ba517f7e738d6d443b0e9f9c8ef5a2e7f +size 66974188 diff --git a/t5/run/110-bce256-0.001-50af/model_3.pth b/t5/run/110-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..f4e4b0196d0211e2b83dd5ba53031b56b80fca01 --- /dev/null +++ b/t5/run/110-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:91add3f589e41e478f86c78935a54b21eb0e50cc953883e8728ebb16357e2e6e +size 66974188 diff --git a/t5/run/110-bce256-0.001-50af/model_4.pth b/t5/run/110-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..fd9a976fe9fe10fa1979c0a253870455ed8d093a --- /dev/null +++ b/t5/run/110-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3c89b713e6b7ffa5b157ae208e94b6c2bd7c5541931f9b40266e01f2fa89baeb +size 66974188 diff --git a/t5/run/110-bce256-0.001-50af/model_5.pth b/t5/run/110-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..556ee8e0de0160253713e74955e9a99392f5250f --- /dev/null +++ b/t5/run/110-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:48953ba66213e4c76a3fa3fea3e7f448f42327210ccfe6588c08223b50f33c7a +size 66974188 diff --git a/t5/run/110-bce256-0.001-50af/result.txt b/t5/run/110-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..6989cd6faba67764bc0ac331fa84ab5f847e2997 --- /dev/null +++ b/t5/run/110-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8270964026451111,0.7950995683670044,0.7261725902557373,0.7672364711761475 +0.010131641561460927,0.011336222405992585,0.008989606768946041,0.010534804555047895 +qlx +0.7921649813652039,0.7270549416542054,0.6587196230888367,0.7738793849945068 +0.012893440897276379,0.010906094129096813,0.026731785922307745,0.018354600991143267 +saap +0.45106415152549745,0.5062595307826996,0.4029999852180481,0.5469066381454468 +0.024832597373409405,0.016614131007408568,0.0249087217926645,0.024214154903104128 diff --git a/t5/run/111-bce256-0.001-50af/config.json b/t5/run/111-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..fc02ccf1e82a09f1869c7ed18de84fb0b0c05b74 --- /dev/null +++ b/t5/run/111-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "channels": 32, "mode": "111", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t5/run/111-bce256-0.001-50af/model_1.pth b/t5/run/111-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..3b7024d04d323dd24e72680cb088912f4fa230cf --- /dev/null +++ b/t5/run/111-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ba123fb54ed0fa5e1b7c4d6cfab9e67f8cd32c79f19ae66ecf2af2e7f99530fc +size 67246492 diff --git a/t5/run/111-bce256-0.001-50af/model_2.pth b/t5/run/111-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..d8cbfe32b34bcea9e8678192d44de88204f2f72d --- /dev/null +++ b/t5/run/111-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1db25a9cbf902232e0dfaa9b21c94c8af87a39a8869744c6c1f4b7e8f901059c +size 67246492 diff --git a/t5/run/111-bce256-0.001-50af/model_3.pth b/t5/run/111-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..1012e1e103ae50a00175f04d7136096829974a0d --- /dev/null +++ b/t5/run/111-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:83c235e4fe8a3f7457bde290287ac614d2bd569bc982b8f9c10d8fd17dd05da0 +size 67246492 diff --git a/t5/run/111-bce256-0.001-50af/model_4.pth b/t5/run/111-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..c16a13d985e90722e5693b83d6a07343080ce32d --- /dev/null +++ b/t5/run/111-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9b819c20af92d4d5860c8c72e4670bdf1bd50ebde8dc684979be0bef606a7f86 +size 67246492 diff --git a/t5/run/111-bce256-0.001-50af/model_5.pth b/t5/run/111-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..10b09610fa13e3132dde106d799822e8e07d2cdd --- /dev/null +++ b/t5/run/111-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:18b4bea3cf438abc226d8ebae2b23511c81c63723e6fc71c2280a9b52cd59839 +size 67246492 diff --git a/t5/run/111-bce256-0.001-50af/result.txt b/t5/run/111-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..c7140c2c75e9b2a3b348c6cd7002db5ea8fd2120 --- /dev/null +++ b/t5/run/111-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8372787475585938,0.8067479491233825,0.7323155999183655,0.7748182892799378 +0.008804611534477416,0.00770415441710182,0.011126953179174464,0.004708410723769284 +qlx +0.7916176319122314,0.7328259587287903,0.6635761499404907,0.7753720879554749 +0.01880474301438526,0.02410084746172429,0.03402996712148009,0.02246717113113222 +saap +0.46253547072410583,0.5001750349998474,0.4326666593551636,0.5602802515029908 +0.02501921927385779,0.02657303290124865,0.06430483706022455,0.03893647005513856 diff --git a/t5/runthro.sh b/t5/runthro.sh new file mode 100644 index 0000000000000000000000000000000000000000..0e8f3911704883b943371c9c685867d16223c3f1 --- /dev/null +++ b/t5/runthro.sh @@ -0,0 +1,5 @@ +#!/bin/bash +python main.py --mode 101 +python main.py --mode 110 +python main.py --mode 011 +python main.py --mode 111 diff --git a/t5/summary.py b/t5/summary.py new file mode 100644 index 0000000000000000000000000000000000000000..420cc50d5a8330d07342a07c2f5bfc482023480a --- /dev/null +++ b/t5/summary.py @@ -0,0 +1,59 @@ +import os +import csv +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument('--dir', type=str, default='./run') +parser.add_argument('--output', type=str, default='summary.csv') +args = parser.parse_args() + +# 设置目录路径和输出CSV文件名 +directory_path = args.dir +output_csv = args.output + +# 创建CSV文件并写入数据 +with open(output_csv, mode='w', newline='') as file: + writer = csv.writer(file) + + # 写入第一个子表的标题和表头 + writer.writerow(['QLX']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 6: + means_5_6 = lines[4].strip().split(',') + std_devs_5_6 = lines[5].strip().split(',') + if len(means_5_6) == 4 and len(std_devs_5_6) == 4: + values_5_6 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_5_6, std_devs_5_6)] + writer.writerow([subdirectory] + values_5_6) + + # 加入空行以分隔两个子表 + writer.writerow([]) + + # 写入第二个子表的标题和表头 + writer.writerow(['SAAP']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 再次遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 9: + means_8_9 = lines[7].strip().split(',') + std_devs_8_9 = lines[8].strip().split(',') + if len(means_8_9) == 4 and len(std_devs_8_9) == 4: + values_8_9 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_8_9, std_devs_8_9)] + writer.writerow([subdirectory] + values_8_9) \ No newline at end of file diff --git a/t5/train.py b/t5/train.py new file mode 100644 index 0000000000000000000000000000000000000000..20195770e743f2f83d4fd5c97e670ea51d672c26 --- /dev/null +++ b/t5/train.py @@ -0,0 +1,61 @@ +import torch +from torchmetrics import F1Score, Accuracy, AveragePrecision, AUROC + + +def train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer): + train_loss = 0 + num_labels = model.classes + avg = args.metric_avg + if num_labels == 1: + task = 'binary' + else: + task = 'multilabel' + metric_macro_acc = Accuracy(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_f1 = F1Score(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_ap = AveragePrecision(average=avg, task=task, num_labels=num_labels).to(device) + metric_auc = AUROC(average=avg, task=task, num_labels=num_labels).to(device) + + if train_loader is not None: + model.train() + for data in train_loader: + voxel, seq, globf, gt = data + # print(seq_lengths) + out = model((voxel.to(device), seq.to(device), globf.to(device))) + # print(out[0]) + # print(gt[0]) + loss = criterion(out, gt.to(device).float()) + # loss_0 = criterion(out[0], gt.to(device).float()) + # loss_1 = criterion(out[1], gt.to(device).float()) + # loss_2 = criterion(out[2], gt.to(device).float()) + # loss = loss_0 + loss_1 + loss_2 + loss.backward() + optimizer.step() + optimizer.zero_grad() + train_loss += loss.item() + + model.eval() + preds = [] + gt_list_valid = [] + with torch.no_grad(): + for data in valid_loader: + voxel, seq, globf, gt = data + gt_list_valid.append(gt.to(device)) + out = model((voxel.to(device), seq.to(device), globf.to(device))) + preds.append(out) + + # calculate metrics + preds = torch.nn.functional.sigmoid(torch.cat(preds, dim=0)) + gt_list_valid = torch.cat(gt_list_valid, dim=0).int() + # if train_loader is None: + # print(preds) + # print(preds.shape) + # print(gt_list_valid) + # print(gt_list_valid.shape) + + macro_ap = metric_macro_ap(preds, gt_list_valid).item() + # class_ap = [round(i.item(), 5) for i in metric_class_ap(preds, gt_list_valid)] + macro_auc = metric_auc(preds, gt_list_valid).item() + macro_f1 = metric_macro_f1(preds, gt_list_valid).item() + macro_acc = metric_macro_acc(preds, gt_list_valid).item() + return train_loss, macro_ap, macro_f1, macro_acc, macro_auc + diff --git a/t5/utils.py b/t5/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..7fd5de2ae11e3a64de85ddc8c0ce0f777a738a48 --- /dev/null +++ b/t5/utils.py @@ -0,0 +1,32 @@ +import random +import torch +import numpy as np + + +def set_seed(seed): + random.seed(seed) + np.random.seed(seed) + torch.manual_seed(seed) + torch.cuda.manual_seed(seed) + torch.cuda.manual_seed_all(seed) + torch.backends.cudnn.deterministic = True + torch.backends.cudnn.benchmark = False + + +def load_pretrain_model(net, weights): + net_keys = list(net.state_dict().keys()) + weights_keys = list(weights.keys()) + # assert(len(net_keys) <= len(weights_keys)) + i = 0 + j = 0 + while i < len(net_keys) and j < len(weights_keys): + name_i = net_keys[i] + name_j = weights_keys[j] + if net.state_dict()[name_i].shape == weights[name_j].shape: + net.state_dict()[name_i].copy_(weights[name_j].cpu()) + i += 1 + j += 1 + else: + i += 1 + # print i, len(net_keys), j, len(weights_keys) + return net diff --git a/t5g/dataset.py b/t5g/dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..a14df8e595f8ef32f48b51bdbaf148d8ad737b4c --- /dev/null +++ b/t5g/dataset.py @@ -0,0 +1,383 @@ +import os +import networkx as nx +import torch +from Bio.PDB import PDBParser, is_aa +from Bio.PDB.Polypeptide import three_to_one +from torch.utils.data import Dataset +import pandas as pd +import numpy as np +from tqdm import tqdm +from Bio.Align import PairwiseAligner +from torch_geometric.data import Data +from torch_geometric.utils import from_networkx +from Bio.SeqUtils.ProtParam import ProteinAnalysis + + +aligner = PairwiseAligner() +# Define the scoring system +aligner.mode = 'global' # Global alignment +aligner.match_score = 1 +aligner.mismatch_score = -1 +aligner.open_gap_score = -0.5 +aligner.extend_gap_score = -0.1 + + +def calculate_similarity(seq_gen, seq_tem): + + # Perform the alignment + score = aligner.score(seq_gen, seq_tem) + return score / len(seq_gen) + + +def parse_ll37_testset(path='metadata/LL37_v0.csv'): + files = open(path, 'r').readlines() + seqs = [] + for file in files: + seqs.append(str(file.strip().split(',')[-1].strip().upper())) + return seqs + + +def gmean(labels): + # Assuming 'labels' is a NumPy array of shape (n, m) + # Calculate the geometric mean across axis 1 + + # Take the product of elements along axis=1 + product = np.prod(labels, axis=1) + + # nth root of product, where n is the number of elements along axis=1 + geometric_means = product ** (1.0 / labels.shape[1]) + return geometric_means + + +def clamp(n, smallest, largest): + return sorted([smallest, n, largest])[1] + + +ATOMS = {'H': 1, 'C': 12, 'N': 14, 'O': 16, 'S': 30} +ATOMS_R = {'H': 1, 'C': 1.5, 'N': 1.5, 'O': 1.5, 'S': 2} +AMINO_ACID_WATER = {'A': 255, 'V': 255, 'P': 255, 'F': 255, 'W': 255, 'I': 255, 'L': 255, 'G': 155, 'M': 155, + 'Y': 55, 'S': 55, 'T': 55, 'C': 55, 'N': 55, 'Q': 55, 'D': 55, 'E': 55, 'K': 55, 'R': 55, 'H': 55} +AMINO_ACID_CHARGE = {'D': 55, 'E': 55, 'A': 155, 'V': 155, 'P': 155, 'F': 155, 'W': 155, 'I': 155, 'L': 155, 'G': 155, + 'M': 155, 'Y': 155, 'S': 155, 'T': 155, 'C': 155, 'N': 155, 'Q': 155, 'K': 255, 'R': 255, 'H': 255} +# AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, +# 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19} +AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, + 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19, 'X': 21} + + +def pdb_parser(structure): + """ + + """ + voxel = np.zeros((4, 64, 64, 64), dtype=np.int8) + id = '' + seq_str = '' + for i in structure[0]: + id = i.id + chain = structure[0][id] + for res in chain: + if is_aa(res.get_resname(), standard=True): + resname = res.get_resname() + amino = three_to_one(resname) + seq_str += str(amino) + ATOM_WATER = AMINO_ACID_WATER[amino] + ATOM_CHARGE = AMINO_ACID_CHARGE[amino] + ATOM_CATEGORY = AMAs[amino] * 20 + + for i in res: + if i.id not in ATOMS.keys(): + continue + x, y, z = i.get_coord() + if abs(x) > 32: + x = clamp(x, -31, 31) + if abs(y) > 32: + y = clamp(x, -31, 31) + if abs(z) > 32: + z = clamp(x, -31, 31) + x_i, y_i, z_i = int(x) + 32, int(y) + 32, int(z) + 32 + ATOM_WEIGHT = ATOMS[i.id] + ATOM_R = ATOMS_R[i.id] + + if ATOM_R <= 1.5: + voxel[0, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WEIGHT + voxel[1, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WATER + voxel[2, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CHARGE + voxel[3, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CATEGORY + else: + voxel[0, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WEIGHT + voxel[1, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WATER + voxel[2, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CHARGE + voxel[3, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CATEGORY + return voxel + + +def read_scoring_functions(pdb): + scoring = False + profile = [] + for line in open(pdb): + if line.startswith("#END_POSE_ENERGIES_TABLE"): + scoring = False + if scoring: + data = [float(v) for v in line.split()[1:]] + profile.append(data) + if line.startswith("pose"): + scoring = True + return profile + + +def load_aa_features(feature_path): + aa_features = {} + for line in open(feature_path): + line = line.strip().split() + aa, features = line[0], [float(feature) for feature in line[1:]] + aa_features[aa] = features + return aa_features + + +class PairData(Data): + def __init__(self, edge_index_s, x_s): + super(PairData, self).__init__() + self.edge_index_s = edge_index_s + self.x_s = x_s + + def __inc__(self, key, value, *args): + if key == 'edge_index_s': + return self.x_s.size(0) + if key == 'wide_nodes': + return self.x_s.num_nodes + else: + return super().__inc__(key, value, *args) + + +def calculate_property(seq): + analysed_seq = ProteinAnalysis(seq) + aa_counts = analysed_seq.count_amino_acids() + aliphatic_index = ((aa_counts['A'] + 2.9 * aa_counts['V'] + 3.9 * (aa_counts['I'] + aa_counts['L'])) / len(seq)) + + positive_charged_amino_acids = ['R', 'K', 'H'] + negative_charged_amino_acids = ['D', 'E'] + + total_positive_charged = sum(aa_counts.get(aa, 0) for aa in positive_charged_amino_acids) + total_negative_charged = sum(aa_counts.get(aa, 0) for aa in negative_charged_amino_acids) + + total_charge = total_positive_charged - total_negative_charged + charge_density = total_charge / len(seq) + alpha_helix, beta_helix, turn_helix = analysed_seq.secondary_structure_fraction() + + return list( + [round(analysed_seq.gravy(), 3) * 10, round(aliphatic_index, 3) * 10, round(analysed_seq.aromaticity(), 3) * 10, + round(analysed_seq.instability_index(), 3), round(alpha_helix * 10, 3), round(beta_helix * 10, 3), + round(turn_helix * 10, 3), round(analysed_seq.charge_at_pH(7), 3), round(analysed_seq.isoelectric_point(), 3), + round(charge_density, 3) * 10]) + + +def construct_graph(structure, seq, gt, pdb_path, threshold, max_length): + # Initialize an empty graph + G = nx.Graph() + + # Check if chain 'A' is present in the structure, return None if not + if 'A' not in structure[0]: + return None + chain = structure[0]['A'] + + # Add nodes to the graph for each amino acid in the chain with their 3D positions + for res in chain: + if is_aa(res.get_resname(), standard=True): + resname = res.get_resname() + alpha_c_atom = res["CA"] + x, y, z = alpha_c_atom.get_coord() # Get 3D coordinates + G.add_node(res.id[1], name=resname, position=(x, y, z)) + + # Add edges between nodes if the distance between alpha carbons is <= 5Å + for i, m in enumerate(G.nodes): + for n in list(G.nodes)[i + 1:]: + distance = chain[m]["CA"] - chain[n]["CA"] + if distance <= 5: + G.add_edge(m, n, weight=5 / distance) + + # Load scoring functions and features + scoring = read_scoring_functions(pdb_path) + # print(pdb_path) + aa_features = load_aa_features('metadata/features.txt') + + # Assign features to nodes in the graph, including the 3D position + for node_id, node_data in G.nodes(data=True): + res = chain[node_id] + aa_feature = aa_features[three_to_one(res.get_resname())] + energy_feature = scoring[node_id - 1] + position_feature = list(node_data['position']) # Convert position tuple to list + node_data['x'] = aa_feature + energy_feature + position_feature + # node_data['x'][20:40]=[0 for i in range(20)] + # node_data['x'][40:43]=[0 for i in range(3)] + + # Convert graph to data format used in ML models + G = nx.convert_node_labels_to_integers(G) + data_wt = from_networkx(G) + data_graph = PairData(data_wt.edge_index, data_wt.x) + data_graph.gt = (gt < threshold).astype(int) + global_properties = calculate_property(seq) + + # Encode the sequence and pad it to a fixed length + seq_emb = np.zeros((max_length, 21), dtype=np.float32) + for pos in range(len(seq)): + seq_emb[pos, AMAs[seq[pos]]] = 1 + + # print(seq_emb) + data_graph.seq = seq_emb + data_graph.global_f = global_properties + return data_graph + + +class MDataset(Dataset): + ''' + x: Features. + y: Targets, if none, do prediction. + ''' + + def __init__(self, threshold=32, mode='train', max_length=50, pdb_src='af', data_ver='0922'): + self.num_classes = 6 + exclude_list = pd.read_csv('../metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() + exclude_filter = False + + p = PDBParser(QUIET=True) + + if mode == 'train': + all_data = pd.read_csv(f'../metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values + exclude_filter = True + elif mode == 'qlx': + all_data = pd.read_csv('../metadata/data_qlx.csv', encoding="unicode_escape").values + elif mode == 'saap': + all_data = pd.read_csv('../metadata/data_saap.csv', encoding="unicode_escape").values + else: + raise NotImplementedError + idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) + + filter_idx_list = [] + seq_new_list = [] + label_list = [] + for idx in range(len(idx_list)): + seq = seq_list[idx].upper().strip() + if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: + continue + if exclude_filter: + if seq in exclude_list: + continue + + filter_idx_list.append(idx) + seq_new_list.append(seq) + label_list.append(labels[idx]) + + if pdb_src == 'af': + pdb_root = '../pdb/pdb_af/' + elif pdb_src == 'hf': + pdb_root = '../pdb/pdb_dbassp/' + else: + raise NotImplementedError + self.data_list = [] + for i in tqdm(range(len(filter_idx_list))): + idx = filter_idx_list[i] + seq = seq_new_list[i] + label = label_list[i] + + # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"): + # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb" + # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"): + # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb" + if os.path.exists(pdb_root + seq + ".pdb"): + pdb_path = pdb_root + seq + ".pdb" + else: + continue + # raise FileNotFoundError + + structure = p.get_structure(idx, pdb_path) + voxel1 = construct_graph(structure, seq, label, pdb_path, threshold, max_length) + self.data_list.append(voxel1) + + def __getitem__(self, idx): + voxel1 = self.data_list[idx] + + return voxel1 + + def __len__(self): + return len(self.data_list) + + +class SDataset(Dataset): + ''' + x: Features. + y: Targets, if none, do prediction. + ''' + + def __init__(self, threshold=32, mode='train', task='0', max_length=50, pdb_src='af', data_ver='0922'): + self.num_classes = 1 + self.task = int(task) + exclude_list = pd.read_csv('metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() + exclude_filter = False + + p = PDBParser(QUIET=True) + + if mode == 'train': + all_data = pd.read_csv(f'metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values + exclude_filter = True + elif mode == 'qlx': + all_data = pd.read_csv('metadata/data_qlx.csv', encoding="unicode_escape").values + elif mode == 'saap': + all_data = pd.read_csv('metadata/data_saap.csv', encoding="unicode_escape").values + else: + raise NotImplementedError + idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) + labels = labels[:, self.task:self.task+1] + + filter_idx_list = [] + seq_new_list = [] + label_list = [] + for idx in range(len(idx_list)): + seq = seq_list[idx].upper().strip() + if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: + continue + if exclude_filter: + if seq in exclude_list: + continue + + filter_idx_list.append(idx) + seq_new_list.append(seq) + label_list.append(labels[idx]) + + if pdb_src == 'af': + pdb_root = './pdb/pdb_af/' + elif pdb_src == 'hf': + pdb_root = './pdb/pdb_dbassp/' + else: + raise NotImplementedError + self.data_list = [] + for i in tqdm(range(len(filter_idx_list))): + idx = filter_idx_list[i] + seq = seq_new_list[i] + label = label_list[i] + + # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"): + # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb" + # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"): + # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb" + if os.path.exists(pdb_root + seq + ".pdb"): + pdb_path = pdb_root + seq + ".pdb" + else: + continue + # raise FileNotFoundError + + structure = p.get_structure(idx, pdb_path) + voxel1 = construct_graph(structure, seq, label, pdb_path, threshold, max_length) + self.data_list.append(voxel1) + + def __getitem__(self, idx): + voxel1 = self.data_list[idx] + + return voxel1 + + def __len__(self): + return len(self.data_list) + diff --git a/t5g/eval.py b/t5g/eval.py new file mode 100644 index 0000000000000000000000000000000000000000..035c4d57f4a93fb51a813ea83410b38e12a80b03 --- /dev/null +++ b/t5g/eval.py @@ -0,0 +1,209 @@ +import argparse +import json +import logging +import os +import time + +from dataset import SDataset, MDataset +from network import MMGraph +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset +from torch_geometric.loader import DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=64, + help="MIC threshold for determine labels (default: 64)") +parser.add_argument('--num-layer', type=int, dest='num_layer', default=2, + help='number of GNN message passing layers (default: 2)') +parser.add_argument('--emb-dim', type=int, dest='emb_dim', default=90, + help='embedding dimensions (default: 200)') +parser.add_argument('--dropout-ratio', type=float, dest='dropout_ratio', default=0.5, + help='dropout ratio (default: 0.5)') +parser.add_argument('--graph-pooling', type=str, dest='graph_pooling', default="attention", + help='graph level pooling (sum, mean, max, attention)') +parser.add_argument('--gnn-type', type=str, dest='gnn_type', default="gatv2", + help='gnn type (gin, gcn, gat, graphsage)') + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=110, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.002, + help='learning rate (default: 0.002)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='micro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='mlce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + weight_dir = "./run/" + args.task_type + '-' + args.task + "-m-" + args.model + '-' + args.loss + str(args.batch_size) + str(args.lr) + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + raise ValueError + + logging.basicConfig(handlers=[ + # logging.FileHandler(filename=os.path.join(weight_dir, "eval.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + # with open(os.path.join(weight_dir, "config.json"), "w") as f: + # f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src) + else: + set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src) + else: + qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + # train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + + # train_loader = DataLoader(train_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=1, follow_batch=['x_s'], shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=1, follow_batch=['x_s'], shuffle=False) + + if args.model == 'mm': + model = MMGraph(num_layer=args.num_layer, input_dim=43, emb_dim=args.emb_dim, out_dim=set_all.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type=args.gnn_type, max_length=args.max_length) + + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + model.load_state_dict(torch.load(weights_path)) + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + + + if args.task_type in ('mlc', 'slc') : + _, ap, f1, acc, auc = train(args, None, model, None, valid_loader, device, None, None) + logging.info(f'ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, None, model, None, qlx_loader, device, None, None) + logging.info(f'QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, None, model, None, saap_loader, device, None, None) + logging.info(f'SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/eval.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/t5g/infer.py b/t5g/infer.py new file mode 100644 index 0000000000000000000000000000000000000000..ad36a8176d809da8df74ccd18da7ebb25744b45e --- /dev/null +++ b/t5g/infer.py @@ -0,0 +1,137 @@ +import argparse +import json +import logging +import os +import time +import pandas as pd + +from dataset import MDataset +from network import FusionGraph +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch_geometric.loader import DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--mode', type=str, default='11', + help="0 for off and 1 for on. First digit for seq, second for globf") +parser.add_argument('--num-layer', type=int, dest='num_layer', default=2, + help='number of GNN message passing layers (default: 2)') +parser.add_argument('--emb-dim', type=int, dest='emb_dim', default=128, + help='embedding dimensions (default: 128)') +parser.add_argument('--dropout-ratio', type=float, dest='dropout_ratio', default=0.3, + help='dropout ratio (default: 0.3)') +parser.add_argument('--graph-pooling', type=str, dest='graph_pooling', default="attention", + help='graph level pooling (sum, mean, max, attention)') +parser.add_argument('--gnn-type', type=str, dest='gnn_type', default="gat", + help='gnn type (gin, gcn, gat, graphsage)') + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--sg', type=str, default='./run/10-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--gd', type=str, default='./run/01-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--sgd', type=str, default='./run/11-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + +model_path = {'LSTM+GAT': args.sg, 'GAT+MLP': args.gd, 'LSTM+GAT+MLP': args.sgd} + + +def main(): + set_seed(args.seed) + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + results = pd.DataFrame() + + logging.info('Loading Test Dataset') + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length) + qlx_loader = DataLoader(qlx_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + + models = {'LSTM+GAT': FusionGraph(num_layer=args.num_layer, input_dim=43, emb_dim=args.emb_dim, out_dim=qlx_set.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type=args.gnn_type, aux_mode='10'), + 'GAT+MLP': FusionGraph(num_layer=args.num_layer, input_dim=43, emb_dim=args.emb_dim, out_dim=qlx_set.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type=args.gnn_type, aux_mode='01'), + 'LSTM+GAT+MLP': FusionGraph(num_layer=args.num_layer, input_dim=43, emb_dim=args.emb_dim, out_dim=qlx_set.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type=args.gnn_type, aux_mode='11')} + + for model_name in model_path.keys(): + model = models[model_name] + pred_all = [] + gt_all = [] + for i in range(1,6): + model.load_state_dict(torch.load(os.path.join(model_path[model_name], f'model_{i}.pth'))) + model.to(device).eval() + with torch.no_grad(): + for data in qlx_loader: + data = data.to(device) + gt_all.append(torch.tensor(data.gt, device=device)) + out = model(data) + pred_all.append(out) + pred_all = torch.nn.functional.sigmoid(torch.cat(pred_all, dim=0)).squeeze().cpu().numpy() + gt_all = torch.cat(gt_all, dim=0).int().squeeze().cpu().numpy() + results[model_name] = pred_all.ravel(order='F') + + results['gt'] = gt_all.ravel(order='F') + + results.to_csv("preds.csv", index=False) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/t5g/loss.py b/t5g/loss.py new file mode 100644 index 0000000000000000000000000000000000000000..d2c1cabdc7bb5df53b8513344372ea557b89aa29 --- /dev/null +++ b/t5g/loss.py @@ -0,0 +1,123 @@ +import torch +from torch import nn +import torch.nn.functional as F +from math import cos, pi, sin +import math +import numpy as np +from scipy.special import lambertw + + +def mixup_criterion(criterion, pred, y_a, y_b, lam, pow=2): + y = lam ** pow * y_a + (1 - lam) ** pow * y_b + return criterion(pred, y) + + +def mixup_data(v, q, a): + '''Returns mixed inputs, pairs of targets, and lambda without organ constraint''' + lam = np.random.beta(1, 1) + + batch_size = v.shape[0] + index = torch.randperm(batch_size) + + mixed_v = lam * v + (1 - lam) * v[index, :] + mixed_q = lam * q + (1 - lam) * q[index, :] + + a_1, a_2 = a, a[index] + return mixed_v, mixed_q, a_1, a_2, lam + + +def linear(epoch, nepoch): + return 1 - epoch / nepoch + + +def convex(epoch, nepoch): + return epoch / (2 - nepoch) + + +def concave(epoch, nepoch): + return 1 - sin((epoch / nepoch) * (pi / 2)) + + +def composite(epoch, nepoch): + return 0.5 * cos((epoch / nepoch) * pi) + 0.5 + + +class LogCoshLoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y_t, y_prime_t): + ey_t = y_t - y_prime_t + return torch.mean(torch.log(torch.cosh(ey_t + 1e-12)))+F.mse_loss(y_t, y_prime_t) + + +class MLCE(nn.Module): + def __init__(self): + super(MLCE, self).__init__() + + def _mlcce(self, y_pred, y_true): + y_pred = (1 - 2 * y_true) * y_pred + y_pred_neg = y_pred - y_true * 1e12 + y_pred_pos = y_pred - (1 - y_true) * 1e12 + zeros = torch.zeros_like(y_pred[..., :1]) + y_pred_neg = torch.cat([y_pred_neg, zeros], dim=-1) + y_pred_pos = torch.cat([y_pred_pos, zeros], dim=-1) + neg_loss = torch.logsumexp(y_pred_neg, dim=-1) + pos_loss = torch.logsumexp(y_pred_pos, dim=-1) + loss = torch.mean(neg_loss + pos_loss) + return loss + + def __call__(self, y_pred, y_true): + return self._mlcce(y_pred, y_true) + + +class SuperLoss(nn.Module): + def __init__(self, C=10, lam=1, batch_size=128): + super(SuperLoss, self).__init__() + self.tau = math.log(C) + self.lam = lam # set to 1 for CIFAR10 and 0.25 for CIFAR100 + self.batch_size = batch_size + + def forward(self, logits, targets): + l_i = F.mse_loss(logits, targets, reduction='none').detach() + sigma = self.sigma(l_i) + loss = (F.mse_loss(logits, targets, reduction='none') - self.tau) * sigma + self.lam * ( + torch.log(sigma) ** 2) + loss = loss.sum() / self.batch_size + return loss + + def sigma(self, l_i): + x = torch.ones(l_i.size()) * (-2 / math.exp(1.)) + x = x.cuda() + y = 0.5 * torch.max(x, (l_i - self.tau) / self.lam) + y = y.cpu().numpy() + sigma = np.exp(-lambertw(y)) + sigma = sigma.real.astype(np.float32) + sigma = torch.from_numpy(sigma).cuda() + return sigma + + +def unbiased_curriculum_loss(out, data, args, epoch, epochs, scheduler='linear'): + losses = [] + scheduler = linear if scheduler == 'linear' else concave + + # calculate difficulty measurement function + adjusted_losses = [] + for idx in range(out.shape[0]): + ground_truth = max(1, abs(data[idx].item())) + loss = F.mse_loss(out[idx], data[idx]) + losses.append(loss) + adjusted_losses.append(loss.item() / ground_truth) + + mean_loss, std_loss = np.mean(adjusted_losses), np.std(adjusted_losses) + + # re-weight losses + total_loss = 0 + for i, loss in enumerate(losses): + if adjusted_losses[i] > mean_loss + 1 * std_loss: + schedule_factor = scheduler(epoch, args.epochs) + total_loss += schedule_factor * loss + else: + total_loss += loss + + return total_loss diff --git a/t5g/main.py b/t5g/main.py new file mode 100644 index 0000000000000000000000000000000000000000..2137491c26a8dd6d5462ff29969c718172c60dd2 --- /dev/null +++ b/t5g/main.py @@ -0,0 +1,244 @@ +import argparse +import json +import logging +import os +import time + +from dataset import SDataset, MDataset +from network import FusionGraph +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset +from torch_geometric.loader import DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--mode', type=str, default='11', + help="0 for off and 1 for on. First digit for seq, second for globf") +parser.add_argument('--num-layer', type=int, dest='num_layer', default=2, + help='number of GNN message passing layers (default: 2)') +parser.add_argument('--emb-dim', type=int, dest='emb_dim', default=128, + help='embedding dimensions (default: 128)') +parser.add_argument('--dropout-ratio', type=float, dest='dropout_ratio', default=0.3, + help='dropout ratio (default: 0.3)') +parser.add_argument('--graph-pooling', type=str, dest='graph_pooling', default="attention", + help='graph level pooling (sum, mean, max, attention)') +parser.add_argument('--gnn-type', type=str, dest='gnn_type', default="gat", + help='gnn type (gin, gcn, gat, graphsage)') + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + if args.loss == 'mlce' and args.task_type != 'slc': + criterion = MLCE() + elif args.loss == "bce" or args.task_type == 'slc': + args.loss = "bce" + criterion = nn.BCEWithLogitsLoss() + else: + criterion = 0 + pass + weight_dir = "./run/" + args.mode + '-' + args.loss + str(args.batch_size) + '-' + str(args.lr) + '-' + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + os.makedirs(weight_dir) + + logging.basicConfig(handlers=[ + logging.FileHandler(filename=os.path.join(weight_dir, "training.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + with open(os.path.join(weight_dir, "config.json"), "w") as f: + f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src, data_ver=args.data_ver) + else: + set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src, data_ver=args.data_ver) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src) + else: + qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + # if args.loss == 'bce': + # weight = torch.Tensor([0] * args.classes) + # for i in train_set: + # weight += i[2] + # weight = (len(train_set) - weight) / weight + # criterion.register_buffer('pos_weight', weight.to(device)) + + train_loader = DataLoader(train_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + + if args.model == 'mm': + model = FusionGraph(num_layer=args.num_layer, input_dim=43, emb_dim=args.emb_dim, out_dim=set_all.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type=args.gnn_type, aux_mode=args.mode) + + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + # optimizer = torch.optim.Adam(model.parameters()) + optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr) + # optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=True, weight_decay=5e-5) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Train set:{len(train_set)}, Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + best_metric = 0 + best_qlx = 0 + best_saap = 0 + start_time = time.time() + + for epoch in range(1, args.epochs + 1): + if args.task_type in ('mlc', 'slc') : + train_loss, ap, f1, acc, auc = train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer) + logging.info(f'Epoch: {epoch:03d}, Train Loss: {train_loss:.3f}, ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + avg_metric = ap + f1 + acc + auc + if avg_metric > best_metric: + logging.info(f'Epoch: {epoch:03d} New Best validation metrics, running test session') + torch.save(model.state_dict(), weights_path) + best_metric = avg_metric + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, epoch, model, None, qlx_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_metric = qlx_ap + qlx_f1 + qlx_acc + qlx_auc + if qlx_metric > best_qlx: + best_qlx = qlx_metric + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, epoch, model, None, saap_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_metric = saap_ap + saap_f1 + saap_acc + saap_auc + if saap_metric > best_saap: + best_saap = saap_metric + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'used time {(time.time()-start_time)/3600:.2f}h') + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/result.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/t5g/network.py b/t5g/network.py new file mode 100644 index 0000000000000000000000000000000000000000..d5f563738df4af79c7318c8083f51f95e0886daf --- /dev/null +++ b/t5g/network.py @@ -0,0 +1,419 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F +import numpy as np +from torch.nn import Sequential, Linear, ReLU +from torch_geometric.nn import GraphConv, GINConv, GATConv, SAGEConv, GPSConv, GINEConv, GATv2Conv +from torch_geometric.nn import global_mean_pool, global_add_pool, global_max_pool, GlobalAttention, Set2Set, MulAggregation +from torch_geometric.nn import aggr +from torch_geometric.nn import GraphNorm + + +import torch +import torch.nn as nn +import torch.nn.functional as F + +class SimpleSelfAttention(nn.Module): + def __init__(self, embedding_dim, num_heads=1): + super(SimpleSelfAttention, self).__init__() + self.embedding_dim = embedding_dim + self.num_heads = num_heads + + # Assuming key_channels = value_channels = embedding_dim + self.key_channels = self.embedding_dim + self.value_channels = self.embedding_dim + + # Linear layers for queries, keys, and values + self.query = nn.Linear(embedding_dim, self.key_channels * num_heads) + self.key = nn.Linear(embedding_dim, self.key_channels * num_heads) + self.value = nn.Linear(embedding_dim, self.value_channels * num_heads) + + # Output projection layer + self.proj = nn.Linear(self.value_channels * num_heads, embedding_dim) + + # Scaling for dot-product attention + self.scale = nn.Parameter(torch.sqrt(torch.FloatTensor([self.key_channels // num_heads]))) + + def forward(self, x1, x2, x3): + # x1, x2, x3 shapes: [Batch_size, Embedding_dim] + # Stack the inputs along a new dimension (sequence dimension) + batch_size = x1.shape[0] + x = torch.stack((x1, x2, x3), dim=1) # [Batch_size, 3, Embedding_dim] + + # Compute queries, keys, values for all three inputs + Q = self.query(x) # [Batch_size, 3, num_heads * embedding_dim] + K = self.key(x) # [Batch_size, 3, num_heads * embedding_dim] + V = self.value(x) # [Batch_size, 3, num_heads * embedding_dim] + + # Reshape for multi-head attention + Q = Q.view(batch_size, -1, self.num_heads, self.embedding_dim).transpose(1, 2) # [Batch_size, num_heads, 3, embedding_dim] + K = K.view(batch_size, -1, self.num_heads, self.embedding_dim).transpose(1, 2) # [Batch_size, num_heads, 3, embedding_dim] + V = V.view(batch_size, -1, self.num_heads, self.embedding_dim).transpose(1, 2) # [Batch_size, num_heads, 3, embedding_dim] + + # Calculate dot product attention + attention_scores = torch.matmul(Q, K.transpose(-2, -1)) / self.scale + attention = F.softmax(attention_scores, dim=-1) + + # Apply attention to V + x = torch.matmul(attention, V) # [Batch_size, num_heads, 3, embedding_dim] + + # Concatenate heads and put through final linear layer + x = x.transpose(1, 2).reshape(batch_size, -1, self.num_heads * self.embedding_dim) + x = self.proj(x) # [Batch_size, 3, embedding_dim] + + # Sum the outputs from the three inputs + out = x.sum(dim=1) # [Batch_size, embedding_dim] + return out + +def cosine_similarity(x,y): + num = x.dot(y.T) + denom = np.linalg.norm(x) * np.linalg.norm(y) + return num / denom + +class LogCoshLoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y_t, y_prime_t): + ey_t = y_t - y_prime_t + return torch.mean(torch.log(torch.cosh(ey_t + 1e-12))) + + +class WeightedMSELoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y, y_t, weights=None): + loss = (y - y_t) ** 2 + if weights is not None: + loss *= weights.expand_as(loss) + return torch.mean(loss) + + +class GNN(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, JK="last", drop_ratio=0, gnn_type="gin"): + super(GNN, self).__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + # self.fc2 = nn.Linear(200, 200) + self.gnns = torch.nn.ModuleList() + for layer in range(num_layer): + in_dim = input_dim if layer == 0 else emb_dim + if gnn_type == "gin": + # self.gnns.append(GINConv(nn.Sequential(nn.Linear(in_dim, emb_dim), nn.BatchNorm1d(emb_dim), nn.ReLU(), + # nn.Linear(emb_dim, emb_dim)))) + self.gnns.append(GINConv(nn.Sequential(nn.Linear(in_dim, emb_dim), GraphNorm(emb_dim), nn.ReLU(), + nn.Linear(emb_dim, emb_dim), nn.ReLU()))) + elif gnn_type == "gps": + nn_ = Sequential( + Linear(in_dim, emb_dim), + ReLU(), + Linear(emb_dim, emb_dim), + ) + conv = GPSConv(emb_dim, GINEConv(nn_), heads=4) + self.gnns.append(conv) + elif gnn_type == "gcn": + self.gnns.append(GraphConv(in_dim, emb_dim)) + elif gnn_type == "gat": + self.gnns.append(GATConv(in_dim, emb_dim)) + elif gnn_type == "gatv2": + self.gnns.append(GATv2Conv(in_dim, emb_dim)) + elif gnn_type == "graphsage": + self.gnns.append(SAGEConv(in_dim, emb_dim)) + else: + raise ValueError("Invalid GNN type.") + + def forward(self, x, edge_index, edge_attr=None): + h_list = [x] + mut_site = [] + for layer in range(self.num_layer): + + h = self.gnns[layer](h_list[layer], edge_index, edge_attr) + # if layer == self.num_layer - 1: + # # remove relu from the last layer + # h = F.dropout(h, self.drop_ratio, training=self.training) + # else: + # h = F.dropout(F.relu(h), self.drop_ratio, training=self.training) # F.relu() + h_list.append(h) + # if len(h_list) == 2: + # previous_mut_site_feature = h_list[-2][mut_res_idx] + # current_mut_site_feature = h_list[-1][mut_res_idx] + # # print(previous_mut_site_feature.shape, current_mut_site_feature.shape) + # h_feature = self.global_encoder(previous_mut_site_feature) + # h_list[-1][mut_res_idx] = h_feature + current_mut_site_feature + # if len(h_list) == 3: + # previous_mut_site_feature = h_list[-2][mut_res_idx].squeeze(0) + # current_mut_site_feature = h_list[-1][mut_res_idx].squeeze(0) + + # h_feature = self.fc2(previous_mut_site_feature) + current_mut_site_feature + # h_list[-1][mut_res_idx] = h_feature.unsqueeze(0) + # mut_site.append() + # print(len(h_list)) + if self.JK == "last": + node_representation = h_list[-1] + elif self.JK == "sum": + h_list = [h.unsqueeze_(0) for h in h_list] + node_representation = torch.sum(torch.cat(h_list[1:], dim=0), dim=0) + # print('node_rep', node_representation.shape) + return h_list[-1] + + +# orthogonal initialization +def init_gru_orth(model, gain=1): + model.reset_parameters() + # orthogonal initialization of gru weights + for _, hh, _, _ in model.all_weights: + for i in range(0, hh.size(0), model.hidden_size): + torch.nn.init.orthogonal_(hh[i:i + model.hidden_size], gain=gain) + + +def init_lstm_orth(model, gain=1): + init_gru_orth(model, gain) + + # positive forget gate bias (Jozefowicz es at. 2015) + for _, _, ih_b, hh_b in model.all_weights: + l = len(ih_b) + ih_b[l // 4: l // 2].data.fill_(1.0) + hh_b[l // 4: l // 2].data.fill_(1.0) + + +class MLP(nn.Module): + def __init__(self, input_dim, hidden_dim, output_dim, num_layers, dropout_rate): + super(MLP, self).__init__() + + layers = [] + layers.append(nn.Linear(input_dim, hidden_dim)) + layers.append(nn.ReLU()) + layers.append(nn.Dropout(dropout_rate)) + + for _ in range(num_layers - 1): + layers.append(nn.Linear(hidden_dim, hidden_dim)) + layers.append(nn.ReLU()) + layers.append(nn.Dropout(dropout_rate)) + + layers.append(nn.Linear(hidden_dim, output_dim)) + + self.network = nn.Sequential(*layers) + + def forward(self, x): + return self.network(x) + + +class FusionGraph(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, out_dim, JK="last", drop_ratio=0.5, graph_pooling="attention", + gnn_type="gat", concat_type=None, fds=False, feature_level='both', contrast_curri=False, aux_mode='11') -> object: + super().__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + self.input_dim = input_dim + self.emb_dim = emb_dim + self.out_dim = out_dim + self.concat_type = concat_type + self.feature_level = feature_level + self.contrast_curri = contrast_curri + self.mode = [False, False] + final_dim = emb_dim + + if aux_mode[0] == '1': + final_dim += 128 + self.mode[0] = True + self.q_encoder = nn.LSTM( + input_size=21, + hidden_size=128, + num_layers=2, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + self.q_fc = nn.Linear(256, 128) + + if aux_mode[1] == '1': + final_dim += 128 + self.mode[1] = True + self.g_encoder = MLP(10, 128, 128, 3, 0.3) + + self.fc = nn.Sequential( + nn.Linear(final_dim, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + nn.Linear(self.emb_dim, self.emb_dim // 2), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + nn.Linear(self.emb_dim // 2, self.out_dim)) + + self.gnn = GNN(num_layer, input_dim, emb_dim, JK, drop_ratio, gnn_type=gnn_type) + + if graph_pooling == "sum": + self.pool = global_add_pool + elif graph_pooling == "mean": + self.pool = global_mean_pool + elif graph_pooling == "max": + self.pool = global_max_pool + elif graph_pooling == "mul": + self.pool = MulAggregation() + elif graph_pooling == "attention": + self.pool = GlobalAttention(gate_nn=torch.nn.Linear(emb_dim, 1)) + elif graph_pooling == "set2set": + self.pool = Set2Set(emb_dim, processing_steps=2) + elif graph_pooling == "lstm": + self.pool = aggr.LSTMAggregation(emb_dim, emb_dim) + else: + raise ValueError("Invalid graph pooling type.") + + def forward_once(self, x, edge_index, batch): + node_representation = self.gnn(x, edge_index) + graph_rep = self.pool(node_representation, batch) + return graph_rep + + def forward(self, data): + fusion = [self.forward_once(data.x_s, data.edge_index_s, data.x_s_batch)] + seq, globf = data.seq, data.global_f + device = self.fc[0].bias.device + if self.mode[0]: + seq = torch.tensor(np.asarray(seq, dtype=np.float32), device=device) + fusion.append(self.q_fc(self.q_encoder(seq)[0][:, -1, :])) + if self.mode[1]: + globf = torch.tensor(np.asarray(globf, dtype=np.float32), device=device) + fusion.append(self.g_encoder(globf)) + fusion = torch.cat(fusion, dim=-1) + x = self.fc(fusion) + return x + + + +class MMGraph(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, out_dim, JK="last", drop_ratio=0.5, graph_pooling="attention", + gnn_type="gat", concat_type=None, fds=False, feature_level='both', contrast_curri=False, max_length=50) -> object: + super(MMGraph, self).__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + self.input_dim = input_dim + self.emb_dim = emb_dim + self.out_dim = out_dim + self.concat_type = concat_type + self.feature_level = feature_level + self.contrast_curri = contrast_curri + + self.graph_pool = nn.Linear(self.emb_dim, 1) + + self.fc = nn.Sequential( + nn.Linear(self.emb_dim, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + nn.Linear(self.emb_dim, self.out_dim)) + + if fds: + self.dir = True + else: + self.dir = False + self.global_encoder = nn.Sequential(nn.Linear(10, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio),) + self.seq_encoder = nn.Sequential( + nn.Linear(max_length, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + ) + self.gnn = GNN(num_layer, input_dim, emb_dim, JK, drop_ratio, gnn_type=gnn_type) + + if graph_pooling == "sum": + self.pool = global_add_pool + elif graph_pooling == "mean": + self.pool = global_mean_pool + elif graph_pooling == "max": + self.pool = global_max_pool + elif graph_pooling == "mul": + self.pool = MulAggregation() + elif graph_pooling == "attention": + self.pool = GlobalAttention(gate_nn=torch.nn.Linear(emb_dim, 1)) + elif graph_pooling == "set2set": + self.pool = Set2Set(emb_dim, processing_steps=2) + elif graph_pooling == "lstm": + self.pool = aggr.LSTMAggregation(emb_dim, emb_dim) + else: + raise ValueError("Invalid graph pooling type.") + self.att = SimpleSelfAttention(emb_dim, num_heads=4) + + def forward_once(self, x, edge_index, batch): + node_representation = self.gnn(x, edge_index) + graph_rep = self.pool(node_representation, batch) + return graph_rep + + def forward(self, data): + seq1, global_1 = data.seq, data.global_f + device = self.graph_pool.bias.device + seq1 = torch.tensor(seq1, dtype=torch.float, device=device) + global_1 = torch.tensor(global_1, dtype=torch.float, device=device) + + graph_rep_be = self.forward_once(data.x_s, data.edge_index_s, data.x_s_batch) + seq1_rep_be = self.seq_encoder(seq1) + global1 = self.global_encoder(global_1) + + a1 = self.att(graph_rep_be, seq1_rep_be, global1) + return self.fc(a1) + + + +class PMMGraph(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, out_dim, JK="last", drop_ratio=0.5, graph_pooling="attention", + gnn_type="gat", concat_type=None, fds=False, feature_level='both', contrast_curri=False): + super(PMMGraph, self).__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + self.input_dim = input_dim + self.emb_dim = emb_dim + self.out_dim = out_dim + self.concat_type = concat_type + self.feature_level = feature_level + self.contrast_curri = contrast_curri + + # Define the learnable prompt token + self.prompt_token = nn.Parameter(torch.randn(1, 10)) + + self.graph_pool = nn.Linear(self.emb_dim, 1) + + self.fc = nn.Sequential( + nn.Linear(self.emb_dim + 10, self.emb_dim), # Adjust input size to include the prompt token + nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + nn.Linear(self.emb_dim, self.out_dim)) + + self.dir = fds + self.global_encoder = nn.Sequential(nn.Linear(10, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio)) + self.seq_encoder = nn.Sequential( + nn.Linear(30, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + ) + self.gnn = GNN(num_layer, input_dim, emb_dim, JK, drop_ratio, gnn_type=gnn_type) + + # Initialize pooling based on the specified type + if graph_pooling in ["sum", "mean", "max", "mul", "attention", "set2set", "lstm"]: + pooling_classes = { + "sum": global_add_pool, + "mean": global_mean_pool, + "max": global_max_pool, + "mul": aggr.MulAggregation(), + "attention": GlobalAttention(gate_nn=torch.nn.Linear(emb_dim, 1)), + "set2set": Set2Set(emb_dim, processing_steps=2), + "lstm": aggr.LSTMAggregation(emb_dim, emb_dim) + } + self.pool = pooling_classes[graph_pooling] + else: + raise ValueError("Invalid graph pooling type.") + + self.att = SimpleSelfAttention(emb_dim + 10, num_heads=4) # Adjust for prompt dimension + + def forward_once(self, x, edge_index, batch): + node_representation = self.gnn(x, edge_index) + graph_rep = self.pool(node_representation, batch) + # Concatenate the prompt token + graph_rep = torch.cat([graph_rep, self.prompt_token.expand(graph_rep.size(0), -1)], dim=1) + return graph_rep + + def forward(self, data): + seq1 = torch.tensor(np.array(data.seq, dtype=np.float32)).to(device='cuda') + global_1 = torch.tensor(np.array(data.global_f, dtype=np.float32)).to(device='cuda') + + graph_rep_be = self.forward_once(data.x_s, data.edge_index_s, data.x_s_batch) + seq1_rep_be = self.seq_encoder(seq1) + global1_rep = self.global_encoder(global_1) + # Concatenate the prompt token to other representations as well + seq1_rep_be = torch.cat([seq1_rep_be, self.prompt_token.expand(seq1_rep_be.size(0), -1)], dim=1) + global1_rep = torch.cat([global1_rep, self.prompt_token.expand(global1_rep.size(0), -1)], dim=1) + + # Process combined representations + a1 = self.att(graph_rep_be, seq1_rep_be, global1_rep) + return self.fc(a1) + + diff --git a/t5g/run/01-bce256-0.001-50af/config.json b/t5g/run/01-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..14917c3dc4c5fdcaa44387f22e3aa710cc77a436 --- /dev/null +++ b/t5g/run/01-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "mm", "mode": "01", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gat", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t5g/run/01-bce256-0.001-50af/model_1.pth b/t5g/run/01-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..4ea4e9bc8aa6e68318a4ad7aaa9256d628b4a150 --- /dev/null +++ b/t5g/run/01-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d257bda2a30d19feb95fffd8ef8083762adb58d175f483d8e0648bd1163b76bb +size 469134 diff --git a/t5g/run/01-bce256-0.001-50af/model_2.pth b/t5g/run/01-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..edbc9d7fa974ab575926a025b697bbc4cb527a47 --- /dev/null +++ b/t5g/run/01-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6dd8f38cabad22757bd867046efc6b1162481bf527d5bafc80aef050ce514d20 +size 469134 diff --git a/t5g/run/01-bce256-0.001-50af/model_3.pth b/t5g/run/01-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..053dbc81a2486d7a897241cd8cdf70f9a1ff5e32 --- /dev/null +++ b/t5g/run/01-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:900f073a388098ccb60adf635374717874c11aade312027b0b17930f38fbb672 +size 469134 diff --git a/t5g/run/01-bce256-0.001-50af/model_4.pth b/t5g/run/01-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..614c22227e6f17273663d6b12b745f701eb39ead --- /dev/null +++ b/t5g/run/01-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:58e50130f3877e58361500ff41264744bec6e1fd27747442bb9a23aed8d4a7f0 +size 469134 diff --git a/t5g/run/01-bce256-0.001-50af/model_5.pth b/t5g/run/01-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..8f4492cfd0c7e089fdc9f7a69173da47dbf9d849 --- /dev/null +++ b/t5g/run/01-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ba644d5f8d3de638f5c64285371f375682a7409f1a5f2b6369f3b1f479783881 +size 469134 diff --git a/t5g/run/01-bce256-0.001-50af/result.txt b/t5g/run/01-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..8272fdae3f4f7bcd7a74f1b8f9db8feacd7318a4 --- /dev/null +++ b/t5g/run/01-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8741754293441772,0.8342178940773011,0.7768283605575561,0.8222662687301636 +0.007679694171613026,0.0062844564849160855,0.008973978676371246,0.009671317138452179 +qlx +0.806091058254242,0.7294132709503174,0.6145695328712464,0.8232532382011414 +0.013265987261867817,0.005716569956052685,0.011542526447343632,0.012249895067759024 +saap +0.5778014183044433,0.5145169138908386,0.3893333196640015,0.6601188540458679 +0.03058930776087868,0.0016180942461706271,0.004666662216186523,0.020496326815797408 diff --git a/t5g/run/10-bce256-0.001-50af/config.json b/t5g/run/10-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..752161d851ea752000550b549e79e8c14096353d --- /dev/null +++ b/t5g/run/10-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "mm", "mode": "10", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gat", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t5g/run/10-bce256-0.001-50af/model_1.pth b/t5g/run/10-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..be85ebcff11e69f4e949dc3e4bb57eb8ff28ac0b --- /dev/null +++ b/t5g/run/10-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0319439346b754c7cc8a07842b442374ae94f3530d3a9ccc251b76a815cc9258 +size 2596381 diff --git a/t5g/run/10-bce256-0.001-50af/model_2.pth b/t5g/run/10-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..5b3a1df676919cd87a31899004e4a0e71624ee92 --- /dev/null +++ b/t5g/run/10-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d2ac814af64eb7bbc02cdbcbd963c19d1701c63eedae20e750ab9d5518753604 +size 2596381 diff --git a/t5g/run/10-bce256-0.001-50af/model_3.pth b/t5g/run/10-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..453733664adfcb098d60fc213f48e93b547a953b --- /dev/null +++ b/t5g/run/10-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4d6fd48969a2fa92e3b5d8998a1d41eab3b9e341dc71af5bfa3fadf620a7fb5c +size 2596381 diff --git a/t5g/run/10-bce256-0.001-50af/model_4.pth b/t5g/run/10-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..538081f87401055d46ad631bcb90b4db7f9bdbbb --- /dev/null +++ b/t5g/run/10-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e705669a484483d01a404140c2710bfffa720823165629389921b50d4c157395 +size 2596381 diff --git a/t5g/run/10-bce256-0.001-50af/model_5.pth b/t5g/run/10-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..c55479feab9455940631c82c9d9ea3f7806893e8 --- /dev/null +++ b/t5g/run/10-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:25a4bfd9a4124098c4379d2e9668e2e3ab12066a4e3e9dea8adb35674a3640ed +size 2596381 diff --git a/t5g/run/10-bce256-0.001-50af/result.txt b/t5g/run/10-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..ddabb30b27f3f1594880b4a993c4a55429cdb6a7 --- /dev/null +++ b/t5g/run/10-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8845034837722778,0.8436129808425903,0.791090214252472,0.8363714933395385 +0.011910343952671138,0.009921088792908943,0.014444051841323843,0.01689491911699818 +qlx +0.7931469082832336,0.7344244122505188,0.6245033264160156,0.8210259079933167 +0.01615771227509041,0.008142814193355484,0.01622779621137731,0.020929554256717504 +saap +0.5980411171913147,0.5158505439758301,0.4056666433811188,0.6709235668182373 +0.011096656531704288,0.0029241479939464796,0.015441641095550032,0.024585265664421478 diff --git a/t5g/run/11-bce256-0.001-50af/config.json b/t5g/run/11-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..e06b80347205d6cb6c376b9bbca51ebf6f4edb56 --- /dev/null +++ b/t5g/run/11-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "mm", "mode": "11", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gat", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t5g/run/11-bce256-0.001-50af/model_1.pth b/t5g/run/11-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..feba5a40ede3741f8e7bbd9f57286b971b2d09d8 --- /dev/null +++ b/t5g/run/11-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:55a453afb6e7b9ee5d35110a287b3f365d7fd09eb8d0407e7f6ffcb6149e577f +size 2868613 diff --git a/t5g/run/11-bce256-0.001-50af/model_2.pth b/t5g/run/11-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..128b10f9734e05a4310a20f58697b3a8e7e45397 --- /dev/null +++ b/t5g/run/11-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2ae6a89b32c67e855f7eade52b2843fd9c9ed163ecedcf8800b4eade78ce4302 +size 2868613 diff --git a/t5g/run/11-bce256-0.001-50af/model_3.pth b/t5g/run/11-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..e4227fa5e9062868c1426757a8f263d8ab1a3b72 --- /dev/null +++ b/t5g/run/11-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:68cc25a71f5c5450aca74f8bf8cc0889a822f8d4d10932c493238f861e80e822 +size 2868613 diff --git a/t5g/run/11-bce256-0.001-50af/model_4.pth b/t5g/run/11-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..8fd477d9ce669523544fe239fa4cac457836b97c --- /dev/null +++ b/t5g/run/11-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2da7e63dd0260748d7a9cd534b1544b56a59418609c8c8cdeb53ae04ddb111b4 +size 2868613 diff --git a/t5g/run/11-bce256-0.001-50af/model_5.pth b/t5g/run/11-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..53f26d42767a5e3411f942fbda2793e386a246bc --- /dev/null +++ b/t5g/run/11-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8a2551619a9fa432e9976e510d7705a0209a44cf56b81fc8a50addd93311c1ea +size 2868613 diff --git a/t5g/run/11-bce256-0.001-50af/result.txt b/t5g/run/11-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..f3f70f5b25f52ddd954c9d2b5ffd2167facb5601 --- /dev/null +++ b/t5g/run/11-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8955766201019287,0.844084358215332,0.7925261616706848,0.8488023042678833 +0.005895243964981057,0.006474674522365845,0.007909156992038782,0.007761523202367333 +qlx +0.7797588348388672,0.7356940746307373,0.6384105801582336,0.7946237444877624 +0.01902798705959743,0.014683722457018874,0.042585975184215766,0.023321385572704634 +saap +0.609518039226532,0.5127695441246033,0.38899998664855956,0.6889056205749512 +0.017819971531152213,0.002027400997609232,0.004898974813535444,0.030498995162469365 diff --git a/t5g/runthro.sh b/t5g/runthro.sh new file mode 100644 index 0000000000000000000000000000000000000000..4acbc387cfaf5f2fd98642d372b043d84ac15e23 --- /dev/null +++ b/t5g/runthro.sh @@ -0,0 +1,6 @@ +#!/bin/bash +python main.py --mode 11 +python main.py --mode 01 +python main.py --mode 10 + + diff --git a/t5g/summary.py b/t5g/summary.py new file mode 100644 index 0000000000000000000000000000000000000000..420cc50d5a8330d07342a07c2f5bfc482023480a --- /dev/null +++ b/t5g/summary.py @@ -0,0 +1,59 @@ +import os +import csv +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument('--dir', type=str, default='./run') +parser.add_argument('--output', type=str, default='summary.csv') +args = parser.parse_args() + +# 设置目录路径和输出CSV文件名 +directory_path = args.dir +output_csv = args.output + +# 创建CSV文件并写入数据 +with open(output_csv, mode='w', newline='') as file: + writer = csv.writer(file) + + # 写入第一个子表的标题和表头 + writer.writerow(['QLX']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 6: + means_5_6 = lines[4].strip().split(',') + std_devs_5_6 = lines[5].strip().split(',') + if len(means_5_6) == 4 and len(std_devs_5_6) == 4: + values_5_6 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_5_6, std_devs_5_6)] + writer.writerow([subdirectory] + values_5_6) + + # 加入空行以分隔两个子表 + writer.writerow([]) + + # 写入第二个子表的标题和表头 + writer.writerow(['SAAP']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 再次遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 9: + means_8_9 = lines[7].strip().split(',') + std_devs_8_9 = lines[8].strip().split(',') + if len(means_8_9) == 4 and len(std_devs_8_9) == 4: + values_8_9 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_8_9, std_devs_8_9)] + writer.writerow([subdirectory] + values_8_9) \ No newline at end of file diff --git a/t5g/train.py b/t5g/train.py new file mode 100644 index 0000000000000000000000000000000000000000..de2a4067e435ff9c1ead1fe0d33496f726d6c3ff --- /dev/null +++ b/t5g/train.py @@ -0,0 +1,56 @@ +import torch +from torchmetrics import F1Score, Accuracy, AveragePrecision, AUROC + + +def train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer): + train_loss = 0 + num_labels = model.out_dim + avg = args.metric_avg + if num_labels == 1: + task = 'binary' + else: + task = 'multilabel' + metric_macro_acc = Accuracy(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_f1 = F1Score(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_ap = AveragePrecision(average=avg, task=task, num_labels=num_labels).to(device) + metric_auc = AUROC(average=avg, task=task, num_labels=num_labels).to(device) + + if train_loader is not None: + model.train() + for data in train_loader: + data = data.to(device) + # print(seq_lengths) + out = model(data) + # print(out[0]) + # print(gt[0]) + loss = criterion(out, torch.tensor(data.gt, dtype=torch.float, device=device)) + # loss_0 = criterion(out[0], gt.to(device).float()) + # loss_1 = criterion(out[1], gt.to(device).float()) + # loss_2 = criterion(out[2], gt.to(device).float()) + # loss = loss_0 + loss_1 + loss_2 + loss.backward() + optimizer.step() + optimizer.zero_grad() + train_loss += loss.item() + + model.eval() + preds = [] + gt_list_valid = [] + with torch.no_grad(): + for data in valid_loader: + data = data.to(device) + gt_list_valid.append(torch.tensor(data.gt, device=device)) + out = model(data) + preds.append(out) + + # calculate metrics + preds = torch.nn.functional.sigmoid(torch.cat(preds, dim=0)) + gt_list_valid = torch.cat(gt_list_valid, dim=0).int() + + macro_ap = metric_macro_ap(preds, gt_list_valid).item() + # class_ap = [round(i.item(), 5) for i in metric_class_ap(preds, gt_list_valid)] + macro_auc = metric_auc(preds, gt_list_valid).item() + macro_f1 = metric_macro_f1(preds, gt_list_valid).item() + macro_acc = metric_macro_acc(preds, gt_list_valid).item() + return train_loss, macro_ap, macro_f1, macro_acc, macro_auc + diff --git a/t5g/utils.py b/t5g/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..7fd5de2ae11e3a64de85ddc8c0ce0f777a738a48 --- /dev/null +++ b/t5g/utils.py @@ -0,0 +1,32 @@ +import random +import torch +import numpy as np + + +def set_seed(seed): + random.seed(seed) + np.random.seed(seed) + torch.manual_seed(seed) + torch.cuda.manual_seed(seed) + torch.cuda.manual_seed_all(seed) + torch.backends.cudnn.deterministic = True + torch.backends.cudnn.benchmark = False + + +def load_pretrain_model(net, weights): + net_keys = list(net.state_dict().keys()) + weights_keys = list(weights.keys()) + # assert(len(net_keys) <= len(weights_keys)) + i = 0 + j = 0 + while i < len(net_keys) and j < len(weights_keys): + name_i = net_keys[i] + name_j = weights_keys[j] + if net.state_dict()[name_i].shape == weights[name_j].shape: + net.state_dict()[name_i].copy_(weights[name_j].cpu()) + i += 1 + j += 1 + else: + i += 1 + # print i, len(net_keys), j, len(weights_keys) + return net diff --git a/t6/dataset.py b/t6/dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..37d766edcc19efe74635315b81a0c8b5beebf3cc --- /dev/null +++ b/t6/dataset.py @@ -0,0 +1,293 @@ +import os +import torch +from Bio.PDB import PDBParser, is_aa +from Bio.PDB.Polypeptide import three_to_one +from Bio.SeqUtils.ProtParam import ProteinAnalysis +from torch.utils.data import Dataset +import pandas as pd +import numpy as np +from tqdm import tqdm +from Bio.Align import PairwiseAligner + + +aligner = PairwiseAligner() +# Define the scoring system +aligner.mode = 'global' # Global alignment +aligner.match_score = 1 +aligner.mismatch_score = -1 +aligner.open_gap_score = -0.5 +aligner.extend_gap_score = -0.1 + + +def calculate_similarity(seq_gen, seq_tem): + + # Perform the alignment + score = aligner.score(seq_gen, seq_tem) + return score / len(seq_gen) + + +def parse_ll37_testset(path='metadata/LL37_v0.csv'): + files = open(path, 'r').readlines() + seqs = [] + for file in files: + seqs.append(str(file.strip().split(',')[-1].strip().upper())) + return seqs + + +def gmean(labels): + # Assuming 'labels' is a NumPy array of shape (n, m) + # Calculate the geometric mean across axis 1 + + # Take the product of elements along axis=1 + product = np.prod(labels, axis=1) + + # nth root of product, where n is the number of elements along axis=1 + geometric_means = product ** (1.0 / labels.shape[1]) + return geometric_means + + +def clamp(n, smallest, largest): + return sorted([smallest, n, largest])[1] + + +ATOMS = {'H': 1, 'C': 12, 'N': 14, 'O': 16, 'S': 30} +ATOMS_R = {'H': 1, 'C': 1.5, 'N': 1.5, 'O': 1.5, 'S': 2} +AMINO_ACID_WATER = {'A': 255, 'V': 255, 'P': 255, 'F': 255, 'W': 255, 'I': 255, 'L': 255, 'G': 155, 'M': 155, + 'Y': 55, 'S': 55, 'T': 55, 'C': 55, 'N': 55, 'Q': 55, 'D': 55, 'E': 55, 'K': 55, 'R': 55, 'H': 55} +AMINO_ACID_CHARGE = {'D': 55, 'E': 55, 'A': 155, 'V': 155, 'P': 155, 'F': 155, 'W': 155, 'I': 155, 'L': 155, 'G': 155, + 'M': 155, 'Y': 155, 'S': 155, 'T': 155, 'C': 155, 'N': 155, 'Q': 155, 'K': 255, 'R': 255, 'H': 255} +# AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, +# 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19} +AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, + 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19, 'X': 21} + + +def pdb_parser(structure): + """ + + """ + voxel = np.zeros((4, 64, 64, 64), dtype=np.int8) + id = '' + seq_str = '' + for i in structure[0]: + id = i.id + chain = structure[0][id] + for res in chain: + if is_aa(res.get_resname(), standard=True): + resname = res.get_resname() + amino = three_to_one(resname) + seq_str += str(amino) + ATOM_WATER = AMINO_ACID_WATER[amino] + ATOM_CHARGE = AMINO_ACID_CHARGE[amino] + ATOM_CATEGORY = AMAs[amino] * 20 + + for i in res: + if i.id not in ATOMS.keys(): + continue + x, y, z = i.get_coord() + if abs(x) > 32: + x = clamp(x, -31, 31) + if abs(y) > 32: + y = clamp(x, -31, 31) + if abs(z) > 32: + z = clamp(x, -31, 31) + x_i, y_i, z_i = int(x) + 32, int(y) + 32, int(z) + 32 + ATOM_WEIGHT = ATOMS[i.id] + ATOM_R = ATOMS_R[i.id] + + if ATOM_R <= 1.5: + voxel[0, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WEIGHT + voxel[1, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WATER + voxel[2, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CHARGE + voxel[3, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CATEGORY + else: + voxel[0, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WEIGHT + voxel[1, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WATER + voxel[2, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CHARGE + voxel[3, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CATEGORY + return voxel + + +def calculate_property(seq): + analysed_seq = ProteinAnalysis(seq) + aa_counts = analysed_seq.count_amino_acids() + aliphatic_index = ((aa_counts['A'] + 2.9 * aa_counts['V'] + 3.9 * (aa_counts['I'] + aa_counts['L'])) / len(seq)) + + positive_charged_amino_acids = ['R', 'K', 'H'] + negative_charged_amino_acids = ['D', 'E'] + + total_positive_charged = sum(aa_counts.get(aa, 0) for aa in positive_charged_amino_acids) + total_negative_charged = sum(aa_counts.get(aa, 0) for aa in negative_charged_amino_acids) + + total_charge = total_positive_charged - total_negative_charged + charge_density = total_charge / len(seq) + alpha_helix, beta_helix, turn_helix = analysed_seq.secondary_structure_fraction() + + return list( + [round(analysed_seq.gravy(), 3) * 10, round(aliphatic_index, 3) * 10, round(analysed_seq.aromaticity(), 3) * 10, + round(analysed_seq.instability_index(), 3), round(alpha_helix * 10, 3), round(beta_helix * 10, 3), + round(turn_helix * 10, 3), round(analysed_seq.charge_at_pH(7), 3), round(analysed_seq.isoelectric_point(), 3), + round(charge_density, 3) * 10]) + + +class MDataset(Dataset): + def __init__(self, threshold=32, mode='train', max_length=50, pdb_src='af', data_ver='0922', model_mode='111'): + self.num_classes = 6 + exclude_list = pd.read_csv('../metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() + exclude_filter = False + + p = PDBParser(QUIET=True) + + if mode == 'train': + all_data = pd.read_csv(f'../metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values + exclude_filter = True + elif mode == 'qlx': + all_data = pd.read_csv('../metadata/data_qlx.csv', encoding="unicode_escape").values + elif mode == 'saap': + all_data = pd.read_csv('../metadata/data_saap.csv', encoding="unicode_escape").values + else: + raise NotImplementedError + idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) + labels = (labels < threshold).astype(int) + + filter_idx_list = [] + seq_new_list = [] + label_list = [] + for idx in range(len(idx_list)): + seq = seq_list[idx].upper().strip() + if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: + continue + if exclude_filter: + if seq in exclude_list: + continue + + filter_idx_list.append(idx) + seq_new_list.append(seq) + label_list.append(labels[idx]) + + if pdb_src == 'af': + pdb_root = '../pdb/pdb_af/' + elif pdb_src == 'hf': + pdb_root = '../pdb/pdb_dbassp/' + else: + raise NotImplementedError + + if model_mode[1] == '0': + read_pdb_flag = False + else: + read_pdb_flag = True + + self.data_list = [] + for i in tqdm(range(len(filter_idx_list))): + idx = filter_idx_list[i] + seq = seq_new_list[i] + label = label_list[i] + if read_pdb_flag: + if os.path.exists(pdb_root + seq + ".pdb"): + pdb_path = pdb_root + seq + ".pdb" + else: + print(f'lacking pdb file {seq}') + continue + # raise FileNotFoundError + + structure = p.get_structure(idx, pdb_path) + voxel = pdb_parser(structure) + else: + voxel = 0 + + seq_emb = np.zeros((max_length, 21), dtype=np.float32) + for pos in range(len(seq)): + seq_emb[pos, AMAs[seq[pos]]] = 1 + + globf = calculate_property(seq) + + self.data_list.append((voxel, seq_emb, globf, label)) + + def __getitem__(self, idx): + voxel, seq_emb, globf, gt = self.data_list[idx] + + return torch.Tensor(voxel).float(), torch.Tensor(seq_emb).float(), torch.tensor(globf).float(), torch.Tensor(gt) + + def __len__(self): + return len(self.data_list) + + +# class SDataset(Dataset): +# ''' +# x: Features. +# y: Targets, if none, do prediction. +# ''' + +# def __init__(self, threshold=32, mode='train', task='0', max_length=50, pdb_src='af'): +# self.num_classes = 1 +# self.task = int(task) +# exclude_list = pd.read_csv('metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() +# exclude_filter = False + +# p = PDBParser(QUIET=True) + +# if mode == 'train': +# all_data = pd.read_csv('metadata/data_0922_i.csv', encoding="unicode_escape").values +# exclude_filter = True +# elif mode == 'qlx': +# all_data = pd.read_csv('metadata/data_qlx.csv', encoding="unicode_escape").values +# elif mode == 'saap': +# all_data = pd.read_csv('metadata/data_saap.csv', encoding="unicode_escape").values +# else: +# raise NotImplementedError +# idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) +# labels = (labels[:, self.task:self.task+1] <= threshold).astype(int) + +# filter_idx_list = [] +# seq_new_list = [] +# label_list = [] +# for idx in range(len(idx_list)): +# seq = seq_list[idx].upper().strip() +# if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: +# continue +# if exclude_filter: +# if seq in exclude_list: +# continue + +# filter_idx_list.append(idx) +# seq_new_list.append(seq) +# label_list.append(labels[idx]) + +# if pdb_src == 'af': +# pdb_root = './pdb/pdb_af/' +# elif pdb_src == 'hf': +# pdb_root = './pdb/pdb_dbassp/' +# else: +# raise NotImplementedError +# self.data_list = [] +# for i in tqdm(range(len(filter_idx_list))): +# idx = filter_idx_list[i] +# seq = seq_new_list[i] +# label = label_list[i] + +# # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"): +# # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb" +# # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"): +# # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb" +# if os.path.exists(pdb_root + seq + ".pdb"): +# pdb_path = pdb_root + seq + ".pdb" +# else: +# continue +# # raise FileNotFoundError + +# structure = p.get_structure(idx, pdb_path) +# voxel = pdb_parser(structure) +# seq_emb = [AMAs[char] for char in seq] + [0] * (max_length - len(seq)) +# self.data_list.append((voxel, seq_emb, label)) + +# def __getitem__(self, idx): +# voxel, seq_emb, gt = self.data_list[idx] + +# return torch.Tensor(voxel).float(), torch.Tensor(seq_emb), torch.Tensor(gt) + +# def __len__(self): +# return len(self.data_list) \ No newline at end of file diff --git a/t6/eval.py b/t6/eval.py new file mode 100644 index 0000000000000000000000000000000000000000..18f38bfff4c7b7f9bf2fef9834b96c07a96d947d --- /dev/null +++ b/t6/eval.py @@ -0,0 +1,203 @@ +import argparse +import json +import logging +import os +import time + +from dataset import SDataset, MDataset +from network import MMPeptide, SEQPeptide, VoxPeptide, MMFPeptide, SMPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=64, + help="MIC threshold for determine labels (default: 64)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=128, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.02, + help='learning rate (default: 0.002)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='micro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='mlce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + weight_dir = "./run/" + args.task_type + '-' + args.task + "-m-" + args.model + '-' + args.loss + str(args.batch_size) + str(args.lr) + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + raise ValueError + + logging.basicConfig(handlers=[ + # logging.FileHandler(filename=os.path.join(weight_dir, "eval.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + # with open(os.path.join(weight_dir, "config.json"), "w") as f: + # f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src) + else: + set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src) + else: + qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + # train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + + # train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=1, shuffle=False) + if args.model == 'seq': + model = SEQPeptide(classes=set_all.num_classes, q_encoder='mlp') + elif args.model == 'voxel': + model = VoxPeptide(classes=set_all.num_classes) + elif args.model == 'mm': + # model = MMPeptide(classes=train_set.num_classes, q_encoder='tf', ) # attention='hamburger' + # model = SMPeptide(classes=train_set.num_classes, q_encoder='mlp', max_length=30) # attention='hamburger' + model = MMPeptide(classes=set_all.num_classes, q_encoder='mlp', max_length=args.max_length)# , attention='hamburger' + elif args.model == 'mmf': + model = MMFPeptide(classes=set_all.num_classes, q_encoder='mlp', max_length=args.max_length) # attention='hamburger' + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + model.load_state_dict(torch.load(weights_path)) + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + + + if args.task_type in ('mlc', 'slc') : + _, ap, f1, acc, auc = train(args, None, model, None, valid_loader, device, None, None) + logging.info(f'ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, None, model, None, qlx_loader, device, None, None) + logging.info(f'QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, None, model, None, saap_loader, device, None, None) + logging.info(f'SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/eval.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/t6/gen_simi.py b/t6/gen_simi.py new file mode 100644 index 0000000000000000000000000000000000000000..c350b6cbccbe8e4884ea3bbbfd150eb1a66b348b --- /dev/null +++ b/t6/gen_simi.py @@ -0,0 +1,38 @@ +import pandas as pd +from dataset import calculate_similarity + +# 读取模板 CSV 文件和目标 CSV 文件 +template_file = './metadata/data_test.csv' # 模板CSV文件路径 +target_file = './metadata/data_0920_i.csv' # 目标CSV文件路径 + +template_df = pd.read_csv(template_file) +target_df = pd.read_csv(target_file) + +# 假设我们要使用 'template_column' 列作为模板中的列,'target_column' 列作为目标表格中的列 +template_column = 'Seq' # 替换为你的模板表格列名 +target_column = 'Seq' # 替换为你的目标表格列名 + +# 创建一个空的 DataFrame,用于存储符合条件的行,并复制目标表格的表头 +extracted_rows = pd.DataFrame(columns=target_df.columns) + +# 遍历目标表格的每一行 +for index, target_row in target_df.iterrows(): + target_value = target_row[target_column] + + # 遍历模板表格的每个值 + for template_value in template_df[template_column]: + + # 计算指标 + metric = calculate_similarity(target_value.strip().upper(), template_value.strip().upper()) + + # 如果指标大于0.3,则提取目标表格的该行 + if metric > 0.3: + extracted_rows = pd.concat([extracted_rows, pd.DataFrame([target_row])], ignore_index=True) + # 只要找到一个匹配值,就可以继续检查下一行 + break + +# 将提取的行保存到一个新的CSV文件中,确保表头与目标表格一致 +output_file = './metadata/data_simi.csv' +extracted_rows.to_csv(output_file, index=False) + +print(f"符合条件的行已保存到 {output_file}") \ No newline at end of file diff --git a/t6/infer.py b/t6/infer.py new file mode 100644 index 0000000000000000000000000000000000000000..734f4e7ff614c5d59dc63efa8b83968635fdb4a7 --- /dev/null +++ b/t6/infer.py @@ -0,0 +1,137 @@ +import argparse +import json +import logging +import os +import time +import pandas as pd + +from dataset import MDataset +from network import FusionPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='resnet34', + help='resnet34 resnet50 densenet') +parser.add_argument('--channels', type=int, default=32) +parser.add_argument('--mode', type=str, default='101', + help="0 for off and 1 for on. First digit for seq, second for voxel, third for globf") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--rliv', type=str, default='./run/101-rliv256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--rlsriv', type=str, default='./run/101-rlsriv256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--rlrb', type=str, default='./run/101-rlrb256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--rlcb', type=str, default='./run/101-rlcb256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--mlce', type=str, default='./run/101-mlce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--bce', type=str, default='./run/101-bce256-0.001-50af', + help='path of the pretrain model') +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + +model_path = {'ResampleLoss(Inv)': args.rliv, + 'ResampleLoss(Sqrt inv)': args.rlsriv, + 'ResampleLoss(Rebalance)': args.rlrb, + 'ResampleLoss(CB)': args.rlcb, + 'MLCE': args.mlce, + 'Weighted BCE': args.bce} + + +def main(): + set_seed(args.seed) + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + results = pd.DataFrame() + + logging.info('Loading Test Dataset') + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + + model = FusionPeptide(classes=qlx_set.num_classes, v_encoder=args.model, channels=args.channels, mode='101') + + for model_name in model_path.keys(): + pred_all = [] + gt_all = [] + for i in range(1,6): + model.load_state_dict(torch.load(os.path.join(model_path[model_name], f'model_{i}.pth'))) + model.to(device).eval() + with torch.no_grad(): + for data in qlx_loader: + voxel, seq, globf, gt = data + gt_all.append(gt.to(device)) + out = model((voxel.to(device), seq.to(device), globf.to(device))) + pred_all.append(out) + if model_name == 'ResampleLoss(CB)': + print(pred_all) + pred_all = torch.nn.functional.sigmoid(torch.cat(pred_all, dim=0)).squeeze().cpu().numpy() + if model_name == 'ResampleLoss(CB)': + print(pred_all) + gt_all = torch.cat(gt_all, dim=0).int().squeeze().cpu().numpy() + results[model_name] = pred_all.ravel(order='F') + + results['gt'] = gt_all.ravel(order='F') + + results.to_csv("preds.csv", index=False) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/t6/loss.py b/t6/loss.py new file mode 100644 index 0000000000000000000000000000000000000000..b24e78c50aab3362c2700bdb536c00934185871d --- /dev/null +++ b/t6/loss.py @@ -0,0 +1,438 @@ +import torch +from torch import nn +import torch.nn.functional as F +from math import cos, pi, sin +import math +import numpy as np +from scipy.special import lambertw + + +def mixup_criterion(criterion, pred, y_a, y_b, lam, pow=2): + y = lam ** pow * y_a + (1 - lam) ** pow * y_b + return criterion(pred, y) + + +def mixup_data(v, q, a): + '''Returns mixed inputs, pairs of targets, and lambda without organ constraint''' + lam = np.random.beta(1, 1) + + batch_size = v.shape[0] + index = torch.randperm(batch_size) + + mixed_v = lam * v + (1 - lam) * v[index, :] + mixed_q = lam * q + (1 - lam) * q[index, :] + + a_1, a_2 = a, a[index] + return mixed_v, mixed_q, a_1, a_2, lam + + +def linear(epoch, nepoch): + return 1 - epoch / nepoch + + +def convex(epoch, nepoch): + return epoch / (2 - nepoch) + + +def concave(epoch, nepoch): + return 1 - sin((epoch / nepoch) * (pi / 2)) + + +def composite(epoch, nepoch): + return 0.5 * cos((epoch / nepoch) * pi) + 0.5 + + +class LogCoshLoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y_t, y_prime_t): + ey_t = y_t - y_prime_t + return torch.mean(torch.log(torch.cosh(ey_t + 1e-12)))+F.mse_loss(y_t, y_prime_t) + + +class MLCE(nn.Module): + def __init__(self): + super(MLCE, self).__init__() + + def _mlcce(self, y_pred, y_true): + y_pred = (1 - 2 * y_true) * y_pred + y_pred_neg = y_pred - y_true * 1e12 + y_pred_pos = y_pred - (1 - y_true) * 1e12 + zeros = torch.zeros_like(y_pred[..., :1]) + y_pred_neg = torch.cat([y_pred_neg, zeros], dim=-1) + y_pred_pos = torch.cat([y_pred_pos, zeros], dim=-1) + neg_loss = torch.logsumexp(y_pred_neg, dim=-1) + pos_loss = torch.logsumexp(y_pred_pos, dim=-1) + loss = torch.mean(neg_loss + pos_loss) + return loss + + def __call__(self, y_pred, y_true): + return self._mlcce(y_pred, y_true) + + +class SuperLoss(nn.Module): + def __init__(self, C=10, lam=1, batch_size=128): + super(SuperLoss, self).__init__() + self.tau = math.log(C) + self.lam = lam # set to 1 for CIFAR10 and 0.25 for CIFAR100 + self.batch_size = batch_size + + def forward(self, logits, targets): + l_i = F.mse_loss(logits, targets, reduction='none').detach() + sigma = self.sigma(l_i) + loss = (F.mse_loss(logits, targets, reduction='none') - self.tau) * sigma + self.lam * ( + torch.log(sigma) ** 2) + loss = loss.sum() / self.batch_size + return loss + + def sigma(self, l_i): + x = torch.ones(l_i.size()) * (-2 / math.exp(1.)) + x = x.cuda() + y = 0.5 * torch.max(x, (l_i - self.tau) / self.lam) + y = y.cpu().numpy() + sigma = np.exp(-lambertw(y)) + sigma = sigma.real.astype(np.float32) + sigma = torch.from_numpy(sigma).cuda() + return sigma + + +def unbiased_curriculum_loss(out, data, args, epoch, epochs, scheduler='linear'): + losses = [] + scheduler = linear if scheduler == 'linear' else concave + + # calculate difficulty measurement function + adjusted_losses = [] + for idx in range(out.shape[0]): + ground_truth = max(1, abs(data[idx].item())) + loss = F.mse_loss(out[idx], data[idx]) + losses.append(loss) + adjusted_losses.append(loss.item() / ground_truth) + + mean_loss, std_loss = np.mean(adjusted_losses), np.std(adjusted_losses) + + # re-weight losses + total_loss = 0 + for i, loss in enumerate(losses): + if adjusted_losses[i] > mean_loss + 1 * std_loss: + schedule_factor = scheduler(epoch, args.epochs) + total_loss += schedule_factor * loss + else: + total_loss += loss + + return total_loss + + +def reduce_loss(loss, reduction): + """Reduce loss as specified. + + Args: + loss (Tensor): Elementwise loss tensor. + reduction (str): Options are "none", "mean" and "sum". + + Return: + Tensor: Reduced loss tensor. + """ + # none: 0, elementwise_mean:1, sum: 2 + if reduction == 'mean': + return loss.mean() + elif reduction == 'sum': + return loss.sum() + else: + return loss + + +def weight_reduce_loss(loss, weight=None, reduction='mean', avg_factor=None): + """Apply element-wise weight and reduce loss. + + Args: + loss (Tensor): Element-wise loss. + weight (Tensor): Element-wise weights. + reduction (str): Same as built-in losses of PyTorch. + avg_factor (float): Avarage factor when computing the mean of losses. + + Returns: + Tensor: Processed loss values. + """ + # if weight is specified, apply element-wise weight + if weight is not None: + loss = loss * weight + + # if avg_factor is not specified, just reduce the loss + if avg_factor is None: + loss = reduce_loss(loss, reduction) + else: + # if reduction is mean, then average the loss by avg_factor + if reduction == 'mean': + loss = loss.sum() / avg_factor + # if reduction is 'none', then do nothing, otherwise raise an error + elif reduction != 'none': + raise ValueError('avg_factor can not be used with reduction="sum"') + return loss + + +def _squeeze_binary_labels(label): + if label.size(1) == 1: + squeeze_label = label.view(len(label), -1) + else: + inds = torch.nonzero(label >= 1).squeeze() + squeeze_label = inds[:,-1] + return squeeze_label + + +def cross_entropy(pred, label, weight=None, reduction='mean', avg_factor=None): + # element-wise losses + if label.size(-1) != pred.size(0): + label = _squeeze_binary_labels(label) + + loss = F.cross_entropy(pred, label, reduction='none') + + # apply weights and do the reduction + if weight is not None: + weight = weight.float() + loss = weight_reduce_loss( + loss, weight=weight, reduction=reduction, avg_factor=avg_factor) + + return loss + + +def _expand_binary_labels(labels, label_weights, label_channels): + bin_labels = labels.new_full((labels.size(0), label_channels), 0) + inds = torch.nonzero(labels >= 1).squeeze() + if inds.numel() > 0: + bin_labels[inds, labels[inds] - 1] = 1 + if label_weights is None: + bin_label_weights = None + else: + bin_label_weights = label_weights.view(-1, 1).expand( + label_weights.size(0), label_channels) + return bin_labels, bin_label_weights + + +def binary_cross_entropy(pred, + label, + weight=None, + reduction='mean', + avg_factor=None): + if pred.dim() != label.dim(): + label, weight = _expand_binary_labels(label, weight, pred.size(-1)) + + # weighted element-wise losses + if weight is not None: + weight = weight.float() + + loss = F.binary_cross_entropy_with_logits( + pred, label.float(), weight, reduction='none') + loss = weight_reduce_loss(loss, reduction=reduction, avg_factor=avg_factor) + + return loss + + +def partial_cross_entropy(pred, + label, + weight=None, + reduction='mean', + avg_factor=None): + if pred.dim() != label.dim(): + label, weight = _expand_binary_labels(label, weight, pred.size(-1)) + + # weighted element-wise losses + if weight is not None: + weight = weight.float() + + mask = label == -1 + loss = F.binary_cross_entropy_with_logits( + pred, label.float(), weight, reduction='none') + if mask.sum() > 0: + loss *= (1-mask).float() + avg_factor = (1-mask).float().sum() + + # do the reduction for the weighted loss + loss = weight_reduce_loss(loss, reduction=reduction, avg_factor=avg_factor) + + return loss + + +class ResampleLoss(nn.Module): + + def __init__(self, + class_freq: torch.Tensor, + neg_class_freq: torch.Tensor, + use_sigmoid=True, + reduction='mean', + loss_weight=1.0, + partial=False, + focal=dict( + focal=True, + balance_param=2.0, + gamma=2, + ), + CB_loss=dict( + CB_beta=0.9, + CB_mode='average_w' # 'by_class', 'average_n', 'average_w', 'min_n' + ), + map_param=dict( + alpha=10.0, + beta=0.2, + gamma=0.1 + ), + logit_reg=dict( + neg_scale=5.0, + init_bias=0.1 + ), + reweight_func=None, # None, 'inv', 'sqrt_inv', 'rebalance', 'CB' + weight_norm=None # None, 'by_instance', 'by_batch' + ): + super(ResampleLoss, self).__init__() + + assert (use_sigmoid is True) or (partial is False) + assert class_freq.device == neg_class_freq.device + self.use_sigmoid = use_sigmoid + self.partial = partial + self.loss_weight = loss_weight + self.reduction = reduction + self.device = class_freq.device + if self.use_sigmoid: + if self.partial: + self.cls_criterion = partial_cross_entropy + else: + self.cls_criterion = binary_cross_entropy + else: + self.cls_criterion = cross_entropy + + # reweighting function + self.reweight_func = reweight_func + + # normalization (optional) + self.weight_norm = weight_norm + + # focal loss params + self.focal = focal['focal'] + self.gamma = focal['gamma'] + self.balance_param = focal['balance_param'] + + # mapping function params + self.map_alpha = map_param['alpha'] + self.map_beta = map_param['beta'] + self.map_gamma = map_param['gamma'] + + # CB loss params (optional) + self.CB_beta = CB_loss['CB_beta'] + self.CB_mode = CB_loss['CB_mode'] + + self.class_freq = class_freq.float() + self.neg_class_freq = neg_class_freq.float() + self.num_classes = self.class_freq.shape[0] + self.train_num = self.class_freq[0] + self.neg_class_freq[0] + # regularization params + self.logit_reg = logit_reg + self.neg_scale = logit_reg[ + 'neg_scale'] if 'neg_scale' in logit_reg else 1.0 + init_bias = logit_reg['init_bias'] if 'init_bias' in logit_reg else 0.0 + self.init_bias = - torch.log( + self.train_num / self.class_freq - 1) * init_bias / self.neg_scale + + self.freq_inv = torch.ones(self.class_freq.shape).to(self.device) / self.class_freq + self.propotion_inv = self.train_num / self.class_freq + + # print('\033[1;35m loading from {} | {} | {} | s\033[0;0m'.format(freq_file, reweight_func, logit_reg)) + # print('\033[1;35m rebalance reweighting mapping params: {:.2f} | {:.2f} | {:.2f} \033[0;0m'.format(self.map_alpha, self.map_beta, self.map_gamma)) + + def forward(self, + cls_score, + label, + avg_factor=None, + **kwargs): + + weight = self.reweight_functions(label) + + cls_score, weight = self.logit_reg_functions(label.float(), cls_score, weight) + + if self.focal: + logpt = - self.cls_criterion( + cls_score.clone(), label, weight=None, reduction='none', + avg_factor=avg_factor) + # pt is sigmoid(logit) for pos or sigmoid(-logit) for neg + pt = torch.exp(logpt) + loss = self.cls_criterion( + cls_score, label.float(), weight=weight, reduction='none') + loss = ((1 - pt) ** self.gamma) * loss + loss = self.balance_param * loss + loss = reduce_loss(loss, reduction=self.reduction) + else: + loss = self.cls_criterion(cls_score, label.float(), weight, + reduction=self.reduction) + + loss = self.loss_weight * loss + return loss + + def reweight_functions(self, label): + if self.reweight_func is None: + return None + elif self.reweight_func in ['inv', 'sqrt_inv']: + weight = self.RW_weight(label.float()) + elif self.reweight_func in 'rebalance': + weight = self.rebalance_weight(label.float()) + elif self.reweight_func in 'CB': + weight = self.CB_weight(label.float()) + else: + return None + + if self.weight_norm is not None: + if 'by_instance' in self.weight_norm: + max_by_instance, _ = torch.max(weight, dim=-1, keepdim=True) + weight = weight / max_by_instance + elif 'by_batch' in self.weight_norm: + weight = weight / torch.max(weight) + + return weight + + def logit_reg_functions(self, labels, logits, weight=None): + if not self.logit_reg: + return logits, weight + if 'init_bias' in self.logit_reg: + logits += self.init_bias + if 'neg_scale' in self.logit_reg: + logits = logits * (1 - labels) * self.neg_scale + logits * labels + weight = weight / self.neg_scale * (1 - labels) + weight * labels + return logits, weight + + def rebalance_weight(self, gt_labels): + repeat_rate = torch.sum( gt_labels.float() * self.freq_inv, dim=1, keepdim=True) + pos_weight = self.freq_inv.clone().detach().unsqueeze(0) / repeat_rate + # pos and neg are equally treated + weight = torch.sigmoid(self.map_beta * (pos_weight - self.map_gamma)) + self.map_alpha + return weight + + def CB_weight(self, gt_labels): + if 'by_class' in self.CB_mode: + weight = torch.tensor((1 - self.CB_beta)).to(self.device) / \ + (1 - torch.pow(self.CB_beta, self.class_freq)).to(self.device) + elif 'average_n' in self.CB_mode: + avg_n = torch.sum(gt_labels * self.class_freq, dim=1, keepdim=True) / \ + torch.sum(gt_labels, dim=1, keepdim=True) + weight = torch.tensor((1 - self.CB_beta)).to(self.device) / \ + (1 - torch.pow(self.CB_beta, avg_n)).to(self.device) + elif 'average_w' in self.CB_mode: + weight_ = torch.tensor((1 - self.CB_beta)).to(self.device) / \ + (1 - torch.pow(self.CB_beta, self.class_freq)).to(self.device) + weight = torch.sum(gt_labels * weight_, dim=1, keepdim=True) / \ + torch.sum(gt_labels, dim=1, keepdim=True) + elif 'min_n' in self.CB_mode: + min_n, _ = torch.min(gt_labels * self.class_freq + + (1 - gt_labels) * 100000, dim=1, keepdim=True) + weight = torch.tensor((1 - self.CB_beta)).to(self.device) / \ + (1 - torch.pow(self.CB_beta, min_n)).to(self.device) + else: + raise NameError + return weight + + def RW_weight(self, gt_labels, by_class=True): + if 'sqrt' in self.reweight_func: + weight = torch.sqrt(self.propotion_inv) + else: + weight = self.propotion_inv + if not by_class: + sum_ = torch.sum(weight * gt_labels, dim=1, keepdim=True) + weight = sum_ / torch.sum(gt_labels, dim=1, keepdim=True) + return weight \ No newline at end of file diff --git a/t6/main.py b/t6/main.py new file mode 100644 index 0000000000000000000000000000000000000000..d307e784f194f35e012c19a6ee1a14718566ee35 --- /dev/null +++ b/t6/main.py @@ -0,0 +1,246 @@ +import argparse +import json +import logging +import os +import time + +from dataset import MDataset +from network import FusionPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss, ResampleLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='resnet34', + help='resnet34 resnet50 densenet') +parser.add_argument('--channels', type=int, default=32) +parser.add_argument('--mode', type=str, default='101', + help="0 for off and 1 for on. First digit for seq, second for voxel, third for globf") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (bce, mlce, rliv, rlsriv, rlrb, rlcb)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + weight_dir = "./run/" + args.mode + '-' + args.loss + str(args.batch_size) + '-' + str(args.lr) + '-' + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + os.makedirs(weight_dir) + + logging.basicConfig(handlers=[ + logging.FileHandler(filename=os.path.join(weight_dir, "training.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + with open(os.path.join(weight_dir, "config.json"), "w") as f: + f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src, data_ver=args.data_ver, model_mode=args.mode) + # else: + # set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src, model_mode=args.mode) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src, model_mode=args.mode) + # else: + # qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + # saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + + if args.loss == 'mlce' and args.task_type != 'slc': + criterion = MLCE() + elif args.loss == "bce" or args.task_type == 'slc': + args.loss = "bce" + weight = torch.Tensor([0] * args.classes) + for i in train_set: + weight += i[-1] + weight = (len(train_set) - weight) / weight + criterion = nn.BCEWithLogitsLoss(pos_weight=weight.to(device)) + elif args.loss in ['rliv', 'rlsriv', 'rlrb', 'rlcb']: + freq = torch.Tensor([0] * args.classes) + for i in train_set: + freq += i[-1] + neg_freq = torch.Tensor([len(train_set)] * args.classes) - freq + if args.loss == 'rliv': + rwf = 'inv' + elif args.loss == 'rlsriv': + rwf = 'sqrt_inv' + elif args.loss == 'rlrb': + rwf = 'rebalance' + elif args.loss == 'rlcb': + rwf = 'CB' + else: + rwf = None + criterion = ResampleLoss(class_freq=freq.to(device), neg_class_freq=neg_freq.to(device), + reweight_func=rwf) + else: + raise NotImplementedError + + train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=1, shuffle=False) + + model = FusionPeptide(classes=set_all.num_classes, v_encoder=args.model, channels=args.channels, mode=args.mode) + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + # optimizer = torch.optim.Adam(model.parameters()) + optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr) + # optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=True, weight_decay=5e-5) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Train set:{len(train_set)}, Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + best_metric = 0 + best_qlx = 0 + best_saap = 0 + start_time = time.time() + + for epoch in range(1, args.epochs + 1): + if args.task_type in ('mlc', 'slc') : + train_loss, ap, f1, acc, auc = train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer) + logging.info(f'Epoch: {epoch:03d}, Train Loss: {train_loss:.3f}, ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + avg_metric = ap + f1 + acc + auc + if avg_metric > best_metric: + logging.info(f'Epoch: {epoch:03d} New Best validation metrics, running test session') + torch.save(model.state_dict(), weights_path) + best_metric = avg_metric + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, epoch, model, None, qlx_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_metric = qlx_ap + qlx_f1 + qlx_acc + qlx_auc + if qlx_metric > best_qlx: + best_qlx = qlx_metric + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, epoch, model, None, saap_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_metric = saap_ap + saap_f1 + saap_acc + saap_auc + if saap_metric > best_saap: + best_saap = saap_metric + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'used time {(time.time()-start_time)/3600:.2f}h') + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/result.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/t6/network.py b/t6/network.py new file mode 100644 index 0000000000000000000000000000000000000000..ea43653482c5a06686c9f54e9d42cb4cd1d03e15 --- /dev/null +++ b/t6/network.py @@ -0,0 +1,789 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F +from math import ceil +from mamba_ssm import Mamba + + +class Hamburger(nn.Module): + def __init__(self, inp, oup, reduction=32): + super(Hamburger, self).__init__() + self.pool_h = nn.AdaptiveAvgPool3d((1, None, None)) + self.pool_w = nn.AdaptiveAvgPool3d((None, 1, None)) + self.pool_d = nn.AdaptiveAvgPool3d((None, None, 1)) + + mip = max(8, inp // reduction) + self.conv1 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.conv2 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.conv3 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.gn1 = nn.GroupNorm(8, mip) + self.gn2 = nn.GroupNorm(8, mip) + self.gn3 = nn.GroupNorm(8, mip) + + self.act = nn.LeakyReLU(0.2) + self.conv_h = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + self.conv_w = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + self.conv_d = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + + def forward(self, x): + n, c, h, w, d = x.size() + x_h = self.pool_h(x) + # print(x_h.shape) + x_w = self.pool_w(x).permute(0, 1, 3, 2, 4) + # print(x_w.shape) + x_d = self.pool_d(x).permute(0, 1, 4, 2, 3) + # print(x_d.shape) + y_hwd = torch.cat([x_h, x_w, x_d], dim=2) + # y_hd = torch.cat([x_h, x_d], dim=2) + # y_dw = torch.cat([x_d, x_w], dim=2) + y_hwd = self.conv1(y_hwd) + # y_hd = self.conv2(y_hd) + # y_dw = self.conv3(y_dw) + y_hwd = self.gn1(y_hwd) + # y_hd = self.gn2(y_hd) + # y_dw = self.gn3(y_dw) + y_hwd = self.act(y_hwd) + # y_hd = self.act(y_hd) + # y_dw = self.act(y_dw) + # print(y_hwd.shape) + x_h, x_w, x_d = torch.split(y_hwd, [1, 1, 1], dim=2) + x_w = x_w + x_h = x_h.permute(0, 1, 3, 2, 4) + x_d = x_d.permute(0, 1, 3, 4, 2) + a_h = self.conv_h(x_h).sigmoid() + a_w = self.conv_w(x_w).sigmoid() + a_d = self.conv_d(x_d).sigmoid() + a_hw = a_w * a_h + out = a_hw * a_d + return out + x + + +class BasicBlock3D(nn.Module): + expansion = 1 + + def __init__(self, in_channels, out_channels, stride=1, downsample=None): + super(BasicBlock3D, self).__init__() + self.conv1 = nn.Conv3d(in_channels, out_channels, kernel_size=3, + stride=stride, padding=1, bias=False) + self.bn1 = nn.BatchNorm3d(out_channels) + self.relu = nn.ReLU(inplace=True) + self.conv2 = nn.Conv3d(out_channels, out_channels, kernel_size=3, + stride=1, padding=1, bias=False) + self.bn2 = nn.BatchNorm3d(out_channels) + self.downsample = downsample + self.stride = stride + + def forward(self, x): + identity = 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: + identity = self.downsample(x) + + out += identity + out = self.relu(out) + + return out + +class Bottleneck3D(nn.Module): + expansion = 4 + + def __init__(self, in_channels, out_channels, stride=1, downsample=None): + super(Bottleneck3D, self).__init__() + self.conv1 = nn.Conv3d(in_channels, out_channels, kernel_size=1, bias=False) + self.bn1 = nn.BatchNorm3d(out_channels) + self.conv2 = nn.Conv3d(out_channels, out_channels, kernel_size=3, + stride=stride, padding=1, bias=False) + self.bn2 = nn.BatchNorm3d(out_channels) + self.conv3 = nn.Conv3d(out_channels, out_channels * self.expansion, + kernel_size=1, bias=False) + self.bn3 = nn.BatchNorm3d(out_channels * self.expansion) + self.relu = nn.ReLU(inplace=True) + self.downsample = downsample + self.stride = stride + + def forward(self, x): + identity = 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: + identity = self.downsample(x) + + out += identity + out = self.relu(out) + + return out + +class ResNet3D(nn.Module): + def __init__(self, block, layers, input_channels=4, base_channels=16, feature_dim=512): + super(ResNet3D, self).__init__() + self.in_channels = base_channels + + self.conv1 = nn.Conv3d(input_channels, base_channels, kernel_size=7, + stride=2, padding=3, bias=False) + self.bn1 = nn.BatchNorm3d(base_channels) + self.relu = nn.ReLU(inplace=True) + self.maxpool = nn.MaxPool3d(kernel_size=3, stride=2, padding=1) + + # 每个layer的通道数是基于 base_channels 乘以扩展因子 + self.layer1 = self._make_layer(block, base_channels, layers[0]) + self.layer2 = self._make_layer(block, base_channels * 2, layers[1], stride=2) + self.layer3 = self._make_layer(block, base_channels * 4, layers[2], stride=2) + self.layer4 = self._make_layer(block, base_channels * 8, layers[3], stride=2) + + self.avgpool = nn.AdaptiveAvgPool3d((1, 1, 1)) + self.fc = nn.Linear(base_channels * 8 * block.expansion, feature_dim) + + # 初始化权重 + self._initialize_weights() + + def _make_layer(self, block, out_channels, blocks, stride=1): + downsample = None + if stride != 1 or self.in_channels != out_channels * block.expansion: + downsample = nn.Sequential( + nn.Conv3d(self.in_channels, out_channels * block.expansion, + kernel_size=1, stride=stride, bias=False), + nn.BatchNorm3d(out_channels * block.expansion), + ) + + layers = [] + layers.append(block(self.in_channels, out_channels, stride, downsample)) + self.in_channels = out_channels * block.expansion + for _ in range(1, blocks): + layers.append(block(self.in_channels, out_channels)) + + return nn.Sequential(*layers) + + def _initialize_weights(self): + for m in self.modules(): + if isinstance(m, nn.Conv3d): + nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') + elif isinstance(m, (nn.BatchNorm3d, nn.GroupNorm)): + nn.init.constant_(m.weight, 1) + nn.init.constant_(m.bias, 0) + elif isinstance(m, nn.Linear): + nn.init.normal_(m.weight, 0, 0.01) + nn.init.constant_(m.bias, 0) + + def forward(self, x): + # Input shape: (B, C, D, H, W) + x = self.conv1(x) + x = self.bn1(x) + x = self.relu(x) + x = self.maxpool(x) + + x = self.layer1(x) # -> (B, C, D/4, H/4, W/4) + x = self.layer2(x) # -> (B, 2C, D/8, H/8, W/8) + x = self.layer3(x) # -> (B, 4C, D/16, H/16, W/16) + x = self.layer4(x) # -> (B, 8C, D/32, H/32, W/32) + + x = self.avgpool(x) # -> (B, 8C, 1, 1, 1) + x = torch.flatten(x, 1) # -> (B, 8C) + x = self.fc(x) # -> (B, feature_dim) + + return x + +def ResNet3D34(input_channels=4, base_channels=16, feature_dim=512): + """Constructs a ResNet-34 3D model.""" + return ResNet3D(BasicBlock3D, [3, 4, 6, 3], + input_channels=input_channels, + base_channels=base_channels, + feature_dim=feature_dim) + +def ResNet3D50(input_channels=4, base_channels=16, feature_dim=512): + """Constructs a ResNet-50 3D model.""" + return ResNet3D(Bottleneck3D, [3, 4, 6, 3], + input_channels=input_channels, + base_channels=base_channels, + feature_dim=feature_dim) + + +class DenseLayer3D(nn.Module): + """DenseNet3D 的基本层,包括批归一化、激活和卷积操作""" + def __init__(self, in_channels, growth_rate, bn_size=4, drop_rate=0.0): + super(DenseLayer3D, self).__init__() + self.bn1 = nn.BatchNorm3d(in_channels) + self.relu = nn.ReLU(inplace=True) + self.conv1 = nn.Conv3d(in_channels, bn_size * growth_rate, kernel_size=1, stride=1, bias=False) + + self.bn2 = nn.BatchNorm3d(bn_size * growth_rate) + self.conv2 = nn.Conv3d(bn_size * growth_rate, growth_rate, kernel_size=3, stride=1, padding=1, bias=False) + + self.drop_rate = drop_rate + + def forward(self, x): + out = self.bn1(x) + out = self.relu(out) + out = self.conv1(out) + + out = self.bn2(out) + out = self.relu(out) + out = self.conv2(out) + + if self.drop_rate > 0: + out = F.dropout3d(out, p=self.drop_rate, training=self.training) + + # 将输入和输出在通道维度上拼接 + out = torch.cat([x, out], 1) + return out + + +class DenseBlock3D(nn.Module): + """由多个 DenseLayer3D 组成的 DenseBlock""" + def __init__(self, num_layers, in_channels, growth_rate, bn_size=4, drop_rate=0.0): + super(DenseBlock3D, self).__init__() + layers = [] + for i in range(num_layers): + layers.append(DenseLayer3D( + in_channels + i * growth_rate, + growth_rate, + bn_size=bn_size, + drop_rate=drop_rate + )) + self.layer = nn.Sequential(*layers) + + def forward(self, x): + return self.layer(x) + + +class Transition3D(nn.Module): + """用于减少特征图的尺寸和通道数的过渡层""" + def __init__(self, in_channels, out_channels): + super(Transition3D, self).__init__() + self.bn = nn.BatchNorm3d(in_channels) + self.relu = nn.ReLU(inplace=True) + self.conv = nn.Conv3d(in_channels, out_channels, kernel_size=1, stride=1, bias=False) + self.pool = nn.AvgPool3d(kernel_size=2, stride=2) + + def forward(self, x): + out = self.bn(x) + out = self.relu(out) + out = self.conv(out) + out = self.pool(out) + return out + + +class DenseNet3D(nn.Module): + """DenseNet3D 模型""" + def __init__(self, input_channels=1, base_channels=64, growth_rate=32, block_layers=[3, 6, 12, 8], + bn_size=4, drop_rate=0.0, feature_dim=1024): + super(DenseNet3D, self).__init__() + self.growth_rate = growth_rate + + # 初始卷积层 + self.features = nn.Sequential( + nn.Conv3d(input_channels, base_channels, kernel_size=7, stride=2, padding=3, bias=False), + nn.BatchNorm3d(base_channels), + nn.ReLU(inplace=True), + nn.MaxPool3d(kernel_size=3, stride=2, padding=1) + ) + + # Dense Blocks 和 Transition Layers + num_features = base_channels + self.block_layers = [] + self.num_blocks = len(block_layers) + self.dense_blocks = nn.ModuleList() + self.trans_blocks = nn.ModuleList() + + for i, num_layers in enumerate(block_layers): + dense_block = DenseBlock3D( + num_layers=num_layers, + in_channels=num_features, + growth_rate=growth_rate, + bn_size=bn_size, + drop_rate=drop_rate + ) + self.dense_blocks.append(dense_block) + num_features = num_features + num_layers * growth_rate + + if i != self.num_blocks - 1: + trans_block = Transition3D( + in_channels=num_features, + out_channels=num_features // 2 + ) + self.trans_blocks.append(trans_block) + num_features = num_features // 2 + + # 最后一个 batch norm + self.bn_final = nn.BatchNorm3d(num_features) + self.relu_final = nn.ReLU(inplace=True) + + # 全局平均池化 + self.global_pool = nn.AdaptiveAvgPool3d((1, 1, 1)) + + # 分类头 + self.classifier = nn.Sequential( + nn.Flatten(), + nn.Linear(num_features, feature_dim) + ) + + # 初始化权重 + self._initialize_weights() + + def forward(self, x): + out = self.features(x) + + for i in range(self.num_blocks): + out = self.dense_blocks[i](out) + if i < self.num_blocks - 1: + out = self.trans_blocks[i](out) + + out = self.bn_final(out) + out = self.relu_final(out) + + out = self.global_pool(out) # (B, C, 1, 1, 1) + out = out.view(out.size(0), -1) # (B, C) + out = self.classifier(out) # (B, feature_dim) + return out + + def _initialize_weights(self): + """初始化权重""" + for m in self.modules(): + if isinstance(m, nn.Conv3d): + nn.init.kaiming_normal_(m.weight) + elif isinstance(m, nn.BatchNorm3d): + nn.init.constant_(m.weight, 1) + nn.init.constant_(m.bias, 0) + elif isinstance(m, nn.Linear): + nn.init.normal_(m.weight, 0, 0.01) + nn.init.constant_(m.bias, 0) + + + + +class FusionPeptide(nn.Module): + def __init__(self, v_encoder='resnet34', q_encoder='lstm', g_encoder='mlp', mode='111', classes=6, channels=16): + super().__init__() + if mode == '000': + raise KeyError('None of the module acitvated') + self.classes = classes + self.mode = [False, False, False] + final_dim = 0 + + if mode[0] == '1': + final_dim += 128 + self.mode[0] = True + if q_encoder == 'lstm': + self.q_encoder = nn.LSTM( + input_size=21, + hidden_size=128, + num_layers=2, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + self.q_fc = nn.Linear(256, 128) + else: + raise NotImplementedError + + if mode[1] == '1': + final_dim += 512 + self.mode[1] = True + if v_encoder == 'resnet34': + self.v_encoder = ResNet3D34(input_channels=4, base_channels=channels, feature_dim=512) + elif v_encoder == 'resnet50': + self.v_encoder = ResNet3D50(input_channels=4, base_channels=channels, feature_dim=512) + elif v_encoder == 'densenet': + self.v_encoder = DenseNet3D(input_channels=4, base_channels=channels, growth_rate=16 if channels < 48 else 32, feature_dim=512) + else: + raise NotImplementedError(f'\'{v_encoder}\' not implemented') + + if mode[2] == '1': + final_dim += 128 + self.mode[2] = True + if g_encoder == 'mlp': + self.g_encoder = MLP(10, 128, 128, 3, 0.3) + else: + raise NotImplementedError + + self.fc = nn.Sequential( + nn.Linear(final_dim, 128), nn.LeakyReLU(0.1), nn.Dropout(0.3), + nn.Linear(128, 64), nn.LeakyReLU(0.1), nn.Dropout(0.3), + nn.Linear(64, self.classes)) + + def forward(self, x): + vox, seq, globf = x + fusion = [] + if self.mode[0]: + fusion.append(self.q_fc(self.q_encoder(seq)[0][:, -1, :])) + if self.mode[1]: + fusion.append(self.v_encoder(vox)) + if self.mode[2]: + fusion.append(self.g_encoder(globf)) + fusion = torch.cat(fusion, dim=-1) + pred = self.fc(fusion) + return pred + + +class MLP(nn.Module): + def __init__(self, input_dim, hidden_dim, output_dim, num_layers, dropout_rate): + super(MLP, self).__init__() + + layers = [] + layers.append(nn.Linear(input_dim, hidden_dim)) + layers.append(nn.ReLU()) + layers.append(nn.Dropout(dropout_rate)) + + for _ in range(num_layers - 1): + layers.append(nn.Linear(hidden_dim, hidden_dim)) + layers.append(nn.ReLU()) + layers.append(nn.Dropout(dropout_rate)) + + layers.append(nn.Linear(hidden_dim, output_dim)) + + self.network = nn.Sequential(*layers) + + def forward(self, x): + return self.network(x) + + +class PositionalEncoding(nn.Module): + def __init__(self, d_model, max_len=50): + super(PositionalEncoding, self).__init__() + + pe = torch.zeros(max_len, d_model) # (max_len, d_model) + position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1) # (max_len, 1) + div_term = torch.exp(torch.arange(0, d_model, 2).float() * + (-torch.log(torch.FloatTensor([10000.0])) / d_model)) # (d_model/2,) + pe[:, 0::2] = torch.sin(position * div_term) # 偶数维 + pe[:, 1::2] = torch.cos(position * div_term) # 奇数维 + pe = pe.unsqueeze(0) # (1, max_len, d_model) + self.register_buffer('pe', pe) + + def forward(self, x): + """ + x: (B, N, d_model) + """ + x = x + self.pe[:, :x.size(1), :] + return x + + +class TransformerModel(nn.Module): + def __init__(self, nheads, d_model, num_layers, out_dim, max_length=50): + super(TransformerModel, self).__init__() + + # 嵌入层,将输入从 (B, N) 转换到 (B, N, embed_dim) + self.embedding = nn.Linear(1, d_model) + + # 位置编码 + self.pos_encoder = PositionalEncoding(d_model, max_length) + + # Transformer 编码器层 + encoder_layers = nn.TransformerEncoderLayer(d_model=d_model, + nhead=nheads, + activation='relu') + self.transformer_encoder = nn.TransformerEncoder(encoder_layers, num_layers=num_layers) + + # 全局池化(可以根据任务选择不同的聚合方式) + self.global_pool = nn.AdaptiveAvgPool1d(1) + + # 输出层 + self.fc = nn.Linear(d_model, out_dim) + + def forward(self, src): + """ + src: (B, N) + """ + # 嵌入 + embedded = self.embedding(src.unsqueeze(-1)) # (B, N, embed_dim) + embedded = self.pos_encoder(embedded) # 添加位置编码 + + # 转置以适应 Transformer (N, B, embed_dim) + embedded = embedded.permute(1, 0, 2) + + # Transformer 编码 + transformer_out = self.transformer_encoder(embedded) # (N, B, embed_dim) + + # 转置回 (B, N, embed_dim) + transformer_out = transformer_out.permute(1, 0, 2) + + # 全局池化,将 (B, N, embed_dim) 转换为 (B, embed_dim) + pooled = self.global_pool(transformer_out.permute(0, 2, 1)).squeeze(-1) + + # 输出层 + output = self.fc(pooled) # (B, output_dim) + + return output + + +class MambaModel(nn.Module): + def __init__(self, d_model, out_dim, max_length=30): + super(MambaModel, self).__init__() + self.linear = nn.Linear(in_features=1, out_features=d_model) + self.pos_encoder = PositionalEncoding(d_model, max_length) + self.mamba = Mamba(d_model=d_model) + self.global_pool = nn.AdaptiveAvgPool1d(1) + self.fc = nn.Linear(d_model * 2, out_dim) + + def forward(self, x: torch.Tensor): + x = self.pos_encoder(self.linear(x.unsqueeze(-1))) + y = self.mamba(x) + y_flip = self.mamba(x.flip([-2])).flip([-2]) + y = torch.cat((y, y_flip), dim=-1) + y = self.fc(self.global_pool(y.permute(0, 2, 1)).squeeze(-1)) + return y + + +class SEQ(nn.Module): + def __init__(self, seq_type='mlp', input_dim=21, hidden_dim=128, out_dim=128, num_layers=2, max_length=30): + super(SEQ, self).__init__() + self.seq_type = seq_type + if seq_type == 'rnn': + self.rnn = nn.RNN( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'gru': + self.rnn = nn.GRU( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'lstm': + self.rnn = nn.LSTM( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'tf': + self.transformer = TransformerModel(nheads=4, d_model=hidden_dim, num_layers=2, out_dim=out_dim, max_length=max_length) + elif seq_type == 'mamba': + self.mamba = MambaModel(d_model=hidden_dim, out_dim=out_dim, max_length=max_length) + else: + # nn.Linear(50, 50, bias=False), nn.ReLU(), + self.rnn = nn.Sequential(nn.Linear(max_length, hidden_dim * 4), nn.ReLU(), nn.Linear(hidden_dim * 4, out_dim)) + self.rnn_fc = nn.Sequential( + nn.Linear(hidden_dim * 2, hidden_dim), + nn.ReLU(), + nn.Linear(hidden_dim, out_dim) + ) + + def forward(self, seq): + if self.seq_type == 'mlp': + return self.rnn(seq.squeeze(1)) + elif self.seq_type == 'tf': + return self.transformer(seq) + elif self.seq_type == 'mamba': + return self.mamba(seq) + else: + one_hot_seq = F.one_hot(seq.to(torch.int64), num_classes=21).float() + r_out = self.rnn(one_hot_seq, None)[0] # None represents zero initial hidden state + out = self.rnn_fc(r_out[:, -1, :]) + return out + # def forward(self, x, seq_lengths): + # + + +class SEQPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + self.classes = classes + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + + self.seq_fc = nn.Linear(128, classes) + + def forward(self, x, seq_lengths=None): + vox, seq = x + seq_emb = self.q_encoder(seq) + pred = self.seq_fc(seq_emb) + return pred + + +class ConvNet(nn.Module): + def __init__(self, num_classes: int = 2): + super(ConvNet, self).__init__() + self.conv1 = nn.Conv1d(3, 16, 3, padding=1) + self.conv2 = nn.Conv1d(16, 32, 3, padding=1) + self.fc1 = nn.Linear(32 * 7, 128) + # self.fc2 = nn.Linear(128, num_classes) + + def forward(self, x): + # print(x.shape) + x = x.permute(0, 2, 1) + x = F.relu(self.conv1(x)) + x = F.max_pool1d(x, 2) + x = F.relu(self.conv2(x)) + x = F.max_pool1d(x, 2) + x = x.view(x.shape[0], -1) + return self.fc1(x) + # x = F.relu(self.fc1(x)) + # x = self.fc2(x) + # return x + + +class ConvNet2D(nn.Module): + def __init__(self, num_classes: int = 2): + super(ConvNet2D, self).__init__() + self.conv1 = nn.Conv2d(1, 8, 3, padding=1) + self.conv2 = nn.Conv2d(8, 16, 3, padding=2, stride=2) + self.conv3 = nn.Conv2d(16, 32, 3, padding=2, stride=2) + # self.pool = nn.AdaptiveAvgPool2d(32) + self.fc1 = nn.Linear(32 * 3 * 9, 128) + # self.fc2 = nn.Linear(128, num_classes) + + def forward(self, x): + # print(x.shape) + x = x.unsqueeze(1) + x = F.relu(self.conv1(x)) + # print(x.shape) + x = F.relu(self.conv2(x)) + # print(x.shape) + x = F.relu(self.conv3(x)) + # x = self.pool(x) + # print(x.shape) + x = x.view(x.shape[0], -1) + return self.fc1(x) + + +# convnet = ConvNet() +# print(convnet) +class MMPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + if attention == 'hamburger': + self.attention = Hamburger(2048, 2048) + else: + self.attention = None + # v_encoder could be resnet26 or resnet50 + if v_encoder == 'resnet26': + self.v_encoder = ResNet3D(Bottleneck3D, [1, 2, 4, 1], self.attention) + # self.v_encoder = SwinUNETR(img_size=(64, 64, 64), in_channels=3, out_channels=1) + elif v_encoder == 'resnet50': + self.v_encoder = ResNet3D(Bottleneck3D, [3, 4, 6, 3], self.attention) + else: + raise NotImplementedError + + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + # self.ss_encoder = SEQ(seq_type=q_encoder) + if fusion == 'mlp': + self.fusion = nn.Linear(512 * 4 + 256, 256) + # self.fusion = nn.Linear(192 + 256, classes) + elif fusion == 'att': + self.fusion = nn.Linear(512 * 4 + 256, 256) + else: + raise NotImplementedError + + # self.vox_fc = nn.Linear(2048, classes) + # self.seq_fc = nn.Linear(256, classes) + self.out = nn.Sequential(nn.ReLU(inplace=True), nn.Linear(256, classes)) + self.classes = classes + + def forward(self, x, seq_lengths=None): + vox, seq = x + # print(vox.shape) + # print(seq.shape) + vox_emb = self.v_encoder(vox) + # print(vox_emb.shape) + seq_emb = self.q_encoder(seq, seq_lengths) + # print(seq_emb.shape) + # ss_emb = self.ss_encoder(second_s) + fused_feature = torch.cat((seq_emb, vox_emb), dim=1) + pred = self.fusion(fused_feature) + pred = self.out(pred) + # pred1 = self.vox_fc(vox_emb) + # pred2 = self.seq_fc(seq_emb) + # return pred, fused_feature + return pred + + +class SMPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, hidden_dim=256, max_length=30): + super().__init__() + self.siamese_encoder1 = MMPeptide(v_encoder, q_encoder, fusion, classes, attention, max_length) + # self.siamese_encoder2 = MMPeptide(v_encoder, q_encoder, fusion, classes, attention) + self.fc = nn.Sequential( + nn.Linear(hidden_dim * 2, hidden_dim), + nn.ReLU(), + nn.Linear(hidden_dim, 1) + ) + + def forward(self, x, seq_lengths=None): + f_mutated = self.siamese_encoder1(x[0]) + f_wide_type = self.siamese_encoder1(x[1]) + return self.fc(torch.cat((f_mutated, f_wide_type), dim=1)) + + +class MMFPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + if attention == 'hamburger': + self.attention = Hamburger(2048, 2048) + else: + self.attention = None + # v_encoder could be resnet26 or resnet50 + if v_encoder == 'resnet26': + self.v_encoder = ResNet3D(Bottleneck3D, [1, 2, 4, 1], self.attention) + # self.v_encoder = ResNet3DFusion(Bottleneck, [1, 2, 4, 1], self.attention) + elif v_encoder == 'resnet50': + self.v_encoder = ResNet3D(Bottleneck3D, [3, 4, 6, 3], self.attention) + else: + raise NotImplementedError + + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + + if fusion == 'mlp': + self.fusion = nn.Linear(512 * 4 + 256, classes) + elif fusion == 'att': + self.fusion = nn.Linear(512 * 4 + 256, classes) + else: + raise NotImplementedError + + self.vox_fc = nn.Linear(2048, classes) + self.seq_fc = nn.Linear(256, classes) + + def forward(self, x, seq_lengths=None): + vox, seq = x + # print(vox.shape) + # print(seq.shape) + seq_emb = self.q_encoder(seq, seq_lengths) + + vox_emb = self.v_encoder(vox, seq_emb) + # print(vox_emb.shape) + # print(seq_emb.shape) + fused_feature = torch.cat((seq_emb, vox_emb), dim=1) + pred = self.fusion(fused_feature) + # pred1 = self.vox_fc(vox_emb) + # pred2 = self.seq_fc(seq_emb) + return pred + + +if __name__ == "__main__": + # model = MMFPeptide() + # voxel = torch.zeros((4, 3, 64, 64, 64)) + # # # h_in = torch.zeros((2, 2048, 2, 2, 2)) + # # # h = Hamburger(2048, 2048) + # # # h(h_in) + # seq = torch.ones((4, 50)) + # res = model.forward((voxel, seq)) + # out = model((voxel, seq)) + # print(out.shape) + # model = ConvNet2D() + input_seq = torch.ones((4, 1, 30)) + # model(input_seq) + transformer = TransformerModel(nhead=4, d_model=32, num_layers=2) + print(transformer(input_seq).shape) diff --git a/t6/run/101-bce256-0.001-50af/config.json b/t6/run/101-bce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..97a5db5672a2263c5caab76b704ad06a04519bc9 --- /dev/null +++ b/t6/run/101-bce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "channels": 32, "mode": "101", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t6/run/101-bce256-0.001-50af/model_1.pth b/t6/run/101-bce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..3f272a8f086701eaef2e7a6449c4ce09298cebed --- /dev/null +++ b/t6/run/101-bce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:070b8f43e85016dee7067104d4fb2d4fab980f3cb0221d799e653e8737232f19 +size 2708515 diff --git a/t6/run/101-bce256-0.001-50af/model_2.pth b/t6/run/101-bce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..0593841bbf594817ca98358d41688985f0ba82ca --- /dev/null +++ b/t6/run/101-bce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fb0ffe392a78e76eca384b9a69c7c4f8f0dc30cdea756bff21bb9101071a4f9b +size 2708515 diff --git a/t6/run/101-bce256-0.001-50af/model_3.pth b/t6/run/101-bce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..0396bed42552f75b8d7a0f6a7f34114b35320b6b --- /dev/null +++ b/t6/run/101-bce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7ea7ddb31b904866fee7edd6c491fbe5dcaccc5e4fd3b0ad11b37394e89aa2d3 +size 2708515 diff --git a/t6/run/101-bce256-0.001-50af/model_4.pth b/t6/run/101-bce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..704ebbb018343282c43c6dd128b58054b96ff8de --- /dev/null +++ b/t6/run/101-bce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:85dd0caa930df46b84ffad71c3a83bd7824059118ddbe926a0a8806289876570 +size 2708515 diff --git a/t6/run/101-bce256-0.001-50af/model_5.pth b/t6/run/101-bce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..5cdd119181d47526f23e67c550b140b3f1cc6bfe --- /dev/null +++ b/t6/run/101-bce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c51758fdbfb682ef8436af9b17b54231d905875c168128dc92e9661087adca2d +size 2708515 diff --git a/t6/run/101-bce256-0.001-50af/result.txt b/t6/run/101-bce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..c6418134a9e214a82531fa7c13198e08af309793 --- /dev/null +++ b/t6/run/101-bce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8924478530883789,0.8331842541694641,0.7886228442192078,0.8471815109252929 +0.006666937415073965,0.010765487475696512,0.01084383790776814,0.008275316914460838 +qlx +0.8225226879119873,0.7827728152275085,0.7183222889900207,0.8451924800872803 +0.016713910280147754,0.01644039847760031,0.0325440709906057,0.019778617593680853 +saap +0.557126384973526,0.5157297134399415,0.4069999694824219,0.6623920321464538 +0.04975160104899776,0.000871195488980625,0.007774595112021652,0.05090301632513177 diff --git a/t6/run/101-mlce256-0.001-50af/config.json b/t6/run/101-mlce256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..3f6bc5999fa7a046c8ce2b11d9838b28e4e77f34 --- /dev/null +++ b/t6/run/101-mlce256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "channels": 32, "mode": "101", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "mlce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t6/run/101-mlce256-0.001-50af/model_1.pth b/t6/run/101-mlce256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..4cacd21d684a9c7e269870eb85c85b8ad248df62 --- /dev/null +++ b/t6/run/101-mlce256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:993a46f0a62799b49255dd7bc5232b0b85d4b702b6aa55fce970d9559bfc7774 +size 2708515 diff --git a/t6/run/101-mlce256-0.001-50af/model_2.pth b/t6/run/101-mlce256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..6d9069d8a067fdc14884287cb54830724cec6696 --- /dev/null +++ b/t6/run/101-mlce256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e5b80aa471cae5072c5887061a85a734bff49fdaa29fba4829a0880008364884 +size 2708515 diff --git a/t6/run/101-mlce256-0.001-50af/model_3.pth b/t6/run/101-mlce256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..52f8951f7dbb955e8cf44155eec373d312736da3 --- /dev/null +++ b/t6/run/101-mlce256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e6b8e8f524df06f65b51492e715b1ffa04f2874a0b8f2bb099f13f1f288d1a52 +size 2708515 diff --git a/t6/run/101-mlce256-0.001-50af/model_4.pth b/t6/run/101-mlce256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..00f56338850997a349e509036422d3706f8e3b02 --- /dev/null +++ b/t6/run/101-mlce256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bceacb6f3219fed7875003cb2a98cbeeb294034113c3be5bfc315fb5c6c8988c +size 2708515 diff --git a/t6/run/101-mlce256-0.001-50af/model_5.pth b/t6/run/101-mlce256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..fe857425897be26ccc5c782a82cb8d9d9dd1461c --- /dev/null +++ b/t6/run/101-mlce256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:da25f3a81c0515b38cba33630aed52a6252fcc19705d2e8da58d071652a56dfc +size 2708515 diff --git a/t6/run/101-mlce256-0.001-50af/result.txt b/t6/run/101-mlce256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..d610b243e597bb0f6fe93bbf84f3019eeeddddcd --- /dev/null +++ b/t6/run/101-mlce256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8893942713737488,0.8525071859359741,0.8020460724830627,0.8466682076454163 +0.005528048207651053,0.007963444905475644,0.010735074383564564,0.006900644888377306 +qlx +0.8205710649490356,0.7585392951965332,0.6704194188117981,0.843675720691681 +0.008288704833774547,0.017245648609590367,0.03251560673720748,0.01209687216471389 +saap +0.5326094567775727,0.517480444908142,0.40233330726623534,0.6183711647987366 +0.05740980978544141,0.004162897675341182,0.011035286380752,0.058606051699574946 diff --git a/t6/run/101-rlcb256-0.001-50af/config.json b/t6/run/101-rlcb256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..eaa7cb95e384b1ff1f228553fc6c33ac031e2143 --- /dev/null +++ b/t6/run/101-rlcb256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "channels": 32, "mode": "101", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "rlcb", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t6/run/101-rlcb256-0.001-50af/model_1.pth b/t6/run/101-rlcb256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..dac0c9b3e665bca134de7691ec9f2a1a63d8d6f7 --- /dev/null +++ b/t6/run/101-rlcb256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b89120a42a148da0aa2d382b4061aac66cc385abdfb9ed6c1a149506fc1e0e63 +size 2708515 diff --git a/t6/run/101-rlcb256-0.001-50af/model_2.pth b/t6/run/101-rlcb256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..6911be7e8d032eed9cf8d7394c61046c720e3184 --- /dev/null +++ b/t6/run/101-rlcb256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ebae3a7f8b4fa963e884306cde2089e9bf938ad354ab44360388420eba5523b6 +size 2708515 diff --git a/t6/run/101-rlcb256-0.001-50af/model_3.pth b/t6/run/101-rlcb256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..fc65a0732f869637a17f34631cea7f859ca67b72 --- /dev/null +++ b/t6/run/101-rlcb256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a99c56621f3e6729b929a3dbe798e1e0f16a5dce499e444f92a166847e560e78 +size 2708515 diff --git a/t6/run/101-rlcb256-0.001-50af/model_4.pth b/t6/run/101-rlcb256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..9984d2704f38ef61480db953376e9210ef689950 --- /dev/null +++ b/t6/run/101-rlcb256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bc403cc695bf48e1668384401f2c6dd31a0a9a3f5d4f63300ef0d737d00d83e0 +size 2708515 diff --git a/t6/run/101-rlcb256-0.001-50af/model_5.pth b/t6/run/101-rlcb256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..662e3130d94bab14e47ed381fc4e74841221b2dd --- /dev/null +++ b/t6/run/101-rlcb256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3e8afd5f3c465e49dec83a733f7afd8a44b1107b8b9de3d4d555f4568bca0a57 +size 2708515 diff --git a/t6/run/101-rlcb256-0.001-50af/result.txt b/t6/run/101-rlcb256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..c97a2f0dffea168c69b79a2e465a969c17bf8483 --- /dev/null +++ b/t6/run/101-rlcb256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.9010853171348572,0.0,0.3628562271595001,0.90149827003479 +0.008228189371857264,0.0,0.0053278571243128535,0.009195097903835735 +qlx +0.380754292011261,0.0,0.46357613801956177,0.187087744474411 +0.0,0.0,0.0,0.0 +saap +0.517193615436554,0.0,0.6150000095367432,0.6489061713218689 +0.0,0.0,0.0,0.0 diff --git a/t6/run/101-rliv256-0.001-50af/config.json b/t6/run/101-rliv256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..f497b330164e1064c194c10c850113976442bba7 --- /dev/null +++ b/t6/run/101-rliv256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "channels": 32, "mode": "101", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 1, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "rliv", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t6/run/101-rliv256-0.001-50af/model_1.pth b/t6/run/101-rliv256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..1580753fe38dbe56d2c3a65d8d4f8cddaeca498e --- /dev/null +++ b/t6/run/101-rliv256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7a05c089cdec5e53bc9d1a2e9ba5c9828641aa195c28a07056047414923e6579 +size 2708515 diff --git a/t6/run/101-rliv256-0.001-50af/model_2.pth b/t6/run/101-rliv256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..554785a9ad79fce472eda439afede523034d3101 --- /dev/null +++ b/t6/run/101-rliv256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:36334d6c766be259f4243b9ffe9fae9256cc309858eec69614e5811bdc5a2851 +size 2708515 diff --git a/t6/run/101-rliv256-0.001-50af/model_3.pth b/t6/run/101-rliv256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..9af011f94afc327b2be76e7cd78066bc9533aef6 --- /dev/null +++ b/t6/run/101-rliv256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:273e305382058adeff63448182267d9d774a098887929afa0027f828c8662fd6 +size 2708515 diff --git a/t6/run/101-rliv256-0.001-50af/model_4.pth b/t6/run/101-rliv256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..718a361859bfe1ee9d43492a1d2d866cff579f18 --- /dev/null +++ b/t6/run/101-rliv256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d367b59755d4c4d45ceb3982b4b259b90b2a960657a1c83841557bf56d2635de +size 2708515 diff --git a/t6/run/101-rliv256-0.001-50af/model_5.pth b/t6/run/101-rliv256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..6869d2665c9612ac1715a948b38b4b1bd892a899 --- /dev/null +++ b/t6/run/101-rliv256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:96a22cb5bf703f458acf34ecbccdbc0cfda0dad9ac745f793398b7f27a10bfba +size 2708515 diff --git a/t6/run/101-rliv256-0.001-50af/result.txt b/t6/run/101-rliv256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..5eefbf26181692e603c5394f14baaaae6c697427 --- /dev/null +++ b/t6/run/101-rliv256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8857136130332947,0.8351638674736023,0.7792113780975342,0.8330256938934326 +0.009819695126358361,0.009211025006598443,0.010801355645574634,0.01199865054491772 +qlx +0.8237228631973267,0.7662089586257934,0.6891832232475281,0.8414216279983521 +0.02850882875006806,0.01315879009710246,0.029997743928705894,0.03783853787783661 +saap +0.5466021776199341,0.5143984436988831,0.3943333148956299,0.6350968718528748 +0.042450255762682486,0.002328798719327312,0.007999992370605469,0.04051461922654054 diff --git a/t6/run/101-rlrb256-0.001-50af/config.json b/t6/run/101-rlrb256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..0499294fc775e00fd01606ed5b39262a4f1b3496 --- /dev/null +++ b/t6/run/101-rlrb256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "channels": 32, "mode": "101", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "rlrb", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t6/run/101-rlrb256-0.001-50af/model_1.pth b/t6/run/101-rlrb256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..6edc973a586ce9aed088526e17d01200656793c9 --- /dev/null +++ b/t6/run/101-rlrb256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f46c7cc67374a9ef933d19846301e4e93151f3602791529752d1cdd791149837 +size 2708515 diff --git a/t6/run/101-rlrb256-0.001-50af/model_2.pth b/t6/run/101-rlrb256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..1307d511eb824794e53893d566228d21cdcda504 --- /dev/null +++ b/t6/run/101-rlrb256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:49a94e846024c28674cbe839353958199054bd0e86966ab9fd43cc1b0a54b40c +size 2708515 diff --git a/t6/run/101-rlrb256-0.001-50af/model_3.pth b/t6/run/101-rlrb256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..ac26b56aa4e8487b5a44a0643ddf1321f93373e6 --- /dev/null +++ b/t6/run/101-rlrb256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7af7a4fde021c387df215ef1239af01c7b38ee571dd6718bb115be7edf052c8f +size 2708515 diff --git a/t6/run/101-rlrb256-0.001-50af/model_4.pth b/t6/run/101-rlrb256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..915c104e95cc71267446313ae4425b0b1f1374e2 --- /dev/null +++ b/t6/run/101-rlrb256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4d8fe41d00bb92ed4f967c2d905d4a21cf7f6bf5e10950051e6d9544e5644efe +size 2708515 diff --git a/t6/run/101-rlrb256-0.001-50af/model_5.pth b/t6/run/101-rlrb256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..84829a8ef06ab18eeae89166524ae36dbd46007e --- /dev/null +++ b/t6/run/101-rlrb256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3241ffa3ecaa2a2c0682fae1400feda328ff285c9116ec4759df20851df0de55 +size 2708515 diff --git a/t6/run/101-rlrb256-0.001-50af/result.txt b/t6/run/101-rlrb256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..eba83bd83cb9ef2f91649a17362ffbec99b1e60a --- /dev/null +++ b/t6/run/101-rlrb256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8896070837974548,0.8314609169960022,0.7788344144821167,0.8351320266723633 +0.007270836578790382,0.010500264586859966,0.011904462936328849,0.011116580159559292 +qlx +0.8012989640235901,0.7719115853309632,0.7017660021781922,0.8205034136772156 +0.01650787722914276,0.00802793246664937,0.018275708296735117,0.018562963903326266 +saap +0.5442710518836975,0.5130856394767761,0.3929999828338623,0.6487240195274353 +0.0330208150426919,0.0003595085930231202,0.007483307636902781,0.03042481597552023 diff --git a/t6/run/101-rlsriv256-0.001-50af/config.json b/t6/run/101-rlsriv256-0.001-50af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..78934c23d9687ee87a5f6e835a74b77e9e1883cd --- /dev/null +++ b/t6/run/101-rlsriv256-0.001-50af/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "channels": 32, "mode": "101", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "rlsriv", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t6/run/101-rlsriv256-0.001-50af/model_1.pth b/t6/run/101-rlsriv256-0.001-50af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..1e9b2b87b98024c0e7b7084439ff6e83c063a5b4 --- /dev/null +++ b/t6/run/101-rlsriv256-0.001-50af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b39302ec73361e973156dd63d0f1975dfc8a18ee62a45833f91e2f50dbd970b1 +size 2708515 diff --git a/t6/run/101-rlsriv256-0.001-50af/model_2.pth b/t6/run/101-rlsriv256-0.001-50af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..dab58a54ae6e20ed5a26db81219feb84c968ae62 --- /dev/null +++ b/t6/run/101-rlsriv256-0.001-50af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a27b53a9e164809b8ae6037a5d0122e8c15af17e2cc80b2216bd5d672c4ee349 +size 2708515 diff --git a/t6/run/101-rlsriv256-0.001-50af/model_3.pth b/t6/run/101-rlsriv256-0.001-50af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..7cc01c7b535168033d33ccee22b59add6df490f8 --- /dev/null +++ b/t6/run/101-rlsriv256-0.001-50af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d647ac23c3012c7101ad9b6ff63b26b77ccd4ad7aadc7fd311d7f9d04ad8fb51 +size 2708515 diff --git a/t6/run/101-rlsriv256-0.001-50af/model_4.pth b/t6/run/101-rlsriv256-0.001-50af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..f4582277dd7266740bb02377513d6bb188a97203 --- /dev/null +++ b/t6/run/101-rlsriv256-0.001-50af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ae06067082ffee5d9b66d702809364a058e5b00cbac0d42bc5e092dfed3889a7 +size 2708515 diff --git a/t6/run/101-rlsriv256-0.001-50af/model_5.pth b/t6/run/101-rlsriv256-0.001-50af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..7910a8b6f1693b937d50064de260545823795147 --- /dev/null +++ b/t6/run/101-rlsriv256-0.001-50af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8ce1c74439dd69952bc60d1e0a9722bf795e02061b1546d80945dc61c5c9e84e +size 2708515 diff --git a/t6/run/101-rlsriv256-0.001-50af/result.txt b/t6/run/101-rlsriv256-0.001-50af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..49f748e247dff3d4836a6855c1d9583081c58f7b --- /dev/null +++ b/t6/run/101-rlsriv256-0.001-50af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8902904629707337,0.8342366576194763,0.7795880913734436,0.8370801568031311 +0.004335169840818766,0.008168783591275913,0.011118049733191575,0.009338318022329544 +qlx +0.8029900789260864,0.770765769481659,0.7024282574653625,0.8193352580070495 +0.018880297111294315,0.010560012719386665,0.026050424216672394,0.01563361811274352 +saap +0.5579963266849518,0.5114621043205261,0.39099998474121095,0.6580934643745422 +0.03806098426902161,0.0017224317962831418,0.004898974813535444,0.041959281254819666 diff --git a/t6/runthro.sh b/t6/runthro.sh new file mode 100644 index 0000000000000000000000000000000000000000..d21528e58710cba8761e8673763483767cb72520 --- /dev/null +++ b/t6/runthro.sh @@ -0,0 +1,5 @@ +#!/bin/bash +python main.py --loss bce +python main.py --loss mlce +# python main.py --mode 011 + diff --git a/t6/summary.py b/t6/summary.py new file mode 100644 index 0000000000000000000000000000000000000000..420cc50d5a8330d07342a07c2f5bfc482023480a --- /dev/null +++ b/t6/summary.py @@ -0,0 +1,59 @@ +import os +import csv +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument('--dir', type=str, default='./run') +parser.add_argument('--output', type=str, default='summary.csv') +args = parser.parse_args() + +# 设置目录路径和输出CSV文件名 +directory_path = args.dir +output_csv = args.output + +# 创建CSV文件并写入数据 +with open(output_csv, mode='w', newline='') as file: + writer = csv.writer(file) + + # 写入第一个子表的标题和表头 + writer.writerow(['QLX']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 6: + means_5_6 = lines[4].strip().split(',') + std_devs_5_6 = lines[5].strip().split(',') + if len(means_5_6) == 4 and len(std_devs_5_6) == 4: + values_5_6 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_5_6, std_devs_5_6)] + writer.writerow([subdirectory] + values_5_6) + + # 加入空行以分隔两个子表 + writer.writerow([]) + + # 写入第二个子表的标题和表头 + writer.writerow(['SAAP']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 再次遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 9: + means_8_9 = lines[7].strip().split(',') + std_devs_8_9 = lines[8].strip().split(',') + if len(means_8_9) == 4 and len(std_devs_8_9) == 4: + values_8_9 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_8_9, std_devs_8_9)] + writer.writerow([subdirectory] + values_8_9) \ No newline at end of file diff --git a/t6/train.py b/t6/train.py new file mode 100644 index 0000000000000000000000000000000000000000..efe33506c8ef7ff6027479b9c173daac20175739 --- /dev/null +++ b/t6/train.py @@ -0,0 +1,62 @@ +import torch +from torchmetrics import F1Score, Accuracy, AveragePrecision, AUROC + + +def train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer): + train_loss = 0 + num_labels = model.classes + avg = args.metric_avg + if num_labels == 1: + task = 'binary' + else: + task = 'multilabel' + metric_macro_acc = Accuracy(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_f1 = F1Score(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_ap = AveragePrecision(average=avg, task=task, num_labels=num_labels).to(device) + metric_auc = AUROC(average=avg, task=task, num_labels=num_labels).to(device) + + if train_loader is not None: + model.train() + for data in train_loader: + voxel, seq, globf, gt = data + # print(seq_lengths) + out = model((voxel.to(device), seq.to(device), globf.to(device))) + # print(out[0]) + # print(gt[0]) + loss = criterion(out, gt.to(device).float()) + # loss_0 = criterion(out[0], gt.to(device).float()) + # loss_1 = criterion(out[1], gt.to(device).float()) + # loss_2 = criterion(out[2], gt.to(device).float()) + # loss = loss_0 + loss_1 + loss_2 + # print(loss) + loss.backward() + optimizer.step() + optimizer.zero_grad() + train_loss += loss.item() + + model.eval() + preds = [] + gt_list_valid = [] + with torch.no_grad(): + for data in valid_loader: + voxel, seq, globf, gt = data + gt_list_valid.append(gt.to(device)) + out = model((voxel.to(device), seq.to(device), globf.to(device))) + preds.append(out) + + # calculate metrics + preds = torch.nn.functional.sigmoid(torch.cat(preds, dim=0)) + gt_list_valid = torch.cat(gt_list_valid, dim=0).int() + # if train_loader is None: + # print(preds) + # print(preds.shape) + # print(gt_list_valid) + # print(gt_list_valid.shape) + + macro_ap = metric_macro_ap(preds, gt_list_valid).item() + # class_ap = [round(i.item(), 5) for i in metric_class_ap(preds, gt_list_valid)] + macro_auc = metric_auc(preds, gt_list_valid).item() + macro_f1 = metric_macro_f1(preds, gt_list_valid).item() + macro_acc = metric_macro_acc(preds, gt_list_valid).item() + return train_loss, macro_ap, macro_f1, macro_acc, macro_auc + diff --git a/t6/utils.py b/t6/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..7fd5de2ae11e3a64de85ddc8c0ce0f777a738a48 --- /dev/null +++ b/t6/utils.py @@ -0,0 +1,32 @@ +import random +import torch +import numpy as np + + +def set_seed(seed): + random.seed(seed) + np.random.seed(seed) + torch.manual_seed(seed) + torch.cuda.manual_seed(seed) + torch.cuda.manual_seed_all(seed) + torch.backends.cudnn.deterministic = True + torch.backends.cudnn.benchmark = False + + +def load_pretrain_model(net, weights): + net_keys = list(net.state_dict().keys()) + weights_keys = list(weights.keys()) + # assert(len(net_keys) <= len(weights_keys)) + i = 0 + j = 0 + while i < len(net_keys) and j < len(weights_keys): + name_i = net_keys[i] + name_j = weights_keys[j] + if net.state_dict()[name_i].shape == weights[name_j].shape: + net.state_dict()[name_i].copy_(weights[name_j].cpu()) + i += 1 + j += 1 + else: + i += 1 + # print i, len(net_keys), j, len(weights_keys) + return net diff --git a/t7g/dataset.py b/t7g/dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..a14df8e595f8ef32f48b51bdbaf148d8ad737b4c --- /dev/null +++ b/t7g/dataset.py @@ -0,0 +1,383 @@ +import os +import networkx as nx +import torch +from Bio.PDB import PDBParser, is_aa +from Bio.PDB.Polypeptide import three_to_one +from torch.utils.data import Dataset +import pandas as pd +import numpy as np +from tqdm import tqdm +from Bio.Align import PairwiseAligner +from torch_geometric.data import Data +from torch_geometric.utils import from_networkx +from Bio.SeqUtils.ProtParam import ProteinAnalysis + + +aligner = PairwiseAligner() +# Define the scoring system +aligner.mode = 'global' # Global alignment +aligner.match_score = 1 +aligner.mismatch_score = -1 +aligner.open_gap_score = -0.5 +aligner.extend_gap_score = -0.1 + + +def calculate_similarity(seq_gen, seq_tem): + + # Perform the alignment + score = aligner.score(seq_gen, seq_tem) + return score / len(seq_gen) + + +def parse_ll37_testset(path='metadata/LL37_v0.csv'): + files = open(path, 'r').readlines() + seqs = [] + for file in files: + seqs.append(str(file.strip().split(',')[-1].strip().upper())) + return seqs + + +def gmean(labels): + # Assuming 'labels' is a NumPy array of shape (n, m) + # Calculate the geometric mean across axis 1 + + # Take the product of elements along axis=1 + product = np.prod(labels, axis=1) + + # nth root of product, where n is the number of elements along axis=1 + geometric_means = product ** (1.0 / labels.shape[1]) + return geometric_means + + +def clamp(n, smallest, largest): + return sorted([smallest, n, largest])[1] + + +ATOMS = {'H': 1, 'C': 12, 'N': 14, 'O': 16, 'S': 30} +ATOMS_R = {'H': 1, 'C': 1.5, 'N': 1.5, 'O': 1.5, 'S': 2} +AMINO_ACID_WATER = {'A': 255, 'V': 255, 'P': 255, 'F': 255, 'W': 255, 'I': 255, 'L': 255, 'G': 155, 'M': 155, + 'Y': 55, 'S': 55, 'T': 55, 'C': 55, 'N': 55, 'Q': 55, 'D': 55, 'E': 55, 'K': 55, 'R': 55, 'H': 55} +AMINO_ACID_CHARGE = {'D': 55, 'E': 55, 'A': 155, 'V': 155, 'P': 155, 'F': 155, 'W': 155, 'I': 155, 'L': 155, 'G': 155, + 'M': 155, 'Y': 155, 'S': 155, 'T': 155, 'C': 155, 'N': 155, 'Q': 155, 'K': 255, 'R': 255, 'H': 255} +# AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, +# 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19} +AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, + 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19, 'X': 21} + + +def pdb_parser(structure): + """ + + """ + voxel = np.zeros((4, 64, 64, 64), dtype=np.int8) + id = '' + seq_str = '' + for i in structure[0]: + id = i.id + chain = structure[0][id] + for res in chain: + if is_aa(res.get_resname(), standard=True): + resname = res.get_resname() + amino = three_to_one(resname) + seq_str += str(amino) + ATOM_WATER = AMINO_ACID_WATER[amino] + ATOM_CHARGE = AMINO_ACID_CHARGE[amino] + ATOM_CATEGORY = AMAs[amino] * 20 + + for i in res: + if i.id not in ATOMS.keys(): + continue + x, y, z = i.get_coord() + if abs(x) > 32: + x = clamp(x, -31, 31) + if abs(y) > 32: + y = clamp(x, -31, 31) + if abs(z) > 32: + z = clamp(x, -31, 31) + x_i, y_i, z_i = int(x) + 32, int(y) + 32, int(z) + 32 + ATOM_WEIGHT = ATOMS[i.id] + ATOM_R = ATOMS_R[i.id] + + if ATOM_R <= 1.5: + voxel[0, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WEIGHT + voxel[1, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WATER + voxel[2, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CHARGE + voxel[3, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CATEGORY + else: + voxel[0, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WEIGHT + voxel[1, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WATER + voxel[2, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CHARGE + voxel[3, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CATEGORY + return voxel + + +def read_scoring_functions(pdb): + scoring = False + profile = [] + for line in open(pdb): + if line.startswith("#END_POSE_ENERGIES_TABLE"): + scoring = False + if scoring: + data = [float(v) for v in line.split()[1:]] + profile.append(data) + if line.startswith("pose"): + scoring = True + return profile + + +def load_aa_features(feature_path): + aa_features = {} + for line in open(feature_path): + line = line.strip().split() + aa, features = line[0], [float(feature) for feature in line[1:]] + aa_features[aa] = features + return aa_features + + +class PairData(Data): + def __init__(self, edge_index_s, x_s): + super(PairData, self).__init__() + self.edge_index_s = edge_index_s + self.x_s = x_s + + def __inc__(self, key, value, *args): + if key == 'edge_index_s': + return self.x_s.size(0) + if key == 'wide_nodes': + return self.x_s.num_nodes + else: + return super().__inc__(key, value, *args) + + +def calculate_property(seq): + analysed_seq = ProteinAnalysis(seq) + aa_counts = analysed_seq.count_amino_acids() + aliphatic_index = ((aa_counts['A'] + 2.9 * aa_counts['V'] + 3.9 * (aa_counts['I'] + aa_counts['L'])) / len(seq)) + + positive_charged_amino_acids = ['R', 'K', 'H'] + negative_charged_amino_acids = ['D', 'E'] + + total_positive_charged = sum(aa_counts.get(aa, 0) for aa in positive_charged_amino_acids) + total_negative_charged = sum(aa_counts.get(aa, 0) for aa in negative_charged_amino_acids) + + total_charge = total_positive_charged - total_negative_charged + charge_density = total_charge / len(seq) + alpha_helix, beta_helix, turn_helix = analysed_seq.secondary_structure_fraction() + + return list( + [round(analysed_seq.gravy(), 3) * 10, round(aliphatic_index, 3) * 10, round(analysed_seq.aromaticity(), 3) * 10, + round(analysed_seq.instability_index(), 3), round(alpha_helix * 10, 3), round(beta_helix * 10, 3), + round(turn_helix * 10, 3), round(analysed_seq.charge_at_pH(7), 3), round(analysed_seq.isoelectric_point(), 3), + round(charge_density, 3) * 10]) + + +def construct_graph(structure, seq, gt, pdb_path, threshold, max_length): + # Initialize an empty graph + G = nx.Graph() + + # Check if chain 'A' is present in the structure, return None if not + if 'A' not in structure[0]: + return None + chain = structure[0]['A'] + + # Add nodes to the graph for each amino acid in the chain with their 3D positions + for res in chain: + if is_aa(res.get_resname(), standard=True): + resname = res.get_resname() + alpha_c_atom = res["CA"] + x, y, z = alpha_c_atom.get_coord() # Get 3D coordinates + G.add_node(res.id[1], name=resname, position=(x, y, z)) + + # Add edges between nodes if the distance between alpha carbons is <= 5Å + for i, m in enumerate(G.nodes): + for n in list(G.nodes)[i + 1:]: + distance = chain[m]["CA"] - chain[n]["CA"] + if distance <= 5: + G.add_edge(m, n, weight=5 / distance) + + # Load scoring functions and features + scoring = read_scoring_functions(pdb_path) + # print(pdb_path) + aa_features = load_aa_features('metadata/features.txt') + + # Assign features to nodes in the graph, including the 3D position + for node_id, node_data in G.nodes(data=True): + res = chain[node_id] + aa_feature = aa_features[three_to_one(res.get_resname())] + energy_feature = scoring[node_id - 1] + position_feature = list(node_data['position']) # Convert position tuple to list + node_data['x'] = aa_feature + energy_feature + position_feature + # node_data['x'][20:40]=[0 for i in range(20)] + # node_data['x'][40:43]=[0 for i in range(3)] + + # Convert graph to data format used in ML models + G = nx.convert_node_labels_to_integers(G) + data_wt = from_networkx(G) + data_graph = PairData(data_wt.edge_index, data_wt.x) + data_graph.gt = (gt < threshold).astype(int) + global_properties = calculate_property(seq) + + # Encode the sequence and pad it to a fixed length + seq_emb = np.zeros((max_length, 21), dtype=np.float32) + for pos in range(len(seq)): + seq_emb[pos, AMAs[seq[pos]]] = 1 + + # print(seq_emb) + data_graph.seq = seq_emb + data_graph.global_f = global_properties + return data_graph + + +class MDataset(Dataset): + ''' + x: Features. + y: Targets, if none, do prediction. + ''' + + def __init__(self, threshold=32, mode='train', max_length=50, pdb_src='af', data_ver='0922'): + self.num_classes = 6 + exclude_list = pd.read_csv('../metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() + exclude_filter = False + + p = PDBParser(QUIET=True) + + if mode == 'train': + all_data = pd.read_csv(f'../metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values + exclude_filter = True + elif mode == 'qlx': + all_data = pd.read_csv('../metadata/data_qlx.csv', encoding="unicode_escape").values + elif mode == 'saap': + all_data = pd.read_csv('../metadata/data_saap.csv', encoding="unicode_escape").values + else: + raise NotImplementedError + idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) + + filter_idx_list = [] + seq_new_list = [] + label_list = [] + for idx in range(len(idx_list)): + seq = seq_list[idx].upper().strip() + if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: + continue + if exclude_filter: + if seq in exclude_list: + continue + + filter_idx_list.append(idx) + seq_new_list.append(seq) + label_list.append(labels[idx]) + + if pdb_src == 'af': + pdb_root = '../pdb/pdb_af/' + elif pdb_src == 'hf': + pdb_root = '../pdb/pdb_dbassp/' + else: + raise NotImplementedError + self.data_list = [] + for i in tqdm(range(len(filter_idx_list))): + idx = filter_idx_list[i] + seq = seq_new_list[i] + label = label_list[i] + + # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"): + # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb" + # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"): + # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb" + if os.path.exists(pdb_root + seq + ".pdb"): + pdb_path = pdb_root + seq + ".pdb" + else: + continue + # raise FileNotFoundError + + structure = p.get_structure(idx, pdb_path) + voxel1 = construct_graph(structure, seq, label, pdb_path, threshold, max_length) + self.data_list.append(voxel1) + + def __getitem__(self, idx): + voxel1 = self.data_list[idx] + + return voxel1 + + def __len__(self): + return len(self.data_list) + + +class SDataset(Dataset): + ''' + x: Features. + y: Targets, if none, do prediction. + ''' + + def __init__(self, threshold=32, mode='train', task='0', max_length=50, pdb_src='af', data_ver='0922'): + self.num_classes = 1 + self.task = int(task) + exclude_list = pd.read_csv('metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() + exclude_filter = False + + p = PDBParser(QUIET=True) + + if mode == 'train': + all_data = pd.read_csv(f'metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values + exclude_filter = True + elif mode == 'qlx': + all_data = pd.read_csv('metadata/data_qlx.csv', encoding="unicode_escape").values + elif mode == 'saap': + all_data = pd.read_csv('metadata/data_saap.csv', encoding="unicode_escape").values + else: + raise NotImplementedError + idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) + labels = labels[:, self.task:self.task+1] + + filter_idx_list = [] + seq_new_list = [] + label_list = [] + for idx in range(len(idx_list)): + seq = seq_list[idx].upper().strip() + if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: + continue + if exclude_filter: + if seq in exclude_list: + continue + + filter_idx_list.append(idx) + seq_new_list.append(seq) + label_list.append(labels[idx]) + + if pdb_src == 'af': + pdb_root = './pdb/pdb_af/' + elif pdb_src == 'hf': + pdb_root = './pdb/pdb_dbassp/' + else: + raise NotImplementedError + self.data_list = [] + for i in tqdm(range(len(filter_idx_list))): + idx = filter_idx_list[i] + seq = seq_new_list[i] + label = label_list[i] + + # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"): + # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb" + # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"): + # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb" + if os.path.exists(pdb_root + seq + ".pdb"): + pdb_path = pdb_root + seq + ".pdb" + else: + continue + # raise FileNotFoundError + + structure = p.get_structure(idx, pdb_path) + voxel1 = construct_graph(structure, seq, label, pdb_path, threshold, max_length) + self.data_list.append(voxel1) + + def __getitem__(self, idx): + voxel1 = self.data_list[idx] + + return voxel1 + + def __len__(self): + return len(self.data_list) + diff --git a/t7g/eval.py b/t7g/eval.py new file mode 100644 index 0000000000000000000000000000000000000000..035c4d57f4a93fb51a813ea83410b38e12a80b03 --- /dev/null +++ b/t7g/eval.py @@ -0,0 +1,209 @@ +import argparse +import json +import logging +import os +import time + +from dataset import SDataset, MDataset +from network import MMGraph +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset +from torch_geometric.loader import DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=64, + help="MIC threshold for determine labels (default: 64)") +parser.add_argument('--num-layer', type=int, dest='num_layer', default=2, + help='number of GNN message passing layers (default: 2)') +parser.add_argument('--emb-dim', type=int, dest='emb_dim', default=90, + help='embedding dimensions (default: 200)') +parser.add_argument('--dropout-ratio', type=float, dest='dropout_ratio', default=0.5, + help='dropout ratio (default: 0.5)') +parser.add_argument('--graph-pooling', type=str, dest='graph_pooling', default="attention", + help='graph level pooling (sum, mean, max, attention)') +parser.add_argument('--gnn-type', type=str, dest='gnn_type', default="gatv2", + help='gnn type (gin, gcn, gat, graphsage)') + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=110, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.002, + help='learning rate (default: 0.002)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='micro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='mlce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + weight_dir = "./run/" + args.task_type + '-' + args.task + "-m-" + args.model + '-' + args.loss + str(args.batch_size) + str(args.lr) + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + raise ValueError + + logging.basicConfig(handlers=[ + # logging.FileHandler(filename=os.path.join(weight_dir, "eval.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + # with open(os.path.join(weight_dir, "config.json"), "w") as f: + # f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src) + else: + set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src) + else: + qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + # train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + + # train_loader = DataLoader(train_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=1, follow_batch=['x_s'], shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=1, follow_batch=['x_s'], shuffle=False) + + if args.model == 'mm': + model = MMGraph(num_layer=args.num_layer, input_dim=43, emb_dim=args.emb_dim, out_dim=set_all.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type=args.gnn_type, max_length=args.max_length) + + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + model.load_state_dict(torch.load(weights_path)) + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + + + if args.task_type in ('mlc', 'slc') : + _, ap, f1, acc, auc = train(args, None, model, None, valid_loader, device, None, None) + logging.info(f'ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, None, model, None, qlx_loader, device, None, None) + logging.info(f'QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, None, model, None, saap_loader, device, None, None) + logging.info(f'SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/eval.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/t7g/infer.py b/t7g/infer.py new file mode 100644 index 0000000000000000000000000000000000000000..05f044a7020a57b9c1080c72db7446bf07b0e0a9 --- /dev/null +++ b/t7g/infer.py @@ -0,0 +1,127 @@ +import argparse +import json +import logging +import os +import time + +from dataset import MDataset +from network import FusionGraph +from sklearn.model_selection import KFold +import pandas as pd +import torch +import torch.nn as nn +from torch_geometric.loader import DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--mode', type=str, default='11', + help="0 for off and 1 for on. First digit for seq, second for globf") +parser.add_argument('--num-layer', type=int, dest='num_layer', default=2, + help='number of GNN message passing layers (default: 2)') +parser.add_argument('--emb-dim', type=int, dest='emb_dim', default=128, + help='embedding dimensions (default: 128)') +parser.add_argument('--dropout-ratio', type=float, dest='dropout_ratio', default=0.3, + help='dropout ratio (default: 0.3)') +parser.add_argument('--graph-pooling', type=str, dest='graph_pooling', default="attention", + help='graph level pooling (sum, mean, max, attention)') +parser.add_argument('--gnn-type', type=str, dest='gnn_type', default="gat", + help='gnn type (gin, gcn, gat, graphsage)') +parser.add_argument('--fusion', type=str, default='attention', + help='fusion type (attention, weighted, concat)') + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +parser.add_argument('--loss', type=str, default='bce', + help='loss function') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + results = pd.DataFrame() + + logging.info('Loading Test Dataset') + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + qlx_loader = DataLoader(qlx_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + + + for fusion in ['attention', 'concat', 'weighted']: + weight_dir = "./run/" + '-'.join([args.mode, fusion, args.loss, str(args.batch_size), str(args.lr), str(args.epochs), args.pdb_src]) + pred_all = [] + gt_all = [] + for i in range(1, 6): + model = FusionGraph(num_layer=args.num_layer, input_dim=43, emb_dim=args.emb_dim, out_dim=qlx_set.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type=args.gnn_type, aux_mode=args.mode, + fusion_type=fusion) + model.load_state_dict(torch.load(os.path.join(weight_dir, f'model_{i}.pth'))) + model.to(device).eval() + with torch.no_grad(): + for data in qlx_loader: + data = data.to(device) + gt_all.append(torch.tensor(data.gt, device=device)) + out = model(data) + pred_all.append(out) + pred_all = torch.nn.functional.sigmoid(torch.cat(pred_all, dim=0)).squeeze().cpu().numpy() + gt_all = torch.cat(gt_all, dim=0).int().squeeze().cpu().numpy() + results[fusion] = pred_all.ravel(order='F') + + results['gt'] = gt_all.ravel(order='F') + + results.to_csv("preds.csv", index=False) + + +if __name__ == "__main__": + main() diff --git a/t7g/loss.py b/t7g/loss.py new file mode 100644 index 0000000000000000000000000000000000000000..d2c1cabdc7bb5df53b8513344372ea557b89aa29 --- /dev/null +++ b/t7g/loss.py @@ -0,0 +1,123 @@ +import torch +from torch import nn +import torch.nn.functional as F +from math import cos, pi, sin +import math +import numpy as np +from scipy.special import lambertw + + +def mixup_criterion(criterion, pred, y_a, y_b, lam, pow=2): + y = lam ** pow * y_a + (1 - lam) ** pow * y_b + return criterion(pred, y) + + +def mixup_data(v, q, a): + '''Returns mixed inputs, pairs of targets, and lambda without organ constraint''' + lam = np.random.beta(1, 1) + + batch_size = v.shape[0] + index = torch.randperm(batch_size) + + mixed_v = lam * v + (1 - lam) * v[index, :] + mixed_q = lam * q + (1 - lam) * q[index, :] + + a_1, a_2 = a, a[index] + return mixed_v, mixed_q, a_1, a_2, lam + + +def linear(epoch, nepoch): + return 1 - epoch / nepoch + + +def convex(epoch, nepoch): + return epoch / (2 - nepoch) + + +def concave(epoch, nepoch): + return 1 - sin((epoch / nepoch) * (pi / 2)) + + +def composite(epoch, nepoch): + return 0.5 * cos((epoch / nepoch) * pi) + 0.5 + + +class LogCoshLoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y_t, y_prime_t): + ey_t = y_t - y_prime_t + return torch.mean(torch.log(torch.cosh(ey_t + 1e-12)))+F.mse_loss(y_t, y_prime_t) + + +class MLCE(nn.Module): + def __init__(self): + super(MLCE, self).__init__() + + def _mlcce(self, y_pred, y_true): + y_pred = (1 - 2 * y_true) * y_pred + y_pred_neg = y_pred - y_true * 1e12 + y_pred_pos = y_pred - (1 - y_true) * 1e12 + zeros = torch.zeros_like(y_pred[..., :1]) + y_pred_neg = torch.cat([y_pred_neg, zeros], dim=-1) + y_pred_pos = torch.cat([y_pred_pos, zeros], dim=-1) + neg_loss = torch.logsumexp(y_pred_neg, dim=-1) + pos_loss = torch.logsumexp(y_pred_pos, dim=-1) + loss = torch.mean(neg_loss + pos_loss) + return loss + + def __call__(self, y_pred, y_true): + return self._mlcce(y_pred, y_true) + + +class SuperLoss(nn.Module): + def __init__(self, C=10, lam=1, batch_size=128): + super(SuperLoss, self).__init__() + self.tau = math.log(C) + self.lam = lam # set to 1 for CIFAR10 and 0.25 for CIFAR100 + self.batch_size = batch_size + + def forward(self, logits, targets): + l_i = F.mse_loss(logits, targets, reduction='none').detach() + sigma = self.sigma(l_i) + loss = (F.mse_loss(logits, targets, reduction='none') - self.tau) * sigma + self.lam * ( + torch.log(sigma) ** 2) + loss = loss.sum() / self.batch_size + return loss + + def sigma(self, l_i): + x = torch.ones(l_i.size()) * (-2 / math.exp(1.)) + x = x.cuda() + y = 0.5 * torch.max(x, (l_i - self.tau) / self.lam) + y = y.cpu().numpy() + sigma = np.exp(-lambertw(y)) + sigma = sigma.real.astype(np.float32) + sigma = torch.from_numpy(sigma).cuda() + return sigma + + +def unbiased_curriculum_loss(out, data, args, epoch, epochs, scheduler='linear'): + losses = [] + scheduler = linear if scheduler == 'linear' else concave + + # calculate difficulty measurement function + adjusted_losses = [] + for idx in range(out.shape[0]): + ground_truth = max(1, abs(data[idx].item())) + loss = F.mse_loss(out[idx], data[idx]) + losses.append(loss) + adjusted_losses.append(loss.item() / ground_truth) + + mean_loss, std_loss = np.mean(adjusted_losses), np.std(adjusted_losses) + + # re-weight losses + total_loss = 0 + for i, loss in enumerate(losses): + if adjusted_losses[i] > mean_loss + 1 * std_loss: + schedule_factor = scheduler(epoch, args.epochs) + total_loss += schedule_factor * loss + else: + total_loss += loss + + return total_loss diff --git a/t7g/main.py b/t7g/main.py new file mode 100644 index 0000000000000000000000000000000000000000..8e199f311a1d9d3dd20a98e741da22d11c409f62 --- /dev/null +++ b/t7g/main.py @@ -0,0 +1,247 @@ +import argparse +import json +import logging +import os +import time + +from dataset import SDataset, MDataset +from network import FusionGraph +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset +from torch_geometric.loader import DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--mode', type=str, default='11', + help="0 for off and 1 for on. First digit for seq, second for globf") +parser.add_argument('--num-layer', type=int, dest='num_layer', default=2, + help='number of GNN message passing layers (default: 2)') +parser.add_argument('--emb-dim', type=int, dest='emb_dim', default=128, + help='embedding dimensions (default: 128)') +parser.add_argument('--dropout-ratio', type=float, dest='dropout_ratio', default=0.3, + help='dropout ratio (default: 0.3)') +parser.add_argument('--graph-pooling', type=str, dest='graph_pooling', default="attention", + help='graph level pooling (sum, mean, max, attention)') +parser.add_argument('--gnn-type', type=str, dest='gnn_type', default="gat", + help='gnn type (gin, gcn, gat, graphsage)') +parser.add_argument('--fusion', type=str, default='attention', + help='fusion type (attention, weighted, concat)') + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + if args.loss == 'mlce' and args.task_type != 'slc': + criterion = MLCE() + elif args.loss == "bce" or args.task_type == 'slc': + args.loss = "bce" + criterion = nn.BCEWithLogitsLoss() + else: + criterion = 0 + pass + weight_dir = "./run/" + '-'.join([args.mode, args.fusion, args.loss, str(args.batch_size), str(args.lr), str(args.epochs), args.pdb_src]) + if not os.path.exists(weight_dir): + os.makedirs(weight_dir) + + logging.basicConfig(handlers=[ + logging.FileHandler(filename=os.path.join(weight_dir, "training.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + with open(os.path.join(weight_dir, "config.json"), "w") as f: + f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src, data_ver=args.data_ver) + else: + set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src, data_ver=args.data_ver) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src) + else: + qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + # if args.loss == 'bce': + # weight = torch.Tensor([0] * args.classes) + # for i in train_set: + # weight += i[2] + # weight = (len(train_set) - weight) / weight + # criterion.register_buffer('pos_weight', weight.to(device)) + + train_loader = DataLoader(train_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=args.batch_size, follow_batch=['x_s'], shuffle=False) + + if args.model == 'mm': + model = FusionGraph(num_layer=args.num_layer, input_dim=43, emb_dim=args.emb_dim, out_dim=set_all.num_classes, JK="last", + drop_ratio=args.dropout_ratio, graph_pooling=args.graph_pooling, gnn_type=args.gnn_type, aux_mode=args.mode, + fusion_type=args.fusion) + + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + # optimizer = torch.optim.Adam(model.parameters()) + optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr) + # optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=True, weight_decay=5e-5) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Train set:{len(train_set)}, Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + best_metric = 0 + best_qlx = 0 + best_saap = 0 + start_time = time.time() + + for epoch in range(1, args.epochs + 1): + if args.task_type in ('mlc', 'slc') : + train_loss, ap, f1, acc, auc = train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer) + logging.info(f'Epoch: {epoch:03d}, Train Loss: {train_loss:.3f}, ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + avg_metric = ap + f1 + acc + auc + if avg_metric > best_metric: + logging.info(f'Epoch: {epoch:03d} New Best validation metrics, running test session') + torch.save(model.state_dict(), weights_path) + best_metric = avg_metric + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, epoch, model, None, qlx_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_metric = qlx_ap + qlx_f1 + qlx_acc + qlx_auc + if qlx_metric > best_qlx: + best_qlx = qlx_metric + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, epoch, model, None, saap_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_metric = saap_ap + saap_f1 + saap_acc + saap_auc + if saap_metric > best_saap: + best_saap = saap_metric + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'used time {(time.time()-start_time)/3600:.2f}h') + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/result.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/t7g/network.py b/t7g/network.py new file mode 100644 index 0000000000000000000000000000000000000000..2ed129436b2cb491e69dd36c46a0581f4363d85e --- /dev/null +++ b/t7g/network.py @@ -0,0 +1,552 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F +import numpy as np +from torch.nn import Sequential, Linear, ReLU +from torch_geometric.nn import GraphConv, GINConv, GATConv, SAGEConv, GPSConv, GINEConv, GATv2Conv +from torch_geometric.nn import global_mean_pool, global_add_pool, global_max_pool, GlobalAttention, Set2Set, MulAggregation +from torch_geometric.nn import aggr +from torch_geometric.nn import GraphNorm + + +import torch +import torch.nn as nn +import torch.nn.functional as F + +class SimpleSelfAttention(nn.Module): + def __init__(self, embedding_dim, num_heads=1): + super(SimpleSelfAttention, self).__init__() + self.embedding_dim = embedding_dim + self.num_heads = num_heads + + # Assuming key_channels = value_channels = embedding_dim + self.key_channels = self.embedding_dim + self.value_channels = self.embedding_dim + + # Linear layers for queries, keys, and values + self.query = nn.Linear(embedding_dim, self.key_channels * num_heads) + self.key = nn.Linear(embedding_dim, self.key_channels * num_heads) + self.value = nn.Linear(embedding_dim, self.value_channels * num_heads) + + # Output projection layer + self.proj = nn.Linear(self.value_channels * num_heads, embedding_dim) + + # Scaling for dot-product attention + self.scale = nn.Parameter(torch.sqrt(torch.FloatTensor([self.key_channels // num_heads]))) + + def forward(self, x1, x2, x3): + # x1, x2, x3 shapes: [Batch_size, Embedding_dim] + # Stack the inputs along a new dimension (sequence dimension) + batch_size = x1.shape[0] + x = torch.stack((x1, x2, x3), dim=1) # [Batch_size, 3, Embedding_dim] + + # Compute queries, keys, values for all three inputs + Q = self.query(x) # [Batch_size, 3, num_heads * embedding_dim] + K = self.key(x) # [Batch_size, 3, num_heads * embedding_dim] + V = self.value(x) # [Batch_size, 3, num_heads * embedding_dim] + + # Reshape for multi-head attention + Q = Q.view(batch_size, -1, self.num_heads, self.embedding_dim).transpose(1, 2) # [Batch_size, num_heads, 3, embedding_dim] + K = K.view(batch_size, -1, self.num_heads, self.embedding_dim).transpose(1, 2) # [Batch_size, num_heads, 3, embedding_dim] + V = V.view(batch_size, -1, self.num_heads, self.embedding_dim).transpose(1, 2) # [Batch_size, num_heads, 3, embedding_dim] + + # Calculate dot product attention + attention_scores = torch.matmul(Q, K.transpose(-2, -1)) / self.scale + attention = F.softmax(attention_scores, dim=-1) + + # Apply attention to V + x = torch.matmul(attention, V) # [Batch_size, num_heads, 3, embedding_dim] + + # Concatenate heads and put through final linear layer + x = x.transpose(1, 2).reshape(batch_size, -1, self.num_heads * self.embedding_dim) + x = self.proj(x) # [Batch_size, 3, embedding_dim] + + # Sum the outputs from the three inputs + out = x.sum(dim=1) # [Batch_size, embedding_dim] + return out + +def cosine_similarity(x,y): + num = x.dot(y.T) + denom = np.linalg.norm(x) * np.linalg.norm(y) + return num / denom + +class LogCoshLoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y_t, y_prime_t): + ey_t = y_t - y_prime_t + return torch.mean(torch.log(torch.cosh(ey_t + 1e-12))) + + +class WeightedMSELoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y, y_t, weights=None): + loss = (y - y_t) ** 2 + if weights is not None: + loss *= weights.expand_as(loss) + return torch.mean(loss) + + +class GNN(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, JK="last", drop_ratio=0, gnn_type="gin"): + super(GNN, self).__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + # self.fc2 = nn.Linear(200, 200) + self.gnns = torch.nn.ModuleList() + for layer in range(num_layer): + in_dim = input_dim if layer == 0 else emb_dim + if gnn_type == "gin": + # self.gnns.append(GINConv(nn.Sequential(nn.Linear(in_dim, emb_dim), nn.BatchNorm1d(emb_dim), nn.ReLU(), + # nn.Linear(emb_dim, emb_dim)))) + self.gnns.append(GINConv(nn.Sequential(nn.Linear(in_dim, emb_dim), GraphNorm(emb_dim), nn.ReLU(), + nn.Linear(emb_dim, emb_dim), nn.ReLU()))) + elif gnn_type == "gps": + nn_ = Sequential( + Linear(in_dim, emb_dim), + ReLU(), + Linear(emb_dim, emb_dim), + ) + conv = GPSConv(emb_dim, GINEConv(nn_), heads=4) + self.gnns.append(conv) + elif gnn_type == "gcn": + self.gnns.append(GraphConv(in_dim, emb_dim)) + elif gnn_type == "gat": + self.gnns.append(GATConv(in_dim, emb_dim)) + elif gnn_type == "gatv2": + self.gnns.append(GATv2Conv(in_dim, emb_dim)) + elif gnn_type == "graphsage": + self.gnns.append(SAGEConv(in_dim, emb_dim)) + else: + raise ValueError("Invalid GNN type.") + + def forward(self, x, edge_index, edge_attr=None): + h_list = [x] + mut_site = [] + for layer in range(self.num_layer): + + h = self.gnns[layer](h_list[layer], edge_index, edge_attr) + # if layer == self.num_layer - 1: + # # remove relu from the last layer + # h = F.dropout(h, self.drop_ratio, training=self.training) + # else: + # h = F.dropout(F.relu(h), self.drop_ratio, training=self.training) # F.relu() + h_list.append(h) + # if len(h_list) == 2: + # previous_mut_site_feature = h_list[-2][mut_res_idx] + # current_mut_site_feature = h_list[-1][mut_res_idx] + # # print(previous_mut_site_feature.shape, current_mut_site_feature.shape) + # h_feature = self.global_encoder(previous_mut_site_feature) + # h_list[-1][mut_res_idx] = h_feature + current_mut_site_feature + # if len(h_list) == 3: + # previous_mut_site_feature = h_list[-2][mut_res_idx].squeeze(0) + # current_mut_site_feature = h_list[-1][mut_res_idx].squeeze(0) + + # h_feature = self.fc2(previous_mut_site_feature) + current_mut_site_feature + # h_list[-1][mut_res_idx] = h_feature.unsqueeze(0) + # mut_site.append() + # print(len(h_list)) + if self.JK == "last": + node_representation = h_list[-1] + elif self.JK == "sum": + h_list = [h.unsqueeze_(0) for h in h_list] + node_representation = torch.sum(torch.cat(h_list[1:], dim=0), dim=0) + # print('node_rep', node_representation.shape) + return h_list[-1] + + +# orthogonal initialization +def init_gru_orth(model, gain=1): + model.reset_parameters() + # orthogonal initialization of gru weights + for _, hh, _, _ in model.all_weights: + for i in range(0, hh.size(0), model.hidden_size): + torch.nn.init.orthogonal_(hh[i:i + model.hidden_size], gain=gain) + + +def init_lstm_orth(model, gain=1): + init_gru_orth(model, gain) + + # positive forget gate bias (Jozefowicz es at. 2015) + for _, _, ih_b, hh_b in model.all_weights: + l = len(ih_b) + ih_b[l // 4: l // 2].data.fill_(1.0) + hh_b[l // 4: l // 2].data.fill_(1.0) + + +class MLP(nn.Module): + def __init__(self, input_dim, hidden_dim, output_dim, num_layers, dropout_rate): + super(MLP, self).__init__() + + layers = [] + layers.append(nn.Linear(input_dim, hidden_dim)) + layers.append(nn.ReLU()) + layers.append(nn.Dropout(dropout_rate)) + + for _ in range(num_layers - 1): + layers.append(nn.Linear(hidden_dim, hidden_dim)) + layers.append(nn.ReLU()) + layers.append(nn.Dropout(dropout_rate)) + + layers.append(nn.Linear(hidden_dim, output_dim)) + + self.network = nn.Sequential(*layers) + + def forward(self, x): + return self.network(x) + + +class FusionGraphOld(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, out_dim, JK="last", drop_ratio=0.5, graph_pooling="attention", + gnn_type="gat", concat_type=None, fds=False, feature_level='both', contrast_curri=False, aux_mode='11') -> object: + super().__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + self.input_dim = input_dim + self.emb_dim = emb_dim + self.out_dim = out_dim + self.concat_type = concat_type + self.feature_level = feature_level + self.contrast_curri = contrast_curri + self.mode = [False, False] + final_dim = emb_dim + + if aux_mode[0] == '1': + final_dim += 128 + self.mode[0] = True + self.q_encoder = nn.LSTM( + input_size=21, + hidden_size=128, + num_layers=2, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + self.q_fc = nn.Linear(256, 128) + + if aux_mode[1] == '1': + final_dim += 128 + self.mode[1] = True + self.g_encoder = MLP(10, 128, 128, 3, 0.3) + + self.fc = nn.Sequential( + nn.Linear(final_dim, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + nn.Linear(self.emb_dim, self.emb_dim // 2), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + nn.Linear(self.emb_dim // 2, self.out_dim)) + + self.gnn = GNN(num_layer, input_dim, emb_dim, JK, drop_ratio, gnn_type=gnn_type) + + if graph_pooling == "sum": + self.pool = global_add_pool + elif graph_pooling == "mean": + self.pool = global_mean_pool + elif graph_pooling == "max": + self.pool = global_max_pool + elif graph_pooling == "mul": + self.pool = MulAggregation() + elif graph_pooling == "attention": + self.pool = GlobalAttention(gate_nn=torch.nn.Linear(emb_dim, 1)) + elif graph_pooling == "set2set": + self.pool = Set2Set(emb_dim, processing_steps=2) + elif graph_pooling == "lstm": + self.pool = aggr.LSTMAggregation(emb_dim, emb_dim) + else: + raise ValueError("Invalid graph pooling type.") + + def forward_once(self, x, edge_index, batch): + node_representation = self.gnn(x, edge_index) + graph_rep = self.pool(node_representation, batch) + return graph_rep + + def forward(self, data): + fusion = [self.forward_once(data.x_s, data.edge_index_s, data.x_s_batch)] + seq, globf = data.seq, data.global_f + device = self.fc[0].bias.device + if self.mode[0]: + seq = torch.tensor(np.asarray(seq, dtype=np.float32), device=device) + fusion.append(self.q_fc(self.q_encoder(seq)[0][:, -1, :])) + if self.mode[1]: + globf = torch.tensor(np.asarray(globf, dtype=np.float32), device=device) + fusion.append(self.g_encoder(globf)) + fusion = torch.cat(fusion, dim=-1) + x = self.fc(fusion) + return x + + +class FusionGraph(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, out_dim, JK="last", drop_ratio=0.5, graph_pooling="attention", + gnn_type="gat", fusion_type="concat", fds=False, feature_level='both', contrast_curri=False, aux_mode='11') -> object: + super().__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + self.input_dim = input_dim + self.emb_dim = emb_dim + self.out_dim = out_dim + self.fusion_type = fusion_type + self.feature_level = feature_level + self.contrast_curri = contrast_curri + self.mode = [False, False] + + # 各模态特征维度 + self.modal_dims = {'graph': emb_dim} + + if aux_mode[0] == '1': + self.mode[0] = True + self.modal_dims['seq'] = 128 + self.q_encoder = nn.LSTM( + input_size=21, + hidden_size=128, + num_layers=2, + batch_first=True, + bidirectional=True + ) + self.q_fc = nn.Linear(256, 128) + + if aux_mode[1] == '1': + self.mode[1] = True + self.modal_dims['glob'] = 128 + self.g_encoder = MLP(10, 128, 128, 3, 0.3) + + # 根据融合方式设置最终维度 + if fusion_type == "concat": + final_dim = sum(self.modal_dims.values()) + elif fusion_type in ["weighted", "attention"]: + final_dim = emb_dim # 使用统一维度 + # 为seq和glob特征添加投影层,使维度统一为emb_dim + if self.mode[0]: + self.seq_proj = nn.Linear(128, emb_dim) + if self.mode[1]: + self.glob_proj = nn.Linear(128, emb_dim) + + if fusion_type == "weighted": + # 可学习权重 + self.modal_weights = nn.Parameter(torch.ones(len(self.modal_dims))) + self.softmax = nn.Softmax(dim=0) + else: # attention + # 多头注意力 + self.multihead_attn = nn.MultiheadAttention( + embed_dim=emb_dim, + num_heads=4, + batch_first=True + ) + self.layer_norm = nn.LayerNorm(emb_dim) + + self.fc = nn.Sequential( + nn.Linear(final_dim, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + nn.Linear(self.emb_dim, self.emb_dim // 2), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + nn.Linear(self.emb_dim // 2, self.out_dim)) + + self.gnn = GNN(num_layer, input_dim, emb_dim, JK, drop_ratio, gnn_type=gnn_type) + + if graph_pooling == "sum": + self.pool = global_add_pool + elif graph_pooling == "mean": + self.pool = global_mean_pool + elif graph_pooling == "max": + self.pool = global_max_pool + elif graph_pooling == "mul": + self.pool = MulAggregation() + elif graph_pooling == "attention": + self.pool = GlobalAttention(gate_nn=torch.nn.Linear(emb_dim, 1)) + elif graph_pooling == "set2set": + self.pool = Set2Set(emb_dim, processing_steps=2) + elif graph_pooling == "lstm": + self.pool = aggr.LSTMAggregation(emb_dim, emb_dim) + else: + raise ValueError("Invalid graph pooling type.") + + def forward_once(self, x, edge_index, batch): + node_representation = self.gnn(x, edge_index) + graph_rep = self.pool(node_representation, batch) + return graph_rep + + def fuse_features(self, features): + if self.fusion_type == "concat": + return torch.cat(features, dim=-1) + + # 将所有特征投影到相同维度 + if self.mode[0]: + features[1] = self.seq_proj(features[1]) + if self.mode[1] and len(features) > 2: + features[2] = self.glob_proj(features[2]) + + if self.fusion_type == "weighted": + # 使用可学习权重加权 + weights = self.softmax(self.modal_weights) + fused = sum(w * f for w, f in zip(weights, features)) + return fused + + elif self.fusion_type == "attention": + # 将特征堆叠成序列 + features = torch.stack(features, dim=1) # [batch_size, num_modalities, emb_dim] + + # 多头注意力 + attn_output, _ = self.multihead_attn(features, features, features) + attn_output = self.layer_norm(attn_output + features) + + # 平均池化得到最终特征 + fused = torch.mean(attn_output, dim=1) # [batch_size, emb_dim] + return fused + + def forward(self, data): + features = [self.forward_once(data.x_s, data.edge_index_s, data.x_s_batch)] + seq, globf = data.seq, data.global_f + device = self.fc[0].bias.device + + if self.mode[0]: + seq = torch.tensor(np.asarray(seq, dtype=np.float32), device=device) + features.append(self.q_fc(self.q_encoder(seq)[0][:, -1, :])) + + if self.mode[1]: + globf = torch.tensor(np.asarray(globf, dtype=np.float32), device=device) + features.append(self.g_encoder(globf)) + + fusion = self.fuse_features(features) + x = self.fc(fusion) + return x + + +class MMGraph(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, out_dim, JK="last", drop_ratio=0.5, graph_pooling="attention", + gnn_type="gat", concat_type=None, fds=False, feature_level='both', contrast_curri=False, max_length=50) -> object: + super(MMGraph, self).__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + self.input_dim = input_dim + self.emb_dim = emb_dim + self.out_dim = out_dim + self.concat_type = concat_type + self.feature_level = feature_level + self.contrast_curri = contrast_curri + + self.graph_pool = nn.Linear(self.emb_dim, 1) + + self.fc = nn.Sequential( + nn.Linear(self.emb_dim, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + nn.Linear(self.emb_dim, self.out_dim)) + + if fds: + self.dir = True + else: + self.dir = False + self.global_encoder = nn.Sequential(nn.Linear(10, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio),) + self.seq_encoder = nn.Sequential( + nn.Linear(max_length, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + ) + self.gnn = GNN(num_layer, input_dim, emb_dim, JK, drop_ratio, gnn_type=gnn_type) + + if graph_pooling == "sum": + self.pool = global_add_pool + elif graph_pooling == "mean": + self.pool = global_mean_pool + elif graph_pooling == "max": + self.pool = global_max_pool + elif graph_pooling == "mul": + self.pool = MulAggregation() + elif graph_pooling == "attention": + self.pool = GlobalAttention(gate_nn=torch.nn.Linear(emb_dim, 1)) + elif graph_pooling == "set2set": + self.pool = Set2Set(emb_dim, processing_steps=2) + elif graph_pooling == "lstm": + self.pool = aggr.LSTMAggregation(emb_dim, emb_dim) + else: + raise ValueError("Invalid graph pooling type.") + self.att = SimpleSelfAttention(emb_dim, num_heads=4) + + def forward_once(self, x, edge_index, batch): + node_representation = self.gnn(x, edge_index) + graph_rep = self.pool(node_representation, batch) + return graph_rep + + def forward(self, data): + seq1, global_1 = data.seq, data.global_f + device = self.graph_pool.bias.device + seq1 = torch.tensor(seq1, dtype=torch.float, device=device) + global_1 = torch.tensor(global_1, dtype=torch.float, device=device) + + graph_rep_be = self.forward_once(data.x_s, data.edge_index_s, data.x_s_batch) + seq1_rep_be = self.seq_encoder(seq1) + global1 = self.global_encoder(global_1) + + a1 = self.att(graph_rep_be, seq1_rep_be, global1) + return self.fc(a1) + + + +class PMMGraph(nn.Module): + def __init__(self, num_layer, input_dim, emb_dim, out_dim, JK="last", drop_ratio=0.5, graph_pooling="attention", + gnn_type="gat", concat_type=None, fds=False, feature_level='both', contrast_curri=False): + super(PMMGraph, self).__init__() + self.num_layer = num_layer + self.drop_ratio = drop_ratio + self.JK = JK + self.input_dim = input_dim + self.emb_dim = emb_dim + self.out_dim = out_dim + self.concat_type = concat_type + self.feature_level = feature_level + self.contrast_curri = contrast_curri + + # Define the learnable prompt token + self.prompt_token = nn.Parameter(torch.randn(1, 10)) + + self.graph_pool = nn.Linear(self.emb_dim, 1) + + self.fc = nn.Sequential( + nn.Linear(self.emb_dim + 10, self.emb_dim), # Adjust input size to include the prompt token + nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + nn.Linear(self.emb_dim, self.out_dim)) + + self.dir = fds + self.global_encoder = nn.Sequential(nn.Linear(10, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio)) + self.seq_encoder = nn.Sequential( + nn.Linear(30, self.emb_dim), nn.LeakyReLU(0.1), nn.Dropout(p=self.drop_ratio), + ) + self.gnn = GNN(num_layer, input_dim, emb_dim, JK, drop_ratio, gnn_type=gnn_type) + + # Initialize pooling based on the specified type + if graph_pooling in ["sum", "mean", "max", "mul", "attention", "set2set", "lstm"]: + pooling_classes = { + "sum": global_add_pool, + "mean": global_mean_pool, + "max": global_max_pool, + "mul": aggr.MulAggregation(), + "attention": GlobalAttention(gate_nn=torch.nn.Linear(emb_dim, 1)), + "set2set": Set2Set(emb_dim, processing_steps=2), + "lstm": aggr.LSTMAggregation(emb_dim, emb_dim) + } + self.pool = pooling_classes[graph_pooling] + else: + raise ValueError("Invalid graph pooling type.") + + self.att = SimpleSelfAttention(emb_dim + 10, num_heads=4) # Adjust for prompt dimension + + def forward_once(self, x, edge_index, batch): + node_representation = self.gnn(x, edge_index) + graph_rep = self.pool(node_representation, batch) + # Concatenate the prompt token + graph_rep = torch.cat([graph_rep, self.prompt_token.expand(graph_rep.size(0), -1)], dim=1) + return graph_rep + + def forward(self, data): + seq1 = torch.tensor(np.array(data.seq, dtype=np.float32)).to(device='cuda') + global_1 = torch.tensor(np.array(data.global_f, dtype=np.float32)).to(device='cuda') + + graph_rep_be = self.forward_once(data.x_s, data.edge_index_s, data.x_s_batch) + seq1_rep_be = self.seq_encoder(seq1) + global1_rep = self.global_encoder(global_1) + # Concatenate the prompt token to other representations as well + seq1_rep_be = torch.cat([seq1_rep_be, self.prompt_token.expand(seq1_rep_be.size(0), -1)], dim=1) + global1_rep = torch.cat([global1_rep, self.prompt_token.expand(global1_rep.size(0), -1)], dim=1) + + # Process combined representations + a1 = self.att(graph_rep_be, seq1_rep_be, global1_rep) + return self.fc(a1) + + diff --git a/t7g/run/11-attention-bce-256-0.001-50-af/config.json b/t7g/run/11-attention-bce-256-0.001-50-af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..0da994ceb998f8b82e2400ea123ee8d8a9f6ad8a --- /dev/null +++ b/t7g/run/11-attention-bce-256-0.001-50-af/config.json @@ -0,0 +1 @@ +{"model": "mm", "mode": "11", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gat", "fusion": "attention", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t7g/run/11-attention-bce-256-0.001-50-af/model_1.pth b/t7g/run/11-attention-bce-256-0.001-50-af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..8ac3bb46c162749ee08b345c25cea70e1eba2f30 --- /dev/null +++ b/t7g/run/11-attention-bce-256-0.001-50-af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:39053a2b810d834445ba99ecead35be0027ca8721c8da2cf130b998b5c32d877 +size 3138151 diff --git a/t7g/run/11-attention-bce-256-0.001-50-af/model_2.pth b/t7g/run/11-attention-bce-256-0.001-50-af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..ed68804c892fba1f137478f9ae084fad78c31f58 --- /dev/null +++ b/t7g/run/11-attention-bce-256-0.001-50-af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:349275fa75a4f4241f78d87d5a29b4f74e1048b71a2b370dbc1289582313379b +size 3138151 diff --git a/t7g/run/11-attention-bce-256-0.001-50-af/model_3.pth b/t7g/run/11-attention-bce-256-0.001-50-af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..85c16a0d9147f86a248c9886332b06376b842350 --- /dev/null +++ b/t7g/run/11-attention-bce-256-0.001-50-af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:deb378d19f892d0b8f81ec75e6e82955fe30df70d4e81d0f6827b0c75f58724f +size 3138151 diff --git a/t7g/run/11-attention-bce-256-0.001-50-af/model_4.pth b/t7g/run/11-attention-bce-256-0.001-50-af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..fad96dd15606709c0f86e0ba961a5648f9904aac --- /dev/null +++ b/t7g/run/11-attention-bce-256-0.001-50-af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b73e7ddd3a9ead529fdde74b966c430a3a49bcea29ac552e50080d0ed059c243 +size 3138151 diff --git a/t7g/run/11-attention-bce-256-0.001-50-af/model_5.pth b/t7g/run/11-attention-bce-256-0.001-50-af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..6609e837c623a79dc8fa941d63f164034b6352e7 --- /dev/null +++ b/t7g/run/11-attention-bce-256-0.001-50-af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:36fea40a758d983ad0f64aa2e3a0e6508caa80e91e1a2d83ce1245ed3ca58c4b +size 3138151 diff --git a/t7g/run/11-attention-bce-256-0.001-50-af/result.txt b/t7g/run/11-attention-bce-256-0.001-50-af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..a240a687235ebcb4cf0d68964dba5d9781445953 --- /dev/null +++ b/t7g/run/11-attention-bce-256-0.001-50-af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8906526088714599,0.8458983659744262,0.794313108921051,0.8459210157394409 +0.006969411690762964,0.009864561067143156,0.010719514785071316,0.008838124121062914 +qlx +0.8155104517936707,0.7446308732032776,0.6496688842773437,0.8239124298095704 +0.029204764945541313,0.019481332953578838,0.045506748692458646,0.019899947725424843 +saap +0.5623661279678345,0.5130134224891663,0.38799998760223386,0.6365700364112854 +0.006656602653275353,0.0006310281806608342,0.0037118393686640994,0.006312324459300946 diff --git a/t7g/run/11-concat-bce-256-0.001-50-af/config.json b/t7g/run/11-concat-bce-256-0.001-50-af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..1064044ac3f632886effc93ead4032ced77cf19a --- /dev/null +++ b/t7g/run/11-concat-bce-256-0.001-50-af/config.json @@ -0,0 +1 @@ +{"model": "mm", "mode": "11", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gat", "fusion": "concat", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t7g/run/11-concat-bce-256-0.001-50-af/model_1.pth b/t7g/run/11-concat-bce-256-0.001-50-af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..25395eaaec5628f39279902e98a69242a8e2e7ff --- /dev/null +++ b/t7g/run/11-concat-bce-256-0.001-50-af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d374e961329de477cee6c3fc7d274633cb96754cca5a01b258bc2cded88d77f1 +size 2868613 diff --git a/t7g/run/11-concat-bce-256-0.001-50-af/model_2.pth b/t7g/run/11-concat-bce-256-0.001-50-af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..738cbbb484d9055f4fc1d4f5eb78fc99a751e791 --- /dev/null +++ b/t7g/run/11-concat-bce-256-0.001-50-af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ec7228544000a999b13f7b5371c00943f795ce574d5061d504350a39d12c7711 +size 2868613 diff --git a/t7g/run/11-concat-bce-256-0.001-50-af/model_3.pth b/t7g/run/11-concat-bce-256-0.001-50-af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..20beb2a33e5c8616d5c9042cdb257c424c3e2cee --- /dev/null +++ b/t7g/run/11-concat-bce-256-0.001-50-af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9c57fc873ebeb9df9e3e9b39400510b04715f53d722f237f26e135297644c31a +size 2868613 diff --git a/t7g/run/11-concat-bce-256-0.001-50-af/model_4.pth b/t7g/run/11-concat-bce-256-0.001-50-af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..836ab6fb87bf562a8b05769a5f6ff8f8833081b2 --- /dev/null +++ b/t7g/run/11-concat-bce-256-0.001-50-af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:093c823050093f7c97689a9fdd8f34d4f20654db21f28804ebfd27d293ff0ac5 +size 2868613 diff --git a/t7g/run/11-concat-bce-256-0.001-50-af/model_5.pth b/t7g/run/11-concat-bce-256-0.001-50-af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..2ebe944e90c3f13fbc9f0462d40a69f511b9c806 --- /dev/null +++ b/t7g/run/11-concat-bce-256-0.001-50-af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6ce37b93d164466523000797ac8339312a8d7f5f92d5e8ce135273f73b65a10e +size 2868613 diff --git a/t7g/run/11-concat-bce-256-0.001-50-af/result.txt b/t7g/run/11-concat-bce-256-0.001-50-af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..b252c1fddb93c0c1bea285b546b237a56a29be1e --- /dev/null +++ b/t7g/run/11-concat-bce-256-0.001-50-af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8937425494194031,0.8502801179885864,0.7986298561096191,0.8496387720108032 +0.009840772995502694,0.004373675472594878,0.007336293991336078,0.010979699392615994 +qlx +0.7978778600692749,0.728077495098114,0.6136865258216858,0.8171471238136292 +0.008000924348193605,0.018518159106426006,0.04150460558843204,0.01350196101771272 +saap +0.5798661947250366,0.5136282682418823,0.39099998474121095,0.6707816004753113 +0.02850160761049151,0.002650815614473853,0.007999992370605469,0.03297382007782335 diff --git a/t7g/run/11-weighted-bce-256-0.001-50-af/config.json b/t7g/run/11-weighted-bce-256-0.001-50-af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..1d39e4c078d17d34608c44f1a590d83500ae9c27 --- /dev/null +++ b/t7g/run/11-weighted-bce-256-0.001-50-af/config.json @@ -0,0 +1 @@ +{"model": "mm", "mode": "11", "num_layer": 2, "emb_dim": 128, "dropout_ratio": 0.3, "graph_pooling": "attention", "gnn_type": "gat", "fusion": "weighted", "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "gpu": 0, "batch_size": 256, "epochs": 50, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/t7g/run/11-weighted-bce-256-0.001-50-af/model_1.pth b/t7g/run/11-weighted-bce-256-0.001-50-af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..5a1e84b2e2369928c86d6a8203e1422839f1e76c --- /dev/null +++ b/t7g/run/11-weighted-bce-256-0.001-50-af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:df8b79ae9e0af332d42150755da71056663f2618cffcd360b8684d355f2967b3 +size 2871286 diff --git a/t7g/run/11-weighted-bce-256-0.001-50-af/model_2.pth b/t7g/run/11-weighted-bce-256-0.001-50-af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..977ebd54e2d31dfdb8f7a568065c392126c2d83f --- /dev/null +++ b/t7g/run/11-weighted-bce-256-0.001-50-af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:da92b193ec32fe4f7243c8821a67f52685869d99881a29b12eb6805e6ae04f76 +size 2871286 diff --git a/t7g/run/11-weighted-bce-256-0.001-50-af/model_3.pth b/t7g/run/11-weighted-bce-256-0.001-50-af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..f8892969c2a6089906a839bbfe20f8e27afaf6f7 --- /dev/null +++ b/t7g/run/11-weighted-bce-256-0.001-50-af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1a2215c668bda75088f5c43a22bf10737dd0afb2420e18bccee248e6c833e698 +size 2871286 diff --git a/t7g/run/11-weighted-bce-256-0.001-50-af/model_4.pth b/t7g/run/11-weighted-bce-256-0.001-50-af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..07f3b830ffeecad9f650f25101a6b19825c3fd47 --- /dev/null +++ b/t7g/run/11-weighted-bce-256-0.001-50-af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3428ec7bbe2ba10e9797a7b8bc1af97f8adfe7ec049cbc01ca7697ac4137cfd1 +size 2871286 diff --git a/t7g/run/11-weighted-bce-256-0.001-50-af/model_5.pth b/t7g/run/11-weighted-bce-256-0.001-50-af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..7ed55bb63f06dfe79efd0b296dbda8b70de5f69e --- /dev/null +++ b/t7g/run/11-weighted-bce-256-0.001-50-af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:13ec62b69b906b9e2e7d1e0f4ec75ac60a59035dcc204efca691e126e9ed0884 +size 2871286 diff --git a/t7g/run/11-weighted-bce-256-0.001-50-af/result.txt b/t7g/run/11-weighted-bce-256-0.001-50-af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..6e06c57c1d4a3284d8a8039a27c24673e984638f --- /dev/null +++ b/t7g/run/11-weighted-bce-256-0.001-50-af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8942725658416748,0.8485615134239197,0.7955899953842163,0.8480118155479431 +0.005666542072457889,0.007345727556226167,0.009063478117582107,0.005927586582715533 +qlx +0.7967317938804627,0.7464370965957642,0.646799111366272,0.8072592258453369 +0.018960282212351274,0.020990932275104818,0.04007093597227089,0.021167790918060606 +saap +0.5488728821277619,0.5133700013160706,0.3883333206176758,0.6320559740066528 +0.04315551552438086,0.0016386814175641737,0.004216366192513858,0.0336483908769936 diff --git a/t7g/runthro.sh b/t7g/runthro.sh new file mode 100644 index 0000000000000000000000000000000000000000..4acbc387cfaf5f2fd98642d372b043d84ac15e23 --- /dev/null +++ b/t7g/runthro.sh @@ -0,0 +1,6 @@ +#!/bin/bash +python main.py --mode 11 +python main.py --mode 01 +python main.py --mode 10 + + diff --git a/t7g/summary.py b/t7g/summary.py new file mode 100644 index 0000000000000000000000000000000000000000..420cc50d5a8330d07342a07c2f5bfc482023480a --- /dev/null +++ b/t7g/summary.py @@ -0,0 +1,59 @@ +import os +import csv +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument('--dir', type=str, default='./run') +parser.add_argument('--output', type=str, default='summary.csv') +args = parser.parse_args() + +# 设置目录路径和输出CSV文件名 +directory_path = args.dir +output_csv = args.output + +# 创建CSV文件并写入数据 +with open(output_csv, mode='w', newline='') as file: + writer = csv.writer(file) + + # 写入第一个子表的标题和表头 + writer.writerow(['QLX']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 6: + means_5_6 = lines[4].strip().split(',') + std_devs_5_6 = lines[5].strip().split(',') + if len(means_5_6) == 4 and len(std_devs_5_6) == 4: + values_5_6 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_5_6, std_devs_5_6)] + writer.writerow([subdirectory] + values_5_6) + + # 加入空行以分隔两个子表 + writer.writerow([]) + + # 写入第二个子表的标题和表头 + writer.writerow(['SAAP']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 再次遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 9: + means_8_9 = lines[7].strip().split(',') + std_devs_8_9 = lines[8].strip().split(',') + if len(means_8_9) == 4 and len(std_devs_8_9) == 4: + values_8_9 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_8_9, std_devs_8_9)] + writer.writerow([subdirectory] + values_8_9) \ No newline at end of file diff --git a/t7g/train.py b/t7g/train.py new file mode 100644 index 0000000000000000000000000000000000000000..de2a4067e435ff9c1ead1fe0d33496f726d6c3ff --- /dev/null +++ b/t7g/train.py @@ -0,0 +1,56 @@ +import torch +from torchmetrics import F1Score, Accuracy, AveragePrecision, AUROC + + +def train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer): + train_loss = 0 + num_labels = model.out_dim + avg = args.metric_avg + if num_labels == 1: + task = 'binary' + else: + task = 'multilabel' + metric_macro_acc = Accuracy(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_f1 = F1Score(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_ap = AveragePrecision(average=avg, task=task, num_labels=num_labels).to(device) + metric_auc = AUROC(average=avg, task=task, num_labels=num_labels).to(device) + + if train_loader is not None: + model.train() + for data in train_loader: + data = data.to(device) + # print(seq_lengths) + out = model(data) + # print(out[0]) + # print(gt[0]) + loss = criterion(out, torch.tensor(data.gt, dtype=torch.float, device=device)) + # loss_0 = criterion(out[0], gt.to(device).float()) + # loss_1 = criterion(out[1], gt.to(device).float()) + # loss_2 = criterion(out[2], gt.to(device).float()) + # loss = loss_0 + loss_1 + loss_2 + loss.backward() + optimizer.step() + optimizer.zero_grad() + train_loss += loss.item() + + model.eval() + preds = [] + gt_list_valid = [] + with torch.no_grad(): + for data in valid_loader: + data = data.to(device) + gt_list_valid.append(torch.tensor(data.gt, device=device)) + out = model(data) + preds.append(out) + + # calculate metrics + preds = torch.nn.functional.sigmoid(torch.cat(preds, dim=0)) + gt_list_valid = torch.cat(gt_list_valid, dim=0).int() + + macro_ap = metric_macro_ap(preds, gt_list_valid).item() + # class_ap = [round(i.item(), 5) for i in metric_class_ap(preds, gt_list_valid)] + macro_auc = metric_auc(preds, gt_list_valid).item() + macro_f1 = metric_macro_f1(preds, gt_list_valid).item() + macro_acc = metric_macro_acc(preds, gt_list_valid).item() + return train_loss, macro_ap, macro_f1, macro_acc, macro_auc + diff --git a/t7g/utils.py b/t7g/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..7fd5de2ae11e3a64de85ddc8c0ce0f777a738a48 --- /dev/null +++ b/t7g/utils.py @@ -0,0 +1,32 @@ +import random +import torch +import numpy as np + + +def set_seed(seed): + random.seed(seed) + np.random.seed(seed) + torch.manual_seed(seed) + torch.cuda.manual_seed(seed) + torch.cuda.manual_seed_all(seed) + torch.backends.cudnn.deterministic = True + torch.backends.cudnn.benchmark = False + + +def load_pretrain_model(net, weights): + net_keys = list(net.state_dict().keys()) + weights_keys = list(weights.keys()) + # assert(len(net_keys) <= len(weights_keys)) + i = 0 + j = 0 + while i < len(net_keys) and j < len(weights_keys): + name_i = net_keys[i] + name_j = weights_keys[j] + if net.state_dict()[name_i].shape == weights[name_j].shape: + net.state_dict()[name_i].copy_(weights[name_j].cpu()) + i += 1 + j += 1 + else: + i += 1 + # print i, len(net_keys), j, len(weights_keys) + return net diff --git a/voxabl/dataset.py b/voxabl/dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..c3ee8be7cf65cf6840f543904e83c2529e6caf50 --- /dev/null +++ b/voxabl/dataset.py @@ -0,0 +1,259 @@ +import os +import torch +from Bio.PDB import PDBParser, is_aa +from Bio.PDB.Polypeptide import three_to_one +from torch.utils.data import Dataset +import pandas as pd +import numpy as np +from tqdm import tqdm +from Bio.Align import PairwiseAligner + + +aligner = PairwiseAligner() +# Define the scoring system +aligner.mode = 'global' # Global alignment +aligner.match_score = 1 +aligner.mismatch_score = -1 +aligner.open_gap_score = -0.5 +aligner.extend_gap_score = -0.1 + + +def calculate_similarity(seq_gen, seq_tem): + + # Perform the alignment + score = aligner.score(seq_gen, seq_tem) + return score / len(seq_gen) + + +def parse_ll37_testset(path='metadata/LL37_v0.csv'): + files = open(path, 'r').readlines() + seqs = [] + for file in files: + seqs.append(str(file.strip().split(',')[-1].strip().upper())) + return seqs + + +def gmean(labels): + # Assuming 'labels' is a NumPy array of shape (n, m) + # Calculate the geometric mean across axis 1 + + # Take the product of elements along axis=1 + product = np.prod(labels, axis=1) + + # nth root of product, where n is the number of elements along axis=1 + geometric_means = product ** (1.0 / labels.shape[1]) + return geometric_means + + +def clamp(n, smallest, largest): + return sorted([smallest, n, largest])[1] + + +ATOMS = {'H': 1, 'C': 12, 'N': 14, 'O': 16, 'S': 30} +ATOMS_R = {'H': 1, 'C': 1.5, 'N': 1.5, 'O': 1.5, 'S': 2} +AMINO_ACID_WATER = {'A': 255, 'V': 255, 'P': 255, 'F': 255, 'W': 255, 'I': 255, 'L': 255, 'G': 155, 'M': 155, + 'Y': 55, 'S': 55, 'T': 55, 'C': 55, 'N': 55, 'Q': 55, 'D': 55, 'E': 55, 'K': 55, 'R': 55, 'H': 55} +AMINO_ACID_CHARGE = {'D': 55, 'E': 55, 'A': 155, 'V': 155, 'P': 155, 'F': 155, 'W': 155, 'I': 155, 'L': 155, 'G': 155, + 'M': 155, 'Y': 155, 'S': 155, 'T': 155, 'C': 155, 'N': 155, 'Q': 155, 'K': 255, 'R': 255, 'H': 255} +# AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, +# 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19} +AMAs = {'G': 20, 'A': 1, 'V': 2, 'L': 3, 'I': 4, 'P': 5, 'F': 6, 'Y': 7, 'W': 8, 'S': 9, 'T': 10, 'C': 11, + 'M': 12, 'N': 13, 'Q': 14, 'D': 15, 'E': 16, 'K': 17, 'R': 18, 'H': 19, 'X': 21} + + +def pdb_parser(structure): + """ + + """ + voxel = np.zeros((4, 64, 64, 64), dtype=np.int8) + id = '' + seq_str = '' + for i in structure[0]: + id = i.id + chain = structure[0][id] + for res in chain: + if is_aa(res.get_resname(), standard=True): + resname = res.get_resname() + amino = three_to_one(resname) + seq_str += str(amino) + ATOM_WATER = AMINO_ACID_WATER[amino] + ATOM_CHARGE = AMINO_ACID_CHARGE[amino] + ATOM_CATEGORY = AMAs[amino] * 20 + + for i in res: + if i.id not in ATOMS.keys(): + continue + x, y, z = i.get_coord() + if abs(x) > 32: + x = clamp(x, -31, 31) + if abs(y) > 32: + y = clamp(x, -31, 31) + if abs(z) > 32: + z = clamp(x, -31, 31) + x_i, y_i, z_i = int(x) + 32, int(y) + 32, int(z) + 32 + ATOM_WEIGHT = ATOMS[i.id] + ATOM_R = ATOMS_R[i.id] + + if ATOM_R <= 1.5: + voxel[0, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WEIGHT + voxel[1, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_WATER + voxel[2, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CHARGE + voxel[3, x_i - 1:x_i + 1, y_i - 1:y_i + 1, z_i - 1:z_i + 1] = ATOM_CATEGORY + else: + voxel[0, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WEIGHT + voxel[1, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_WATER + voxel[2, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CHARGE + voxel[3, x_i - ATOM_R: x_i + ATOM_R, x_i - ATOM_R: x_i + ATOM_R, + x_i - ATOM_R: x_i + ATOM_R] = ATOM_CATEGORY + return voxel + + +class MDataset(Dataset): + def __init__(self, threshold=32, mode='train', max_length=50, pdb_src='af', data_ver='0922', exclude_channel: int=None): + self.num_classes = 6 + self.channel = [0, 1, 2, 3] + if exclude_channel is not None: + self.channel.pop(exclude_channel) + exclude_list = pd.read_csv('../metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() + exclude_filter = False + + p = PDBParser(QUIET=True) + + if mode == 'train': + all_data = pd.read_csv(f'../metadata/data_{data_ver}_i.csv', encoding="unicode_escape").values + exclude_filter = True + elif mode == 'qlx': + all_data = pd.read_csv('../metadata/data_qlx.csv', encoding="unicode_escape").values + elif mode == 'saap': + all_data = pd.read_csv('../metadata/data_saap.csv', encoding="unicode_escape").values + else: + raise NotImplementedError + idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) + labels = (labels < threshold).astype(int) + + filter_idx_list = [] + seq_new_list = [] + label_list = [] + for idx in range(len(idx_list)): + seq = seq_list[idx].upper().strip() + if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: + continue + if exclude_filter: + if seq in exclude_list: + continue + + filter_idx_list.append(idx) + seq_new_list.append(seq) + label_list.append(labels[idx]) + + if pdb_src == 'af': + pdb_root = '../pdb/pdb_af/' + elif pdb_src == 'hf': + pdb_root = '../pdb/pdb_dbassp/' + else: + raise NotImplementedError + self.data_list = [] + for i in tqdm(range(len(filter_idx_list))): + idx = filter_idx_list[i] + seq = seq_new_list[i] + label = label_list[i] + + if os.path.exists(pdb_root + seq + ".pdb"): + pdb_path = pdb_root + seq + ".pdb" + else: + print(f'lacking pdb file {seq}') + continue + # raise FileNotFoundError + + structure = p.get_structure(idx, pdb_path) + voxel = pdb_parser(structure) + seq_emb = [AMAs[char] for char in seq] + [0] * (max_length - len(seq)) + self.data_list.append((voxel, seq_emb, label)) + + def __getitem__(self, idx): + voxel, seq_emb, gt = self.data_list[idx] + + return torch.Tensor(voxel)[self.channel].float(), torch.Tensor(seq_emb), torch.Tensor(gt) + + def __len__(self): + return len(self.data_list) + + +# class SDataset(Dataset): +# ''' +# x: Features. +# y: Targets, if none, do prediction. +# ''' + +# def __init__(self, threshold=32, mode='train', task='0', max_length=50, pdb_src='af'): +# self.num_classes = 1 +# self.task = int(task) +# exclude_list = pd.read_csv('metadata/data_simi.csv', encoding="unicode_escape")['Seq'].str.upper().str.strip().tolist() +# exclude_filter = False + +# p = PDBParser(QUIET=True) + +# if mode == 'train': +# all_data = pd.read_csv('metadata/data_0922_i.csv', encoding="unicode_escape").values +# exclude_filter = True +# elif mode == 'qlx': +# all_data = pd.read_csv('metadata/data_qlx.csv', encoding="unicode_escape").values +# elif mode == 'saap': +# all_data = pd.read_csv('metadata/data_saap.csv', encoding="unicode_escape").values +# else: +# raise NotImplementedError +# idx_list, seq_list, labels = all_data[:, 0], all_data[:, 1], np.concatenate((all_data[:, 4:9], all_data[:, 10:11]), axis=1) +# labels = (labels[:, self.task:self.task+1] <= threshold).astype(int) + +# filter_idx_list = [] +# seq_new_list = [] +# label_list = [] +# for idx in range(len(idx_list)): +# seq = seq_list[idx].upper().strip() +# if 'X' in seq or 'B' in seq or 'J' in seq or 'Z' in seq or 'U' in seq or 'O' in seq or len(seq) > max_length or len(seq) < 6: +# continue +# if exclude_filter: +# if seq in exclude_list: +# continue + +# filter_idx_list.append(idx) +# seq_new_list.append(seq) +# label_list.append(labels[idx]) + +# if pdb_src == 'af': +# pdb_root = './pdb/pdb_af/' +# elif pdb_src == 'hf': +# pdb_root = './pdb/pdb_dbassp/' +# else: +# raise NotImplementedError +# self.data_list = [] +# for i in tqdm(range(len(filter_idx_list))): +# idx = filter_idx_list[i] +# seq = seq_new_list[i] +# label = label_list[i] + +# # if os.path.exists("./pdb/pdb_dbassp/" + seq + ".pdb"): +# # pdb_path = "./pdb/pdb_dbassp/" + seq + ".pdb" +# # elif os.path.exists("./pdb/pdb_gen/" + seq + ".pdb"): +# # pdb_path = "./pdb/pdb_gen/" + seq + ".pdb" +# if os.path.exists(pdb_root + seq + ".pdb"): +# pdb_path = pdb_root + seq + ".pdb" +# else: +# continue +# # raise FileNotFoundError + +# structure = p.get_structure(idx, pdb_path) +# voxel = pdb_parser(structure) +# seq_emb = [AMAs[char] for char in seq] + [0] * (max_length - len(seq)) +# self.data_list.append((voxel, seq_emb, label)) + +# def __getitem__(self, idx): +# voxel, seq_emb, gt = self.data_list[idx] + +# return torch.Tensor(voxel).float(), torch.Tensor(seq_emb), torch.Tensor(gt) + +# def __len__(self): +# return len(self.data_list) \ No newline at end of file diff --git a/voxabl/eval.py b/voxabl/eval.py new file mode 100644 index 0000000000000000000000000000000000000000..18f38bfff4c7b7f9bf2fef9834b96c07a96d947d --- /dev/null +++ b/voxabl/eval.py @@ -0,0 +1,203 @@ +import argparse +import json +import logging +import os +import time + +from dataset import SDataset, MDataset +from network import MMPeptide, SEQPeptide, VoxPeptide, MMFPeptide, SMPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='mm', + help='model resnet26, bi-gru') +parser.add_argument('--fusion', type=str, default='1', + help="Seed for splitting dataset (default 1)") + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=64, + help="MIC threshold for determine labels (default: 64)") + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=128, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=50, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.02, + help='learning rate (default: 0.002)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='micro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='mlce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + weight_dir = "./run/" + args.task_type + '-' + args.task + "-m-" + args.model + '-' + args.loss + str(args.batch_size) + str(args.lr) + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + raise ValueError + + logging.basicConfig(handlers=[ + # logging.FileHandler(filename=os.path.join(weight_dir, "eval.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + # with open(os.path.join(weight_dir, "config.json"), "w") as f: + # f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src) + else: + set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src) + else: + qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + # train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + + # train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=1, shuffle=False) + if args.model == 'seq': + model = SEQPeptide(classes=set_all.num_classes, q_encoder='mlp') + elif args.model == 'voxel': + model = VoxPeptide(classes=set_all.num_classes) + elif args.model == 'mm': + # model = MMPeptide(classes=train_set.num_classes, q_encoder='tf', ) # attention='hamburger' + # model = SMPeptide(classes=train_set.num_classes, q_encoder='mlp', max_length=30) # attention='hamburger' + model = MMPeptide(classes=set_all.num_classes, q_encoder='mlp', max_length=args.max_length)# , attention='hamburger' + elif args.model == 'mmf': + model = MMFPeptide(classes=set_all.num_classes, q_encoder='mlp', max_length=args.max_length) # attention='hamburger' + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + model.load_state_dict(torch.load(weights_path)) + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + + + if args.task_type in ('mlc', 'slc') : + _, ap, f1, acc, auc = train(args, None, model, None, valid_loader, device, None, None) + logging.info(f'ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, None, model, None, qlx_loader, device, None, None) + logging.info(f'QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, None, model, None, saap_loader, device, None, None) + logging.info(f'SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/eval.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/voxabl/gen_simi.py b/voxabl/gen_simi.py new file mode 100644 index 0000000000000000000000000000000000000000..c350b6cbccbe8e4884ea3bbbfd150eb1a66b348b --- /dev/null +++ b/voxabl/gen_simi.py @@ -0,0 +1,38 @@ +import pandas as pd +from dataset import calculate_similarity + +# 读取模板 CSV 文件和目标 CSV 文件 +template_file = './metadata/data_test.csv' # 模板CSV文件路径 +target_file = './metadata/data_0920_i.csv' # 目标CSV文件路径 + +template_df = pd.read_csv(template_file) +target_df = pd.read_csv(target_file) + +# 假设我们要使用 'template_column' 列作为模板中的列,'target_column' 列作为目标表格中的列 +template_column = 'Seq' # 替换为你的模板表格列名 +target_column = 'Seq' # 替换为你的目标表格列名 + +# 创建一个空的 DataFrame,用于存储符合条件的行,并复制目标表格的表头 +extracted_rows = pd.DataFrame(columns=target_df.columns) + +# 遍历目标表格的每一行 +for index, target_row in target_df.iterrows(): + target_value = target_row[target_column] + + # 遍历模板表格的每个值 + for template_value in template_df[template_column]: + + # 计算指标 + metric = calculate_similarity(target_value.strip().upper(), template_value.strip().upper()) + + # 如果指标大于0.3,则提取目标表格的该行 + if metric > 0.3: + extracted_rows = pd.concat([extracted_rows, pd.DataFrame([target_row])], ignore_index=True) + # 只要找到一个匹配值,就可以继续检查下一行 + break + +# 将提取的行保存到一个新的CSV文件中,确保表头与目标表格一致 +output_file = './metadata/data_simi.csv' +extracted_rows.to_csv(output_file, index=False) + +print(f"符合条件的行已保存到 {output_file}") \ No newline at end of file diff --git a/voxabl/loss.py b/voxabl/loss.py new file mode 100644 index 0000000000000000000000000000000000000000..d2c1cabdc7bb5df53b8513344372ea557b89aa29 --- /dev/null +++ b/voxabl/loss.py @@ -0,0 +1,123 @@ +import torch +from torch import nn +import torch.nn.functional as F +from math import cos, pi, sin +import math +import numpy as np +from scipy.special import lambertw + + +def mixup_criterion(criterion, pred, y_a, y_b, lam, pow=2): + y = lam ** pow * y_a + (1 - lam) ** pow * y_b + return criterion(pred, y) + + +def mixup_data(v, q, a): + '''Returns mixed inputs, pairs of targets, and lambda without organ constraint''' + lam = np.random.beta(1, 1) + + batch_size = v.shape[0] + index = torch.randperm(batch_size) + + mixed_v = lam * v + (1 - lam) * v[index, :] + mixed_q = lam * q + (1 - lam) * q[index, :] + + a_1, a_2 = a, a[index] + return mixed_v, mixed_q, a_1, a_2, lam + + +def linear(epoch, nepoch): + return 1 - epoch / nepoch + + +def convex(epoch, nepoch): + return epoch / (2 - nepoch) + + +def concave(epoch, nepoch): + return 1 - sin((epoch / nepoch) * (pi / 2)) + + +def composite(epoch, nepoch): + return 0.5 * cos((epoch / nepoch) * pi) + 0.5 + + +class LogCoshLoss(nn.Module): + def __init__(self): + super().__init__() + + def forward(self, y_t, y_prime_t): + ey_t = y_t - y_prime_t + return torch.mean(torch.log(torch.cosh(ey_t + 1e-12)))+F.mse_loss(y_t, y_prime_t) + + +class MLCE(nn.Module): + def __init__(self): + super(MLCE, self).__init__() + + def _mlcce(self, y_pred, y_true): + y_pred = (1 - 2 * y_true) * y_pred + y_pred_neg = y_pred - y_true * 1e12 + y_pred_pos = y_pred - (1 - y_true) * 1e12 + zeros = torch.zeros_like(y_pred[..., :1]) + y_pred_neg = torch.cat([y_pred_neg, zeros], dim=-1) + y_pred_pos = torch.cat([y_pred_pos, zeros], dim=-1) + neg_loss = torch.logsumexp(y_pred_neg, dim=-1) + pos_loss = torch.logsumexp(y_pred_pos, dim=-1) + loss = torch.mean(neg_loss + pos_loss) + return loss + + def __call__(self, y_pred, y_true): + return self._mlcce(y_pred, y_true) + + +class SuperLoss(nn.Module): + def __init__(self, C=10, lam=1, batch_size=128): + super(SuperLoss, self).__init__() + self.tau = math.log(C) + self.lam = lam # set to 1 for CIFAR10 and 0.25 for CIFAR100 + self.batch_size = batch_size + + def forward(self, logits, targets): + l_i = F.mse_loss(logits, targets, reduction='none').detach() + sigma = self.sigma(l_i) + loss = (F.mse_loss(logits, targets, reduction='none') - self.tau) * sigma + self.lam * ( + torch.log(sigma) ** 2) + loss = loss.sum() / self.batch_size + return loss + + def sigma(self, l_i): + x = torch.ones(l_i.size()) * (-2 / math.exp(1.)) + x = x.cuda() + y = 0.5 * torch.max(x, (l_i - self.tau) / self.lam) + y = y.cpu().numpy() + sigma = np.exp(-lambertw(y)) + sigma = sigma.real.astype(np.float32) + sigma = torch.from_numpy(sigma).cuda() + return sigma + + +def unbiased_curriculum_loss(out, data, args, epoch, epochs, scheduler='linear'): + losses = [] + scheduler = linear if scheduler == 'linear' else concave + + # calculate difficulty measurement function + adjusted_losses = [] + for idx in range(out.shape[0]): + ground_truth = max(1, abs(data[idx].item())) + loss = F.mse_loss(out[idx], data[idx]) + losses.append(loss) + adjusted_losses.append(loss.item() / ground_truth) + + mean_loss, std_loss = np.mean(adjusted_losses), np.std(adjusted_losses) + + # re-weight losses + total_loss = 0 + for i, loss in enumerate(losses): + if adjusted_losses[i] > mean_loss + 1 * std_loss: + schedule_factor = scheduler(epoch, args.epochs) + total_loss += schedule_factor * loss + else: + total_loss += loss + + return total_loss diff --git a/voxabl/main.py b/voxabl/main.py new file mode 100644 index 0000000000000000000000000000000000000000..3c551e5d7b10c352025f5464431e792bb0d01a88 --- /dev/null +++ b/voxabl/main.py @@ -0,0 +1,236 @@ +import argparse +import json +import logging +import os +import time + +from dataset import MDataset +from network import VoxPeptide +from sklearn.model_selection import KFold +from train import train +import torch +import torch.nn as nn +from torch.utils.data import Subset, DataLoader +import numpy as np +from loss import MLCE, SuperLoss, LogCoshLoss +from utils import set_seed + + +parser = argparse.ArgumentParser(description='resnet26') +# model setting +parser.add_argument('--model', type=str, default='resnet34', + help='resnet34 resnet50 densenet convnext vit swintf') +parser.add_argument('--in-channels', dest='in_channels', type=int, default=3) +parser.add_argument('--channels', type=int, default=16) + +# task & dataset setting +parser.add_argument('--pdb-src', type=str, dest='pdb_src', default='af', + help='af or hf') +parser.add_argument('--task-type', type=str, dest='task_type', default='mlc', + help='mlc or slc') +parser.add_argument('--data-ver', type=str, dest='data_ver', default='0920', + help='data version') +parser.add_argument('--task', type=str, default='all', + help='task: anti toxin anti-all mechanism anti-binary anti-regression mic') +parser.add_argument('--classes', type=int, default=6, + help='model') +parser.add_argument('--max-length', dest='max_length', type=int, default=30, + help='Max length for sequence filtering') +parser.add_argument('--split', type=int, default=5, + help="Split k fold in cross validation (default: 5)") +parser.add_argument('--seed', type=int, default=1, + help="Seed for splitting dataset (default: 1)") +parser.add_argument('--threshold', type=float, default=128, + help="MIC threshold for determine labels (default: 128)") +parser.add_argument('--exclude-channel', dest='exclude_channel', type=int, default=None) + +# training setting +parser.add_argument('--gpu', type=int, default=0, + help='GPU index to use, -1 for CPU (default: 0)') +parser.add_argument('--batch-size', type=int, dest='batch_size', default=256, + help='input batch size for training (default: 128)') +parser.add_argument('--epochs', type=int, default=10, + help='number of epochs to train (default: 50)') +parser.add_argument('--lr', type=float, default=0.001, + help='learning rate (default: 0.001)') +parser.add_argument('--decay', type=float, default=0.0005, + help='weight decay (default: 0.0005)') +parser.add_argument('--warm-steps', type=int, dest='warm_steps', default=0, + help='number of warm start steps for learning rate (default: 10)') +parser.add_argument('--patience', type=int, default=10, + help='patience for early stopping (default: 10)') +parser.add_argument('--pretrain', type=str, dest='pretrain', default='', + help='path of the pretrain model') # /home/duadua/Desktop/fetal/3dpretrain/runs/e50.pth +parser.add_argument('--metric-avg', type=str, dest='metric_avg', default='macro', + help='metric average type') + +# args for losses +parser.add_argument('--loss', type=str, default='bce', + help='loss function (mlce, sl, mix)') + +parser.add_argument('--bias-curri', dest='bias_curri', action='store_true', default=False, + help='directly use loss as the training data (biased) or not (unbiased)') +parser.add_argument('--anti-curri', dest='anti_curri', action='store_true', default=False, + help='easy to hard (curri), hard to easy (anti)') +parser.add_argument('--std-coff', dest='std_coff', type=float, default=1, + help='the hyper-parameter of std') + +args = parser.parse_args() + + +def main(): + set_seed(args.seed) + + if args.task_type == 'slc': + if args.task == 'all': + raise ValueError('Choose one task number to run single label classification') + args.classes = 1 + elif args.task_type == 'mlc': + pass + else: + raise NotImplementedError + + if args.loss == 'mlce' and args.task_type != 'slc': + criterion = MLCE() + elif args.loss == "bce" or args.task_type == 'slc': + args.loss = "bce" + criterion = nn.BCEWithLogitsLoss() + else: + criterion = 0 + pass + if args.exclude_channel is None: + args.in_channels = 4 + weight_dir = "./run/" + str(args.exclude_channel) + '-' + args.loss + str(args.batch_size) + '-' + str(args.lr) + '-' + str(args.epochs) + args.pdb_src + if not os.path.exists(weight_dir): + os.makedirs(weight_dir) + + logging.basicConfig(handlers=[ + logging.FileHandler(filename=os.path.join(weight_dir, "training.log"), encoding='utf-8', mode='w+'), + logging.StreamHandler()], + format="%(asctime)s: %(message)s", datefmt="%F %T", level=logging.INFO) + + logging.info(f'saving_dir: {weight_dir}') + + with open(os.path.join(weight_dir, "config.json"), "w") as f: + f.write(json.dumps(vars(args))) + + device = torch.device("cpu" if args.gpu == -1 or not torch.cuda.is_available() else f"cuda:{args.gpu}") + + logging.info('Loading Training Dataset') + if args.task_type == 'mlc': + set_all = MDataset(threshold=args.threshold, mode='train', max_length=args.max_length, pdb_src=args.pdb_src, + data_ver=args.data_ver, exclude_channel=args.exclude_channel) + # else: + # set_all = SDataset(threshold=args.threshold, mode='train', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + logging.info('Loading Test Dataset') + if args.task_type == 'mlc': + qlx_set = MDataset(threshold=args.threshold, mode='qlx', max_length=args.max_length, pdb_src=args.pdb_src, + exclude_channel=args.exclude_channel) + saap_set = MDataset(threshold=args.threshold, mode='saap', max_length=args.max_length, pdb_src=args.pdb_src, + exclude_channel=args.exclude_channel) + # else: + # qlx_set = SDataset(threshold=args.threshold, mode='qlx', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + # saap_set = SDataset(threshold=args.threshold, mode='saap', task=args.task, max_length=args.max_length, pdb_src=args.pdb_src) + + best_perform_list = [[] for i in range(5)] + qlx_perform_list = [[] for i in range(5)] + saap_perform_list = [[] for i in range(5)] + + kf = KFold(n_splits=5, shuffle=True, random_state=42) + + for fold, (train_idx, val_idx) in enumerate(kf.split(set_all)): + train_set= Subset(set_all, train_idx) + valid_set = Subset(set_all, val_idx) + # if args.loss == 'bce': + # weight = torch.Tensor([0] * args.classes) + # for i in train_set: + # weight += i[2] + # weight = (len(train_set) - weight) / weight + # criterion.register_buffer('pos_weight', weight.to(device)) + + train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, drop_last=True) + valid_loader = DataLoader(valid_set, batch_size=args.batch_size, shuffle=False) + qlx_loader = DataLoader(qlx_set, batch_size=1, shuffle=False) + saap_loader = DataLoader(saap_set, batch_size=1, shuffle=False) + + model = VoxPeptide(classes=set_all.num_classes, v_encoder=args.model, channels=args.channels, in_channels=args.in_channels) + if len(args.pretrain) != 0: + logging.info('loading pretrain model') + # model = load_pretrain_model(model, torch.load(args.pretrain)) + model_state = model.state_dict() + pretrained_state = torch.load(args.pretrain) + pretrained_state = {k: v for k, v in pretrained_state.items() if + k in model_state and v.size() == model_state[k].size()} + model_state.update(pretrained_state) + model.load_state_dict(model_state) + # model.load_state_dict(torch.load(args.pretrain), strict=False) + model.to(device) + # optimizer = torch.optim.Adam(model.parameters()) + optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr) + # optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=True, weight_decay=5e-5) + weights_path = f"{weight_dir}/model_{fold + 1}.pth" + # early_stopping = EarlyStopping(patience=args.patience, path=weights_path) + logging.info(f'Running Cross Validation {fold + 1}') + logging.info(f'Fold {fold + 1} Train set:{len(train_set)}, Valid set:{len(valid_set)}, Test set:qlx {len(qlx_set)} saap {len(saap_set)}') + best_metric = 0 + best_qlx = 0 + best_saap = 0 + start_time = time.time() + + for epoch in range(1, args.epochs + 1): + if args.task_type in ('mlc', 'slc') : + train_loss, ap, f1, acc, auc = train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer) + logging.info(f'Epoch: {epoch:03d}, Train Loss: {train_loss:.3f}, ap: {ap:.3f}, f1: {f1:.3f}, acc: {acc:.3f}, auc: {auc:.3f}') + avg_metric = ap + f1 + acc + auc + if avg_metric > best_metric: + logging.info(f'Epoch: {epoch:03d} New Best validation metrics, running test session') + torch.save(model.state_dict(), weights_path) + best_metric = avg_metric + best_perform_list[fold] = np.asarray([ap, f1, acc, auc]) + + _, qlx_ap, qlx_f1, qlx_acc, qlx_auc = train(args, epoch, model, None, qlx_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} QLX results, ap: {qlx_ap:.3f}, f1: {qlx_f1:.3f}, acc: {qlx_acc:.3f}, auc: {qlx_auc:.3f}') + qlx_metric = qlx_ap + qlx_f1 + qlx_acc + qlx_auc + if qlx_metric > best_qlx: + best_qlx = qlx_metric + qlx_perform_list[fold] = np.asarray([qlx_ap, qlx_f1, qlx_acc, qlx_auc]) + + _, saap_ap, saap_f1, saap_acc, saap_auc = train(args, epoch, model, None, saap_loader, device, None, None) + logging.info(f'Epoch: {epoch:03d} SAAP results, ap: {saap_ap:.3f}, f1: {saap_f1:.3f}, acc: {saap_acc:.3f}, auc: {saap_auc:.3f}') + saap_metric = saap_ap + saap_f1 + saap_acc + saap_auc + if saap_metric > best_saap: + best_saap = saap_metric + saap_perform_list[fold] = np.asarray([saap_ap, saap_f1, saap_acc, saap_auc]) + + else: + raise NotImplementedError + + logging.info(f'used time {(time.time()-start_time)/3600:.2f}h') + + logging.info(f'Cross Validation Finished!') + best_perform_list = np.asarray(best_perform_list) + qlx_perform_list = np.asarray(qlx_perform_list) + saap_perform_list = np.asarray(saap_perform_list) + logging.info('Best validation perform list\n%s', best_perform_list) + logging.info('mean: %s', np.round(np.mean(best_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(best_perform_list, 0), 3)) + logging.info('Best qlx perform list\n%s', qlx_perform_list) + logging.info('mean: %s', np.round(np.mean(qlx_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(qlx_perform_list, 0), 3)) + logging.info('Best saap perform list\n%s', saap_perform_list) + logging.info('mean: %s', np.round(np.mean(saap_perform_list, 0), 3)) + logging.info('std: %s', np.round(np.std(saap_perform_list, 0), 3)) + perform = open(weight_dir+'/result.txt', 'w') + perform.write('Valid\n') + perform.write(','.join([str(i) for i in np.mean(best_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(best_perform_list, 0)])+'\n') + perform.write('qlx\n') + perform.write(','.join([str(i) for i in np.mean(qlx_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(qlx_perform_list, 0)])+'\n') + perform.write('saap\n') + perform.write(','.join([str(i) for i in np.mean(saap_perform_list, 0)])+'\n') + perform.write(','.join([str(i) for i in np.std(saap_perform_list, 0)])+'\n') + + +if __name__ == "__main__": + main() diff --git a/voxabl/network.py b/voxabl/network.py new file mode 100644 index 0000000000000000000000000000000000000000..c643a89208689e9aad367f813c7c4bd5f4c4d9cc --- /dev/null +++ b/voxabl/network.py @@ -0,0 +1,1338 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F +from math import ceil +from mamba_ssm import Mamba + + +class Hamburger(nn.Module): + def __init__(self, inp, oup, reduction=32): + super(Hamburger, self).__init__() + self.pool_h = nn.AdaptiveAvgPool3d((1, None, None)) + self.pool_w = nn.AdaptiveAvgPool3d((None, 1, None)) + self.pool_d = nn.AdaptiveAvgPool3d((None, None, 1)) + + mip = max(8, inp // reduction) + self.conv1 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.conv2 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.conv3 = nn.Conv3d(inp, mip, kernel_size=1, stride=1, padding=0) + self.gn1 = nn.GroupNorm(8, mip) + self.gn2 = nn.GroupNorm(8, mip) + self.gn3 = nn.GroupNorm(8, mip) + + self.act = nn.LeakyReLU(0.2) + self.conv_h = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + self.conv_w = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + self.conv_d = nn.Conv3d(mip, oup, kernel_size=1, stride=1, padding=0) + + def forward(self, x): + n, c, h, w, d = x.size() + x_h = self.pool_h(x) + # print(x_h.shape) + x_w = self.pool_w(x).permute(0, 1, 3, 2, 4) + # print(x_w.shape) + x_d = self.pool_d(x).permute(0, 1, 4, 2, 3) + # print(x_d.shape) + y_hwd = torch.cat([x_h, x_w, x_d], dim=2) + # y_hd = torch.cat([x_h, x_d], dim=2) + # y_dw = torch.cat([x_d, x_w], dim=2) + y_hwd = self.conv1(y_hwd) + # y_hd = self.conv2(y_hd) + # y_dw = self.conv3(y_dw) + y_hwd = self.gn1(y_hwd) + # y_hd = self.gn2(y_hd) + # y_dw = self.gn3(y_dw) + y_hwd = self.act(y_hwd) + # y_hd = self.act(y_hd) + # y_dw = self.act(y_dw) + # print(y_hwd.shape) + x_h, x_w, x_d = torch.split(y_hwd, [1, 1, 1], dim=2) + x_w = x_w + x_h = x_h.permute(0, 1, 3, 2, 4) + x_d = x_d.permute(0, 1, 3, 4, 2) + a_h = self.conv_h(x_h).sigmoid() + a_w = self.conv_w(x_w).sigmoid() + a_d = self.conv_d(x_d).sigmoid() + a_hw = a_w * a_h + out = a_hw * a_d + return out + x + + +class BasicBlock3D(nn.Module): + expansion = 1 + + def __init__(self, in_channels, out_channels, stride=1, downsample=None): + super(BasicBlock3D, self).__init__() + self.conv1 = nn.Conv3d(in_channels, out_channels, kernel_size=3, + stride=stride, padding=1, bias=False) + self.bn1 = nn.BatchNorm3d(out_channels) + self.relu = nn.ReLU(inplace=True) + self.conv2 = nn.Conv3d(out_channels, out_channels, kernel_size=3, + stride=1, padding=1, bias=False) + self.bn2 = nn.BatchNorm3d(out_channels) + self.downsample = downsample + self.stride = stride + + def forward(self, x): + identity = 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: + identity = self.downsample(x) + + out += identity + out = self.relu(out) + + return out + +class Bottleneck3D(nn.Module): + expansion = 4 + + def __init__(self, in_channels, out_channels, stride=1, downsample=None): + super(Bottleneck3D, self).__init__() + self.conv1 = nn.Conv3d(in_channels, out_channels, kernel_size=1, bias=False) + self.bn1 = nn.BatchNorm3d(out_channels) + self.conv2 = nn.Conv3d(out_channels, out_channels, kernel_size=3, + stride=stride, padding=1, bias=False) + self.bn2 = nn.BatchNorm3d(out_channels) + self.conv3 = nn.Conv3d(out_channels, out_channels * self.expansion, + kernel_size=1, bias=False) + self.bn3 = nn.BatchNorm3d(out_channels * self.expansion) + self.relu = nn.ReLU(inplace=True) + self.downsample = downsample + self.stride = stride + + def forward(self, x): + identity = 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: + identity = self.downsample(x) + + out += identity + out = self.relu(out) + + return out + +class ResNet3D(nn.Module): + def __init__(self, block, layers, input_channels=4, base_channels=16, feature_dim=512): + super(ResNet3D, self).__init__() + self.in_channels = base_channels + + self.conv1 = nn.Conv3d(input_channels, base_channels, kernel_size=7, + stride=2, padding=3, bias=False) + self.bn1 = nn.BatchNorm3d(base_channels) + self.relu = nn.ReLU(inplace=True) + self.maxpool = nn.MaxPool3d(kernel_size=3, stride=2, padding=1) + + # 每个layer的通道数是基于 base_channels 乘以扩展因子 + self.layer1 = self._make_layer(block, base_channels, layers[0]) + self.layer2 = self._make_layer(block, base_channels * 2, layers[1], stride=2) + self.layer3 = self._make_layer(block, base_channels * 4, layers[2], stride=2) + self.layer4 = self._make_layer(block, base_channels * 8, layers[3], stride=2) + + self.avgpool = nn.AdaptiveAvgPool3d((1, 1, 1)) + self.fc = nn.Linear(base_channels * 8 * block.expansion, feature_dim) + + # 初始化权重 + self._initialize_weights() + + def _make_layer(self, block, out_channels, blocks, stride=1): + downsample = None + if stride != 1 or self.in_channels != out_channels * block.expansion: + downsample = nn.Sequential( + nn.Conv3d(self.in_channels, out_channels * block.expansion, + kernel_size=1, stride=stride, bias=False), + nn.BatchNorm3d(out_channels * block.expansion), + ) + + layers = [] + layers.append(block(self.in_channels, out_channels, stride, downsample)) + self.in_channels = out_channels * block.expansion + for _ in range(1, blocks): + layers.append(block(self.in_channels, out_channels)) + + return nn.Sequential(*layers) + + def _initialize_weights(self): + for m in self.modules(): + if isinstance(m, nn.Conv3d): + nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') + elif isinstance(m, (nn.BatchNorm3d, nn.GroupNorm)): + nn.init.constant_(m.weight, 1) + nn.init.constant_(m.bias, 0) + elif isinstance(m, nn.Linear): + nn.init.normal_(m.weight, 0, 0.01) + nn.init.constant_(m.bias, 0) + + def forward(self, x): + # Input shape: (B, C, D, H, W) + x = self.conv1(x) + x = self.bn1(x) + x = self.relu(x) + x = self.maxpool(x) + + x = self.layer1(x) # -> (B, C, D/4, H/4, W/4) + x = self.layer2(x) # -> (B, 2C, D/8, H/8, W/8) + x = self.layer3(x) # -> (B, 4C, D/16, H/16, W/16) + x = self.layer4(x) # -> (B, 8C, D/32, H/32, W/32) + + x = self.avgpool(x) # -> (B, 8C, 1, 1, 1) + x = torch.flatten(x, 1) # -> (B, 8C) + x = self.fc(x) # -> (B, feature_dim) + + return x + +def ResNet3D34(input_channels=4, base_channels=16, feature_dim=512): + """Constructs a ResNet-34 3D model.""" + return ResNet3D(BasicBlock3D, [3, 4, 6, 3], + input_channels=input_channels, + base_channels=base_channels, + feature_dim=feature_dim) + +def ResNet3D50(input_channels=4, base_channels=16, feature_dim=512): + """Constructs a ResNet-50 3D model.""" + return ResNet3D(Bottleneck3D, [3, 4, 6, 3], + input_channels=input_channels, + base_channels=base_channels, + feature_dim=feature_dim) + + +class DenseLayer3D(nn.Module): + """DenseNet3D 的基本层,包括批归一化、激活和卷积操作""" + def __init__(self, in_channels, growth_rate, bn_size=4, drop_rate=0.0): + super(DenseLayer3D, self).__init__() + self.bn1 = nn.BatchNorm3d(in_channels) + self.relu = nn.ReLU(inplace=True) + self.conv1 = nn.Conv3d(in_channels, bn_size * growth_rate, kernel_size=1, stride=1, bias=False) + + self.bn2 = nn.BatchNorm3d(bn_size * growth_rate) + self.conv2 = nn.Conv3d(bn_size * growth_rate, growth_rate, kernel_size=3, stride=1, padding=1, bias=False) + + self.drop_rate = drop_rate + + def forward(self, x): + out = self.bn1(x) + out = self.relu(out) + out = self.conv1(out) + + out = self.bn2(out) + out = self.relu(out) + out = self.conv2(out) + + if self.drop_rate > 0: + out = F.dropout3d(out, p=self.drop_rate, training=self.training) + + # 将输入和输出在通道维度上拼接 + out = torch.cat([x, out], 1) + return out + + +class DenseBlock3D(nn.Module): + """由多个 DenseLayer3D 组成的 DenseBlock""" + def __init__(self, num_layers, in_channels, growth_rate, bn_size=4, drop_rate=0.0): + super(DenseBlock3D, self).__init__() + layers = [] + for i in range(num_layers): + layers.append(DenseLayer3D( + in_channels + i * growth_rate, + growth_rate, + bn_size=bn_size, + drop_rate=drop_rate + )) + self.layer = nn.Sequential(*layers) + + def forward(self, x): + return self.layer(x) + + +class Transition3D(nn.Module): + """用于减少特征图的尺寸和通道数的过渡层""" + def __init__(self, in_channels, out_channels): + super(Transition3D, self).__init__() + self.bn = nn.BatchNorm3d(in_channels) + self.relu = nn.ReLU(inplace=True) + self.conv = nn.Conv3d(in_channels, out_channels, kernel_size=1, stride=1, bias=False) + self.pool = nn.AvgPool3d(kernel_size=2, stride=2) + + def forward(self, x): + out = self.bn(x) + out = self.relu(out) + out = self.conv(out) + out = self.pool(out) + return out + + +class DenseNet3D(nn.Module): + """DenseNet3D 模型""" + def __init__(self, input_channels=1, base_channels=64, growth_rate=32, block_layers=[3, 6, 12, 8], + bn_size=4, drop_rate=0.0, feature_dim=1024): + super(DenseNet3D, self).__init__() + self.growth_rate = growth_rate + + # 初始卷积层 + self.features = nn.Sequential( + nn.Conv3d(input_channels, base_channels, kernel_size=7, stride=2, padding=3, bias=False), + nn.BatchNorm3d(base_channels), + nn.ReLU(inplace=True), + nn.MaxPool3d(kernel_size=3, stride=2, padding=1) + ) + + # Dense Blocks 和 Transition Layers + num_features = base_channels + self.block_layers = [] + self.num_blocks = len(block_layers) + self.dense_blocks = nn.ModuleList() + self.trans_blocks = nn.ModuleList() + + for i, num_layers in enumerate(block_layers): + dense_block = DenseBlock3D( + num_layers=num_layers, + in_channels=num_features, + growth_rate=growth_rate, + bn_size=bn_size, + drop_rate=drop_rate + ) + self.dense_blocks.append(dense_block) + num_features = num_features + num_layers * growth_rate + + if i != self.num_blocks - 1: + trans_block = Transition3D( + in_channels=num_features, + out_channels=num_features // 2 + ) + self.trans_blocks.append(trans_block) + num_features = num_features // 2 + + # 最后一个 batch norm + self.bn_final = nn.BatchNorm3d(num_features) + self.relu_final = nn.ReLU(inplace=True) + + # 全局平均池化 + self.global_pool = nn.AdaptiveAvgPool3d((1, 1, 1)) + + # 分类头 + self.classifier = nn.Sequential( + nn.Flatten(), + nn.Linear(num_features, feature_dim) + ) + + # 初始化权重 + self._initialize_weights() + + def forward(self, x): + out = self.features(x) + + for i in range(self.num_blocks): + out = self.dense_blocks[i](out) + if i < self.num_blocks - 1: + out = self.trans_blocks[i](out) + + out = self.bn_final(out) + out = self.relu_final(out) + + out = self.global_pool(out) # (B, C, 1, 1, 1) + out = out.view(out.size(0), -1) # (B, C) + out = self.classifier(out) # (B, feature_dim) + return out + + def _initialize_weights(self): + """初始化权重""" + for m in self.modules(): + if isinstance(m, nn.Conv3d): + nn.init.kaiming_normal_(m.weight) + elif isinstance(m, nn.BatchNorm3d): + nn.init.constant_(m.weight, 1) + nn.init.constant_(m.bias, 0) + elif isinstance(m, nn.Linear): + nn.init.normal_(m.weight, 0, 0.01) + nn.init.constant_(m.bias, 0) + + +class DepthwiseConv3D(nn.Module): + """深度可分离卷积(3D)""" + def __init__(self, dim, kernel_size=7, padding=3): + super(DepthwiseConv3D, self).__init__() + self.dw_conv = nn.Conv3d(dim, dim, kernel_size=kernel_size, padding=padding, groups=dim, bias=False) + + def forward(self, x): + return self.dw_conv(x) + + +class ConvNeXtBlock3D(nn.Module): + """ConvNeXt 基本块(3D)""" + def __init__(self, dim, drop_path=0.0): + super(ConvNeXtBlock3D, self).__init__() + self.depthwise_conv = DepthwiseConv3D(dim) + self.norm = nn.LayerNorm(dim, eps=1e-6) + self.pointwise_conv1 = nn.Linear(dim, 4 * dim) + self.act = nn.GELU() + self.pointwise_conv2 = nn.Linear(4 * dim, dim) + self.drop_path = nn.Identity() if drop_path == 0.0 else nn.Dropout(drop_path) + + def forward(self, x): + # 输入x形状: (B, C, D, H, W) + residual = x + x = self.depthwise_conv(x) + # 转换为 (B, D, H, W, C) 以应用 LayerNorm 和 Linear + x = x.permute(0, 2, 3, 4, 1) + x = self.norm(x) + x = self.pointwise_conv1(x) + x = self.act(x) + x = self.pointwise_conv2(x) + x = self.drop_path(x) + # 转换回 (B, C, D, H, W) + x = x.permute(0, 4, 1, 2, 3) + x = residual + x # 残差连接 + return x + + +class DownSample3D(nn.Module): + """下采样层(3D)""" + def __init__(self, in_channels, out_channels): + super(DownSample3D, self).__init__() + self.layer = nn.Sequential( + nn.LayerNorm(in_channels, eps=1e-6), + nn.Conv3d(in_channels, out_channels, kernel_size=2, stride=2) + ) + + def forward(self, x): + # 输入x形状: (B, C, D, H, W) + # 需要在 LayerNorm 之前转换为 (B, D, H, W, C) + x = x.permute(0, 2, 3, 4, 1) + x = self.layer[0](x) # LayerNorm + x = x.permute(0, 4, 1, 2, 3) # 转回 (B, C, D, H, W) + x = self.layer[1](x) # Conv3d 下采样 + return x + + +class ConvNeXt3D(nn.Module): + """ConvNeXt 模型(3D)""" + def __init__(self, input_channels=3, base_channels=96, feature_dim=1024, + depths=[3, 3, 9, 3], drop_path_rate=0.1, layer_scale_init_value=1e-6): + super(ConvNeXt3D, self).__init__() + self.num_stages = len(depths) + self.drop_path_rate = drop_path_rate + + # 计算每个阶段的通道数 + self.dims = [base_channels * (2 ** i) for i in range(self.num_stages)] + + # Stem 层:卷积下采样 + self.stem = nn.Sequential( + nn.Conv3d(input_channels, self.dims[0], kernel_size=4, stride=4), + nn.LayerNorm(self.dims[0], eps=1e-6) + ) + + # 将每个阶段的块和下采样层组合 + self.stages = nn.ModuleList() + self.downsamples = nn.ModuleList() + total_blocks = sum(depths) + # 为 DropPath 计算每个块的丢弃概率 + dpr = [x.item() for x in torch.linspace(0, drop_path_rate, total_blocks)] + + block_idx = 0 + for i in range(self.num_stages): + # ConvNeXt 块 + stage = nn.Sequential( + *[ConvNeXtBlock3D(dim=self.dims[i], drop_path=dpr[block_idx + j]) for j in range(depths[i])] + ) + self.stages.append(stage) + block_idx += depths[i] + # 下采样层(除最后一个阶段外) + if i < self.num_stages - 1: + self.downsamples.append(DownSample3D(self.dims[i], self.dims[i+1])) + + # 全局池化和分类头 + self.global_pool = nn.AdaptiveAvgPool3d(1) + self.norm_head = nn.LayerNorm(self.dims[-1], eps=1e-6) + self.flatten = nn.Flatten() + self.fc = nn.Linear(self.dims[-1], feature_dim) + + def forward(self, x): + # Stem 层 + x = self.stem(x) + + # 各个阶段 + for i in range(self.num_stages): + x = self.stages[i](x) + if i < self.num_stages - 1: + x = self.downsamples[i](x) + + # 全局池化 + x = self.global_pool(x) # (B, C, 1, 1, 1) + x = x.view(x.shape[0], x.shape[1]) # (B, C) + x = self.norm_head(x) # LayerNorm + x = self.flatten(x) # (B, C) + x = self.fc(x) # (B, feature_dim) + return x + + +class PatchEmbed3D(nn.Module): + """3D Patch Embedding Layer""" + def __init__(self, input_channels=4, embed_dim=16, patch_size=(4, 8, 8)): + super(PatchEmbed3D, self).__init__() + self.patch_size = patch_size # (D, H, W) + self.proj = nn.Conv3d( + input_channels, + embed_dim, + kernel_size=patch_size, + stride=patch_size + ) + self.norm = nn.LayerNorm(embed_dim) + + def forward(self, x): + # x: (B, C, D, H, W) + x = self.proj(x) # (B, embed_dim, D', H', W') + B, C, D, H, W = x.shape + x = x.permute(0, 2, 3, 4, 1) # (B, D', H', W', C) + x = x.reshape(B, D * H * W, C) # (B, N, C), N = D'*H'*W' = (64/4)*(64/8)*(64/8)=16*8*8=1024 + x = self.norm(x) + return x # (B, N, C) + + +class TransformerEncoderLayer3D(nn.Module): + """Standard Transformer Encoder Layer for 3D ViT""" + def __init__(self, embed_dim, num_heads, mlp_ratio=4.0, drop=0.0, attn_drop=0.0): + super(TransformerEncoderLayer3D, self).__init__() + self.norm1 = nn.LayerNorm(embed_dim) + self.attn = nn.MultiheadAttention(embed_dim, num_heads, dropout=attn_drop, batch_first=True) + self.drop1 = nn.Dropout(drop) + self.norm2 = nn.LayerNorm(embed_dim) + hidden_dim = int(embed_dim * mlp_ratio) + self.mlp = nn.Sequential( + nn.Linear(embed_dim, hidden_dim), + nn.GELU(), + nn.Dropout(drop), + nn.Linear(hidden_dim, embed_dim), + nn.Dropout(drop) + ) + + def forward(self, x): + # x: (B, N, C) + x2 = self.norm1(x) + attn_output, _ = self.attn(x2, x2, x2) # attn_output: (B, N, C) + x = x + self.drop1(attn_output) + x2 = self.norm2(x) + x = x + self.mlp(x2) + return x # (B, N, C) + + +class TransformerEncoder3D(nn.Module): + """Transformer Encoder consisting of multiple TransformerEncoderLayer3D""" + def __init__(self, depth, embed_dim, num_heads, mlp_ratio=4.0, drop=0.0, attn_drop=0.0): + super(TransformerEncoder3D, self).__init__() + self.layers = nn.ModuleList([ + TransformerEncoderLayer3D(embed_dim, num_heads, mlp_ratio, drop, attn_drop) + for _ in range(depth) + ]) + self.norm = nn.LayerNorm(embed_dim) + + def forward(self, x): + for layer in self.layers: + x = layer(x) + x = self.norm(x) + return x # (B, N, C) + + +class VisionTransformer3D(nn.Module): + """3D Vision Transformer (ViT)""" + def __init__(self, input_channels=4, base_channels=16, feature_dim=512, + patch_size=(8, 8, 8), depth=4, num_heads=4, + mlp_ratio=4.0, drop_rate=0.1, attn_drop_rate=0.1): + super(VisionTransformer3D, self).__init__() + self.patch_embed = PatchEmbed3D(input_channels, base_channels, patch_size) + # Calculate number of patches + D_patch, H_patch, W_patch = patch_size + self.num_patches = (64 // D_patch) * (64 // H_patch) * (64 // W_patch) # 8*8*8=512 + + # CLS token + self.cls_token = nn.Parameter(torch.zeros(1, 1, base_channels)) + # Position Embedding + self.pos_embed = nn.Parameter(torch.zeros(1, self.num_patches + 1, base_channels)) + self.pos_drop = nn.Dropout(p=drop_rate) + + # Transformer Encoder + self.encoder = TransformerEncoder3D(depth, base_channels, num_heads, mlp_ratio, drop_rate, attn_drop_rate) + + # Classification head + self.norm_head = nn.LayerNorm(base_channels) + self.fc = nn.Linear(base_channels, feature_dim) + + self._init_weights() + + def _init_weights(self): + nn.init.trunc_normal_(self.pos_embed, std=0.02) + nn.init.trunc_normal_(self.cls_token, std=0.02) + nn.init.xavier_uniform_(self.fc.weight) + nn.init.constant_(self.fc.bias, 0) + + def forward(self, x): + # x: (B, C, D, H, W) + B = x.shape[0] + x = self.patch_embed(x) # (B, N, C) + + # Expand CLS token + cls_tokens = self.cls_token.expand(B, -1, -1) # (B, 1, C) + x = torch.cat((cls_tokens, x), dim=1) # (B, N+1, C) + x = x + self.pos_embed # (B, N+1, C) + x = self.pos_drop(x) + + x = self.encoder(x) # (B, N+1, C) + + cls_token_final = x[:, 0] # (B, C) + cls_token_final = self.norm_head(cls_token_final) # (B, C) + features = self.fc(cls_token_final) # (B, feature_dim) + return features # (B, feature_dim) + + +class SwinPatchEmbed3D(nn.Module): + """将3D输入分割成patches并进行嵌入""" + + def __init__(self, patch_size=(4, 4, 4), in_channels=4, embed_dim=16): + super(SwinPatchEmbed3D, self).__init__() + self.patch_size = patch_size + self.proj = nn.Conv3d(in_channels, embed_dim, kernel_size=patch_size, stride=patch_size) + + def forward(self, x): + # x shape: (B, C, D, H, W) + B, C, D, H, W = x.shape + x = self.proj(x) # (B, embed_dim, D/p, H/p, W/p) + x = x.flatten(2).transpose(1, 2) # (B, num_patches, embed_dim) + D_p, H_p, W_p = x.shape[1] // (H // self.patch_size[1] * W // self.patch_size[2]), H // self.patch_size[1], W // self.patch_size[2] + return x, (D_p, H_p, W_p) + + +class WindowAttention3D(nn.Module): + """3D窗口多头自注意力""" + + def __init__(self, dim, window_size=(7, 7, 7), num_heads=8, qkv_bias=True, attn_drop=0., proj_drop=0.): + super(WindowAttention3D, self).__init__() + self.dim = dim + self.window_size = window_size # (Wd, Wh, Ww) + self.num_heads = num_heads + head_dim = dim // num_heads + self.scale = head_dim ** -0.5 + + # 相对位置编码 + self.relative_position_bias_table = nn.Parameter( + torch.zeros( + (2 * window_size[0] - 1) * (2 * window_size[1] - 1) * (2 * window_size[2] - 1), + num_heads + ) + ) # 每个相对位置一个bias + + # 生成相对位置的index + coords_d = torch.arange(window_size[0]) + coords_h = torch.arange(window_size[1]) + coords_w = torch.arange(window_size[2]) + coords = torch.stack(torch.meshgrid(coords_d, coords_h, coords_w, indexing='ij')) # 3, Wd, Wh, Ww + coords_flatten = torch.flatten(coords, 1) # 3, Wd*Wh*Ww + relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :] # 3, Wd*Wh*Ww, Wd*Wh*Ww + relative_coords = relative_coords.permute(1, 2, 0).contiguous() # Wd*Wh*Ww, Wd*Wh*Ww, 3 + relative_coords += torch.tensor(self.window_size) - 1 # shift to start from 0 + relative_coords[:, :, 0] *= (2 * window_size[1] - 1) * (2 * window_size[2] - 1) + relative_coords[:, :, 1] *= (2 * window_size[2] - 1) + relative_position_index = relative_coords.sum(-1) # Wd*Wh*Ww, Wd*Wh*Ww + self.register_buffer("relative_position_index", relative_position_index) + + self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias) + self.attn_drop = nn.Dropout(attn_drop) + self.proj = nn.Linear(dim, dim) + self.proj_drop = nn.Dropout(proj_drop) + + nn.init.trunc_normal_(self.relative_position_bias_table, std=0.02) + + def forward(self, x, mask=None): + """ + x: (num_windows*B, N, C) + mask: (num_windows, N, N) or None + """ + B_, N, C = x.shape + qkv = self.qkv(x).reshape(B_, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4) + # qkv shape: (3, B_, num_heads, N, head_dim) + q, k, v = qkv[0], qkv[1], qkv[2] # each: (B_, num_heads, N, head_dim) + + q = q * self.scale + attn = (q @ k.transpose(-2, -1)) # (B_, num_heads, N, N) + + # 添加相对位置编码 + relative_position_bias = self.relative_position_bias_table[self.relative_position_index.view(-1)].view( + N, N, -1 + ) # (N, N, num_heads) + relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # (num_heads, N, N) + attn = attn + relative_position_bias.unsqueeze(0) + + if mask is not None: + nW = mask.shape[0] + attn = attn.view(B_ // nW, nW, self.num_heads, N, N) + mask.unsqueeze(1).unsqueeze(0) + attn = attn.view(-1, self.num_heads, N, N) + attn = F.softmax(attn, dim=-1) + else: + attn = F.softmax(attn, dim=-1) + + attn = self.attn_drop(attn) + out = (attn @ v) # (B_, num_heads, N, head_dim) + out = out.transpose(1, 2).reshape(B_, N, C) # (B_, N, C) + out = self.proj(out) + out = self.proj_drop(out) + return out + + +class SwinTransformerBlock3D(nn.Module): + """3D Swin Transformer Block""" + + def __init__(self, dim, num_heads, window_size=(7, 7, 7), shift_size=(0, 0, 0), + mlp_ratio=4., qkv_bias=True, drop=0., attn_drop=0., drop_path=0.): + super(SwinTransformerBlock3D, self).__init__() + self.dim = dim + self.num_heads = num_heads + self.window_size = window_size # Wd, Wh, Ww + self.shift_size = shift_size # Wd_shift, Wh_shift, Ww_shift + self.mlp_ratio = mlp_ratio + + self.norm1 = nn.LayerNorm(dim) + self.attn = WindowAttention3D(dim, window_size, num_heads, qkv_bias, attn_drop, drop) + + self.drop_path = nn.Identity() if drop_path == 0 else nn.Dropout(drop_path) + self.norm2 = nn.LayerNorm(dim) + mlp_hidden_dim = int(dim * mlp_ratio) + self.mlp = nn.Sequential( + nn.Linear(dim, mlp_hidden_dim), + nn.GELU(), + nn.Dropout(drop), + nn.Linear(mlp_hidden_dim, dim), + nn.Dropout(drop) + ) + + @staticmethod + def window_partition(x, window_size): + """ + 将输入x (B, D, H, W, C) 分割成窗口 + 返回: windows (num_windows*B, window_size D, window_size H, window_size W, C) + """ + B, D, H, W, C = x.shape + x = x.view( + B, + D // window_size[0], window_size[0], + H // window_size[1], window_size[1], + W // window_size[2], window_size[2], + C + ) + windows = x.permute(0, 1, 3, 5, 2, 4, 6, 7).contiguous().view(-1, *window_size, C) + return windows + + @staticmethod + def window_reverse(windows, window_size, B, D, H, W): + """ + 将窗口逆操作合并回原始特征图 + Input: + windows: (num_windows*B, window_size D, window_size H, window_size W, C) + window_size: tuple (Wd, Wh, Ww) + B, D, H, W: 原始体数据的维度 + Output: + x: (B, D, H, W, C) + """ + C = windows.shape[-1] + x = windows.view( + B, + D // window_size[0], + H // window_size[1], + W // window_size[2], + window_size[0], + window_size[1], + window_size[2], + C + ) + x = x.permute(0, 1, 2, 3, 4, 5, 6, 7).contiguous().view(B, D, H, W, C) + return x + + def create_attn_mask(self, input_resolution): + """创建注意力掩码,用于处理移动窗口边界""" + D, H, W = input_resolution + img_mask = torch.zeros((1, D, H, W, 1), device=self.attn.relative_position_bias_table.device) # (1, D, H, W, 1) + Wd, Wh, Ww = self.window_size + sd, sh, sw = self.shift_size + + # 计算每个区块的位置标记 + cnt = 0 + for d in (slice(0, -Wd), slice(-Wd, -sd) if sd > 0 else slice(0, None), slice(-sd, None) if sd > 0 else slice(0, None)): + for h in (slice(0, -Wh), slice(-Wh, -sh) if sh > 0 else slice(0, None), slice(-sh, None) if sh > 0 else slice(0, None)): + for w in (slice(0, -Ww), slice(-Ww, -sw) if sw > 0 else slice(0, None), slice(-sw, None) if sw > 0 else slice(0, None)): + img_mask[:, d, h, w, :] = cnt + cnt += 1 + + # 分割为窗口 + mask_windows = self.window_partition(img_mask, self.window_size) # (num_windows, Wd, Wh, Ww, 1) + mask_windows = mask_windows.view(-1, Wd * Wh * Ww) + attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2) + attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0)) + return attn_mask + + def forward(self, x, input_resolution): + """ + x: (B, N, C), N = D*H*W + input_resolution: tuple (D, H, W) + """ + D, H, W = input_resolution + B, N, C = x.shape + assert N == D * H * W, "Input has incorrect size" + + shortcut = x + x = self.norm1(x) + x = x.view(B, D, H, W, C) + + # 1. Shift + if any(s > 0 for s in self.shift_size): + shifted_x = torch.roll(x, shifts=(-self.shift_size[0], -self.shift_size[1], -self.shift_size[2]), dims=(1, 2, 3)) + else: + shifted_x = x + + # 2. Window partition + x_windows = self.window_partition(shifted_x, self.window_size) # (num_windows*B, Wd, Wh, Ww, C) + x_windows = x_windows.view(-1, self.window_size[0] * self.window_size[1] * self.window_size[2], C) # (num_windows*B, Nw, C) + + # 3. Window Multi-Head Self-Attention + if any(s > 0 for s in self.shift_size): + attn_mask = self.create_attn_mask(input_resolution) + else: + attn_mask = None + + attn_windows = self.attn(x_windows, mask=attn_mask) # (num_windows*B, Nw, C) + + # 4. Merge windows + attn_windows = attn_windows.view(-1, *self.window_size, C) + shifted_x = self.window_reverse(attn_windows, self.window_size, B, D, H, W) # (B, D, H, W, C) + + # 5. Reverse shift + if any(s > 0 for s in self.shift_size): + x = torch.roll(shifted_x, shifts=(self.shift_size[0], self.shift_size[1], self.shift_size[2]), dims=(1, 2, 3)) + else: + x = shifted_x + + x = x.view(B, D * H * W, C) + + # 6. MLP + x = shortcut + self.drop_path(x) + x = x + self.drop_path(self.mlp(self.norm2(x))) + + return x + + +class PatchMerging3D(nn.Module): + """3D Patch Merging""" + + def __init__(self, dim): + super(PatchMerging3D, self).__init__() + self.dim = dim + self.reduction = nn.Linear(dim * 8, 2 * dim, bias=False) # 2x reduction in resolution + self.norm = nn.LayerNorm(dim * 8) + + def forward(self, x, input_resolution): + # x: (B, N, C), N = D*H*W + B, N, C = x.shape + D, H, W = input_resolution + assert N == D * H * W, "Input has incorrect size" + + x = x.view(B, D, H, W, C) + + # 2x2x2_merge for D, H, W + x0 = x[:, 0::2, 0::2, 0::2, :] # (B, D/2, H/2, W/2, C) + x1 = x[:, 1::2, 0::2, 0::2, :] + x2 = x[:, 0::2, 1::2, 0::2, :] + x3 = x[:, 0::2, 0::2, 1::2, :] + x4 = x[:, 1::2, 1::2, 0::2, :] + x5 = x[:, 1::2, 0::2, 1::2, :] + x6 = x[:, 0::2, 1::2, 1::2, :] + x7 = x[:, 1::2, 1::2, 1::2, :] + x = torch.cat([x0, x1, x2, x3, x4, x5, x6, x7], -1) # (B, D/2, H/2, W/2, 8*C) + x = x.view(B, -1, 8 * C) # (B, D/2 * H/2 * W/2, 8*C) + + x = self.norm(x) + x = self.reduction(x) # (B, D/2 * H/2 * W/2, 2*C) + + return x + + +class SwinTransformerStage3D(nn.Module): + """一个Swin Transformer阶段,包括多个Block和Patch Merging""" + + def __init__(self, dim, depth, num_heads, window_size=(7, 7, 7), mlp_ratio=4., + qkv_bias=True, drop=0., attn_drop=0., drop_path=None, downsample=True): + super(SwinTransformerStage3D, self).__init__() + self.blocks = nn.ModuleList() + for i in range(depth): + shift_size = ( + window_size[0] // 2 if (i % 2 == 1 and window_size[0] > 1) else 0, + window_size[1] // 2 if (i % 2 == 1 and window_size[1] > 1) else 0, + window_size[2] // 2 if (i % 2 == 1 and window_size[2] > 1) else 0, + ) + block = SwinTransformerBlock3D( + dim=dim, + num_heads=num_heads, + window_size=window_size, + shift_size=shift_size, + mlp_ratio=mlp_ratio, + qkv_bias=qkv_bias, + drop=drop, + attn_drop=attn_drop, + drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path + ) + self.blocks.append(block) + + self.downsample = PatchMerging3D(dim) if downsample else None + + def forward(self, x, input_resolution): + """ + x: (B, N, C) + input_resolution: tuple (D, H, W) + """ + for blk in self.blocks: + x = blk(x, input_resolution) + if self.downsample: + x = self.downsample(x, input_resolution) + D, H, W = input_resolution + input_resolution = (D // 2, H // 2, W // 2) + return x, input_resolution + + +class SwinTransformer3D(nn.Module): + """3D Swin Transformer模型""" + + def __init__(self, input_channels=4, base_channels=16, feature_dim=512, + patch_size=(4, 4, 4), + depths=[2, 2, 6, 2], num_heads=[2, 4, 8, 16], + window_size=(2, 2, 2), mlp_ratio=4., qkv_bias=True, + drop_rate=0., attn_drop_rate=0., drop_path_rate=0.1): + super(SwinTransformer3D, self).__init__() + self.num_layers = len(depths) + self.embed_dim = base_channels + self.patch_size = patch_size + self.patch_embed = SwinPatchEmbed3D(patch_size, in_channels=input_channels, embed_dim=self.embed_dim) + + # 计算drop path率 + total_depth = sum(depths) + dpr = [x.item() for x in torch.linspace(0, drop_path_rate, total_depth)] + + self.layers = nn.ModuleList() + current_depth = 0 + for i_layer in range(self.num_layers): + layer = SwinTransformerStage3D( + dim=base_channels * 2**i_layer, + depth=depths[i_layer], + num_heads=num_heads[i_layer], + window_size=window_size, + mlp_ratio=mlp_ratio, + qkv_bias=qkv_bias, + drop=drop_rate, + attn_drop=attn_drop_rate, + drop_path=dpr[current_depth:current_depth + depths[i_layer]], + downsample=(i_layer < self.num_layers - 1) + ) + self.layers.append(layer) + current_depth += depths[i_layer] + + self.norm = nn.LayerNorm(base_channels * 2**(self.num_layers - 1)) + self.avgpool = nn.AdaptiveAvgPool1d(1) + self.head = nn.Linear(base_channels * 2**(self.num_layers - 1), feature_dim) + + def forward_features(self, x): + """ + x: (B, C, D, H, W) + """ + x, input_resolution = self.patch_embed(x) # (B, num_patches, embed_dim), (D_p, H_p, W_p) + for layer in self.layers: + x, input_resolution = layer(x, input_resolution) + x = self.norm(x) # (B, num_patches, dim) + return x + + def forward(self, x): + """ + x: (B, C, D, H, W) + """ + x = self.forward_features(x) # (B, num_patches, dim) + x = x.transpose(1, 2) # (B, dim, num_patches) + x = self.avgpool(x).squeeze(-1) # (B, dim) + x = self.head(x) # (B, feature_dim) + return x + + +class VoxPeptide(nn.Module): + def __init__(self, v_encoder='resnet34', classes=6, channels=16, in_channels=4): + super().__init__() + self.classes = classes + + if v_encoder == 'resnet34': + self.v_encoder = ResNet3D34(input_channels=in_channels, base_channels=channels, feature_dim=512) + elif v_encoder == 'resnet50': + self.v_encoder = ResNet3D50(input_channels=in_channels, base_channels=channels, feature_dim=512) + elif v_encoder == 'densenet': + self.v_encoder = DenseNet3D(input_channels=in_channels, base_channels=channels, growth_rate=16 if channels < 48 else 32, feature_dim=512) + elif v_encoder == 'convnext': + self.v_encoder = ConvNeXt3D(input_channels=in_channels, base_channels=channels, feature_dim=512) + elif v_encoder == 'vit': + self.v_encoder = VisionTransformer3D(input_channels=in_channels, base_channels=channels, feature_dim=512) + elif v_encoder == 'swintf': + self.v_encoder = SwinTransformer3D(input_channels=in_channels, base_channels=channels, feature_dim=512) + else: + raise NotImplementedError(f'\'{v_encoder}\' not implemented') + + self.vox_fc = nn.Linear(512, classes) + + def forward(self, x): + vox, seq = x + seq_emb = self.v_encoder(vox) + pred = self.vox_fc(seq_emb) + return pred + + +class PositionalEncoding(nn.Module): + def __init__(self, d_model, max_len=50): + super(PositionalEncoding, self).__init__() + + pe = torch.zeros(max_len, d_model) # (max_len, d_model) + position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1) # (max_len, 1) + div_term = torch.exp(torch.arange(0, d_model, 2).float() * + (-torch.log(torch.FloatTensor([10000.0])) / d_model)) # (d_model/2,) + pe[:, 0::2] = torch.sin(position * div_term) # 偶数维 + pe[:, 1::2] = torch.cos(position * div_term) # 奇数维 + pe = pe.unsqueeze(0) # (1, max_len, d_model) + self.register_buffer('pe', pe) + + def forward(self, x): + """ + x: (B, N, d_model) + """ + x = x + self.pe[:, :x.size(1), :] + return x + + +class TransformerModel(nn.Module): + def __init__(self, nheads, d_model, num_layers, out_dim, max_length=50): + super(TransformerModel, self).__init__() + + # 嵌入层,将输入从 (B, N) 转换到 (B, N, embed_dim) + self.embedding = nn.Linear(1, d_model) + + # 位置编码 + self.pos_encoder = PositionalEncoding(d_model, max_length) + + # Transformer 编码器层 + encoder_layers = nn.TransformerEncoderLayer(d_model=d_model, + nhead=nheads, + activation='relu') + self.transformer_encoder = nn.TransformerEncoder(encoder_layers, num_layers=num_layers) + + # 全局池化(可以根据任务选择不同的聚合方式) + self.global_pool = nn.AdaptiveAvgPool1d(1) + + # 输出层 + self.fc = nn.Linear(d_model, out_dim) + + def forward(self, src): + """ + src: (B, N) + """ + # 嵌入 + embedded = self.embedding(src.unsqueeze(-1)) # (B, N, embed_dim) + embedded = self.pos_encoder(embedded) # 添加位置编码 + + # 转置以适应 Transformer (N, B, embed_dim) + embedded = embedded.permute(1, 0, 2) + + # Transformer 编码 + transformer_out = self.transformer_encoder(embedded) # (N, B, embed_dim) + + # 转置回 (B, N, embed_dim) + transformer_out = transformer_out.permute(1, 0, 2) + + # 全局池化,将 (B, N, embed_dim) 转换为 (B, embed_dim) + pooled = self.global_pool(transformer_out.permute(0, 2, 1)).squeeze(-1) + + # 输出层 + output = self.fc(pooled) # (B, output_dim) + + return output + + +class MambaModel(nn.Module): + def __init__(self, d_model, out_dim, max_length=30): + super(MambaModel, self).__init__() + self.linear = nn.Linear(in_features=1, out_features=d_model) + self.pos_encoder = PositionalEncoding(d_model, max_length) + self.mamba = Mamba(d_model=d_model) + self.global_pool = nn.AdaptiveAvgPool1d(1) + self.fc = nn.Linear(d_model * 2, out_dim) + + def forward(self, x: torch.Tensor): + x = self.pos_encoder(self.linear(x.unsqueeze(-1))) + y = self.mamba(x) + y_flip = self.mamba(x.flip([-2])).flip([-2]) + y = torch.cat((y, y_flip), dim=-1) + y = self.fc(self.global_pool(y.permute(0, 2, 1)).squeeze(-1)) + return y + + +class SEQ(nn.Module): + def __init__(self, seq_type='mlp', input_dim=21, hidden_dim=128, out_dim=128, num_layers=2, max_length=30): + super(SEQ, self).__init__() + self.seq_type = seq_type + if seq_type == 'rnn': + self.rnn = nn.RNN( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'gru': + self.rnn = nn.GRU( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'lstm': + self.rnn = nn.LSTM( + input_size=input_dim, + hidden_size=hidden_dim, + num_layers=num_layers, + batch_first=True, # input & output will take batch size as 1 dim (batch, time_step, input_size) + bidirectional=True + ) + elif seq_type == 'tf': + self.transformer = TransformerModel(nheads=4, d_model=hidden_dim, num_layers=2, out_dim=out_dim, max_length=max_length) + elif seq_type == 'mamba': + self.mamba = MambaModel(d_model=hidden_dim, out_dim=out_dim, max_length=max_length) + else: + # nn.Linear(50, 50, bias=False), nn.ReLU(), + self.rnn = nn.Sequential(nn.Linear(max_length, hidden_dim * 4), nn.ReLU(), nn.Linear(hidden_dim * 4, out_dim)) + self.rnn_fc = nn.Sequential( + nn.Linear(hidden_dim * 2, hidden_dim), + nn.ReLU(), + nn.Linear(hidden_dim, out_dim) + ) + + def forward(self, seq): + if self.seq_type == 'mlp': + return self.rnn(seq.squeeze(1)) + elif self.seq_type == 'tf': + return self.transformer(seq) + elif self.seq_type == 'mamba': + return self.mamba(seq) + else: + one_hot_seq = F.one_hot(seq.to(torch.int64), num_classes=21).float() + r_out = self.rnn(one_hot_seq, None)[0] # None represents zero initial hidden state + out = self.rnn_fc(r_out[:, -1, :]) + return out + # def forward(self, x, seq_lengths): + # + + +class SEQPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + self.classes = classes + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + + self.seq_fc = nn.Linear(128, classes) + + def forward(self, x, seq_lengths=None): + vox, seq = x + seq_emb = self.q_encoder(seq) + pred = self.seq_fc(seq_emb) + return pred + + +class ConvNet(nn.Module): + def __init__(self, num_classes: int = 2): + super(ConvNet, self).__init__() + self.conv1 = nn.Conv1d(3, 16, 3, padding=1) + self.conv2 = nn.Conv1d(16, 32, 3, padding=1) + self.fc1 = nn.Linear(32 * 7, 128) + # self.fc2 = nn.Linear(128, num_classes) + + def forward(self, x): + # print(x.shape) + x = x.permute(0, 2, 1) + x = F.relu(self.conv1(x)) + x = F.max_pool1d(x, 2) + x = F.relu(self.conv2(x)) + x = F.max_pool1d(x, 2) + x = x.view(x.shape[0], -1) + return self.fc1(x) + # x = F.relu(self.fc1(x)) + # x = self.fc2(x) + # return x + + +class ConvNet2D(nn.Module): + def __init__(self, num_classes: int = 2): + super(ConvNet2D, self).__init__() + self.conv1 = nn.Conv2d(1, 8, 3, padding=1) + self.conv2 = nn.Conv2d(8, 16, 3, padding=2, stride=2) + self.conv3 = nn.Conv2d(16, 32, 3, padding=2, stride=2) + # self.pool = nn.AdaptiveAvgPool2d(32) + self.fc1 = nn.Linear(32 * 3 * 9, 128) + # self.fc2 = nn.Linear(128, num_classes) + + def forward(self, x): + # print(x.shape) + x = x.unsqueeze(1) + x = F.relu(self.conv1(x)) + # print(x.shape) + x = F.relu(self.conv2(x)) + # print(x.shape) + x = F.relu(self.conv3(x)) + # x = self.pool(x) + # print(x.shape) + x = x.view(x.shape[0], -1) + return self.fc1(x) + + +# convnet = ConvNet() +# print(convnet) +class MMPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + if attention == 'hamburger': + self.attention = Hamburger(2048, 2048) + else: + self.attention = None + # v_encoder could be resnet26 or resnet50 + if v_encoder == 'resnet26': + self.v_encoder = ResNet3D(Bottleneck3D, [1, 2, 4, 1], self.attention) + # self.v_encoder = SwinUNETR(img_size=(64, 64, 64), in_channels=3, out_channels=1) + elif v_encoder == 'resnet50': + self.v_encoder = ResNet3D(Bottleneck3D, [3, 4, 6, 3], self.attention) + else: + raise NotImplementedError + + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + # self.ss_encoder = SEQ(seq_type=q_encoder) + if fusion == 'mlp': + self.fusion = nn.Linear(512 * 4 + 256, 256) + # self.fusion = nn.Linear(192 + 256, classes) + elif fusion == 'att': + self.fusion = nn.Linear(512 * 4 + 256, 256) + else: + raise NotImplementedError + + # self.vox_fc = nn.Linear(2048, classes) + # self.seq_fc = nn.Linear(256, classes) + self.out = nn.Sequential(nn.ReLU(inplace=True), nn.Linear(256, classes)) + self.classes = classes + + def forward(self, x, seq_lengths=None): + vox, seq = x + # print(vox.shape) + # print(seq.shape) + vox_emb = self.v_encoder(vox) + # print(vox_emb.shape) + seq_emb = self.q_encoder(seq, seq_lengths) + # print(seq_emb.shape) + # ss_emb = self.ss_encoder(second_s) + fused_feature = torch.cat((seq_emb, vox_emb), dim=1) + pred = self.fusion(fused_feature) + pred = self.out(pred) + # pred1 = self.vox_fc(vox_emb) + # pred2 = self.seq_fc(seq_emb) + # return pred, fused_feature + return pred + + +class SMPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, hidden_dim=256, max_length=30): + super().__init__() + self.siamese_encoder1 = MMPeptide(v_encoder, q_encoder, fusion, classes, attention, max_length) + # self.siamese_encoder2 = MMPeptide(v_encoder, q_encoder, fusion, classes, attention) + self.fc = nn.Sequential( + nn.Linear(hidden_dim * 2, hidden_dim), + nn.ReLU(), + nn.Linear(hidden_dim, 1) + ) + + def forward(self, x, seq_lengths=None): + f_mutated = self.siamese_encoder1(x[0]) + f_wide_type = self.siamese_encoder1(x[1]) + return self.fc(torch.cat((f_mutated, f_wide_type), dim=1)) + + +class MMFPeptide(nn.Module): + def __init__(self, v_encoder='resnet26', q_encoder='mlp', fusion='mlp', classes=6, attention=None, max_length=30): + super().__init__() + if attention == 'hamburger': + self.attention = Hamburger(2048, 2048) + else: + self.attention = None + # v_encoder could be resnet26 or resnet50 + if v_encoder == 'resnet26': + self.v_encoder = ResNet3D(Bottleneck3D, [1, 2, 4, 1], self.attention) + # self.v_encoder = ResNet3DFusion(Bottleneck, [1, 2, 4, 1], self.attention) + elif v_encoder == 'resnet50': + self.v_encoder = ResNet3D(Bottleneck3D, [3, 4, 6, 3], self.attention) + else: + raise NotImplementedError + + # q_encoder could be mlp, gru, rnn, lstm, transformer + self.q_encoder = SEQ(seq_type=q_encoder, max_length=max_length) + + if fusion == 'mlp': + self.fusion = nn.Linear(512 * 4 + 256, classes) + elif fusion == 'att': + self.fusion = nn.Linear(512 * 4 + 256, classes) + else: + raise NotImplementedError + + self.vox_fc = nn.Linear(2048, classes) + self.seq_fc = nn.Linear(256, classes) + + def forward(self, x, seq_lengths=None): + vox, seq = x + # print(vox.shape) + # print(seq.shape) + seq_emb = self.q_encoder(seq, seq_lengths) + + vox_emb = self.v_encoder(vox, seq_emb) + # print(vox_emb.shape) + # print(seq_emb.shape) + fused_feature = torch.cat((seq_emb, vox_emb), dim=1) + pred = self.fusion(fused_feature) + # pred1 = self.vox_fc(vox_emb) + # pred2 = self.seq_fc(seq_emb) + return pred + + +if __name__ == "__main__": + # model = MMFPeptide() + # voxel = torch.zeros((4, 3, 64, 64, 64)) + # # # h_in = torch.zeros((2, 2048, 2, 2, 2)) + # # # h = Hamburger(2048, 2048) + # # # h(h_in) + # seq = torch.ones((4, 50)) + # res = model.forward((voxel, seq)) + # out = model((voxel, seq)) + # print(out.shape) + # model = ConvNet2D() + input_seq = torch.ones((4, 1, 30)) + # model(input_seq) + transformer = TransformerModel(nhead=4, d_model=32, num_layers=2) + print(transformer(input_seq).shape) diff --git a/voxabl/run/0-bce256-0.001-10af/config.json b/voxabl/run/0-bce256-0.001-10af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..490604d469d8bd9ac38c01a819346c7c3d028b4a --- /dev/null +++ b/voxabl/run/0-bce256-0.001-10af/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "in_channels": 3, "channels": 16, "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "exclude_channel": 0, "gpu": 0, "batch_size": 256, "epochs": 10, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/voxabl/run/0-bce256-0.001-10af/model_1.pth b/voxabl/run/0-bce256-0.001-10af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..6723946e6fe9f275a09c2777dab23a9953579ef9 --- /dev/null +++ b/voxabl/run/0-bce256-0.001-10af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3c3a467df0b0b9567fccb18447682c0d31caecc7a57162ca2150f79b7a2096db +size 16309562 diff --git a/voxabl/run/0-bce256-0.001-10af/model_2.pth b/voxabl/run/0-bce256-0.001-10af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..d7ef05b1577cea5e008fe83c75fc3fca2002cb2b --- /dev/null +++ b/voxabl/run/0-bce256-0.001-10af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fdcdf3a99afaffb215613295f9581fa63129a240a369a646e50120774760dd46 +size 16309562 diff --git a/voxabl/run/0-bce256-0.001-10af/model_3.pth b/voxabl/run/0-bce256-0.001-10af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..6fdc7e038ad9c82bc6d90522975669342ef7f0e6 --- /dev/null +++ b/voxabl/run/0-bce256-0.001-10af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:22bb28e86cf77019aad051376261804859c7b09431ff2a8c9aa87c2a72b4666b +size 16309562 diff --git a/voxabl/run/0-bce256-0.001-10af/model_4.pth b/voxabl/run/0-bce256-0.001-10af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..f207b356bba1e1891bda20846390da3f29c0211d --- /dev/null +++ b/voxabl/run/0-bce256-0.001-10af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ed937d12a25092e4e0a2cccb654ef9bce1d624e72303438ab73a69393b82a8ca +size 16309562 diff --git a/voxabl/run/0-bce256-0.001-10af/model_5.pth b/voxabl/run/0-bce256-0.001-10af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..3d5c72103890df6f444cdd7c63ea4f04cf6947a3 --- /dev/null +++ b/voxabl/run/0-bce256-0.001-10af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e730f0c9c328c321a25d087b09607b5346516e140ec49fcd8b490ee6d60f4ba2 +size 16309562 diff --git a/voxabl/run/0-bce256-0.001-10af/result.txt b/voxabl/run/0-bce256-0.001-10af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..5f7b8500682fd88d8d21324943bb2c721c54dfbf --- /dev/null +++ b/voxabl/run/0-bce256-0.001-10af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8218082427978516,0.8002940654754639,0.7230491399765014,0.755567216873169 +0.0095470934015053,0.0059161697510239164,0.013522313774473464,0.012082846732023 +qlx +0.7430623650550843,0.7106038451194763,0.6238410472869873,0.7267000079154968 +0.03158853314798018,0.01131539297646625,0.020297065014030592,0.03299290309651308 +saap +0.4356351852416992,0.5104583978652955,0.4073333263397217,0.5273991167545319 +0.017492316623782256,0.003873284513219544,0.03047039165628022,0.033751317770177265 diff --git a/voxabl/run/1-bce256-0.001-10af/config.json b/voxabl/run/1-bce256-0.001-10af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..6021c68cc2f4d3a87dd7b299959e8beb4778a63f --- /dev/null +++ b/voxabl/run/1-bce256-0.001-10af/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "in_channels": 3, "channels": 16, "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "exclude_channel": 1, "gpu": 1, "batch_size": 256, "epochs": 10, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/voxabl/run/1-bce256-0.001-10af/model_1.pth b/voxabl/run/1-bce256-0.001-10af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..d9279bef0072db66dc7237ba2971fce3a447f165 --- /dev/null +++ b/voxabl/run/1-bce256-0.001-10af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f8d79a9d10c9bd02c3772d7c38f7ad9f07d936dbe9f07212ad6759fa2175f638 +size 16309562 diff --git a/voxabl/run/1-bce256-0.001-10af/model_2.pth b/voxabl/run/1-bce256-0.001-10af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..15e8cef374344efcd386998d35baf0a624cff3b6 --- /dev/null +++ b/voxabl/run/1-bce256-0.001-10af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9b9420b53c9dd1fc57e70b0e2c890f4f0752d47c5b12f815e79b13877a26465f +size 16309562 diff --git a/voxabl/run/1-bce256-0.001-10af/model_3.pth b/voxabl/run/1-bce256-0.001-10af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..a0db18e08c94640d469c0c8e89470d3db884eff2 --- /dev/null +++ b/voxabl/run/1-bce256-0.001-10af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4d509e2b0081a3350c72b72a52306b26ef7c87a9f04368cbd3ea8070daa3caef +size 16309562 diff --git a/voxabl/run/1-bce256-0.001-10af/model_4.pth b/voxabl/run/1-bce256-0.001-10af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..7e05208e48049af8f52e7863b3578667982048d6 --- /dev/null +++ b/voxabl/run/1-bce256-0.001-10af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9dbc59cbbf1c8e859b06e40ee6e1840616672f1fc1595480dffc408d234c91ab +size 16309562 diff --git a/voxabl/run/1-bce256-0.001-10af/model_5.pth b/voxabl/run/1-bce256-0.001-10af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..852f22eca09e731b1aace8700f78319c8b5143d3 --- /dev/null +++ b/voxabl/run/1-bce256-0.001-10af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8e8ef4b01b97ae6962e4bf38cd4a330ee036b6056271aaab34bb15edd989a66a +size 16309562 diff --git a/voxabl/run/1-bce256-0.001-10af/result.txt b/voxabl/run/1-bce256-0.001-10af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..0364d61cbcf90015c9d32c47ad4842127348ae01 --- /dev/null +++ b/voxabl/run/1-bce256-0.001-10af/result.txt @@ -0,0 +1,9 @@ +Valid +0.803332257270813,0.7971772789955139,0.7145976305007935,0.739523422718048 +0.012179648988521013,0.008238942210757204,0.011168450340468575,0.01068392832203007 +qlx +0.6946816563606262,0.6688870191574097,0.5792494535446167,0.6608845233917237 +0.04039728745415219,0.022117968280353097,0.025662206801961546,0.04744562077577602 +saap +0.46133403182029725,0.49143685698509215,0.40433332324028015,0.5198772609233856 +0.02049845525204666,0.03787643842237774,0.019020457068714827,0.046830421737563466 diff --git a/voxabl/run/2-bce256-0.001-10af/config.json b/voxabl/run/2-bce256-0.001-10af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..f223a5027aca2b6f8a367437681a0bd2cf60087c --- /dev/null +++ b/voxabl/run/2-bce256-0.001-10af/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "in_channels": 3, "channels": 16, "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "exclude_channel": 2, "gpu": 0, "batch_size": 256, "epochs": 10, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/voxabl/run/2-bce256-0.001-10af/model_1.pth b/voxabl/run/2-bce256-0.001-10af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..a3189bb3458d78dee1e4785742cc43c2a3203aa0 --- /dev/null +++ b/voxabl/run/2-bce256-0.001-10af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3bf9d8e2caa8c618b49f7f9e336ba5fca9442c6a1cbe6c805b485751008b65c9 +size 16309562 diff --git a/voxabl/run/2-bce256-0.001-10af/model_2.pth b/voxabl/run/2-bce256-0.001-10af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..97b285a8b3acf9fd19b901e4de09bbc0568796fe --- /dev/null +++ b/voxabl/run/2-bce256-0.001-10af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:979edd468a7e9aa9d00e7ba38071d2ac29e0a7b53a1bf50c8bdc788c628eeb49 +size 16309562 diff --git a/voxabl/run/2-bce256-0.001-10af/model_3.pth b/voxabl/run/2-bce256-0.001-10af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..fe6648a40134528c0b47743d94e1d49cef512ed1 --- /dev/null +++ b/voxabl/run/2-bce256-0.001-10af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:76109aca7cc555772b4297f5697ebc9da0b9887cab397e15c2b23a76d1b2d2a7 +size 16309562 diff --git a/voxabl/run/2-bce256-0.001-10af/model_4.pth b/voxabl/run/2-bce256-0.001-10af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..d9a97648ff65e3542fbca5aaddc96cb1775ab0f8 --- /dev/null +++ b/voxabl/run/2-bce256-0.001-10af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0b1db627e7fa02788747d9619730a6025d334b58b1bab8d9e15c929558790465 +size 16309562 diff --git a/voxabl/run/2-bce256-0.001-10af/model_5.pth b/voxabl/run/2-bce256-0.001-10af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..b1159c9dcdf0b942e40b87d2dd916f36939eac8f --- /dev/null +++ b/voxabl/run/2-bce256-0.001-10af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fb8f88781234bfc578bc8bc1d463cf5aea5dbfe409fa8b3dfbebbda7dd291754 +size 16309562 diff --git a/voxabl/run/2-bce256-0.001-10af/result.txt b/voxabl/run/2-bce256-0.001-10af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..d45148d117fe26e52b00d04cb407226a66690037 --- /dev/null +++ b/voxabl/run/2-bce256-0.001-10af/result.txt @@ -0,0 +1,9 @@ +Valid +0.8056214928627015,0.7909632682800293,0.6892090678215027,0.7271499991416931 +0.010747766171866145,0.009895288268957659,0.017090126090978427,0.0139955571123481 +qlx +0.7636130690574646,0.7007147550582886,0.6108167767524719,0.7425684690475464 +0.024556689876874406,0.008721644033219685,0.01892026876580081,0.027924032456986325 +saap +0.44070292115211485,0.5129449367523193,0.38499999046325684,0.4919934332370758 +0.02690371145020462,0.0,0.0,0.044959057628275234 diff --git a/voxabl/run/3-bce256-0.001-10af/config.json b/voxabl/run/3-bce256-0.001-10af/config.json new file mode 100644 index 0000000000000000000000000000000000000000..6dbdc5f11b34832275e7b9817d3f2e62a6af2bb3 --- /dev/null +++ b/voxabl/run/3-bce256-0.001-10af/config.json @@ -0,0 +1 @@ +{"model": "resnet34", "in_channels": 3, "channels": 16, "pdb_src": "af", "task_type": "mlc", "data_ver": "0920", "task": "all", "classes": 6, "max_length": 30, "split": 5, "seed": 1, "threshold": 128, "exclude_channel": 3, "gpu": 1, "batch_size": 256, "epochs": 10, "lr": 0.001, "decay": 0.0005, "warm_steps": 0, "patience": 10, "pretrain": "", "metric_avg": "macro", "loss": "bce", "bias_curri": false, "anti_curri": false, "std_coff": 1} \ No newline at end of file diff --git a/voxabl/run/3-bce256-0.001-10af/model_1.pth b/voxabl/run/3-bce256-0.001-10af/model_1.pth new file mode 100644 index 0000000000000000000000000000000000000000..a8d53e63e6824ba0bbd45b82b8df1e875fc3763c --- /dev/null +++ b/voxabl/run/3-bce256-0.001-10af/model_1.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6deeeec7a272e597f525e5e0e411a2b995d442d0940992c2aa06f9115d77f396 +size 16309562 diff --git a/voxabl/run/3-bce256-0.001-10af/model_2.pth b/voxabl/run/3-bce256-0.001-10af/model_2.pth new file mode 100644 index 0000000000000000000000000000000000000000..0edf84d665a917204cdcaaeff76756dec3a46297 --- /dev/null +++ b/voxabl/run/3-bce256-0.001-10af/model_2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a0251ec9b749952c6e9ff1e3faf3468f7269962e3c45da942bf328a95a13eba9 +size 16309562 diff --git a/voxabl/run/3-bce256-0.001-10af/model_3.pth b/voxabl/run/3-bce256-0.001-10af/model_3.pth new file mode 100644 index 0000000000000000000000000000000000000000..ed46b19ddc82a84099b944e01218743ed8de312d --- /dev/null +++ b/voxabl/run/3-bce256-0.001-10af/model_3.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cc637675c9ae11751ab6d5f7684a9da9ab17ef7fa51e8a827716e2e9465b7c3d +size 16309562 diff --git a/voxabl/run/3-bce256-0.001-10af/model_4.pth b/voxabl/run/3-bce256-0.001-10af/model_4.pth new file mode 100644 index 0000000000000000000000000000000000000000..dc9f5a158690d5b9096c7beefca206ea290581e4 --- /dev/null +++ b/voxabl/run/3-bce256-0.001-10af/model_4.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a480ca0156a8c1190b083c45d4121474f4f78ff5be6f8797d353c5a5982e6644 +size 16309562 diff --git a/voxabl/run/3-bce256-0.001-10af/model_5.pth b/voxabl/run/3-bce256-0.001-10af/model_5.pth new file mode 100644 index 0000000000000000000000000000000000000000..035a000622f47ce54b4bb016a203603d28d7e361 --- /dev/null +++ b/voxabl/run/3-bce256-0.001-10af/model_5.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:901c7ed5127bc429cdbe9c40d46860a4b0bd81e15d50db1f6f4ee2c7d377c46c +size 16309562 diff --git a/voxabl/run/3-bce256-0.001-10af/result.txt b/voxabl/run/3-bce256-0.001-10af/result.txt new file mode 100644 index 0000000000000000000000000000000000000000..20d74c8d0f92d3f815366feb57df25fd3f67edd0 --- /dev/null +++ b/voxabl/run/3-bce256-0.001-10af/result.txt @@ -0,0 +1,9 @@ +Valid +0.830552613735199,0.7927715420722962,0.7227811098098755,0.7627143859863281 +0.008097691669649144,0.009201773005051043,0.012475553389977575,0.010107358646421248 +qlx +0.7329757928848266,0.7016539573669434,0.614128029346466,0.7149881839752197 +0.008214818512145968,0.014466567994402074,0.023904354258766792,0.011745686854732644 +saap +0.4826329231262207,0.4937749087810516,0.4696666657924652,0.5737005352973938 +0.035031623550308214,0.020815276355059923,0.059014121200314545,0.028946403135745324 diff --git a/voxabl/summary.py b/voxabl/summary.py new file mode 100644 index 0000000000000000000000000000000000000000..420cc50d5a8330d07342a07c2f5bfc482023480a --- /dev/null +++ b/voxabl/summary.py @@ -0,0 +1,59 @@ +import os +import csv +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument('--dir', type=str, default='./run') +parser.add_argument('--output', type=str, default='summary.csv') +args = parser.parse_args() + +# 设置目录路径和输出CSV文件名 +directory_path = args.dir +output_csv = args.output + +# 创建CSV文件并写入数据 +with open(output_csv, mode='w', newline='') as file: + writer = csv.writer(file) + + # 写入第一个子表的标题和表头 + writer.writerow(['QLX']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 6: + means_5_6 = lines[4].strip().split(',') + std_devs_5_6 = lines[5].strip().split(',') + if len(means_5_6) == 4 and len(std_devs_5_6) == 4: + values_5_6 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_5_6, std_devs_5_6)] + writer.writerow([subdirectory] + values_5_6) + + # 加入空行以分隔两个子表 + writer.writerow([]) + + # 写入第二个子表的标题和表头 + writer.writerow(['SAAP']) + writer.writerow(['Run', 'AP', 'F1', 'ACC', 'AUC']) + + # 再次遍历指定目录中的子目录 + for subdirectory in os.listdir(directory_path): + subdirectory_path = os.path.join(directory_path, subdirectory) + if os.path.isdir(subdirectory_path): + result_file_path = os.path.join(subdirectory_path, 'result.txt') + if os.path.isfile(result_file_path): + with open(result_file_path, 'r') as result_file: + lines = result_file.readlines() + if len(lines) >= 9: + means_8_9 = lines[7].strip().split(',') + std_devs_8_9 = lines[8].strip().split(',') + if len(means_8_9) == 4 and len(std_devs_8_9) == 4: + values_8_9 = [f'{float(mean.strip()) * 100:.2f}$_{{\\pm{{{float(std_dev.strip()) * 100:.2f}}}}}$' + for mean, std_dev in zip(means_8_9, std_devs_8_9)] + writer.writerow([subdirectory] + values_8_9) \ No newline at end of file diff --git a/voxabl/train.py b/voxabl/train.py new file mode 100644 index 0000000000000000000000000000000000000000..0204d2cc203fcf064ff5307d7c35ba1ff1cfaec3 --- /dev/null +++ b/voxabl/train.py @@ -0,0 +1,61 @@ +import torch +from torchmetrics import F1Score, Accuracy, AveragePrecision, AUROC + + +def train(args, epoch, model, train_loader, valid_loader, device, criterion, optimizer): + train_loss = 0 + num_labels = model.classes + avg = args.metric_avg + if num_labels == 1: + task = 'binary' + else: + task = 'multilabel' + metric_macro_acc = Accuracy(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_f1 = F1Score(average=avg, task=task, num_labels=num_labels, threshold=0.5).to(device) + metric_macro_ap = AveragePrecision(average=avg, task=task, num_labels=num_labels).to(device) + metric_auc = AUROC(average=avg, task=task, num_labels=num_labels).to(device) + + if train_loader is not None: + model.train() + for data in train_loader: + voxel, seq, gt = data + # print(seq_lengths) + out = model((voxel.to(device), seq.to(device))) + # print(out[0]) + # print(gt[0]) + loss = criterion(out, gt.to(device).float()) + # loss_0 = criterion(out[0], gt.to(device).float()) + # loss_1 = criterion(out[1], gt.to(device).float()) + # loss_2 = criterion(out[2], gt.to(device).float()) + # loss = loss_0 + loss_1 + loss_2 + loss.backward() + optimizer.step() + optimizer.zero_grad() + train_loss += loss.item() + + model.eval() + preds = [] + gt_list_valid = [] + with torch.no_grad(): + for data in valid_loader: + voxel, seq, gt = data + gt_list_valid.append(gt.to(device)) + out = model((voxel.to(device), seq.to(device))) + preds.append(out) + + # calculate metrics + preds = torch.nn.functional.sigmoid(torch.cat(preds, dim=0)) + gt_list_valid = torch.cat(gt_list_valid, dim=0).int() + # if train_loader is None: + # print(preds) + # print(preds.shape) + # print(gt_list_valid) + # print(gt_list_valid.shape) + + macro_ap = metric_macro_ap(preds, gt_list_valid).item() + # class_ap = [round(i.item(), 5) for i in metric_class_ap(preds, gt_list_valid)] + macro_auc = metric_auc(preds, gt_list_valid).item() + macro_f1 = metric_macro_f1(preds, gt_list_valid).item() + macro_acc = metric_macro_acc(preds, gt_list_valid).item() + return train_loss, macro_ap, macro_f1, macro_acc, macro_auc + diff --git a/voxabl/utils.py b/voxabl/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..7fd5de2ae11e3a64de85ddc8c0ce0f777a738a48 --- /dev/null +++ b/voxabl/utils.py @@ -0,0 +1,32 @@ +import random +import torch +import numpy as np + + +def set_seed(seed): + random.seed(seed) + np.random.seed(seed) + torch.manual_seed(seed) + torch.cuda.manual_seed(seed) + torch.cuda.manual_seed_all(seed) + torch.backends.cudnn.deterministic = True + torch.backends.cudnn.benchmark = False + + +def load_pretrain_model(net, weights): + net_keys = list(net.state_dict().keys()) + weights_keys = list(weights.keys()) + # assert(len(net_keys) <= len(weights_keys)) + i = 0 + j = 0 + while i < len(net_keys) and j < len(weights_keys): + name_i = net_keys[i] + name_j = weights_keys[j] + if net.state_dict()[name_i].shape == weights[name_j].shape: + net.state_dict()[name_i].copy_(weights[name_j].cpu()) + i += 1 + j += 1 + else: + i += 1 + # print i, len(net_keys), j, len(weights_keys) + return net