Image Classification with PyTorch and ApertureDB
The following notebook illustrates image classification on a set of images retrieved using a dynamic query. The images were ingested into ApertureDB using a related tool prepare_aperturedb.py. We use the pre-trained Alexnet model in PyTorch for classification
Prerequisites:
- Access to an ApertureDB instance.
- aperturedb-python installed.
- PyTorch installed
- COCO dataset files downloaded like it's done in PyTorch COCO Data example. We will use the validation set in the following cells.
import time
import AlexNetClassifier as alexnet
import dbinfo
from aperturedb import PyTorchDataset
from IPython.display import display, Image
import cv2
db = dbinfo.create_connector()
out_file_name = "classification.txt"
query = [{
"FindImage": {
"constraints": {
"dataset_name": ["==", "prepare_aperturedb"]
},
"operations": [
{
"type": "resize",
"width": 256,
"height": 256
}
],
"results": {
"list": ["image_id"],
}
}
}]
classifier = alexnet.AlexNetClassifier()
with open(out_file_name, 'w') as classification:
dataset = PyTorchDataset.ApertureDBDataset(db=db, query=query, label_prop='image_id')
start = time.time()
for item in dataset:
image, id = item
label, conf = classifier.classify(image)
classification.write(f"{id}: {label}, confidence = {conf}\n")
converted = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
encoded = cv2.imencode(ext=".jpeg", img=converted)[1]
ipyimage = Image(data=encoded, format="JPEG")
display(ipyimage, f"{id}: {label}")
print("\rRetrieval performance (imgs/s):",
len(dataset) / (time.time() - start), end="")
print(f"\nWritten classification results into {out_file_name}")
'139: restaurant, eating house, eating place, eatery'
'5193: sax, saxophone'
'2261: snorkel'
'2299: pickelhaube'
'2431: restaurant, eating house, eating place, eatery'
'7816: crash helmet'
'5529: ski'
'2473: ski'
'5586: tennis ball'
'2532: ski'
'785: ski'
'7977: unicycle, monocycle'
'872: baseball'
'2685: French horn, horn'
'8021: stage'
'885: racket, racquet'
'6040: streetcar, tram, tramcar, trolley, trolley car'
'8211: motor scooter, scooter'
'1000: steel drum'
'3156: marimba, xylophone'
'1268: liner, ocean liner'
'6460: shopping cart'
'3255: alp'
'8532: bow tie, bow-tie, bowtie'
'1296: cellular telephone, cellular phone, cellphone, cell, mobile phone'
'6471: ballplayer, baseball player'
'1353: barber chair'
'3553: cannon'
'8690: groenendael'
'1490: paddle, boat paddle'
'6763: slot, one-armed bandit'
'8844: pineapple, ananas'
'6771: torch'
'3934: butcher shop, meat market'
'4134: groom, bridegroom'
'9378: stage'
'1584: trolleybus, trolley coach, trackless trolley'
'6894: African elephant, Loxodonta africana'
'4395: bow tie, bow-tie, bowtie'
'9400: hair spray'
'6954: tractor'
'9448: umbrella'
'1761: steel arch bridge'
'7088: umbrella'
'4765: paddle, boat paddle'
'9483: desktop computer'
'9590: restaurant, eating house, eating place, eatery'
'7278: canoe'
'5001: stage'
'9769: snowplow, snowplough'
'139: restaurant, eating house, eating place, eatery'
'5193: sax, saxophone'
'2261: snorkel'
'2299: pickelhaube'
'2431: restaurant, eating house, eating place, eatery'
'7816: crash helmet'
'5529: ski'
'2473: ski'
Retrieval performance (imgs/s): 25.22241178544816
Written classification results into classification.txt