Skip to content

Imagenette Example

Open In Colab

Install imageatm via PyPi

pip install imageatm

Download the Imagenette dataset (320px) and ImageNet mapping

wget --no-check-certificate \

wget --no-check-certificate \

Untar the dataset

tar -xzf imagenette-320.tgz

Create mapping for Imagenette classes and prepare the data.json

import os
import json

def load_json(file_path):
    with open(file_path, 'r') as f:
        return json.load(f)

mapping = load_json('mapping_imagenet.json')

mapping_synset_txt = {}
for i, j in enumerate(mapping):
  mapping_synset_txt[j['v3p0']] = j['label'].split(',')[0]

classes = os.listdir('imagenette-320/train')
sample_json = []
for c in classes:
  filenames = os.listdir('imagenette-320/train/{}'.format(c))
  for i in filenames:
          'image_id': i,
          'label': mapping_synset_txt[c]

with open('data.json', 'w') as outfile:
    json.dump(sample_json, outfile, indent=4, sort_keys=True)

Prepare our image directory

IMAGE_DIR ='images'

if not os.path.exists(IMAGE_DIR):

classes = os.listdir('imagenette-320/train')
for c in classes:
  cmd = 'cp -r {}. {}'.format(os.path.join('imagenette-320/train', c) + '/', os.path.join(IMAGE_DIR))

Run the data preparation

from imageatm.components import DataPrep

dp = DataPrep(
    image_dir = 'images',
    samples_file = 'data.json',
    job_dir = 'imagenette'

Initialize the Training class and run it

from imageatm.components import Training

trainer = Training(
     dp.image_dir, dp.job_dir, epochs_train_dense=5, epochs_train_all=5, batch_size=64,

Evaluate the best model

from imageatm.components import Evaluation

e = Evaluation(image_dir=dp.image_dir, job_dir=dp.job_dir)

Visualize CAM analysis on the correct and wrong examples

c, w = e.get_correct_wrong_examples(label=1)

e.visualize_images(w, show_heatmap=True)

e.visualize_images(c, show_heatmap=True)