<jupyter_start><jupyter_text>Data Generator<jupyter_code>train_data_dir = '../data/CollisionData/'

img_width, img_height = 224, 224
nb_train_samples = 730 
nb_validation_samples = 181
epochs = 10
batch_size = 16

if K.image_data_format() == 'channels_first': 
    input_shape = (3, img_width, img_height) 
else: 
    input_shape = (img_width, img_height, 3) 
train_datagen = ImageDataGenerator(rescale=1./255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    validation_split=0.2,
    ) # set validation split

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_height, img_width),
    batch_size=batch_size,
    class_mode='binary',
    subset='training') # set as training data

validation_generator = train_datagen.flow_from_directory(
    train_data_dir, # same directory as training data
    target_size=(img_height, img_width),
    batch_size=batch_size,
    class_mode='binary',
    shuffle=False,
    subset='validation') # set as validation data<jupyter_output>Found 730 images belonging to 2 classes.
Found 181 images belonging to 2 classes.<jupyter_text>Model ArchitectureMobileNet V1 BaseUsing imagenet weights as initialization. The entire architecture is retrainable.<jupyter_code>def build_finetune_model(base_model, dropout, fc_layers, num_classes):
    for layer in base_model.layers:
        layer.trainable = True #change this to False is retraining is not to be done in base layers

    x = base_model.output
    x = Flatten()(x)
    for fc in fc_layers:
        # New FC layer, random init
        x = Dense(fc, activation='relu')(x) 
        x = Dropout(dropout)(x)

    # New softmax layer
    predictions = Dense(1, activation='sigmoid')(x) 
    
    finetune_model = Model(inputs=base_model.input, outputs=predictions)

    return finetune_model

base_model = MobileNet(weights='imagenet', 
                      include_top=False, 
                      input_shape=input_shape)

class_list = ['CollisionOrClose', 'NoCollision']
FC_LAYERS = [256, 256]
dropout = 0.5

finetune_model = build_finetune_model(base_model, 
                                      dropout=dropout, 
                                      fc_layers=FC_LAYERS, 
                                      num_classes=len(class_list))
adam = Adam(lr=0.00001)
finetune_model.compile(adam, loss='binary_crossentropy', metrics=['accuracy'])

filepath="./checkpoints/" + "MobileNetV1" + "_model_weights.h5"
checkpoint = ModelCheckpoint(filepath, monitor=["acc"], verbose=1, mode='max')
callbacks_list = [checkpoint]
finetune_model.summary()<jupyter_output>Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 224, 224, 3)]     0         
_________________________________________________________________
conv1_pad (ZeroPadding2D)    (None, 225, 225, 3)       0         
_________________________________________________________________
conv1 (Conv2D)               (None, 112, 112, 32)      864       
_________________________________________________________________
conv1_bn (BatchNormalization (None, 112, 112, 32)      128       
_________________________________________________________________
conv1_relu (ReLU)            (None, 112, 112, 32)      0         
_________________________________________________________________
conv_dw_1 (DepthwiseConv2D)  (None, 112, 112, 32)      288       
_____________________________________________________________[...]<jupyter_text>Model Size<jupyter_code>shapes_count = int(np.sum([np.prod(np.array([s if isinstance(s, int) else 1 for s in l.output_shape])) for l in finetune_model.layers]))
memory = shapes_count * 4

print ("Model Size(mb): %d" % (memory/1000000))
trainable_count = np.sum([K.count_params(w) for w in finetune_model.trainable_weights])
non_trainable_count = np.sum([K.count_params(w) for w in finetune_model.non_trainable_weights])

print('Total params: {:,}'.format(trainable_count + non_trainable_count))
print('Trainable params: {:,}'.format(trainable_count))
print('Non-trainable params: {:,}'.format(non_trainable_count))<jupyter_output>Total params: 16,140,225
Trainable params: 16,118,337
Non-trainable params: 21,888<jupyter_text>Training and Testing<jupyter_code>history = finetune_model.fit_generator(train_generator, 
    steps_per_epoch = nb_train_samples // batch_size, 
    epochs = epochs, validation_data = validation_generator, 
    validation_steps = nb_validation_samples // batch_size, shuffle=True,)<jupyter_output>WARNING:tensorflow:From <ipython-input-13-8649367d8c28>:4: Model.fit_generator (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
Please use Model.fit, which supports generators.
Epoch 1/10
45/45 [==============================] - 16s 351ms/step - loss: 0.9170 - accuracy: 0.7213 - val_loss: 0.4882 - val_accuracy: 0.7841
Epoch 2/10
45/45 [==============================] - 15s 339ms/step - loss: 0.5381 - accuracy: 0.7955 - val_loss: 0.3164 - val_accuracy: 0.8409
Epoch 3/10
45/45 [==============================] - 15s 342ms/step - loss: 0.5090 - accuracy: 0.8069 - val_loss: 0.2904 - val_accuracy: 0.8636
Epoch 4/10
45/45 [==============================] - 15s 336ms/step - loss: 0.4049 - accuracy: 0.8361 - val_loss: 0.2291 - val_accuracy: 0.8864
Epoch 5/10
45/45 [==============================] - 15s 335ms/step - loss: 0.3884 - accuracy: 0.8501 - val_loss: 0.1943 - val_accuracy: 0.9148
Epoch 6/10
45/45 [==========[...]<jupyter_text>Performance<jupyter_code>acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

loss=history.history['loss']
val_loss=history.history['val_loss']

epochs_range = range(epochs)

plt.figure(figsize=(15, 15))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
from sklearn.metrics import confusion_matrix


probabilities = finetune_model.predict_generator(generator=validation_generator)
print(probabilities)
y_true = validation_generator.classes
y_pred = probabilities > 0.5
class_labels = list(validation_generator.class_indices.keys())  
report = metrics.classification_report(y_true, y_pred, target_names=class_labels)
print(report)  
mat = confusion_matrix(y_true, y_pred)
print(mat)<jupyter_output>[[ 38   3]
 [ 20 120]]<jupyter_text>Analyze Results<jupyter_code>def plotImages1(images_arr, y1, y2, row_size):
    fig, axes = plt.subplots(1, row_size, figsize=(20,20))
    axes = axes.flatten()
    for img, l, p, ax in zip(images_arr, y1, y2, axes):
        ax.imshow(img)
        ax.set_title("Label:"+str(int(l))+", Pred:"+str(int(p)))
        ax.axis('off')
    plt.tight_layout()
    plt.show()
cnt = 0
row_size = 5
validation_generator.reset()
fig=plt.figure(figsize=(20,20))

ax = []
for i in range(10):
    x,y = next(validation_generator)
    img = x[:row_size]
    y_label = y[:row_size]
    y_predicted = y_pred[cnt:cnt+row_size]
    plotImages1(img, y_label, y_predicted, row_size)
    cnt += 16
labels = (validation_generator.class_indices)
labels<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13907985193019567, 'fraction_numerical': 0.04931253305129561, 'mean_word_length': 4.587149187592319, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '292781', 'license': 'MIT', 'n_tokens_mistral': 2735, 'n_tokens_neox': 2402, 'n_words': 512, 'path': 'src/.ipynb_checkpoints/Mobile_Net_Wave_Collision-checkpoint.ipynb', 'repo_name': 'UNCG-DAISY/TinyDuneCollision', 'size': '7564'}