簡體   English   中英

使用初始網絡對遷移學習模型進行預處理

[英]Preprocessing for Transfer Learning Model with an Inception Network

我正在嘗試使用 Inception Network 作為基礎構建圖像分類模型。 這是一個簡單的二元分類模型。

我的圖像可以在一個大目錄中的許多小目錄中找到。 它們每個都有自己的“圖像 ID”,這就是它們的命名方式。 除此之外,我還有一些 tsv 文件,其中包含這些圖像 ID 和相應的標簽(“正面”或“負面”)。

當我訓練模型時,我發現我的准確率在沒有太大進展的情況下波動。 我想知道我准備數據集的方式是否有問題。 為此,我編寫了一些函數。

在我了解這些功能之前,下面給出了我如何定義我的模型,

base_model = InceptionV3(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D(name='avg_pool')(x)
x = Dropout(0.4)(x)
predictions = Dense(2, activation='sigmoid')(x)
model = Model(inputs=base_model.input, outputs=predictions)

for layer in base_model.layers:
    layer.trainable = False

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

這些是我為了准備數據而編寫的函數,

def vectorize_img(img_path):
  img = load_img(img_path, target_size=(224, 224)) #size is 224,224 by default
  x = img_to_array(img) #change to np array
  x = preprocess_input(x) #make input confirm to InceptionV3 input format
  return x

def prepare_features(base_dir, limit):
  features_dict = dict()
  for dir1 in os.listdir(base_dir):
      for dir2 in os.listdir(base_dir + dir1):
          for file in os.listdir(base_dir + dir1 + '/' + dir2):
            if len(features_dict) < limit:
              try:
                img_path = base_dir + dir1 + '/' + dir2 + '/' + file
                x = vectorize_img(img_path)
        
                name_id = file.split('.')[0] #take the file name and use as id in dict
                features_dict[name_id] = x
              except Exception as e:
                print(e)
                pass

  return features_dict

def prepare_data(file_path, features_dict):
  inputs = []
  labels = []
  df = pd.read_csv(file_path, sep='\t')
  df = df[['image_id', 'label_text_image']]
  df['class'] = df[['image_id', 'label_text_image']].apply(lambda x: 1 if x['label_text_image'] == 'Positive' else 0, axis = 1)

  for index, row in df.iterrows():
    try:
      inputs.append(features_dict[row['image_id']])
      labels.append(row['class'])
    except:
      pass

  return np.asarray(inputs), tf.one_hot(np.asarray(labels), depth=2)

然后調用這些函數來准備我的數據集,

features_dict = prepare_features('/path/to/img/dir', 8000)

x_train, y_train = prepare_data('/path/to/train/tsv', features_dict)
x_dev, y_dev = prepare_data('/path/to/dev/tsv', features_dict)
x_test, y_test = prepare_data('/path/to/test/tsv', features_dict)

最后,訓練模型,

EPOCHS = 50
BATCH_SIZE = 32
STEPS_PER_EPOCH = 1
history = model.fit(x=x_train, y=y_train, validation_data=(x_dev, y_dev), epochs=EPOCHS, steps_per_epoch=STEPS_PER_EPOCH, batch_size=BATCH_SIZE)

model.evaluate(x=x_test, y=y_test, batch_size=BATCH_SIZE)

難道我做錯了什么?

這是我的模型實現的結果,

Epoch 1/50
1/1 [==============================] - 158s 158s/step - loss: 0.8298 - accuracy: 0.5000 - val_loss: 0.7432 - val_accuracy: 0.5227
Epoch 2/50
1/1 [==============================] - 113s 113s/step - loss: 0.7775 - accuracy: 0.4688 - val_loss: 0.8225 - val_accuracy: 0.5153
Epoch 3/50
1/1 [==============================] - 113s 113s/step - loss: 0.7663 - accuracy: 0.5625 - val_loss: 0.8431 - val_accuracy: 0.5174
Epoch 4/50
1/1 [==============================] - 156s 156s/step - loss: 1.1292 - accuracy: 0.5312 - val_loss: 0.7763 - val_accuracy: 0.5227
Epoch 5/50
1/1 [==============================] - 114s 114s/step - loss: 0.7452 - accuracy: 0.5312 - val_loss: 0.7332 - val_accuracy: 0.5448
Epoch 6/50
1/1 [==============================] - 156s 156s/step - loss: 0.7884 - accuracy: 0.5312 - val_loss: 0.7072 - val_accuracy: 0.5606
Epoch 7/50
1/1 [==============================] - 114s 114s/step - loss: 0.7856 - accuracy: 0.5312 - val_loss: 0.7195 - val_accuracy: 0.5764
Epoch 8/50
1/1 [==============================] - 156s 156s/step - loss: 0.9203 - accuracy: 0.5312 - val_loss: 0.7348 - val_accuracy: 0.5616
Epoch 9/50
1/1 [==============================] - 156s 156s/step - loss: 0.8639 - accuracy: 0.4062 - val_loss: 0.7275 - val_accuracy: 0.5690
Epoch 10/50
1/1 [==============================] - 156s 156s/step - loss: 0.6170 - accuracy: 0.7188 - val_loss: 0.7125 - val_accuracy: 0.5880
Epoch 11/50
1/1 [==============================] - 156s 156s/step - loss: 0.5756 - accuracy: 0.7188 - val_loss: 0.6979 - val_accuracy: 0.6017
Epoch 12/50
1/1 [==============================] - 113s 113s/step - loss: 0.9976 - accuracy: 0.4375 - val_loss: 0.6834 - val_accuracy: 0.5933
Epoch 13/50
1/1 [==============================] - 156s 156s/step - loss: 0.7025 - accuracy: 0.5938 - val_loss: 0.6863 - val_accuracy: 0.5838

您提到它是一個二元分類,因此標簽是{0,1} 在這種情況下,您的模型輸出應該是

predictions = Dense(2, activation='softmax')(x)

帶有分類標簽[0,1] or [1,0]

predictions = Dense(1, activation='sigmoid')(x)

使用二進制標簽1 or 0但您使用的是帶有sigmoid輸出 2,即predictions = Dense(2, activation='sigmoid')(x)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM