Skip to content

Instantly share code, notes, and snippets.

View SamratSahoo's full-sized avatar
👽
alien

Samrat Sahoo SamratSahoo

👽
alien
View GitHub Profile
if __name__ == '__main__':
model = Autoencoder()
model.trainModel()
tensor = model.testImage(7777)
# Reshape
tensor = torch.reshape(tensor, (28, 28))
# toImage function
toImage = torchvision.transforms.ToPILImage()
# Convert to image
image = toImage(tensor)
def testImage(self, indexSample):
sample, _ = self.data[indexSample]
sample = sample.view(sample.size(0), -1)
sample = Variable(sample)
return self(sample)
def trainModel(self):
for epoch in range(self.epochs):
for data in self.dataLoader:
image, _ = data
image = image.view(image.size(0), -1)
image = Variable(image)
# Predictions
output = self(image)
# Calculate Loss
loss = self.criterion(output, image)
def forward(self, x):
x = self.encoder(x)
x = self.decoder(x)
return x
@SamratSahoo
SamratSahoo / autoencoder_init.py
Last active October 31, 2020 23:57
Init for the autoencoder class
class Autoencoder(nn.Module):
def __init__(self, epochs=100, batchSize=128, learningRate=1e-3):
super(Autoencoder, self).__init__()
# Encoder Network
self.encoder = nn.Sequential(nn.Linear(784, 128),
nn.ReLU(True),
nn.Linear(128, 64),
nn.ReLU(True),
nn.Linear(64, 12),
if __name__ == '__main__':
# Grayscale Image
image = processImage('Image.jpeg')
# Edge Detection Kernel
kernel = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])
# Convolve and Save Output
output = convolve2D(image, kernel, padding=2)
cv2.imwrite('2DConvolved.jpg', output)
@SamratSahoo
SamratSahoo / convolution.py
Last active May 3, 2025 10:47
2D Convolution Implementation with NumPy
def convolve2D(image, kernel, padding=0, strides=1):
# Cross Correlation
kernel = np.flipud(np.fliplr(kernel))
# Gather Shapes of Kernel + Image + Padding
xKernShape = kernel.shape[0]
yKernShape = kernel.shape[1]
xImgShape = image.shape[0]
yImgShape = image.shape[1]
@SamratSahoo
SamratSahoo / convolution.py
Created June 18, 2020 02:32
OpenCV BGR to Grayscale
def processImage(image):
image = cv2.imread(image)
image = cv2.cvtColor(src=image, code=cv2.COLOR_BGR2GRAY)
return image