This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"value": { | |
"format": "rle", | |
"rle": [ | |
0, 18, 192, 0, 57, 27, 255, 255, 255, 0, 255, 255, 224, 31, 255, | |
252, 3, 255, 255, 128, 113, 224, 112, 8, 212, 35, 207, 159, 254, | |
58, 248, 211, 248, 79, 24, 4, 100, 24, 95, 254, 61, 248, 223, | |
227, 16, 225, 58, 224, 17, 168, 97, 255, 248, 251, 227, 80, 225, | |
58, 96, 17, 168, 98, 127, 248, 239, 227, 64, 225, 57, 96, 17, | |
144, 98, 255, 248, 239, 227, 100, 140, 255, 132, 225, 128, 70, | |
161, 30, 134, 51, 255, 143, 190, 52, 14, 19, 142, 1, 27, 4, 120, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
cd ~/Desktop | |
# --------------------------- | |
## 0. Requirements | |
# --------------------------- | |
sudo apt-get update | |
sudo apt-get install -y build-essential git pkg-config cmake make \ | |
gcc curl wget unzip \ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def singleImage(path, label= None, show= False): | |
img = cv2.imread(path) | |
assert img is not None,"Immage wasn't read properly" | |
img = cv2.resize(img, (100, 100)) | |
img = torch.from_numpy(img) | |
img = img.permute((2, 0,1)) # model expects image to be of shape [3, 100, 100] | |
img = img.unsqueeze(dim=0).float() # convert single image to batch [1, 3, 100, 100] | |
img = img.to('cuda') # Using the same device as the model | |
pred = model(img) | |
_, preds = torch.max(pred, dim=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@torch.no_grad() | |
def evaluate(model, val_loader): | |
model.eval() | |
outputs = [model.validation_step(batch) for batch in val_loader] | |
return model.validation_epoch_end(outputs) | |
def fit(epochs, lr, model, train_loader, val_loader, opt_func=torch.optim.SGD): | |
history = [] | |
optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9) | |
for epoch in range(epochs): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_default_device(): | |
"""Pick GPU if available, else CPU""" | |
if torch.cuda.is_available(): | |
return torch.device('cuda') | |
else: | |
return torch.device('cpu') | |
device = get_default_device() | |
def to_device(data, device): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImageClassificationBase(nn.Module): | |
def training_step(self, batch): | |
images, labels = batch | |
out = self(images) # Generate predictions | |
loss = F.cross_entropy(out, labels.long()) # Calculate loss | |
return loss | |
def validation_step(self, batch): | |
images, labels = batch | |
out = self(images) # Generate predictions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MaskDetection(ImageClassificationBase): | |
def __init__(self): | |
super().__init__() | |
self.network = nn.Sequential( | |
nn.Conv2d(3, 100, kernel_size=3, padding=1), | |
nn.ReLU(), | |
nn.Conv2d(100, 128, kernel_size=3, stride=1, padding=1), | |
nn.ReLU(), | |
nn.MaxPool2d(2, 2), # output: 128 x 8 x 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from torch.utils.data.dataloader import DataLoader | |
from torchvision.utils import make_grid | |
batch_size = 32 | |
train_dl = DataLoader(train_ds, batch_size*2, shuffle=True) | |
val_dl = DataLoader(val_ds, batch_size*2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from torch.utils.data.dataset import Dataset | |
from torchvision.transforms import Compose, ToTensor | |
class MaskDataset(Dataset): | |
""" Masked faces dataset | |
0 = 'no mask' | |
1 = 'mask' | |
""" | |
def __init__(self, train_data): | |
self.train_data = train_data | |
self.transformations = Compose([ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REBUILD_DATA = True | |
if REBUILD_DATA: #if we are running it for the first time | |
data_path = Path('F:/FILES/AI/face-mask-dataset/') | |
maskPath = data_path/'dataset1/AFDB_masked_face_dataset' | |
maskPath2= data_path/'dataset2/webface_masked' | |
nonMaskPath = data_path/'dataset1/AFDB_face_dataset' | |
path_dirs = [ [maskPath,1],[nonMaskPath,0] ] #path and label | |
if not os.path.exists(data_path): | |
raise Exception("The data path doesn't exist") |