Skip to content

Instantly share code, notes, and snippets.

@FreeFly19
FreeFly19 / serial_uart_esp32_proxy.c
Created May 12, 2024 00:26
ESP32 USB to UART proxy
void setup() {
Serial.begin(230400,SERIAL_8N1);
Serial1.begin(230400, SERIAL_8N1, 21, 22);
}
int n;
void loop() {
n = Serial1.available();
if (n != 0)
{
import json
from argparse import ArgumentParser
def convert(input_file, output_file):
with open(input_file, 'r') as fin:
data2 = json.load(fin)
features = data2['features']
with open(output_file, 'w') as fout:
@FreeFly19
FreeFly19 / mnist_cnn.py
Created July 27, 2023 16:13
MNIST PyTorch CNN with skip connections
import numpy as np
import torch
import torchvision
from torch.utils.data import DataLoader
from torchvision.transforms import ToTensor
train_dataset = torchvision.datasets.MNIST('data', train=True, transform=ToTensor(), download=True)
test_dataset = torchvision.datasets.MNIST('data', train=False, transform=ToTensor(), download=True)
batch_size = 16
@FreeFly19
FreeFly19 / sqrt_sgd_pytorch.py
Created July 21, 2023 16:16
Square root finder with SGD(momentum + decay) on pytorch
import torch
w = torch.tensor([5.], requires_grad=True)
lr = 0.6
w_mom_grad = 0
momentum_coef = 0.9
weight_decay = 0.001
for i in range(10):
@FreeFly19
FreeFly19 / logistic_regression.py
Last active July 21, 2023 16:17
Logistic Regression from scratch with PyTorch
import torch
x = torch.tensor([1.5,2.8,15.,19.5])
y = torch.tensor([0.,0.,1.,1.])
w = torch.tensor([-.3], requires_grad=True)
b = torch.tensor([0.1232154], requires_grad=True)
def model(x):
import time
import av
import tqdm
from av.video import VideoStream
desired_fps = 2
timestamp = int(time.time())
@FreeFly19
FreeFly19 / client.ovpn
Last active March 6, 2023 02:27
Port forwarding + ovpn
client
# put the line below to disable tunneling outcoming trafic
route-nopull
....
@FreeFly19
FreeFly19 / data_loading_cell.py
Last active November 21, 2022 20:22
Tensorflow 2.0 Dataloading with aug and caching
import tensorflow as tf
train_ds = tf.keras.utils.image_dataset_from_directory(
train_dir,
validation_split=0.1,
subset="training",
label_mode="categorical",
#shuffle=True,
seed=123,
image_size=(image_size, image_size),
@FreeFly19
FreeFly19 / f1_definition.py
Created November 18, 2022 15:34
F1 metric for keras
import keras.backend as K
def f1(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
recall = true_positives / (possible_positives + K.epsilon())
f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())
return f1_val