make sure this code doesnt create the error Traceback (most recent call last): File…
- User
make sure this code doesnt create the error Traceback (most recent call last): File "D:\NeuralNeighborStyleTransfer-main\1.txt", line 110, in run_style_transfer cnn = misc.to_device(Vgg16Pretrained(), device=device) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: to_device() got an unexpected keyword argument 'device': import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QLineEdit, QPushButton, QVBoxLayout, QWidget, QFileDialog, QCheckBox, QMessageBox import argparse import random import time import torch import numpy as np from imageio import imwrite from pretrained.vgg import Vgg16Pretrained from utils import misc as misc from utils.misc import load_path_for_pytorch from utils.stylize import produce_stylization
class StyleTransferApp(QMainWindow): def init(self): super().init() self.setWindowTitle("Style Transfer")
self.content_path_label = QLabel("Content Image Path:") self.content_path_input = QLineEdit() self.content_path_button = QPushButton("Browse") self.content_path_button.clicked.connect(self.browse_content_image) self.style_path_label = QLabel("Style Image Path:") self.style_path_input = QLineEdit() self.style_path_button = QPushButton("Browse") self.style_path_button.clicked.connect(self.browse_style_image) self.output_path_label = QLabel("Output Path:") self.output_path_input = QLineEdit() self.output_path_button = QPushButton("Browse") self.output_path_button.clicked.connect(self.browse_output_path) self.high_res_checkbox = QCheckBox("High Resolution") self.content_loss_checkbox = QCheckBox("Content Loss") self.dont_colorize_checkbox = QCheckBox("Don't Colorize") self.cpu_checkbox = QCheckBox("Use CPU") # New checkbox for CPU self.alpha_label = QLabel("Alpha:") self.alpha_input = QLineEdit("0.75") self.run_button = QPushButton("Run Style Transfer") self.run_button.clicked.connect(self.run_style_transfer) layout = QVBoxLayout() layout.addWidget(self.content_path_label) layout.addWidget(self.content_path_input) layout.addWidget(self.content_path_button) layout.addWidget(self.style_path_label) layout.addWidget(self.style_path_input) layout.addWidget(self.style_path_button) layout.addWidget(self.output_path_label) layout.addWidget(self.output_path_input) layout.addWidget(self.output_path_button) layout.addWidget(self.high_res_checkbox) layout.addWidget(self.content_loss_checkbox) layout.addWidget(self.dont_colorize_checkbox) layout.addWidget(self.cpu_checkbox) # Add CPU checkbox layout.addWidget(self.alpha_label) layout.addWidget(self.alpha_input) layout.addWidget(self.run_button) central_widget = QWidget() central_widget.setLayout(layout) self.setCentralWidget(central_widget) def browse_content_image(self): file_path, _ = QFileDialog.getOpenFileName(self, "Select Content Image") if file_path: self.content_path_input.setText(file_path) def browse_style_image(self): file_path, _ = QFileDialog.getOpenFileName(self, "Select Style Image") if file_path: self.style_path_input.setText(file_path) def browse_output_path(self): file_path, _ = QFileDialog.getSaveFileName(self, "Save Output As") if file_path: self.output_path_input.setText(file_path) def run_style_transfer(self): content_path = self.content_path_input.text() style_path = self.style_path_input.text() output_path = self.output_path_input.text() high_res = self.high_res_checkbox.isChecked() content_loss = self.content_loss_checkbox.isChecked() dont_colorize = self.dont_colorize_checkbox.isChecked() use_cpu = self.cpu_checkbox.isChecked() # Retrieve CPU checkbox value alpha = float(self.alpha_input.text()) # Core Logic from your provided code random.seed(0) np.random.seed(0) torch.manual_seed(0) max_scls = 4 sz = 512 if high_res: max_scls = 5 sz = 1024 flip_aug = (not dont_colorize) content_weight = 1. - alpha assert (0.0 <= content_weight) and (content_weight <= 1.0), "alpha must be between 0 and 1" # Modify device based on checkbox value if use_cpu: device = torch.device('cpu') else: device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') cnn = misc.to_device(Vgg16Pretrained(), device=device) phi = lambda x, y, z: cnn.forward(x, inds=y, concat=z) content_im_orig = misc.to_device(load_path_for_pytorch(content_path, target_size=sz), device=device).unsqueeze(0) style_im_orig = misc.to_device(load_path_for_pytorch(style_path, target_size=sz), device=device).unsqueeze(0) try: torch.cuda.synchronize() start_time = time.time() output = produce_stylization(content_im_orig, style_im_orig, phi, max_iter=200, lr=2e-3, content_weight=content_weight, max_scls=max_scls, flip_aug=flip_aug, content_loss=content_loss, dont_colorize=dont_colorize) torch.cuda.synchronize() print('Done! total time: {}'.format(time.time() - start_time)) new_im_out = np.clip(output[0].permute(1, 2, 0).detach().cpu().numpy(), 0., 1.) save_im = (new_im_out * 255).astype(np.uint8) imwrite(output_path, save_im) if not use_cpu: torch.cuda.empty_cache() QMessageBox.information(self, "Success", "Style Transfer completed successfully!") except Exception as e: QMessageBox.critical(self, "Error", f"An error occurred: {str(e)}") - Assistant
The error message you're seeing suggests that the function
to_device()from themiscmodule does not accept a keyword argument nameddevice. This typically happens when the function definition does not includedeviceas a named parameter, or the function expects arguments in a different format.To resolve this issue, check the definition of
to_device()within themiscmodule. It's likely that the function expects the device as a positional argument rather than a keyword argument. Alternatively, the function's interface might have changed, or a custom modification may not have been correctly implemented.If the original
to_device()function looks something like this: - User
then show me the full code with those changes
- Assistant