[英]Unable to implement the multi class transfer learning using VGG16 pre-trained model
I am trying to re-implement the transfer learning from this link , I wanted to reimplement the code for the multiclass classification of my data.我正在尝试从此链接重新实现迁移学习,我想重新实现数据多类分类的代码。
My sample data is at https://www.dropbox.com/s/esirpr6q1lsdsms/ricetransfer1.zip?dl=0我的示例数据位于https://www.dropbox.com/s/esirpr6q1lsdsms/ricetransfer1.zip?dl=0
I have tried the different suggestion available on the StackOverflow but it doesn't work.我已经尝试了 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: could not broadcast input array from the shape (16,4) into shape (16) ValueError: 无法将输入数组从形状 (16,4) 广播到形状 (16)
I wanted to use the transfer learning for the multiclass classification of image.我想将迁移学习用于图像的多类分类。
您需要使用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.