This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def testImage(self, indexSample): | |
| sample, _ = self.data[indexSample] | |
| sample = sample.view(sample.size(0), -1) | |
| sample = Variable(sample) | |
| return self(sample) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def forward(self, x): | |
| x = self.encoder(x) | |
| x = self.decoder(x) | |
| return x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def processImage(image): | |
| image = cv2.imread(image) | |
| image = cv2.cvtColor(src=image, code=cv2.COLOR_BGR2GRAY) | |
| return image |