示例#1
0
def main(_):
    # Print FLAGS values
    pprint(FLAGS.flag_values_dict())

    # Define GPU configuration
    os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu
    gpu_config = tf.ConfigProto()
    gpu_config.gpu_options.allow_growth = True

    # Define model name
    if not FLAGS.phase:
        setup_list = [
            f"ngf_{FLAGS.ngf}", f"ndf_{FLAGS.ndf}", f"nz_{FLAGS.nz}",
            f"lrD_{FLAGS.lrD}", f"lrG_{FLAGS.lrG}", f"var_{FLAGS.var}",
            f"phase_{FLAGS.phase}", f"da_{FLAGS.da}", f"clip_{FLAGS.clip}"
        ]
    else:
        setup_list = [
            f"ngf_{FLAGS.ngf}", f"ndf_{FLAGS.ndf}", f"nz_{FLAGS.nz}",
            f"lrD_{FLAGS.lrD}", f"lrG_{FLAGS.lrG}", f"var_{FLAGS.var}",
            f"phase_{FLAGS.phase}", f"da_{FLAGS.da}", f"clip_{FLAGS.clip}",
            f"nhl_{FLAGS.nhl}", f"nhw_{FLAGS.nhw}"
        ]

    model_name = '_'.join(setup_list)
    print(f"Model name: {model_name}")

    M = model(FLAGS, gpu_config)
    M.sess.run(tf.global_variables_initializer())

    if FLAGS.phase:
        # Previously learned autoencoder model name
        setup_list = [
            f"ngf_{FLAGS.ngf}", f"ndf_{FLAGS.ndf}", f"nz_{FLAGS.nz}",
            f"lrD_{FLAGS.lrD}", f"lrG_{FLAGS.lrG}", f"var_{FLAGS.var}",
            f"phase_0", f"da_{FLAGS.da}", f"clip_{FLAGS.clip}"
        ]
        lgan_name = '_'.join(setup_list)
        # just for now
        # lgan_name = 'ngf_64_ndf_64_nz_64_lrD_5e-05_lrG_0.001_dg_1_aug_0_lw_20.0_ow_0.01_var_3.0_phase_0_nosig'
        # lgan_name = 'ngf_64_ndf_64_nz_16_lw_20.0_ow_0.01_var_3.0_phase_0'
        var_lgan = tf.get_collection('trainable_variables', 'lgan/gen')
        path = tf.train.latest_checkpoint(
            os.path.join(FLAGS.ckptdir, lgan_name))
        tf.train.Saver(var_lgan).restore(M.sess, path)
        print(colored(f"LGAN model is restored from {path}", "blue"))

    saver = tf.train.Saver()

    # Train the main model
    train(M, FLAGS, saver=saver, model_name=model_name)
示例#2
0
 def build_model(self):
   if (self.config.model_choice==0):
     logits=model0.model(self)
   elif (self.config.model_choice==1):
     logits=model1(self)      
   elif (self.config.model_choice==2):
     logits=model2.model(self)      
   elif (self.config.model_choice==3):
     logits=model3.model(self)      
   elif (self.config.model_choice==4):
     logits=model4.model(self)      
   elif (self.config.model_choice==5):
     logits=model5.model(self)      
   elif (self.config.model_choice==6):
     logits=model6.model(self)      
   elif (self.config.model_choice==7):
     logits=model7.model(self)            
   
   # cast Logits and Label
   logits = tf.cast(logits, tf.float64)
   label=tf.cast(self.label, tf.int64)  #(batch)
   
   #Compute batch loss
   losses_t = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=label,logits=logits)
   batch_loss = tf.reduce_mean(losses_t, name='batch_loss') 
   self.batch_loss= batch_loss
   
   #add batch loss to the collection of losses 
   batch_loss = tf.cast(batch_loss, tf.float32)
   self.batch_loss=batch_loss
   tf.losses.add_loss(batch_loss)
   total_loss = tf.losses.get_total_loss(add_regularization_losses=False)
   
   #determine the total number of parameters
   total_parameters=0
   
   for var in tf.trainable_variables():
     tf.summary.histogram("parameters/" + var.op.name, var)
       shape = var.get_shape()
       variable_parametes = 1
       for dim in shape:
         variable_parametes *= dim.value
         total_parameters += variable_parametes
示例#3
0
def setup_model(hps, sess):

    # Create tensorflow session

    # Download and load dataset.
    print(hvd.rank() + hvd.size() * hps.seed)

    tf.compat.v1.set_random_seed(hvd.rank() + hvd.size() * hps.seed)
    np.random.seed(hvd.rank() + hvd.size() * hps.seed)

    # Get data and set train_its and valid_its
    train_iterator, test_iterator, data_init = get_data(hps, sess)
    hps.train_its, hps.test_its, hps.full_test_its = get_its(hps)

    # Create log dir
    # Create model
    import model2
    model = model2.model(sess, hps, train_iterator, test_iterator, data_init)

    return model
    # Initialize visualization functions
    visualise = init_visualizations(hps, model, logdir)
