(venv) C:\WINDOWS\system32>python test.py Training the model... Traceback (most recent…
- User
(venv) C:\WINDOWS\system32>python test.py Training the model... Traceback (most recent call last): File "C:\WINDOWS\system32\test.py", line 160, in main() File "C:\WINDOWS\system32\test.py", line 144, in main train(model, train_loader, criterion, optimizer) File "C:\WINDOWS\system32\test.py", line 52, in train images = preprocess_image(image_path) # Remove [0] since train_loader now provides the path directly ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\WINDOWS\system32\test.py", line 34, in preprocess_image image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: Can't convert object to 'str' for 'filename'
import os import cv2 import torch import torch.nn as nn import torch.optim as optim import random import numpy as np import torchvision.transforms as transforms import pygetwindow as gw import pydirectinput import time
Define the CNN model
class SimpleCNN(nn.Module): def init(self, output_size): super(SimpleCNN, self).init() self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=2) self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=2) self.conv3 = nn.Conv2d(64, 64, kernel_size=3, stride=2) self.fc1 = nn.Linear(64 * 6 * 6, 128) self.fc2 = nn.Linear(128, output_size)
def forward(self, x): x = torch.relu(self.conv1(x)) x = torch.relu(self.conv2(x)) x = torch.relu(self.conv3(x)) x = x.view(x.size(0), -1) x = torch.relu(self.fc1(x)) x = self.fc2(x) return xImage preprocessing
def preprocess_image(image_path): image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) image = cv2.resize(image, (84, 84)) # Resize to a consistent size image = image / 255.0 # Normalize pixel values to [0, 1] image = torch.from_numpy(image).float().unsqueeze(0).unsqueeze(0) # Add batch and channel dimensions return image
def load_dataset(data_folder): dataset = [] for root, dirs, files in os.walk(data_folder): for file in files: image_path = os.path.join(root, file) dataset.append((image_path,)) return dataset
Training function
def train(model, train_loader, criterion, optimizer, epochs=5): for epoch in range(epochs): for image_path in train_loader: images = preprocess_image(image_path) # Remove [0] since train_loader now provides the path directly labels = torch.randint(0, 4, (1,)) optimizer.zero_grad() outputs = model(images) loss = criterion(outputs, labels) loss.backward() optimizer.step()
Testing function
def test(model, test_loader): for image_path in test_loader: image = preprocess_image(image_path) output = model(image) predicted_action = torch.argmax(output).item() print(f"Predicted Action: {predicted_action} for Image: {image_path}") simulate_action(predicted_action)
Function to check if the game is open
def is_game_open(): game_window = gw.getWindowsWithTitle('Superfighters Deluxe') return bool(game_window)
Function to simulate actions based on the agent's output
def simulate_action(action): if action == 0: # Randomly choose between Jump / ClimbLadder and Move to the Left or Move to the Right if np.random.rand() < 0.5: pydirectinput.keyDown('up')
pydirectinput.keyDown('left') print("Simulating action: Jump / ClimbLadder and Move to the Left") else: pydirectinput.keyDown('up') pydirectinput.keyDown('right') print("Simulating action: Jump / ClimbLadder and Move to the Right") elif action == 1: pydirectinput.keyDown('down') # Crouch / SlideDownLadder print("Simulating action: Crouch / SlideDownLadder") elif action == 2: # Move to the left pydirectinput.keyDown('left') print("Simulating action: Move to the left") elif action == 3: # Move to the right pydirectinput.keyDown('right') print("Simulating action: Move to the right")time.sleep(0.1) keys_to_release = ['up', 'down', 'left', 'right', 'd', 'a', 's', 'shift'] keys_to_release.extend(map(str, range(1, 6))) keys_to_release.extend(['left', 'right', 'down', 'f']) for key in keys_to_release: pydirectinput.keyUp(key)Main function
def main(): data_folder = r"C:\Users\Ninja.000\Downloads\SFD_AI\gameplay\Tutorial" output_size = 4 # Assuming there are 4 possible actions
# Create a simple CNN model model = SimpleCNN(output_size) # Specify the path to save the trained model model_path = r"C:\Users\Ninja.000\Downloads\SFD_AI\trained_model.pth" # Check if the trained model file exists if os.path.exists(model_path): # Load the trained model model.load_state_dict(torch.load(model_path)) model.eval() else: # Train the model if the file doesn't exist print("Training the model...") # Load the dataset dataset = load_dataset(data_folder) # Define the loss function and optimizer criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=0.001) # Split the dataset into training and testing sets random.shuffle(dataset) split_index = int(0.8 * len(dataset)) train_dataset = dataset[:split_index] test_dataset = dataset[split_index:] # Create data loaders train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True) test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=32, shuffle=False) # Train the model train(model, train_loader, criterion, optimizer) # Test the model test(model, test_loader) # Save the trained model torch.save(model.state_dict(), model_path) print(f"Trained model saved at: {model_path}") # List all images in the dataset folder dataset = [os.path.join(data_folder, f) for f in os.listdir(data_folder) if f.endswith('.jpg')] # Test the model on the dataset test(model, dataset)if name == "main": main()
- Assistant
The error message you're seeing suggests that an object that isn't a string (and it seems that the object could be a tuple) is being passed to
cv2.imread()instead of a file path string. This is evident from the line:TypeError: Can't convert object to 'str' for 'filename'In the
load_datasetfunction, when you're appending to thedatasetlist, you're placing the image path inside a tuple:dataset.append((image_path,)). Later, this tuple is passed to the preprocessing function, butcv2.imread()expects a string, not a tuple.When you iterate over
train_loaderandtest_loader, the loaders return batches, and each item within a batch is a tuple (even if there's only one element per sample thanks to how you've structuredload_dataset).To fix this, you need to ensure that you're extracting the string path from the tuple before passing it to the
preprocess_imagefunction. Here's what you need to change:In your
trainfunction, replace:images = preprocess_image(image_path)with:
image_path = image_path[0] # extract the string from the tuple images = preprocess_image(image_path)Do the same in the
testfunction where you callpreprocess_image(image_path):image_path = image_path[0] # extract the string from the tuple image = preprocess_image(image_path)Also, you should be looping over batches rather than individual samples if you're using a
DataLoaderwithbatch_size=32. And you'll need to handle batches of labels accordingly too, not just single labels.Finally, make sure that
train_loaderandtest_loaderare actually returning image file paths as strings. If you applied the above changes to theload_datasetfunction, then they should be, but double-check this part to be certain. If you're usingDataLoader, ensure that yourDatasetclass is returning the correct information in its__getitem__method.Additionally, unless you have multiple images in a batch, it would be wise to adjust batch processing so that labels and images align correctly. If each batch is supposed to have 32 images, then instead of generating a random label for each batch, you should be creating a tensor of labels one for each image.
Finally, it's not a good idea to run Python scripts, especially ones that write files, in the
C:\WINDOWS\system32directory due to potential permissions issues and the risk of affecting system stability. You might want to run your scripts in a user directory or another non-system critical location instead.