I’ve been working on a CNN model using both the Sequential API and the Subclassing API in TensorFlow. However, I’m seeing a significant difference in accuracy between the two implementations on the same dataset. Specifically, the Sequential API consistently gives an accuracy of around 0.95-0.97, while the Subclassing API gives an accuracy of only 0.5-0.6.
Here is my Subclassing API implementation:
class Custom(tf.keras.models.Model):
def __init__(self,img_size):
super(Custom, self).__init__()
self.feature_extractor=FeatureExtractor()
self.flatten=Flatten()
self.dense1=Dense(1000,activation='relu')
self.batch1=BatchNormalization()
self.dense2=Dense(10,activation='relu')
self.batch2=BatchNormalization()
self.dense3=Dense(1,activation='sigmoid')
def call(self, x):
x=self.feature_extractor(x)
x=self.flatten(x)
x=self.dense1(x)
x=self.batch1(x)
x=self.dense2(x)
x=self.batch2(x)
return self.dense3(x)
Here is my Sequential API implementation:
model=tf.keras.Sequential()
model.add(InputLayer(shape=(IMG_SIZE,IMG_SIZE,3)))
model.add(FeatureExtractor())
model.add(Flatten())
model.add(Dense(1000,activation='relu'))
model.add(BatchNormalization())
model.add(Dense(10,activation='relu'))
model.add(BatchNormalization())
model.add(Dense(1,activation='sigmoid'))
model.summary()
Observation
- The Sequential model consistently achieves an accuracy of 0.95-0.97.
- The Subclassing model only achieves an accuracy of 0.5-0.6, even after multiple runs.
Question:
Why is there such a large discrepancy in accuracy(as well for other metrics as well) between the two approaches? What might I be doing wrong in the Subclassing API implementation that’s causing this difference?
Additional Information:
- The dataset consists of batches of images.
- The FeatureExtractor class is used in both implementations and works as expected.batches of images.
I’ve been troubleshooting the issue by doing the following:
- Reviewed TensorFlow Documentation and Articles: I double-checked the implementation against the TensorFlow documentation and various articles on using the Subclassing API. Most examples I found follow a similar structure to my implementation, so the approach seems correct.
- Experimented with Initialization: I tried initializing the model with different dummy values, as recommended in the documentation, to ensure that initialization wasn’t the issue.
- Used tf.keras.Input Layer in the call Function: I tried adding a tf.keras.Input layer in the call function to see if the lack of an explicit input layer was causing the problem. However, this led to errors, thus indicating that the issue might not be related to the input layer.
- Validated Feature Extractor Layer: I also checked the FeatureExtractor layer, which works fine with the Sequential model, so I’ve ruled out any issues with this part of the model.