def __init__(self, input_channel=1, output_features=22): super(FCN, self).__init__() self.feature = Unet(1, 3) self.layer1 = nn.Sequential(nn.Linear(256 * 256 * 3, 512), nn.ReLU(True)) self.layer2 = nn.Sequential(nn.Linear(512, output_features), nn.ReLU(True))
def main(): import argparse parser = argparse.ArgumentParser() parser.add_argument('-w', '--weight_path', default='models/table-line-fine.h5') parser.add_argument('-o', '--output_path', default='models/table-line/100000') args = parser.parse_args() weight_path = args.weight_path output_path = args.output_path # # 加载网络模型 model = Unet((1024, 1024, 3), 2) # 加载权重模型 model.load_weights(weight_path) # model.summary() h5_to_pb(model, output_path) print('model saved')
# 种类少(几类)时,设置为True # 种类多(十几类)时,如果batch_size比较大(10以上),那么设置为True # 种类多(十几类)时,如果batch_size比较小(10以下),那么设置为False #---------------------------------------------------------------------# dice_loss = False #-------------------------------# # 主干网络预训练权重的使用 #-------------------------------# pretrained = False #-------------------------------# # Cuda的使用 #-------------------------------# Cuda = True model = Unet(num_classes=NUM_CLASSES, in_channels=inputs_size[-1], pretrained=pretrained).train() #-------------------------------------------# # 权值文件的下载请看README # 权值和主干特征提取网络一定要对应 #-------------------------------------------# model_path = r"model_data/unet_voc.pth" print('Loading weights into state dict...') device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model_dict = model.state_dict() pretrained_dict = torch.load(model_path, map_location=device) pretrained_dict = { k: v for k, v in pretrained_dict.items() if np.shape(model_dict[k]) == np.shape(v)
from nets.unet import Unet model = Unet([512,512,3],21) model.summary() for i,layer in enumerate(model.layers): print(i,layer.name)
#--------------------------------------------# # 该部分代码只用于看网络结构,并非测试代码 #--------------------------------------------# from nets.unet import Unet if __name__ == "__main__": model = Unet([512, 512, 3], 21, 'resnet50') model.summary() for i,layer in enumerate(model.layers): print(i,layer.name)
#------------------------------# inputs_size = [512, 512, 3] #--------------------------------------------------------------------# # 简单的医药分割只分背景和边缘 #--------------------------------------------------------------------# num_classes = 2 #--------------------------------------------------------------------# # 建议选项: # 种类少(几类)时,设置为True # 种类多(十几类)时,如果batch_size比较大(10以上),那么设置为True # 种类多(十几类)时,如果batch_size比较小(10以下),那么设置为False #---------------------------------------------------------------------# dice_loss = True # 获取model model = Unet(inputs_size, num_classes) #-------------------------------------------# # 权值文件的下载请看README # 权值和主干特征提取网络一定要对应 #-------------------------------------------# model_path = "./model_data/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5" model.load_weights(model_path, by_name=True, skip_mismatch=True) # 打开数据集的txt with open(r"./Medical_Datasets/ImageSets/Segmentation/train.txt", "r") as f: train_lines = f.readlines() #-------------------------------------------------------------------------------# # 训练参数的设置
def main(_): tf.logging.set_verbosity(tf.logging.INFO) # TensorFlow session: grow memory when needed. TF, DO NOT USE ALL MY GPU MEMORY!!! gpu_options = tf.GPUOptions(allow_growth=True) config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options) sess = tf.InteractiveSession(config=config) tf.reset_default_graph() X = tf.placeholder(tf.float32, shape=[None, IMG_HEIGHT, IMG_WIDTH, 3], name="X") mode = tf.placeholder(tf.bool, name="mode") # training or not pred = Unet(X, mode, FLAGS) # evaluation = tf.argmax(logits, 1) sess.run(tf.global_variables_initializer()) # Restore variables from training checkpoints. saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) # Assuming model_checkpoint_path looks something like: # /my-favorite-path/imagenet_train/model.ckpt-0, # extract global_step from it. global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] print('Successfully loaded model from %s at step=%s.' % (ckpt.model_checkpoint_path, global_step)) else: print('No checkpoint file found at %s' % FLAGS.checkpoint_dir) return ############################ # Get data ############################ raw = Data(FLAGS.data_dir) test_data = DataLoader(raw.data_dir, raw.get_data('prediction'), FLAGS.batch_size) iterator = tf.data.Iterator.from_structure(test_data.dataset.output_types, test_data.dataset.output_shapes) next_batch = iterator.get_next() # Ops for initializing the two different iterators test_init_op = iterator.make_initializer(test_data.dataset) test_batches_per_epoch = int(test_data.data_size / FLAGS.batch_size) if test_data.data_size % FLAGS.batch_size > 0: test_batches_per_epoch += 1 ################################################## # start test & make csv file. ################################################## start_time = datetime.datetime.now() print("Start test: {}".format(start_time)) submission = dict() # Initialize iterator with the test dataset sess.run(test_init_op) for i in range(test_batches_per_epoch): batch_xs, fnames = sess.run(next_batch) pred = sess.run(pred, feed_dict={ X: batch_xs, mode: False, }) # TODO # size = len(fnames) # for n in xrange(0, size): # submission[fnames[n].decode('UTF-8')] = id2name[pred[n]] # # count += size # print(count, ' completed') end_time = datetime.datetime.now() print('{} Data, End prediction: {}'.format(test_data.data_size, end_time)) print('prediction waste time: {}'.format(end_time - start_time))
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Thu Sep 9 23:11:51 2020 table line detect @author: chineseocr """ from utils.config import tableModeLinePath from utils.utils import letterbox_image,get_table_line,adjust_lines,line_to_line import numpy as np import cv2 from nets.unet import unet as Unet model = Unet((None,None,3),2) model.load_weights(tableModeLinePath) def table_line(img,size=(512,512),hprob=0.5,vprob=0.5,row=50,col=10,alph=15): sizew,sizeh = size inputBlob,fx,fy = letterbox_image(img[...,::-1],(sizew,sizeh)) pred = model.predict(np.array([np.array(inputBlob)/255.0])) pred = pred[0] vpred = pred[...,1]>vprob##竖线 hpred = pred[...,0]>hprob##横线 vpred = vpred.astype(int) hpred = hpred.astype(int) colboxes = get_table_line(vpred,axis=1,lineW=col) rowboxes = get_table_line(hpred,axis=0,lineW=row) # ccolbox = [] # crowlbox= [] if len(rowboxes)>0:
#---------------------------------------------------------------------# # 是否进行冻结训练,默认先冻结主干训练后解冻训练。 #---------------------------------------------------------------------# Freeze_Train = True #---------------------------------------------------------------------# # 用于设置是否使用多线程读取数据,1代表关闭多线程 # 开启后会加快数据读取速度,但是会占用更多内存 # keras里开启多线程有些时候速度反而慢了许多 # 在IO为瓶颈的时候再开启多线程,即GPU运算速度远大于读取图片的速度。 #---------------------------------------------------------------------# num_workers = 1 #------------------------------------------------------# # 获取model #------------------------------------------------------# model = Unet([input_shape[0], input_shape[1], 3], num_classes, backbone) if model_path != '': #------------------------------------------------------# # 载入预训练权重 #------------------------------------------------------# model.load_weights(model_path, by_name=True, skip_mismatch=True) #---------------------------# # 读取数据集对应的txt #---------------------------# with open(os.path.join(VOCdevkit_path, "ImageSets/Segmentation/train.txt"), "r") as f: train_lines = f.readlines() #-------------------------------------------------------------------------------# # 训练参数的设置
def main(_): # We want to see all the logging messages for this tutorial. tf.logging.set_verbosity(tf.logging.INFO) tf.reset_default_graph() X = tf.placeholder(tf.float32, shape=[None, IMG_HEIGHT, IMG_WIDTH, 3], name="X") y = tf.placeholder(tf.float32, shape=[None, IMG_HEIGHT, IMG_WIDTH, 1], name="y") mode = tf.placeholder(tf.bool, name="mode") # training or not pred = Unet(X, mode, FLAGS) tf.add_to_collection("inputs", X) tf.add_to_collection("inputs", mode) tf.add_to_collection("outputs", pred) tf.summary.histogram("Predicted Mask", pred) tf.summary.image("Predicted Mask", pred) # Updates moving mean and moving variance for BatchNorm (train/inference) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = make_train_op(pred, y) IOU_op = _IOU(pred, y) # IOU_op = -_IOU(pred, y) tf.summary.scalar("IOU", IOU_op) sess = tf.Session() sess.run(tf.global_variables_initializer()) summary_op = tf.summary.merge_all() train_summary_writer = tf.summary.FileWriter(FLAGS.logdir + '/train', sess.graph) val_summary_writer = tf.summary.FileWriter(FLAGS.logdir + '/validation') saver = tf.train.Saver() start_epoch = 1 epoch_from_ckpt = 0 if FLAGS.ckpt_dir: saver.restore(sess, FLAGS.ckpt_dir) tmp = FLAGS.ckpt_dir tmp = tmp.split('-') tmp.reverse() epoch_from_ckpt = int(tmp[0]) start_epoch = epoch_from_ckpt + 1 if epoch_from_ckpt != FLAGS.epochs + 1: tf.logging.info('Training from epoch: %d ', start_epoch) # Saving as Protocol Buffer (pb) tf.train.write_graph(sess.graph_def, FLAGS.train_dir, 'unet.pbtxt', as_text=True) ############################ # Get data ############################ raw = Data(FLAGS.data_dir, FLAGS.validation_percentage) tr_data = DataLoader(raw.data_dir, raw.get_data('training'), FLAGS.batch_size) val_data = DataLoader(raw.data_dir, raw.get_data('validation'), FLAGS.batch_size) iterator = tf.data.Iterator.from_structure(tr_data.dataset.output_types, tr_data.dataset.output_shapes) next_batch = iterator.get_next() # Ops for initializing the two different iterators tr_init_op = iterator.make_initializer(tr_data.dataset) val_init_op = iterator.make_initializer(val_data.dataset) tr_batches_per_epoch = int(tr_data.data_size / FLAGS.batch_size) if tr_data.data_size % FLAGS.batch_size > 0: tr_batches_per_epoch += 1 val_batches_per_epoch = int(val_data.data_size / FLAGS.batch_size) if val_data.data_size % FLAGS.batch_size > 0: val_batches_per_epoch += 1 ############################ # Training ############################ print("{} Training start ++++++++ ".format(datetime.datetime.now())) for epoch in xrange(start_epoch, FLAGS.epochs + 1): tf.logging.info('epoch #%d start >>> ', epoch) sess.run(tr_init_op) for step in range(tr_batches_per_epoch): X_train, y_train = sess.run(next_batch) train_summary, accuracy, _ = \ sess.run([summary_op, IOU_op, train_op], feed_dict={X: X_train, y: y_train, mode: True}) train_summary_writer.add_summary(train_summary, step) tf.logging.info('epoch #%d, step #%d/%d, accuracy(iou) %.5f%%' % (epoch, step, tr_batches_per_epoch, accuracy)) print("{} Validation start ++++++++ ".format(datetime.datetime.now())) total_val_accuracy = 0 val_count = 0 sess.run(val_init_op) for n in range(val_batches_per_epoch): X_val, y_val = sess.run(next_batch) val_summary, val_accuracy = \ sess.run([summary_op, IOU_op], feed_dict={X: X_val, y: y_val, mode: False}) # total_val_accuracy += val_step_iou * X_val.shape[0] total_val_accuracy += val_accuracy val_count += 1 val_summary_writer.add_summary(val_summary, epoch) tf.logging.info('step #%d/%d, accuracy(iou) %.5f%%' % (n, val_batches_per_epoch, total_val_accuracy * 100)) total_val_accuracy /= val_count tf.logging.info('step %d: Validation accuracy = %.1f%% (N=%d)' % (epoch, total_val_accuracy * 100, raw.get_size('validation'))) checkpoint_path = os.path.join(FLAGS.train_dir, 'unet.ckpt') tf.logging.info('Saving to "%s-%d"', checkpoint_path, epoch) saver.save(sess, checkpoint_path, global_step=epoch)
#--------------------------------------------# # 该部分代码只用于看网络结构,并非测试代码 #--------------------------------------------# import torch from torchsummary import summary from nets.unet import Unet if __name__ == "__main__": model = Unet(num_classes=2).train().cuda() summary(model, (3, 512, 512))
pr = self.net(images)[0] pr = F.softmax(pr.permute(1, 2, 0), dim=-1).cpu().numpy().argmax(axis=-1) pr = pr[int((self.model_image_size[0] - nh) // 2):int((self.model_image_size[0] - nh) // 2 + nh), int((self.model_image_size[1] - nw) // 2):int((self.model_image_size[1] - nw) // 2 + nw)] t1 = time.time() for _ in range(test_interval): with torch.no_grad(): pr = self.net(images)[0] pr = F.softmax(pr.permute(1, 2, 0), dim=-1).cpu().numpy().argmax(axis=-1) pr = pr[int((self.model_image_size[0] - nh) // 2):int((self.model_image_size[0] - nh) // 2 + nh), int((self.model_image_size[1] - nw) // 2):int((self.model_image_size[1] - nw) // 2 + nw)] t2 = time.time() tact_time = (t2 - t1) / test_interval return tact_time unet = FPS_Unet() test_interval = 100 img = Image.open('img/street.jpg') tact_time = unet.get_FPS(img, test_interval) print( str(tact_time) + ' seconds, ' + str(1 / tact_time) + 'FPS, @batch_size 1')
#------------------------------# # 分类个数+1 # 种类+背景 #------------------------------# num_classes = 7 #--------------------------------------------------------------------# # dice loss #---------------------------------------------------------------------# dice_loss = True #------------------------------# # 数据集路径 #------------------------------# dataset_path = "dataset_processing/classfication/" # 获取model model = Unet(inputs_size, num_classes) #-------------------------------------------# # 权值文件的下载请看README # 权值和主干特征提取网络一定要对应 #-------------------------------------------# model_path = "model_data/unet_voc.h5" model.load_weights(model_path, by_name=True, skip_mismatch=True) # 打开数据集的txt with open(os.path.join(dataset_path, "ImageSets/Segmentation/train.txt"), "r") as f: train_lines = f.readlines() # 打开数据集的txt with open(os.path.join(dataset_path, "ImageSets/Segmentation/val.txt"),