This commit is contained in:
sprague_rick 2024-09-21 21:50:07 -04:00
parent 477374f362
commit 73e28b7ff8
18 changed files with 328 additions and 0 deletions

Binary file not shown.

91
python/epipolar.py Normal file
View File

@ -0,0 +1,91 @@
import numpy as np
import cv2
from matplotlib import pyplot as plt
# Load the left and right images
# in gray scale
imgLeft = cv2.imread('image_l.jpg', 0)
imgRight = cv2.imread('image_r.jpg', 0)
# Detect the SIFT key points and
# compute the descriptors for the
# two images
sift = cv2.SIFT_create()
keyPointsLeft, descriptorsLeft = sift.detectAndCompute(imgLeft, None)
keyPointsRight, descriptorsRight = sift.detectAndCompute(imgRight, None)
assert(len(keyPointsLeft) == len(descriptorsLeft))
assert(len(keyPointsRight) == len(descriptorsRight))
# Create FLANN matcher object
FLANN_INDEX_KDTREE = 0
indexParams = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
searchParams = dict(checks=50)
flann = cv2.FlannBasedMatcher(indexParams, searchParams)
matches = flann.knnMatch(descriptorsLeft, descriptorsRight, 2)
# Apply ratio test
goodMatches = []
ptsLeft = []
ptsRight = []
for m, n in matches:
if m.distance < 0.8 * n.distance:
goodMatches.append([m])
# ptsLeft.append(keyPointsLeft[m.trainIdx].pt)
# ptsRight.append(keyPointsRight[n.trainIdx].pt)
ptsLeft.append(keyPointsLeft[m.queryIdx].pt)
ptsRight.append(keyPointsRight[m.trainIdx].pt)
ptsLeft = np.int32(ptsLeft)
ptsRight = np.int32(ptsRight)
# r F l = 0 <- find f
F, mask = cv2.findFundamentalMat(ptsLeft,
ptsRight,
cv2.FM_LMEDS)
# We select only inlier points
ptsLeft = ptsLeft[mask.ravel() == 1]
ptsRight = ptsRight[mask.ravel() == 1]
def drawlines(img1, img2, lines, pts1, pts2):
r, c = img1.shape
img1 = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR)
img2 = cv2.cvtColor(img2, cv2.COLOR_GRAY2BGR)
for i, (r, pt1, pt2) in enumerate(zip(lines, pts1, pts2)):
color = tuple(np.random.randint(0, 255, 3).tolist())
x0, y0 = map(int, [0, -r[2] / r[1]])
x1, y1 = map(int, [c, -(r[2] + r[0] * c) / r[1]])
img1 = cv2.line(img1, (x0, y0), (x1, y1), color, 1)
img1 = cv2.circle(img1, tuple(pt1), 5, color, -1)
img2 = cv2.circle(img2, tuple(pt2), 5, color, -1)
return img1, img2
# Find epilines corresponding to points
# in right image (second image) and
# drawing its lines on left image
linesLeft = cv2.computeCorrespondEpilines(ptsRight.reshape(-1, 1, 2), 2, F)
linesLeft = linesLeft.reshape(-1, 3)
img5, img6 = drawlines(imgLeft, imgRight,
linesLeft, ptsLeft,
ptsRight)
# Find epilines corresponding to
# points in left image (first image) and
# drawing its lines on right image
linesRight = cv2.computeCorrespondEpilines(ptsLeft.reshape(-1, 1, 2), 1, F)
linesRight = linesRight.reshape(-1, 3)
img3, img4 = drawlines(imgRight, imgLeft,
linesRight, ptsRight,
ptsLeft)
plt.subplot(121), plt.imshow(img5)
plt.subplot(122), plt.imshow(img3)
plt.show()

BIN
python/image_l.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
python/image_r.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
python/model.pth Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
torchtut/data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

BIN
torchtut/data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

