-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe10e2d
commit f577cd5
Showing
4 changed files
with
164 additions
and
34 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,37 @@ | ||
import os | ||
import pandas as pd | ||
from torchvision.io import read_image | ||
import glob | ||
from torchvision.transforms import Resize | ||
from torchvision.io import read_image, ImageReadMode | ||
from torch.utils.data import Dataset | ||
from sklearn import preprocessing | ||
import torch | ||
import imghdr | ||
|
||
supported_ext = ['jpg'] | ||
|
||
class MemeDataset(Dataset): | ||
def __init__(self, labels, img_dir, transform=None, target_transform=None): | ||
self.img_labels = pd.read_csv(labels) | ||
self.img_dir = img_dir | ||
def __init__(self, data_dir, transform=None): | ||
self.transform = transform | ||
self.target_transform = target_transform | ||
self.img_path = [] | ||
for e in supported_ext: | ||
path = glob.glob(os.path.join(data_dir, '*', f'*.{e}')) | ||
for p in path: | ||
if imghdr.what(p) == 'jpeg': | ||
self.img_path.append(p) | ||
classes = set() | ||
for path in self.img_path: | ||
classes.add(os.path.basename(os.path.dirname(path))) | ||
self.labels = {cls: i for i, cls in enumerate(sorted(list(classes)))} | ||
self.resize = Resize(size = (640, 640)) | ||
|
||
def __len__(self): | ||
return len(self.img_labels) | ||
|
||
return len(self.img_path) | ||
def __getitem__(self, idx): | ||
img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx, 0]) | ||
image = self.img_labels.iloc[idx, 1] | ||
img = read_image(self.img_path[idx], ImageReadMode.RGB).float() | ||
cls = os.path.basename(os.path.dirname(self.img_path[idx])) | ||
label = self.labels[cls] | ||
|
||
if self.transform: | ||
image = self.transform(image) | ||
if self.target_transform: | ||
label = self.target_transform(label) | ||
return image, label | ||
return self.transform(img), torch.tensor(label) | ||
|
||
return self.resize(img), torch.tensor(label) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import warnings; warnings.filterwarnings('ignore') | ||
import torch | ||
from torch.utils.data import DataLoader | ||
from torchvision.transforms import Resize | ||
from torchvision.io import read_image, ImageReadMode | ||
from classes.MemeDataset import MemeDataset | ||
from models.DankCNN import DankCNN | ||
import argparse | ||
|
||
cls = {0: 'dank', 1: 'normie'} | ||
|
||
def eval(image): | ||
""" | ||
SETUP | ||
""" | ||
if torch.cuda.is_available(): | ||
device = torch.device('cuda') | ||
else: | ||
device = torch.device('cpu') | ||
|
||
""" | ||
DATA LOADING | ||
- Load all data: train, test, validation | ||
""" | ||
image = read_image(image, ImageReadMode.RGB).float() | ||
resize = Resize(size = (640, 640)) | ||
image = resize(image) | ||
|
||
""" | ||
MODEL INITIALIZATION | ||
- optimizer: Adam with weight decay as regularization technique | ||
- loss function: binary cross entropy loss | ||
""" | ||
model = DankCNN() | ||
model.load_state_dict(torch.load('models/trained/model.pt')) | ||
model = model.to(device) | ||
|
||
model.eval() | ||
with torch.no_grad(): | ||
image = image.unsqueeze(0) | ||
output = model(image.to(device)) | ||
prediction = output.data | ||
print('This meme is', cls[0 if prediction.item() < .5 else 1]) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('-i', '--image', nargs=1, help='Image to evaluate', required=True) | ||
args = parser.parse_args() | ||
eval( | ||
image=args.image[0] | ||
) |
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
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