Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

machine learning - ValueError: Data cardinality is ambiguous. Make sure all arrays contain the same number of samples?

I am trying to use a MobileNet model but facing above mentioned issue . I don't know if it is occuring due to train_test_split or else . Architecture is shown below Can I use model.fit instead of model.fit_generator here ?

    mobilenet = MobileNet(input_shape=(224,224,3) , weights='imagenet', include_top=False)
    
    # don't train existing weights
    for layer in mobilenet.layers:
      layer.trainable = False
     
    folders = glob('/content/drive/MyDrive/AllClasses/*')
    print("Total number of classes are",len(folders))  

    x = Flatten()(mobilenet.output)
    
    prediction = Dense(len(folders), activation='softmax')(x)
    
    model = Model(inputs=mobilenet.input, outputs=prediction)
    
    model.summary()
    
    model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
    
  
  dataset = ImageDataGenerator(rescale=1./255)
    
    dataset = dataset.flow_from_directory('/content/drive/MyDrive/AllClasses',target_size=(224, 224),batch_size=32,class_mode='categorical',color_mode='grayscale')

    train_data, test_data = train_test_split(dataset,random_state=42, test_size=0.20,shuffle=True)
 
    r = model.fit(train_data,validation_data=(test_data),epochs=5)
question from:https://stackoverflow.com/questions/65881153/valueerror-data-cardinality-is-ambiguous-make-sure-all-arrays-contain-the-same

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...