2017-09-08 22 views
7

मैं अभी भी tensorflow में नौसिखिया हूं इसलिए मुझे खेद है कि यह एक बेवकूफ सवाल है। मैं इस site पर प्रकाशित ImageNet डेटासेट पर inception_V4model pretrained का उपयोग करने का प्रयास कर रहा हूं। इसके अलावा, मैं अपने नेटवर्क का उपयोग कर रहा हूं, मेरा मतलब है कि वह site पर प्रकाशित हुआ है।नई श्रेणियों के लिए प्राप्ति V4 की अंतिम परत को रेट करें: स्थानीय चर प्रारंभ नहीं किया गया

यहाँ कैसे मैं नेटवर्क कहते हैं:

def network(images_op, keep_prob): 

    width_needed_InceptionV4Net = 342 
    shape = images_op.get_shape().as_list() 
    H = int(round(width_needed_InceptionV4Net * shape[1]/shape[2], 2)) 
    resized_images = tf.image.resize_images(images_op, [width_needed_InceptionV4Net, H], tf.image.ResizeMethod.BILINEAR) 

    with slim.arg_scope(inception.inception_v4_arg_scope()): 
     logits, _ = inception.inception_v4(resized_images, num_classes=20, is_training=True, dropout_keep_prob = keep_prob) 

    return logits 

के बाद से मैं अपने श्रेणियों के लिए Inception_V4 की अंतिम परत का प्रशिक्षण प्राप्त करना, मैं वर्गों की संख्या संशोधित 20 होने के लिए के रूप में आप विधि में देख सकते हैं कॉल करें (inception.inception_v4)।

def optimistic_restore(session, save_file, flags): 
reader = tf.train.NewCheckpointReader(save_file) 
saved_shapes = reader.get_variable_to_shape_map() 
var_names = sorted([(var.name, var.name.split(':')[0]) for var in tf.global_variables() 
     if var.name.split(':')[0] in saved_shapes]) 
restore_vars = [] 
name2var = dict(zip(map(lambda x:x.name.split(':')[0], tf.global_variables()), tf.global_variables())) 

if flags.checkpoint_exclude_scopes is not None: 
    exclusions = [scope.strip() for scope in flags.checkpoint_exclude_scopes.split(',')] 

with tf.variable_scope('', reuse=True): 
    variables_to_init = [] 
    for var_name, saved_var_name in var_names: 
     curr_var = name2var[saved_var_name] 
     var_shape = curr_var.get_shape().as_list() 
     if var_shape == saved_shapes[saved_var_name]: 
      print(saved_var_name) 
      excluded = False 
      for exclusion in exclusions: 
       if saved_var_name.startswith(exclusion): 
        variables_to_init.append(var) 
        excluded = True 
        break 
      if not excluded: 
       restore_vars.append(curr_var) 
saver = tf.train.Saver(restore_vars) 
saver.restore(session, save_file) 

def train(images, ids, labels, total_num_examples, batch_size, train_dir, network, flags, 
    optimizer, log_periods, resume): 
"""[email protected] Trains the network for a number of steps. 

@param images image tensor 
@param ids id tensor 
@param labels label tensor 
@param total_num_examples total number of training examples 
@param batch_size batch size 
@param train_dir directory where checkpoints should be saved 
@param network pointer to a function describing the network 
@param flags command-line arguments 
@param optimizer pointer to the optimization class 
@param log_periods list containing the step intervals at which 1) logs should be printed, 
    2) logs should be saved for TensorBoard and 3) variables should be saved 
@param resume should training be resumed (or restarted from scratch)? 

@return the number of training steps performed since the first call to 'train' 
""" 

# clearing the training directory 
if not resume: 
    if tf.gfile.Exists(train_dir): 
     tf.gfile.DeleteRecursively(train_dir) 
    tf.gfile.MakeDirs(train_dir) 

