How to classify a third category that doesn't belong to these two types?

“My goal is to perform binary classification, where anything not belonging to these two classes is classified as a third category. I have data split into ‘train’ and ‘test’ folders. The ‘test’ folder contains two subfolders, one for class ‘A’ and the other for class ‘B.’ Here is my code, and I would like to know how to modify it to classify anything that doesn’t belong to these two classes as a third category. Currently, it’s binary classification, and the output probabilities are close to 100 or 0, making it unable to distinguish items that do not belong to these two classes.”

train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True
)

test_datagen = ImageDataGenerator(rescale = 1./255)
train_set = train_datagen.flow_from_directory(train_data_dir,
target_size = (64,64),
batch_size = 32,
shuffle=True,
class_mode = ‘binary’)

test_set = test_datagen.flow_from_directory(test_data_dir,
target_size = (64, 64),
batch_size = 32,
shuffle=True,
class_mode = ‘binary’)

class MyModel(tf.keras.Model):
def init(self,L2=0.01):
super(MyModel, self).init()
self.conv1=Conv2D(64,3,activation=‘tanh’,input_shape=(64,64,3))
self.conv2=Conv2D(64,3,activation=‘tanh’)
self.pool1=MaxPooling2D(3)
self.pool2=MaxPooling2D(3)
self.flatten = Flatten()
self.d1 = Dense(128, activation=‘tanh’,kernel_regularizer=tf.keras.regularizers.l2(L2))
self.d2 = Dense(64, activation=‘tanh’,kernel_regularizer=tf.keras.regularizers.l2(L2))
self.dropout = Dropout(0.5)
self.d3 = Dense(1, activation=‘sigmoid’)

#kernel_regularizer=tf.keras.regularizers.l2(L2)(初始化)

def call(self,x):
x = self.conv1(x)
x = self.pool1(x)
x = self.dropout(x)
x = self.conv2(x)
x = self.pool2(x)
x = self.dropout(x)
x = self.flatten(x)
x = self.dropout(x)
x = self.d1(x)
x = self.d2(x)
return self.d3(x)

model = MyModel()

@tf.function
def train_step(images, labels):
with tf.GradientTape() as tape:
predictions = model(images)
loss = loss_object(labels, predictions)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))

train_loss(loss)
train_accuracy(labels, predictions)

max_test_iter=7
max_valid_iter=10
EPOCHS = 100

for epoch in range(EPOCHS):
train_loss.reset_states()
train_accuracy.reset_states()
test_loss.reset_states()
test_accuracy.reset_states()

train_iter=iter(train_set)
test_iter=iter(test_set)
valid_iter=iter(validation_set)
time=0
while time<=max_train_iter:
    try:
        time+=1
        images,labels=next(train_set)
        train_step(images,labels)
    except StopIteration:
        break
time=0
while time<=max_test_iter:
    try:
        time+=1
        images,labels=next(test_set)
        test_step(images,labels)
    except StopIteration:
        break
time=0

Hi @920204q, Generally you can follow the OVR(One vs Rest) approach for your problem statement. In this approach you have to build a separate binary classifier for each of the two main classes, and anything that doesn’t belong to either class is classified as the third category. For more details please refer to this document. Thank You.