-
Notifications
You must be signed in to change notification settings - Fork 0
/
clustering_recipe_workflow.qmd
229 lines (185 loc) · 4.49 KB
/
clustering_recipe_workflow.qmd
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
---
title: "clustering"
format: html
editor: source
execute:
echo: false
---
## Environment Preparation
```{r}
#| output: false
library(reticulate)
library(keras)
library(tensorflow)
library(tidyverse)
library(tidymodels)
library(tidyclust)
library(recipes)
library(factoextra)
library(cluster)
```
```{r}
#use_condaenv("condatascience") # On my Windows Machine
myPy <- py_config()$python
myPy
```
```{r}
tf$constant("Hello Tensorflow!")
```
```{r}
get_time <- function(){
myTime <- Sys.time()
myTime <- gsub(":","_",myTime)
myTime <- gsub(" ","_",myTime)
myTime <- gsub("-","_",myTime)
return (myTime)
}
```
## Preparation for Clustering
```{r}
get_features <- function(image_file,myModel){
# takes: image file and model
# returns: feature vector for the provided image
# description:
# the image is loaded with color and scaled to 224x224 px.
# In the next step the image is converted to an array and this array is prepared for prediction on the provided model
img <- image_load(image_file, grayscale=FALSE,target_size = c(224,224))
img_array <- image_to_array(img)
reshaped_image_array <- array_reshape(img_array,c(1,dim(img_array)))
prepro_img <- imagenet_preprocess_input(reshaped_image_array)
features <- myModel |> predict(prepro_img)
return(features)
}
```
```{r}
# Load model
model <- application_vgg16(weights="imagenet",include_top=TRUE)
# Change Output to second last layer to access the feature map instead of classification result.
output <- model$layers[[length(model$layers)-1]]$output
model <- keras_model(inputs=model$input, outputs=output)
```
## Feature Extraction with Recipe
```{r}
# Switch to toggle between a prototype flower-images and the actual images to be clustered
flower_data <- TRUE
if (flower_data){
myPath <- "flowers"
print("Flower Data loaded.")
} else {
myPath <- "weapons"
print("Weapon Data loaded.")
}
# Actual loading of files to a list
myFiles <- list.files(myPath)
```
```{r}
#| output: false
#| eval: false
#| include: false
# empty list to hold extracted features per image
myFeatures <- list()
# fill list with features by looping through file list and calling the function get_features
for (img_file in myFiles){
# create path and call function
path <- paste(myPath,"/",img_file,sep="")
feat <- get_features(path,model)
# append List with features
len <- length(myFeatures)
myFeatures[[len+1]]<-feat
}
#myFeatArray <- array(unlist(myFeatures, recursive = TRUE),dim=c(length(myFeatures),length(myFeatures[[1]])))
myFeatArray <- matrix(unlist(myFeatures), ncol=4096, byrow=TRUE)
```
```{r}
# transform myFiles list to a single column dataframe
file_names <- as.data.frame(myFiles)
# create a dataframe with pcomp per row
# file_features <- myFeatures |> map(as_tibble) |> reduce(bind_rows)
file_features <- as.data.frame(myFeatArray)
# append both dataframes to one (no join due to missing parameter)
df_data <- bind_cols(file_names,file_features)
```
## PCA
```{r}
img_recipe <- recipe(~.,data=myFeatArray) |>
step_pca(all_numeric(),num_comp=100)
```
```{r}
# can be skipped with workflow()
img_prep <- prep(img_recipe)
baked_rec <- bake(img_prep,myFeatArray)
write_csv(baked_rec,paste(get_time(),"_","recipe_pcaResults_",myPath,".csv",sep=""))
```
```{r}
km <- k_means() |>
set_args(num_clusters=tune())
km_wf <- workflow(img_recipe,km)
```
```{r}
boots <- bootstraps(df_data,times=1)
tune_res <- tune_cluster(
km_wf,
resample = boots
)
```
```{r}
collect_metrics(tune_res)
```
```{r}
km <- k_means() |>
set_args(num_clusters=10)
km_wf <- workflow(img_recipe,km)
```
```{r}
km_wf
```
```{r}
km_fit <- fit(km_wf,data=df_data)
```
```{r}
df_clustered <- bind_cols(file_names,extract_cluster_assignment(km_fit))
df_clustered
write_csv(df_clustered,paste("recipe_",myPath,"_",get_time(),"_","cluster.csv",sep=""))
```
## Learning to export the model for deployment
```{r}
saveRDS(km_fit, "weapon_cluster.rds")
```
```{r}
loadModel <- readRDS("weapon_cluster.rds")
```
```{r}
library(butcher)
```
```{r}
weigh(loadModel)
```
```{r}
stripped_model <- butcher(loadModel)
```
```{r}
weigh(stripped_model)
```
```{r}
saveRDS(stripped_model,"test.rds")
```
```{r}
newResults <- predict(stripped_model,new_data=as.data.frame(t(myFeatArray[3,])))
```
```{r}
newResults[[1]]
```
## Display example cluster
```{r}
library(imager)
```
```{r}
myPlotData <- df_clustered |>
filter(.cluster=="Cluster_3") |>
select(myFiles)
myPlotData <- sample(myPlotData$myFiles,3)
for (i in myPlotData){
im <- load.image(paste(myPath,"/",i,sep=""))
plot(im)
}
```