print('Training the network in directory %s...' % train_dir) 
global_step = tf.Variable(0, trainable = False) 

# creating a placeholder, set to ones, used to assess the importance of each pixel 
mask, ones = _mask(images, batch_size, flags) 

# building a Graph that computes the logits predictions from the inference model 
keep_prob = tf.placeholder_with_default(0.5, []) 
logits = network(images * mask, keep_prob) 

# creating the optimizer 
if optimizer == tf.train.MomentumOptimizer: 
    opt = optimizer(flags.learning_rate, flags.momentum) 
else: 
    opt = optimizer(flags.learning_rate) 


# calculating the semantic loss, defined as the classification or regression loss 
if flags.boosting_weights is not None and os.path.isfile(flags.boosting_weights): 
    boosting_weights_value = np.loadtxt(flags.boosting_weights, dtype = np.float32, 
             delimiter = ',') 
    boosting_weights = tf.placeholder_with_default(boosting_weights_value, 
                list(boosting_weights_value.shape), 
                name = 'boosting_weights') 
    semantic_loss = _boosting_loss(logits, ids, boosting_weights, flags) 
else: 
    semantic_loss = _loss(logits, labels, flags) 
tf.add_to_collection('losses', semantic_loss) 

# computing the loss gradient with respect to the mask (i.e. the insight tensor) and 
# penalizing its L1-norm 
# replace 'semantic_loss' with 'tf.reduce_sum(logits)'? 
insight = tf.gradients(semantic_loss, [mask])[0] 
insight_loss = tf.reduce_sum(tf.abs(insight)) 
if flags.insight_loss > 0.0: 
    with tf.control_dependencies([semantic_loss]): 
     tf.add_to_collection('losses', tf.multiply(flags.insight_loss, insight_loss, 
                name = 'insight_loss')) 
else: 
    tf.summary.scalar('insight_loss_raw', insight_loss) 

# summing all loss factors and computing the moving average of all individual losses and of 
# the sum 
loss = tf.add_n(tf.get_collection('losses'), name = 'total_loss') 
loss_averages_op = tf.train.ExponentialMovingAverage(0.9, name = 'avg') 
losses = tf.get_collection('losses') 
loss_averages = loss_averages_op.apply(losses + [loss]) 

# attaching a scalar summary to all individual losses and the total loss; 
# do the same for the averaged version of the losses 
for l in losses + [loss]: 
    tf.summary.scalar(l.op.name + '_raw', l) 
    tf.summary.scalar(l.op.name + '_avg', loss_averages_op.average(l)) 

# computing and applying gradients 
with tf.control_dependencies([loss_averages]): 
    grads = opt.compute_gradients(loss) 
apply_gradient = opt.apply_gradients(grads, global_step = global_step) 

# adding histograms for trainable variables and gradients 
for var in tf.trainable_variables(): 
    tf.summary.histogram(var.op.name, var) 
for grad, var in grads: 
    if grad is not None: 
     tf.summary.histogram(var.op.name + '/gradients', grad) 
tf.summary.histogram('insight', insight) 

# tracking the moving averages of all trainable variables 
variable_averages_op = tf.train.ExponentialMovingAverage(flags.moving_average_decay, 
                 global_step) 
variable_averages = variable_averages_op.apply(tf.trainable_variables()) 

# building a Graph that trains the model with one batch of examples and 
# updates the model parameters 
with tf.control_dependencies([apply_gradient, variable_averages]): 
    train_op = tf.no_op(name = 'train') 

# creating a saver 
saver = tf.train.Saver(tf.global_variables()) 

# building the summary operation based on the TF collection of Summaries 
summary_op = tf.summary.merge_all() 

