Skip to content

Commit 8393ceb

Browse files
authored
Add support for Intel GPU to Fast Neural Style example (#1318)
1 parent 5dfeb46 commit 8393ceb

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

Diff for: fast_neural_style/README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ python neural_style/neural_style.py eval --content-image </path/to/content/image
2626
- `--model`: saved model to be used for stylizing the image (eg: `mosaic.pth`)
2727
- `--output-image`: path for saving the output image.
2828
- `--content-scale`: factor for scaling down the content image if memory is an issue (eg: value of 2 will halve the height and width of content-image)
29-
- `--cuda`: set it to 1 for running on GPU, 0 for CPU.
30-
- `--mps`: set it to 1 for running on macOS GPU
29+
- `--cuda 0|1`: set it to 1 for running on GPU, 0 for CPU.
30+
- `--mps`: use MPS device backend.
31+
- `--xpu`: use XPU device backend.
3132

3233
Train model
3334

@@ -40,8 +41,9 @@ There are several command line arguments, the important ones are listed below
4041
- `--dataset`: path to training dataset, the path should point to a folder containing another folder with all the training images. I used COCO 2014 Training images dataset [80K/13GB] [(download)](https://cocodataset.org/#download).
4142
- `--style-image`: path to style-image.
4243
- `--save-model-dir`: path to folder where trained model will be saved.
43-
- `--cuda`: set it to 1 for running on GPU, 0 for CPU.
44-
- `--mps`: set it to 1 for running on macOS GPU
44+
- `--cuda 0|1`: set it to 1 for running on GPU, 0 for CPU.
45+
- `--mps`: use MPS device backend.
46+
- `--xpu`: use XPU device backend.
4547

4648
Refer to `neural_style/neural_style.py` for other command line arguments. For training new models you might have to tune the values of `--content-weight` and `--style-weight`. The mosaic style model shown above was trained with `--content-weight 1e5` and `--style-weight 1e10`. The remaining 3 models were also trained with similar order of weight parameters with slight variation in the `--style-weight` (`5e10` or `1e11`).
4749

Diff for: fast_neural_style/neural_style/neural_style.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ def train(args):
3333
device = torch.device("cuda")
3434
elif args.mps:
3535
device = torch.device("mps")
36+
elif args.xpu:
37+
device = torch.device("xpu")
3638
else:
3739
device = torch.device("cpu")
3840

41+
print("Device to use: ", device)
42+
3943
np.random.seed(args.seed)
4044
torch.manual_seed(args.seed)
4145

@@ -126,6 +130,9 @@ def train(args):
126130

127131
def stylize(args):
128132
device = torch.device("cuda" if args.cuda else "cpu")
133+
device = torch.device("xpu" if args.xpu else "cpu")
134+
135+
print("Device to use: ", device)
129136

130137
content_image = utils.load_image(args.content_image, scale=args.content_scale)
131138
content_transform = transforms.Compose([
@@ -219,6 +226,10 @@ def main():
219226
help="number of images after which the training loss is logged, default is 500")
220227
train_arg_parser.add_argument("--checkpoint-interval", type=int, default=2000,
221228
help="number of batches after which a checkpoint of the trained model will be created")
229+
train_arg_parser.add_argument('--mps', action='store_true',
230+
help='enable macOS GPU training')
231+
train_arg_parser.add_argument('--xpu', action='store_true',
232+
help='enable Intel XPU training')
222233

223234
eval_arg_parser = subparsers.add_parser("eval", help="parser for evaluation/stylizing arguments")
224235
eval_arg_parser.add_argument("--content-image", type=str, required=True,
@@ -233,7 +244,11 @@ def main():
233244
help="set it to 1 for running on cuda, 0 for CPU")
234245
eval_arg_parser.add_argument("--export_onnx", type=str,
235246
help="export ONNX model to a given file")
236-
eval_arg_parser.add_argument('--mps', action='store_true', default=False, help='enable macOS GPU training')
247+
eval_arg_parser.add_argument('--mps', action='store_true',
248+
help='enable macOS GPU evaluation')
249+
eval_arg_parser.add_argument('--xpu', action='store_true',
250+
help='enable Intel XPU evaluation')
251+
237252

238253
args = main_arg_parser.parse_args()
239254

@@ -245,6 +260,8 @@ def main():
245260
sys.exit(1)
246261
if not args.mps and torch.backends.mps.is_available():
247262
print("WARNING: mps is available, run with --mps to enable macOS GPU")
263+
if not args.xpu and torch.xpu.is_available():
264+
print("WARNING: XPU is available, run with --xpu to enable Intel XPU")
248265

249266
if args.subcommand == "train":
250267
check_paths(args)

0 commit comments

Comments
 (0)