Index Error using sample_weight to initialize model weights

人盡茶涼 提交于 2020-04-18 05:47:29

问题


I'm working on multi-class segmentation using Keras and U-net.

I have as output of my NN 12 classes using soft max Activation function. the shape of my output is (N,288,288,12).

to fit my model I use sparse_categorical_crossentropy.

I want to initialize weights of my model for my unbalanced dataset.

The right answer to my question can be found here : How to initialize sample weights for multi-class segmentation?

I tried to implement it on my code and replace y by my training mask Y_train which Y_train contains list of values between ( 0 and 11 : my classes )

sample_weights = np.zeros(len(Y_train))  
sample_weights[Y_train==0] = 7  .....

but I'm getting this error :

IndexError: too many indices for array

The shape of Y_train is (481, 288, 288) should I reshape my Y_train ?

Code :

    inputs = tf.keras.layers.Input((IMG_WIDHT, IMG_HEIGHT, IMG_CHANNELS))                                                                
    smooth = 1.                                                                                                                          

    s = tf.keras.layers.Lambda(lambda x: x / 255)(inputs)                                                                                
    c1 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(                          
        s)  # Kernelsize : start with some weights initial value                                                                         
    c1 = tf.keras.layers.Dropout(0.1)(c1)                                                                                                
    c1 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(                          
        c1)  # Kernelsize : start with some weights initial value                                                                        

    outputs = tf.keras.layers.Conv2D(12, (1, 1), activation='softmax')(c9)                                                               
    model = tf.keras.Model(inputs=[inputs], outputs=[outputs])                                                                           

    cc = tf.keras.optimizers.Adam(learning_rate=0.0001, beta_1=0.9, beta_2=0.999, amsgrad=False)                                         
    model.compile(optimizer=cc, loss='sparse_categorical_crossentropy',                                                                  
                  metrics=['sparse_categorical_accuracy'],sample_weight_mode="temporal")  # metrics =[dice_coeff]                                                                                                                

    sample_weights = np.zeros(len(Y_train))                                                                                              
    # your own weight corresponding here:                                                                                                
    sample_weights[Y_train==0] = 7                                                                                                       
    sample_weights[Y_train==1] = 10                                                                                                      
    sample_weights[Y_train==2] = 2                                                                                                       
    sample_weights[Y_train==3] = 3                                                                                                       
    sample_weights[Y_train==4] = 4                                                                                                       
    sample_weights[Y_train==5] = 5                                                                                                       
    sample_weights[Y_train==6] = 6                                                                                                       
    sample_weights[Y_train==7] = 50                                                                                                      
    sample_weights[Y_train==8] = 8                                                                                                       
    sample_weights[Y_train==9] = 9                                                                                                       
    sample_weights[Y_train==10] = 50                                                                                                     
    sample_weights[Y_train==11] = 11                                                                                                     

    history = model.fit(X_train, Y_train, validation_split=0.18, batch_size=1,epochs = 60 ,sample_weight=sample_weights) #class_weight=cl

PS : Y_train[0][![Y_train[0]][2]][2]

来源:https://stackoverflow.com/questions/61159988/index-error-using-sample-weight-to-initialize-model-weights

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!