Skip to content

Instantly share code, notes, and snippets.

View JulieProst's full-sized avatar

JulieProst

  • Sicara
  • Paris
View GitHub Profile
@JulieProst
JulieProst / grabcut_with_cachier.py
Created November 15, 2021 13:33
Python gist to use cachier cache library for grabcut image segmentation
from pathlib import Path
import cv2.cv2 as cv2
import numpy as np
from cachier import cachier
ROOT_FOLDER = Path(__file__).resolve().parents[0]
CACHIER_FOLDER = ROOT_FOLDER / "cache"
NUMBER_OF_ITERATIONS = 5
@JulieProst
JulieProst / grabcut_with_combined_initialization.py
Created November 16, 2020 08:16
GrabCut for image segmentation with combined initialization
# Binarize input image
gray_image = cv2.cvtColor(original_image, cv2.COLOR_RGB2GRAY)
show_image(gray_image, "Gray image")
binarized_image = cv2.adaptiveThreshold(
gray_image,
maxValue=1,
adaptiveMethod=cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
thresholdType=cv2.THRESH_BINARY,
blockSize=9,
@JulieProst
JulieProst / grabcut_with_input_bounding_rectangle.py
Last active November 16, 2020 08:14
GrabCut image segmentation initialized with a bounding rectangle
import cv2.cv2 as cv2
import numpy as np
# Read the original image
IMAGES_FOLDER = "Path to your image folder"
original_image = cv2.imread(str(IMAGES_FOLDER / "spoon.jpg"))
# Define boundary rectangle containing the foreground object
height, width, _ = original_image.shape
left_margin_proportion = 0.3
N_EPOCH_SEARCH = 40
tuner.search(x_train, y_train, epochs=N_EPOCH_SEARCH, validation_split=0.1)
tuner.search_space_summary()
HYPERBAND_MAX_EPOCHS = 40
MAX_TRIALS = 20
EXECUTION_PER_TRIAL = 2
# Show a summary of the search
tuner.results_summary()
# Retrieve the best model.
best_model = tuner.get_best_models(num_models=1)[0]
# Evaluate the best model.
loss, accuracy = best_model.evaluate(x_test, y_test)
hp.Float(
'learning_rate',
min_value=1e-5,
max_value=1e-2,
sampling='LOG',
default=1e-3
)
model.compile(
optimizer=keras.optimizers.Adam(1e-3),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
Dense(
units=hp.Int(
'units',
min_value=32,
max_value=512,
step=32,
default=128
),
activation=hp.Choice(
'dense_activation',