def main(): args = get_args() label_cols = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate'] df_train = pd.read_csv(args["train_file"]) train_datas = df_train['comment_text'].tolist() train_labels = df_train[label_cols].values.tolist() print("train data size: ", len(train_datas)) print("train label size: ", len(train_labels)) train_data, val_data, train_label, val_label = train_test_split(train_datas, train_labels, test_size=0.2, random_state=0) tokenizer = get_tokenizer(args["bert_model_name"], args["pretrain_model_path"]) train_x, train_y = get_model_data(train_data, train_label, tokenizer, args["max_length"]) val_x, val_y = get_model_data(val_data, val_label, tokenizer, args["max_length"]) label_cols = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate'] model = create_model(args["bert_model_name"], len(label_cols)) # 自定义计算f1 score # metrics = Metrics(val_x, val_y) # callbacks = [metrics] # 设置保存最优的模型,保存的是pb模型 callbacks = [ tf.keras.callbacks.ModelCheckpoint( # Path where to save the model # The two parameters below mean that we will overwrite # the current checkpoint if and only if # the `val_loss` score has improved. # The saved model name will include the current epoch. filepath="./output/model/1", # {epoch} save_best_only=True, # Only save a model if `val_loss` has improved. monitor='auc', # 'accuracy', verbose=1, ) ] model.fit(train_x, train_y, epochs=args["epoch"], verbose=1, batch_size=args["batch_size"], callbacks=callbacks, validation_data=(val_x, val_y), validation_batch_size=args["batch_size"]) if not os.path.exists(args["model_path"]): os.makedirs(args["model_path"]) model.save_weights(args["model_path"]) if not os.path.exists(args["pbmodel_path"]): os.makedirs(args["pbmodel_path"]) tf.keras.models.save_model(model, args["pbmodel_path"], save_format="tf")
def subnet1(image, keep_prob, reuse=False): with tf.variable_scope("subnet1", reuse=reuse): def _vgg_net(weights, image): print('setting up vgg model initialized params --> extractor1') layers = ( 'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1', 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2', 'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3', 'relu3_3', 'conv3_4', 'relu3_4', 'pool3', 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3', 'relu4_3', 'conv4_4', 'relu4_4', 'pool4', 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3', 'relu5_3', 'conv5_4', 'relu5_4' ) net = {} current = image for i, name in enumerate(layers): kind = name[:4] if kind == 'conv': kernels, bias = weights[i][0][0][0][0] # kernels are [width, height, in_channles, out_channles] # tensorflow are [height, width, in channles, out_channles] kernels = utils.get_variable( np.transpose(kernels, (1, 0, 2, 3)), name=name + '_w') bias = utils.get_variable(bias.reshape(-1), name=name + '_b') current = utils.conv2d_basic(current, kernels, bias) elif kind == 'relu': current = tf.nn.relu(current, name=name) elif kind == 'pool': current = utils.avg_pool_2x2(current) net[name] = current return net model_data = utils.get_model_data("data", MODEL_URL) weights = np.squeeze(model_data['layers']) with tf.variable_scope('inference'): image_net = _vgg_net(weights, image) conv_final_layer = image_net['relu5_4'] pool5 = utils.max_pool_2x2(conv_final_layer) pool5_flatten = tf.reshape(pool5, [-1, 7 * 7 * 512]) W1 = utils.get_weights_variable(7 * 7 * 512, 4096, name='W1') b1 = utils.get_bias_variable(4096, name='b1') matmul1 = tf.nn.relu(tf.nn.bias_add(tf.matmul(pool5_flatten, W1), b1)) matmul1 = tf.nn.dropout(matmul1, keep_prob) W2 = utils.get_weights_variable(4096, 4096, name='W2') b2 = utils.get_bias_variable(4096, name='b2') matmul2 = tf.nn.bias_add(tf.matmul(matmul1, W2), b2) matmul2 = tf.nn.dropout(matmul2, keep_prob) W3 = utils.get_weights_variable(4096, CLASS_COUNTS, name='W3') b3 = utils.get_bias_variable(CLASS_COUNTS, name='b3') matmul3 = tf.nn.bias_add(tf.matmul(matmul2, W3), b3) return matmul3
def subnet1(image, reuse=False): with tf.variable_scope("subnet1", reuse=reuse): def _vgg_net(weights, image): print('setting up vgg model initialized params --> extractor1') layers = ( 'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1', 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2', 'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3', 'relu3_3', 'conv3_4', 'relu3_4', 'pool3', 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3', 'relu4_3', 'conv4_4', 'relu4_4', 'pool4', 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3', 'relu5_3', 'conv5_4', 'relu5_4' ) net = {} current = image for i, name in enumerate(layers): kind = name[:4] if kind == 'conv': kernels, bias = weights[i][0][0][0][0] # kernels are [width, height, in_channles, out_channles] # tensorflow are [height, width, in channles, out_channles] kernels = utils.get_variable( np.transpose(kernels, (1, 0, 2, 3)), name=name + '_w') bias = utils.get_variable(bias.reshape(-1), name=name + '_b') current = utils.conv2d_basic(current, kernels, bias) elif kind == 'relu': current = tf.nn.relu(current, name=name) elif kind == 'pool': current = utils.avg_pool_2x2(current) net[name] = current return net model_data = utils.get_model_data("data", MODEL_URL) weights = np.squeeze(model_data['layers']) with tf.variable_scope('inference'): image = tf.reshape(image, [-1, IMAGE_SIZE, IMAGE_SIZE, IMAGE_CHANNLES]) image_net = _vgg_net(weights, image) conv_final_layer = image_net['relu5_4'] pool5 = utils.max_pool_2x2(conv_final_layer) W6 = utils.weights_variable([4, 4, 512, 4096], name="W6") b6 = utils.bias_variable([4096], name='b6') conv6 = utils.conv2d_strided(pool5, W6, b6, stride=1) relu6 = tf.nn.relu(conv6, name='relu6') relu6 = utils.max_pool_2x2(relu6) disc_out = tf.reshape(relu6, [-1, 4096]) return disc_out
def inference(self, image): """ Semantic segmentation network definition :param image: input image. Should have values in range 0-255 :param keep_prob: :return: """ print("setting up vgg initialized conv layers ...") model_data = utils.get_model_data('./model_zoo', MODEL_URL) mean = model_data['normalization'][0][0][0] # mean_pixel = np.mean(mean, axis=(0, 1)) weights = np.squeeze(model_data['layers']) # processed_image = utils.process_image(image, mean_pixel) with tf.variable_scope("inference"): image_net = self.vgg_net(weights, image) net = image_net["conv5_2"] map = tf.layers.conv2d(net, 2, kernel_size=3, strides=1, activation=tf.nn.relu, padding='same') out = tf.reduce_mean(map, [1, 2]) return map, out
def inference(image, keep_prob): """ Semantic segmentation network definition :param image: :param keep_prob: :return: """ print('setting up vgg model initialized params') model_data = utils.get_model_data("data", MODEL_URL) mean = model_data['normalization'][0][0][0] mean_pixel = np.mean(mean, axis=(0, 1)) weights = np.squeeze(model_data['layers']) processed_image = utils.process_image(image, mean_pixel) with tf.name_scope('inference'): image_net = vgg_net(weights, processed_image) conv_final_layer = image_net['conv5_3'] pool5 = utils.max_pool_2x2(conv_final_layer) W6 = utils.weights_variable([7, 7, 512, 4096], name="W6") b6 = utils.bias_variable([4096], name='b6') conv6 = utils.conv2d_basic(pool5, W6, b6) relu6 = tf.nn.relu(conv6, name='relu6') relu_dropout6 = tf.nn.dropout(relu6, keep_prob=keep_prob) W7 = utils.weights_variable([1, 1, 4096, 4096], name="W7") b7 = utils.bias_variable([4096], name="b7") conv7 = utils.conv2d_basic(relu_dropout6, W7, b7) relu7 = tf.nn.relu(conv7, name="relu7") relu_dropout7 = tf.nn.dropout(relu7, keep_prob=keep_prob) W8 = utils.weights_variable([1, 1, 4096, NUM_OF_CLASSESS], name='W8') b8 = utils.bias_variable([NUM_OF_CLASSESS], name="b8") conv8 = utils.conv2d_basic(relu_dropout7, W8, b8) #unsampling to actual image size deconv_shape1 = image_net['pool4'].get_shape() W_t1 = utils.weights_variable( [4, 4, deconv_shape1[3].value, NUM_OF_CLASSESS], name='W_t1') b_t1 = utils.bias_variable([deconv_shape1[3].value], name="b_t1") conv_t1 = utils.conv2d_transpose_strided(conv8, W_t1, b_t1, output_shape=tf.shape( image_net['pool4'])) fuse_1 = tf.add(conv_t1, image_net['pool4'], name='fuse_1') deconv_shape2 = image_net['pool3'].get_shape() W_t2 = utils.weights_variable( [4, 4, deconv_shape2[3].value, deconv_shape1[3].value], name='W_t2') b_t2 = utils.bias_variable([deconv_shape2[3].value], name="b_t2") conv_t2 = utils.conv2d_transpose_strided(fuse_1, W_t2, b_t2, output_shape=tf.shape( image_net['pool3'])) fuse_2 = tf.add(conv_t2, image_net["pool3"], name="fuse_2") shape = tf.shape(image) output_shape = tf.stack( [shape[0], shape[1], shape[2], NUM_OF_CLASSESS]) W_t3 = utils.weights_variable( [7, 7, NUM_OF_CLASSESS, deconv_shape2[3].value], name='W_t3') b_t3 = utils.bias_variable([NUM_OF_CLASSESS], name="b_t3") conv_t3 = utils.conv2d_transpose_strided(fuse_2, W_t3, b_t3, output_shape=output_shape) annotation_pre = tf.argmax(conv_t3, dimension=3, name='prediction') return tf.expand_dims(annotation_pre, dim=3), conv_t3
def fine_tune_net(image, keep_prob): """ the network to be fine tuned and used to perform the semantic segmentation :param image: input image. :param keep_prob: for doupout :return: annotation prediction, probability map and 2nd last layer of vgg """ print("setting up vgg initialized conv layers ...") model_data = utils.get_model_data(FLAGS.model_dir, MODEL_URL) mean = model_data['normalization'][0][0][0] mean_pixel = np.mean(mean, axis=(0, 1)) weights = np.squeeze(model_data['layers']) processed_image = image - mean_pixel with tf.variable_scope("fine_tune"): image_net = vgg_net(weights, processed_image) conv_final_layer = image_net["conv5_3"] # 14x14x512 pool5 = utils.max_pool_2x2(conv_final_layer) # 7x7x512 W6 = utils.weight_variable([7, 7, 512, 4096], name="W6") b6 = utils.bias_variable([4096], name="b6") conv6 = utils.conv2d_basic(pool5, W6, b6) relu6 = tf.nn.relu(conv6, name="relu6") relu_dropout6 = tf.nn.dropout(relu6, keep_prob=keep_prob) # 7x7x4096 W7 = utils.weight_variable([1, 1, 4096, 4096], name="W7") b7 = utils.bias_variable([4096], name="b7") conv7 = utils.conv2d_basic(relu_dropout6, W7, b7) relu7 = tf.nn.relu(conv7, name="relu7") relu_dropout7 = tf.nn.dropout(relu7, keep_prob=keep_prob) # 7x7x4096 W8 = utils.weight_variable([1, 1, 4096, NUM_OF_CLASSESS], name="W8") b8 = utils.bias_variable([NUM_OF_CLASSESS], name="b8") conv8 = utils.conv2d_basic(relu_dropout7, W8, b8) # annotation_pred1 = tf.argmax(conv8, dimension=3, name="prediction1") # upscale deconv_shape1 = image_net["pool4"].get_shape() # 14x14x512 W_t1 = utils.weight_variable( [4, 4, deconv_shape1[3].value, NUM_OF_CLASSESS], name="W_t1") b_t1 = utils.bias_variable([deconv_shape1[3].value], name="b_t1") conv_t1 = utils.conv2d_transpose_strided(conv8, W_t1, b_t1, output_shape=tf.shape( image_net["pool4"])) # fuse_1 = tf.add(conv_t1, image_net["pool4"], name="fuse_1") deconv_shape2 = image_net["pool3"].get_shape() W_t2 = utils.weight_variable( [4, 4, deconv_shape2[3].value, deconv_shape1[3].value], name="W_t2") b_t2 = utils.bias_variable([deconv_shape2[3].value], name="b_t2") conv_t2 = utils.conv2d_transpose_strided(fuse_1, W_t2, b_t2, output_shape=tf.shape( image_net["pool3"])) fuse_2 = tf.add(conv_t2, image_net["pool3"], name="fuse_2") shape = tf.shape(image) deconv_shape3 = tf.stack( [shape[0], shape[1], shape[2], NUM_OF_CLASSESS]) W_t3 = utils.weight_variable( [4, 4, NUM_OF_CLASSESS, deconv_shape2[3].value], name="W_t3") b_t3 = utils.bias_variable([NUM_OF_CLASSESS], name="b_t3") conv_t3 = utils.conv2d_transpose_strided(fuse_2, W_t3, b_t3, output_shape=deconv_shape3, stride=8) #conv_t3 = tf.layers.conv2d_transpose(fuse_2,NUM_OF_CLASSESS,16,strides=(8,8),padding='SAME') #conv_t3.set_shape([None,IMAGE_SIZE,IMAGE_SIZE,NUM_OF_CLASSESS]) conv_t3 = tf.nn.softmax(conv_t3, axis=-1) annotation_pred = tf.argmax(conv_t3, axis=-1, name="prediction") return tf.expand_dims(annotation_pred, axis=-1), conv_t3, conv_final_layer
def NetworkModel(image, keep_prob, reuse=False): with tf.variable_scope('NetworkModel', reuse=reuse): def _vgg_net(weights, image): layers = ( 'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1', 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2', 'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3', 'relu3_3', 'conv3_4', 'relu3_4', 'pool3', 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3', 'relu4_3', 'conv4_4', 'relu4_4', 'pool4', 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3', 'relu5_3', 'conv5_4', 'relu5_4' ) net = {} current = image for i, name in enumerate(layers): kind = name[:4] if kind == 'conv': kernels, bias = weights[i][0][0][0][0] kernels = utils.get_variable( np.transpose(kernels, (1, 0, 2, 3)), name=name + '_w') bias = utils.get_variable(bias.reshape(-1), name=name + '_b') current = utils.conv2d_basic(current, kernels, bias) tf.add_to_collection("losses", tf.contrib.layers.l2_regularizer(0.0005)(kernels)) elif kind == 'relu': current = tf.nn.relu(current, name=name) elif kind == 'pool': current = utils.max_pool_2x2(current) net[name] = current return net print('setting up vgg model initialized params --> extractor') model_data = utils.get_model_data("data", MODEL_URL) weights = np.squeeze(model_data['layers']) with tf.name_scope('inference'): image_net = _vgg_net(weights, image) conv_final_layer = image_net['relu5_4'] pool5 = utils.max_pool_2x2(conv_final_layer) pool5_flatten = tf.reshape(pool5, [-1, 7 * 7 * 512]) W1 = utils.get_weights_variable(7 * 7 * 512, 4096, name='W1') tf.add_to_collection("losses", tf.contrib.layers.l2_regularizer(0.0005)(W1)) b1 = utils.get_bias_variable(4096, name='b1') matmul1 = tf.nn.bias_add(tf.matmul(pool5_flatten, W1), b1) matmul1 = tf.nn.relu(matmul1) matmul1 = tf.nn.dropout(matmul1, keep_prob) W2 = utils.get_weights_variable(4096, 4096, name='W2') tf.add_to_collection("losses", tf.contrib.layers.l2_regularizer(0.0005)(W2)) b2 = utils.get_bias_variable(4096, name='b2') matmul2 = tf.nn.bias_add(tf.matmul(matmul1, W2), b2) matmul2 = tf.nn.relu(matmul2) matmul2 = tf.nn.dropout(matmul2, keep_prob) W3 = utils.get_weights_variable(4096, CLASS_COUNTS, name='W3') tf.add_to_collection("losses", tf.contrib.layers.l2_regularizer(0.0005)(W3)) b3 = utils.get_bias_variable(CLASS_COUNTS, name='b3') pre_logits = tf.nn.bias_add(tf.matmul(matmul2, W3), b3) # logits = tf.nn.softmax(pre_logits) # regularizer_losses = tf.get_collection("losses") return pre_logits #, logits
def segmentation(image, keep_prob): """ 图像语义分割模型定义 Parameters ---------- image: 输入图像,每个通道的像素值为0到255 keep_prob: 防止过拟合的dropout参数 Returns ------- """ print("setting up vgg initialized conv layers ...") model_data = utils.get_model_data(FLAGS.model_dir) # vgg模型的权重值 weights = np.squeeze(model_data['layers']) # 计算图片像素值的均值, 然后对图像加上均值 mean = model_data['normalization'][0][0][0] mean_pixel = np.mean(mean, axis=(0, 1)) processed_image = utils.process_image(image, mean_pixel) # 共享变量名空间-segmentation with tf.variable_scope("segmentation"): image_net = vgg_net(weights, processed_image) conv_final_layer = image_net["conv5_3"] pool5 = utils.max_pool_2x2(conv_final_layer) # 全连接层用卷积层来代替 W6 = utils.weight_variable([7, 7, 512, 4096], name = "W6") b6 = utils.bias_variable([4096], name="b6") conv6 = utils.conv2d_basic(pool5, W6, b6) relu6 = tf.nn.relu(conv6, name="relu6") if FLAGS.debug: utils.add_activation_summary(relu6) # 随机去掉一些神经元防止过拟合 relu_dropout6 = tf.nn.dropout(relu6, keep_prob=keep_prob) W7 = utils.weight_variable([1, 1, 4096, 4096], name="W7") b7 = utils.bias_variable([4096], name="b7") conv7 = utils.conv2d_basic(relu_dropout6, W7, b7) relu7 = tf.nn.relu(conv7, name="relu7") if FLAGS.debug: utils.add_activation_summary(relu7) relu_dropout7 = tf.nn.dropout(relu7, keep_prob=keep_prob) W8 = utils.weight_variable([1, 1, 4096, NUM_OF_CLASSESS], name="W8") b8 = utils.bias_variable([NUM_OF_CLASSESS], name="b8") conv8 = utils.conv2d_basic(relu_dropout7, W8, b8) # annotation_pred1 = tf.argmax(conv8, dimension=3, name="prediction1") # now to upscale to actual image size deconv_shape1 = image_net["pool4"].get_shape() W_t1 = utils.weight_variable([4, 4, deconv_shape1[3].value, NUM_OF_CLASSESS], name="W_t1") b_t1 = utils.bias_variable([deconv_shape1[3].value], name="b_t1") conv_t1 = utils.conv2d_transpose_strided(conv8, W_t1, b_t1, output_shape=tf.shape(image_net["pool4"])) fuse_1 = tf.add(conv_t1, image_net["pool4"], name="fuse_1") deconv_shape2 = image_net["pool3"].get_shape() W_t2 = utils.weight_variable([4, 4, deconv_shape2[3].value, deconv_shape1[3].value], name="W_t2") b_t2 = utils.bias_variable([deconv_shape2[3].value], name="b_t2") conv_t2 = utils.conv2d_transpose_strided(fuse_1, W_t2, b_t2, output_shape=tf.shape(image_net["pool3"])) fuse_2 = tf.add(conv_t2, image_net["pool3"], name="fuse_2") shape = tf.shape(image) deconv_shape3 = tf.stack([shape[0], shape[1], shape[2], NUM_OF_CLASSESS]) W_t3 = utils.weight_variable([16, 16, NUM_OF_CLASSESS, deconv_shape2[3].value], name="W_t3") b_t3 = utils.bias_variable([NUM_OF_CLASSESS], name = "b_t3") conv_t3 = utils.conv2d_transpose_strided(fuse_2, W_t3, b_t3, output_shape = deconv_shape3, stride = 8) # 预测结果层 annotation_pred = tf.argmax(conv_t3, dimension = 3, name = "prediction") return tf.expand_dims(annotation_pred, dim = 3), conv_t3
def inference(image, keep_prob, train=False): """ Semantic segmentation network definition :param image: input image. Should have values in range 0-255 :param keep_prob: :return: """ print("setting up vgg initialized conv layers ...") model_data = utils.get_model_data(FLAGS.model_dir, MODEL_URL) mean = model_data['normalization'][0][0][0] mean_pixel = np.mean(mean, axis=(0, 1)) weights = np.squeeze(model_data['layers']) # accounts for the mean being subtracted from the image processed_image = utils.process_image(image, mean_pixel) with tf.variable_scope("inference"): image_net = vgg_net(weights, processed_image) conv_final_layer = image_net["conv5_3"] pool5 = utils.max_pool_2x2(conv_final_layer) W6 = utils.weight_variable([7, 7, 512, 4096], name="W6") b6 = utils.bias_variable([4096], name="b6") conv6 = utils.conv2d_basic(pool5, W6, b6) relu6 = tf.nn.relu(conv6, name="relu6") if FLAGS.debug: utils.add_activation_summary(relu6) if train: relu6 = tf.nn.dropout(relu6, keep_prob=keep_prob) W7 = utils.weight_variable([1, 1, 4096, 4096], name="W7") b7 = utils.bias_variable([4096], name="b7") conv7 = utils.conv2d_basic(relu6, W7, b7) relu7 = tf.nn.relu(conv7, name="relu7") if FLAGS.debug: utils.add_activation_summary(relu7) if train: relu7 = tf.nn.dropout(relu7, keep_prob=keep_prob) W8 = utils.weight_variable([1, 1, 4096, FLAGS.NUM_OF_CLASSES], name="W8") b8 = utils.bias_variable([FLAGS.NUM_OF_CLASSES], name="b8") conv8 = utils.conv2d_basic(relu7, W8, b8) # annotation_pred1 = tf.argmax(conv8, dimension=3, name="prediction1") # now to upscale to actual image size deconv_shape1 = image_net["pool4"].get_shape() W_t1 = utils.weight_variable([4, 4, deconv_shape1[3].value, FLAGS.NUM_OF_CLASSES], name="W_t1") b_t1 = utils.bias_variable([deconv_shape1[3].value], name="b_t1") conv_t1 = utils.conv2d_transpose_strided(conv8, W_t1, b_t1, output_shape=tf.shape(image_net["pool4"])) fuse_1 = tf.add(conv_t1, image_net["pool4"], name="fuse_1") deconv_shape2 = image_net["pool3"].get_shape() W_t2 = utils.weight_variable([4, 4, deconv_shape2[3].value, deconv_shape1[3].value], name="W_t2") b_t2 = utils.bias_variable([deconv_shape2[3].value], name="b_t2") conv_t2 = utils.conv2d_transpose_strided(fuse_1, W_t2, b_t2, output_shape=tf.shape(image_net["pool3"])) fuse_2 = tf.add(conv_t2, image_net["pool3"], name="fuse_2") shape = tf.shape(image) deconv_shape3 = tf.pack([shape[0], shape[1], shape[2], FLAGS.NUM_OF_CLASSES]) W_t3 = utils.weight_variable([16, 16, FLAGS.NUM_OF_CLASSES, deconv_shape2[3].value], name="W_t3") b_t3 = utils.bias_variable([FLAGS.NUM_OF_CLASSES], name="b_t3") conv_t3 = utils.conv2d_transpose_strided(fuse_2, W_t3, b_t3, output_shape=deconv_shape3, stride=8) annotation_pred = tf.argmax(conv_t3, dimension=3, name="prediction") return tf.expand_dims(annotation_pred, dim=3), conv_t3
def main(): args = get_args() train_df = pd.read_csv(args["train_file"]) train_df = shuffle(train_df) train_datas = train_df["content"].tolist() train_label_total = train_df["label"].unique().tolist() print("total data size: {}".format(len(train_datas))) # get lable dict label_list = read_dict(args["labeldict"])["label"] if not os.path.exists(args["labeldict"]): for label in train_label_total: if "|" in label: temp = label.split("|") for item in temp: if item not in label_list: label_list.append(item) else: if label not in label_list: label_list.append(label) print("label cate size: {}".format(len(label_list))) label_dict = {"label": label_list} with open(args["labeldict"], "w", encoding="utf-8") as f: f.write(json.dumps(label_dict, ensure_ascii=False, indent=4)) # label encoder train_labels = label_encoder(train_df["label"].tolist(), label_list) train_data, val_data, train_label, val_label = train_test_split( train_datas, train_labels, test_size=0.2, random_state=0) print("train data size: {}".format(len(train_data))) print("val data size: {}".format(len(val_data))) tokenizer = get_tokenizer(args["bert_model_name"], args["pretrain_model_path"]) train_x, train_y = get_model_data(train_data, train_label, tokenizer, args["max_length"]) val_x, val_y = get_model_data(val_data, val_label, tokenizer, args["max_length"]) model = create_model(args["bert_model_name"], len(label_list)) if not os.path.exists(args["model_path"]): os.makedirs(args["model_path"]) if not os.path.exists(args["pbmodel_path"]): os.makedirs(args["pbmodel_path"]) # 设置保存最优的模型,保存的是pb模型 callbacks = [ tf.keras.callbacks.ModelCheckpoint( # Path where to save the model # The two parameters below mean that we will overwrite # the current checkpoint if and only if # the `val_loss` score has improved. # The saved model name will include the current epoch. filepath=args["model_path"], # {epoch} save_best_only=True, # Only save a model if `val_loss` has improved. monitor='val_auc', # 'accuracy', verbose=1, mode='max') ] model.fit(train_x, train_y, epochs=args["epoch"], verbose=1, batch_size=args["batch_size"], callbacks=callbacks, validation_data=(val_x, val_y), validation_batch_size=args["batch_size"]) model_path = os.path.join("./output/model/", "mulclassifition.h5") model.save_weights(model_path) tf.keras.models.save_model(model, args["pbmodel_path"], save_format="tf", overwrite=True)