Pretrain model developing in tensorflow for image classification

I have a issue regarding on how to modify a pretrained model to classify 3 classes instead of 1000. These are the 2 methods i came up with so far.. im not sure which one is the best.

NASNetMobile_model = tf.keras.applications.NASNetMobile (
                                                input_shape=(224,224,3),
                                                include_top=False,
                                                pooling='avg',
                                                classes=3,
                                                weights='imagenet'
                                                )
NASNetMobile_model.trainable=False
NASNetMobile_model.summary()type here

In, Method 1, the NASNetMobile model is initialized with pre-trained ImageNet weights, excluding the top layer and using average pooling. The model is set to be non-trainable to prevent its weights from being updated during training. A new Sequential model is then constructed, which includes the pre-trained NASNetMobile model followed by two dense layers: one with 128 units and ReLU activation, and another with 3 units and softmax activation for the final classification. The Sequential model is compiled with the Adam optimizer and sparse categorical cross-entropy loss. Finally, the model is trained on the dataset for 20 epochs with a batch size of 4 and a validation split of 20%.

#Method 1

new_pretrained_model = tf.keras.Sequential()

new_pretrained_model.add(NASNetMobile_model)
new_pretrained_model.add(tf.keras.layers.Dense(128, activation='relu'))
new_pretrained_model.add(tf.keras.layers.Dense(3, activation='softmax'))

new_pretrained_model.layers[0].trainable = False
new_pretrained_model.summary() here


new_pretrained_model.compile(
            optimizer='adam',
            loss='sparse_categorical_crossentropy',
            metrics=['accuracy']
            )

new_pretrained_model.fit(
        Xtrain,
        Ytrain,
        epochs=20,
        batch_size=4,
        validation_split=0.2
        )

Method 2

In Method 2, the functional API is used to create a new model. The output of the pre-trained NASNetMobile model is taken as the input for a new dense layer with 128 units and ReLU activation, followed by a final dense layer with 3 units and softmax activation. This approach explicitly connects the input of the NASNetMobile model to the new output layers, forming a new model with the same input as the original NASNetMobile model but with additional dense layers for classification. The new model is then compiled with the Adam optimizer and sparse categorical cross-entropy loss and trained on the dataset for 20 epochs with a batch size of 4 and a validation split of 20%.

NASNetMobile_model_out = NASNetMobile_model.output
x = tf.keras.layers.Dense(128, activation='relu')(NASNetMobile_model_out)
output = tf.keras.layers.Dense(3, activation='softmax')(x)
model_2 = tf.keras.Model(inputs = NASNetMobile_model.input, outputs=output)

model_2.summary()


model_2.compile(
            optimizer='adam',
            loss='sparse_categorical_crossentropy',
            metrics=['accuracy']
            )

model_2.fit(
        Xtrain,
        Ytrain,
        epochs=20,
        batch_size=4,
        validation_split=0.2
        )

New contributor

Redlightning is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật