Skip to content

Instantly share code, notes, and snippets.

View doleron's full-sized avatar
💭
I may be slow to respond.

Luiz doleron | Luiz d'Oleron doleron

💭
I may be slow to respond.
View GitHub Profile
# adapted from: https://pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/
def intersection_over_union(boxA, boxB):
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[0] + boxA[2], boxB[0] + boxB[2])
yB = min(boxA[1] + boxA[3], boxB[1] + boxB[3])
interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
boxAArea = (boxA[2] + 1) * (boxA[3] + 1)
boxBArea = (boxB[2] + 1) * (boxB[3] + 1)
iou = interArea / float(boxAArea + boxBArea - interArea)
plt.figure(figsize=(20, 10))
for images, labels in train_ds.take(1):
for i in range(BATCH_SIZE):
ax = plt.subplot(4, BATCH_SIZE//4, i + 1)
label = labels[0][i]
box = (labels[1][i] * input_size)
box = tf.cast(box, tf.int32)
image = images[i].numpy().astype("float") * 255.0
image = image.astype(np.uint8)
input_size = 244
def format_image(img, box):
height, width = img.shape
max_size = max(height, width)
r = max_size / input_size
new_width = int(width / r)
new_height = int(height / r)
new_size = (new_width, new_height)
resized = cv.resize(img, new_size, interpolation= cv.INTER_LINEAR)
import os, random
def list_files(full_data_path = "data/obj/", image_ext = '.jpg', split_percentage = [70, 20]):
files = []
discarded = 0
masked_instance = 0
for r, d, f in os.walk(full_data_path):
model = build_model(tf.keras.layers.Input(shape=(input_size, input_size, 1,)))
model.compile(optimizer=tf.keras.optimizers.Adam(),
loss = {'classifier_head' : 'categorical_crossentropy', 'regressor_head' : 'mse' },
metrics = {'classifier_head' : 'accuracy', 'regressor_head' : 'mse' })
EPOCHS = 100
BATCH_SIZE = 32
def build_feature_extractor(inputs):
x = tf.keras.layers.Conv2D(16, kernel_size=3, activation='relu', input_shape=(input_size, input_size, 1))(inputs)
x = tf.keras.layers.AveragePooling2D(2,2)(x)
x = tf.keras.layers.Conv2D(32, kernel_size=3, activation = 'relu')(x)
x = tf.keras.layers.AveragePooling2D(2,2)(x)
x = tf.keras.layers.Conv2D(64, kernel_size=3, activation = 'relu')(x)
x = tf.keras.layers.AveragePooling2D(2,2)(x)
def build_regressor(inputs):
x = tf.keras.layers.Conv2D(16, kernel_size=3, activation='relu', input_shape=(input_size, input_size, 1))(inputs)
x = tf.keras.layers.AveragePooling2D(2,2)(x)
x = tf.keras.layers.Conv2D(32, kernel_size=3, activation = 'relu')(x)
x = tf.keras.layers.AveragePooling2D(2,2)(x)
x = tf.keras.layers.Conv2D(64, kernel_size=3, activation = 'relu')(x)
x = tf.keras.layers.AveragePooling2D(2,2)(x)
CLASSES = 2
def build_classifier(inputs):
x = tf.keras.layers.Conv2D(16, kernel_size=3, activation='relu', input_shape=(input_size, input_size, 1))(inputs)
x = tf.keras.layers.AveragePooling2D(2,2)(x)
x = tf.keras.layers.Conv2D(32, kernel_size=3, activation = 'relu')(x)
x = tf.keras.layers.AveragePooling2D(2,2)(x)
CLASSES = 2
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, kernel_size=3, activation='relu', input_shape=(224, 224, 1)),
tf.keras.layers.AveragePooling2D(2,2),
tf.keras.layers.Conv2D(32, kernel_size=3, activation = 'relu'),
tf.keras.layers.AveragePooling2D(2,2),
tf.keras.layers.Conv2D(64, kernel_size=3, activation = 'relu'),
@doleron
doleron / medium_mse.js
Created February 1, 2022 14:06
Mean Squared Error
function mse(model, data) {
let result = 0.0;
for (let i = 0; i < data.length; ++i) {
const elem = data[i];
const x = elem.x;
const y = elem.y;
const y_hat = predict(x, model);
const diff = y_hat - y;
result += diff * diff;
}