<jupyter_start><jupyter_text>Path Setting<jupyter_code>annotation_path = 'train.txt'
log_dir = 'logs/svhn_weights/'
classes_path = 'model_data/svhn_classes.txt'
anchors_path = 'model_data/yolo_anchors.txt'
class_names = get_classes(classes_path)
num_classes = len(class_names)
anchors = get_anchors(anchors_path)<jupyter_output><empty_output><jupyter_text>SVHN Data Loading and Preprocessing<jupyter_code>val_split = 0.1

with open(annotation_path) as f:
    lines = f.readlines()
# print(lines)
np.random.seed(10101)
np.random.shuffle(lines)
np.random.seed(None)
num_val = int(len(lines)*val_split)
num_train = len(lines) - num_val<jupyter_output><empty_output><jupyter_text>Training Callback<jupyter_code>logging = TensorBoard(log_dir=log_dir)
checkpoint = ModelCheckpoint(log_dir + 'ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5',
    monitor='val_loss', save_weights_only=True, save_best_only=True, period=3)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1)
early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1)<jupyter_output><empty_output><jupyter_text>Model & Weights Loading<jupyter_code>input_shape = (416,416) # multiple of 32, hw

model = create_model(input_shape, anchors, num_classes, freeze_body=2, weights_path='model_data/yolo_weights.h5')
model.summary()<jupyter_output>Create YOLOv3 model with 9 anchors and 10 classes.<jupyter_text>* Optimizer:Adam* Learning Rate: 1e-3* Loss: yolo loss* Batch size: 32* Epoch: 50<jupyter_code># Train with frozen layers first, to get a stable loss.
# Adjust num epochs to your dataset. This step is enough to obtain a not bad model.
model.compile(optimizer=Adam(lr=1e-4), loss={'yolo_loss': lambda y_true, y_pred: y_pred})

batch_size = 4
print('Train on {} samples, val on {} samples, with batch size {}.'.format(num_train, num_val, batch_size))
model.fit_generator(data_generator_wrapper(lines[:num_train], batch_size, input_shape, anchors, num_classes),
                    steps_per_epoch=max(1, num_train//batch_size),
                    validation_data=data_generator_wrapper(lines[num_train:], batch_size, input_shape, anchors, num_classes),
                    validation_steps=max(1, num_val//batch_size),
                    epochs=50,
                    initial_epoch=0,
                    callbacks=[logging, checkpoint])
model.save_weights(log_dir + 'trained_weights_stage_1.h5')<jupyter_output>Train on 30062 samples, val on 3340 samples, with batch size 4.
Epoch 1/50
{'fraction_non_alphanumeric': 0.11640842272546682, 'fraction_numerical': 0.02542709574890743, 'mean_word_length': 5.842391304347826, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '190216', 'license': 'MIT', 'n_tokens_mistral': 931, 'n_tokens_neox': 875, 'n_words': 167, 'path': 'train.ipynb', 'repo_name': 'EricZhang1028/HW2-SVHN-Detection', 'size': '2517'}