无法复现PyTorch Neural Transfer示例问题求助
Hey there, let’s break down the possible reasons and fixes for your unexpected output size, especially given your older PyTorch setup (0.3.1) on a CUDA-less Mac:
Check conditional image size logic in the code
Older PyTorch neural style transfer examples often include a conditional check for CUDA availability to set the default image size. Look for lines like:imsize = 128 if not torch.cuda.is_available() else 512Since your Mac has no CUDA, the code is automatically falling back to 128x128. Try manually overriding this value to
512(just make sure your CPU has enough memory to handle it—more on that next).Verify image preprocessing transforms
Double-check theloadertransform chain. You’ll likely see something like:loader = transforms.Compose([ transforms.Resize(imsize), # This is where the resize happens transforms.ToTensor()])Ensure the
Resizeparameter isn’t being forced to 128 by the conditional logic mentioned above. Hardcode512here if needed, e.g.,transforms.Resize(512).Assess CPU memory constraints
Running 512x512 style transfer on a CPU (especially with PyTorch 0.3.1) can eat up a lot of RAM. If your Mac has limited memory, the code might silently fall back to a smaller size to avoid crashing. Try testing with a 256x256 size first—if that works, you can try 512 again, but close other apps to free up RAM. Keep an eye on your system’s memory usage while running the notebook.Cross-reference tutorial vs. notebook code for version differences
PyTorch 0.3.1 is quite outdated (released in 2018), and the official tutorial might have updated defaults for newer versions. Compare the code in your downloaded notebook with the tutorial’s current code—look for any discrepancies in how image size is set. Older examples prioritized CPU compatibility with smaller sizes, while newer ones assume CUDA is available by default.Check for unintended resizing during output saving
Sometimes the issue isn’t in the model’s output, but in how you’re saving the image. Look at the code that converts the tensor back to a PIL image—make sure there’s no extraresizecall there. For example, if you have something likeimg.resize((128,128))before saving, that’s the culprit.
内容的提问来源于stack exchange,提问作者Jane Wayne

