Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 36 additions & 31 deletions image_clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class image_clustering:

def __init__(self, folder_path="data", n_clusters=10, max_examples=None, use_imagenets=False, use_pca=False):
def __init__(self, folder_path='data/', n_clusters=10, max_examples=None, use_imagenets=False, use_pca=False, shape):
paths = os.listdir(folder_path)
if max_examples == None:
self.max_examples = len(paths)
Expand All @@ -23,47 +23,48 @@ def __init__(self, folder_path="data", n_clusters=10, max_examples=None, use_ima
self.image_paths = paths[:self.max_examples]
self.use_imagenets = use_imagenets
self.use_pca = use_pca
self.shape = shape if shape is not None else (224,224)
del paths
try:
shutil.rmtree("output")
shutil.rmtree('output')
except FileExistsError:
pass
print("\n output folders created.")
os.makedirs("output")
os.makedirs('output')
for i in range(self.n_clusters):
os.makedirs("output\\cluster" + str(i))
print("\n Object of class \"image_clustering\" has been initialized.")
os.makedirs('output/cluster' + str(i))
print('Object of class "image_clustering" has been initialized.')

def load_images(self):
self.images = []
for image in self.image_paths:
self.images.append(cv2.cvtColor(cv2.resize(cv2.imread(self.folder_path + "\\" + image), (224,224)), cv2.COLOR_BGR2RGB))
self.images = np.float32(self.images).reshape(len(self.images), -1)
self.images /= 255
print("\n " + str(self.max_examples) + " images from the \"" + self.folder_path + "\" folder have been loaded in a random order.")
img = cv2.cvtColor(cv2.resize(cv2.imread(self.folder_path + image), self.shape[::-1]), cv2.COLOR_BGR2RGB) / 255
self.images.append(img)
self.images = np.array(self.images)
print(str(self.max_examples) + ' images from "{}" folder have been loaded in a random order.'.format(self.folder_path))

def get_new_imagevectors(self):
if self.use_imagenets == False:
self.images_new = self.images
else:
if use_imagenets.lower() == "vgg16":
model1 = keras.applications.vgg19.vgg19(include_top=False, weights="imagenet", input_shape=(224,224,3))
elif use_imagenets.lower() == "vgg19":
model1 = keras.applications.vgg19.vgg19(include_top=False, weights="imagenet", input_shape=(224,224,3))
elif use_imagenets.lower() == "resnet50":
model1 = keras.applications.resnet50.ResNet50(include_top=False, weights="imagenet", input_shape=(224,224,3))
elif use_imagenets.lower() == "xception":
model1 = keras.applications.xception.Xception(include_top=False, weights='imagenet',input_shape=(224,224,3))
elif use_imagenets.lower() == "inceptionv3":
keras.applications.inception_v3.InceptionV3(include_top=False, weights='imagenet', input_shape=(224,224,3))
elif use_imagenets.lower() == "inceptionresnetv2":
model1 = keras.applications.inception_resnet_v2.InceptionResNetV2(include_top=False, weights='imagenet', input_shape=(224,224,3))
elif use_imagenets.lower() == "densenet":
model1 = keras.applications.densenet.DenseNet201(include_top=False, weights='imagenet', input_shape=(224,224,3))
elif use_imagenets.lower() == "mobilenetv2":
model1 = keras.applications.mobilenetv2.MobileNetV2(input_shape=(224,224,3), alpha=1.0, depth_multiplier=1, include_top=False, weights='imagenet', pooling=None)
imagenet = self.use_imagenets[0].lower()
if imagenet == "vgg16":
model1 = keras.applications.vgg19.vgg19(include_top=False, weights='imagenet', input_shape=(*self.shape,3))
elif imagenet == "vgg19":
model1 = keras.applications.vgg19.vgg19(include_top=False, weights='imagenet', input_shape=(*self.shape,3))
elif imagenet == "resnet50":
model1 = keras.applications.resnet50.ResNet50(include_top=False, weights='imagenet', input_shape=(*self.shape,3))
elif imagenet == "xception":
model1 = keras.applications.xception.Xception(include_top=False, weights='imagenet',input_shape=(*self.shape,3))
elif imagenet == "inceptionv3":
model1 = keras.applications.inception_v3.InceptionV3(include_top=False, weights='imagenet', input_shape=(*self.shape,3))
elif imagenet == "inceptionresnetv2":
model1 = keras.applications.inception_resnet_v2.InceptionResNetV2(include_top=False, weights='imagenet', input_shape=(*self.shape,3))
elif imagenet == "densenet":
model1 = keras.applications.densenet.DenseNet201(include_top=False, weights='imagenet', input_shape=(*self.shape,3))
elif imagenet == "mobilenetv2":
model1 = keras.applications.mobilenetv2.MobileNetV2(input_shape=(*self.shape,3), alpha=1.0, depth_multiplier=1, include_top=False, weights='imagenet', pooling=None)
else:
print("\n\n Please use one of the following keras applications only [ \"vgg16\", \"vgg19\", \"resnet50\", \"xception\", \"inceptionv3\", \"inceptionresnetv2\", \"densenet\", \"mobilenetv2\" ] or False")
print('Please use one of the following keras applications only [ "vgg16", "vgg19", "resnet50", "xception", "inceptionv3", "inceptionresnetv2", "densenet", "mobilenetv2" ] or False')
sys.exit()

pred = model1.predict(self.images)
Expand All @@ -79,10 +80,12 @@ def clustering(self):
model = KMeans(n_clusters=self.n_clusters, n_jobs=-1, random_state=728)
model.fit(self.images_new)
predictions = model.predict(self.images_new)
#print(predictions)
for i in range(self.max_examples):
shutil.copy2(self.folder_path+"\\"+self.image_paths[i], "output\cluster"+str(predictions[i]))
print("\n Clustering complete! \n\n Clusters and the respective images are stored in the \"output\" folder.")
shutil.copy2(self.folder_path + self.image_paths[i], "output/cluster" + str(predictions[i]))

print('Clustering complete!')
print('Clusters and the respective images are stored in the "output" folder.')

if __name__ == "__main__":

Expand All @@ -99,12 +102,14 @@ def clustering(self):
# choose from: "Xception", "VGG16", "VGG19", "ResNet50", "InceptionV3", "InceptionResNetV2", "DenseNet", "MobileNetV2" and "False" -> Default is: False
# you have to use the correct spelling! (case of the letters are irrelevant as the lower() function has been used)

shape = (224,224)

if use_imagenets == False:
use_pca = False
else:
use_pca = False # Make it True if you want to use PCA for dimentionaity reduction -> Default is: False

temp = image_clustering(data_path, number_of_clusters, max_examples, use_imagenets, use_pca)
temp = image_clustering(data_path, number_of_clusters, max_examples, use_imagenets, use_pca, shape=shape)
temp.load_images()
temp.get_new_imagevectors()
temp.clustering()
Expand Down