def forward(self, input, is_train=False): _input = noisy(self.vocab, input, * self.args.noise) if is_train else input mu, logvar = self.encode(_input) z = reparameterize(mu, logvar) logits, _ = self.decode(z, input) return mu, logvar, z, logits
def forward(self, input): if self.training: _input = noisy(self.padidx, input, 0.3, 0, 0, 0) else: _input = input #mu, logvar = self.encode(_input) h = self.encode(_input) return h #mu, logvar
def eval_genomes(genomes, config): for genome_id,genome in genomes: ob = env.reset() inx, iny, inc = env.observation_space.shape inx = int(inx/8) iny = int(iny/8) net = neat.nn.recurrent.RecurrentNetwork.create(genome, config) fitness_current = 0 frame = 0 counter = 0 done = False while not done: frame += 1 factor = 0.5 ob = np.uint8(noise.noisy(ob, factor)) ob = cv2.resize(ob, (inx,iny)) ob = cv2.cvtColor(ob, cv2.COLOR_BGR2GRAY) imageArr = np.ndarray.flatten(ob) nnOutput = net.activate(imageArr) numerical_input = nnOutput.index(max(nnOutput)) ob, rew, done, info = env.step(numerical_input) fitness_current += rew if(rew > 0): counter = 0 else: counter += 1 env.render() if(done or counter == 250): done = True print(genome_id, fitness_current) genome.fitness = fitness_current
def eval_genomes(genomes, config): for genome_id, genome in genomes: observation = env.reset() inputx, inputy, inputColour = env.observation_space.shape inputx = int(inputx / 8) inputy = int(inputy / 8) net = neat.nn.recurrent.RecurrentNetwork.create(genome, config) fitness_current = 0 frame = 0 counter = 0 game_done = False while not game_done: frame += 1 factor = 0.5 observation = np.uint8(noise.noisy(observation, factor)) observation = cv2.resize(observation, (inputx, inputy)) #observation = cv2.cvtColor(observation, cv2.COLOR_RGB2GRAY) imagearray = np.ndarray.flatten(observation) nnOutput = net.activate(imagearray) numerical_input = nnOutput.index(max(nnOutput)) observation, reward, game_done, info = env.step(numerical_input) fitness_current += reward if reward > 0: counter = 0 else: counter += 1 env.render() if game_done or counter == 300: game_done = True print(genome_id, fitness_current, counter) genome.fitness = fitness_current
def imageCapture(self): monitor = {'left': 0, 'top': 0, 'width': self.w, 'height': self.h} with mss.mss() as sct: while True: sct_img = sct.grab(monitor) img_np = np.array(sct_img) if self.noise: img_np = noise.noisy(img_np, self.noiseType) img_np = cv2.normalize(img_np, None, 0, 255, cv2.NORM_MINMAX).astype('uint8') gray_frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY) threash_triangle = threshold_triangle(gray_frame) binary_triangle = gray_frame > threash_triangle binary_triangle = img_as_ubyte(binary_triangle) cropped_bw_frame = binary_triangle[ int(7 * (self.h / 13.0)):int(self.h - (self.h / 5)), int(self.w / 5):int(self.w - (self.w / 5))] resized_bw_frame = cv2.resize(cropped_bw_frame, (int(160), int(90)), interpolation=cv2.INTER_AREA) self.bw_frame = cv2.bitwise_not(resized_bw_frame) self.rgb_frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB) _ = cv2.waitKey(1)
import noise import numpy as np import cv2 img = cv2.imread('buried.jpg') img = np.uint8(noise.noisy(img, 0.5)) cv2.imshow("image", img) cv2.waitKey(0) cv2.desAllWindows()
out1 = VideoWriter('s&p_2.mp4', fourcc, 60.0, (w4, h4)) in2 = VideoWriter('gauss_1.mp4', fourcc, 60.0, (w1, h1)) out2 = VideoWriter('gauss_2.mp4', fourcc, 60.0, (w4, h4)) in3 = VideoWriter('poisson_1.mp4', fourcc, 60.0, (w1, h1)) out3 = VideoWriter('poisson_2.mp4', fourcc, 60.0, (w4, h4)) in4 = VideoWriter('speckle_1.mp4', fourcc, 60.0, (w1, h1)) out4 = VideoWriter('speckle_2.mp4', fourcc, 60.0, (w4, h4)) bw1 = list() bw2 = list() bw3 = list() bw4 = list() for i in tqdm(range(int(cap.get(7)))): ret, frame = cap.read() if ret: n1 = noise.noisy(frame, "s&p") n1 = cv2.normalize(n1, None, 0, 255, cv2.NORM_MINMAX).astype('uint8') in1.write(n1) gray_frame = cv2.cvtColor(n1, cv2.COLOR_BGR2GRAY) threash_triangle = threshold_triangle(gray_frame) binary_triangle = gray_frame > threash_triangle binary_triangle = img_as_ubyte(binary_triangle) cropped_bw_frame = binary_triangle[int(7 * (h / 13.0)):int(h - (h / 5)), int(w / 5):int(w - (w / 5))] resized_bw_frame = cv2.resize(cropped_bw_frame, (int(160), int(90)), interpolation=cv2.INTER_AREA) bw_frame = cv2.bitwise_not(resized_bw_frame) bw1.append(bw_frame) if i > 20: difference = cv2.bitwise_not(bw1[i - 20] - bw_frame)