forked from asayeed/lt2326-h24-wa_modeling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikiart.py
87 lines (76 loc) · 2.7 KB
/
wikiart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import sys
import os
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from torchvision.io import read_image
import matplotlib.pyplot as plt
import torchvision.transforms.functional as F
from torch.optim import Adam
import tqdm
class WikiArtImage:
def __init__(self, imgdir, label, filename):
self.imgdir = imgdir
self.label = label
self.filename = filename
self.image = None
self.loaded = False
def get(self):
if not self.loaded:
self.image = read_image(os.path.join(self.imgdir, self.label, self.filename)).float()
self.loaded = True
return self.image
class WikiArtDataset(Dataset):
def __init__(self, imgdir, device="cpu"):
walking = os.walk(imgdir)
filedict = {}
indices = []
classes = set()
print("Gathering files for {}".format(imgdir))
for item in walking:
sys.stdout.write('.')
arttype = os.path.basename(item[0])
artfiles = item[2]
for art in artfiles:
filedict[art] = WikiArtImage(imgdir, arttype, art)
indices.append(art)
classes.add(arttype)
print("...finished")
self.filedict = filedict
self.imgdir = imgdir
self.indices = indices
self.classes = list(classes)
self.device = device
def __len__(self):
return len(self.filedict)
def __getitem__(self, idx):
imgname = self.indices[idx]
imgobj = self.filedict[imgname]
ilabel = self.classes.index(imgobj.label)
image = imgobj.get().to(self.device)
return image, ilabel
class WikiArtModel(nn.Module):
def __init__(self, num_classes=27):
super().__init__()
self.conv2d = nn.Conv2d(3, 1, (4,4), padding=2)
self.maxpool2d = nn.MaxPool2d((4,4), padding=2)
self.flatten = nn.Flatten()
self.batchnorm1d = nn.BatchNorm1d(105*105)
self.linear1 = nn.Linear(105*105, 300)
self.dropout = nn.Dropout(0.01)
self.relu = nn.ReLU()
self.linear2 = nn.Linear(300, num_classes)
self.softmax = nn.LogSoftmax(dim=1)
def forward(self, image):
output = self.conv2d(image)
#print("convout {}".format(output.size()))
output = self.maxpool2d(output)
#print("poolout {}".format(output.size()))
output = self.flatten(output)
output = self.batchnorm1d(output)
#print("poolout {}".format(output.size()))
output = self.linear1(output)
output = self.dropout(output)
output = self.relu(output)
output = self.linear2(output)
return self.softmax(output)