# creating a session 
current_global_step = -1 
with tf.Session(config = tf.ConfigProto(log_device_placement = False, 
              inter_op_parallelism_threads = flags.num_cpus, 
              device_count = {'GPU': flags.num_gpus})) as sess: 

    # initializing variables 
    if flags.checkpoint_exclude_scopes is not None: 
      optimistic_restore(sess, os.path.join(train_dir, 'inception_V4.ckpt'), flags) 

    # starting the queue runners 
    .. 
    # creating a summary writer 
    .. 
    # training itself 
    .. 
    # saving the model checkpoint 
    checkpoint_path = os.path.join(train_dir, 'model.ckpt') 
    saver.save(sess, checkpoint_path, global_step = current_global_step) 

    # stopping the queue runners 
    .. 
return current_global_step 

मैं checkpoint_exclude_scopes कहा जाता अजगर स्क्रिप्ट जहाँ मैं सटीक जो Tensors बहाल नहीं किया जाना चाहिए करने के लिए एक झंडा कहा:

यहाँ ट्रेन विधि मैं अब तक है। नेटवर्क की आखिरी परत में कक्षाओं की संख्या को बदलने की आवश्यकता है। यहाँ कैसे मैं अजगर स्क्रिप्ट कहते हैं: क्योंकि मुझे मिल गया

./toolDetectionInceptions.py --batch_size=32 --running_mode=resume --checkpoint_exclude_scopes=InceptionV4/Logits,InceptionV4/AuxLogits 

मेरा पहला परीक्षण भयानक थे बहुत ज्यादा समस्याओं .. कुछ की तरह:

tensorflow.python.framework.errors.NotFoundError: Tensor name "InceptionV4/Mixed_6b/Branch_3/Conv2d_0b_1x1/weights/read:0" not found in checkpoint files 

कुछ Googling के बाद मैं इस site पर एक समाधान मिल सकता है जहां वे ऊपर दिए गए कोड में प्रस्तुत optimistic_restore फ़ंक्शन का उपयोग करने का प्रस्ताव देते हैं जिसमें कुछ संशोधन शामिल हैं।

लेकिन अब समस्या कुछ और है:

