I have a set of images of micro-structures of the anode of different batteries and their…
- User
I have a set of images of micro-structures of the anode of different batteries and their corresponding current outputs for 400 time steps, I have the current outputs in a csv file, with each row containing 401 values, with the first 400 being current and the last column contains a number which corresponds to the name of the image, for example if the last column contains 451, then the micro-structure image corresponding to this image is named 451.png, The name of the csv file is "final_dataset.csv" and the images are stored in a folder named "Image_final", Now i want to use deep learning to develop a model which will generate a micro structure image given an array of current with 400 time steps, The images are grayscale and have a size 256x256 and the current values are in the order of e-2 to e-5, Can you implement the solution in pytorch
- Assistant
To create a model that generates a micro-structure image based on an input array of currents with 400 time steps, you can use a conditional Generative Adversarial Network (cGAN). The network would consist of a generator (to generate the images) and a discriminator (to determine if images are real or generated).
Here's a PyTorch implementation:
- Import necessary libraries
import os import numpy as np import pandas as pd from PIL import Image from torch.utils.data import Dataset, DataLoader from torchvision import transforms import torch import torch.nn as nn import torch.optim as optim- Create a custom dataset
class MicroStructureDataset(Dataset): def __init__(self, csv_file, root_dir, transform=None): self.data_frame = pd.read_csv(csv_file) self.root_dir = root_dir self.transform = transform def __len__(self): return len(self.data_frame) def __getitem__(self, idx): if torch.is_tensor(idx): idx = idx.tolist() img_id = int(self.data_frame.iloc[idx, -1]) img_path = os.path.join(self.root_dir, f"{img_id}.png") image = Image.open(img_path).convert('L') current = self.data_frame.iloc[idx, :-1].values current = current.astype("float") if self.transform: image = self.transform(image) return current, image- Load the dataset
# Load image transformation image_transform = transforms.Compose([ transforms.Resize((256, 256)), transforms.ToTensor(), transforms.Normalize((0.5), (0.5)) ]) dataset = MicroStructureDataset(csv_file="final_dataset.csv", root_dir="Image_final", transform=image_transform) dataloader = DataLoader(dataset, batch_size=64, shuffle=True)- Build the Generator and Discriminator
class Generator(nn.Module): def __init__(self): super(Generator, self).__init__() # Input is 400 length array of currents self.current_FC = nn.Sequential(nn.Linear(400, 256)) self.generator = nn.Sequential( # Input is 256 channels (split into 8x8x4) nn.ConvTranspose2d(256, 128, 4, 2, 1, bias=False), nn.BatchNorm2d(128), nn.ReLU(True), nn.ConvTranspose2d(128, 64, 4, 2, 1, bias=False), nn.BatchNorm2d(64), nn.ReLU(True), nn.ConvTranspose2d(64, 1, 4, 2, 1, bias=False), nn.Tanh() ) def forward(self, x): x = self.current_FC(x) x = x.view(x.shape[0], 256, 1, 1) x = self.generator(x) return x class Discriminator(nn.Module): def __init__(self): super(Discriminator, self).__init__() self.image_discriminator = nn.Sequential( nn.Conv2d(1, 64, 4, 2, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), nn.Conv2d(64, 128, 4, 2, 1, bias=False), nn.BatchNorm2d(128), nn.LeakyReLU(0.2, inplace=True), nn.Conv2d(128, 1, 8, 1, 0, bias=False), ) def forward(self, x): x = self.image_discriminator(x) x = x.view(x.shape[0], -1) return x- Train the model
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") generator = Generator().to(device) discriminator = Discriminator().to(device) criterion = nn.BCEWithLogitsLoss() lr = 0.0002 optim_G = optim.Adam(generator.parameters(), lr=lr, betas=(0.5, 0.999)) optim_D = optim.Adam(discriminator.parameters(), lr=lr, betas=(0.5, 0.999)) num_epochs = 200 for epoch in range(num_epochs): for i, (current, images) in enumerate(dataloader): current, images = current.to(device), images.to(device) batch_size = current.size(0) label = torch.full((batch_size, 1), 0.9, device=device) # Update discriminator - real images output = discriminator(images) errD_real = criterion(output, label) D_x = output.mean().item() # Update discriminator - generated images noise = generator(current) label.fill_(0.0) output = discriminator(noise.detach()) errD_fake = criterion(output, label) D_G_z1 = output.mean().item() errD = errD_real + errD_fake optim_D.zero_grad() errD.backward() optim_D.step() # Update generator label.fill_(0.9) output = discriminator(noise) errG = criterion(output, label) D_G_z2 = output.mean().item() optim_G.zero_grad() errG.backward() optim_G.step() print(f"[{epoch+1}/{num_epochs}] Loss_D: {errD.item()}, Loss_G: {errG.item()}, D(x): {D_x}, D(G(z)): {D_G_z1}/{D_G_z2}")After training, the generator can now produce micro-structure images for given input current values. Here's code for testing:
generator.eval() input_current = torch.FloatTensor(sample_current_array).to(device) # Replace "sample_current_array" with the input current data of shape (400,) generated_image = generator(input_current.unsqueeze(0)) generated_image = (generated_image.detach().cpu().numpy().squeeze() * 255).astype(np.uint8) Image.fromarray(generated_image)This will generate the micro-structure image based on the input current values. Note that this model could likely still be improved, and you might need to experiment with the generator and discriminator architectures, learning rate, and other hyperparameters.
- User
How to save the model after training
- Assistant
After training, you can save the model using PyTorch's
torch.save()function. To save the generator model, you can use the following code snippet:# Save the generator model model_path = "generator_model.pth" torch.save(generator.state_dict(), model_path)Later, if you want to load the trained model for inference, you can create a new generator instance and load the state dictionary from the saved file as follows:
# Load the generator model loaded_generator = Generator().to(device) loaded_generator.load_state_dict(torch.load(model_path)) loaded_generator.eval()Now, you can use the
loaded_generatorfor generating the micro-structure images, just as you would with the originalgenerator.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.