def test_find_key_or_empty(self): """Test dictionary key wrapper""" val = ImageLoader.find_key_or_empty({"a": 1, "b": 2}, "a") self.assertEqual(val, 1) val = ImageLoader.find_key_or_empty({"a": 1, "b": 2}, "c") self.assertEqual(val, '') val = ImageLoader.find_key_or_empty({}, "a") self.assertEqual(val, '')
def __init__(self, game): self.game = game self.ctrl = game.ctrl self.ImageLoader = ImageLoader() self.groups = game.all_sprites self._layer = 3 pg.sprite.Sprite.__init__(self, self.groups) self.init_graphics()
def test_form_proposed_url(self): """Test url forming""" loader = ImageLoader(['-u', 'http://www.google.com']) url1 = ImageLoader.form_proposed_url(loader, "//google.com/media/test.png", "append") self.assertEqual(url1, "http://google.com/media/test.png") url2 = ImageLoader.form_proposed_url(loader, "/media/test.png", "combine") self.assertEqual(url2, "http://www.google.com//media/test.png")
def run(self): channels = 4 if self.USE_ALPHA else 3 start_dimension = 4 if self.SCALE_TO_128 else 15 n_blocks = 6 if self.SCALE_TO_128 else 4 # [4, 8, 16, 32 ,64 ,128] or [15, 30, 60, 120] n_batch = [64, 64, 32, 32, 16, 16] if self.SCALE_TO_128 else [64, 64, 32, 8] n_epochs = [250, 250, 500, 500, 1000, 1000] if self.SCALE_TO_128 else [250, 250, 500, 1000] start_shape = (start_dimension, start_dimension, channels) latent_dim = 100 images_location = '/home/dustin/Documents/Projects/Python/PokeGan/images' dataset = ImageLoader.load_pokemon_dataset(images_location, self.USE_ALPHA, self.SCALE_TO_128) print(f'Loaded dataset with shape {len(dataset)}x{dataset[0].shape}') plt.imshow(dataset[600]) plt.show() # Prevent out of memory issue gpus = tf.config.experimental.list_physical_devices('GPU') print(gpus) if gpus: try: for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) except RuntimeError as e: print(e) # Select eGPU if present gpu_count = len(gpus) device = '/GPU:1' if gpu_count > 1 else '/GPU:0' with tf.device(device): gan = ProGAN(dataset, n_blocks, start_shape, latent_dim) gan.train(n_epochs, n_epochs, n_batch)
def __init__(self, SCR_WIDTH=640, SCR_HEIGHT=480): pygame.sprite.Sprite.__init__(self) self.SCR_WIDTH = SCR_WIDTH self.SCR_HEIGHT = SCR_HEIGHT resource_path = cfg.resource_path img_ship_1 = ImageLoader.load(resource_path, 'ship.png', 2.0) img_ship_2 = ImageLoader.load(resource_path, 'ship2.png', 2.0) self.images = [img_ship_1, img_ship_2] self.image = self.images[0] self.imgShot = ImageLoader.load(resource_path, 'player_shot.png', 1.0) self.rect = self.image.get_rect() self.rect.centerx = self.SCR_WIDTH / 2 self.rect.bottom = self.SCR_HEIGHT self.bullets = [] self.shot_delay = 0 self.score = 0 self.lives = Player.MAX_LIVES self.fire_sound = pygame.mixer.Sound( os.path.join(resource_path, 'laser1.wav')) self.fire_sound.set_volume(.01) self.safe_time = Player.SAFE_TIME # New player starts "safe" self.left, self.right, self.fire, self.safe = False, False, False, False
def __init__(self, SCR_WIDTH, SCR_HEIGHT): self.SCR_SIZE = SCR_WIDTH, SCR_HEIGHT self.NUM_ENEMIES = 6 self.EFFECT_DURATION = 10 self.NUM_STARS = 30 self.SAFE_TIME = 90 self.player = Player(SCR_WIDTH, SCR_HEIGHT) self.enemies = [] for e in range(self.NUM_ENEMIES): self.enemies.append(Enemy(self.SCR_SIZE[0], self.SCR_SIZE[1])) self.resource_path = cfg.resource_path self.effect_images = self.load_images() self.sounds = self.load_sounds() self.hud_font = pygame.font.Font(os.path.join(self.resource_path, 'joystix monospace.ttf'), 24) self.msg_font = pygame.font.Font(os.path.join(self.resource_path, 'joystix monospace.ttf'), 42) self.img_shipIcon = ImageLoader.load(self.resource_path, 'ship.png', 1.2) self.effects = [] self.enemy_level_step = 20 self.paused = False
from imageloader import ImageLoader from generator import Generator from discriminator import Discriminator import time from IPython.display import clear_output # 1. Import Data # ref: http://cmp.felk.cvut.cz/~tylecr1/facade/ # dataset download link: https://people.eecs.berkeley.edu/~tinghuiz/projects/pix2pix/datasets/ _URL = 'https://people.eecs.berkeley.edu/~tinghuiz/projects/pix2pix/datasets/facades.tar.gz' BUFFER_SIZE = 400 BATCH_SIZE = 1 IMG_WIDTH, IMG_HEIGHT = 256, 256 il = ImageLoader(_URL, 'facades', img_height=IMG_HEIGHT, img_width=IMG_WIDTH) PATH = il.PATH # 2. Input Pipeline train_dataset = tf.data.Dataset.list_files(PATH + 'train/*.jpg') print(train_dataset) train_dataset = train_dataset.map( il.load_image_train, num_parallel_calls=tf.data.experimental.AUTOTUNE) train_dataset = train_dataset.shuffle(BUFFER_SIZE) train_dataset = train_dataset.batch(BATCH_SIZE) test_dataset = tf.data.Dataset.list_files(PATH + 'test/*.jpg') test_dataset = test_dataset.map(il.load_image_test) test_dataset = test_dataset.batch(BATCH_SIZE) # 3. Build the models, losses and optimizers
class Player(ImageLoader, pg.sprite.Sprite): def __init__(self, game): self.game = game self.ctrl = game.ctrl self.ImageLoader = ImageLoader() self.groups = game.all_sprites self._layer = 3 pg.sprite.Sprite.__init__(self, self.groups) self.init_graphics() def init_graphics(self): self.frame_selector_walk = 0 self.frame_selector_walk_invert = False self.frame_selector_stand = 0 self.frame_selector_stand_invert = False self.frame_selector_attack = 0 self.animation_walk = self.ImageLoader.load_images(7, 'images/walk/walk', 'png', True) self.animation_stand = self.ImageLoader.load_images(10, 'images/stand/stand', 'png', True) self.animation_attack = self.ImageLoader.load_images(14, 'images/attack/attack', 'png', True) self.animation_walk = self.ImageLoader.down_scale_images(self.animation_walk, 2) self.animation_stand = self.ImageLoader.down_scale_images(self.animation_stand, 2) self.animation_attack = self.ImageLoader.down_scale_images(self.animation_attack, 2) self.image = self.animation_stand[0] self.rect = self.image.get_rect() self.mask = pg.mask.from_surface(self.image) self.rect.centerx = screen_width / 2 self.rect.y = screen_height - (self.rect.height + 10) def update(self): self.ctrl.Player.get_keys() if self.ctrl.Player.jumping: if not self.ctrl.Player.falling: self.rect.y -= 5 else: self.rect.y += 5 if self.ctrl.Player.jump_height == 0 and not self.ctrl.Player.falling: self.ctrl.Player.falling = True self.ctrl.Player.jump_height = jump_height elif self.ctrl.Player.jump_height == 0 and self.ctrl.Player.falling: self.ctrl.Player.jumping = False self.ctrl.Player.falling = False self.ctrl.Player.jump_height = jump_height self.ctrl.Player.jump_height -= 1 if self.ctrl.Player.attacking: if self.game.frame_counter %2 == 0: self.image = self.animation_attack[self.frame_selector_attack] if self.ctrl.Player.facing_right: self.image = pg.transform.flip(self.image, True, False) self.mask = pg.mask.from_surface(self.image) self.frame_selector_attack += 1 if self.frame_selector_attack + 1 > len(self.animation_attack): self.frame_selector_attack = 0 self.ctrl.Player.attacking = False elif self.ctrl.Player.move_left: if self.game.frame_counter %2 == 0: self.image = self.animation_walk[self.frame_selector_walk] self.mask = pg.mask.from_surface(self.image) if self.rect.centerx > screen_width * 0.2: self.game.Background.scroll = False self.rect.x -= 5 else: self.game.Background.scroll = True if not self.ctrl.Player.jumping: self.frame_selector_walk += 1 if self.frame_selector_walk + 1 > len(self.animation_walk): self.frame_selector_walk = 0 elif self.ctrl.Player.move_right: if self.game.frame_counter %2 == 0: self.image = self.animation_walk[self.frame_selector_walk] self.image = pg.transform.flip(self.image, True, False) self.mask = pg.mask.from_surface(self.image) if self.rect.centerx < screen_width * 0.8: self.game.Background.scroll = False self.rect.x += 5 else: self.game.Background.scroll = True if not self.ctrl.Player.jumping: self.frame_selector_walk += 1 if self.frame_selector_walk + 1 > len(self.animation_walk): self.frame_selector_walk = 0 elif self.ctrl.Player.standing: if not self.frame_selector_stand_invert: self.image = self.animation_stand[::-1][self.frame_selector_stand] else: self.image = self.animation_stand[self.frame_selector_stand] if self.ctrl.Player.facing_right: self.image = pg.transform.flip(self.image, True, False) self.mask = pg.mask.from_surface(self.image) self.frame_selector_stand += 1 if self.frame_selector_stand + 1 > len(self.animation_stand): self.frame_selector_stand_invert = not self.frame_selector_stand_invert self.frame_selector_stand = 0 if self.ctrl.Player.health == 0: self.kill()
def test_geturlresponse_notfound(self): """Get error response from incorrect url""" loader = ImageLoader(['-u', 'http://www.google.com']) _, status = loader.get_url_response( "http://www.google.com/this-does-not-exist.xml") self.assertEqual(status, 404)
def test_downloadrawurl_ok(self): """Get OK response from proper url""" loader = ImageLoader(['-u', 'http://www.google.com']) _, status = loader.get_url_response("http://www.google.com/") self.assertEqual(status, 200)
def test_config_case2(self): """Config arguments case2""" loader = ImageLoader(['-u', 'http://www.google.com', '-D', 'dldir']) self.assertEqual(loader.args.url, 'http://www.google.com') self.assertEqual(loader.debug_mode, False) self.assertEqual(loader.args.dir, 'dldir')
def test_config_case1(self): """Config arguments case1""" loader = ImageLoader(['-u', 'http://www.google.com', '-d']) self.assertEqual(loader.args.url, 'http://www.google.com') self.assertEqual(loader.debug_mode, True) self.assertEqual(loader.args.dir, 'saved-images')
def load_images(self): img_boom_1 = ImageLoader.load(self.resource_path, 'boom.png', 2.0) img_boom_2 = ImageLoader.load(self.resource_path, 'boom2.png', 2.0) img_boom_3 = ImageLoader.load(self.resource_path, 'boom3.png', 2.0) return [img_boom_1, img_boom_2, img_boom_3]