示例#4
0
    parser.add_argument('--checkpoint_file',
                        type=str,
                        help='Path to model checkpoint file')
    parser.add_argument('--log_file', type=str, help='Path to log file')
    parser.add_argument(
        '--continue_model',
        type=str,
        default=None,
        help='Whether to continue training from the given checkpoint file')

    args = parser.parse_args()

    if K.image_data_format() == 'channels_first':
        input_shape = (3, args.image_width, args.image_height)
    else:
        input_shape = (args.image_width, args.image_height, 3)

    if (args.continue_model is not None):
        model = load_model(args.continue_model,
                           custom_objects={
                               'accuracy': accuracy,
                               'maxout': maxout,
                               'maxout_shape': maxout_shape
                           })
    else:
        model = model(input_shape)
    train(model, args.train_dir, args.validation_dir, args.num_epochs,
          args.batch_size, args.learning_rate,
          (args.image_width, args.image_height), args.checkpoint_file,
          args.checkpoint_dir, args.log_file)
示例#5
0
from model2 import model
from data_base import image_cols, image_rows, data_base
import tensorflow as tf
import os
import cv2
import numpy as np

os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

ID_num = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "X", "e"]

batch = 1

x = tf.placeholder(tf.float32, [batch, image_rows, image_cols, 3])

ID = model(x)
saver = tf.train.Saver()

data = data_base("/media/shilei/ʩ/picture")

with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)

    ckpt = tf.train.get_checkpoint_state("./checkpoints")
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)

    for step in range(1000):
        images = []
        image = data.test_image()
import tensorflow as tf
import get_data
import model
import model2
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import cv2


X, Y, y = model2.model(tf.contrib.layers.l2_regularizer(0.0001))
cameraCapture = cv2.VideoCapture(0)
saver = tf.train.Saver()
save_path = r'./checkpoint_64w_average_op/'
def find_face(image):
    c = sess.run(y, feed_dict={X: image})
    c = c[0][:]
    a1_x = int(c[0] - 10)
    a1_y = int(c[1] - 10)
    a2_x = int(c[2] + 10)
    a2_y = a1_y
    a3_x = a2_x
    a3_y = int(c[7] + 10)
    a4_x = a1_x
    a4_y = a3_y
    cv2.line(image_show, (a1_x * 2, a1_y * 2),
             (a2_x * 2, a2_y * 2), (255, 155, 155), 5)
    cv2.line(image_show, (a2_x * 2, a2_y * 2),
             (a3_x * 2, a3_y * 2), (255, 155, 155), 5)
    cv2.line(image_show, (a3_x * 2, a3_y * 2),
             (a4_x * 2, a4_y * 2), (255, 155, 155), 5)
示例#7
0
def save_pb(sess):
    constant_graph = graph_util.convert_variables_to_constants(
        sess, sess.graph_def, ['result'])
    with tf.gfile.FastGFile("savePB/model.pb", mode='wb') as f:
        f.write(constant_graph.SerializeToString())


os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

batch = 8

x = tf.placeholder(tf.float32, [batch, image_rows, image_cols, 3],
                   name="image")
target = tf.placeholder(tf.float32, [batch, 18, 12])

ID, Loss = model(x, target)
global_step = tf.Variable(0, trainable=False)
train_step = tf.train.AdamOptimizer(1e-3).minimize(Loss,
                                                   global_step=global_step)

saver = tf.train.Saver()

data = data_base("/home/shilei/CLionProjects/ID_card/")

with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)

    ckpt = tf.train.get_checkpoint_state("./checkpoints")
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
示例#8
0
with slim.arg_scope(pre_trained_resnet.resnet_arg_scope(0.0)):
    net, _ = pre_trained_resnet.resnet_v1_50(x1)
    net = tf.squeeze(net, axis=[1, 2])  # 去除第一、第二个维度
    logits1 = slim.fully_connected(net, num_outputs=class_num,
                              activation_fn=None, scope='predict')

with slim.arg_scope(vgg.vgg_arg_scope(0.0)):
    logits2, _ = vgg.vgg_16(x2, class_num, False, 1.0)
