-
Notifications
You must be signed in to change notification settings - Fork 8
/
Image cluster.py
104 lines (54 loc) · 1.59 KB
/
Image cluster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import numpy as np
import torch
import torchvision.models as models
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import os
from DataLoader import ImageDataset
from torch.utils.data import DataLoader
import pickle
# In[ ]:
csv_path = "./DL_info.csv"
Image_slices_dir = "/home/parv/Dropbox/Final_Images_2/"
# In[ ]:
vgg16 = models.vgg16(pretrained=True).features.cuda()
print (vgg16)
# In[ ]:
train_dataset = ImageDataset(root_dir = Image_slices_dir, dataset_type = 1)
#print('Dataset length: ' + str(len(new_dataset)))
dataloader = DataLoader(dataset = train_dataset, batch_size = 5)
print (len(dataloader))
# In[ ]:
curr_features = []
filenames = []
batch_no = 0
for batch in dataloader:
out = list(vgg16(batch['image'].cuda()).view(-1, 512*16*16).cpu().detach().numpy())
# print (out)
curr_features.extend(out)
# print (batch['Filename'])
filenames.extend(batch['Filename'])
batch_no+=1
print (batch_no)
# if batch_no==10:
# break
# In[ ]:
cluster_function = KMeans(n_clusters= 8)
assigned_clusters = cluster_function.fit_predict(curr_features)
# In[ ]:
#pickle.dump(cluster_function,open('./cluster_function.p','wb'))
#pickle.dump(assigned_clusters, open('./assigned_clusters.p','wb'))
pickle.dump(filenames, open('./filenames.p','wb'))
pickle.dump(curr_features, open('./curr_features', 'wb'))
# In[ ]:
assigned_clusters
# In[ ]:
len (filenames)
# In[ ]:
assigned_clusters_2 = pickle.load(open('./assigned_clusters.p','rb'))
# In[ ]:
assigned_clusters_2
# In[ ]: