Skip to content

Add support for Intel GPU to Siamese Network example #1317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion siamese_network/README.md
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 11 additions & 4 deletions siamese_network/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down