簡體   English   中英

無法使用 VGG16 預訓練模型實現多類遷移學習

[英]Unable to implement the multi class transfer learning using VGG16 pre-trained model

我正在嘗試從此鏈接重新實現遷移學習,我想重新實現數據多類分類的代碼。

我的示例數據位於https://www.dropbox.com/s/esirpr6q1lsdsms/ricetransfer1.zip?dl=0

我已經嘗試了 StackOverflow 上可用的不同建議,但它不起作用。

# Extract features
import os, shutil
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
train_size, validation_size, test_size = 148, 27, 31

datagen = ImageDataGenerator(rescale=1./255)
batch_size = 16
train_dir = "ricetransfer1/train"
validation_dir = "ricetransfer1/validation"
test_dir="ricetransfer1/test"
#indices = np.random.choice(range(len(X_train)))

def extract_features(directory, sample_count):
   #sample_count= X_train.ravel()

    features = np.zeros(shape=(sample_count, 7, 7, 512))  # Must be 
    equal to the output of the convolutional base
    labels = np.zeros(shape=(sample_count))
    # Preprocess data
    generator = datagen.flow_from_directory(directory,
                                        target_size=(img_width,img_height),
                                        batch_size = batch_size,
                                        class_mode='binary')
    # Pass data through convolutional base
    i = 0
    for inputs_batch, labels_batch in generator:
        features_batch = conv_base.predict(inputs_batch)
        features[i * batch_size: (i + 1) * batch_size] = features_batch
        labels[i * batch_size: (i + 1) * batch_size] = labels_batch
        i += 1
        if i * batch_size >= sample_count:
            break
return features, labels

train_features, train_labels = extract_features(train_dir, train_size)  # Agree with our small dataset size
validation_features, validation_labels = extract_features(validation_dir, validation_size)
test_features, test_labels = extract_features(test_dir, test_size)

ValueError: 無法將輸入數組從形狀 (16,4) 廣播到形狀 (16)

我想將遷移學習用於圖像的多類分類。

您需要使用labels = np.zeros(shape=(sample_count, 4))修復labels = np.zeros(shape=(sample_count)) labels = np.zeros(shape=(sample_count, 4)) ,其中四個代表四個類,現在它將無誤地廣播它。

暫無
暫無

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

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