def run(self): # ## albert-base # remote_helper.get_remote_date('https://www.flyai.com/m/albert_base_zh_tensorflow.zip') # convert_tf_checkpoint_to_pytorch( # tf_checkpoint_path="./data/input/model", # bert_config_file="./data/input/model/albert_config_base.json", # pytorch_dump_path="./data/input/model/pytorch_model.bin", # share_type="all") # ## albert-large remote_helper.get_remote_date('https://www.flyai.com/m/albert_large_zh.zip') convert_tf_checkpoint_to_pytorch( tf_checkpoint_path="./data/input/model", bert_config_file="./data/input/model/albert_config_large.json", pytorch_dump_path="./data/input/model/pytorch_model.bin", share_type="all") # ## albert-xlarge # remote_helper.get_remote_date('https://www.flyai.com/m/albert_xlarge_zh_183k.zip') # convert_tf_checkpoint_to_pytorch(tf_checkpoint_path="./data/input/model", # bert_config_file="./data/input/model/albert_config_xlarge.json", # pytorch_dump_path="./data/input/model/pytorch_model.bin", # share_type="all") self.model = Net( tag_map=self.tag_map, batch_size=self.args.BATCH, dropout=self.args.dropout, embedding_dim=self.args.embedding_size, hidden_dim=self.args.hidden_size, ) train_source, train_target, dev_source, dev_target = self.generate() self.train(train_source, train_target, dev_source, dev_target)
def run(self): remote_helper.get_remote_date( "https://www.flyai.com/m/chinese_base.zip") before_vocab_dir = os.path.join(os.getcwd(), 'vocab.txt') after_vocab_dir = os.path.join(args.bert_model_dir, 'vocab.txt') logger.info('>before_vocab_dir:{}'.format(before_vocab_dir)) logger.info('>after_vocab_dir:{}'.format(after_vocab_dir)) shutil.copyfile(before_vocab_dir, after_vocab_dir) if not os.path.exists(self.arguments.output_dir): os.mkdir(self.arguments.output_dir) self.arguments.BATCH = self.arguments.BATCH // self.arguments.gradient_accumulation_steps # 数据准备 分词器选择 tokenizer = BertTokenizer( self.arguments.bert_vocab_file).from_pretrained( self.arguments.bert_model_dir, do_lower_case=self.arguments.do_lower_case) # 获取数据 news/keywords train_news, train_category, dev_news, dev_category = self.generate() self.train(Net=Net, train_category=train_category, dev_category=dev_category, train_news=train_news, dev_news=dev_news, tokenizer=tokenizer)
def inception_v3(pretrained=False, progress=True, **kwargs): r"""Inception v3 model architecture from `"Rethinking the Inception Architecture for Computer Vision" <http://arxiv.org/abs/1512.00567>`_. .. note:: **Important**: In contrast to the other models the inception_v3 expects tensors with a size of N x 3 x 299 x 299, so ensure your images are sized accordingly. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet progress (bool): If True, displays a progress bar of the download to stderr aux_logits (bool): If True, add an auxiliary branch that can improve training. Default: *True* transform_input (bool): If True, preprocesses the input according to the method with which it was trained on ImageNet. Default: *False* """ if pretrained: if 'transform_input' not in kwargs: kwargs['transform_input'] = True if 'aux_logits' in kwargs: original_aux_logits = kwargs['aux_logits'] kwargs['aux_logits'] = True else: original_aux_logits = True model = Inception3(**kwargs) path = remote_helper.get_remote_date(model_urls['inception_v3_google']) state_dict = torch.load(path) model.load_state_dict(state_dict) if not original_aux_logits: model.aux_logits = False del model.AuxLogits return model return Inception3(**kwargs)
def _resnet(arch, block, layers, pretrained, progress, **kwargs): model = ResNet(block, layers, **kwargs) if pretrained: path = remote_helper.get_remote_date(model_urls[arch]) state_dict = torch.load(path) model.load_state_dict(state_dict) return model
def _squeezenet(version, pretrained, progress, **kwargs): model = SqueezeNet(version, **kwargs) if pretrained: arch = 'squeezenet' + version path = remote_helper.get_remote_date(model_urls[arch]) state_dict = torch.load(path) model.load_state_dict(state_dict) return model
def get_pre_train_model(): """加载预训练模型""" # 必须使用该方法下载模型,然后加载 from flyai.utils import remote_helper # 下载到项目中的data/input/文件夹,默认会自动解压,具体文件路径可以下之后查看使用 path = remote_helper.get_remote_date( 'https://www.flyai.com/m/gpt-2-chinese-wiki.zip') return path
def load_model(self): ''' 模型初始化,必须在构造方法中加载模型 ''' torch.cuda.empty_cache() path = remote_helper.get_remote_date( 'https://www.flyai.com/m/glove.twitter.27B.zip') glove_path = os.path.split(path)[0] self.word_emb = load_word_emb( os.path.join(glove_path, 'glove.twitter.27B.25d.txt')) self.model = SQLNet(self.word_emb, N_word=N_word, gpu=GPU)
def Resnet50( ): # 另一个比较关键的地方 一定要用别人论文已经用好的网络 工业应用ResNet极其以后的,最低ResNet。VGG Inception不用尝试了 path = remote_helper.get_remote_date( "https://www.flyai.com/m/resnet50-19c8e357.pth") # 直接加载imagenet预训练模型 model = torchvision.models.resnet50(pretrained=False) model.load_state_dict(torch.load(path)) # 先将所有的特征层freeze for param in model.parameters(): # 将模型所有参数进行固定 param.requires_grad = False # 放开想学习的层 可以通过查看model变量 # 放开要学习的参数 整个ResNet分四个block for param in model.layer4.parameters( ): # 将最后一个block也让学习,这里是需要调整的。放到GPU要取消注释。不光要学最后一个block,和最后两个block param.requires_grad = True num_fc_ftr = model.fc.in_features # 特征提取层、全连接层 # 之前为特征提取,最后分类任务,全连接进行分类 # 对应20 model.fc = nn.Linear(num_fc_ftr, 2) # 全连接两分类,之前的代码不是在CPU跑得,没有GPU model = model.to(DEVICE) # 转到CUDA return model
def densenet161(pretrained=False, **kwargs): model = DenseNet(num_init_features=96, growth_rate=48, block_config=(6, 12, 36, 24), **kwargs) if pretrained: pattern = re.compile( r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$' ) path = remote_helper.get_remote_date(model_urls['densenet161']) state_dict = torch.load(path) for key in list(state_dict.keys()): res = pattern.match(key) if res: new_key = res.group(1) + res.group(2) state_dict[new_key] = state_dict[key] del state_dict[key] model.load_state_dict(state_dict) return model
def train(self): # 构建模型 batch_steps = len(label_list) // args.BATCH # 构建不带分类器的预训练模型 base_model = InceptionV3(weights=None, include_top=False) path = remote_helper.get_remote_date( 'https://www.flyai.com/m/v0.5|inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5' ) base_model.load_weights(path) # 添加全局平均池化层 x = base_model.output x = GlobalAveragePooling2D()(x) # 添加一个全连接层 x = Dense(1024, activation='relu')(x) predictions = Dense(1884)(x) # 构建我们需要训练的完整模型 model = Model(inputs=base_model.input, outputs=predictions) # 编译模型 model.compile(optimizer='rmsprop', loss='mse') print('model done!!!') min_loss = 100 for epoch in range(args.EPOCHS): loss_50 = [] for i in range(batch_steps): now_step = epoch * batch_steps + i batch_x, batch_y = self.get_batch_data(image_path_list, label_list, args.BATCH) loss = model.train_on_batch(batch_x, batch_y) print('epoch: %d/%d, batch: %d/%d, loss: %f/%f' % (epoch, args.EPOCHS, i, batch_steps, loss, min_loss)) loss_50.append(loss) if now_step % 50 == 0: mean_loss = np.mean(np.array(loss_50)) loss_50 = [] if mean_loss < min_loss: min_loss = mean_loss model.save(model_path) print('saved model!!!')
def get_net(modelName, num_classes): if modelName == 'efficientnet-b0': print('using efficientnet-b0') path = remote_helper.get_remote_date( 'https://www.flyai.com/m/adv-efficientnet-b0-b64d5a18.pth') model = EfficientNet.from_name("efficientnet-b0") model.load_state_dict(torch.load(path)) model._fc = torch.nn.Linear(1280, num_classes) return model elif modelName == 'efficientnet-b2': print('using efficientnet-b2') path = remote_helper.get_remote_date( 'https://www.flyai.com/m/efficientnet-b2-8bb594d6.pth') model = EfficientNet.from_name("efficientnet-b2") model.load_state_dict(torch.load(path)) # model._avg_pooling = GeM() model._fc = nn.Sequential(torch.nn.Linear(1408, 256), nn.ReLU(), nn.Dropout(0.5), nn.Linear(256, num_classes)) return model elif modelName == 'efficientnet-b3': print('using efficientnet-b3') # 必须使用该方法下载模型,然后加载 path = remote_helper.get_remote_date( 'https://www.flyai.com/m/efficientnet-b3-5fb5a3c3.pth') model = EfficientNet.from_name("efficientnet-b3") model.load_state_dict(torch.load(path)) model._fc = nn.Sequential(torch.nn.Linear(1536, 256), nn.ReLU(), nn.Dropout(0.5), nn.Linear(256, num_classes)) return model elif modelName == 'efficientnet-b4': print('using efficientnet-b4') # 必须使用该方法下载模型,然后加载 path = remote_helper.get_remote_date( 'https://www.flyai.com/m/efficientnet-b4-6ed6700e.pth') model = EfficientNet.from_name("efficientnet-b4") model.load_state_dict(torch.load(path)) # model._avg_pooling = GeM() model._fc = nn.Sequential(torch.nn.Linear(1792, 256), nn.ReLU(), nn.Dropout(0.5), nn.Linear(256, num_classes)) return model elif modelName == 'senet34': print('using senet34') path = remote_helper.get_remote_date( 'https://www.flyai.com/m/seresnet34-a4004e63.pth') model = seresnet34() model.load_state_dict(torch.load(path)) model.last_linear = torch.nn.Linear(512, num_classes) return model elif modelName == 'inceptionresnetv2': print('using inceptionresnetv2') path = remote_helper.get_remote_date( 'https://www.flyai.com/m/inceptionresnetv2-520b38e4.pth') model = inceptionresnetv2(pretrained=False) model.load_state_dict(torch.load(path)) model.last_linear = nn.Sequential(torch.nn.Linear(1536, 256), nn.ReLU(), nn.Dropout(0.5), nn.Linear(256, num_classes)) return model elif modelName == 'inceptionv4': print('using inceptionv4') path = remote_helper.get_remote_date( 'https://www.flyai.com/m/inceptionv4-8e4777a0.pth') model = inceptionv4(pretrained=False) pretrained_dict = torch.load(path) model_dict = model.state_dict() for k in model_dict.keys(): if (('module.' + k) in pretrained_dict.keys()): model_dict[k] = pretrained_dict.get(('module.' + k)) model.load_state_dict(model_dict) model.last_linear = nn.Sequential(torch.nn.Linear(1536, 256), nn.ReLU(), nn.Dropout(0.5), nn.Linear(256, num_classes)) return model elif modelName == 'seresnext50_32x4d': print('using seresnext50_32x4d') # 必须使用该方法下载模型,然后加载 path = remote_helper.get_remote_date( 'https://www.flyai.com/m/se_resnext50_32x4d-a260b3a4.pth') model = seresnext50_32x4d(pretrained=False) model.load_state_dict(torch.load(path)) model.last_linear = nn.Sequential(torch.nn.Linear(18432, 512), nn.ReLU(), nn.Dropout(0.5), nn.Linear(512, num_classes)) return model elif modelName == 'resnest50': print('using resnest50') # 必须使用该方法下载模型,然后加载 path = remote_helper.get_remote_date( 'https://www.flyai.com/m/resnest50-528c19ca.pth') model = resnest50(pretrained=False) model.load_state_dict(torch.load(path)) model.last_linear = nn.Sequential(torch.nn.Linear(2048, 256), nn.ReLU(), nn.Dropout(0.5), nn.Linear(256, num_classes)) return model else: print('error,please check your model name!')
from config import size import os import config model_name=config.model_name MODEL_URL={ "vgg16":"https://www.flyai.com/m/v0.1|vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5", "vgg19": "https://www.flyai.com/m/v0.1|vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5", "xception": "https://www.flyai.com/m/v0.4|xception_weights_tf_dim_ordering_tf_kernels_notop.h5", "resnet50": "https://www.flyai.com/m/v0.2|resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5", "densenet121":"https://www.flyai.com/m/v0.8|densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5", "densenet201": "https://www.flyai.com/m/v0.8|densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5" } # 必须使用该方法下载模型,然后加载 from flyai.utils import remote_helper path = remote_helper.get_remote_date(MODEL_URL[model_name]) for rt, dirs, files in os.walk('./'): for f in files: if model_name in f: weight_path = os.path.join(rt, f) print(weight_path) if model_name=="vgg16": base_model=VGG16(include_top=False, weights=weight_path, input_shape=(size, size, 3)) elif model_name=="vgg19": base_model=VGG19(include_top=False, weights=weight_path, input_shape=(size, size, 3)) elif model_name=="xception": base_model=Xception(include_top=False, weights=weight_path, input_shape=(size, size, 3)) elif model_name=="resnet50": base_model=ResNet50(include_top=False, weights=weight_path, input_shape=(size, size, 3)) elif model_name=="densenet121":
def train_model(self, needInit=True, loadmodelType= 1, epochs=2, ): input_x = self.inputParams['input_x'] input_y = self.inputParams['input_y'] keep_prob = self.inputParams['keep_prob'] loss = self.outputParams['loss'] accuracy = self.outputParams['accuracy'] train_op = self.outputParams['train_op'] merged_summary = self.summaryParams['merged_summary'] with tf.Session() as sess: default_graph = sess.graph if needInit: init = tf.global_variables_initializer() sess.run(init) elif loadmodelType == 'all': # all表示加载所有变量 init_saver = tf.train.Saver() init_saver.restore(sess, os.path.join(MODEL_PATH, TENSORFLOW_MODEL_DIR)) else: # 其他表示紧加载词嵌入 init_saver = tf.train.Saver({"bert/embeddings/word_embeddings": default_graph.get_tensor_by_name("embedding/encoder_embedding:0")}) # 必须使用该方法下载模型,然后加载 path = remote_helper.get_remote_date("https://www.flyai.com/m/chinese_L-12_H-768_A-12.zip") init_saver.restore(sess, path) # 本地测试用 # init_saver.restore(sess, os.path.join(os.getcwd(),'chinese_L-12_H-768_A-12','bert_model.ckpt')) global_vars = tf.global_variables() is_not_initialized = sess.run([tf.is_variable_initialized(var) for var in global_vars]) not_initialized_vars = [v for (v, f) in zip(global_vars, is_not_initialized) if not f] # print([str(i.name) for i in not_initialized_vars]) # only for testing if len(not_initialized_vars): sess.run(tf.variables_initializer(not_initialized_vars)) filters = default_graph.get_tensor_by_name("cnn/filters:0") fc1 = default_graph.get_tensor_by_name('fc1/fc1:0') bias1 = default_graph.get_tensor_by_name('fc1/bias1:0') fc2 = default_graph.get_tensor_by_name('fc2/fc2:0') bias2 = default_graph.get_tensor_by_name('fc2/bias2:0') # partial_init = default_graph.get_operation_by_name('partial_init') # default_graph.g # sess.run(partial_init) train_writer = tf.summary.FileWriter(LOG_PATH, sess.graph) # dataset = Dataset(train_batch=128, val_batch=64, split_ratio = 0.9,) # epochs = 2 # step = math.ceil(self.data.get_train_length() / min(256,)) max_acc = 0 min_loss = 0 save_saver = tf.train.Saver() for j in range(self.data.get_step()): x_train, y_train = self.data.next_train_batch() fetches = [loss, accuracy, train_op] feed_dict = {input_x: x_train, input_y: y_train, keep_prob: 0.8} loss_, accuracy_, _ = sess.run(fetches, feed_dict=feed_dict) if j % 100 == 0 or j == self.data.get_step()-1: summary_train = sess.run(merged_summary, feed_dict=feed_dict, ) train_writer.add_summary(summary_train, j) nSmp_val = 0 nCount = 0 ave_loss = 0 for i in range(10): x_val, y_val = self.data.next_validation_batch() summary_val = sess.run([loss, accuracy], feed_dict={input_x: x_val, input_y: y_val, keep_prob: 1.0}) nSmp_val += x_val.shape[0] nCount += summary_val[1] * x_val.shape[0] ave_loss += summary_val[0] val_accuracy = nCount / nSmp_val ave_loss = ave_loss / 10 print('当前批次: {} | 当前训练损失: {} | 当前训练准确率: {} | ' '当前验证集损失: {} | 当前验证集准确率: {}'.format(j, loss_, accuracy_, ave_loss, val_accuracy)) if val_accuracy > max_acc or (val_accuracy == max_acc and ave_loss < min_loss): max_acc, min_loss = val_accuracy, ave_loss save_path = save_saver.save(sess, os.path.join(MODEL_PATH, TENSORFLOW_MODEL_DIR)) print("Model saved in path: %s" % save_path)
from flyai.utils import remote_helper from flyai.dataset import Dataset DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # ## albert-base # remote_helper.get_remote_date('https://www.flyai.com/m/albert_base_zh_tensorflow.zip') # convert_tf_checkpoint_to_pytorch( # tf_checkpoint_path="./data/input/model", # bert_config_file="./data/input/model/albert_config_base.json", # pytorch_dump_path="./data/input/model/pytorch_model.bin", # share_type="all") # ## albert-large remote_helper.get_remote_date('https://www.flyai.com/m/albert_large_zh.zip') convert_tf_checkpoint_to_pytorch( tf_checkpoint_path="./data/input/model", bert_config_file="./data/input/model/albert_config_large.json", pytorch_dump_path="./data/input/model/pytorch_model.bin", share_type="all") # ## albert-xlarge # remote_helper.get_remote_date('https://www.flyai.com/m/albert_xlarge_zh_183k.zip') # convert_tf_checkpoint_to_pytorch(tf_checkpoint_path="./data/input/model", # bert_config_file="./data/input/model/albert_config_xlarge.json", # pytorch_dump_path="./data/input/model/pytorch_model.bin", # share_type="all") class Instructor(object):
# -*- coding: utf-8 -* from torch import nn from torchvision.models.detection.faster_rcnn import FastRCNNPredictor import torchvision import torch from torchsummary import summary # 必须使用该方法下载模型,然后加载 from flyai.utils import remote_helper weights_path = remote_helper.get_remote_date( 'https://www.flyai.com/m/fasterrcnn_resnet50_fpn_coco-258fb6c6.pth') # 构建模型 def get_net(): num_classes = 2 + 1 # (2个类别) + background # num_classes = 2 # (2个类别) # 加载经过预训练的模型 # my_model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True) # my_model = torchvision.models.resnet50(pretrained=True) my_model = torchvision.models.detection.fasterrcnn_resnet50_fpn( pretrained=False) pre = torch.load(weights_path) my_model.load_state_dict(pre) # 获取分类器的输入参数的数量 in_features = my_model.roi_heads.box_predictor.cls_score.in_features # 用新的头部替换预先训练好的头部 my_model.roi_heads.box_predictor = FastRCNNPredictor( in_features, num_classes)
from model import Model from path import MODEL_PATH from keras.callbacks import EarlyStopping, TensorBoard,ModelCheckpoint,ReduceLROnPlateau from keras.optimizers import SGD,adam import numpy as np from keras.preprocessing.image import ImageDataGenerator import tensorflow as tf import sys import os from model import KERAS_MODEL_NAME import WangyiUtilOnFlyai as wangyi # 必须使用该方法下载模型,然后加载 from flyai.utils import remote_helper try: weights_path = remote_helper.get_remote_date("https://www.flyai.com/m/v0.2|resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5") except OSError: weights_path = 'imagenet' ''' 2019-07-26 获取数据值,是否train set有问题??读取label ''' parser = argparse.ArgumentParser() parser.add_argument("-e", "--EPOCHS", default=10, type=int, help="train epochs") parser.add_argument("-b", "--BATCH", default=8, type=int, help="batch size") args = parser.parse_args() ''' flyai库中的提供的数据处理方法 传入整个数据训练多少轮,每批次批大小 '''
from torch import nn from time import strftime, localtime from pytorch_transformers import BertModel from torch.utils.data import DataLoader, random_split from flyai.utils import remote_helper import argparse from BERT import args as arguments from BERT.data_utils import ABSADataset, Tokenizer4Bert from BERT.data_utils import Util logger = logging.getLogger() logger.setLevel(logging.INFO) logger.addHandler(logging.StreamHandler(sys.stdout)) remote_helper.get_remote_date("https://www.flyai.com/m/chinese_base.zip") shutil.copyfile(os.path.join(os.getcwd(), 'vocab.txt'), os.path.join(os.getcwd(), arguments.pretrained_bert_name, 'vocab.txt')) class Instructor(object): def __init__(self, arguments): # 项目的超参 parser = argparse.ArgumentParser() parser.add_argument("-e", "--EPOCHS", default=50, type=int, help="train epochs") parser.add_argument("-b", "--BATCH", default=2, type=int, help="batch size") self.args = parser.parse_args() self.arguments = arguments if 'bert' in self.arguments.model_name: tokenizer = Tokenizer4Bert(max_seq_len=self.arguments.max_seq_len,
def __init__(self): path = remote_helper.get_remote_date( "https://www.flyai.com/m/uncased_L-24_H-1024_A-16.zip") data_root = os.path.splitext(path)[0] bert_config_file = os.path.join(data_root, 'bert_config.json') bert_config = modeling.BertConfig.from_json_file(bert_config_file) init_checkpoint = os.path.join(data_root, 'bert_model.ckpt') bert_vocab_file = os.path.join(data_root, 'vocab.txt') self.input_ids = tf.placeholder(tf.int32, shape=[None, None], name='input_ids') self.input_mask = tf.placeholder(tf.int32, shape=[None, None], name='input_masks') self.segment_ids = tf.placeholder(tf.int32, shape=[None, None], name='segment_ids') self.labels = tf.placeholder(tf.int32, shape=[ None, ], name="labels") self.is_training = tf.placeholder_with_default(False, shape=(), name='is_training') self.learning_rate = tf.placeholder_with_default(config.learning_rate, shape=(), name='learning_rate') self.global_step = tf.Variable(0, trainable=False, name='global_step') # 创建bert模型 with tf.name_scope('Bert'): model = modeling.BertModel( config=bert_config, is_training=True, input_ids=self.input_ids, input_mask=self.input_mask, token_type_ids=self.segment_ids, # 这里如果使用TPU 设置为True,速度会快些。使用CPU 或GPU 设置为False ,速度会快些。 use_one_hot_embeddings=False) # 这个获取每个token的output 输入数据[batch_size, seq_length, embedding_size] 如果做seq2seq 或者ner 用这个 # output_layer = model.get_sequence_output() tvars = tf.trainable_variables() # 加载BERT模型 (assignment_map, initialized_variable_names) = \ modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint) tf.train.init_from_checkpoint(init_checkpoint, assignment_map) output_layer = model.get_pooled_output() # 这个获取句子的output hidden_size = output_layer.shape[-1].value # 获取输出的维度 # 构建W 和 b output_weights = tf.get_variable( "output_weights", [hidden_size, config.num_labels], initializer=tf.truncated_normal_initializer(stddev=0.02)) output_bias = tf.get_variable("output_bias", [config.num_labels], initializer=tf.zeros_initializer()) with tf.variable_scope("predict"): if self.is_training is True: # I.e., 0.1 dropout output_layer = tf.nn.dropout(output_layer, keep_prob=0.5) # logits = tf.matmul(output_layer, output_weights) logits = tf.matmul(output_layer, output_weights) logits = tf.nn.bias_add(logits, output_bias) # probabilities = tf.nn.softmax(logits, axis=-1) log_probs = tf.nn.log_softmax(logits, axis=-1) self.pred = tf.argmax(log_probs, 1, name='pred') with tf.name_scope("accuracy"): # 准确率 correct_pred = tf.equal(self.labels, tf.cast(self.pred, tf.int32)) self.accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name='acc') with tf.name_scope("loss"): # 将label进行onehot转化 one_hot_labels = tf.one_hot(self.labels, depth=config.num_labels, dtype=tf.float32) # # 构建损失函数 per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1) self.loss = tf.reduce_mean(per_example_loss) # # 优化器 # self.train_op = tf.train.AdamOptimizer(learning_rate=model_config.learning_rate).minimize(self.loss) with tf.name_scope('optimize'): optimizer = tf.train.AdamOptimizer(self.learning_rate) gradients, variables = zip(*optimizer.compute_gradients(self.loss)) gradients, _ = tf.clip_by_global_norm(gradients, config.grad_clip) self.train_op = optimizer.apply_gradients( zip(gradients, variables), global_step=self.global_step)
def __init__(self, num_classes): """Declare all needed layers.""" self.num_classes = num_classes try: weights_path = None # weights_path = remote_helper.get_remote_date('https://www.flyai.com/m/resnext101_imagenet_1000_no_top.h5') # weights_path = remote_helper.get_remote_date('https://www.flyai.com/m/v0.8|NASNet-mobile-no-top.h5') # weights_path = remote_helper.get_remote_date("https://www.flyai.com/m/v0.2|resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5") # weights_path = remote_helper.get_remote_data('https://www.flyai.com/m/v0.8|densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5') # weights_path = remote_helper.get_remote_date( 'https://www.flyai.com/m/v0.8|densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5') # weights_path = remote_helper.get_remote_date( 'https://www.flyai.com/m/v0.8|densenet201_weights_tf_dim_ordering_tf_kernels.h5') # weights_path = remote_helper.get_remote_date('https://www.flyai.com/m/v0.8|densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5') weights_path = remote_helper.get_remote_date( 'https://www.flyai.com/m/v0.7|inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5' ) # weights_path = remote_helper.get_remote_date('https://www.flyai.com/m/v0.8|NASNet-large-no-top.h5') except OSError: weights_path = 'imagenet' # base_model = ResNet50(weights=None, input_shape=(img_size[0], img_size[1], 3), include_top=False) # base_model = ResNet50(weights=weights_path, include_top=False ,input_shape=(img_size[0], img_size[1],3)) # base_model = DenseNet201(weights=weights_path, include_top=False, input_shape=(img_size[0], img_size[1], 3)) # base_model = DenseNet201(weights=weights_path, include_top=True) # base_model = NASNetMobile(weights=weights_path, include_top=False,input_shape=(img_size[0], img_size[1], 3)) base_model = InceptionResNetV2(weights=weights_path, include_top=False, input_shape=(img_size[0], img_size[1], 3)) Inp = Input(shape=(img_size[0], img_size[1], 3)) # x = Conv2D(256,3, # activation='relu', # padding='same', # name='wangyi_conv1')(Inp) # x = Conv2D(256,5, # activation='relu', # padding='same', # name='wangyi_conv2')(x) # x = MaxPooling2D((2, 2), strides=(1, 1), name='wangyi_pool')(x) # x =Flatten()(x) # x = Conv2D(3,7, # activation='relu', # padding='same', # name='wangyi_conv3')(x) # x = base_model.output # x = GlobalAveragePooling2D()(x) # x = Flatten(name='flatten_1')(x) # 冻结不打算训练的层。 # print('base_model.layers', len(base_model.layers)) # for i, layer in enumerate(base_model.layers): # print(i, layer.name) # # for layer in base_model.layers[:]: # layer.trainable = False # 增加定制层 x = base_model(Inp) # print(layer) # x = LeakyReLU()(x) # x = Dense(2048 ,kernel_initializer='he_uniform')(x) # x = BatchNormalization()(x) # x = LeakyReLU()(x) # x = Dense(2048 ,kernel_initializer='he_uniform')(x) # x = BatchNormalization()(x) x = GlobalAveragePooling2D()(x) # x = Dense(128, activation='relu')(x) # x = Flatten(name='flatten_1')(x) # x = Dense(1024, activation='relu' )(x) # x = LeakyReLU()(x) # x = Dense(128)(x) # x = Dense(25)(x) # x = LeakyReLU()(x) predictions = Dense(num_classes, activation="softmax")(x) # 创建最终模型 self.model_cnn = keras_model(inputs=Inp, outputs=predictions)
import tensorflow as tf from flyai.utils import remote_helper from flyai.dataset import Dataset import tensorflow.keras.backend as K from tensorflow.keras.applications.densenet import DenseNet201, preprocess_input # import keras.backend as K # from keras.applications.densenet import DenseNet201, preprocess_input # from keras.applications.densenet import DenseNet201, preprocess_input from path import MODEL_PATH, LOG_PATH from model_back import Model # 获取预训练模型路径 # path = remote_helper.get_remote_date("https://www.flyai.com/m/v0.2|resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5") path = remote_helper.get_remote_date( "https://www.flyai.com/m/v0.8|densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5" ) # path = r"D:/jack_doc/python_src/flyai/data/SceneClassification_FlyAI_data/v0.8-densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5" ''' Tensorflow模版项目下载: https://www.flyai.com/python/tensorflow_template.zip PyTorch模版项目下载: https://www.flyai.com/python/pytorch_template.zip Keras模版项目下载: https://www.flyai.com/python/keras_template.zip 第一次使用请看项目中的:第一次使用请读我.html文件 常见问题请访问:https://www.flyai.com/question 意见和问题反馈有红包哦!添加客服微信:flyaixzs ''' ''' 项目的超参 ''' parser = argparse.ArgumentParser() parser.add_argument("-e", "--EPOCHS", default=1, type=int, help="train epochs")
def get_pre_train_model(self): # 必须使用该方法下载模型,然后加载 from flyai.utils import remote_helper path = remote_helper.get_remote_date('https://www.flyai.com/m/chinese_wwm_ext_L-12_H-768_A-12.zip') return path
from shutil import copyfile from flyai.utils import remote_helper # 训练数据的路径 DATA_PATH = os.path.join(sys.path[0], 'data', 'input') # 模型保存的路径 MODEL_PATH = os.path.join(sys.path[0], 'data', 'output', 'model') # 训练log的输出路径 LOG_PATH = os.path.join(sys.path[0], 'data', 'output', 'logs') QUE_DICT_FILE = os.path.join(DATA_PATH, 'que.dict') ANS_DICT_FILE = os.path.join(DATA_PATH, 'ans.dict') # 必须使用该方法下载模型,然后加载 BERT_PATH = remote_helper.get_remote_date( 'https://www.flyai.com/m/chinese_wwm_ext_pytorch.zip') print('after get_remote_date BERT_PATH:{}'.format(BERT_PATH)) BERT_PATH_TMP = os.path.dirname(BERT_PATH) BERT_PATH = os.path.join(BERT_PATH_TMP, "chinese_wwm_ext_pytorch_bert") print('BERT_PATH:{}'.format(BERT_PATH)) if not os.path.exists(BERT_PATH): os.makedirs(BERT_PATH) #获取目录列表 list_dir = os.listdir(BERT_PATH_TMP) #打印目录列表 for temp in list_dir: temp_long = os.path.join(BERT_PATH_TMP, temp) if not temp.endswith(".zip") and os.path.isfile(temp_long):
import args as arguments from net import Net from vec_utils import read_emb from vec_text import make_datasets, load_tvt from model_utils import load_torch_model, test, classify_batch from flyai.dataset import Dataset from flyai.utils import remote_helper torch.manual_seed(arguments.seed) DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu') torch.cuda.manual_seed(arguments.seed) remote_helper.get_remote_date('https://www.flyai.com/m/sgns.weibo.word.bz2') class StanceDetection(object): def __init__(self, exec_type='train'): # 项目的超参 parser = argparse.ArgumentParser() parser.add_argument("-e", "--EPOCHS", default=50, type=int, help="train epochs") parser.add_argument("-b", "--BATCH", default=2, type=int, help="batch size") self.args = parser.parse_args() self.dataset = Dataset(epochs=self.args.EPOCHS, batch=self.args.BATCH) self.model_dir = os.path.join(os.getcwd(), arguments.model_dir) # 1. Split the data, read into defined format label2idx = dict((arguments.labels[i], i) for i in range(len(arguments.labels))) target_text, stance, _, _ = self.dataset.get_all_data()
zoom_range=0.1, brightness_range=[0.9, 1.1]) ''' 实现自己的网络机构 ''' #接下来就是考虑什么模型可以来做这个人脸预测了,测试vgg_face,senet50 from keras_vggface.vggface import VGGFace from keras.layers import * from lin import categorical_focal_loss vgg_model = VGGFace(include_top=False, model='senet50', input_shape=(224, 224, 3), weights=None) # 必须使用该方法下载模型,然后加载 from flyai.utils import remote_helper path = remote_helper.get_remote_date( 'https://www.flyai.com/m/rcmalli_vggface_tf_notop_senet50.h5') vgg_model.load_weights(path) x = vgg_model.output x = Flatten()(x) x = BatchNormalization()(x) x = Dense(128, activation='relu')(x) x = Dropout(rate=0.5)(x) x = Dense(128, activation='relu')(x) x = Dropout(rate=0.5)(x) o = Dense(10, activation='softmax', name='prediction')(x) seque = Keras_Model(inputs=vgg_model.input, outputs=o) #学习率设置 from keras.optimizers import * from keras.callbacks import ReduceLROnPlateau, ModelCheckpoint sgd = SGD(lr=0.0001)
#!/usr/bin/env python # -*- coding: utf-8 -*- # @File : path.py # @Author: Zhan # @Date : 7/18/2019 # @Desc : 数据、模型、字典等文件路径 import os from flyai.utils import remote_helper cPath = os.getcwd() # 训练数据的路径 DATA_PATH = os.path.join(cPath, 'data', 'input') # 模型保存的路径 MODEL_PATH = os.path.join(cPath, 'data', 'output', 'model') # 训练log的输出路径 LOG_PATH = os.path.join(cPath, 'data', 'output', 'logs') # 必须使用该方法下载模型,然后加载 BERT_PATH = remote_helper.get_remote_date( "https://www.flyai.com/m/chinese_L-12_H-768_A-12.zip") print('BERT_PATH:{}'.format(BERT_PATH)) BERT_PATH = os.path.dirname(BERT_PATH) BERT_PATH = os.path.join(BERT_PATH, 'chinese_L-12_H-768_A-12') # BERT_PATH = r'D:\Study\flyai_contest\chinese_L-12_H-768_A-12' BERT_CONFIG = os.path.join(BERT_PATH, "bert_config.json") BERT_CKPT = os.path.join(BERT_PATH, 'bert_model.ckpt') VOCAB_FILE = os.path.join(BERT_PATH, "vocab.txt") KERAS_BERT_CLASSIFY_MODEL = "spam_classify_net.h5"
def __init__(self): path = remote_helper.get_remote_date( "https://www.flyai.com/m/uncased_L-24_H-1024_A-16.zip") data_root = os.path.splitext(path)[0] bert_config_file = os.path.join(data_root, 'bert_config.json') bert_config = modeling.BertConfig.from_json_file(bert_config_file) init_checkpoint = os.path.join(data_root, 'bert_model.ckpt') bert_vocab_file = os.path.join(data_root, 'vocab.txt') self.input_ids = tf.placeholder(tf.int32, shape=[None, None], name='input_ids') self.input_mask = tf.placeholder(tf.int32, shape=[None, None], name='input_masks') self.segment_ids = tf.placeholder(tf.int32, shape=[None, None], name='segment_ids') self.labels = tf.placeholder(tf.int32, shape=[ None, ], name="labels") self.is_training = tf.placeholder_with_default(False, shape=(), name='is_training') self.learning_rate = tf.placeholder_with_default(config.learning_rate, shape=(), name='learning_rate') # 创建bert模型 with tf.name_scope('Bert'): model = modeling.BertModel( config=bert_config, is_training=True, input_ids=self.input_ids, input_mask=self.input_mask, token_type_ids=self.segment_ids, # 这里如果使用TPU 设置为True,速度会快些。使用CPU 或GPU 设置为False ,速度会快些。 use_one_hot_embeddings=False) # 这个获取每个token的output 输入数据[batch_size, seq_length, embedding_size] 如果做seq2seq 或者ner 用这个 # output_layer = model.get_sequence_output() tvars = tf.trainable_variables() # 加载BERT模型 (assignment_map, initialized_variable_names) = \ modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint) tf.train.init_from_checkpoint(init_checkpoint, assignment_map) # output_layer = model.get_pooled_output() # 这个获取句子的output # hidden_size = output_layer.shape[-1].value # 获取输出的维度 embedding = model.get_sequence_output() max_seq_length = embedding.shape[1].value used = tf.sign(tf.abs(self.input_ids)) lengths = tf.reduce_sum( used, reduction_indices=1) # [batch_size] 大小的向量,包含了当前batch中的序列长度 blstm_crf = BiLstmCrf(embedded_chars=embedding, max_seq_length=max_seq_length, labels=self.labels, lengths=lengths, is_training=self.is_training) self.loss, logits, trans, pred_ids = blstm_crf.add_blstm_crf_layer() with tf.variable_scope("predict"): self.pred = tf.Variable(pred_ids, name='pred') with tf.name_scope("train_op"): self.train_op = tf.train.AdamOptimizer( learning_rate=model_config.learning_rate).minimize(self.loss)