dl-workshop-2022

Transfer learning (images)

Transfer network

from tensorflow.keras.applications import DenseNet121

dense_base = DenseNet121(weights='imagenet', include_top=False, input_shape=(48,48,3))

import re
dense_base.trainable = False
for layer in dense_base.layers: 
    if bool(re.search('conv5',layer.name)):
      layer.trainable = True
      print(layer.name,": Trainable")

model = models.Sequential()
model.add( dense_base)
model.add( layers.Flatten())
model.add( layers.Dense(units=25, activation = 'relu' ))
model.add( layers.Dense(units=6) )
model.add( layers.Softmax() )
model.summary()

model.compile(optimizer='rmsprop', loss = 'categorical_crossentropy', metrics=['accuracy'])
history = model.fit(train_ds, epochs=20, validation_data=valid_ds)

plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0, 1])
plt.legend(loc='lower right')


Dense121 Net

dense_base = DenseNet121(
    include_top=True,
    weights="imagenet",
    input_tensor=None,
    input_shape=None,
    pooling=None,
    classes=1000,
    classifier_activation="softmax",
)