亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當我更改其屬性時,驗證生成器的準確性幾乎會下降 - keras ImageDataGenerator

當我更改其屬性時,驗證生成器的準確性幾乎會下降 - keras ImageDataGenerator

HUH函數 2023-12-26 14:47:55
我正在從目錄層次結構中讀取圖像(flow_from_directory 使用 ImageDataGenerator 類中的生成器)。該模型是固定參數的mobilenetv2 + 可訓練的softmax層。當我將模型擬合到訓練數據時,訓練和驗證的準確度水平相當。如果我使用驗證參數或重置生成器,則使用 model.evaluate 驗證生成器的準確性會顯著下降,或者如果我重新使用 model.fit 擬合模型。該數據庫是3D視圖數據庫。相關代碼:'''batch_size=16rescaled3D_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255, zoom_range=0.2,                                                                  shear_range=0.2,                                                                   horizontal_flip=True)             train_gen =rescaled3D_gen.flow_from_directory(data_directory + '/train/', seed=3,                                              target_size = (pixels, pixels), shuffle=True,                                              batch_size = batch_size, class_mode='binary')val_gen =rescaled3D_gen.flow_from_directory(data_directory + '/test/', seed=3,                                            target_size = (pixels, pixels), shuffle=True,                                            batch_size = batch_size, class_mode='binary')#MODELinputs = tf.keras.Input(shape=(None, None, 3), batch_size=batch_size)x = tf.keras.layers.Lambda(lambda img: tf.image.resize(img, (pixels,pixels)))(inputs)x = tf.keras.layers.Lambda(tf.keras.applications.mobilenet_v2.preprocess_input)(x)mobilev2 = tf.keras.applications.mobilenet_v2.MobileNetV2(weights = 'imagenet', input_tensor = x,                                                          input_shape=(pixels,pixels,3),                                                          include_top=True, pooling = 'avg')#add a dense layer for task-specific categorization.full_model = tf.keras.Sequential([mobilev2,                                 tf.keras.layers.Dense(train_gen.num_classes, activation='softmax')])for idx, layers in enumerate(mobilev2.layers):    layers.trainable = Falsemobilev2.layers[-1].trainable=True
查看完整描述

1 回答

?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

這里有一些你可以嘗試的事情。您可以通過更改 train_gen 來消除 Lambda 層,如下所示


rescaled3D_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255, zoom_range=0.2,shear_range=0.2, horizontal_flip=True,

            preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input) 

您不需要 Lamda 調整大小圖層,因為您在目錄流中指定了目標大小。在 val_gen 中,您有 shuffle=True。這將打亂每個時期的驗證圖像順序。為了保持一致性,最好將其設置為 False。在 mobilenet 的代碼中,您有 include_top=True 和 pooling='avg' 當 include_top 為 True 時,池參數將被忽略。設置 include_top=True 會使模型的頂層具有 1000 個節點的密集層和 softmax 激活函數。我會設置 include_top=False。這樣,mobilenet 的輸出就是一個全局池化層,可以直接為您的密集分類層提供數據。在生成器中設置 class_mode='binary'。但在 model.compile 中,您將損失設置為稀疏_分類_交叉熵。這可以工作,但使用 loss=BinaryCrossentropy 進行編譯會更好。為了保持一致性,最好每個時期只檢查一次驗證樣本。為此,應選擇批量大小,使驗證樣本/batch_size 為整數,并使用該整數作為驗證步驟數。下面的代碼將為您做到這一點。


b_max=80 # set this to the maximum batch size you will allow based on memory capacity

length=val_gen.samples       

batch_size=sorted([int(length/n) for n in range(1,length+1) if length % n ==0 and length/n<=b_max],reverse=True)[0]  

val_steps=int(length/batch_size)

更改驗證批量大小可能會改變驗證損失和準確性的結果。一般來說,較大的批量大小會導致損失波動較小,但可能會導致陷入局部最小值的可能性較高。嘗試這些更改,看看結果的差異是否較小。


查看完整回答
反對 回復 2023-12-26
  • 1 回答
  • 0 關注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號