BIN
torchtut/data/FashionMNIST/raw/train-images-idx3-ubyte.gz (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

BIN
torchtut/data/FashionMNIST/raw/train-labels-idx1-ubyte.gz (Stored with Git LFS) Normal file

Binary file not shown.

145
torchtut/first.py Normal file
View File

@ -0,0 +1,145 @@
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor
training_data = datasets.FashionMNIST(
root="data",
train=True,
download=True,
transform=ToTensor(),
)
test_data = datasets.FashionMNIST(
root="data",
train=False,
download=True,
transform=ToTensor(),
)
print(training_data)
print(test_data)
batch_size=8192
train_dataloader = DataLoader(training_data, batch_size=batch_size, shuffle=True)
test_dataloader = DataLoader(test_data, batch_size=batch_size, shuffle=True)
for X, y in test_dataloader:
print(f"Shape of X [N, C, H, W]: {X.shape}")
print(f"Shape of y: {y.shape} {y.dtype}")
break
device = (
"cuda" if torch.cuda.is_available()
else "mps" if torch.backends.mps.is_available()
else "cpu"
)
print(device)
class NeuralNetwork(nn.Module):
def __init__(self):
super().__init__()
self.flatten = nn.Flatten()
self.linear_relu_stack = nn.Sequential(
nn.Linear(28 * 28, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Linear(512, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Linear(512, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Linear(512, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Linear(512, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Linear(512, 10)
)
def forward(self, x):
# torch.Size([64, 1, 28, 28])
# torch.Size([64, 784])
# torch.Size([64, 10])
# print("=====")
#print(x.shape)
x = self.flatten(x)
#print(x.shape)
logits = self.linear_relu_stack(x)
#print(logits.shape)
return logits
model = NeuralNetwork().to(device)
print(model)
loss_fn = nn.CrossEntropyLoss().to(device)
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3)
def train(dataloader, model, loss_fn, optimizer):
size = len(dataloader.dataset)
model.train()
for batch, (X, y) in enumerate(dataloader):
X, y = X.to(device), y.to(device)
pred = model(X)
loss = loss_fn(pred, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if batch % 100 == 0:
loss, current = loss.item(), (batch + 1) * len(X)
print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
def test(dataloader, model, loss_fn):
size = len(dataloader.dataset)
num_batches = len(dataloader)
model.eval()
test_loss, correct = 0, 0
with torch.no_grad():
for X, y in dataloader:
X, y = X.to(device), y.to(device)
pred = model(X)
test_loss += loss_fn(pred, y).item()
correct += (pred.argmax(1) == y).type(torch.float).sum().item()
test_loss /= num_batches
correct /= size
print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")
epochs = 5
for t in range(epochs):
print(f"Epoch {t+1}\n---------------------")
train(train_dataloader, model, loss_fn, optimizer)
test(test_dataloader, model, loss_fn)
print("Done!")
torch.save(model.state_dict(), "model.pth")
print("Saved PyTorch Model Sate to model.pth")
model = NeuralNetwork()
model.load_state_dict(torch.load("model.pth"))
classes = [
"T-shirt/top",
"Trouser",
"Pullover",
"Dress",
"Coat",
"Sandal",
"Shirt",
"Sneaker",
"Bag",
"Ankle Boot",
]
model.eval()
x, y = test_data[0][0], test_data[0][1]
with torch.no_grad():
pred = model(x)
predicted, actual = classes[pred[0].argmax(0)], classes[y]
print(f'Predicted: "{predicted}", Actual: "{actual}"')

BIN
torchtut/model.pth Normal file

Binary file not shown.

12
torchtut/rick.py Normal file
View File

@ -0,0 +1,12 @@
import torch
import numpy as np
import math
target = torch.tensor([0, 1.0, 0])
input = torch.tensor([.333, .333, .334])
print("nats: ", -math.log(.333))
print("bits: ", -math.log(.333, 2))
b = torch.nn.functional.cross_entropy(input, target).item()
print(b)
print(b / math.log(2))

62
torchtut/tensor.py Normal file
View File

@ -0,0 +1,62 @@
import torch
import numpy as np
data = [[1,2],[3,4]]
x_data = torch.tensor(data)
print(x_data)
np_array = np.array(data)
x_np = torch.from_numpy(np_array)
x_ones = torch.ones_like(x_data)
print(f"Ones Tensor: \n {x_ones}\n")
print(x_ones.dtype)
x_rand = torch.rand_like(x_data, dtype=torch.float)
print(f"Random Tensor: \n {x_rand} \n")
print(x_rand.dtype)
shape=(2, 3)
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeros(shape)
print(f"Random Tensor: \n {rand_tensor} \n")
print(f"Ones Tensor: \n {ones_tensor} \n")
print(f"Zeros Tensor: \n {zeros_tensor} \n")
tensor = torch.rand(3, 4)
print(f"Shape of tensor: {tensor.shape}")
print(f"Datatype of tensor: {tensor.dtype}")
print(f"Device tensor is stored on: {tensor.device}")
tensor = torch.ones(4, 4)
if torch.cuda.is_available():
tensor = tensor.to("cuda")
print(f"First row: {tensor[0]}")
print(f"First column: {tensor[:, 0]}")
print(f"Last Column: {tensor[..., -1]}")
tensor[:, 1] = 0
print(tensor)
t1 = torch.cat([tensor, tensor, tensor], dim=1)
print(t1)
y1 = tensor @ tensor.T
y2 = tensor.matmul(tensor.T)
print(y1)
print(y2)
y3 = torch.rand_like(y1)
torch.matmul(tensor, tensor.T, out=y3)
print(y3)
print(y3.cpu().numpy())
agg = y3.sum()
print(agg)
print(agg.item())