<jupyter_start><jupyter_text><jupyter_code>images[0].shape
len(images)
from models.custom_model_builder import get_custom_model
model = get_custom_model(input_shape=(224, 224, 3))
model
model.summary()


from keras.optimizers import SGD
losses = {
    "gender_output": "binary_crossentropy",
    "image_quality_output": "categorical_crossentropy",
    "age_output": "categorical_crossentropy",
    "weight_output": "categorical_crossentropy",
    "bag_output": "categorical_crossentropy",
    "footwear_output": "categorical_crossentropy", 
    "pose_output": "categorical_crossentropy", 
    "emotion_output": "categorical_crossentropy"
} 

loss_weights = {
    "gender_output": 1.0,
    "image_quality_output": 1.0,
    "age_output": 1.0,
    "weight_output": 1.0,
    "bag_output": 1.0,
    "footwear_output": 1.0, 
    "pose_output": 1.0, 
    "emotion_output": 1.0
} 

opt = SGD(lr=0.001, momentum=0.9)

model.compile(
    optimizer = opt,
    loss = losses, 
    loss_weights = loss_weights, 
    metrics=["accuracy"]
)
model_info = model.fit_generator(
    generator=train_gen,
    validation_data=valid_gen,
    use_multiprocessing=True,
    workers=6, 
    epochs=5,
    verbose=1
)
labels = [key for key, _ in targets.items()]
labels
from visualization_scripts.plot_model_history import plot_cumulative, plot_feature_history

from keras.callbacks import ModelCheckpoint, LearningRateScheduler
from keras.callbacks import ReduceLROnPlateau
project_dir

from google.colab import drive
drive.mount('/content/gdrive', force_remount=True)
def lr_schedule(epoch):
  lr = round(0.003 * pow(0.5, epoch//5), 10) #  halving lr every 5 epochs
  print ('Learning rate is : ', lr)
  return lr

# Prepare model model saving directory.
save_dir = Path.join(project_dir, 'models', 'saved_models')
model_name = 'train_1_model.{epoch:03d}.h5'
if not Path.isdir(save_dir):
    os.makedirs(save_dir, exist_ok=True)
filepath = os.path.join(save_dir, model_name)

# Prepare callbacks for model saving and for learning rate adjustment.

checkpoint = ModelCheckpoint(filepath=filepath,
                             monitor='val_acc',
                             verbose=1,
                             save_best_only=True)


lr_scheduler = LearningRateScheduler(lr_schedule)

lr_reducer = ReduceLROnPlateau(factor=np.sqrt(0.1),
                               cooldown=0,
                               patience=5,
                               min_lr=0.5e-6)

callbacks = [checkpoint, lr_reducer, lr_scheduler]
model_info = model.fit_generator(
    generator=train_gen,
    validation_data=valid_gen,
    use_multiprocessing=True,
    workers=6, 
    epochs=30,
    verbose=1,
    callbacks=callbacks
)
plot_cumulative(model_info)
for label in labels:
    plot_feature_history(model_info, label)
results = model.evaluate_generator(valid_gen, verbose=1)
dict(zip(model.metrics_names, results))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10618556701030928, 'fraction_numerical': 0.020618556701030927, 'mean_word_length': 3.7721311475409838, 'pattern_counts': {'":': 16, '<': 5, '<?xml version=': 0, '>': 5, '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': '482577', 'license': 'MIT', 'n_tokens_mistral': 1037, 'n_tokens_neox': 992, 'n_words': 182, 'path': 'Week5/PersonAttributes/notebooks/A5_overfit_plots_selected_model.ipynb', 'repo_name': 'hemanth346/EIP4', 'size': '2910'}