1
$\begingroup$

Does this exist? What algorithm or combinations of algorithms would be able to classify images without supervision?

For example if you have many pictures of cats and dogs, then without being trained to distinguish them first, it would go through the images and realize there are two distinctive groups of images (either cat or dog)

Or if you have many random pictures but a good portion of them have trees, then going through the images, the algorithm realizes there is one consistent pattern within some of the images (trees)

$\endgroup$
1
  • $\begingroup$ Interesting! Would be great if you share some code here in case you come up with some well working solution... $\endgroup$
    – Peter
    Nov 20, 2019 at 9:14

2 Answers 2

3
$\begingroup$

As a starting point, you can think about unsuvervized image classification as a type of image clustering. You can - for instance - use VGG16 weights, extract image pseudo-features, and run some clustering on this feature set.

Here is some "starter code" (Python):

# Imports
from keras.preprocessing import image
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
import numpy as np
from sklearn.cluster import KMeans
import os, shutil, glob, os.path
from PIL import Image as pil_image
image.LOAD_TRUNCATED_IMAGES = True 
model = VGG16(weights='imagenet', include_top=False)

# Variables
imdir = 'C:/indir/' # DIR containing images
targetdir = "C:/outdir/" # DIR to copy clustered images to
number_clusters = 10

# Loop over files and get features
filelist = glob.glob(os.path.join(imdir, '*.jpg'))
filelist.sort()
featurelist = []
for i, imagepath in enumerate(filelist):
    try:
        print("    Status: %s / %s" %(i, len(filelist)), end="\r")
        img = image.load_img(imagepath, target_size=(224, 224))
        img_data = image.img_to_array(img)
        img_data = np.expand_dims(img_data, axis=0)
        img_data = preprocess_input(img_data)
        features = np.array(model.predict(img_data))
        featurelist.append(features.flatten())
    except:
        continue

# Clustering
kmeans = KMeans(n_clusters=number_clusters, random_state=0).fit(np.array(featurelist))

# Copy images renamed by cluster 
# Check if target dir exists
try:
    os.makedirs(targetdir)
except OSError:
    pass
# Copy with cluster name
print("\n")
for i, m in enumerate(kmeans.labels_):
    try:
        print("    Copy: %s / %s" %(i, len(kmeans.labels_)), end="\r")
        shutil.copy(filelist[i], targetdir + str(m) + "_" + str(i) + ".jpg")
    except:
        continue
$\endgroup$
0
$\begingroup$

Check this paper. They achieved seminal scores on popular data sets using 1kkk unlabeled images from Instagram.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.