- Machine Learning cheatsheet: https://stanford.edu/~shervine/teaching/cs-229.html
- Pattern Recognition and Machine Learning Book
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def display_yolo(file, model, score_threshold, iou_threshold): | |
| ''' | |
| Display predictions from YOLO model. | |
| Parameters | |
| ---------- | |
| - file : string list : list of images path. | |
| - model : YOLO model. | |
| - score_threshold : threshold used for filtering predicted bounding boxes. | |
| - iou_threshold : threshold used for non max suppression. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def train(epochs, model, train_dataset, val_dataset, steps_per_epoch_train, steps_per_epoch_val, train_name = 'train'): | |
| ''' | |
| Train YOLO model for n epochs. | |
| Eval loss on training and validation dataset. | |
| Log training loss and validation loss for tensorboard. | |
| Save best weights during training (according to validation loss). | |
| Parameters | |
| ---------- | |
| - epochs : integer, number of epochs to train the model. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def yolov2_loss(detector_mask, matching_true_boxes, class_one_hot, true_boxes_grid, y_pred, info=False): | |
| ''' | |
| Calculate YOLO V2 loss from prediction (y_pred) and ground truth tensors (detector_mask, | |
| matching_true_boxes, class_one_hot, true_boxes_grid,) | |
| Parameters | |
| ---------- | |
| - detector_mask : tensor, shape (batch, size, GRID_W, GRID_H, anchors_count, 1) | |
| 1 if bounding box detected by grid cell, else 0 | |
| - matching_true_boxes : tensor, shape (batch_size, GRID_W, GRID_H, anchors_count, 5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def augmentation_generator(yolo_dataset): | |
| ''' | |
| Augmented batch generator from a yolo dataset | |
| Parameters | |
| ---------- | |
| - YOLO dataset | |
| Returns | |
| ------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val_dataset = None | |
| val_dataset= get_dataset(val_image_folder, val_annot_folder, LABELS, VAL_BATCH_SIZE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| train_dataset = None | |
| train_dataset= get_dataset(train_image_folder, train_annot_folder, LABELS, TRAIN_BATCH_SIZE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def parse_function(img_obj, true_boxes): | |
| x_img_string = tf.io.read_file(img_obj) | |
| x_img = tf.image.decode_png(x_img_string, channels=3) # dtype=tf.uint8 | |
| x_img = tf.image.convert_image_dtype(x_img, tf.float32) # pixel value /255, dtype=tf.float32, channels : RGB | |
| return x_img, true_boxes | |
| def get_dataset(img_dir, ann_dir, labels, batch_size): | |
| ''' | |
| Create a YOLO dataset | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class WeightReader: | |
| def __init__(self, weight_file): | |
| self.offset = 4 | |
| self.all_weights = np.fromfile(weight_file, dtype='float32') | |
| def read_bytes(self, size): | |
| self.offset = self.offset + size | |
| return self.all_weights[self.offset-size:self.offset] | |
| def reset(self): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Yolo model (thanks to https://github.com/experiencor/keras-yolo2) | |
| input_image = tf.keras.layers.Input((IMAGE_H, IMAGE_W, 3), dtype='float32') | |
| # Layer 1 | |
| x = Conv2D(32, (3,3), strides=(1,1), padding='same', name='conv_1', use_bias=False)(input_image) | |
| x = BatchNormalization(name='norm_1')(x) | |
| x = LeakyReLU(alpha=0.1)(x) | |
| x = MaxPooling2D(pool_size=(2, 2))(x) |
NewerOlder