Skip to content

Commit

Permalink
Added CIFAR10 ontology and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrzej Uszok committed May 29, 2021
1 parent 00d2607 commit d76ab65
Show file tree
Hide file tree
Showing 11 changed files with 551 additions and 127 deletions.
1 change: 1 addition & 0 deletions .settings/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/org.eclipse.core.resources.prefs
125 changes: 125 additions & 0 deletions examples/CIFAR10/CIFAR10.owl
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:mlgraph="http://ontology.ihmc.us/ML/MLGraph.owl#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:emr="http://ontology.ihmc.us/ML/CIFAR10.owl#"
xml:base="http://ontology.ihmc.us/ML/CIFAR10.owl">
<owl:Ontology rdf:about="">
<owl:imports rdf:resource="http://ontology.ihmc.us/ML/PhraseGraph.owl"/>
<owl:imports rdf:resource="http://ontology.ihmc.us/ML/MLGraph.owl"/>
<rdfs:comment rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>This ontology contains CIFAR10 concepts. Created by Andrzej Uszok ([email protected]).</rdfs:comment>
</owl:Ontology>

<owl:Class rdf:ID="image"/>

<owl:Class rdf:ID="animal">
<rdfs:subClassOf rdf:resource="#image"/>

<owl:disjointWith rdf:resource="#vehicle"/>
</owl:Class>

<owl:Class rdf:ID="vehicle">
<rdfs:subClassOf rdf:resource="#image"/>

<owl:disjointWith rdf:resource="#animal"/>
</owl:Class>

<owl:Class rdf:ID="airplane">
<rdfs:subClassOf rdf:resource="#vehicle"/>

<owl:disjointWith rdf:resource="#truck"/>
<owl:disjointWith rdf:resource="#automobile"/>
<owl:disjointWith rdf:resource="#ship"/>
</owl:Class>

<owl:Class rdf:ID="truck">
<rdfs:subClassOf rdf:resource="#vehicle"/>

<owl:disjointWith rdf:resource="#airplane"/>
<owl:disjointWith rdf:resource="#automobile"/>
<owl:disjointWith rdf:resource="#ship"/>
</owl:Class>

<owl:Class rdf:ID="automobile">
<rdfs:subClassOf rdf:resource="#vehicle"/>

<owl:disjointWith rdf:resource="#airplane"/>
<owl:disjointWith rdf:resource="#truck"/>
<owl:disjointWith rdf:resource="#ship"/>
</owl:Class>

<owl:Class rdf:ID="ship">
<rdfs:subClassOf rdf:resource="#vehicle"/>

<owl:disjointWith rdf:resource="#airplane"/>
<owl:disjointWith rdf:resource="#truck"/>
<owl:disjointWith rdf:resource="#automobile"/>
</owl:Class>

<owl:Class rdf:ID="dog">
<rdfs:subClassOf rdf:resource="#animal"/>

<owl:disjointWith rdf:resource="#bird"/>
<owl:disjointWith rdf:resource="#cat"/>
<owl:disjointWith rdf:resource="#deer"/>
<owl:disjointWith rdf:resource="#frog"/>
<owl:disjointWith rdf:resource="#horse"/>
</owl:Class>

<owl:Class rdf:ID="bird">
<rdfs:subClassOf rdf:resource="#animal"/>

<owl:disjointWith rdf:resource="#dog"/>
<owl:disjointWith rdf:resource="#cat"/>
<owl:disjointWith rdf:resource="#deer"/>
<owl:disjointWith rdf:resource="#frog"/>
<owl:disjointWith rdf:resource="#horse"/>
</owl:Class>

<owl:Class rdf:ID="cat">
<rdfs:subClassOf rdf:resource="#animal"/>

<owl:disjointWith rdf:resource="#dog"/>
<owl:disjointWith rdf:resource="#bird"/>
<owl:disjointWith rdf:resource="#deer"/>
<owl:disjointWith rdf:resource="#frog"/>
<owl:disjointWith rdf:resource="#horse"/>
</owl:Class>


<owl:Class rdf:ID="deer">
<rdfs:subClassOf rdf:resource="#animal"/>

<owl:disjointWith rdf:resource="#dog"/>
<owl:disjointWith rdf:resource="#bird"/>
<owl:disjointWith rdf:resource="#cat"/>
<owl:disjointWith rdf:resource="#frog"/>
<owl:disjointWith rdf:resource="#horse"/>
</owl:Class>

<owl:Class rdf:ID="frog">
<rdfs:subClassOf rdf:resource="#animal"/>

<owl:disjointWith rdf:resource="#dog"/>
<owl:disjointWith rdf:resource="#bird"/>
<owl:disjointWith rdf:resource="#cat"/>
<owl:disjointWith rdf:resource="#deer"/>
<owl:disjointWith rdf:resource="#horse"/>
</owl:Class>

<owl:Class rdf:ID="horse">
<rdfs:subClassOf rdf:resource="#animal"/>

