Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayank Sati committed May 18, 2020
0 parents commit 1f4bb69
Show file tree
Hide file tree
Showing 102 changed files with 20,273 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
log
build
install
launch/*cache*
data/*
nohup.out
domain_id
*.idea
*~
*.pyc
*.pth
*.csv
Empty file added .gitkeep
Empty file.
14 changes: 14 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"configurations": [
{
"browse": {
"databaseFilename": "",
"limitSymbolsToIncludedHeaders": true
},
"includePath": [
"/usr/include/**"
],
"name": "ROS"
}
]
}
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"python.autoComplete.extraPaths": [
"/opt/ros/dashing/lib/python3.6/site-packages"
]
}
1 change: 1 addition & 0 deletions .~lock.xshui.csv#
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
,mayank_sati,gwmidclap0045-Precision-7730,05.09.2019 14:21,file:///home/mayank_sati/.config/libreoffice/4;
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Hiromichi Kamata

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

## QATM:Quality-Aware Template Matching For Deep Learning
arxiv: https://arxiv.org/abs/1903.07254
original code (tensorflow+keras): https://github.com/cplusx/QATM

I used code from https://github.com/kamata1729/QATM_pytorch and modified it to
my purpose.

For base code credit to @https://github.com/kamata1729.
## Dependencies

* torch(1.0.0)
* torchvision(0.2.1)
* cv2
* seaborn
* sklearn
* pathlib

## Installing the right version of PyTorch
This project is updated to be compatible with pytorch 1.0.1 and requires python 3.6


You can find other project requirements in `requirements.txt` , which you can install using `pip install -r requirements.txt`



## Inference

* Run : inference.py
* For custum datasets : inference_custom.py


`template1_1.png` to `template1_4.png` are contained in `sample1.jpg`, however, `template1_dummy.png` is a dummy and not contained

|template1_1.png|template1_2.png|template1_3.png|template1_4.png|template1_dummy.png|
|---|---|---|---|---|
|![](https://i.imgur.com/lP0Wy4I.png)|![](https://i.imgur.com/xDJoQOz.png)|![image.png](https://qiita-image-store.s3.amazonaws.com/0/262908/472c81ae-9afb-db49-a64c-86604cbe0884.png)|![image.png](https://qiita-image-store.s3.amazonaws.com/0/262908/d402a9d2-bbd4-5353-16aa-567b79ca06b8.png)|![](https://i.imgur.com/p10g33j.png)|

![image.png](https://qiita-image-store.s3.amazonaws.com/0/262908/2e4c4b8b-2889-7962-4f35-c313048dc403.png)


# Usage

See [`qatm_pytorch.ipynb`](https://github.com/kamata1729/QATM_pytorch/blob/master/qatm_pytorch.ipynb)

or


```
python qatm.py -s sample/sample1.jpg -t template --cuda
```

* Add `--cuda` option to use GPU
* Add `-s`/`--sample_image` to specify sample image
**only single** sample image can be specified in this present implementation
* Add `-t`/`--template_images_dir` to specify template image dir

**[notice]** If neither `-s` nor `-t` is specified, the demo program will be executed, which is the same as:
```
python qatm.py -s sample/sample1.jpg -t template
```

* `--thresh_csv` and `--alpha` option can also be added

170 changes: 170 additions & 0 deletions color_detect_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
from __future__ import print_function
import argparse

import torchvision.datasets as dset
import torchvision.transforms as transforms
from torch.utils.data import DataLoader, Dataset

import random
from PIL import Image
import torch

import PIL.ImageOps
import torch.nn as nn
from torch import optim
import torch.nn.functional as F


class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(3, 8, kernel_size=3, stride=1, padding=2),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),

nn.Conv2d(8, 16, kernel_size=3, stride=1, padding=2),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),

nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=2),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2))

self.fc1 = nn.Linear(5 * 5 * 32, 1000)
# self.fc1 = nn.Linear(800, 1000)
self.fc2 = nn.Linear(1000, 64)
self.fc3 = nn.Linear(64, 4)

def forward(self, x):
out = self.layer1(x)
# out = self.layer2(out)
out = out.reshape(out.size(0), -1)
# out = self.drop_out(out)
out = F.relu(self.fc1(out))
out = F.relu(self.fc2(out))
# out = F.relu(self.fc1(out))
out = self.fc3(out)
# return out
return F.log_softmax(out, dim=1)


def train(args, model, device, train_loader, optimizer, epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = F.nll_loss(output, target)
loss.backward()
optimizer.step()
if batch_idx % args.log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))


# def test(args, model, device, test_loader):
# model.eval()
# test_loss = 0
# correct = 0
# with torch.no_grad():
# for data, target in test_loader:
# data, target = data.to(device), target.to(device)
# output = model(data)
# test_loss += F.nll_loss(output, target, reduction='sum').item() # sum up batch loss
# pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability
# correct += pred.eq(target.view_as(pred)).sum().item()
#
# test_loss /= len(test_loader.dataset)
#
# print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
# test_loss, correct, len(test_loader.dataset),
# 100. * correct / len(test_loader.dataset)))
#


class Create_Image_Datasets(Dataset):

def __init__(self, imageFolderDataset, transform=None, should_invert=True):
self.imageFolderDataset = imageFolderDataset
self.transform = transform
self.should_invert = should_invert

def __getitem__(self, index):
img0_tuple = random.choice(self.imageFolderDataset.imgs)
# img0_tuple = random.choice(self.imageFolderDataset.samples)

img0 = Image.open(img0_tuple[0])
img0 = img0.convert("RGB")
if self.should_invert:
img0 = PIL.ImageOps.invert(img0)
# img1 = PIL.ImageOps.invert(img1)
if self.transform is not None:
img0 = self.transform(img0)
# return img0, img1 , torch.from_numpy(np.array([int(img1_tuple[1]!=img0_tuple[1])],dtype=np.float32))
label = img0_tuple[1]
return img0, label

def __len__(self):
return len(self.imageFolderDataset.imgs)

def main():
# Training settings
training_dir = "/home/mayank_sati/Desktop/traffic_light/sorting_light/final_sort"
parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
parser.add_argument('--train_dir', type=str, default=training_dir, metavar='N',
help='path for training directory')
parser.add_argument('--batch_size', type=int, default=64, metavar='N',
help='input batch size for training (default: 64)')
parser.add_argument('--test-batch-size', type=int, default=1000, metavar='N',
help='input batch size for testing (default: 1000)')
parser.add_argument('--epochs', type=int, default=10, metavar='N',
help='number of epochs to train (default: 10)')
parser.add_argument('--lr', type=float, default=0.01, metavar='LR',
help='learning rate (default: 0.01)')
parser.add_argument('--momentum', type=float, default=0.5, metavar='M',
help='SGD momentum (default: 0.5)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
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=True,
help='For Saving the current Model')
args = parser.parse_args()

use_cuda = not args.no_cuda and torch.cuda.is_available()

torch.manual_seed(args.seed)

device = torch.device("cuda" if use_cuda else "cpu")

kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
########################################33

res = 30

train_folder_dataset = dset.ImageFolder(root=args.train_dir)
train_dataset = Create_Image_Datasets(imageFolderDataset=train_folder_dataset, transform=transforms.Compose(
[transforms.Resize((res, res)), transforms.ToTensor()]), should_invert=False)
train_dataloader = DataLoader(train_dataset, shuffle=True, num_workers=4, batch_size=args.batch_size)

model = Net().to(device)
optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum)
# optimizer = optim.Adam(model.parameters(), lr=0.1, betas=(0.9, 0.999), eps=1e-08,
# weight_decay=0.0)

for epoch in range(1, args.epochs + 1):
train(args, model, device, train_dataloader, optimizer, epoch)
# test(args, model, device, test_loader)

if (args.save_model):
torch.save(model.state_dict(), "color_model.pt")
# torch.save(model.state_dict(), './model-save_dict_color-%s.pt' % epoch)


if __name__ == '__main__':
main()
Loading

0 comments on commit 1f4bb69

Please sign in to comment.