leo@pop-os ~ [1]> python3 model.py
/home/leo/.local/lib/python3.10/site-packages/fastai/callback/core.py:71: UserWarning: You are shadowing an attribute (generator) that exists in the learner. Use `self.learn.generator` to avoid this
  warn(f"You are shadowing an attribute ({name}) that exists in the learner. Use `self.learn.{name}` to avoid this")
/home/leo/.local/lib/python3.10/site-packages/fastai/callback/core.py:71: UserWarning: You are shadowing an attribute (critic) that exists in the learner. Use `self.learn.critic` to avoid this
  warn(f"You are shadowing an attribute ({name}) that exists in the learner. Use `self.learn.{name}` to avoid this")
/home/leo/.local/lib/python3.10/site-packages/fastai/callback/core.py:71: UserWarning: You are shadowing an attribute (gen_mode) that exists in the learner. Use `self.learn.gen_mode` to avoid this
  warn(f"You are shadowing an attribute ({name}) that exists in the learner. Use `self.learn.{name}` to avoid this")
epoch     train_loss  valid_loss  gen_loss  crit_loss  time    
/home/leo/.local/lib/python3.10/site-packages/fastprogress/fastprogress.py:73: UserWarning: Your generator is empty.
  warn("Your generator is empty.")
Traceback (most recent call last):--------------------------------------------------------------------------------| 0.00% [0/1 00:00<?]
  File "/home/leo/model.py", line 51, in <module>
    learn.fit(10, 1)  # Train for 10 epochs
  File "/home/leo/.local/lib/python3.10/site-packages/fastai/learner.py", line 266, in fit
    self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
  File "/home/leo/.local/lib/python3.10/site-packages/fastai/learner.py", line 201, in _with_events
    try: self(f'before_{event_type}');  f()
  File "/home/leo/.local/lib/python3.10/site-packages/fastai/learner.py", line 255, in _do_fit
    self._with_events(self._do_epoch, 'epoch', CancelEpochException)
  File "/home/leo/.local/lib/python3.10/site-packages/fastai/learner.py", line 201, in _with_events
    try: self(f'before_{event_type}');  f()
  File "/home/leo/.local/lib/python3.10/site-packages/fastai/learner.py", line 250, in _do_epoch
    self._do_epoch_validate()
  File "/home/leo/.local/lib/python3.10/site-packages/fastai/learner.py", line 246, in _do_epoch_validate
    with torch.no_grad(): self._with_events(self.all_batches, 'validate', CancelValidException)
  File "/home/leo/.local/lib/python3.10/site-packages/fastai/learner.py", line 201, in _with_events
    try: self(f'before_{event_type}');  f()
  File "/home/leo/.local/lib/python3.10/site-packages/fastai/learner.py", line 207, in all_batches
    for o in enumerate(self.dl): self.one_batch(*o)
  File "/home/leo/.local/lib/python3.10/site-packages/fastai/learner.py", line 237, in one_batch
    self._with_events(self._do_one_batch, 'batch', CancelBatchException)
  File "/home/leo/.local/lib/python3.10/site-packages/fastai/learner.py", line 201, in _with_events
    try: self(f'before_{event_type}');  f()
  File "/home/leo/.local/lib/python3.10/site-packages/fastai/learner.py", line 221, in _do_one_batch
    self.loss_grad = self.loss_func(self.pred, *self.yb)
  File "/home/leo/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1553, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/leo/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1562, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/leo/.local/lib/python3.10/site-packages/fastai/vision/gan.py", line 29, in forward
    return self.generator(*args) if self.gen_mode else self.critic(*args)
  File "/home/leo/.local/lib/python3.10/site-packages/fastai/vision/gan.py", line 144, in generator
    fake_pred = self.gan_model.critic(output)
  File "/home/leo/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1553, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/leo/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1562, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/leo/.local/lib/python3.10/site-packages/torch/nn/modules/container.py", line 219, in forward
    input = module(input)
  File "/home/leo/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1553, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/leo/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1562, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/leo/.local/lib/python3.10/site-packages/torch/nn/modules/conv.py", line 948, in forward
    return F.conv_transpose2d(
RuntimeError: Given transposed=1, weight of size [100, 128, 4, 4], expected input[8, 1, 13, 13] to have 100 channels, but got 1 channels instead

on

# Import necessary libraries
from fastai.vision.all import *
from fastai.vision.gan import *
import torch

# Set the path to your images directory
path = Path('/home/leo/Documents/images/')  # Change to your dataset path
# Define image size and batch size
img_size = 64  # Size of the images (should be a power of 2)
bs = 128  # Batch size

# Load and transform the data
dls = ImageDataLoaders.from_folder(path, 
                                    valid_pct=0.2, 
                                    item_tfms=Resize(img_size), 
                                    bs=bs)

# Define the GAN architecture
class SimpleGAN(Module):
    def __init__(self):
        super().__init__()
        self.generator = nn.Sequential(
            nn.ConvTranspose2d(100, 128, kernel_size=4, stride=1, padding=0),  # 1
            nn.ReLU(),
            nn.ConvTranspose2d(128, 64, kernel_size=4, stride=2, padding=1),  # 2
            nn.ReLU(),
            nn.ConvTranspose2d(64, 3, kernel_size=4, stride=2, padding=1),  # 3
            nn.Tanh()  # Output layer, use Tanh for normalizing to [-1, 1]
        )
        
        self.discriminator = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=4, stride=2, padding=1),  # 1
            nn.LeakyReLU(0.2),
            nn.Conv2d(64, 128, kernel_size=4, stride=2, padding=1),  # 2
            nn.LeakyReLU(0.2),
            nn.Conv2d(128, 1, kernel_size=4, stride=1, padding=0),  # 3
            nn.Sigmoid()  # Output layer for binary classification
        )

    def forward(self, noise):
        return self.generator(noise)


gan = SimpleGAN()

# Setup the GAN learner
learn = GANLearner.wgan(dls, gan.discriminator, gan.generator, opt_func=Adam, lr=2e-4)


# Train the GAN
learn.fit(10, 1)  # Train for 10 epochs

# Save the trained model
learn.save('gan_model')

# Generate some images
def generate_images(num_images):
    noise = torch.randn(num_images, 100, 1, 1)  # Latent space input
    with torch.no_grad():
        generated_images = learn.model.generator(noise).cpu()
    
    # Save generated images
    for i in range(num_images):
        img = tensor2img(generated_images[i])
        img.save(f'/home/leo/images/generated/image_{i}.png')

# Generate a specific number of images
generate_images(10)