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 Generator: | |
| #HELPFUL FUNCTIONS | |
| def widen_hole_transformation(self,racetrack,start_cell,end_cell): | |
| δ = 1 | |
| while(1): | |
| if ((start_cell[1] < δ) or (start_cell[0] < δ)): | |
| racetrack[0:end_cell[0],0:end_cell[1]] = -1 | |
| break |
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 Environment: | |
| #HELPFUL FUNCTIONS | |
| def get_new_state(self, state, action): | |
| ''' | |
| Get new state after applying action on this state | |
| Assumption: The car keeps on moving with the current velocity and then action is applied to | |
| change the velocity | |
| ''' |
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 Agent: | |
| #HELPFUL FUNCTIONS | |
| def possible_actions(self, velocity): | |
| ''' | |
| *** Performs two tasks, can be split up *** | |
| Universe of actions: α = [(-1,-1),(-1,0),(0,-1),(-1,1),(0,0),(1,-1),(0,1),(1,0),(1,1)] | |
| Uses constraints to filter out invalid actions given the velocity | |
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 Visualizer: | |
| #HELPFUL FUNCTIONS | |
| def create_window(self): | |
| ''' | |
| Creates window and assigns self.display variable | |
| ''' | |
| self.display = pygame.display.set_mode((self.width, self.height)) | |
| pygame.display.set_caption("Racetrack") |
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 Monte_Carlo_Control: | |
| #HELPFUL FUNCTIONS | |
| def evaluate_target_policy(self): | |
| env.reset() | |
| state = env.start() | |
| self.data.episode['S'].append(state) | |
| rew = -1 | |
| while rew!=None: |
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
| #IMPORTS | |
| import torch | |
| import torchvision | |
| import torchvision.transforms as T | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from torchsummary import summary | |
| import requests | |
| from PIL import 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 download(url,fname): | |
| response = requests.get(url) | |
| with open(fname,"wb") as f: | |
| f.write(response.content) | |
| # Downloading the image | |
| download("https://specials-images.forbesimg.com/imageserve/5db4c7b464b49a0007e9dfac/960x0.jpg?fit=scale","input.jpg") | |
| # Opening the image | |
| img = Image.open('input.jpg') |
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
| # Preprocess the image | |
| def preprocess(image, size=224): | |
| transform = T.Compose([ | |
| T.Resize((size,size)), | |
| T.ToTensor(), | |
| T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), | |
| T.Lambda(lambda x: x[None]), | |
| ]) | |
| return transform(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
| # preprocess the image | |
| X = preprocess(img) | |
| # we would run the model in evaluation mode | |
| model.eval() | |
| # we need to find the gradient with respect to the input image, so we need to call requires_grad_ on it | |
| X.requires_grad_() | |
| ''' |
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 get_color_distortion(s=1.0): | |
| # s is the strength of color distortion. | |
| color_jitter = T.ColorJitter(0.8 * s, 0.8 * s, 0.8 * s, 0.2 * s) | |
| rnd_color_jitter = T.RandomApply([color_jitter], p=0.8) | |
| rnd_gray = T.RandomGrayscale(p=0.2) | |
| color_distort = T.Compose([rnd_color_jitter, rnd_gray]) | |
| return color_distort | |
| class MyDataset(Dataset): |