import os
import json
# Keras Imports
from keras import layers
from keras import metrics
import keras.backend as K
from keras.optimizers import Adam
from keras.models import Model, load_model
from keras.layers.convolutional import Conv1D, Conv2D, MaxPooling1D, MaxPooling2D
from keras.callbacks import ModelCheckpoint, EarlyStopping, TensorBoard, ReduceLROnPlateau
from keras.layers import Input, Dense, Dropout, BatchNormalization, SpatialDropout2D, Activation, Flatten
from . import plots
from . import defines
from . import arguments
from . import tensor_maps
def start_session_get_args_and_model(intra_ops, inter_ops, semantics_json, weights_hd5=None, tensor_type=None):
K.clear_session()
K.get_session().close()
cfg = K.tf.ConfigProto(intra_op_parallelism_threads=intra_ops, inter_op_parallelism_threads=inter_ops)
cfg.gpu_options.allow_growth = True
K.set_session(K.tf.Session(config=cfg))
return args_and_model_from_semantics(semantics_json, weights_hd5, tensor_type)
def args_and_model_from_semantics(semantics_json, weights_hd5=None, tensor_type=None):
args = arguments.parse_args()
if semantics_json is not None and os.path.exists(semantics_json):
print('try to load from semantics json:', semantics_json)
model = set_args_and_get_model_from_semantics(args, semantics_json, weights_hd5)
else:
print('try to load from weights HD5:', weights_hd5)
model = load_model(weights_hd5, custom_objects=get_metric_dict(args.labels))
args.tensor_name = tensor_type
return args, model
def set_args_and_get_model_from_semantics(args, semantics_json, weights_hd5=None):
'''Recreate a model from a json file specifying model semantics.
Update the args namespace from the semantics file values.
Assert that the serialized tensor map and the recreated one are the same.
Arguments:
args.tensor_name: String which indicates tensor map to use or None
args.window_size: sites included in the tensor map
args.read_limit: Maximum reads included in the tensor map
args.annotations: List of annotations or None
semantics_json: Semantics json file (created with serialize_model_semantics())
Returns:
The Keras model
'''
with open(semantics_json, 'r') as infile:
semantics = json.load(infile)
if 'input_tensor_map' in semantics:
args.tensor_name = semantics['input_tensor_map_name']
args.window_size = semantics['window_size']
args.read_limit = semantics['read_limit']
tm = tensor_maps.get_tensor_channel_map_from_args(args)
assert(len(tm) == len(semantics['input_tensor_map']))
for key in tm:
assert(tm[key] == semantics['input_tensor_map'][key])
if 'input_annotations' in semantics:
args.annotations = semantics['input_annotations']
args.annotation_set = semantics['input_annotation_set']
args.input_symbols = semantics['input_symbols']
args.labels = semantics['output_labels']
if 'channels_last' in semantics:
args.channels_last = semantics['channels_last']
if args.channels_last:
K.set_image_data_format('channels_last')
else:
K.set_image_data_format('channels_first')
if weights_hd5 is None:
weights_hd5 = os.path.join(os.path.dirname(semantics_json), semantics['architecture'])
print('Updated arguments:', args, '\nWeight file from:', weights_hd5)
model = load_model(weights_hd5, custom_objects=get_metric_dict(args.labels))
model.summary()
return model
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~ Models ~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def build_reference_annotation_model(args):
'''Build Reference 1d CNN model for classifying variants with skip connected annotations.
Convolutions followed by dense connection, concatenated with annotations.
Dynamically sets input channels based on args via tensor_maps.get_tensor_channel_map_from_args(args)
Uses the functional API.
Prints out model summary.
Arguments
args.tensor_name: The name of the tensor mapping which data goes to which channels
args.annotation_set: The variant annotation set, perhaps from a HaplotypeCaller VCF.
args.labels: The output labels (e.g. SNP, NOT_SNP, INDEL, NOT_INDEL)
Returns
The keras model
'''
if K.image_data_format() == 'channels_last':
channel_axis = -1
else:
channel_axis = 1
channel_map = tensor_maps.get_tensor_channel_map_from_args(args)
reference = Input(shape=(args.window_size, len(channel_map)), name=args.tensor_name)
conv_width = 12
conv_dropout = 0.1
fc_dropout = 0.2
x = Conv1D(filters=256, kernel_size=conv_width, activation="relu", kernel_initializer='he_normal')(reference)
x = Conv1D(filters=256, kernel_size=conv_width, activation="relu", kernel_initializer='he_normal')(x)
x = Dropout(conv_dropout)(x)
x = Conv1D(filters=128, kernel_size=conv_width, activation="relu", kernel_initializer='he_normal')(x)
x = Dropout(conv_dropout)(x)
x = Flatten()(x)
annotations = Input(shape=(len(args.annotations),), name=args.annotation_set)
annos_normed = BatchNormalization(axis=channel_axis)(annotations)
annos_normed_x = Dense(units=40, kernel_initializer='normal', activation='relu')(annos_normed)
x = layers.concatenate([x, annos_normed_x], axis=channel_axis)
x = Dense(units=40, kernel_initializer='normal', activation='relu')(x)
x = Dropout(fc_dropout)(x)
x = layers.concatenate([x, annos_normed], axis=channel_axis)
prob_output = Dense(units=len(args.labels), kernel_initializer='glorot_normal', activation='softmax')(x)
model = Model(inputs=[reference, annotations], outputs=[prob_output])
adamo = Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, clipnorm=1.)
model.compile(optimizer=adamo, loss='categorical_crossentropy', metrics=get_metrics(args.labels))
model.summary()
if os.path.exists(args.weights_hd5):
model.load_weights(args.weights_hd5, by_name=True)
print('Loaded model weights from:', args.weights_hd5)
return model
def build_read_tensor_2d_and_annotations_model(args):
'''Build Read Tensor 2d CNN model with variant annotations mixed in for classifying variants.
2d Convolutions followed by dense connection mixed with annotation values.
Dynamically sets input channels based on args via defines.total_input_channels_from_args(args)
Uses the functional API. Supports theano or tensorflow channel ordering via K.image_data_format().
Prints out model summary.
Arguments
args.window_size: Length in base-pairs of sequence centered at the variant to use as input.
args.labels: The output labels (e.g. SNP, NOT_SNP, INDEL, NOT_INDEL)
Returns
The keras model
'''
in_channels = tensor_maps.total_input_channels_from_args(args)
if K.image_data_format() == 'channels_last':
in_shape = (args.read_limit, args.window_size, in_channels)
concat_axis = -1
else:
in_shape = (in_channels, args.read_limit, args.window_size)
concat_axis = 1
read_tensor = Input(shape=in_shape, name=args.tensor_name)
read_conv_width = 16
conv_dropout = 0.2
fc_dropout = 0.3
x = Conv2D(216, (read_conv_width, 1), padding='valid', activation="relu")(read_tensor)
x = Conv2D(160, (1, read_conv_width), padding='valid', activation="relu")(x)
x = Conv2D(128, (read_conv_width, 1), padding='valid', activation="relu")(x)
x = MaxPooling2D((2,1))(x)
x = Conv2D(96, (1, read_conv_width), padding='valid', activation="relu")(x)
x = MaxPooling2D((2,1))(x)
x = Dropout(conv_dropout)(x)
x = Conv2D(64, (read_conv_width, 1), padding='valid', activation="relu")(x)
x = MaxPooling2D((2,1))(x)
x = Dropout(conv_dropout)(x)
x = Flatten()(x)
# Mix the variant annotations in
PyPI 官网下载 | vqsr_cnn-0.0.174.tar.gz
版权申诉
171 浏览量
2022-01-17
09:02:41
上传
评论
收藏 16KB GZ 举报
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
最新资源
- Python课设-学生信息管理系统
- 广东省2021年普通高考音乐表演-器乐类总分分数段统计表(含本、专科层次加分)
- ubuntu20.04安装教程.md
- 广东省2021年普通高考音乐表演-声乐类总分分数段统计表(含本、专科层次加分)
- 广东省2021年普通高考音乐学类总分分数段统计表(含本、专科层次加分)
- 广东省2021年普通高考舞蹈类总分分数段统计表(含本、专科层次加分)
- Python教程-快速入门基础必看课程10-函数基础
- tensorflow-2.9.2-cp39-cp39-win-amd64.whl
- tensorflow-2.9.2-cp37-cp37m-win-amd64.whl
- 广东省2021年普通高考体育类总分分数段统计表(含本、专科层次加分)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