def ugoira2gif(ugoira_file, exportname, delete_ugoira, fmt='gif', image=None): print_and_log('info', 'processing ugoira to animated gif...') temp_folder = tempfile.mkdtemp() # imageio cannot handle utf-8 filename temp_name = temp_folder + os.sep + "temp.gif" with zipfile.ZipFile(ugoira_file) as f: f.extractall(temp_folder) filenames = os.listdir(temp_folder) filenames.remove('animation.json') anim_info = json.load(open(temp_folder + '/animation.json')) durations = [] images = [] for info in anim_info["frames"]: images.append(imageio.imread(temp_folder + os.sep + info["file"])) durations.append(float(info["delay"]) / 1000) kargs = {'duration': durations} imageio.mimsave(temp_name, images, fmt, **kargs) shutil.move(temp_name, exportname) print_and_log('info', 'ugoira exported to: ' + exportname) shutil.rmtree(temp_folder) if delete_ugoira: print_and_log('info', 'deleting ugoira {0}'.format(ugoira_file)) os.remove(ugoira_file) # set last-modified and last-accessed timestamp if image is not None and _config.setLastModified and exportname is not None and os.path.isfile(exportname): ts = time.mktime(image.worksDateDateTime.timetuple()) os.utime(exportname, (ts, ts))
def make_gif(gif_name): frames = [] for x in range(len(input_image)): #for j in range(3): frames.append(imageio.imread('{0}.jpg'.format(x))) # Save them as frames into a gif imageio.mimsave(gif_name, frames, 'GIF', duration = 1.4)
def test_types(): need_internet() fname1 = get_remote_file("images/stent.swf", test_dir) fname2 = fname1[:-4] + ".out3.swf" for dtype in [ np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16, np.int32, np.int64, np.float16, np.float32, np.float64, ]: for shape in [(100, 1), (100, 3)]: # Repeats an identity matrix, just for testing im1 = np.dstack((np.identity(shape[0], dtype=dtype),) * shape[1]) imageio.mimsave(fname2, [im1], "swf") im2 = imageio.mimread(fname2, "swf")[0] assert im2.shape == (100, 100, 4) assert im2.dtype == np.uint8 if len(shape) == 3 and dtype == np.uint8: assert (im1[:, :, 0] == im2[:, :, 0]).all()
def test_ico(): for float in (False, True): for crop in (0, 1, 2): for colors in (0, 1, 3, 4): fname = fnamebase + '%i.%i.%i.ico' % (float, crop, colors) rim = get_ref_im(colors, crop, float) imageio.imsave(fname, rim) im = imageio.imread(fname) mul = 255 if float else 1 assert_close(rim * mul, im, 0.1) # lossless # Meta data R = imageio.read(fnamebase + '0.0.0.ico') assert isinstance(R.get_meta_data(0), dict) assert isinstance(R.get_meta_data(None), dict) # But this print warning writer = imageio.save(fnamebase + 'I.ico') writer.set_meta_data({}) writer.close() # Parameters. Note that with makealpha, RGBA images are read in incorrectly im = imageio.imread(fnamebase + '0.0.0.ico', makealpha=True) assert im.ndim == 3 and im.shape[-1] == 4 # Parameter fail raises(TypeError, imageio.imread, fname, notavalidkwarg=True) raises(TypeError, imageio.imsave, fnamebase + '1.gif', im, notavalidk=True) # Multiple images im = get_ref_im(4, 0, 0) ims1 = im, np.column_stack([im, im]), np.row_stack([im, im]) imageio.mimsave(fnamebase + 'I.ico', ims1) ims2 = imageio.mimread(fnamebase + 'I.ico') for im1, im2 in zip(ims1, ims2): assert_close(im1, im2, 0.1)
def ugoira2gif(ugoira_file, exportname): printAndLog('info', 'processing ugoira to animated gif...') temp_folder = tempfile.mkdtemp() # imageio cannot handle utf-8 filename temp_name = temp_folder + os.sep + "temp.gif" z = zipfile.ZipFile(ugoira_file) z.extractall(temp_folder) filenames = os.listdir(temp_folder) filenames.remove('animation.json') anim_info = json.load(open(temp_folder + '/animation.json')) durations = [] images = [] for info in anim_info["frames"]: images.append(imageio.imread(temp_folder + os.sep + info["file"])) durations.append(float(info["delay"]) / 1000) kargs = {'duration': durations} imageio.mimsave(temp_name, images, 'GIF', **kargs) shutil.move(temp_name, exportname) printAndLog('info', 'ugoira exported to: ' + exportname) shutil.rmtree(temp_folder)
def generate_gif(save='tmp', epochs=10): images = [] filenames = ["./." + save + "%d.png" % (epoch + 1) for epoch in np.arange(epochs)] for filename in filenames: images.append(imageio.imread(filename)) os.remove(filename) imageio.mimsave('./figures/' + save + '.gif', images, duration=.5)
def save_masked_image (path, image_path, masks_path): images = [] image = cv2.imread(image_path, -1) if len(image.shape) == 2: image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) assert len(image.shape) == 3 rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) images.append(rgb) #vis = np.zeros_like(rgb, dtype=np.uint8) vis = np.copy(rgb).astype(np.float32) mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE) print(np.unique(mask)) vis[:, :, 0][mask > 0.5] *= 0.5 vis[:, :, 1][mask > 0.5] *= 0.5 vis[:, :, 2][mask > 0.5] *= 0.5 b, g, r = 180, 119, 31 vis[:, :, 0] += b * mask * 0.5 vis[:, :, 1] += g * mask * 0.5 vis[:, :, 2] += r * mask * 0.5 images.append(np.clip(vis, 0, 255).astype(np.uint8)) imageio.mimsave(path + ".gif",images, duration = 1) sp.check_call('gifsicle --colors 256 -O3 < %s.gif > %s; rm %s.gif' % (path, path, path), shell=True) pass
def test_series(): im1 = imageio.imread("imageio:chelsea.png") ims1 = [im1, im1 * 0.8, im1 * 0.5] fname = os.path.join(test_dir, "chelseam.bsdf") imageio.mimsave(fname, ims1) # Does it look alright if we open it in bsdf without extensions? raw = bsdf.load(fname, []) assert isinstance(raw, list) and len(raw) == 3 for r in raw: assert set(r.keys()) == set(["meta", "array"]) assert isinstance(r["meta"], dict) assert isinstance(r["array"], dict) assert r["array"]["shape"] == list(im1.shape) assert isinstance(r["array"]["data"], bytes) # Read multi-image as singleton im2 = imageio.imread(fname) assert np.all(im1 == im2) # Read multi-image as series ims2 = imageio.mimread(fname) assert len(ims2) == 3 and all(np.all(ims1[i] == ims2[i]) for i in range(3)) # Read + write back without image extensions bsdf.save(fname, bsdf.load(fname)) ims3 = imageio.mimread(fname) assert len(ims3) == 3 and all(np.all(ims1[i] == ims3[i]) for i in range(3))
def omni_view_gif(filenames, name='omni_movie.gif'): '''Give it a full path for images and all the images you want to use in order to use. list format.''' import imageio images = [] for filename in filenames: images.append(imageio.imread(filename)) imageio.mimsave(name, images)
def create_gif(image_list, gif_name): frames = [] for image_name in image_list: frames.append(imageio.imread(image_name)) # Save them as frames into a gif imageio.mimsave(gif_name, frames, 'GIF', duration=1) return
def convert_images_to_gif(output_images_dir, output_gif): """Convert a list of images to a gif.""" image_dir = "{0}/*.png".format(output_images_dir) list_images = glob.glob(image_dir) file_names = sort_nicely(list_images) images = [imageio.imread(fn) for fn in file_names] imageio.mimsave(output_gif, images)
def notify(self, key, frames): import imageio video_filename = '/tmp/' + key_hash(key) + '.gif' frames = [imresize(img, 0.25) for img in frames.transpose([2, 0, 1])] imageio.mimsave(video_filename, frames, duration=0.5) msg = 'eye frames for {animal_id}-{session}-{scan_idx}'.format(**key) slack_user = notify.SlackUser() & (experiment.Session() & key) slack_user.notify(file=video_filename, file_title=msg, channel='#pipeline_quality')
def save_gif(images, filename, reverse=True): dly_amt = .1 durations = [] durations.append(1) durations += [dly_amt]*(len(images)-2) durations.append(1) if reverse: for img in reversed(images[1:-1]): images.append(img) durations.append(dly_amt) imageio.mimsave(filename, images, subrectangles=True, palettesize=32, duration=durations, loop=0)
def test_tifffile_reading_writing(): """ Test reading and saving tiff """ need_internet() # We keep a test image in the imageio-binary repo im2 = np.ones((10, 10, 3), np.uint8) * 2 filename1 = os.path.join(test_dir, 'test_tiff.tiff') # One image imageio.imsave(filename1, im2) im = imageio.imread(filename1) ims = imageio.mimread(filename1) assert (im == im2).all() assert len(ims) == 1 # Multiple images imageio.mimsave(filename1, [im2, im2, im2]) im = imageio.imread(filename1) ims = imageio.mimread(filename1) assert (im == im2).all() assert len(ims) == 3, ims[0].shape # remote multipage rgb file filename2 = get_remote_file('images/multipage_rgb.tif') img = imageio.mimread(filename2) assert len(img) == 2 assert img[0].shape == (3, 10, 10) # Mixed W = imageio.save(filename1) W.set_meta_data({'planarconfig': 'planar'}) assert W.format.name == 'TIFF' W.append_data(im2) W.append_data(im2) W.close() # R = imageio.read(filename1) assert R.format.name == 'TIFF' ims = list(R) # == [im for im in R] assert (ims[0] == im2).all() meta = R.get_meta_data() assert meta['orientation'] == 'top_left' # Fail raises(IndexError, R.get_data, -1) raises(IndexError, R.get_data, 3) # Ensure imwrite write works round trip filename3 = os.path.join(test_dir, 'test_tiff2.tiff') R = imageio.imread(filename1) imageio.imwrite(filename3, R) R2 = imageio.imread(filename3) assert (R == R2).all()
def png2gif(dir_name): path = os.getcwd() os.chdir(dir_name) dirs = os.listdir(dir_name) images = [] num = 0 for d in dirs: if d.split('-')[-1] == 'txt.png': images.append(imageio.imread(d)) num += 1 os.chdir(path) imageio.mimsave(d.split('-')[0]+'-txt_c.gif',images,duration = DURARION)
def test_random_access(): im1 = imageio.imread("imageio:chelsea.png") ims1 = [im1, im1 * 0.8, im1 * 0.5] fname = os.path.join(test_dir, "chelseam.bsdf") imageio.mimsave(fname, ims1) r = imageio.get_reader(fname) for i in (1, 0, 2, 0, 1, 2): assert np.all(ims1[i] == r.get_data(i))
def save_as_gif(self): image_files = next(os.walk(self.download_path))[2] image_data = [] for image in image_files: if os.path.splitext(image)[1] == ".jpg": image_data.append(imageio.imread(self.download_path + image)) os.remove(self.download_path + image) imageio.mimsave(self.download_path + 'movie.gif', image_data) print("successfully saved gif to " + self.download_path + "movie.gif")
def test_npz_reading_writing(): """ Test reading and saveing npz """ if IS_PYPY: return # no support for npz format :( im2 = np.ones((10, 10), np.uint8) * 2 im3 = np.ones((10, 10, 10), np.uint8) * 3 im4 = np.ones((10, 10, 10, 10), np.uint8) * 4 filename1 = os.path.join(test_dir, 'test_npz.npz') # One image imageio.imsave(filename1, im2) im = imageio.imread(filename1) ims = imageio.mimread(filename1) assert (im == im2).all() assert len(ims) == 1 # Multiple images imageio.mimsave(filename1, [im2, im2, im2]) im = imageio.imread(filename1) ims = imageio.mimread(filename1) assert (im == im2).all() assert len(ims) == 3 # Volumes imageio.mvolsave(filename1, [im3, im3]) im = imageio.volread(filename1) ims = imageio.mvolread(filename1) assert (im == im3).all() assert len(ims) == 2 # Mixed W = imageio.save(filename1) assert W.format.name == 'NPZ' W.append_data(im2) W.append_data(im3) W.append_data(im4) raises(RuntimeError, W.set_meta_data, {}) # no meta data support W.close() # R = imageio.read(filename1) assert R.format.name == 'NPZ' ims = list(R) # == [im for im in R] assert (ims[0] == im2).all() assert (ims[1] == im3).all() assert (ims[2] == im4).all() # Fail raises(IndexError, R.get_data, -1) raises(IndexError, R.get_data, 3) raises(RuntimeError, R.get_meta_data, None) # no meta data support raises(RuntimeError, R.get_meta_data, 0) # no meta data support
def test_types(): fname1 = get_remote_file('images/stent.swf', test_dir) fname2 = fname1[:-4] + '.out3.swf' for dtype in [np.uint8, np.float32]: for shape in [(100, 100), (100, 100, 1), (100, 100, 3)]: im1 = np.empty(shape, dtype) # empty is nice for testing nan imageio.mimsave(fname2, [im1], 'swf') im2 = imageio.mimread(fname2, 'swf')[0] assert im2.shape == (100, 100, 4) assert im2.dtype == np.uint8 if len(shape) == 3 and dtype == np.uint8: assert (im1[:, :, 0] == im2[:, :, 0]).all()
def make_gif(image_dir_path, target_path): images = [] for item in os.listdir(image_dir_path): print item item_path = os.path.join(image_dir_path, item) if os.path.isfile(item_path): try: # image = Image.open(item_path) image = imageio.imread(item_path) except IOError: print "File called '{}' is not an image.".format(item) continue images.append(image) print len(images) imageio.mimsave(target_path, images)
def tearDown(self): self.save_screenshot() # Create an animated gif of the captured screenshots for this test. images = [] durations = [] prev_screenshot_time = None for screenshot in self.screenshots: images.append(imageio.imread(screenshot['filename'])) os.unlink(screenshot['filename']) if prev_screenshot_time: durations.append( screenshot['time'] - prev_screenshot_time ) prev_screenshot_time = screenshot['time'] # We didn't record a duration for the first frame because we # couldn't know it until we processed the second frame -- we won't # have a frame to process after the last frame, so let's mark # that frame to be displayed for 1s. durations.append(1) imageio.mimsave( os.path.join( self.SCREENSHOT_DIR, '{test_name}.gif'.format( test_name=self.id() ) ), images, duration=durations, ) max_termination_wait_seconds = 10 terminated = time.time() self.gui_proc.send_signal(signal.SIGINT) while(time.time() < terminated + max_termination_wait_seconds): if self.gui_proc.poll(): return try: # If we've made it this far, the process is probably still running self.gui_proc.kill() except OSError: # Whoops; guess not pass self.grbl_proc.send_signal(signal.SIGINT)
def main(argv): if len(argv) == 2: dir = argv[1] title = os.path.splitext(os.path.basename(dir))[0] else: print ('usage:\n python images_2gif.py <pasta com imagens>') return vis_folders = [f for f in listdir(dir) if isdir(join(dir, f))] filenames = [[f for f in listdir(join(dir, v_fold)) if isfile(join(join(dir, v_fold), f))] for v_fold in vis_folders] for i, vis in enumerate(vis_folders): images = [] for filename in filenames[i]: images.append(imageio.imread(dir+vis+'/'+filename)) imageio.mimsave(dir+vis+'/movie.gif', images, duration=2.4) return
def render_single_gif(self, participant=None, phase=None, image=None, traj=None, temp_file=None): if not traj: traj = self.data[participant][phase][image] if traj: data = map(lambda d: d.get_stabilized_position()[:2], traj) animation = self.animate(data) print "Rendering at %s FPS" % self.fps # print participant, phase, meaning if temp_file is None: meaning = image.split("(")[-1].split(".")[0] fname = os.path.join(self.txtOutputPath.text(), "%s_phase%s_%s.%s") % \ (participant, phase, meaning, self.extension) print "Outputting %s..." % fname im.mimsave(fname, animation, fps=self.fps) else: im.mimsave(temp_file, animation, fps=self.fps)
def test_ico(): if os.getenv("TRAVIS", "") == "true" and sys.version_info >= (3, 4): skip("Freeimage ico is unstable for this Travis build") for float in (False, True): for crop in (0,): for colors in (1, 3, 4): fname = fnamebase + "%i.%i.%i.ico" % (float, crop, colors) rim = get_ref_im(colors, crop, float) rim = rim[:32, :32] # ico needs nice size imageio.imsave(fname, rim) im = imageio.imread(fname) mul = 255 if float else 1 assert_close(rim * mul, im, 0.1) # lossless # Meta data R = imageio.read(fnamebase + "0.0.1.ico") assert isinstance(R.get_meta_data(0), dict) assert isinstance(R.get_meta_data(None), dict) # But this print warning R.close() writer = imageio.save(fnamebase + "I.ico") writer.set_meta_data({}) writer.close() # Parameters. Note that with makealpha, RGBA images are read in incorrectly im = imageio.imread(fnamebase + "0.0.1.ico", makealpha=True) assert im.ndim == 3 and im.shape[-1] == 4 # Parameter fail raises(TypeError, imageio.imread, fname, notavalidkwarg=True) raises(TypeError, imageio.imsave, fnamebase + "1.gif", im, notavalidk=True) if sys.platform.startswith("win"): # issue #21 skip("Windows has a known issue with multi-icon files") # Multiple images im = get_ref_im(4, 0, 0)[:32, :32] ims = [np.repeat(np.repeat(im, i, 1), i, 0) for i in (1, 2)] # SegF on win ims = im, np.column_stack((im, im)), np.row_stack((im, im)) # error on win imageio.mimsave(fnamebase + "I2.ico", ims) ims2 = imageio.mimread(fnamebase + "I2.ico") for im1, im2 in zip(ims, ims2): assert_close(im1, im2, 0.1)
def save_movie(images, num_x, num_y, out_file=None, movie_id=0): if out_file is None: logger.warning('`out_file` not provided. Not saving.') else: images_ = [] for i, image in enumerate(images): if _options['quantized']: image = dequantize(image) dim_c, dim_x, dim_y = image.shape[-3:] image = image.reshape((num_x, num_y, dim_c, dim_x, dim_y)) image = image.transpose(0, 3, 1, 4, 2) image = image.reshape(num_x * dim_x, num_y * dim_y, dim_c) if _options['use_tanh']: image = 0.5 * (image + 1.) images_.append(image) imageio.mimsave(out_file, images_) visualizer.video(videofile=out_file, env=exp.NAME, win='movie_{}'.format(movie_id))
def test_tifffile_reading_writing(): """ Test reading and saveing tiff """ im2 = np.ones((10, 10, 3), np.uint8) * 2 filename1 = os.path.join(test_dir, "test_tiff.tiff") # One image imageio.imsave(filename1, im2) im = imageio.imread(filename1) ims = imageio.mimread(filename1) assert (im == im2).all() assert len(ims) == 1 # Multiple images imageio.mimsave(filename1, [im2, im2, im2]) im = imageio.imread(filename1) ims = imageio.mimread(filename1) assert (im == im2).all() assert len(ims) == 3, ims[0].shape # remote multipage rgb file filename2 = get_remote_file("images/multipage_rgb.tif") img = imageio.mimread(filename2) assert len(img) == 2 assert img[0].shape == (3, 10, 10) # Mixed W = imageio.save(filename1) W.set_meta_data({"planarconfig": "planar"}) assert W.format.name == "TIFF" W.append_data(im2) W.append_data(im2) W.close() # R = imageio.read(filename1) assert R.format.name == "TIFF" ims = list(R) # == [im for im in R] assert (ims[0] == im2).all() meta = R.get_meta_data() assert meta["is_rgb"] # Fail raises(IndexError, R.get_data, -1) raises(IndexError, R.get_data, 3)
def to_movie(self, output_fn, fps=15., dpi=50, cut=None, cmap='gray', extension=1): viz = [] with click.progressbar(self.mosaic_filenames, label="Reading mosaics", show_pos=True) as bar: for fn in bar: try: frame = KeplerMosaicMovieFrame(fn) fig = frame.to_fig(rowrange=self.rowrange, colrange=self.colrange, dpi=dpi, cut=cut, cmap=cmap, extension=extension,) img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='') img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,)) pl.close(fig) # Avoid memory leak! viz.append(img) except InvalidFrameException: print("InvalidFrameException for {}".format(fn)) # Save the output as a movie if output_fn.endswith('.gif'): kwargs = {'duration': 1. / fps} else: kwargs = {'fps': fps} imageio.mimsave(output_fn, viz, **kwargs)
def test_ico(): for float in (False, True): for crop in (0, 1, 2): for colors in (1, 3, 4): fname = fnamebase + '%i.%i.%i.ico' % (float, crop, colors) rim = get_ref_im(colors, crop, float) imageio.imsave(fname, rim) im = imageio.imread(fname) mul = 255 if float else 1 assert_close(rim * mul, im, 0.1) # lossless # Meta data R = imageio.read(fnamebase + '0.0.1.ico') assert isinstance(R.get_meta_data(0), dict) assert isinstance(R.get_meta_data(None), dict) # But this print warning R.close() writer = imageio.save(fnamebase + 'I.ico') writer.set_meta_data({}) writer.close() # Parameters. Note that with makealpha, RGBA images are read in incorrectly im = imageio.imread(fnamebase + '0.0.1.ico', makealpha=True) assert im.ndim == 3 and im.shape[-1] == 4 # Parameter fail raises(TypeError, imageio.imread, fname, notavalidkwarg=True) raises(TypeError, imageio.imsave, fnamebase + '1.gif', im, notavalidk=True) if sys.platform.startswith('win'): # issue #21 skip('Windows has a known issue with multi-icon files') # Multiple images im = get_ref_im(4, 0, 0) ims = [np.repeat(np.repeat(im, i, 1), i, 0) for i in (1, 2)] # SegF on win ims = im, np.column_stack((im, im)), np.row_stack((im, im)) # error on win imageio.mimsave(fnamebase + 'I2.ico', ims) ims2 = imageio.mimread(fnamebase + 'I2.ico') for im1, im2 in zip(ims, ims2): assert_close(im1, im2, 0.1)
def ascii_seq_to_gif(seq, output_path, fps=15.0, font_size=10, font_path=None): """ Creates a gif from a sequence of ascii images. Parameters ---------- output_path : str Path for gif output. fps : float FPS for gif playback. font_size : int Font size for ascii. """ images = [] status = StatusBar(len(seq), text="Generating frames: ",) for index, ascii_img in enumerate(seq): if type(ascii_img) == str: #raw text text = ascii_img else: #AsciiImage instance text = ascii_img.data images.append( ascii_to_pil(text, font_size=font_size, font_path=font_path ) ) status.update(index) status.complete() duration = 1.0/fps images_np = [np.array(img) for img in images] imageio.mimsave(output_path, images_np, duration=duration)
def makeGif(self, exportName, loop = False, sourcesDelete = False): self.exportName = exportName #This is the file name you want to be given to the exported file. self.sourcesDelete = sourcesDelete #Set this to true if you want to delete the sources - Default False #We need to save each PIL image into a temp file so it can be loaded by imageio into the frames list. frames = [] for img in self.images: img.save("temp.jpg") frames.append(imageio.imread("temp.jpg")) #If there is a request to loop it then copy and reverse the list if loop: frames = frames + frames[::-1] # Save them as frames into a gif imageio.mimsave(self.exportName, frames) #Delete the temp files and sources if requested os.remove("temp.jpg") if self.sourcesDelete: for img in os.listdir(self.sourcesDir): os.remove(self.sourcesDir + "/" + img)
def render_hpgl(hpgl, output, pos, scale): x, y = pos frames = [] image = Image.open("static/harryplotter_roll.gif") palette = image.palette.getdata() print("Rendering hpgl") # So let's be honest. I have no idea why exactly this is working and # why i need four \x00 instead of just three of them. But hey, it's # working! #i = 0 #while not (palette[1][i] == 0xFF and palette[1][i + 1] == 0xFF and palette[1][i + 2] == 0xFF): # i += 4 tmp_palette = bytearray(image.palette.palette) tmp_palette[100 * 3] = 0 tmp_palette[100 * 3 + 1] = 0 tmp_palette[100 * 3 + 2] = 0 image.palette.palette = bytes(tmp_palette) black_idx = 100 w, h = image.size draw = ImageDraw.Draw(image) hpgl_cmds = open(hpgl).read().split(";") last_coord = (0, 0) ani_counter = 0 # Count pen moves pen_moves = 0 frame_count = 30 for cmd in hpgl_cmds: if cmd[:2] == "PD" or cmd[:2] == "PA": pen_moves += cmd.count(",") frame_skip = pen_moves // (frame_count - 1) for cmd in hpgl_cmds: if cmd[:2] == "PU": coords = scale_coords(get_coords(cmd[2:]), (x, y), (w, h), scale) last_coord = coords[-1] elif cmd[:2] == "PD" or cmd[:2] == "PA": coords = scale_coords(get_coords(cmd[2:]), (x, y), (w, h), scale) for coord in coords: draw.line(last_coord + coord, fill=black_idx) last_coord = coord ani_counter += 1 if frame_skip == 0: image.save("/tmp/frame.gif") frames.append(imageio.imread("/tmp/frame.gif")) elif ani_counter % frame_skip == 0: image.save("/tmp/frame.gif") frames.append(imageio.imread("/tmp/frame.gif")) image.save("/tmp/frame.gif") for i in range(frame_count // 2): frames.append(imageio.imread("/tmp/frame.gif")) del draw print("Writing gif") #writeGif(output, frames, duration=0.2) imageio.mimsave(output, frames)
def test_animated_gif(): # Read newton's cradle ims = imageio.mimread("imageio:newtonscradle.gif") assert len(ims) == 36 for im in ims: assert im.shape == (150, 200, 4) assert im.min() > 0 assert im.max() <= 255 # Get images im = get_ref_im(4, 0, 0) ims = [] for i in range(10): im = im.copy() im[:, -5:, 0] = i * 20 ims.append(im) # Store - animated GIF always poops out RGB for isfloat in (False, True): for colors in (3, 4): ims1 = ims[:] if isfloat: ims1 = [x.astype(np.float32) / 256 for x in ims1] ims1 = [x[:, :, :colors] for x in ims1] fname = fnamebase + ".animated.%i.gif" % colors imageio.mimsave(fname, ims1, duration=0.2) # Retrieve print("fooo", fname, isfloat, colors) ims2 = imageio.mimread(fname) ims1 = [x[:, :, :3] for x in ims] # fresh ref ims2 = [x[:, :, :3] for x in ims2] # discart alpha for im1, im2 in zip(ims1, ims2): assert_close(im1, im2, 1.1) # We can also store grayscale fname = fnamebase + ".animated.%i.gif" % 1 imageio.mimsave(fname, [x[:, :, 0] for x in ims], duration=0.2) imageio.mimsave(fname, [x[:, :, :1] for x in ims], duration=0.2) # Irragular duration. You probably want to check this manually (I did) duration = [0.1 for i in ims] for i in [2, 5, 7]: duration[i] = 0.5 imageio.mimsave(fnamebase + ".animated_irr.gif", ims, duration=duration) # Other parameters imageio.mimsave(fnamebase + ".animated.loop2.gif", ims, loop=2, fps=20) R = imageio.read(fnamebase + ".animated.loop2.gif") W = imageio.save(fnamebase + ".animated.palettes100.gif", palettesize=100) assert W._writer.opt_palette_size == 128 # Fail assert raises(IndexError, R.get_meta_data, -1) assert raises(ValueError, imageio.mimsave, fname, ims, palettesize=300) assert raises(ValueError, imageio.mimsave, fname, ims, quantizer="foo") assert raises(ValueError, imageio.mimsave, fname, ims, duration="foo") # Add one duplicate image to ims to touch subractangle with not change ims.append(ims[-1]) # Test subrectangles imageio.mimsave(fnamebase + ".subno.gif", ims, subrectangles=False) imageio.mimsave(fnamebase + ".subyes.gif", ims, subrectangles=True) s1 = os.stat(fnamebase + ".subno.gif").st_size s2 = os.stat(fnamebase + ".subyes.gif").st_size assert s2 < s1 # Meta (dummy, because always {}) imageio.mimsave(fname, [x[:, :, 0] for x in ims], duration=0.2) assert isinstance(imageio.read(fname).get_meta_data(), dict)
viz.show3Dpose(p3d, ax, lcolor="#9b59b6", rcolor="#2ecc71") pngName = 'png/pose_frame_{0}.png'.format(str(frame).zfill(12)) plt.savefig(pngName) if FLAGS.write_gif: png_lib.append(imageio.imread(pngName)) if FLAGS.cache_on_fail: before_pose = poses3d if FLAGS.write_gif: if FLAGS.interpolation: #take every frame on gif_fps * multiplier_inv png_lib = np.array([png_lib[png_image] for png_image in range(0,len(png_lib), int(multiplier_inv)) ]) logger.info("creating Gif gif_output/animation.gif, please Wait!") imageio.mimsave('gif_output/animation.gif', png_lib, fps=FLAGS.gif_fps) _out_file = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'maya/3d_data.json') twod_out_file = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'maya/2d_data.json') with open(_out_file, 'w') as outfile: logger.info("exported maya json to {0}".format(_out_file)) json.dump(export_units, outfile) with open(twod_out_file, 'w') as outfile: logger.info("exported maya json to {0}".format(twod_out_file)) json.dump(twod_export_units, outfile) logger.info("Done!".format(pngName)) if __name__ == "__main__": openpose_output_dir = FLAGS.pose_estimation_json
def create_gif(images, output_file, duration=0.1): imageio.mimsave(output_file, images, duration=duration)
def reconstruction(config, generator, kp_detector, checkpoint, log_dir, dataset): png_dir = os.path.join(log_dir, 'reconstruction/png') log_dir = os.path.join(log_dir, 'reconstruction') if checkpoint is not None: Logger.load_cpk(checkpoint, generator=generator, kp_detector=kp_detector) else: raise AttributeError( "Checkpoint should be specified for mode='reconstruction'.") dataloader = DataLoader(dataset, batch_size=1, shuffle=False, num_workers=1) if not os.path.exists(log_dir): os.makedirs(log_dir) if not os.path.exists(png_dir): os.makedirs(png_dir) loss_list = [] if torch.cuda.is_available(): generator = DataParallelWithCallback(generator) kp_detector = DataParallelWithCallback(kp_detector) generator.eval() kp_detector.eval() for it, x in tqdm(enumerate(dataloader)): if config['reconstruction_params']['num_videos'] is not None: if it > config['reconstruction_params']['num_videos']: break with torch.no_grad(): predictions = [] visualizations = [] if torch.cuda.is_available(): x['video'] = x['video'].cuda() kp_source = kp_detector(x['video'][:, :, 0]) for frame_idx in range(x['video'].shape[2]): source = x['video'][:, :, 0] driving = x['video'][:, :, frame_idx] kp_driving = kp_detector(driving) out = generator(source, kp_source=kp_source, kp_driving=kp_driving) out['kp_source'] = kp_source out['kp_driving'] = kp_driving del out['sparse_deformed'] predictions.append( np.transpose(out['prediction'].data.cpu().numpy(), [0, 2, 3, 1])[0]) visualization = Visualizer( **config['visualizer_params']).visualize(source=source, driving=driving, out=out) visualizations.append(visualization) loss_list.append( torch.abs(out['prediction'] - driving).mean().cpu().numpy()) predictions = np.concatenate(predictions, axis=1) imageio.imsave(os.path.join(png_dir, x['name'][0] + '.png'), (255 * predictions).astype(np.uint8)) image_name = x['name'][0] + config['reconstruction_params'][ 'format'] imageio.mimsave(os.path.join(log_dir, image_name), visualizations) print("Reconstruction loss: %s" % np.mean(loss_list))
def get_curvature_density_bait(window, path, kern_sizes=[]): column_names = [ "plate", "inst", "treatment", "angle", "curvature", "density", "growth", "speed", "straightness", "dist_P", "dist_N", "angle_to_P", "angle_to_N", "t", "hyph", 'x', 'y', 'vx', 'vy', 'xinit', 'yinit' ] for kern_size in kern_sizes: column_names.append(f'density{kern_size}') column_names.append(f'grad_density_x{kern_size}') column_names.append(f'grad_density_y{kern_size}') column_names.append(f'grad_density_norm{kern_size}') column_names.append(f'area{kern_size}') compress = 100 infos = pd.DataFrame(columns=column_names) for treatment in ['25*', '25', 'baits']: insts = treatments[treatment] for inst in insts: exp = get_exp(inst, path) exp.load_compressed_skel() density_maps = [ get_density_maps(exp, t, compress, kern_sizes) for t in range(exp.ts) ] for index, density_map in enumerate(density_maps): plt.close('all') fig = plt.figure(figsize=(14, 12)) ax = fig.add_subplot(111) im = density_map[kern_size][0] figure = ax.imshow(im >= 0.0005, vmax=0.01) plt.colorbar(figure, orientation='horizontal') save = f'/home/cbisot/pycode/MscThesis/amftrack/notebooks/plotting/Figure/im*{index}.png' plt.savefig(save) img_array = [] for index in range(len(density_maps)): img = cv2.imread( f'/home/cbisot/pycode/MscThesis/amftrack/notebooks/plotting/Figure/im*{index}.png' ) img_array.append(img) imageio.mimsave( f'/home/cbisot/pycode/MscThesis/amftrack/notebooks/plotting/Figure/movie_dense_{kern_size}_{plate_number[inst]}_thresh.gif', img_array, duration=1) for index, density_map in enumerate(density_maps): plt.close('all') fig = plt.figure(figsize=(14, 12)) ax = fig.add_subplot(111) im = density_map[kern_size][0] figure = ax.imshow(im, vmax=0.01) plt.colorbar(figure, orientation='horizontal') save = f'/home/cbisot/pycode/MscThesis/amftrack/notebooks/plotting/Figure/im*{index}.png' plt.savefig(save) img_array = [] for index in range(len(density_maps)): img = cv2.imread( f'/home/cbisot/pycode/MscThesis/amftrack/notebooks/plotting/Figure/im*{index}.png' ) img_array.append(img) imageio.mimsave( f'/home/cbisot/pycode/MscThesis/amftrack/notebooks/plotting/Figure/movie_dense_{kern_size}_{plate_number[inst]}.gif', img_array, duration=1) skeletons = [sparse.csr_matrix(skel) for skel in exp.skeletons] RH, BAS, max_speeds, total_growths, widths_sp, lengths, branch_frequ, select_hyph = get_rh_bas( exp, criter) for hyph in RH: for i, t in enumerate(hyph.ts[:-1]): pos_P, side_P = get_pos_nut(inst, t, 'P') pos_N, side_N = get_pos_nut(inst, t, 'N') tp1 = hyph.ts[i + 1] segs, nodes = get_pixel_growth_and_new_children( hyph, t, tp1) speed = np.sum([get_length_um(seg) for seg in segs]) / get_time(exp, t, tp1) total_growth = speed * get_time(exp, t, tp1) curve = [pixel for seg in segs for pixel in seg] pos = hyph.end.pos(t) pos_tp1 = hyph.end.pos(tp1) v_vector = np.array(pos_tp1) - np.array(pos) vx = v_vector[0] vy = v_vector[1] dist_P = np.linalg.norm(np.array(pos) - np.array(pos_P) ) * pixel_conversion_factor dist_N = np.linalg.norm(np.array(pos) - np.array(pos_N) ) * pixel_conversion_factor x, y = pos[0], pos[1] straight_distance = np.linalg.norm( hyph.end.pos(t) - hyph.end.pos(tp1)) skeleton = skeletons[t][x - window:x + window, y - window:y + window] density = skeleton.count_nonzero() / (window * 1.725) curve_array = np.array(curve) res = 50 index = min(res, curve_array.shape[0] - 1) vec1 = curve_array[index] - curve_array[0] vec2 = curve_array[-1] - curve_array[-index - 1] if np.linalg.norm(vec1) > 0 and np.linalg.norm(vec2) > 0: angle = get_angle(vec1, vec2) vec_growth = v_vector vec_to_P = np.array(pos_P) - np.array(pos) angle_to_P = get_angle(vec_growth, vec_to_P) vec_to_N = np.array(pos_N) - np.array(pos) angle_to_N = get_angle(vec_growth, vec_to_N) inv_tortuosity = (straight_distance * 1.725) / total_growth if hyph.end.degree( tp1 ) < 3 and inv_tortuosity > 0.8 and inv_tortuosity < 1.1: if np.isnan((angle / total_growth)): print(angle, total_growth, dot_product) new_line_dic = { "plate": [plate_number[inst]], "inst": [inst], "treatment": [treatment], "angle": [angle], "curvature": [angle / total_growth], "density": [density], "growth": [total_growth], "speed": [speed], "straightness": [inv_tortuosity], "t": [get_time(exp, 0, t)], "hyph": [hyph.end.label], "dist_P": [dist_P], "dist_N": [dist_N], "angle_to_P": [angle_to_P], "angle_to_N": [angle_to_N], "x": [x], "y": [y], "vx": [vx], "vy": [vy], "xinit": [vec1[0]], "yinit": [vec1[1]] } for kern_size in kern_sizes: pos_comp = x // compress, y // compress new_line_dic[ f'density{kern_size}'] = density_maps[t][ kern_size][0][pos_comp] new_line_dic[ f'grad_density_x{kern_size}'] = density_maps[ t][kern_size][1][pos_comp] new_line_dic[ f'grad_density_y{kern_size}'] = density_maps[ t][kern_size][2][pos_comp] new_line_dic[ f'grad_density_norm{kern_size}'] = density_maps[ t][kern_size][3][pos_comp] new_line_dic[f'area{kern_size}'] = np.sum( density_maps[t][kern_size][0] >= 0.0005 ) * compress**2 new_line = pd.DataFrame( new_line_dic) # index 0 for # mothers need to be modified to resolve multi mother issue infos = infos.append(new_line, ignore_index=True) return (infos)
def main(): path = os.path.join(os.getcwd(), "GIFs") if not os.path.exists(path): os.mkdir(path) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print("Device:", device) print() positions = get_all_positions() agent = DQNAgent(device) add_movies(agent) env = retro.make(game="DonkeyKong-Nes") agent.set(env) render = True stack_size = 1 total_rewards = [] for episode in range(1_000): images = [] best_x = positions["start"][0]['x'] best_y = positions["start"][0]['y'] start_time = time.time() print("Episode:", episode + 1) env.reset() steps = 0 done = False prev = None actions = [] rewards = [] stacked_frames = [] while not done: if keyboard.is_pressed("f12"): while keyboard.is_pressed("f12"): pass render = ~render if render: env.render() if steps >= stack_size: offset = steps - stack_size action = agent.act(stacked_frames[offset], episode) else: action = get_action(random.randint(0, 3)) current_frame, reward, done, info = env.step(action) reward += compute_added_reward(prev, info, current_frame) images += [env.render(mode="rgb_array")] if info["status"] != 4 and 0 < info['y'] <= best_y: best_x = info['x'] best_y = info['y'] current_frame = downscale(current_frame, info['x'], info['y']) stacked_frames.append(current_frame) info["reward"] = reward info["action"] = action actions.append(action) rewards.append(reward) for i in range(1, stack_size): if steps - i >= 0: stacked_frames[steps - i] = (np.hstack( (stacked_frames[steps - i], current_frame))) if steps > stack_size: offset = steps - (stack_size + 1) average = np.array(rewards[-offset:]).mean() agent.memorize(stacked_frames[offset], actions[offset], average, stacked_frames[offset + 1], done) prev = info if steps % 10 == 0: agent.train() steps += 1 total_rewards += [np.array(rewards).sum()] print("Reward: %.3f" % np.array(rewards).sum()) print("Steps:", steps) print("Duration: %.3f" % (time.time() - start_time)) print() if best_y <= 205: imageio.mimsave(os.path.join( path, str(best_y) + ' ' + str(best_x) + ' ' + str(int(rewards[-1])) + ' ' + datetime.now().strftime("%Y-%m-%d %H-%M-%S") + ".gif"), [np.array(image) for image in images], fps=30)
border = pi x = linspace(-border, border) rank = 60 if path.exists(getcwd() + '\\fourier\\'): rmtree(getcwd() + '\\fourier\\') mkdir(getcwd() + '\\fourier\\') for r in range(1, rank + 1): a = fourier(r, x) figure() plot(x, a, 'r-') grid() title('k = ' + str(r)) savefig(getcwd() + '\\fourier\\' + str(r) + ".png") filenames = list() for i in range(1, rank + 1): filenames.append(str(i) + '.png') images = list() for filename in filenames: images.append(imread(getcwd() + '\\fourier\\' + str(filename))) mimsave(getcwd() + '\\fourier\\animation.gif', images, duration=0.1) call(getcwd() + '\\fourier\\animation.gif', shell=True)
def visualize_deformation(cfg, img_tensor, deform_pyramid, reg_boxes, default_boxes, pred_boxes, ground_truth, img_idx): """ :param cfg: :param img_tensor: :param deform_pyramid: :param reg_boxes: input is in point form :param default_boxes: input is in center form :param pred_boxes: :param ground_truth: :param img_idx: :return: """ path = os.path.expanduser("~/Pictures/deform_vis_%s" % args.name) img_path = os.path.join(path, "img_%d" % img_idx) # reg_boxes to pyramid format: start_idx = 0 reg_boxes_pyramids = [] prior_pyramids = [] for i, fm_size in enumerate(cfg["feature_maps"]): end_idx = start_idx + fm_size**2 * (2 * len(cfg["aspect_ratios"][i]) + 2) reg_boxes_pyramids.append(reg_boxes[:, start_idx:end_idx, :]) prior_pyramids.append(default_boxes[start_idx:end_idx, :]) start_idx = end_idx if not os.path.exists(path): os.mkdir(path) height = img_tensor.size(2) width = img_tensor.size(3) fm_size = cfg['feature_maps'][:len(deform_pyramid)] # get deformation maps at different scale for i, deform_maps in enumerate(deform_pyramid): if i == 0: # feature are too small continue if deform_maps is None: continue # get deformation maps for different ratio per_ratio = [] #start_idx = 0 fm_reg_boxes = reg_boxes_pyramids[i] fm_priors = prior_pyramids[i] for _d, deform in enumerate(deform_maps): scale = height / deform.size(2) # Get corresponding prior boxes and regressed box idx = torch.tensor([ _d + len(deform_maps) * i for i in range(deform.size(2)**2) ]).long() priors = fm_priors[idx, :] boxes = fm_reg_boxes[:, idx, :] if args.rematch > 0: overlaps = jaccard( torch.tensor(ground_truth[:, :-1]).float() / args.img_size, boxes.squeeze(0).data) else: overlaps = jaccard( torch.tensor(ground_truth[:, :-1]).float() / args.img_size, point_form(priors)) try: matched_idx = [ int(_idx) for _idx in torch.sum(overlaps >= 0.5, dim=0).nonzero().squeeze() ] except TypeError: # Means no priors match the ground truth matched_idx = [] d_x = torch.mean(deform[:, 0::2, :, :], dim=1).unsqueeze(1) d_y = torch.mean(deform[:, 1::2, :, :], dim=1).unsqueeze(1) deform = torch.cat([d_x, d_y], dim=1) per_batch = [] for j in range(deform.size(0)): box = boxes[j] img = img_tensor[j].permute(1, 2, 0).data.cpu().numpy() # Convert to numpy and RGB form img = ((img - np.min(img)) / (np.max(img) - np.min(img)) * 255).astype("uint8")[:, :, (2, 1, 0)].copy() dm = deform[j].view(2, -1).data.permute(1, 0).cpu().numpy() idx_x = [ int(round(num)) for num in np.linspace(width / fm_size[i], width, fm_size[i] + 1) ][:-1] idx_y = [ int(round(num)) for num in np.linspace(height / fm_size[i], height, fm_size[i] + 1) ][:-1] assert dm.shape[0] == len(idx_x) * len(idx_y) x_coords = idx_x * len(idx_y) y_coords = [val for val in idx_y for _ in idx_x] # Draw prediction on image for pred in pred_boxes: x1, y1, x2, y2 = norm(pred[0]), norm(pred[1]), norm( pred[2]), norm(pred[3]) cv2.rectangle(img, (x1, y1), (x2, y2), (94, 218, 250), 2) # Draw Ground Truth on image for gt in ground_truth: x1, y1, x2, y2 = norm(gt[0]), norm(gt[1]), norm( gt[2]), norm(gt[3]) cv2.rectangle(img, (x1, y1), (x2, y2), (33, 235, 63), 2) gifs = [] for k, x in enumerate(x_coords): cv2.line(img, (x, y_coords[k]), (int(round(x + dm[k, 1] * scale)), int(round(y_coords[k] + dm[k, 0] * scale))), (0, 0, 255), 1) _img = img.copy() # Draw Priors if k in matched_idx: # purple color = (238, 23, 179) line_width = 2 else: # blue color = (255, 105, 65) line_width = 1 x1, y1, x2, y2 = coord_to_rect(priors[k], args.img_size, args.img_size) cv2.rectangle(_img, (x1, y1), (x2, y2), color, line_width) # Draw Regressed Boxes x1, y1, x2, y2 = (box[k].clamp(min=0, max=1) * args.img_size).long().cpu().numpy() cv2.rectangle(_img, (x1, y1), (x2, y2), (70, 67, 238), 1) if args.visualize_gif: gifs.append(_img[:, :, [2, 1, 0]]) per_batch.append(img) #for _idx, img in enumerate(gifs): #cv2.imwrite("/home/wang/Pictures/tmp_%s.jpg"%str(_idx).zfill(4), img) if not os.path.exists(img_path): os.mkdir(img_path) if args.visualize_gif: imageio.mimsave( os.path.join( img_path, 'fm_%s_asr_%s.gif' % (cfg["feature_maps"][i], _d)), gifs) per_ratio.append(per_batch) per_ratio = list(map(list, zip(*per_ratio))) for batch_id, ratio in enumerate(per_ratio): odd_ratio = np.concatenate( [r for i, r in enumerate(ratio) if i % 2 == 1], axis=1) even_ratio = np.concatenate( [r for i, r in enumerate(ratio) if i % 2 == 0], axis=1) img = np.concatenate([odd_ratio, even_ratio], axis=0) name = "iter_%s_fm_%s.jpg" % (str( args.iter).zfill(5), str(fm_size[i]).zfill(2)) cv2.imwrite(os.path.join(img_path, name), img)
def create_gif(filename, gifframes, dur=0.05): import imageio imageio.mimsave(filename, gifframes, 'GIF', duration=dur) return
def get_pixel_error(restore, linear=False, path='', real_mpe=False, checkpoint='checkpoint'): """Restore a model and calculate error from reconstructions.""" # do not write any new runs extras = { 'nolog': True, 'checkpoint_path': os.path.join(restore, checkpoint) } self = restore_model(restore=restore, extras=extras) # ignore supairvised runs for now if self.c.supairvised is True: return None # make sure all runs access the same data! print(self.c.testdata) step = self.c.frame_step visible = self.c.num_visible batch_size = self.c.batch_size skip = self.c.skip # make sure this is the same print(step, visible, batch_size, skip) long_rollout_length = self.c.num_frames // step - visible lrl = long_rollout_length total_images = self.test_dataset.total_img total_labels = self.test_dataset.total_data # apply step and batch size once total_images = total_images[:batch_size, ::step] total_labels = total_labels[:batch_size, ::step] # true data to compare against true_images = total_images[:, skip:(visible + long_rollout_length)] true_images = torch.tensor(true_images).to(self.c.device).type( self.c.dtype) # First obtain reconstruction of input. stove_input = total_images[:, :visible] stove_input = torch.tensor(stove_input).to(self.c.device).type( self.c.dtype) _, prop_dict2, _ = self.stove(stove_input, self.c.plot_every) z_recon = prop_dict2['z'] # Use last state to do rollout if not linear: z_pred, _ = self.stove.rollout(z_recon[:, -1], long_rollout_length) else: # propagate last speed v = z_recon[:, -1, :, 4:6].unsqueeze(1) v = v.repeat(1, long_rollout_length, 1, 1) t = torch.arange(1, long_rollout_length + 1) t = t.repeat(v.shape[0], *v.shape[2:], 1).permute(0, 3, 1, 2).double() dx = v * t new_x = z_recon[:, -1, :, 2:4].unsqueeze(1) new_x = new_x.repeat(1, long_rollout_length, 1, 1) + dx z_pred = torch.cat([ z_recon[:, -1, :, :2].unsqueeze(1).repeat(1, lrl, 1, 1), new_x, v, z_recon[:, -1, :, 6:].unsqueeze(1).repeat(1, lrl, 1, 1) ], -1) z_seq = torch.cat([z_recon, z_pred], 1) # sigmoid positions to make errors comparable if linear: print('clamp positions to 0.9') frame_lim = 0.8 if self.c.coord_lim == 10 else 0.9 z_seq = torch.cat([ z_seq[..., :2], torch.clamp(z_seq[..., 2:4], -frame_lim, frame_lim), z_seq[..., 6:] ], -1) # Simple Reconstruction of Sequences # stove_input = total_images[:10] # stove_input = torch.tensor(stove_input).to(self.c.device).type(self.c.dtype) # elbo, prop_dict2, _ = self.stove(stove_input, self.c.plot_every) # z_recon = prop_dict2['z'] # if self.c.debug_bw: # img = stove_input.sum(2) # img = torch.clamp(img, 0, 1) # img = torch.unsqueeze(img, 2) # model_images = self.stove.reconstruct_from_z( # z_recon, img[:, skip:], max_activation=False, single_image=False) # use mpe to get reconstructed images if real_mpe: if self.c.debug_bw: img = stove_input[:, skip].sum(1) img = torch.clamp(img, 0, 1) img = torch.unsqueeze(img, 1) model_images = self.stove.reconstruct_from_z(z_seq, img, max_activation=False, single_image=True) else: model_images = self.stove.reconstruct_from_z(z_seq) if self.c.debug_bw: true_images = bw_transform(true_images) model_images = torch.clamp(model_images, 0, 1) mse = torch.mean(((true_images - model_images)**2), dim=(0, 2, 3, 4)) plot_sample = model_images[:10, :, 0].detach().cpu().numpy() plot_sample = (255 * plot_sample.reshape(-1, self.c.height, self.c.width)) plot_sample = plot_sample.astype(np.uint8) filename = 'linear_' if linear else '' filename += 'pixel_error_sample.gif' filepath = os.path.join(path, filename) print('Saving gif to ', filepath) imageio.mimsave(filepath, plot_sample, fps=24) # also log state differences # bug_potential... for some reason self.c.coord_lim is 30 but max # true_states is 10 for gravity true_states = total_labels[:, skip:(visible + long_rollout_length)] print(true_states.max(), ' is coord max.') true_states = torch.tensor(true_states).to(self.c.device).type( self.c.dtype) permutations = list(itertools.permutations(range(0, self.c.num_obj))) errors = [] for perm in permutations: error = ((true_states[:, :5, :, :2] - z_seq[:, :5, perm, 2:4])**2).sum(-1) error = torch.sqrt(error).mean((1, 2)) errors += [error] errors = torch.stack(errors, 1) _, idx = errors.min(1) selector = list(zip(range(idx.shape[0]), idx.cpu().tolist())) pos_matched = [z_seq[i, :, permutations[j]] for i, j in selector] pos_matched = torch.stack(pos_matched, 0) mse_states = torch.sqrt( ((true_states[..., :2] - pos_matched[..., 2:4])**2).sum(-1)).mean( (0, 2)) return mse, mse_states
import os import imageio o_image_directory = './output/llama_and_starry_night/' images = [] counter = 0 for filename in os.listdir(o_image_directory): if os.path.splitext(filename)[1] == '.jpg' and counter < 13: images.append(imageio.imread(o_image_directory + filename)) counter += 1 imageio.mimsave(o_image_directory + 'collected_images.gif', images, duration=0.3)
def render(self): """Visualize the env.""" envs = self.envs all_frames = [] for episode in range(self.all_args.render_episodes): obs = envs.reset() if self.all_args.save_gifs: image = envs.render('rgb_array')[0][0] all_frames.append(image) else: envs.render('human') rnn_states = np.zeros((self.n_rollout_threads, self.num_agents, self.recurrent_N, self.hidden_size), dtype=np.float32) masks = np.ones((self.n_rollout_threads, self.num_agents, 1), dtype=np.float32) episode_rewards = [] for step in range(self.episode_length): calc_start = time.time() self.trainer.prep_rollout() action, rnn_states = self.trainer.policy.act( np.concatenate(obs), np.concatenate(rnn_states), np.concatenate(masks), deterministic=True) actions = np.array( np.split(_t2n(action), self.n_rollout_threads)) rnn_states = np.array( np.split(_t2n(rnn_states), self.n_rollout_threads)) if envs.action_space[0].__class__.__name__ == 'MultiDiscrete': for i in range(envs.action_space[0].shape): uc_actions_env = np.eye(envs.action_space[0].high[i] + 1)[actions[:, :, i]] if i == 0: actions_env = uc_actions_env else: actions_env = np.concatenate( (actions_env, uc_actions_env), axis=2) elif envs.action_space[0].__class__.__name__ == 'Discrete': actions_env = np.squeeze( np.eye(envs.action_space[0].n)[actions], 2) else: raise NotImplementedError # Obser reward and next obs obs, rewards, dones, infos = envs.step(actions_env) episode_rewards.append(rewards) rnn_states[dones == True] = np.zeros( ((dones == True).sum(), self.recurrent_N, self.hidden_size), dtype=np.float32) masks = np.ones((self.n_rollout_threads, self.num_agents, 1), dtype=np.float32) masks[dones == True] = np.zeros(((dones == True).sum(), 1), dtype=np.float32) if self.all_args.save_gifs: image = envs.render('rgb_array')[0][0] all_frames.append(image) calc_end = time.time() elapsed = calc_end - calc_start if elapsed < self.all_args.ifi: time.sleep(self.all_args.ifi - elapsed) else: envs.render('human') print("average episode rewards is: " + str(np.mean(np.sum(np.array(episode_rewards), axis=0)))) if self.all_args.save_gifs: imageio.mimsave(str(self.gif_dir) + '/render.gif', all_frames, duration=self.all_args.ifi)
def train(): logger.info('Start training...') fig, ax = plt.subplots(n_class, N) ln, = plt.plot([], [], animated=True) # begin to train images = [] generator.train() discriminator.train() for epoch in range(args.epoch): loss = torch.zeros((2)) turn_g = 1 turn_d = 1 t = 0 for id, (x, label) in enumerate(train_loader): if args.method == 'DCGAN': b_x = torch.Tensor(x).float().cuda() else: b_x = torch.Tensor(x.view(-1, W * H)).float().cuda() B = b_x.shape[0] z = torch.randn(B, latent_dim).view(B, latent_dim, 1, 1).cuda() fake = generator(z) fake_score = discriminator(fake) g_loss = criteria(fake_score, torch.ones_like(fake_score)) optimizer_g.zero_grad() g_loss.backward() optimizer_g.step() loss[0] += g_loss.data * turn_g fake = generator(z) fake_score = discriminator(fake) real_score = discriminator(b_x) d_fake_loss = criteria(fake_score, torch.zeros_like(fake_score)) d_real_loss = criteria(real_score, torch.ones_like(fake_score)) d_loss = d_fake_loss + d_real_loss optimizer_d.zero_grad() d_loss.backward() optimizer_d.step() loss[1] += d_loss.data t += 1 logger.info('Epoch %d : G Loss %.4f D Loss %.4f' % (epoch, loss[0] / t, loss[1] / t)) print('Epoch: ', epoch, "| G Loss %.4f D Loss %.4f" % (loss[0] / t, loss[1] / t)) if epoch % 10 == 0: for i in range(n_class): z = torch.randn(B, latent_dim).view(B, latent_dim, 1, 1).cuda() img = generator(z) for j in range(N): ax[i][j].clear() ax[i][j].imshow(np.reshape(img.cpu().data.numpy()[j], (W, H)), cmap='gray') ax[i][j].set_xticks(()) ax[i][j].set_yticks(()) # plt.show() plt.savefig("results/imgs/{}/{}.png".format(args.method, epoch)) images.append( imageio.imread("results/imgs/{}/{}.png".format( args.method, epoch))) imageio.mimsave("results/imgs/{}/{}.gif".format(args.method, args.method), images, duration=0.5)
print "Number of locs perturbed: ", pert_loc_count print "Percent locs perturbed: ", pert_loc_count / float( WIDTH * HEIGHT * 11) #print_new_and_old(np.copy(Img_arr), np.copy(tmp_img_arr), np.squeeze(np.copy(garbage_stack)), np.squeeze(np.copy(tmp_of))) num_succeed += 1 #print_image_array(np.copy(tmp_img_arr)) # Save the img array as a gif import imageio images = [] for pimg in tmp_img_arr: images.append(pimg[:, :, (2, 1, 0)]) imageio.mimsave( '~/shared_host/adversarial_results/adversarial_video.gif', images) exit() adv = 1 break # We have evaluated moving the box across all of the images. Before we jump back up to start a new # perturbation sequence, we will pick the image array from this run that had the absolute best # confidence score to use as the representative image array for this run. This is important because # at the start of next run we check if the best run from the previous iteration is better than the # global best run. # Find the index of the lowest conficence score for the run best_ind, lowest_pert_conf = min(enumerate(saved_confidence_scores), key=operator.itemgetter(1))
StpX = 1 # step size along the x axis ax.grid(b=False) # disable the grid # create the quiver with custom scaling, this is not adaptive # becauses the highpass filtered velocity field is purely for # visualization purposes ax.quiver(x[(x.shape[0]+1)%2::StpY,::StpX], y[(x.shape[0]+1)%2::StpY,::StpX],\ u_hp[(x.shape[0]+1)%2::StpY,::StpX], v_hp[(x.shape[0]+1)%2::StpY,::StpX],\ scale = 300, color = 'k', scale_units = 'height', units = 'width', width = 0.005) Name_Out = Fol_Gif_HP + os.sep + 'test%06d.png' % Image_Idx # set the output name fig.savefig(Name_Out, dpi=Dpi) # save the figure plt.close(fig) # close it in the plot window IMAGES_HP.append(imageio.imread(Name_Out)) # append into list # render the gifs if desired if PLOT_ROI == True: imageio.mimsave(GIF_ROI, IMAGES_ROI, duration=Duration) if PLOT_PROF == True: imageio.mimsave(GIF_PROF, IMAGES_PROF, duration=Duration) if PLOT_H == True: imageio.mimsave(GIF_H, IMAGES_H, duration=Duration) if PLOT_FLUX == True: imageio.mimsave(GIF_FLUX, IMAGES_FLUX, duration=Duration) if PLOT_S2N == True: imageio.mimsave(GIF_S2N, IMAGES_S2N, duration=Duration) if PLOT_QUIV == True: imageio.mimsave(GIF_QUIV, IMAGES_QUIV, duration=Duration) if PLOT_HP == True: imageio.mimsave(GIF_HP, IMAGES_HP, duration=Duration) ppf.save_run_params(Fol_Out + 'Params_' + Run + '.txt', Run, v_avg_max, t[-1], Frame0, NX, 1 / Dt, Scale, Height, Width,
import imageio import glob images = [] filenames = glob.glob("images/*.png") for filename in filenames: images.append(imageio.imread(filename)) imageio.mimsave('lsgan.gif', images)
G_losses.append(G_train_loss.data[0]) print('[%d/%d]: loss_d: %.3f, loss_g: %.3f' % ((epoch + 1), train_epoch, torch.mean(torch.FloatTensor(D_losses)), torch.mean(torch.FloatTensor(G_losses)))) p = 'MNIST_GAN_results/Random_results/MNIST_GAN_' + str(epoch + 1) + '.png' fixed_p = 'MNIST_GAN_results/Fixed_results/MNIST_GAN_' + str(epoch + 1) + '.png' show_result((epoch + 1), save=True, path=p, isFix=False) show_result((epoch + 1), save=True, path=fixed_p, isFix=True) train_hist['D_losses'].append(torch.mean(torch.FloatTensor(D_losses))) train_hist['G_losses'].append(torch.mean(torch.FloatTensor(G_losses))) print("Training finish!... save training results") torch.save(G.state_dict(), "MNIST_GAN_results/generator_param.pkl") torch.save(D.state_dict(), "MNIST_GAN_results/discriminator_param.pkl") with open('MNIST_GAN_results/train_hist.pkl', 'wb') as f: pickle.dump(train_hist, f) show_train_hist(train_hist, save=True, path='MNIST_GAN_results/MNIST_GAN_train_hist.png') images = [] for e in range(train_epoch): img_name = 'MNIST_GAN_results/Fixed_results/MNIST_GAN_' + str(e + 1) + '.png' images.append(imageio.imread(img_name)) imageio.mimsave('MNIST_GAN_results/generation_animation.gif', images, fps=5)
plt.gcf().tight_layout(pad=0) parser = argparse.ArgumentParser(description='Python Graph Slam') parser.add_argument('--input', type=str, help='Input CLF File.', required=True) parser.add_argument('--draw_last', default=float('inf'), type=int, help='Number of point clouds to draw.') parser.add_argument('--save_gif', dest='save_gif', action='store_true') parser.set_defaults(save_gif=False) args = parser.parse_args() if args.save_gif: import atexit images = [] atexit.register(lambda: imageio.mimsave( f'./slam_{int(time.time())}.gif', images, fps=10)) # Read Data with open(args.input, 'r') as f: lasers = [] odoms = [] for line in f: tokens = line.split(' ') if tokens[0] == 'FLASER': num_readings = int(tokens[1]) scans = np.array(tokens[2:2 + num_readings], dtype=np.float) scan_time = float(tokens[2 + num_readings + 6]) index = np.arange(-90, 90 + 180 / num_readings, 180 / num_readings) index = np.delete(index, num_readings // 2) converted_scans = [] angles = np.radians(index)
driving_forward = driving_video[i:] driving_backward = driving_video[:(i + 1)][::-1] predictions_forward = make_animation( source_image, driving_forward, generator, kp_detector, relative=opt.relative, adapt_movement_scale=opt.adapt_scale, cpu=opt.cpu) predictions_backward = make_animation( source_image, driving_backward, generator, kp_detector, relative=opt.relative, adapt_movement_scale=opt.adapt_scale, cpu=opt.cpu) predictions = predictions_backward[::-1] + predictions_forward[1:] else: predictions = make_animation(source_image, driving_video, generator, kp_detector, relative=opt.relative, adapt_movement_scale=opt.adapt_scale, cpu=opt.cpu) imageio.mimsave(opt.result_video, [img_as_ubyte(frame) for frame in predictions], fps=fps)
def run(self): mean = np.array([0.485, 0.456, 0.406]) std = np.array([0.229, 0.224, 0.225]) mean = np.reshape(mean, (1, 1, 3)) std = np.reshape(std, (1, 1, 3)) self.start_pos = [-0.1, -0.4, 0.5] self.dt = 1. / 30.0 if self.wid == 0: self.p_id = pybullet.connect(pybullet.GUI) else: self.p_id = pybullet.connect(pybullet.DIRECT) self.env = RobotEnv(worker_id=self.wid, p_id=pybullet, dt=self.dt, maxSteps=20) total_step = 1 + self.init_step suc_check = 0 reward_check = 0 episode_check = 0 sigma_check1 = 0 sigma_check2 = 0 total_episode = 0 buffer_s, buffer_a, buffer_r, buffer_done = [], [], [], [] while self.g_ep.value < MAX_EP: observation = self.env.reset() observation = observation / 255.0 observation = (observation - mean) / std observation = np.reshape(observation, (-1, )) while True: action, mu_r, sigma_r = self.lnet.choose_action( v_wrap(observation[None, :])) action = action.clip(-0.03, 0.03) if action[2] > 0.005: action[2] = 0.005 observation_next, reward, done, suc = self.env.step( np.array([action[0], action[1], action[2]])) observation_next = observation_next / 255.0 observation_next = (observation_next - mean) / std recordGif = False if recordGif and total_step > 10: imageio.mimsave('pokingSthSlightly.gif', self.env.obs_list) return observation_next = np.reshape(observation_next, (-1, )) buffer_s.append(observation) buffer_r.append(reward) buffer_a.append(action) buffer_done.append(done) if total_step % ( UPDATE_GLOBAL_ITER + self.wid ) == 0 or done: # or self.g_ep.value % (10 * UPDATE_GLOBAL_ITER): #sync push_and_pull(self.opt, self.lnet, self.gnet, done, observation_next, buffer_s, buffer_a, buffer_r, buffer_done, GAMMA) buffer_s, buffer_a, buffer_r, buffer_done = [], [], [], [] if done: suc_check += suc episode_check += 1 total_episode += 1 observation = observation_next total_step += 1 reward_check += reward if total_step % 100 == 0: current_performance = float(suc_check) / episode_check avg_sigma1 = sigma_check1 / 100.0 avg_sigma2 = sigma_check2 / 100.0 if self.wid == 0: print("total step %d, avg suc %f, avg reward %f" % (total_step, suc_check / 100.0, reward_check / 100.0)) save_path = os.path.join(self.SAVE_TOP_DIR, str(total_step) + 'model.pth.tar') if self.wid == 0 and total_step % 100 == 0: torch.save(self.gnet.state_dict(), save_path) suc_check = 0 reward_check = 0 episode_check = 0 sigma_check1 = 0.0 sigma_check2 = 0.0 if done: break
def render(env, path, image): tf.reset_default_graph() env_id = env def create_env(): env = make_env.make_env(env_id) env.seed(5) # env = bench.Monitor(env, '/tmp/', allow_early_resets=True) set_global_seeds(5) return env env = create_env() # path = '/Users/LantaoYu/PycharmProjects/multiagent-irl/models/mack-simple_tag-checkpoint20000' # if env_id == 'simple_spread': # and traj_limitation > 200: # path = '/atlas/u/hyren/exps/mack/simple_spread/l-0.1-b-1000/seed-1/checkpoint17500' # if env_id == 'simple_speaker_listener': # and traj_limitation > 200: # path = '/atlas/u/hyren/exps/mack/simple_speaker_listener/l-0.1-b-1000/seed-1/checkpoint06600' # if env_id == 'simple_tag': # and traj_limitation > 200: # path = '/atlas/u/tsong/exps/mack/simple_tag/l-0.1-b-1000/seed-1/checkpoint20000' # if env_id == 'simple_push': # path = '/atlas/u/hyren/exps/mack/simple_push/l-0.1-b-1000/seed-1/checkpoint17500' n_agents = len(env.action_space) ob_space = env.observation_space ac_space = env.action_space print('observation space') print(ob_space) print('action space') print(ac_space) n_actions = [action.n for action in ac_space] make_model = lambda: Model(CategoricalPolicy, ob_space, ac_space, 1, total_timesteps=1e7, nprocs=2, nsteps=500, nstack=1, ent_coef=0.01, vf_coef=0.5, vf_fisher_coef=1.0, lr=0.01, max_grad_norm=0.5, kfac_clip=0.001, lrschedule='linear', identical=make_env.get_identical(env_id), use_kfac=False) model = make_model() print("load model from", path) model.load(path) images = [] sample_trajs = [] num_trajs = 1000 max_steps = 50 avg_ret = [[] for _ in range(n_agents)] for i in range(num_trajs): all_ob, all_agent_ob, all_ac, all_rew, ep_ret = [], [], [], [], [ 0 for k in range(n_agents) ] for k in range(n_agents): all_ob.append([]) all_ac.append([]) all_rew.append([]) obs = env.reset() obs = [ob[None, :] for ob in obs] action = [np.zeros([1]) for _ in range(n_agents)] step = 0 done = False while not done: action, _, _ = model.step(obs, action) actions_list = [ onehot(action[k][0], n_actions[k]) for k in range(n_agents) ] for k in range(n_agents): all_ob[k].append(obs[k]) all_ac[k].append(actions_list[k]) all_agent_ob.append(np.concatenate(obs, axis=1)) obs, rew, done, _ = env.step(actions_list) for k in range(n_agents): all_rew[k].append(rew[k]) ep_ret[k] += rew[k] obs = [ob[None, :] for ob in obs] step += 1 if image: img = env.render(mode='rgb_array') images.append(img[0]) time.sleep(0.02) if step == max_steps or True in done: done = True step = 0 else: done = False for k in range(n_agents): all_ob[k] = np.squeeze(all_ob[k]) all_agent_ob = np.squeeze(all_agent_ob) traj_data = { "ob": all_ob, "ac": all_ac, "rew": all_rew, "ep_ret": ep_ret, "all_ob": all_agent_ob } sample_trajs.append(traj_data) print('traj_num', i, 'expected_return', ep_ret) for k in range(n_agents): avg_ret[k].append(ep_ret[k]) for k in range(n_agents): print('agent', k, np.mean(avg_ret[k]), np.std(avg_ret[k])) images = np.array(images) pkl.dump(sample_trajs, open(path + '-%dtra.pkl' % num_trajs, 'wb')) if image: print(images.shape) imageio.mimsave(path + '.mp4', images, fps=25)
def get_animation(country_name): country_name = country_name.lower() get_files(country_name) if not os.path.exists(f'plots/{country_name}/cummulative'): os.makedirs(f'plots/{country_name}/cummulative') if not os.path.exists(f'plots/{country_name}/discrete'): os.makedirs(f'plots/{country_name}/discrete') if not os.path.exists(f'results/{country_name}'): os.makedirs(f'results/{country_name}') files_dir = os.listdir(f'predictions/{country_name}') oldest_file = min([pd.to_datetime(x[-14:-4]) for x in files_dir]) newest_file = max([pd.to_datetime(x[-14:-4]) for x in files_dir]) oldest_file= pd.read_csv(f'predictions/{country_name}/{country_name}_{str(oldest_file)[:10]}.csv') newest_file = pd.read_csv(f'predictions/{country_name}/{country_name}_{str(newest_file)[:10]}.csv') start_date = min(pd.to_datetime(oldest_file.date.copy())) end_date = max(pd.to_datetime(newest_file.date.copy())) index = pd.date_range(start = start_date, end = end_date) cummulative = pd.DataFrame() cummulative['date'] = pd.to_datetime(newest_file.date.copy()) cummulative['deaths']= newest_file.total_deaths cummulative = eliminate_negatives(cummulative) discrete = pd.DataFrame() discrete['date'] = pd.to_datetime(newest_file.date.copy()) discrete['deaths']= newest_file.actual_deaths discrete = eliminate_negatives(discrete) for file in files_dir: process_data(index, cummulative, discrete, file, country_name) os.system('cls') print('##########') print(f'{file[-14:-4]} ready...') print('##########') print('Processing final animations...') # repeate last image so the final frame of the video lingers on pic_dates = [x[:-4] for x in os.listdir(f'plots/{country_name}/cummulative')] max_date = str(max(pd.to_datetime(pic_dates)))[:10] for _ in range(50): copy2(f'plots/{country_name}/cummulative/{max_date}.jpg', f'plots/{country_name}/cummulative/{max_date}_{_}.jpg') copy2(f'plots/{country_name}/discrete/{max_date}.jpg', f'plots/{country_name}/discrete/{max_date}_{_}.jpg') cum_images = [] for filename in os.listdir(f'plots/{country_name}/cummulative'): cum_images.append(imageio.imread(os.path.join(f'plots/{country_name}/cummulative',filename))) imageio.mimsave(f'results/{country_name}/cummulative_{country_name}.mp4', cum_images) print('##########') print('Cummulative animation ready...') dis_images = [] for filename in os.listdir(f'plots/{country_name}/discrete'): dis_images.append(imageio.imread(os.path.join(f'plots/{country_name}/discrete',filename))) imageio.mimsave(f'results/{country_name}/discrete_{country_name}.mp4', dis_images) print('##########') print('Discrete animation ready...') print('##########') print(f'Animations ready, con be found on:\nresults/{country_name}')
def save_render(self, renders_array, iteration): """Save GIF of an array of np arrays representing images""" render_images = [Image.fromarray(i) for i in renders_array] imageio.mimsave( f'{self.path}/{self.run_label}/simulation-iter-{iteration}.gif', render_images, 'GIF')
def generate_gif(Gs, Zs, reals, NoiseAmp, opt, alpha=0.1, beta=0.9, start_scale=2, fps=10): in_s = torch.full(Zs[0].shape, 0, device=opt.device) images_cur = [] count = 0 for G, Z_opt, noise_amp, real in zip(Gs, Zs, NoiseAmp, reals): pad_image = int(((opt.ker_size - 1) * opt.num_layer) / 2) nzh = Z_opt.shape[2] nzw = Z_opt.shape[3] #pad_noise = 0 #m_noise = nn.ZeroPad2d(int(pad_noise)) m_image = nn.ZeroPad2d(int(pad_image)) images_prev = images_cur images_cur = [] if count == 0: z_rand = functions.generate_noise([1, nzh, nzw], device=opt.device) z_rand = z_rand.expand(1, 3, Z_opt.shape[2], Z_opt.shape[3]) z_prev1 = 0.95 * Z_opt + 0.05 * z_rand z_prev2 = Z_opt else: z_prev1 = 0.95 * Z_opt + 0.05 * functions.generate_noise( [opt.nc_z, nzh, nzw], device=opt.device) z_prev2 = Z_opt for i in range(0, 100, 1): if count == 0: z_rand = functions.generate_noise([1, nzh, nzw], device=opt.device) z_rand = z_rand.expand(1, 3, Z_opt.shape[2], Z_opt.shape[3]) diff_curr = beta * (z_prev1 - z_prev2) + (1 - beta) * z_rand else: diff_curr = beta * (z_prev1 - z_prev2) + (1 - beta) * ( functions.generate_noise([opt.nc_z, nzh, nzw], device=opt.device)) z_curr = alpha * Z_opt + (1 - alpha) * (z_prev1 + diff_curr) z_prev2 = z_prev1 z_prev1 = z_curr if images_prev == []: I_prev = in_s else: I_prev = images_prev[i] I_prev = imresize(I_prev, 1 / opt.scale_factor, opt) I_prev = I_prev[:, :, 0:real.shape[2], 0:real.shape[3]] #I_prev = functions.upsampling(I_prev,reals[count].shape[2],reals[count].shape[3]) I_prev = m_image(I_prev) if count < start_scale: z_curr = Z_opt z_in = noise_amp * z_curr + I_prev I_curr = G(z_in.detach(), I_prev) if (count == len(Gs) - 1): I_curr = functions.denorm(I_curr).detach() I_curr = I_curr[0, :, :, :].cpu().numpy() I_curr = I_curr.transpose(1, 2, 0) * 255 I_curr = I_curr.astype(np.uint8) images_cur.append(I_curr) count += 1 dir2save = functions.generate_dir2save(opt) try: os.makedirs('%s/start_scale=%d' % (dir2save, start_scale)) except OSError: pass imageio.mimsave('%s/start_scale=%d/alpha=%f_beta=%f.gif' % (dir2save, start_scale, alpha, beta), images_cur, fps=fps) del images_cur
edgecolor='grey', legend=True) mapSN.set_title('% de réussite au bac au Sénégal par région (' + str(year) + ')') leg = mapSN.get_legend() leg.set_title('% des bacheliers') mapSN.set_axis_off() ## Placement des noms des régions sur la carte if getFile('..', 'regionsName.json') == 1: sys.exit() regionsName = gpd.read_file(getFile('..', 'regionsName.json')) for idx, row in regionsName.iterrows(): coordinates = row['geometry'].coords.xy x, y = coordinates[0][0], coordinates[1][0] mapSN.annotate(row['name'], xy=(x, y), xytext=(x, y), family='serif', fontweight='bold', horizontalalignment='center') ## Sauvegarde dans les fichiers .png respectifs fileName = 'BAC_results_' + str(year) + '.png' plt.savefig(fileName, bbox_inches='tight') images.append(imageio.imread(fileName)) # Génération du gif à partir des images stockées dans la variable images kargs = {'duration': 1} # Duréee de défilement des images imageio.mimsave('BAC_results_per_year.gif', images, **kargs)
def write_gif(gif_filename: str, png_filenames: List[str], time: float=2.0, onesided: bool=True, nrepeat: int=0, delete_images: bool=False, make_gif: bool=True) -> bool: """ Makes an animated gif Parameters ---------- gif_filename : str path to the output gif & png folder png_filenames : List[str] the pictures to make the gif from time : float; default=2.0 the runtime of the gif (seconds) onesided : bool; default=True should the animation go up and back down True : the video will use images [0...N] False : the video will use images [0...N...0] nrepeat : int; default=0 0 : loop infinitely 1 : loop 1 time 2 : loop 2 times delete_images : bool; default=False cleanup the png files at the end make_gif : bool; default=True actually make the gif at the end Returns ------- success : bool was the gif made """ if not IS_IMAGEIO: return False #assert fps >= 1, fps nframes = len(png_filenames) assert nframes > 0, png_filenames # duration : float # frame time (seconds) duration = time / nframes gif_dirname = os.path.dirname(os.path.abspath(gif_filename)) if not os.path.exists(gif_dirname): os.makedirs(gif_dirname) #if not onesided: # drop the duplicate middle frame # >>> a = [1, 2, 3, 4, 5] # >>> a + a[-2::-1] # [1, 2, 3, 4, 5, 4, 3, 2, 1] #png_filenames = png_filenames + png_filenames[-2::-1] if make_gif and IS_IMAGEIO: images = [] for png_filename in png_filenames: if not isinstance(png_filename, str) and os.path.exists(png_filename): raise TypeError(f'png_filename={png_filename!r} is invalid') imagei = imageio.imread(png_filename) images.append(imagei) if nrepeat is True: nrepeat = 0 try: imageio.mimsave(gif_filename, images, duration=duration, loop=nrepeat) except IOError: # file is open raise IOError(f'{gif_filename} is likely open') if delete_images: remove_files(png_filenames) return True
sign = np.transpose(w).dot(x) if (sign >= 0): sign = 1 else: sign = 0 res = new[row][2] - sign w = w + learning_rate * res * x fig, ax = plt.subplots() x = np.linspace(-3, 2, 1000) ax.scatter(class0[:, 0], class0[:, 1], label="dots", color="blue", marker=".", s=3) ax.scatter(class1[:, 0], class1[:, 1], label="dots", color="red", marker=".", s=3) y = (-w[0] / w[1]) * x - w[2] / w[1] ax.plot(x, y, label='the line', color="green") fig.canvas.draw() image = np.frombuffer(fig.canvas.tostring_rgb(), dtype='uint8') gif.append(image.reshape(fig.canvas.get_width_height()[::-1] + (3, ))) imageio.mimsave('Outputs/Perceptron/dataset-' + str(dataset_no) + '.gif', gif, fps=2) print("w: {}".format(w))
def create_gif(image_list, gif_name): frames = [] for image_name in image_list: frames.append(imageio.imread(image_name)) imageio.mimsave(gif_name, frames, 'GIF', duration=0.1) return
def interpolate(state_dict, generator, preview = True, interpolate = False, large_sample=False, disp_size=6, large_dim=256, samples=[random.randint(0, BATCH_SIZE - 1), random.randint(0, BATCH_SIZE - 1)], counter = 1): global noise """ Args: state_dict: saved copy of trained params generator: generator model preview: show preview of images in grid form in original size interpolate: create interpolation gif large_sample: create single images disp_size: size of a grid to show images large_dim: dimensions of an individual picture (powers of 2) samples: indices of the samples you want to interpolate """ c_d = 1 position = 4 x_large = large_dim y_large = large_dim x, y, r = chinese.get_coordinates(x_large//2 + 2*x_large//64, y_large//2+ 2*y_large//64, batch_size=1, scale = 8) if use_cuda: x = x.cuda() y = y.cuda() r = r.cuda() generator_int = generator generator_int.load_state_dict(torch.load(state_dict, map_location=lambda storage, loc: storage)) if x_large>64: new_conv = torch.nn.Conv2d(64, 1, (5*x_large//64, 5*y_large//64)) print(generator.conv_seq[1].weight.shape) new_conv.weight = torch.nn.Parameter(torch.nn.functional.upsample(generator.conv_seq[1].weight, size=(5*x_large//64, 5*y_large//64), mode = 'nearest')/((x_large//64)**2)) generator.conv_seq[1] = new_conv print(generator.conv_seq[1].weight.shape) if use_cuda: generator_int.cuda(gpu) print(noise.shape) if preview: noisev = noise[:disp_size**2] ones = torch.ones(disp_size**2, x.shape[1], c_d) seed = torch.bmm(ones, noisev.unsqueeze(1)) if use_cuda: ones = ones.cuda() seed = seed.cuda() gen_imgs = generator_int(x, y, r, seed) gen_imgs = gen_imgs.cpu().data.numpy() lib.save_images.save_images( gen_imgs, 'generated_img/samples_chines{}_disp{}_{}x{}.png'.format(counter, disp_size, x_large, x_large) ) elif large_sample: ones = torch.ones(1, x.shape[1], 1) seed = torch.bmm(ones, noise[samples[0],:].unsqueeze(0).unsqueeze(1)) if use_cuda: ones = ones.cuda() seed = seed.cuda() gen_imgs = generator_int(x, y, r, seed) gen_imgs = gen_imgs.cpu().data.numpy() lib.save_images.save_images( gen_imgs, 'generated_img/large_sample_chines{}_samp{}_{}x{}.png'.format(counter, samples[0], x_large, x_large) ) elif interpolate: nbSteps = 12 initial_noise = torch.randn((disp_size**2)*2,1,LATENT_DIM) to_take=[0,1,6,8,10,11,12,13,14] for i in range(15,15+disp_size**2-9): to_take= to_take +[i] to_take=to_take[:(disp_size**2)] initial_noise = torch.cat([initial_noise[x] for x in to_take]) alphaValues = np.linspace(0, 1, nbSteps)[1:] images = [] if use_cuda: noise = noise.cuda(gpu) initial_noise = initial_noise.cuda() samples.append(samples[0]) for i in range(len(samples) - 1): for alpha in alphaValues: vector = noise[samples[i]]*(1-alpha) + noise[samples[i + 1]]*alpha vector = (vector.unsqueeze(0) + initial_noise)/2 print(x.shape,vector.unsqueeze(1).shape) ones = torch.ones(disp_size**2, x.shape[1], 1) if use_cuda: ones = ones.cuda() print(ones.shape, vector.unsqueeze(1).shape ) seed = torch.bmm(ones, vector.unsqueeze(1)) print(seed.shape) with torch.no_grad(): gen_imgs = generator_int(x, y, r, seed) print(gen_imgs.shape) if c_d == 3: gen_img_np = np.transpose(gen_imgs.data[0].numpy()) elif c_d == 1: gen_img_np = gen_imgs.data.cpu().numpy() gen_img_np = gen_img_np.reshape((disp_size**2, x_large-x_large//64+1, y_large-y_large//64+1,1),order = 'C') print(gen_img_np.shape) gen_img_np = gen_img_np.reshape((disp_size, disp_size,gen_img_np.shape[1],gen_img_np.shape[2]),order='C') #gen_img_np = gen_img_np[0]#[0] print(gen_img_np.shape) gen_img_np = np.transpose(gen_img_np,[0,2,1,3]).reshape((gen_img_np.shape[0],gen_img_np.shape[2], gen_img_np.shape[1]*gen_img_np.shape[3]),order='C') \ .reshape((gen_img_np.shape[0]*gen_img_np.shape[2], gen_img_np.shape[1]*gen_img_np.shape[3]),order='C') print(gen_img_np.shape) images.append(gen_img_np) imageio.mimsave('generated_img/movie_chines{}im{}_{}x{}.gif'.format(counter, disp_size, large_dim, large_dim), images) print('saved')