W tensorflow/core/framework/op_kernel.cc:993] Failed precondition: Attempting to use uninitialized value Variable 
    [[Node: Variable/read = Identity[T=DT_INT32, _class=["loc:@Variable"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable)]] 

लगता है कहीं कोई स्थानीय चर है कि यह प्रारंभ नहीं किया गया है, लेकिन मैं यह नहीं मिल सकता है। क्या आप कृपया मदद कर सकते हैं?

संपादित:

इस समस्या को डिबग करने के लिए, मैं चर प्रारंभ और बहाल किया जाना चाहिए समारोह optimistic_restore में कुछ लॉग जोड़कर की संख्या की जाँच की।

# saved_shapes 609 
    # var_names 608 
    # name2var 1519 
    # variables_to_init: 7 
    # restore_vars: 596 
    # global_variables: 1519 

आपकी जानकारी, CheckpointReader.get_variable_to_shape_map(): ints की सूची के लिए एक dict मानचित्रण टेन्सर नाम देता है, चौकी में इसी टेन्सर के आकार का प्रतिनिधित्व करने के लिए: यहाँ एक संक्षिप्त है। इसका मतलब है कि इस चेकपॉइंट में चर की संख्या 609 है और पुनर्स्थापना के लिए आवश्यक चर की कुल संख्या 1519 है।

ऐसा लगता है कि प्रक्षेपित चेकपॉइंट टेंसर और नेटवर्क आर्किटेक्चर द्वारा उपयोग किए जाने वाले चर के बीच एक बड़ा अंतर है (यह वास्तव में उनके नेटवर्क भी है)। क्या चेकपॉइंट पर कोई भी संपीड़न किया गया है? क्या यह सही है कि मैं क्या कह रहा हूं? मुझे पता है कि अब क्या गुम है: यह केवल उन चरों का प्रारंभिकरण है जिन्हें पुनर्स्थापित नहीं किया गया है। फिर भी, मुझे यह जानने की जरूरत है कि उनके InceptionV4 नेटवर्क आर्किटेक्चर और प्रक्षेपित चेकपॉइंट के बीच एक बड़ा अंतर क्यों है?

+0

'sess.run (tf.global_variables_initializer()) आपकी समस्या का समाधान' करता है? – rvinas

+0

धन्यवाद @rvinas। जैसा कि मैं इसे समझता हूं, यह सभी चर शुरू करता है और यह वज़न के मूल्यों को बहाल नहीं करता है। इसका मतलब है: इस विधि का उपयोग हम finetuning की अवधारणा तोड़ रहे हैं। क्या मै गलत हु? बीटीडब्ल्यू, मैंने कोशिश की और यह काम करता है लेकिन मुझे लगता है कि यह वास्तव में मुझे जो चाहिए वह वास्तव में नहीं करता है। – Maystro

+0

आपका स्वागत है। यदि ऐसा है, तो मेरा जवाब शायद आपकी समस्या का समाधान करेगा। – rvinas

उत्तर

2

यहाँ कैसे मैं इसे अपेक्षित तरीके से जाने के लिए optimistic_restore समारोह परिभाषित करना चाहिए है:

def optimistic_restore(session, save_file, flags): 

if flags.checkpoint_exclude_scopes is not None: 
    exclusions = [scope.strip() for scope in flags.checkpoint_exclude_scopes.split(',')] 

reader = tf.train.NewCheckpointReader(save_file) 
saved_shapes = reader.get_variable_to_shape_map() 
print ('saved_shapes %d' % len(saved_shapes)) 
var_names = sorted([(var.name, var.name.split(':')[0]) for var in tf.global_variables() 
        if var.name.split(':')[0] in saved_shapes]) 

var_names_to_be_initialized = sorted([(var.name, var.name.split(':')[0]) for var in tf.global_variables() 
        if var.name.split(':')[0] not in saved_shapes]) 
print('var_names %d' % len(var_names)) 
print('var_names_to_be_initialized %d' % len(var_names_to_be_initialized)) 
restore_vars = [] 
name2var = dict(zip(map(lambda x: x.name.split(':')[0], tf.global_variables()), tf.global_variables())) 
print('name2var %d' % len(name2var)) 

with tf.variable_scope('', reuse=True): 
    variables_to_init = [] 
    for var_name, saved_var_name in var_names: 
     curr_var = name2var[saved_var_name] 
     var_shape = curr_var.get_shape().as_list() 
     if var_shape == saved_shapes[saved_var_name]: 
      excluded = False 
      for exclusion in exclusions: 
       if saved_var_name.startswith(exclusion): 
        variables_to_init.append(curr_var) 
        excluded = True 
        break 
      if not excluded: 
       restore_vars.append(curr_var) 
     else: 
      variables2_to_init.append(curr_var) 
    for var_name, saved_var_name in var_names_to_be_initialized: 
     curr_var = name2var[saved_var_name] 
     variables2_to_init.append(curr_var) 

print('variables2_to_init : %d ' % len(variables_to_init)) 
print('global_variables: %d ' % len(tf.global_variables())) 
print('restore_vars: %d ' % len(restore_vars)) 
saver = tf.train.Saver(restore_vars) 
saver.restore(session, save_file) 
session.run(tf.variables_initializer(variables_to_init)) 
3

वेरिएबल जिन्हें सेवर के साथ बहाल नहीं किया जाना आवश्यक है। इस अंत तक, आप प्रत्येक चर v के लिए v.initializer.run() चला सकते हैं जिसे आप पुनर्स्थापित नहीं करते हैं।

+0

आपके उत्तर के लिए धन्यवाद। यह अवधारणा है कि मुझे अपने कोड पर आवेदन करना चाहिए ताकि मैं आपके उत्तर को उखाड़ दूंगा – Maystro

संबंधित मुद्दे

 संबंधित मुद्दे