diff --git a/python/__pycache__/torch.cpython-310.pyc b/python/__pycache__/torch.cpython-310.pyc new file mode 100644 index 0000000..d47bb61 Binary files /dev/null and b/python/__pycache__/torch.cpython-310.pyc differ diff --git a/python/epipolar.py b/python/epipolar.py new file mode 100644 index 0000000..150b02b --- /dev/null +++ b/python/epipolar.py @@ -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() \ No newline at end of file diff --git a/python/image_l.jpg b/python/image_l.jpg new file mode 100644 index 0000000..b7a3353 --- /dev/null +++ b/python/image_l.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cff9731c233298261522d399a2196a1e4a3dc7fbae6dff090e3a948d83c6e13d +size 66999 diff --git a/python/image_r.jpg b/python/image_r.jpg new file mode 100644 index 0000000..28b787e --- /dev/null +++ b/python/image_r.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:180f201f94c07ac76e98fc447a4cdb2924699c29faa8ba2a50bbd903ec0cde66 +size 61545 diff --git a/python/model.pth b/python/model.pth new file mode 100644 index 0000000..cdf1f5b Binary files /dev/null and b/python/model.pth differ diff --git a/torchtut/__pycache__/infra.cpython-310.pyc b/torchtut/__pycache__/infra.cpython-310.pyc new file mode 100644 index 0000000..07e23d6 Binary files /dev/null and b/torchtut/__pycache__/infra.cpython-310.pyc differ diff --git a/torchtut/data/FashionMNIST/raw/t10k-images-idx3-ubyte b/torchtut/data/FashionMNIST/raw/t10k-images-idx3-ubyte new file mode 100644 index 0000000..37bac79 Binary files /dev/null and b/torchtut/data/FashionMNIST/raw/t10k-images-idx3-ubyte differ diff --git a/torchtut/data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz b/torchtut/data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz new file mode 100644 index 0000000..e07f380 --- /dev/null +++ b/torchtut/data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:346e55b948d973a97e58d2351dde16a484bd415d4595297633bb08f03db6a073 +size 4422102 diff --git a/torchtut/data/FashionMNIST/raw/t10k-labels-idx1-ubyte b/torchtut/data/FashionMNIST/raw/t10k-labels-idx1-ubyte new file mode 100644 index 0000000..2195a4d Binary files /dev/null and b/torchtut/data/FashionMNIST/raw/t10k-labels-idx1-ubyte differ diff --git a/torchtut/data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz b/torchtut/data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz new file mode 100644 index 0000000..b203da4 --- /dev/null +++ b/torchtut/data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:67da17c76eaffca5446c3361aaab5c3cd6d1c2608764d35dfb1850b086bf8dd5 +size 5148 diff --git a/torchtut/data/FashionMNIST/raw/train-images-idx3-ubyte b/torchtut/data/FashionMNIST/raw/train-images-idx3-ubyte new file mode 100644 index 0000000..ff2f5a9 Binary files /dev/null and b/torchtut/data/FashionMNIST/raw/train-images-idx3-ubyte differ diff --git a/torchtut/data/FashionMNIST/raw/train-images-idx3-ubyte.gz b/torchtut/data/FashionMNIST/raw/train-images-idx3-ubyte.gz new file mode 100644 index 0000000..91fb354 --- /dev/null +++ b/torchtut/data/FashionMNIST/raw/train-images-idx3-ubyte.gz @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3aede38d61863908ad78613f6a32ed271626dd12800ba2636569512369268a84 +size 26421880 diff --git a/torchtut/data/FashionMNIST/raw/train-labels-idx1-ubyte b/torchtut/data/FashionMNIST/raw/train-labels-idx1-ubyte new file mode 100644 index 0000000..30424ca Binary files /dev/null and b/torchtut/data/FashionMNIST/raw/train-labels-idx1-ubyte differ diff --git a/torchtut/data/FashionMNIST/raw/train-labels-idx1-ubyte.gz b/torchtut/data/FashionMNIST/raw/train-labels-idx1-ubyte.gz new file mode 100644 index 0000000..371f432 --- /dev/null +++ b/torchtut/data/FashionMNIST/raw/train-labels-idx1-ubyte.gz @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a04f17134ac03560a47e3764e11b92fc97de4d1bfaf8ba1a3aa29af54cc90845 +size 29515 diff --git a/torchtut/first.py b/torchtut/first.py new file mode 100644 index 0000000..db1e841 --- /dev/null +++ b/torchtut/first.py @@ -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}"') + diff --git a/torchtut/model.pth b/torchtut/model.pth new file mode 100644 index 0000000..d1b9627 Binary files /dev/null and b/torchtut/model.pth differ diff --git a/torchtut/rick.py b/torchtut/rick.py new file mode 100644 index 0000000..21af408 --- /dev/null +++ b/torchtut/rick.py @@ -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)) diff --git a/torchtut/tensor.py b/torchtut/tensor.py new file mode 100644 index 0000000..e749e02 --- /dev/null +++ b/torchtut/tensor.py @@ -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())