Example #1
0
    def __init__(self, num_classes=10):
        """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/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')
        except OSError:
            weights_path = 'imagenet'

        # base_model = ResNet50(weights=None, input_shape=input_shape=(img_size[0], img_size[1], 3), include_top=False)
        base_model = DenseNet121(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(Inp)
        # 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
        # print(layer)

        x = GlobalAveragePooling2D()(x)
        # x = Flatten(name='flatten_1')(x)
        # x = Dense(2048, activation='relu' )(x)
        predictions = Dense(num_classes, activation="softmax")(x)
        # 创建最终模型

        self.model_cnn = keras_model(inputs=Inp, outputs=predictions)
Example #2
0
    def get_pre_train_model_path(self):
        """FLYAI框架下获取预训练模型的PATH"""
        path = remote_helper.get_remote_data('https://www.flyai.com/m/CDial-GPT2_LCCC-base.zip')
        if platform.system() == "Linux":
            path_list = path.split('/')
            path = "/".join(path_list[:-1])
        elif platform.system() == "Windows":
            path_list = path.split('\\')
            path = "\\".join(path_list[:-1])

        logger.info("Model path: {}".format(path))
        return path
Example #3
0
 def __init__(self, preTrained_file='sgns.weibo.bigram-char'):
     wordVecURL = 'https://www.flyai.com/m/sgns.weibo.word.bz2'
     path = remote_helper.get_remote_data(wordVecURL)
     with open('./data/input/model/sgns', 'r') as f:
         for line in f:
             line = line.strip()
             if not line:
                 continue
             index = line.find(' ')
             word = line[:index]
             vector = np.array(line[index:].split(), dtype='float32')
             self.embeddings[word] = vector
 def train(self):
     '''
     训练模型,必须实现此方法
     :return:
     '''
     dataset = FacialBeautyDataset()
     model = osnet_x1_0(num_classes=1,
                        pretrained=True,
                        loss='smoothL1Loss',
                        use_gpu=True)
     # load_pretrained_weights(model, './weights/pretrained/osnet_x1_0_imagenet.pth')
     path = remote_helper.get_remote_data(
         'https://www.flyai.com/m/osnet_x1_0_imagenet.pth')
     load_pretrained_weights(model, path)
     model = model.cuda()
     optimizer = build_optimizer(model)
     max_epoch = args.EPOCHS
     batch_size = args.BATCH
     scheduler = build_scheduler(optimizer,
                                 lr_scheduler='cosine',
                                 max_epoch=max_epoch)
     criterion = nn.SmoothL1Loss()
     model.train()
     train_loader = DataLoader(dataset=dataset,
                               batch_size=batch_size,
                               shuffle=True)
     cudnn.benchmark = True
     for epoch in range(max_epoch):
         for index, data in enumerate(train_loader):
             im, label = data
             im = im.cuda()
             label = label.float().cuda()
             optimizer.zero_grad()
             out = model(im)
             loss = criterion(out, label)
             loss.backward()
             optimizer.step()
             if index % 100 == 0:
                 print("Epoch: [{}/{}][{}/{}]  Loss {:.4f}".format(
                     epoch + 1, max_epoch, index + 1, len(train_loader),
                     loss))
         scheduler.step()
     torch.save(model.state_dict(), 'last.pth')
import sys
import os
from model import KERAS_MODEL_NAME
import WangyiUtilOnFlyai as wangyi
from flyai.utils import remote_helper
from time import clock
from processor import img_size
'''
设置项目的超级参数
'''

try:
    # weights_path =None
    # 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')
except OSError:
    weights_path = 'imagenet'

parser = argparse.ArgumentParser()
parser.add_argument("-e",
                    "--EPOCHS",
                    default=50,
                    type=int,
                    help="train epochs")
parser.add_argument("-b", "--BATCH", default=8, type=int, help="batch size")
args = parser.parse_args()

MODEL_PATH_FILE = os.path.join(MODEL_PATH, KERAS_MODEL_NAME)
Example #6
0
    def __init__(self):
        path = remote_helper.get_remote_data(
            "https://www.flyai.com/m/uncased_L-12_H-768_A-12.zip")
        # 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)