Files
TashaSkyUp-EternalKernelPyt…/modules/noise.py
T
Tasha Upchurch d2d16cadfe Enhanced PyTorch Functionalities and Server Endpoints; Introduced Torchvision and Hugging Face Diffusers Integration
This commit introduces significant enhancements and new features across multiple files in the project. Major changes include:

1. `pytorch_nodes.py`: Expanded PyTorch node functionalities with additional imports, device compatibility checks, optimizer state updates, and training enhancements. Introduced new methods like `optimizer_to` for transferring optimizer state to a specified device, `reset_optimizer_weights` to clear optimizer states, and `make_image_tensor` for converting outputs to image tensors. Also, improved the training process with device compatibility checks, optimizer updates, and the ability to save and load models during training for efficiency.

2. `server_endpoints.py`: Modified server endpoints for improved git integration and user management. Adjustments include dynamic handling of different git repositories, refined branch and user management in GitIO class, and the integration of user-specific git settings in user management functions.

3. `config.yaml`: Added new configurations for temporary directory and git repositories, providing more flexibility and control over file management and version control integration.

4. `torchvision_nodes.py`: Introduced a new file to integrate torchvision functionalities. It includes classes for transform composition, application of torchvision transforms, and handling of torchvision-specific data structures.

5. `hf_diffusers_nodes.py`: Integrated Hugging Face's diffusers library with new nodes for loading and utilizing DDPMPipelines, handling tensor to image conversions, and managing datasets from the Hugging Face ecosystem.

6. `image.py`, `video.py`, `functional.py`: Refactored and enhanced existing functionalities. Included minor tweaks and bug fixes to improve the overall efficiency and robustness of image and video processing capabilities.

7. `requirements.txt`: Updated dependencies, replacing `fastnoisesimd` with `diffusers` to align with the newly integrated Hugging Face diffusers functionalities.

8. `config.py`: Improved the configuration file loading mechanism with dynamic path handling and the ability to replace placeholders with actual paths, enhancing the flexibility of configuration management.

This comprehensive update significantly enhances the project's capabilities in machine learning, especially in PyTorch and torchvision, and improves server endpoint functionalities for better user and version control management.
2023-12-27 13:22:48 -05:00

87 lines
2.8 KiB
Python

import pyfastnoisesimd as fns
def get_perlin_2d(width, height, scale, octaves=6, lacunarity=3, gain=.5, seed=0, offset=0, workers=4) -> any:
"""
Returns a 2D fractal perlin noise array of size (width, height) with the given parameters.
"""
noise = fns.Noise(seed=seed, numWorkers=workers)
noise.frequency = scale
# noise.fractal.fractalType = fns.FractalType.RigidMulti
noise.fractal.octaves = octaves
noise.fractal.lacunarity = lacunarity
noise.fractal.gain = gain
noise.noiseType = fns.NoiseType.PerlinFractal
noise.fractal.offset = offset
return noise.genAsGrid([width, height])
def get_perlin_2d_as_torch_image_stack(width, height, scale, octaves=6, lacunarity=3, gain=.5, seed=0, offset=0,
workers=4, norm_zero_to_1=True) -> any:
"""
just uses get_perlin_2d and converts it to a torch image stack
(B,H,W,C=3) format
"""
import torch
import numpy as np
noise = get_perlin_2d(width, height, scale, octaves, lacunarity, gain, seed, offset, workers)
# noise = np.transpose(noise, (2, 0, 1))
noise = torch.from_numpy(noise)
# noise = noise.unsqueeze(0)
noise = torch.reshape(noise, (1, height, width, 1))
# now stack it 3 times
# noise = torch.normal(noise)
# use torch to put it from 0-1
# noise = torch.sigmoid(noise)
# normalize it using torch
if norm_zero_to_1:
# noise = noise.view(noise.size(0), -1)
noise -= noise.min()
noise /= noise.max()
# noise = noise + 0.5
# noise = noise / 1.0
return noise
if __name__ == '__main__':
import matplotlib.pyplot as plt
import time
for i in range(10):
start = time.time()
# noise = get_perlin_2d(1024, 1024, 0.001, i + 1, 3, .5, 1, 0)
# noise = get_perlin_2d(1024*10, 1024*10, i/5, i + 1, 3, .5, i, 0)
# print(type(noise))
# print(f"Time taken: {time.time() - start}")
# print the min and max
# print(f"Min: {noise.min()} Max: {noise.max()}")
# plt.imshow(noise, cmap='gray')
# plt.show()
start = time.time()
for i in range(10):
# now test the torch version
noise = get_perlin_2d_as_torch_image_stack(1024,
1024,
0.001,
6,
3,
.5,
1,
0,
4,
2)
print(f"Time taken: {time.time() - start}")