问题
I am trying to train a model to identify images containing fire VS images that contain forests. I am training the model on a remote server using Linode. I am using Python 2.7 and Ubuntu 16.04.5.
When i run the following code locally or in Jupyter notebooks it will create 2 classes, but when i want to run it on the server it creates 3 classes.
The code that classifies the model:
def onehot(x): return np.array(OneHotEncoder().fit_transform(x.reshape(-1,1)).todense())
model = keras.applications.InceptionV3(weights='imagenet', include_top=False)
batch_size=16
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
train_generator = train_datagen.flow_from_directory(
'datareal/train', # this is the target directory
batch_size=batch_size,
target_size=(224, 224),
class_mode='binary')
test_datagen = ImageDataGenerator(rescale=1./255)
validation_generator = test_datagen.flow_from_directory(
'datareal/valid',
batch_size=batch_size,
target_size=(224, 224),
class_mode='binary')
x = model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer -- let's say we have 2 classes
predictions = Dense(2, activation='softmax')(x)
newmodel = Model(inputs=model.input, outputs=predictions)
newmodel.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
Output:
Found 173 images belonging to 3 classes.
Found 40 images belonging to 3 classes.
The directory containing all the images is structured as follows:
datareal
valid
forest
fire
train
forest
fire
How do i get the model to label only 2 classes instead of 3?
回答1:
I figured it out. the problem was that i still had some hidden folders in my train and validation folder.
来源:https://stackoverflow.com/questions/52609798/keras-creating-three-classes-instead-of-two