From c317713e91a830aa0e033ca12d2d353b23fde665 Mon Sep 17 00:00:00 2001 From: eromomon Date: Fri, 21 Feb 2025 11:47:29 -0600 Subject: [PATCH] Add support for Intel GPU to Siamese Network example --- siamese_network/README.md | 37 ++++++++++++++++++++++++++++++++++++- siamese_network/main.py | 15 +++++++++++---- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/siamese_network/README.md b/siamese_network/README.md index 973a0414a4..19b19f0e76 100644 --- a/siamese_network/README.md +++ b/siamese_network/README.md @@ -1,7 +1,42 @@ # Siamese Network Example +Siamese network for image similarity estimation. +The network is composed of two identical networks, one for each input. +The output of each network is concatenated and passed to a linear layer. +The output of the linear layer passed through a sigmoid function. +[FaceNet](https://arxiv.org/pdf/1503.03832.pdf) is a variant of the Siamese network. +This implementation varies from FaceNet as we use the `ResNet-18` model from +[Deep Residual Learning for Image Recognition](https://arxiv.org/pdf/1512.03385.pdf) as our feature extractor. +In addition, we aren't using `TripletLoss` as the MNIST dataset is simple, so `BCELoss` can do the trick. + ```bash pip install -r requirements.txt python main.py -# CUDA_VISIBLE_DEVICES=2 python main.py # to specify GPU id to ex. 2 ``` + +Optionally, you can add the following arguments to customize your execution. + +```bash +--batch-size input batch size for training (default: 64) +--test-batch-size input batch size for testing (default: 1000) +--epochs number of epochs to train (default: 14) +--lr learning rate (default: 1.0) +--gamma learning rate step gamma (default: 0.7) +--no-cuda disables CUDA training +--no-xpu disables XPU training +--no-mps disables macOS GPU training +--dry-run quickly check a single pass +--seed random seed (default: 1) +--log-interval how many batches to wait before logging training status +--save-model Saving the current Model +``` + +If a GPU device (CUDA, XPU, or MPS) is detected, the example will be executed on the GPU by default; otherwise, it will run on the CPU. + +To disable the GPU option, add the appropriate argument to the command. For example: + +```bash +python main.py --no-xpu +``` + +This command will execute the example on the CPU even if your system successfully detects an XPU. diff --git a/siamese_network/main.py b/siamese_network/main.py index 8f420a9b01..6bd55235e2 100644 --- a/siamese_network/main.py +++ b/siamese_network/main.py @@ -247,32 +247,39 @@ def main(): help='learning rate (default: 1.0)') parser.add_argument('--gamma', type=float, default=0.7, metavar='M', help='Learning rate step gamma (default: 0.7)') - parser.add_argument('--no-cuda', action='store_true', default=False, + parser.add_argument('--no-cuda', action='store_true', help='disables CUDA training') - parser.add_argument('--no-mps', action='store_true', default=False, + parser.add_argument('--no-xpu', action='store_true', + help='disables XPU training') + parser.add_argument('--no-mps', action='store_true', help='disables macOS GPU training') - parser.add_argument('--dry-run', action='store_true', default=False, + parser.add_argument('--dry-run', action='store_true', help='quickly check a single pass') parser.add_argument('--seed', type=int, default=1, metavar='S', help='random seed (default: 1)') parser.add_argument('--log-interval', type=int, default=10, metavar='N', help='how many batches to wait before logging training status') - parser.add_argument('--save-model', action='store_true', default=False, + parser.add_argument('--save-model', action='store_true', help='For Saving the current Model') args = parser.parse_args() use_cuda = not args.no_cuda and torch.cuda.is_available() + use_xpu = not args.no_xpu and torch.xpu.is_available() use_mps = not args.no_mps and torch.backends.mps.is_available() torch.manual_seed(args.seed) if use_cuda: device = torch.device("cuda") + elif use_xpu: + device = torch.device("xpu") elif use_mps: device = torch.device("mps") else: device = torch.device("cpu") + print('Device to use: ', device) + train_kwargs = {'batch_size': args.batch_size} test_kwargs = {'batch_size': args.test_batch_size} if use_cuda: