【问题标题】:Transfer Learning with Tensorflow Problem带有 TensorFlow 问题的迁移学习
【发布时间】:2021-08-16 16:44:17
【问题描述】:

我正在尝试解决一个深度学习课程的问题,我必须修改的代码块看起来像这样

def alpaca_model(image_shape=IMG_SIZE, data_augmentation=data_augmenter()):
    """ Define a tf.keras model for binary classification out of the MobileNetV2 model
    Arguments:
        image_shape -- Image width and height
        data_augmentation -- data augmentation function
    Returns:
        tf.keras.model
    """
    
    
    input_shape = image_shape + (3,)
    
    # START CODE HERE


    base_model=tf.keras.applications.MobileNetV2(input_shape=input_shape, include_top=False, weights="imagenet")

    # Freeze the base model by making it non trainable
    base_model.trainable = None 

    # create the input layer (Same as the imageNetv2 input size)
    inputs = tf.keras.Input(shape=None) 
    
    # apply data augmentation to the inputs
    x = None
    
    # data preprocessing using the same weights the model was trained on
    x = preprocess_input(None) 
    
    # set training to False to avoid keeping track of statistics in the batch norm layer
    x = base_model(None, training=None) 
    
    # Add the new Binary classification layers
    # use global avg pooling to summarize the info in each channel
    x = None()(x) 
    #include dropout with probability of 0.2 to avoid overfitting
    x = None(None)(x)
        
    # create a prediction layer with one neuron (as a classifier only needs one)
    prediction_layer = None
    
    # END CODE HERE
    
    outputs = prediction_layer(x) 
    model = tf.keras.Model(inputs, outputs)
    
    return model

IMG_SIZE = (160, 160)
def data_augmentation():
    data = tl.keras.Sequential()
    data.add(RandomFlip("horizontal")
    data.add(RandomRotation(0.2)
    return data

我按照说明从该模板开始尝试了 3 次,并进行了大量试验和错误。我不知道我错过了什么。我已经到了训练模型的地步,我可以得到它的摘要,但摘要不正确。

请帮忙,我想弄清楚这一点快疯了。我知道它非常简单,但它的简单问题让我很头疼。

【问题讨论】:

  • 编辑顶部的字符串注释。它使整个代码看起来像字符串。
  • 如果您遵循一些教程然后显示有问题的链接(不在评论中)。如果您收到错误,则显示有问题的完整错误消息(不在评论中)。如果您有任何其他信息,请在 queston 中显示 - 我们无法运行您的代码,也无法阅读您的想法,因此您必须在问题中提出所有详细信息。
  • 如果你得到了错误的总结,那么你必须显示有问题的数据,你得到的总结和预期的总结。如果我们无法对其进行测试并发现问题,我们将无法帮助您。
  • 你为什么用名字None()?这没有道理。 None 是 Python 中的特殊值,您不能将其用作函数。它应该总是给你错误信息。

标签: python tensorflow transfer-learning


【解决方案1】:

您可能必须使用以下代码来运行您的算法。

input_shape = image_shape + (3,)

### START CODE HERE

base_model = tf.keras.applications.MobileNetV2(input_shape=input_shape,
                                               include_top=False, # <== Important!!!!
                                               weights='imagenet') # From imageNet

# Freeze the base model by making it non trainable
base_model.trainable = False 

# create the input layer (Same as the imageNetv2 input size)
inputs = tf.keras.Input(shape=input_shape) 

# apply data augmentation to the inputs
x = data_augmentation(inputs)

# data preprocessing using the same weights the model was trained on
x = preprocess_input(x) 

# set training to False to avoid keeping track of statistics in the batch norm layer
x = base_model(x, training=False) 

# Add the new Binary classification layers
# use global avg pooling to summarize the info in each channel
x = tf.keras.layers.GlobalAveragePooling2D()(x)
#include dropout with probability of 0.2 to avoid overfitting
x = tf.keras.layers.Dropout(0.2)(x)
    
# create a prediction layer with one neuron (as a classifier only needs one)
prediction_layer = tf.keras.layers.Dense(1 ,activation='linear')(x)

### END CODE HERE

outputs = prediction_layer
model = tf.keras.Model(inputs, outputs)

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,但我的错误是将 (x) 放在最后的密集层中,这是对我有用的代码:

    def alpaca_model(image_shape=IMG_SIZE, data_augmentation=data_augmenter()):
    ''' Define a tf.keras model for binary classification out of the MobileNetV2 model
    Arguments:
        image_shape -- Image width and height
        data_augmentation -- data augmentation function
    Returns:
    Returns:
        tf.keras.model
    '''
    
    
    input_shape = image_shape + (3,)
    
    ### START CODE HERE
    
    base_model = tf.keras.applications.MobileNetV2(input_shape=input_shape,
                                                   include_top=False, # <== Important!!!!
                                                   weights='imagenet') # From imageNet
    
    # Freeze the base model by making it non trainable
    base_model.trainable = False 
    
    # create the input layer (Same as the imageNetv2 input size)
    inputs = tf.keras.Input(shape=input_shape) 
    
    # apply data augmentation to the inputs
    x = data_augmentation(inputs)
    
    # data preprocessing using the same weights the model was trained on
    x = preprocess_input(x) 
    
    # set training to False to avoid keeping track of statistics in the batch norm layer
    x = base_model(x, training=False) 
    
    # Add the new Binary classification layers
    # use global avg pooling to summarize the info in each channel
    x = tfl.GlobalAveragePooling2D()(x) 
    #include dropout with probability of 0.2 to avoid overfitting
    x = tfl.Dropout(0.2)(x)
        
    # create a prediction layer with one neuron (as a classifier only needs one)
    prediction_layer = tfl.Dense(1, activation = 'linear')
    
    ### END CODE HERE
    
    outputs = prediction_layer(x) 
    model = tf.keras.Model(inputs, outputs)
    
    return model
    

    【讨论】:

      【解决方案3】:

      在def数据增强下,你的括号没有很好地闭合

      【讨论】:

      • 是的,但我认为这是问题中的一个简单错字,而不是他们的真实代码。请注意他们如何不谈论错误消息,而只是谈论无法获得结果。
      猜你喜欢
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      • 2022-11-27
      相关资源
      最近更新 更多