"""
# net, _ = resnet_v1.resnet_v1_26(x1, is_training=False)
# net = tf.squeeze(net, axis=[1, 2])  # 去除第一、第二个维度
# logits1 = slim.fully_connected(net, num_outputs=class_num,
#                             activation_fn=None, scope='predict')

logits1 = model1.model(x1, False, 0.0, class_num, 0.0)
logits2 = model2.model(x2, False, 0.0, class_num, 0.0)

logits = tf.nn.softmax(logits1 + logits2)
logits = tf.clip_by_value(logits, 1e-10, 1)
result = tf.argmax(logits, 1)

sess = tf.Session()
saver = tf.train.Saver()
saver.restore(
    sess, './checkpoint/new_val_double_vgg_0.769/val_double_vgg_model_26.ckpt')

n_test_samples = s2_test.shape[0]
batch_size = 200
result_list = []
for i in range(0, n_test_samples, batch_size):
    start_pos = i
示例#9
0
                for num in each_class_num]
# tensorflow 深度学习模型设置

weight_decay = 0.001
dropout_rate = 0.5
is_training = True
x1 = tf.placeholder(tf.float32, [None, 32, 32, 8], name="x1")
x2 = tf.placeholder(tf.float32, [None, 32, 32, 10], name="x2")
y_ = tf.placeholder(tf.int64, [None], name="y_")

# net, _ = resnet_v1.resnet_v1_26(x1)
# net = tf.squeeze(net, axis=[1, 2])  # 去除第一、第二个维度
# y1 = slim.fully_connected(net, num_outputs=class_num,
#                          activation_fn=None, scope='predict')
y1 = model1.model(x1, is_training, dropout_rate, class_num, weight_decay)
y2 = model2.model(x2, is_training, dropout_rate, class_num, weight_decay)
# net, _ = resnet_v1.resnet_v1_26(x1)
# net = tf.squeeze(net, axis=[1, 2])  # 去除第一、第二个维度
# logits = slim.fully_connected(net, num_outputs=class_num,
#                              activation_fn=None, scope='predict')

gamma = 2
# sen1 和 sen2的权重
sen1_weight = 0.1
# 计算交叉熵及其平均值
with tf.name_scope('training'):
    labels = tf.one_hot(y_, class_num)
    logits = tf.nn.softmax(y1 + y2)
    logits = tf.clip_by_value(logits, 1e-10, 1.0)
    # loss = -tf.reduce_mean(tf.reduce_sum(labels * tf.log(logits)))
    class_weight = np.array(class_weight)
示例#10
0
def run(image_shape, train_list, num_classes):
    # Check for a GPU
    if not tf.test.gpu_device_name():
        warnings.warn(
            'No GPU found. Please use a GPU to train your neural network.')
    else:
        print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
    epochs = 1
    batch_size = 2

    runs_dir = './runs'
    restore_dir = "./model/checkpoints/"
    log_path = '/tmp/tensorboard/data/sem_seg'

    # Create function to get batches
    train_generator = gen.gen_batch_function(train_list, num_classes)

    if os.path.isfile(restore_dir + 'model_final.meta'):
        # continue training from the last checkpoint
        with tf.Session() as sess:
            x_input, y_label, learning_rate, is_training, merged_summary, model_output, optimiser, loss = load_last_model(
                sess, restore_dir)

            val_mean_iou = evaluate(model_output, y_label, num_classes)

            sess.run(tf.local_variables_initializer())

            train_nn(sess, epochs, batch_size, train_generator, optimiser,
                     loss, x_input, y_label, learning_rate, is_training,
                     val_mean_iou, merged_summary, log_path,
                     restore_dir + 'model_final')

            #predict(logits)

            # Save inference data using helper.save_inference_samples
            helper.save_inference_samples(runs_dir, sess, image_shape, logits,
                                          x_input, is_training)

    else:
        # create a new model and start training
        x_input, learning_rate, is_training, y_label = Placeholders(
            image_shape, num_classes)
        #nn_last_layer = model.networkModel(x_input, num_classes, 'model', is_training)
        nn_last_layer = model2.model(x_input,
                                     num_classes,
                                     is_training,
                                     name='model')
        logits, loss, optimiser, mean_iou, merged_summary = create_optimiser(
            nn_last_layer, y_label, learning_rate, num_classes)

        val_mean_iou = evaluate(nn_last_layer, y_label, num_classes)

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())

            #train_model(sess, x, y, merged_summary, training, accuracy)
            train_nn(sess, epochs, batch_size, train_generator, optimiser,
                     loss, x_input, y_label, learning_rate, is_training,
                     val_mean_iou, merged_summary, log_path,
                     restore_dir + 'model_final')

            #predict(logits)

            # Save inference data using helper.save_inference_samples
            helper.save_inference_samples(runs_dir, sess, image_shape, logits,
                                          x_input, is_training)

    print("\nFinished")
    print("Run the command line:\n"
          "--> tensorboard --logdir={} "
          "\nThen open http://0.0.0.0:6006/ into your web browser".format(
              log_path))
# -*- coding:utf-8 -*-
"""
	Start entry for training and testing
"""
import tensorflow as tf
import os
from model2 import model
import numpy as np

from project_config import *

if __name__ =='__main__':
    print(" beigin...")
    epoch = 5
    model = model(learning_rate,keep_prob,batch_size,epoch)
    model.inference(normalization_output_path,test_path,2,True)