tensorflow 快速减少图像分割损失的方法

6psbrbz9  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(110)

以下是详细信息:

  • 我在用骰子输。
  • 我使用Paris cityscapes数据集。
  • 由于内存限制,我的训练数据集由1800张图像组成,验证数据集由200张图像组成。
  • 使用的模型是transunet

以下是网络的hyper-params:

#hyperparams.
patch_size = 16
num_layers = 9
inp_H = inp_W = 256
num_patches = (inp_H * inp_W)//(patch_size ** 2)  
batch_size = 8

num_filters = [50, 100, 200]
drop_rate = 0.2
hidden_dim = 64
num_heads = 10
num_skip_conn = 3
num_classes = 3

smooth= 1e-5
learning_rate = 1e-5

字符串
x1c 0d1x的数据
下面是patchespatch_encodertrans_enc的代码。

#'X'-> the input that will be fed into the transformer encoder
class patches(tf.keras.layers.Layer):
    
    def __init__(self):
        super().__init__()
        
    def call(self, X):
        
        patch = tf.image.extract_patches(X,
                                            sizes= [1, patch_size, patch_size, 1],
                                            strides= [1, patch_size, patch_size, 1],
                                            rates= [1, 1, 1, 1],
                                            padding="VALID")
                
        patch_dims = patch.shape[-1]
        #num_patches replaced with -1
        patch = tf.reshape(patch, [batch_size, -1, patch_dims])
                
        return patch
        
    #x, y, z = patch.shape
    #x->Number of images; y-> Number of patches; z-> flattened 2D rep. of a patch

class patchEncoder(tf.keras.layers.Layer):
    
    def __init__(self, num_patches, hidden_dim):
        super().__init__()
        self.num_patches = num_patches
        self.projLayer = Dense(units= hidden_dim) #the projection layer
        self.pos_emb = Embedding(input_dim= num_patches, output_dim= hidden_dim) #position embeddings
        
    #X-> patches to be encoded    
    def call(self, X):
        positions = tf.range(start=0, limit=self.num_patches, delta=1)        
        X_enc = self.projLayer(X) + self.pos_emb(positions)
        return X_enc

#the tranformer encoder
from tensorflow.keras.layers import LayerNormalization, MultiHeadAttention

class transEnc(tf.keras.layers.Layer):
    
    def __init__(self):
        super().__init__()
        self.mha = MultiHeadAttention(num_heads= num_heads, key_dim= hidden_dim, dropout= drop_rate)
        self.lnorm = LayerNormalization()
        self.drop = Dropout(drop_rate)
        self.dense1 = Dense(units=hidden_dim*2, kernel_initializer= 'he_normal')
        self.dense2 = Dense(units=hidden_dim, kernel_initializer= 'he_normal')
        self.add = tf.keras.layers.Add()
            
    #here 'z' is the encoded patches    
    def call(self, z):
        
        for _ in range(num_layers):
            #LayerNorm1
            lnorm_op = self.lnorm(z)
            msa_op = self.mha(lnorm_op, lnorm_op)
            
            msa_op = self.add([lnorm_op, msa_op])  #skip connection 
  
            lnorm_op = self.lnorm(msa_op) #LayerNorm2
            mlp_op = self.dense1(lnorm_op) #mlp/ffn
            mlp_op = self.drop(mlp_op)
            
            mlp_op = self.dense2(mlp_op)
            mlp_op = self.drop(mlp_op)
            
            #calculating 'z' for the next transformer layer
            z = self.add([msa_op, mlp_op])
        
        return z


不幸的是,训练损失和验证损失在2个epoch内迅速减少。以下是训练日志:

epoch,loss,val_loss
0,0.015301775187253952,1.949299439729657e-05
1,9.153317478194367e-06,6.366377419908531e-06


我试过改变超参数,网络大小,训练图像的数量等。但似乎都不管用由于提前停止,网络在2/3个epoch后停止训练(验证损失不满足最小增量标准)。
到底出了什么问题

6tdlim6h

6tdlim6h1#

我已经解决了这个问题。原来这是我写的一段代码中的一个非常简单的错误(这里没有出现),不小心将两个Tensor指向了同一个位置(而不是复制它们的值)。
基本上

model_input = conv_op = tf.keras.Input(shape= shape)

字符串
我就是这么做的此问题已通过使用

conv_op = tf.Tensor(tensor= model_input)

相关问题