Skip to content

Instantly share code, notes, and snippets.

# uvx --with "git+https://github.com/travishsu/mimm.git" --with numpy ipython
import mlx.core as mx
from mimm import get_model, list_models
model = get_model('resnet50', pretrained=True)
img = mx.random.normal((3, 256, 256, 3))
features = model.features(img)
for layer, feature in features.items():
print(layer, feature.shape)
# uvx --with torch --with torchvision ipython
from torchvision.models import get_model
model = get_model('resnet50', pretrained=True)
# https://pytorch.org/vision/main/feature_extraction.html
from torchvision.models.feature_extraction import create_feature_extractor
return_nodes = {f'layer{i}': f'layer{i}' for i in range(1, 5)}
extractor = create_feature_extractor(model, return_nodes)
import torch
@travishsu
travishsu / ttt.py
Last active April 20, 2025 08:33
Tic Tac Toe Python implementation rewrite from https://github.com/antirez/ttt-rl
# Tic Tac Toe Python implementation
import random
from math import exp
from tqdm import tqdm
NN_INPUT_SIZE = 18
NN_HIDDEN_SIZE = 100
NN_OUTPUT_SIZE = 9
@travishsu
travishsu / compare_dirs.py
Created July 12, 2024 03:07
Qt GUI to show difference between two directories
import os
import glob
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QListWidget, QHBoxLayout, QFileDialog
def compare_folders(dir1, dir2):
files_in_dir1 = [
f.replace('._', '').replace(dir1 + '/', '')
for f in glob.glob(dir1 + '/**/*', recursive=True)
@travishsu
travishsu / pascal.py
Created November 15, 2022 22:51
Pascal's Triangle
def pascal(n):
L = n + n - 1
rows = []
row = [1]
rows.append(row)
while len(rows) < n:
row_cat = [0] + row + [0]
row = [row_cat[i] + row_cat[i+1] for i in range(len(row_cat)-1)]
rows.append(row)
for row_idx, row in enumerate(rows):
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[[runners]]
pre_build_script = "mkdir -p $HOME/.docker/ && echo \"{ \\\"proxies\\\": { \\\"default\\\": { \\\"httpProxy\\\": \\\"$HTTP_PROXY\\\", \\\"httpsProxy\\\": \\\"$HTTPS_PROXY\\\", \\\"noProxy\\\": \\\"$NO_PROXY\\\" } } }\" > $HOME/.docker/config.json"
pre_clone_script = "git config --global http.proxy $HTTP_PROXY; git config --global https.proxy $HTTPS_PROXY"
environment = ["https_proxy=http://docker0_interface_ip:3128", "http_proxy=http://docker0_interface_ip:3128", "HTTPS_PROXY=docker0_interface_ip:3128", "HTTP_PROXY=docker0_interface_ip:3128"]
@travishsu
travishsu / coco2labelme.py
Last active July 19, 2024 01:51
Convert COCO format segmentation annotation to LabelMe format
import os
import json
import subprocess
import numpy as np
import pandas as pd
from skimage.measure import find_contours
class CocoDatasetHandler:
def __init__(self, jsonpath, imgpath):
class YourDataset(torch.utils.data.Dataset):
def __init__(self, ...):
...
def __len__(self):
...
return num_all_examples
def __getitem__(self, idx):
...
@travishsu
travishsu / tensorboard_concept_example.py
Last active March 5, 2017 16:10
用 TensorBoard 的好處就是不用在寫 printout 惹~
x = tf.placeholder(.....)
# Scope: 運算區塊
with tf.name_scope("operation_scope"):
weight = ...
l1 = op(x, w)
...
...
tf.summary.histogram("weight", weight) # 可以看 weight 值的分佈,只是我還不清楚分佈是指什麼