Could you make it so that this code instead of getting tutorial frames it uses this pre…
- User
Could you make it so that this code instead of getting tutorial frames it uses this pre trained dataset model model_path = r"C:\Windows\System32\yolov5\models\best.pt"
import re import os import cv2 import logging import pydirectinput import torch import torch.nn as nn import torch.optim as optim import random import pygetwindow as gw import numpy as np import subprocess import pyautogui import torchvision.models as models import time from collections import namedtuple from PIL import Image
Disable failsafe feature of PyAutoGUI
pyautogui.FAILSAFE = False
Configure logging
LOG_FILE = r"C:\Users\Ninja.000\Downloads\SFD_AI\training_log.txt" logging.basicConfig(filename=LOG_FILE, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger(name) device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
Define the DQN model
class DQN(nn.Module): def init(self, input_size, output_size, use_pretrained=False): super(DQN, self).init()
code# Use a pre-trained model (e.g., ResNet18) as the feature extractor if use_pretrained: self.feature_extractor = models.resnet18(pretrained=True) self.feature_extractor.fc = nn.Identity() # Remove the fully connected layer else: # Define your own convolutional layers for feature extraction self.conv1 = nn.Conv2d(in_channels=input_size[0], out_channels=32, kernel_size=8, stride=4) self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=4, stride=2) self.conv3 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1) # Common layers for both cases self.fc1 = nn.Linear(64 * 7 * 7, 512) # Set the correct input size self.fc2 = nn.Linear(512, output_size) def forward(self, x): if hasattr(self, 'feature_extractor'): # Check if using a pre-trained model x = self.feature_extractor(x) x = x.view(x.size(0), -1) # Flatten the output else: x = x / 255.0 # Normalize pixel values x = torch.as_tensor(x, dtype=torch.float32) # Remove clone().detach() x = x.permute(0, 3, 1, 2) # Change the order of dimensions (N, H, W, C) to (N, C, H, W) x = self.conv1(x) x = torch.relu(x) x = self.conv2(x) x = torch.relu(x) x = self.conv3(x) x = torch.relu(x) # Print the shape of the tensor at this point print("Shape after convolutional layers:", x.shape) x = torch.flatten(x, start_dim=1) # Flatten the output # Update the fully connected layers with the correct input size self.fc1 = nn.Linear(x.size(1), 512) self.fc2 = nn.Linear(512, output_size) x = self.fc1(x) x = torch.relu(x) x = self.fc2(x) return x - Assistant