Modify the code so that it goes through all subfolders: import torch import torch.nn as…
- User
Modify the code so that it goes through all subfolders: import torch import torch.nn as nn import torch.optim as optim from torchvision import transforms from PIL import Image import numpy as np import os
Define a simple CNN model using PyTorch
class SimpleCNN(nn.Module): def init(self): super(SimpleCNN, self).init() self.conv1 = nn.Conv2d(1, 32, kernel_size=3, padding=1) self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1) self.conv3 = nn.Conv2d(64, 1, kernel_size=3, padding=1)
def forward(self, x): x = torch.relu(self.conv1(x)) x = torch.relu(self.conv2(x)) x = torch.sigmoid(self.conv3(x)) return xdef load_images_from_directory(directory, transform, image_size=(64, 64)): images = [] for filename in sorted(os.listdir(directory)): if filename.endswith('.png') or filename.endswith('.jpg'): with Image.open(os.path.join(directory, filename)) as img: img = img.convert('L') # Convert to grayscale img = img.resize(image_size) images.append(transform(img)) return torch.stack(images)
Image transformation and normalization
transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,)) ])
Load training data
input_train_dir = "D:\Bilder\Python\1. Input" output_train_dir = "D:\Bilder\Python\2. Output"
input_train_images = load_images_from_directory(input_train_dir, transform) output_train_images = load_images_from_directory(output_train_dir, transform)
Load testing data
input_test_dir = "D:\Bilder\Python\Test\Input" output_test_dir = "D:\Bilder\Python\Test\Output"
input_test_images = load_images_from_directory(input_test_dir, transform) output_test_images = load_images_from_directory(output_test_dir, transform)
Initialize the model, loss function, and optimizer
model = SimpleCNN() criterion = nn.MSELoss() optimizer = optim.Adam(model.parameters(), lr=0.001)
Training loop
epochs = 10 batch_size = 8 for epoch in range(epochs): permutation = torch.randperm(input_train_images.size()[0]) for i in range(0, input_train_images.size()[0], batch_size): indices = permutation[i:i+batch_size] batch_inputs, batch_outputs = input_train_images[indices], output_train_images[indices]
optimizer.zero_grad() outputs = model(batch_inputs) loss = criterion(outputs, batch_outputs) loss.backward() optimizer.step() print(f'Epoch {epoch+1}, Loss: {loss.item()}')Evaluate the model
with torch.no_grad(): test_outputs = model(input_test_images) test_loss = criterion(test_outputs, output_test_images) print(f'Evaluation Loss: {test_loss.item()}')
Save the model
save_path = "D:\Bilder\Python\grayscale_adjustment_model.pth" torch.save(model.state_dict(), save_path) print(f'Model saved to {save_path}')
- Assistant
To modify the given code to recursively load images from all subdirectories, you can use the
os.walk()function. This function generates the file names in a directory tree by walking the tree either top-down or bottom-up. Here's how you can modify theload_images_from_directoryfunction to achieve this:def load_images_from_directory(directory, transform, image_size=(64, 64)): images = [] for root, _, files in os.walk(directory): for filename in sorted(files): if filename.endswith('.png') or filename.endswith('.jpg'): filepath = os.path.join(root, filename) with Image.open(filepath) as img: img = img.convert('L') # Convert to grayscale img = img.resize(image_size) images.append(transform(img)) return torch.stack(images)This revised function will recursively go through each subdirectory within the specified directory, ensuring all images within those subdirectories are processed. The rest of your code remains unchanged. Here, is the current directory path being traversed, is a placeholder for directories in the current level (which we are not using here), and contains the files in the current directory. We use to ensure we have the correct path to each file.
- User
print the new code in full
- Assistant
- User
implement a process show to show how long the training will still take
- Assistant
- User
how to use the new neural network to adjust other images?
- Assistant
- User
how can i uses for all image sizes not just 64/64
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.