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
import itertools | |
import PIL.Image | |
import numpy as np | |
def four_corners_similar(image: PIL.Image.Image): | |
pixel_value_diff_tolerance = 40 # tolerance for pixel value difference, 0-255 | |
discrepancy_threshold = 0.01 # max percentage of pixels that can be different between corners to be considered similar | |
corner_image_ratio = 0.06 | |
crop_size = int(corner_image_ratio * min(image.size)) | |
# print(crop_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
import PIL.Image | |
from PIL import ImageFont | |
from torchvision.utils import make_grid as torch_make_grid | |
from torchvision.transforms.functional import pil_to_tensor, to_pil_image | |
from pathlib import Path | |
import platform | |
def make_grid(images: list[PIL.Image.Image | str]): | |
if isinstance(images[0], str): | |
images = [PIL.Image.open(img) for img in images] |
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
import logging | |
import numpy as np | |
import PIL.Image | |
import matplotlib.pyplot as plt | |
from datasets import load_dataset | |
from rembg import remove | |
import cv2 | |
def show_mask(mask, ax, random_color=False): |
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
import logging | |
import numpy as np | |
import PIL.Image | |
from segment_anything import SamPredictor, sam_model_registry | |
import matplotlib.pyplot as plt | |
logger = logging.getLogger(__name__) | |
model_type = "default" | |
_SAM_CKPT = "sam_vit_h_4b8939.pth" | |
sam = sam_model_registry[model_type](checkpoint=_SAM_CKPT).to(device="cuda") |
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
{ | |
"config": | |
{ | |
"background": "#ffffff", | |
"axis": | |
{ | |
"labelColor": "#262730", | |
"titleColor": "#262730", | |
"gridColor": "rgba(38, 39, 48, 0.2)", | |
"labelFont": "IBM Plex Sans, sans-serif", |
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 Solution { | |
// f(n) = 1 / n + (n - 2) / n * f(n - 1) | |
// | |
// f(n) = 1/n -> 1st person picks his own seat | |
// + 1/n * 0 -> 1st person picks last one's seat | |
// + (n-2)/n * ( ->1st person picks one of seat from 2nd to (n-1)th | |
// 1/(n-2) * f(n-1) -> 1st person pick 2nd's seat | |
// 1/(n-2) * f(n-2) -> 1st person pick 3rd's seat | |
// ...... | |
// 1/(n-2) * f(2) -> 1st person pick (n-1)th's seat |
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 Solution { | |
// The unbounded knapsack problem | |
// dp[i][j]: the fewest # coins among 0 to i-1 to make up to amount j | |
// dp[i][j] = min(dp[i-1][j], dp[i-1][j-coins[i]] + 1, dp[i][j-coins[i]] + 1) | |
// since dp[i][j-coins[i]] includes dp[i-1][j-coins[i]], thus: | |
// dp[i][j] = min(dp[i-1][j], dp[i][j-coins[i]] + 1) | |
// dp[i][j] < 0 means there is no solution. | |
// | |
// Time: O(n * amount) | |
// Space: O(n * amount) |
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 Solution { | |
// A variant of the knapsack problem | |
// dp[i][j]: is there a subset of stones (0 to i-1) can sum up to weight j | |
// dp[i][j] = dp[i-1][j] || dp[i-1][j-nums[i]] | |
// result: last stone weight = the shortest distance between any two nodes among dp[n][0] ... dp[n][W] | |
// | |
// Time: O(n * totalWeight) | |
// Space: O(n * totalWeight) | |
public int lastStoneWeightII(int[] stones) { | |
int totalWeight = Arrays.stream(stones).sum(); |
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 Solution { | |
// We maintain a 2D array , dp[n][subSetSum] | |
// For an array element i and sum j in array nums, | |
// dp[i][j]=true if the sum j can be formed by array elements | |
// in subset nums[0]..nums[i], otherwise dp[i][j]=false | |
// | |
// Time: O(n * sum) | |
// Space: O(n * sum) | |
public boolean canPartition(int[] nums) { | |
int totalSum = Arrays.stream(nums).sum(); |
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 Solution { | |
// Time: O(nlogn) | |
// Space: O(n) | |
public int lastStoneWeight(int[] stones) { | |
if (stones.length == 1) { | |
return stones[0]; | |
} | |
PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder()); | |
for (int weight : stones) { | |
queue.add(weight); |
NewerOlder