<jupyter_start><jupyter_text>DenseNet<jupyter_code>from tensorflow.keras.applications import DenseNet121
from tensorflow.keras.applications.densenet import preprocess_input

from keras.layers import Dense, GlobalAveragePooling2D, Conv2D, BatchNormalization, MaxPooling2D, Dropout

from keras.models import Model

from tensorflow.keras.optimizers import Adam
# Set pre-trained Densenet model as base
base_model = DenseNet121(weights='imagenet', include_top=False, input_shape=(224, 224, 3))

# Freeze DenseNet121 model
base_model.trainable = False

model = base_model.output

# Add to pre-trained Densenet model
model = GlobalAveragePooling2D()(model)
model = BatchNormalization()(model)
model = Dropout(0.2)(model)
model = Dense(1024,activation='relu')(model) 
model = Dense(512,activation='relu')(model) 
model = BatchNormalization()(model)
model = Dropout(0.2)(model)

output = Dense(n_classes, activation='softmax')(model)

model = Model(inputs=base_model.input, outputs=output)
model.summary()
def compile_model(model):
    optimizer=Adam(learning_rate=0.001)
    model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])

    return model

def train_model(model, train_set, val_set):
    history = model.fit(train_set, epochs=2, verbose=2, validation_data=val_set, shuffle=True) # batch_size not specified because images are in dataset form

    return history

def eval_model(model, test_set):
    test_loss, test_accuracy = model.evaluate(test_set)

    return test_loss, test_accuracy
# model = compile_model(model)

model.compile(optimizer=Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(train_generator, epochs=5, verbose=2, validation_data=val_generator, shuffle=True)

# save the model
model.save('densenet_baseline.h5')

# summarize history for accuracy

plt.plot(history.history['loss'])
plt.title('loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['loss'], loc='upper left')
plt.show()<jupyter_output>Epoch 1/5
{'fraction_non_alphanumeric': 0.1183754333828628, 'fraction_numerical': 0.022783556215948488, 'mean_word_length': 7.416666666666667, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '641745', 'license': 'MIT', 'n_tokens_mistral': 715, 'n_tokens_neox': 667, 'n_words': 136, 'path': 'DenseNet/DenseNet_Transfer_Learning_baseline.ipynb', 'repo_name': 'mdjoh/MMAI894-ASL-ImageClassification', 'size': '2019'}