<owl:disjointWith rdf:resource="#dog"/>
<owl:disjointWith rdf:resource="#bird"/>
<owl:disjointWith rdf:resource="#cat"/>
<owl:disjointWith rdf:resource="#deer"/>
<owl:disjointWith rdf:resource="#frog"/>
</owl:Class>
</rdf:RDF>

<!-- Created with TopBraid -->
24 changes: 24 additions & 0 deletions examples/CIFAR10/graph_ont.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from regr.graph import Graph, Concept, Relation

Graph.clear()
Concept.clear()
Relation.clear()

with Graph('CIFAR10') as graph_ont:
graph_ont.ontology = ('http://ontology.ihmc.us/ML/CIFAR10.owl', './')

image = Concept(name='image')

animal = image(name='animal')
vehicle = image(name='vehicle')

airplane = image(name='airplane')
dog = image(name='dog')
truck = image(name='truck')
automobile = image(name='automobile')
bird = image(name='bird')
cat = image(name='cat')
deer = image(name='deer')
frog = image(name='frog')
horse = image(name='horse')
ship = image(name='ship')
49 changes: 25 additions & 24 deletions examples/CIFAR10/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def __init__(self, graph):
metric=PRF1Tracker(DatanodeCMMetric()))

from graph import graph, image, truck, dog, airplane, automobile, bird, cat, deer, frog, horse, ship
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

def model_declaration():
from regr.sensor.pytorch.sensors import ReaderSensor
Expand All @@ -75,29 +76,29 @@ def model_declaration():
horse = graph['horse']
ship = graph['ship']

image['pixels'] = ReaderSensor(keyword='pixels', device="cuda:1")
image[airplane] = ReaderSensor(keyword='airplane',label=True, device="cuda:1")
image[dog] = ReaderSensor(keyword='dog',label=True, device="cuda:1")
image[truck] = ReaderSensor(keyword='truck',label=True, device="cuda:1")
image[automobile] = ReaderSensor(keyword='automobile',label=True, device="cuda:1")
image[bird] = ReaderSensor(keyword='bird',label=True, device="cuda:1")
image[cat] = ReaderSensor(keyword='cat',label=True, device="cuda:1")
image[deer] = ReaderSensor(keyword='deer',label=True, device="cuda:1")
image[frog] = ReaderSensor(keyword='frog',label=True, device="cuda:1")
image[horse] = ReaderSensor(keyword='horse',label=True, device="cuda:1")
image[ship] = ReaderSensor(keyword='ship',label=True, device="cuda:1")

image['emb'] = ModuleLearner('pixels', module=ImageNetwork(), device="cuda:1")
image[airplane] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device="cuda:1")
image[dog] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device="cuda:1")
image[truck] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device="cuda:1")
image[automobile] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device="cuda:1")
image[bird] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device="cuda:1")
image[cat] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device="cuda:1")
image[deer] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device="cuda:1")
image[frog] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device="cuda:1")
image[horse] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device="cuda:1")
image[ship] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device="cuda:1")
image['pixels'] = ReaderSensor(keyword='pixels', device=device)
image[airplane] = ReaderSensor(keyword='airplane',label=True, device=device)
image[dog] = ReaderSensor(keyword='dog',label=True, device=device)
image[truck] = ReaderSensor(keyword='truck',label=True, device=device)
image[automobile] = ReaderSensor(keyword='automobile',label=True, device=device)
image[bird] = ReaderSensor(keyword='bird',label=True, device=device)
image[cat] = ReaderSensor(keyword='cat',label=True, device=device)
image[deer] = ReaderSensor(keyword='deer',label=True, device=device)
image[frog] = ReaderSensor(keyword='frog',label=True, device=device)
image[horse] = ReaderSensor(keyword='horse',label=True, device=device)
image[ship] = ReaderSensor(keyword='ship',label=True, device=device)

image['emb'] = ModuleLearner('pixels', module=ImageNetwork(), device=device)
image[airplane] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device=device)
image[dog] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device=device)
image[truck] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device=device)
image[automobile] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device=device)
image[bird] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device=device)
image[cat] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device=device)
image[deer] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device=device)
image[frog] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device=device)
image[horse] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device=device)
image[ship] = ModuleLearner('emb', module=nn.Linear(16 * 5 * 5, 2), device=device)


program = SolverPOIProgram(graph, loss=MacroAverageTracker(NBCrossEntropyLoss()), metric=PRF1Tracker(DatanodeCMMetric()))
Expand Down Expand Up @@ -206,7 +207,7 @@ def main():
trainset = load_cifar10(train=True)
testset = load_cifar10(train=False)

program.train(trainset, train_epoch_num=1, Optim=lambda param: torch.optim.SGD(param, lr=1), device="cuda:1")
program.train(trainset, train_epoch_num=1, Optim=lambda param: torch.optim.SGD(param, lr=1), device=device)
program.test(testset)

# label_list = ['airplane', 'automobile','bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
Expand Down
Loading

0 comments on commit d76ab65

Please sign in to comment.